File diff r11089:babc36cbd948 → r11090:9276cea703d4
src/unmovable_cmd.cpp
Show inline comments
 
@@ -28,158 +28,158 @@
 
#include "table/sprites.h"
 
#include "table/unmovable_land.h"
 

	
 
/**
 
 * Accessor for array _original_unmovable.
 
 * This will ensure at once : proper access and
 
 * not allowing modifications of it.
 
 * @param type of unmovable (which is the index in _original_unmovable)
 
 * @pre type < UNMOVABLE_MAX
 
 * @return a pointer to the corresponding unmovable spec
 
 **/
 
static inline const UnmovableSpec *GetUnmovableSpec(UnmovableType type)
 
{
 
	assert(type < UNMOVABLE_MAX);
 
	return &_original_unmovable[type];
 
}
 

	
 
/** Destroy a HQ.
 
 * During normal gameplay you can only implicitely destroy a HQ when you are
 
 * rebuilding it. Otherwise, only water can destroy it.
 
 * @param cid Company requesting the destruction of his HQ
 
 * @param flags docommand flags of calling function
 
 * @return cost of the operation
 
 */
 
static CommandCost DestroyCompanyHQ(CompanyID cid, uint32 flags)
 
static CommandCost DestroyCompanyHQ(CompanyID cid, DoCommandFlag flags)
 
{
 
	Company *c = GetCompany(cid);
 

	
 
	if (flags & DC_EXEC) {
 
		TileIndex t = c->location_of_HQ;
 

	
 
		DoClearSquare(t);
 
		DoClearSquare(t + TileDiffXY(0, 1));
 
		DoClearSquare(t + TileDiffXY(1, 0));
 
		DoClearSquare(t + TileDiffXY(1, 1));
 
		c->location_of_HQ = INVALID_TILE; // reset HQ position
 
		InvalidateWindow(WC_COMPANY, cid);
 
	}
 

	
 
	/* cost of relocating company is 1% of company value */
 
	return CommandCost(EXPENSES_PROPERTY, CalculateCompanyValue(c) / 100);
 
}
 

	
 
void UpdateCompanyHQ(Company *c, uint score)
 
{
 
	byte val;
 
	TileIndex tile = c->location_of_HQ;
 

	
 
	if (tile == INVALID_TILE) return;
 

	
 
	(val = 0, score < 170) ||
 
	(val++, score < 350) ||
 
	(val++, score < 520) ||
 
	(val++, score < 720) ||
 
	(val++, true);
 

	
 
	EnlargeCompanyHQ(tile, val);
 

	
 
	MarkTileDirtyByTile(tile);
 
	MarkTileDirtyByTile(tile + TileDiffXY(0, 1));
 
	MarkTileDirtyByTile(tile + TileDiffXY(1, 0));
 
	MarkTileDirtyByTile(tile + TileDiffXY(1, 1));
 
}
 

	
 
extern CommandCost CheckFlatLandBelow(TileIndex tile, uint w, uint h, uint flags, uint invalid_dirs, StationID *station, bool check_clear = true);
 
extern CommandCost CheckFlatLandBelow(TileIndex tile, uint w, uint h, DoCommandFlag flags, uint invalid_dirs, StationID *station, bool check_clear = true);
 

	
 
/** Build or relocate the HQ. This depends if the HQ is already built or not
 
 * @param tile tile where the HQ will be built or relocated to
 
 * @param flags type of operation
 
 * @param p1 unused
 
 * @param p2 unused
 
 */
 
CommandCost CmdBuildCompanyHQ(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, const char *text)
 
