Files
@ r18616:9c795bbf7d6f
Branch filter:
Location: cpp/openttd-patchpack/source/src/cargopacket.h - annotation
r18616:9c795bbf7d6f
9.7 KiB
text/x-c
(svn r23473) -Codechange: refactor the error message data into a separate structure
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r9111:983de9c5a848 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r11969:3526c3cc75dc r8119:8fdb3a371896 r8769:6c1f27d01bf5 r12648:24c912bd53f6 r13217:a7416accbcb8 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r13210:6ef3c7aaa995 r7380:61767264770c r7380:61767264770c r7380:61767264770c r15882:30fbd9be7c68 r17360:71e102cec3ce r17101:93f76317eea0 r11967:df0600d2c7e7 r7380:61767264770c r13281:e10563871a55 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r7010:fd8e3ec6f727 r17101:93f76317eea0 r7010:fd8e3ec6f727 r11967:df0600d2c7e7 r13203:2e49af5aeba4 r17101:93f76317eea0 r17101:93f76317eea0 r17101:93f76317eea0 r17101:93f76317eea0 r17101:93f76317eea0 r17101:93f76317eea0 r17101:93f76317eea0 r17101:93f76317eea0 r7010:fd8e3ec6f727 r13203:2e49af5aeba4 r13281:e10563871a55 r13276:3604b09589ee r13276:3604b09589ee r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13204:037e2fceda8f r13204:037e2fceda8f r13203:2e49af5aeba4 r13317:40d17c38b14e r13317:40d17c38b14e r13288:93fbc02f05b0 r13203:2e49af5aeba4 r17101:93f76317eea0 r11967:df0600d2c7e7 r7010:fd8e3ec6f727 r17102:d51196d5afa1 r17102:d51196d5afa1 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r17102:d51196d5afa1 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r17101:93f76317eea0 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r17692:fd74534e7c5b r13203:2e49af5aeba4 r17101:93f76317eea0 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13203:2e49af5aeba4 r13289:26a0ed664dc0 r17101:93f76317eea0 r17101:93f76317eea0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r17101:93f76317eea0 r17101:93f76317eea0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r17101:93f76317eea0 r17101:93f76317eea0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r17101:93f76317eea0 r17101:93f76317eea0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r17101:93f76317eea0 r17101:93f76317eea0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13289:26a0ed664dc0 r13203:2e49af5aeba4 r12648:24c912bd53f6 r13285:765ad409980c r13287:52547c48c3ba r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r17101:93f76317eea0 r17101:93f76317eea0 r17101:93f76317eea0 r7010:fd8e3ec6f727 r11966:921bea0ca112 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r17101:93f76317eea0 r17101:93f76317eea0 r7010:fd8e3ec6f727 r11966:921bea0ca112 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r17101:93f76317eea0 r17101:93f76317eea0 r7010:fd8e3ec6f727 r13281:e10563871a55 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r17101:93f76317eea0 r7010:fd8e3ec6f727 r17101:93f76317eea0 r13294:1c7a8cd8e77d r17101:93f76317eea0 r13294:1c7a8cd8e77d r7010:fd8e3ec6f727 r17101:93f76317eea0 r7010:fd8e3ec6f727 r17101:93f76317eea0 r17101:93f76317eea0 r17101:93f76317eea0 r17101:93f76317eea0 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r13276:3604b09589ee r17101:93f76317eea0 r17101:93f76317eea0 r7010:fd8e3ec6f727 r17101:93f76317eea0 r13215:922f97d8d59c r13218:a349a5df045c r17101:93f76317eea0 r13218:a349a5df045c r13218:a349a5df045c r7010:fd8e3ec6f727 r17101:93f76317eea0 r13295:dd56059817bc r17101:93f76317eea0 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r17355:e67faa335e9c r17355:e67faa335e9c r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r17101:93f76317eea0 r7010:fd8e3ec6f727 r13281:e10563871a55 r13210:6ef3c7aaa995 r13210:6ef3c7aaa995 r13210:6ef3c7aaa995 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r17101:93f76317eea0 r17101:93f76317eea0 r7010:fd8e3ec6f727 r13210:6ef3c7aaa995 r13210:6ef3c7aaa995 r13214:4d6bd80fcf2a r13210:6ef3c7aaa995 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r17101:93f76317eea0 r17101:93f76317eea0 r7010:fd8e3ec6f727 r13210:6ef3c7aaa995 r13210:6ef3c7aaa995 r13210:6ef3c7aaa995 r13210:6ef3c7aaa995 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r17101:93f76317eea0 r17101:93f76317eea0 r7010:fd8e3ec6f727 r13210:6ef3c7aaa995 r13210:6ef3c7aaa995 r13214:4d6bd80fcf2a r13210:6ef3c7aaa995 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r17101:93f76317eea0 r17101:93f76317eea0 r7010:fd8e3ec6f727 r13210:6ef3c7aaa995 r13210:6ef3c7aaa995 r13216:2c9a757db2cc r13210:6ef3c7aaa995 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r13219:41299110cd66 r7010:fd8e3ec6f727 r13281:e10563871a55 r13290:9954cbdc2063 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r7010:fd8e3ec6f727 r13276:3604b09589ee r13276:3604b09589ee r13276:3604b09589ee r13281:e10563871a55 r13293:58ebfc85fa07 r17101:93f76317eea0 r13293:58ebfc85fa07 r13293:58ebfc85fa07 r17101:93f76317eea0 r13293:58ebfc85fa07 r13293:58ebfc85fa07 r13293:58ebfc85fa07 r13293:58ebfc85fa07 r13276:3604b09589ee r17101:93f76317eea0 r13293:58ebfc85fa07 r13276:3604b09589ee r13276:3604b09589ee r13276:3604b09589ee r13276:3604b09589ee r17101:93f76317eea0 r17101:93f76317eea0 r13293:58ebfc85fa07 r13293:58ebfc85fa07 r13293:58ebfc85fa07 r13293:58ebfc85fa07 r13293:58ebfc85fa07 r13293:58ebfc85fa07 r13276:3604b09589ee r13282:5abfbbdbe99a r13293:58ebfc85fa07 r13293:58ebfc85fa07 r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r17101:93f76317eea0 r17101:93f76317eea0 r17101:93f76317eea0 r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13276:3604b09589ee r13276:3604b09589ee r13276:3604b09589ee r13276:3604b09589ee r13276:3604b09589ee r13281:e10563871a55 r13276:3604b09589ee r17101:93f76317eea0 r13293:58ebfc85fa07 r13276:3604b09589ee r13276:3604b09589ee r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r17101:93f76317eea0 r17101:93f76317eea0 r17101:93f76317eea0 r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13282:5abfbbdbe99a r13276:3604b09589ee r13276:3604b09589ee r7010:fd8e3ec6f727 | /* $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 cargopacket.h Base class for cargo packets. */
#ifndef CARGOPACKET_H
#define CARGOPACKET_H
#include "core/pool_type.hpp"
#include "economy_type.h"
#include "station_type.h"
#include "cargo_type.h"
#include "vehicle_type.h"
#include <list>
/** Unique identifier for a single cargo packet. */
typedef uint32 CargoPacketID;
struct CargoPacket;
/** Type of the pool for cargo packets for a little over 16 million packets. */
typedef Pool<CargoPacket, CargoPacketID, 1024, 0xFFF000, PT_NORMAL, true, false> CargoPacketPool;
/** The actual pool with cargo packets. */
extern CargoPacketPool _cargopacket_pool;
template <class Tinst> class CargoList;
extern const struct SaveLoad *GetCargoPacketDesc();
/**
* Container for cargo from the same location and time.
*/
struct CargoPacket : CargoPacketPool::PoolItem<&_cargopacket_pool> {
private:
Money feeder_share; ///< Value of feeder pickup to be paid for on delivery of cargo.
uint16 count; ///< The amount of cargo in this packet.
byte days_in_transit; ///< Amount of days this packet has been in transit.
SourceTypeByte source_type; ///< Type of \c source_id.
SourceID source_id; ///< Index of source, INVALID_SOURCE if unknown/invalid.
StationID source; ///< The station where the cargo came from first.
TileIndex source_xy; ///< The origin of the cargo (first station in feeder chain).
TileIndex loaded_at_xy; ///< Location where this cargo has been loaded into the vehicle.
/** The CargoList caches, thus needs to know about it. */
template <class Tinst> friend class CargoList;
friend class VehicleCargoList;
friend class StationCargoList;
/** We want this to be saved, right? */
friend const struct SaveLoad *GetCargoPacketDesc();
public:
/** Maximum number of items in a single cargo packet. */
static const uint16 MAX_COUNT = UINT16_MAX;
CargoPacket();
CargoPacket(StationID source, TileIndex source_xy, uint16 count, SourceType source_type, SourceID source_id);
CargoPacket(uint16 count, byte days_in_transit, StationID source, TileIndex source_xy, TileIndex loaded_at_xy, Money feeder_share = 0, SourceType source_type = ST_INDUSTRY, SourceID source_id = INVALID_SOURCE);
/** Destroy the packet. */
~CargoPacket() { }
CargoPacket *Split(uint new_size);
void Merge(CargoPacket *cp);
/**
* Gets the number of 'items' in this packet.
* @return Item count.
*/
FORCEINLINE uint16 Count() const
{
return this->count;
}
/**
* Gets the amount of money already paid to earlier vehicles in
* the feeder chain.
* @return Feeder share.
*/
FORCEINLINE Money FeederShare() const
{
return this->feeder_share;
}
/**
* Gets the number of days this cargo has been in transit.
* This number isn't really in days, but in 2.5 days (CARGO_AGING_TICKS = 185 ticks) and
* it is capped at 255.
* @return Length this cargo has been in transit.
*/
FORCEINLINE byte DaysInTransit() const
{
return this->days_in_transit;
}
/**
* Gets the type of the cargo's source. industry, town or head quarter.
* @return Source type.
*/
FORCEINLINE SourceType SourceSubsidyType() const
{
return this->source_type;
}
/**
* Gets the ID of the cargo's source. An IndustryID, TownID or CompanyID.
* @return Source ID.
*/
FORCEINLINE SourceID SourceSubsidyID() const
{
return this->source_id;
}
/**
* Gets the ID of the station where the cargo was loaded for the first time.
* @return StationID.
*/
FORCEINLINE SourceID SourceStation() const
{
return this->source;
}
/**
* Gets the coordinates of the cargo's source station.
* @return Source station's coordinates.
*/
FORCEINLINE TileIndex SourceStationXY() const
{
return this->source_xy;
}
/**
* Gets the coordinates of the cargo's last loading station.
* @return Last loading station's coordinates.
*/
FORCEINLINE TileIndex LoadedAtXY() const
{
return this->loaded_at_xy;
}
static void InvalidateAllFrom(SourceType src_type, SourceID src);
static void InvalidateAllFrom(StationID sid);
static void AfterLoad();
};
/**
* Iterate over all _valid_ cargo packets from the given start.
* @param var Variable used as "iterator".
* @param start Cargo packet ID of the first packet to iterate over.
*/
#define FOR_ALL_CARGOPACKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(CargoPacket, cargopacket_index, var, start)
/**
* Iterate over all _valid_ cargo packets from the begin of the pool.
* @param var Variable used as "iterator".
*/
#define FOR_ALL_CARGOPACKETS(var) FOR_ALL_CARGOPACKETS_FROM(var, 0)
/**
* Simple collection class for a list of cargo packets.
* @tparam Tinst Actual instantation of this cargo list.
*/
template <class Tinst>
class CargoList {
public:
/** Container with cargo packets. */
typedef std::list<CargoPacket *> List;
/** The iterator for our container. */
typedef List::iterator Iterator;
/** The const iterator for our container. */
typedef List::const_iterator ConstIterator;
/** Kind of actions that could be done with packets on move. */
enum MoveToAction {
MTA_FINAL_DELIVERY, ///< "Deliver" the packet to the final destination, i.e. destroy the packet.
MTA_CARGO_LOAD, ///< Load the packet onto a vehicle, i.e. set the last loaded station ID.
MTA_TRANSFER, ///< The cargo is moved as part of a transfer.
MTA_UNLOAD, ///< The cargo is moved as part of a forced unload.
};
protected:
uint count; ///< Cache for the number of cargo entities.
uint cargo_days_in_transit; ///< Cache for the sum of number of days in transit of each entity; comparable to man-hours.
List packets; ///< The cargo packets in this list.
void AddToCache(const CargoPacket *cp);
void RemoveFromCache(const CargoPacket *cp);
public:
/** Create the cargo list. */
CargoList() {}
~CargoList();
void OnCleanPool();
/**
* Returns a pointer to the cargo packet list (so you can iterate over it etc).
* @return Pointer to the packet list.
*/
FORCEINLINE const List *Packets() const
{
return &this->packets;
}
/**
* Checks whether this list is empty.
* @return True if and only if the list is empty.
*/
FORCEINLINE bool Empty() const
{
return this->count == 0;
}
/**
* Returns the number of cargo entities in this list.
* @return The before mentioned number.
*/
FORCEINLINE uint Count() const
{
return this->count;
}
/**
* Returns source of the first cargo packet in this list.
* @return The before mentioned source.
*/
FORCEINLINE StationID Source() const
{
return this->Empty() ? INVALID_STATION : this->packets.front()->source;
}
/**
* Returns average number of days in transit for a cargo entity.
* @return The before mentioned number.
*/
FORCEINLINE uint DaysInTransit() const
{
return this->count == 0 ? 0 : this->cargo_days_in_transit / this->count;
}
void Append(CargoPacket *cp);
void Truncate(uint max_remaining);
template <class Tother_inst>
bool MoveTo(Tother_inst *dest, uint count, MoveToAction mta, CargoPayment *payment, uint data = 0);
void InvalidateCache();
};
/**
* CargoList that is used for vehicles.
*/
class VehicleCargoList : public CargoList<VehicleCargoList> {
protected:
/** The (direct) parent of this class. */
typedef CargoList<VehicleCargoList> Parent;
Money feeder_share; ///< Cache for the feeder share.
void AddToCache(const CargoPacket *cp);
void RemoveFromCache(const CargoPacket *cp);
public:
/** The super class ought to know what it's doing. */
friend class CargoList<VehicleCargoList>;
/** The vehicles have a cargo list (and we want that saved). */
friend const struct SaveLoad *GetVehicleDescription(VehicleType vt);
/**
* Returns total sum of the feeder share for all packets.
* @return The before mentioned number.
*/
FORCEINLINE Money FeederShare() const
{
return this->feeder_share;
}
void AgeCargo();
void InvalidateCache();
/**
* Are two the two CargoPackets mergeable in the context of
* a list of CargoPackets for a Vehicle?
* @param cp1 First CargoPacket.
* @param cp2 Second CargoPacket.
* @return True if they are mergeable.
*/
static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
{
return cp1->source_xy == cp2->source_xy &&
cp1->days_in_transit == cp2->days_in_transit &&
cp1->source_type == cp2->source_type &&
cp1->source_id == cp2->source_id &&
cp1->loaded_at_xy == cp2->loaded_at_xy;
}
};
/**
* CargoList that is used for stations.
*/
class StationCargoList : public CargoList<StationCargoList> {
public:
/** The super class ought to know what it's doing. */
friend class CargoList<StationCargoList>;
/** The stations, via GoodsEntry, have a CargoList. */
friend const struct SaveLoad *GetGoodsDesc();
/**
* Are two the two CargoPackets mergeable in the context of
* a list of CargoPackets for a Vehicle?
* @param cp1 First CargoPacket.
* @param cp2 Second CargoPacket.
* @return True if they are mergeable.
*/
static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
{
return cp1->source_xy == cp2->source_xy &&
cp1->days_in_transit == cp2->days_in_transit &&
cp1->source_type == cp2->source_type &&
cp1->source_id == cp2->source_id;
}
};
#endif /* CARGOPACKET_H */
|