Changeset - r13097:c59fc0ed2eeb
[Not reviewed]
master
0 5 0
smatz - 15 years ago 2009-09-21 20:22:56
smatz@openttd.org
(svn r17608) -Codechange: use CmdFoundTown when founding 'Random town' too
5 files changed with 69 insertions and 42 deletions:
0 comments (0 inline, 0 general)
src/callback_table.cpp
Show inline comments
 
@@ -34,13 +34,12 @@ CommandCallback CcBuildCanal;
 
CommandCallback CcCloneVehicle;
 

	
 
/* main_gui.cpp */
 
CommandCallback CcPlaySound10;
 
CommandCallback CcPlaceSign;
 
CommandCallback CcTerraform;
 
CommandCallback CcFoundTown;
 
CommandCallback CcGiveMoney;
 

	
 
/* rail_gui.cpp */
 
CommandCallback CcPlaySound1E;
 
CommandCallback CcRailDepot;
 
CommandCallback CcStation;
 
@@ -58,12 +57,16 @@ CommandCallback CcBuildRoadVeh;
 
CommandCallback CcBuildShip;
 

	
 
/* train_gui.cpp */
 
CommandCallback CcBuildWagon;
 
CommandCallback CcBuildLoco;
 

	
 
/* town_gui.cpp */
 
CommandCallback CcFoundTown;
 
CommandCallback CcFoundRandomTown;
 

	
 
/* group_gui.cpp */
 
CommandCallback CcCreateGroup;
 

	
 
/* ai/ai_core.cpp */
 
CommandCallback CcAI;
 

	
 
@@ -90,9 +93,10 @@ CommandCallback * const _callback_table[
 
	/* 0x13 */ CcStation,
 
	/* 0x14 */ CcTerraform,
 
	/* 0x15 */ CcAI,
 
	/* 0x16 */ CcCloneVehicle,
 
	/* 0x17 */ CcGiveMoney,
 
	/* 0x18 */ CcCreateGroup,
 
	/* 0x19 */ CcFoundRandomTown,
 
};
 

	
 
const int _callback_table_count = lengthof(_callback_table);
src/command.cpp
Show inline comments
 
@@ -293,13 +293,13 @@ static const Command _command_proc_table
 
	{CmdPause,                             CMD_SERVER}, // CMD_PAUSE
 

	
 
	{CmdBuyShareInCompany,                          0}, // CMD_BUY_SHARE_IN_COMPANY
 
	{CmdSellShareInCompany,                         0}, // CMD_SELL_SHARE_IN_COMPANY
 
	{CmdBuyCompany,                                 0}, // CMD_BUY_COMANY
 

	
 
	{CmdFoundTown,                        CMD_OFFLINE}, // CMD_FOUND_TOWN
 
	{CmdFoundTown,          CMD_NO_TEST | CMD_OFFLINE}, // CMD_FOUND_TOWN
 
	{CmdRenameTown,                        CMD_SERVER}, // CMD_RENAME_TOWN
 
	{CmdDoTownAction,                               0}, // CMD_DO_TOWN_ACTION
 

	
 
	{CmdSellShip,                                   0}, // CMD_SELL_SHIP
 
	{CmdBuildShip,                                  0}, // CMD_BUILD_SHIP
 
	{CmdSendShipToDepot,                            0}, // CMD_SEND_SHIP_TO_DEPOT
src/town.h
Show inline comments
 
@@ -160,13 +160,12 @@ struct Town : TownPool::PoolItem<&_town_
 
uint32 GetWorldPopulation();
 

	
 
void UpdateAllTownVirtCoords();
 
void InitializeTown();
 
void ShowTownViewWindow(TownID town);
 
void ExpandTown(Town *t);
 
Town *CreateRandomTown(uint attempts, TownSize size, bool city, TownLayout layout);
 

	
 
enum TownRatingCheckType {
 
	ROAD_REMOVE         = 0,
 
	TUNNELBRIDGE_REMOVE = 1,
 
	TOWN_RATING_CHECK_TYPE_COUNT,
 
};
 
@@ -236,12 +235,13 @@ enum TownActions {
 
	TACT_FUNDS            = TACT_BUY_RIGHTS | TACT_BRIBE,                                        ///< All possible funding actions.
 
	TACT_ALL              = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS,                     ///< All possible actions.
 
};
 
DECLARE_ENUM_AS_BIT_SET(TownActions);
 

	
 
