File diff r12233:9d0861714103 → r12234:fd1a494e7620
src/unmovable_cmd.cpp
Show inline comments
 
@@ -198,318 +198,318 @@ static void DrawTile_Unmovable(TileInfo 
 
			/* This should prevent statues from sinking into the ground when on a slope. */
 
			if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, GetFoundation_Unmovable(ti->tile, ti->tileh));
 

	
 
			DrawGroundSprite(SPR_CONCRETE_GROUND, PAL_NONE);
 

	
 
			if (IsInvisibilitySet(TO_STRUCTURES)) break;
 

	
 
			AddSortableSpriteToDraw(SPR_STATUE_COMPANY, COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile)), ti->x, ti->y, 16, 16, 25, ti->z, IsTransparencySet(TO_STRUCTURES));
 
			break;
 

	
 
		case UNMOVABLE_OWNED_LAND:
 
			DrawClearLandTile(ti, 0);
 

	
 
			AddSortableSpriteToDraw(
 
				SPR_BOUGHT_LAND, COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile)),
 
				ti->x + TILE_SIZE / 2, ti->y + TILE_SIZE / 2, 1, 1, BB_HEIGHT_UNDER_BRIDGE, GetSlopeZ(ti->x + TILE_SIZE / 2, ti->y + TILE_SIZE / 2)
 
			);
 
			DrawBridgeMiddle(ti);
 
			break;
 

	
 
		case UNMOVABLE_HQ:{
 
			assert(IsCompanyHQ(ti->tile));
 
			if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
 

	
 
			SpriteID palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
 

	
 
			const DrawTileSprites *t = &_unmovable_display_datas[GetCompanyHQSize(ti->tile) << 2 | GetCompanyHQSection(ti->tile)];
 
			DrawGroundSprite(t->ground.sprite, palette);
 

	
 
			if (IsInvisibilitySet(TO_STRUCTURES)) break;
 

	
 
			const DrawTileSeqStruct *dtss;
 
			foreach_draw_tile_seq(dtss, t->seq) {
 
				AddSortableSpriteToDraw(
 
					dtss->image.sprite, palette,
 
					ti->x + dtss->delta_x, ti->y + dtss->delta_y,
 
					dtss->size_x, dtss->size_y,
 
					dtss->size_z, ti->z + dtss->delta_z,
 
					IsTransparencySet(TO_STRUCTURES)
 
				);
 
			}
 
			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, DoCommandFlag flags)
 
