src/town_cmd.cpp
author michi_cc
Mon, 20 Oct 2008 19:35:48 +0000
changeset 10269 eb8e4225dbc6
parent 10260 c6ec6b3c1b18
child 10289 801e5451f0ca
permissions -rw-r--r--
(svn r14504) -Cleanup: Use the right variable type for tile offsets.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 9071
diff changeset
     3
/** @file town_cmd.cpp Handling of town tiles. */
6201
bee01dc45e39 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6133
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1881
diff changeset
     6
#include "openttd.h"
8119
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8116
diff changeset
     7
#include "tile_cmd.h"
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
     8
#include "debug.h"
3144
33e42feae531 (svn r3763) Adapt to the new 'map accessors go in foo_map.h'-scheme
tron
parents: 3071
diff changeset
     9
#include "road_map.h"
8102
906a3d3b6df1 (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8088
diff changeset
    10
#include "road_internal.h" /* Cleaning up road bits */
6343
76d17f784c13 (svn r9371) -Feature: Add support for variable snow lines in the arctic climate, supplied
maedhros
parents: 6342
diff changeset
    11
#include "landscape.h"
3319
16c0f06829a5 (svn r4085) Add GetTown{Index,ByTile}() to get the town index resp. the town from a tile
tron
parents: 3310
diff changeset
    12
#include "town_map.h"
3154
6ab0cb6b7ab3 (svn r3777) Add some functions to handle tunnels
tron
parents: 3150
diff changeset
    13
#include "tunnel_map.h"
8224
c5a64d87cc54 (svn r11787) -Codechange: more header rewrites. This time related to viewport.h.
rubidium
parents: 8214
diff changeset
    14
#include "viewport_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include "town.h"
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8114
diff changeset
    16
#include "command_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
#include "industry.h"
8785
871586967963 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 8763
diff changeset
    18
#include "station_base.h"
10208
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    19
#include "company_base.h"
8763
81fadd257f67 (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 8707
diff changeset
    20
#include "news_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
#include "saveload.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
#include "gui.h"
3310
a19f247b75b6 (svn r4073) Add functions to make and test for (most) unmovable tiles
tron
parents: 3282
diff changeset
    23
#include "unmovable_map.h"
3433
4e0d2ea104b3 (svn r4262) -Codechange: use IsClearWaterTile instead of some "home-brewn" marco. town_cmd is now map access free
celestar
parents: 3432
diff changeset
    24
#include "water_map.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2150
diff changeset
    25
#include "variables.h"
3359
633290bf1117 (svn r4154) -Moved MAX_BRIDGES in bridge.h and made it an enum. This makes two drops ...
celestar
parents: 3319
diff changeset
    26
#include "bridge.h"
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5378
diff changeset
    27
#include "bridge_map.h"
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4261
diff changeset
    28
#include "genworld.h"
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
    29
#include "newgrf.h"
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
    30
#include "newgrf_callbacks.h"
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
    31
#include "newgrf_house.h"
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents: 6610
diff changeset
    32
#include "newgrf_commons.h"
6956
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6950
diff changeset
    33
#include "newgrf_townname.h"
7582
80c8517b08d1 (svn r11107) -Feature: some tool so one can still build tunnels under rails (and other structures) when the owner of the structure built it on foundations and if you have enough "empty" space ofcourse. One could use the tool for some other construction needs too. Patch by frosch.
rubidium
parents: 7577
diff changeset
    34
#include "autoslope.h"
7709
439dcda6d74b (svn r11243) -Fix: update waypoint signs when renaming a town
glx
parents: 7684
diff changeset
    35
#include "waypoint.h"
7849
0a1c0af2c96c (svn r11399) -Feature(ette): transparency settings can now be saved and thus remembered.
belugas
parents: 7847
diff changeset
    36
#include "transparency.h"
8083
ad22eade501f (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 7969
diff changeset
    37
#include "tunnelbridge_map.h"
8114
dd6d21dc99c1 (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8108
diff changeset
    38
#include "strings_func.h"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8124
diff changeset
    39
#include "window_func.h"
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents: 8197
diff changeset
    40
#include "string_func.h"
8787
41d9d5adbe87 (svn r12495) -Codechange: reduce the dependency on newgrf_station.h (especially because newgrf_station.h includes a lot of stuff).
rubidium
parents: 8785
diff changeset
    41
#include "newgrf_cargo.h"
8847
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents: 8846
diff changeset
    42
#include "oldpool_func.h"
8962
1b263c69799d (svn r12754) -Codechange: split depot.h into depot_map.h, depot_func.h and depot_base.h and remove quite a lot of unneeded (before this) includes of depot.h.
rubidium
parents: 8944
diff changeset
    43
#include "sprite.h"
1b263c69799d (svn r12754) -Codechange: split depot.h into depot_map.h, depot_func.h and depot_base.h and remove quite a lot of unneeded (before this) includes of depot.h.
rubidium
parents: 8944
diff changeset
    44
#include "economy_func.h"
1b263c69799d (svn r12754) -Codechange: split depot.h into depot_map.h, depot_func.h and depot_base.h and remove quite a lot of unneeded (before this) includes of depot.h.
rubidium
parents: 8944
diff changeset
    45
#include "station_func.h"
8965
29a591456a2f (svn r12757) -Codechange: move all cheat related stuff from all over the place to a single location.
rubidium
parents: 8962
diff changeset
    46
#include "cheat_func.h"
9006
7666e7b47088 (svn r12801) -Codechange: remove the dependency of function.h in town_map.h
rubidium
parents: 8970
diff changeset
    47
#include "functions.h"
7666e7b47088 (svn r12801) -Codechange: remove the dependency of function.h in town_map.h
rubidium
parents: 8970
diff changeset
    48
#include "animated_tile_func.h"
7666e7b47088 (svn r12801) -Codechange: remove the dependency of function.h in town_map.h
rubidium
parents: 8970
diff changeset
    49
#include "date_func.h"
9581
0d8f232e1721 (svn r13618) -Codechange: move tar-specific declarations to separate file
smatz
parents: 9522
diff changeset
    50
#include <map>
8083
ad22eade501f (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 7969
diff changeset
    51
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    52
#include "table/strings.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    53
#include "table/sprites.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    54
#include "table/town_land.h"
1260
632f63689178 (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1252
diff changeset
    55
8268
5027ad5e70a0 (svn r11832) -Codechange: get rid of (quite) some VARDEFs.
rubidium
parents: 8264
diff changeset
    56
uint _total_towns;
5027ad5e70a0 (svn r11832) -Codechange: get rid of (quite) some VARDEFs.
rubidium
parents: 8264
diff changeset
    57
HouseSpec _house_specs[HOUSE_MAX];
5027ad5e70a0 (svn r11832) -Codechange: get rid of (quite) some VARDEFs.
rubidium
parents: 8264
diff changeset
    58
5027ad5e70a0 (svn r11832) -Codechange: get rid of (quite) some VARDEFs.
rubidium
parents: 8264
diff changeset
    59
Town *_cleared_town;
5027ad5e70a0 (svn r11832) -Codechange: get rid of (quite) some VARDEFs.
rubidium
parents: 8264
diff changeset
    60
int _cleared_town_rating;
5027ad5e70a0 (svn r11832) -Codechange: get rid of (quite) some VARDEFs.
rubidium
parents: 8264
diff changeset
    61
1260
632f63689178 (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1252
diff changeset
    62
/* Initialize the town-pool */
7386
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    63
DEFINE_OLD_POOL_GENERIC(Town, Town)
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    64
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    65
Town::Town(TileIndex tile)
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    66
{
7410
14dc251e7ddb (svn r10787) -Fix r10755: _total_towns was not increased when placing town manually in scenario editor
glx
parents: 7401
diff changeset
    67
	if (tile != 0) _total_towns++;
7386
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    68
	this->xy = tile;
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    69
}
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    70
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    71
Town::~Town()
4396
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    72
{
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
    73
	free(this->name);
7413
a590f7f0edb3 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7410
diff changeset
    74
a590f7f0edb3 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7410
diff changeset
    75
	if (CleaningPool()) return;
a590f7f0edb3 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7410
diff changeset
    76
4396
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    77
	Industry *i;
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    78
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    79
	/* Delete town authority window
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    80
	 * and remove from list of sorted towns */
7386
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    81
	DeleteWindowById(WC_TOWN_VIEW, this->index);
9387
ede823d445f5 (svn r13297) -Codechange: Use GUIList for the town directory window
peter1138
parents: 9358
diff changeset
    82
	InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 0);
5298
46eabcb5c2b2 (svn r7451) -Fix (7372): GetNum(Towns|Industries) should return the actual number of towns and industries.
rubidium
parents: 5247
diff changeset
    83
	_total_towns--;
4396
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    84
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    85
	/* Delete all industries belonging to the town */
7390
16fb97ba8133 (svn r10759) -Codechange: make the industry struct use the pool item class as super class.
rubidium
parents: 7386
diff changeset
    86
	FOR_ALL_INDUSTRIES(i) if (i->town == this) delete i;
4396
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    87
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    88
	/* Go through all tiles and delete those belonging to the town */
7386
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    89
	for (TileIndex tile = 0; tile < MapSize(); ++tile) {
4396
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    90
		switch (GetTileType(tile)) {
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    91
			case MP_HOUSE:
7386
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
    92
				if (GetTownByTile(tile) == this) DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
4396
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    93
				break;
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    94
7370
41adc721b1fa (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7335
diff changeset
    95
			case MP_ROAD:
4396
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    96
			case MP_TUNNELBRIDGE:
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    97
				if (IsTileOwner(tile, OWNER_TOWN) &&
10236
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10208
diff changeset
    98
						ClosestTownFromTile(tile, UINT_MAX) == this)
4396
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
    99
					DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
   100
				break;
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
   101
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
   102
			default:
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
   103
				break;
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
   104
		}
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
   105
	}
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
   106
7386
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
   107
	DeleteSubsidyWithTown(this->index);
4396
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
   108
28cfaf264db6 (svn r6149) -Codechange: DeleteTown removes a town from the pool
truelight
parents: 4384
diff changeset
   109
	MarkWholeScreenDirty();
7386
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
   110
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
   111
	this->xy = 0;
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
   112
}
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
   113
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   114
/**
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   115
 * Generate a random town road layout.
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   116
 *
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   117
 * The layout is based on the TileHash.
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   118
 */
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   119
void Town::InitializeLayout()
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   120
{
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   121
	this->layout = (TownLayout)(TileHash(TileX(this->xy), TileY(this->xy)) % NUM_TLS);
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   122
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   123
	/* Set invalid layouts to valid ones */
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   124
	switch (this->layout) {
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   125
		default: break;
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   126
		case TL_RANDOM: this->layout = TL_ORIGINAL; break;
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   127
		case TL_NO_ROADS: this->layout = TL_BETTER_ROADS; break;
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   128
	}
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   129
}
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   130
9463
174d00929d44 (svn r13383) -Codechange: Put the cost of house removal in a class member
belugas
parents: 9450
diff changeset
   131
Money HouseSpec::GetRemovalCost() const
174d00929d44 (svn r13383) -Codechange: Put the cost of house removal in a class member
belugas
parents: 9450
diff changeset
   132
{
174d00929d44 (svn r13383) -Codechange: Put the cost of house removal in a class member
belugas
parents: 9450
diff changeset
   133
	return (_price.remove_house * this->removal_cost) >> 8;
174d00929d44 (svn r13383) -Codechange: Put the cost of house removal in a class member
belugas
parents: 9450
diff changeset
   134
}
174d00929d44 (svn r13383) -Codechange: Put the cost of house removal in a class member
belugas
parents: 9450
diff changeset
   135
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
// Local
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
static int _grow_town_result;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   138
7684
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
   139
/* Describe the possible states */
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
   140
enum TownGrowthResult {
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
   141
	GROWTH_SUCCEED         = -1,
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
   142
	GROWTH_SEARCH_STOPPED  =  0
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
   143
//	GROWTH_SEARCH_RUNNING >=  1
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
   144
};
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
   145
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   146
static bool BuildTownHouse(Town *t, TileIndex tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   147
2436
7d5df545bd5d (svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents: 2430
diff changeset
   148
static void TownDrawHouseLift(const TileInfo *ti)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
{
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5587
diff changeset
   150
	AddChildSpriteScreen(SPR_LIFT, PAL_NONE, 14, 60 - GetLiftPosition(ti->tile));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
2436
7d5df545bd5d (svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents: 2430
diff changeset
   153
typedef void TownDrawTileProc(const TileInfo *ti);
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   154
static TownDrawTileProc *const _town_draw_tile_procs[1] = {
2065
c3810847175d (svn r2574) Fix: AnimatedTile leak in town_cmd.c (this one has probably been here since day 1)
ludde
parents: 2061
diff changeset
   155
	TownDrawHouseLift
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   156
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   157
8791
a882cc95f19c (svn r12513) -Codechange: rename OriginalTileRandomiser something more descriptive
skidd13
parents: 8787
diff changeset
   158
/**
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   159
 * Return a random direction
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   160
 *
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   161
 * @return a random direction
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   162
 */
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   163
static inline DiagDirection RandomDiagDir()
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   164
{
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   165
	return (DiagDirection)(3 & Random());
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   166
}
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   167
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   168
/**
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   169
 * House Tile drawing handler.
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   170
 * Part of the tile loop process
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   171
 * @param ti TileInfo of the tile to draw
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   172
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
static void DrawTile_Town(TileInfo *ti)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
{
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   175
	HouseID house_id = GetHouseType(ti->tile);
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   176
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   177
	if (house_id >= NEW_HOUSE_OFFSET) {
6342
a44ad9acf82a (svn r9367) -Fix (r9315): If a new house doesn't have a corresponding action 3, the
maedhros
parents: 6341
diff changeset
   178
		/* Houses don't necessarily need new graphics. If they don't have a
a44ad9acf82a (svn r9367) -Fix (r9315): If a new house doesn't have a corresponding action 3, the
maedhros
parents: 6341
diff changeset
   179
		 * spritegroup associated with them, then the sprite for the substitute
a44ad9acf82a (svn r9367) -Fix (r9315): If a new house doesn't have a corresponding action 3, the
maedhros
parents: 6341
diff changeset
   180
		 * house id is drawn instead. */
a44ad9acf82a (svn r9367) -Fix (r9315): If a new house doesn't have a corresponding action 3, the
maedhros
parents: 6341
diff changeset
   181
		if (GetHouseSpecs(house_id)->spritegroup != NULL) {
a44ad9acf82a (svn r9367) -Fix (r9315): If a new house doesn't have a corresponding action 3, the
maedhros
parents: 6341
diff changeset
   182
			DrawNewHouseTile(ti, house_id);
a44ad9acf82a (svn r9367) -Fix (r9315): If a new house doesn't have a corresponding action 3, the
maedhros
parents: 6341
diff changeset
   183
			return;
a44ad9acf82a (svn r9367) -Fix (r9315): If a new house doesn't have a corresponding action 3, the
maedhros
parents: 6341
diff changeset
   184
		} else {
a44ad9acf82a (svn r9367) -Fix (r9315): If a new house doesn't have a corresponding action 3, the
maedhros
parents: 6341
diff changeset
   185
			house_id = GetHouseSpecs(house_id)->substitute_id;
a44ad9acf82a (svn r9367) -Fix (r9315): If a new house doesn't have a corresponding action 3, the
maedhros
parents: 6341
diff changeset
   186
		}
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   187
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   188
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
	/* Retrieve pointer to the draw town tile struct */
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   190
	const DrawBuildingsTileStruct *dcts = &_town_draw_tile_data[house_id << 4 | TileHash2Bit(ti->x, ti->y) << 2 | GetHouseBuildingStage(ti->tile)];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   191
7335
141c6b86ec1f (svn r10698) -Codechange [FS#1082]: simplify the code related to foundations. Primarily removal of (duplicated|magic) code and introduction of few helper functions to ease foundation determination. Patch by frosch.
rubidium
parents: 7333
diff changeset
   192
	if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5587
diff changeset
   193
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   194
	DrawGroundSprite(dcts->ground.sprite, dcts->ground.pal);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   195
8806
4fe163e0b52a (svn r12547) -Feature: invisibility options to make objects invisible instead of transparent
smatz
parents: 8796
diff changeset
   196
	/* If houses are invisible, do not draw the upper part */
4fe163e0b52a (svn r12547) -Feature: invisibility options to make objects invisible instead of transparent
smatz
parents: 8796
diff changeset
   197
	if (IsInvisibilitySet(TO_HOUSES)) return;
4fe163e0b52a (svn r12547) -Feature: invisibility options to make objects invisible instead of transparent
smatz
parents: 8796
diff changeset
   198
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
	/* Add a house on top of the ground? */
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   200
	SpriteID image = dcts->building.sprite;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   201
	if (image != 0) {
7333
e48228e44be8 (svn r10696) -Codechange: remove duplication of the "make sprite transparent" code.
rubidium
parents: 7226
diff changeset
   202
		AddSortableSpriteToDraw(image, dcts->building.pal,
482
3999e4338a7c (svn r760) Replace some bit-juggling with bit fields
tron
parents: 480
diff changeset
   203
			ti->x + dcts->subtile_x,
3999e4338a7c (svn r760) Replace some bit-juggling with bit fields
tron
parents: 480
diff changeset
   204
			ti->y + dcts->subtile_y,
7577
ab000224166b (svn r11102) -Codechange: remove some pointless addition+substractions. Patch by frosch.
rubidium
parents: 7566
diff changeset
   205
			dcts->width,
ab000224166b (svn r11102) -Codechange: remove some pointless addition+substractions. Patch by frosch.
rubidium
parents: 7566
diff changeset
   206
			dcts->height,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
			dcts->dz,
7333
e48228e44be8 (svn r10696) -Codechange: remove duplication of the "make sprite transparent" code.
rubidium
parents: 7226
diff changeset
   208
			ti->z,
7849
0a1c0af2c96c (svn r11399) -Feature(ette): transparency settings can now be saved and thus remembered.
belugas
parents: 7847
diff changeset
   209
			IsTransparencySet(TO_HOUSES)
4053
7396defc2412 (svn r5327) Use DrawFoundation() for houses
tron
parents: 4000
diff changeset
   210
		);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
7849
0a1c0af2c96c (svn r11399) -Feature(ette): transparency settings can now be saved and thus remembered.
belugas
parents: 7847
diff changeset
   212
		if (IsTransparencySet(TO_HOUSES)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   213
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   214
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
	{
3654
c09872510a61 (svn r4568) CodeChange : Bring definitions and uses of DrawTypesStructures toguether.
belugas
parents: 3645
diff changeset
   216
		int proc = dcts->draw_proc - 1;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   217
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   218
		if (proc >= 0) _town_draw_tile_procs[proc](ti);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   221
4231
2823b3643862 (svn r5794) Pass the TileIndex plus x and y coordinates into GetSlopeZ_* instead of a TileInfo
tron
parents: 4077
diff changeset
   222
static uint GetSlopeZ_Town(TileIndex tile, uint x, uint y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   223
{
4231
2823b3643862 (svn r5794) Pass the TileIndex plus x and y coordinates into GetSlopeZ_* instead of a TileInfo
tron
parents: 4077
diff changeset
   224
	return GetTileMaxZ(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   225
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
10260
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 10259
diff changeset
   227
/** Tile callback routine */
7335
141c6b86ec1f (svn r10698) -Codechange [FS#1082]: simplify the code related to foundations. Primarily removal of (duplicated|magic) code and introduction of few helper functions to ease foundation determination. Patch by frosch.
rubidium
parents: 7333
diff changeset
   228
static Foundation GetFoundation_Town(TileIndex tile, Slope tileh)
39
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 4
diff changeset
   229
{
7335
141c6b86ec1f (svn r10698) -Codechange [FS#1082]: simplify the code related to foundations. Primarily removal of (duplicated|magic) code and introduction of few helper functions to ease foundation determination. Patch by frosch.
rubidium
parents: 7333
diff changeset
   230
	return FlatteningFoundation(tileh);
39
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 4
diff changeset
   231
}
d177340ed556 (svn r40) Final slope graphics fix
dominik
parents: 4
diff changeset
   232
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   233
/**
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   234
 * Animate a tile for a town
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   235
 * Only certain houses can be animated
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   236
 * The newhouses animation superseeds regular ones
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   237
 * @param tile TileIndex of the house to animate
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   238
 */
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   239
static void AnimateTile_Town(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   240
{
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   241
	if (GetHouseType(tile) >= NEW_HOUSE_OFFSET) {
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   242
		AnimateNewHouseTile(tile);
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   243
		return;
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   244
	}
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   245
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   246
	if (_tick_counter & 3) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   248
	/* If the house is not one with a lift anymore, then stop this animating.
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   249
	 * Not exactly sure when this happens, but probably when a house changes.
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   250
	 * Before this was just a return...so it'd leak animated tiles..
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   251
	 * That bug seems to have been here since day 1?? */
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   252
	if (!(GetHouseSpecs(GetHouseType(tile))->building_flags & BUILDING_IS_ANIMATED)) {
2065
c3810847175d (svn r2574) Fix: AnimatedTile leak in town_cmd.c (this one has probably been here since day 1)
ludde
parents: 2061
diff changeset
   253
		DeleteAnimatedTile(tile);
c3810847175d (svn r2574) Fix: AnimatedTile leak in town_cmd.c (this one has probably been here since day 1)
ludde
parents: 2061
diff changeset
   254
		return;
c3810847175d (svn r2574) Fix: AnimatedTile leak in town_cmd.c (this one has probably been here since day 1)
ludde
parents: 2061
diff changeset
   255
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   257
	if (!LiftHasDestination(tile)) {
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   258
		uint i;
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   259
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   260
		/* Building has 6 floors, number 0 .. 6, where 1 is illegal.
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   261
		 * This is due to the fact that the first floor is, in the graphics,
2891
7a9dbadd3f28 (svn r3445) - Fix: [ 1415379 ] Enhance documentation of lift destination
Darkvater
parents: 2817
diff changeset
   262
		 *  the height of 2 'normal' floors.
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   263
		 * Furthermore, there are 6 lift positions from floor N (incl) to floor N + 1 (excl) */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   264
		do {
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   265
			i = RandomRange(7);
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   266
		} while (i == 1 || i * 6 == GetLiftPosition(tile));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   267
3426
451a6367552d (svn r4254) -Codechange: Add and make use of map accessors for town lifts.
celestar
parents: 3422
diff changeset
   268
		SetLiftDestination(tile, i);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   270
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   271
	int pos = GetLiftPosition(tile);
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   272
	int dest = GetLiftDestination(tile) * 6;
3426
451a6367552d (svn r4254) -Codechange: Add and make use of map accessors for town lifts.
celestar
parents: 3422
diff changeset
   273
	pos += (pos < dest) ? 1 : -1;
451a6367552d (svn r4254) -Codechange: Add and make use of map accessors for town lifts.
celestar
parents: 3422
diff changeset
   274
	SetLiftPosition(tile, pos);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   275
9006
7666e7b47088 (svn r12801) -Codechange: remove the dependency of function.h in town_map.h
rubidium
parents: 8970
diff changeset
   276
	if (pos == dest) {
7666e7b47088 (svn r12801) -Codechange: remove the dependency of function.h in town_map.h
rubidium
parents: 8970
diff changeset
   277
		HaltLift(tile);
7666e7b47088 (svn r12801) -Codechange: remove the dependency of function.h in town_map.h
rubidium
parents: 8970
diff changeset
   278
		DeleteAnimatedTile(tile);
7666e7b47088 (svn r12801) -Codechange: remove the dependency of function.h in town_map.h
rubidium
parents: 8970
diff changeset
   279
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   280
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   281
	MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   284
/**
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   285
 * Determines if a town is close to a tile
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   286
 * @param tile TileIndex of the tile to query
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   287
 * @param dist maximum distance to be accepted
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   288
 * @returns true if the tile correspond to the distance criteria
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   289
 */
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   290
static bool IsCloseToTown(TileIndex tile, uint dist)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   291
{
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   292
	const Town *t;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   293
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   294
	FOR_ALL_TOWNS(t) {
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   295
		if (DistanceManhattan(tile, t->xy) < dist) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   296
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   297
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   298
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   299
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   300
/**
7545
d44e19c5671e (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 7500
diff changeset
   301
 * Marks the town sign as needing a repaint.
d44e19c5671e (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 7500
diff changeset
   302
 *
d44e19c5671e (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 7500
diff changeset
   303
 * This function marks the area of the sign of a town as dirty for repaint.
d44e19c5671e (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 7500
diff changeset
   304
 *
d44e19c5671e (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 7500
diff changeset
   305
 * @param t Town requesting town sign for repaint
d44e19c5671e (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 7500
diff changeset
   306
 * @ingroup dirty
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   307
 */
835
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
   308
static void MarkTownSignDirty(Town *t)
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
   309
{
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
   310
	MarkAllViewportsDirty(
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6486
diff changeset
   311
		t->sign.left - 6,
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6486
diff changeset
   312
		t->sign.top - 3,
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6486
diff changeset
   313
		t->sign.left + t->sign.width_1 * 4 + 12,
835
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
   314
		t->sign.top + 45
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
   315
	);
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
   316
}
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
   317
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   318
/**
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   319
 * Resize the sign(label) of the town after changes in
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   320
 * population (creation or growth or else)
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   321
 * @param t Town to update
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   322
 */
835
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
   323
void UpdateTownVirtCoord(Town *t)
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
   324
{
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
   325
	MarkTownSignDirty(t);
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   326
	Point pt = RemapCoords2(TileX(t->xy) * TILE_SIZE, TileY(t->xy) * TILE_SIZE);
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2065
diff changeset
   327
	SetDParam(0, t->index);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2065
diff changeset
   328
	SetDParam(1, t->population);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2065
diff changeset
   329
	UpdateViewportSignPos(&t->sign, pt.x, pt.y - 24,
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
   330
		_settings_client.gui.population_in_label ? STR_TOWN_LABEL_POP : STR_TOWN_LABEL);
835
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
   331
	MarkTownSignDirty(t);
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
   332
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
7969
5f1292aa867f (svn r11525) -Fix: do not do all kinds of 'updates' for town, waypoint, station and other signs when you haven't converted the map to the 'current' format as that means you are going to read data in the 'old' format when you assume that it is in the 'current' format.
rubidium
parents: 7967
diff changeset
   334
/** Update the virtual coords needed to draw the town sign for all towns. */
5f1292aa867f (svn r11525) -Fix: do not do all kinds of 'updates' for town, waypoint, station and other signs when you haven't converted the map to the 'current' format as that means you are going to read data in the 'old' format when you assume that it is in the 'current' format.
rubidium
parents: 7967
diff changeset
   335
void UpdateAllTownVirtCoords()
5f1292aa867f (svn r11525) -Fix: do not do all kinds of 'updates' for town, waypoint, station and other signs when you haven't converted the map to the 'current' format as that means you are going to read data in the 'old' format when you assume that it is in the 'current' format.
rubidium
parents: 7967
diff changeset
   336
{
5f1292aa867f (svn r11525) -Fix: do not do all kinds of 'updates' for town, waypoint, station and other signs when you haven't converted the map to the 'current' format as that means you are going to read data in the 'old' format when you assume that it is in the 'current' format.
rubidium
parents: 7967
diff changeset
   337
	Town *t;
5f1292aa867f (svn r11525) -Fix: do not do all kinds of 'updates' for town, waypoint, station and other signs when you haven't converted the map to the 'current' format as that means you are going to read data in the 'old' format when you assume that it is in the 'current' format.
rubidium
parents: 7967
diff changeset
   338
	FOR_ALL_TOWNS(t) {
5f1292aa867f (svn r11525) -Fix: do not do all kinds of 'updates' for town, waypoint, station and other signs when you haven't converted the map to the 'current' format as that means you are going to read data in the 'old' format when you assume that it is in the 'current' format.
rubidium
parents: 7967
diff changeset
   339
		UpdateTownVirtCoord(t);
5f1292aa867f (svn r11525) -Fix: do not do all kinds of 'updates' for town, waypoint, station and other signs when you haven't converted the map to the 'current' format as that means you are going to read data in the 'old' format when you assume that it is in the 'current' format.
rubidium
parents: 7967
diff changeset
   340
	}
5f1292aa867f (svn r11525) -Fix: do not do all kinds of 'updates' for town, waypoint, station and other signs when you haven't converted the map to the 'current' format as that means you are going to read data in the 'old' format when you assume that it is in the 'current' format.
rubidium
parents: 7967
diff changeset
   341
}
5f1292aa867f (svn r11525) -Fix: do not do all kinds of 'updates' for town, waypoint, station and other signs when you haven't converted the map to the 'current' format as that means you are going to read data in the 'old' format when you assume that it is in the 'current' format.
rubidium
parents: 7967
diff changeset
   342
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   343
/**
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   344
 * Change the towns population
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   345
 * @param t Town which polulation has changed
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   346
 * @param mod polulation change (can be positive or negative)
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   347
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   348
static void ChangePopulation(Town *t, int mod)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   349
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   350
	t->population += mod;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   351
	InvalidateWindow(WC_TOWN_VIEW, t->index);
835
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
   352
	UpdateTownVirtCoord(t);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
9387
ede823d445f5 (svn r13297) -Codechange: Use GUIList for the town directory window
peter1138
parents: 9358
diff changeset
   354
	InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   357
/**
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   358
 * Determines the world population
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   359
 * Basically, count population of all towns, one by one
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   360
 * @return uint32 the calculated population of the world
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   361
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
   362
uint32 GetWorldPopulation()
1080
23797dda4792 (svn r1581) Added a display for the total map population to the town display (Jango)
celestar
parents: 1041
diff changeset
   363
{
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   364
	uint32 pop = 0;
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   365
	const Town *t;
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   366
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   367
	FOR_ALL_TOWNS(t) pop += t->population;
1080
23797dda4792 (svn r1581) Added a display for the total map population to the town display (Jango)
celestar
parents: 1041
diff changeset
   368
	return pop;
23797dda4792 (svn r1581) Added a display for the total map population to the town display (Jango)
celestar
parents: 1041
diff changeset
   369
}
23797dda4792 (svn r1581) Added a display for the total map population to the town display (Jango)
celestar
parents: 1041
diff changeset
   370
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   371
/**
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   372
 * Helper function for house completion stages progression
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   373
 * @param tile TileIndex of the house (or parts of it) to "grow"
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   374
 */
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   375
static void MakeSingleHouseBigger(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
{
1035
812f837ee03f (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1005
diff changeset
   377
	assert(IsTileType(tile, MP_HOUSE));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   378
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   379
	/* means it is completed, get out. */
3426
451a6367552d (svn r4254) -Codechange: Add and make use of map accessors for town lifts.
celestar
parents: 3422
diff changeset
   380
	if (LiftHasDestination(tile)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   382
	/* progress in construction stages */
3432
650dd1972479 (svn r4261) CodeChange : Add and Use Accessor for Houses Construction. And cleaning on town.flags12 too
belugas
parents: 3426
diff changeset
   383
	IncHouseConstructionTick(tile);
650dd1972479 (svn r4261) CodeChange : Add and Use Accessor for Houses Construction. And cleaning on town.flags12 too
belugas
parents: 3426
diff changeset
   384
	if (GetHouseConstructionTick(tile) != 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
9018
403898e0b54e (svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
rubidium
parents: 9006
diff changeset
   386
	const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
403898e0b54e (svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
rubidium
parents: 9006
diff changeset
   387
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   388
	/* Check and/or  */
9018
403898e0b54e (svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
rubidium
parents: 9006
diff changeset
   389
	if (HasBit(hs->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) {
7215
7f90af37655d (svn r10493) -Codechange: update some callback ID enums to reflect their changed usage, add a few and update the comments.
rubidium
parents: 7139
diff changeset
   390
		uint16 callback_res = GetHouseCallback(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
9018
403898e0b54e (svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
rubidium
parents: 9006
diff changeset
   391
		if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(hs->grffile, tile, callback_res);
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   392
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   394
	if (IsHouseCompleted(tile)) {
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   395
		/* Now that construction is complete, we can add the population of the
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   396
		 * building to the town. */
9018
403898e0b54e (svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
rubidium
parents: 9006
diff changeset
   397
		ChangePopulation(GetTownByTile(tile), hs->population);
9006
7666e7b47088 (svn r12801) -Codechange: remove the dependency of function.h in town_map.h
rubidium
parents: 8970
diff changeset
   398
		SetHouseConstructionYear(tile, _cur_year);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
	MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   402
10260
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 10259
diff changeset
   403
/** Make the house advance in its construction stages until completion
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   404
 * @param tile TileIndex of house
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   405
 */
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   406
static void MakeTownHouseBigger(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
{
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   408
	uint flags = GetHouseSpecs(GetHouseType(tile))->building_flags;
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   409
	if (flags & BUILDING_HAS_1_TILE)  MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 0));
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   410
	if (flags & BUILDING_2_TILES_Y)   MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 1));
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   411
	if (flags & BUILDING_2_TILES_X)   MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 0));
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   412
	if (flags & BUILDING_HAS_4_TILES) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 1));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   415
/**
10260
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 10259
diff changeset
   416
 * Tile callback function.
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 10259
diff changeset
   417
 *
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   418
 * Periodic tic handler for houses and town
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   419
 * @param tile been asked to do its stuff
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   420
 */
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   421
static void TileLoop_Town(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
{
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   423
	HouseID house_id = GetHouseType(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   425
	/* NewHouseTileLoop returns false if Callback 21 succeeded, i.e. the house
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   426
	 * doesn't exist any more, so don't continue here. */
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   427
	if (house_id >= NEW_HOUSE_OFFSET && !NewHouseTileLoop(tile)) return;
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   428
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   429
	if (!IsHouseCompleted(tile)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   430
		/* Construction is not completed. See if we can go further in construction*/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
		MakeTownHouseBigger(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   433
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   435
	const HouseSpec *hs = GetHouseSpecs(house_id);
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   436
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   437
	/* If the lift has a destination, it is already an animated tile. */
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   438
	if ((hs->building_flags & BUILDING_IS_ANIMATED) &&
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   439
			house_id < NEW_HOUSE_OFFSET &&
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   440
			!LiftHasDestination(tile) &&
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   441
			Chance16(1, 2)) {
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   442
		AddAnimatedTile(tile);
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   443
	}
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   444
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   445
	Town *t = GetTownByTile(tile);
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   446
	uint32 r = Random();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7903
diff changeset
   448
	if (HasBit(hs->callback_mask, CBM_HOUSE_PRODUCE_CARGO)) {
6645
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   449
		for (uint i = 0; i < 256; i++) {
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   450
			uint16 callback = GetHouseCallback(CBID_HOUSE_PRODUCE_CARGO, i, r, house_id, t, tile);
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   451
8287
77eed374cc00 (svn r11851) -Codechange: A few magic numbers removal, plus a little code style
belugas
parents: 8268
diff changeset
   452
			if (callback == CALLBACK_FAILED || callback == CALLBACK_HOUSEPRODCARGO_END) break;
6645
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   453
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   454
			CargoID cargo = GetCargoTranslation(GB(callback, 8, 7), hs->grffile);
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   455
			if (cargo == CT_INVALID) continue;
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   456
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   457
			uint amt = GB(callback, 0, 8);
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   458
			uint moved = MoveGoodsToStation(tile, 1, 1, cargo, amt);
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   459
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   460
			const CargoSpec *cs = GetCargo(cargo);
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   461
			switch (cs->town_effect) {
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   462
				case TE_PASSENGERS:
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   463
					t->new_max_pass += amt;
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   464
					t->new_act_pass += moved;
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   465
					break;
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   466
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   467
				case TE_MAIL:
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   468
					t->new_max_mail += amt;
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   469
					t->new_act_mail += moved;
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   470
					break;
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   471
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   472
				default:
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   473
					break;
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   474
			}
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   475
		}
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   476
	} else {
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   477
		if (GB(r, 0, 8) < hs->population) {
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   478
			uint amt = GB(r, 0, 8) / 8 + 1;
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   479
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   480
			if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   481
			t->new_max_pass += amt;
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   482
			t->new_act_pass += MoveGoodsToStation(tile, 1, 1, CT_PASSENGERS, amt);
6645
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   483
		}
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   484
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   485
		if (GB(r, 8, 8) < hs->mail_generation) {
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   486
			uint amt = GB(r, 8, 8) / 8 + 1;
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   487
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   488
			if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   489
			t->new_max_mail += amt;
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   490
			t->new_act_mail += MoveGoodsToStation(tile, 1, 1, CT_MAIL, amt);
6645
289779d21015 (svn r9876) -Codechange: [NewHouses] Add support for callback 2E (cargo production)
peter1138
parents: 6642
diff changeset
   491
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   492
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   493
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
   494
	_current_company = OWNER_TOWN;
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   495
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   496
	if (hs->building_flags & BUILDING_HAS_1_TILE &&
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7903
diff changeset
   497
			HasBit(t->flags12, TOWN_IS_FUNDED) &&
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   498
			CanDeleteHouse(tile) &&
6605
d92f454a00a6 (svn r9823) -Feature: Add support for house property 1F - minimum life span.
maedhros
parents: 6571
diff changeset
   499
			max(_cur_year - GetHouseConstructionYear(tile), 0) >= hs->minimum_life &&
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   500
			--t->time_until_rebuild == 0) {
6454
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
   501
		t->time_until_rebuild = GB(r, 16, 8) + 192;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   502
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   503
		ClearTownHouse(t, tile);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   504
6454
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
   505
		/* Rebuild with another house? */
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
   506
		if (GB(r, 24, 8) >= 12) BuildTownHouse(t, tile);
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   507
	}
314
75fe42199623 (svn r320) -Fix: some last _current_player fixes
truelight
parents: 260
diff changeset
   508
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
   509
	_current_company = OWNER_NONE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   510
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   511
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   512
/**
10260
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 10259
diff changeset
   513
 * Dummy tile callback function for handling tile clicks in towns
6534
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   514
 * @param tile unused
cbfd00fc5d88 (svn r9722) -Documentation: Document some functions of town_cmd.cpp. Loosely based on work from TheJosh
belugas
parents: 6525
diff changeset
   515
 */
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   516
static void ClickTile_Town(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   517
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   518
	/* not used */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   519
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   520
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6914
diff changeset
   521
static CommandCost ClearTile_Town(TileIndex tile, byte flags)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   522
{
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   523
	if ((flags & DC_AUTO) && !(flags & DC_AI_BUILDING)) return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED);
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   524
	if (!CanDeleteHouse(tile)) return CMD_ERROR;
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   525
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   526
	const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   527
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   528
	CommandCost cost(EXPENSES_CONSTRUCTION);
9463
174d00929d44 (svn r13383) -Codechange: Put the cost of house removal in a class member
belugas
parents: 9450
diff changeset
   529
	cost.AddCost(hs->GetRemovalCost());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   530
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   531
	int rating = hs->remove_rating_decrease;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   532
	_cleared_town_rating += rating;
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   533
	Town *t = _cleared_town = GetTownByTile(tile);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   534
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
   535
	if (IsValidCompanyID(_current_company)) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
   536
		if (rating > t->ratings[_current_company] && !(flags & DC_NO_TOWN_RATING) && !_cheats.magic_bulldozer.value) {
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 516
diff changeset
   537
			SetDParam(0, t->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
			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
   539
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
8232
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
   542
	ChangeTownRating(t, -rating, RATING_HOUSE_MINIMUM);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   544
		ClearTownHouse(t, tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   546
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   547
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   549
8846
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   550
static void GetProducedCargo_Town(TileIndex tile, CargoID *b)
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   551
{
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   552
	HouseID house_id = GetHouseType(tile);
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   553
	const HouseSpec *hs = GetHouseSpecs(house_id);
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   554
	Town *t = GetTownByTile(tile);
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   555
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   556
	if (HasBit(hs->callback_mask, CBM_HOUSE_PRODUCE_CARGO)) {
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   557
		for (uint i = 0; i < 256; i++) {
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   558
			uint16 callback = GetHouseCallback(CBID_HOUSE_PRODUCE_CARGO, i, 0, house_id, t, tile);
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   559
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   560
			if (callback == CALLBACK_FAILED || callback == CALLBACK_HOUSEPRODCARGO_END) break;
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   561
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   562
			CargoID cargo = GetCargoTranslation(GB(callback, 8, 7), hs->grffile);
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   563
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   564
			if (cargo == CT_INVALID) continue;
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   565
			*(b++) = cargo;
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   566
		}
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   567
	} else {
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   568
		if (hs->population > 0) {
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   569
			*(b++) = CT_PASSENGERS;
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   570
		}
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   571
		if (hs->mail_generation > 0) {
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   572
			*(b++) = CT_MAIL;
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   573
		}
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   574
	}
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   575
}
1b90782b2c4e (svn r12596) -Feature: show what cargos a station could be supplied with. Patch by Roujin.
rubidium
parents: 8818
diff changeset
   576
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   577
static void GetAcceptedCargo_Town(TileIndex tile, AcceptedCargo ac)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
{
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   579
	const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
6525
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   580
	CargoID accepts[3];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   581
6525
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   582
	/* Set the initial accepted cargo types */
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   583
	for (uint8 i = 0; i < lengthof(accepts); i++) {
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   584
		accepts[i] = hs->accepts_cargo[i];
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   585
	}
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   586
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   587
	/* Check for custom accepted cargo types */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7903
diff changeset
   588
	if (HasBit(hs->callback_mask, CBM_HOUSE_ACCEPT_CARGO)) {
6642
ced51f7c19aa (svn r9873) -Codechange: Add missing second callback parameter for houses
peter1138
parents: 6629
diff changeset
   589
		uint16 callback = GetHouseCallback(CBID_HOUSE_ACCEPT_CARGO, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
6525
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   590
		if (callback != CALLBACK_FAILED) {
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   591
			/* Replace accepted cargo types with translated values from callback */
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   592
			accepts[0] = GetCargoTranslation(GB(callback,  0, 5), hs->grffile);
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   593
			accepts[1] = GetCargoTranslation(GB(callback,  5, 5), hs->grffile);
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   594
			accepts[2] = GetCargoTranslation(GB(callback, 10, 5), hs->grffile);
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   595
		}
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   596
	}
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   597
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   598
	/* Check for custom cargo acceptance */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7903
diff changeset
   599
	if (HasBit(hs->callback_mask, CBM_HOUSE_CARGO_ACCEPTANCE)) {
6642
ced51f7c19aa (svn r9873) -Codechange: Add missing second callback parameter for houses
peter1138
parents: 6629
diff changeset
   600
		uint16 callback = GetHouseCallback(CBID_HOUSE_CARGO_ACCEPTANCE, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
6525
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   601
		if (callback != CALLBACK_FAILED) {
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   602
			if (accepts[0] != CT_INVALID) ac[accepts[0]] = GB(callback, 0, 4);
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   603
			if (accepts[1] != CT_INVALID) ac[accepts[1]] = GB(callback, 4, 4);
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
   604
			if (_settings_game.game_creation.landscape != LT_TEMPERATE && HasBit(callback, 12)) {
6525
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   605
				/* The 'S' bit indicates food instead of goods */
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   606
				ac[CT_FOOD] = GB(callback, 8, 4);
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   607
			} else {
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   608
				if (accepts[2] != CT_INVALID) ac[accepts[2]] = GB(callback, 8, 4);
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   609
			}
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   610
			return;
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   611
		}
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   612
	}
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   613
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   614
	/* No custom acceptance, so fill in with the default values */
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   615
	for (uint8 i = 0; i < lengthof(accepts); i++) {
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   616
		if (accepts[i] != CT_INVALID) ac[accepts[i]] = hs->cargo_acceptance[i];
a21429c6f16f (svn r9712) -Codechange: Implement accepted cargo types and cargo acceptance (there is a difference) callbacks for newhouses.
peter1138
parents: 6491
diff changeset
   617
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   618
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   619
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   620
static void GetTileDesc_Town(TileIndex tile, TileDesc *td)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   621
{
9714
5d785e184f2d (svn r13830) -Feature: show additional NewGRF info in the Tile Info window for stations, houses and industries
smatz
parents: 9659
diff changeset
   622
	const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
5d785e184f2d (svn r13830) -Feature: show additional NewGRF info in the Tile Info window for stations, houses and industries
smatz
parents: 9659
diff changeset
   623
5d785e184f2d (svn r13830) -Feature: show additional NewGRF info in the Tile Info window for stations, houses and industries
smatz
parents: 9659
diff changeset
   624
	td->str = hs->building_name;
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
   625
	if (!IsHouseCompleted(tile)) {
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 516
diff changeset
   626
		SetDParamX(td->dparam, 0, td->str);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
		td->str = STR_2058_UNDER_CONSTRUCTION;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
9714
5d785e184f2d (svn r13830) -Feature: show additional NewGRF info in the Tile Info window for stations, houses and industries
smatz
parents: 9659
diff changeset
   630
	if (hs->grffile != NULL) {
5d785e184f2d (svn r13830) -Feature: show additional NewGRF info in the Tile Info window for stations, houses and industries
smatz
parents: 9659
diff changeset
   631
		const GRFConfig *gc = GetGRFConfig(hs->grffile->grfid);
5d785e184f2d (svn r13830) -Feature: show additional NewGRF info in the Tile Info window for stations, houses and industries
smatz
parents: 9659
diff changeset
   632
		td->grf = gc->name;
5d785e184f2d (svn r13830) -Feature: show additional NewGRF info in the Tile Info window for stations, houses and industries
smatz
parents: 9659
diff changeset
   633
	}
5d785e184f2d (svn r13830) -Feature: show additional NewGRF info in the Tile Info window for stations, houses and industries
smatz
parents: 9659
diff changeset
   634
9322
cf7dc39f9576 (svn r13206) -Feature(ette): Display all owners of a tile in the tile-info-window.
frosch
parents: 9234
diff changeset
   635
	td->owner[0] = OWNER_TOWN;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8596
diff changeset
   638
static TrackStatus GetTileTrackStatus_Town(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   640
	/* not used */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   641
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   642
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
   644
static void ChangeTileOwner_Town(TileIndex tile, Owner old_owner, Owner new_owner)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   646
	/* not used */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   647
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   648
2817
cdf488223c23 (svn r3365) Staticise 36 functions
tron
parents: 2791
diff changeset
   649
static bool GrowTown(Town *t);
cdf488223c23 (svn r3365) Staticise 36 functions
tron
parents: 2791
diff changeset
   650
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   651
static void TownTickHandler(Town *t)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   652
{
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7903
diff changeset
   653
	if (HasBit(t->flags12, TOWN_IS_FUNDED)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
		int i = t->grow_counter - 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
		if (i < 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
			if (GrowTown(t)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   657
				i = t->growth_rate;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   658
			} else {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   659
				i = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   660
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   661
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   662
		t->grow_counter = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   663
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   664
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   665
	UpdateTownRadius(t);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   666
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   667
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
   668
void OnTick_Town()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   669
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   670
	if (_game_mode == GM_EDITOR) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   671
1451
eb24b5a58340 (svn r1955) Fix: Make the town growth frequency scale properly both up and down. The scaling is now also based on the number of towns instead of the map size. (In cooperation with HackyKid.)
pasky
parents: 1448
diff changeset
   672
	/* Make sure each town's tickhandler invocation frequency is about the
eb24b5a58340 (svn r1955) Fix: Make the town growth frequency scale properly both up and down. The scaling is now also based on the number of towns instead of the map size. (In cooperation with HackyKid.)
pasky
parents: 1448
diff changeset
   673
	 * same - TOWN_GROWTH_FREQUENCY - independent on the number of towns. */
5247
1f982de55b88 (svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.
matthijs
parents: 5216
diff changeset
   674
	for (_cur_town_iter += GetMaxTownIndex() + 1;
1529
3a8196d15b01 (svn r2033) - Fix: Fix some more desync by saving the town growth frequency iterator _cur_town_iter. Needed to bump a svg revision for that thanks to the braindead SlGlob thing, or at least I don't know how to do it without bumping it.
pasky
parents: 1517
diff changeset
   675
	     _cur_town_iter >= TOWN_GROWTH_FREQUENCY;
3a8196d15b01 (svn r2033) - Fix: Fix some more desync by saving the town growth frequency iterator _cur_town_iter. Needed to bump a svg revision for that thanks to the braindead SlGlob thing, or at least I don't know how to do it without bumping it.
pasky
parents: 1517
diff changeset
   676
	     _cur_town_iter -= TOWN_GROWTH_FREQUENCY) {
1517
596d52828893 (svn r2021) Fix: Enlarge _cur_town_ctr from byte to uint32 so that all the towns are considered when growing them even for big maps, where much more than 256 towns are around; reported by Tomasz Dubi?ski <uboottd@hydra.polsl.gliwice.pl>. The savegame still saves just the lowest 8 bits but that doesn't hurt so much.
pasky
parents: 1500
diff changeset
   677
		uint32 i = _cur_town_ctr;
1451
eb24b5a58340 (svn r1955) Fix: Make the town growth frequency scale properly both up and down. The scaling is now also based on the number of towns instead of the map size. (In cooperation with HackyKid.)
pasky
parents: 1448
diff changeset
   678
5247
1f982de55b88 (svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.
matthijs
parents: 5216
diff changeset
   679
		if (++_cur_town_ctr > GetMaxTownIndex())
1448
472b3b3855e7 (svn r1952) Fix: Hopefully fixed the enormous towns growth slowdown in large maps. (Inspired by toholio.)
pasky
parents: 1421
diff changeset
   680
			_cur_town_ctr = 0;
1260
632f63689178 (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1252
diff changeset
   681
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4346
diff changeset
   682
		if (IsValidTownID(i)) TownTickHandler(GetTown(i));
1448
472b3b3855e7 (svn r1952) Fix: Hopefully fixed the enormous towns growth slowdown in large maps. (Inspired by toholio.)
pasky
parents: 1421
diff changeset
   683
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   684
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   685
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   686
/**
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   687
 * Return the RoadBits of a tile
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   688
 *
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   689
 * @note There are many other functions doing things like that.
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   690
 * @note Needs to be checked for needlessness.
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   691
 * @param tile The tile we want to analyse
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   692
 * @return The roadbits of the given tile
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   693
 */
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   694
static RoadBits GetTownRoadBits(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   695
{
6661
1716fce5ad29 (svn r9892) -Codechange: lots of ground work for allowing multiple types of "road" with multiple owners on a single tile.
rubidium
parents: 6645
diff changeset
   696
	TrackBits b = GetAnyRoadTrackBits(tile, ROADTYPE_ROAD);
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   697
	RoadBits r = ROAD_NONE;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   698
7225
9d9a1e554903 (svn r10504) -Codechange: Shorten the test if no track bit has been found
belugas
parents: 7215
diff changeset
   699
	if (b == TRACK_BIT_NONE) return r;
3150
729951cb5448 (svn r3773) Shove some semantics down ottd's throat by replacing ints and magic numbers by enums and some related changes
tron
parents: 3144
diff changeset
   700
	if (b & TRACK_BIT_X)     r |= ROAD_X;
729951cb5448 (svn r3773) Shove some semantics down ottd's throat by replacing ints and magic numbers by enums and some related changes
tron
parents: 3144
diff changeset
   701
	if (b & TRACK_BIT_Y)     r |= ROAD_Y;
729951cb5448 (svn r3773) Shove some semantics down ottd's throat by replacing ints and magic numbers by enums and some related changes
tron
parents: 3144
diff changeset
   702
	if (b & TRACK_BIT_UPPER) r |= ROAD_NE | ROAD_NW;
729951cb5448 (svn r3773) Shove some semantics down ottd's throat by replacing ints and magic numbers by enums and some related changes
tron
parents: 3144
diff changeset
   703
	if (b & TRACK_BIT_LOWER) r |= ROAD_SE | ROAD_SW;
729951cb5448 (svn r3773) Shove some semantics down ottd's throat by replacing ints and magic numbers by enums and some related changes
tron
parents: 3144
diff changeset
   704
	if (b & TRACK_BIT_LEFT)  r |= ROAD_NW | ROAD_SW;
729951cb5448 (svn r3773) Shove some semantics down ottd's throat by replacing ints and magic numbers by enums and some related changes
tron
parents: 3144
diff changeset
   705
	if (b & TRACK_BIT_RIGHT) r |= ROAD_NE | ROAD_SE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   706
	return r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   707
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   708
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   709
/**
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   710
 * Check if a neighboring tile has a road
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   711
 *
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   712
 * @param tile curent tile
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   713
 * @param dir target direction
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   714
 * @param dist_multi distance multiplyer
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   715
 * @return true if one of the neighboring tiles at the
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   716
 *  given distance is a road tile else false
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   717
 */
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   718
static bool IsNeighborRoadTile(TileIndex tile, const DiagDirection dir, uint dist_multi)
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   719
{
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   720
	/* Lookup table for the used diff values */
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   721
	const TileIndexDiff tid_lt[3] = {
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   722
		TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90RIGHT)),
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   723
		TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT)),
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   724
		TileOffsByDiagDir(ReverseDiagDir(dir)),
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   725
	};
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   726
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   727
	/* We add 1 to the distance because we want to get 1 for
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   728
	 * the min distance multiplyer and not 0.
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   729
	 * Therefore we start at 4. The 4 is used because
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   730
	 * there are 4 tiles per distance step to check. */
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   731
	dist_multi = (dist_multi + 1) * 4;
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   732
	for (uint pos = 4; pos < dist_multi; pos++) {
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   733
		TileIndexDiff cur = 0;
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   734
		/* For each even value of pos add the right TileIndexDiff
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   735
		 * for each uneven value the left TileIndexDiff
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   736
		 * for each with 2nd bit set (2,3,6,7,..) add the reversed TileIndexDiff */
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   737
		cur += tid_lt[(pos & 1) ? 0 : 1];
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   738
		if (pos & 2) cur += tid_lt[2];
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   739
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   740
		cur = (uint)(pos / 4) * cur; // Multiply for the fitting distance
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   741
		if (GetTownRoadBits(TILE_ADD(tile, cur)) & DiagDirToRoadBits((pos & 2) ? dir : ReverseDiagDir(dir))) return true;
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   742
	}
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   743
	return false;
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   744
}
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   745
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   746
/**
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   747
 * Check if a Road is allowed on a given tile
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   748
 *
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   749
 * @param t The current town
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   750
 * @param tile The target tile
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   751
 * @param dir The direction in which we want to extend the town
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   752
 * @return true if it is allowed else false
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   753
 */
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   754
static bool IsRoadAllowedHere(Town *t, TileIndex tile, DiagDirection dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   755
{
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   756
	if (TileX(tile) < 2 || TileX(tile) >= MapMaxX() || TileY(tile) < 2 || TileY(tile) >= MapMaxY()) return false;
6993
96260dbb34d6 (svn r10249) -Fix [FS#906]: town tried to gather information about the neighbourhood of a tile when it couldn't even *ever* build on that tile.
rubidium
parents: 6956
diff changeset
   757
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   758
	Slope cur_slope, desired_slope;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   759
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   760
	for (;;) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   761
		/* Check if there already is a road at this point? */
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   762
		if (GetTownRoadBits(tile) == ROAD_NONE) {
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   763
			/* No, try if we are able to build a road piece there.
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   764
			 * If that fails clear the land, and if that fails exit.
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   765
			 * This is to make sure that we can build a road here later. */
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   766
			if (CmdFailed(DoCommand(tile, ((dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? ROAD_X : ROAD_Y), 0, DC_AUTO, CMD_BUILD_ROAD)) &&
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3433
diff changeset
   767
					CmdFailed(DoCommand(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   768
				return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   769
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   770
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   771
		cur_slope = GetTileSlope(tile, NULL);
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   772
		if (cur_slope == SLOPE_FLAT) {
465
21f9985e12d1 (svn r694) Make the town sometimes build streets on slopes.
pasky
parents: 314
diff changeset
   773
no_slope:
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   774
			/* Tile has no slope */
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   775
			switch (t->GetActiveLayout()) {
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   776
				default: NOT_REACHED();
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   777
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   778
				case TL_ORIGINAL: // Disallow the road if any neighboring tile has a road (distance: 1)
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   779
					return !IsNeighborRoadTile(tile, dir, 1);
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   780
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   781
				case TL_BETTER_ROADS: // Disallow the road if any neighboring tile has a road (distance: 1 and 2).
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   782
					return !IsNeighborRoadTile(tile, dir, 2);
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   783
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   784
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
   785
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   786
		/* If the tile is not a slope in the right direction, then
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   787
		 * maybe terraform some. */
7885
706462b6f293 (svn r11436) -Fix [FS#1439]: Towns would not build roads on slopes. Patch by divide.
rubidium
parents: 7880
diff changeset
   788
		desired_slope = (dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? SLOPE_NW : SLOPE_NE;
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   789
		if (desired_slope != cur_slope && ComplementSlope(desired_slope) != cur_slope) {
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7931
diff changeset
   790
			if (Chance16(1, 8)) {
7766
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   791
				CommandCost res = CMD_ERROR;
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7931
diff changeset
   792
				if (!_generating_world && Chance16(1, 10)) {
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   793
					/* Note: Do not replace "^ SLOPE_ELEVATED" with ComplementSlope(). The slope might be steep. */
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   794
					res = DoCommand(tile, Chance16(1, 16) ? cur_slope : cur_slope ^ SLOPE_ELEVATED, 0,
7766
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   795
							DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
   796
				}
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7931
diff changeset
   797
				if (CmdFailed(res) && Chance16(1, 3)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   798
					/* We can consider building on the slope, though. */
465
21f9985e12d1 (svn r694) Make the town sometimes build streets on slopes.
pasky
parents: 314
diff changeset
   799
					goto no_slope;
2646
c01dfbc7b7e3 (svn r3188) Use CmdFailed() to check, if a command failed, don't compare with CMD_ERROR
tron
parents: 2639
diff changeset
   800
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   801
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   802
			return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   803
		}
835
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
   804
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   805
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   806
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   807
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   808
static bool TerraformTownTile(TileIndex tile, int edges, int dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   809
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   810
	TILE_ASSERT(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   811
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   812
	CommandCost r = DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
8818
15491686c2ac (svn r12564) -Fix: towns couldn't terraform when inflation rised terraform prices enough
smatz
parents: 8816
diff changeset
   813
	if (CmdFailed(r) || r.GetCost() >= (_price.terraform + 2) * 8) return false;
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3433
diff changeset
   814
	DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER | DC_EXEC, CMD_TERRAFORM_LAND);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   815
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   816
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   817
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   818
static void LevelTownLand(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   819
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
	TILE_ASSERT(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   822
	/* Don't terraform if land is plain or if there's a house there. */
3055
ed20cba49858 (svn r3644) Don't use FindLandscapeHeightByTile() when it's overkill - often it was just a complicated way of writing GetTileSlope(tile, NULL)
tron
parents: 3047
diff changeset
   823
	if (IsTileType(tile, MP_HOUSE)) return;
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
   824
	Slope tileh = GetTileSlope(tile, NULL);
3636
a36cc46e754d (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3585
diff changeset
   825
	if (tileh == SLOPE_FLAT) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
   827
	/* First try up, then down */
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   828
	if (!TerraformTownTile(tile, ~tileh & SLOPE_ELEVATED, 1)) {
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   829
		TerraformTownTile(tile, tileh & SLOPE_ELEVATED, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   830
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   831
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   832
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   833
/**
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   834
 * Generate the RoadBits of a grid tile
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   835
 *
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   836
 * @param t current town
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   837
 * @param tile tile in reference to the town
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   838
 * @param dir The direction to which we are growing ATM
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   839
 * @return the RoadBit of the current tile regarding
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   840
 *  the selected town layout
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   841
 */
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   842
static RoadBits GetTownRoadGridElement(Town *t, TileIndex tile, DiagDirection dir)
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   843
{
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   844
	/* align the grid to the downtown */
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   845
	TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile); // Vector from downtown to the tile
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   846
	RoadBits rcmd = ROAD_NONE;
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   847
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
   848
	switch (t->GetActiveLayout()) {
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   849
		default: NOT_REACHED();
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   850
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   851
		case TL_2X2_GRID:
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   852
			if ((grid_pos.x % 3) == 0) rcmd |= ROAD_Y;
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   853
			if ((grid_pos.y % 3) == 0) rcmd |= ROAD_X;
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   854
			break;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   855
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   856
		case TL_3X3_GRID:
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   857
			if ((grid_pos.x % 4) == 0) rcmd |= ROAD_Y;
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   858
			if ((grid_pos.y % 4) == 0) rcmd |= ROAD_X;
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   859
			break;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   860
	}
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   861
7603
1f067a18eb61 (svn r11130) -Fix [FS#1207]: towns build roads that weren't connected to any other road.
rubidium
parents: 7582
diff changeset
   862
	/* Optimise only X-junctions */
7766
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   863
	if (rcmd != ROAD_ALL) return rcmd;
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   864
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   865
	RoadBits rb_template;
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   866
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   867
	switch (GetTileSlope(tile, NULL)) {
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   868
		default:       rb_template = ROAD_ALL; break;
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   869
		case SLOPE_W:  rb_template = ROAD_NW | ROAD_SW; break;
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   870
		case SLOPE_SW: rb_template = ROAD_Y  | ROAD_SW; break;
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   871
		case SLOPE_S:  rb_template = ROAD_SW | ROAD_SE; break;
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   872
		case SLOPE_SE: rb_template = ROAD_X  | ROAD_SE; break;
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   873
		case SLOPE_E:  rb_template = ROAD_SE | ROAD_NE; break;
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   874
		case SLOPE_NE: rb_template = ROAD_Y  | ROAD_NE; break;
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   875
		case SLOPE_N:  rb_template = ROAD_NE | ROAD_NW; break;
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   876
		case SLOPE_NW: rb_template = ROAD_X  | ROAD_NW; break;
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   877
		case SLOPE_STEEP_W:
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   878
		case SLOPE_STEEP_S:
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   879
		case SLOPE_STEEP_E:
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   880
		case SLOPE_STEEP_N:
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   881
			rb_template = ROAD_NONE;
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   882
			break;
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   883
	}
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
   884
7766
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   885
	/* Stop if the template is compatible to the growth dir */
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   886
	if (DiagDirToRoadBits(ReverseDiagDir(dir)) & rb_template) return rb_template;
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   887
	/* If not generate a straight road in the direction of the growth */
4b94147f4bfa (svn r11315) -Fix [FS#1353]: towns are not growing beyond height differences. Patch by skidd13.
rubidium
parents: 7762
diff changeset
   888
	return DiagDirToRoadBits(dir) | DiagDirToRoadBits(ReverseDiagDir(dir));
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   889
}
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   890
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   891
/**
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   892
 * Grows the town with an extra house.
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   893
 *  Check if there are enough neighbor house tiles
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   894
 *  next to the current tile. If there are enough
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   895
 *  add another house.
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   896
 *
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   897
 * @param t The current town
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   898
 * @param tile The target tile for the extra house
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   899
 * @return true if an extra house has been added
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   900
 */
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   901
static bool GrowTownWithExtraHouse(Town *t, TileIndex tile)
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   902
{
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   903
	/* We can't look further than that. */
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   904
	if (TileX(tile) < 2 || TileY(tile) < 2 || MapMaxX() <= TileX(tile) || MapMaxY() <= TileY(tile)) return false;
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   905
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   906
	uint counter = 0; // counts the house neighbor tiles
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   907
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   908
	/* Check the tiles E,N,W and S of the current tile for houses */
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   909
	for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   910
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   911
		if (IsTileType(TileAddByDiagDir(tile, dir), MP_HOUSE)) counter++;
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   912
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   913
		/* If there are enough neighbors stop here */
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   914
		if (counter >= 3) {
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   915
			if (BuildTownHouse(t, tile)) {
7684
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
   916
				_grow_town_result = GROWTH_SUCCEED;
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   917
				return true;
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   918
			}
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   919
			return false;
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   920
		}
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   921
	}
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   922
	return false;
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   923
}
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   924
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   925
/**
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   926
 * Grows the town with a road piece.
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   927
 *
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   928
 * @param t The current town
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   929
 * @param tile The current tile
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   930
 * @param rcmd The RoadBits we want to build on the tile
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   931
 * @return true if the RoadBits have been added else false
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   932
 */
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   933
static bool GrowTownWithRoad(const Town *t, TileIndex tile, RoadBits rcmd)
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   934
{
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   935
	if (CmdSucceeded(DoCommand(tile, rcmd, t->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD))) {
7684
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
   936
		_grow_town_result = GROWTH_SUCCEED;
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   937
		return true;
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   938
	}
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   939
	return false;
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   940
}
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   941
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   942
/**
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   943
 * Grows the town with a bridge.
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   944
 *  At first we check if a bridge is reasonable.
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   945
 *  If so we check if we are able to build it.
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   946
 *
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   947
 * @param t The current town
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   948
 * @param tile The current tile
7845
4e5c90c389ca (svn r11395) -Fix: allow town-bridges to be build on slopes (Rafal Rzepecki)
truelight
parents: 7766
diff changeset
   949
 * @param bridge_dir The valid direction in which to grow a bridge
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   950
 * @return true if a bridge has been build else false
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   951
 */
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   952
static bool GrowTownWithBridge(const Town *t, const TileIndex tile, const DiagDirection bridge_dir)
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   953
{
7845
4e5c90c389ca (svn r11395) -Fix: allow town-bridges to be build on slopes (Rafal Rzepecki)
truelight
parents: 7766
diff changeset
   954
	assert(bridge_dir < DIAGDIR_END);
4e5c90c389ca (svn r11395) -Fix: allow town-bridges to be build on slopes (Rafal Rzepecki)
truelight
parents: 7766
diff changeset
   955
4e5c90c389ca (svn r11395) -Fix: allow town-bridges to be build on slopes (Rafal Rzepecki)
truelight
parents: 7766
diff changeset
   956
	const Slope slope = GetTileSlope(tile, NULL);
4e5c90c389ca (svn r11395) -Fix: allow town-bridges to be build on slopes (Rafal Rzepecki)
truelight
parents: 7766
diff changeset
   957
	if (slope == SLOPE_FLAT) return false; // no slope, no bridge
4e5c90c389ca (svn r11395) -Fix: allow town-bridges to be build on slopes (Rafal Rzepecki)
truelight
parents: 7766
diff changeset
   958
4e5c90c389ca (svn r11395) -Fix: allow town-bridges to be build on slopes (Rafal Rzepecki)
truelight
parents: 7766
diff changeset
   959
	/* Make sure the direction is compatible with the slope.
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   960
	 * Well we check if the slope has an up bit set in the
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   961
	 * reverse direction. */
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   962
	if (HASBITS(slope, InclinedSlope(bridge_dir))) return false;
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   963
7847
76271a2885d5 (svn r11397) -Fix r11395: some minor fixes for better town-bridge results (and better comments) (skidd13 / TrueLight)
truelight
parents: 7845
diff changeset
   964
	/* Assure that the bridge is connectable to the start side */
76271a2885d5 (svn r11397) -Fix r11395: some minor fixes for better town-bridge results (and better comments) (skidd13 / TrueLight)
truelight
parents: 7845
diff changeset
   965
	if (!(GetTownRoadBits(TileAddByDiagDir(tile, ReverseDiagDir(bridge_dir))) & DiagDirToRoadBits(bridge_dir))) return false;
76271a2885d5 (svn r11397) -Fix r11395: some minor fixes for better town-bridge results (and better comments) (skidd13 / TrueLight)
truelight
parents: 7845
diff changeset
   966
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   967
	/* We are in the right direction */
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   968
	uint8 bridge_length = 0;      // This value stores the length of the possible bridge
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   969
	TileIndex bridge_tile = tile; // Used to store the other waterside
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   970
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   971
	const int delta = TileOffsByDiagDir(bridge_dir);
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   972
	do {
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   973
		if (bridge_length++ >= 11) {
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   974
			/* Max 11 tile long bridges */
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   975
			return false;
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   976
		}
7871
4e5b81629881 (svn r11421) -Fix [FS#1431]: do not use a function that asserts when wrapping around the map's edge when you use the wrapping (and MP_VOID tiles) to determine whether the bridge can be build.
rubidium
parents: 7849
diff changeset
   977
		bridge_tile += delta;
8451
a3ae65b58eec (svn r12021) -Cleanup: Make some parts of the town growth a bit more descriptive
skidd13
parents: 8359
diff changeset
   978
	} while (TileX(bridge_tile) != 0 && TileY(bridge_tile) != 0 && IsWaterTile(bridge_tile));
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   979
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   980
	/* no water tiles in between? */
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   981
	if (bridge_length == 1) return false;
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   982
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   983
	for (uint8 times = 0; times <= 22; times++) {
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   984
		byte bridge_type = RandomRange(MAX_BRIDGES - 1);
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   985
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   986
		/* Can we actually build the bridge? */
10149
c855d5982951 (svn r14335) -Codechange: Remove some magic numbers.
frosch
parents: 10148
diff changeset
   987
		if (CmdSucceeded(DoCommand(tile, bridge_tile, bridge_type | ROADTYPES_ROAD << 8 | TRANSPORT_ROAD << 15, DC_AUTO, CMD_BUILD_BRIDGE))) {
c855d5982951 (svn r14335) -Codechange: Remove some magic numbers.
frosch
parents: 10148
diff changeset
   988
			DoCommand(tile, bridge_tile, bridge_type | ROADTYPES_ROAD << 8 | TRANSPORT_ROAD << 15, DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE);
7684
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
   989
			_grow_town_result = GROWTH_SUCCEED;
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   990
			return true;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   991
		}
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   992
	}
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
   993
	/* Quit if it selecting an appropiate bridge type fails a large number of times. */
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   994
	return false;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   995
}
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   996
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   997
/**
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   998
 * Grows the given town.
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
   999
 * There are at the moment 3 possible way's for
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1000
 * the town expansion:
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1001
 *  @li Generate a random tile and check if there is a road allowed
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1002
 *  @li TL_ORIGINAL
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1003
 *  @li TL_BETTER_ROADS
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1004
 *  @li Check if the town geometry allows a road and which one
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1005
 *  @li TL_2X2_GRID
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1006
 *  @li TL_3X3_GRID
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1007
 *  @li Forbid roads, only build houses
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1008
 *  @li TL_NO_ROADS
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1009
 *
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1010
 * @param tile_ptr The current tile
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1011
 * @param cur_rb The current tiles RoadBits
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1012
 * @param target_dir The target road dir
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1013
 * @param t1 The current town
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1014
 */
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1015
static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection target_dir, Town *t1)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1016
{
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1017
	RoadBits rcmd = ROAD_NONE;  // RoadBits for the road construction command
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1018
	TileIndex tile = *tile_ptr; // The main tile on which we base our growth
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1019
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1020
	TILE_ASSERT(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1021
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1022
	if (cur_rb == ROAD_NONE) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1023
		/* Tile has no road. First reset the status counter
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1024
		 * to say that this is the last iteration. */
7684
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
  1025
		_grow_town_result = GROWTH_SEARCH_STOPPED;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1026
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1027
		/* Remove hills etc */
10191
a7c7d7608f10 (svn r14404) -Change [FS#2176]: don't make the town flatten land unconditionally when build on slopes is turned on. Based on a patch by Eddi.
rubidium
parents: 10149
diff changeset
  1028
		if (!_settings_game.construction.build_on_slopes || Chance16(1, 6)) LevelTownLand(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1029
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1030
		/* Is a road allowed here? */
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
  1031
		switch (t1->GetActiveLayout()) {
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1032
			default: NOT_REACHED();
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1033
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1034
			case TL_NO_ROADS: /* Disallow Roads */
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1035
				return;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1036
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1037
			case TL_3X3_GRID:
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1038
			case TL_2X2_GRID:
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1039
				rcmd = GetTownRoadGridElement(t1, tile, target_dir);
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1040
				if (rcmd == ROAD_NONE) return;
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1041
				break;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1042
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1043
			case TL_BETTER_ROADS:
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1044
			case TL_ORIGINAL:
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
  1045
				if (!IsRoadAllowedHere(t1, tile, target_dir)) return;
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1046
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1047
				DiagDirection source_dir = ReverseDiagDir(target_dir);
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1048
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7931
diff changeset
  1049
				if (Chance16(1, 4)) {
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1050
					/* Randomize a new target dir */
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1051
					do target_dir = RandomDiagDir(); while (target_dir == source_dir);
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1052
				}
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1053
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
  1054
				if (!IsRoadAllowedHere(t1, TileAddByDiagDir(tile, target_dir), target_dir)) {
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1055
					/* A road is not allowed to continue the randomized road,
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1056
					 *  return if the road we're trying to build is curved. */
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1057
					if (target_dir != ReverseDiagDir(source_dir)) return;
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1058
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1059
					/* Return if neither side of the new road is a house */
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1060
					if (!IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90RIGHT)), MP_HOUSE) &&
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1061
							!IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90LEFT)), MP_HOUSE)) {
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1062
						return;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1063
					}
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1064
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1065
					/* That means that the road is only allowed if there is a house
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1066
					 *  at any side of the new road. */
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1067
				}
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1068
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1069
				rcmd = DiagDirToRoadBits(target_dir) | DiagDirToRoadBits(source_dir);
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1070
				break;
2637
722dae65c4a8 (svn r3179) - RandomRange() and RandomTile() instead of home brewed versions
tron
parents: 2635
diff changeset
  1071
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1072
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1073
	} else if (target_dir < DIAGDIR_END && !(cur_rb & DiagDirToRoadBits(ReverseDiagDir(target_dir)))) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1074
		/* Continue building on a partial road.
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1075
		 * Should be allways OK, so we only generate
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1076
		 * the fitting RoadBits */
7684
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
  1077
		_grow_town_result = GROWTH_SEARCH_STOPPED;
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1078
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
  1079
		switch (t1->GetActiveLayout()) {
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1080
			default: NOT_REACHED();
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1081
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1082
			case TL_NO_ROADS: /* Disallow Roads */
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1083
				return;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1084
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1085
			case TL_3X3_GRID:
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1086
			case TL_2X2_GRID:
8970
da7261f48b7e (svn r12762) -Fix: tabs after the first non-tab character are generally not okay (or lines starting with a space and then tabs).
rubidium
parents: 8969
diff changeset
  1087
				rcmd = GetTownRoadGridElement(t1, tile, target_dir);
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1088
				break;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1089
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1090
			case TL_BETTER_ROADS:
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1091
			case TL_ORIGINAL:
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1092
				rcmd = DiagDirToRoadBits(ReverseDiagDir(target_dir));
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1093
				break;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1094
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1095
	} else {
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1096
		bool allow_house = false; // Value which decides if we want to construct a house
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1097
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1098
		/* Reached a tunnel/bridge? Then continue at the other side of it. */
5445
0e8698f61eeb (svn r7698) -Fix (r7573): towns did not grow beyond bridges.
rubidium
parents: 5385
diff changeset
  1099
		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
8088
92fca5b09665 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8083
diff changeset
  1100
			if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD) {
8197
3af783256580 (svn r11760) -Codechange: unify the way how other end of a tunnel/bridge is determined at some places
smatz
parents: 8187
diff changeset
  1101
				*tile_ptr = GetOtherTunnelBridgeEnd(tile);
5445
0e8698f61eeb (svn r7698) -Fix (r7573): towns did not grow beyond bridges.
rubidium
parents: 5385
diff changeset
  1102
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1103
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1104
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  1105
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1106
		/* Possibly extend the road in a direction.
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1107
		 * Randomize a direction and if it has a road, bail out. */
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1108
		target_dir = RandomDiagDir();
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1109
		if (cur_rb & DiagDirToRoadBits(target_dir)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1110
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1111
		/* This is the tile we will reach if we extend to this direction. */
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1112
		TileIndex house_tile = TileAddByDiagDir(tile, target_dir); // position of a possible house
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1113
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1114
		/* Don't walk into water. */
7739
0b6f3da5c083 (svn r11276) -Codechange: be more consistent with naming of some accessors.
rubidium
parents: 7709
diff changeset
  1115
		if (IsWaterTile(house_tile)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1116
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
  1117
		switch (t1->GetActiveLayout()) {
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1118
			default: NOT_REACHED();
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1119
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1120
			case TL_NO_ROADS:
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1121
				allow_house = true;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1122
				break;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1123
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1124
			case TL_3X3_GRID: /* Use 2x2 grid afterwards! */
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1125
				GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir));
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1126
				/* FALL THROUGH */
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1127
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1128
			case TL_2X2_GRID:
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1129
				rcmd = GetTownRoadGridElement(t1, house_tile, target_dir);
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1130
				allow_house = (rcmd == ROAD_NONE);
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1131
				break;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1132
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1133
			case TL_BETTER_ROADS: /* Use original afterwards! */
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1134
				GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir));
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1135
				/* FALL THROUGH */
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1136
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1137
			case TL_ORIGINAL:
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1138
				 /* Allow a house at the edge. 60% chance or
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1139
				  * always ok if no road allowed. */
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1140
				rcmd = DiagDirToRoadBits(target_dir);
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
  1141
				allow_house = (!IsRoadAllowedHere(t1, house_tile, target_dir) || Chance16(6, 10));
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1142
				break;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1143
		}
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1144
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1145
		if (allow_house) {
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1146
			/* Build a house, but not if there already is a house there. */
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1147
			if (!IsTileType(house_tile, MP_HOUSE)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1148
				/* Level the land if possible */
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7931
diff changeset
  1149
				if (Chance16(1, 6)) LevelTownLand(house_tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1150
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1151
				/* And build a house.
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1152
				 * Set result to -1 if we managed to build it. */
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1153
				if (BuildTownHouse(t1, house_tile)) {
7684
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
  1154
					_grow_town_result = GROWTH_SUCCEED;
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1155
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1156
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1157
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1158
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1159
7684
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
  1160
		_grow_town_result = GROWTH_SEARCH_STOPPED;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1161
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1162
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1163
	/* Return if a water tile */
7739
0b6f3da5c083 (svn r11276) -Codechange: be more consistent with naming of some accessors.
rubidium
parents: 7709
diff changeset
  1164
	if (IsWaterTile(tile)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1165
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1166
	/* Make the roads look nicer */
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1167
	rcmd = CleanUpRoadBits(tile, rcmd);
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1168
	if (rcmd == ROAD_NONE) return;
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1169
7845
4e5c90c389ca (svn r11395) -Fix: allow town-bridges to be build on slopes (Rafal Rzepecki)
truelight
parents: 7766
diff changeset
  1170
	/* Only use the target direction for bridges to ensure they're connected.
4e5c90c389ca (svn r11395) -Fix: allow town-bridges to be build on slopes (Rafal Rzepecki)
truelight
parents: 7766
diff changeset
  1171
	 * The target_dir is as computed previously according to town layout, so
4e5c90c389ca (svn r11395) -Fix: allow town-bridges to be build on slopes (Rafal Rzepecki)
truelight
parents: 7766
diff changeset
  1172
	 * it will match it perfectly. */
4e5c90c389ca (svn r11395) -Fix: allow town-bridges to be build on slopes (Rafal Rzepecki)
truelight
parents: 7766
diff changeset
  1173
	if (GrowTownWithBridge(t1, tile, target_dir)) return;
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1174
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1175
	GrowTownWithRoad(t1, tile, rcmd);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1176
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1177
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1178
/** Returns "growth" if a house was built, or no if the build failed.
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1179
 * @param t town to inquiry
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1180
 * @param tile to inquiry
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1181
 * @return something other than zero(0)if town expansion was possible
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1182
 */
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
  1183
static int GrowTownAtRoad(Town *t, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1184
{
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1185
	/* Special case.
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1186
	 * @see GrowTownInTile Check the else if
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1187
	 */
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1188
	DiagDirection target_dir = DIAGDIR_END; // The direction in which we want to extend the town
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1189
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1190
	TILE_ASSERT(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1191
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1192
	/* Number of times to search.
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1193
	 * Better roads, 2X2 and 3X3 grid grow quite fast so we give
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1194
	 * them a little handicap. */
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
  1195
	switch (t->GetActiveLayout()) {
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1196
		case TL_BETTER_ROADS:
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1197
			_grow_town_result = 10 + t->num_houses * 2 / 9;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1198
			break;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1199
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1200
		case TL_3X3_GRID:
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1201
		case TL_2X2_GRID:
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1202
			_grow_town_result = 10 + t->num_houses * 1 / 9;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1203
			break;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1204
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1205
		default:
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1206
			_grow_town_result = 10 + t->num_houses * 4 / 9;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1207
			break;
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1208
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1209
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1210
	do {
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1211
		RoadBits cur_rb = GetTownRoadBits(tile); // The RoadBits of the current tile
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1212
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1213
		/* Try to grow the town from this point */
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1214
		GrowTownInTile(&tile, cur_rb, target_dir, t);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1215
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1216
		/* Exclude the source position from the bitmask
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1217
		 * and return if no more road blocks available */
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1218
		cur_rb &= ~DiagDirToRoadBits(ReverseDiagDir(target_dir));
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1219
		if (cur_rb == ROAD_NONE)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1220
			return _grow_town_result;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1221
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1222
		/* Select a random bit from the blockmask, walk a step
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1223
		 * and continue the search from there. */
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1224
		do target_dir = RandomDiagDir(); while (!(cur_rb & DiagDirToRoadBits(target_dir)));
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1225
		tile = TileAddByDiagDir(tile, target_dir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1226
9341
bd60c3b2d1e0 (svn r13233) -Fix: Replace some (incorrect) evaluations of TileOwner by RoadOwner.
frosch
parents: 9334
diff changeset
  1227
		if (IsTileType(tile, MP_ROAD) && !IsRoadDepot(tile) && HasTileRoadType(tile, ROADTYPE_ROAD)) {
1327
6e507eb10edd (svn r1831) -Fix: Scenario Editor now handles human-made roads better (try to build
truelight
parents: 1309
diff changeset
  1228
			/* Don't allow building over roads of other cities */
9341
bd60c3b2d1e0 (svn r13233) -Fix: Replace some (incorrect) evaluations of TileOwner by RoadOwner.
frosch
parents: 9334
diff changeset
  1229
			if (IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN) && GetTownByTile(tile) != t) {
7684
a79c09246c27 (svn r11215) -Codechange: replace magic constants with less magic symbols. Patch by skidd13.
rubidium
parents: 7666
diff changeset
  1230
				_grow_town_result = GROWTH_SUCCEED;
9341
bd60c3b2d1e0 (svn r13233) -Fix: Replace some (incorrect) evaluations of TileOwner by RoadOwner.
frosch
parents: 9334
diff changeset
  1231
			} else if (IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_NONE) && _game_mode == GM_EDITOR) {
1327
6e507eb10edd (svn r1831) -Fix: Scenario Editor now handles human-made roads better (try to build
truelight
parents: 1309
diff changeset
  1232
				/* If we are in the SE, and this road-piece has no town owner yet, it just found an
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
  1233
				 * owner :) (happy happy happy road now) */
9341
bd60c3b2d1e0 (svn r13233) -Fix: Replace some (incorrect) evaluations of TileOwner by RoadOwner.
frosch
parents: 9334
diff changeset
  1234
				SetRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN);
3432
650dd1972479 (svn r4261) CodeChange : Add and Use Accessor for Houses Construction. And cleaning on town.flags12 too
belugas
parents: 3426
diff changeset
  1235
				SetTownIndex(tile, t->index);
1327
6e507eb10edd (svn r1831) -Fix: Scenario Editor now handles human-made roads better (try to build
truelight
parents: 1309
diff changeset
  1236
			}
6e507eb10edd (svn r1831) -Fix: Scenario Editor now handles human-made roads better (try to build
truelight
parents: 1309
diff changeset
  1237
		}
1280
33a251c30758 (svn r1784) -Fix: removed ClosestTownFromTile where possible, or replaced it
truelight
parents: 1264
diff changeset
  1238
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1239
		/* Max number of times is checked. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1240
	} while (--_grow_town_result >= 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1241
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1242
	return (_grow_town_result == -2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1243
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1244
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1245
/**
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1246
 * Generate a random road block.
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1247
 * The probability of a straight road
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1248
 * is somewhat higher than a curved.
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1249
 *
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1250
 * @return A RoadBits value with 2 bits set
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1251
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
  1252
static RoadBits GenRandomRoadBits()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1253
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1254
	uint32 r = Random();
2140
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2070
diff changeset
  1255
	uint a = GB(r, 0, 2);
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2070
diff changeset
  1256
	uint b = GB(r, 8, 2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1257
	if (a == b) b ^= 2;
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1258
	return (RoadBits)((ROAD_NW << a) + (ROAD_NW << b));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1259
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1260
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1261
/** Grow the town
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1262
 * @param t town to grow
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1263
 * @return true iff a house was built
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1264
 */
2817
cdf488223c23 (svn r3365) Staticise 36 functions
tron
parents: 2791
diff changeset
  1265
static bool GrowTown(Town *t)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1266
{
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1267
	/* Let the town be a ghost town
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1268
	 * The player wanted it in such a way. Thus there he has it. ;)
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1269
	 * Never reached in editor mode. */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  1270
	if (_settings_game.economy.town_layout == TL_NO_ROADS && _generating_world) {
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1271
		return false;
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1272
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1273
909
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1274
	static const TileIndexDiffC _town_coord_mod[] = {
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1275
		{-1,  0},
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1276
		{ 1,  1},
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1277
		{ 1, -1},
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1278
		{-1, -1},
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1279
		{-1,  0},
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1280
		{ 0,  2},
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1281
		{ 2,  0},
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1282
		{ 0, -2},
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1283
		{-1, -1},
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1284
		{-2,  2},
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1285
		{ 2,  2},
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1286
		{ 2, -2},
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1287
		{ 0,  0}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1288
	};
6571
6a8a1d0835d9 (svn r9779) -Feature: Add the possiblity to choose different road patterns for towns to use.
belugas
parents: 6560
diff changeset
  1289
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  1290
	/* Current "company" is a town */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  1291
	CompanyID old_company = _current_company;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  1292
	_current_company = OWNER_TOWN;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1293
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  1294
	TileIndex tile = t->xy; // The tile we are working with ATM
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1295
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1296
	/* Find a road that we can base the construction on. */
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1297
	const TileIndexDiffC *ptr;
909
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1298
	for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
7566
a8dd64b03224 (svn r11091) -Codechange: Partial rewrite of the road management code of towns, in order to make it more readable and with improved performance. (FS#1161 by skidd13)
belugas
parents: 7549
diff changeset
  1299
		if (GetTownRoadBits(tile) != ROAD_NONE) {
260
4819bcce8389 (svn r266) -Fix: hopefully fixed the desync problem nicely (and reverted the
truelight
parents: 193
diff changeset
  1300
			int r = GrowTownAtRoad(t, tile);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  1301
			_current_company = old_company;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1302
			return r != 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1303
		}
909
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1304
		tile = TILE_ADD(tile, ToTileIndexDiff(*ptr));
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1305
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1306
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1307
	/* No road available, try to build a random road block by
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1308
	 * clearing some land and then building a road there. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1309
	tile = t->xy;
909
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1310
	for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
4317
19c5108cd0ec (svn r5970) -Fix [FS#49]: town-growth removed houses under construction to make way for road. Not wanted behavoir (Rubidium)
truelight
parents: 4300
diff changeset
  1311
		/* Only work with plain land that not already has a house */
19c5108cd0ec (svn r5970) -Fix [FS#49]: town-growth removed houses under construction to make way for road. Not wanted behavoir (Rubidium)
truelight
parents: 4300
diff changeset
  1312
		if (!IsTileType(tile, MP_HOUSE) && GetTileSlope(tile, NULL) == SLOPE_FLAT) {
6946
a48b23f1cc8a (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 6943
diff changeset
  1313
			if (CmdSucceeded(DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR))) {
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3433
diff changeset
  1314
				DoCommand(tile, GenRandomRoadBits(), t->index, DC_EXEC | DC_AUTO, CMD_BUILD_ROAD);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  1315
				_current_company = old_company;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1316
				return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1317
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1318
		}
909
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1319
		tile = TILE_ADD(tile, ToTileIndexDiff(*ptr));
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
  1320
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1321
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  1322
	_current_company = old_company;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1323
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1324
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1325
8707
e57a09994e12 (svn r12381) -Fix [FS1835] [FS1535] (r11855): The number of houses wasn't computed right. A few other things regaring the updating had to be changed. Big thanks for support to frosch123 and SmatZ, to name just a few. (Inspired by a patch of bilbo)
skidd13
parents: 8618
diff changeset
  1326
void UpdateTownRadius(Town *t)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1327
{
8944
80b1d6fe25c5 (svn r12731) -Fix (r12726): copying a 16bit array into 32bit wouldn't work
smatz
parents: 8943
diff changeset
  1328
	static const uint32 _town_squared_town_zone_radius_data[23][5] = {
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1329
		{  4,  0,  0,  0,  0}, // 0
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1330
		{ 16,  0,  0,  0,  0},
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1331
		{ 25,  0,  0,  0,  0},
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1332
		{ 36,  0,  0,  0,  0},
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1333
		{ 49,  0,  4,  0,  0},
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1334
		{ 64,  0,  4,  0,  0}, // 20
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1335
		{ 64,  0,  9,  0,  1},
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1336
		{ 64,  0,  9,  0,  4},
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1337
		{ 64,  0, 16,  0,  4},
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1338
		{ 81,  0, 16,  0,  4},
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1339
		{ 81,  0, 16,  0,  4}, // 40
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1340
		{ 81,  0, 25,  0,  9},
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1341
		{ 81, 36, 25,  0,  9},
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1342
		{ 81, 36, 25, 16,  9},
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1343
		{ 81, 49,  0, 25,  9},
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1344
		{ 81, 64,  0, 25,  9}, // 60
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1345
		{ 81, 64,  0, 36,  9},
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1346
		{ 81, 64,  0, 36, 16},
470
08b29b012837 (svn r703) Attempt to improve the town growth algorithm - now it scales over 76 houses, is slightly exponential and travels further for larger towns.
pasky
parents: 468
diff changeset
  1347
		{100, 81,  0, 49, 16},
08b29b012837 (svn r703) Attempt to improve the town growth algorithm - now it scales over 76 houses, is slightly exponential and travels further for larger towns.
pasky
parents: 468
diff changeset
  1348
		{100, 81,  0, 49, 25},
08b29b012837 (svn r703) Attempt to improve the town growth algorithm - now it scales over 76 houses, is slightly exponential and travels further for larger towns.
pasky
parents: 468
diff changeset
  1349
		{121, 81,  0, 49, 25}, // 80
08b29b012837 (svn r703) Attempt to improve the town growth algorithm - now it scales over 76 houses, is slightly exponential and travels further for larger towns.
pasky
parents: 468
diff changeset
  1350
		{121, 81,  0, 49, 25},
08b29b012837 (svn r703) Attempt to improve the town growth algorithm - now it scales over 76 houses, is slightly exponential and travels further for larger towns.
pasky
parents: 468
diff changeset
  1351
		{121, 81,  0, 49, 36}, // 88
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1352
	};
470
08b29b012837 (svn r703) Attempt to improve the town growth algorithm - now it scales over 76 houses, is slightly exponential and travels further for larger towns.
pasky
parents: 468
diff changeset
  1353
08b29b012837 (svn r703) Attempt to improve the town growth algorithm - now it scales over 76 houses, is slightly exponential and travels further for larger towns.
pasky
parents: 468
diff changeset
  1354
	if (t->num_houses < 92) {
8943
53044ba4676f (svn r12726) -Fix [FS#1877]: overflow causing strange building behaviour in towns.
rubidium
parents: 8942
diff changeset
  1355
		memcpy(t->squared_town_zone_radius, _town_squared_town_zone_radius_data[t->num_houses / 4], sizeof(t->squared_town_zone_radius));
470
08b29b012837 (svn r703) Attempt to improve the town growth algorithm - now it scales over 76 houses, is slightly exponential and travels further for larger towns.
pasky
parents: 468
diff changeset
  1356
	} else {
08b29b012837 (svn r703) Attempt to improve the town growth algorithm - now it scales over 76 houses, is slightly exponential and travels further for larger towns.
pasky
parents: 468
diff changeset
  1357
		int mass = t->num_houses / 8;
8943
53044ba4676f (svn r12726) -Fix [FS#1877]: overflow causing strange building behaviour in towns.
rubidium
parents: 8942
diff changeset
  1358
		/* Actually we are proportional to sqrt() but that's right because we are covering an area.
53044ba4676f (svn r12726) -Fix [FS#1877]: overflow causing strange building behaviour in towns.
rubidium
parents: 8942
diff changeset
  1359
		 * The offsets are to make sure the radii do not decrease in size when going from the table
53044ba4676f (svn r12726) -Fix [FS#1877]: overflow causing strange building behaviour in towns.
rubidium
parents: 8942
diff changeset
  1360
		 * to the calculated value.*/
53044ba4676f (svn r12726) -Fix [FS#1877]: overflow causing strange building behaviour in towns.
rubidium
parents: 8942
diff changeset
  1361
		t->squared_town_zone_radius[0] = mass * 15 - 40;
53044ba4676f (svn r12726) -Fix [FS#1877]: overflow causing strange building behaviour in towns.
rubidium
parents: 8942
diff changeset
  1362
		t->squared_town_zone_radius[1] = mass * 9 - 15;
53044ba4676f (svn r12726) -Fix [FS#1877]: overflow causing strange building behaviour in towns.
rubidium
parents: 8942
diff changeset
  1363
		t->squared_town_zone_radius[2] = 0;
53044ba4676f (svn r12726) -Fix [FS#1877]: overflow causing strange building behaviour in towns.
rubidium
parents: 8942
diff changeset
  1364
		t->squared_town_zone_radius[3] = mass * 5 - 5;
53044ba4676f (svn r12726) -Fix [FS#1877]: overflow causing strange building behaviour in towns.
rubidium
parents: 8942
diff changeset
  1365
		t->squared_town_zone_radius[4] = mass * 3 + 5;
470
08b29b012837 (svn r703) Attempt to improve the town growth algorithm - now it scales over 76 houses, is slightly exponential and travels further for larger towns.
pasky
parents: 468
diff changeset
  1366
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1367
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1368
1421
0532cbf429f1 (svn r1925) Fixed an infinite loop if the town generator runs out of town names. The number of generated towns is then limited by the number of available names.
pasky
parents: 1377
diff changeset
  1369
static bool CreateTownName(uint32 *townnameparts)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1370
{
6956
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6950
diff changeset
  1371
	extern int _nb_orig_names;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1372
	Town *t2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1373
	char buf1[64];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1374
	char buf2[64];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1375
	uint32 r;
1421
0532cbf429f1 (svn r1925) Fixed an infinite loop if the town generator runs out of town names. The number of generated towns is then limited by the number of available names.
pasky
parents: 1377
diff changeset
  1376
	/* Do not set too low tries, since when we run out of names, we loop
0532cbf429f1 (svn r1925) Fixed an infinite loop if the town generator runs out of town names. The number of generated towns is then limited by the number of available names.
pasky
parents: 1377
diff changeset
  1377
	 * for #tries only one time anyway - then we stop generating more
0532cbf429f1 (svn r1925) Fixed an infinite loop if the town generator runs out of town names. The number of generated towns is then limited by the number of available names.
pasky
parents: 1377
diff changeset
  1378
	 * towns. Do not show it too high neither, since looping through all
0532cbf429f1 (svn r1925) Fixed an infinite loop if the town generator runs out of town names. The number of generated towns is then limited by the number of available names.
pasky
parents: 1377
diff changeset
  1379
	 * the other towns may take considerable amount of time (10000 is
0532cbf429f1 (svn r1925) Fixed an infinite loop if the town generator runs out of town names. The number of generated towns is then limited by the number of available names.
pasky
parents: 1377
diff changeset
  1380
	 * too much). */
0532cbf429f1 (svn r1925) Fixed an infinite loop if the town generator runs out of town names. The number of generated towns is then limited by the number of available names.
pasky
parents: 1377
diff changeset
  1381
	int tries = 1000;
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  1382
	bool grf = (_settings_game.game_creation.town_name >= _nb_orig_names);
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  1383
	uint32 grfid = grf ? GetGRFTownNameId(_settings_game.game_creation.town_name - _nb_orig_names) : 0;
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  1384
	uint16 townnametype = grf ? GetGRFTownNameType(_settings_game.game_creation.town_name - _nb_orig_names) : SPECSTR_TOWNNAME_START + _settings_game.game_creation.town_name;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1385
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1386
	assert(townnameparts != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1387
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1388
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1389
restart:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1390
		r = Random();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1391
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 516
diff changeset
  1392
		SetDParam(0, r);
6956
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6950
diff changeset
  1393
		if (grf && grfid != 0) {
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6950
diff changeset
  1394
			GRFTownNameGenerate(buf1, grfid, townnametype, r, lastof(buf1));
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6950
diff changeset
  1395
		} else {
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6950
diff changeset
  1396
			GetString(buf1, townnametype, lastof(buf1));
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6950
diff changeset
  1397
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  1398
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1399
		/* Check size and width */
9914
c07d0352d8d5 (svn r14064) -Fix [FS#1752]: check for the length of strings (in bytes) in the command. Checking for the length in pixels is impossible because that differs per client.
rubidium
parents: 9714
diff changeset
  1400
		if (strlen(buf1) >= MAX_LENGTH_TOWN_NAME_BYTES || GetStringBoundingBox(buf1).width > MAX_LENGTH_TOWN_NAME_PIXELS) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1401
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1402
		FOR_ALL_TOWNS(t2) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1403
			/* We can't just compare the numbers since
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1404
			 * several numbers may map to a single name. */
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  1405
			SetDParam(0, t2->index);
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4850
diff changeset
  1406
			GetString(buf2, STR_TOWN, lastof(buf2));
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  1407
			if (strcmp(buf1, buf2) == 0) {
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  1408
				if (tries-- < 0) return false;
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  1409
				goto restart;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1410
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1411
		}
1421
0532cbf429f1 (svn r1925) Fixed an infinite loop if the town generator runs out of town names. The number of generated towns is then limited by the number of available names.
pasky
parents: 1377
diff changeset
  1412
		*townnameparts = r;
0532cbf429f1 (svn r1925) Fixed an infinite loop if the town generator runs out of town names. The number of generated towns is then limited by the number of available names.
pasky
parents: 1377
diff changeset
  1413
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1414
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1415
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1416
1377
7ab329d1fc28 (svn r1881) -Fix: [ 1119308 ] Max passengers / mail variables are now 32 bit
celestar
parents: 1362
diff changeset
  1417
void UpdateTownMaxPass(Town *t)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1418
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1419
	t->max_pass = t->population >> 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1420
	t->max_mail = t->population >> 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1421
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1422
6486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1423
/**
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1424
 * Does the actual town creation.
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1425
 *
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1426
 * @param t The town
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1427
 * @param tile Where to put it
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1428
 * @param townnameparts The town name
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1429
 * @param size_mode How the size should be determined
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1430
 * @param size Parameter for size determination
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1431
 */
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1432
static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSizeMode size_mode, uint size)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1433
{
6956
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6950
diff changeset
  1434
	extern int _nb_orig_names;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1435
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1436
	t->xy = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1437
	t->num_houses = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1438
	t->time_until_rebuild = 10;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1439
	UpdateTownRadius(t);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1440
	t->flags12 = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1441
	t->population = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1442
	t->grow_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1443
	t->growth_rate = 250;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1444
	t->new_max_pass = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1445
	t->new_max_mail = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1446
	t->new_act_pass = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1447
	t->new_act_mail = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1448
	t->max_pass = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1449
	t->max_mail = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1450
	t->act_pass = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1451
	t->act_mail = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1452
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1453
	t->pct_pass_transported = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1454
	t->pct_mail_transported = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1455
	t->fund_buildings_months = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1456
	t->new_act_food = 0;
4
cad62d5f9708 (svn r5) -Fix: townname generation of TTDLX savegames. All work
darkvater
parents: 0
diff changeset
  1457
	t->new_act_water = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1458
	t->act_food = 0;
4
cad62d5f9708 (svn r5) -Fix: townname generation of TTDLX savegames. All work
darkvater
parents: 0
diff changeset
  1459
	t->act_water = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1460
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  1461
	for (uint i = 0; i != MAX_COMPANIES; i++) t->ratings[i] = RATING_INITIAL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1462
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1463
	t->have_ratings = 0;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  1464
	t->exclusivity = INVALID_COMPANY;
121
c2f18f4d8be1 (svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents: 39
diff changeset
  1465
	t->exclusive_counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1466
	t->statues = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1467
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  1468
	if (_settings_game.game_creation.town_name < _nb_orig_names) {
6956
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6950
diff changeset
  1469
		/* Original town name */
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6950
diff changeset
  1470
		t->townnamegrfid = 0;
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  1471
		t->townnametype = SPECSTR_TOWNNAME_START + _settings_game.game_creation.town_name;
6956
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6950
diff changeset
  1472
	} else {
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6950
diff changeset
  1473
		/* Newgrf town name */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  1474
		t->townnamegrfid = GetGRFTownNameId(_settings_game.game_creation.town_name  - _nb_orig_names);
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  1475
		t->townnametype  = GetGRFTownNameType(_settings_game.game_creation.town_name - _nb_orig_names);
6956
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6950
diff changeset
  1476
	}
1421
0532cbf429f1 (svn r1925) Fixed an infinite loop if the town generator runs out of town names. The number of generated towns is then limited by the number of available names.
pasky
parents: 1377
diff changeset
  1477
	t->townnameparts = townnameparts;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  1478
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1479
	UpdateTownVirtCoord(t);
9387
ede823d445f5 (svn r13297) -Codechange: Use GUIList for the town directory window
peter1138
parents: 9358
diff changeset
  1480
	InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1481
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
  1482
	t->InitializeLayout();
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
  1483
6486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1484
	/* Random town size. */
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1485
	int x = (Random() & 0xF) + 8;
6486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1487
	switch (size_mode) {
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1488
		default: NOT_REACHED();
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1489
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1490
		case TSM_RANDOM:
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1491
			t->larger_town = false;
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1492
			break;
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1493
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1494
		case TSM_FIXED:
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1495
			x = size * 16 + 3;
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1496
			t->larger_town = false;
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1497
			break;
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1498
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1499
		case TSM_CITY:
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  1500
			x *= _settings_game.economy.initial_city_size;
6486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1501
			t->larger_town = true;
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1502
			break;
3674
d6c991bdcbe1 (svn r4591) -Fix (FS#122) Game no longer errors out when "Many random towns" is selected in the scenario editor.
celestar
parents: 3657
diff changeset
  1503
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1504
9334
28ac6c8e0795 (svn r13226) -Feature: Allow to have more than only two airports per town. The number of airports is now controlled by the noise each of them generates, the distance from town's center and how tolerant the town is.
belugas
parents: 9322
diff changeset
  1505
	t->noise_reached = 0;
28ac6c8e0795 (svn r13226) -Feature: Allow to have more than only two airports per town. The number of airports is now controlled by the noise each of them generates, the distance from town's center and how tolerant the town is.
belugas
parents: 9322
diff changeset
  1506
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1507
	t->num_houses += x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1508
	UpdateTownRadius(t);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1509
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1510
	int i = x * 4;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1511
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1512
		GrowTown(t);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1513
	} while (--i);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1514
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1515
	t->num_houses -= x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1516
	UpdateTownRadius(t);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1517
	UpdateTownMaxPass(t);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1518
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1519
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  1520
/** Create a new town.
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  1521
 * This obviously only works in the scenario editor. Function not removed
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  1522
 * as it might be possible in the future to fund your own town :)
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3433
diff changeset
  1523
 * @param tile coordinates where town is built
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1524
 * @param flags type of operation
6486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1525
 * @param p1 size of the town (0 = small, 1 = medium, 2 = large)
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1526
 * @param p2 size mode (@see TownSizeMode)
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  1527
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6914
diff changeset
  1528
CommandCost CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1529
{
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  1530
	/* Only in the scenario editor */
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  1531
	if (_game_mode != GM_EDITOR) return CMD_ERROR;
6486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1532
	if (p2 > TSM_CITY) return CMD_ERROR;
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  1533
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1534
	/* Check if too close to the edge of map */
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1209
diff changeset
  1535
	if (DistanceFromEdge(tile) < 12)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1536
		return_cmd_error(STR_0237_TOO_CLOSE_TO_EDGE_OF_MAP);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1537
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1538
	/* Can only build on clear flat areas, possibly with trees. */
5085
19b2b317c7eb (svn r7152) -Feature (FS#396): allow towns to be built on top of trees in the scenario editor.
rubidium
parents: 5065
diff changeset
  1539
	if ((!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES)) || GetTileSlope(tile, NULL) != SLOPE_FLAT) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1540
		return_cmd_error(STR_0239_SITE_UNSUITABLE);
2986
f440014bdbf3 (svn r3561) Don't use FindLandscapeHeightByTile() when it's overkill. Also use a sprite enum instead of a magic number.
tron
parents: 2958
diff changeset
  1541
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1542
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1543
	/* Check distance to all other towns. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1544
	if (IsCloseToTown(tile, 20))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1545
		return_cmd_error(STR_0238_TOO_CLOSE_TO_ANOTHER_TOWN);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1546
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1547
	uint32 townnameparts;
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1548
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1549
	/* Get a unique name for the town. */
1421
0532cbf429f1 (svn r1925) Fixed an infinite loop if the town generator runs out of town names. The number of generated towns is then limited by the number of available names.
pasky
parents: 1377
diff changeset
  1550
	if (!CreateTownName(&townnameparts))
0532cbf429f1 (svn r1925) Fixed an infinite loop if the town generator runs out of town names. The number of generated towns is then limited by the number of available names.
pasky
parents: 1377
diff changeset
  1551
		return_cmd_error(STR_023A_TOO_MANY_TOWNS);
0532cbf429f1 (svn r1925) Fixed an infinite loop if the town generator runs out of town names. The number of generated towns is then limited by the number of available names.
pasky
parents: 1377
diff changeset
  1552
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1553
	/* Allocate town struct */
9036
6368fe55fd6d (svn r12855) -Codechange: do not use autoptr's for testing whether certain objects can be build, but check it directly in the pool so we do not have to call destructors in the testing phase. Stations still use the autoptr though.
rubidium
parents: 9020
diff changeset
  1554
	if (!Town::CanAllocateItem()) return_cmd_error(STR_023A_TOO_MANY_TOWNS);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1555
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1556
	/* Create the town */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1557
	if (flags & DC_EXEC) {
9036
6368fe55fd6d (svn r12855) -Codechange: do not use autoptr's for testing whether certain objects can be build, but check it directly in the pool so we do not have to call destructors in the testing phase. Stations still use the autoptr though.
rubidium
parents: 9020
diff changeset
  1558
		Town *t = new Town(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1559
		_generating_world = true;
6486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1560
		DoCreateTown(t, tile, townnameparts, (TownSizeMode)p2, p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1561
		_generating_world = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1562
	}
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1563
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1564
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1565
6486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1566
Town *CreateRandomTown(uint attempts, TownSizeMode mode, uint size)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1567
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1568
	do {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1569
		/* Generate a tile index not too close from the edge */
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1570
		TileIndex tile = RandomTile();
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2891
diff changeset
  1571
		if (DistanceFromEdge(tile) < 20) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1572
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1573
		/* Make sure the tile is plain */
3636
a36cc46e754d (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3585
diff changeset
  1574
		if (!IsTileType(tile, MP_CLEAR) || GetTileSlope(tile, NULL) != SLOPE_FLAT) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1575
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1576
		/* Check not too close to a town */
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2891
diff changeset
  1577
		if (IsCloseToTown(tile, 20)) continue;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  1578
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1579
		uint32 townnameparts;
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1580
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1581
		/* Get a unique name for the town. */
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2891
diff changeset
  1582
		if (!CreateTownName(&townnameparts)) break;
1421
0532cbf429f1 (svn r1925) Fixed an infinite loop if the town generator runs out of town names. The number of generated towns is then limited by the number of available names.
pasky
parents: 1377
diff changeset
  1583
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1584
		/* Allocate a town struct */
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1585
		Town *t = new Town(tile);
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2891
diff changeset
  1586
		if (t == NULL) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1587
6486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1588
		DoCreateTown(t, tile, townnameparts, mode, size);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1589
		return t;
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1590
	} while (--attempts != 0);
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1591
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1592
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1593
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1594
9334
28ac6c8e0795 (svn r13226) -Feature: Allow to have more than only two airports per town. The number of airports is now controlled by the noise each of them generates, the distance from town's center and how tolerant the town is.
belugas
parents: 9322
diff changeset
  1595
static const byte _num_initial_towns[4] = {5, 11, 23, 46};  // very low, low, normal, high
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1596
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
  1597
bool GenerateTowns()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1598
{
1362
bceb3c57353d (svn r1866) -Fix: Intercepted generated maps with 0 towns on it. Currently just an
celestar
parents: 1335
diff changeset
  1599
	uint num = 0;
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  1600
	uint n = ScaleByMapSize(_num_initial_towns[_settings_game.difficulty.number_towns] + (Random() & 7));
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  1601
	uint num_cities = _settings_game.economy.larger_towns == 0 ? 0 : n / _settings_game.economy.larger_towns;
1202
4d2a20c50760 (svn r1706) Implement ScaleByMapSize() and ScaleByMapSize1D()
tron
parents: 1174
diff changeset
  1602
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4261
diff changeset
  1603
	SetGeneratingWorldProgress(GWP_TOWN, n);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4261
diff changeset
  1604
1362
bceb3c57353d (svn r1866) -Fix: Intercepted generated maps with 0 towns on it. Currently just an
celestar
parents: 1335
diff changeset
  1605
	do {
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4261
diff changeset
  1606
		IncreaseGeneratingWorldProgress(GWP_TOWN);
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1607
		/* try 20 times to create a random-sized town for the first loop. */
6486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1608
		TownSizeMode mode = num_cities > 0 ? TSM_CITY : TSM_RANDOM;
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  1609
		if (CreateRandomTown(20, mode, _settings_game.economy.initial_city_size) != NULL) num++;
6486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1610
		if (num_cities > 0) num_cities--;
1362
bceb3c57353d (svn r1866) -Fix: Intercepted generated maps with 0 towns on it. Currently just an
celestar
parents: 1335
diff changeset
  1611
	} while (--n);
bceb3c57353d (svn r1866) -Fix: Intercepted generated maps with 0 towns on it. Currently just an
celestar
parents: 1335
diff changeset
  1612
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1613
	/* give it a last try, but now more aggressive */
6486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  1614
	if (num == 0 && CreateRandomTown(10000, TSM_RANDOM, 0) == NULL) {
5247
1f982de55b88 (svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.
matthijs
parents: 5216
diff changeset
  1615
		if (GetNumTowns() == 0) {
4384
7e0d3ed719d9 (svn r6137) -Codechange: some very minor cleanups:
truelight
parents: 4357
diff changeset
  1616
			/* XXX - can we handle that more gracefully? */
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9463
diff changeset
  1617
			if (_game_mode != GM_EDITOR) usererror("Could not generate any town");
2430
b8bb9d74253b (svn r2956) - Fix: [ 1253736 ] creating many town crash to desktop. Now it 'dies' with an ingame error message informing the gamer if it couldn't generate any towns in user-space. Still if it happens during new-game generation it crashes since we don't yet have actions to do in such a circumstance.
Darkvater
parents: 2425
diff changeset
  1618
4384
7e0d3ed719d9 (svn r6137) -Codechange: some very minor cleanups:
truelight
parents: 4357
diff changeset
  1619
			return false;
4000
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3983
diff changeset
  1620
		}
1362
bceb3c57353d (svn r1866) -Fix: Intercepted generated maps with 0 towns on it. Currently just an
celestar
parents: 1335
diff changeset
  1621
	}
2430
b8bb9d74253b (svn r2956) - Fix: [ 1253736 ] creating many town crash to desktop. Now it 'dies' with an ingame error message informing the gamer if it couldn't generate any towns in user-space. Still if it happens during new-game generation it crashes since we don't yet have actions to do in such a circumstance.
Darkvater
parents: 2425
diff changeset
  1622
b8bb9d74253b (svn r2956) - Fix: [ 1253736 ] creating many town crash to desktop. Now it 'dies' with an ingame error message informing the gamer if it couldn't generate any towns in user-space. Still if it happens during new-game generation it crashes since we don't yet have actions to do in such a circumstance.
Darkvater
parents: 2425
diff changeset
  1623
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1624
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1625
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1626
8308
bc75a4a27ec9 (svn r11873) -Codechange: less a few magical numbers and a tiny bit more comments on town zones
belugas
parents: 8291
diff changeset
  1627
/** Returns the bit corresponding to the town zone of the specified tile
8943
53044ba4676f (svn r12726) -Fix [FS#1877]: overflow causing strange building behaviour in towns.
rubidium
parents: 8942
diff changeset
  1628
 * @param t Town on which town zone is to be found
53044ba4676f (svn r12726) -Fix [FS#1877]: overflow causing strange building behaviour in towns.
rubidium
parents: 8942
diff changeset
  1629
 * @param tile TileIndex where town zone needs to be found
8308
bc75a4a27ec9 (svn r11873) -Codechange: less a few magical numbers and a tiny bit more comments on town zones
belugas
parents: 8291
diff changeset
  1630
 * @return the bit position of the given zone, as defined in HouseZones
bc75a4a27ec9 (svn r11873) -Codechange: less a few magical numbers and a tiny bit more comments on town zones
belugas
parents: 8291
diff changeset
  1631
 */
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1632
HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1633
{
4000
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3983
diff changeset
  1634
	uint dist = DistanceSquare(tile, t->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1635
8308
bc75a4a27ec9 (svn r11873) -Codechange: less a few magical numbers and a tiny bit more comments on town zones
belugas
parents: 8291
diff changeset
  1636
	if (t->fund_buildings_months && dist <= 25) return HZB_TOWN_CENTRE;
bc75a4a27ec9 (svn r11873) -Codechange: less a few magical numbers and a tiny bit more comments on town zones
belugas
parents: 8291
diff changeset
  1637
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1638
	HouseZonesBits smallest = HZB_TOWN_EDGE;
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1639
	for (HouseZonesBits i = HZB_BEGIN; i < HZB_END; i++) {
8943
53044ba4676f (svn r12726) -Fix [FS#1877]: overflow causing strange building behaviour in towns.
rubidium
parents: 8942
diff changeset
  1640
		if (dist < t->squared_town_zone_radius[i]) smallest = i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1641
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1642
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1643
	return smallest;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1644
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1645
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1646
/**
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1647
 * Clears tile and builds a house or house part.
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1648
 * @param t tile index
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1649
 * @param tid Town index
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1650
 * @param counter of construction step
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1651
 * @param stage of construction (used for drawing)
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1652
 * @param type of house. Index into house specs array
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1653
 * @param random_bits required for newgrf houses
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1654
 * @pre house can be built here
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1655
 */
9522
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  1656
static inline void ClearMakeHouseTile(TileIndex tile, Town *t, byte counter, byte stage, HouseID type, byte random_bits)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1657
{
9436
2eabc2c8ac08 (svn r13351) -Codechange: disable warnings about unused variable for builds without asserts
smatz
parents: 9413
diff changeset
  1658
	CommandCost cc = DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR);
8618
f71a4a047db3 (svn r12201) -Fix (r12060): compilation warnings - uninitialized variable when compiling with assert disabled
smatz
parents: 8616
diff changeset
  1659
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1660
	assert(CmdSucceeded(cc));
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1661
9522
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  1662
	IncreaseBuildingCount(t, type);
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  1663
	MakeHouseTile(tile, t->index, counter, stage, type, random_bits);
9006
7666e7b47088 (svn r12801) -Codechange: remove the dependency of function.h in town_map.h
rubidium
parents: 8970
diff changeset
  1664
	if (GetHouseSpecs(type)->building_flags & BUILDING_IS_ANIMATED) AddAnimatedTile(tile);
7666e7b47088 (svn r12801) -Codechange: remove the dependency of function.h in town_map.h
rubidium
parents: 8970
diff changeset
  1665
7666e7b47088 (svn r12801) -Codechange: remove the dependency of function.h in town_map.h
rubidium
parents: 8970
diff changeset
  1666
	MarkTileDirtyByTile(tile);
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1667
}
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1668
8580
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1669
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1670
/**
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1671
 * Write house information into the map. For houses > 1 tile, all tiles are marked.
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1672
 * @param t tile index
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1673
 * @param tid Town index
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1674
 * @param counter of construction step
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1675
 * @param stage of construction (used for drawing)
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1676
 * @param type of house. Index into house specs array
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1677
 * @param random_bits required for newgrf houses
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1678
 * @pre house can be built here
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1679
 */
9522
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  1680
static void MakeTownHouse(TileIndex t, Town *town, byte counter, byte stage, HouseID type, byte random_bits)
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1681
{
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1682
	BuildingFlags size = GetHouseSpecs(type)->building_flags;
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1683
9522
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  1684
	ClearMakeHouseTile(t, town, counter, stage, type, random_bits);
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  1685
	if (size & BUILDING_2_TILES_Y)   ClearMakeHouseTile(t + TileDiffXY(0, 1), town, counter, stage, ++type, random_bits);
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  1686
	if (size & BUILDING_2_TILES_X)   ClearMakeHouseTile(t + TileDiffXY(1, 0), town, counter, stage, ++type, random_bits);
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  1687
	if (size & BUILDING_HAS_4_TILES) ClearMakeHouseTile(t + TileDiffXY(1, 1), town, counter, stage, ++type, random_bits);
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1688
}
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1689
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1690
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1691
/**
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1692
 * Checks if a house can be built here. Important is slope, bridge above
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1693
 * and ability to clear the land.
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1694
 * @param tile tile to check
8581
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1695
 * @param town town that is checking
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1696
 * @param noslope are slopes (foundations) allowed?
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1697
 * @return true iff house can be built here
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1698
 */
8581
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1699
static inline bool CanBuildHouseHere(TileIndex tile, TownID town, bool noslope)
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1700
{
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1701
	/* cannot build on these slopes... */
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1702
	Slope slope = GetTileSlope(tile, NULL);
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1703
	if ((noslope && slope != SLOPE_FLAT) || IsSteepSlope(slope)) return false;
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1704
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1705
	/* building under a bridge? */
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1706
	if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return false;
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1707
8581
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1708
	/* do not try to build over house owned by another town */
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1709
	if (IsTileType(tile, MP_HOUSE) && GetTownIndex(tile) != town) return false;
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1710
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1711
	/* can we clear the land? */
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1712
	return CmdSucceeded(DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR));
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1713
}
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1714
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1715
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1716
/**
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1717
 * Checks if a house can be built at this tile, must have the same max z as parameter.
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1718
 * @param tile tile to check
8581
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1719
 * @param town town that is checking
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1720
 * @param z max z of this tile so more parts of a house are at the same height (with foundation)
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1721
 * @param noslope are slopes (foundations) allowed?
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1722
 * @return true iff house can be built here
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1723
 * @see CanBuildHouseHere()
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1724
 */
8581
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1725
static inline bool CheckBuildHouseSameZ(TileIndex tile, TownID town, uint z, bool noslope)
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1726
{
8581
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1727
	if (!CanBuildHouseHere(tile, town, noslope)) return false;
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1728
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1729
	/* if building on slopes is allowed, there will be flattening foundation (to tile max z) */
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1730
	if (GetTileMaxZ(tile) != z) return false;
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1731
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1732
	return true;
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1733
}
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1734
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1735
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1736
/**
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1737
 * Checks if a house of size 2x2 can be built at this tile
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1738
 * @param tile tile, N corner
8581
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1739
 * @param town town that is checking
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1740
 * @param z maximum tile z so all tile have the same max z
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1741
 * @param noslope are slopes (foundations) allowed?
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1742
 * @return true iff house can be built
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1743
 * @see CheckBuildHouseSameZ()
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1744
 */
8581
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1745
static bool CheckFree2x2Area(TileIndex tile, TownID town, uint z, bool noslope)
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1746
{
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1747
	/* we need to check this tile too because we can be at different tile now */
8581
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1748
	if (!CheckBuildHouseSameZ(tile, town, z, noslope)) return false;
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1749
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1750
	for (DiagDirection d = DIAGDIR_SE; d < DIAGDIR_END; d++) {
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1751
		tile += TileOffsByDiagDir(d);
8581
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1752
		if (!CheckBuildHouseSameZ(tile, town, z, noslope)) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1753
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1754
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1755
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1756
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1757
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1758
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1759
/**
8580
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1760
 * Checks if current town layout allows building here
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1761
 * @param t town
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1762
 * @param tile tile to check
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1763
 * @return true iff town layout allows building here
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1764
 * @note see layouts
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1765
 */
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1766
static inline bool TownLayoutAllowsHouseHere(Town *t, TileIndex tile)
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1767
{
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1768
	TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile);
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1769
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
  1770
	switch (t->GetActiveLayout()) {
8580
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1771
		case TL_2X2_GRID:
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1772
			if ((grid_pos.x % 3) == 0 || (grid_pos.y % 3) == 0) return false;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1773
			break;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1774
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1775
		case TL_3X3_GRID:
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1776
			if ((grid_pos.x % 4) == 0 || (grid_pos.y % 4) == 0) return false;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1777
			break;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1778
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1779
		default:
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1780
			break;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1781
	}
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1782
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1783
	return true;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1784
}
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1785
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1786
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1787
/**
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1788
 * Checks if current town layout allows 2x2 building here
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1789
 * @param t town
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1790
 * @param tile tile to check
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1791
 * @return true iff town layout allows 2x2 building here
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1792
 * @note see layouts
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1793
 */
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1794
static inline bool TownLayoutAllows2x2HouseHere(Town *t, TileIndex tile)
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1795
{
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1796
	/* MapSize() is sure dividable by both MapSizeX() and MapSizeY(),
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1797
	 * so to do only one memory access, use MapSize() */
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1798
	uint dx = MapSize() + TileX(t->xy) - TileX(tile);
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1799
	uint dy = MapSize() + TileY(t->xy) - TileY(tile);
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1800
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
  1801
	switch (t->GetActiveLayout()) {
8580
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1802
		case TL_2X2_GRID:
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1803
			if ((dx % 3) != 0 || (dy % 3) != 0) return false;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1804
			break;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1805
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1806
		case TL_3X3_GRID:
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1807
			if ((dx % 4) < 2 || (dy % 4) < 2) return false;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1808
			break;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1809
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1810
		default:
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1811
			break;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1812
	}
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1813
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1814
	return true;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1815
}
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1816
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1817
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1818
/**
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1819
 * Checks if 1x2 or 2x1 building is allowed here, also takes into account current town layout
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1820
 * Also, tests both building positions that occupy this tile
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1821
 * @param tile tile where the building should be built
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1822
 * @param t town
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1823
 * @param maxz all tiles should have the same height
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1824
 * @param noslope are slopes forbidden?
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1825
 * @param second diagdir from first tile to second tile
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1826
 **/
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1827
static bool CheckTownBuild2House(TileIndex *tile, Town *t, uint maxz, bool noslope, DiagDirection second)
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1828
{
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1829
	/* 'tile' is already checked in BuildTownHouse() - CanBuildHouseHere() and slope test */
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1830
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1831
	TileIndex tile2 = *tile + TileOffsByDiagDir(second);
8581
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1832
	if (TownLayoutAllowsHouseHere(t, tile2) && CheckBuildHouseSameZ(tile2, t->index, maxz, noslope)) return true;
8580
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1833
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1834
	tile2 = *tile + TileOffsByDiagDir(ReverseDiagDir(second));
8581
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1835
	if (TownLayoutAllowsHouseHere(t, tile2) && CheckBuildHouseSameZ(tile2, t->index, maxz, noslope)) {
8580
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1836
		*tile = tile2;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1837
		return true;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1838
	}
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1839
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1840
	return false;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1841
}
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1842
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1843
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1844
/**
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1845
 * Checks if 2x2 building is allowed here, also takes into account current town layout
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1846
 * Also, tests all four building positions that occupy this tile
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1847
 * @param tile tile where the building should be built
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1848
 * @param t town
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1849
 * @param maxz all tiles should have the same height
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1850
 * @param noslope are slopes forbidden?
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1851
 **/
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1852
static bool CheckTownBuild2x2House(TileIndex *tile, Town *t, uint maxz, bool noslope)
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1853
{
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1854
	TileIndex tile2 = *tile;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1855
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1856
	for (DiagDirection d = DIAGDIR_SE;;d++) { // 'd' goes through DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_END
8581
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1857
		if (TownLayoutAllows2x2HouseHere(t, tile2) && CheckFree2x2Area(tile2, t->index, maxz, noslope)) {
8580
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1858
			*tile = tile2;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1859
			return true;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1860
		}
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1861
		if (d == DIAGDIR_END) break;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1862
		tile2 += TileOffsByDiagDir(ReverseDiagDir(d)); // go clockwise
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1863
	}
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1864
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1865
	return false;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1866
}
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1867
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1868
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1869
/**
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1870
 * Tries to build a house at this tile
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1871
 * @param t town the house will belong to
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1872
 * @param tile where the house will be built
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1873
 * @return false iff no house can be built at this tile
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1874
 */
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1875
static bool BuildTownHouse(Town *t, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1876
{
8580
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1877
	/* forbidden building here by town layout */
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1878
	if (!TownLayoutAllowsHouseHere(t, tile)) return false;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1879
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1880
	/* no house allowed at all, bail out */
8581
9cfd7c9cc3b7 (svn r12162) -Fix [FS#1757]: towns shouldn't build over houses owned by another town
smatz
parents: 8580
diff changeset
  1881
	if (!CanBuildHouseHere(tile, t->index, false)) return false;
8485
00c2183c2a7f (svn r12060) -Fix: do not clear tiles when the town won't be able to build any buildings anyway
smatz
parents: 8461
diff changeset
  1882
8488
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1883
	uint z;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1884
	Slope slope = GetTileSlope(tile, &z);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1885
8308
bc75a4a27ec9 (svn r11873) -Codechange: less a few magical numbers and a tiny bit more comments on town zones
belugas
parents: 8291
diff changeset
  1886
	/* Get the town zone type of the current tile, as well as the climate.
bc75a4a27ec9 (svn r11873) -Codechange: less a few magical numbers and a tiny bit more comments on town zones
belugas
parents: 8291
diff changeset
  1887
	 * This will allow to easily compare with the specs of the new house to build */
8488
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1888
	HouseZonesBits rad = GetTownRadiusGroup(t, tile);
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1889
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1890
	/* Above snow? */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  1891
	int land = _settings_game.game_creation.landscape;
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  1892
	if (land == LT_ARCTIC && z >= _settings_game.game_creation.snow_line) land = -1;
8488
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1893
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1894
	uint bitmask = (1 << rad) + (1 << (land + 12));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1895
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1896
	/* bits 0-4 are used
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1897
	 * bits 11-15 are used
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  1898
	 * bits 5-10 are not used. */
8488
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1899
	HouseID houses[HOUSE_MAX];
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1900
	uint num = 0;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1901
	uint probs[HOUSE_MAX];
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1902
	uint probability_max = 0;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1903
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1904
	/* Generate a list of all possible houses that can be built. */
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1905
	for (uint i = 0; i < HOUSE_MAX; i++) {
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1906
		const HouseSpec *hs = GetHouseSpecs(i);
8488
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1907
		/* Verify that the candidate house spec matches the current tile status */
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1908
		if ((~hs->building_availability & bitmask) == 0 && hs->enabled) {
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1909
			/* Without NewHouses, all houses have probability '1' */
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1910
			uint cur_prob = (_loaded_newgrf_features.has_newhouses ? hs->probability : 1);
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1911
			probability_max += cur_prob;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1912
			probs[num] = cur_prob;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1913
			houses[num++] = (HouseID)i;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1914
		}
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1915
	}
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1916
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1917
	uint maxz = GetTileMaxZ(tile);
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1918
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1919
	while (probability_max > 0) {
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1920
		uint r = RandomRange(probability_max);
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1921
		uint i;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1922
		for (i = 0; i < num; i++) {
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1923
			if (probs[i] > r) break;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1924
			r -= probs[i];
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1925
		}
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1926
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1927
		HouseID house = houses[i];
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1928
		probability_max -= probs[i];
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1929
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1930
		/* remove tested house from the set */
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1931
		num--;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1932
		houses[i] = houses[num];
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1933
		probs[i] = probs[num];
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1934
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  1935
		const HouseSpec *hs = GetHouseSpecs(house);
8488
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1936
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1937
		if (_loaded_newgrf_features.has_newhouses) {
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1938
			if (hs->override != 0) {
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1939
				house = hs->override;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1940
				hs = GetHouseSpecs(house);
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1941
			}
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1942
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1943
			if ((hs->extra_flags & BUILDING_IS_HISTORICAL) && !_generating_world) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1944
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1945
9450
9e7d05b0b1e5 (svn r13368) -Codechange: give house's min/max_date a better name, as it is really year, not date
belugas
parents: 9449
diff changeset
  1946
		if (_cur_year < hs->min_year || _cur_year > hs->max_year) continue;
8488
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1947
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1948
		/* Special houses that there can be only one of. */
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1949
		uint oneof = 0;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1950
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1951
		if (hs->building_flags & BUILDING_IS_CHURCH) {
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1952
			SetBit(oneof, TOWN_HAS_CHURCH);
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1953
		} else if (hs->building_flags & BUILDING_IS_STADIUM) {
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1954
			SetBit(oneof, TOWN_HAS_STADIUM);
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1955
		}
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1956
9505
fd985c1fe3f7 (svn r13489) -Fix: first determine where to *exactly* build a house before asking a NewGRF whether the location is good instead of possibly moving the house a tile after the NewGRF said the location is good.
rubidium
parents: 9470
diff changeset
  1957
		if (HASBITS(t->flags12, oneof)) continue;
8488
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1958
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1959
		/* Make sure there is no slope? */
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1960
		bool noslope = (hs->building_flags & TILE_NOT_SLOPED) != 0;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1961
		if (noslope && slope != SLOPE_FLAT) continue;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1962
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1963
		if (hs->building_flags & TILE_SIZE_2x2) {
8580
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1964
			if (!CheckTownBuild2x2House(&tile, t, maxz, noslope)) continue;
8488
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1965
		} else if (hs->building_flags & TILE_SIZE_2x1) {
8580
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1966
			if (!CheckTownBuild2House(&tile, t, maxz, noslope, DIAGDIR_SW)) continue;
8488
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1967
		} else if (hs->building_flags & TILE_SIZE_1x2) {
8580
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1968
			if (!CheckTownBuild2House(&tile, t, maxz, noslope, DIAGDIR_SE)) continue;
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1969
		} else {
b9ad9ca32df2 (svn r12161) -Fix: towns will no longer build houses > 1x1 there where should be road (with 2x2, 3x3 grid town layouts)
smatz
parents: 8488
diff changeset
  1970
			/* 1x1 house checks are already done */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1971
		}
8488
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1972
9505
fd985c1fe3f7 (svn r13489) -Fix: first determine where to *exactly* build a house before asking a NewGRF whether the location is good instead of possibly moving the house a tile after the NewGRF said the location is good.
rubidium
parents: 9470
diff changeset
  1973
		if (HasBit(hs->callback_mask, CBM_HOUSE_ALLOW_CONSTRUCTION)) {
fd985c1fe3f7 (svn r13489) -Fix: first determine where to *exactly* build a house before asking a NewGRF whether the location is good instead of possibly moving the house a tile after the NewGRF said the location is good.
rubidium
parents: 9470
diff changeset
  1974
			uint16 callback_res = GetHouseCallback(CBID_HOUSE_ALLOW_CONSTRUCTION, 0, 0, house, t, tile);
fd985c1fe3f7 (svn r13489) -Fix: first determine where to *exactly* build a house before asking a NewGRF whether the location is good instead of possibly moving the house a tile after the NewGRF said the location is good.
rubidium
parents: 9470
diff changeset
  1975
			if (callback_res != CALLBACK_FAILED && GB(callback_res, 0, 8) == 0) continue;
fd985c1fe3f7 (svn r13489) -Fix: first determine where to *exactly* build a house before asking a NewGRF whether the location is good instead of possibly moving the house a tile after the NewGRF said the location is good.
rubidium
parents: 9470
diff changeset
  1976
		}
fd985c1fe3f7 (svn r13489) -Fix: first determine where to *exactly* build a house before asking a NewGRF whether the location is good instead of possibly moving the house a tile after the NewGRF said the location is good.
rubidium
parents: 9470
diff changeset
  1977
8488
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1978
		/* build the house */
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1979
		t->num_houses++;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1980
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1981
		/* Special houses that there can be only one of. */
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1982
		t->flags12 |= oneof;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1983
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1984
		byte construction_counter = 0;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1985
		byte construction_stage = 0;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1986
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1987
		if (_generating_world) {
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1988
			uint32 r = Random();
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1989
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1990
			construction_stage = TOWN_HOUSE_COMPLETED;
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1991
			if (Chance16(1, 7)) construction_stage = GB(r, 0, 2);
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1992
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1993
			if (construction_stage == TOWN_HOUSE_COMPLETED) {
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1994
				ChangePopulation(t, hs->population);
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1995
			} else {
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1996
				construction_counter = GB(r, 2, 2);
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1997
			}
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1998
		}
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  1999
9522
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  2000
		MakeTownHouse(tile, t, construction_counter, construction_stage, house, Random());
8488
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  2001
20a38b2c53f5 (svn r12063) -Cleanup: use C++ indenting and variable scope/declaration in BuildTownHouse()
smatz
parents: 8487
diff changeset
  2002
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2003
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2004
8487
9d7e14b019fa (svn r12062) -Fix: possible deadlock when there are no houses available to build at given tile
smatz
parents: 8485
diff changeset
  2005
	return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2006
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2007
10260
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 10259
diff changeset
  2008
/**
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 10259
diff changeset
  2009
 * Update data structures when a house is removed
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 10259
diff changeset
  2010
 * @param tile  Tile of the house
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 10259
diff changeset
  2011
 * @param t     Town owning the house
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 10259
diff changeset
  2012
 * @param house House type
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 10259
diff changeset
  2013
 */
9522
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  2014
static void DoClearTownHouseHelper(TileIndex tile, Town *t, HouseID house)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2015
{
1035
812f837ee03f (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1005
diff changeset
  2016
	assert(IsTileType(tile, MP_HOUSE));
9522
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  2017
	DecreaseBuildingCount(t, house);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2018
	DoClearSquare(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2019
	DeleteAnimatedTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2020
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2021
8942
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2022
/**
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2023
 * Determines if a given HouseID is part of a multitile house.
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2024
 * The given ID is set to the ID of the north tile and the TileDiff to the north tile is returned.
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2025
 *
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2026
 * @param house Is changed to the HouseID of the north tile of the same house
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2027
 * @return TileDiff from the tile of the given HouseID to the north tile
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2028
 */
10269
eb8e4225dbc6 (svn r14504) -Cleanup: Use the right variable type for tile offsets.
michi_cc
parents: 10260
diff changeset
  2029
TileIndexDiff GetHouseNorthPart(HouseID &house)
8942
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2030
{
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2031
	if (house >= 3) { // house id 0,1,2 MUST be single tile houses, or this code breaks.
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2032
		if (GetHouseSpecs(house - 1)->building_flags & TILE_SIZE_2x1) {
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2033
			house--;
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2034
			return TileDiffXY(-1, 0);
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2035
		} else if (GetHouseSpecs(house - 1)->building_flags & BUILDING_2_TILES_Y) {
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2036
			house--;
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2037
			return TileDiffXY(0, -1);
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2038
		} else if (GetHouseSpecs(house - 2)->building_flags & BUILDING_HAS_4_TILES) {
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2039
			house -= 2;
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2040
			return TileDiffXY(-1, 0);
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2041
		} else if (GetHouseSpecs(house - 3)->building_flags & BUILDING_HAS_4_TILES) {
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2042
			house -= 3;
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2043
			return TileDiffXY(-1, -1);
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2044
		}
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2045
	}
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2046
	return 0;
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2047
}
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2048
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2049
void ClearTownHouse(Town *t, TileIndex tile)
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
  2050
{
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2051
	assert(IsTileType(tile, MP_HOUSE));
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2052
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2053
	HouseID house = GetHouseType(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2054
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2055
	/* need to align the tile to point to the upper left corner of the house */
8942
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2056
	tile += GetHouseNorthPart(house); // modifies house to the ID of the north tile
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  2057
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2058
	const HouseSpec *hs = GetHouseSpecs(house);
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2059
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2060
	/* Remove population from the town if the house is finished. */
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2061
	if (IsHouseCompleted(tile)) {
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2062
		ChangePopulation(t, -hs->population);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2063
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2064
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2065
	t->num_houses--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2066
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2067
	/* Clear flags for houses that only may exist once/town. */
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2068
	if (hs->building_flags & BUILDING_IS_CHURCH) {
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  2069
		ClrBit(t->flags12, TOWN_HAS_CHURCH);
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2070
	} else if (hs->building_flags & BUILDING_IS_STADIUM) {
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  2071
		ClrBit(t->flags12, TOWN_HAS_STADIUM);
483
d8374ce6b365 (svn r764) Enumerate the houses only one per town can exist and use the enums instead of magic numbers to check for these
tron
parents: 482
diff changeset
  2072
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  2073
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2074
	/* Do the actual clearing of tiles */
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2075
	uint eflags = hs->building_flags;
9522
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  2076
	DoClearTownHouseHelper(tile, t, house);
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  2077
	if (eflags & BUILDING_2_TILES_Y)   DoClearTownHouseHelper(tile + TileDiffXY(0, 1), t, ++house);
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  2078
	if (eflags & BUILDING_2_TILES_X)   DoClearTownHouseHelper(tile + TileDiffXY(1, 0), t, ++house);
c830a0923804 (svn r13518) -Fix [FS#2083]: Properly count number of non-north housetiles.
frosch
parents: 9505
diff changeset
  2079
	if (eflags & BUILDING_HAS_4_TILES) DoClearTownHouseHelper(tile + TileDiffXY(1, 1), t, ++house);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2080
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2081
7097
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2082
static bool IsUniqueTownName(const char *name)
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2083
{
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2084
	const Town *t;
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2085
	char buf[512];
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2086
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2087
	FOR_ALL_TOWNS(t) {
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2088
		SetDParam(0, t->index);
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2089
		GetString(buf, STR_TOWN, lastof(buf));
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2090
		if (strcmp(buf, name) == 0) return false;
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2091
	}
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2092
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2093
	return true;
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2094
}
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2095
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  2096
/** Rename a town (server-only).
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3433
diff changeset
  2097
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2098
 * @param flags type of operation
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  2099
 * @param p1 town ID to rename
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  2100
 * @param p2 unused
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  2101
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6914
diff changeset
  2102
CommandCost CmdRenameTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2103
{
9914
c07d0352d8d5 (svn r14064) -Fix [FS#1752]: check for the length of strings (in bytes) in the command. Checking for the length in pixels is impossible because that differs per client.
rubidium
parents: 9714
diff changeset
  2104
	if (!IsValidTownID(p1)) return CMD_ERROR;
10148
ad8e8a65f6a2 (svn r14334) -Feature: ability to reset name to default/automatic value (for vehicles, engines, towns, groups, stations, waypoints, managers and companies)
smatz
parents: 9914
diff changeset
  2105
ad8e8a65f6a2 (svn r14334) -Feature: ability to reset name to default/automatic value (for vehicles, engines, towns, groups, stations, waypoints, managers and companies)
smatz
parents: 9914
diff changeset
  2106
	bool reset = StrEmpty(_cmd_text);
ad8e8a65f6a2 (svn r14334) -Feature: ability to reset name to default/automatic value (for vehicles, engines, towns, groups, stations, waypoints, managers and companies)
smatz
parents: 9914
diff changeset
  2107
ad8e8a65f6a2 (svn r14334) -Feature: ability to reset name to default/automatic value (for vehicles, engines, towns, groups, stations, waypoints, managers and companies)
smatz
parents: 9914
diff changeset
  2108
	if (!reset) {
ad8e8a65f6a2 (svn r14334) -Feature: ability to reset name to default/automatic value (for vehicles, engines, towns, groups, stations, waypoints, managers and companies)
smatz
parents: 9914
diff changeset
  2109
		if (strlen(_cmd_text) >= MAX_LENGTH_TOWN_NAME_BYTES) return CMD_ERROR;
ad8e8a65f6a2 (svn r14334) -Feature: ability to reset name to default/automatic value (for vehicles, engines, towns, groups, stations, waypoints, managers and companies)
smatz
parents: 9914
diff changeset
  2110
		if (!IsUniqueTownName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
ad8e8a65f6a2 (svn r14334) -Feature: ability to reset name to default/automatic value (for vehicles, engines, towns, groups, stations, waypoints, managers and companies)
smatz
parents: 9914
diff changeset
  2111
	}
7097
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7058
diff changeset
  2112
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2113
	if (flags & DC_EXEC) {
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2114
		Town *t = GetTown(p1);
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2115
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
  2116
		free(t->name);
10148
ad8e8a65f6a2 (svn r14334) -Feature: ability to reset name to default/automatic value (for vehicles, engines, towns, groups, stations, waypoints, managers and companies)
smatz
parents: 9914
diff changeset
  2117
		t->name = reset ? NULL : strdup(_cmd_text);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2118
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2119
		UpdateTownVirtCoord(t);
9387
ede823d445f5 (svn r13297) -Codechange: Use GUIList for the town directory window
peter1138
parents: 9358
diff changeset
  2120
		InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2121
		UpdateAllStationVirtCoord();
7709
439dcda6d74b (svn r11243) -Fix: update waypoint signs when renaming a town
glx
parents: 7684
diff changeset
  2122
		UpdateAllWaypointSigns();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2123
		MarkWholeScreenDirty();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2124
	}
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  2125
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2126
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2127
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2128
/** Called from GUI */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2129
void ExpandTown(Town *t)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2130
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2131
	_generating_world = true;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  2132
835
f6a341f541d7 (svn r1312) -Add: Patch which is on by default: population in label of the town
truelight
parents: 830
diff changeset
  2133
	/* The more houses, the faster we grow */
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2134
	uint amount = RandomRange(ClampToU16(t->num_houses / 10)) + 3;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2135
	t->num_houses += amount;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2136
	UpdateTownRadius(t);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2137
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2138
	uint n = amount * 10;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2139
	do GrowTown(t); while (--n);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2140
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2141
	t->num_houses -= amount;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2142
	UpdateTownRadius(t);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2143
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2144
	UpdateTownMaxPass(t);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2145
	_generating_world = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2146
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2147
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2148
extern const byte _town_action_costs[8] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2149
	2, 4, 9, 35, 48, 53, 117, 175
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2150
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2151
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2152
static void TownActionAdvertiseSmall(Town *t)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2153
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2154
	ModifyStationRatingAround(t->xy, _current_company, 0x40, 10);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2155
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2156
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2157
static void TownActionAdvertiseMedium(Town *t)
3877
0a298def0202 (svn r4920) Remove parameters, which get only used in certain functions, by splitting those functions.
tron
parents: 3674
diff changeset
  2158
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2159
	ModifyStationRatingAround(t->xy, _current_company, 0x70, 15);
3877
0a298def0202 (svn r4920) Remove parameters, which get only used in certain functions, by splitting those functions.
tron
parents: 3674
diff changeset
  2160
}
0a298def0202 (svn r4920) Remove parameters, which get only used in certain functions, by splitting those functions.
tron
parents: 3674
diff changeset
  2161
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2162
static void TownActionAdvertiseLarge(Town *t)
3877
0a298def0202 (svn r4920) Remove parameters, which get only used in certain functions, by splitting those functions.
tron
parents: 3674
diff changeset
  2163
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2164
	ModifyStationRatingAround(t->xy, _current_company, 0xA0, 20);
3877
0a298def0202 (svn r4920) Remove parameters, which get only used in certain functions, by splitting those functions.
tron
parents: 3674
diff changeset
  2165
}
0a298def0202 (svn r4920) Remove parameters, which get only used in certain functions, by splitting those functions.
tron
parents: 3674
diff changeset
  2166
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2167
static void TownActionRoadRebuild(Town *t)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2168
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2169
	t->road_build_months = 6;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  2170
9658
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
  2171
	char *company_name = MallocT<char>(64);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2172
	SetDParam(0, _current_company);
9658
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
  2173
	GetString(company_name, STR_COMPANY_NAME, company_name + 64);
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
  2174
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 516
diff changeset
  2175
	SetDParam(0, t->index);
9658
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
  2176
	SetDParamStr(1, company_name);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2177
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  2178
	AddNewsItem(STR_2055_TRAFFIC_CHAOS_IN_ROAD_REBUILDING,
9658
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
  2179
		NS_GENERAL, t->xy, 0, company_name);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2180
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2181
6257
5e5a864cacf6 (svn r9066) -Fix [FS#638]: store the owner of a statue, so when it gets removed, the town is notified of it
truelight
parents: 6247
diff changeset
  2182
static bool DoBuildStatueOfCompany(TileIndex tile, TownID town_id)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2183
{
7549
53483e249123 (svn r11069) -Codechange: allow slopes under statues. Patch by kaan.
rubidium
parents: 7545
diff changeset
  2184
	/* Statues can be build on slopes, just like houses. Only the steep slopes is a no go. */
53483e249123 (svn r11069) -Codechange: allow slopes under statues. Patch by kaan.
rubidium
parents: 7545
diff changeset
  2185
	if (IsSteepSlope(GetTileSlope(tile, NULL))) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2186
2986
f440014bdbf3 (svn r3561) Don't use FindLandscapeHeightByTile() when it's overkill. Also use a sprite enum instead of a magic number.
tron
parents: 2958
diff changeset
  2187
	if (!IsTileType(tile, MP_HOUSE) &&
f440014bdbf3 (svn r3561) Don't use FindLandscapeHeightByTile() when it's overkill. Also use a sprite enum instead of a magic number.
tron
parents: 2958
diff changeset
  2188
			!IsTileType(tile, MP_CLEAR) &&
f440014bdbf3 (svn r3561) Don't use FindLandscapeHeightByTile() when it's overkill. Also use a sprite enum instead of a magic number.
tron
parents: 2958
diff changeset
  2189
			!IsTileType(tile, MP_TREES)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2190
		return false;
2986
f440014bdbf3 (svn r3561) Don't use FindLandscapeHeightByTile() when it's overkill. Also use a sprite enum instead of a magic number.
tron
parents: 2958
diff changeset
  2191
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2192
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2193
	CompanyID old = _current_company;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2194
	_current_company = OWNER_NONE;
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2195
	CommandCost r = DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2196
	_current_company = old;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2197
2646
c01dfbc7b7e3 (svn r3188) Use CmdFailed() to check, if a command failed, don't compare with CMD_ERROR
tron
parents: 2639
diff changeset
  2198
	if (CmdFailed(r)) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2199
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2200
	MakeStatue(tile, _current_company, town_id);
3310
a19f247b75b6 (svn r4073) Add functions to make and test for (most) unmovable tiles
tron
parents: 3282
diff changeset
  2201
	MarkTileDirtyByTile(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2202
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2203
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2204
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2205
5118
9640617e1abb (svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents: 5085
diff changeset
  2206
/**
9640617e1abb (svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents: 5085
diff changeset
  2207
 * Search callback function for TownActionBuildStatue
6484
79156627e1ba (svn r9665) -Documentation: Doxygen corrections,errors, corrections of corrections...
belugas
parents: 6455
diff changeset
  2208
 * @param tile on which to perform the search
9592
835ccfd13653 (svn r13632) -Codechange: Use 'void *' for user-data of CircularTileSearch().
frosch
parents: 9581
diff changeset
  2209
 * @param user_data The town_id for which we want a statue
6201
bee01dc45e39 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6133
diff changeset
  2210
 * @return the result of the test
5118
9640617e1abb (svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents: 5085
diff changeset
  2211
 */
9592
835ccfd13653 (svn r13632) -Codechange: Use 'void *' for user-data of CircularTileSearch().
frosch
parents: 9581
diff changeset
  2212
static bool SearchTileForStatue(TileIndex tile, void *user_data)
5118
9640617e1abb (svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents: 5085
diff changeset
  2213
{
9592
835ccfd13653 (svn r13632) -Codechange: Use 'void *' for user-data of CircularTileSearch().
frosch
parents: 9581
diff changeset
  2214
	TownID *town_id = (TownID *)user_data;
835ccfd13653 (svn r13632) -Codechange: Use 'void *' for user-data of CircularTileSearch().
frosch
parents: 9581
diff changeset
  2215
	return DoBuildStatueOfCompany(tile, *town_id);
5118
9640617e1abb (svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents: 5085
diff changeset
  2216
}
9640617e1abb (svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents: 5085
diff changeset
  2217
9640617e1abb (svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents: 5085
diff changeset
  2218
/**
9640617e1abb (svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents: 5085
diff changeset
  2219
 * Perform a 9x9 tiles circular search from the center of the town
9640617e1abb (svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents: 5085
diff changeset
  2220
 * in order to find a free tile to place a statue
9640617e1abb (svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents: 5085
diff changeset
  2221
 * @param t town to search in
9640617e1abb (svn r7198) -Codechange: Implement a circular tile search function.
belugas
parents: 5085
diff changeset
  2222
 */
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2223
static void TownActionBuildStatue(Town *t)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2224
{
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
  2225
	TileIndex tile = t->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2226
9592
835ccfd13653 (svn r13632) -Codechange: Use 'void *' for user-data of CircularTileSearch().
frosch
parents: 9581
diff changeset
  2227
	if (CircularTileSearch(&tile, 9, SearchTileForStatue, &t->index)) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2228
		SetBit(t->statues, _current_company); // Once found and built, "inform" the Town
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2229
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2230
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2231
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2232
static void TownActionFundBuildings(Town *t)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2233
{
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2234
	/* Build next tick */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2235
	t->grow_counter = 1;
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2236
	/* If we were not already growing */
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  2237
	SetBit(t->flags12, TOWN_IS_FUNDED);
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2238
	/* And grow for 3 months */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2239
	t->fund_buildings_months = 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2240
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2241
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2242
static void TownActionBuyRights(Town *t)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2243
{
7657
2b55af55cc51 (svn r11188) -Codechange: rewrite of the town action related code (remove some of the magic).
rubidium
parents: 7642
diff changeset
  2244
	/* Check if it's allowed to by the rights */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  2245
	if (!_settings_game.economy.exclusive_rights) return;
7657
2b55af55cc51 (svn r11188) -Codechange: rewrite of the town action related code (remove some of the magic).
rubidium
parents: 7642
diff changeset
  2246
121
c2f18f4d8be1 (svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents: 39
diff changeset
  2247
	t->exclusive_counter = 12;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2248
	t->exclusivity = _current_company;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2249
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2250
	ModifyStationRatingAround(t->xy, _current_company, 130, 17);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2251
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2252
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2253
static void TownActionBribe(Town *t)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2254
{
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2255
	if (Chance16(1, 14)) {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2256
		/* set as unwanted for 6 months */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2257
		t->unwanted[_current_company] = 6;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2258
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2259
		/* set all close by station ratings to 0 */
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2260
		Station *st;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2261
		FOR_ALL_STATIONS(st) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2262
			if (st->town == t && st->owner == _current_company) {
6350
04b19f551aec (svn r9388) -Codechange: variable scope and type, and standardify all CargoID loops.
peter1138
parents: 6343
diff changeset
  2263
				for (CargoID i = 0; i < NUM_CARGO; i++) st->goods[i].rating = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2264
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2265
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2266
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2267
		/* only show errormessage to the executing player. All errors are handled command.c
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2268
		 * but this is special, because it can only 'fail' on a DC_EXEC */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2269
		if (IsLocalCompany()) ShowErrorMessage(STR_BRIBE_FAILED_2, STR_BRIBE_FAILED, 0, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2270
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4396
diff changeset
  2271
		/* decrease by a lot!
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4396
diff changeset
  2272
		 * ChangeTownRating is only for stuff in demolishing. Bribe failure should
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4396
diff changeset
  2273
		 * be independent of any cheat settings
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2274
		 */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2275
		if (t->ratings[_current_company] > RATING_BRIBE_DOWN_TO) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2276
			t->ratings[_current_company] = RATING_BRIBE_DOWN_TO;
9444
7afe01cdeec5 (svn r13362) -Fix: make the town authority window a bit less glitchy
smatz
parents: 9436
diff changeset
  2277
			InvalidateWindow(WC_TOWN_AUTHORITY, t->index);
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2548
diff changeset
  2278
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2279
	} else {
1005
f48b2bdd84fc (svn r1504) enummed town ratings (Jango)
celestar
parents: 926
diff changeset
  2280
		ChangeTownRating(t, RATING_BRIBE_UP_STEP, RATING_BRIBE_MAXIMUM);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2281
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2282
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2283
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2284
typedef void TownActionProc(Town *t);
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2285
static TownActionProc *const _town_action_proc[] = {
3877
0a298def0202 (svn r4920) Remove parameters, which get only used in certain functions, by splitting those functions.
tron
parents: 3674
diff changeset
  2286
	TownActionAdvertiseSmall,
0a298def0202 (svn r4920) Remove parameters, which get only used in certain functions, by splitting those functions.
tron
parents: 3674
diff changeset
  2287
	TownActionAdvertiseMedium,
0a298def0202 (svn r4920) Remove parameters, which get only used in certain functions, by splitting those functions.
tron
parents: 3674
diff changeset
  2288
	TownActionAdvertiseLarge,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2289
	TownActionRoadRebuild,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2290
	TownActionBuildStatue,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2291
	TownActionFundBuildings,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2292
	TownActionBuyRights,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2293
	TownActionBribe
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2294
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2295
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2296
extern uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t);
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  2297
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  2298
/** Do a town action.
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  2299
 * This performs an action such as advertising, building a statue, funding buildings,
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  2300
 * but also bribing the town-council
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3433
diff changeset
  2301
 * @param tile unused
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2302
 * @param flags type of operation
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  2303
 * @param p1 town to do the action at
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  2304
 * @param p2 action to perform, @see _town_action_proc for the list of available actions
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  2305
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6914
diff changeset
  2306
CommandCost CmdDoTownAction(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2307
{
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4346
diff changeset
  2308
	if (!IsValidTownID(p1) || p2 > lengthof(_town_action_proc)) return CMD_ERROR;
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  2309
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2310
	Town *t = GetTown(p1);
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1638
diff changeset
  2311
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2312
	if (!HasBit(GetMaskOfTownActions(NULL, _current_company, t), p2)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2313
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
  2314
	CommandCost cost(EXPENSES_OTHER, (_price.build_industry >> 8) * _town_action_costs[p2]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2315
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2316
	if (flags & DC_EXEC) {
3877
0a298def0202 (svn r4920) Remove parameters, which get only used in certain functions, by splitting those functions.
tron
parents: 3674
diff changeset
  2317
		_town_action_proc[p2](t);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2318
		InvalidateWindow(WC_TOWN_AUTHORITY, p1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2319
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2320
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2321
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2322
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2323
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2324
static void UpdateTownGrowRate(Town *t)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2325
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2326
	/* Increase company ratings if they're low */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2327
	const Company *c;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2328
	FOR_ALL_COMPANIES(c) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2329
		if (t->ratings[c->index] < RATING_GROWTH_MAXIMUM) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2330
			t->ratings[c->index] = min((int)RATING_GROWTH_MAXIMUM, t->ratings[c->index] + RATING_GROWTH_UP_STEP);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2331
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2332
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2333
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2334
	int n = 0;
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2335
8816
9f99611177eb (svn r12562) -Cleanup: variable scope in terraform_cmd.cpp
smatz
parents: 8815
diff changeset
  2336
	const Station *st;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2337
	FOR_ALL_STATIONS(st) {
8943
53044ba4676f (svn r12726) -Fix [FS#1877]: overflow causing strange building behaviour in towns.
rubidium
parents: 8942
diff changeset
  2338
		if (DistanceSquare(st->xy, t->xy) <= t->squared_town_zone_radius[0]) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2339
			if (st->time_since_load <= 20 || st->time_since_unload <= 20) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2340
				n++;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2341
				if (IsValidCompanyID(st->owner)) {
8815
b778e626f005 (svn r12561) -Fix: do not affect town rating change by the order in which we examine stations
smatz
parents: 8814
diff changeset
  2342
					int new_rating = t->ratings[st->owner] + RATING_STATION_UP_STEP;
b778e626f005 (svn r12561) -Fix: do not affect town rating change by the order in which we examine stations
smatz
parents: 8814
diff changeset
  2343
					t->ratings[st->owner] = min(new_rating, INT16_MAX); // do not let it overflow
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2344
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2345
			} else {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2346
				if (IsValidCompanyID(st->owner)) {
8815
b778e626f005 (svn r12561) -Fix: do not affect town rating change by the order in which we examine stations
smatz
parents: 8814
diff changeset
  2347
					int new_rating = t->ratings[st->owner] + RATING_STATION_DOWN_STEP;
b778e626f005 (svn r12561) -Fix: do not affect town rating change by the order in which we examine stations
smatz
parents: 8814
diff changeset
  2348
					t->ratings[st->owner] = max(new_rating, INT16_MIN);
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2349
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2350
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2351
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2352
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2353
8815
b778e626f005 (svn r12561) -Fix: do not affect town rating change by the order in which we examine stations
smatz
parents: 8814
diff changeset
  2354
	/* clamp all ratings to valid values */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2355
	for (uint i = 0; i < MAX_COMPANIES; i++) {
8815
b778e626f005 (svn r12561) -Fix: do not affect town rating change by the order in which we examine stations
smatz
parents: 8814
diff changeset
  2356
		t->ratings[i] = Clamp(t->ratings[i], RATING_MINIMUM, RATING_MAXIMUM);
b778e626f005 (svn r12561) -Fix: do not affect town rating change by the order in which we examine stations
smatz
parents: 8814
diff changeset
  2357
	}
b778e626f005 (svn r12561) -Fix: do not affect town rating change by the order in which we examine stations
smatz
parents: 8814
diff changeset
  2358
9444
7afe01cdeec5 (svn r13362) -Fix: make the town authority window a bit less glitchy
smatz
parents: 9436
diff changeset
  2359
	InvalidateWindow(WC_TOWN_AUTHORITY, t->index);
7afe01cdeec5 (svn r13362) -Fix: make the town authority window a bit less glitchy
smatz
parents: 9436
diff changeset
  2360
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  2361
	ClrBit(t->flags12, TOWN_IS_FUNDED);
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  2362
	if (_settings_game.economy.town_growth_rate == 0 && t->fund_buildings_months == 0) return;
6454
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
  2363
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
  2364
	/** Towns are processed every TOWN_GROWTH_FREQUENCY ticks, and this is the
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
  2365
	 * number of times towns are processed before a new building is built. */
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
  2366
	static const uint16 _grow_count_values[2][6] = {
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  2367
		{ 120, 120, 120, 100,  80,  60 }, // Fund new buildings has been activated
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  2368
		{ 320, 420, 300, 220, 160, 100 }  // Normal values
6454
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
  2369
	};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2370
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2371
	uint16 m;
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2372
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2373
	if (t->fund_buildings_months != 0) {
6454
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
  2374
		m = _grow_count_values[0][min(n, 5)];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2375
		t->fund_buildings_months--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2376
	} else {
6454
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
  2377
		m = _grow_count_values[1][min(n, 5)];
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7931
diff changeset
  2378
		if (n == 0 && !Chance16(1, 12)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2379
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2380
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  2381
	if (_settings_game.game_creation.landscape == LT_ARCTIC) {
6343
76d17f784c13 (svn r9371) -Feature: Add support for variable snow lines in the arctic climate, supplied
maedhros
parents: 6342
diff changeset
  2382
		if (TilePixelHeight(t->xy) >= GetSnowLine() && t->act_food == 0 && t->population > 90)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2383
			return;
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  2384
	} else if (_settings_game.game_creation.landscape == LT_TROPIC) {
8969
6d1c74e0e2cd (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents: 8965
diff changeset
  2385
		if (GetTropicZone(t->xy) == TROPICZONE_DESERT && (t->act_food == 0 || t->act_water == 0) && t->population > 60)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2386
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2387
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2388
6455
d9783947f1b0 (svn r9614) -Feature: Use the normal growth rate values when the growth rate is set to none and "Fund new buildings" is used.
maedhros
parents: 6454
diff changeset
  2389
	/* Use the normal growth rate values if new buildings have been funded in
d9783947f1b0 (svn r9614) -Feature: Use the normal growth rate values when the growth rate is set to none and "Fund new buildings" is used.
maedhros
parents: 6454
diff changeset
  2390
	 * this town and the growth rate is set to none. */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  2391
	uint growth_multiplier = _settings_game.economy.town_growth_rate != 0 ? _settings_game.economy.town_growth_rate - 1 : 1;
6455
d9783947f1b0 (svn r9614) -Feature: Use the normal growth rate values when the growth rate is set to none and "Fund new buildings" is used.
maedhros
parents: 6454
diff changeset
  2392
d9783947f1b0 (svn r9614) -Feature: Use the normal growth rate values when the growth rate is set to none and "Fund new buildings" is used.
maedhros
parents: 6454
diff changeset
  2393
	m >>= growth_multiplier;
6486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  2394
	if (t->larger_town) m /= 2;
6454
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
  2395
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4396
diff changeset
  2396
	t->growth_rate = m / (t->num_houses / 50 + 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2397
	if (m <= t->grow_counter)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2398
		t->grow_counter = m;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2399
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  2400
	SetBit(t->flags12, TOWN_IS_FUNDED);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2401
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2402
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2403
static void UpdateTownAmounts(Town *t)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2404
{
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2405
	/* Using +1 here to prevent overflow and division by zero */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2406
	t->pct_pass_transported = t->new_act_pass * 256 / (t->new_max_pass + 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2407
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2408
	t->max_pass = t->new_max_pass; t->new_max_pass = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2409
	t->act_pass = t->new_act_pass; t->new_act_pass = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2410
	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
  2411
	t->act_water = t->new_act_water; t->new_act_water = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2412
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2413
	/* Using +1 here to prevent overflow and division by zero */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2414
	t->pct_mail_transported = t->new_act_mail * 256 / (t->new_max_mail + 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2415
	t->max_mail = t->new_max_mail; t->new_max_mail = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2416
	t->act_mail = t->new_act_mail; t->new_act_mail = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2417
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2418
	InvalidateWindow(WC_TOWN_VIEW, t->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2419
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2420
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2421
static void UpdateTownUnwanted(Town *t)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2422
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2423
	const Company *c;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2424
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2425
	FOR_ALL_COMPANIES(c) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2426
		if (t->unwanted[c->index] > 0) t->unwanted[c->index]--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2427
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2428
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2429
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
  2430
bool CheckIfAuthorityAllows(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2431
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2432
	if (!IsValidCompanyID(_current_company)) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2433
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  2434
	Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
4000
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3983
diff changeset
  2435
	if (t == NULL) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2436
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2437
	if (t->ratings[_current_company] > RATING_VERYPOOR) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2438
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2439
	_error_message = STR_2009_LOCAL_AUTHORITY_REFUSES;
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 516
diff changeset
  2440
	SetDParam(0, t->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2441
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2442
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2443
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2444
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2445
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2446
Town *CalcClosestTownFromTile(TileIndex tile, uint threshold)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2447
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2448
	Town *t;
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2449
	uint best = threshold;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2450
	Town *best_town = NULL;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  2451
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2452
	FOR_ALL_TOWNS(t) {
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2453
		uint dist = DistanceManhattan(tile, t->xy);
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  2454
		if (dist < best) {
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  2455
			best = dist;
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  2456
			best_town = t;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2457
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2458
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2459
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2460
	return best_town;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2461
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2462
3983
0f815283e04b (svn r5171) Get rid of an ungly hack in the load routine, which temporarily turned house and road tiles into void tiles to calculate the closest town
tron
parents: 3977
diff changeset
  2463
0f815283e04b (svn r5171) Get rid of an ungly hack in the load routine, which temporarily turned house and road tiles into void tiles to calculate the closest town
tron
parents: 3977
diff changeset
  2464
Town *ClosestTownFromTile(TileIndex tile, uint threshold)
0f815283e04b (svn r5171) Get rid of an ungly hack in the load routine, which temporarily turned house and road tiles into void tiles to calculate the closest town
tron
parents: 3977
diff changeset
  2465
{
0f815283e04b (svn r5171) Get rid of an ungly hack in the load routine, which temporarily turned house and road tiles into void tiles to calculate the closest town
tron
parents: 3977
diff changeset
  2466
	if (IsTileType(tile, MP_HOUSE) || (
9341
bd60c3b2d1e0 (svn r13233) -Fix: Replace some (incorrect) evaluations of TileOwner by RoadOwner.
frosch
parents: 9334
diff changeset
  2467
				IsTileType(tile, MP_ROAD) && HasTileRoadType(tile, ROADTYPE_ROAD) &&
bd60c3b2d1e0 (svn r13233) -Fix: Replace some (incorrect) evaluations of TileOwner by RoadOwner.
frosch
parents: 9334
diff changeset
  2468
				IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN)
3983
0f815283e04b (svn r5171) Get rid of an ungly hack in the load routine, which temporarily turned house and road tiles into void tiles to calculate the closest town
tron
parents: 3977
diff changeset
  2469
			)) {
0f815283e04b (svn r5171) Get rid of an ungly hack in the load routine, which temporarily turned house and road tiles into void tiles to calculate the closest town
tron
parents: 3977
diff changeset
  2470
		return GetTownByTile(tile);
0f815283e04b (svn r5171) Get rid of an ungly hack in the load routine, which temporarily turned house and road tiles into void tiles to calculate the closest town
tron
parents: 3977
diff changeset
  2471
	} else {
0f815283e04b (svn r5171) Get rid of an ungly hack in the load routine, which temporarily turned house and road tiles into void tiles to calculate the closest town
tron
parents: 3977
diff changeset
  2472
		return CalcClosestTownFromTile(tile, threshold);
0f815283e04b (svn r5171) Get rid of an ungly hack in the load routine, which temporarily turned house and road tiles into void tiles to calculate the closest town
tron
parents: 3977
diff changeset
  2473
	}
0f815283e04b (svn r5171) Get rid of an ungly hack in the load routine, which temporarily turned house and road tiles into void tiles to calculate the closest town
tron
parents: 3977
diff changeset
  2474
}
0f815283e04b (svn r5171) Get rid of an ungly hack in the load routine, which temporarily turned house and road tiles into void tiles to calculate the closest town
tron
parents: 3977
diff changeset
  2475
8232
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2476
static bool _town_rating_test = false;
9040
05467c606a0e (svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
rubidium
parents: 9036
diff changeset
  2477
std::map<const Town *, int> _town_test_ratings;
8232
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2478
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2479
void SetTownRatingTestMode(bool mode)
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2480
{
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2481
	static int ref_count = 0;
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2482
	if (mode) {
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2483
		if (ref_count == 0) {
9068
84318afa7067 (svn r12920) -Fix (r12859): town rating was affected even after the test run
smatz
parents: 9040
diff changeset
  2484
			_town_test_ratings.clear();
8232
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2485
		}
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2486
		ref_count++;
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2487
	} else {
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2488
		assert(ref_count > 0);
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2489
		ref_count--;
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2490
	}
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2491
	_town_rating_test = !(ref_count == 0);
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2492
}
3983
0f815283e04b (svn r5171) Get rid of an ungly hack in the load routine, which temporarily turned house and road tiles into void tiles to calculate the closest town
tron
parents: 3977
diff changeset
  2493
9040
05467c606a0e (svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
rubidium
parents: 9036
diff changeset
  2494
static int GetRating(const Town *t)
05467c606a0e (svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
rubidium
parents: 9036
diff changeset
  2495
{
05467c606a0e (svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
rubidium
parents: 9036
diff changeset
  2496
	if (_town_rating_test) {
05467c606a0e (svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
rubidium
parents: 9036
diff changeset
  2497
		std::map<const Town *, int>::iterator it = _town_test_ratings.find(t);
05467c606a0e (svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
rubidium
parents: 9036
diff changeset
  2498
		if (it != _town_test_ratings.end()) {
05467c606a0e (svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
rubidium
parents: 9036
diff changeset
  2499
			return (*it).second;
05467c606a0e (svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
rubidium
parents: 9036
diff changeset
  2500
		}
05467c606a0e (svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
rubidium
parents: 9036
diff changeset
  2501
	}
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2502
	return t->ratings[_current_company];
9040
05467c606a0e (svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
rubidium
parents: 9036
diff changeset
  2503
}
05467c606a0e (svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
rubidium
parents: 9036
diff changeset
  2504
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2505
void ChangeTownRating(Town *t, int add, int max)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2506
{
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2507
	/* if magic_bulldozer cheat is active, town doesn't penaltize for removing stuff */
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2508
	if (t == NULL ||
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2509
			!IsValidCompanyID(_current_company) ||
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2510
			(_cheats.magic_bulldozer.value && add < 0)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2511
		return;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2637
diff changeset
  2512
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2513
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2514
	SetBit(t->have_ratings, _current_company);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  2515
9040
05467c606a0e (svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
rubidium
parents: 9036
diff changeset
  2516
	int rating = GetRating(t);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2517
	if (add < 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2518
		if (rating > max) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2519
			rating += add;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2520
			if (rating < max) rating = max;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2521
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2522
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2523
		if (rating < max) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2524
			rating += add;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2525
			if (rating > max) rating = max;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2526
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2527
	}
8232
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2528
	if (_town_rating_test) {
9040
05467c606a0e (svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
rubidium
parents: 9036
diff changeset
  2529
		_town_test_ratings[t] = rating;
8232
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2530
	} else {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2531
		t->ratings[_current_company] = rating;
9444
7afe01cdeec5 (svn r13362) -Fix: make the town authority window a bit less glitchy
smatz
parents: 9436
diff changeset
  2532
		InvalidateWindow(WC_TOWN_AUTHORITY, t->index);
8232
a4883a0598db (svn r11795) -Fix [FS#1616]: take town rating into account when testing if a command can be executed.
glx
parents: 8230
diff changeset
  2533
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2534
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2535
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4396
diff changeset
  2536
/* penalty for removing town-owned stuff */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2537
static const int _default_rating_settings [3][3] = {
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2538
	/* ROAD_REMOVE, TUNNELBRIDGE_REMOVE, INDUSTRY_REMOVE */
7641
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  2539
	{  0, 128, 384}, // Permissive
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  2540
	{ 48, 192, 480}, // Neutral
3a75903fd892 (svn r11172) -Codechange: rewrite of town road building and addition of the possibility to clean up unconnected road bits during the local road reconstructions. Based on a patch by skidd13.
rubidium
parents: 7603
diff changeset
  2541
	{ 96, 384, 768}, // Hostile
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2542
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2543
2958
ac0a9673b522 (svn r3520) Remove unused parameters from some functions
tron
parents: 2952
diff changeset
  2544
bool CheckforTownRating(uint32 flags, Town *t, byte type)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2545
{
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2546
	/* if magic_bulldozer cheat is active, town doesn't restrict your destructive actions */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2547
	if (t == NULL || !IsValidCompanyID(_current_company) || _cheats.magic_bulldozer.value)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2548
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2549
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4396
diff changeset
  2550
	/* check if you're allowed to remove the street/bridge/tunnel/industry
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4396
diff changeset
  2551
	 * owned by a town no removal if rating is lower than ... depends now on
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4396
diff changeset
  2552
	 * difficulty setting. Minimum town rating selected by difficulty level
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2553
	 */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9387
diff changeset
  2554
	int modemod = _default_rating_settings[_settings_game.difficulty.town_council_tolerance][type];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2555
9040
05467c606a0e (svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
rubidium
parents: 9036
diff changeset
  2556
	if (GetRating(t) < 16 + modemod && !(flags & DC_NO_TOWN_RATING)) {
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 516
diff changeset
  2557
		SetDParam(0, t->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2558
		_error_message = STR_2009_LOCAL_AUTHORITY_REFUSES;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2559
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2560
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2561
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2562
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2563
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2564
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
  2565
void TownsMonthlyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2566
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2567
	Town *t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2568
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  2569
	FOR_ALL_TOWNS(t) {
4000
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3983
diff changeset
  2570
		if (t->road_build_months != 0) t->road_build_months--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2571
121
c2f18f4d8be1 (svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents: 39
diff changeset
  2572
		if (t->exclusive_counter != 0)
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10191
diff changeset
  2573
			if (--t->exclusive_counter == 0) t->exclusivity = INVALID_COMPANY;
121
c2f18f4d8be1 (svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents: 39
diff changeset
  2574
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2575
		UpdateTownGrowRate(t);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2576
		UpdateTownAmounts(t);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2577
		UpdateTownUnwanted(t);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2578
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2579
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2580
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
  2581
void InitializeTowns()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2582
{
1260
632f63689178 (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1252
diff changeset
  2583
	/* Clean the town pool and create 1 block in it */
7401
7a72cc2a1196 (svn r10773) -Codechange: use pool.CleanPool instead of CleanPool(&pool) and similarly for AddBlock*.
rubidium
parents: 7390
diff changeset
  2584
	_Town_pool.CleanPool();
7a72cc2a1196 (svn r10773) -Codechange: use pool.CleanPool instead of CleanPool(&pool) and similarly for AddBlock*.
rubidium
parents: 7390
diff changeset
  2585
	_Town_pool.AddBlockToPool();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2586
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2587
	memset(_subsidies, 0, sizeof(_subsidies));
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2588
	for (Subsidy *s = _subsidies; s != endof(_subsidies); s++) {
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  2589
		s->cargo_type = CT_INVALID;
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2590
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2591
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2592
	_cur_town_ctr = 0;
1529
3a8196d15b01 (svn r2033) - Fix: Fix some more desync by saving the town growth frequency iterator _cur_town_iter. Needed to bump a svg revision for that thanks to the braindead SlGlob thing, or at least I don't know how to do it without bumping it.
pasky
parents: 1517
diff changeset
  2593
	_cur_town_iter = 0;
1260
632f63689178 (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1252
diff changeset
  2594
	_total_towns = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2595
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2596
7494
ab05bad37410 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7483
diff changeset
  2597
static CommandCost TerraformTile_Town(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new)
ab05bad37410 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7483
diff changeset
  2598
{
7582
80c8517b08d1 (svn r11107) -Feature: some tool so one can still build tunnels under rails (and other structures) when the owner of the structure built it on foundations and if you have enough "empty" space ofcourse. One could use the tool for some other construction needs too. Patch by frosch.
rubidium
parents: 7577
diff changeset
  2599
	if (AutoslopeEnabled()) {
80c8517b08d1 (svn r11107) -Feature: some tool so one can still build tunnels under rails (and other structures) when the owner of the structure built it on foundations and if you have enough "empty" space ofcourse. One could use the tool for some other construction needs too. Patch by frosch.
rubidium
parents: 7577
diff changeset
  2600
		HouseID house = GetHouseType(tile);
8942
a53f56bb1639 (svn r12717) -Fix (r11107): Check the TILE_NOT_SLOPED flag of the _north_ tile of multi-tile houses to decide if autoslope is allowed.
frosch
parents: 8847
diff changeset
  2601
		GetHouseNorthPart(house); // modifies house to the ID of the north tile
8814
bff94f5654b5 (svn r12560) -Cleanup: town_cmd.cpp and header files - variable scope and a bit of coding style
smatz
parents: 8806
diff changeset
  2602
		const HouseSpec *hs = GetHouseSpecs(house);
7582
80c8517b08d1 (svn r11107) -Feature: some tool so one can still build tunnels under rails (and other structures) when the owner of the structure built it on foundations and if you have enough "empty" space ofcourse. One could use the tool for some other construction needs too. Patch by frosch.
rubidium
parents: 7577
diff changeset
  2603
80c8517b08d1 (svn r11107) -Feature: some tool so one can still build tunnels under rails (and other structures) when the owner of the structure built it on foundations and if you have enough "empty" space ofcourse. One could use the tool for some other construction needs too. Patch by frosch.
rubidium
parents: 7577
diff changeset
  2604
		/* Here we differ from TTDP by checking TILE_NOT_SLOPED */
80c8517b08d1 (svn r11107) -Feature: some tool so one can still build tunnels under rails (and other structures) when the owner of the structure built it on foundations and if you have enough "empty" space ofcourse. One could use the tool for some other construction needs too. Patch by frosch.
rubidium
parents: 7577
diff changeset
  2605
		if (((hs->building_flags & TILE_NOT_SLOPED) == 0) && !IsSteepSlope(tileh_new) &&
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
  2606
			(GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
7582
80c8517b08d1 (svn r11107) -Feature: some tool so one can still build tunnels under rails (and other structures) when the owner of the structure built it on foundations and if you have enough "empty" space ofcourse. One could use the tool for some other construction needs too. Patch by frosch.
rubidium
parents: 7577
diff changeset
  2607
	}
80c8517b08d1 (svn r11107) -Feature: some tool so one can still build tunnels under rails (and other structures) when the owner of the structure built it on foundations and if you have enough "empty" space ofcourse. One could use the tool for some other construction needs too. Patch by frosch.
rubidium
parents: 7577
diff changeset
  2608
7494
ab05bad37410 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7483
diff changeset
  2609
	return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
ab05bad37410 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7483
diff changeset
  2610
}
ab05bad37410 (svn r11005) -Codechange: move the tiletype specific terraforming checks to the functions for those tile types.
rubidium
parents: 7483
diff changeset
  2611
10260
c6ec6b3c1b18 (svn r14491) -Documentation: updates/additions of doxygen docs (Alberth)
rubidium
parents: 10259
diff changeset
  2612
/** Tile callback functions for a town */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2613
extern const TileTypeProcs _tile_type_town_procs = {
10259
0c47efd6a0c3 (svn r14490) -Codechange: fix comment style on a few locations (Alberth)
rubidium
parents: 10236
diff changeset
  2614
	DrawTile_Town,           // draw_tile_proc
0c47efd6a0c3 (svn r14490) -Codechange: fix comment style on a few locations (Alberth)
rubidium
parents: 10236
diff changeset
  2615
	GetSlopeZ_Town,          // get_slope_z_proc
0c47efd6a0c3 (svn r14490) -Codechange: fix comment style on a few locations (Alberth)
rubidium
parents: 10236
diff changeset
  2616
	ClearTile_Town,          // clear_tile_proc
0c47efd6a0c3 (svn r14490) -Codechange: fix comment style on a few locations (Alberth)
rubidium
parents: 10236
diff changeset
  2617
	GetAcceptedCargo_Town,   // get_accepted_cargo_proc
0c47efd6a0c3 (svn r14490) -Codechange: fix comment style on a few locations (Alberth)
rubidium
parents: 10236
diff changeset
  2618
	GetTileDesc_Town,        // get_tile_desc_proc
0c47efd6a0c3 (svn r14490) -Codechange: fix comment style on a few locations (Alberth)
rubidium
parents: 10236
diff changeset
  2619
	GetTileTrackStatus_Town, // get_tile_track_status_proc
0c47efd6a0c3 (svn r14490) -Codechange: fix comment style on a few locations (Alberth)
rubidium
parents: 10236
diff changeset
  2620
	ClickTile_Town,          // click_tile_proc
0c47efd6a0c3 (svn r14490) -Codechange: fix comment style on a few locations (Alberth)
rubidium
parents: 10236
diff changeset
  2621
	AnimateTile_Town,        // animate_tile_proc
0c47efd6a0c3 (svn r14490) -Codechange: fix comment style on a few locations (Alberth)
rubidium
parents: 10236
diff changeset
  2622
	TileLoop_Town,           // tile_loop_clear
0c47efd6a0c3 (svn r14490) -Codechange: fix comment style on a few locations (Alberth)
rubidium
parents: 10236
diff changeset
  2623
	ChangeTileOwner_Town,    // change_tile_owner_clear
0c47efd6a0c3 (svn r14490) -Codechange: fix comment style on a few locations (Alberth)
rubidium
parents: 10236
diff changeset
  2624
	GetProducedCargo_Town,   // get_produced_cargo_proc
0c47efd6a0c3 (svn r14490) -Codechange: fix comment style on a few locations (Alberth)
rubidium
parents: 10236
diff changeset
  2625
	NULL,                    // vehicle_enter_tile_proc
0c47efd6a0c3 (svn r14490) -Codechange: fix comment style on a few locations (Alberth)
rubidium
parents: 10236
diff changeset
  2626
	GetFoundation_Town,      // get_foundation_proc
0c47efd6a0c3 (svn r14490) -Codechange: fix comment style on a few locations (Alberth)
rubidium
parents: 10236
diff changeset
  2627
	TerraformTile_Town,      // terraform_tile_proc
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2628
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2629
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2630
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2631
/** Save and load of towns. */
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1840
diff changeset
  2632
static const SaveLoad _town_desc[] = {
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2633
	SLE_CONDVAR(Town, xy,                    SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2634
	SLE_CONDVAR(Town, xy,                    SLE_UINT32,                 6, SL_MAX_VERSION),
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 159
diff changeset
  2635
8707
e57a09994e12 (svn r12381) -Fix [FS1835] [FS1535] (r11855): The number of houses wasn't computed right. A few other things regaring the updating had to be changed. Big thanks for support to frosch123 and SmatZ, to name just a few. (Inspired by a patch of bilbo)
skidd13
parents: 8618
diff changeset
  2636
	SLE_CONDNULL(2, 0, 2),                   ///< population, no longer in use
e57a09994e12 (svn r12381) -Fix [FS1835] [FS1535] (r11855): The number of houses wasn't computed right. A few other things regaring the updating had to be changed. Big thanks for support to frosch123 and SmatZ, to name just a few. (Inspired by a patch of bilbo)
skidd13
parents: 8618
diff changeset
  2637
	SLE_CONDNULL(4, 3, 84),                  ///< population, no longer in use
e57a09994e12 (svn r12381) -Fix [FS1835] [FS1535] (r11855): The number of houses wasn't computed right. A few other things regaring the updating had to be changed. Big thanks for support to frosch123 and SmatZ, to name just a few. (Inspired by a patch of bilbo)
skidd13
parents: 8618
diff changeset
  2638
	SLE_CONDNULL(2, 0, 91),                  ///< num_houses, no longer in use
e57a09994e12 (svn r12381) -Fix [FS1835] [FS1535] (r11855): The number of houses wasn't computed right. A few other things regaring the updating had to be changed. Big thanks for support to frosch123 and SmatZ, to name just a few. (Inspired by a patch of bilbo)
skidd13
parents: 8618
diff changeset
  2639
6956
3579bfc5157b (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents: 6950
diff changeset
  2640
	SLE_CONDVAR(Town, townnamegrfid,         SLE_UINT32, 66, SL_MAX_VERSION),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2641
	    SLE_VAR(Town, townnametype,          SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2642
	    SLE_VAR(Town, townnameparts,         SLE_UINT32),
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
  2643
	SLE_CONDSTR(Town, name,                  SLE_STR, 0, 84, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2644
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2645
	    SLE_VAR(Town, flags12,               SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2646
	    SLE_VAR(Town, statues,               SLE_UINT8),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2647
8707
e57a09994e12 (svn r12381) -Fix [FS1835] [FS1535] (r11855): The number of houses wasn't computed right. A few other things regaring the updating had to be changed. Big thanks for support to frosch123 and SmatZ, to name just a few. (Inspired by a patch of bilbo)
skidd13
parents: 8618
diff changeset
  2648
	SLE_CONDNULL(1, 0, 1),                   ///< sort_index, no longer in use
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2649
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2650
	    SLE_VAR(Town, have_ratings,          SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2651
	    SLE_ARR(Town, ratings,               SLE_INT16, 8),
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2652
	/* failed bribe attempts are stored since savegame format 4 */
8707
e57a09994e12 (svn r12381) -Fix [FS1835] [FS1535] (r11855): The number of houses wasn't computed right. A few other things regaring the updating had to be changed. Big thanks for support to frosch123 and SmatZ, to name just a few. (Inspired by a patch of bilbo)
skidd13
parents: 8618
diff changeset
  2653
	SLE_CONDARR(Town, unwanted,              SLE_INT8, 8, 4, SL_MAX_VERSION),
1377
7ab329d1fc28 (svn r1881) -Fix: [ 1119308 ] Max passengers / mail variables are now 32 bit
celestar
parents: 1362
diff changeset
  2654
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2655
	SLE_CONDVAR(Town, max_pass,              SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2656
	SLE_CONDVAR(Town, max_mail,              SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2657
	SLE_CONDVAR(Town, new_max_pass,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2658
	SLE_CONDVAR(Town, new_max_mail,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2659
	SLE_CONDVAR(Town, act_pass,              SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2660
	SLE_CONDVAR(Town, act_mail,              SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2661
	SLE_CONDVAR(Town, new_act_pass,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2662
	SLE_CONDVAR(Town, new_act_mail,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2663
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2664
	SLE_CONDVAR(Town, max_pass,              SLE_UINT32,                 9, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2665
	SLE_CONDVAR(Town, max_mail,              SLE_UINT32,                 9, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2666
	SLE_CONDVAR(Town, new_max_pass,          SLE_UINT32,                 9, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2667
	SLE_CONDVAR(Town, new_max_mail,          SLE_UINT32,                 9, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2668
	SLE_CONDVAR(Town, act_pass,              SLE_UINT32,                 9, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2669
	SLE_CONDVAR(Town, act_mail,              SLE_UINT32,                 9, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2670
	SLE_CONDVAR(Town, new_act_pass,          SLE_UINT32,                 9, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2671
	SLE_CONDVAR(Town, new_act_mail,          SLE_UINT32,                 9, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2672
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2673
	    SLE_VAR(Town, pct_pass_transported,  SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2674
	    SLE_VAR(Town, pct_mail_transported,  SLE_UINT8),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2675
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2676
	    SLE_VAR(Town, act_food,              SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2677
	    SLE_VAR(Town, act_water,             SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2678
	    SLE_VAR(Town, new_act_food,          SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2679
	    SLE_VAR(Town, new_act_water,         SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2680
6454
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
  2681
	SLE_CONDVAR(Town, time_until_rebuild,    SLE_UINT8,                  0, 53),
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
  2682
	SLE_CONDVAR(Town, grow_counter,          SLE_UINT8,                  0, 53),
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
  2683
	SLE_CONDVAR(Town, growth_rate,           SLE_UINT8,                  0, 53),
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
  2684
8359
6efdb08cedb8 (svn r11925) -Fix (r122, r9867): loading old, pre savegame version 2, savegames.
rubidium
parents: 8308
diff changeset
  2685
	SLE_CONDVAR(Town, time_until_rebuild,    SLE_UINT16,                54, SL_MAX_VERSION),
6efdb08cedb8 (svn r11925) -Fix (r122, r9867): loading old, pre savegame version 2, savegames.
rubidium
parents: 8308
diff changeset
  2686
	SLE_CONDVAR(Town, grow_counter,          SLE_UINT16,                54, SL_MAX_VERSION),
6efdb08cedb8 (svn r11925) -Fix (r122, r9867): loading old, pre savegame version 2, savegames.
rubidium
parents: 8308
diff changeset
  2687
	SLE_CONDVAR(Town, growth_rate,           SLE_INT16,                 54, SL_MAX_VERSION),
6454
ad53226c2027 (svn r9613) -Feature: Make it possible to have some control over the town growth. The
maedhros
parents: 6427
diff changeset
  2688
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2689
	    SLE_VAR(Town, fund_buildings_months, SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2690
	    SLE_VAR(Town, road_build_months,     SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2691
8359
6efdb08cedb8 (svn r11925) -Fix (r122, r9867): loading old, pre savegame version 2, savegames.
rubidium
parents: 8308
diff changeset
  2692
	SLE_CONDVAR(Town, exclusivity,           SLE_UINT8,                  2, SL_MAX_VERSION),
6efdb08cedb8 (svn r11925) -Fix (r122, r9867): loading old, pre savegame version 2, savegames.
rubidium
parents: 8308
diff changeset
  2693
	SLE_CONDVAR(Town, exclusive_counter,     SLE_UINT8,                  2, SL_MAX_VERSION),
6486
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  2694
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  2695
	SLE_CONDVAR(Town, larger_town,           SLE_BOOL,                  56, SL_MAX_VERSION),
7d2ae0a167fb (svn r9667) -Feature: Add the concept of cities. A (configurable) proportion of towns can
maedhros
parents: 6484
diff changeset
  2696
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6357
diff changeset
  2697
	/* reserve extra space in savegame here. (currently 30 bytes) */
3222
6de22e06a1e9 (svn r3895) - Add proper SLE(G)_CONDNULL macros for the empty space reservation in savegames and update where used
Darkvater
parents: 3184
diff changeset
  2698
	SLE_CONDNULL(30, 2, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2699
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2700
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2701
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2702
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2703
/* Save and load the mapping between the house id on the map, and the grf file
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2704
 * it came from. */
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2705
static const SaveLoad _house_id_mapping_desc[] = {
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents: 6610
diff changeset
  2706
	SLE_VAR(EntityIDMapping, grfid,         SLE_UINT32),
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents: 6610
diff changeset
  2707
	SLE_VAR(EntityIDMapping, entity_id,     SLE_UINT8),
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents: 6610
diff changeset
  2708
	SLE_VAR(EntityIDMapping, substitute_id, SLE_UINT8),
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2709
	SLE_END()
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2710
};
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2711
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2712
static void Save_HOUSEIDS()
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2713
{
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents: 6610
diff changeset
  2714
	uint j = _house_mngr.GetMaxMapping();
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents: 6610
diff changeset
  2715
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents: 6610
diff changeset
  2716
	for (uint i = 0; i < j; i++) {
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2717
		SlSetArrayIndex(i);
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents: 6610
diff changeset
  2718
		SlObject(&_house_mngr.mapping_ID[i], _house_id_mapping_desc);
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2719
	}
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2720
}
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2721
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2722
static void Load_HOUSEIDS()
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2723
{
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2724
	int index;
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2725
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents: 6610
diff changeset
  2726
	_house_mngr.ResetMapping();
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents: 6610
diff changeset
  2727
	uint max_id = _house_mngr.GetMaxMapping();
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2728
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2729
	while ((index = SlIterateArray()) != -1) {
6629
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents: 6610
diff changeset
  2730
		if ((uint)index >= max_id) break;
eba0ac353e4d (svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
belugas
parents: 6610
diff changeset
  2731
		SlObject(&_house_mngr.mapping_ID[index], _house_id_mapping_desc);
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2732
	}
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2733
}
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2734
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
  2735
static void Save_TOWN()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2736
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2737
	Town *t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2738
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
  2739
	FOR_ALL_TOWNS(t) {
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  2740
		SlSetArrayIndex(t->index);
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  2741
		SlObject(t, _town_desc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2742
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2743
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2744
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
  2745
static void Load_TOWN()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2746
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2747
	int index;
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
  2748
1261
914ecc20a2df (svn r1765) -Fix: on loading, the total amount of towns wasn't reset to zero
truelight
parents: 1260
diff changeset
  2749
	_total_towns = 0;
914ecc20a2df (svn r1765) -Fix: on loading, the total amount of towns wasn't reset to zero
truelight
parents: 1260
diff changeset
  2750
1260
632f63689178 (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1252
diff changeset
  2751
	while ((index = SlIterateArray()) != -1) {
7386
93f6a042d1c3 (svn r10755) -Codechange: make the town struct use the pool item class as super class.
rubidium
parents: 7370
diff changeset
  2752
		Town *t = new (index) Town();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2753
		SlObject(t, _town_desc);
1260
632f63689178 (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1252
diff changeset
  2754
5298
46eabcb5c2b2 (svn r7451) -Fix (7372): GetNum(Towns|Industries) should return the actual number of towns and industries.
rubidium
parents: 5247
diff changeset
  2755
		_total_towns++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2756
	}
1260
632f63689178 (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1252
diff changeset
  2757
632f63689178 (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1252
diff changeset
  2758
	/* This is to ensure all pointers are within the limits of
632f63689178 (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1252
diff changeset
  2759
	 *  the size of the TownPool */
5247
1f982de55b88 (svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.
matthijs
parents: 5216
diff changeset
  2760
	if (_cur_town_ctr > GetMaxTownIndex())
1260
632f63689178 (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1252
diff changeset
  2761
		_cur_town_ctr = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2762
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2763
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
  2764
void AfterLoadTown()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2765
{
8796
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
  2766
	Town *t;
ccf3ed611354 (svn r12534) -Feature: Add a new type of town road layouts - random for each town
skidd13
parents: 8791
diff changeset
  2767
	FOR_ALL_TOWNS(t) t->InitializeLayout();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2768
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2769
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2770
extern const ChunkHandler _town_chunk_handlers[] = {
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2771
	{ 'HIDS', Save_HOUSEIDS, Load_HOUSEIDS, CH_ARRAY },
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2772
	{ 'CITY', Save_TOWN,     Load_TOWN,     CH_ARRAY | CH_LAST},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2773
};
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2774
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2775
void ResetHouses()
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2776
{
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2777
	memset(&_house_specs, 0, sizeof(_house_specs));
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2778
	memcpy(&_house_specs, &_original_house_specs, sizeof(_original_house_specs));
7666
69cabc700c4d (svn r11197) -Fix: It is not useful to reset the override of an entity every time a new grf file is been submitted.
belugas
parents: 7657
diff changeset
  2779
69cabc700c4d (svn r11197) -Fix: It is not useful to reset the override of an entity every time a new grf file is been submitted.
belugas
parents: 7657
diff changeset
  2780
	/* Reset any overrides that have been set. */
69cabc700c4d (svn r11197) -Fix: It is not useful to reset the override of an entity every time a new grf file is been submitted.
belugas
parents: 7657
diff changeset
  2781
	_house_mngr.ResetOverride();
6332
f3f436dcd7d0 (svn r9315) -Merge: The newhouses branch. With this merge comes almost complete support for
maedhros
parents: 6257
diff changeset
  2782
}