Codechange: Use null pointer literal instead of the NULL macro
[openttd-github.git] / src / script / api / script_tunnel.cpp
bloba8bfcbfd3ce194c0604dd439aeacb029be7cd76a
1 /* $Id$ */
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_tunnel.cpp Implementation of ScriptTunnel. */
12 #include "../../stdafx.h"
13 #include "script_tunnel.hpp"
14 #include "script_rail.hpp"
15 #include "../script_instance.hpp"
16 #include "../../tunnel_map.h"
18 #include "../../safeguards.h"
20 /* static */ bool ScriptTunnel::IsTunnelTile(TileIndex tile)
22 if (!::IsValidTile(tile)) return false;
23 return ::IsTunnelTile(tile);
26 /* static */ TileIndex ScriptTunnel::GetOtherTunnelEnd(TileIndex tile)
28 if (!::IsValidTile(tile)) return INVALID_TILE;
30 /* If it's a tunnel already, take the easy way out! */
31 if (IsTunnelTile(tile)) return ::GetOtherTunnelEnd(tile);
33 int start_z;
34 Slope start_tileh = ::GetTileSlope(tile, &start_z);
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 ::GetTileSlope(tile, &end_z);
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 EnforcePrecondition(false, ::IsValidTile(start));
85 EnforcePrecondition(false, vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_ROAD);
86 EnforcePrecondition(false, vehicle_type != ScriptVehicle::VT_RAIL || ScriptRail::IsRailTypeAvailable(ScriptRail::GetCurrentRailType()));
87 EnforcePrecondition(false, vehicle_type != ScriptVehicle::VT_ROAD || ScriptRoad::IsRoadTypeAvailable(ScriptRoad::GetCurrentRoadType()));
88 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY || vehicle_type == ScriptVehicle::VT_ROAD);
90 uint type = 0;
91 if (vehicle_type == ScriptVehicle::VT_ROAD) {
92 type |= (TRANSPORT_ROAD << 8);
93 type |= ::RoadTypeToRoadTypes((::RoadType)ScriptObject::GetRoadType());
94 } else {
95 type |= (TRANSPORT_RAIL << 8);
96 type |= ScriptRail::GetCurrentRailType();
99 /* For rail we do nothing special */
100 if (vehicle_type == ScriptVehicle::VT_RAIL) {
101 return ScriptObject::DoCommand(start, type, 0, CMD_BUILD_TUNNEL);
104 ScriptObject::SetCallbackVariable(0, start);
105 return ScriptObject::DoCommand(start, type, 0, CMD_BUILD_TUNNEL, nullptr, &::_DoCommandReturnBuildTunnel1);
108 /* static */ bool ScriptTunnel::_BuildTunnelRoad1()
110 /* Build the piece of road on the 'start' side of the tunnel */
111 TileIndex end = ScriptObject::GetCallbackVariable(0);
112 TileIndex start = ScriptTunnel::GetOtherTunnelEnd(end);
114 DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
115 DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
117 return ScriptObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (ScriptObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, nullptr, &::_DoCommandReturnBuildTunnel2);
120 /* static */ bool ScriptTunnel::_BuildTunnelRoad2()
122 /* Build the piece of road on the 'end' side of the tunnel */
123 TileIndex end = ScriptObject::GetCallbackVariable(0);
124 TileIndex start = ScriptTunnel::GetOtherTunnelEnd(end);
126 DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
127 DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
129 return ScriptObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (ScriptObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
132 /* static */ bool ScriptTunnel::RemoveTunnel(TileIndex tile)
134 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
135 EnforcePrecondition(false, IsTunnelTile(tile));
137 return ScriptObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);