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-- |
9476 | 1 |
/* $Id$ */ |
2 |
||
3 |
/** @file newgrf_house.cpp */ |
|
4 |
||
5 |
#include "stdafx.h" |
|
6 |
#include "openttd.h" |
|
7 |
#include "variables.h" |
|
8 |
#include "debug.h" |
|
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
9 |
#include "viewport_func.h" |
9505 | 10 |
#include "landscape.h" |
9476 | 11 |
#include "town.h" |
12 |
#include "town_map.h" |
|
13 |
#include "sprite.h" |
|
14 |
#include "table/strings.h" |
|
15 |
#include "table/sprites.h" |
|
16 |
#include "table/town_land.h" |
|
17 |
#include "newgrf.h" |
|
18 |
#include "newgrf_house.h" |
|
19 |
#include "newgrf_spritegroup.h" |
|
20 |
#include "newgrf_callbacks.h" |
|
21 |
#include "newgrf_town.h" |
|
22 |
#include "newgrf_sound.h" |
|
9624 | 23 |
#include "newgrf_commons.h" |
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
24 |
#include "transparency.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
25 |
#include "functions.h" |
9476 | 26 |
|
27 |
static BuildingCounts _building_counts; |
|
28 |
static HouseClassMapping _class_mapping[HOUSE_CLASS_MAX]; |
|
29 |
||
9624 | 30 |
HouseOverrideManager _house_mngr(NEW_HOUSE_OFFSET, HOUSE_MAX, INVALID_HOUSE_ID); |
9476 | 31 |
|
32 |
void CheckHouseIDs() |
|
33 |
{ |
|
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
34 |
InitializeBuildingCounts(); |
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
35 |
|
9476 | 36 |
for (TileIndex t = 0; t < MapSize(); t++) { |
37 |
HouseID house_id; |
|
38 |
||
39 |
if (!IsTileType(t, MP_HOUSE)) continue; |
|
40 |
||
41 |
house_id = GetHouseType(t); |
|
42 |
if (!GetHouseSpecs(house_id)->enabled && house_id >= NEW_HOUSE_OFFSET) { |
|
43 |
/* The specs for this type of house are not available any more, so |
|
44 |
* replace it with the substitute original house type. */ |
|
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
45 |
house_id = _house_mngr.GetSubstituteID(house_id); |
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
46 |
SetHouseType(t, house_id); |
9476 | 47 |
} |
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
48 |
IncreaseBuildingCount(GetTownByTile(t), house_id); |
9476 | 49 |
} |
50 |
} |
|
51 |
||
52 |
HouseClassID AllocateHouseClassID(byte grf_class_id, uint32 grfid) |
|
53 |
{ |
|
54 |
/* Start from 1 because 0 means that no class has been assigned. */ |
|
55 |
for (int i = 1; i != lengthof(_class_mapping); i++) { |
|
56 |
HouseClassMapping *map = &_class_mapping[i]; |
|
57 |
||
58 |
if (map->class_id == grf_class_id && map->grfid == grfid) return (HouseClassID)i; |
|
59 |
||
60 |
if (map->class_id == 0 && map->grfid == 0) { |
|
61 |
map->class_id = grf_class_id; |
|
62 |
map->grfid = grfid; |
|
63 |
return (HouseClassID)i; |
|
64 |
} |
|
65 |
} |
|
66 |
return HOUSE_NO_CLASS; |
|
67 |
} |
|
68 |
||
69 |
void InitializeBuildingCounts() |
|
70 |
{ |
|
71 |
memset(&_building_counts, 0, sizeof(_building_counts)); |
|
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* IncreaseBuildingCount() |
|
76 |
* Increase the count of a building when it has been added by a town. |
|
77 |
* @param t The town that the building is being built in |
|
78 |
* @param house_id The id of the house being added |
|
79 |
*/ |
|
80 |
void IncreaseBuildingCount(Town *t, HouseID house_id) |
|
81 |
{ |
|
82 |
HouseClassID class_id = GetHouseSpecs(house_id)->class_id; |
|
83 |
||
9628
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9626
diff
changeset
|
84 |
if (!_loaded_newgrf_features.has_newhouses) return; |
9476 | 85 |
|
86 |
/* If there are 255 buildings of this type in this town, there are also |
|
87 |
* at least that many houses of the same class in the town, and |
|
88 |
* therefore on the map as well. */ |
|
89 |
if (t->building_counts.id_count[house_id] == 255) return; |
|
90 |
||
91 |
t->building_counts.id_count[house_id]++; |
|
92 |
if (_building_counts.id_count[house_id] < 255) _building_counts.id_count[house_id]++; |
|
93 |
||
94 |
/* Similarly, if there are 255 houses of this class in this town, there |
|
95 |
* must be at least that number on the map too. */ |
|
96 |
if (class_id == HOUSE_NO_CLASS || t->building_counts.class_count[class_id] == 255) return; |
|
97 |
||
98 |
t->building_counts.class_count[class_id]++; |
|
99 |
if (_building_counts.class_count[class_id] < 255) _building_counts.class_count[class_id]++; |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* DecreaseBuildingCount() |
|
104 |
* Decrease the number of a building when it is deleted. |
|
105 |
* @param t The town that the building was built in |
|
106 |
* @param house_id The id of the house being removed |
|
107 |
*/ |
|
108 |
void DecreaseBuildingCount(Town *t, HouseID house_id) |
|
109 |
{ |
|
110 |
HouseClassID class_id = GetHouseSpecs(house_id)->class_id; |
|
111 |
||
9628
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9626
diff
changeset
|
112 |
if (!_loaded_newgrf_features.has_newhouses) return; |
9476 | 113 |
|
114 |
if (t->building_counts.id_count[house_id] > 0) t->building_counts.id_count[house_id]--; |
|
115 |
if (_building_counts.id_count[house_id] > 0) _building_counts.id_count[house_id]--; |
|
116 |
||
117 |
if (class_id == HOUSE_NO_CLASS) return; |
|
118 |
||
119 |
if (t->building_counts.class_count[class_id] > 0) t->building_counts.class_count[class_id]--; |
|
120 |
if (_building_counts.class_count[class_id] > 0) _building_counts.class_count[class_id]--; |
|
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* AfterLoadCountBuildings() |
|
125 |
* |
|
126 |
* After a savegame has been loaded, count the number of buildings on the map. |
|
127 |
*/ |
|
128 |
void AfterLoadCountBuildings() |
|
129 |
{ |
|
9628
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9626
diff
changeset
|
130 |
if (!_loaded_newgrf_features.has_newhouses) return; |
9476 | 131 |
|
132 |
for (TileIndex t = 0; t < MapSize(); t++) { |
|
133 |
if (!IsTileType(t, MP_HOUSE)) continue; |
|
134 |
IncreaseBuildingCount(GetTownByTile(t), GetHouseType(t)); |
|
135 |
} |
|
136 |
} |
|
137 |
||
138 |
||
139 |
static uint32 HouseGetRandomBits(const ResolverObject *object) |
|
140 |
{ |
|
141 |
const TileIndex tile = object->u.house.tile; |
|
142 |
return (tile == INVALID_TILE || !IsTileType(tile, MP_HOUSE)) ? 0 : GetHouseRandomBits(tile); |
|
143 |
} |
|
144 |
||
145 |
static uint32 HouseGetTriggers(const ResolverObject *object) |
|
146 |
{ |
|
147 |
const TileIndex tile = object->u.house.tile; |
|
148 |
return (tile == INVALID_TILE || !IsTileType(tile, MP_HOUSE)) ? 0 : GetHouseTriggers(tile); |
|
149 |
} |
|
150 |
||
151 |
static void HouseSetTriggers(const ResolverObject *object, int triggers) |
|
152 |
{ |
|
153 |
const TileIndex tile = object->u.house.tile; |
|
154 |
if (IsTileType(tile, MP_HOUSE)) SetHouseTriggers(tile, triggers); |
|
155 |
} |
|
156 |
||
157 |
static uint32 GetNumHouses(HouseID house_id, const Town *town) |
|
158 |
{ |
|
159 |
uint8 map_id_count, town_id_count, map_class_count, town_class_count; |
|
160 |
HouseClassID class_id = GetHouseSpecs(house_id)->class_id; |
|
161 |
||
162 |
map_id_count = _building_counts.id_count[house_id]; |
|
163 |
map_class_count = _building_counts.class_count[class_id]; |
|
164 |
town_id_count = town->building_counts.id_count[house_id]; |
|
165 |
town_class_count = town->building_counts.class_count[class_id]; |
|
166 |
||
167 |
return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count; |
|
168 |
} |
|
169 |
||
170 |
static uint32 GetGRFParameter(HouseID house_id, byte parameter) |
|
171 |
{ |
|
172 |
const HouseSpec *hs = GetHouseSpecs(house_id); |
|
173 |
const GRFFile *file = hs->grffile; |
|
174 |
||
175 |
if (parameter >= file->param_end) return 0; |
|
176 |
return file->param[parameter]; |
|
177 |
} |
|
178 |
||
9626 | 179 |
uint32 GetNearbyTileInformation(byte parameter, TileIndex tile) |
180 |
{ |
|
181 |
uint32 tile_type; |
|
182 |
||
183 |
tile = GetNearbyTile(parameter, tile); |
|
184 |
tile_type = GetTerrainType(tile) << 2 | (IsTileType(tile, MP_WATER) ? 1 : 0) << 1; |
|
185 |
||
9703
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9701
diff
changeset
|
186 |
uint z; |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9701
diff
changeset
|
187 |
Slope tileh = GetTileSlope(tile, &z); |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9701
diff
changeset
|
188 |
return GetTileType(tile) << 24 | z << 16 | tile_type << 8 | tileh; |
9626 | 189 |
} |
190 |
||
9476 | 191 |
/** |
192 |
* HouseGetVariable(): |
|
193 |
* |
|
194 |
* Used by the resolver to get values for feature 07 deterministic spritegroups. |
|
195 |
*/ |
|
196 |
static uint32 HouseGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available) |
|
197 |
{ |
|
198 |
const Town *town = object->u.house.town; |
|
199 |
TileIndex tile = object->u.house.tile; |
|
200 |
HouseID house_id = object->u.house.house_id; |
|
201 |
||
202 |
if (object->scope == VSG_SCOPE_PARENT) { |
|
203 |
return TownGetVariable(variable, parameter, available, town); |
|
204 |
} |
|
205 |
||
206 |
switch (variable) { |
|
207 |
/* Construction stage. */ |
|
208 |
case 0x40: return (IsTileType(tile, MP_HOUSE) ? GetHouseBuildingStage(tile) : 0) | OriginalTileRandomiser(TileX(tile), TileY(tile)) << 2; |
|
209 |
||
210 |
/* Building age. */ |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
211 |
case 0x41: return Clamp(_cur_year - GetHouseConstructionYear(tile), 0, 0xFF); |
9476 | 212 |
|
213 |
/* Town zone */ |
|
214 |
case 0x42: return GetTownRadiusGroup(town, tile); |
|
215 |
||
216 |
/* Terrain type */ |
|
217 |
case 0x43: return GetTerrainType(tile); |
|
218 |
||
219 |
/* Number of this type of building on the map. */ |
|
220 |
case 0x44: return GetNumHouses(house_id, town); |
|
221 |
||
222 |
/* Whether the town is being created or just expanded. */ |
|
223 |
case 0x45: return _generating_world ? 1 : 0; |
|
224 |
||
225 |
/* Current animation frame. */ |
|
226 |
case 0x46: return IsTileType(tile, MP_HOUSE) ? GetHouseAnimationFrame(tile) : 0; |
|
227 |
||
228 |
||
229 |
/* Building counts for old houses with id = parameter. */ |
|
230 |
case 0x60: return GetNumHouses(parameter, town); |
|
231 |
||
232 |
/* Building counts for new houses with id = parameter. */ |
|
233 |
case 0x61: { |
|
234 |
const HouseSpec *hs = GetHouseSpecs(house_id); |
|
235 |
if (hs->grffile == NULL) return 0; |
|
236 |
||
9624 | 237 |
HouseID new_house = _house_mngr.GetID(parameter, hs->grffile->grfid); |
9476 | 238 |
return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, town); |
239 |
} |
|
240 |
||
241 |
/* Land info for nearby tiles. */ |
|
9626 | 242 |
case 0x62: return GetNearbyTileInformation(parameter, tile); |
9476 | 243 |
|
244 |
/* Read GRF parameter */ |
|
245 |
case 0x7F: return GetGRFParameter(object->u.house.house_id, parameter); |
|
246 |
} |
|
247 |
||
248 |
DEBUG(grf, 1, "Unhandled house property 0x%X", variable); |
|
249 |
||
250 |
*available = false; |
|
251 |
return UINT_MAX; |
|
252 |
} |
|
253 |
||
254 |
static const SpriteGroup *HouseResolveReal(const ResolverObject *object, const SpriteGroup *group) |
|
255 |
{ |
|
256 |
/* Houses do not have 'real' groups */ |
|
257 |
return NULL; |
|
258 |
} |
|
259 |
||
260 |
/** |
|
261 |
* NewHouseResolver(): |
|
262 |
* |
|
263 |
* Returns a resolver object to be used with feature 07 spritegroups. |
|
264 |
*/ |
|
265 |
static void NewHouseResolver(ResolverObject *res, HouseID house_id, TileIndex tile, Town *town) |
|
266 |
{ |
|
267 |
res->GetRandomBits = HouseGetRandomBits; |
|
268 |
res->GetTriggers = HouseGetTriggers; |
|
269 |
res->SetTriggers = HouseSetTriggers; |
|
270 |
res->GetVariable = HouseGetVariable; |
|
271 |
res->ResolveReal = HouseResolveReal; |
|
272 |
||
273 |
res->u.house.tile = tile; |
|
274 |
res->u.house.town = town; |
|
275 |
res->u.house.house_id = house_id; |
|
276 |
||
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9628
diff
changeset
|
277 |
res->callback = CBID_NO_CALLBACK; |
9476 | 278 |
res->callback_param1 = 0; |
279 |
res->callback_param2 = 0; |
|
280 |
res->last_value = 0; |
|
281 |
res->trigger = 0; |
|
282 |
res->reseed = 0; |
|
283 |
} |
|
284 |
||
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9628
diff
changeset
|
285 |
uint16 GetHouseCallback(CallbackID callback, uint32 param1, uint32 param2, HouseID house_id, Town *town, TileIndex tile) |
9476 | 286 |
{ |
287 |
ResolverObject object; |
|
288 |
const SpriteGroup *group; |
|
289 |
||
290 |
NewHouseResolver(&object, house_id, tile, town); |
|
291 |
object.callback = callback; |
|
292 |
object.callback_param1 = param1; |
|
9624 | 293 |
object.callback_param2 = param2; |
9476 | 294 |
|
295 |
group = Resolve(GetHouseSpecs(house_id)->spritegroup, &object); |
|
296 |
if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED; |
|
297 |
||
298 |
return group->g.callback.result; |
|
299 |
} |
|
300 |
||
301 |
void DrawTileLayout(const TileInfo *ti, const SpriteGroup *group, byte stage, HouseID house_id) |
|
302 |
{ |
|
303 |
const DrawTileSprites *dts = group->g.layout.dts; |
|
304 |
const DrawTileSeqStruct *dtss; |
|
305 |
||
306 |
SpriteID image = dts->ground_sprite; |
|
307 |
SpriteID pal = dts->ground_pal; |
|
308 |
||
9704 | 309 |
if (IS_CUSTOM_SPRITE(image)) image += stage; |
310 |
||
9476 | 311 |
if (GB(image, 0, SPRITE_WIDTH) != 0) DrawGroundSprite(image, pal); |
312 |
||
313 |
foreach_draw_tile_seq(dtss, dts->seq) { |
|
314 |
if (GB(dtss->image, 0, SPRITE_WIDTH) == 0) continue; |
|
315 |
||
9704 | 316 |
image = dtss->image; |
9476 | 317 |
pal = dtss->pal; |
318 |
||
9704 | 319 |
if (IS_CUSTOM_SPRITE(image)) image += stage; |
320 |
||
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
321 |
if ((HasBit(image, SPRITE_MODIFIER_OPAQUE) || !IsTransparencySet(TO_HOUSES)) && HasBit(image, PALETTE_MODIFIER_COLOR)) { |
9476 | 322 |
if (pal == 0) { |
323 |
const HouseSpec *hs = GetHouseSpecs(house_id); |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
324 |
if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) { |
9704 | 325 |
uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, GetTownByTile(ti->tile), ti->tile); |
9476 | 326 |
if (callback != CALLBACK_FAILED) { |
327 |
/* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */ |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
328 |
pal = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback; |
9476 | 329 |
} |
330 |
} else { |
|
331 |
pal = hs->random_colour[OriginalTileRandomiser(ti->x, ti->y)] + PALETTE_RECOLOR_START; |
|
332 |
} |
|
333 |
} |
|
334 |
} else { |
|
335 |
pal = PAL_NONE; |
|
336 |
} |
|
337 |
||
338 |
if ((byte)dtss->delta_z != 0x80) { |
|
339 |
AddSortableSpriteToDraw( |
|
340 |
image, pal, |
|
341 |
ti->x + dtss->delta_x, ti->y + dtss->delta_y, |
|
342 |
dtss->size_x, dtss->size_y, |
|
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9628
diff
changeset
|
343 |
dtss->size_z, ti->z + dtss->delta_z, |
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
344 |
IsTransparencySet(TO_HOUSES) |
9476 | 345 |
); |
346 |
} else { |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
347 |
AddChildSpriteScreen(image, pal, (byte)dtss->delta_x, (byte)dtss->delta_y, IsTransparencySet(TO_HOUSES)); |
9476 | 348 |
} |
349 |
} |
|
350 |
} |
|
351 |
||
352 |
void DrawNewHouseTile(TileInfo *ti, HouseID house_id) |
|
353 |
{ |
|
354 |
const HouseSpec *hs = GetHouseSpecs(house_id); |
|
355 |
const SpriteGroup *group; |
|
356 |
ResolverObject object; |
|
357 |
||
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9628
diff
changeset
|
358 |
if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED); |
9476 | 359 |
|
360 |
NewHouseResolver(&object, house_id, ti->tile, GetTownByTile(ti->tile)); |
|
361 |
||
362 |
group = Resolve(hs->spritegroup, &object); |
|
363 |
if (group == NULL || group->type != SGT_TILELAYOUT) { |
|
364 |
/* XXX: This is for debugging purposes really, and shouldn't stay. */ |
|
365 |
DrawGroundSprite(SPR_SHADOW_CELL, PAL_NONE); |
|
366 |
} else { |
|
367 |
/* Limit the building stage to the number of stages supplied. */ |
|
368 |
byte stage = GetHouseBuildingStage(ti->tile); |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
369 |
stage = Clamp(stage - 4 + group->g.layout.num_sprites, 0, group->g.layout.num_sprites - 1); |
9476 | 370 |
DrawTileLayout(ti, group, stage, house_id); |
371 |
} |
|
372 |
} |
|
373 |
||
374 |
void AnimateNewHouseTile(TileIndex tile) |
|
375 |
{ |
|
376 |
const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); |
|
377 |
byte animation_speed = hs->animation_speed; |
|
378 |
bool frame_set_by_callback = false; |
|
379 |
||
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
380 |
if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_SPEED)) { |
9624 | 381 |
uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_SPEED, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile); |
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
382 |
if (callback_res != CALLBACK_FAILED) animation_speed = Clamp(callback_res & 0xFF, 2, 16); |
9476 | 383 |
} |
384 |
||
385 |
/* An animation speed of 2 means the animation frame changes 4 ticks, and |
|
386 |
* increasing this value by one doubles the wait. 2 is the minimum value |
|
387 |
* allowed for animation_speed, which corresponds to 120ms, and 16 is the |
|
388 |
* maximum, corresponding to around 33 minutes. */ |
|
389 |
if (_tick_counter % (1 << animation_speed) != 0) return; |
|
390 |
||
391 |
byte frame = GetHouseAnimationFrame(tile); |
|
392 |
byte num_frames = GB(hs->animation_frames, 0, 7); |
|
393 |
||
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
394 |
if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_NEXT_FRAME)) { |
9476 | 395 |
uint32 param = (hs->extra_flags & CALLBACK_1A_RANDOM_BITS) ? Random() : 0; |
9624 | 396 |
uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_NEXT_FRAME, param, 0, GetHouseType(tile), GetTownByTile(tile), tile); |
9476 | 397 |
|
398 |
if (callback_res != CALLBACK_FAILED) { |
|
399 |
frame_set_by_callback = true; |
|
400 |
||
401 |
switch (callback_res & 0xFF) { |
|
402 |
case 0xFF: |
|
403 |
DeleteAnimatedTile(tile); |
|
404 |
break; |
|
405 |
case 0xFE: |
|
406 |
/* Carry on as normal. */ |
|
407 |
frame_set_by_callback = false; |
|
408 |
break; |
|
409 |
default: |
|
410 |
frame = callback_res & 0xFF; |
|
411 |
break; |
|
412 |
} |
|
413 |
||
414 |
/* If the lower 7 bits of the upper byte of the callback |
|
415 |
* result are not empty, it is a sound effect. */ |
|
416 |
if (GB(callback_res, 8, 7) != 0) PlayHouseSound(GB(callback_res, 8, 7), tile); |
|
417 |
} |
|
418 |
} |
|
419 |
||
420 |
if (!frame_set_by_callback) { |
|
421 |
if (frame < num_frames) { |
|
422 |
frame++; |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
423 |
} else if (frame == num_frames && HasBit(hs->animation_frames, 7)) { |
9476 | 424 |
/* This animation loops, so start again from the beginning */ |
425 |
frame = 0; |
|
426 |
} else { |
|
427 |
/* This animation doesn't loop, so stay here */ |
|
428 |
DeleteAnimatedTile(tile); |
|
429 |
} |
|
430 |
} |
|
431 |
||
432 |
SetHouseAnimationFrame(tile, frame); |
|
433 |
MarkTileDirtyByTile(tile); |
|
434 |
} |
|
435 |
||
436 |
void ChangeHouseAnimationFrame(TileIndex tile, uint16 callback_result) |
|
437 |
{ |
|
438 |
switch (callback_result & 0xFF) { |
|
439 |
case 0xFD: /* Do nothing. */ break; |
|
440 |
case 0xFE: AddAnimatedTile(tile); break; |
|
441 |
case 0xFF: DeleteAnimatedTile(tile); break; |
|
442 |
default: |
|
443 |
SetHouseAnimationFrame(tile, callback_result & 0xFF); |
|
444 |
AddAnimatedTile(tile); |
|
445 |
break; |
|
446 |
} |
|
447 |
/* If the lower 7 bits of the upper byte of the callback |
|
448 |
* result are not empty, it is a sound effect. */ |
|
449 |
if (GB(callback_result, 8, 7) != 0) PlayHouseSound(GB(callback_result, 8, 7), tile); |
|
450 |
} |
|
451 |
||
452 |
bool CanDeleteHouse(TileIndex tile) |
|
453 |
{ |
|
454 |
const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); |
|
455 |
||
456 |
/* Human players are always allowed to remove buildings, as is water and |
|
457 |
* anyone using the scenario editor. */ |
|
458 |
if ((IsValidPlayer(_current_player) && IsHumanPlayer(_current_player)) |
|
459 |
|| _current_player == OWNER_WATER || _current_player == OWNER_NONE) return true; |
|
460 |
||
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
461 |
if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) { |
9624 | 462 |
uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile); |
9476 | 463 |
return (callback_res == CALLBACK_FAILED || callback_res == 0); |
464 |
} else { |
|
465 |
return !(hs->extra_flags & BUILDING_IS_PROTECTED); |
|
466 |
} |
|
467 |
} |
|
468 |
||
469 |
static void AnimationControl(TileIndex tile, uint16 random_bits) |
|
470 |
{ |
|
471 |
const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); |
|
472 |
||
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
473 |
if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) { |
9476 | 474 |
uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random(); |
9624 | 475 |
uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_START_STOP, param, 0, GetHouseType(tile), GetTownByTile(tile), tile); |
9476 | 476 |
|
477 |
if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(tile, callback_res); |
|
478 |
} |
|
479 |
} |
|
480 |
||
481 |
bool NewHouseTileLoop(TileIndex tile) |
|
482 |
{ |
|
483 |
const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); |
|
484 |
||
485 |
if (GetHouseProcessingTime(tile) > 0) { |
|
486 |
DecHouseProcessingTime(tile); |
|
487 |
return true; |
|
488 |
} |
|
489 |
||
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
490 |
TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
491 |
TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP); |
9476 | 492 |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
493 |
if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) { |
9476 | 494 |
/* If this house is marked as having a synchronised callback, all the |
495 |
* tiles will have the callback called at once, rather than when the |
|
496 |
* tile loop reaches them. This should only be enabled for the northern |
|
497 |
* tile, or strange things will happen (here, and in TTDPatch). */ |
|
498 |
if (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) { |
|
499 |
uint16 random = GB(Random(), 0, 16); |
|
500 |
||
501 |
if (hs->building_flags & BUILDING_HAS_1_TILE) AnimationControl(tile, random); |
|
502 |
if (hs->building_flags & BUILDING_2_TILES_Y) AnimationControl(TILE_ADDXY(tile, 0, 1), random); |
|
503 |
if (hs->building_flags & BUILDING_2_TILES_X) AnimationControl(TILE_ADDXY(tile, 1, 0), random); |
|
504 |
if (hs->building_flags & BUILDING_HAS_4_TILES) AnimationControl(TILE_ADDXY(tile, 1, 1), random); |
|
505 |
} else { |
|
506 |
AnimationControl(tile, 0); |
|
507 |
} |
|
508 |
} |
|
509 |
||
510 |
/* Check callback 21, which determines if a house should be destroyed. */ |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
511 |
if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) { |
9624 | 512 |
uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile); |
9476 | 513 |
if (callback_res != CALLBACK_FAILED && callback_res > 0) { |
514 |
ClearTownHouse(GetTownByTile(tile), tile); |
|
515 |
return false; |
|
516 |
} |
|
517 |
} |
|
518 |
||
519 |
SetHouseProcessingTime(tile, hs->processing_time); |
|
520 |
return true; |
|
521 |
} |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
522 |
|
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
523 |
static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, byte base_random, bool first) |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
524 |
{ |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
525 |
ResolverObject object; |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
526 |
|
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
527 |
/* We can't trigger a non-existent building... */ |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
528 |
assert(IsTileType(tile, MP_HOUSE)); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
529 |
|
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
530 |
HouseID hid = GetHouseType(tile); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
531 |
HouseSpec *hs = GetHouseSpecs(hid); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
532 |
|
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
533 |
NewHouseResolver(&object, hid, tile, GetTownByTile(tile)); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
534 |
|
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
535 |
object.callback = CBID_RANDOM_TRIGGER; |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
536 |
object.trigger = trigger; |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
537 |
|
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
538 |
const SpriteGroup *group = Resolve(hs->spritegroup, &object); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
539 |
if (group == NULL) return; |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
540 |
|
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
541 |
byte new_random_bits = Random(); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
542 |
byte random_bits = GetHouseRandomBits(tile); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
543 |
random_bits &= ~object.reseed; |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
544 |
random_bits |= (first ? new_random_bits : base_random) & object.reseed; |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
545 |
SetHouseRandomBits(tile, random_bits); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
546 |
|
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
547 |
switch (trigger) { |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
548 |
case HOUSE_TRIGGER_TILE_LOOP: |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
549 |
/* Random value already set. */ |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
550 |
break; |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
551 |
|
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
552 |
case HOUSE_TRIGGER_TILE_LOOP_TOP: |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
553 |
if (!first) break; |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
554 |
/* Random value of first tile already set. */ |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
555 |
if (hs->building_flags & BUILDING_2_TILES_Y) DoTriggerHouse(TILE_ADDXY(tile, 0, 1), trigger, random_bits, false); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
556 |
if (hs->building_flags & BUILDING_2_TILES_X) DoTriggerHouse(TILE_ADDXY(tile, 1, 0), trigger, random_bits, false); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
557 |
if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TILE_ADDXY(tile, 1, 1), trigger, random_bits, false); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
558 |
break; |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
559 |
} |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
560 |
} |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
561 |
|
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
562 |
void TriggerHouse(TileIndex t, HouseTrigger trigger) |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
563 |
{ |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
564 |
DoTriggerHouse(t, trigger, 0, true); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9718
diff
changeset
|
565 |
} |