forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimline.cc
300 lines (239 loc) · 7.21 KB
/
simline.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "dataobj/translator.h"
#include "simtypes.h"
#include "simline.h"
#include "simhalt.h"
#include "simplay.h"
#include "vehicle/simvehikel.h"
#include "simconvoi.h"
#include "convoihandle_t.h"
#include "simworld.h"
#include "simlinemgmt.h"
uint8 simline_t::convoi_to_line_catgory[MAX_CONVOI_COST]={LINE_CAPACITY, LINE_TRANSPORTED_GOODS, LINE_REVENUE, LINE_OPERATIONS, LINE_PROFIT };
karte_t *simline_t::welt=NULL;
simline_t::simline_t(simlinemgmt_t* simlinemgmt, fahrplan_t* fpl)
{
welt = simlinemgmt->get_welt();
self = linehandle_t(this);
init_financial_history();
const int i = simlinemgmt->get_unique_line_id();
memset(this->name, 0, 128);
sprintf(this->name, "%s (%02d)", translator::translate("Line"), i);
this->fpl = fpl;
this->old_fpl = new fahrplan_t(fpl);
this->id = i;
this->state_color = COL_WHITE;
type = simline_t::line;
simlinemgmt->add_line(self);
DBG_MESSAGE("simline_t::simline_t(karte_t,simlinemgmt,fahrplan_t)","create with %d (unique %d)",self.get_id(),get_line_id());
}
simline_t::simline_t(karte_t* welt, loadsave_t* file)
{
self = linehandle_t(this);
init_financial_history();
rdwr(file);
DBG_MESSAGE("simline_t::simline_t(karte_t,simlinemgmt,loadsave_t)","load line id=%d",id);
this->welt = welt;
this->old_fpl = new fahrplan_t(fpl);
}
simline_t::~simline_t()
{
int count = count_convoys() - 1;
for (int i = count; i>=0; i--)
{
DBG_DEBUG("simline_t::~simline_t()", "convoi '%d' removed", i);
DBG_DEBUG("simline_t::~simline_t()", "convoi '%d'->fpl=%p", i, get_convoy(i)->gib_fahrplan());
// Hajo: take care - this call will do "remove_convoi()"
// on our list!
get_convoy(i)->unset_line();
}
unregister_stops();
DBG_DEBUG("simline_t::~simline_t()", "deleting fpl=%p and old_fpl=%p", fpl, old_fpl);
delete (fpl);
delete (old_fpl);
self.detach();
DBG_MESSAGE("simline_t::~simline_t()", "line %d (%p) destroyed", id, this);
}
void simline_t::add_convoy(convoihandle_t cnv)
{
if (line_managed_convoys.empty()) {
// first convoi -> ok, now we can announce this connection to the stations
register_stops(fpl);
}
// first convoi may change line type
if (type == trainline && line_managed_convoys.empty() && cnv.is_bound()) {
if(cnv->gib_vehikel(0)) {
// check, if needed to convert to tram line?
if(cnv->gib_vehikel(0)->gib_besch()->get_waytype()==tram_wt) {
type = simline_t::tramline;
}
// check, if needed to convert to monorail line?
if(cnv->gib_vehikel(0)->gib_besch()->get_waytype()==monorail_wt) {
type = simline_t::monorailline;
// elevated monorail were saved with wrong coordinates for some versions.
// We try to recover here
}
}
}
// only add convoy if not allready member of line
line_managed_convoys.append_unique(cnv,16);
// what goods can this line transport?
for(uint i=0; i<cnv->gib_vehikel_anzahl(); i++ ) {
const ware_besch_t *ware=cnv->gib_vehikel(i)->gib_fracht_typ();
if(ware!=warenbauer_t::nichts) {
goods_catg_index.append_unique( ware->gib_catg_index(), 1 );
}
}
// will not hurt ...
financial_history[0][LINE_CONVOIS] = count_convoys();
if(state_color==COL_BLACK && cnv->has_obsolete_vehicles()) {
state_color = COL_DARK_BLUE;
}
}
void simline_t::remove_convoy(convoihandle_t cnv)
{
if(line_managed_convoys.is_contained(cnv)) {
line_managed_convoys.remove(cnv);
recalc_catg_index();
financial_history[0][LINE_CONVOIS] = count_convoys();
recalc_status();
}
if (line_managed_convoys.empty()) {
unregister_stops();
}
}
void simline_t::rdwr(loadsave_t *file)
{
// only create a new fahrplan if we are loading a savegame!
if (file->is_loading()) {
fpl = new fahrplan_t();
}
else {
file->rdwr_enum(type, "\n");
}
file->rdwr_str(name, sizeof(name));
if(file->get_version()<88003) {
sint32 dummy=id;
file->rdwr_long(dummy, " ");
id = (uint16)dummy;
}
else {
file->rdwr_short(id, " ");
}
fpl->rdwr(file);
//financial history
for (int j = 0; j<MAX_LINE_COST; j++) {
for (int k = MAX_MONTHS-1; k>=0; k--) {
file->rdwr_longlong(financial_history[k][j], " ");
}
}
// otherwise inintialized to zero if loading ...
financial_history[0][LINE_CONVOIS] = count_convoys();
}
void simline_t::laden_abschliessen()
{
register_stops(fpl);
recalc_status();
}
void simline_t::register_stops(fahrplan_t * fpl)
{
DBG_DEBUG("simline_t::register_stops()", "%d fpl entries in schedule %p", fpl->maxi(),fpl);
for (int i = 0; i<fpl->maxi(); i++) {
const halthandle_t halt = haltestelle_t::gib_halt(welt, fpl->eintrag[i].pos.gib_2d());
if(halt.is_bound()) {
//DBG_DEBUG("simline_t::register_stops()", "halt not null");
halt->add_line(self);
}
else {
DBG_DEBUG("simline_t::register_stops()", "halt null");
}
}
}
void simline_t::unregister_stops()
{
unregister_stops(fpl);
}
void simline_t::unregister_stops(fahrplan_t * fpl)
{
for (int i = 0; i<fpl->maxi(); i++) {
halthandle_t halt = haltestelle_t::gib_halt(welt, fpl->eintrag[i].pos.gib_2d());
if (halt.is_bound()) {
halt->remove_line(self);
}
}
}
void simline_t::renew_stops()
{
unregister_stops(this->old_fpl);
register_stops(this->fpl);
DBG_DEBUG("simline_t::renew_stops()", "Line id=%d, name='%s'", id, name);
}
void simline_t::new_month()
{
recalc_status();
for (int j = 0; j<MAX_LINE_COST; j++) {
for (int k = MAX_MONTHS-1; k>0; k--) {
financial_history[k][j] = financial_history[k-1][j];
}
financial_history[0][j] = 0;
}
financial_history[0][LINE_CONVOIS] = count_convoys();
}
/*
* called from line_management_gui.cc to prepare line for a change of its schedule
*/
void simline_t::prepare_for_update()
{
DBG_DEBUG("simline_t::prepare_for_update()", "line %d (%p)", id, this);
delete (old_fpl);
this->old_fpl = new fahrplan_t(fpl);
}
void simline_t::init_financial_history()
{
memset( financial_history, 0, sizeof(financial_history) );
}
/*
* the current state saved as color
* Meanings are BLACK (ok), WHITE (no convois), YELLOW (no vehicle moved), RED (last month income minus), BLUE (at least one convoi vehicle is obsolete)
*/
void simline_t::recalc_status()
{
if(financial_history[0][LINE_CONVOIS]==0) {
// noconvois assigned to this line
state_color = COL_WHITE;
}
else if(financial_history[0][LINE_PROFIT]<0) {
// ok, not performing best
state_color = COL_RED;
} else if((financial_history[0][LINE_OPERATIONS]|financial_history[1][LINE_OPERATIONS])==0) {
// nothing moved
state_color = COL_YELLOW;
}
else if(welt->use_timeline()) {
// convois has obsolete vehicles?
bool has_obsolete = false;
for(unsigned i=0; !has_obsolete && i<line_managed_convoys.get_count(); i++ ) {
has_obsolete = line_managed_convoys[i]->has_obsolete_vehicles();
}
// now we have to set it
state_color = has_obsolete ? COL_DARK_BLUE : COL_BLACK;
}
else {
// normal state
state_color = COL_BLACK;
}
}
// recalc what good this line is moving
void simline_t::recalc_catg_index()
{
goods_catg_index.clear();
for(unsigned i=0; i<line_managed_convoys.get_count(); i++ ) {
// what goods can this line transport?
const convoihandle_t cnv = line_managed_convoys[i];
for(uint i=0; i<cnv->gib_vehikel_anzahl(); i++ ) {
const ware_besch_t *ware=cnv->gib_vehikel(i)->gib_fracht_typ();
if(ware!=warenbauer_t::nichts) {
goods_catg_index.append_unique( ware->gib_catg_index(), 1 );
}
}
}
}