extern const byte _town_action_costs[TACT_COUNT];
 
extern TownID _new_town_id;
 

	
 
/**
 
 * Calculate a hash value from a tile position
 
 *
 
 * @param x The X coordinate
 
 * @param y The Y coordinate
src/town_cmd.cpp
Show inline comments
 
@@ -53,12 +53,13 @@
 

	
 
#include "table/strings.h"
 
#include "table/town_land.h"
 

	
 
Town *_cleared_town;
 
int _cleared_town_rating;
 
TownID _new_town_id;
 

	
 
uint32 _cur_town_ctr;     ///< iterator through all towns in OnTick_Town
 
uint32 _cur_town_iter;    ///< frequency iterator at the same place
 

	
 
/* Initialize the town-pool */
 
TownPool _town_pool("Town");
 
@@ -171,12 +172,13 @@ enum TownGrowthResult {
 
	GROWTH_SUCCEED         = -1,
 
	GROWTH_SEARCH_STOPPED  =  0
 
//	GROWTH_SEARCH_RUNNING >=  1
 
};
 

	
 
static bool BuildTownHouse(Town *t, TileIndex tile);
 
static Town *CreateRandomTown(uint attempts, uint32 townnameparts, TownSize size, bool city, TownLayout layout);
 

	
 
static void TownDrawHouseLift(const TileInfo *ti)
 
{
 
	AddChildSpriteScreen(SPR_LIFT, PAL_NONE, 14, 60 - GetLiftPosition(ti->tile));
 
}
 

	
 
@@ -1578,23 +1580,24 @@ static CommandCost TownCanBePlacedHere(T
 

	
 
	/* Can only build on clear flat areas, possibly with trees. */
 
	if ((!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES)) || GetTileSlope(tile, NULL) != SLOPE_FLAT) {
 
		return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
 
	}
 

	
 
	return CommandCost();
 
	return CommandCost(EXPENSES_OTHER);
 
}
 

	
 
/** 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 :)
 
 * @param tile coordinates where town is built
 
 * @param flags type of operation
 
 * @param p1  0..1 size of the town (@see TownSize)
 
 *               2 true iff it should be a city
 
 *            3..5 town road layout (@see TownLayout)
 
 *               6 use random location (randomize \c tile )
 
 * @param p2 town name parts
 
 * @param text unused
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdFoundTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
@@ -1602,35 +1605,48 @@ CommandCost CmdFoundTown(TileIndex tile,
 
	if (_game_mode != GM_EDITOR) return CMD_ERROR;
 

	
 
	TownSize size = (TownSize)GB(p1, 0, 2);
 
	bool city = HasBit(p1, 2);
 
	TownLayout layout = (TownLayout)GB(p1, 3, 3);
 
	TownNameParams par(_settings_game.game_creation.town_name);
 
	bool random = HasBit(p1, 6);
 
	uint32 townnameparts = p2;
 

	
 
	if (size > TS_RANDOM) return CMD_ERROR;
 
	if (layout > TL_RANDOM) return CMD_ERROR;
 

	
 
	if (!VerifyTownName(townnameparts, &par)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
 

	
 
	CommandCost cost = TownCanBePlacedHere(tile);
 
	if (CmdFailed(cost)) return cost;
 

	
 
	/* Allocate town struct */
 
	if (!Town::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_TOWNS);
 

	
 
	CommandCost cost(EXPENSES_OTHER);
 
	if (!random) {
 
		cost = TownCanBePlacedHere(tile);
 
		if (CmdFailed(cost)) return cost;
 
	}
 

	
 
	/* Create the town */
 
	if (flags & DC_EXEC) {
 
		Town *t = new Town(tile);
 
		_generating_world = true;
 
		UpdateNearestTownForRoadTiles(true);
 
		if (random) {
 
			const Town *t = CreateRandomTown(20, townnameparts, size, city, layout);
 
			if (t == NULL) {
 
				cost = CommandCost(STR_ERROR_NO_SPACE_FOR_TOWN);
 
			} else {
 
				_new_town_id = t->index;
 
			}
 
		} else {
 
			Town *t = new Town(tile);
 
		DoCreateTown(t, tile, townnameparts, size, city, layout);
 
		}
 
		UpdateNearestTownForRoadTiles(false);
 
		_generating_world = false;
 
	}
 
	return CommandCost();
 
	return cost;
 
}
 

	
 
