forked from teamhimeh/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimdepot.cc
518 lines (417 loc) · 12.1 KB
/
simdepot.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
* simgui.cc
*
* Copyright (c) 1997 - 2001 Hansjörg Malthaner
*
* This file is part of the Simutrans project and may not be used
* in other projects without written permission of the author.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "simintr.h"
#include "simconvoi.h"
#include "simvehikel.h"
#include "simwin.h"
#include "simware.h"
#include "simhalt.h"
#include "simplay.h"
#include "simworld.h"
#include "blockmanager.h"
#include "simio.h"
#include "simdepot.h"
#include "simline.h"
#include "simlinemgmt.h"
#include "gui/depot_frame.h"
#include "gui/messagebox.h"
#include "dataobj/fahrplan.h"
#include "dataobj/loadsave.h"
#include "dataobj/translator.h"
#include "bauer/hausbauer.h"
#include "bauer/vehikelbauer.h"
#include "besch/haus_besch.h"
depot_t::depot_t(karte_t *welt,loadsave_t *file) : gebaeude_t(welt)
{
depot_info = NULL;
rdwr(file);
if(file->get_version()<88002) {
setze_yoff(0);
}
}
depot_t::depot_t(karte_t *welt, koord3d pos, spieler_t *sp, const haus_tile_besch_t *t) :
gebaeude_t(welt, pos, sp, t)
{
depot_info = NULL;
}
depot_t::~depot_t()
{
if(depot_info) {
destroy_win(depot_info);
delete depot_info;
depot_info = NULL;
}
slist_iterator_tpl<convoihandle_t> iter(convois);
while(iter.next()) {
iter.access_current().unbind();
}
convois.clear();
}
// works with all vehicles perfectly!
fahrplan_t *
depot_t::erzeuge_fahrplan()
{
vehikel_t *v=get_vehicle(0);
return (v==NULL) ? new fahrplan_t() : v->erzeuge_neuen_fahrplan();
}
void
depot_t::convoi_arrived(convoihandle_t acnv, bool fpl_adjust)
{
for(unsigned i=0; i< acnv->gib_vehikel_anzahl(); i++) {
vehikel_t *v = acnv->gib_vehikel(i);
if(v) {
// Hajo: reset vehikel data
v->loesche_fracht();
v->setze_fahrtrichtung( ribi_t::sued );
}
}
if(fpl_adjust) {
// Volker: remove depot from schedule
fahrplan_t *fpl = acnv->gib_fahrplan();
fpl->remove();
acnv->setze_fahrplan(fpl);
}
convois.append(acnv);
if(depot_info) {
depot_info->action_triggered(NULL);
}
DBG_MESSAGE("depot_t::convoi_arrived()", "convoi %d, %p entered depot",
acnv.get_id(), acnv.get_rep());
}
void
depot_t::zeige_info()
{
if(depot_info==NULL) {
depot_info = new depot_frame_t(welt, this, gib_besitzer()->kennfarbe);
} else {
depot_info->build_vehicle_lists();
}
create_win(20, 20, depot_info, w_info);
}
bool depot_t::can_convoi_start(int /*icnv*/) const
{
return true;
}
int depot_t::buy_vehicle(int image)
{
if(image != -1) {
// Offen: prüfen ob noch platz im depot ist???
const vehikel_besch_t * info = vehikelbauer_t::gib_info(image);
DBG_DEBUG("depot_t::buy_vehicle()",info->gib_name());
vehikel_t *veh = vehikelbauer_t::baue(welt, gib_pos(), gib_besitzer(), NULL, info);
DBG_DEBUG("depot_t::buy_vehicle()","vehiclebauer %p",veh);
if(veh) {
vehicles.append(veh);
DBG_DEBUG("depot_t::buy_vehicle()","appended %i vehicle", vehicles.count());
return vehicles.count() - 1;
}
}
return -1;
}
void depot_t::append_vehicle(int icnv, int iveh, bool infront)
{
vehikel_t *veh = get_vehicle(iveh);
if(veh) {
convoihandle_t cnv = get_convoi(icnv);
/*
* create a new convoi, if necessary
*/
if(!cnv.is_bound()) {
cnv = add_convoi();
}
veh->setze_pos(gib_pos());
cnv->add_vehikel(veh, infront);
cnv->set_home_depot(gib_pos());
vehicles.remove(veh);
}
}
void depot_t::remove_vehicle(int icnv, int ipos)
{
convoihandle_t cnv = get_convoi(icnv);
if(cnv.is_bound()) {
vehikel_t *veh = cnv->remove_vehikel_bei(ipos);
if(veh) {
vehicles.append(veh);
}
}
}
void depot_t::sell_vehicle(int iveh)
{
vehikel_t *veh = get_vehicle(iveh);
if(veh) {
vehicles.remove(veh);
gib_besitzer()->buche(veh->calc_restwert(), gib_pos().gib_2d(), COST_NEW_VEHICLE);
DBG_MESSAGE("depot_t::sell_vehicle()","this=%p sells %p",this,veh);
delete veh;
}
}
convoihandle_t depot_t::add_convoi()
{
convoi_t *new_cnv = new convoi_t(welt, gib_besitzer());
convois.append(new_cnv->self);
return new_cnv->self;
}
convoihandle_t depot_t::copy_convoi(int icnv)
{
convoihandle_t old_cnv = get_convoi(icnv);
if (old_cnv.is_bound()) {
convoihandle_t new_cnv = add_convoi();
char new_name[128];
sprintf(new_name, "%s", old_cnv->gib_name());
new_cnv->setze_name(new_name);
int vehicle_count = old_cnv->gib_vehikel_anzahl();
for (int i = 0; i<vehicle_count; i++) {
const vehikel_besch_t * info = old_cnv->gib_vehikel(i)->gib_besch();
if (info != NULL) {
// search in depot for an existing vehicle of correct type
int oldest_vehicle = get_oldest_vehicle(old_cnv->gib_vehikel(i)->gib_basis_bild());
if (oldest_vehicle != -1) {
// append existing vehicle
append_vehicle(convoi_count()-1, oldest_vehicle, false);
} else {
// buy new vehicle
vehikel_t *veh = vehikelbauer_t::baue(welt, gib_pos(), gib_besitzer(), NULL, info);
veh->setze_pos(gib_pos());
new_cnv->add_vehikel(veh, false);
}
}
}
if (old_cnv->has_line()) {
new_cnv->set_line(old_cnv->get_line());
} else {
if (old_cnv->gib_fahrplan() != NULL) {
new_cnv->setze_fahrplan(old_cnv->gib_fahrplan()->copy());
}
}
return new_cnv->self;
}
return NULL;
}
bool depot_t::disassemble_convoi(int icnv, bool sell)
{
convoihandle_t cnv = get_convoi(icnv);
if(cnv.is_bound()) {
if(cnv->gib_vehikel(0)) {
vehikel_t *v = NULL;
if(sell) {
gib_besitzer()->buche(cnv->calc_restwert(), gib_pos().gib_2d(), COST_NEW_VEHICLE);
}
do {
v = cnv->remove_vehikel_bei(0);
if( v ) {
if(sell) {
DBG_MESSAGE("depot_t::convoi_aufloesen()", "sell %p", v);
DBG_MESSAGE("depot_t::convoi_aufloesen()", "sell %s", v->gib_besch()->gib_name());
delete v;
} else {
// Hajo: reset vehikel data
v->loesche_fracht();
v->setze_fahrtrichtung( ribi_t::sued );
v->setze_erstes(false);
v->setze_letztes(false);
vehicles.append(v);
}
}
} while(v != NULL);
}
// Hajo: update all stations from schedule that this convoi des not
// drive any more
fahrplan_t * fpl = cnv->gib_fahrplan();
if(fpl) {
for(int i=0; i<fpl->maxi(); i++) {
halthandle_t halt;
DBG_MESSAGE("depot_t::convoi_aufloesen()", "Unlinking schedule entry %d", i);
// Hajo: Hält dieser convoi hier?
if( (halt = haltestelle_t::gib_halt(welt, fpl->eintrag.get(i).pos.gib_2d())).is_bound() ) {
halt->rebuild_destinations();
}
}
}
DBG_MESSAGE("depot_t::convoi_aufloesen()", "convois.remove_at(%i)", icnv);
convois.remove_at(icnv);
// Hajo: destruktor also removes convoi from convoi list
delete cnv.detach();
return true;
}
return false;
}
bool
depot_t::start_convoi(int icnv)
{
convoihandle_t cnv = get_convoi(icnv);
if(cnv.is_bound() && cnv->gib_fahrplan() != NULL && cnv->gib_fahrplan()->ist_abgeschlossen() && cnv->gib_fahrplan()->maxi() > 0) {
// pruefen ob zug vollstaendig
if(cnv->gib_sum_leistung() == 0 || !cnv->pruefe_alle()) {
create_win(100, 64, MESG_WAIT, new nachrichtenfenster_t(welt, "Diese Zusammenstellung kann nicht fahren!\n"), w_autodelete);
}
else if(!cnv->gib_vehikel(0)->calc_route(welt, this->gib_pos(), cnv->gib_fahrplan()->eintrag.at(cnv->gib_fahrplan()->get_aktuell()).pos, cnv->gib_min_top_speed(), cnv->get_route()) ) {
// no route to go ...
static char buf[256];
sprintf(buf,translator::translate("Vehicle %s can't find a route!"), cnv->gib_name());
create_win(100, 64, MESG_WAIT, new nachrichtenfenster_t(welt, buf), w_autodelete);
}
else if(can_convoi_start(icnv)) {
// der Convoi kann losdüsen
cnv->setze_fahrplan( cnv->gib_fahrplan() ); // do not delete: this inform all stops!
welt->sync_add( cnv.get_rep() );
cnv->start();
convois.remove_at(icnv);
return true;
}
else {
create_win(100, 64, new nachrichtenfenster_t(welt, "Blockstrecke ist\nbelegt\n"), w_autodelete);
}
}
else {
create_win(100, 64, new nachrichtenfenster_t(welt, "Noch kein Fahrzeug\nmit Fahrplan\nvorhanden\n"), w_autodelete);
if(!cnv.is_bound()) {
dbg->warning("depot_t::starte_convoi()","No convoi to start!");
}
if(cnv.is_bound() && cnv->gib_fahrplan() == NULL) {
dbg->warning("depot_t::starte_convoi()","No schedule for convoi.");
}
if(cnv.is_bound() && cnv->gib_fahrplan() != NULL && cnv->gib_fahrplan()->ist_abgeschlossen() == false) {
dbg->warning("depot_t::starte_convoi()","Schedule is incomplete/not finished");
}
}
return false;
}
void
depot_t::rdwr(loadsave_t *file)
{
gebaeude_t::rdwr(file);
if(file->get_version() < 81033) {
slist_tpl<vehikel_t *> waggons;
rdwr_vehikel(vehicles, file);
rdwr_vehikel(waggons, file);
while(waggons.count() > 0) {
vehicles.append(waggons.at(0));
waggons.remove_at(0);
}
}
else {
rdwr_vehikel(vehicles, file);
}
}
void
depot_t::rdwr_vehikel(slist_tpl<vehikel_t *> &list, loadsave_t *file)
{
int count;
if(file->is_saving()) {
count = list.count();
DBG_MESSAGE("depot_t::vehikel_laden()","saving %d vehicles",count);
}
file->rdwr_long(count, "\n");
if(file->is_loading()) {
DBG_MESSAGE("depot_t::vehikel_laden()","loading %d vehicles",count);
for(int i=0; i<count; i++) {
ding_t::typ typ = (ding_t::typ)file->rd_obj_id();
vehikel_t *v = NULL;
switch( typ ) {
case automobil: v = new automobil_t(welt, file); break;
case waggon: v = new waggon_t(welt, file); break;
case schiff: v = new schiff_t(welt, file); break;
case aircraft: v = new aircraft_t(welt,file); break;
case monorailwaggon: v = new monorail_waggon_t(welt,file); break;
default:
dbg->fatal("depot_t::vehikel_laden()","invalid vehicle type $%X", typ);
}
DBG_MESSAGE("depot_t::vehikel_laden()","loaded %s", v->gib_besch()->gib_name());
list.insert( v );
}
}
else {
slist_iterator_tpl<vehikel_t *> l_iter ( list);
while(l_iter.next()) {
l_iter.get_current()->rdwr( file );
}
}
}
/**
* @returns NULL wenn OK, ansonsten eine Fehlermeldung
* @author Hj. Malthaner
*/
const char * depot_t::ist_entfernbar(const spieler_t *)
{
if(vehicles.count()>0) {
return "There are still vehicles\nstored in this depot!\n";
}
slist_iterator_tpl<convoihandle_t> iter(convois);
while(iter.next()) {
if(iter.get_current()->gib_vehikel_anzahl() > 0) {
return "There are still vehicles\nstored in this depot!\n";
}
}
return NULL;
}
const vehikel_besch_t *
depot_t::get_vehicle_type(int itype)
{
return vehikelbauer_t::gib_info(get_wegtyp(), itype);
}
slist_tpl<simline_t *> *
depot_t::get_line_list()
{
gib_besitzer()->simlinemgmt.build_line_list(get_line_type(), &lines);
return &lines;
}
simline_t *
depot_t::create_line()
{
return gib_besitzer()->simlinemgmt.create_line(get_line_type());
}
int
depot_t::get_oldest_vehicle(int id)
{
int oldest_veh = -1;
long insta_time_old = 0;
int i = 0;
slist_iterator_tpl<vehikel_t *> iter(get_vehicle_list());
while(iter.next()) {
if(iter.get_current()->gib_basis_bild() == id) {
if(oldest_veh == -1 || insta_time_old > iter.get_current()->gib_insta_zeit()) {
oldest_veh = i;
insta_time_old = iter.get_current()->gib_insta_zeit();
}
}
i++;
}
return oldest_veh;
}
/* deoptmanagement for railway depots and all other vehicles which uses the blockmanager */
void
bahndepot_t::convoi_arrived(convoihandle_t cnv, bool fpl_adjust)
{
depot_t::convoi_arrived(cnv, fpl_adjust);
blockmanager::gib_manager()->pruefe_blockstrecke(welt, gib_pos());
}
bool
bahndepot_t::can_convoi_start(int icnv) const
{
bool ok = false;
// teste, ob abfahrt erlaubt
blockmanager *bm = blockmanager::gib_manager();
blockhandle_t bs = bm->finde_blockstrecke(welt, gib_pos());
// prüfe ob blockstrecke frei
if(bs.is_bound() && bs->ist_frei()) {
// blockstrecke ist frei, wir können starten
///////////////////////////Signal
// simuliere überfahren des ersten feldes der blockstrecke
int i = 0;
do {
vehikel_t *v = get_convoi(icnv)->gib_vehikel( i++ );
bs->betrete( v );
} while(get_convoi(icnv)->gib_vehikel( i ) != NULL);
ok = true;
}
return ok;
}