Changeset - r9036:e33a0264e0c3
[Not reviewed]
master
0 11 0
rubidium - 16 years ago 2008-04-23 20:56:08
rubidium@openttd.org
(svn r12855) -Codechange: do not use autoptr's for testing whether certain objects can be build, but check it directly in the pool so we do not have to call destructors in the testing phase. Stations still use the autoptr though.
11 files changed with 57 insertions and 76 deletions:
0 comments (0 inline, 0 general)
src/engine.cpp
Show inline comments
 
@@ -6,25 +6,24 @@
 
#include "openttd.h"
 
#include "debug.h"
 
#include "player_base.h"
 
#include "player_func.h"
 
#include "command_func.h"
 
#include "news_func.h"
 
#include "saveload.h"
 
#include "variables.h"
 
#include "train.h"
 
#include "aircraft.h"
 
#include "newgrf_cargo.h"
 
#include "group.h"
 
#include "misc/autoptr.hpp"
 
#include "strings_func.h"
 
#include "gfx_func.h"
 
#include "functions.h"
 
#include "window_func.h"
 
#include "date_func.h"
 
#include "autoreplace_base.h"
 
#include "autoreplace_gui.h"
 
#include "string_func.h"
 
#include "settings_type.h"
 
#include "oldpool_func.h"
 

	
 
#include "table/strings.h"
 
@@ -507,37 +506,33 @@ EngineID EngineReplacement(EngineRenewLi
 

	
 
CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, uint32 flags)
 
{
 
	EngineRenew *er;
 

	
 
	/* Check if the old vehicle is already in the list */
 
	er = GetEngineReplacement(*erl, old_engine, group);
 
	if (er != NULL) {
 
		if (flags & DC_EXEC) er->to = new_engine;
 
		return CommandCost();
 
	}
 

	
 
	er = new EngineRenew(old_engine, new_engine);
 
	if (er == NULL) return CMD_ERROR;
 
	AutoPtrT<EngineRenew> er_auto_delete = er;
 

	
 
	if (!EngineRenew::CanAllocateItem()) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		er = new EngineRenew(old_engine, new_engine);
 
		er->group_id = group;
 

	
 
		/* Insert before the first element */
 
		er->next = (EngineRenew *)(*erl);
 
		*erl = (EngineRenewList)er;
 

	
 
		er_auto_delete.Detach();
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 
CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, uint32 flags)
 
