2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/** @file script_tunnel.cpp Implementation of ScriptTunnel. */
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-01-22 10:33:16 +00:00
|
|
|
#include "../../stdafx.h"
|
2011-11-29 23:07:38 +00:00
|
|
|
#include "script_tunnel.hpp"
|
|
|
|
#include "script_rail.hpp"
|
2011-11-29 23:27:17 +00:00
|
|
|
#include "../script_instance.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "../../tunnel_map.h"
|
2021-11-01 20:30:34 +00:00
|
|
|
#include "../../landscape_cmd.h"
|
|
|
|
#include "../../road_cmd.h"
|
|
|
|
#include "../../tunnelbridge_cmd.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptTunnel::IsTunnelTile(TileIndex tile)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!::IsValidTile(tile)) return false;
|
|
|
|
return ::IsTunnelTile(tile);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ TileIndex ScriptTunnel::GetOtherTunnelEnd(TileIndex tile)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!::IsValidTile(tile)) return INVALID_TILE;
|
|
|
|
|
2010-04-12 14:12:47 +00:00
|
|
|
/* If it's a tunnel already, take the easy way out! */
|
2009-01-12 17:11:45 +00:00
|
|
|
if (IsTunnelTile(tile)) return ::GetOtherTunnelEnd(tile);
|
|
|
|
|
2011-11-04 11:30:37 +00:00
|
|
|
int start_z;
|
2011-11-04 10:25:33 +00:00
|
|
|
Slope start_tileh = ::GetTileSlope(tile, &start_z);
|
2009-05-06 21:38:59 +00:00
|
|
|
DiagDirection direction = ::GetInclinedSlopeDirection(start_tileh);
|
|
|
|
if (direction == INVALID_DIAGDIR) return INVALID_TILE;
|
|
|
|
|
|
|
|
TileIndexDiff delta = ::TileOffsByDiagDir(direction);
|
2011-11-04 11:30:37 +00:00
|
|
|
int end_z;
|
2009-05-06 21:38:59 +00:00
|
|
|
do {
|
|
|
|
tile += delta;
|
|
|
|
if (!::IsValidTile(tile)) return INVALID_TILE;
|
|
|
|
|
2011-11-04 10:25:33 +00:00
|
|
|
::GetTileSlope(tile, &end_z);
|
2009-05-06 21:38:59 +00:00
|
|
|
} while (start_z != end_z);
|
|
|
|
|
|
|
|
return tile;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-05-01 09:24:19 +00:00
|
|
|
/**
|
|
|
|
* Helper function to connect a just built tunnel to nearby roads.
|
2012-03-04 16:54:12 +00:00
|
|
|
* @param instance The script instance we have to built the road for.
|
2011-05-01 09:24:19 +00:00
|
|
|
*/
|
2011-11-29 23:21:33 +00:00
|
|
|
static void _DoCommandReturnBuildTunnel2(class ScriptInstance *instance)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
if (!ScriptTunnel::_BuildTunnelRoad2()) {
|
2011-11-29 23:21:33 +00:00
|
|
|
ScriptInstance::DoCommandReturn(instance);
|
2009-01-12 17:11:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This can never happen, as in test-mode this callback is never executed,
|
|
|
|
* and in execute-mode, the other callback is called. */
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2011-05-01 09:24:19 +00:00
|
|
|
/**
|
|
|
|
* Helper function to connect a just built tunnel to nearby roads.
|
2012-03-04 16:54:12 +00:00
|
|
|
* @param instance The script instance we have to built the road for.
|
2011-05-01 09:24:19 +00:00
|
|
|
*/
|
2011-11-29 23:21:33 +00:00
|
|
|
static void _DoCommandReturnBuildTunnel1(class ScriptInstance *instance)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
if (!ScriptTunnel::_BuildTunnelRoad1()) {
|
2011-11-29 23:21:33 +00:00
|
|
|
ScriptInstance::DoCommandReturn(instance);
|
2009-01-12 17:11:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This can never happen, as in test-mode this callback is never executed,
|
|
|
|
* and in execute-mode, the other callback is called. */
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptTunnel::BuildTunnel(ScriptVehicle::VehicleType vehicle_type, TileIndex start)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
EnforcePrecondition(false, ::IsValidTile(start));
|
2011-11-29 23:15:35 +00:00
|
|
|
EnforcePrecondition(false, vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_ROAD);
|
|
|
|
EnforcePrecondition(false, vehicle_type != ScriptVehicle::VT_RAIL || ScriptRail::IsRailTypeAvailable(ScriptRail::GetCurrentRailType()));
|
2013-12-08 15:44:09 +00:00
|
|
|
EnforcePrecondition(false, vehicle_type != ScriptVehicle::VT_ROAD || ScriptRoad::IsRoadTypeAvailable(ScriptRoad::GetCurrentRoadType()));
|
2011-12-19 21:05:25 +00:00
|
|
|
EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY || vehicle_type == ScriptVehicle::VT_ROAD);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
if (vehicle_type == ScriptVehicle::VT_RAIL) {
|
2021-11-14 23:03:01 +00:00
|
|
|
/* For rail we do nothing special */
|
|
|
|
return ScriptObject::Command<CMD_BUILD_TUNNEL>::Do(start, TRANSPORT_RAIL, ScriptRail::GetCurrentRailType());
|
|
|
|
} else {
|
|
|
|
ScriptObject::SetCallbackVariable(0, start);
|
|
|
|
return ScriptObject::Command<CMD_BUILD_TUNNEL>::Do(&::_DoCommandReturnBuildTunnel1, start, TRANSPORT_ROAD, ScriptRoad::GetCurrentRoadType());
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptTunnel::_BuildTunnelRoad1()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
/* Build the piece of road on the 'start' side of the tunnel */
|
2011-11-29 23:15:35 +00:00
|
|
|
TileIndex end = ScriptObject::GetCallbackVariable(0);
|
|
|
|
TileIndex start = ScriptTunnel::GetOtherTunnelEnd(end);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-03-21 21:43:23 +00:00
|
|
|
DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
|
2009-01-12 17:11:45 +00:00
|
|
|
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
|
|
|
|
|
2021-11-14 23:03:01 +00:00
|
|
|
return ScriptObject::Command<CMD_BUILD_ROAD>::Do(&::_DoCommandReturnBuildTunnel2, start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2), ScriptRoad::GetRoadType(), DRD_NONE, 0);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptTunnel::_BuildTunnelRoad2()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
/* Build the piece of road on the 'end' side of the tunnel */
|
2011-11-29 23:15:35 +00:00
|
|
|
TileIndex end = ScriptObject::GetCallbackVariable(0);
|
|
|
|
TileIndex start = ScriptTunnel::GetOtherTunnelEnd(end);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-03-21 21:43:23 +00:00
|
|
|
DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
|
2009-01-12 17:11:45 +00:00
|
|
|
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
|
|
|
|
|
2021-11-14 23:03:01 +00:00
|
|
|
return ScriptObject::Command<CMD_BUILD_ROAD>::Do(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1), ScriptRoad::GetRoadType(), DRD_NONE, 0);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptTunnel::RemoveTunnel(TileIndex tile)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-12-19 21:05:36 +00:00
|
|
|
EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
|
2009-01-12 17:11:45 +00:00
|
|
|
EnforcePrecondition(false, IsTunnelTile(tile));
|
|
|
|
|
2021-11-21 22:02:29 +00:00
|
|
|
return ScriptObject::Command<CMD_LANDSCAPE_CLEAR>::Do(tile);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|