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