CommandCost CmdBuildCompanyHQ(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	Company *c = GetCompany(_current_company);
 
	CommandCost cost(EXPENSES_PROPERTY);
 

	
 
	cost = CheckFlatLandBelow(tile, 2, 2, flags, 0, NULL);
 
	if (CmdFailed(cost)) return cost;
 

	
 
	if (c->location_of_HQ != INVALID_TILE) { // Moving HQ
 
		cost.AddCost(DestroyCompanyHQ(_current_company, flags));
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		int score = UpdateCompanyRatingAndValue(c, false);
 

	
 
		c->location_of_HQ = tile;
 

	
 
		MakeCompanyHQ(tile, _current_company);
 

	
 
		UpdateCompanyHQ(c, score);
 
		InvalidateWindow(WC_COMPANY, c->index);
 
	}
 

	
 
	return cost;
 
}
 

	
 
/** Purchase a land area. Actually you only purchase one tile, so
 
 * the name is a bit confusing ;p
 
 * @param tile the tile the company is purchasing
 
 * @param flags for this command type
 
 * @param p1 unused
 
 * @param p2 unused
 
 * @return error of cost of operation
 
 */
 
CommandCost CmdPurchaseLandArea(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, const char *text)
 
CommandCost CmdPurchaseLandArea(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	CommandCost cost(EXPENSES_CONSTRUCTION);
 

	
 
	if (IsOwnedLandTile(tile) && IsTileOwner(tile, _current_company)) {
 
		return_cmd_error(STR_5807_YOU_ALREADY_OWN_IT);
 
	}
 

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

	
 
	if (flags & DC_EXEC) {
 
		MakeOwnedLand(tile, _current_company);
 
		MarkTileDirtyByTile(tile);
 
	}
 

	
 
	return cost.AddCost(GetUnmovableSpec(UNMOVABLE_OWNED_LAND)->GetBuildingCost());
 
}
 

	
 
/** Sell a land area. Actually you only sell one tile, so
 
 * the name is a bit confusing ;p
 
 * @param tile the tile the company is selling
 
 * @param flags for this command type
 
 * @param p1 unused
 
 * @param p2 unused
 
 * @return error or cost of operation
 
 */
 
CommandCost CmdSellLandArea(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, const char *text)
 
CommandCost CmdSellLandArea(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	if (!IsOwnedLandTile(tile)) return CMD_ERROR;
 
	if (!CheckTileOwnership(tile) && _current_company != OWNER_WATER) return CMD_ERROR;
 

	
 
	if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) DoClearSquare(tile);
 

	
 
	return CommandCost(EXPENSES_CONSTRUCTION, - GetUnmovableSpec(UNMOVABLE_OWNED_LAND)->GetRemovalCost());
 
}
 

	
 
static Foundation GetFoundation_Unmovable(TileIndex tile, Slope tileh);
 

	
 
static void DrawTile_Unmovable(TileInfo *ti)
 
{
 

	
 
	switch (GetUnmovableType(ti->tile)) {
 
		default: NOT_REACHED(); break;
 
		case UNMOVABLE_TRANSMITTER:
 
		case UNMOVABLE_LIGHTHOUSE: {
 
			const DrawTileSeqStruct *dtu = &_draw_tile_transmitterlighthouse_data[GetUnmovableType(ti->tile)];
 

	
 
			if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
 
			DrawClearLandTile(ti, 2);
 
@@ -237,49 +237,49 @@ static void DrawTile_Unmovable(TileInfo 
 
				);
 
			}
 
			break;
 
		}
 
	}
 
}
 

	
 
static uint GetSlopeZ_Unmovable(TileIndex tile, uint x, uint y)
 
{
 
	if (IsOwnedLand(tile)) {
 
		uint z;
 
		Slope tileh = GetTileSlope(tile, &z);
 

	
 
		return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
 
	} else {
 
		return GetTileMaxZ(tile);
 
	}
 
}
 

	
 
static Foundation GetFoundation_Unmovable(TileIndex tile, Slope tileh)
 
{
 
	return IsOwnedLand(tile) ? FOUNDATION_NONE : FlatteningFoundation(tileh);
 
}
 

	
 
static CommandCost ClearTile_Unmovable(TileIndex tile, byte flags)
 
static CommandCost ClearTile_Unmovable(TileIndex tile, DoCommandFlag flags)
 
