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