2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file script_marine.cpp Implementation of ScriptMarine. */
10 #include "../../stdafx.h"
11 #include "script_marine.hpp"
12 #include "script_station.hpp"
13 #include "../../station_base.h"
14 #include "../../dock_cmd.h"
15 #include "../../landscape_cmd.h"
16 #include "../../station_cmd.h"
17 #include "../../tile_cmd.h"
18 #include "../../water_cmd.h"
19 #include "../../waypoint_cmd.h"
21 #include "../../safeguards.h"
24 /* static */ bool ScriptMarine::IsWaterDepotTile(TileIndex tile
)
26 if (!::IsValidTile(tile
)) return false;
28 return ::IsTileType(tile
, MP_WATER
) && ::GetWaterTileType(tile
) == WATER_TILE_DEPOT
;
31 /* static */ bool ScriptMarine::IsDockTile(TileIndex tile
)
33 if (!::IsValidTile(tile
)) return false;
35 return ::IsTileType(tile
, MP_STATION
) && ::IsDock(tile
);
38 /* static */ bool ScriptMarine::IsBuoyTile(TileIndex tile
)
40 if (!::IsValidTile(tile
)) return false;
42 return ::IsTileType(tile
, MP_STATION
) && ::IsBuoy(tile
);
45 /* static */ bool ScriptMarine::IsLockTile(TileIndex tile
)
47 if (!::IsValidTile(tile
)) return false;
49 return ::IsTileType(tile
, MP_WATER
) && ::GetWaterTileType(tile
) == WATER_TILE_LOCK
;
52 /* static */ bool ScriptMarine::IsCanalTile(TileIndex tile
)
54 if (!::IsValidTile(tile
)) return false;
56 return ::IsTileType(tile
, MP_WATER
) && ::IsCanal(tile
);
59 /* static */ bool ScriptMarine::AreWaterTilesConnected(TileIndex t1
, TileIndex t2
)
61 if (!::IsValidTile(t1
)) return false;
62 if (!::IsValidTile(t2
)) return false;
64 /* Tiles not neighbouring */
65 if (::DistanceManhattan(t1
, t2
) != 1) return false;
67 DiagDirection to_other_tile
= ::DiagdirBetweenTiles(t2
, t1
);
69 /* Determine the reachable tracks from the shared edge */
70 TrackBits gtts1
= ::TrackStatusToTrackBits(::GetTileTrackStatus(t1
, TRANSPORT_WATER
, 0, ReverseDiagDir(to_other_tile
))) & ::DiagdirReachesTracks(to_other_tile
);
71 if (gtts1
== TRACK_BIT_NONE
) return false;
73 to_other_tile
= ReverseDiagDir(to_other_tile
);
74 TrackBits gtts2
= ::TrackStatusToTrackBits(::GetTileTrackStatus(t2
, TRANSPORT_WATER
, 0, ReverseDiagDir(to_other_tile
))) & ::DiagdirReachesTracks(to_other_tile
);
76 return gtts2
!= TRACK_BIT_NONE
;
79 /* static */ bool ScriptMarine::BuildWaterDepot(TileIndex tile
, TileIndex front
)
81 EnforceCompanyModeValid(false);
82 EnforcePrecondition(false, ::IsValidTile(tile
));
83 EnforcePrecondition(false, ::IsValidTile(front
));
84 EnforcePrecondition(false, (::TileX(front
) == ::TileX(tile
)) != (::TileY(front
) == ::TileY(tile
)));
86 return ScriptObject::Command
<CMD_BUILD_SHIP_DEPOT
>::Do(tile
, ::TileX(front
) == ::TileX(tile
) ? AXIS_Y
: AXIS_X
);
89 /* static */ bool ScriptMarine::BuildDock(TileIndex tile
, StationID station_id
)
91 EnforceCompanyModeValid(false);
92 EnforcePrecondition(false, ::IsValidTile(tile
));
93 EnforcePrecondition(false, station_id
== ScriptStation::STATION_NEW
|| station_id
== ScriptStation::STATION_JOIN_ADJACENT
|| ScriptStation::IsValidStation(station_id
));
95 return ScriptObject::Command
<CMD_BUILD_DOCK
>::Do(tile
, ScriptStation::IsValidStation(station_id
) ? station_id
: INVALID_STATION
, station_id
!= ScriptStation::STATION_JOIN_ADJACENT
);
98 /* static */ bool ScriptMarine::BuildBuoy(TileIndex tile
)
100 EnforceCompanyModeValid(false);
101 EnforcePrecondition(false, ::IsValidTile(tile
));
103 return ScriptObject::Command
<CMD_BUILD_BUOY
>::Do(tile
);
106 /* static */ bool ScriptMarine::BuildLock(TileIndex tile
)
108 EnforceCompanyModeValid(false);
109 EnforcePrecondition(false, ::IsValidTile(tile
));
111 return ScriptObject::Command
<CMD_BUILD_LOCK
>::Do(tile
);
114 /* static */ bool ScriptMarine::BuildCanal(TileIndex tile
)
116 EnforceCompanyModeValid(false);
117 EnforcePrecondition(false, ::IsValidTile(tile
));
119 return ScriptObject::Command
<CMD_BUILD_CANAL
>::Do(tile
, tile
, WATER_CLASS_CANAL
, false);
122 /* static */ bool ScriptMarine::RemoveWaterDepot(TileIndex tile
)
124 EnforceCompanyModeValid(false);
125 EnforcePrecondition(false, ::IsValidTile(tile
));
126 EnforcePrecondition(false, IsWaterDepotTile(tile
));
128 return ScriptObject::Command
<CMD_LANDSCAPE_CLEAR
>::Do(tile
);
131 /* static */ bool ScriptMarine::RemoveDock(TileIndex tile
)
133 EnforceCompanyModeValid(false);
134 EnforcePrecondition(false, ::IsValidTile(tile
));
135 EnforcePrecondition(false, IsDockTile(tile
));
137 return ScriptObject::Command
<CMD_LANDSCAPE_CLEAR
>::Do(tile
);
140 /* static */ bool ScriptMarine::RemoveBuoy(TileIndex tile
)
142 EnforceCompanyModeValid(false);
143 EnforcePrecondition(false, ::IsValidTile(tile
));
144 EnforcePrecondition(false, IsBuoyTile(tile
));
146 return ScriptObject::Command
<CMD_LANDSCAPE_CLEAR
>::Do(tile
);
149 /* static */ bool ScriptMarine::RemoveLock(TileIndex tile
)
151 EnforceCompanyModeValid(false);
152 EnforcePrecondition(false, ::IsValidTile(tile
));
153 EnforcePrecondition(false, IsLockTile(tile
));
155 return ScriptObject::Command
<CMD_LANDSCAPE_CLEAR
>::Do(tile
);
158 /* static */ bool ScriptMarine::RemoveCanal(TileIndex tile
)
160 EnforceCompanyModeValid(false);
161 EnforcePrecondition(false, ::IsValidTile(tile
));
162 EnforcePrecondition(false, IsCanalTile(tile
));
164 return ScriptObject::Command
<CMD_LANDSCAPE_CLEAR
>::Do(tile
);
167 /* static */ Money
ScriptMarine::GetBuildCost(BuildType build_type
)
169 switch (build_type
) {
170 case BT_DOCK
: return ::GetPrice(PR_BUILD_STATION_DOCK
, 1, nullptr);
171 case BT_DEPOT
: return ::GetPrice(PR_BUILD_DEPOT_SHIP
, 1, nullptr);
172 case BT_BUOY
: return ::GetPrice(PR_BUILD_WAYPOINT_BUOY
, 1, nullptr);
173 case BT_LOCK
: return ::GetPrice(PR_BUILD_LOCK
, 1, nullptr);
174 case BT_CANAL
: return ::GetPrice(PR_BUILD_CANAL
, 1, nullptr);