Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / script / api / script_marine.cpp
blobbfa79d255d5d5c6847d5de430708594fbbefc95c
1 /* $Id: script_marine.cpp 23633 2011-12-19 21:05:36Z truebrain $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file script_marine.cpp Implementation of ScriptMarine. */
12 #include "../../stdafx.h"
13 #include "script_marine.hpp"
14 #include "script_station.hpp"
15 #include "../../station_base.h"
16 #include "../../tile_cmd.h"
18 #include "../../safeguards.h"
21 /* static */ bool ScriptMarine::IsWaterDepotTile(TileIndex tile)
23 if (!::IsValidTile(tile)) return false;
25 return ::IsTileType(tile, MP_WATER) && ::GetWaterTileType(tile) == WATER_TILE_DEPOT;
28 /* static */ bool ScriptMarine::IsDockTile(TileIndex tile)
30 if (!::IsValidTile(tile)) return false;
32 return ::IsTileType(tile, MP_STATION) && ::IsDock(tile);
35 /* static */ bool ScriptMarine::IsBuoyTile(TileIndex tile)
37 if (!::IsValidTile(tile)) return false;
39 return ::IsTileType(tile, MP_STATION) && ::IsBuoy(tile);
42 /* static */ bool ScriptMarine::IsLockTile(TileIndex tile)
44 if (!::IsValidTile(tile)) return false;
46 return ::IsTileType(tile, MP_WATER) && ::GetWaterTileType(tile) == WATER_TILE_LOCK;
49 /* static */ bool ScriptMarine::IsCanalTile(TileIndex tile)
51 if (!::IsValidTile(tile)) return false;
53 return ::IsTileType(tile, MP_WATER) && ::IsCanal(tile);
56 /* static */ bool ScriptMarine::AreWaterTilesConnected(TileIndex t1, TileIndex t2)
58 if (!::IsValidTile(t1)) return false;
59 if (!::IsValidTile(t2)) return false;
61 /* Tiles not neighbouring */
62 if (::DistanceManhattan(t1, t2) != 1) return false;
64 DiagDirection to_other_tile = ::DiagdirBetweenTiles(t2, t1);
66 /* Determine the reachable tracks from the shared edge */
67 TrackBits gtts1 = ::TrackStatusToTrackBits(::GetTileTrackStatus(t1, TRANSPORT_WATER, 0, to_other_tile)) & ::DiagdirReachesTracks(to_other_tile);
68 if (gtts1 == TRACK_BIT_NONE) return false;
70 to_other_tile = ReverseDiagDir(to_other_tile);
71 TrackBits gtts2 = ::TrackStatusToTrackBits(::GetTileTrackStatus(t2, TRANSPORT_WATER, 0, to_other_tile)) & ::DiagdirReachesTracks(to_other_tile);
73 return gtts2 != TRACK_BIT_NONE;
76 /* static */ bool ScriptMarine::BuildWaterDepot(TileIndex tile, TileIndex front)
78 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
79 EnforcePrecondition(false, ::IsValidTile(tile));
80 EnforcePrecondition(false, ::IsValidTile(front));
81 EnforcePrecondition(false, (::TileX(front) == ::TileX(tile)) != (::TileY(front) == ::TileY(tile)));
83 return ScriptObject::DoCommand(tile, ::TileX(front) == ::TileX(tile), 0, CMD_BUILD_SHIP_DEPOT);
86 /* static */ bool ScriptMarine::BuildDock(TileIndex tile, StationID station_id)
88 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
89 EnforcePrecondition(false, ::IsValidTile(tile));
90 EnforcePrecondition(false, station_id == ScriptStation::STATION_NEW || station_id == ScriptStation::STATION_JOIN_ADJACENT || ScriptStation::IsValidStation(station_id));
92 uint p1 = station_id == ScriptStation::STATION_JOIN_ADJACENT ? 0 : 1;
93 uint p2 = (ScriptStation::IsValidStation(station_id) ? station_id : INVALID_STATION) << 16;
94 return ScriptObject::DoCommand(tile, p1, p2, CMD_BUILD_DOCK);
97 /* static */ bool ScriptMarine::BuildBuoy(TileIndex tile)
99 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
100 EnforcePrecondition(false, ::IsValidTile(tile));
102 return ScriptObject::DoCommand(tile, 0, 0, CMD_BUILD_BUOY);
105 /* static */ bool ScriptMarine::BuildLock(TileIndex tile)
107 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
108 EnforcePrecondition(false, ::IsValidTile(tile));
110 return ScriptObject::DoCommand(tile, 0, 0, CMD_BUILD_LOCK);
113 /* static */ bool ScriptMarine::BuildCanal(TileIndex tile)
115 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
116 EnforcePrecondition(false, ::IsValidTile(tile));
118 return ScriptObject::DoCommand(tile, tile, WATER_CLASS_CANAL, CMD_BUILD_CANAL);
121 /* static */ bool ScriptMarine::RemoveWaterDepot(TileIndex tile)
123 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
124 EnforcePrecondition(false, ::IsValidTile(tile));
125 EnforcePrecondition(false, IsWaterDepotTile(tile));
127 return ScriptObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
130 /* static */ bool ScriptMarine::RemoveDock(TileIndex tile)
132 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
133 EnforcePrecondition(false, ::IsValidTile(tile));
134 EnforcePrecondition(false, IsDockTile(tile));
136 return ScriptObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
139 /* static */ bool ScriptMarine::RemoveBuoy(TileIndex tile)
141 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
142 EnforcePrecondition(false, ::IsValidTile(tile));
143 EnforcePrecondition(false, IsBuoyTile(tile));
145 return ScriptObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
148 /* static */ bool ScriptMarine::RemoveLock(TileIndex tile)
150 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
151 EnforcePrecondition(false, ::IsValidTile(tile));
152 EnforcePrecondition(false, IsLockTile(tile));
154 return ScriptObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
157 /* static */ bool ScriptMarine::RemoveCanal(TileIndex tile)
159 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
160 EnforcePrecondition(false, ::IsValidTile(tile));
161 EnforcePrecondition(false, IsCanalTile(tile));
163 return ScriptObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
166 /* static */ Money ScriptMarine::GetBuildCost(BuildType build_type)
168 switch (build_type) {
169 case BT_DOCK: return ::GetPrice(PR_BUILD_STATION_DOCK, 1, NULL);
170 case BT_DEPOT: return ::GetPrice(PR_BUILD_DEPOT_SHIP, 1, NULL);
171 case BT_BUOY: return ::GetPrice(PR_BUILD_WAYPOINT_BUOY, 1, NULL);
172 default: return -1;