author | truelight |
Fri, 13 Jul 2007 09:18:50 +0000 | |
branch | noai |
changeset 9641 | 855e32c08c9b |
parent 9629 | 66dde6412125 |
child 9703 | d2a6acdbd665 |
permissions | -rw-r--r-- |
9624 | 1 |
/* $Id$ */ |
2 |
||
3 |
/** @file newgrf_commons.cpp Implementation of the class OverrideManagerBase |
|
4 |
* and its descendance, present and futur |
|
5 |
*/ |
|
6 |
||
7 |
#include "stdafx.h" |
|
8 |
#include "openttd.h" |
|
9626 | 9 |
#include "variables.h" |
10 |
#include "landscape.h" |
|
9624 | 11 |
#include "town.h" |
12 |
#include "industry.h" |
|
13 |
#include "newgrf.h" |
|
14 |
#include "newgrf_commons.h" |
|
15 |
||
16 |
/** Constructor of generic class |
|
17 |
* @param offset end of original data for this entity. i.e: houses = 110 |
|
18 |
* @param maximum of entities this manager can deal with. i.e: houses = 512 |
|
19 |
* @param invalid is the ID used to identify an invalid entity id |
|
20 |
*/ |
|
21 |
OverrideManagerBase::OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid) |
|
22 |
{ |
|
23 |
max_offset = offset; |
|
24 |
max_new_entities = maximum; |
|
25 |
invalid_ID = invalid; |
|
26 |
||
27 |
mapping_ID = CallocT<EntityIDMapping>(max_new_entities); |
|
28 |
entity_overrides = MallocT<uint16>(max_offset); |
|
29 |
memset(entity_overrides, invalid, sizeof(entity_overrides)); |
|
30 |
} |
|
31 |
||
32 |
/** Destructor of the generic class. |
|
33 |
* Frees allocated memory of constructor |
|
34 |
*/ |
|
35 |
OverrideManagerBase::~OverrideManagerBase() |
|
36 |
{ |
|
37 |
free(mapping_ID); |
|
38 |
free(entity_overrides); |
|
39 |
} |
|
40 |
||
41 |
/** Since the entity IDs defined by the GRF file does not necessarily correlate |
|
42 |
* to those used by the game, the IDs used for overriding old entities must be |
|
43 |
* translated when the entity spec is set. |
|
44 |
* @param local_id id in grf file |
|
45 |
* @param entity_type original entity type |
|
46 |
*/ |
|
47 |
void OverrideManagerBase::Add(uint8 local_id, uint entity_type) |
|
48 |
{ |
|
49 |
assert(entity_type < max_offset); |
|
50 |
entity_overrides[entity_type] = local_id; |
|
51 |
} |
|
52 |
||
53 |
/** Resets the mapping, which is used while initializing game */ |
|
54 |
void OverrideManagerBase::ResetMapping() |
|
55 |
{ |
|
56 |
memset(mapping_ID, 0, (max_new_entities - 1) * sizeof(EntityIDMapping)); |
|
57 |
} |
|
58 |
||
59 |
/** Resets the override, which is used while initializing game */ |
|
60 |
void OverrideManagerBase::ResetOverride() |
|
61 |
{ |
|
62 |
for (uint16 i = 0; i < max_offset; i++) { |
|
63 |
entity_overrides[i] = invalid_ID; |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
/** Return the ID (if ever available) of a previously inserted entity. |
|
68 |
* @param grf_local_id ID of this enity withing the grfID |
|
69 |
* @param grfid ID of the grf file |
|
70 |
* @return the ID of the candidate, of the Invalid flag item ID |
|
71 |
*/ |
|
72 |
uint16 OverrideManagerBase::GetID(uint8 grf_local_id, uint32 grfid) |
|
73 |
{ |
|
74 |
const EntityIDMapping *map; |
|
75 |
||
9641
855e32c08c9b
(svn r10533) [NoAI] -Sync with trunk r10460:r10532.
truelight
parents:
9629
diff
changeset
|
76 |
for (uint16 id = 0; id < max_new_entities; id++) { |
9624 | 77 |
map = &mapping_ID[id]; |
78 |
if (map->entity_id == grf_local_id && map->grfid == grfid) { |
|
79 |
return id; |
|
80 |
} |
|
81 |
} |
|
82 |
return invalid_ID; |
|
83 |
} |
|
84 |
||
85 |
/** Reserves a place in the mapping array for an entity to be installed |
|
86 |
* @param grf_local_id is an arbitrary id given by the grf's author. Also known as setid |
|
87 |
* @param grfid is the id of the grf file itself |
|
88 |
* @param substitute_id is the original entity from which data is copied for the new one |
|
89 |
* @return the proper usable slot id, or invalid marker if none is found |
|
90 |
*/ |
|
91 |
uint16 OverrideManagerBase::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id) |
|
92 |
{ |
|
93 |
uint16 id = this->GetID(grf_local_id, grfid); |
|
94 |
EntityIDMapping *map; |
|
95 |
||
96 |
/* Look to see if this entity has already been added. This is done |
|
97 |
* separately from the loop below in case a GRF has been deleted, and there |
|
98 |
* are any gaps in the array. |
|
99 |
*/ |
|
100 |
if (id != invalid_ID) { |
|
101 |
return id; |
|
102 |
} |
|
103 |
||
104 |
/* This entity hasn't been defined before, so give it an ID now. */ |
|
105 |
for (id = max_offset; id < max_new_entities; id++) { |
|
106 |
map = &mapping_ID[id]; |
|
107 |
||
108 |
if (map->entity_id == 0 && map->grfid == 0) { |
|
109 |
map->entity_id = grf_local_id; |
|
110 |
map->grfid = grfid; |
|
111 |
map->substitute_id = substitute_id; |
|
112 |
return id; |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
return invalid_ID; |
|
117 |
} |
|
118 |
||
119 |
/** Gives the substitute of the entity, as specified by the grf file |
|
120 |
* @param entity_id of the entity being queried |
|
121 |
* @return mapped id |
|
122 |
*/ |
|
123 |
uint16 OverrideManagerBase::GetSubstituteID(byte entity_id) |
|
124 |
{ |
|
125 |
return mapping_ID[entity_id].substitute_id; |
|
126 |
} |
|
127 |
||
128 |
/** Install the specs into the HouseSpecs array |
|
129 |
* It will find itself the proper slot onwhich it will go |
|
130 |
* @param hs HouseSpec read from the grf file, ready for inclusion |
|
131 |
*/ |
|
132 |
void HouseOverrideManager::SetEntitySpec(const HouseSpec *hs) |
|
133 |
{ |
|
134 |
HouseID house_id = this->AddEntityID(hs->local_id, hs->grffile->grfid, hs->substitute_id); |
|
135 |
||
136 |
if (house_id == invalid_ID) { |
|
137 |
grfmsg(1, "House.SetEntitySpec: Too many houses allocated. Ignoring."); |
|
138 |
return; |
|
139 |
} |
|
140 |
||
141 |
memcpy(&_house_specs[house_id], hs, sizeof(*hs)); |
|
142 |
||
143 |
/* Now add the overrides. */ |
|
144 |
for (int i = 0; i != max_offset; i++) { |
|
145 |
HouseSpec *overridden_hs = GetHouseSpecs(i); |
|
146 |
||
147 |
if (entity_overrides[i] != hs->local_id) continue; |
|
148 |
||
149 |
overridden_hs->override = house_id; |
|
150 |
entity_overrides[i] = invalid_ID; |
|
151 |
} |
|
152 |
} |
|
9626 | 153 |
|
154 |
/** Method to find an entity ID and to mark it as reserved for the Industry to be included. |
|
155 |
* @param grf_local_id ID used by the grf file for pre-installation work (equivalent of TTDPatch's setid |
|
156 |
* @param grfid ID of the current grf file |
|
157 |
* @param substitute_id industry from which data has been copied |
|
158 |
* @return a free entity id (slotid) if ever one has been found, or Invalid_ID marker otherwise |
|
159 |
*/ |
|
160 |
uint16 IndustryOverrideManager::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id) |
|
161 |
{ |
|
162 |
/* This entity hasn't been defined before, so give it an ID now. */ |
|
163 |
for (uint16 id = 0; id < max_new_entities; id++) { |
|
164 |
/* Get the real live industry */ |
|
165 |
const IndustrySpec *inds = GetIndustrySpec(id); |
|
166 |
||
167 |
/* This industry must be one that is not available(enabled), mostly because of climate. |
|
168 |
* And it must not already be used by a grf (grffile == NULL). |
|
169 |
* So reseve this slot here, as it is the chosen one */ |
|
170 |
if (!inds->enabled && inds->grf_prop.grffile == NULL) { |
|
171 |
EntityIDMapping *map = &mapping_ID[id]; |
|
172 |
||
173 |
if (map->entity_id == 0 && map->grfid == 0) { |
|
174 |
/* winning slot, mark it as been used */ |
|
175 |
map->entity_id = grf_local_id; |
|
176 |
map->grfid = grfid; |
|
177 |
map->substitute_id = substitute_id; |
|
178 |
return id; |
|
179 |
} |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
return invalid_ID; |
|
184 |
} |
|
185 |
||
186 |
/** Method to install the new indistry data in its proper slot |
|
187 |
* The slot assigment is internal of this method, since it requires |
|
188 |
* checking what is available |
|
189 |
* @param inds Industryspec that comes from the grf decoding process |
|
190 |
*/ |
|
191 |
void IndustryOverrideManager::SetEntitySpec(const IndustrySpec *inds) |
|
192 |
{ |
|
193 |
/* First step : We need to find if this industry is already specified in the savegame data */ |
|
194 |
IndustryType ind_id = this->GetID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid); |
|
195 |
||
196 |
if (ind_id == invalid_ID) { // not found? So this is the introduction of a new industry |
|
197 |
/* Second step is dealing with the override. */ |
|
198 |
if (inds->grf_prop.override != invalid_ID && _industry_specs[inds->grf_prop.override].grf_prop.override == invalid_ID) { |
|
199 |
/* this is an override, which means it will take the place of the industry it is |
|
200 |
* designed to replace. Before we conclude that the override is allowed, |
|
201 |
* we first need to verify that the slot is not holding another override |
|
202 |
* If it's the case,it will be considered as a normal substitute */ |
|
203 |
ind_id = inds->grf_prop.override; |
|
204 |
} else { |
|
205 |
/* It has already been overriden, so you've lost your place old boy. |
|
206 |
* Or it is a simple substitute. |
|
207 |
* In both case, we need to find a free available slot */ |
|
208 |
ind_id = this->AddEntityID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid, inds->grf_prop.subst_id); |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
if (ind_id == invalid_ID) { |
|
213 |
grfmsg(1, "Industry.SetEntitySpec: Too many industries allocated. Ignoring."); |
|
214 |
return; |
|
215 |
} |
|
216 |
||
217 |
/* Now that we know we can use the given id, copy the spech to its final destination*/ |
|
218 |
memcpy(&_industry_specs[ind_id], inds, sizeof(*inds)); |
|
219 |
/* and mark it as usable*/ |
|
220 |
_industry_specs[ind_id].enabled = true; |
|
221 |
} |
|
222 |
||
9629 | 223 |
void IndustryTileOverrideManager::SetEntitySpec(const IndustryTileSpec *its) |
224 |
{ |
|
225 |
IndustryGfx indt_id = this->AddEntityID(its->grf_prop.local_id, its->grf_prop.grffile->grfid, its->grf_prop.subst_id); |
|
226 |
||
227 |
if (indt_id == invalid_ID) { |
|
228 |
grfmsg(1, "IndustryTile.SetEntitySpec: Too many industry tiles allocated. Ignoring."); |
|
229 |
return; |
|
230 |
} |
|
231 |
||
232 |
memcpy(&_industry_tile_specs[indt_id], its, sizeof(*its)); |
|
233 |
||
234 |
/* Now add the overrides. */ |
|
235 |
for (int i = 0; i < max_offset; i++) { |
|
236 |
IndustryTileSpec *overridden_its = &_industry_tile_specs[i]; |
|
237 |
||
238 |
if (entity_overrides[i] != its->grf_prop.local_id) continue; |
|
239 |
||
240 |
overridden_its->grf_prop.override = indt_id; |
|
241 |
overridden_its->enabled = false; |
|
242 |
entity_overrides[i] = invalid_ID; |
|
243 |
} |
|
244 |
} |
|
245 |
||
9626 | 246 |
/** Function used by houses (and soon industries) to get information |
247 |
* on type of "terrain" the tile it is queries sits on. |
|
248 |
* @param tile TileIndex of the tile been queried |
|
249 |
* @return value corresponding to the grf expected format: |
|
250 |
* Terrain type: 0 normal, 1 desert, 2 rainforest, 4 on or above snowline */ |
|
251 |
uint32 GetTerrainType(TileIndex tile) |
|
252 |
{ |
|
253 |
switch (_opt.landscape) { |
|
254 |
case LT_TROPIC: return GetTropicZone(tile) == TROPICZONE_DESERT ? 1 : 2; |
|
255 |
case LT_ARCTIC: return GetTileZ(tile) >= GetSnowLine() ? 4 : 0; |
|
256 |
default: return 0; |
|
257 |
} |
|
258 |
} |
|
259 |
||
260 |
TileIndex GetNearbyTile(byte parameter, TileIndex tile) |
|
261 |
{ |
|
262 |
int8 x = GB(parameter, 0, 4); |
|
263 |
int8 y = GB(parameter, 4, 4); |
|
264 |
||
265 |
if (x >= 8) x -= 16; |
|
266 |
if (y >= 8) y -= 16; |
|
267 |
||
9641
855e32c08c9b
(svn r10533) [NoAI] -Sync with trunk r10460:r10532.
truelight
parents:
9629
diff
changeset
|
268 |
/* Make sure we never roam outside of the map */ |
855e32c08c9b
(svn r10533) [NoAI] -Sync with trunk r10460:r10532.
truelight
parents:
9629
diff
changeset
|
269 |
return TILE_MASK(tile + TileDiffXY(x, y)); |
9626 | 270 |
} |