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