/**
 
 * Towns must all be placed on the same grid or when they eventually
 
 * interpenetrate their road networks will not mesh nicely; this
 
 * function adjusts a tile so that it aligns properly.
 
@@ -1741,13 +1757,13 @@ static TileIndex FindNearestGoodCoastalT
 
	}
 

	
 
	/* if we get here just give up */
 
	return INVALID_TILE;
 
}
 

	
 
Town *CreateRandomTown(uint attempts, TownSize size, bool city, TownLayout layout)
 
static Town *CreateRandomTown(uint attempts, uint32 townnameparts, TownSize size, bool city, TownLayout layout)
 
{
 
	if (!Town::CanAllocateItem()) return NULL;
 

	
 
	do {
 
		/* Generate a tile index not too close from the edge */
 
		TileIndex tile = AlignTileToGrid(RandomTile(), layout);
 
@@ -1759,16 +1775,12 @@ Town *CreateRandomTown(uint attempts, To
 
			if (tile == INVALID_TILE) continue;
 
		}
 

	
 
		/* Make sure town can be placed here */
 
		if (CmdFailed(TownCanBePlacedHere(tile))) continue;
 

	
 
		uint32 townnameparts;
 
		/* Get a unique name for the town. */
 
		if (!GenerateTownName(&townnameparts)) break;
 

	
 
		/* Allocate a town struct */
 
		Town *t = new Town(tile);
 

	
 
		DoCreateTown(t, tile, townnameparts, size, city, layout);
 

	
 
		/* if the population is still 0 at the point, then the
 
@@ -1790,38 +1802,45 @@ static const byte _num_initial_towns[4] 
 
 */
 
bool GenerateTowns(TownLayout layout)
 
{
 
	uint num = 0;
 
	uint difficulty = _settings_game.difficulty.number_towns;
 
	uint n = (difficulty == (uint)CUSTOM_TOWN_NUMBER_DIFFICULTY) ? _settings_game.game_creation.custom_town_number : ScaleByMapSize(_num_initial_towns[difficulty] + (Random() & 7));
 
	uint32 townnameparts;
 

	
 
	SetGeneratingWorldProgress(GWP_TOWN, n);
 

	
 
	/* First attempt will be made at creating the suggested number of towns.
 
	 * Note that this is really a suggested value, not a required one.
 
	 * We would not like the system to lock up just because the user wanted 100 cities on a 64*64 map, would we? */
 
	do {
 
		bool city = (_settings_game.economy.larger_towns != 0 && Chance16(1, _settings_game.economy.larger_towns));
 
		IncreaseGeneratingWorldProgress(GWP_TOWN);
 
		/* Get a unique name for the town. */
 
		if (!GenerateTownName(&townnameparts)) continue;
 
		/* try 20 times to create a random-sized town for the first loop. */
 
		if (CreateRandomTown(20, TS_RANDOM, city, layout) != NULL) num++; // if creation successfull, raise a flag
 
		if (CreateRandomTown(20, townnameparts, TS_RANDOM, city, layout) != NULL) num++; // if creation successfull, raise a flag
 
	} while (--n);
 

	
 
	if (num != 0) return true;
 

	
 
	/* If num is still zero at this point, it means that not a single town has been created.
 
	 * So give it a last try, but now more aggressive */
 
	if (num == 0 && CreateRandomTown(10000, TS_RANDOM, _settings_game.economy.larger_towns != 0, layout) == NULL) {
 
		if (Town::GetNumItems() == 0) {
 
			if (_game_mode != GM_EDITOR) {
 
	if (GenerateTownName(&townnameparts) &&
 
			CreateRandomTown(10000, townnameparts, TS_RANDOM, _settings_game.economy.larger_towns != 0, layout) != NULL) {
 
		return true;
 
	}
 

	
 
	/* If there are no towns at all and we are generating new game, bail out */
 
	if (Town::GetNumItems() == 0 && _game_mode != GM_EDITOR) {
 
				extern StringID _switch_mode_errorstr;
 
				_switch_mode_errorstr = STR_ERROR_COULD_NOT_CREATE_TOWN;
 
			}
 
		}
 

	
 
		return false;  // we are still without a town? we failed, simply
 
	}
 
	return true;
 
}
 

	
 

	
 
/** Returns the bit corresponding to the town zone of the specified tile
 
 * @param t Town on which town zone is to be found
 
 * @param tile TileIndex where town zone needs to be found
 
 * @return the bit position of the given zone, as defined in HouseZones
src/town_gui.cpp
Show inline comments
 
@@ -845,12 +845,21 @@ void CcFoundTown(bool success, TileIndex
 
	if (success) {
 
		SndPlayTileFx(SND_1F_SPLAT, tile);
 
		if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
 
	}
 
}
 

	
 
void CcFoundRandomTown(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) {
 
		tile = Town::Get(_new_town_id)->xy;
 
		SndPlayTileFx(SND_1F_SPLAT, tile);
 
		ScrollMainWindowToTile(tile);
 
	}
 
}
 

	
 
/** Widget numbers of town scenario editor window. */
 
enum TownScenarioEditorWidgets {
 
	TSEW_CLOSEBOX,
 
	TSEW_CAPTION,
 
	TSEW_STICKYBOX,
 
	TSEW_BACKGROUND,
 
@@ -960,38 +969,40 @@ public:
 
			this->SetWidgetLoweredState(i, i == TSEW_LAYOUT_ORIGINAL + this->town_layout);
 
		}
 

	
 
		this->SetDirty();
 
	}
 

	
 
	void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, CommandCallback cc)
 
	{
 
		uint32 townnameparts;
 
		if (!GenerateTownName(&townnameparts)) {
 
			ShowErrorMessage(STR_ERROR_TOO_MANY_TOWNS, errstr, 0, 0);
 
			return;
 
		}
 

	
 
		DoCommandP(tile, this->town_size | this->city << 2 | this->town_layout << 3 | random << 6,
 
				townnameparts, CMD_FOUND_TOWN | CMD_MSG(errstr), cc);
 
	}
 

	
 
	virtual void OnPaint()
 
	{
 
		this->DrawWidgets();
 
	}
 

	
 
	virtual void OnClick(Point pt, int widget)
 
	{
 
		switch (widget) {
 
			case TSEW_NEWTOWN:
 
				HandlePlacePushButton(this, TSEW_NEWTOWN, SPR_CURSOR_TOWN, HT_RECT, NULL);
 
				break;
 

	
 
			case TSEW_RANDOMTOWN: {
 
			case TSEW_RANDOMTOWN:
 
				this->HandleButtonClick(TSEW_RANDOMTOWN);
 
				_generating_world = true;
 
				UpdateNearestTownForRoadTiles(true);
 
				const Town *t = CreateRandomTown(20, this->town_size, this->city, this->town_layout);
 
				UpdateNearestTownForRoadTiles(false);
 
				_generating_world = false;
 

	
 
				if (t == NULL) {
 
					ShowErrorMessage(STR_ERROR_NO_SPACE_FOR_TOWN, STR_ERROR_CAN_T_GENERATE_TOWN, 0, 0);
 
				} else {
 
					ScrollMainWindowToTile(t->xy);
 
				}
 
			} break;
 
				this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN, CcFoundRandomTown);
 
				break;
 

	
 
			case TSEW_MANYRANDOMTOWNS:
 
				this->HandleButtonClick(TSEW_MANYRANDOMTOWNS);
 

	
 
				_generating_world = true;
 
				UpdateNearestTownForRoadTiles(true);
 
@@ -1027,20 +1038,13 @@ public:
 
		this->RaiseWidget(TSEW_MANYRANDOMTOWNS);
 
		this->SetDirty();
 
	}
 

	
 
	virtual void OnPlaceObject(Point pt, TileIndex tile)
 
	{
 
		uint32 townnameparts;
 
		if (!GenerateTownName(&townnameparts)) {
 
			ShowErrorMessage(STR_ERROR_TOO_MANY_TOWNS, STR_ERROR_CAN_T_FOUND_TOWN_HERE, 0, 0);
 
			return;
 
		}
 

	
 
		DoCommandP(tile, this->town_size | this->city << 2 | this->town_layout << 3, townnameparts,
 
				CMD_FOUND_TOWN | CMD_MSG(STR_ERROR_CAN_T_FOUND_TOWN_HERE), CcFoundTown);
 
		this->ExecuteFoundTownCommand(tile, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE, CcFoundTown);
 
	}
 

	
 
	virtual void OnPlaceObjectAbort()
 
	{
 
		this->RaiseButtons();
 
		this->UpdateButtons();
0 comments (0 inline, 0 general)