{
 
	if (IsCompanyHQ(tile)) {
 
		if (_current_company == OWNER_WATER) {
 
			return DestroyCompanyHQ(GetTileOwner(tile), DC_EXEC);
 
		} else {
 
			return_cmd_error(flags & DC_AUTO ? STR_ERROR_COMPANY_HEADQUARTERS_IN : INVALID_STRING_ID);
 
		}
 
	}
 

	
 
	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(flags & DC_AUTO ? STR_ERROR_OBJECT_IN_THE_WAY : INVALID_STRING_ID);
 

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

	
 
		TownID town = GetStatueTownID(tile);
 
		ClrBit(Town::Get(town)->statues, GetTileOwner(tile));
 
		InvalidateWindow(WC_TOWN_AUTHORITY, town);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		DoClearSquare(tile);
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 
static void GetAcceptedCargo_Unmovable(TileIndex tile, AcceptedCargo ac)
 
static void AddAcceptedCargo_Unmovable(TileIndex tile, AcceptedCargo ac)
 
{
 
	if (!IsCompanyHQ(tile)) return;
 

	
 
	/* HQ accepts passenger and mail; but we have to divide the values
 
	 * between 4 tiles it occupies! */
 

	
 
	/* HQ level (depends on company performance) in the range 1..5. */
 
	uint level = GetCompanyHQSize(tile) + 1;
 

	
 
	/* Top town building generates 10, so to make HQ interesting, the top
 
	 * type makes 20. */
 
	ac[CT_PASSENGERS] = max(1U, level);
 
	ac[CT_PASSENGERS] += max(1U, level);
 

	
 
	/* Top town building generates 4, HQ can make up to 8. The
 
	 * proportion passengers:mail is different because such a huge
 
	 * commercial building generates unusually high amount of mail
 
	 * correspondence per physical visitor. */
 
	ac[CT_MAIL] = max(1U, level / 2);
 
	ac[CT_MAIL] += max(1U, level / 2);
 
}
 

	
 

	
 
static void GetTileDesc_Unmovable(TileIndex tile, TileDesc *td)
 
{
 
	td->str = GetUnmovableSpec(GetUnmovableType(tile))->name;
 
	td->owner[0] = GetTileOwner(tile);
 
}
 

	
 
static void AnimateTile_Unmovable(TileIndex tile)
 
{
 
	/* not used */
 
}
 

	
 
static void TileLoop_Unmovable(TileIndex tile)
 
{
 
	if (!IsCompanyHQ(tile)) return;
 

	
 
	/* HQ accepts passenger and mail; but we have to divide the values
 
	 * between 4 tiles it occupies! */
 

	
 
	/* HQ level (depends on company performance) in the range 1..5. */
 
	uint level = GetCompanyHQSize(tile) + 1;
 
	assert(level < 6);
 

	
 
	uint r = Random();
 
	/* Top town buildings generate 250, so the top HQ type makes 256. */
 
	if (GB(r, 0, 8) < (256 / 4 / (6 - level))) {
 
		uint amt = GB(r, 0, 8) / 8 / 4 + 1;
 
		if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
 
		MoveGoodsToStation(tile, 2, 2, CT_PASSENGERS, amt);
 
	}
 

	
 
	/* Top town building generates 90, HQ can make up to 196. The
 
	 * proportion passengers:mail is about the same as in the acceptance
 
	 * equations. */
 
	if (GB(r, 8, 8) < (196 / 4 / (6 - level))) {
 
		uint amt = GB(r, 8, 8) / 8 / 4 + 1;
 
		if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
 
		MoveGoodsToStation(tile, 2, 2, CT_MAIL, amt);
 
	}
 
}
 

	
 

	
 
static TrackStatus GetTileTrackStatus_Unmovable(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
 
{
 
	return 0;
 
}
 

	
 
static bool ClickTile_Unmovable(TileIndex tile)
 
{
 
	if (!IsCompanyHQ(tile)) return false;
 

	
 
	ShowCompany(GetTileOwner(tile));
 
	return true;
 
}
 

	
 

	
 
/* checks, if a radio tower is within a 9x9 tile square around tile */
 
static bool IsRadioTowerNearby(TileIndex tile)
 
{
 
	TileIndex tile_s = tile - TileDiffXY(min(TileX(tile), 4U), min(TileY(tile), 4U));
 
	uint w = min(TileX(tile), 4U) + 1 + min(MapMaxX() - TileX(tile), 4U);
 
	uint h = min(TileY(tile), 4U) + 1 + min(MapMaxY() - TileY(tile), 4U);
 

	
 
	BEGIN_TILE_LOOP(tile, w, h, tile_s)
 
		if (IsTransmitterTile(tile)) return true;
 
	END_TILE_LOOP(tile, w, h, tile_s)
 

	
 
	return false;
 
}
 

	
 
void GenerateUnmovables()
 
{
 
	if (_settings_game.game_creation.landscape == LT_TOYLAND) return;
 

	
 
	/* add radio tower */
 
	int radiotower_to_build = ScaleByMapSize(15); // maximum number of radio towers on the map
 
	int lighthouses_to_build = _settings_game.game_creation.landscape == LT_TROPIC ? 0 : ScaleByMapSize1D((Random() & 3) + 7);
 

	
 
	/* Scale the amount of lighthouses with the amount of land at the borders. */
 
	if (_settings_game.construction.freeform_edges && lighthouses_to_build != 0) {
 
		uint num_water_tiles = 0;
 
		for (uint x = 0; x < MapMaxX(); x++) {
 
			if (IsTileType(TileXY(x, 1), MP_WATER)) num_water_tiles++;
 
			if (IsTileType(TileXY(x, MapMaxY() - 1), MP_WATER)) num_water_tiles++;
 
		}
 
		for (uint y = 1; y < MapMaxY() - 1; y++) {
 
			if (IsTileType(TileXY(1, y), MP_WATER)) num_water_tiles++;
 
			if (IsTileType(TileXY(MapMaxX() - 1, y), MP_WATER)) num_water_tiles++;
 
		}
 
		/* The -6 is because the top borders are MP_VOID (-2) and all corners
 
		 * are counted twice (-4). */
 
		lighthouses_to_build = lighthouses_to_build * num_water_tiles / (2 * MapMaxY() + 2 * MapMaxX() - 6);
 
	}
 

	
 
	SetGeneratingWorldProgress(GWP_UNMOVABLE, radiotower_to_build + lighthouses_to_build);
 

	
 
	for (uint i = ScaleByMapSize(1000); i != 0; i--) {
 
		TileIndex tile = RandomTile();
 

	
 
		uint h;
 
		if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h >= TILE_HEIGHT * 4 && !IsBridgeAbove(tile)) {
 
			if (IsRadioTowerNearby(tile)) continue;
 

	
 
			MakeTransmitter(tile);
 
			IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
 
			if (--radiotower_to_build == 0) break;
 
		}
 
	}
 

	
 
	/* add lighthouses */
 
	uint maxx = MapMaxX();
 
	uint maxy = MapMaxY();
 
	for (int loop_count = 0; loop_count < 1000 && lighthouses_to_build != 0; loop_count++) {
 
		uint r = Random();
 

	
 
		/* Scatter the lighthouses more evenly around the perimeter */
 
		int perimeter = (GB(r, 16, 16) % (2 * (maxx + maxy))) - maxy;
 
		DiagDirection dir;
 
		for (dir = DIAGDIR_NE; perimeter > 0; dir++) {
 
			perimeter -= (DiagDirToAxis(dir) == AXIS_X) ? maxx : maxy;
 
		}
 

	
 
		TileIndex tile;
 
		switch (dir) {
 
			default:
 
			case DIAGDIR_NE: tile = TileXY(maxx - 1, r % maxy); break;
 
			case DIAGDIR_SE: tile = TileXY(r % maxx, 1); break;
 
			case DIAGDIR_SW: tile = TileXY(1,        r % maxy); break;
 
			case DIAGDIR_NW: tile = TileXY(r % maxx, maxy - 1); break;
 
		}
 

	
 
		/* Only build lighthouses at tiles where the border is sea. */
 
		if (!IsTileType(tile, MP_WATER)) continue;
 

	
 
		for (int j = 0; j < 19; j++) {
 
			uint h;
 
			if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h <= TILE_HEIGHT * 2 && !IsBridgeAbove(tile)) {
 
				MakeLighthouse(tile);
 
				IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
 
				lighthouses_to_build--;
 
				assert(tile < MapSize());
 
				break;
 
			}
 
			tile = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDiagDir(dir));
 
			if (tile == INVALID_TILE) break;
 
		}
 
	}
 
}
 

	
 
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 = Town::Get(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, 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
 
	AddAcceptedCargo_Unmovable,     // add_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
 
	GetFoundation_Unmovable,        // get_foundation_proc
 
	TerraformTile_Unmovable,        // terraform_tile_proc
 
};