newgrf_commons.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_commons.cpp 20168 2010-07-17 11:45:42Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00014 #include "stdafx.h"
00015 #include "landscape.h"
00016 #include "house.h"
00017 #include "industrytype.h"
00018 #include "newgrf.h"
00019 #include "newgrf_commons.h"
00020 #include "clear_map.h"
00021 #include "station_map.h"
00022 #include "tree_map.h"
00023 #include "tunnelbridge_map.h"
00024 #include "core/mem_func.hpp"
00025 
00031 OverrideManagerBase::OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid)
00032 {
00033   max_offset = offset;
00034   max_new_entities = maximum;
00035   invalid_ID = invalid;
00036 
00037   mapping_ID = CallocT<EntityIDMapping>(max_new_entities);
00038   entity_overrides = MallocT<uint16>(max_offset);
00039   for (size_t i = 0; i < max_offset; i++) entity_overrides[i] = invalid;
00040   grfid_overrides = CallocT<uint32>(max_offset);
00041 }
00042 
00046 OverrideManagerBase::~OverrideManagerBase()
00047 {
00048   free(mapping_ID);
00049   free(entity_overrides);
00050   free(grfid_overrides);
00051 }
00052 
00060 void OverrideManagerBase::Add(uint8 local_id, uint32 grfid, uint entity_type)
00061 {
00062   assert(entity_type < max_offset);
00063   /* An override can be set only once */
00064   if (entity_overrides[entity_type] != invalid_ID) return;
00065   entity_overrides[entity_type] = local_id;
00066   grfid_overrides[entity_type] = grfid;
00067 }
00068 
00070 void OverrideManagerBase::ResetMapping()
00071 {
00072   memset(mapping_ID, 0, (max_new_entities - 1) * sizeof(EntityIDMapping));
00073 }
00074 
00076 void OverrideManagerBase::ResetOverride()
00077 {
00078   for (uint16 i = 0; i < max_offset; i++) {
00079     entity_overrides[i] = invalid_ID;
00080     grfid_overrides[i] = 0;
00081   }
00082 }
00083 
00089 uint16 OverrideManagerBase::GetID(uint8 grf_local_id, uint32 grfid)
00090 {
00091   const EntityIDMapping *map;
00092 
00093   for (uint16 id = 0; id < max_new_entities; id++) {
00094     map = &mapping_ID[id];
00095     if (map->entity_id == grf_local_id && map->grfid == grfid) {
00096       return id;
00097     }
00098   }
00099 
00100   return invalid_ID;
00101 }
00102 
00109 uint16 OverrideManagerBase::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
00110 {
00111   uint16 id = this->GetID(grf_local_id, grfid);
00112   EntityIDMapping *map;
00113 
00114   /* Look to see if this entity has already been added. This is done
00115    * separately from the loop below in case a GRF has been deleted, and there
00116    * are any gaps in the array.
00117    */
00118   if (id != invalid_ID) {
00119     return id;
00120   }
00121 
00122   /* This entity hasn't been defined before, so give it an ID now. */
00123   for (id = max_offset; id < max_new_entities; id++) {
00124     map = &mapping_ID[id];
00125 
00126     if (CheckValidNewID(id) && map->entity_id == 0 && map->grfid == 0) {
00127       map->entity_id     = grf_local_id;
00128       map->grfid         = grfid;
00129       map->substitute_id = substitute_id;
00130       return id;
00131     }
00132   }
00133 
00134   return invalid_ID;
00135 }
00136 
00141 uint16 OverrideManagerBase::GetSubstituteID(uint16 entity_id)
00142 {
00143   return mapping_ID[entity_id].substitute_id;
00144 }
00145 
00150 void HouseOverrideManager::SetEntitySpec(const HouseSpec *hs)
00151 {
00152   HouseID house_id = this->AddEntityID(hs->local_id, hs->grffile->grfid, hs->substitute_id);
00153 
00154   if (house_id == invalid_ID) {
00155     grfmsg(1, "House.SetEntitySpec: Too many houses allocated. Ignoring.");
00156     return;
00157   }
00158 
00159   MemCpyT(HouseSpec::Get(house_id), hs);
00160 
00161   /* Now add the overrides. */
00162   for (int i = 0; i != max_offset; i++) {
00163     HouseSpec *overridden_hs = HouseSpec::Get(i);
00164 
00165     if (entity_overrides[i] != hs->local_id || grfid_overrides[i] != hs->grffile->grfid) continue;
00166 
00167     overridden_hs->override = house_id;
00168     entity_overrides[i] = invalid_ID;
00169     grfid_overrides[i] = 0;
00170   }
00171 }
00172 
00178 uint16 IndustryOverrideManager::GetID(uint8 grf_local_id, uint32 grfid)
00179 {
00180   uint16 id = OverrideManagerBase::GetID(grf_local_id, grfid);
00181   if (id != invalid_ID) return id;
00182 
00183   /* No mapping found, try the overrides */
00184   for (id = 0; id < max_offset; id++) {
00185     if (entity_overrides[id] == grf_local_id && grfid_overrides[id] == grfid) return id;
00186   }
00187 
00188   return invalid_ID;
00189 }
00190 
00197 uint16 IndustryOverrideManager::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
00198 {
00199   /* This entity hasn't been defined before, so give it an ID now. */
00200   for (uint16 id = 0; id < max_new_entities; id++) {
00201     /* Skip overriden industries */
00202     if (id < max_offset && entity_overrides[id] != invalid_ID) continue;
00203 
00204     /* Get the real live industry */
00205     const IndustrySpec *inds = GetIndustrySpec(id);
00206 
00207     /* This industry must be one that is not available(enabled), mostly because of climate.
00208      * And it must not already be used by a grf (grffile == NULL).
00209      * So reseve this slot here, as it is the chosen one */
00210     if (!inds->enabled && inds->grf_prop.grffile == NULL) {
00211       EntityIDMapping *map = &mapping_ID[id];
00212 
00213       if (map->entity_id == 0 && map->grfid == 0) {
00214         /* winning slot, mark it as been used */
00215         map->entity_id     = grf_local_id;
00216         map->grfid         = grfid;
00217         map->substitute_id = substitute_id;
00218         return id;
00219       }
00220     }
00221   }
00222 
00223   return invalid_ID;
00224 }
00225 
00231 void IndustryOverrideManager::SetEntitySpec(IndustrySpec *inds)
00232 {
00233   /* First step : We need to find if this industry is already specified in the savegame data */
00234   IndustryType ind_id = this->GetID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid);
00235 
00236   if (ind_id == invalid_ID) {
00237     /* Not found.
00238      * Or it has already been overriden, so you've lost your place old boy.
00239      * Or it is a simple substitute.
00240      * We need to find a free available slot */
00241     ind_id = this->AddEntityID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid, inds->grf_prop.subst_id);
00242     inds->grf_prop.override = invalid_ID;  // make sure it will not be detected as overriden
00243   }
00244 
00245   if (ind_id == invalid_ID) {
00246     grfmsg(1, "Industry.SetEntitySpec: Too many industries allocated. Ignoring.");
00247     return;
00248   }
00249 
00250   /* Now that we know we can use the given id, copy the spech to its final destination*/
00251   memcpy(&_industry_specs[ind_id], inds, sizeof(*inds));
00252   /* and mark it as usable*/
00253   _industry_specs[ind_id].enabled = true;
00254 }
00255 
00256 void IndustryTileOverrideManager::SetEntitySpec(const IndustryTileSpec *its)
00257 {
00258   IndustryGfx indt_id = this->AddEntityID(its->grf_prop.local_id, its->grf_prop.grffile->grfid, its->grf_prop.subst_id);
00259 
00260   if (indt_id == invalid_ID) {
00261     grfmsg(1, "IndustryTile.SetEntitySpec: Too many industry tiles allocated. Ignoring.");
00262     return;
00263   }
00264 
00265   memcpy(&_industry_tile_specs[indt_id], its, sizeof(*its));
00266 
00267   /* Now add the overrides. */
00268   for (int i = 0; i < max_offset; i++) {
00269     IndustryTileSpec *overridden_its = &_industry_tile_specs[i];
00270 
00271     if (entity_overrides[i] != its->grf_prop.local_id || grfid_overrides[i] != its->grf_prop.grffile->grfid) continue;
00272 
00273     overridden_its->grf_prop.override = indt_id;
00274     overridden_its->enabled = false;
00275     entity_overrides[i] = invalid_ID;
00276     grfid_overrides[i] = 0;
00277   }
00278 }
00279 
00286 uint32 GetTerrainType(TileIndex tile, bool upper_halftile)
00287 {
00288   switch (_settings_game.game_creation.landscape) {
00289     case LT_TROPIC: return GetTropicZone(tile);
00290     case LT_ARCTIC: {
00291       bool has_snow;
00292       switch (GetTileType(tile)) {
00293         case MP_CLEAR:
00294           has_snow = IsSnowTile(tile) && GetClearDensity(tile) >= 2;
00295           break;
00296 
00297         case MP_RAILWAY: {
00298           RailGroundType ground = GetRailGroundType(tile);
00299           has_snow = (ground == RAIL_GROUND_ICE_DESERT || (upper_halftile && ground == RAIL_GROUND_HALF_SNOW));
00300           break;
00301         }
00302 
00303         case MP_ROAD:
00304           has_snow = IsOnSnow(tile);
00305           break;
00306 
00307         case MP_TREES: {
00308           TreeGround ground = GetTreeGround(tile);
00309           has_snow = (ground == TREE_GROUND_SNOW_DESERT || ground == TREE_GROUND_ROUGH_SNOW) && GetTreeDensity(tile) >= 2;
00310           break;
00311         }
00312 
00313         case MP_TUNNELBRIDGE:
00314           has_snow = HasTunnelBridgeSnowOrDesert(tile);
00315           break;
00316 
00317         case MP_STATION:
00318         case MP_HOUSE:
00319         case MP_INDUSTRY:
00320         case MP_UNMOVABLE:
00321           /* These tiles usually have a levelling foundation. So use max Z */
00322           has_snow = (GetTileMaxZ(tile) > GetSnowLine());
00323           break;
00324 
00325         case MP_WATER:
00326           has_snow = (GetTileZ(tile) > GetSnowLine());
00327           break;
00328 
00329         default: NOT_REACHED();
00330       }
00331       return has_snow ? 4 : 0;
00332     }
00333     default:        return 0;
00334   }
00335 }
00336 
00337 TileIndex GetNearbyTile(byte parameter, TileIndex tile)
00338 {
00339   int8 x = GB(parameter, 0, 4);
00340   int8 y = GB(parameter, 4, 4);
00341 
00342   if (x >= 8) x -= 16;
00343   if (y >= 8) y -= 16;
00344 
00345   /* Swap width and height depending on axis for railway stations */
00346   if (HasStationTileRail(tile) && GetRailStationAxis(tile) == AXIS_Y) Swap(x, y);
00347 
00348   /* Make sure we never roam outside of the map, better wrap in that case */
00349   return TILE_MASK(tile + TileDiffXY(x, y));
00350 }
00351 
00358 uint32 GetNearbyTileInformation(TileIndex tile)
00359 {
00360   TileType tile_type = GetTileType(tile);
00361 
00362   /* Fake tile type for trees on shore */
00363   if (IsTileType(tile, MP_TREES) && GetTreeGround(tile) == TREE_GROUND_SHORE) tile_type = MP_WATER;
00364 
00365   uint z;
00366   Slope tileh = GetTileSlope(tile, &z);
00367   byte terrain_type = GetTerrainType(tile) << 2 | (tile_type == MP_WATER ? 1 : 0) << 1;
00368   return tile_type << 24 | z << 16 | terrain_type << 8 | tileh;
00369 }

Generated on Sat Jul 31 21:37:49 2010 for OpenTTD by  doxygen 1.6.1