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