author | rubidium |
Sun, 06 Apr 2008 12:26:40 +0000 | |
branch | noai |
changeset 9867 | b7d9ffe24f81 |
parent 9837 | c9ec4f82e0d0 |
child 10142 | 56ee7da4ad56 |
permissions | -rw-r--r-- |
9629 | 1 |
/* $Id$ */ |
2 |
||
3 |
/** @file cargopacket.cpp Implementation of the cargo packets */ |
|
4 |
||
5 |
#include "stdafx.h" |
|
6 |
#include "openttd.h" |
|
9837
c9ec4f82e0d0
(svn r12503) [NoAI] -Sync: with trunk r12461:12501.
rubidium
parents:
9732
diff
changeset
|
7 |
#include "station_base.h" |
9629 | 8 |
#include "cargopacket.h" |
9 |
#include "saveload.h" |
|
10 |
||
11 |
/* Initialize the cargopacket-pool */ |
|
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9631
diff
changeset
|
12 |
DEFINE_OLD_POOL_GENERIC(CargoPacket, CargoPacket) |
9629 | 13 |
|
14 |
void InitializeCargoPackets() |
|
15 |
{ |
|
16 |
/* Clean the cargo packet pool and create 1 block in it */ |
|
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9631
diff
changeset
|
17 |
_CargoPacket_pool.CleanPool(); |
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9631
diff
changeset
|
18 |
_CargoPacket_pool.AddBlockToPool(); |
9629 | 19 |
} |
20 |
||
21 |
CargoPacket::CargoPacket(StationID source, uint16 count) |
|
22 |
{ |
|
23 |
if (source != INVALID_STATION) assert(count != 0); |
|
24 |
||
25 |
this->source = source; |
|
26 |
this->source_xy = (source != INVALID_STATION) ? GetStation(source)->xy : 0; |
|
27 |
this->loaded_at_xy = this->source_xy; |
|
28 |
||
29 |
this->count = count; |
|
30 |
this->days_in_transit = 0; |
|
31 |
this->feeder_share = 0; |
|
32 |
this->paid_for = false; |
|
33 |
} |
|
34 |
||
35 |
CargoPacket::~CargoPacket() |
|
36 |
{ |
|
37 |
this->count = 0; |
|
38 |
} |
|
39 |
||
9732 | 40 |
bool CargoPacket::SameSource(const CargoPacket *cp) const |
9629 | 41 |
{ |
42 |
return this->source_xy == cp->source_xy && this->days_in_transit == cp->days_in_transit && this->paid_for == cp->paid_for; |
|
43 |
} |
|
44 |
||
45 |
static const SaveLoad _cargopacket_desc[] = { |
|
46 |
SLE_VAR(CargoPacket, source, SLE_UINT16), |
|
47 |
SLE_VAR(CargoPacket, source_xy, SLE_UINT32), |
|
48 |
SLE_VAR(CargoPacket, loaded_at_xy, SLE_UINT32), |
|
49 |
SLE_VAR(CargoPacket, count, SLE_UINT16), |
|
50 |
SLE_VAR(CargoPacket, days_in_transit, SLE_UINT8), |
|
51 |
SLE_VAR(CargoPacket, feeder_share, SLE_INT64), |
|
52 |
SLE_VAR(CargoPacket, paid_for, SLE_BOOL), |
|
53 |
||
54 |
SLE_END() |
|
55 |
}; |
|
56 |
||
57 |
static void Save_CAPA() |
|
58 |
{ |
|
59 |
CargoPacket *cp; |
|
60 |
||
61 |
FOR_ALL_CARGOPACKETS(cp) { |
|
62 |
SlSetArrayIndex(cp->index); |
|
63 |
SlObject(cp, _cargopacket_desc); |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
static void Load_CAPA() |
|
68 |
{ |
|
69 |
int index; |
|
70 |
||
71 |
while ((index = SlIterateArray()) != -1) { |
|
9694
e72987579514
(svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents:
9631
diff
changeset
|
72 |
CargoPacket *cp = new (index) CargoPacket(); |
9629 | 73 |
SlObject(cp, _cargopacket_desc); |
74 |
} |
|
75 |
} |
|
76 |
||
77 |
extern const ChunkHandler _cargopacket_chunk_handlers[] = { |
|
78 |
{ 'CAPA', Save_CAPA, Load_CAPA, CH_ARRAY | CH_LAST}, |
|
79 |
}; |
|
80 |
||
81 |
/* |
|
82 |
* |
|
83 |
* Cargo list implementation |
|
84 |
* |
|
85 |
*/ |
|
86 |
||
87 |
CargoList::~CargoList() |
|
88 |
{ |
|
89 |
while (!packets.empty()) { |
|
90 |
delete packets.front(); |
|
91 |
packets.pop_front(); |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
const CargoList::List *CargoList::Packets() const |
|
96 |
{ |
|
97 |
return &packets; |
|
98 |
} |
|
99 |
||
100 |
void CargoList::AgeCargo() |
|
101 |
{ |
|
102 |
if (empty) return; |
|
103 |
||
104 |
uint dit = 0; |
|
105 |
for (List::const_iterator it = packets.begin(); it != packets.end(); it++) { |
|
106 |
if ((*it)->days_in_transit != 0xFF) (*it)->days_in_transit++; |
|
107 |
dit += (*it)->days_in_transit * (*it)->count; |
|
108 |
} |
|
109 |
days_in_transit = dit / count; |
|
110 |
} |
|
111 |
||
112 |
bool CargoList::Empty() const |
|
113 |
{ |
|
114 |
return empty; |
|
115 |
} |
|
116 |
||
117 |
uint CargoList::Count() const |
|
118 |
{ |
|
119 |
return count; |
|
120 |
} |
|
121 |
||
122 |
bool CargoList::UnpaidCargo() const |
|
123 |
{ |
|
124 |
return unpaid_cargo; |
|
125 |
} |
|
126 |
||
127 |
Money CargoList::FeederShare() const |
|
128 |
{ |
|
129 |
return feeder_share; |
|
130 |
} |
|
131 |
||
132 |
StationID CargoList::Source() const |
|
133 |
{ |
|
134 |
return source; |
|
135 |
} |
|
136 |
||
137 |
uint CargoList::DaysInTransit() const |
|
138 |
{ |
|
139 |
return days_in_transit; |
|
140 |
} |
|
141 |
||
142 |
void CargoList::Append(CargoPacket *cp) |
|
143 |
{ |
|
144 |
assert(cp != NULL); |
|
145 |
assert(cp->IsValid()); |
|
146 |
||
147 |
for (List::iterator it = packets.begin(); it != packets.end(); it++) { |
|
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
9629
diff
changeset
|
148 |
if ((*it)->SameSource(cp) && (*it)->count + cp->count <= 65535) { |
9629 | 149 |
(*it)->count += cp->count; |
150 |
(*it)->feeder_share += cp->feeder_share; |
|
151 |
delete cp; |
|
152 |
||
153 |
InvalidateCache(); |
|
154 |
return; |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
/* The packet could not be merged with another one */ |
|
159 |
packets.push_back(cp); |
|
160 |
InvalidateCache(); |
|
161 |
} |
|
162 |
||
163 |
||
164 |
void CargoList::Truncate(uint count) |
|
165 |
{ |
|
166 |
for (List::iterator it = packets.begin(); it != packets.end(); it++) { |
|
167 |
uint local_count = (*it)->count; |
|
168 |
if (local_count <= count) { |
|
169 |
count -= local_count; |
|
170 |
continue; |
|
171 |
} |
|
172 |
||
173 |
(*it)->count = count; |
|
174 |
count = 0; |
|
175 |
} |
|
176 |
||
177 |
while (!packets.empty()) { |
|
178 |
CargoPacket *cp = packets.back(); |
|
179 |
if (cp->count != 0) break; |
|
180 |
delete cp; |
|
181 |
packets.pop_back(); |
|
182 |
} |
|
183 |
||
184 |
InvalidateCache(); |
|
185 |
} |
|
186 |
||
187 |
bool CargoList::MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta, uint data) |
|
188 |
{ |
|
189 |
assert(mta == MTA_FINAL_DELIVERY || dest != NULL); |
|
190 |
CargoList tmp; |
|
191 |
||
192 |
while (!packets.empty() && count > 0) { |
|
193 |
CargoPacket *cp = *packets.begin(); |
|
194 |
if (cp->count <= count) { |
|
195 |
/* Can move the complete packet */ |
|
196 |
packets.remove(cp); |
|
197 |
switch (mta) { |
|
198 |
case MTA_FINAL_DELIVERY: |
|
199 |
if (cp->source == data) { |
|
200 |
tmp.Append(cp); |
|
201 |
} else { |
|
202 |
count -= cp->count; |
|
203 |
delete cp; |
|
204 |
} |
|
205 |
break; |
|
206 |
case MTA_CARGO_LOAD: |
|
207 |
cp->loaded_at_xy = data; |
|
208 |
/* When cargo is moved into another vehicle you have *always* paid for it */ |
|
209 |
cp->paid_for = false; |
|
210 |
/* FALL THROUGH */ |
|
211 |
case MTA_OTHER: |
|
212 |
count -= cp->count; |
|
213 |
dest->packets.push_back(cp); |
|
214 |
break; |
|
215 |
} |
|
216 |
} else { |
|
217 |
/* Can move only part of the packet, so split it into two pieces */ |
|
218 |
if (mta != MTA_FINAL_DELIVERY) { |
|
219 |
CargoPacket *cp_new = new CargoPacket(); |
|
220 |
cp_new->source = cp->source; |
|
221 |
cp_new->source_xy = cp->source_xy; |
|
222 |
cp_new->loaded_at_xy = (mta == MTA_CARGO_LOAD) ? data : cp->loaded_at_xy; |
|
223 |
||
224 |
cp_new->days_in_transit = cp->days_in_transit; |
|
225 |
cp_new->feeder_share = cp->feeder_share / count; |
|
226 |
/* When cargo is moved into another vehicle you have *always* paid for it */ |
|
227 |
cp_new->paid_for = (mta == MTA_CARGO_LOAD) ? false : cp->paid_for; |
|
228 |
||
229 |
cp_new->count = count; |
|
230 |
dest->packets.push_back(cp_new); |
|
231 |
||
232 |
cp->feeder_share /= cp->count - count; |
|
233 |
} |
|
234 |
cp->count -= count; |
|
235 |
||
236 |
count = 0; |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
bool remaining = !packets.empty(); |
|
241 |
||
242 |
if (mta == MTA_FINAL_DELIVERY && !tmp.Empty()) { |
|
243 |
/* There are some packets that could not be delivered at the station, put them back */ |
|
244 |
tmp.MoveTo(this, MAX_UVALUE(uint)); |
|
245 |
tmp.packets.clear(); |
|
246 |
} |
|
247 |
||
248 |
if (dest != NULL) dest->InvalidateCache(); |
|
249 |
InvalidateCache(); |
|
250 |
||
251 |
return remaining; |
|
252 |
} |
|
253 |
||
254 |
void CargoList::InvalidateCache() |
|
255 |
{ |
|
256 |
empty = packets.empty(); |
|
257 |
count = 0; |
|
258 |
unpaid_cargo = false; |
|
259 |
feeder_share = 0; |
|
260 |
source = INVALID_STATION; |
|
261 |
days_in_transit = 0; |
|
262 |
||
263 |
if (empty) return; |
|
264 |
||
265 |
uint dit = 0; |
|
266 |
for (List::const_iterator it = packets.begin(); it != packets.end(); it++) { |
|
267 |
count += (*it)->count; |
|
268 |
unpaid_cargo |= !(*it)->paid_for; |
|
269 |
dit += (*it)->days_in_transit * (*it)->count; |
|
270 |
feeder_share += (*it)->feeder_share; |
|
271 |
} |
|
272 |
days_in_transit = dit / count; |
|
273 |
source = (*packets.begin())->source; |
|
274 |
} |