{
 
	EngineRenew *er = (EngineRenew *)(*erl);
 
	EngineRenew *prev = NULL;
 

	
 
	while (er)
 
	{
src/group_cmd.cpp
Show inline comments
 
@@ -3,25 +3,24 @@
 
/** @file group_cmd.cpp Handling of the engine groups */
 

	
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "variables.h"
 
#include "command_func.h"
 
#include "saveload.h"
 
#include "debug.h"
 
#include "group.h"
 
#include "train.h"
 
#include "aircraft.h"
 
#include "vehicle_gui.h"
 
#include "misc/autoptr.hpp"
 
#include "strings_func.h"
 
#include "functions.h"
 
#include "window_func.h"
 
#include "vehicle_func.h"
 
#include "autoreplace_base.h"
 
#include "autoreplace_func.h"
 
#include "string_func.h"
 
#include "player_func.h"
 
#include "order_func.h"
 
#include "oldpool_func.h"
 

	
 
#include "table/strings.h"
 
@@ -85,38 +84,32 @@ static WindowClass GetWCForVT(VehicleTyp
 

	
 
/**
 
 * Create a new vehicle group.
 
 * @param tile unused
 
 * @param p1   vehicle type
 
 * @param p2   unused
 
 */
 
CommandCost CmdCreateGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	VehicleType vt = (VehicleType)p1;
 
	if (!IsPlayerBuildableVehicleType(vt)) return CMD_ERROR;
 

	
 
	AutoPtrT<Group> g_auto_delete;
 

	
 
	Group *g = new Group(_current_player);
 
	if (g == NULL) return CMD_ERROR;
 

	
 
	g_auto_delete = g;
 
	if (!Group::CanAllocateItem()) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		Group *g = new Group(_current_player);
 
		g->replace_protection = false;
 
		g->vehicle_type = vt;
 

	
 
		InvalidateWindowData(GetWCForVT(vt), (vt << 11) | VLW_GROUP_LIST | _current_player);
 

	
 
		g_auto_delete.Detach();
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 

	
 
/**
 
 * Add all vehicles in the given group to the default group and then deletes the group.
 
 * @param tile unused
 
 * @param p1   index of array group
 
 *      - p1 bit 0-15 : GroupID
 
 * @param p2   unused
src/industry_cmd.cpp
Show inline comments
 
@@ -17,25 +17,24 @@
 
#include "saveload.h"
 
#include "variables.h"
 
#include "cheat_func.h"
 
#include "genworld.h"
 
#include "water_map.h"
 
#include "tree_map.h"
 
#include "cargotype.h"
 
#include "newgrf.h"
 
#include "newgrf_commons.h"
 
#include "newgrf_industries.h"
 
#include "newgrf_industrytiles.h"
 
#include "newgrf_callbacks.h"
 
#include "misc/autoptr.hpp"
 
#include "autoslope.h"
 
#include "transparency.h"
 
#include "water.h"
 
#include "strings_func.h"
 
#include "tile_cmd.h"
 
#include "functions.h"
 
#include "window_func.h"
 
#include "date_func.h"
 
#include "vehicle_func.h"
 
#include "sound_func.h"
 
#include "station_base.h"
 
#include "oldpool_func.h"
 
@@ -1583,35 +1582,37 @@ static Industry *CreateNewIndustryHelper
 
		if (!_check_new_industry_procs[indspec->check_proc](tile)) return NULL;
 
	}
 

	
 
	if (!custom_shape_check && _patches.land_generator == LG_TERRAGENESIS && _generating_world && !_ignore_restrictions && !CheckIfCanLevelIndustryPlatform(tile, 0, it, type)) return NULL;
 
	if (!CheckIfFarEnoughFromIndustry(tile, type)) return NULL;
 

	
 
	const Town *t = CheckMultipleIndustryInTown(tile, type);
 
	if (t == NULL) return NULL;
 

	
 
	if (!CheckIfIndustryIsAllowed(tile, type, t)) return NULL;
 
	if (!CheckSuitableIndustryPos(tile)) return NULL;
 

	
 
	Industry *i = new Industry(tile);
 
	if (i == NULL) return NULL;
 
	AutoPtrT<Industry> i_auto_delete = i;
 
	if (!Industry::CanAllocateItem()) return NULL;
 

	
 
	if (flags & DC_EXEC) {
 
		Industry *i = new Industry(tile);
 
		if (!custom_shape_check) CheckIfCanLevelIndustryPlatform(tile, DC_EXEC, it, type);
 
		DoCreateNewIndustry(i, tile, type, it, itspec_index, t, OWNER_NONE);
 
		i_auto_delete.Detach();
 

	
 
		return i;
 
	}
 

	
 
	return i;
 
	/* We need to return a non-NULL pointer to tell we have created an industry.
 
	 * However, we haven't created a real one (no DC_EXEC), so return a fake one. */
 
	return GetIndustry(0);
 
}
 

	
 
/** Build/Fund an industry
 
 * @param tile tile where industry is built
 
 * @param flags of operations to conduct
 
 * @param p1 various bitstuffed elements
 
 * - p1 = (bit  0 - 15) - industry type see build_industry.h and see industry.h
 
 * - p1 = (bit 16 - 31) - first layout to try
 
 * @param p2 seed to use for variable 8F
 
 * @return index of the newly create industry, or CMD_ERROR if it failed
 
 */
 
CommandCost CmdBuildIndustry(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
src/oldpool.h
Show inline comments
 
@@ -283,24 +283,36 @@ protected:
 

	
 
		return AllocateSafeRaw(first);
 
	}
 

	
 
	/**
 
	 * Are we cleaning this pool?
 
	 * @return true if we are
 
	 */
 
	static inline bool CleaningPool()
 
	{
 
		return Tpool->CleaningPool();
 
	}
 

	
 
public:
 
	/**
 
	 * Check whether we can allocate an item in this pool. This to prevent the
 
	 * need to actually construct the object and then destructing it again,
 
	 * which could be *very* costly.
 
	 * @return true if and only if at least ONE item can be allocated.
 
	 */
 
	static inline bool CanAllocateItem()
 
	{
 
		return AllocateRaw() != NULL;
 
	}
 
};
 

	
 

	
 
#define OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \
 
	enum { \
 
		name##_POOL_BLOCK_SIZE_BITS = block_size_bits, \
 
		name##_POOL_MAX_BLOCKS      = max_blocks \
 
	};
 

	
 

	
 
#define OLD_POOL_ACCESSORS(name, type) \
 
	static inline type* Get##name(uint index) { return _##name##_pool.Get(index);  } \
src/rail_cmd.cpp
Show inline comments
 
@@ -21,25 +21,24 @@
 
#include "town.h"
 
#include "sprite.h"
 
#include "depot_base.h"
 
#include "depot_func.h"
 
#include "waypoint.h"
 
#include "rail.h"
 
#include "newgrf.h"
 
#include "yapf/yapf.h"
 
#include "newgrf_engine.h"
 
#include "newgrf_callbacks.h"
 
#include "newgrf_station.h"
 
#include "train.h"
 
#include "misc/autoptr.hpp"
 
#include "variables.h"
 
#include "autoslope.h"
 
#include "transparency.h"
 
#include "water.h"
 
#include "tunnelbridge_map.h"
 
#include "window_func.h"
 
#include "vehicle_func.h"
 
#include "sound_func.h"
 
#include "signal_func.h"
 
#include "tunnelbridge.h"
 
#include "station_map.h"
 
#include "water_map.h"
 
@@ -757,38 +756,35 @@ CommandCost CmdBuildTrainDepot(TileIndex
 
				!_patches.build_on_slopes ||
 
				IsSteepSlope(tileh) ||
 
				!CanBuildDepotByTileh(dir, tileh)
 
			)) {
 
		return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
 
	}
 

	
 
	CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (CmdFailed(cost)) return CMD_ERROR;
 

	
 
	if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
 

	
 
	Depot *d = new Depot(tile);
 

	
 
	if (d == NULL) return CMD_ERROR;
 
	AutoPtrT<Depot> d_auto_delete = d;
 
	if (!Depot::CanAllocateItem()) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		Depot *d = new Depot(tile);
 
		MakeRailDepot(tile, _current_player, dir, (RailType)p1);
 
		MarkTileDirtyByTile(tile);
 

	
 
		d->town_index = ClosestTownFromTile(tile, (uint)-1)->index;
 

	
 
		AddSideToSignalBuffer(tile, INVALID_DIAGDIR, _current_player);
 
		YapfNotifyTrackLayoutChange(tile, TrackdirToTrack(DiagdirToDiagTrackdir(dir)));
 
		d_auto_delete.Detach();
 
	}
 

	
 
	return cost.AddCost(_price.build_train_depot);
 
}
 

	
 
