# newgrf_commons.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_commons.cpp 19439 2010-03-16 20:54:26Z 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 "station_map.h"
    00021 #include "tree_map.h"
    00022 #include "core/mem_func.hpp"
    00023
    00029 OverrideManagerBase::OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid)
    00030 {
    00031   max_offset = offset;
    00032   max_new_entities = maximum;
    00033   invalid_ID = invalid;
    00034
    00035   mapping_ID = CallocT<EntityIDMapping>(max_new_entities);
    00036   entity_overrides = MallocT<uint16>(max_offset);
    00037   for (size_t i = 0; i < max_offset; i++) entity_overrides[i] = invalid;
    00038   grfid_overrides = CallocT<uint32>(max_offset);
    00039 }
    00040
    00044 OverrideManagerBase::~OverrideManagerBase()
    00045 {
    00046   free(mapping_ID);
    00047   free(entity_overrides);
    00048   free(grfid_overrides);
    00049 }
    00050
    00058 void OverrideManagerBase::Add(uint8 local_id, uint32 grfid, uint entity_type)
    00059 {
    00060   assert(entity_type < max_offset);
    00061   /* An override can be set only once */
    00062   if (entity_overrides[entity_type] != invalid_ID) return;
    00063   entity_overrides[entity_type] = local_id;
    00064   grfid_overrides[entity_type] = grfid;
    00065 }
    00066
    00068 void OverrideManagerBase::ResetMapping()
    00069 {
    00070   memset(mapping_ID, 0, (max_new_entities - 1) * sizeof(EntityIDMapping));
    00071 }
    00072
    00074 void OverrideManagerBase::ResetOverride()
    00075 {
    00076   for (uint16 i = 0; i < max_offset; i++) {
    00077     entity_overrides[i] = invalid_ID;
    00078     grfid_overrides[i] = 0;
    00079   }
    00080 }
    00081
    00087 uint16 OverrideManagerBase::GetID(uint8 grf_local_id, uint32 grfid)
    00088 {
    00089   const EntityIDMapping *map;
    00090
    00091   for (uint16 id = 0; id < max_new_entities; id++) {
    00092     map = &mapping_ID[id];
    00093     if (map->entity_id == grf_local_id && map->grfid == grfid) {
    00094       return id;
    00095     }
    00096   }
    00097
    00098   return invalid_ID;
    00099 }
    00100
    00107 uint16 OverrideManagerBase::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
    00108 {
    00109   uint16 id = this->GetID(grf_local_id, grfid);
    00110   EntityIDMapping *map;
    00111
    00112   /* Look to see if this entity has already been added. This is done
    00113    * separately from the loop below in case a GRF has been deleted, and there
    00114    * are any gaps in the array.
    00115    */
    00116   if (id != invalid_ID) {
    00117     return id;
    00118   }
    00119
    00120   /* This entity hasn't been defined before, so give it an ID now. */
    00121   for (id = max_offset; id < max_new_entities; id++) {
    00122     map = &mapping_ID[id];
    00123
    00124     if (CheckValidNewID(id) && map->entity_id == 0 && map->grfid == 0) {
    00125       map->entity_id     = grf_local_id;
    00126       map->grfid         = grfid;
    00127       map->substitute_id = substitute_id;
    00128       return id;
    00129     }
    00130   }
    00131
    00132   return invalid_ID;
    00133 }
    00134
    00139 uint16 OverrideManagerBase::GetSubstituteID(uint16 entity_id)
    00140 {
    00141   return mapping_ID[entity_id].substitute_id;
    00142 }
    00143
    00148 void HouseOverrideManager::SetEntitySpec(const HouseSpec *hs)
    00149 {
    00150   HouseID house_id = this->AddEntityID(hs->local_id, hs->grffile->grfid, hs->substitute_id);
    00151
    00152   if (house_id == invalid_ID) {
    00153     grfmsg(1, "House.SetEntitySpec: Too many houses allocated. Ignoring.");
    00154     return;
    00155   }
    00156
    00157   MemCpyT(HouseSpec::Get(house_id), hs);
    00158
    00159   /* Now add the overrides. */
    00160   for (int i = 0; i != max_offset; i++) {
    00161     HouseSpec *overridden_hs = HouseSpec::Get(i);
    00162
    00163     if (entity_overrides[i] != hs->local_id || grfid_overrides[i] != hs->grffile->grfid) continue;
    00164
    00165     overridden_hs->override = house_id;
    00166     entity_overrides[i] = invalid_ID;
    00167     grfid_overrides[i] = 0;
    00168   }
    00169 }
    00170
    00176 uint16 IndustryOverrideManager::GetID(uint8 grf_local_id, uint32 grfid)
    00177 {
    00178   uint16 id = OverrideManagerBase::GetID(grf_local_id, grfid);
    00179   if (id != invalid_ID) return id;
    00180
    00181   /* No mapping found, try the overrides */
    00182   for (id = 0; id < max_offset; id++) {
    00183     if (entity_overrides[id] == grf_local_id && grfid_overrides[id] == grfid) return id;
    00184   }
    00185
    00186   return invalid_ID;
    00187 }
    00188
    00195 uint16 IndustryOverrideManager::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
    00196 {
    00197   /* This entity hasn't been defined before, so give it an ID now. */
    00198   for (uint16 id = 0; id < max_new_entities; id++) {
    00199     /* Skip overriden industries */
    00200     if (id < max_offset && entity_overrides[id] != invalid_ID) continue;
    00201
    00202     /* Get the real live industry */
    00203     const IndustrySpec *inds = GetIndustrySpec(id);
    00204
    00205     /* This industry must be one that is not available(enabled), mostly because of climate.
    00206      * And it must not already be used by a grf (grffile == NULL).
    00207      * So reseve this slot here, as it is the chosen one */
    00208     if (!inds->enabled && inds->grf_prop.grffile == NULL) {
    00209       EntityIDMapping *map = &mapping_ID[id];
    00210
    00211       if (map->entity_id == 0 && map->grfid == 0) {
    00212         /* winning slot, mark it as been used */
    00213         map->entity_id     = grf_local_id;
    00214         map->grfid         = grfid;
    00215         map->substitute_id = substitute_id;
    00216         return id;
    00217       }
    00218     }
    00219   }
    00220
    00221   return invalid_ID;
    00222 }
    00223
    00229 void IndustryOverrideManager::SetEntitySpec(IndustrySpec *inds)
    00230 {
    00231   /* First step : We need to find if this industry is already specified in the savegame data */
    00232   IndustryType ind_id = this->GetID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid);
    00233
    00234   if (ind_id == invalid_ID) {
    00235     /* Not found.
    00236      * Or it has already been overriden, so you've lost your place old boy.
    00237      * Or it is a simple substitute.
    00238      * We need to find a free available slot */
    00239     ind_id = this->AddEntityID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid, inds->grf_prop.subst_id);
    00240     inds->grf_prop.override = invalid_ID;  // make sure it will not be detected as overriden
    00241   }
    00242
    00243   if (ind_id == invalid_ID) {
    00244     grfmsg(1, "Industry.SetEntitySpec: Too many industries allocated. Ignoring.");
    00245     return;
    00246   }
    00247
    00248   /* Now that we know we can use the given id, copy the spech to its final destination*/
    00249   memcpy(&_industry_specs[ind_id], inds, sizeof(*inds));
    00250   /* and mark it as usable*/
    00251   _industry_specs[ind_id].enabled = true;
    00252 }
    00253
    00254 void IndustryTileOverrideManager::SetEntitySpec(const IndustryTileSpec *its)
    00255 {
    00256   IndustryGfx indt_id = this->AddEntityID(its->grf_prop.local_id, its->grf_prop.grffile->grfid, its->grf_prop.subst_id);
    00257
    00258   if (indt_id == invalid_ID) {
    00259     grfmsg(1, "IndustryTile.SetEntitySpec: Too many industry tiles allocated. Ignoring.");
    00260     return;
    00261   }
    00262
    00263   memcpy(&_industry_tile_specs[indt_id], its, sizeof(*its));
    00264
    00265   /* Now add the overrides. */
    00266   for (int i = 0; i < max_offset; i++) {
    00267     IndustryTileSpec *overridden_its = &_industry_tile_specs[i];
    00268
    00269     if (entity_overrides[i] != its->grf_prop.local_id || grfid_overrides[i] != its->grf_prop.grffile->grfid) continue;
    00270
    00271     overridden_its->grf_prop.override = indt_id;
    00272     overridden_its->enabled = false;
    00273     entity_overrides[i] = invalid_ID;
    00274     grfid_overrides[i] = 0;
    00275   }
    00276 }
    00277
    00283 uint32 GetTerrainType(TileIndex tile)
    00284 {
    00285   switch (_settings_game.game_creation.landscape) {
    00286     case LT_TROPIC: return GetTropicZone(tile);
    00287     case LT_ARCTIC: return GetTileZ(tile) > GetSnowLine() ? 4 : 0;
    00288     default:        return 0;
    00289   }
    00290 }
    00291
    00292 TileIndex GetNearbyTile(byte parameter, TileIndex tile)
    00293 {
    00294   int8 x = GB(parameter, 0, 4);
    00295   int8 y = GB(parameter, 4, 4);
    00296
    00297   if (x >= 8) x -= 16;
    00298   if (y >= 8) y -= 16;
    00299
    00300   /* Swap width and height depending on axis for railway stations */
    00301   if (HasStationTileRail(tile) && GetRailStationAxis(tile) == AXIS_Y) Swap(x, y);
    00302
    00303   /* Make sure we never roam outside of the map, better wrap in that case */
    00304   return TILE_MASK(tile + TileDiffXY(x, y));
    00305 }
    00306
    00313 uint32 GetNearbyTileInformation(TileIndex tile)
    00314 {
    00315   TileType tile_type = GetTileType(tile);
    00316
    00317   /* Fake tile type for trees on shore */
    00318   if (IsTileType(tile, MP_TREES) && GetTreeGround(tile) == TREE_GROUND_SHORE) tile_type = MP_WATER;
    00319
    00320   uint z;
    00321   Slope tileh = GetTileSlope(tile, &z);
    00322   byte terrain_type = GetTerrainType(tile) << 2 | (tile_type == MP_WATER ? 1 : 0) << 1;
    00323   return tile_type << 24 | z << 16 | terrain_type << 8 | tileh;
    00324 }