{
 
	if (IsCompanyHQ(tile)) {
 
		if (_current_company == OWNER_WATER) {
 
			return DestroyCompanyHQ(GetTileOwner(tile), DC_EXEC);
 
		} else {
 
			return_cmd_error(STR_5804_COMPANY_HEADQUARTERS_IN);
 
		}
 
	}
 

	
 
	if (IsOwnedLand(tile)) {
 
		return DoCommand(tile, 0, 0, flags, CMD_SELL_LAND_AREA);
 
	}
 

	
 
	/* checks if you're allowed to remove unmovable things */
 
	if (_game_mode != GM_EDITOR && _current_company != OWNER_WATER && ((flags & DC_AUTO || !_cheats.magic_bulldozer.value)) )
 
		return_cmd_error(STR_5800_OBJECT_IN_THE_WAY);
 

	
 
	if (IsStatue(tile)) {
 
		if (flags & DC_AUTO) return_cmd_error(STR_5800_OBJECT_IN_THE_WAY);
 

	
 
		TownID town = GetStatueTownID(tile);
 
		ClrBit(GetTown(town)->statues, GetTileOwner(tile));
 
		InvalidateWindow(WC_TOWN_AUTHORITY, town);
 
	}
 
@@ -465,49 +465,49 @@ void GenerateUnmovables()
 
static void ChangeTileOwner_Unmovable(TileIndex tile, Owner old_owner, Owner new_owner)
 
{
 
	if (!IsTileOwner(tile, old_owner)) return;
 

	
 
	if (IsOwnedLand(tile) && new_owner != INVALID_OWNER) {
 
		SetTileOwner(tile, new_owner);
 
	} else if (IsStatueTile(tile)) {
 
		TownID town = GetStatueTownID(tile);
 
		Town *t = GetTown(town);
 
		ClrBit(t->statues, old_owner);
 
		if (new_owner != INVALID_OWNER && !HasBit(t->statues, new_owner)) {
 
			/* Transfer ownership to the new company */
 
			SetBit(t->statues, new_owner);
 
			SetTileOwner(tile, new_owner);
 
		} else {
 
			DoClearSquare(tile);
 
		}
 

	
 
		InvalidateWindow(WC_TOWN_AUTHORITY, town);
 
	} else {
 
		DoClearSquare(tile);
 
	}
 
}
 

	
 
static CommandCost TerraformTile_Unmovable(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new)
 
static CommandCost TerraformTile_Unmovable(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
 
{
 
	/* Owned land remains unsold */
 
	if (IsOwnedLand(tile) && CheckTileOwnership(tile)) return CommandCost();
 

	
 
	if (AutoslopeEnabled() && (IsStatue(tile) || IsCompanyHQ(tile))) {
 
		if (!IsSteepSlope(tileh_new) && (z_new + GetSlopeMaxZ(tileh_new) == GetTileMaxZ(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
 
	}
 

	
 
	return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
}
 

	
 
extern const TileTypeProcs _tile_type_unmovable_procs = {
 
	DrawTile_Unmovable,             /* draw_tile_proc */
 
	GetSlopeZ_Unmovable,            /* get_slope_z_proc */
 
	ClearTile_Unmovable,            /* clear_tile_proc */
 
	GetAcceptedCargo_Unmovable,     /* get_accepted_cargo_proc */
 
	GetTileDesc_Unmovable,          /* get_tile_desc_proc */
 
	GetTileTrackStatus_Unmovable,   /* get_tile_track_status_proc */
 
	ClickTile_Unmovable,            /* click_tile_proc */
 
	AnimateTile_Unmovable,          /* animate_tile_proc */
 
	TileLoop_Unmovable,             /* tile_loop_clear */
 
	ChangeTileOwner_Unmovable,      /* change_tile_owner_clear */
 
	NULL,                           /* get_produced_cargo_proc */
 
	NULL,                           /* vehicle_enter_tile_proc */