Files
@ r7362:10cac61763f7
Branch filter:
Location: cpp/openttd-patchpack/source/src/cargopacket.cpp
r7362:10cac61763f7
8.4 KiB
text/x-c
(svn r10725) -Codechange: move some window related code out of gfx.cpp to windows.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 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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | /* $Id$ */
/** @file cargopacket.cpp Implementation of the cargo packets */
#include "stdafx.h"
#include "openttd.h"
#include "station.h"
#include "cargopacket.h"
#include "saveload.h"
/** Cache for speeding up lookups in AllocateRaw */
static uint _first_free_cargo_packet_index;
/**
* Called if a new block is added to the station-pool
*/
static void CargoPacketPoolNewBlock(uint cpart_item)
{
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage, this will be removed. */
for (CargoPacket *cp = GetCargoPacket(cpart_item); cp != NULL; cp = (cp->index + 1U < GetCargoPacketPoolSize()) ? GetCargoPacket(cp->index + 1U) : NULL) cp->index = cpart_item++;
}
static void CargoPacketPoolCleanBlock(uint cpart_item, uint end_item)
{
for (uint i = cpart_item; i <= end_item; i++) {
CargoPacket *cp = GetCargoPacket(i);
if (cp->IsValid()) cp->~CargoPacket();
}
}
/* Initialize the cargopacket-pool */
DEFINE_OLD_POOL(CargoPacket, CargoPacket, CargoPacketPoolNewBlock, CargoPacketPoolCleanBlock)
void InitializeCargoPackets()
{
_first_free_cargo_packet_index = 0;
/* Clean the cargo packet pool and create 1 block in it */
CleanPool(&_CargoPacket_pool);
AddBlockToPool(&_CargoPacket_pool);
/* Check whether our &cargolist == &cargolist.packets "hack" works */
CargoList::AssertOnWrongPacketOffset();
}
CargoPacket::CargoPacket(StationID source, uint16 count)
{
if (source != INVALID_STATION) assert(count != 0);
this->source = source;
this->source_xy = (source != INVALID_STATION) ? GetStation(source)->xy : 0;
this->loaded_at_xy = this->source_xy;
this->count = count;
this->days_in_transit = 0;
this->feeder_share = 0;
this->paid_for = false;
}
CargoPacket::~CargoPacket()
{
if (this->index < _first_free_cargo_packet_index) _first_free_cargo_packet_index = this->index;
this->count = 0;
}
bool CargoPacket::SameSource(CargoPacket *cp)
{
return this->source_xy == cp->source_xy && this->days_in_transit == cp->days_in_transit && this->paid_for == cp->paid_for;
}
void *CargoPacket::operator new(size_t size)
{
CargoPacket *cp = AllocateRaw();
return cp;
}
void *CargoPacket::operator new(size_t size, CargoPacket::ID cp_idx)
{
if (!AddBlockIfNeeded(&_CargoPacket_pool, cp_idx))
error("CargoPackets: failed loading savegame: too many cargo packets");
CargoPacket *cp = GetCargoPacket(cp_idx);
return cp;
}
void CargoPacket::operator delete(void *p)
{
}
void CargoPacket::operator delete(void *p, CargoPacket::ID cp_idx)
{
}
/*static*/ CargoPacket *CargoPacket::AllocateRaw()
{
CargoPacket *cp = NULL;
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage, this will be removed. */
for (cp = GetCargoPacket(_first_free_cargo_packet_index); cp != NULL; cp = (cp->index + 1U < GetCargoPacketPoolSize()) ? GetCargoPacket(cp->index + 1U) : NULL) {
if (!cp->IsValid()) {
CargoPacket::ID index = cp->index;
memset(cp, 0, sizeof(CargoPacket));
cp->index = index;
_first_free_cargo_packet_index = cp->index;
return cp;
}
}
/* Check if we can add a block to the pool */
if (AddBlockToPool(&_CargoPacket_pool)) return AllocateRaw();
error("CargoPackets: too many cargo packets");
}
static const SaveLoad _cargopacket_desc[] = {
SLE_VAR(CargoPacket, source, SLE_UINT16),
SLE_VAR(CargoPacket, source_xy, SLE_UINT32),
SLE_VAR(CargoPacket, loaded_at_xy, SLE_UINT32),
SLE_VAR(CargoPacket, count, SLE_UINT16),
SLE_VAR(CargoPacket, days_in_transit, SLE_UINT8),
SLE_VAR(CargoPacket, feeder_share, SLE_INT64),
SLE_VAR(CargoPacket, paid_for, SLE_BOOL),
SLE_END()
};
static void Save_CAPA()
{
CargoPacket *cp;
FOR_ALL_CARGOPACKETS(cp) {
SlSetArrayIndex(cp->index);
SlObject(cp, _cargopacket_desc);
}
}
static void Load_CAPA()
{
int index;
while ((index = SlIterateArray()) != -1) {
if (!AddBlockIfNeeded(&_CargoPacket_pool, index)) {
error("CargoPackets: failed loading savegame: too many cargo packets");
}
CargoPacket *cp = GetCargoPacket(index);
SlObject(cp, _cargopacket_desc);
}
}
extern const ChunkHandler _cargopacket_chunk_handlers[] = {
{ 'CAPA', Save_CAPA, Load_CAPA, CH_ARRAY | CH_LAST},
};
/*
*
* Cargo list implementation
*
*/
/* static */ void CargoList::AssertOnWrongPacketOffset()
{
CargoList cl;
if ((void*)&cl != (void*)cl.Packets()) NOT_REACHED();
}
CargoList::~CargoList()
{
while (!packets.empty()) {
delete packets.front();
packets.pop_front();
}
}
const CargoList::List *CargoList::Packets() const
{
return &packets;
}
void CargoList::AgeCargo()
{
if (empty) return;
uint dit = 0;
for (List::const_iterator it = packets.begin(); it != packets.end(); it++) {
if ((*it)->days_in_transit != 0xFF) (*it)->days_in_transit++;
dit += (*it)->days_in_transit * (*it)->count;
}
days_in_transit = dit / count;
}
bool CargoList::Empty() const
{
return empty;
}
uint CargoList::Count() const
{
return count;
}
bool CargoList::UnpaidCargo() const
{
return unpaid_cargo;
}
Money CargoList::FeederShare() const
{
return feeder_share;
}
StationID CargoList::Source() const
{
return source;
}
uint CargoList::DaysInTransit() const
{
return days_in_transit;
}
void CargoList::Append(CargoPacket *cp)
{
assert(cp != NULL);
assert(cp->IsValid());
for (List::iterator it = packets.begin(); it != packets.end(); it++) {
if ((*it)->SameSource(cp) && (*it)->count + cp->count <= 65535) {
(*it)->count += cp->count;
(*it)->feeder_share += cp->feeder_share;
delete cp;
InvalidateCache();
return;
}
}
/* The packet could not be merged with another one */
packets.push_back(cp);
InvalidateCache();
}
void CargoList::Truncate(uint count)
{
for (List::iterator it = packets.begin(); it != packets.end(); it++) {
uint local_count = (*it)->count;
if (local_count <= count) {
count -= local_count;
continue;
}
(*it)->count = count;
count = 0;
}
while (!packets.empty()) {
CargoPacket *cp = packets.back();
if (cp->count != 0) break;
delete cp;
packets.pop_back();
}
InvalidateCache();
}
bool CargoList::MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta, uint data)
{
assert(mta == MTA_FINAL_DELIVERY || dest != NULL);
CargoList tmp;
while (!packets.empty() && count > 0) {
CargoPacket *cp = *packets.begin();
if (cp->count <= count) {
/* Can move the complete packet */
packets.remove(cp);
switch (mta) {
case MTA_FINAL_DELIVERY:
if (cp->source == data) {
tmp.Append(cp);
} else {
count -= cp->count;
delete cp;
}
break;
case MTA_CARGO_LOAD:
cp->loaded_at_xy = data;
/* When cargo is moved into another vehicle you have *always* paid for it */
cp->paid_for = false;
/* FALL THROUGH */
case MTA_OTHER:
count -= cp->count;
dest->packets.push_back(cp);
break;
}
} else {
/* Can move only part of the packet, so split it into two pieces */
if (mta != MTA_FINAL_DELIVERY) {
CargoPacket *cp_new = new CargoPacket();
cp_new->source = cp->source;
cp_new->source_xy = cp->source_xy;
cp_new->loaded_at_xy = (mta == MTA_CARGO_LOAD) ? data : cp->loaded_at_xy;
cp_new->days_in_transit = cp->days_in_transit;
cp_new->feeder_share = cp->feeder_share / count;
/* When cargo is moved into another vehicle you have *always* paid for it */
cp_new->paid_for = (mta == MTA_CARGO_LOAD) ? false : cp->paid_for;
cp_new->count = count;
dest->packets.push_back(cp_new);
cp->feeder_share /= cp->count - count;
}
cp->count -= count;
count = 0;
}
}
bool remaining = !packets.empty();
if (mta == MTA_FINAL_DELIVERY && !tmp.Empty()) {
/* There are some packets that could not be delivered at the station, put them back */
tmp.MoveTo(this, MAX_UVALUE(uint));
tmp.packets.clear();
}
if (dest != NULL) dest->InvalidateCache();
InvalidateCache();
return remaining;
}
void CargoList::InvalidateCache()
{
empty = packets.empty();
count = 0;
unpaid_cargo = false;
feeder_share = 0;
source = INVALID_STATION;
days_in_transit = 0;
if (empty) return;
uint dit = 0;
for (List::const_iterator it = packets.begin(); it != packets.end(); it++) {
count += (*it)->count;
unpaid_cargo |= !(*it)->paid_for;
dit += (*it)->days_in_transit * (*it)->count;
feeder_share += (*it)->feeder_share;
}
days_in_transit = dit / count;
source = (*packets.begin())->source;
}
|