author | truelight |
Fri, 10 Sep 2004 19:02:27 +0000 | |
changeset 193 | 0a7025304867 |
parent 159 | 139cf78bfb28 |
child 260 | 32fcaaf9f4ef |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
3 |
#include "viewport.h" |
|
4 |
#include "town.h" |
|
5 |
#include "command.h" |
|
6 |
#include "pathfind.h" |
|
7 |
#include "gfx.h" |
|
8 |
#include "industry.h" |
|
9 |
#include "station.h" |
|
10 |
#include "player.h" |
|
11 |
#include "news.h" |
|
12 |
#include "saveload.h" |
|
13 |
#include "economy.h" |
|
14 |
#include "gui.h" |
|
15 |
||
16 |
// Local |
|
17 |
static int _grow_town_result; |
|
18 |
||
19 |
static bool BuildTownHouse(Town *t, uint tile); |
|
20 |
static void ClearTownHouse(Town *t, uint tile); |
|
21 |
static void DoBuildTownHouse(Town *t, uint tile); |
|
22 |
||
23 |
typedef struct DrawTownTileStruct { |
|
24 |
uint32 sprite_1; |
|
25 |
uint32 sprite_2; |
|
26 |
||
27 |
byte subtile_xy; |
|
28 |
byte width_height; |
|
29 |
byte dz; |
|
30 |
byte proc; |
|
31 |
} DrawTownTileStruct; |
|
32 |
||
33 |
#include "table/town_land.h" |
|
34 |
||
35 |
||
36 |
static void TownDrawTileProc1(TileInfo *ti) |
|
37 |
{ |
|
38 |
AddChildSpriteScreen(0x5A3, 0xE, 0x3C - (_map_owner[ti->tile]&0x7F)); |
|
39 |
} |
|
40 |
||
41 |
typedef void TownDrawTileProc(TileInfo *ti); |
|
42 |
static TownDrawTileProc * const _town_draw_tile_procs[1] = { |
|
43 |
TownDrawTileProc1 |
|
44 |
}; |
|
45 |
||
46 |
||
47 |
static void DrawTile_Town(TileInfo *ti) |
|
48 |
{ |
|
49 |
const DrawTownTileStruct *dcts; |
|
50 |
byte z; |
|
51 |
uint32 image; |
|
52 |
||
53 |
/* Retrieve pointer to the draw town tile struct */ |
|
54 |
{ |
|
55 |
uint hash, t; |
|
56 |
hash = ti->x >> 4; |
|
57 |
hash ^= hash>>2; |
|
58 |
hash ^= (t=(ti->y >> 4)); |
|
59 |
hash -= t>>2; |
|
60 |
dcts = &_town_draw_tile_data[((_map2[ti->tile]<<4)|(_map3_lo[ti->tile]>>6)|((hash&3)<<2))]; |
|
61 |
} |
|
62 |
||
63 |
z = ti->z; |
|
64 |
||
65 |
/* Add bricks below the house? */ |
|
66 |
if (ti->tileh) { |
|
67 |
AddSortableSpriteToDraw(0x3DD + ti->tileh, ti->x, ti->y, 16, 16, 7, z); |
|
68 |
AddChildSpriteScreen(dcts->sprite_1, 0x1F, 1); |
|
69 |
z += 8; |
|
70 |
} else { |
|
71 |
/* Else draw regular ground */ |
|
72 |
DrawGroundSprite(dcts->sprite_1); |
|
73 |
} |
|
74 |
||
75 |
/* Add a house on top of the ground? */ |
|
76 |
if ((image = dcts->sprite_2) != 0) { |
|
77 |
if (!(_display_opt & DO_TRANS_BUILDINGS)) |
|
78 |
image = (image & 0x3FFF) | 0x3224000; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
79 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
80 |
AddSortableSpriteToDraw(image, |
0 | 81 |
ti->x | (dcts->subtile_xy>>4), |
82 |
ti->y | (dcts->subtile_xy&0xF), |
|
83 |
(dcts->width_height>>4)+1, |
|
84 |
(dcts->width_height&0xF)+1, |
|
85 |
dcts->dz, |
|
86 |
z); |
|
87 |
||
88 |
if (!(_display_opt & DO_TRANS_BUILDINGS)) |
|
89 |
return; |
|
90 |
} |
|
91 |
||
92 |
{ |
|
93 |
int proc; |
|
94 |
if ((proc=dcts->proc-1) >= 0 ) |
|
95 |
_town_draw_tile_procs[proc](ti); |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
static uint GetSlopeZ_Town(TileInfo *ti) |
|
100 |
{ |
|
101 |
uint z = GetPartialZ(ti->x&0xF, ti->y&0xF, ti->tileh) + ti->z; |
|
102 |
if (ti->tileh != 0) z = (z & ~7) + 4; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
103 |
return (uint16) z; |
0 | 104 |
} |
105 |
||
39 | 106 |
static uint GetSlopeTileh_Town(TileInfo *ti) |
107 |
{ |
|
108 |
return ti->tileh; |
|
109 |
} |
|
110 |
||
0 | 111 |
static void AnimateTile_Town(uint tile) |
112 |
{ |
|
113 |
int old; |
|
114 |
int i; |
|
115 |
int a,b; |
|
116 |
||
117 |
if (_tick_counter & 3) |
|
118 |
return; |
|
119 |
||
120 |
if (_map2[tile] != 4 && _map2[tile] != 5) |
|
121 |
return; |
|
122 |
||
123 |
if (!((old=_map_owner[tile])&0x80)) { |
|
124 |
_map_owner[tile] |= 0x80; |
|
125 |
||
126 |
do { |
|
127 |
i = (Random()&7) - 1; |
|
128 |
} while (i < 0 || i == 1 || i*6==old); |
|
129 |
||
130 |
_map5[tile] = (_map5[tile] & ~0x3F) | i; |
|
131 |
} |
|
132 |
||
133 |
a = _map_owner[tile]&0x7F; |
|
134 |
b = (_map5[tile]&0x3F) * 6; |
|
135 |
a += (a < b) ? 1 : -1; |
|
136 |
_map_owner[tile] = (_map_owner[tile]&0x80)|a; |
|
137 |
||
138 |
if (a == b) { |
|
139 |
_map5[tile] &= 0x40; |
|
140 |
_map_owner[tile] &= 0x7F; |
|
141 |
DeleteAnimatedTile(tile); |
|
142 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
143 |
|
0 | 144 |
MarkTileDirtyByTile(tile); |
145 |
} |
|
146 |
||
147 |
static void UpdateTownRadius(Town *t); |
|
148 |
||
149 |
static bool IsCloseToTown(uint tile, uint dist) |
|
150 |
{ |
|
151 |
Town *t; |
|
152 |
||
153 |
FOR_ALL_TOWNS(t) { |
|
154 |
if (t->xy != 0 && GetTileDist(tile, t->xy) < dist) |
|
155 |
return true; |
|
156 |
} |
|
157 |
return false; |
|
158 |
} |
|
159 |
||
160 |
||
161 |
static void ChangePopulation(Town *t, int mod) |
|
162 |
{ |
|
163 |
t->population += mod; |
|
164 |
InvalidateWindow(WC_TOWN_VIEW, t->index); |
|
165 |
||
166 |
if (_town_sort_order & 2) _town_sort_dirty = true; |
|
167 |
} |
|
168 |
||
169 |
static void MakeSingleHouseBigger(uint tile) |
|
170 |
{ |
|
171 |
byte b; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
172 |
|
0 | 173 |
assert(IS_TILETYPE(tile, MP_HOUSE)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
174 |
|
0 | 175 |
b = _map5[tile]; |
176 |
if (b & 0x80) |
|
177 |
return; |
|
178 |
||
179 |
_map5[tile] = (b & 0xC0) | ((b+1)&7); |
|
180 |
||
181 |
if ((_map5[tile]&7) != 0) |
|
182 |
return; |
|
183 |
||
184 |
_map3_lo[tile] = _map3_lo[tile] + 0x40; |
|
185 |
||
186 |
if ( (_map3_lo[tile] & 0xC0) == 0xC0) { |
|
187 |
Town *t = ClosestTownFromTile(tile, (uint)-1); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
188 |
ChangePopulation(t, _housetype_population[_map2[tile]]); |
0 | 189 |
} |
190 |
MarkTileDirtyByTile(tile); |
|
191 |
} |
|
192 |
||
193 |
static void MakeTownHouseBigger(uint tile) |
|
194 |
{ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
195 |
uint flags = _house_more_flags[_map2[tile]]; |
0 | 196 |
if (flags & 8) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 0)); |
197 |
if (flags & 4) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 1)); |
|
198 |
if (flags & 2) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 0)); |
|
199 |
if (flags & 1) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 1)); |
|
200 |
} |
|
201 |
||
202 |
static void TileLoop_Town(uint tile) |
|
203 |
{ |
|
204 |
int house; |
|
205 |
Town *t; |
|
206 |
uint32 r; |
|
207 |
||
208 |
if ((_map3_lo[tile] & 0xC0) != 0xC0) { |
|
209 |
MakeTownHouseBigger(tile); |
|
210 |
return; |
|
211 |
} |
|
212 |
||
213 |
house = _map2[tile]; |
|
214 |
if (_housetype_extra_flags[house] & 0x20 && |
|
215 |
!(_map5[tile] & 0x80) && |
|
216 |
CHANCE16(1,2) && |
|
217 |
AddAnimatedTile(tile)) { |
|
218 |
_map5[tile] = (_map5[tile] & 0x40)|0x80; |
|
219 |
} |
|
220 |
||
221 |
t = ClosestTownFromTile(tile, (uint)-1); |
|
222 |
||
223 |
r = Random(); |
|
224 |
||
225 |
if ( (byte)r < _housetype_population[house] ) { |
|
226 |
uint amt = ((byte)r >> 3) + 1, moved; |
|
227 |
if (_economy.fluct <= 0) amt = (amt + 1) >> 1; |
|
228 |
t->new_max_pass += amt; |
|
229 |
moved = MoveGoodsToStation(tile, 1, 1, CT_PASSENGERS, amt); |
|
230 |
t->new_act_pass += moved; |
|
231 |
} |
|
232 |
||
233 |
if ( (byte)(r>>8) < _housetype_mailamount[house] ) { |
|
234 |
uint amt = ((byte)(r>>8) >> 3) + 1, moved; |
|
235 |
if (_economy.fluct <= 0) amt = (amt + 1) >> 1; |
|
236 |
t->new_max_mail += amt; |
|
237 |
moved = MoveGoodsToStation(tile, 1, 1, CT_MAIL, amt); |
|
238 |
t->new_act_mail += moved; |
|
239 |
} |
|
240 |
||
241 |
if (_house_more_flags[house]&8 && (t->flags12&1) && --t->time_until_rebuild == 0) { |
|
242 |
r>>=16; |
|
243 |
t->time_until_rebuild = (r & 63) + 130; |
|
244 |
||
245 |
_current_player = OWNER_TOWN; |
|
246 |
||
247 |
ClearTownHouse(t, tile); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
248 |
|
0 | 249 |
// rebuild with another house? |
250 |
if ( (byte) (r >> 8) >= 12) { |
|
251 |
DoBuildTownHouse(t, tile); |
|
252 |
} |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
static void ClickTile_Town(uint tile) |
|
257 |
{ |
|
258 |
/* not used */ |
|
259 |
} |
|
260 |
||
261 |
static int32 ClearTile_Town(uint tile, byte flags) |
|
262 |
{ |
|
263 |
int house, rating; |
|
264 |
int32 cost; |
|
265 |
Town *t; |
|
266 |
||
267 |
// safety checks |
|
268 |
if (!EnsureNoVehicle(tile)) return CMD_ERROR; |
|
269 |
if (flags&DC_AUTO && !(flags&DC_AI_BUILDING)) return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED); |
|
270 |
||
271 |
house = _map2[tile]; |
|
272 |
cost = _price.remove_house * _housetype_remove_cost[house] >> 8; |
|
273 |
||
274 |
rating = _housetype_remove_ratingmod[house]; |
|
275 |
_cleared_town_rating += rating; |
|
276 |
_cleared_town = t = ClosestTownFromTile(tile, (uint)-1); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
277 |
|
0 | 278 |
if (_current_player < MAX_PLAYERS) { |
279 |
if (rating > t->ratings[_current_player] && !(flags & DC_NO_TOWN_RATING) && !_cheats.magic_bulldozer.value) { |
|
280 |
SET_DPARAM16(0, t->index); |
|
281 |
return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
282 |
} |
0 | 283 |
} |
284 |
||
285 |
if (flags & DC_EXEC) { |
|
286 |
ChangeTownRating(t, -rating, -1000); |
|
287 |
ClearTownHouse(t, tile); |
|
288 |
} |
|
289 |
||
290 |
return cost; |
|
291 |
} |
|
292 |
||
293 |
static void GetAcceptedCargo_Town(uint tile, AcceptedCargo *ac) |
|
294 |
{ |
|
295 |
int type = _map2[tile]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
296 |
|
0 | 297 |
ac->type_1 = CT_PASSENGERS; |
298 |
ac->amount_1 = _housetype_cargo_passengers[type]; |
|
299 |
||
300 |
ac->type_2 = CT_GOODS; |
|
301 |
ac->amount_2 = _housetype_cargo_goods[type]; |
|
302 |
if (ac->amount_2 & 0x80) { |
|
303 |
ac->amount_2 &= 0x7F; |
|
304 |
ac->type_2 = CT_FOOD; |
|
305 |
} |
|
306 |
||
307 |
ac->type_3 = CT_MAIL; |
|
308 |
ac->amount_3 = _housetype_cargo_mail[type]; |
|
309 |
} |
|
310 |
||
311 |
static void GetTileDesc_Town(uint tile, TileDesc *td) |
|
312 |
{ |
|
313 |
td->str = _town_tile_names[_map2[tile]]; |
|
314 |
if ((_map3_lo[tile] & 0xC0) != 0xC0) { |
|
315 |
SET_DPARAMX16(td->dparam, 0, td->str); |
|
316 |
td->str = STR_2058_UNDER_CONSTRUCTION; |
|
317 |
} |
|
318 |
||
319 |
td->owner = OWNER_TOWN; |
|
320 |
} |
|
321 |
||
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
121
diff
changeset
|
322 |
static uint32 GetTileTrackStatus_Town(uint tile, TransportType mode) |
0 | 323 |
{ |
324 |
/* not used */ |
|
325 |
return 0; |
|
326 |
} |
|
327 |
||
328 |
static void ChangeTileOwner_Town(uint tile, byte old_player, byte new_player) |
|
329 |
{ |
|
330 |
/* not used */ |
|
331 |
} |
|
332 |
||
333 |
||
334 |
static const TileIndexDiff _roadblock_tileadd[4+3] = { |
|
335 |
TILE_XY(0,-1), |
|
336 |
TILE_XY(1,0), |
|
337 |
TILE_XY(0,1), |
|
338 |
TILE_XY(-1,0), |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
339 |
|
0 | 340 |
// Store the first 3 elements again. |
341 |
// Lets us rotate without using &3. |
|
342 |
TILE_XY(0,-1), |
|
343 |
TILE_XY(1,0), |
|
344 |
TILE_XY(0,1), |
|
345 |
}; |
|
346 |
||
347 |
static void TownTickHandler(Town *t) |
|
348 |
{ |
|
349 |
if (t->flags12&1) { |
|
350 |
int i = t->grow_counter - 1; |
|
351 |
if (i < 0) { |
|
352 |
if (GrowTown(t)) { |
|
353 |
i = t->growth_rate; |
|
354 |
} else { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
355 |
i = 0; |
0 | 356 |
} |
357 |
} |
|
358 |
t->grow_counter = i; |
|
359 |
} |
|
360 |
||
361 |
UpdateTownRadius(t); |
|
362 |
} |
|
363 |
||
364 |
void OnTick_Town() |
|
365 |
{ |
|
366 |
uint i; |
|
367 |
Town *t; |
|
368 |
||
369 |
if (_game_mode == GM_EDITOR) |
|
370 |
return; |
|
371 |
||
372 |
i = _cur_town_ctr; |
|
373 |
t = DEREF_TOWN(i); |
|
374 |
if (++i == lengthof(_towns)) i = 0; |
|
375 |
_cur_town_ctr = i; |
|
376 |
||
377 |
if (t->xy != 0) |
|
378 |
TownTickHandler(t); |
|
379 |
||
380 |
} |
|
381 |
||
382 |
static byte GetTownRoadMask(TileIndex tile) |
|
383 |
{ |
|
384 |
byte b = GetRoadBitsByTile(tile); |
|
385 |
byte r=0; |
|
386 |
if (b&1) r|=10; |
|
387 |
if (b&2) r|=5; |
|
388 |
if (b&4) r|=9; |
|
389 |
if (b&8) r|=6; |
|
390 |
if (b&16) r|=3; |
|
391 |
if (b&32) r|=12; |
|
392 |
return r; |
|
393 |
} |
|
394 |
||
395 |
static bool IsRoadAllowedHere(uint tile, int dir) |
|
396 |
{ |
|
397 |
uint k; |
|
398 |
uint slope; |
|
399 |
||
400 |
// If this assertion fails, it might be because the world contains |
|
401 |
// land at the edges. This is not ok. |
|
402 |
TILE_ASSERT(tile); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
403 |
|
0 | 404 |
for(;;) { |
405 |
// Check if there already is a road at this point? |
|
406 |
if (GetRoadBitsByTile(tile) == 0) { |
|
407 |
// No, try to build one in the direction. |
|
408 |
// if that fails clear the land, and if that fails exit. |
|
409 |
// This is to make sure that we can build a road here later. |
|
410 |
if (DoCommandByTile(tile, (dir&1)?0xA:0x5, 0, DC_AUTO, CMD_BUILD_ROAD) == CMD_ERROR && |
|
411 |
DoCommandByTile(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR) == CMD_ERROR) |
|
412 |
return false; |
|
413 |
} |
|
414 |
||
415 |
slope = GetTileSlope(tile, NULL); |
|
416 |
if (slope == 0) { |
|
417 |
// Tile has no slope |
|
418 |
// Disallow the road if any neighboring tile has a road. |
|
419 |
if (HASBIT(GetTownRoadMask(TILE_ADD(tile, _roadblock_tileadd[dir+1])), dir^2) || |
|
420 |
HASBIT(GetTownRoadMask(TILE_ADD(tile, _roadblock_tileadd[dir+3])), dir^2) || |
|
421 |
HASBIT(GetTownRoadMask(TILE_ADD(tile, _roadblock_tileadd[dir+1] + _roadblock_tileadd[dir+2])), dir) || |
|
422 |
HASBIT(GetTownRoadMask(TILE_ADD(tile, _roadblock_tileadd[dir+3] + _roadblock_tileadd[dir+2])), dir)) |
|
423 |
return false; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
424 |
|
0 | 425 |
// Otherwise allow |
426 |
return true; |
|
427 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
428 |
|
0 | 429 |
// If the tile is not a slope in the right direction, then |
430 |
// maybe terraform some. |
|
431 |
if ((k = (dir&1)?0xC:0x9) != slope && (k^0xF) != slope) { |
|
432 |
uint32 r = Random(); |
|
433 |
||
434 |
if (CHANCE16I(1,8, r) && !_generating_world) { |
|
435 |
if (CHANCE16I(1,16,r)) |
|
436 |
DoCommandByTile(tile, slope, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); |
|
437 |
else |
|
438 |
DoCommandByTile(tile, slope^0xF, 1, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); |
|
439 |
} |
|
440 |
return false; |
|
441 |
} |
|
442 |
||
443 |
tile = TILE_ADD(tile, _roadblock_tileadd[dir]); |
|
444 |
} |
|
445 |
} |
|
446 |
||
447 |
static bool TerraformTownTile(uint tile, int edges, int dir) |
|
448 |
{ |
|
449 |
int32 r; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
450 |
|
0 | 451 |
TILE_ASSERT(tile); |
452 |
||
453 |
r = DoCommandByTile(tile, edges, dir, DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); |
|
454 |
if (r == CMD_ERROR || r >= 126*16) |
|
455 |
return false; |
|
456 |
DoCommandByTile(tile, edges, dir, DC_AUTO | DC_NO_WATER | DC_EXEC, CMD_TERRAFORM_LAND); |
|
457 |
return true; |
|
458 |
} |
|
459 |
||
460 |
static void LevelTownLand(uint tile) |
|
461 |
{ |
|
462 |
TileInfo ti; |
|
463 |
||
464 |
TILE_ASSERT(tile); |
|
465 |
||
466 |
// Don't terraform if land is plain or if there's a house there. |
|
467 |
FindLandscapeHeightByTile(&ti, tile); |
|
468 |
if (ti.tileh == 0 || ti.type == MP_HOUSE) |
|
469 |
return; |
|
470 |
||
471 |
// First try up, then down |
|
472 |
if (!TerraformTownTile(tile, ~ti.tileh & 0xF, 1)) { |
|
473 |
TerraformTownTile(tile, ti.tileh & 0xF, 0); |
|
474 |
} |
|
475 |
} |
|
476 |
||
477 |
#define IS_WATER_TILE(t) (IS_TILETYPE((t), MP_WATER) && _map5[(t)] == 0) |
|
478 |
||
479 |
static void GrowTownInTile(uint *tile_ptr, uint mask, int block, Town *t1) |
|
480 |
{ |
|
481 |
Town *t2; |
|
482 |
uint16 r; |
|
483 |
int a,b,rcmd; |
|
484 |
uint tmptile; |
|
485 |
TileInfo ti; |
|
486 |
int i; |
|
487 |
int j; |
|
488 |
uint tile = *tile_ptr; |
|
489 |
||
490 |
TILE_ASSERT(tile); |
|
491 |
||
492 |
if (mask == 0) { |
|
493 |
// Tile has no road. First reset the status counter |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
494 |
// to say that this is the last iteration. |
0 | 495 |
_grow_town_result = 0; |
496 |
||
497 |
// Then check if the tile we are at belongs to the town, |
|
498 |
// if not, bail out. |
|
499 |
t2 = ClosestTownFromTile(tile, (uint)-1); |
|
500 |
if (t2 != t1) |
|
501 |
return; |
|
502 |
||
503 |
// Remove hills etc |
|
504 |
LevelTownLand(tile); |
|
505 |
||
506 |
// Is a road allowed here? |
|
507 |
if (!IsRoadAllowedHere(tile, block)) |
|
508 |
return; |
|
509 |
||
510 |
// Randomize new road block numbers |
|
511 |
a = block; |
|
512 |
b = block ^ 2; |
|
513 |
r = (uint16)Random(); |
|
514 |
if (r <= 0x4000) do { |
|
515 |
a = (int)Random() & 3; |
|
516 |
} while(a == b); |
|
517 |
||
518 |
if (!IsRoadAllowedHere(TILE_ADD(tile,_roadblock_tileadd[a]), a)) { |
|
519 |
// A road is not allowed to continue the randomized road, |
|
520 |
// return if the road we're trying to build is curved. |
|
521 |
if ( a != (b^2)) |
|
522 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
523 |
|
0 | 524 |
// Return if neither side of the new road is a house |
525 |
if (!IS_TILETYPE(TILE_ADD(tile,_roadblock_tileadd[a+1]), MP_HOUSE) && |
|
526 |
!IS_TILETYPE(TILE_ADD(tile,_roadblock_tileadd[a+3]), MP_HOUSE)) |
|
527 |
return; |
|
528 |
||
529 |
// That means that the road is only allowed if there is a house |
|
530 |
// at any side of the new road. |
|
531 |
} |
|
532 |
rcmd = (1 << a) + (1 << b); |
|
533 |
||
534 |
} else if (block < 5 && !HASBIT(mask,block^2)) { |
|
535 |
// Continue building on a partial road. |
|
536 |
// Always OK. |
|
537 |
_grow_town_result = 0; |
|
538 |
rcmd = 1 << (block^2); |
|
539 |
} else { |
|
540 |
||
541 |
// Reached a tunnel? Then continue at the other side of it. |
|
542 |
if (IS_TILETYPE(tile, MP_TUNNELBRIDGE) && (_map5[tile]&~3)==4) { |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
121
diff
changeset
|
543 |
FindLengthOfTunnelResult flotr = FindLengthOfTunnel(tile, _map5[tile]&3); |
0 | 544 |
*tile_ptr = flotr.tile; |
545 |
return; |
|
546 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
547 |
|
0 | 548 |
// For any other kind of tunnel/bridge, bail out. |
549 |
if (IS_TILETYPE(tile, MP_TUNNELBRIDGE)) |
|
550 |
return; |
|
551 |
||
552 |
// Possibly extend the road in a direction. |
|
553 |
// Randomize a direction and if it has a road, bail out. |
|
554 |
i = (int)Random() & 3; |
|
555 |
if (HASBIT(mask, i)) |
|
556 |
return; |
|
557 |
||
558 |
// This is the tile we will reach if we extend to this direction. |
|
559 |
tmptile = TILE_ADD(tile,_roadblock_tileadd[i]); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
560 |
|
0 | 561 |
// Don't do it if it reaches to water. |
562 |
if (IS_WATER_TILE(tmptile)) |
|
563 |
return; |
|
564 |
||
565 |
// If the new tile belongs to another town, |
|
566 |
// then stop the search altogether. |
|
567 |
if (ClosestTownFromTile(tmptile, (uint)-1) != t1) { |
|
568 |
_grow_town_result = 0; |
|
569 |
return; |
|
570 |
} |
|
571 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
572 |
// Build a house at the edge. 60% chance or |
0 | 573 |
// always ok if no road allowed. |
574 |
if (!IsRoadAllowedHere(tmptile, i) || CHANCE16(6,10)) { |
|
575 |
// But not if there already is a house there. |
|
576 |
if (!IS_TILETYPE(tmptile, MP_HOUSE)) { |
|
577 |
// Level the land if possible |
|
578 |
LevelTownLand(tmptile); |
|
579 |
||
580 |
// And build a house. |
|
581 |
// Set result to -1 if we managed to build it. |
|
582 |
if (BuildTownHouse(t1, tmptile)) |
|
583 |
_grow_town_result = -1; |
|
584 |
} |
|
585 |
return; |
|
586 |
} |
|
587 |
||
588 |
_grow_town_result = 0; |
|
589 |
rcmd = 1 << i; |
|
590 |
} |
|
591 |
||
592 |
FindLandscapeHeightByTile(&ti, tile); |
|
593 |
||
594 |
// Return if a water tile |
|
595 |
if (ti.type == MP_WATER && ti.map5==0) |
|
596 |
return; |
|
597 |
||
598 |
// Determine direction of slope, |
|
599 |
// and build a road if not a special slope. |
|
600 |
if ((i=0,ti.tileh != 3) && |
|
601 |
(i++,ti.tileh != 9) && |
|
602 |
(i++,ti.tileh != 12) && |
|
603 |
(i++,ti.tileh != 6)) { |
|
604 |
build_road_and_exit: |
|
605 |
if (DoCommandByTile(tile, rcmd, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD) != CMD_ERROR) |
|
606 |
_grow_town_result = -1; |
|
607 |
return; |
|
608 |
} |
|
609 |
||
610 |
tmptile = tile; |
|
611 |
||
612 |
// Now it contains the direction of the slope |
|
613 |
j = -11; // max 11 tile long bridges |
|
614 |
do { |
|
615 |
if (++j == 0) |
|
616 |
goto build_road_and_exit; |
|
617 |
tmptile = TILE_MASK(tmptile + _tileoffs_by_dir[i]); |
|
618 |
} while (IS_WATER_TILE(tmptile)); |
|
619 |
||
620 |
// no water tiles in between? |
|
621 |
if (j == -10) |
|
622 |
goto build_road_and_exit; |
|
623 |
||
624 |
// Quit if it selecting an appropiate bridge type fails a large number of times. |
|
625 |
j = 22; |
|
626 |
{ |
|
627 |
int32 bridge_len = GetBridgeLength(tile, tmptile); |
|
628 |
do { |
|
629 |
byte bridge_type = RandomRange(MAX_BRIDGES - 1); |
|
630 |
if (CheckBridge_Stuff(bridge_type, bridge_len)) { |
|
631 |
if (DoCommandByTile(tile, tmptile, 0x8000 + bridge_type, DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE) != CMD_ERROR) |
|
632 |
_grow_town_result = -1; |
|
633 |
||
634 |
// obviously, if building any bridge would fail, there is no need to try other bridge-types |
|
635 |
return; |
|
636 |
} |
|
637 |
} while(--j != 0); |
|
638 |
} |
|
639 |
} |
|
640 |
#undef IS_WATER_TILE |
|
641 |
||
642 |
||
643 |
// Returns true if a house was built, or no if the build failed. |
|
644 |
static int GrowTownAtRoad(Town *t, uint tile) |
|
645 |
{ |
|
646 |
uint mask; |
|
647 |
int block = 5; // special case |
|
648 |
||
649 |
TILE_ASSERT(tile); |
|
650 |
||
651 |
// Number of times to search. |
|
652 |
_grow_town_result = 20; |
|
653 |
||
654 |
do { |
|
655 |
// Get a bitmask of the road blocks on a tile |
|
656 |
mask = GetTownRoadMask(tile); |
|
657 |
||
658 |
// Try to grow the town from this point |
|
659 |
GrowTownInTile(&tile,mask,block,t); |
|
660 |
||
661 |
// Exclude the source position from the bitmask |
|
662 |
// and return if no more road blocks available |
|
663 |
CLRBIT(mask, (block ^ 2)); |
|
664 |
if (mask == 0) |
|
665 |
return _grow_town_result; |
|
666 |
||
667 |
// Select a random bit from the blockmask, walk a step |
|
668 |
// and continue the search from there. |
|
669 |
do block = Random() & 3; while (!HASBIT(mask,block)); |
|
670 |
tile += _roadblock_tileadd[block]; |
|
671 |
||
672 |
// Max number of times is checked. |
|
673 |
} while (--_grow_town_result >= 0); |
|
674 |
||
675 |
return (_grow_town_result == -2); |
|
676 |
} |
|
677 |
||
678 |
// Generate a random road block |
|
679 |
// The probability of a straight road |
|
680 |
// is somewhat higher than a curved. |
|
681 |
static int GenRandomRoadBits() |
|
682 |
{ |
|
683 |
uint32 r = Random(); |
|
684 |
int a = r&3, b = (r >> 8) & 3; |
|
685 |
if (a == b) b ^= 2; |
|
686 |
return (1<<a)+(1<<b); |
|
687 |
} |
|
688 |
||
689 |
// Grow the town |
|
690 |
// Returns true if a house was built, or no if the build failed. |
|
691 |
bool GrowTown(Town *t) |
|
692 |
{ |
|
693 |
uint tile; |
|
694 |
const TileIndexDiff *ptr; |
|
695 |
int offs; |
|
696 |
TileInfo ti; |
|
697 |
||
698 |
static const TileIndexDiff _town_coord_mod[] = { |
|
699 |
TILE_XY(-1,0), |
|
700 |
TILE_XY(1,1), |
|
701 |
TILE_XY(1,-1), |
|
702 |
TILE_XY(-1,-1), |
|
703 |
TILE_XY(-1,0), |
|
704 |
TILE_XY(0,2), |
|
705 |
TILE_XY(2,0), |
|
706 |
TILE_XY(0,-2), |
|
707 |
TILE_XY(-1,-1), |
|
708 |
TILE_XY(-2,2), |
|
709 |
TILE_XY(2,2), |
|
710 |
TILE_XY(2,-2), |
|
711 |
0, |
|
712 |
}; |
|
713 |
||
714 |
// Current player is a town |
|
715 |
_current_player = OWNER_TOWN; |
|
716 |
||
717 |
// Find a road that we can base the construction on. |
|
718 |
tile = t->xy; |
|
719 |
ptr = _town_coord_mod; |
|
720 |
do { |
|
721 |
if (GetRoadBitsByTile(tile) != 0) { |
|
722 |
return GrowTownAtRoad(t, tile); |
|
723 |
} |
|
724 |
offs = *ptr++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
725 |
|
0 | 726 |
tile = TILE_ADD(tile, offs); |
727 |
} while (offs); |
|
728 |
||
729 |
// No road available, try to build a random road block by |
|
730 |
// clearing some land and then building a road there. |
|
731 |
tile = t->xy; |
|
732 |
ptr = _town_coord_mod; |
|
733 |
do { |
|
734 |
FindLandscapeHeightByTile(&ti, tile); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
735 |
|
0 | 736 |
// Only work with plain land that not already has a house with map5=0 |
737 |
if (ti.tileh == 0 && !(ti.type==MP_HOUSE && ti.map5==0)) { |
|
738 |
if (DoCommandByTile(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR) != CMD_ERROR) { |
|
739 |
DoCommandByTile(tile, GenRandomRoadBits(), 0, DC_EXEC | DC_AUTO, CMD_BUILD_ROAD); |
|
740 |
return true; |
|
741 |
} |
|
742 |
} |
|
743 |
offs = *ptr++; |
|
744 |
tile = TILE_ADD(tile, offs); |
|
745 |
} while (offs != 0); |
|
746 |
||
747 |
return false; |
|
748 |
} |
|
749 |
||
750 |
static void UpdateTownRadius(Town *t) |
|
751 |
{ |
|
752 |
static const uint16 _town_radius_data[23][5] = { |
|
753 |
{ 4, 0, 0, 0, 0}, |
|
754 |
{16, 0, 0, 0, 0}, |
|
755 |
{25, 0, 0, 0, 0}, |
|
756 |
{36, 0, 0, 0, 0}, |
|
757 |
{49, 0, 4, 0, 0}, |
|
758 |
{64, 0, 4, 0, 0}, |
|
759 |
{64, 0, 9, 0, 1}, |
|
760 |
{64, 0, 9, 0, 4}, |
|
761 |
{64, 0, 16, 0, 4}, |
|
762 |
{81, 0, 16, 0, 4}, |
|
763 |
{81, 0, 16, 0, 4}, |
|
764 |
{81, 0, 25, 0, 9}, |
|
765 |
{81, 36, 25, 0, 9}, |
|
766 |
{81, 36, 25, 16, 9}, |
|
767 |
{81, 49, 0, 25, 9}, |
|
768 |
{81, 64, 0, 25, 9}, |
|
769 |
{81, 64, 0, 36, 9}, |
|
770 |
{81, 64, 0, 36, 16}, |
|
771 |
{ 0, 81, 0, 49, 16}, |
|
772 |
{ 0, 81, 0, 49, 25}, |
|
773 |
{ 0,100, 0, 49, 25}, |
|
774 |
{ 0,100, 0, 64, 25}, |
|
775 |
{ 0,100, 0, 64, 36}, |
|
776 |
}; |
|
777 |
int i = min(t->num_houses, 88) >> 2; |
|
778 |
memcpy(t->radius, _town_radius_data[i], sizeof(t->radius)); |
|
779 |
} |
|
780 |
||
781 |
static void UpdateTownVirtCoord(Town *t) |
|
782 |
{ |
|
783 |
Point pt = RemapCoords2(GET_TILE_X(t->xy)*16, GET_TILE_Y(t->xy)*16); |
|
784 |
SET_DPARAM32(0, t->townnameparts); |
|
785 |
UpdateViewportSignPos(&t->sign, pt.x, pt.y - 24, t->townnametype); |
|
786 |
} |
|
787 |
||
788 |
static void CreateTownName(Town *t1) |
|
789 |
{ |
|
790 |
Town *t2; |
|
791 |
char buf1[64]; |
|
792 |
char buf2[64]; |
|
793 |
uint32 r; |
|
794 |
||
795 |
t1->townnametype = SPECSTR_TOWNNAME_START + _opt.town_name; |
|
796 |
||
797 |
for(;;) { |
|
798 |
restart: |
|
799 |
r = Random(); |
|
800 |
||
801 |
SET_DPARAM32(0, r); |
|
802 |
GetString(buf1, t1->townnametype); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
803 |
|
0 | 804 |
// Check size and width |
805 |
if (strlen(buf1) >= 31 || GetStringWidth(buf1) > 130) |
|
806 |
continue; |
|
807 |
||
808 |
FOR_ALL_TOWNS(t2) { |
|
809 |
if (t2->xy != 0) { |
|
810 |
SET_DPARAM32(0, t2->townnameparts); |
|
811 |
GetString(buf2, t2->townnametype); |
|
812 |
if (str_eq(buf1, buf2)) |
|
813 |
goto restart; |
|
814 |
} |
|
815 |
} |
|
816 |
t1->townnameparts = r; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
817 |
|
0 | 818 |
return; |
819 |
} |
|
820 |
} |
|
821 |
||
822 |
static void UpdateTownMaxPass(Town *t) |
|
823 |
{ |
|
824 |
t->max_pass = t->population >> 3; |
|
825 |
t->max_mail = t->population >> 4; |
|
826 |
} |
|
827 |
||
828 |
static void DoCreateTown(Town *t, TileIndex tile) |
|
829 |
{ |
|
830 |
int x, i; |
|
831 |
||
832 |
// clear the town struct |
|
833 |
i = t->index; |
|
834 |
memset(t, 0, sizeof(Town)); |
|
835 |
t->index = i; |
|
836 |
||
837 |
t->xy = tile; |
|
838 |
t->num_houses = 0; |
|
839 |
t->time_until_rebuild = 10; |
|
840 |
UpdateTownRadius(t); |
|
841 |
t->flags12 = 0; |
|
842 |
t->population = 0; |
|
843 |
t->grow_counter = 0; |
|
844 |
t->growth_rate = 250; |
|
845 |
t->new_max_pass = 0; |
|
846 |
t->new_max_mail = 0; |
|
847 |
t->new_act_pass = 0; |
|
848 |
t->new_act_mail = 0; |
|
849 |
t->max_pass = 0; |
|
850 |
t->max_mail = 0; |
|
851 |
t->act_pass = 0; |
|
852 |
t->act_mail = 0; |
|
853 |
||
854 |
t->pct_pass_transported = 0; |
|
855 |
t->pct_mail_transported = 0; |
|
856 |
t->fund_buildings_months = 0; |
|
857 |
t->new_act_food = 0; |
|
4
cad62d5f9708
(svn r5) -Fix: townname generation of TTDLX savegames. All work
darkvater
parents:
0
diff
changeset
|
858 |
t->new_act_water = 0; |
0 | 859 |
t->act_food = 0; |
4
cad62d5f9708
(svn r5) -Fix: townname generation of TTDLX savegames. All work
darkvater
parents:
0
diff
changeset
|
860 |
t->act_water = 0; |
0 | 861 |
|
862 |
for(i = 0; i != MAX_PLAYERS; i++) |
|
863 |
t->ratings[i] = 500; |
|
864 |
||
865 |
t->have_ratings = 0; |
|
121
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
39
diff
changeset
|
866 |
t->exclusivity = (byte)-1; |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
39
diff
changeset
|
867 |
t->exclusive_counter = 0; |
0 | 868 |
t->statues = 0; |
869 |
||
870 |
CreateTownName(t); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
871 |
|
0 | 872 |
UpdateTownVirtCoord(t); |
873 |
_town_sort_dirty = true; |
|
874 |
||
875 |
x = (Random() & 0xF) + 8; |
|
876 |
if (_game_mode == GM_EDITOR) |
|
877 |
x = _new_town_size * 16 + 3; |
|
878 |
||
879 |
t->num_houses += x; |
|
880 |
UpdateTownRadius(t); |
|
881 |
||
882 |
i = x * 4; |
|
883 |
do { |
|
884 |
GrowTown(t); |
|
885 |
} while (--i); |
|
886 |
||
887 |
t->num_houses -= x; |
|
888 |
UpdateTownRadius(t); |
|
889 |
UpdateTownMaxPass(t); |
|
890 |
} |
|
891 |
||
892 |
static Town *AllocateTown() |
|
893 |
{ |
|
894 |
Town *t; |
|
895 |
FOR_ALL_TOWNS(t) { |
|
896 |
if (t->xy == 0) { |
|
897 |
_total_towns++; |
|
898 |
return t; |
|
899 |
} |
|
900 |
} |
|
901 |
return NULL; |
|
902 |
} |
|
903 |
||
904 |
int32 CmdBuildTown(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
905 |
{ |
|
906 |
uint tile = TILE_FROM_XY(x,y); |
|
907 |
TileInfo ti; |
|
908 |
Town *t; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
909 |
|
0 | 910 |
SET_EXPENSES_TYPE(EXPENSES_OTHER); |
911 |
||
912 |
// Check if too close to the edge of map |
|
913 |
if (!CheckDistanceFromEdge(tile, 12)) |
|
914 |
return_cmd_error(STR_0237_TOO_CLOSE_TO_EDGE_OF_MAP); |
|
915 |
||
916 |
// Can only build on clear flat areas. |
|
917 |
FindLandscapeHeightByTile(&ti, tile); |
|
918 |
if (ti.type != MP_CLEAR || ti.tileh != 0) |
|
919 |
return_cmd_error(STR_0239_SITE_UNSUITABLE); |
|
920 |
||
921 |
// Check distance to all other towns. |
|
922 |
if (IsCloseToTown(tile, 20)) |
|
923 |
return_cmd_error(STR_0238_TOO_CLOSE_TO_ANOTHER_TOWN); |
|
924 |
||
925 |
// Allocate town struct |
|
926 |
t = AllocateTown(); |
|
927 |
if (t == NULL) |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
928 |
return_cmd_error(STR_023A_TOO_MANY_TOWNS); |
0 | 929 |
|
930 |
// Create the town |
|
931 |
if (flags & DC_EXEC) { |
|
932 |
_generating_world = true; |
|
933 |
DoCreateTown(t, tile); |
|
934 |
_generating_world = false; |
|
935 |
} |
|
936 |
return 0; |
|
937 |
} |
|
938 |
||
939 |
Town *CreateRandomTown() |
|
940 |
{ |
|
941 |
uint tile; |
|
942 |
TileInfo ti; |
|
943 |
Town *t; |
|
944 |
int n; |
|
945 |
||
946 |
// Try 20 times. |
|
947 |
n = 20; |
|
948 |
do { |
|
949 |
// Generate a tile index not too close from the edge |
|
950 |
tile = TILE_MASK(Random()); |
|
951 |
if (!CheckDistanceFromEdge(tile, 20)) |
|
952 |
continue; |
|
953 |
||
954 |
// Make sure the tile is plain |
|
955 |
FindLandscapeHeightByTile(&ti, tile); |
|
956 |
if (ti.type != MP_CLEAR || ti.tileh != 0) |
|
957 |
continue; |
|
958 |
||
959 |
// Check not too close to a town |
|
960 |
if (IsCloseToTown(tile, 20)) |
|
961 |
continue; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
962 |
|
0 | 963 |
// Allocate a town struct |
964 |
t = AllocateTown(); |
|
965 |
if (t == NULL) |
|
966 |
break; |
|
967 |
||
968 |
DoCreateTown(t, tile); |
|
969 |
return t; |
|
970 |
} while (--n); |
|
971 |
return NULL; |
|
972 |
} |
|
973 |
||
974 |
static const byte _num_initial_towns[3] = { |
|
975 |
11, 23, 46 |
|
976 |
}; |
|
977 |
||
978 |
void GenerateTowns() |
|
979 |
{ |
|
980 |
uint n; |
|
981 |
n = _num_initial_towns[_opt.diff.number_towns] + (Random()&7); |
|
982 |
do CreateRandomTown(); while (--n); |
|
983 |
} |
|
984 |
||
985 |
static bool CheckBuildHouseMode(Town *t1, uint tile, uint tileh, int mode) { |
|
986 |
Town *t2 = ClosestTownFromTile(tile, (uint)-1); |
|
987 |
int b; |
|
988 |
uint slope; |
|
989 |
||
990 |
static const byte _masks[8] = { |
|
991 |
0xC,0x3,0x9,0x6, |
|
992 |
0x3,0xC,0x6,0x9, |
|
993 |
}; |
|
994 |
||
995 |
if (t2 != t1) |
|
996 |
return false; |
|
997 |
||
998 |
slope = GetTileSlope(tile, NULL); |
|
999 |
if (slope & 0x10) |
|
1000 |
return false; |
|
1001 |
||
1002 |
b = 0; |
|
1003 |
if ((slope & 0xF && ~slope & _masks[mode])) b = ~b; |
|
1004 |
if ((tileh & 0xF && ~tileh & _masks[mode+4])) b = ~b; |
|
1005 |
if (b) |
|
1006 |
return false; |
|
1007 |
||
1008 |
return DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR) != CMD_ERROR; |
|
1009 |
} |
|
1010 |
||
1011 |
int GetTownRadiusGroup(Town *t, uint tile) |
|
1012 |
{ |
|
1013 |
uint dist; |
|
1014 |
int i,smallest; |
|
1015 |
||
1016 |
dist = GetTileDistAdv(tile, t->xy); |
|
1017 |
if (t->fund_buildings_months && dist <= 25) |
|
1018 |
return 4; |
|
1019 |
||
1020 |
smallest = 0; |
|
1021 |
for(i=0; i!=lengthof(t->radius); i++) { |
|
1022 |
if (dist < t->radius[i]) |
|
1023 |
smallest = i; |
|
1024 |
} |
|
1025 |
||
1026 |
return smallest; |
|
1027 |
} |
|
1028 |
||
1029 |
static bool CheckFree2x2Area(Town *t1, uint tile) |
|
1030 |
{ |
|
1031 |
Town *t; |
|
1032 |
int i; |
|
1033 |
||
1034 |
static const TileIndexDiff _tile_add[4] = { |
|
1035 |
TILE_XY(0,0), |
|
1036 |
TILE_XY(0,1) - TILE_XY(0,0), |
|
1037 |
TILE_XY(1,0) - TILE_XY(0,1), |
|
1038 |
TILE_XY(1,1) - TILE_XY(1,0), |
|
1039 |
}; |
|
1040 |
||
1041 |
for(i=0; i!=4; i++) { |
|
1042 |
tile += _tile_add[i]; |
|
1043 |
||
1044 |
t = ClosestTownFromTile(tile, (uint)-1); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1045 |
if (t1 != t) |
0 | 1046 |
return false; |
1047 |
||
1048 |
if (GetTileSlope(tile, NULL)) |
|
1049 |
return false; |
|
1050 |
||
1051 |
if (DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER | DC_FORCETEST, CMD_LANDSCAPE_CLEAR) == CMD_ERROR) |
|
1052 |
return false; |
|
1053 |
} |
|
1054 |
||
1055 |
return true; |
|
1056 |
} |
|
1057 |
||
1058 |
static void DoBuildTownHouse(Town *t, uint tile) |
|
1059 |
{ |
|
1060 |
int i; |
|
1061 |
uint bitmask; |
|
1062 |
int house; |
|
1063 |
uint slope; |
|
1064 |
int z; |
|
1065 |
uint oneof; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1066 |
|
0 | 1067 |
// Above snow? |
1068 |
slope = GetTileSlope(tile, &z); |
|
1069 |
||
1070 |
// Get the town zone type |
|
1071 |
{ |
|
1072 |
uint rad = GetTownRadiusGroup(t, tile); |
|
1073 |
||
1074 |
int land = _opt.landscape; |
|
1075 |
if (land == LT_HILLY && z >= _opt.snow_line) |
|
1076 |
land = -1; |
|
1077 |
||
1078 |
bitmask = (1 << rad) + (1 << (land + 12)); |
|
1079 |
} |
|
1080 |
||
1081 |
// bits 0-4 are used |
|
1082 |
// bits 11-15 are used |
|
1083 |
// bits 5-10 are not used. |
|
1084 |
{ |
|
1085 |
byte houses[lengthof(_housetype_flags)]; |
|
1086 |
int num = 0; |
|
1087 |
||
1088 |
// Generate a list of all possible houses that can be built. |
|
1089 |
for(i=0; i!=lengthof(_housetype_flags); i++) { |
|
1090 |
if ((~_housetype_flags[i] & bitmask) == 0) |
|
1091 |
houses[num++] = (byte)i; |
|
1092 |
} |
|
1093 |
||
1094 |
for(;;) { |
|
1095 |
house = houses[RandomRange(num)]; |
|
1096 |
||
1097 |
if (_cur_year < _housetype_years[house].min || _cur_year > _housetype_years[house].max) |
|
1098 |
continue; |
|
1099 |
||
1100 |
// Special houses that there can be only one of. |
|
1101 |
oneof = 0; |
|
1102 |
if (house == 0x5B || house == 0x53 || house == 3 || house == 0x3C || house == 0x3D) |
|
1103 |
oneof = 2; |
|
1104 |
else if (house == 0x20 || house == 0x14) |
|
1105 |
oneof = 4; |
|
1106 |
||
1107 |
if (t->flags12 & oneof) |
|
1108 |
continue; |
|
1109 |
||
1110 |
// Make sure there is no slope? |
|
1111 |
if (_housetype_extra_flags[house]&0x12 && slope) |
|
1112 |
continue; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1113 |
|
0 | 1114 |
if (_housetype_extra_flags[house]&0x10) { |
1115 |
if (CheckFree2x2Area(t,tile) || |
|
1116 |
CheckFree2x2Area(t,(tile+=TILE_XY(-1,0))) || |
|
1117 |
CheckFree2x2Area(t,(tile+=TILE_XY(0,-1))) || |
|
1118 |
CheckFree2x2Area(t,(tile+=TILE_XY(1,0)))) |
|
1119 |
break; |
|
1120 |
tile += TILE_XY(0,1); |
|
1121 |
} else if (_housetype_extra_flags[house]&4) { |
|
1122 |
if (CheckBuildHouseMode(t, tile+TILE_XY(1,0), slope, 0)) |
|
1123 |
break; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1124 |
|
0 | 1125 |
if (CheckBuildHouseMode(t, tile+TILE_XY(-1,0), slope, 1)) { |
1126 |
tile += TILE_XY(-1,0); |
|
1127 |
break; |
|
1128 |
} |
|
1129 |
} else if (_housetype_extra_flags[house]&8) { |
|
1130 |
if (CheckBuildHouseMode(t, tile+TILE_XY(0,1), slope, 2)) |
|
1131 |
break; |
|
1132 |
||
1133 |
if (CheckBuildHouseMode(t, tile+TILE_XY(0,-1), slope, 3)) { |
|
1134 |
tile += TILE_XY(0,-1); |
|
1135 |
break; |
|
1136 |
} |
|
1137 |
} else |
|
1138 |
break; |
|
1139 |
} |
|
1140 |
} |
|
1141 |
||
1142 |
t->num_houses++; |
|
1143 |
||
1144 |
// Special houses that there can be only one of. |
|
1145 |
t->flags12 |= oneof; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1146 |
|
0 | 1147 |
{ |
1148 |
int m3lo,m5,eflags; |
|
1149 |
||
1150 |
// ENDING_2 |
|
1151 |
m3lo = 0; |
|
1152 |
m5 = 0; |
|
1153 |
if (_generating_world) { |
|
1154 |
uint32 r = Random(); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1155 |
|
0 | 1156 |
// Value for map3lo |
1157 |
m3lo = 0xC0; |
|
1158 |
if ((byte)r >= 220) m3lo &= (r>>8); |
|
1159 |
||
1160 |
if (m3lo == 0xC0) |
|
1161 |
ChangePopulation(t, _housetype_population[house]); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1162 |
|
0 | 1163 |
// Initial value for map5. |
1164 |
m5 = (r >> 16) & 0x3F; |
|
1165 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1166 |
|
0 | 1167 |
assert(IS_TILETYPE(tile, MP_CLEAR)); |
1168 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1169 |
ModifyTile(tile, |
0 | 1170 |
MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5 | MP_MAPOWNER, |
1171 |
house, /* map2 */ |
|
1172 |
m3lo, /* map3_lo */ |
|
1173 |
0, /* map_owner */ |
|
1174 |
m5 /* map5 */ |
|
1175 |
); |
|
1176 |
||
1177 |
eflags = _housetype_extra_flags[house]; |
|
1178 |
||
1179 |
if (eflags&0x18) { |
|
1180 |
assert(IS_TILETYPE(tile + TILE_XY(0,1), MP_CLEAR)); |
|
1181 |
ModifyTile(tile + TILE_XY(0,1), |
|
1182 |
MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5 | MP_MAPOWNER, |
|
1183 |
++house, /* map2 */ |
|
1184 |
m3lo, /* map3_lo */ |
|
1185 |
0, /* map_owner */ |
|
1186 |
m5 /* map5 */ |
|
1187 |
); |
|
1188 |
} |
|
1189 |
||
1190 |
if (eflags&0x14) { |
|
1191 |
assert(IS_TILETYPE(tile + TILE_XY(1,0), MP_CLEAR)); |
|
1192 |
ModifyTile(tile + TILE_XY(1,0), |
|
1193 |
MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5 | MP_MAPOWNER, |
|
1194 |
++house, /* map2 */ |
|
1195 |
m3lo, /* map3_lo */ |
|
1196 |
0, /* map_owner */ |
|
1197 |
m5 /* map5 */ |
|
1198 |
); |
|
1199 |
} |
|
1200 |
||
1201 |
if (eflags&0x10) { |
|
1202 |
assert(IS_TILETYPE(tile + TILE_XY(1,1), MP_CLEAR)); |
|
1203 |
ModifyTile(tile + TILE_XY(1,1), |
|
1204 |
MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5 | MP_MAPOWNER, |
|
1205 |
++house, /* map2 */ |
|
1206 |
m3lo, /* map3_lo */ |
|
1207 |
0, /* map_owner */ |
|
1208 |
m5 /* map5 */ |
|
1209 |
); |
|
1210 |
} |
|
1211 |
} |
|
1212 |
||
1213 |
// ENDING |
|
1214 |
} |
|
1215 |
||
1216 |
static bool BuildTownHouse(Town *t, uint tile) |
|
1217 |
{ |
|
1218 |
int32 r; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1219 |
|
0 | 1220 |
// make sure it's possible |
1221 |
if (!EnsureNoVehicle(tile)) return false; |
|
1222 |
if (GetTileSlope(tile, NULL) & 0x10) return false; |
|
1223 |
||
1224 |
r = DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR); |
|
1225 |
if (r == CMD_ERROR) return false; |
|
1226 |
||
1227 |
DoBuildTownHouse(t, tile); |
|
1228 |
return true; |
|
1229 |
} |
|
1230 |
||
1231 |
||
1232 |
static void DoClearTownHouseHelper(uint tile) |
|
1233 |
{ |
|
1234 |
assert(IS_TILETYPE(tile, MP_HOUSE)); |
|
1235 |
DoClearSquare(tile); |
|
1236 |
DeleteAnimatedTile(tile); |
|
1237 |
} |
|
1238 |
||
1239 |
static void ClearTownHouse(Town *t, uint tile) { |
|
1240 |
uint house = _map2[tile]; |
|
1241 |
uint eflags; |
|
1242 |
||
1243 |
assert(IS_TILETYPE(tile, MP_HOUSE)); |
|
1244 |
||
1245 |
// need to align the tile to point to the upper left corner of the house |
|
1246 |
if (house >= 3) { // house id 0,1,2 MUST be single tile houses, or this code breaks. |
|
1247 |
if (_housetype_extra_flags[house-1] & 0x04) { |
|
1248 |
house--; |
|
1249 |
tile += TILE_XY(-1,0); |
|
1250 |
} else if (_housetype_extra_flags[house-1] & 0x18) { |
|
1251 |
house--; |
|
1252 |
tile += TILE_XY(0,-1); |
|
1253 |
} else if (_housetype_extra_flags[house-2] & 0x10) { |
|
1254 |
house-=2; |
|
1255 |
tile += TILE_XY(-1,0); |
|
1256 |
} else if (_housetype_extra_flags[house-3] & 0x10) { |
|
1257 |
house-=3; |
|
1258 |
tile += TILE_XY(-1,-1); |
|
1259 |
} |
|
1260 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1261 |
|
0 | 1262 |
// Remove population from the town if the |
1263 |
// house is finished. |
|
1264 |
if ((~_map3_lo[tile] & 0xC0) == 0) { |
|
1265 |
ChangePopulation(t, -_housetype_population[house]); |
|
1266 |
} |
|
1267 |
||
1268 |
t->num_houses--; |
|
1269 |
||
1270 |
// Clear flags for houses that only may exist once/town. |
|
1271 |
if (house == 0x5B || house == 0x53 || house == 0x3C || |
|
1272 |
house == 0x3D || house == 0x03) |
|
1273 |
t->flags12 &= ~2; |
|
1274 |
if (house == 0x14 || house == 0x20) |
|
1275 |
t->flags12 &= ~4; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1276 |
|
0 | 1277 |
// Do the actual clearing of tiles |
1278 |
eflags = _housetype_extra_flags[house]; |
|
1279 |
DoClearTownHouseHelper(tile); |
|
1280 |
if (eflags & 0x14) DoClearTownHouseHelper(tile + TILE_XY(1,0)); |
|
1281 |
if (eflags & 0x18) DoClearTownHouseHelper(tile + TILE_XY(0,1)); |
|
1282 |
if (eflags & 0x10) DoClearTownHouseHelper(tile + TILE_XY(1,1)); |
|
1283 |
} |
|
1284 |
||
1285 |
int32 CmdRenameTown(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
1286 |
{ |
|
1287 |
StringID str; |
|
1288 |
Town *t = DEREF_TOWN(p1); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1289 |
|
0 | 1290 |
str = AllocateName((byte*)_decode_parameters, 4); |
1291 |
if (str == 0) |
|
1292 |
return CMD_ERROR; |
|
1293 |
||
1294 |
if (flags & DC_EXEC) { |
|
1295 |
StringID old_str = t->townnametype; |
|
1296 |
t->townnametype = str; |
|
1297 |
DeleteName(old_str); |
|
1298 |
||
1299 |
UpdateTownVirtCoord(t); |
|
1300 |
_town_sort_dirty = true; |
|
1301 |
UpdateAllStationVirtCoord(); |
|
1302 |
MarkWholeScreenDirty(); |
|
1303 |
} else { |
|
1304 |
DeleteName(str); |
|
1305 |
} |
|
1306 |
return 0; |
|
1307 |
} |
|
1308 |
||
1309 |
// Called from GUI |
|
1310 |
void DeleteTown(Town *t) |
|
1311 |
{ |
|
1312 |
Industry *i; |
|
1313 |
uint tile; |
|
1314 |
||
1315 |
// Delete town authority window |
|
1316 |
// and remove from list of sorted towns |
|
1317 |
DeleteWindowById(WC_TOWN_VIEW, t->index); |
|
1318 |
_town_sort_dirty = true; |
|
1319 |
||
1320 |
// Delete all industries belonging to the town |
|
1321 |
for(i=_industries; i != endof(_industries); i++) { |
|
1322 |
if (i->xy && i->town == t) |
|
1323 |
DeleteIndustry(i); |
|
1324 |
} |
|
1325 |
||
1326 |
// Go through all tiles and delete those belonging to the town |
|
1327 |
tile = 0; |
|
1328 |
do { |
|
1329 |
if (IS_TILETYPE(tile, MP_HOUSE)) { |
|
1330 |
if (ClosestTownFromTile(tile, (uint)-1) == t) { |
|
1331 |
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
|
1332 |
} |
|
1333 |
} else if (IS_TILETYPE(tile, MP_TUNNELBRIDGE) || IS_TILETYPE(tile, MP_STREET)) { |
|
1334 |
if (_map_owner[tile] == OWNER_TOWN && ClosestTownFromTile(tile, (uint)-1) == t) { |
|
1335 |
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
|
1336 |
} |
|
1337 |
} |
|
1338 |
} while (++tile != TILES_X * TILES_Y); |
|
1339 |
||
1340 |
t->xy = 0; |
|
1341 |
DeleteName(t->townnametype); |
|
1342 |
||
1343 |
MarkWholeScreenDirty(); |
|
1344 |
} |
|
1345 |
||
1346 |
// Called from GUI |
|
1347 |
void ExpandTown(Town *t) |
|
1348 |
{ |
|
1349 |
int amount, n; |
|
1350 |
||
1351 |
_generating_world = true; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1352 |
|
0 | 1353 |
amount = ((int)Random()&3) + 3; |
1354 |
t->num_houses += amount; |
|
1355 |
UpdateTownRadius(t); |
|
1356 |
||
1357 |
n = amount * 4; |
|
1358 |
do GrowTown(t); while (--n); |
|
1359 |
||
1360 |
t->num_houses -= amount; |
|
1361 |
UpdateTownRadius(t); |
|
1362 |
||
1363 |
UpdateTownMaxPass(t); |
|
1364 |
_generating_world = false; |
|
1365 |
} |
|
1366 |
||
1367 |
const byte _town_action_costs[8] = { |
|
1368 |
2, 4, 9, 35, 48, 53, 117, 175 |
|
1369 |
}; |
|
1370 |
||
1371 |
typedef void TownActionProc(Town *t, int action); |
|
1372 |
||
1373 |
static void TownActionAdvertise(Town *t, int action) |
|
1374 |
{ |
|
1375 |
static const byte _advertising_amount[3] = {0x40, 0x70, 0xA0}; |
|
1376 |
static const byte _advertising_radius[3] = {10,15,20}; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1377 |
ModifyStationRatingAround(t->xy, _current_player, |
0 | 1378 |
_advertising_amount[action], |
1379 |
_advertising_radius[action]); |
|
1380 |
} |
|
1381 |
||
1382 |
static void TownActionRoadRebuild(Town *t, int action) |
|
1383 |
{ |
|
1384 |
Player *p; |
|
1385 |
||
1386 |
t->road_build_months = 6; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1387 |
|
0 | 1388 |
SET_DPARAM16(0, t->index); |
1389 |
||
1390 |
p = DEREF_PLAYER(_current_player); |
|
1391 |
SET_DPARAM16(1, p->name_1); |
|
1392 |
SET_DPARAM32(2, p->name_2); |
|
1393 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1394 |
AddNewsItem(STR_2055_TRAFFIC_CHAOS_IN_ROAD_REBUILDING, |
0 | 1395 |
NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_GENERAL, 0), t->xy, 0); |
1396 |
} |
|
1397 |
||
1398 |
static bool DoBuildStatueOfCompany(uint tile) |
|
1399 |
{ |
|
1400 |
TileInfo ti; |
|
1401 |
byte old; |
|
1402 |
int32 r; |
|
1403 |
||
1404 |
FindLandscapeHeightByTile(&ti, tile); |
|
1405 |
if (ti.tileh != 0) |
|
1406 |
return false; |
|
1407 |
||
1408 |
if (ti.type != MP_HOUSE && ti.type != MP_CLEAR && ti.type != MP_TREES) |
|
1409 |
return false; |
|
1410 |
||
1411 |
||
1412 |
old = _current_player; |
|
1413 |
_current_player = OWNER_NONE; |
|
1414 |
r = DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
|
1415 |
_current_player = old; |
|
1416 |
||
1417 |
if (r == CMD_ERROR) |
|
1418 |
return false; |
|
1419 |
||
1420 |
ModifyTile(tile, MP_SETTYPE(MP_UNMOVABLE) | MP_MAPOWNER_CURRENT | MP_MAP5, |
|
1421 |
2 /* map5 */ |
|
1422 |
); |
|
1423 |
||
1424 |
return true; |
|
1425 |
} |
|
1426 |
||
1427 |
static void TownActionBuildStatue(Town *t, int action) |
|
1428 |
{ |
|
1429 |
// Layouted as an outward spiral |
|
1430 |
static const TileIndexDiff _statue_tiles[] = { |
|
1431 |
TILE_XY(-1,0), TILE_XY(0,1), TILE_XY(1,0), TILE_XY(1,0), |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1432 |
TILE_XY(0,-1), TILE_XY(0,-1), TILE_XY(-1,0), TILE_XY(-1,0), |
0 | 1433 |
TILE_XY(-1,0), TILE_XY(0,1), TILE_XY(0,1), TILE_XY(0,1), |
1434 |
TILE_XY(1,0), TILE_XY(1,0), TILE_XY(1,0), TILE_XY(1,0), |
|
1435 |
TILE_XY(0,-1), TILE_XY(0,-1), TILE_XY(0,-1), TILE_XY(0,-1), |
|
1436 |
TILE_XY(-1,0), TILE_XY(-1,0), TILE_XY(-1,0), TILE_XY(-1,0), |
|
1437 |
TILE_XY(-1,0), TILE_XY(0,1), TILE_XY(0,1), TILE_XY(0,1), |
|
1438 |
TILE_XY(0,1), TILE_XY(0,1), TILE_XY(1,0), TILE_XY(1,0), |
|
1439 |
TILE_XY(1,0), TILE_XY(1,0), TILE_XY(1,0), TILE_XY(1,0), |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1440 |
TILE_XY(0,-1), TILE_XY(0,-1), TILE_XY(0,-1), TILE_XY(0,-1), |
0 | 1441 |
TILE_XY(0,-1), TILE_XY(0,-1), TILE_XY(-1,0), TILE_XY(-1,0), |
1442 |
TILE_XY(-1,0), TILE_XY(-1,0), TILE_XY(-1,0), TILE_XY(-1,0), |
|
1443 |
TILE_XY(-1,0), TILE_XY(0,1), TILE_XY(0,1), TILE_XY(0,1), |
|
1444 |
TILE_XY(0,1), TILE_XY(0,1), TILE_XY(0,1), TILE_XY(0,1), |
|
1445 |
TILE_XY(1,0), TILE_XY(1,0), TILE_XY(1,0), TILE_XY(1,0), |
|
1446 |
TILE_XY(1,0), TILE_XY(1,0), TILE_XY(1,0), TILE_XY(1,0), |
|
1447 |
TILE_XY(0,-1), TILE_XY(0,-1), TILE_XY(0,-1), TILE_XY(0,-1), |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1448 |
TILE_XY(0,-1), TILE_XY(0,-1), TILE_XY(0,-1), TILE_XY(0,-1), |
0 | 1449 |
TILE_XY(-1,0), TILE_XY(-1,0), TILE_XY(-1,0), TILE_XY(-1,0), |
1450 |
TILE_XY(-1,0), TILE_XY(-1,0), TILE_XY(-1,0), TILE_XY(-1,0), |
|
1451 |
0, |
|
1452 |
}; |
|
1453 |
int offs; |
|
1454 |
uint tile = t->xy; |
|
1455 |
const TileIndexDiff *p = _statue_tiles; |
|
1456 |
||
1457 |
SETBIT(t->statues, _current_player); |
|
1458 |
||
1459 |
do { |
|
1460 |
if (DoBuildStatueOfCompany(tile)) |
|
1461 |
return; |
|
1462 |
offs = *p++; |
|
1463 |
tile = TILE_ADD(tile, offs); |
|
1464 |
} while (offs); |
|
1465 |
} |
|
1466 |
||
1467 |
static void TownActionFundBuildings(Town *t, int action) |
|
1468 |
{ |
|
1469 |
t->grow_counter = 1; |
|
1470 |
t->flags12 |= 1; |
|
1471 |
t->fund_buildings_months = 3; |
|
1472 |
} |
|
1473 |
||
1474 |
static void TownActionBuyRights(Town *t, int action) |
|
1475 |
{ |
|
121
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
39
diff
changeset
|
1476 |
t->exclusive_counter = 12; |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
39
diff
changeset
|
1477 |
t->exclusivity = _current_player; |
0 | 1478 |
|
1479 |
ModifyStationRatingAround(t->xy, _current_player, 130, 17); |
|
1480 |
} |
|
1481 |
||
1482 |
static void TownActionBribe(Town *t, int action) |
|
1483 |
{ |
|
1484 |
if (!RandomRange(15)) { |
|
1485 |
GoodsEntry *ge; |
|
1486 |
Station *st; |
|
1487 |
int i, rating; |
|
1488 |
||
1489 |
// set as unwanted for 6 months |
|
1490 |
t->unwanted[_current_player] = 6; |
|
1491 |
||
1492 |
// set all close by station ratings to 0 |
|
1493 |
FOR_ALL_STATIONS(st) { |
|
1494 |
if (st->town == t && st->owner == _current_player) { |
|
1495 |
for (i=0, ge = st->goods; i != NUM_CARGO; i++, ge++) |
|
1496 |
ge->rating = 0; |
|
1497 |
} |
|
1498 |
} |
|
1499 |
||
1500 |
// only show errormessage to the executing player. All errors are handled command.c |
|
1501 |
// but this is special, because it can only 'fail' on a DC_EXEC |
|
1502 |
if (!_networking || (_current_player == _local_player)) |
|
1503 |
ShowErrorMessage(STR_BRIBE_FAILED_2, STR_BRIBE_FAILED, 0, 0); |
|
1504 |
||
1505 |
/* decrease by a lot! |
|
1506 |
* ChangeTownRating is only for stuff in demolishing. Bribe failure should |
|
1507 |
* be independent of any cheat settings |
|
1508 |
* ChangeTownRating(c, -1000, -50); |
|
1509 |
*/ |
|
1510 |
rating = t->ratings[_current_player]; |
|
1511 |
if (rating > -50) |
|
1512 |
t->ratings[_current_player] = -50; |
|
1513 |
} else { |
|
1514 |
ChangeTownRating(t, 200, 800); |
|
1515 |
} |
|
1516 |
} |
|
1517 |
||
1518 |
static TownActionProc * const _town_action_proc[] = { |
|
1519 |
TownActionAdvertise, |
|
1520 |
TownActionAdvertise, |
|
1521 |
TownActionAdvertise, |
|
1522 |
TownActionRoadRebuild, |
|
1523 |
TownActionBuildStatue, |
|
1524 |
TownActionFundBuildings, |
|
1525 |
TownActionBuyRights, |
|
1526 |
TownActionBribe |
|
1527 |
}; |
|
1528 |
||
1529 |
// p1 = town |
|
1530 |
// p2 = action |
|
1531 |
int32 CmdDoTownAction(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
1532 |
{ |
|
1533 |
int32 cost; |
|
1534 |
||
1535 |
SET_EXPENSES_TYPE(EXPENSES_OTHER); |
|
1536 |
||
1537 |
cost = (_price.build_industry >> 8) * _town_action_costs[p2]; |
|
1538 |
||
1539 |
if (flags & DC_EXEC) { |
|
1540 |
_town_action_proc[p2](DEREF_TOWN(p1), p2); |
|
1541 |
InvalidateWindow(WC_TOWN_AUTHORITY, p1); |
|
1542 |
} |
|
1543 |
||
1544 |
return cost; |
|
1545 |
} |
|
1546 |
||
1547 |
static void UpdateTownGrowRate(Town *t) |
|
1548 |
{ |
|
1549 |
int n; |
|
1550 |
Station *st; |
|
1551 |
byte m; |
|
1552 |
Player *p; |
|
1553 |
||
1554 |
// Reset player ratings if they're low |
|
1555 |
FOR_ALL_PLAYERS(p) { |
|
1556 |
if (p->is_active && t->ratings[p->index] <= 200) { |
|
1557 |
t->ratings[p->index] += 5; |
|
1558 |
} |
|
1559 |
} |
|
1560 |
||
1561 |
n = 0; |
|
1562 |
FOR_ALL_STATIONS(st) { |
|
1563 |
if (GetTileDistAdv(st->xy, t->xy) <= t->radius[0]) { |
|
1564 |
if (st->time_since_load <= 20 || st->time_since_unload <= 20) { |
|
1565 |
n++; |
|
1566 |
if (st->owner < MAX_PLAYERS && t->ratings[st->owner] <= 1000-12) |
|
1567 |
t->ratings[st->owner] += 12; |
|
1568 |
} else { |
|
1569 |
if (st->owner < MAX_PLAYERS && t->ratings[st->owner] >= -1000+15) |
|
1570 |
t->ratings[st->owner] -= 15; |
|
1571 |
} |
|
1572 |
} |
|
1573 |
} |
|
1574 |
||
1575 |
t->flags12 &= ~1; |
|
1576 |
||
1577 |
if (t->fund_buildings_months != 0) { |
|
1578 |
t->fund_buildings_months--; |
|
1579 |
m = 60; |
|
1580 |
} else if (n == 0) { |
|
1581 |
m = 160; |
|
1582 |
if (!CHANCE16(1, 12)) |
|
1583 |
return; |
|
1584 |
} else { |
|
1585 |
static const byte _grow_count_values[5] = { |
|
1586 |
210, 150, 110, 80, 50 |
|
1587 |
}; |
|
1588 |
m = _grow_count_values[min(n, 5) - 1]; |
|
1589 |
} |
|
1590 |
||
1591 |
if (_opt.landscape == LT_HILLY) { |
|
1592 |
if (GET_TILEHEIGHT(t->xy) >= _opt.snow_line && t->act_food == 0) |
|
1593 |
return; |
|
1594 |
} else if (_opt.landscape == LT_DESERT) { |
|
4
cad62d5f9708
(svn r5) -Fix: townname generation of TTDLX savegames. All work
darkvater
parents:
0
diff
changeset
|
1595 |
if (GetMapExtraBits(t->xy) == 1 && (t->act_food==0 || t->act_water==0)) |
0 | 1596 |
return; |
1597 |
} |
|
1598 |
||
1599 |
t->growth_rate = m; |
|
1600 |
if (m <= t->grow_counter) |
|
1601 |
t->grow_counter = m; |
|
1602 |
||
1603 |
t->flags12 |= 1; |
|
1604 |
} |
|
1605 |
||
1606 |
static void UpdateTownAmounts(Town *t) |
|
1607 |
{ |
|
1608 |
// Using +1 here to prevent overflow and division by zero |
|
1609 |
t->pct_pass_transported = t->new_act_pass * 256 / (t->new_max_pass + 1); |
|
1610 |
||
1611 |
t->max_pass = t->new_max_pass; t->new_max_pass = 0; |
|
1612 |
t->act_pass = t->new_act_pass; t->new_act_pass = 0; |
|
1613 |
t->act_food = t->new_act_food; t->new_act_food = 0; |
|
4
cad62d5f9708
(svn r5) -Fix: townname generation of TTDLX savegames. All work
darkvater
parents:
0
diff
changeset
|
1614 |
t->act_water = t->new_act_water; t->new_act_water = 0; |
0 | 1615 |
|
1616 |
// Using +1 here to prevent overflow and division by zero |
|
1617 |
t->pct_mail_transported = t->new_act_mail * 256 / (t->new_max_mail + 1); |
|
1618 |
t->max_mail = t->new_max_mail; t->new_max_mail = 0; |
|
1619 |
t->act_mail = t->new_act_mail; t->new_act_mail = 0; |
|
1620 |
||
1621 |
InvalidateWindow(WC_TOWN_VIEW, t->index); |
|
1622 |
} |
|
1623 |
||
1624 |
static void UpdateTownUnwanted(Town *t) |
|
1625 |
{ |
|
1626 |
Player *p; |
|
1627 |
||
1628 |
FOR_ALL_PLAYERS(p) { |
|
1629 |
if (t->unwanted[p->index] > 0) |
|
1630 |
t->unwanted[p->index]--; |
|
1631 |
} |
|
1632 |
} |
|
1633 |
||
1634 |
bool CheckIfAuthorityAllows(uint tile) |
|
1635 |
{ |
|
1636 |
Town *t; |
|
1637 |
||
1638 |
if (_current_player >= MAX_PLAYERS) |
|
1639 |
return true; |
|
1640 |
||
1641 |
t = ClosestTownFromTile(tile, _patches.dist_local_authority); |
|
1642 |
if (t == NULL) |
|
1643 |
return true; |
|
1644 |
||
1645 |
if (t->ratings[_current_player] > -200) |
|
1646 |
return true; |
|
1647 |
||
1648 |
_error_message = STR_2009_LOCAL_AUTHORITY_REFUSES; |
|
1649 |
SET_DPARAM16(0, t->index); |
|
1650 |
||
1651 |
return false; |
|
1652 |
} |
|
1653 |
||
1654 |
||
1655 |
Town *ClosestTownFromTile(uint tile, uint threshold) |
|
1656 |
{ |
|
1657 |
Town *t; |
|
1658 |
uint dist, best = threshold; |
|
1659 |
Town *best_town = NULL; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1660 |
|
0 | 1661 |
FOR_ALL_TOWNS(t) { |
1662 |
if (t->xy != 0) { |
|
1663 |
dist = GetTileDist(tile, t->xy); |
|
1664 |
if (dist < best) { |
|
1665 |
best = dist; |
|
1666 |
best_town = t; |
|
1667 |
} |
|
1668 |
} |
|
1669 |
} |
|
1670 |
||
1671 |
return best_town; |
|
1672 |
} |
|
1673 |
||
1674 |
void ChangeTownRating(Town *t, int add, int max) |
|
1675 |
{ |
|
1676 |
int rating; |
|
1677 |
||
1678 |
// if magic_bulldozer cheat is active, town doesn't penaltize for removing stuff |
|
1679 |
if (t == NULL || _current_player >= MAX_PLAYERS || (_cheats.magic_bulldozer.value && add < 0) ) |
|
1680 |
return; |
|
1681 |
||
1682 |
SETBIT(t->have_ratings, _current_player); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1683 |
|
0 | 1684 |
rating = t->ratings[_current_player]; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1685 |
|
0 | 1686 |
if (add < 0) { |
1687 |
if (rating > max) { |
|
1688 |
rating += add; |
|
1689 |
if (rating < max) rating = max; |
|
1690 |
} |
|
1691 |
} else { |
|
1692 |
if (rating < max) { |
|
1693 |
rating += add; |
|
1694 |
if (rating > max) rating = max; |
|
1695 |
} |
|
1696 |
} |
|
1697 |
t->ratings[_current_player] = rating; |
|
1698 |
} |
|
1699 |
||
1700 |
/* penalty for removing town-owned stuff */ |
|
1701 |
static const int _default_rating_settings [3][3] = { |
|
1702 |
// ROAD_REMOVE, TUNNELBRIDGE_REMOVE, INDUSTRY_REMOVE |
|
1703 |
{ 0, 128, 384}, // Permissive |
|
1704 |
{ 48, 192, 480}, // Neutral |
|
1705 |
{ 96, 384, 768}, // Hostile |
|
1706 |
}; |
|
1707 |
||
1708 |
bool CheckforTownRating(uint tile, uint32 flags, Town *t, byte type) |
|
1709 |
{ |
|
1710 |
int modemod; |
|
1711 |
||
1712 |
// if magic_bulldozer cheat is active, town doesn't restrict your destructive actions |
|
1713 |
if (t == NULL || _current_player >= MAX_PLAYERS || _cheats.magic_bulldozer.value) |
|
1714 |
return true; |
|
1715 |
||
1716 |
/* check if you're allowed to remove the street/bridge/tunnel/industry |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1717 |
* owned by a town no removal if rating is lower than ... depends now on |
0 | 1718 |
* difficulty setting. Minimum town rating selected by difficulty level |
1719 |
*/ |
|
1720 |
modemod = _default_rating_settings[_opt_mod_ptr->diff.town_council_tolerance][type]; |
|
1721 |
||
1722 |
if (t->ratings[_current_player] < 16 + modemod && !(flags & DC_NO_TOWN_RATING)) { |
|
1723 |
SET_DPARAM16(0, t->index); |
|
1724 |
_error_message = STR_2009_LOCAL_AUTHORITY_REFUSES; |
|
1725 |
return false; |
|
1726 |
} |
|
1727 |
||
1728 |
return true; |
|
1729 |
} |
|
1730 |
||
1731 |
void TownsMonthlyLoop() |
|
1732 |
{ |
|
1733 |
Town *t; |
|
1734 |
||
1735 |
FOR_ALL_TOWNS(t) if (t->xy != 0) { |
|
1736 |
if (t->road_build_months != 0) |
|
1737 |
t->road_build_months--; |
|
1738 |
||
121
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
39
diff
changeset
|
1739 |
if (t->exclusive_counter != 0) |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
39
diff
changeset
|
1740 |
if(--t->exclusive_counter==0) |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
39
diff
changeset
|
1741 |
t->exclusivity = (byte)-1; |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
39
diff
changeset
|
1742 |
|
0 | 1743 |
UpdateTownGrowRate(t); |
1744 |
UpdateTownAmounts(t); |
|
1745 |
UpdateTownUnwanted(t); |
|
1746 |
} |
|
1747 |
} |
|
1748 |
||
1749 |
void InitializeTowns() |
|
1750 |
{ |
|
1751 |
Subsidy *s; |
|
1752 |
int i; |
|
1753 |
||
1754 |
memset(_towns, 0, sizeof(_towns)); |
|
1755 |
for(i=0; i!=lengthof(_towns); i++) |
|
1756 |
_towns[i].index = i; |
|
1757 |
||
1758 |
memset(_subsidies, 0, sizeof(_subsidies)); |
|
1759 |
for (s=_subsidies; s != endof(_subsidies); s++) |
|
1760 |
s->cargo_type = 0xFF; |
|
1761 |
||
1762 |
_cur_town_ctr = 0; |
|
1763 |
_town_sort_dirty = true; |
|
1764 |
_total_towns = 0; |
|
1765 |
} |
|
1766 |
||
1767 |
const TileTypeProcs _tile_type_town_procs = { |
|
1768 |
DrawTile_Town, /* draw_tile_proc */ |
|
1769 |
GetSlopeZ_Town, /* get_slope_z_proc */ |
|
1770 |
ClearTile_Town, /* clear_tile_proc */ |
|
1771 |
GetAcceptedCargo_Town, /* get_accepted_cargo_proc */ |
|
1772 |
GetTileDesc_Town, /* get_tile_desc_proc */ |
|
1773 |
GetTileTrackStatus_Town, /* get_tile_track_status_proc */ |
|
1774 |
ClickTile_Town, /* click_tile_proc */ |
|
1775 |
AnimateTile_Town, /* animate_tile_proc */ |
|
1776 |
TileLoop_Town, /* tile_loop_clear */ |
|
1777 |
ChangeTileOwner_Town, /* change_tile_owner_clear */ |
|
1778 |
NULL, /* get_produced_cargo_proc */ |
|
1779 |
NULL, /* vehicle_enter_tile_proc */ |
|
1780 |
NULL, /* vehicle_leave_tile_proc */ |
|
39 | 1781 |
GetSlopeTileh_Town, /* get_slope_tileh_proc */ |
0 | 1782 |
}; |
1783 |
||
1784 |
||
1785 |
// Save and load of towns. |
|
1786 |
static const byte _town_desc[] = { |
|
1787 |
SLE_VAR(Town,xy, SLE_UINT16), |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
159
diff
changeset
|
1788 |
|
0 | 1789 |
SLE_CONDVAR(Town,population, SLE_FILE_U16 | SLE_VAR_U32, 0, 2), |
1790 |
SLE_CONDVAR(Town,population, SLE_UINT32, 3, 255), |
|
1791 |
||
1792 |
||
1793 |
SLE_VAR(Town,num_houses, SLE_UINT16), |
|
1794 |
SLE_VAR(Town,townnametype,SLE_UINT16), |
|
1795 |
SLE_VAR(Town,townnameparts,SLE_UINT32), |
|
1796 |
||
1797 |
SLE_VAR(Town,flags12, SLE_UINT8), |
|
1798 |
SLE_VAR(Town,statues, SLE_UINT8), |
|
1799 |
||
1800 |
// sort_index_obsolete was stored here in savegame format 0 - 1 |
|
1801 |
SLE_CONDARR(NullStruct,null,SLE_FILE_U8 | SLE_VAR_NULL, 1, 0, 1), |
|
1802 |
||
1803 |
SLE_VAR(Town,have_ratings,SLE_UINT8), |
|
1804 |
SLE_ARR(Town,ratings, SLE_INT16, 8), |
|
1805 |
// failed bribe attempts are stored since savegame format 4 |
|
1806 |
SLE_CONDARR(Town,unwanted, SLE_INT8, 8, 4,255), |
|
1807 |
||
1808 |
SLE_VAR(Town,max_pass, SLE_UINT16), |
|
1809 |
SLE_VAR(Town,max_mail, SLE_UINT16), |
|
1810 |
SLE_VAR(Town,new_max_pass,SLE_UINT16), |
|
1811 |
SLE_VAR(Town,new_max_mail,SLE_UINT16), |
|
1812 |
SLE_VAR(Town,act_pass, SLE_UINT16), |
|
1813 |
SLE_VAR(Town,act_mail, SLE_UINT16), |
|
1814 |
SLE_VAR(Town,new_act_pass,SLE_UINT16), |
|
1815 |
SLE_VAR(Town,new_act_mail,SLE_UINT16), |
|
1816 |
||
1817 |
SLE_VAR(Town,pct_pass_transported,SLE_UINT8), |
|
1818 |
SLE_VAR(Town,pct_mail_transported,SLE_UINT8), |
|
1819 |
||
1820 |
SLE_VAR(Town,act_food, SLE_UINT16), |
|
4
cad62d5f9708
(svn r5) -Fix: townname generation of TTDLX savegames. All work
darkvater
parents:
0
diff
changeset
|
1821 |
SLE_VAR(Town,act_water, SLE_UINT16), |
0 | 1822 |
SLE_VAR(Town,new_act_food,SLE_UINT16), |
4
cad62d5f9708
(svn r5) -Fix: townname generation of TTDLX savegames. All work
darkvater
parents:
0
diff
changeset
|
1823 |
SLE_VAR(Town,new_act_water,SLE_UINT16), |
0 | 1824 |
|
1825 |
SLE_VAR(Town,time_until_rebuild, SLE_UINT8), |
|
1826 |
SLE_VAR(Town,grow_counter, SLE_UINT8), |
|
1827 |
SLE_VAR(Town,growth_rate, SLE_UINT8), |
|
1828 |
SLE_VAR(Town,fund_buildings_months, SLE_UINT8), |
|
1829 |
SLE_VAR(Town,road_build_months, SLE_UINT8), |
|
1830 |
||
121
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
39
diff
changeset
|
1831 |
SLE_VAR(Town,exclusivity, SLE_UINT8), |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
39
diff
changeset
|
1832 |
SLE_VAR(Town,exclusive_counter, SLE_UINT8), |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
39
diff
changeset
|
1833 |
// reserve extra space in savegame here. (currently 30 bytes) |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
39
diff
changeset
|
1834 |
SLE_CONDARR(NullStruct,null,SLE_FILE_U8 | SLE_VAR_NULL, 30, 2, 255), |
0 | 1835 |
|
1836 |
SLE_END() |
|
1837 |
}; |
|
1838 |
||
1839 |
static void Save_TOWN() |
|
1840 |
{ |
|
1841 |
Town *t; |
|
1842 |
||
1843 |
FOR_ALL_TOWNS(t) if (t->xy != 0) { |
|
1844 |
SlSetArrayIndex(t->index); |
|
1845 |
SlObject(t, _town_desc); |
|
1846 |
} |
|
1847 |
} |
|
1848 |
||
1849 |
static void Load_TOWN() |
|
1850 |
{ |
|
1851 |
int index; |
|
1852 |
while ((index = SlIterateArray()) != -1) { |
|
1853 |
Town *t = DEREF_TOWN(index); |
|
1854 |
SlObject(t, _town_desc); |
|
1855 |
_total_towns++; |
|
1856 |
} |
|
1857 |
} |
|
1858 |
||
1859 |
void AfterLoadTown() |
|
1860 |
{ |
|
1861 |
Town *t; |
|
1862 |
FOR_ALL_TOWNS(t) { |
|
1863 |
if (t->xy != 0) { |
|
1864 |
UpdateTownRadius(t); |
|
1865 |
UpdateTownVirtCoord(t); |
|
1866 |
} |
|
1867 |
} |
|
1868 |
_town_sort_dirty = true; |
|
1869 |
} |
|
1870 |
||
1871 |
||
1872 |
const ChunkHandler _town_chunk_handlers[] = { |
|
1873 |
{ 'CITY', Save_TOWN, Load_TOWN, CH_ARRAY | CH_LAST}, |
|
1874 |
}; |
|
1875 |
||
1876 |