Update: Translations from eints
[openttd-github.git] / src / script / api / script_tunnel.cpp
blobed7680593c7254fe9e62ba3f5d68726550430bd5
1 /*
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/>.
6 */
8 /** @file script_tunnel.cpp Implementation of ScriptTunnel. */
10 #include "../../stdafx.h"
11 #include "script_tunnel.hpp"
12 #include "script_rail.hpp"
13 #include "../script_instance.hpp"
14 #include "../../tunnel_map.h"
15 #include "../../landscape_cmd.h"
16 #include "../../road_cmd.h"
17 #include "../../tunnelbridge_cmd.h"
19 #include "../../safeguards.h"
21 /* static */ bool ScriptTunnel::IsTunnelTile(TileIndex tile)
23 if (!::IsValidTile(tile)) return false;
24 return ::IsTunnelTile(tile);
27 /* static */ TileIndex ScriptTunnel::GetOtherTunnelEnd(TileIndex tile)
29 if (!::IsValidTile(tile)) return INVALID_TILE;
31 /* If it's a tunnel already, take the easy way out! */
32 if (IsTunnelTile(tile)) return ::GetOtherTunnelEnd(tile);
34 auto [start_tileh, start_z] = ::GetTileSlopeZ(tile);
35 DiagDirection direction = ::GetInclinedSlopeDirection(start_tileh);
36 if (direction == INVALID_DIAGDIR) return INVALID_TILE;
38 TileIndexDiff delta = ::TileOffsByDiagDir(direction);
39 int end_z;
40 do {
41 tile += delta;
42 if (!::IsValidTile(tile)) return INVALID_TILE;
44 std::tie(std::ignore, end_z) = ::GetTileSlopeZ(tile);
45 } while (start_z != end_z);
47 return tile;
50 /**
51 * Helper function to connect a just built tunnel to nearby roads.
52 * @param instance The script instance we have to built the road for.
54 static void _DoCommandReturnBuildTunnel2(class ScriptInstance *instance)
56 if (!ScriptTunnel::_BuildTunnelRoad2()) {
57 ScriptInstance::DoCommandReturn(instance);
58 return;
61 /* This can never happen, as in test-mode this callback is never executed,
62 * and in execute-mode, the other callback is called. */
63 NOT_REACHED();
66 /**
67 * Helper function to connect a just built tunnel to nearby roads.
68 * @param instance The script instance we have to built the road for.
70 static void _DoCommandReturnBuildTunnel1(class ScriptInstance *instance)
72 if (!ScriptTunnel::_BuildTunnelRoad1()) {
73 ScriptInstance::DoCommandReturn(instance);
74 return;
77 /* This can never happen, as in test-mode this callback is never executed,
78 * and in execute-mode, the other callback is called. */
79 NOT_REACHED();
82 /* static */ bool ScriptTunnel::BuildTunnel(ScriptVehicle::VehicleType vehicle_type, TileIndex start)
84 EnforceDeityOrCompanyModeValid(false);
85 EnforcePrecondition(false, ::IsValidTile(start));
86 EnforcePrecondition(false, vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_ROAD);
87 EnforcePrecondition(false, vehicle_type != ScriptVehicle::VT_RAIL || ScriptRail::IsRailTypeAvailable(ScriptRail::GetCurrentRailType()));
88 EnforcePrecondition(false, vehicle_type != ScriptVehicle::VT_ROAD || ScriptRoad::IsRoadTypeAvailable(ScriptRoad::GetCurrentRoadType()));
89 EnforcePrecondition(false, ScriptCompanyMode::IsValid() || vehicle_type == ScriptVehicle::VT_ROAD);
91 if (vehicle_type == ScriptVehicle::VT_RAIL) {
92 /* For rail we do nothing special */
93 return ScriptObject::Command<CMD_BUILD_TUNNEL>::Do(start, TRANSPORT_RAIL, ScriptRail::GetCurrentRailType());
94 } else {
95 ScriptObject::SetCallbackVariable(0, start.base());
96 return ScriptObject::Command<CMD_BUILD_TUNNEL>::Do(&::_DoCommandReturnBuildTunnel1, start, TRANSPORT_ROAD, ScriptRoad::GetCurrentRoadType());
100 /* static */ bool ScriptTunnel::_BuildTunnelRoad1()
102 EnforceDeityOrCompanyModeValid(false);
104 /* Build the piece of road on the 'start' side of the tunnel */
105 TileIndex end = ScriptObject::GetCallbackVariable(0);
106 TileIndex start = ScriptTunnel::GetOtherTunnelEnd(end);
108 DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
109 DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
111 return ScriptObject::Command<CMD_BUILD_ROAD>::Do(&::_DoCommandReturnBuildTunnel2, start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2), ScriptRoad::GetRoadType(), DRD_NONE, 0);
114 /* static */ bool ScriptTunnel::_BuildTunnelRoad2()
116 EnforceDeityOrCompanyModeValid(false);
118 /* Build the piece of road on the 'end' side of the tunnel */
119 TileIndex end = ScriptObject::GetCallbackVariable(0);
120 TileIndex start = ScriptTunnel::GetOtherTunnelEnd(end);
122 DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
123 DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
125 return ScriptObject::Command<CMD_BUILD_ROAD>::Do(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1), ScriptRoad::GetRoadType(), DRD_NONE, 0);
128 /* static */ bool ScriptTunnel::RemoveTunnel(TileIndex tile)
130 EnforceCompanyModeValid(false);
131 EnforcePrecondition(false, IsTunnelTile(tile));
133 return ScriptObject::Command<CMD_LANDSCAPE_CLEAR>::Do(tile);