Files
@ r18358:e21717cc4b4c
Branch filter:
Location: cpp/openttd-patchpack/source/src/ai/api/ai_tunnel.cpp
r18358:e21717cc4b4c
4.5 KiB
text/x-c
(svn r23204) -Fix (r23201): if you rename a constant, then also rename it in the helper scripts that use it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | /* $Id$ */
/*
* 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/>.
*/
/** @file ai_tunnel.cpp Implementation of AITunnel. */
#include "../../stdafx.h"
#include "ai_tunnel.hpp"
#include "ai_rail.hpp"
#include "../ai_instance.hpp"
#include "../../tunnel_map.h"
#include "../../command_func.h"
/* static */ bool AITunnel::IsTunnelTile(TileIndex tile)
{
if (!::IsValidTile(tile)) return false;
return ::IsTunnelTile(tile);
}
/* static */ TileIndex AITunnel::GetOtherTunnelEnd(TileIndex tile)
{
if (!::IsValidTile(tile)) return INVALID_TILE;
/* If it's a tunnel already, take the easy way out! */
if (IsTunnelTile(tile)) return ::GetOtherTunnelEnd(tile);
int start_z;
Slope start_tileh = ::GetTileSlope(tile, &start_z);
DiagDirection direction = ::GetInclinedSlopeDirection(start_tileh);
if (direction == INVALID_DIAGDIR) return INVALID_TILE;
TileIndexDiff delta = ::TileOffsByDiagDir(direction);
int end_z;
do {
tile += delta;
if (!::IsValidTile(tile)) return INVALID_TILE;
::GetTileSlope(tile, &end_z);
} while (start_z != end_z);
return tile;
}
/**
* Helper function to connect a just built tunnel to nearby roads.
* @param instance The AI we have to built the road for.
*/
static void _DoCommandReturnBuildTunnel2(class AIInstance *instance)
{
if (!AITunnel::_BuildTunnelRoad2()) {
AIInstance::DoCommandReturn(instance);
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();
}
/**
* Helper function to connect a just built tunnel to nearby roads.
* @param instance The AI we have to built the road for.
*/
static void _DoCommandReturnBuildTunnel1(class AIInstance *instance)
{
if (!AITunnel::_BuildTunnelRoad1()) {
AIInstance::DoCommandReturn(instance);
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();
}
/* static */ bool AITunnel::BuildTunnel(AIVehicle::VehicleType vehicle_type, TileIndex start)
{
EnforcePrecondition(false, ::IsValidTile(start));
EnforcePrecondition(false, vehicle_type == AIVehicle::VT_RAIL || vehicle_type == AIVehicle::VT_ROAD);
EnforcePrecondition(false, vehicle_type != AIVehicle::VT_RAIL || AIRail::IsRailTypeAvailable(AIRail::GetCurrentRailType()));
uint type = 0;
if (vehicle_type == AIVehicle::VT_ROAD) {
type |= (TRANSPORT_ROAD << 8);
type |= ::RoadTypeToRoadTypes((::RoadType)AIObject::GetRoadType());
} else {
type |= (TRANSPORT_RAIL << 8);
type |= AIRail::GetCurrentRailType();
}
/* For rail we do nothing special */
if (vehicle_type == AIVehicle::VT_RAIL) {
return AIObject::DoCommand(start, type, 0, CMD_BUILD_TUNNEL);
}
AIObject::SetCallbackVariable(0, start);
return AIObject::DoCommand(start, type, 0, CMD_BUILD_TUNNEL, NULL, &::_DoCommandReturnBuildTunnel1);
}
/* static */ bool AITunnel::_BuildTunnelRoad1()
{
/* Build the piece of road on the 'start' side of the tunnel */
TileIndex end = AIObject::GetCallbackVariable(0);
TileIndex start = AITunnel::GetOtherTunnelEnd(end);
DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
return AIObject::DoCommand(start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD, NULL, &::_DoCommandReturnBuildTunnel2);
}
/* static */ bool AITunnel::_BuildTunnelRoad2()
{
/* Build the piece of road on the 'end' side of the tunnel */
TileIndex end = AIObject::GetCallbackVariable(0);
TileIndex start = AITunnel::GetOtherTunnelEnd(end);
DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
return AIObject::DoCommand(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1) | (AIObject::GetRoadType() << 4), 0, CMD_BUILD_ROAD);
}
/* static */ bool AITunnel::RemoveTunnel(TileIndex tile)
{
EnforcePrecondition(false, IsTunnelTile(tile));
return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
}
|