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