2009-01-12 17:11:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
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"
|
|
|
|
|
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()));
|
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
|
|
|
|
|
|
|
uint type = 0;
|
2011-11-29 23:15:35 +00:00
|
|
|
if (vehicle_type == ScriptVehicle::VT_ROAD) {
|
2010-04-14 20:28:37 +00:00
|
|
|
type |= (TRANSPORT_ROAD << 8);
|
2011-11-29 23:15:35 +00:00
|
|
|
type |= ::RoadTypeToRoadTypes((::RoadType)ScriptObject::GetRoadType());
|
2009-01-12 17:11:45 +00:00
|
|
|
} else {
|
2010-04-14 20:28:37 +00:00
|
|
|
type |= (TRANSPORT_RAIL << 8);
|
2011-11-29 23:15:35 +00:00
|
|
|
type |= ScriptRail::GetCurrentRailType();
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For rail we do nothing special */
|
2011-11-29 23:15:35 +00:00
|
|
|
if (vehicle_type == ScriptVehicle::VT_RAIL) {
|
|
|
|
return ScriptObject::DoCommand(start, type, 0, CMD_BUILD_TUNNEL);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
ScriptObject::SetCallbackVariable(0, start);
|
|
|
|
return ScriptObject::DoCommand(start, type, 0, CMD_BUILD_TUNNEL, NULL, &::_DoCommandReturnBuildTunnel1);
|
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);
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (ScriptObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &::_DoCommandReturnBuildTunnel2);
|
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);
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (ScriptObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
|
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));
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|