author | truebrain |
Fri, 18 Jul 2008 10:15:16 +0000 | |
branch | noai |
changeset 11168 | 3842648184cd |
parent 10455 | 22c441f5adf9 |
permissions | -rw-r--r-- |
9629 | 1 |
/* $Id$ */ |
2 |
||
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
10294
diff
changeset
|
3 |
/** @file cargopacket.h Base class for cargo packets. */ |
9629 | 4 |
|
5 |
#ifndef CARGOPACKET_H |
|
6 |
#define CARGOPACKET_H |
|
7 |
||
9837
c9ec4f82e0d0
(svn r12503) [NoAI] -Sync: with trunk r12461:12501.
rubidium
parents:
9732
diff
changeset
|
8 |
#include "oldpool.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9701
diff
changeset
|
9 |
#include "economy_type.h" |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
10 |
#include "tile_type.h" |
9837
c9ec4f82e0d0
(svn r12503) [NoAI] -Sync: with trunk r12461:12501.
rubidium
parents:
9732
diff
changeset
|
11 |
#include "station_type.h" |
9629 | 12 |
#include <list> |
13 |
||
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
10294
diff
changeset
|
14 |
struct BackuppedVehicle; |
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
10294
diff
changeset
|
15 |
|
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9631
diff
changeset
|
16 |
typedef uint32 CargoPacketID; |
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9631
diff
changeset
|
17 |
struct CargoPacket; |
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9631
diff
changeset
|
18 |
|
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9631
diff
changeset
|
19 |
/** We want to use a pool */ |
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9631
diff
changeset
|
20 |
DECLARE_OLD_POOL(CargoPacket, CargoPacket, 10, 1000) |
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9631
diff
changeset
|
21 |
|
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9631
diff
changeset
|
22 |
|
9629 | 23 |
/** |
24 |
* Container for cargo from the same location and time |
|
25 |
*/ |
|
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9631
diff
changeset
|
26 |
struct CargoPacket : PoolItem<CargoPacket, CargoPacketID, &_CargoPacket_pool> { |
10294 | 27 |
Money feeder_share; ///< Value of feeder pickup to be paid for on delivery of cargo |
9629 | 28 |
TileIndex source_xy; ///< The origin of the cargo (first station in feeder chain) |
29 |
TileIndex loaded_at_xy; ///< Location where this cargo has been loaded into the vehicle |
|
10294 | 30 |
StationID source; ///< The station where the cargo came from first |
9629 | 31 |
|
32 |
uint16 count; ///< The amount of cargo in this packet |
|
33 |
byte days_in_transit; ///< Amount of days this packet has been in transit |
|
34 |
bool paid_for; ///< Have we been paid for this cargo packet? |
|
35 |
||
36 |
/** |
|
37 |
* Creates a new cargo packet |
|
38 |
* @param source the source of the packet |
|
39 |
* @param count the number of cargo entities to put in this packet |
|
40 |
* @pre count != 0 || source == INVALID_STATION |
|
41 |
*/ |
|
42 |
CargoPacket(StationID source = INVALID_STATION, uint16 count = 0); |
|
43 |
||
44 |
/** Destroy the packet */ |
|
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9631
diff
changeset
|
45 |
virtual ~CargoPacket(); |
9629 | 46 |
|
47 |
||
48 |
/** |
|
49 |
* Is this a valid cargo packet ? |
|
50 |
* @return true if and only it is valid |
|
51 |
*/ |
|
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
52 |
inline bool IsValid() const { return this->count != 0; } |
9629 | 53 |
|
54 |
/** |
|
55 |
* Checks whether the cargo packet is from (exactly) the same source |
|
56 |
* in time and location. |
|
57 |
* @param cp the cargo packet to compare to |
|
58 |
* @return true if and only if days_in_transit and source_xy are equal |
|
59 |
*/ |
|
9732 | 60 |
bool SameSource(const CargoPacket *cp) const; |
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
10294
diff
changeset
|
61 |
|
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
10294
diff
changeset
|
62 |
void RestoreBackup() const; |
9629 | 63 |
}; |
64 |
||
65 |
/** |
|
66 |
* Iterate over all _valid_ cargo packets from the given start |
|
67 |
* @param cp the variable used as "iterator" |
|
68 |
* @param start the cargo packet ID of the first packet to iterate over |
|
69 |
*/ |
|
70 |
#define FOR_ALL_CARGOPACKETS_FROM(cp, start) for (cp = GetCargoPacket(start); cp != NULL; cp = (cp->index + 1U < GetCargoPacketPoolSize()) ? GetCargoPacket(cp->index + 1U) : NULL) if (cp->IsValid()) |
|
71 |
||
72 |
/** |
|
73 |
* Iterate over all _valid_ cargo packets from the begin of the pool |
|
74 |
* @param cp the variable used as "iterator" |
|
75 |
*/ |
|
76 |
#define FOR_ALL_CARGOPACKETS(cp) FOR_ALL_CARGOPACKETS_FROM(cp, 0) |
|
77 |
||
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
78 |
extern void SaveLoad_STNS(Station *st); |
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
79 |
|
9629 | 80 |
/** |
81 |
* Simple collection class for a list of cargo packets |
|
82 |
*/ |
|
83 |
class CargoList { |
|
84 |
public: |
|
85 |
/** List of cargo packets */ |
|
86 |
typedef std::list<CargoPacket *> List; |
|
87 |
||
88 |
/** Kind of actions that could be done with packets on move */ |
|
89 |
enum MoveToAction { |
|
90 |
MTA_FINAL_DELIVERY, ///< "Deliver" the packet to the final destination, i.e. destroy the packet |
|
91 |
MTA_CARGO_LOAD, ///< Load the packet onto a vehicle, i.e. set the last loaded station ID |
|
92 |
MTA_OTHER ///< "Just" move the packet to another cargo list |
|
93 |
}; |
|
94 |
||
95 |
private: |
|
96 |
List packets; ///< The cargo packets in this list |
|
97 |
||
98 |
bool empty; ///< Cache for whether this list is empty or not |
|
99 |
uint count; ///< Cache for the number of cargo entities |
|
100 |
bool unpaid_cargo; ///< Cache for the unpaid cargo |
|
101 |
Money feeder_share; ///< Cache for the feeder share |
|
102 |
StationID source; ///< Cache for the source of the packet |
|
103 |
uint days_in_transit; ///< Cache for the number of days in transit |
|
104 |
||
105 |
public: |
|
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
10294
diff
changeset
|
106 |
friend struct BackuppedVehicle; |
9701
d1ac22c62f64
(svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents:
9694
diff
changeset
|
107 |
friend void SaveLoad_STNS(Station *st); |
9629 | 108 |
|
109 |
/** Create the cargo list */ |
|
110 |
CargoList() { this->InvalidateCache(); } |
|
111 |
/** And destroy it ("frees" all cargo packets) */ |
|
112 |
~CargoList(); |
|
113 |
||
114 |
/** |
|
115 |
* Returns a pointer to the cargo packet list (so you can iterate over it etc). |
|
116 |
* @return pointer to the packet list |
|
117 |
*/ |
|
118 |
const CargoList::List *Packets() const; |
|
119 |
||
120 |
/** |
|
121 |
* Ages the all cargo in this list |
|
122 |
*/ |
|
123 |
void AgeCargo(); |
|
124 |
||
125 |
/** |
|
126 |
* Checks whether this list is empty |
|
127 |
* @return true if and only if the list is empty |
|
128 |
*/ |
|
129 |
bool Empty() const; |
|
130 |
||
131 |
/** |
|
132 |
* Returns the number of cargo entities in this list |
|
133 |
* @return the before mentioned number |
|
134 |
*/ |
|
135 |
uint Count() const; |
|
136 |
||
137 |
/** |
|
138 |
* Is there some cargo that has not been paid for? |
|
139 |
* @return true if and only if there is such a cargo |
|
140 |
*/ |
|
141 |
bool UnpaidCargo() const; |
|
142 |
||
143 |
/** |
|
144 |
* Returns total sum of the feeder share for all packets |
|
145 |
* @return the before mentioned number |
|
146 |
*/ |
|
147 |
Money FeederShare() const; |
|
148 |
||
149 |
/** |
|
150 |
* Returns source of the first cargo packet in this list |
|
151 |
* @return the before mentioned source |
|
152 |
*/ |
|
153 |
StationID Source() const; |
|
154 |
||
155 |
/** |
|
156 |
* Returns average number of days in transit for a cargo entity |
|
157 |
* @return the before mentioned number |
|
158 |
*/ |
|
159 |
uint DaysInTransit() const; |
|
160 |
||
161 |
||
162 |
/** |
|
163 |
* Appends the given cargo packet |
|
164 |
* @warning After appending this packet may not exist anymore! |
|
165 |
* @note Do not use the cargo packet anymore after it has been appended to this CargoList! |
|
166 |
* @param cp the cargo packet to add |
|
167 |
* @pre cp != NULL |
|
168 |
*/ |
|
169 |
void Append(CargoPacket *cp); |
|
170 |
||
171 |
/** |
|
172 |
* Truncates the cargo in this list to the given amount. It leaves the |
|
173 |
* first count cargo entities and removes the rest. |
|
174 |
* @param count the maximum amount of entities to be in the list after the command |
|
175 |
*/ |
|
176 |
void Truncate(uint count); |
|
177 |
||
178 |
/** |
|
179 |
* Moves the given amount of cargo to another list. |
|
180 |
* Depending on the value of mta the side effects of this function differ: |
|
181 |
* - MTA_FINAL_DELIVERY: destroys the packets that do not originate from a specific station |
|
182 |
* - MTA_CARGO_LOAD: sets the loaded_at_xy value of the moved packets |
|
183 |
* - MTA_OTHER: just move without side effects |
|
184 |
* @param dest the destination to move the cargo to |
|
185 |
* @param count the amount of cargo entities to move |
|
186 |
* @param mta how to handle the moving (side effects) |
|
187 |
* @param data Depending on mta the data of this variable differs: |
|
188 |
* - MTA_FINAL_DELIVERY - station ID of packet's origin not to remove |
|
189 |
* - MTA_CARGO_LOAD - station's tile index of load |
|
190 |
* - MTA_OTHER - unused |
|
191 |
* @param mta == MTA_FINAL_DELIVERY || dest != NULL |
|
192 |
* @return true if there are still packets that might be moved from this cargo list |
|
193 |
*/ |
|
194 |
bool MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta = MTA_OTHER, uint data = 0); |
|
195 |
||
196 |
/** Invalidates the cached data and rebuild it */ |
|
197 |
void InvalidateCache(); |
|
198 |
}; |
|
199 |
||
200 |
#endif /* CARGOPACKET_H */ |