Files
@ r28025:9116d6e411b0
Branch filter:
Location: cpp/openttd-patchpack/source/src/script/api/script_bridge.cpp - annotation
r28025:9116d6e411b0
6.8 KiB
text/x-c
Codechange: replace x.size() == 0 with x.empty()
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18501:8e3d905ea4bc r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18515:6bcd41f64d33 r18500:14a481bc4437 r18500:14a481bc4437 r26105:23588534dec9 r26105:23588534dec9 r26105:23588534dec9 r27148:4e041ae27b9d r23249:0dd80ccaa0af r18500:14a481bc4437 r21383:942c32fb8b0e r21383:942c32fb8b0e r18501:8e3d905ea4bc r18500:14a481bc4437 r27148:4e041ae27b9d r18500:14a481bc4437 r18500:14a481bc4437 r18501:8e3d905ea4bc r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18501:8e3d905ea4bc r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r19133:3165ed8baa58 r18500:14a481bc4437 r18506:b0c2bbd32450 r18500:14a481bc4437 r18501:8e3d905ea4bc r18506:b0c2bbd32450 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r19133:3165ed8baa58 r18500:14a481bc4437 r18506:b0c2bbd32450 r18500:14a481bc4437 r18501:8e3d905ea4bc r18506:b0c2bbd32450 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18501:8e3d905ea4bc r18500:14a481bc4437 r27005:8631fe151182 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18501:8e3d905ea4bc r18501:8e3d905ea4bc r21067:d63e3d1dfbf8 r26996:7064173a70ca r18500:14a481bc4437 r18500:14a481bc4437 r18501:8e3d905ea4bc r27799:ab7d36d10264 r27799:ab7d36d10264 r26114:91e053fa2e46 r18501:8e3d905ea4bc r26114:91e053fa2e46 r18501:8e3d905ea4bc r26114:91e053fa2e46 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18501:8e3d905ea4bc r18500:14a481bc4437 r27005:8631fe151182 r27005:8631fe151182 r18500:14a481bc4437 r18501:8e3d905ea4bc r18501:8e3d905ea4bc r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r26114:91e053fa2e46 r18500:14a481bc4437 r18500:14a481bc4437 r18501:8e3d905ea4bc r18500:14a481bc4437 r27005:8631fe151182 r27005:8631fe151182 r18500:14a481bc4437 r18501:8e3d905ea4bc r18501:8e3d905ea4bc r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r26114:91e053fa2e46 r18500:14a481bc4437 r18500:14a481bc4437 r18501:8e3d905ea4bc r18500:14a481bc4437 r26996:7064173a70ca r18500:14a481bc4437 r26118:2b515956558b r18500:14a481bc4437 r18500:14a481bc4437 r27364:db69a5f5c496 r18500:14a481bc4437 r27364:db69a5f5c496 r27364:db69a5f5c496 r18500:14a481bc4437 r23249:0dd80ccaa0af r18500:14a481bc4437 r18500:14a481bc4437 r26962:1a225cbe6c5b r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r26962:1a225cbe6c5b r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r26962:1a225cbe6c5b r26962:1a225cbe6c5b r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r26962:1a225cbe6c5b r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r26962:1a225cbe6c5b r18500:14a481bc4437 r18500:14a481bc4437 r26962:1a225cbe6c5b r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r26962:1a225cbe6c5b r18500:14a481bc4437 r18500:14a481bc4437 r18501:8e3d905ea4bc r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 r18500:14a481bc4437 | /*
* 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 script_bridge.cpp Implementation of ScriptBridge. */
#include "../../stdafx.h"
#include "script_bridge.hpp"
#include "script_rail.hpp"
#include "../script_instance.hpp"
#include "../../bridge_map.h"
#include "../../strings_func.h"
#include "../../landscape_cmd.h"
#include "../../road_cmd.h"
#include "../../tunnelbridge_cmd.h"
#include "../../timer/timer_game_calendar.h"
#include "table/strings.h"
#include "../../safeguards.h"
/* static */ bool ScriptBridge::IsValidBridge(BridgeID bridge_id)
{
return bridge_id < MAX_BRIDGES && ::GetBridgeSpec(bridge_id)->avail_year <= TimerGameCalendar::year;
}
/* static */ bool ScriptBridge::IsBridgeTile(TileIndex tile)
{
if (!::IsValidTile(tile)) return false;
return ::IsBridgeTile(tile);
}
/* static */ BridgeID ScriptBridge::GetBridgeID(TileIndex tile)
{
if (!IsBridgeTile(tile)) return (BridgeID)-1;
return (BridgeID)::GetBridgeType(tile);
}
/**
* Helper function to connect a just built bridge to nearby roads.
* @param instance The script instance we have to built the road for.
*/
static void _DoCommandReturnBuildBridge2(class ScriptInstance *instance)
{
if (!ScriptBridge::_BuildBridgeRoad2()) {
ScriptInstance::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 bridge to nearby roads.
* @param instance The script instance we have to built the road for.
*/
static void _DoCommandReturnBuildBridge1(class ScriptInstance *instance)
{
if (!ScriptBridge::_BuildBridgeRoad1()) {
ScriptInstance::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 ScriptBridge::BuildBridge(ScriptVehicle::VehicleType vehicle_type, BridgeID bridge_id, TileIndex start, TileIndex end)
{
EnforceDeityOrCompanyModeValid(false);
EnforcePrecondition(false, start != end);
EnforcePrecondition(false, ::IsValidTile(start) && ::IsValidTile(end));
EnforcePrecondition(false, TileX(start) == TileX(end) || TileY(start) == TileY(end));
EnforcePrecondition(false, vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER);
EnforcePrecondition(false, vehicle_type != ScriptVehicle::VT_RAIL || ScriptRail::IsRailTypeAvailable(ScriptRail::GetCurrentRailType()));
EnforcePrecondition(false, vehicle_type != ScriptVehicle::VT_ROAD || ScriptRoad::IsRoadTypeAvailable(ScriptRoad::GetCurrentRoadType()));
EnforcePrecondition(false, ScriptCompanyMode::IsValid() || vehicle_type == ScriptVehicle::VT_ROAD);
switch (vehicle_type) {
case ScriptVehicle::VT_ROAD:
ScriptObject::SetCallbackVariable(0, static_cast<uint32_t>(start));
ScriptObject::SetCallbackVariable(1, static_cast<uint32_t>(end));
return ScriptObject::Command<CMD_BUILD_BRIDGE>::Do(&::_DoCommandReturnBuildBridge1, end, start, TRANSPORT_ROAD, bridge_id, ScriptRoad::GetCurrentRoadType());
case ScriptVehicle::VT_RAIL:
return ScriptObject::Command<CMD_BUILD_BRIDGE>::Do(end, start, TRANSPORT_RAIL, bridge_id, ScriptRail::GetCurrentRailType());
case ScriptVehicle::VT_WATER:
return ScriptObject::Command<CMD_BUILD_BRIDGE>::Do(end, start, TRANSPORT_WATER, bridge_id, 0);
default: NOT_REACHED();
}
}
/* static */ bool ScriptBridge::_BuildBridgeRoad1()
{
EnforceDeityOrCompanyModeValid(false);
/* Build the piece of road on the 'start' side of the bridge */
TileIndex end = ScriptObject::GetCallbackVariable(0);
TileIndex start = ScriptObject::GetCallbackVariable(1);
DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
return ScriptObject::Command<CMD_BUILD_ROAD>::Do(&::_DoCommandReturnBuildBridge2, start + ::TileOffsByDiagDir(dir_1), ::DiagDirToRoadBits(dir_2), (::RoadType)ScriptRoad::GetCurrentRoadType(), DRD_NONE, 0);
}
/* static */ bool ScriptBridge::_BuildBridgeRoad2()
{
EnforceDeityOrCompanyModeValid(false);
/* Build the piece of road on the 'end' side of the bridge */
TileIndex end = ScriptObject::GetCallbackVariable(0);
TileIndex start = ScriptObject::GetCallbackVariable(1);
DiagDirection dir_1 = ::DiagdirBetweenTiles(end, start);
DiagDirection dir_2 = ::ReverseDiagDir(dir_1);
return ScriptObject::Command<CMD_BUILD_ROAD>::Do(end + ::TileOffsByDiagDir(dir_2), ::DiagDirToRoadBits(dir_1), (::RoadType)ScriptRoad::GetCurrentRoadType(), DRD_NONE, 0);
}
/* static */ bool ScriptBridge::RemoveBridge(TileIndex tile)
{
EnforceCompanyModeValid(false);
EnforcePrecondition(false, IsBridgeTile(tile));
return ScriptObject::Command<CMD_LANDSCAPE_CLEAR>::Do(tile);
}
/* static */ std::optional<std::string> ScriptBridge::GetName(BridgeID bridge_id, ScriptVehicle::VehicleType vehicle_type)
{
EnforcePrecondition(std::nullopt, vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER);
if (!IsValidBridge(bridge_id)) return std::nullopt;
return GetString(vehicle_type == ScriptVehicle::VT_WATER ? STR_LAI_BRIDGE_DESCRIPTION_AQUEDUCT : ::GetBridgeSpec(bridge_id)->transport_name[vehicle_type]);
}
/* static */ SQInteger ScriptBridge::GetMaxSpeed(BridgeID bridge_id)
{
if (!IsValidBridge(bridge_id)) return -1;
return ::GetBridgeSpec(bridge_id)->speed; // km-ish/h
}
/* static */ Money ScriptBridge::GetPrice(BridgeID bridge_id, SQInteger length)
{
if (!IsValidBridge(bridge_id)) return -1;
length = Clamp<SQInteger>(length, 0, INT32_MAX);
return ::CalcBridgeLenCostFactor(length) * _price[PR_BUILD_BRIDGE] * ::GetBridgeSpec(bridge_id)->price >> 8;
}
/* static */ SQInteger ScriptBridge::GetMaxLength(BridgeID bridge_id)
{
if (!IsValidBridge(bridge_id)) return -1;
return std::min<SQInteger>(::GetBridgeSpec(bridge_id)->max_length, _settings_game.construction.max_bridge_length) + 2;
}
/* static */ SQInteger ScriptBridge::GetMinLength(BridgeID bridge_id)
{
if (!IsValidBridge(bridge_id)) return -1;
return static_cast<SQInteger>(::GetBridgeSpec(bridge_id)->min_length) + 2;
}
/* static */ TileIndex ScriptBridge::GetOtherBridgeEnd(TileIndex tile)
{
if (!::IsValidTile(tile)) return INVALID_TILE;
if (!IsBridgeTile(tile)) return INVALID_TILE;
return ::GetOtherBridgeEnd(tile);
}
|