/** Build signals, alternate between double/single, signal/semaphore,
 
 * pre/exit/combo-signals, and what-else not. If the rail piece does not
 
 * have any signals, bit 4 (cycle signal-type) is ignored
 
 * @param tile tile where to build the signals
 
 * @param flags operation to perform
 
 * @param p1 various bitstuffed elements
 
 * - p1 = (bit 0-2) - track-orientation, valid values: 0-5 (Track enum)
src/road_cmd.cpp
Show inline comments
 
@@ -14,25 +14,24 @@
 
#include "tile_cmd.h"
 
#include "landscape.h"
 
#include "town_map.h"
 
#include "viewport_func.h"
 
#include "command_func.h"
 
#include "town.h"
 
#include "yapf/yapf.h"
 
#include "depot_base.h"
 
#include "depot_func.h"
 
#include "newgrf.h"
 
#include "station_map.h"
 
#include "tunnel_map.h"
 
#include "misc/autoptr.hpp"
 
#include "variables.h"
 
#include "autoslope.h"
 
#include "transparency.h"
 
#include "tunnelbridge_map.h"
 
#include "window_func.h"
 
#include "strings_func.h"
 
#include "vehicle_func.h"
 
#include "vehicle_base.h"
 
#include "sound_func.h"
 
#include "road_func.h"
 
#include "tunnelbridge.h"
 
#include "cheat_func.h"
 
@@ -808,34 +807,32 @@ CommandCost CmdBuildRoadDepot(TileIndex 
 
				!_patches.build_on_slopes ||
 
				IsSteepSlope(tileh) ||
 
				!CanBuildDepotByTileh(dir, tileh)
 
			)) {
 
		return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
 
	}
 

	
 
	CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (CmdFailed(cost)) return CMD_ERROR;
 

	
 
	if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
 

	
 
	Depot *dep = new Depot(tile);
 
	if (dep == NULL) return CMD_ERROR;
 
	AutoPtrT<Depot> d_auto_delete = dep;
 
	if (!Depot::CanAllocateItem()) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		Depot *dep = new Depot(tile);
 
		dep->town_index = ClosestTownFromTile(tile, (uint)-1)->index;
 

	
 
		MakeRoadDepot(tile, _current_player, dir, rt);
 
		MarkTileDirtyByTile(tile);
 
		d_auto_delete.Detach();
 
	}
 
	return cost.AddCost(_price.build_road_depot);
 
}
 

	
 
static CommandCost RemoveRoadDepot(TileIndex tile, uint32 flags)
 
{
 
	if (!CheckTileOwnership(tile) && _current_player != OWNER_WATER) return CMD_ERROR;
 

	
 
	if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		DoClearSquare(tile);
src/ship_cmd.cpp
Show inline comments
 
@@ -19,25 +19,24 @@
 
#include "npf.h"
 
#include "depot_base.h"
 
#include "depot_func.h"
 
#include "vehicle_gui.h"
 
#include "newgrf_engine.h"
 
#include "water_map.h"
 
#include "yapf/yapf.h"
 
#include "debug.h"
 
#include "newgrf_callbacks.h"
 
#include "newgrf_text.h"
 
#include "newgrf_sound.h"
 
#include "spritecache.h"
 
#include "misc/autoptr.hpp"
 
#include "strings_func.h"
 
#include "functions.h"
 
#include "window_func.h"
 
#include "date_func.h"
 
#include "vehicle_func.h"
 
#include "sound_func.h"
 
#include "variables.h"
 
#include "autoreplace_gui.h"
 
#include "gfx_func.h"
 
#include "settings_type.h"
 
#include "order_func.h"
 
#include "effectvehicle_func.h"
src/signs.cpp
Show inline comments
 
@@ -2,25 +2,24 @@
 

	
 
/** @file signs.cpp */
 

	
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "landscape.h"
 
