2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2005-02-06 08:18:00 +00:00
|
|
|
#include "strings.h"
|
2006-03-05 10:19:33 +00:00
|
|
|
#include "road_map.h"
|
2004-11-25 10:47:30 +00:00
|
|
|
#include "table/strings.h"
|
2005-07-20 22:05:13 +00:00
|
|
|
#include "table/sprites.h"
|
2004-12-15 22:18:54 +00:00
|
|
|
#include "map.h"
|
2005-01-29 12:19:05 +00:00
|
|
|
#include "tile.h"
|
2006-03-24 12:00:24 +00:00
|
|
|
#include "town_map.h"
|
2006-03-06 20:55:24 +00:00
|
|
|
#include "tunnel_map.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "viewport.h"
|
|
|
|
#include "town.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "gfx.h"
|
|
|
|
#include "industry.h"
|
|
|
|
#include "station.h"
|
|
|
|
#include "player.h"
|
|
|
|
#include "news.h"
|
|
|
|
#include "saveload.h"
|
|
|
|
#include "economy.h"
|
|
|
|
#include "gui.h"
|
2006-03-23 20:47:56 +00:00
|
|
|
#include "unmovable_map.h"
|
2006-04-03 15:11:17 +00:00
|
|
|
#include "water_map.h"
|
2005-07-21 22:15:02 +00:00
|
|
|
#include "variables.h"
|
2006-03-29 19:03:47 +00:00
|
|
|
#include "bridge.h"
|
2006-08-14 14:21:15 +00:00
|
|
|
#include "date.h"
|
2006-04-24 21:10:56 +00:00
|
|
|
#include "table/town_land.h"
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
#include "genworld.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-01 18:32:01 +00:00
|
|
|
/**
|
|
|
|
* Called if a new block is added to the town-pool
|
|
|
|
*/
|
|
|
|
static void TownPoolNewBlock(uint start_item)
|
|
|
|
{
|
|
|
|
Town *t;
|
|
|
|
|
2006-08-22 15:33:35 +00:00
|
|
|
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
|
|
|
|
* TODO - This is just a temporary stage, this will be removed. */
|
2006-10-28 11:55:29 +00:00
|
|
|
for (t = GetTown(start_item); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) t->index = start_item++;
|
2005-02-01 18:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the town-pool */
|
2006-10-28 11:55:29 +00:00
|
|
|
DEFINE_POOL(Town, Town, TownPoolNewBlock, NULL)
|
2005-02-01 18:32:01 +00:00
|
|
|
|
2006-08-26 18:05:05 +00:00
|
|
|
void DestroyTown(Town *t)
|
|
|
|
{
|
|
|
|
Industry *i;
|
|
|
|
TileIndex tile;
|
|
|
|
|
|
|
|
/* Delete town authority window
|
|
|
|
* and remove from list of sorted towns */
|
|
|
|
DeleteWindowById(WC_TOWN_VIEW, t->index);
|
|
|
|
_town_sort_dirty = true;
|
|
|
|
|
|
|
|
/* Delete all industries belonging to the town */
|
|
|
|
FOR_ALL_INDUSTRIES(i) if (i->town == t) DeleteIndustry(i);
|
|
|
|
|
|
|
|
/* Go through all tiles and delete those belonging to the town */
|
|
|
|
for (tile = 0; tile < MapSize(); ++tile) {
|
|
|
|
switch (GetTileType(tile)) {
|
|
|
|
case MP_HOUSE:
|
|
|
|
if (GetTownByTile(tile) == t) DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_STREET:
|
|
|
|
case MP_TUNNELBRIDGE:
|
|
|
|
if (IsTileOwner(tile, OWNER_TOWN) &&
|
|
|
|
ClosestTownFromTile(tile, (uint)-1) == t)
|
|
|
|
DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DeleteName(t->townnametype);
|
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Local
|
|
|
|
static int _grow_town_result;
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool BuildTownHouse(Town *t, TileIndex tile);
|
|
|
|
static void ClearTownHouse(Town *t, TileIndex tile);
|
|
|
|
static void DoBuildTownHouse(Town *t, TileIndex tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-09-18 20:56:44 +00:00
|
|
|
static void TownDrawHouseLift(const TileInfo *ti)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-04-03 11:46:28 +00:00
|
|
|
AddChildSpriteScreen(SPR_LIFT, 14, 60 - GetLiftPosition(ti->tile));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-09-18 20:56:44 +00:00
|
|
|
typedef void TownDrawTileProc(const TileInfo *ti);
|
2004-08-09 17:04:08 +00:00
|
|
|
static TownDrawTileProc * const _town_draw_tile_procs[1] = {
|
2005-07-15 16:29:30 +00:00
|
|
|
TownDrawHouseLift
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void DrawTile_Town(TileInfo *ti)
|
|
|
|
{
|
2006-04-24 21:10:56 +00:00
|
|
|
const DrawBuildingsTileStruct *dcts;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint32 image;
|
|
|
|
|
|
|
|
/* Retrieve pointer to the draw town tile struct */
|
|
|
|
{
|
2004-11-22 10:09:37 +00:00
|
|
|
/* this "randomizes" on the (up to) 4 variants of a building */
|
|
|
|
uint variant;
|
|
|
|
variant = ti->x >> 4;
|
|
|
|
variant ^= ti->x >> 6;
|
|
|
|
variant ^= ti->y >> 4;
|
|
|
|
variant -= ti->y >> 6;
|
|
|
|
variant &= 3;
|
2006-04-24 21:10:56 +00:00
|
|
|
dcts = &_town_draw_tile_data[GetHouseType(ti->tile) << 4 | variant << 2 | GetHouseBuildingStage(ti->tile)];
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-06-21 16:40:23 +00:00
|
|
|
if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, ti->tileh);
|
|
|
|
DrawGroundSprite(dcts->ground);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* Add a house on top of the ground? */
|
2006-04-24 21:10:56 +00:00
|
|
|
image = dcts->building;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (image != 0) {
|
|
|
|
if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
|
|
|
AddSortableSpriteToDraw(image,
|
2004-11-22 11:01:30 +00:00
|
|
|
ti->x + dcts->subtile_x,
|
|
|
|
ti->y + dcts->subtile_y,
|
|
|
|
dcts->width + 1,
|
|
|
|
dcts->height + 1,
|
2004-08-09 17:04:08 +00:00
|
|
|
dcts->dz,
|
2006-06-21 16:40:23 +00:00
|
|
|
ti->z
|
|
|
|
);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_display_opt & DO_TRANS_BUILDINGS) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2006-04-24 21:10:56 +00:00
|
|
|
int proc = dcts->draw_proc - 1;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
|
|
|
if (proc >= 0) _town_draw_tile_procs[proc](ti);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-06 16:32:49 +00:00
|
|
|
static uint GetSlopeZ_Town(TileIndex tile, uint x, uint y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-08-06 16:32:49 +00:00
|
|
|
return GetTileMaxZ(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-04-23 13:48:16 +00:00
|
|
|
static Slope GetSlopeTileh_Town(TileIndex tile, Slope tileh)
|
2004-08-13 18:27:33 +00:00
|
|
|
{
|
2006-04-23 13:48:16 +00:00
|
|
|
return SLOPE_FLAT;
|
2004-08-13 18:27:33 +00:00
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void AnimateTile_Town(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-04-03 11:46:28 +00:00
|
|
|
int pos, dest;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_tick_counter & 3) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-15 16:29:30 +00:00
|
|
|
// If the house is not one with a lift anymore, then stop this animating.
|
|
|
|
// Not exactly sure when this happens, but probably when a house changes.
|
|
|
|
// Before this was just a return...so it'd leak animated tiles..
|
|
|
|
// That bug seems to have been here since day 1??
|
2006-03-30 15:06:49 +00:00
|
|
|
if (!(_housetype_extra_flags[GetHouseType(tile)] & 0x20)) {
|
2005-07-15 16:29:30 +00:00
|
|
|
DeleteAnimatedTile(tile);
|
|
|
|
return;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-03 11:46:28 +00:00
|
|
|
if (!IsLiftMoving(tile)) {
|
2005-11-14 19:48:04 +00:00
|
|
|
int i;
|
|
|
|
|
2006-01-26 18:36:14 +00:00
|
|
|
/** Building has 6 floors, number 0 .. 6, where 1 is illegal.
|
|
|
|
* This is due to the fact that the first floor is, in the graphics,
|
|
|
|
* the height of 2 'normal' floors.
|
|
|
|
* Furthermore, there are 6 lift positions from floor N (incl) to floor N + 1 (excl) */
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2005-11-14 19:48:04 +00:00
|
|
|
i = (Random() & 7) - 1;
|
2006-04-03 11:46:28 +00:00
|
|
|
} while (i < 0 || i == 1 || i * 6 == GetLiftPosition(tile));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-03 11:46:28 +00:00
|
|
|
SetLiftDestination(tile, i);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-04-03 11:46:28 +00:00
|
|
|
pos = GetLiftPosition(tile);
|
|
|
|
dest = GetLiftDestination(tile) * 6;
|
|
|
|
pos += (pos < dest) ? 1 : -1;
|
|
|
|
SetLiftPosition(tile, pos);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-03 11:46:28 +00:00
|
|
|
if (pos == dest) HaltLift(tile);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdateTownRadius(Town *t);
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool IsCloseToTown(TileIndex tile, uint dist)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-11-13 13:43:55 +00:00
|
|
|
const Town* t;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
FOR_ALL_TOWNS(t) {
|
2006-08-22 15:33:35 +00:00
|
|
|
if (DistanceManhattan(tile, t->xy) < dist) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-12-31 18:57:24 +00:00
|
|
|
static void MarkTownSignDirty(Town *t)
|
|
|
|
{
|
|
|
|
MarkAllViewportsDirty(
|
|
|
|
t->sign.left-6,
|
|
|
|
t->sign.top-3,
|
|
|
|
t->sign.left+t->sign.width_1*4+12,
|
|
|
|
t->sign.top + 45
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateTownVirtCoord(Town *t)
|
|
|
|
{
|
2004-12-31 18:59:22 +00:00
|
|
|
Point pt;
|
|
|
|
|
2004-12-31 18:57:24 +00:00
|
|
|
MarkTownSignDirty(t);
|
2006-04-03 09:07:21 +00:00
|
|
|
pt = RemapCoords2(TileX(t->xy) * TILE_SIZE, TileY(t->xy) * TILE_SIZE);
|
2005-07-15 18:30:13 +00:00
|
|
|
SetDParam(0, t->index);
|
|
|
|
SetDParam(1, t->population);
|
|
|
|
UpdateViewportSignPos(&t->sign, pt.x, pt.y - 24,
|
|
|
|
_patches.population_in_label ? STR_TOWN_LABEL_POP : STR_TOWN_LABEL);
|
2004-12-31 18:57:24 +00:00
|
|
|
MarkTownSignDirty(t);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static void ChangePopulation(Town *t, int mod)
|
|
|
|
{
|
|
|
|
t->population += mod;
|
|
|
|
InvalidateWindow(WC_TOWN_VIEW, t->index);
|
2004-12-31 18:57:24 +00:00
|
|
|
UpdateTownVirtCoord(t);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (_town_sort_order & 2) _town_sort_dirty = true;
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
uint32 GetWorldPopulation(void)
|
2005-01-21 16:51:25 +00:00
|
|
|
{
|
|
|
|
uint32 pop;
|
2005-11-13 13:43:55 +00:00
|
|
|
const Town* t;
|
|
|
|
|
2005-01-21 16:51:25 +00:00
|
|
|
pop = 0;
|
2005-11-14 19:48:04 +00:00
|
|
|
FOR_ALL_TOWNS(t) pop += t->population;
|
2005-01-21 16:51:25 +00:00
|
|
|
return pop;
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void MakeSingleHouseBigger(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-16 11:24:58 +00:00
|
|
|
assert(IsTileType(tile, MP_HOUSE));
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-04-03 11:46:28 +00:00
|
|
|
if (LiftHasDestination(tile)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-03 14:56:07 +00:00
|
|
|
IncHouseConstructionTick(tile);
|
|
|
|
if (GetHouseConstructionTick(tile) != 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-03 14:56:07 +00:00
|
|
|
IncHouseBuildingStage(tile); /*increase construction stage of one more step*/
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-03 14:56:07 +00:00
|
|
|
if (GetHouseBuildingStage(tile) == TOWN_HOUSE_COMPLETED){
|
|
|
|
/*Now, construction is completed. Can add population of building to the town*/
|
2006-03-30 15:06:49 +00:00
|
|
|
ChangePopulation(GetTownByTile(tile), _housetype_population[GetHouseType(tile)]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void MakeTownHouseBigger(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-03-30 15:06:49 +00:00
|
|
|
uint flags = _house_more_flags[GetHouseType(tile)];
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & 8) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 0));
|
|
|
|
if (flags & 4) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 1));
|
|
|
|
if (flags & 2) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 0));
|
|
|
|
if (flags & 1) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 1));
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void TileLoop_Town(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int house;
|
|
|
|
Town *t;
|
|
|
|
uint32 r;
|
|
|
|
|
2006-04-03 14:56:07 +00:00
|
|
|
if (GetHouseBuildingStage(tile) != TOWN_HOUSE_COMPLETED) {
|
|
|
|
/*Construction is not completed. See if we can go further in construction*/
|
2004-08-09 17:04:08 +00:00
|
|
|
MakeTownHouseBigger(tile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-03-30 15:06:49 +00:00
|
|
|
house = GetHouseType(tile);
|
2006-04-03 11:46:28 +00:00
|
|
|
if ((_housetype_extra_flags[house] & 0x20) && !LiftHasDestination(tile) && CHANCE16(1, 2) && AddAnimatedTile(tile)) BeginLiftMovement(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-03-24 12:00:24 +00:00
|
|
|
t = GetTownByTile(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
r = Random();
|
|
|
|
|
2005-07-21 06:31:02 +00:00
|
|
|
if (GB(r, 0, 8) < _housetype_population[house]) {
|
|
|
|
uint amt = GB(r, 0, 8) / 8 + 1;
|
|
|
|
uint moved;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
|
|
|
|
t->new_max_pass += amt;
|
|
|
|
moved = MoveGoodsToStation(tile, 1, 1, CT_PASSENGERS, amt);
|
|
|
|
t->new_act_pass += moved;
|
|
|
|
}
|
|
|
|
|
2005-07-21 06:31:02 +00:00
|
|
|
if (GB(r, 8, 8) < _housetype_mailamount[house] ) {
|
|
|
|
uint amt = GB(r, 8, 8) / 8 + 1;
|
|
|
|
uint moved;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
|
|
|
|
t->new_max_mail += amt;
|
|
|
|
moved = MoveGoodsToStation(tile, 1, 1, CT_MAIL, amt);
|
|
|
|
t->new_act_mail += moved;
|
|
|
|
}
|
|
|
|
|
2006-04-03 14:56:07 +00:00
|
|
|
if (_house_more_flags[house] & 8 && HASBIT(t->flags12, TOWN_IS_FUNDED) && --t->time_until_rebuild == 0) {
|
2005-07-21 06:31:02 +00:00
|
|
|
t->time_until_rebuild = GB(r, 16, 6) + 130;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
_current_player = OWNER_TOWN;
|
|
|
|
|
|
|
|
ClearTownHouse(t, tile);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// rebuild with another house?
|
2005-07-21 06:31:02 +00:00
|
|
|
if (GB(r, 24, 8) >= 12) DoBuildTownHouse(t, tile);
|
2004-09-25 17:37:32 +00:00
|
|
|
|
|
|
|
_current_player = OWNER_NONE;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void ClickTile_Town(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
/* not used */
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static int32 ClearTile_Town(TileIndex tile, byte flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int house, rating;
|
|
|
|
int32 cost;
|
|
|
|
Town *t;
|
|
|
|
|
|
|
|
// safety checks
|
|
|
|
if (!EnsureNoVehicle(tile)) return CMD_ERROR;
|
|
|
|
if (flags&DC_AUTO && !(flags&DC_AI_BUILDING)) return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED);
|
|
|
|
|
2006-03-30 15:06:49 +00:00
|
|
|
house = GetHouseType(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
cost = _price.remove_house * _housetype_remove_cost[house] >> 8;
|
|
|
|
|
|
|
|
rating = _housetype_remove_ratingmod[house];
|
|
|
|
_cleared_town_rating += rating;
|
2006-03-24 12:00:24 +00:00
|
|
|
_cleared_town = t = GetTownByTile(tile);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-10-14 22:31:18 +00:00
|
|
|
if (IsValidPlayer(_current_player)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (rating > t->ratings[_current_player] && !(flags & DC_NO_TOWN_RATING) && !_cheats.magic_bulldozer.value) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, t->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES);
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2005-01-14 09:20:12 +00:00
|
|
|
ChangeTownRating(t, -rating, RATING_HOUSE_MINIMUM);
|
2004-08-09 17:04:08 +00:00
|
|
|
ClearTownHouse(t, tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void GetAcceptedCargo_Town(TileIndex tile, AcceptedCargo ac)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-03-30 15:06:49 +00:00
|
|
|
byte type = GetHouseType(tile);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-11-21 10:49:40 +00:00
|
|
|
ac[CT_PASSENGERS] = _housetype_cargo_passengers[type];
|
2005-11-14 19:48:04 +00:00
|
|
|
ac[CT_MAIL] = _housetype_cargo_mail[type];
|
|
|
|
ac[CT_GOODS] = _housetype_cargo_goods[type];
|
|
|
|
ac[CT_FOOD] = _housetype_cargo_food[type];
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void GetTileDesc_Town(TileIndex tile, TileDesc *td)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-03-30 15:06:49 +00:00
|
|
|
td->str = _town_tile_names[GetHouseType(tile)];
|
2006-04-03 14:56:07 +00:00
|
|
|
if (GetHouseBuildingStage(tile) != TOWN_HOUSE_COMPLETED) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParamX(td->dparam, 0, td->str);
|
2004-08-09 17:04:08 +00:00
|
|
|
td->str = STR_2058_UNDER_CONSTRUCTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
td->owner = OWNER_TOWN;
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static uint32 GetTileTrackStatus_Town(TileIndex tile, TransportType mode)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
/* not used */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-18 20:56:44 +00:00
|
|
|
static void ChangeTileOwner_Town(TileIndex tile, PlayerID old_player, PlayerID new_player)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
/* not used */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-06 11:39:00 +00:00
|
|
|
static const TileIndexDiffC _roadblock_tileadd[] = {
|
|
|
|
{ 0, -1},
|
|
|
|
{ 1, 0},
|
|
|
|
{ 0, 1},
|
|
|
|
{-1, 0},
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Store the first 3 elements again.
|
|
|
|
// Lets us rotate without using &3.
|
2005-01-06 11:39:00 +00:00
|
|
|
{ 0, -1},
|
|
|
|
{ 1, 0},
|
|
|
|
{ 0, 1}
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2006-01-05 12:40:50 +00:00
|
|
|
|
|
|
|
static bool GrowTown(Town *t);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static void TownTickHandler(Town *t)
|
|
|
|
{
|
2006-04-03 14:56:07 +00:00
|
|
|
if (HASBIT(t->flags12, TOWN_IS_FUNDED)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
int i = t->grow_counter - 1;
|
|
|
|
if (i < 0) {
|
|
|
|
if (GrowTown(t)) {
|
|
|
|
i = t->growth_rate;
|
|
|
|
} else {
|
2004-09-10 19:02:27 +00:00
|
|
|
i = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
t->grow_counter = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateTownRadius(t);
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void OnTick_Town(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_game_mode == GM_EDITOR) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-07 13:20:22 +00:00
|
|
|
/* Make sure each town's tickhandler invocation frequency is about the
|
|
|
|
* same - TOWN_GROWTH_FREQUENCY - independent on the number of towns. */
|
2006-08-22 20:41:26 +00:00
|
|
|
for (_cur_town_iter += GetTownArraySize();
|
2005-03-20 00:32:26 +00:00
|
|
|
_cur_town_iter >= TOWN_GROWTH_FREQUENCY;
|
|
|
|
_cur_town_iter -= TOWN_GROWTH_FREQUENCY) {
|
2005-03-17 23:12:23 +00:00
|
|
|
uint32 i = _cur_town_ctr;
|
2005-03-07 13:20:22 +00:00
|
|
|
|
2006-08-22 20:41:26 +00:00
|
|
|
if (++_cur_town_ctr >= GetTownArraySize())
|
2005-03-06 23:46:52 +00:00
|
|
|
_cur_town_ctr = 0;
|
2005-02-01 18:32:01 +00:00
|
|
|
|
2006-08-22 18:15:17 +00:00
|
|
|
if (IsValidTownID(i)) TownTickHandler(GetTown(i));
|
2005-03-06 23:46:52 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-03-06 13:11:08 +00:00
|
|
|
static RoadBits GetTownRoadMask(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-03-06 13:11:08 +00:00
|
|
|
TrackBits b = GetAnyRoadTrackBits(tile);
|
|
|
|
RoadBits r = 0;
|
|
|
|
|
|
|
|
if (b & TRACK_BIT_X) r |= ROAD_X;
|
|
|
|
if (b & TRACK_BIT_Y) r |= ROAD_Y;
|
|
|
|
if (b & TRACK_BIT_UPPER) r |= ROAD_NE | ROAD_NW;
|
|
|
|
if (b & TRACK_BIT_LOWER) r |= ROAD_SE | ROAD_SW;
|
|
|
|
if (b & TRACK_BIT_LEFT) r |= ROAD_NW | ROAD_SW;
|
|
|
|
if (b & TRACK_BIT_RIGHT) r |= ROAD_NE | ROAD_SE;
|
2004-08-09 17:04:08 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool IsRoadAllowedHere(TileIndex tile, int dir)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-04-23 13:48:16 +00:00
|
|
|
Slope k;
|
|
|
|
Slope slope;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// If this assertion fails, it might be because the world contains
|
|
|
|
// land at the edges. This is not ok.
|
|
|
|
TILE_ASSERT(tile);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
for (;;) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// Check if there already is a road at this point?
|
2006-03-06 13:11:08 +00:00
|
|
|
if (GetAnyRoadTrackBits(tile) == 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// No, try to build one in the direction.
|
|
|
|
// if that fails clear the land, and if that fails exit.
|
|
|
|
// This is to make sure that we can build a road here later.
|
2006-04-10 07:15:58 +00:00
|
|
|
if (CmdFailed(DoCommand(tile, (dir & 1 ? ROAD_X : ROAD_Y), 0, DC_AUTO, CMD_BUILD_ROAD)) &&
|
|
|
|
CmdFailed(DoCommand(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR)))
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
slope = GetTileSlope(tile, NULL);
|
2006-04-23 13:48:16 +00:00
|
|
|
if (slope == SLOPE_FLAT) {
|
2004-11-20 01:08:41 +00:00
|
|
|
no_slope:
|
2004-08-09 17:04:08 +00:00
|
|
|
// Tile has no slope
|
|
|
|
// Disallow the road if any neighboring tile has a road.
|
2005-01-06 11:39:00 +00:00
|
|
|
if (HASBIT(GetTownRoadMask(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[dir+1]))), dir^2) ||
|
|
|
|
HASBIT(GetTownRoadMask(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[dir+3]))), dir^2) ||
|
|
|
|
HASBIT(GetTownRoadMask(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[dir+1]) + ToTileIndexDiff(_roadblock_tileadd[dir+2]))), dir) ||
|
|
|
|
HASBIT(GetTownRoadMask(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[dir+3]) + ToTileIndexDiff(_roadblock_tileadd[dir+2]))), dir))
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Otherwise allow
|
|
|
|
return true;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// If the tile is not a slope in the right direction, then
|
|
|
|
// maybe terraform some.
|
2006-04-23 13:48:16 +00:00
|
|
|
k = (dir & 1) ? SLOPE_NE : SLOPE_NW;
|
|
|
|
if (k != slope && ComplementSlope(k) != slope) {
|
2004-08-09 17:04:08 +00:00
|
|
|
uint32 r = Random();
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (CHANCE16I(1, 8, r) && !_generating_world) {
|
2004-11-20 01:08:41 +00:00
|
|
|
int32 res;
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (CHANCE16I(1, 16, r)) {
|
2006-04-10 07:15:58 +00:00
|
|
|
res = DoCommand(tile, slope, 0, DC_EXEC | DC_AUTO | DC_NO_WATER,
|
2004-11-20 01:08:41 +00:00
|
|
|
CMD_TERRAFORM_LAND);
|
2005-11-14 19:48:04 +00:00
|
|
|
} else {
|
2006-04-23 17:58:07 +00:00
|
|
|
res = DoCommand(tile, slope ^ 0xF, 1, DC_EXEC | DC_AUTO | DC_NO_WATER,
|
2004-11-20 01:08:41 +00:00
|
|
|
CMD_TERRAFORM_LAND);
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2005-11-15 10:50:43 +00:00
|
|
|
if (CmdFailed(res) && CHANCE16I(1, 3, r)) {
|
2004-11-20 01:08:41 +00:00
|
|
|
// We can consider building on the slope, though.
|
|
|
|
goto no_slope;
|
2005-11-15 10:50:43 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2004-12-31 18:57:24 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool TerraformTownTile(TileIndex tile, int edges, int dir)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int32 r;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
TILE_ASSERT(tile);
|
|
|
|
|
2006-04-10 07:15:58 +00:00
|
|
|
r = DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
|
2005-11-15 10:50:43 +00:00
|
|
|
if (CmdFailed(r) || r >= 126 * 16) return false;
|
2006-04-10 07:15:58 +00:00
|
|
|
DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER | DC_EXEC, CMD_TERRAFORM_LAND);
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void LevelTownLand(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-04-23 13:48:16 +00:00
|
|
|
Slope tileh;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
TILE_ASSERT(tile);
|
|
|
|
|
|
|
|
// Don't terraform if land is plain or if there's a house there.
|
2006-02-21 07:41:54 +00:00
|
|
|
if (IsTileType(tile, MP_HOUSE)) return;
|
|
|
|
tileh = GetTileSlope(tile, NULL);
|
2006-04-23 13:48:16 +00:00
|
|
|
if (tileh == SLOPE_FLAT) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// First try up, then down
|
2006-02-21 07:41:54 +00:00
|
|
|
if (!TerraformTownTile(tile, ~tileh & 0xF, 1)) {
|
|
|
|
TerraformTownTile(tile, tileh & 0xF, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-06 13:11:08 +00:00
|
|
|
static void GrowTownInTile(TileIndex* tile_ptr, RoadBits mask, int block, Town* t1)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-03-06 13:11:08 +00:00
|
|
|
RoadBits rcmd;
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tmptile;
|
2006-03-06 13:11:08 +00:00
|
|
|
DiagDirection i;
|
2004-08-09 17:04:08 +00:00
|
|
|
int j;
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile = *tile_ptr;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
TILE_ASSERT(tile);
|
|
|
|
|
|
|
|
if (mask == 0) {
|
2006-03-06 13:11:08 +00:00
|
|
|
int a;
|
|
|
|
int b;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Tile has no road. First reset the status counter
|
2004-09-10 19:02:27 +00:00
|
|
|
// to say that this is the last iteration.
|
2004-08-09 17:04:08 +00:00
|
|
|
_grow_town_result = 0;
|
|
|
|
|
|
|
|
// Remove hills etc
|
|
|
|
LevelTownLand(tile);
|
|
|
|
|
|
|
|
// Is a road allowed here?
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!IsRoadAllowedHere(tile, block)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Randomize new road block numbers
|
|
|
|
a = block;
|
|
|
|
b = block ^ 2;
|
2005-11-14 09:21:05 +00:00
|
|
|
if (CHANCE16(1, 4)) {
|
|
|
|
do {
|
|
|
|
a = GB(Random(), 0, 2);
|
2006-02-01 07:36:15 +00:00
|
|
|
} while (a == b);
|
2005-11-14 09:21:05 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-06 11:39:00 +00:00
|
|
|
if (!IsRoadAllowedHere(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a])), a)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// A road is not allowed to continue the randomized road,
|
|
|
|
// return if the road we're trying to build is curved.
|
2005-11-14 19:48:04 +00:00
|
|
|
if (a != (b ^ 2)) return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Return if neither side of the new road is a house
|
2005-01-16 11:24:58 +00:00
|
|
|
if (!IsTileType(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a + 1])), MP_HOUSE) &&
|
|
|
|
!IsTileType(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a + 3])), MP_HOUSE))
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// That means that the road is only allowed if there is a house
|
|
|
|
// at any side of the new road.
|
|
|
|
}
|
|
|
|
rcmd = (1 << a) + (1 << b);
|
|
|
|
|
|
|
|
} else if (block < 5 && !HASBIT(mask,block^2)) {
|
|
|
|
// Continue building on a partial road.
|
|
|
|
// Always OK.
|
|
|
|
_grow_town_result = 0;
|
2005-11-14 19:48:04 +00:00
|
|
|
rcmd = 1 << (block ^ 2);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2006-03-06 13:11:08 +00:00
|
|
|
int i;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Reached a tunnel? Then continue at the other side of it.
|
2006-03-12 15:04:03 +00:00
|
|
|
if (IsTunnelTile(tile) && GetTunnelTransportType(tile) == TRANSPORT_ROAD) {
|
2006-03-06 20:55:24 +00:00
|
|
|
*tile_ptr = GetOtherTunnelEnd(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// For any other kind of tunnel/bridge, bail out.
|
2005-11-14 19:48:04 +00:00
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Possibly extend the road in a direction.
|
|
|
|
// Randomize a direction and if it has a road, bail out.
|
2005-11-14 08:09:57 +00:00
|
|
|
i = GB(Random(), 0, 2);
|
2005-11-14 19:48:04 +00:00
|
|
|
if (HASBIT(mask, i)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// This is the tile we will reach if we extend to this direction.
|
2005-01-06 11:39:00 +00:00
|
|
|
tmptile = TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[i]));
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Don't do it if it reaches to water.
|
2006-04-03 15:11:17 +00:00
|
|
|
if (IsClearWaterTile(tmptile)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
// Build a house at the edge. 60% chance or
|
2004-08-09 17:04:08 +00:00
|
|
|
// always ok if no road allowed.
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!IsRoadAllowedHere(tmptile, i) || CHANCE16(6, 10)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// But not if there already is a house there.
|
2005-01-16 11:24:58 +00:00
|
|
|
if (!IsTileType(tmptile, MP_HOUSE)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// Level the land if possible
|
|
|
|
LevelTownLand(tmptile);
|
|
|
|
|
|
|
|
// And build a house.
|
|
|
|
// Set result to -1 if we managed to build it.
|
2005-11-14 19:48:04 +00:00
|
|
|
if (BuildTownHouse(t1, tmptile)) _grow_town_result = -1;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_grow_town_result = 0;
|
|
|
|
rcmd = 1 << i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return if a water tile
|
2006-04-03 15:11:17 +00:00
|
|
|
if (IsClearWaterTile(tile)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Determine direction of slope,
|
|
|
|
// and build a road if not a special slope.
|
2006-02-21 07:41:54 +00:00
|
|
|
switch (GetTileSlope(tile, NULL)) {
|
2006-04-23 13:48:16 +00:00
|
|
|
case SLOPE_SW: i = DIAGDIR_NE; break;
|
|
|
|
case SLOPE_SE: i = DIAGDIR_NW; break;
|
|
|
|
case SLOPE_NW: i = DIAGDIR_SE; break;
|
|
|
|
case SLOPE_NE: i = DIAGDIR_SW; break;
|
2006-02-21 07:41:54 +00:00
|
|
|
|
|
|
|
default:
|
2004-08-09 17:04:08 +00:00
|
|
|
build_road_and_exit:
|
2006-04-10 07:15:58 +00:00
|
|
|
if (!CmdFailed(DoCommand(tile, rcmd, t1->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD))) {
|
2006-02-21 07:41:54 +00:00
|
|
|
_grow_town_result = -1;
|
|
|
|
}
|
|
|
|
return;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmptile = tile;
|
|
|
|
|
|
|
|
// Now it contains the direction of the slope
|
2006-08-28 18:53:03 +00:00
|
|
|
j = -11; // max 11 tile long bridges
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
|
|
|
if (++j == 0)
|
|
|
|
goto build_road_and_exit;
|
2006-09-05 23:21:41 +00:00
|
|
|
tmptile = TILE_MASK(tmptile + TileOffsByDiagDir(i));
|
2006-04-03 15:11:17 +00:00
|
|
|
} while (IsClearWaterTile(tmptile));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// no water tiles in between?
|
|
|
|
if (j == -10)
|
|
|
|
goto build_road_and_exit;
|
|
|
|
|
|
|
|
// Quit if it selecting an appropiate bridge type fails a large number of times.
|
|
|
|
j = 22;
|
|
|
|
{
|
|
|
|
int32 bridge_len = GetBridgeLength(tile, tmptile);
|
|
|
|
do {
|
|
|
|
byte bridge_type = RandomRange(MAX_BRIDGES - 1);
|
|
|
|
if (CheckBridge_Stuff(bridge_type, bridge_len)) {
|
2006-04-10 07:15:58 +00:00
|
|
|
if (!CmdFailed(DoCommand(tile, tmptile, 0x8000 + bridge_type, DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE)))
|
2004-08-09 17:04:08 +00:00
|
|
|
_grow_town_result = -1;
|
|
|
|
|
|
|
|
// obviously, if building any bridge would fail, there is no need to try other bridge-types
|
|
|
|
return;
|
|
|
|
}
|
2006-02-01 07:36:15 +00:00
|
|
|
} while (--j != 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if a house was built, or no if the build failed.
|
2005-06-24 12:38:35 +00:00
|
|
|
static int GrowTownAtRoad(Town *t, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int block = 5; // special case
|
|
|
|
|
|
|
|
TILE_ASSERT(tile);
|
|
|
|
|
|
|
|
// Number of times to search.
|
2004-11-20 11:57:45 +00:00
|
|
|
_grow_town_result = 10 + t->num_houses * 4 / 9;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
// Get a bitmask of the road blocks on a tile
|
2006-03-06 13:11:08 +00:00
|
|
|
RoadBits mask = GetTownRoadMask(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Try to grow the town from this point
|
|
|
|
GrowTownInTile(&tile,mask,block,t);
|
|
|
|
|
|
|
|
// Exclude the source position from the bitmask
|
|
|
|
// and return if no more road blocks available
|
|
|
|
CLRBIT(mask, (block ^ 2));
|
|
|
|
if (mask == 0)
|
|
|
|
return _grow_town_result;
|
|
|
|
|
|
|
|
// Select a random bit from the blockmask, walk a step
|
|
|
|
// and continue the search from there.
|
|
|
|
do block = Random() & 3; while (!HASBIT(mask,block));
|
2005-01-06 11:39:00 +00:00
|
|
|
tile += ToTileIndexDiff(_roadblock_tileadd[block]);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-06 20:29:32 +00:00
|
|
|
if (IsTileType(tile, MP_STREET)) {
|
|
|
|
/* Don't allow building over roads of other cities */
|
2006-06-27 21:25:53 +00:00
|
|
|
if (IsTileOwner(tile, OWNER_TOWN) && GetTownByTile(tile) != t) {
|
2005-02-06 20:29:32 +00:00
|
|
|
_grow_town_result = -1;
|
2006-06-27 21:25:53 +00:00
|
|
|
} else if (_game_mode == GM_EDITOR) {
|
2005-02-06 20:29:32 +00:00
|
|
|
/* If we are in the SE, and this road-piece has no town owner yet, it just found an
|
2006-09-04 20:40:33 +00:00
|
|
|
* owner :) (happy happy happy road now) */
|
2005-06-04 12:13:24 +00:00
|
|
|
SetTileOwner(tile, OWNER_TOWN);
|
2006-04-03 14:56:07 +00:00
|
|
|
SetTownIndex(tile, t->index);
|
2005-02-06 20:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
2005-02-04 13:56:51 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Max number of times is checked.
|
|
|
|
} while (--_grow_town_result >= 0);
|
|
|
|
|
|
|
|
return (_grow_town_result == -2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a random road block
|
|
|
|
// The probability of a straight road
|
|
|
|
// is somewhat higher than a curved.
|
2006-03-06 13:11:08 +00:00
|
|
|
static RoadBits GenRandomRoadBits(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint32 r = Random();
|
2005-07-20 15:29:28 +00:00
|
|
|
uint a = GB(r, 0, 2);
|
|
|
|
uint b = GB(r, 8, 2);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (a == b) b ^= 2;
|
2005-07-20 15:29:28 +00:00
|
|
|
return (1 << a) + (1 << b);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Grow the town
|
|
|
|
// Returns true if a house was built, or no if the build failed.
|
2006-01-05 12:40:50 +00:00
|
|
|
static bool GrowTown(Town *t)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
2005-01-06 11:39:00 +00:00
|
|
|
const TileIndexDiffC *ptr;
|
2005-10-07 07:35:15 +00:00
|
|
|
PlayerID old_player;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-06 11:39:00 +00:00
|
|
|
static const TileIndexDiffC _town_coord_mod[] = {
|
|
|
|
{-1, 0},
|
|
|
|
{ 1, 1},
|
|
|
|
{ 1, -1},
|
|
|
|
{-1, -1},
|
|
|
|
{-1, 0},
|
|
|
|
{ 0, 2},
|
|
|
|
{ 2, 0},
|
|
|
|
{ 0, -2},
|
|
|
|
{-1, -1},
|
|
|
|
{-2, 2},
|
|
|
|
{ 2, 2},
|
|
|
|
{ 2, -2},
|
|
|
|
{ 0, 0}
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Current player is a town
|
2004-09-15 18:36:33 +00:00
|
|
|
old_player = _current_player;
|
2004-08-09 17:04:08 +00:00
|
|
|
_current_player = OWNER_TOWN;
|
|
|
|
|
|
|
|
// Find a road that we can base the construction on.
|
|
|
|
tile = t->xy;
|
2005-01-06 11:39:00 +00:00
|
|
|
for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
|
2006-03-06 13:11:08 +00:00
|
|
|
if (GetAnyRoadTrackBits(tile) != 0) {
|
2004-09-15 18:36:33 +00:00
|
|
|
int r = GrowTownAtRoad(t, tile);
|
|
|
|
_current_player = old_player;
|
|
|
|
return r;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-06 11:39:00 +00:00
|
|
|
tile = TILE_ADD(tile, ToTileIndexDiff(*ptr));
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// No road available, try to build a random road block by
|
|
|
|
// clearing some land and then building a road there.
|
|
|
|
tile = t->xy;
|
2005-01-06 11:39:00 +00:00
|
|
|
for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
|
2006-08-20 11:41:34 +00:00
|
|
|
/* Only work with plain land that not already has a house */
|
|
|
|
if (!IsTileType(tile, MP_HOUSE) && GetTileSlope(tile, NULL) == SLOPE_FLAT) {
|
2006-04-10 07:15:58 +00:00
|
|
|
if (!CmdFailed(DoCommand(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR))) {
|
|
|
|
DoCommand(tile, GenRandomRoadBits(), t->index, DC_EXEC | DC_AUTO, CMD_BUILD_ROAD);
|
2004-09-15 18:36:33 +00:00
|
|
|
_current_player = old_player;
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2005-01-06 11:39:00 +00:00
|
|
|
tile = TILE_ADD(tile, ToTileIndexDiff(*ptr));
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-15 18:36:33 +00:00
|
|
|
_current_player = old_player;
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdateTownRadius(Town *t)
|
|
|
|
{
|
|
|
|
static const uint16 _town_radius_data[23][5] = {
|
2006-08-22 14:38:37 +00:00
|
|
|
{ 4, 0, 0, 0, 0}, // 0
|
|
|
|
{ 16, 0, 0, 0, 0},
|
|
|
|
{ 25, 0, 0, 0, 0},
|
|
|
|
{ 36, 0, 0, 0, 0},
|
|
|
|
{ 49, 0, 4, 0, 0},
|
|
|
|
{ 64, 0, 4, 0, 0}, // 20
|
|
|
|
{ 64, 0, 9, 0, 1},
|
|
|
|
{ 64, 0, 9, 0, 4},
|
|
|
|
{ 64, 0, 16, 0, 4},
|
|
|
|
{ 81, 0, 16, 0, 4},
|
|
|
|
{ 81, 0, 16, 0, 4}, // 40
|
|
|
|
{ 81, 0, 25, 0, 9},
|
|
|
|
{ 81, 36, 25, 0, 9},
|
|
|
|
{ 81, 36, 25, 16, 9},
|
|
|
|
{ 81, 49, 0, 25, 9},
|
|
|
|
{ 81, 64, 0, 25, 9}, // 60
|
|
|
|
{ 81, 64, 0, 36, 9},
|
|
|
|
{ 81, 64, 0, 36, 16},
|
2004-11-20 11:57:45 +00:00
|
|
|
{100, 81, 0, 49, 16},
|
|
|
|
{100, 81, 0, 49, 25},
|
|
|
|
{121, 81, 0, 49, 25}, // 80
|
|
|
|
{121, 81, 0, 49, 25},
|
|
|
|
{121, 81, 0, 49, 36}, // 88
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
2004-11-20 11:57:45 +00:00
|
|
|
|
|
|
|
if (t->num_houses < 92) {
|
|
|
|
memcpy(t->radius, _town_radius_data[t->num_houses / 4], sizeof(t->radius));
|
|
|
|
} else {
|
|
|
|
int mass = t->num_houses / 8;
|
|
|
|
// At least very roughly extrapolate. Empirical numbers dancing between
|
|
|
|
// overwhelming by cottages and skyscrapers outskirts.
|
|
|
|
t->radius[0] = mass * mass;
|
|
|
|
// Actually we are proportional to sqrt() but that's right because
|
|
|
|
// we are covering an area.
|
|
|
|
t->radius[1] = mass * 7;
|
|
|
|
t->radius[2] = 0;
|
|
|
|
t->radius[3] = mass * 4;
|
|
|
|
t->radius[4] = mass * 3;
|
|
|
|
//debug("%d (->%d): %d %d %d %d\n", t->num_houses, mass, t->radius[0], t->radius[1], t->radius[3], t->radius[4]);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 13:49:43 +00:00
|
|
|
static bool CreateTownName(uint32 *townnameparts)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Town *t2;
|
|
|
|
char buf1[64];
|
|
|
|
char buf2[64];
|
|
|
|
uint32 r;
|
2005-03-05 13:49:43 +00:00
|
|
|
/* Do not set too low tries, since when we run out of names, we loop
|
|
|
|
* for #tries only one time anyway - then we stop generating more
|
|
|
|
* towns. Do not show it too high neither, since looping through all
|
|
|
|
* the other towns may take considerable amount of time (10000 is
|
|
|
|
* too much). */
|
|
|
|
int tries = 1000;
|
|
|
|
uint16 townnametype = SPECSTR_TOWNNAME_START + _opt.town_name;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-05 13:49:43 +00:00
|
|
|
assert(townnameparts);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2004-08-09 17:04:08 +00:00
|
|
|
restart:
|
|
|
|
r = Random();
|
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, r);
|
2006-10-21 23:31:34 +00:00
|
|
|
GetString(buf1, townnametype, lastof(buf1));
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Check size and width
|
2006-09-16 13:20:14 +00:00
|
|
|
if (strlen(buf1) >= 31 || GetStringBoundingBox(buf1).width > 130) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
FOR_ALL_TOWNS(t2) {
|
2006-08-22 15:33:35 +00:00
|
|
|
// We can't just compare the numbers since
|
|
|
|
// several numbers may map to a single name.
|
|
|
|
SetDParam(0, t2->index);
|
2006-10-21 23:31:34 +00:00
|
|
|
GetString(buf2, STR_TOWN, lastof(buf2));
|
2006-08-22 15:33:35 +00:00
|
|
|
if (strcmp(buf1, buf2) == 0) {
|
|
|
|
if (tries-- < 0) return false;
|
|
|
|
goto restart;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-05 13:49:43 +00:00
|
|
|
*townnameparts = r;
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-17 10:56:19 +00:00
|
|
|
void UpdateTownMaxPass(Town *t)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
t->max_pass = t->population >> 3;
|
|
|
|
t->max_mail = t->population >> 4;
|
|
|
|
}
|
|
|
|
|
2006-04-27 11:19:12 +00:00
|
|
|
static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, uint size_mode)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int x, i;
|
|
|
|
|
|
|
|
// clear the town struct
|
|
|
|
i = t->index;
|
|
|
|
memset(t, 0, sizeof(Town));
|
|
|
|
t->index = i;
|
|
|
|
|
|
|
|
t->xy = tile;
|
|
|
|
t->num_houses = 0;
|
|
|
|
t->time_until_rebuild = 10;
|
|
|
|
UpdateTownRadius(t);
|
|
|
|
t->flags12 = 0;
|
|
|
|
t->population = 0;
|
|
|
|
t->grow_counter = 0;
|
|
|
|
t->growth_rate = 250;
|
|
|
|
t->new_max_pass = 0;
|
|
|
|
t->new_max_mail = 0;
|
|
|
|
t->new_act_pass = 0;
|
|
|
|
t->new_act_mail = 0;
|
|
|
|
t->max_pass = 0;
|
|
|
|
t->max_mail = 0;
|
|
|
|
t->act_pass = 0;
|
|
|
|
t->act_mail = 0;
|
|
|
|
|
|
|
|
t->pct_pass_transported = 0;
|
|
|
|
t->pct_mail_transported = 0;
|
|
|
|
t->fund_buildings_months = 0;
|
|
|
|
t->new_act_food = 0;
|
2004-08-10 14:42:52 +00:00
|
|
|
t->new_act_water = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
t->act_food = 0;
|
2004-08-10 14:42:52 +00:00
|
|
|
t->act_water = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
for (i = 0; i != MAX_PLAYERS; i++)
|
2004-08-09 17:04:08 +00:00
|
|
|
t->ratings[i] = 500;
|
|
|
|
|
|
|
|
t->have_ratings = 0;
|
2004-08-23 21:04:39 +00:00
|
|
|
t->exclusivity = (byte)-1;
|
|
|
|
t->exclusive_counter = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
t->statues = 0;
|
|
|
|
|
2005-03-05 13:49:43 +00:00
|
|
|
t->townnametype = SPECSTR_TOWNNAME_START + _opt.town_name;
|
|
|
|
t->townnameparts = townnameparts;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateTownVirtCoord(t);
|
|
|
|
_town_sort_dirty = true;
|
|
|
|
|
2006-04-27 11:19:12 +00:00
|
|
|
if (size_mode == 0) {
|
|
|
|
x = (Random() & 0xF) + 8;
|
|
|
|
} else {
|
|
|
|
x = (size_mode - 1) * 16 + 3;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
t->num_houses += x;
|
|
|
|
UpdateTownRadius(t);
|
|
|
|
|
|
|
|
i = x * 4;
|
|
|
|
do {
|
|
|
|
GrowTown(t);
|
|
|
|
} while (--i);
|
|
|
|
|
|
|
|
t->num_houses -= x;
|
|
|
|
UpdateTownRadius(t);
|
|
|
|
UpdateTownMaxPass(t);
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static Town *AllocateTown(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Town *t;
|
2006-08-22 15:33:35 +00:00
|
|
|
|
|
|
|
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
|
|
|
|
* TODO - This is just a temporary stage, this will be removed. */
|
2006-10-28 11:55:29 +00:00
|
|
|
for (t = GetTown(0); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) {
|
2006-08-22 15:33:35 +00:00
|
|
|
if (!IsValidTown(t)) {
|
2006-08-20 19:31:58 +00:00
|
|
|
TownID index = t->index;
|
2005-02-01 18:32:01 +00:00
|
|
|
|
2006-08-22 21:17:19 +00:00
|
|
|
if (t->index >= _total_towns) _total_towns = t->index + 1;
|
2005-02-01 18:32:01 +00:00
|
|
|
|
|
|
|
memset(t, 0, sizeof(Town));
|
|
|
|
t->index = index;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
2005-02-01 18:32:01 +00:00
|
|
|
|
|
|
|
/* Check if we can add a block to the pool */
|
2006-10-28 11:55:29 +00:00
|
|
|
if (AddBlockToPool(&_Town_pool))
|
2005-02-01 18:32:01 +00:00
|
|
|
return AllocateTown();
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Create a new town.
|
|
|
|
* This obviously only works in the scenario editor. Function not removed
|
|
|
|
* as it might be possible in the future to fund your own town :)
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile coordinates where town is built
|
2006-11-09 13:03:34 +00:00
|
|
|
* @param p1 size of the town (0 = random, 1 = small, 2 = medium, 3 = large)
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2006-04-10 07:15:58 +00:00
|
|
|
int32 CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Town *t;
|
2005-03-05 13:49:43 +00:00
|
|
|
uint32 townnameparts;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/* Only in the scenario editor */
|
|
|
|
if (_game_mode != GM_EDITOR) return CMD_ERROR;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_OTHER);
|
|
|
|
|
|
|
|
// Check if too close to the edge of map
|
2005-01-31 07:23:15 +00:00
|
|
|
if (DistanceFromEdge(tile) < 12)
|
2004-08-09 17:04:08 +00:00
|
|
|
return_cmd_error(STR_0237_TOO_CLOSE_TO_EDGE_OF_MAP);
|
|
|
|
|
2006-11-15 18:57:00 +00:00
|
|
|
// Can only build on clear flat areas, possibly with trees.
|
|
|
|
if ((!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES)) || GetTileSlope(tile, NULL) != SLOPE_FLAT) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return_cmd_error(STR_0239_SITE_UNSUITABLE);
|
2006-02-06 08:15:30 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Check distance to all other towns.
|
|
|
|
if (IsCloseToTown(tile, 20))
|
|
|
|
return_cmd_error(STR_0238_TOO_CLOSE_TO_ANOTHER_TOWN);
|
|
|
|
|
2005-03-05 13:49:43 +00:00
|
|
|
// Get a unique name for the town.
|
|
|
|
if (!CreateTownName(&townnameparts))
|
|
|
|
return_cmd_error(STR_023A_TOO_MANY_TOWNS);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Allocate town struct
|
|
|
|
t = AllocateTown();
|
2005-05-12 00:11:37 +00:00
|
|
|
if (t == NULL) return_cmd_error(STR_023A_TOO_MANY_TOWNS);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Create the town
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
_generating_world = true;
|
2006-04-27 11:19:12 +00:00
|
|
|
DoCreateTown(t, tile, townnameparts, p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
_generating_world = false;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-27 11:19:12 +00:00
|
|
|
Town *CreateRandomTown(uint attempts, uint size_mode)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
Town *t;
|
2005-03-05 13:49:43 +00:00
|
|
|
uint32 townnameparts;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
// Generate a tile index not too close from the edge
|
2005-07-13 19:51:31 +00:00
|
|
|
tile = RandomTile();
|
2006-02-01 06:32:03 +00:00
|
|
|
if (DistanceFromEdge(tile) < 20) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Make sure the tile is plain
|
2006-04-23 13:48:16 +00:00
|
|
|
if (!IsTileType(tile, MP_CLEAR) || GetTileSlope(tile, NULL) != SLOPE_FLAT) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Check not too close to a town
|
2006-02-01 06:32:03 +00:00
|
|
|
if (IsCloseToTown(tile, 20)) continue;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-03-05 13:49:43 +00:00
|
|
|
// Get a unique name for the town.
|
2006-02-01 06:32:03 +00:00
|
|
|
if (!CreateTownName(&townnameparts)) break;
|
2005-03-05 13:49:43 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Allocate a town struct
|
|
|
|
t = AllocateTown();
|
2006-02-01 06:32:03 +00:00
|
|
|
if (t == NULL) break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-27 11:19:12 +00:00
|
|
|
DoCreateTown(t, tile, townnameparts, size_mode);
|
2004-08-09 17:04:08 +00:00
|
|
|
return t;
|
2005-02-13 09:42:49 +00:00
|
|
|
} while (--attempts);
|
2004-08-09 17:04:08 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-09-16 00:33:33 +00:00
|
|
|
static const byte _num_initial_towns[3] = {11, 23, 46};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-09-16 00:33:33 +00:00
|
|
|
bool GenerateTowns(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-02-13 09:42:49 +00:00
|
|
|
uint num = 0;
|
2005-09-16 00:33:33 +00:00
|
|
|
uint n = ScaleByMapSize(_num_initial_towns[_opt.diff.number_towns] + (Random() & 7));
|
2005-01-28 15:31:04 +00:00
|
|
|
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
SetGeneratingWorldProgress(GWP_TOWN, n);
|
|
|
|
|
2005-02-13 09:42:49 +00:00
|
|
|
do {
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
IncreaseGeneratingWorldProgress(GWP_TOWN);
|
2006-06-10 08:37:41 +00:00
|
|
|
// try 20 times to create a random-sized town for the first loop.
|
|
|
|
if (CreateRandomTown(20, 0) != NULL) num++;
|
2005-02-13 09:42:49 +00:00
|
|
|
} while (--n);
|
|
|
|
|
2005-09-16 00:33:33 +00:00
|
|
|
// give it a last try, but now more aggressive
|
2006-04-27 11:19:12 +00:00
|
|
|
if (num == 0 && CreateRandomTown(10000, 0) == NULL) {
|
2006-08-26 14:22:54 +00:00
|
|
|
if (GetTownArraySize() == 0) {
|
|
|
|
/* XXX - can we handle that more gracefully? */
|
|
|
|
if (_game_mode != GM_EDITOR) error("Could not generate any town");
|
2005-09-16 00:33:33 +00:00
|
|
|
|
2006-08-26 14:22:54 +00:00
|
|
|
return false;
|
2006-06-10 08:37:41 +00:00
|
|
|
}
|
2005-02-13 09:42:49 +00:00
|
|
|
}
|
2005-09-16 00:33:33 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-04-23 13:48:16 +00:00
|
|
|
static bool CheckBuildHouseMode(TileIndex tile, Slope tileh, int mode)
|
2005-06-24 12:38:35 +00:00
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
int b;
|
2006-04-23 13:48:16 +00:00
|
|
|
Slope slope;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static const byte _masks[8] = {
|
|
|
|
0xC,0x3,0x9,0x6,
|
|
|
|
0x3,0xC,0x6,0x9,
|
|
|
|
};
|
|
|
|
|
|
|
|
slope = GetTileSlope(tile, NULL);
|
2006-04-23 13:48:16 +00:00
|
|
|
if (IsSteepSlope(slope)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
b = 0;
|
2006-04-23 13:48:16 +00:00
|
|
|
if ((slope != SLOPE_FLAT && ~slope & _masks[mode])) b = ~b;
|
|
|
|
if ((tileh != SLOPE_FLAT && ~tileh & _masks[mode+4])) b = ~b;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (b)
|
|
|
|
return false;
|
|
|
|
|
2006-04-10 07:15:58 +00:00
|
|
|
return !CmdFailed(DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-06-10 08:37:41 +00:00
|
|
|
|
|
|
|
uint GetTownRadiusGroup(const Town* t, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-06-10 08:37:41 +00:00
|
|
|
uint dist = DistanceSquare(tile, t->xy);
|
|
|
|
uint smallest;
|
|
|
|
uint i;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-06-10 08:37:41 +00:00
|
|
|
if (t->fund_buildings_months && dist <= 25) return 4;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
smallest = 0;
|
2005-09-18 20:56:44 +00:00
|
|
|
for (i = 0; i != lengthof(t->radius); i++) {
|
2006-06-10 08:37:41 +00:00
|
|
|
if (dist < t->radius[i]) smallest = i;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return smallest;
|
|
|
|
}
|
|
|
|
|
2006-02-02 07:15:46 +00:00
|
|
|
static bool CheckFree2x2Area(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2005-01-06 11:39:00 +00:00
|
|
|
static const TileIndexDiffC _tile_add[] = {
|
|
|
|
{0 , 0 },
|
|
|
|
{0 - 0, 1 - 0},
|
|
|
|
{1 - 0, 0 - 1},
|
|
|
|
{1 - 1, 1 - 0}
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
for (i = 0; i != 4; i++) {
|
2005-01-06 11:39:00 +00:00
|
|
|
tile += ToTileIndexDiff(_tile_add[i]);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-23 13:48:16 +00:00
|
|
|
if (GetTileSlope(tile, NULL) != SLOPE_FLAT) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-10 07:15:58 +00:00
|
|
|
if (CmdFailed(DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER | DC_FORCETEST, CMD_LANDSCAPE_CLEAR)))
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void DoBuildTownHouse(Town *t, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint bitmask;
|
|
|
|
int house;
|
2006-04-23 13:48:16 +00:00
|
|
|
Slope slope;
|
2005-02-07 10:41:45 +00:00
|
|
|
uint z;
|
2006-04-03 14:56:07 +00:00
|
|
|
uint oneof = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Above snow?
|
|
|
|
slope = GetTileSlope(tile, &z);
|
|
|
|
|
|
|
|
// Get the town zone type
|
|
|
|
{
|
|
|
|
uint rad = GetTownRadiusGroup(t, tile);
|
|
|
|
|
|
|
|
int land = _opt.landscape;
|
2006-06-10 08:37:41 +00:00
|
|
|
if (land == LT_HILLY && z >= _opt.snow_line) land = -1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
bitmask = (1 << rad) + (1 << (land + 12));
|
|
|
|
}
|
|
|
|
|
|
|
|
// bits 0-4 are used
|
|
|
|
// bits 11-15 are used
|
|
|
|
// bits 5-10 are not used.
|
|
|
|
{
|
|
|
|
byte houses[lengthof(_housetype_flags)];
|
|
|
|
int num = 0;
|
|
|
|
|
|
|
|
// Generate a list of all possible houses that can be built.
|
2006-02-01 07:36:15 +00:00
|
|
|
for (i=0; i!=lengthof(_housetype_flags); i++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if ((~_housetype_flags[i] & bitmask) == 0)
|
|
|
|
houses[num++] = (byte)i;
|
|
|
|
}
|
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2004-08-09 17:04:08 +00:00
|
|
|
house = houses[RandomRange(num)];
|
|
|
|
|
|
|
|
if (_cur_year < _housetype_years[house].min || _cur_year > _housetype_years[house].max)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Special houses that there can be only one of.
|
2004-11-22 18:42:03 +00:00
|
|
|
switch (house) {
|
|
|
|
case HOUSE_TEMP_CHURCH:
|
|
|
|
case HOUSE_ARCT_CHURCH:
|
|
|
|
case HOUSE_SNOW_CHURCH:
|
|
|
|
case HOUSE_TROP_CHURCH:
|
|
|
|
case HOUSE_TOY_CHURCH:
|
2006-04-03 14:56:07 +00:00
|
|
|
SETBIT(oneof, TOWN_HAS_CHURCH);
|
2004-11-22 18:42:03 +00:00
|
|
|
break;
|
|
|
|
case HOUSE_STADIUM:
|
|
|
|
case HOUSE_MODERN_STADIUM:
|
2006-04-03 14:56:07 +00:00
|
|
|
SETBIT(oneof, TOWN_HAS_STADIUM);
|
2004-11-22 18:42:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
oneof = 0;
|
|
|
|
break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-03 14:56:07 +00:00
|
|
|
if (HASBITS(t->flags12 , oneof)) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Make sure there is no slope?
|
2006-04-23 13:48:16 +00:00
|
|
|
if (_housetype_extra_flags[house] & 0x12 && slope != SLOPE_FLAT) continue;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
if (_housetype_extra_flags[house] & 0x10) {
|
2006-02-02 07:15:46 +00:00
|
|
|
if (CheckFree2x2Area(tile) ||
|
|
|
|
CheckFree2x2Area(tile += TileDiffXY(-1, 0)) ||
|
|
|
|
CheckFree2x2Area(tile += TileDiffXY( 0, -1)) ||
|
|
|
|
CheckFree2x2Area(tile += TileDiffXY( 1, 0))) {
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
|
|
|
tile += TileDiffXY(0, 1);
|
|
|
|
} else if (_housetype_extra_flags[house] & 4) {
|
2006-02-02 07:15:46 +00:00
|
|
|
if (CheckBuildHouseMode(tile + TileDiffXY(1, 0), slope, 0)) break;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-02-02 07:15:46 +00:00
|
|
|
if (CheckBuildHouseMode(tile + TileDiffXY(-1, 0), slope, 1)) {
|
2005-06-25 16:44:57 +00:00
|
|
|
tile += TileDiffXY(-1, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-02-01 06:32:03 +00:00
|
|
|
} else if (_housetype_extra_flags[house] & 8) {
|
2006-02-02 07:15:46 +00:00
|
|
|
if (CheckBuildHouseMode(tile + TileDiffXY(0, 1), slope, 2)) break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-02 07:15:46 +00:00
|
|
|
if (CheckBuildHouseMode(tile + TileDiffXY(0, -1), slope, 3)) {
|
2005-06-25 16:44:57 +00:00
|
|
|
tile += TileDiffXY(0, -1);
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-02-01 06:32:03 +00:00
|
|
|
} else {
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t->num_houses++;
|
|
|
|
|
|
|
|
// Special houses that there can be only one of.
|
|
|
|
t->flags12 |= oneof;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-03-31 08:18:14 +00:00
|
|
|
byte construction_counter = 0, construction_stage = 0, size_flags;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (_generating_world) {
|
|
|
|
uint32 r = Random();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-04-03 14:56:07 +00:00
|
|
|
construction_stage = TOWN_HOUSE_COMPLETED;
|
2006-03-31 08:18:14 +00:00
|
|
|
if (CHANCE16(1, 7)) construction_stage = GB(r, 0, 2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-03 14:56:07 +00:00
|
|
|
if (construction_stage == TOWN_HOUSE_COMPLETED) {
|
2004-08-09 17:04:08 +00:00
|
|
|
ChangePopulation(t, _housetype_population[house]);
|
2006-03-31 08:18:14 +00:00
|
|
|
} else {
|
|
|
|
construction_counter = GB(r, 2, 2);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-03-31 08:18:14 +00:00
|
|
|
size_flags = GB(_housetype_extra_flags[house], 2, 3);
|
|
|
|
MakeTownHouse(tile, t->index, construction_counter, construction_stage, size_flags, house);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool BuildTownHouse(Town *t, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int32 r;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// make sure it's possible
|
|
|
|
if (!EnsureNoVehicle(tile)) return false;
|
2006-04-23 13:48:16 +00:00
|
|
|
if (IsSteepSlope(GetTileSlope(tile, NULL))) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-10 07:15:58 +00:00
|
|
|
r = DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR);
|
2005-12-10 12:05:39 +00:00
|
|
|
if (CmdFailed(r)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
DoBuildTownHouse(t, tile);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void DoClearTownHouseHelper(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-16 11:24:58 +00:00
|
|
|
assert(IsTileType(tile, MP_HOUSE));
|
2004-08-09 17:04:08 +00:00
|
|
|
DoClearSquare(tile);
|
|
|
|
DeleteAnimatedTile(tile);
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void ClearTownHouse(Town *t, TileIndex tile)
|
|
|
|
{
|
2006-03-30 15:06:49 +00:00
|
|
|
uint house = GetHouseType(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
uint eflags;
|
|
|
|
|
2005-01-16 11:24:58 +00:00
|
|
|
assert(IsTileType(tile, MP_HOUSE));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// need to align the tile to point to the upper left corner of the house
|
|
|
|
if (house >= 3) { // house id 0,1,2 MUST be single tile houses, or this code breaks.
|
|
|
|
if (_housetype_extra_flags[house-1] & 0x04) {
|
|
|
|
house--;
|
2005-06-25 16:44:57 +00:00
|
|
|
tile += TileDiffXY(-1, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (_housetype_extra_flags[house-1] & 0x18) {
|
|
|
|
house--;
|
2005-06-25 16:44:57 +00:00
|
|
|
tile += TileDiffXY(0, -1);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (_housetype_extra_flags[house-2] & 0x10) {
|
|
|
|
house-=2;
|
2005-06-25 16:44:57 +00:00
|
|
|
tile += TileDiffXY(-1, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (_housetype_extra_flags[house-3] & 0x10) {
|
|
|
|
house-=3;
|
2005-06-25 16:44:57 +00:00
|
|
|
tile += TileDiffXY(-1, -1);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-04-03 14:56:07 +00:00
|
|
|
// Remove population from the town if the house is finished.
|
|
|
|
if (GetHouseBuildingStage(tile) == TOWN_HOUSE_COMPLETED) {
|
2004-08-09 17:04:08 +00:00
|
|
|
ChangePopulation(t, -_housetype_population[house]);
|
|
|
|
}
|
|
|
|
|
|
|
|
t->num_houses--;
|
|
|
|
|
|
|
|
// Clear flags for houses that only may exist once/town.
|
2004-11-22 18:42:03 +00:00
|
|
|
switch (house) {
|
|
|
|
case HOUSE_TEMP_CHURCH:
|
|
|
|
case HOUSE_ARCT_CHURCH:
|
|
|
|
case HOUSE_SNOW_CHURCH:
|
|
|
|
case HOUSE_TROP_CHURCH:
|
|
|
|
case HOUSE_TOY_CHURCH:
|
2006-04-03 14:56:07 +00:00
|
|
|
CLRBIT(t->flags12, TOWN_HAS_CHURCH);
|
2004-11-22 18:42:03 +00:00
|
|
|
break;
|
|
|
|
case HOUSE_STADIUM:
|
|
|
|
case HOUSE_MODERN_STADIUM:
|
2006-04-03 14:56:07 +00:00
|
|
|
CLRBIT(t->flags12, TOWN_HAS_STADIUM);
|
2004-11-22 18:42:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Do the actual clearing of tiles
|
|
|
|
eflags = _housetype_extra_flags[house];
|
|
|
|
DoClearTownHouseHelper(tile);
|
2005-06-25 16:44:57 +00:00
|
|
|
if (eflags & 0x14) DoClearTownHouseHelper(tile + TileDiffXY(1, 0));
|
|
|
|
if (eflags & 0x18) DoClearTownHouseHelper(tile + TileDiffXY(0, 1));
|
|
|
|
if (eflags & 0x10) DoClearTownHouseHelper(tile + TileDiffXY(1, 1));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Rename a town (server-only).
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 town ID to rename
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2006-04-10 07:15:58 +00:00
|
|
|
int32 CmdRenameTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
StringID str;
|
2005-05-12 00:11:37 +00:00
|
|
|
Town *t;
|
|
|
|
|
2006-08-22 18:15:17 +00:00
|
|
|
if (!IsValidTownID(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
|
2005-05-12 00:11:37 +00:00
|
|
|
|
|
|
|
t = GetTown(p1);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-05-15 18:50:55 +00:00
|
|
|
str = AllocateNameUnique(_cmd_text, 4);
|
2005-05-12 00:11:37 +00:00
|
|
|
if (str == 0) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2005-05-12 00:11:37 +00:00
|
|
|
DeleteName(t->townnametype);
|
2004-08-09 17:04:08 +00:00
|
|
|
t->townnametype = str;
|
|
|
|
|
|
|
|
UpdateTownVirtCoord(t);
|
|
|
|
_town_sort_dirty = true;
|
|
|
|
UpdateAllStationVirtCoord();
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
} else {
|
|
|
|
DeleteName(str);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called from GUI
|
|
|
|
void ExpandTown(Town *t)
|
|
|
|
{
|
|
|
|
int amount, n;
|
|
|
|
|
|
|
|
_generating_world = true;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-31 18:57:24 +00:00
|
|
|
/* The more houses, the faster we grow */
|
|
|
|
amount = RandomRange(t->num_houses / 10) + 3;
|
2004-08-09 17:04:08 +00:00
|
|
|
t->num_houses += amount;
|
|
|
|
UpdateTownRadius(t);
|
|
|
|
|
2004-12-31 18:57:24 +00:00
|
|
|
n = amount * 10;
|
2004-08-09 17:04:08 +00:00
|
|
|
do GrowTown(t); while (--n);
|
|
|
|
|
|
|
|
t->num_houses -= amount;
|
|
|
|
UpdateTownRadius(t);
|
|
|
|
|
|
|
|
UpdateTownMaxPass(t);
|
|
|
|
_generating_world = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const byte _town_action_costs[8] = {
|
|
|
|
2, 4, 9, 35, 48, 53, 117, 175
|
|
|
|
};
|
|
|
|
|
2006-05-20 16:46:37 +00:00
|
|
|
static void TownActionAdvertiseSmall(Town* t)
|
|
|
|
{
|
|
|
|
ModifyStationRatingAround(t->xy, _current_player, 0x40, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TownActionAdvertiseMedium(Town* t)
|
|
|
|
{
|
|
|
|
ModifyStationRatingAround(t->xy, _current_player, 0x70, 15);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-05-20 16:46:37 +00:00
|
|
|
static void TownActionAdvertiseLarge(Town* t)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-05-20 16:46:37 +00:00
|
|
|
ModifyStationRatingAround(t->xy, _current_player, 0xA0, 20);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-05-20 16:46:37 +00:00
|
|
|
static void TownActionRoadRebuild(Town* t)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-10-22 06:39:32 +00:00
|
|
|
const Player* p;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
t->road_build_months = 6;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, t->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-06-21 16:28:17 +00:00
|
|
|
p = GetPlayer(_current_player);
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(1, p->name_1);
|
|
|
|
SetDParam(2, p->name_2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
AddNewsItem(STR_2055_TRAFFIC_CHAOS_IN_ROAD_REBUILDING,
|
2004-08-09 17:04:08 +00:00
|
|
|
NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_GENERAL, 0), t->xy, 0);
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool DoBuildStatueOfCompany(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-10-07 07:35:15 +00:00
|
|
|
PlayerID old;
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 r;
|
|
|
|
|
2006-04-23 13:48:16 +00:00
|
|
|
if (GetTileSlope(tile, NULL) != SLOPE_FLAT) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-06 08:15:30 +00:00
|
|
|
if (!IsTileType(tile, MP_HOUSE) &&
|
|
|
|
!IsTileType(tile, MP_CLEAR) &&
|
|
|
|
!IsTileType(tile, MP_TREES)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
2006-02-06 08:15:30 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
old = _current_player;
|
|
|
|
_current_player = OWNER_NONE;
|
2006-04-10 07:15:58 +00:00
|
|
|
r = DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
_current_player = old;
|
|
|
|
|
2005-11-15 10:50:43 +00:00
|
|
|
if (CmdFailed(r)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-03-23 20:47:56 +00:00
|
|
|
MakeStatue(tile, _current_player);
|
|
|
|
MarkTileDirtyByTile(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-11-17 23:01:58 +00:00
|
|
|
/**
|
|
|
|
* Search callback function for TownActionBuildStatue
|
|
|
|
* @param data that is passed by the caller. In this case, nothing
|
|
|
|
* @result of the test
|
|
|
|
*/
|
|
|
|
static bool SearchTileForStatue(TileIndex tile, uint32 data)
|
|
|
|
{
|
|
|
|
return DoBuildStatueOfCompany(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a 9x9 tiles circular search from the center of the town
|
|
|
|
* in order to find a free tile to place a statue
|
|
|
|
* @param t town to search in
|
|
|
|
*/
|
2006-05-20 16:46:37 +00:00
|
|
|
static void TownActionBuildStatue(Town* t)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile = t->xy;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-17 23:01:58 +00:00
|
|
|
if (CircularTileSearch(tile, 9, SearchTileForStatue, 0))
|
|
|
|
SETBIT(t->statues, _current_player); ///< Once found and built, "inform" the Town
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-05-20 16:46:37 +00:00
|
|
|
static void TownActionFundBuildings(Town* t)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-04-03 14:56:07 +00:00
|
|
|
// Build next tick
|
2004-08-09 17:04:08 +00:00
|
|
|
t->grow_counter = 1;
|
2006-04-03 14:56:07 +00:00
|
|
|
// If we were not already growing
|
|
|
|
SETBIT(t->flags12, TOWN_IS_FUNDED);
|
|
|
|
// And grow for 3 months
|
2004-08-09 17:04:08 +00:00
|
|
|
t->fund_buildings_months = 3;
|
|
|
|
}
|
|
|
|
|
2006-05-20 16:46:37 +00:00
|
|
|
static void TownActionBuyRights(Town* t)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2004-08-23 21:04:39 +00:00
|
|
|
t->exclusive_counter = 12;
|
|
|
|
t->exclusivity = _current_player;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
ModifyStationRatingAround(t->xy, _current_player, 130, 17);
|
|
|
|
}
|
|
|
|
|
2006-05-20 16:46:37 +00:00
|
|
|
static void TownActionBribe(Town* t)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
if (!RandomRange(15)) {
|
|
|
|
Station *st;
|
|
|
|
|
|
|
|
// set as unwanted for 6 months
|
|
|
|
t->unwanted[_current_player] = 6;
|
|
|
|
|
|
|
|
// set all close by station ratings to 0
|
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
if (st->town == t && st->owner == _current_player) {
|
2005-10-23 13:04:44 +00:00
|
|
|
uint i;
|
|
|
|
|
|
|
|
for (i = 0; i != NUM_CARGO; i++) st->goods[i].rating = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// only show errormessage to the executing player. All errors are handled command.c
|
|
|
|
// but this is special, because it can only 'fail' on a DC_EXEC
|
2005-09-14 18:03:38 +00:00
|
|
|
if (IsLocalPlayer()) ShowErrorMessage(STR_BRIBE_FAILED_2, STR_BRIBE_FAILED, 0, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-28 18:53:03 +00:00
|
|
|
/* decrease by a lot!
|
|
|
|
* ChangeTownRating is only for stuff in demolishing. Bribe failure should
|
|
|
|
* be independent of any cheat settings
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
2005-10-23 13:04:44 +00:00
|
|
|
if (t->ratings[_current_player] > RATING_BRIBE_DOWN_TO) {
|
2005-01-14 09:20:12 +00:00
|
|
|
t->ratings[_current_player] = RATING_BRIBE_DOWN_TO;
|
2005-10-23 13:04:44 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2005-01-14 09:20:12 +00:00
|
|
|
ChangeTownRating(t, RATING_BRIBE_UP_STEP, RATING_BRIBE_MAXIMUM);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-20 16:46:37 +00:00
|
|
|
typedef void TownActionProc(Town* t);
|
2004-08-09 17:04:08 +00:00
|
|
|
static TownActionProc * const _town_action_proc[] = {
|
2006-05-20 16:46:37 +00:00
|
|
|
TownActionAdvertiseSmall,
|
|
|
|
TownActionAdvertiseMedium,
|
|
|
|
TownActionAdvertiseLarge,
|
2004-08-09 17:04:08 +00:00
|
|
|
TownActionRoadRebuild,
|
|
|
|
TownActionBuildStatue,
|
|
|
|
TownActionFundBuildings,
|
|
|
|
TownActionBuyRights,
|
|
|
|
TownActionBribe
|
|
|
|
};
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
extern uint GetMaskOfTownActions(int *nump, PlayerID pid, const Town *t);
|
|
|
|
|
|
|
|
/** Do a town action.
|
|
|
|
* This performs an action such as advertising, building a statue, funding buildings,
|
|
|
|
* but also bribing the town-council
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 town to do the action at
|
|
|
|
* @param p2 action to perform, @see _town_action_proc for the list of available actions
|
|
|
|
*/
|
2006-04-10 07:15:58 +00:00
|
|
|
int32 CmdDoTownAction(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int32 cost;
|
2005-05-12 00:11:37 +00:00
|
|
|
Town *t;
|
|
|
|
|
2006-08-22 18:15:17 +00:00
|
|
|
if (!IsValidTownID(p1) || p2 > lengthof(_town_action_proc)) return CMD_ERROR;
|
2005-05-12 00:11:37 +00:00
|
|
|
|
|
|
|
t = GetTown(p1);
|
|
|
|
|
|
|
|
if (!HASBIT(GetMaskOfTownActions(NULL, _current_player, t), p2)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_OTHER);
|
|
|
|
|
|
|
|
cost = (_price.build_industry >> 8) * _town_action_costs[p2];
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2006-05-20 16:46:37 +00:00
|
|
|
_town_action_proc[p2](t);
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_TOWN_AUTHORITY, p1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdateTownGrowRate(Town *t)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
Station *st;
|
|
|
|
byte m;
|
|
|
|
Player *p;
|
|
|
|
|
|
|
|
// Reset player ratings if they're low
|
|
|
|
FOR_ALL_PLAYERS(p) {
|
|
|
|
if (p->is_active && t->ratings[p->index] <= 200) {
|
|
|
|
t->ratings[p->index] += 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
FOR_ALL_STATIONS(st) {
|
2005-01-31 07:23:15 +00:00
|
|
|
if (DistanceSquare(st->xy, t->xy) <= t->radius[0]) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (st->time_since_load <= 20 || st->time_since_unload <= 20) {
|
|
|
|
n++;
|
2006-10-14 22:31:18 +00:00
|
|
|
if (IsValidPlayer(st->owner) && t->ratings[st->owner] <= 1000-12)
|
2004-08-09 17:04:08 +00:00
|
|
|
t->ratings[st->owner] += 12;
|
|
|
|
} else {
|
2006-10-14 22:31:18 +00:00
|
|
|
if (IsValidPlayer(st->owner) && t->ratings[st->owner] >= -1000+15)
|
2004-08-09 17:04:08 +00:00
|
|
|
t->ratings[st->owner] -= 15;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-03 14:56:07 +00:00
|
|
|
CLRBIT(t->flags12, TOWN_IS_FUNDED);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (t->fund_buildings_months != 0) {
|
2004-11-20 11:57:45 +00:00
|
|
|
static const byte _grow_count_values[6] = {
|
|
|
|
60, 60, 60, 50, 40, 30
|
|
|
|
};
|
|
|
|
m = _grow_count_values[min(n, 5)];
|
2004-08-09 17:04:08 +00:00
|
|
|
t->fund_buildings_months--;
|
|
|
|
} else if (n == 0) {
|
|
|
|
m = 160;
|
|
|
|
if (!CHANCE16(1, 12))
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
static const byte _grow_count_values[5] = {
|
|
|
|
210, 150, 110, 80, 50
|
|
|
|
};
|
|
|
|
m = _grow_count_values[min(n, 5) - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_opt.landscape == LT_HILLY) {
|
2006-08-28 18:53:03 +00:00
|
|
|
if (TilePixelHeight(t->xy) >= _opt.snow_line && t->act_food == 0 && t->population > 90)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
} else if (_opt.landscape == LT_DESERT) {
|
2006-08-28 18:53:03 +00:00
|
|
|
if (GetTropicZone(t->xy) == TROPICZONE_DESERT && (t->act_food==0 || t->act_water==0) && t->population > 60)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-08-28 18:53:03 +00:00
|
|
|
t->growth_rate = m / (t->num_houses / 50 + 1);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (m <= t->grow_counter)
|
|
|
|
t->grow_counter = m;
|
|
|
|
|
2006-04-03 14:56:07 +00:00
|
|
|
SETBIT(t->flags12, TOWN_IS_FUNDED);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdateTownAmounts(Town *t)
|
|
|
|
{
|
|
|
|
// Using +1 here to prevent overflow and division by zero
|
|
|
|
t->pct_pass_transported = t->new_act_pass * 256 / (t->new_max_pass + 1);
|
|
|
|
|
|
|
|
t->max_pass = t->new_max_pass; t->new_max_pass = 0;
|
|
|
|
t->act_pass = t->new_act_pass; t->new_act_pass = 0;
|
|
|
|
t->act_food = t->new_act_food; t->new_act_food = 0;
|
2004-08-10 14:42:52 +00:00
|
|
|
t->act_water = t->new_act_water; t->new_act_water = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Using +1 here to prevent overflow and division by zero
|
|
|
|
t->pct_mail_transported = t->new_act_mail * 256 / (t->new_max_mail + 1);
|
|
|
|
t->max_mail = t->new_max_mail; t->new_max_mail = 0;
|
|
|
|
t->act_mail = t->new_act_mail; t->new_act_mail = 0;
|
|
|
|
|
|
|
|
InvalidateWindow(WC_TOWN_VIEW, t->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdateTownUnwanted(Town *t)
|
|
|
|
{
|
2006-06-10 08:37:41 +00:00
|
|
|
const Player* p;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
FOR_ALL_PLAYERS(p) {
|
2006-06-10 08:37:41 +00:00
|
|
|
if (t->unwanted[p->index] > 0) t->unwanted[p->index]--;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
bool CheckIfAuthorityAllows(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Town *t;
|
|
|
|
|
2006-10-14 22:31:18 +00:00
|
|
|
if (!IsValidPlayer(_current_player)) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
t = ClosestTownFromTile(tile, _patches.dist_local_authority);
|
2006-06-10 08:37:41 +00:00
|
|
|
if (t == NULL) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-06-10 08:37:41 +00:00
|
|
|
if (t->ratings[_current_player] > -200) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
_error_message = STR_2009_LOCAL_AUTHORITY_REFUSES;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, t->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-08 18:31:54 +00:00
|
|
|
Town* CalcClosestTownFromTile(TileIndex tile, uint threshold)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Town *t;
|
|
|
|
uint dist, best = threshold;
|
|
|
|
Town *best_town = NULL;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
FOR_ALL_TOWNS(t) {
|
2006-08-22 15:33:35 +00:00
|
|
|
dist = DistanceManhattan(tile, t->xy);
|
|
|
|
if (dist < best) {
|
|
|
|
best = dist;
|
|
|
|
best_town = t;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return best_town;
|
|
|
|
}
|
|
|
|
|
2006-06-08 18:31:54 +00:00
|
|
|
|
|
|
|
Town *ClosestTownFromTile(TileIndex tile, uint threshold)
|
|
|
|
{
|
|
|
|
if (IsTileType(tile, MP_HOUSE) || (
|
|
|
|
IsTileType(tile, MP_STREET) &&
|
|
|
|
(IsLevelCrossing(tile) ? GetCrossingRoadOwner(tile) : GetTileOwner(tile)) == OWNER_TOWN
|
|
|
|
)) {
|
|
|
|
return GetTownByTile(tile);
|
|
|
|
} else {
|
|
|
|
return CalcClosestTownFromTile(tile, threshold);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
void ChangeTownRating(Town *t, int add, int max)
|
|
|
|
{
|
|
|
|
int rating;
|
|
|
|
|
2006-08-28 18:53:03 +00:00
|
|
|
// if magic_bulldozer cheat is active, town doesn't penaltize for removing stuff
|
2005-11-14 19:48:04 +00:00
|
|
|
if (t == NULL ||
|
2006-10-14 22:31:18 +00:00
|
|
|
!IsValidPlayer(_current_player) ||
|
2005-11-14 19:48:04 +00:00
|
|
|
(_cheats.magic_bulldozer.value && add < 0)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SETBIT(t->have_ratings, _current_player);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
rating = t->ratings[_current_player];
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (add < 0) {
|
|
|
|
if (rating > max) {
|
|
|
|
rating += add;
|
|
|
|
if (rating < max) rating = max;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (rating < max) {
|
|
|
|
rating += add;
|
|
|
|
if (rating > max) rating = max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t->ratings[_current_player] = rating;
|
|
|
|
}
|
|
|
|
|
2006-08-28 18:53:03 +00:00
|
|
|
/* penalty for removing town-owned stuff */
|
2004-08-09 17:04:08 +00:00
|
|
|
static const int _default_rating_settings [3][3] = {
|
|
|
|
// ROAD_REMOVE, TUNNELBRIDGE_REMOVE, INDUSTRY_REMOVE
|
2006-08-22 14:38:37 +00:00
|
|
|
{ 0, 128, 384}, // Permissive
|
|
|
|
{ 48, 192, 480}, // Neutral
|
|
|
|
{ 96, 384, 768}, // Hostile
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2006-02-02 07:15:46 +00:00
|
|
|
bool CheckforTownRating(uint32 flags, Town *t, byte type)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int modemod;
|
|
|
|
|
2006-08-28 18:53:03 +00:00
|
|
|
// if magic_bulldozer cheat is active, town doesn't restrict your destructive actions
|
2006-10-14 22:31:18 +00:00
|
|
|
if (t == NULL || !IsValidPlayer(_current_player) || _cheats.magic_bulldozer.value)
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
|
2006-08-28 18:53:03 +00:00
|
|
|
/* check if you're allowed to remove the street/bridge/tunnel/industry
|
|
|
|
* owned by a town no removal if rating is lower than ... depends now on
|
|
|
|
* difficulty setting. Minimum town rating selected by difficulty level
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
2005-03-12 21:21:47 +00:00
|
|
|
modemod = _default_rating_settings[_opt.diff.town_council_tolerance][type];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (t->ratings[_current_player] < 16 + modemod && !(flags & DC_NO_TOWN_RATING)) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, t->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
_error_message = STR_2009_LOCAL_AUTHORITY_REFUSES;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void TownsMonthlyLoop(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Town *t;
|
|
|
|
|
2006-08-22 15:33:35 +00:00
|
|
|
FOR_ALL_TOWNS(t) {
|
2006-06-10 08:37:41 +00:00
|
|
|
if (t->road_build_months != 0) t->road_build_months--;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-08-23 21:04:39 +00:00
|
|
|
if (t->exclusive_counter != 0)
|
2006-06-10 08:37:41 +00:00
|
|
|
if (--t->exclusive_counter == 0) t->exclusivity = (byte)-1;
|
2004-08-23 21:04:39 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateTownGrowRate(t);
|
|
|
|
UpdateTownAmounts(t);
|
|
|
|
UpdateTownUnwanted(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void InitializeTowns(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Subsidy *s;
|
2005-01-06 22:31:58 +00:00
|
|
|
|
2005-02-01 18:32:01 +00:00
|
|
|
/* Clean the town pool and create 1 block in it */
|
2006-10-28 11:55:29 +00:00
|
|
|
CleanPool(&_Town_pool);
|
|
|
|
AddBlockToPool(&_Town_pool);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
memset(_subsidies, 0, sizeof(_subsidies));
|
|
|
|
for (s=_subsidies; s != endof(_subsidies); s++)
|
2005-09-28 19:35:36 +00:00
|
|
|
s->cargo_type = CT_INVALID;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
_cur_town_ctr = 0;
|
2005-03-20 00:32:26 +00:00
|
|
|
_cur_town_iter = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
_total_towns = 0;
|
2005-02-01 18:32:01 +00:00
|
|
|
_town_sort_dirty = true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const TileTypeProcs _tile_type_town_procs = {
|
2006-08-22 14:38:37 +00:00
|
|
|
DrawTile_Town, /* draw_tile_proc */
|
|
|
|
GetSlopeZ_Town, /* get_slope_z_proc */
|
|
|
|
ClearTile_Town, /* clear_tile_proc */
|
|
|
|
GetAcceptedCargo_Town, /* get_accepted_cargo_proc */
|
|
|
|
GetTileDesc_Town, /* get_tile_desc_proc */
|
|
|
|
GetTileTrackStatus_Town, /* get_tile_track_status_proc */
|
|
|
|
ClickTile_Town, /* click_tile_proc */
|
|
|
|
AnimateTile_Town, /* animate_tile_proc */
|
|
|
|
TileLoop_Town, /* tile_loop_clear */
|
|
|
|
ChangeTileOwner_Town, /* change_tile_owner_clear */
|
|
|
|
NULL, /* get_produced_cargo_proc */
|
|
|
|
NULL, /* vehicle_enter_tile_proc */
|
|
|
|
GetSlopeTileh_Town, /* get_slope_tileh_proc */
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Save and load of towns.
|
2005-05-30 22:16:05 +00:00
|
|
|
static const SaveLoad _town_desc[] = {
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_CONDVAR(Town, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Town, xy, SLE_UINT32, 6, SL_MAX_VERSION),
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_CONDVAR(Town, population, SLE_FILE_U16 | SLE_VAR_U32, 0, 2),
|
|
|
|
SLE_CONDVAR(Town, population, SLE_UINT32, 3, SL_MAX_VERSION),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_VAR(Town, num_houses, SLE_UINT16),
|
|
|
|
SLE_VAR(Town, townnametype, SLE_UINT16),
|
|
|
|
SLE_VAR(Town, townnameparts, SLE_UINT32),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_VAR(Town, flags12, SLE_UINT8),
|
|
|
|
SLE_VAR(Town, statues, SLE_UINT8),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// sort_index_obsolete was stored here in savegame format 0 - 1
|
2006-03-16 00:20:33 +00:00
|
|
|
SLE_CONDNULL(1, 0, 1),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_VAR(Town, have_ratings, SLE_UINT8),
|
|
|
|
SLE_ARR(Town, ratings, SLE_INT16, 8),
|
2004-08-09 17:04:08 +00:00
|
|
|
// failed bribe attempts are stored since savegame format 4
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_CONDARR(Town, unwanted, SLE_INT8, 8, 4,SL_MAX_VERSION),
|
|
|
|
|
|
|
|
SLE_CONDVAR(Town, max_pass, SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
|
|
|
|
SLE_CONDVAR(Town, max_mail, SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
|
|
|
|
SLE_CONDVAR(Town, new_max_pass, SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
|
|
|
|
SLE_CONDVAR(Town, new_max_mail, SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
|
|
|
|
SLE_CONDVAR(Town, act_pass, SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
|
|
|
|
SLE_CONDVAR(Town, act_mail, SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
|
|
|
|
SLE_CONDVAR(Town, new_act_pass, SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
|
|
|
|
SLE_CONDVAR(Town, new_act_mail, SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
|
|
|
|
|
|
|
|
SLE_CONDVAR(Town, max_pass, SLE_UINT32, 9, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Town, max_mail, SLE_UINT32, 9, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Town, new_max_pass, SLE_UINT32, 9, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Town, new_max_mail, SLE_UINT32, 9, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Town, act_pass, SLE_UINT32, 9, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Town, act_mail, SLE_UINT32, 9, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Town, new_act_pass, SLE_UINT32, 9, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Town, new_act_mail, SLE_UINT32, 9, SL_MAX_VERSION),
|
|
|
|
|
|
|
|
SLE_VAR(Town, pct_pass_transported, SLE_UINT8),
|
|
|
|
SLE_VAR(Town, pct_mail_transported, SLE_UINT8),
|
|
|
|
|
|
|
|
SLE_VAR(Town, act_food, SLE_UINT16),
|
|
|
|
SLE_VAR(Town, act_water, SLE_UINT16),
|
|
|
|
SLE_VAR(Town, new_act_food, SLE_UINT16),
|
|
|
|
SLE_VAR(Town, new_act_water, SLE_UINT16),
|
|
|
|
|
|
|
|
SLE_VAR(Town, time_until_rebuild, SLE_UINT8),
|
|
|
|
SLE_VAR(Town, grow_counter, SLE_UINT8),
|
|
|
|
SLE_VAR(Town, growth_rate, SLE_UINT8),
|
|
|
|
SLE_VAR(Town, fund_buildings_months, SLE_UINT8),
|
|
|
|
SLE_VAR(Town, road_build_months, SLE_UINT8),
|
|
|
|
|
|
|
|
SLE_VAR(Town, exclusivity, SLE_UINT8),
|
|
|
|
SLE_VAR(Town, exclusive_counter, SLE_UINT8),
|
2004-08-23 21:04:39 +00:00
|
|
|
// reserve extra space in savegame here. (currently 30 bytes)
|
2006-03-16 00:20:33 +00:00
|
|
|
SLE_CONDNULL(30, 2, SL_MAX_VERSION),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void Save_TOWN(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Town *t;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
FOR_ALL_TOWNS(t) {
|
2006-08-22 15:33:35 +00:00
|
|
|
SlSetArrayIndex(t->index);
|
|
|
|
SlObject(t, _town_desc);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void Load_TOWN(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int index;
|
2005-02-01 18:32:01 +00:00
|
|
|
|
2005-02-01 18:46:49 +00:00
|
|
|
_total_towns = 0;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
while ((index = SlIterateArray()) != -1) {
|
2005-02-01 18:32:01 +00:00
|
|
|
Town *t;
|
2005-01-06 22:31:58 +00:00
|
|
|
|
2006-10-28 11:55:29 +00:00
|
|
|
if (!AddBlockIfNeeded(&_Town_pool, index))
|
2005-02-01 18:32:01 +00:00
|
|
|
error("Towns: failed loading savegame: too many towns");
|
|
|
|
|
|
|
|
t = GetTown(index);
|
2004-08-09 17:04:08 +00:00
|
|
|
SlObject(t, _town_desc);
|
2005-02-01 18:32:01 +00:00
|
|
|
|
2006-08-22 21:17:19 +00:00
|
|
|
if ((uint)index >= _total_towns) _total_towns = index + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-02-01 18:32:01 +00:00
|
|
|
|
|
|
|
/* This is to ensure all pointers are within the limits of
|
|
|
|
* the size of the TownPool */
|
2006-08-22 20:41:26 +00:00
|
|
|
if (_cur_town_ctr >= GetTownArraySize())
|
2005-02-01 18:32:01 +00:00
|
|
|
_cur_town_ctr = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void AfterLoadTown(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Town *t;
|
|
|
|
FOR_ALL_TOWNS(t) {
|
2006-08-22 15:33:35 +00:00
|
|
|
UpdateTownRadius(t);
|
|
|
|
UpdateTownVirtCoord(t);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
_town_sort_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const ChunkHandler _town_chunk_handlers[] = {
|
|
|
|
{ 'CITY', Save_TOWN, Load_TOWN, CH_ARRAY | CH_LAST},
|
|
|
|
};
|