/
depot.cpp
233 lines (199 loc) · 6.67 KB
/
depot.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* 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 depot.cpp Handling of depots. */
#include "stdafx.h"
#include "depot_base.h"
#include "order_backup.h"
#include "order_func.h"
#include "window_func.h"
#include "core/pool_func.hpp"
#include "vehicle_gui.h"
#include "vehiclelist.h"
#include "command_func.h"
#include "vehicle_base.h"
#include "platform_func.h"
#include "safeguards.h"
#include "table/strings.h"
/** All our depots tucked away in a pool. */
DepotPool _depot_pool("Depot");
INSTANTIATE_POOL_METHODS(Depot)
/**
* Clean up a depot
*/
Depot::~Depot()
{
if (CleaningPool()) return;
/* Clear the order backup. */
OrderBackup::Reset(this->index, false);
/* Clear the depot from all order-lists */
RemoveOrderFromAllVehicles(OT_GOTO_DEPOT, this->index);
/* Delete the depot-window */
DeleteWindowById(WC_VEHICLE_DEPOT, this->index);
/* Delete the depot list */
DeleteWindowById(GetWindowClassForVehicleType(this->veh_type),
VehicleListIdentifier(VL_DEPOT_LIST,
this->veh_type, this->company, this->index).Pack());
}
/**
* Of all the depot parts a depot has, return the best destination for a vehicle.
* @param v The vehicle.
* @param dep Depot the vehicle \a v is heading for.
* @return The free and closest (if none is free, just closest) part of depot to vehicle v.
*/
TileIndex Depot::GetBestDepotTile(Vehicle *v) const
{
assert(this->veh_type == v->type);
TileIndex best_depot = INVALID_TILE;
DepotReservation best_found_type = DEPOT_RESERVATION_END;
uint best_distance = UINT_MAX;
for (std::vector<TileIndex>::const_iterator it = this->depot_tiles.begin(); it != this->depot_tiles.end(); ++it) {
TileIndex tile = *it;
DepotReservation depot_reservation = GetDepotReservation(tile);
uint new_distance = DistanceManhattan(v->tile, tile);
if (((best_found_type == depot_reservation) && new_distance < best_distance) || (depot_reservation < best_found_type)) {
best_depot = tile;
best_distance = new_distance;
best_found_type = depot_reservation;
}
}
return best_depot;
}
/* Check we can add some tiles to this depot. */
CommandCost Depot::BeforeAddTiles(TileArea ta)
{
assert(ta.tile != INVALID_TILE);
if (this->ta.tile != INVALID_TILE) {
/* Important when the old rect is completely inside the new rect, resp. the old one was empty. */
ta.Add(this->ta.tile);
ta.Add(TILE_ADDXY(this->ta.tile, this->ta.w - 1, this->ta.h - 1));
}
if ((ta.w > _settings_game.station.station_spread) || (ta.h > _settings_game.station.station_spread)) {
return_cmd_error(STR_ERROR_DEPOT_TOO_SPREAD_OUT);
}
return CommandCost();
}
/* Add some tiles to this depot and rescan area for depot_tiles. */
void Depot::AfterAddRemove(TileArea ta, bool adding)
{
assert(ta.tile != INVALID_TILE);
if (adding) {
if (this->ta.tile != INVALID_TILE) {
ta.Add(this->ta.tile);
ta.Add(TILE_ADDXY(this->ta.tile, this->ta.w - 1, this->ta.h - 1));
}
} else {
ta = this->ta;
}
this->ta.Clear();
TILE_AREA_LOOP(tile, ta) {
if (!IsDepotTile(tile)) continue;
if (GetDepotIndex(tile) != this->index) continue;
this->ta.Add(tile);
}
VehicleType veh_type = this->veh_type;
if (this->ta.tile != INVALID_TILE) {
this->RescanDepotTiles();
assert(this->depot_tiles.size() > 0);
this->xy = this->depot_tiles[0];
InvalidateWindowData(WC_VEHICLE_DEPOT, this->index);
assert(IsDepotTile(this->xy));
} else {
delete this;
}
InvalidateWindowData(WC_SELECT_DEPOT, veh_type);
}
bool IsDepotDestTile(Depot *dep, TileIndex tile)
{
switch (dep->veh_type) {
case VEH_TRAIN:
assert(IsRailDepotTile(tile));
return !IsBigRailDepot(tile) || IsAnyStartPlatformTile(tile);
case VEH_ROAD:
case VEH_SHIP:
case VEH_AIRCRAFT:
return true;
default: NOT_REACHED();
}
}
/* Rescan depot_tiles. Done after AfterAddRemove and SaveLoad. */
void Depot::RescanDepotTiles()
{
this->depot_tiles.clear();
RailTypes old_rail_types = this->r_types.rail_types;
this->r_types.rail_types = RAILTYPES_NONE;
TILE_AREA_LOOP(tile, this->ta) {
if (!IsDepotTile(tile)) continue;
if (GetDepotIndex(tile) != this->index) continue;
if (IsDepotDestTile(this, tile)) this->depot_tiles.push_back(tile);
switch (veh_type) {
case VEH_ROAD:
this->r_types.road_types |= GetPresentRoadTypes(tile);
break;
case VEH_TRAIN:
this->r_types.rail_types |= (RailTypes)(1 << GetRailType(tile));
default: break;
}
}
if (old_rail_types != this->r_types.rail_types) {
InvalidateWindowData(WC_BUILD_VEHICLE, this->index, 0, true);
}
}
DepotReservation GetDepotReservationStatus(TileIndex tile) {
assert(IsBigDepotTile(tile) && IsRoadDepot(tile));
uint num_veh = GetNumDepotVehicles(tile);
switch (num_veh) {
case 0:
return DEPOT_RESERVATION_EMPTY;
default:
assert(num_veh < ROAD_DEPOT_CAPACITY);
return DEPOT_RESERVATION_IN_USE;
case ROAD_DEPOT_CAPACITY:
num_veh = 0;
for (const Vehicle *u : Vehicle::Iterate()) {
if (u->type != VEH_ROAD) continue;
if (!u->IsFrontEngine()) continue;
if (!u->IsInDepot()) continue;
if (u->tile != tile) continue;
if ((u->vehstatus & VS_STOPPED) == 0) continue;
num_veh++;
}
assert(num_veh <= ROAD_DEPOT_CAPACITY);
return num_veh == ROAD_DEPOT_CAPACITY ?
DEPOT_RESERVATION_FULL_STOPPED_VEH : DEPOT_RESERVATION_IN_USE;
}
}
/**
* Fix tile reservations on big depots and vehicle changes.
* @param v Vehicle to be revised.
* @param reserve Whether to reserve or free the position v is occupying.
*/
void SetBigDepotReservation(Vehicle *v, bool reserve)
{
assert(v != nullptr);
assert(IsBigDepotTile(v->tile));
DepotReservation res_type = DEPOT_RESERVATION_EMPTY;
if (reserve && v->type != VEH_ROAD) {
res_type = (v->vehstatus & VS_STOPPED) ?
DEPOT_RESERVATION_FULL_STOPPED_VEH : DEPOT_RESERVATION_IN_USE;
}
switch (v->type) {
case VEH_ROAD:
res_type = GetDepotReservationStatus(v->tile);
SetDepotReservation(v->tile, res_type);
break;
case VEH_SHIP:
SetDepotReservation(v->tile, res_type);
break;
case VEH_TRAIN: {
DiagDirection dir = GetRailDepotDirection(v->tile);
SetDepotReservation(GetPlatformExtremeTile(v->tile, dir), res_type);
SetDepotReservation(GetPlatformExtremeTile(v->tile, ReverseDiagDir(dir)), res_type);
break;
}
default: NOT_REACHED();
}
}