#include "player_func.h"
 
#include "signs_base.h"
 
#include "signs_func.h"
 
#include "saveload.h"
 
#include "command_func.h"
 
#include "variables.h"
 
#include "misc/autoptr.hpp"
 
#include "strings_func.h"
 
#include "viewport_func.h"
 
#include "zoom_func.h"
 
#include "functions.h"
 
#include "window_func.h"
 
#include "map_func.h"
 
#include "string_func.h"
 
#include "oldpool_func.h"
 

	
 
#include "table/strings.h"
 

	
 
SignID _new_sign_id;
 
@@ -90,43 +89,41 @@ static void MarkSignDirty(Sign *si)
 
/**
 
 * Place a sign at the given coordinates. Ownership of sign has
 
 * no effect whatsoever except for the colour the sign gets for easy recognition,
 
 * but everybody is able to rename/remove it.
 
 * @param tile tile to place sign at
 
 * @param flags type of operation
 
 * @param p1 unused
 
 * @param p2 unused
 
 */
 
CommandCost CmdPlaceSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	/* Try to locate a new sign */
 
	Sign *si = new Sign(_current_player);
 
	if (si == NULL) return_cmd_error(STR_2808_TOO_MANY_SIGNS);
 
	AutoPtrT<Sign> s_auto_delete = si;
 
	if (!Sign::CanAllocateItem()) return_cmd_error(STR_2808_TOO_MANY_SIGNS);
 

	
 
	/* When we execute, really make the sign */
 
	if (flags & DC_EXEC) {
 
		Sign *si = new Sign(_current_player);
 
		int x = TileX(tile) * TILE_SIZE;
 
		int y = TileY(tile) * TILE_SIZE;
 

	
 
		si->x = x;
 
		si->y = y;
 
		si->z = GetSlopeZ(x, y);
 
		UpdateSignVirtCoords(si);
 
		MarkSignDirty(si);
 
		InvalidateWindow(WC_SIGN_LIST, 0);
 
		_sign_sort_dirty = true;
 
		_new_sign_id = si->index;
 
		_total_signs++;
 
		s_auto_delete.Detach();
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 
/** Rename a sign. If the new name of the sign is empty, we assume
 
 * the user wanted to delete it. So delete it. Ownership of signs
 
 * has no meaning/effect whatsoever except for eyecandy
 
 * @param tile unused
 
 * @param flags type of operation
 
 * @param p1 index of the sign to be renamed/removed
 
 * @param p2 unused
src/town_cmd.cpp
Show inline comments
 
@@ -22,25 +22,24 @@
 
#include "gui.h"
 
#include "unmovable_map.h"
 
#include "water_map.h"
 
#include "variables.h"
 
#include "bridge.h"
 
#include "bridge_map.h"
 
#include "genworld.h"
 
#include "newgrf.h"
 
#include "newgrf_callbacks.h"
 
#include "newgrf_house.h"
 
#include "newgrf_commons.h"
 
#include "newgrf_townname.h"
 
#include "misc/autoptr.hpp"
 
#include "autoslope.h"
 
#include "waypoint.h"
 
#include "transparency.h"
 
#include "tunnelbridge_map.h"
 
#include "strings_func.h"
 
#include "window_func.h"
 
#include "string_func.h"
 
#include "newgrf_cargo.h"
 
#include "oldpool_func.h"
 
#include "sprite.h"
 
#include "economy_func.h"
 
#include "station_func.h"
 
@@ -1529,34 +1528,32 @@ CommandCost CmdBuildTown(TileIndex tile,
 

	
 
	/* Check distance to all other towns. */
 
	if (IsCloseToTown(tile, 20))
 
		return_cmd_error(STR_0238_TOO_CLOSE_TO_ANOTHER_TOWN);
 

	
 
	uint32 townnameparts;
 

	
 
	/* Get a unique name for the town. */
 
	if (!CreateTownName(&townnameparts))
 
		return_cmd_error(STR_023A_TOO_MANY_TOWNS);
 

	
 
	/* Allocate town struct */
 
	Town *t = new Town(tile);
 
	if (t == NULL) return_cmd_error(STR_023A_TOO_MANY_TOWNS);
 
	AutoPtrT<Town> t_auto_delete = t;
 
	if (!Town::CanAllocateItem()) return_cmd_error(STR_023A_TOO_MANY_TOWNS);
 

	
 
	/* Create the town */
 
	if (flags & DC_EXEC) {
 
		Town *t = new Town(tile);
 
		_generating_world = true;
 
		DoCreateTown(t, tile, townnameparts, (TownSizeMode)p2, p1);
 
		_generating_world = false;
 
		t_auto_delete.Detach();
 
	}
 
	return CommandCost();
 
}
 

	
 
