src/cargopacket.cpp
changeset 7529 5a277cbe8d8f
parent 7506 e52d89f5c7c1
child 7626 baab18f2a00c
equal deleted inserted replaced
7528:4df48baf9ee0 7529:5a277cbe8d8f
     6 #include "openttd.h"
     6 #include "openttd.h"
     7 #include "station.h"
     7 #include "station.h"
     8 #include "cargopacket.h"
     8 #include "cargopacket.h"
     9 #include "saveload.h"
     9 #include "saveload.h"
    10 
    10 
       
    11 /** Cache for speeding up lookups in AllocateRaw */
       
    12 static uint _first_free_cargo_packet_index;
       
    13 
    11 /**
    14 /**
    12  * Called if a new block is added to the station-pool
    15  * Called if a new block is added to the station-pool
    13  */
    16  */
    14 static void CargoPacketPoolNewBlock(uint cpart_item)
    17 static void CargoPacketPoolNewBlock(uint cpart_item)
    15 {
    18 {
    29 /* Initialize the cargopacket-pool */
    32 /* Initialize the cargopacket-pool */
    30 DEFINE_OLD_POOL(CargoPacket, CargoPacket, CargoPacketPoolNewBlock, CargoPacketPoolCleanBlock)
    33 DEFINE_OLD_POOL(CargoPacket, CargoPacket, CargoPacketPoolNewBlock, CargoPacketPoolCleanBlock)
    31 
    34 
    32 void InitializeCargoPackets()
    35 void InitializeCargoPackets()
    33 {
    36 {
       
    37 	_first_free_cargo_packet_index = 0;
    34 	/* Clean the cargo packet pool and create 1 block in it */
    38 	/* Clean the cargo packet pool and create 1 block in it */
    35 	CleanPool(&_CargoPacket_pool);
    39 	CleanPool(&_CargoPacket_pool);
    36 	AddBlockToPool(&_CargoPacket_pool);
    40 	AddBlockToPool(&_CargoPacket_pool);
    37 
    41 
    38 	/* Check whether our &cargolist == &cargolist.packets "hack" works */
    42 	/* Check whether our &cargolist == &cargolist.packets "hack" works */
    53 	this->paid_for        = false;
    57 	this->paid_for        = false;
    54 }
    58 }
    55 
    59 
    56 CargoPacket::~CargoPacket()
    60 CargoPacket::~CargoPacket()
    57 {
    61 {
       
    62 	if (this->index < _first_free_cargo_packet_index) _first_free_cargo_packet_index = this->index;
    58 	this->count = 0;
    63 	this->count = 0;
    59 }
    64 }
    60 
    65 
    61 bool CargoPacket::SameSource(CargoPacket *cp)
    66 bool CargoPacket::SameSource(CargoPacket *cp)
    62 {
    67 {
    90 {
    95 {
    91 	CargoPacket *cp = NULL;
    96 	CargoPacket *cp = NULL;
    92 
    97 
    93 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
    98 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
    94 	 * TODO - This is just a temporary stage, this will be removed. */
    99 	 * TODO - This is just a temporary stage, this will be removed. */
    95 	for (cp = GetCargoPacket(0); cp != NULL; cp = (cp->index + 1U < GetCargoPacketPoolSize()) ? GetCargoPacket(cp->index + 1U) : NULL) {
   100 	for (cp = GetCargoPacket(_first_free_cargo_packet_index); cp != NULL; cp = (cp->index + 1U < GetCargoPacketPoolSize()) ? GetCargoPacket(cp->index + 1U) : NULL) {
    96 		if (!cp->IsValid()) {
   101 		if (!cp->IsValid()) {
    97 			CargoPacket::ID index = cp->index;
   102 			CargoPacket::ID index = cp->index;
    98 
   103 
    99 			memset(cp, 0, sizeof(CargoPacket));
   104 			memset(cp, 0, sizeof(CargoPacket));
   100 			cp->index = index;
   105 			cp->index = index;
       
   106 			_first_free_cargo_packet_index = cp->index;
   101 			return cp;
   107 			return cp;
   102 		}
   108 		}
   103 	}
   109 	}
   104 
   110 
   105 	/* Check if we can add a block to the pool */
   111 	/* Check if we can add a block to the pool */