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