Town *CreateRandomTown(uint attempts, TownSizeMode mode, uint size)
 
{
 
	do {
 
		/* Generate a tile index not too close from the edge */
 
		TileIndex tile = RandomTile();
 
		if (DistanceFromEdge(tile) < 20) continue;
 

	
 
		/* Make sure the tile is plain */
src/water_cmd.cpp
Show inline comments
 
@@ -15,25 +15,24 @@
 
#include "town.h"
 
#include "news_func.h"
 
#include "depot_base.h"
 
#include "depot_func.h"
 
#include "vehicle_gui.h"
 
#include "train.h"
 
#include "roadveh.h"
 
#include "water.h"
 
#include "water_map.h"
 
#include "industry_map.h"
 
#include "newgrf.h"
 
#include "newgrf_canal.h"
 
#include "misc/autoptr.hpp"
 
#include "transparency.h"
 
#include "strings_func.h"
 
#include "functions.h"
 
#include "window_func.h"
 
#include "vehicle_func.h"
 
#include "sound_func.h"
 
#include "variables.h"
 
#include "player_func.h"
 
#include "settings_type.h"
 
#include "clear_map.h"
 
#include "tree_map.h"
 
#include "station_base.h"
 
@@ -192,36 +191,34 @@ CommandCost CmdBuildShipDepot(TileIndex 
 
	if (GetTileSlope(tile, NULL) != SLOPE_FLAT || GetTileSlope(tile2, NULL) != SLOPE_FLAT) {
 
		/* Prevent depots on rapids */
 
		return_cmd_error(STR_0239_SITE_UNSUITABLE);
 
	}
 

	
 
	WaterClass wc1 = GetWaterClass(tile);
 
	WaterClass wc2 = GetWaterClass(tile2);
 
	ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (CmdFailed(ret)) return CMD_ERROR;
 
	ret = DoCommand(tile2, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (CmdFailed(ret)) return CMD_ERROR;
 

	
 
	Depot *depot = new Depot(tile);
 
	if (depot == NULL) return CMD_ERROR;
 
	AutoPtrT<Depot> d_auto_delete = depot;
 
	if (!Depot::CanAllocateItem()) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		Depot *depot = new Depot(tile);
 
		depot->town_index = ClosestTownFromTile(tile, (uint)-1)->index;
 

	
 
		MakeShipDepot(tile,  _current_player, DEPOT_NORTH, axis, wc1);
 
		MakeShipDepot(tile2, _current_player, DEPOT_SOUTH, axis, wc2);
 
		MarkTileDirtyByTile(tile);
 
		MarkTileDirtyByTile(tile2);
 
		d_auto_delete.Detach();
 
	}
 

	
 
	return CommandCost(EXPENSES_CONSTRUCTION, _price.build_ship_depot);
 
}
 

	
 
void MakeWaterKeepingClass(TileIndex tile, Owner o)
 
{
 
	assert(IsTileType(tile, MP_WATER) || (IsTileType(tile, MP_STATION) && (IsBuoy(tile) || IsDock(tile))));
 

	
 
	switch (GetWaterClass(tile)) {
 
		case WATER_CLASS_SEA:   MakeWater(tile);              break;
 
		case WATER_CLASS_CANAL: MakeCanal(tile, o, Random()); break;
src/waypoint.cpp
Show inline comments
 
@@ -9,25 +9,24 @@
 
#include "landscape.h"
 
#include "order_func.h"
 
#include "rail_map.h"
 
#include "rail.h"
 
#include "bridge_map.h"
 
#include "saveload.h"
 
#include "station_base.h"
 
#include "town.h"
 
#include "waypoint.h"
 
#include "variables.h"
 
#include "yapf/yapf.h"
 
#include "newgrf.h"
 
#include "misc/autoptr.hpp"
 
#include "strings_func.h"
 
#include "gfx_func.h"
 
#include "functions.h"
 
#include "window_func.h"
 
#include "economy_func.h"
 
#include "date_func.h"
 
#include "vehicle_func.h"
 
#include "vehicle_base.h"
 
#include "string_func.h"
 
#include "signal_func.h"
 
#include "player_func.h"
 
#include "settings_type.h"
 
@@ -182,25 +181,24 @@ void AfterLoadWaypoints()
 
 * piece of rail
 
 * @param tile tile where waypoint will be built
 
 * @param flags type of operation
 
 * @param p1 graphics for waypoint type, 0 indicates standard graphics
 
 * @param p2 unused
 
 *
 
 * @todo When checking for the tile slope,
 
 * distingush between "Flat land required" and "land sloped in wrong direction"
 
 */
 
CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Waypoint *wp;
 
	AutoPtrT<Waypoint> wp_auto_delete;
 
	Slope tileh;
 
	Axis axis;
 

	
 
	/* if custom gfx are used, make sure it is within bounds */
 
	if (p1 >= GetNumCustomStations(STAT_CLASS_WAYP)) return CMD_ERROR;
 

	
 
	if (!IsTileType(tile, MP_RAILWAY) ||
 
			GetRailTileType(tile) != RAIL_TILE_NORMAL || (
 
				(axis = AXIS_X, GetTrackBits(tile) != TRACK_BIT_X) &&
 
				(axis = AXIS_Y, GetTrackBits(tile) != TRACK_BIT_Y)
 
			)) {
 
		return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK);
 
@@ -210,53 +208,53 @@ CommandCost CmdBuildTrainWaypoint(TileIn
 
	if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
 

	
 
	tileh = GetTileSlope(tile, NULL);
 
	if (tileh != SLOPE_FLAT &&
 
			(!_patches.build_on_slopes || IsSteepSlope(tileh) || !(tileh & (0x3 << axis)) || !(tileh & ~(0x3 << axis)))) {
 
		return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
 
	}
 

	
 
	if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
 

	
 
	/* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
 
	wp = FindDeletedWaypointCloseTo(tile);
 
	if (wp == NULL) {
 
		wp = new Waypoint(tile);
 
		if (wp == NULL) return CMD_ERROR;
 
	if (wp == NULL && !Waypoint::CanAllocateItem()) return CMD_ERROR;
 

	
 
		wp_auto_delete = wp;
 
	if (flags & DC_EXEC) {
 
		if (wp == NULL) {
 
			wp = new Waypoint(tile);
 
			if (wp == NULL) return CMD_ERROR;
 

	
 
			wp->town_index = INVALID_TOWN;
 
			wp->name = NULL;
 
			wp->town_cn = 0;
 
		} else {
 
			/* Move existing (recently deleted) waypoint to the new location */
 

	
 
		wp->town_index = INVALID_TOWN;
 
		wp->name = NULL;
 
		wp->town_cn = 0;
 
	} else if (flags & DC_EXEC) {
 
		/* Move existing (recently deleted) waypoint to the new location */
 
			/* First we update the destination for all vehicles that
 
			* have the old waypoint in their orders. */
 
			Vehicle *v;
 
			FOR_ALL_VEHICLES(v) {
 
				if (v->type == VEH_TRAIN &&
 
						v->First() == v &&
 
						v->current_order.IsType(OT_GOTO_WAYPOINT) &&
 
						v->dest_tile == wp->xy) {
 
					v->dest_tile = tile;
 
				}
 
			}
 

	
 
		/* First we update the destination for all vehicles that
 
		 * have the old waypoint in their orders. */
 
		Vehicle *v;
 
		FOR_ALL_VEHICLES(v) {
 
			if (v->type == VEH_TRAIN &&
 
					v->First() == v &&
 
					v->current_order.IsType(OT_GOTO_WAYPOINT) &&
 
					v->dest_tile == wp->xy) {
 
				v->dest_tile = tile;
 
			}
 
			RedrawWaypointSign(wp);
 
			wp->xy = tile;
 
		}
 

	
 
		RedrawWaypointSign(wp);
 
		wp->xy = tile;
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		const StationSpec* statspec;
 

	
 
		MakeRailWaypoint(tile, GetTileOwner(tile), axis, GetRailType(tile), wp->index);
 
		MarkTileDirtyByTile(tile);
 

	
 
		statspec = GetCustomStationSpec(STAT_CLASS_WAYP, p1);
 

	
 
		if (statspec != NULL) {
 
			wp->stat_id = p1;
 
			wp->grfid = statspec->grffile->grfid;
 
			wp->localidx = statspec->localidx;
 
		} else {
 
@@ -265,25 +263,24 @@ CommandCost CmdBuildTrainWaypoint(TileIn
 
			wp->grfid = 0;
 
			wp->localidx = 0;
 
		}
 

	
 
		wp->deleted = 0;
 
		wp->build_date = _date;
 

	
 
		if (wp->town_index == INVALID_TOWN) MakeDefaultWaypointName(wp);
 

	
 
		UpdateWaypointSign(wp);
 
		RedrawWaypointSign(wp);
 
		YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
 
		wp_auto_delete.Detach();
 
	}
 

	
 
	return CommandCost(EXPENSES_CONSTRUCTION, _price.build_train_depot);
 
}
 

	
 
/**
 
 * Daily loop for waypoints
 
 */
 
void WaypointsDailyLoop()
 
{
 
	Waypoint *wp;
 

	
0 comments (0 inline, 0 general)