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