src/water_cmd.cpp
branchnoai
changeset 9701 d1ac22c62f64
parent 9694 e72987579514
child 9703 d2a6acdbd665
equal deleted inserted replaced
9700:e442ce398e83 9701:d1ac22c62f64
    24 #include "depot.h"
    24 #include "depot.h"
    25 #include "vehicle_gui.h"
    25 #include "vehicle_gui.h"
    26 #include "train.h"
    26 #include "train.h"
    27 #include "roadveh.h"
    27 #include "roadveh.h"
    28 #include "water_map.h"
    28 #include "water_map.h"
       
    29 #include "industry_map.h"
    29 #include "newgrf.h"
    30 #include "newgrf.h"
    30 #include "newgrf_canal.h"
    31 #include "newgrf_canal.h"
    31 #include "misc/autoptr.hpp"
    32 #include "misc/autoptr.hpp"
    32 
    33 
    33 static const SpriteID _water_shore_sprites[] = {
    34 static const SpriteID _water_shore_sprites[] = {
    64 
    65 
    65 	CommandCost cost, ret;
    66 	CommandCost cost, ret;
    66 
    67 
    67 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
    68 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
    68 
    69 
    69 	if (!EnsureNoVehicle(tile)) return CMD_ERROR;
       
    70 
       
    71 	Axis axis = Extract<Axis, 0>(p1);
    70 	Axis axis = Extract<Axis, 0>(p1);
    72 
    71 
    73 	tile2 = tile + (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
    72 	tile2 = tile + (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
    74 	if (!EnsureNoVehicle(tile2)) return CMD_ERROR;
       
    75 
    73 
    76 	if (!IsClearWaterTile(tile) || !IsClearWaterTile(tile2))
    74 	if (!IsClearWaterTile(tile) || !IsClearWaterTile(tile2))
    77 		return_cmd_error(STR_3801_MUST_BE_BUILT_ON_WATER);
    75 		return_cmd_error(STR_3801_MUST_BE_BUILT_ON_WATER);
    78 
    76 
    79 	if (IsBridgeAbove(tile) || IsBridgeAbove(tile2)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
    77 	if (IsBridgeAbove(tile) || IsBridgeAbove(tile2)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
   286 {
   284 {
   287 	switch (GetWaterTileType(tile)) {
   285 	switch (GetWaterTileType(tile)) {
   288 		case WATER_TILE_CLEAR:
   286 		case WATER_TILE_CLEAR:
   289 			if (flags & DC_NO_WATER) return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER);
   287 			if (flags & DC_NO_WATER) return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER);
   290 
   288 
   291 			/* Make sure no vehicle is on the tile */
       
   292 			if (!EnsureNoVehicle(tile)) return CMD_ERROR;
       
   293 
       
   294 			/* Make sure it's not an edge tile. */
   289 			/* Make sure it's not an edge tile. */
   295 			if (!IS_INT_INSIDE(TileX(tile), 1, MapMaxX() - 1) ||
   290 			if (!IS_INT_INSIDE(TileX(tile), 1, MapMaxX() - 1) ||
   296 					!IS_INT_INSIDE(TileY(tile), 1, MapMaxY() - 1)) {
   291 					!IS_INT_INSIDE(TileY(tile), 1, MapMaxY() - 1)) {
   297 				return_cmd_error(STR_0002_TOO_CLOSE_TO_EDGE_OF_MAP);
   292 				return_cmd_error(STR_0002_TOO_CLOSE_TO_EDGE_OF_MAP);
   298 			}
   293 			}
   299 
   294 
       
   295 			/* Make sure no vehicle is on the tile */
       
   296 			if (!EnsureNoVehicle(tile)) return CMD_ERROR;
       
   297 
   300 			if (GetTileOwner(tile) != OWNER_WATER && GetTileOwner(tile) != OWNER_NONE && !CheckTileOwnership(tile)) return CMD_ERROR;
   298 			if (GetTileOwner(tile) != OWNER_WATER && GetTileOwner(tile) != OWNER_NONE && !CheckTileOwnership(tile)) return CMD_ERROR;
   301 
   299 
   302 			if (flags & DC_EXEC) DoClearSquare(tile);
   300 			if (flags & DC_EXEC) DoClearSquare(tile);
   303 			return CommandCost(_price.clear_water);
   301 			return CommandCost(_price.clear_water);
   304 
   302 
   305 		case WATER_TILE_COAST: {
   303 		case WATER_TILE_COAST: {
   306 			Slope slope = GetTileSlope(tile, NULL);
   304 			Slope slope = GetTileSlope(tile, NULL);
   307 
   305 
   308 			/* Make sure no vehicle is on the tile */
   306 			/* Make sure no vehicle is on the tile */
   309 			if (!EnsureNoVehicle(tile)) return CMD_ERROR;
   307 			if (!EnsureNoVehicle(tile)) return CMD_ERROR;
   310 
       
   311 			/* Make sure it's not an edge tile. */
       
   312 			if (!IS_INT_INSIDE(TileX(tile), 1, MapMaxX() - 1) ||
       
   313 					!IS_INT_INSIDE(TileY(tile), 1, MapMaxY() - 1)) {
       
   314 				return_cmd_error(STR_0002_TOO_CLOSE_TO_EDGE_OF_MAP);
       
   315 			}
       
   316 
   308 
   317 			if (flags & DC_EXEC) DoClearSquare(tile);
   309 			if (flags & DC_EXEC) DoClearSquare(tile);
   318 			if (slope == SLOPE_N || slope == SLOPE_E || slope == SLOPE_S || slope == SLOPE_W) {
   310 			if (slope == SLOPE_N || slope == SLOPE_E || slope == SLOPE_S || slope == SLOPE_W) {
   319 				return CommandCost(_price.clear_water);
   311 				return CommandCost(_price.clear_water);
   320 			} else {
   312 			} else {
   360 
   352 
   361 				default:
   353 				default:
   362 					return false;
   354 					return false;
   363 			}
   355 			}
   364 
   356 
   365 		case MP_STATION: return IsOilRig(tile) || IsDock(tile) || IsBuoy(tile);
   357 		case MP_STATION:  return IsOilRig(tile) || IsDock(tile) || IsBuoy(tile);
   366 		default:         return false;
   358 		case MP_INDUSTRY: return (GetIndustrySpec(GetIndustryType(tile))->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0;
       
   359 		default:          return false;
   367 	}
   360 	}
   368 }
   361 }
   369 
   362 
   370 /** draw a canal styled water tile with dikes around */
   363 /** draw a canal styled water tile with dikes around */
   371 void DrawCanalWater(TileIndex tile)
   364 void DrawCanalWater(TileIndex tile)
   375 	/* Test for custom graphics, else use the default */
   368 	/* Test for custom graphics, else use the default */
   376 	SpriteID dikes_base = GetCanalSprite(CF_DIKES, tile);
   369 	SpriteID dikes_base = GetCanalSprite(CF_DIKES, tile);
   377 	if (dikes_base == 0) dikes_base = SPR_CANALS_BASE + 57;
   370 	if (dikes_base == 0) dikes_base = SPR_CANALS_BASE + 57;
   378 
   371 
   379 	/* determine the edges around with water. */
   372 	/* determine the edges around with water. */
   380 	wa = IsWateredTile(TILE_ADDXY(tile, -1, 0)) << 0;
   373 	wa  = IsWateredTile(TILE_ADDXY(tile, -1,  0)) << 0;
   381 	wa += IsWateredTile(TILE_ADDXY(tile, 0, 1)) << 1;
   374 	wa += IsWateredTile(TILE_ADDXY(tile,  0,  1)) << 1;
   382 	wa += IsWateredTile(TILE_ADDXY(tile, 1, 0)) << 2;
   375 	wa += IsWateredTile(TILE_ADDXY(tile,  1,  0)) << 2;
   383 	wa += IsWateredTile(TILE_ADDXY(tile, 0, -1)) << 3;
   376 	wa += IsWateredTile(TILE_ADDXY(tile,  0, -1)) << 3;
   384 
   377 
   385 	if (!(wa & 1)) DrawGroundSprite(dikes_base,     PAL_NONE);
   378 	if (!(wa & 1)) DrawGroundSprite(dikes_base,     PAL_NONE);
   386 	if (!(wa & 2)) DrawGroundSprite(dikes_base + 1, PAL_NONE);
   379 	if (!(wa & 2)) DrawGroundSprite(dikes_base + 1, PAL_NONE);
   387 	if (!(wa & 4)) DrawGroundSprite(dikes_base + 2, PAL_NONE);
   380 	if (!(wa & 4)) DrawGroundSprite(dikes_base + 2, PAL_NONE);
   388 	if (!(wa & 8)) DrawGroundSprite(dikes_base + 3, PAL_NONE);
   381 	if (!(wa & 8)) DrawGroundSprite(dikes_base + 3, PAL_NONE);
   565 							(slope == SLOPE_E && tracks == TRACK_BIT_LEFT)  ||
   558 							(slope == SLOPE_E && tracks == TRACK_BIT_LEFT)  ||
   566 							(slope == SLOPE_N && tracks == TRACK_BIT_LOWER)
   559 							(slope == SLOPE_N && tracks == TRACK_BIT_LOWER)
   567 						)) {
   560 						)) {
   568 					break;
   561 					break;
   569 				}
   562 				}
       
   563 
       
   564 				Vehicle *v = FindFloodableVehicleOnTile(target);
       
   565 				if (v != NULL) FloodVehicle(v);
   570 			}
   566 			}
   571 			/* FALLTHROUGH */
   567 			/* FALLTHROUGH */
   572 
   568 
   573 			case MP_CLEAR:
   569 			case MP_CLEAR:
   574 			case MP_TREES:
   570 			case MP_TREES:
   653 
   649 
   654 				if (v->z_pos != airport->delta_z + 1) return;
   650 				if (v->z_pos != airport->delta_z + 1) return;
   655 			}
   651 			}
   656 			Vehicle *u;
   652 			Vehicle *u;
   657 
   653 
   658 			if (v->type != VEH_AIRCRAFT) v = GetFirstVehicleInChain(v);
   654 			if (v->type != VEH_AIRCRAFT) v = v->First();
   659 			u = v;
   655 			u = v;
   660 
   656 
   661 			/* crash all wagons, and count passengers */
   657 			/* crash all wagons, and count passengers */
   662 			BEGIN_ENUM_WAGONS(v)
   658 			BEGIN_ENUM_WAGONS(v)
   663 				if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) pass += v->cargo.Count();
   659 				if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) pass += v->cargo.Count();
   795 }
   791 }
   796 
   792 
   797 static uint32 VehicleEnter_Water(Vehicle *v, TileIndex tile, int x, int y)
   793 static uint32 VehicleEnter_Water(Vehicle *v, TileIndex tile, int x, int y)
   798 {
   794 {
   799 	return VETSB_CONTINUE;
   795 	return VETSB_CONTINUE;
       
   796 }
       
   797 
       
   798 static CommandCost TerraformTile_Water(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new)
       
   799 {
       
   800 	/* Canals can't be terraformed */
       
   801 	if (IsClearWaterTile(tile) && IsCanal(tile)) return_cmd_error(STR_MUST_DEMOLISH_CANAL_FIRST);
       
   802 
       
   803 	return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
   800 }
   804 }
   801 
   805 
   802 
   806 
   803 extern const TileTypeProcs _tile_type_water_procs = {
   807 extern const TileTypeProcs _tile_type_water_procs = {
   804 	DrawTile_Water,           /* draw_tile_proc */
   808 	DrawTile_Water,           /* draw_tile_proc */
   812 	TileLoop_Water,           /* tile_loop_clear */
   816 	TileLoop_Water,           /* tile_loop_clear */
   813 	ChangeTileOwner_Water,    /* change_tile_owner_clear */
   817 	ChangeTileOwner_Water,    /* change_tile_owner_clear */
   814 	NULL,                     /* get_produced_cargo_proc */
   818 	NULL,                     /* get_produced_cargo_proc */
   815 	VehicleEnter_Water,       /* vehicle_enter_tile_proc */
   819 	VehicleEnter_Water,       /* vehicle_enter_tile_proc */
   816 	GetFoundation_Water,      /* get_foundation_proc */
   820 	GetFoundation_Water,      /* get_foundation_proc */
       
   821 	TerraformTile_Water,      /* terraform_tile_proc */
   817 };
   822 };