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