newgrf_commons.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 
00015 #include "stdafx.h"
00016 #include "landscape.h"
00017 #include "house.h"
00018 #include "industrytype.h"
00019 #include "newgrf.h"
00020 #include "clear_map.h"
00021 #include "station_map.h"
00022 #include "tree_map.h"
00023 #include "tunnelbridge_map.h"
00024 #include "newgrf_object.h"
00025 #include "genworld.h"
00026 
00033 OverrideManagerBase::OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid)
00034 {
00035   max_offset = offset;
00036   max_new_entities = maximum;
00037   invalid_ID = invalid;
00038 
00039   mapping_ID = CallocT<EntityIDMapping>(max_new_entities);
00040   entity_overrides = MallocT<uint16>(max_offset);
00041   for (size_t i = 0; i < max_offset; i++) entity_overrides[i] = invalid;
00042   grfid_overrides = CallocT<uint32>(max_offset);
00043 }
00044 
00049 OverrideManagerBase::~OverrideManagerBase()
00050 {
00051   free(mapping_ID);
00052   free(entity_overrides);
00053   free(grfid_overrides);
00054 }
00055 
00064 void OverrideManagerBase::Add(uint8 local_id, uint32 grfid, uint entity_type)
00065 {
00066   assert(entity_type < max_offset);
00067   /* An override can be set only once */
00068   if (entity_overrides[entity_type] != invalid_ID) return;
00069   entity_overrides[entity_type] = local_id;
00070   grfid_overrides[entity_type] = grfid;
00071 }
00072 
00074 void OverrideManagerBase::ResetMapping()
00075 {
00076   memset(mapping_ID, 0, (max_new_entities - 1) * sizeof(EntityIDMapping));
00077 }
00078 
00080 void OverrideManagerBase::ResetOverride()
00081 {
00082   for (uint16 i = 0; i < max_offset; i++) {
00083     entity_overrides[i] = invalid_ID;
00084     grfid_overrides[i] = 0;
00085   }
00086 }
00087 
00094 uint16 OverrideManagerBase::GetID(uint8 grf_local_id, uint32 grfid) const
00095 {
00096   const EntityIDMapping *map;
00097 
00098   for (uint16 id = 0; id < max_new_entities; id++) {
00099     map = &mapping_ID[id];
00100     if (map->entity_id == grf_local_id && map->grfid == grfid) {
00101       return id;
00102     }
00103   }
00104 
00105   return invalid_ID;
00106 }
00107 
00115 uint16 OverrideManagerBase::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
00116 {
00117   uint16 id = this->GetID(grf_local_id, grfid);
00118   EntityIDMapping *map;
00119 
00120   /* Look to see if this entity has already been added. This is done
00121    * separately from the loop below in case a GRF has been deleted, and there
00122    * are any gaps in the array.
00123    */
00124   if (id != invalid_ID) {
00125     return id;
00126   }
00127 
00128   /* This entity hasn't been defined before, so give it an ID now. */
00129   for (id = max_offset; id < max_new_entities; id++) {
00130     map = &mapping_ID[id];
00131 
00132     if (CheckValidNewID(id) && map->entity_id == 0 && map->grfid == 0) {
00133       map->entity_id     = grf_local_id;
00134       map->grfid         = grfid;
00135       map->substitute_id = substitute_id;
00136       return id;
00137     }
00138   }
00139 
00140   return invalid_ID;
00141 }
00142 
00148 uint16 OverrideManagerBase::GetSubstituteID(uint16 entity_id) const
00149 {
00150   return mapping_ID[entity_id].substitute_id;
00151 }
00152 
00158 void HouseOverrideManager::SetEntitySpec(const HouseSpec *hs)
00159 {
00160   HouseID house_id = this->AddEntityID(hs->grf_prop.local_id, hs->grf_prop.grffile->grfid, hs->grf_prop.subst_id);
00161 
00162   if (house_id == invalid_ID) {
00163     grfmsg(1, "House.SetEntitySpec: Too many houses allocated. Ignoring.");
00164     return;
00165   }
00166 
00167   MemCpyT(HouseSpec::Get(house_id), hs);
00168 
00169   /* Now add the overrides. */
00170   for (int i = 0; i != max_offset; i++) {
00171     HouseSpec *overridden_hs = HouseSpec::Get(i);
00172 
00173     if (entity_overrides[i] != hs->grf_prop.local_id || grfid_overrides[i] != hs->grf_prop.grffile->grfid) continue;
00174 
00175     overridden_hs->grf_prop.override = house_id;
00176     entity_overrides[i] = invalid_ID;
00177     grfid_overrides[i] = 0;
00178   }
00179 }
00180 
00187 uint16 IndustryOverrideManager::GetID(uint8 grf_local_id, uint32 grfid) const
00188 {
00189   uint16 id = OverrideManagerBase::GetID(grf_local_id, grfid);
00190   if (id != invalid_ID) return id;
00191 
00192   /* No mapping found, try the overrides */
00193   for (id = 0; id < max_offset; id++) {
00194     if (entity_overrides[id] == grf_local_id && grfid_overrides[id] == grfid) return id;
00195   }
00196 
00197   return invalid_ID;
00198 }
00199 
00207 uint16 IndustryOverrideManager::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
00208 {
00209   /* This entity hasn't been defined before, so give it an ID now. */
00210   for (uint16 id = 0; id < max_new_entities; id++) {
00211     /* Skip overriden industries */
00212     if (id < max_offset && entity_overrides[id] != invalid_ID) continue;
00213 
00214     /* Get the real live industry */
00215     const IndustrySpec *inds = GetIndustrySpec(id);
00216 
00217     /* This industry must be one that is not available(enabled), mostly because of climate.
00218      * And it must not already be used by a grf (grffile == NULL).
00219      * So reseve this slot here, as it is the chosen one */
00220     if (!inds->enabled && inds->grf_prop.grffile == NULL) {
00221       EntityIDMapping *map = &mapping_ID[id];
00222 
00223       if (map->entity_id == 0 && map->grfid == 0) {
00224         /* winning slot, mark it as been used */
00225         map->entity_id     = grf_local_id;
00226         map->grfid         = grfid;
00227         map->substitute_id = substitute_id;
00228         return id;
00229       }
00230     }
00231   }
00232 
00233   return invalid_ID;
00234 }
00235 
00242 void IndustryOverrideManager::SetEntitySpec(IndustrySpec *inds)
00243 {
00244   /* First step : We need to find if this industry is already specified in the savegame data. */
00245   IndustryType ind_id = this->GetID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid);
00246 
00247   if (ind_id == invalid_ID) {
00248     /* Not found.
00249      * Or it has already been overriden, so you've lost your place old boy.
00250      * Or it is a simple substitute.
00251      * We need to find a free available slot */
00252     ind_id = this->AddEntityID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid, inds->grf_prop.subst_id);
00253     inds->grf_prop.override = invalid_ID;  // make sure it will not be detected as overriden
00254   }
00255 
00256   if (ind_id == invalid_ID) {
00257     grfmsg(1, "Industry.SetEntitySpec: Too many industries allocated. Ignoring.");
00258     return;
00259   }
00260 
00261   /* Now that we know we can use the given id, copy the spec to its final destination... */
00262   memcpy(&_industry_specs[ind_id], inds, sizeof(*inds));
00263   /* ... and mark it as usable*/
00264   _industry_specs[ind_id].enabled = true;
00265 }
00266 
00267 void IndustryTileOverrideManager::SetEntitySpec(const IndustryTileSpec *its)
00268 {
00269   IndustryGfx indt_id = this->AddEntityID(its->grf_prop.local_id, its->grf_prop.grffile->grfid, its->grf_prop.subst_id);
00270 
00271   if (indt_id == invalid_ID) {
00272     grfmsg(1, "IndustryTile.SetEntitySpec: Too many industry tiles allocated. Ignoring.");
00273     return;
00274   }
00275 
00276   memcpy(&_industry_tile_specs[indt_id], its, sizeof(*its));
00277 
00278   /* Now add the overrides. */
00279   for (int i = 0; i < max_offset; i++) {
00280     IndustryTileSpec *overridden_its = &_industry_tile_specs[i];
00281 
00282     if (entity_overrides[i] != its->grf_prop.local_id || grfid_overrides[i] != its->grf_prop.grffile->grfid) continue;
00283 
00284     overridden_its->grf_prop.override = indt_id;
00285     overridden_its->enabled = false;
00286     entity_overrides[i] = invalid_ID;
00287     grfid_overrides[i] = 0;
00288   }
00289 }
00290 
00297 void ObjectOverrideManager::SetEntitySpec(ObjectSpec *spec)
00298 {
00299   /* First step : We need to find if this object is already specified in the savegame data. */
00300   ObjectType type = this->GetID(spec->grf_prop.local_id, spec->grf_prop.grffile->grfid);
00301 
00302   if (type == invalid_ID) {
00303     /* Not found.
00304      * Or it has already been overriden, so you've lost your place old boy.
00305      * Or it is a simple substitute.
00306      * We need to find a free available slot */
00307     type = this->AddEntityID(spec->grf_prop.local_id, spec->grf_prop.grffile->grfid, OBJECT_TRANSMITTER);
00308   }
00309 
00310   if (type == invalid_ID) {
00311     grfmsg(1, "Object.SetEntitySpec: Too many objects allocated. Ignoring.");
00312     return;
00313   }
00314 
00315   extern ObjectSpec _object_specs[NUM_OBJECTS];
00316 
00317   /* Now that we know we can use the given id, copy the spec to its final destination. */
00318   memcpy(&_object_specs[type], spec, sizeof(*spec));
00319   ObjectClass::Assign(&_object_specs[type]);
00320 }
00321 
00330 uint32 GetTerrainType(TileIndex tile, TileContext context)
00331 {
00332   switch (_settings_game.game_creation.landscape) {
00333     case LT_TROPIC: return GetTropicZone(tile);
00334     case LT_ARCTIC: {
00335       bool has_snow;
00336       switch (GetTileType(tile)) {
00337         case MP_CLEAR:
00338           /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
00339           if (_generating_world) goto genworld;
00340           has_snow = IsSnowTile(tile) && GetClearDensity(tile) >= 2;
00341           break;
00342 
00343         case MP_RAILWAY: {
00344           /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
00345           if (_generating_world) goto genworld; // we do not care about foundations here
00346           RailGroundType ground = GetRailGroundType(tile);
00347           has_snow = (ground == RAIL_GROUND_ICE_DESERT || (context == TCX_UPPER_HALFTILE && ground == RAIL_GROUND_HALF_SNOW));
00348           break;
00349         }
00350 
00351         case MP_ROAD:
00352           /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
00353           if (_generating_world) goto genworld; // we do not care about foundations here
00354           has_snow = IsOnSnow(tile);
00355           break;
00356 
00357         case MP_TREES: {
00358           /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
00359           if (_generating_world) goto genworld;
00360           TreeGround ground = GetTreeGround(tile);
00361           has_snow = (ground == TREE_GROUND_SNOW_DESERT || ground == TREE_GROUND_ROUGH_SNOW) && GetTreeDensity(tile) >= 2;
00362           break;
00363         }
00364 
00365         case MP_TUNNELBRIDGE:
00366           if (context == TCX_ON_BRIDGE) {
00367             has_snow = (GetBridgeHeight(tile) > GetSnowLine());
00368           } else {
00369             /* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
00370             if (_generating_world) goto genworld; // we do not care about foundations here
00371             has_snow = HasTunnelBridgeSnowOrDesert(tile);
00372           }
00373           break;
00374 
00375         case MP_STATION:
00376         case MP_HOUSE:
00377         case MP_INDUSTRY:
00378         case MP_OBJECT:
00379           /* These tiles usually have a levelling foundation. So use max Z */
00380           has_snow = (GetTileMaxZ(tile) > GetSnowLine());
00381           break;
00382 
00383         case MP_VOID:
00384         case MP_WATER:
00385         genworld:
00386           has_snow = (GetTileZ(tile) > GetSnowLine());
00387           break;
00388 
00389         default: NOT_REACHED();
00390       }
00391       return has_snow ? 4 : 0;
00392     }
00393     default:        return 0;
00394   }
00395 }
00396 
00404 TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets)
00405 {
00406   int8 x = GB(parameter, 0, 4);
00407   int8 y = GB(parameter, 4, 4);
00408 
00409   if (signed_offsets && x >= 8) x -= 16;
00410   if (signed_offsets && y >= 8) y -= 16;
00411 
00412   /* Swap width and height depending on axis for railway stations */
00413   if (HasStationTileRail(tile) && GetRailStationAxis(tile) == AXIS_Y) Swap(x, y);
00414 
00415   /* Make sure we never roam outside of the map, better wrap in that case */
00416   return TILE_MASK(tile + TileDiffXY(x, y));
00417 }
00418 
00425 uint32 GetNearbyTileInformation(TileIndex tile)
00426 {
00427   TileType tile_type = GetTileType(tile);
00428 
00429   /* Fake tile type for trees on shore */
00430   if (IsTileType(tile, MP_TREES) && GetTreeGround(tile) == TREE_GROUND_SHORE) tile_type = MP_WATER;
00431 
00432   uint z;
00433   Slope tileh = GetTileSlope(tile, &z);
00434   byte terrain_type = GetTerrainType(tile) << 2 | (tile_type == MP_WATER ? 1 : 0) << 1;
00435   return tile_type << 24 | z << 16 | terrain_type << 8 | tileh;
00436 }

Generated on Mon May 9 05:18:56 2011 for OpenTTD by  doxygen 1.6.1