forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 53
/
ai_goods.cc
1618 lines (1431 loc) · 53.3 KB
/
ai_goods.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the Simutrans-Extended project under the Artistic License.
* (see LICENSE.txt)
*/
#include "../simconvoi.h"
#include "../simfab.h"
#include "../simmenu.h"
#include "../simtypes.h"
#include "../simtool.h"
#include "../simunits.h"
#include "finance.h"
#include "simplay.h"
#include "../simhalt.h"
#include "../simintr.h"
#include "../simline.h"
#include "../simmesg.h"
#include "../simworld.h"
#include "../bauer/brueckenbauer.h"
#include "../bauer/hausbauer.h"
#include "../bauer/tunnelbauer.h"
#include "../bauer/vehikelbauer.h"
#include "../bauer/wegbauer.h"
#include "../dataobj/schedule.h"
#include "../dataobj/loadsave.h"
#include "../obj/wayobj.h"
#include "../utils/simrandom.h"
#include "../utils/simstring.h"
#include "../utils/cbuffer_t.h"
#include "../vehicle/vehicle.h"
#include "ai_goods.h"
ai_goods_t::ai_goods_t(uint8 nr) : ai_t(nr)
{
state = NR_INIT;
freight = NULL;
root = NULL;
start = NULL;
ziel = NULL;
count = 0;
rail_engine = NULL;
rail_vehicle = NULL;
rail_weg = NULL;
road_vehicle = NULL;
ship_vehicle = NULL;
road_weg = NULL;
next_construction_steps = welt->get_steps()+ 50;
road_transport = nr!=7;
rail_transport = nr>2;
ship_transport = true;
air_transport = false;
}
/**
* Methode fuer jaehrliche Aktionen
*/
void ai_goods_t::new_year()
{
player_t::new_year();
// AI will reconsider the oldest unbuiltable lines again
uint remove = (uint)max(0,(int)forbidden_connections.get_count()-3);
while( remove < forbidden_connections.get_count() ) {
forbidden_connections.remove_first();
}
}
void ai_goods_t::rotate90( const sint16 y_size )
{
player_t::rotate90( y_size );
// rotate places
platz1.rotate90( y_size );
platz2.rotate90( y_size );
size1.rotate90( 0 );
size2.rotate90( 0 );
harbour.rotate90( y_size );
}
/**
* Activates/deactivates a player
*/
bool ai_goods_t::set_active(bool new_state)
{
// something to change?
if(active!=new_state) {
if(!new_state) {
// deactivate AI
active = false;
state = NR_INIT;
start = ziel = NULL;
}
else {
// activate AI
active = true;
}
}
return active;
}
/* recursive lookup of a factory tree:
* sets start and destination to the next needed supplier
* start always with the first branch, if there are more goods
*/
bool ai_goods_t::get_factory_tree_lowest_missing( fabrik_t *fab )
{
// now check for all products (should be changed later for the root)
for( int i=0; i<fab->get_desc()->get_supplier_count(); i++ ) {
const goods_desc_t *ware = fab->get_desc()->get_supplier(i)->get_input_type();
// find out how much is there
const array_tpl<ware_production_t>& input = fab->get_input();
uint ware_nr;
for( ware_nr=0; ware_nr<input.get_count() && input[ware_nr].get_typ()!=ware; ware_nr++ )
;
if( input[ware_nr].menge > input[ware_nr].max/4 ) {
// already enough supplied to
continue;
}
for(auto q : fab->get_suppliers()) {
fabrik_t* const qfab = fabrik_t::get_fab(q);
const factory_desc_t* const fd = qfab->get_desc();
for( uint qq = 0; qq < fd->get_product_count(); qq++ ) {
if( fd->get_product(qq)->get_output_type() == ware &&
!is_forbidden(qfab, fab, ware) &&
!is_connected(q, fab->get_pos().get_2d(), ware) ) {
// find out how much is there
const array_tpl<ware_production_t>& output = qfab->get_output();
uint ware_nr;
for(ware_nr=0; ware_nr<output.get_count() && output[ware_nr].get_typ()!=ware; ware_nr++ )
;
// ok, there is no connection and it is not banned, so we if there is enough for us
if( output[ware_nr].menge < 1+output[ware_nr].max/8 ) {
// try better supplying this first
if( qfab->get_suppliers().get_count()>0 && get_factory_tree_lowest_missing( qfab )) {
return true;
}
}
start = qfab;
ziel = fab;
freight = ware;
return true;
}
}
}
// completely supplied???
}
return false;
}
/* recursive lookup of a tree and how many factories must be at least connected
* returns -1, if this tree is can't be completed
*/
int ai_goods_t::get_factory_tree_missing_count( fabrik_t *fab )
{
int numbers=0; // how many missing?
factory_desc_t const& d = *fab->get_desc();
// ok, this is a source ...
if (d.is_producer_only()) {
return 0;
}
bool complete = false; // found at least one factory
// now check for all
for (int i = 0; i < d.get_supplier_count(); ++i) {
goods_desc_t const* const ware = d.get_supplier(i)->get_input_type();
if (!d.is_consumer_only())
{
complete = false;
}
for(auto q : fab->get_suppliers()) {
fabrik_t* const qfab = fabrik_t::get_fab(q);
if(!qfab) {
dbg->error("fabrik_t::get_fab()","fab %s at %s does not find supplier at %s.", fab->get_name(), fab->get_pos().get_str(), q.get_str());
continue;
}
if( !is_forbidden( qfab, fab, ware ) ) {
const factory_desc_t* const fd = qfab->get_desc();
for (uint qq = 0; qq < fd->get_product_count(); qq++) {
if (fd->get_product(qq)->get_output_type() == ware ) {
int n = get_factory_tree_missing_count( qfab );
if(n>=0) {
complete = true;
if (!is_connected(q, fab->get_pos().get_2d(), ware)) {
numbers += 1;
}
numbers += n;
}
}
}
}
}
if(!complete) {
return -1;
}
}
return numbers;
}
void add_neighbourhood( vector_tpl<koord> &list, const uint16 size)
{
uint32 old_size = list.get_count();
koord test;
for( uint32 i = 0; i < old_size; i++ ) {
for( test.x = -size; test.x < size+1; test.x++ ) {
for( test.y = -size; test.y < size+1; test.y++ ) {
list.append_unique( list[i] + test );
}
}
}
}
bool ai_goods_t::suche_platz1_platz2(fabrik_t *qfab, fabrik_t *zfab, int length )
{
clean_marker(platz1,size1);
clean_marker(platz2,size2);
koord start( qfab->get_pos().get_2d() );
koord start_size( length, 0 );
koord ziel( zfab->get_pos().get_2d() );
koord ziel_size( length, 0 );
bool ok = false;
bool has_ziel = false;
if(qfab->get_desc()->get_placement()!=factory_desc_t::Water) {
if( length == 0 ) {
vector_tpl<koord3d> tile_list[2];
uint16 const cov = welt->get_settings().get_station_coverage_factories() - 1;
koord test;
for( uint8 i = 0; i < 2; i++ ) {
fabrik_t *fab = i==0 ? qfab : zfab;
vector_tpl<koord> fab_tiles;
fab->get_tile_list( fab_tiles );
add_neighbourhood( fab_tiles, cov );
vector_tpl<koord> one_more( fab_tiles );
add_neighbourhood( one_more, 1 );
// Any halts here?
vector_tpl<koord> halts;
FOR(vector_tpl<koord>, const& j, one_more) {
halthandle_t const halt = get_halt(j);
if( halt.is_bound() && !halts.is_contained(halt->get_basis_pos()) ) {
bool halt_connected = halt->get_fab_list().is_contained( fab );
FOR( slist_tpl<haltestelle_t::tile_t>, const& i, halt->get_tiles() ) {
koord const pos = i.grund->get_pos().get_2d();
if( halt_connected || fab_tiles.is_contained(pos) ) {
halts.append_unique( pos );
}
}
}
}
add_neighbourhood( halts, 1 );
vector_tpl<koord> *next = &halts;
for( uint8 k = 0; k < 2; k++ ) {
// On which tiles we can start?
FOR(vector_tpl<koord>, const& j, *next) {
grund_t const* const gr = welt->lookup_kartenboden(j);
if( gr && gr->get_grund_hang() == slope_t::flat && !gr->hat_wege() && !gr->get_leitung() ) {
tile_list[i].append_unique( gr->get_pos() );
}
}
if( !tile_list[i].empty() ) {
// Skip, if found tiles beneath halts.
break;
}
next = &fab_tiles;
}
}
// Test which tiles are the best:
way_builder_t bauigel(this);
bauigel.init_builder( way_builder_t::strasse, road_weg, tunnel_builder_t::get_tunnel_desc(road_wt,road_weg->get_topspeed(),welt->get_timeline_year_month()), bridge_builder_t::find_bridge(road_wt,road_weg->get_topspeed(),welt->get_timeline_year_month()) );
// we won't destroy cities (and save the money)
bauigel.set_keep_existing_faster_ways(true);
bauigel.set_keep_city_roads(true);
bauigel.set_maximum(10000);
bauigel.calc_route(tile_list[0], tile_list[1]);
if( bauigel.get_count() > 2 ) {
// Sometimes reverse route is the best, so we have to change the koords.
koord3d_vector_t const& r = bauigel.get_route();
start = r.front().get_2d();
ziel = r.back().get_2d();
if( !tile_list[0].is_contained(r.front()) ) {
sim::swap(start, ziel);
}
ok = true;
has_ziel = true;
}
}
if( !ok ) {
ok = find_place(start, start_size, ziel, qfab->get_desc()->get_building()->get_size(qfab->get_rotate()) );
}
}
else {
// water factory => find harbour location
ok = find_harbour(start, start_size, ziel );
}
if( ok && !has_ziel ) {
// found a place, search for target
ok = find_place(ziel, ziel_size, start, zfab->get_desc()->get_building()->get_size(zfab->get_rotate()) );
}
INT_CHECK("simplay 1729");
if( !ok ) {
// no suitable locations found
DBG_MESSAGE( "ai_t::suche_platz1_platz2()", "no suitable locations found" );
}
else {
// save places
platz1 = start;
size1 = start_size;
platz2 = ziel;
size2 = ziel_size;
DBG_MESSAGE( "ai_t::suche_platz1_platz2()", "platz1=%d,%d platz2=%d,%d", platz1.x, platz1.y, platz2.x, platz2.y );
// reserve space with marker
set_marker( platz1, size1 );
set_marker( platz2, size2 );
}
return ok;
}
/**
* build docks and ships
*/
bool ai_goods_t::create_ship_transport_vehicle(fabrik_t *qfab, int vehicle_count)
{
// pak64 has barges ...
const vehicle_desc_t *v_second = NULL;
if(ship_vehicle->get_power()==0) {
v_second = ship_vehicle;
if(v_second->get_leader_count()==0 || v_second->get_leader(0)==NULL) {
// pushed barge?
if(ship_vehicle->get_trailer_count()>0 && ship_vehicle->get_trailer(0)!=NULL) {
v_second = ship_vehicle->get_trailer(0);
}
else {
return false;
}
}
else {
ship_vehicle = v_second->get_leader(0);
}
}
DBG_MESSAGE( "ai_goods_t::create_ship_transport_vehicle()", "for %i ships", vehicle_count );
if( convoihandle_t::is_exhausted() ) {
// too many convois => cannot do anything about this ...
return false;
}
// must remove marker
grund_t* gr = welt->lookup_kartenboden(platz1);
if (gr) {
gr->obj_loesche_alle(this);
}
// try to built dock
const building_desc_t* h = hausbauer_t::get_random_station(building_desc_t::dock, water_wt, welt->get_timeline_year_month(), haltestelle_t::WARE);
if(h==NULL || !call_general_tool(TOOL_BUILD_STATION, platz1, h->get_name())) {
return false;
}
// sea pos (and not on harbour ... )
halthandle_t halt = haltestelle_t::get_halt(gr->get_pos(),this);
koord pos1 = platz1 - koord(gr->get_grund_hang())*h->get_size().y;
koord best_pos = pos1;
uint16 const cov = welt->get_settings().get_station_coverage_factories() - 1;
for (int y = pos1.y - cov; y <= pos1.y + cov; ++y) {
for (int x = pos1.x - cov; x <= pos1.x + cov; ++x) {
koord p(x,y);
grund_t *gr = welt->lookup_kartenboden(p);
if( gr->is_water() && halt == haltestelle_t::get_halt( gr->get_pos(), this ) && gr->get_depot()==NULL ) {
if( koord_distance(best_pos,platz2)<koord_distance(p,platz2) ) {
best_pos = p;
}
}
}
}
// no stop position found
if(best_pos==koord::invalid) {
return false;
}
// since 86.01 we use lines for vehicles ...
schedule_t *schedule=new ship_schedule_t();
schedule->append( welt->lookup_kartenboden(best_pos), 0 );
schedule->append( welt->lookup(qfab->get_pos()), 100 );
schedule->set_current_stop( 1 );
schedule->finish_editing();
linehandle_t line=simlinemgmt.create_line(simline_t::shipline,this,schedule);
delete schedule;
// now create all vehicles as convois
for(int i=0; i<vehicle_count; i++) {
if( convoihandle_t::is_exhausted() ) {
// too many convois => cannot do anything about this ...
return i>0;
}
vehicle_t* v = vehicle_builder_t::build( qfab->get_pos(), this, NULL, ship_vehicle);
convoi_t* cnv = new convoi_t(this);
// give the new convoi name from first vehicle
cnv->set_name(v->get_desc()->get_name());
cnv->add_vehicle( v );
// two part consist
if(v_second!=NULL) {
v = vehicle_builder_t::build( qfab->get_pos(), this, NULL, v_second );
cnv->add_vehicle( v );
}
cnv->set_line(line);
cnv->start();
}
clean_marker(platz1,size1);
clean_marker(platz2,size2);
platz1 += koord(welt->lookup_kartenboden(platz1)->get_grund_hang());
return true;
}
void ai_goods_t::create_road_transport_vehicle(fabrik_t *qfab, int vehicle_count)
{
const building_desc_t* fh = hausbauer_t::get_random_station(building_desc_t::generic_stop, road_wt, welt->get_timeline_year_month(), haltestelle_t::WARE);
// succeed in frachthof creation
if(fh && call_general_tool(TOOL_BUILD_STATION, platz1, fh->get_name()) && call_general_tool(TOOL_BUILD_STATION, platz2, fh->get_name()) ) {
koord3d pos1 = welt->lookup_kartenboden(platz1)->get_pos();
koord3d pos2 = welt->lookup_kartenboden(platz2)->get_pos();
int start_location=0;
// sometimes, when factories are very close, we need exact calculation
const koord3d& qpos = qfab->get_pos();
if ((qpos.x - platz1.x) * (qpos.x - platz1.x) + (qpos.y - platz1.y) * (qpos.y - platz1.y) >
(qpos.x - platz2.x) * (qpos.x - platz2.x) + (qpos.y - platz2.y) * (qpos.y - platz2.y)) {
start_location = 1;
}
// calculate vehicle start position
koord3d startpos=(start_location==0)?pos1:pos2;
ribi_t::ribi w_ribi = welt->lookup(startpos)->get_weg_ribi_unmasked(road_wt);
// now start all vehicle one field before, so they load immediately
startpos = welt->lookup_kartenboden(koord(startpos.get_2d())+koord(w_ribi))->get_pos();
// since 86.01 we use lines for road vehicles ...
schedule_t *schedule=new truck_schedule_t();
schedule->append(welt->lookup(pos1), start_location == 0 ? 100 : 0);
schedule->append(welt->lookup(pos2), start_location == 1 ? 100 : 0);
schedule->set_current_stop( start_location );
schedule->finish_editing();
linehandle_t line=simlinemgmt.create_line(simline_t::truckline,this,schedule);
delete schedule;
// now create all vehicles as convois
for(int i=0; i<vehicle_count; i++) {
if( convoihandle_t::is_exhausted() ) {
// too many convois => cannot do anything about this ...
return;
}
vehicle_t* v = vehicle_builder_t::build(startpos, this, NULL, road_vehicle);
convoi_t* cnv = new convoi_t(this);
// give the new convoi name from first vehicle
cnv->set_name(v->get_desc()->get_name());
cnv->add_vehicle( v );
cnv->set_line(line);
cnv->start();
}
}
}
void ai_goods_t::create_rail_transport_vehicle(const koord platz1, const koord platz2, int vehicle_count, int minimum_loading)
{
// now obeys timeline and use "more clever" scheme for vehicle selection
schedule_t *schedule;
if( convoihandle_t::is_exhausted() ) {
// too many convois => cannot do anything about this ...
return;
}
convoi_t* cnv = new convoi_t(this);
koord3d pos1= welt->lookup_kartenboden(platz1)->get_pos();
koord3d pos2 = welt->lookup_kartenboden(platz2)->get_pos();
// probably need to electrify the track?
if( rail_engine->get_engine_type()==vehicle_desc_t::electric ) {
// we need overhead wires
const way_obj_desc_t *e = wayobj_t::get_overhead_line(track_wt,welt->get_timeline_year_month());
tool_build_wayobj_t tool;
tool.set_default_param(e->get_name());
tool.init( this );
tool.work( this, welt->lookup_kartenboden(platz1)->get_pos() );
tool.work( this, welt->lookup_kartenboden(platz2)->get_pos() );
tool.exit( this );
}
koord3d start_pos = welt->lookup_kartenboden(pos1.get_2d() + (abs(size1.x)>abs(size1.y) ? koord(size1.x,0) : koord(0,size1.y)))->get_pos();
vehicle_t* v = vehicle_builder_t::build( start_pos, this, NULL, rail_engine);
// give the new convoi name from first vehicle
cnv->set_name(rail_engine->get_name());
cnv->add_vehicle( v );
DBG_MESSAGE( "ai_goods_t::create_rail_transport_vehicle","for %i cars",vehicle_count);
/* now we add cars:
* check here also for introduction years
*/
for(int i = 0; i < vehicle_count; i++) {
// use the vehicle we searched before
vehicle_t* v = vehicle_builder_t::build(start_pos, this, NULL, rail_vehicle);
cnv->add_vehicle( v );
}
schedule = cnv->front()->generate_new_schedule();
schedule->set_current_stop( 0 );
schedule->append(welt->lookup(pos1), minimum_loading);
schedule->append(welt->lookup(pos2), 0);
schedule->finish_editing();
cnv->set_schedule(schedule);
cnv->start();
}
/**
* build a station
* Can fail even though check has been done before
*/
int ai_goods_t::baue_bahnhof(const koord* p, int vehicle_count)
{
int laenge = max(((rail_vehicle->get_length()*vehicle_count)+rail_engine->get_length()+CARUNITS_PER_TILE-1)/CARUNITS_PER_TILE,1);
int baulaenge = 0;
ribi_t::ribi ribi = welt->lookup_kartenboden(*p)->get_weg_ribi(track_wt);
koord zv ( ribi );
koord t = *p;
bool ok = true;
for( int i=0; i<laenge; i++ ) {
grund_t *gr = welt->lookup_kartenboden(t);
ok &= (gr != NULL) && !gr->has_two_ways() && gr->get_weg_hang()==slope_t::flat;
if(!ok) {
break;
}
baulaenge ++;
t += zv;
}
// too short
if(baulaenge<=1) {
return 0;
}
// to avoid broken stations, they will be always built next to an existing
bool make_all_bahnhof=false;
// find a freight train station
const building_desc_t* desc = hausbauer_t::get_random_station(building_desc_t::generic_stop, track_wt, welt->get_timeline_year_month(), haltestelle_t::WARE);
if(desc==NULL) {
// no freight station
return 0;
}
koord pos;
for( pos=t-zv; pos!=*p; pos-=zv ) {
if( make_all_bahnhof ||
get_halt(pos+koord(-1,-1)).is_bound() ||
get_halt(pos+koord(-1, 1)).is_bound() ||
get_halt(pos+koord( 1,-1)).is_bound() ||
get_halt(pos+koord( 1, 1)).is_bound()
) {
// start building, if next to an existing station
make_all_bahnhof = true;
call_general_tool( TOOL_BUILD_STATION, pos, desc->get_name() );
}
INT_CHECK("simplay 753");
}
// now add the other squares (going backwards)
for( pos=*p; pos!=t; pos+=zv ) {
if( !get_halt(pos).is_bound() ) {
call_general_tool( TOOL_BUILD_STATION, pos, desc->get_name() );
}
}
laenge = min( vehicle_count, (baulaenge*CARUNITS_PER_TILE - rail_engine->get_length())/rail_vehicle->get_length() );
//DBG_MESSAGE("ai_goods_t::baue_bahnhof","Final station at (%i,%i) with %i tiles for %i cars",p->x,p->y,baulaenge,laenge);
return laenge;
}
/**
* built a very simple track with just the minimum effort
* usually good enough, since it can use road crossings
*/
bool ai_goods_t::create_simple_rail_transport()
{
clean_marker(platz1,size1);
clean_marker(platz2,size2);
way_builder_t bauigel(this);
bauigel.init_builder( way_builder_t::schiene|way_builder_t::bot_flag, rail_weg, tunnel_builder_t::get_tunnel_desc(track_wt,rail_engine->get_topspeed(),welt->get_timeline_year_month()), bridge_builder_t::find_bridge(track_wt,rail_engine->get_topspeed(),welt->get_timeline_year_month()) );
bauigel.set_keep_existing_ways(false);
// for stations
way_builder_t bauigel1(this);
bauigel1.init_builder( way_builder_t::schiene|way_builder_t::bot_flag, rail_weg, tunnel_builder_t::get_tunnel_desc(track_wt,rail_engine->get_topspeed(),welt->get_timeline_year_month()), bridge_builder_t::find_bridge(track_wt,rail_engine->get_topspeed(),welt->get_timeline_year_month()) );
bauigel1.set_keep_existing_ways(false);
way_builder_t bauigel2(this);
bauigel2.init_builder( way_builder_t::schiene|way_builder_t::bot_flag, rail_weg, tunnel_builder_t::get_tunnel_desc(track_wt,rail_engine->get_topspeed(),welt->get_timeline_year_month()), bridge_builder_t::find_bridge(track_wt,rail_engine->get_topspeed(),welt->get_timeline_year_month()) );
bauigel2.set_keep_existing_ways(false);
// first: make plain stations tiles as intended
sint8 z1 = max( welt->get_water_hgt(platz1) + 1, welt->lookup_kartenboden(platz1)->get_hoehe() );
koord k = platz1;
koord diff1( sgn(size1.x), sgn(size1.y) );
koord perpend( sgn(size1.y), sgn(size1.x) );
while(k!=size1+platz1) {
climate c = welt->get_climate(k);
if(!welt->flatten_tile( this, k, z1 )) {
return false;
}
// ensure is land
grund_t* bd = welt->lookup_kartenboden(k);
if (bd->get_typ() == grund_t::wasser) {
welt->set_water_hgt_nocheck(k, bd->get_hoehe()-1);
welt->access(k)->correct_water();
welt->set_climate(k, c, true);
}
k += diff1;
}
// make the second ones flat ...
sint8 z2 = max( welt->get_water_hgt(platz2) + 1, welt->lookup_kartenboden(platz2)->get_hoehe() );
k = platz2;
perpend = koord( sgn(size2.y), sgn(size2.x) );
koord diff2( sgn(size2.x), sgn(size2.y) );
while(k!=size2+platz2) {
climate c = welt->get_climate(k);
if(!welt->flatten_tile(this,k,z2)) {
return false;
}
// ensure is land
grund_t* bd = welt->lookup_kartenboden(k);
if (bd->get_typ() == grund_t::wasser) {
welt->set_water_hgt_nocheck(k, bd->get_hoehe()-1);
welt->access(k)->correct_water();
welt->set_climate(k, c, true);
}
k += diff2;
}
bauigel1.calc_route( koord3d(platz1,z1), koord3d(platz1+size1-diff1, z1));
bauigel2.calc_route( koord3d(platz2,z2), koord3d(platz2+size2-diff2, z2));
// build immediately, otherwise way-builder could get confused and connect way to a tile in the middle of the station
bauigel1.build();
bauigel2.build();
vector_tpl<koord3d> starttiles, endtiles;
// now calc the route
starttiles.append(welt->lookup_kartenboden(platz1 + size1)->get_pos());
starttiles.append(welt->lookup_kartenboden(platz1 - diff1)->get_pos());
endtiles.append(welt->lookup_kartenboden(platz2 + size2)->get_pos());
endtiles.append(welt->lookup_kartenboden(platz2 - diff2)->get_pos());
bauigel.calc_route( starttiles, endtiles );
INT_CHECK("ai_goods 672");
// build only if enough cash available
bool build_no_tf = (bauigel.get_count() > 4) && (bauigel.calc_costs() <= finance->get_netwealth());
// now try route with terraforming
way_builder_t baumaulwurf(this);
baumaulwurf.init_builder( way_builder_t::schiene|way_builder_t::bot_flag|way_builder_t::terraform_flag, rail_weg, tunnel_builder_t::get_tunnel_desc(track_wt,rail_engine->get_topspeed(),welt->get_timeline_year_month()), bridge_builder_t::find_bridge(track_wt,rail_engine->get_topspeed(),welt->get_timeline_year_month()) );
baumaulwurf.set_keep_existing_ways(false);
baumaulwurf.calc_route( starttiles, endtiles );
// build with terraforming if shorter and enough money is available
bool with_tf = (baumaulwurf.get_count() > 4) && (10*baumaulwurf.get_count() < 9*bauigel.get_count() || bauigel.get_count() <= 4);
// too expensive ?
with_tf = with_tf && (baumaulwurf.calc_costs() <= finance->get_netwealth());
// now build with or without terraforming
if (with_tf) {
baumaulwurf.build();
}
else if (build_no_tf) {
bauigel.build();
}
// connect track to station
if( with_tf || build_no_tf ) {
DBG_MESSAGE("ai_goods_t::create_simple_rail_transport()","building simple track from %d,%d to %d,%d",platz1.x, platz1.y, platz2.x, platz2.y);
// connect to track
koord3d_vector_t const& r = with_tf ? baumaulwurf.get_route() : bauigel.get_route();
koord3d tile1 = r.front();
koord3d tile2 = r.back();
if (!starttiles.is_contained(tile1)) sim::swap(tile1, tile2);
// No botflag, since we want to connect with the station.
bauigel.init_builder( way_builder_t::schiene, rail_weg, tunnel_builder_t::get_tunnel_desc(track_wt,rail_engine->get_topspeed(),welt->get_timeline_year_month()), bridge_builder_t::find_bridge(track_wt,rail_engine->get_topspeed(),welt->get_timeline_year_month()) );
bauigel.calc_straight_route( koord3d(platz1,z1), tile1);
bauigel.build();
bauigel.calc_straight_route( koord3d(platz2,z2), tile2);
bauigel.build();
// If connection is built not at platz1/2, we must alter platz1/2, otherwise baue_bahnhof gets confused.
if( tile1.get_2d() != platz1 + size1 ) {
platz1 = platz1 + size1 - diff1;
size1 = -size1;
}
if( tile2.get_2d() != platz2 + size2 ) {
platz2 = platz2 + size2 - diff2;
}
return true;
}
else {
// not ok: remove station ...
k = platz1;
while(k!=size1+platz1) {
int cost = -welt->lookup_kartenboden(k)->weg_entfernen( track_wt, true );
book_construction_costs(this, cost, k, track_wt);
k += diff1;
}
k = platz2;
while(k!=size2+platz2) {
int cost = -welt->lookup_kartenboden(k)->weg_entfernen( track_wt, true );
book_construction_costs(this, cost, k, track_wt);
k += diff2;
}
}
return false;
}
// the normal length procedure for freight AI
void ai_goods_t::step()
{
// needed for schedule of stops ...
player_t::step();
if(!active) {
// I am off ...
return;
}
// one route per month ...
if( welt->get_steps() < next_construction_steps ) {
return;
}
if( finance->get_netwealth() < (finance->get_starting_money()/8) ) {
// nothing to do but to remove unneeded convois to gain some money
state = CHECK_CONVOI;
}
switch(state) {
case NR_INIT:
state = NR_SAMMLE_ROUTEN;
count = 0;
built_update_headquarter();
if(root==NULL) {
// find a tree root to complete
weighted_vector_tpl<fabrik_t *> start_fabs(20);
FOR(vector_tpl<fabrik_t*>, const fab, welt->get_fab_list()) {
// consumer and not completely overcrowded
if( fab->get_desc()->is_consumer_only() && fab->get_status() < fabrik_t::bad ) {
int missing = get_factory_tree_missing_count( fab );
if( missing>0 ) {
start_fabs.append( fab, 100/(missing+1)+1 );
}
}
}
if( !start_fabs.empty() ) {
root = pick_any_weighted(start_fabs);
}
}
// still nothing => we have to check convois ...
if(root==NULL) {
state = CHECK_CONVOI;
}
break;
/* try to built a network:
* last target also new target ...
*/
case NR_SAMMLE_ROUTEN:
// @author Bernd Gabriel: root may have been deleted and thus set to NULL.
if (root == NULL)
{
state = CHECK_CONVOI;
break;
}
if( get_factory_tree_lowest_missing(root) ) {
if( start->get_desc()->get_placement()!=factory_desc_t::Water || vehicle_search( water_wt, 0, 10, freight, false)!=NULL ) {
DBG_MESSAGE("ai_goods_t::do_ki", "Consider route from %s (%i,%i) to %s (%i,%i)", start->get_name(), start->get_pos().x, start->get_pos().y, ziel->get_name(), ziel->get_pos().x, ziel->get_pos().y );
state = NR_BAUE_ROUTE1;
}
else {
// add to impossible connections
forbidden_connections.append( new fabconnection_t( start, ziel, freight ) );
state = CHECK_CONVOI;
}
}
else {
// did all I could do here ...
root = NULL;
state = CHECK_CONVOI;
}
break;
// now we need so select the cheapest mean to get maximum profit
case NR_BAUE_ROUTE1:
{
// @author Bernd Gabriel: start or ziel may have been deleted and thus set to NULL.
if (start == NULL || ziel == NULL)
{
state = CHECK_CONVOI;
break;
}
/* if we reached here, we decide to built a route;
* the KI just chooses the way to run the operation at maximum profit (minimum loss).
* The KI will built also a loosing route; this might be required by future versions to
* be able to built a network!
*/
/* for the calculation we need:
* a suitable car (and engine)
* a suitable way
*/
const uint32 dist = koord_distance( start->get_pos(), ziel->get_pos() );
const uint32 distance_meters = dist * welt->get_settings().get_meters_per_tile();
// guess the "optimum" speed (usually a little too low)
sint32 best_rail_speed = 80;// is ok enough for goods, was: min(60+freight->get_speed_bonus()*5, 140 );
const way_desc_t* w_desc = world()->get_settings().get_intercity_road_type(world()->get_timeline_year_month());
sint32 best_road_speed = w_desc ? world()->get_settings().get_intercity_road_type(world()->get_timeline_year_month())->get_topspeed() : world()->get_settings().get_intercity_road_type(0)->get_topspeed(); // was: min(60+freight->get_adjusted_speed_bonus(distance_meters)*5, 130 );
INT_CHECK("simplay 1265");
// is rail transport allowed?
if(rail_transport) {
// any rail car that transport this good (actually this way_t the largest)
rail_vehicle = vehicle_search( track_wt, 0, best_rail_speed, freight, true);
}
rail_engine = NULL;
rail_weg = NULL;
DBG_MESSAGE("do_ki()","rail vehicle %p",rail_vehicle);
// is road transport allowed?
if(road_transport) {
// any road car that transport this good (actually this returns the largest)
road_vehicle = vehicle_search( road_wt, 10, best_road_speed, freight, false);
}
road_weg = NULL;
DBG_MESSAGE("do_ki()","road vehicle %p",road_vehicle);
ship_vehicle = NULL;
if(start->get_desc()->get_placement()==factory_desc_t::Water) {
// largest ship available
ship_vehicle = vehicle_search( water_wt, 0, 20, freight, false);
}
INT_CHECK("simplay 1265");
// properly calculate production
const array_tpl<ware_production_t>& output = start->get_output();
uint start_ware=0;
while( start_ware<output.get_count() && output[start_ware].get_typ()!=freight ) {
start_ware++;
}
assert( start_ware<output.get_count() );
const int prod = min((uint32)ziel->get_base_production(),
( start->get_base_production() * start->get_desc()->get_product(start_ware)->get_factor() )/256u - (uint32)(start->get_output()[start_ware].get_stat(1, FAB_GOODS_DELIVERED)) );
DBG_MESSAGE("do_ki()","check railway");
/* calculate number of cars for railroad */
count_rail=255; // no cars yet
if( rail_vehicle!=NULL ) {
// if our car is faster: well use slower speed to save money
best_rail_speed = min(51, rail_vehicle->get_topspeed());
// for engine: guess number of cars
count_rail = (prod*dist) / (rail_vehicle->get_total_capacity()*best_rail_speed)+1;
// assume the engine weight 100 tons for power needed calculation
int total_weight = count_rail*( rail_vehicle->get_total_capacity()*freight->get_weight_per_unit() + rail_vehicle->get_weight() );
// sint32 power_needed = (long)(((best_rail_speed*best_rail_speed)/2500.0+1.0)*(100.0+count_rail*(rail_vehicle->get_weight()+rail_vehicle->get_total_capacity()*freight->get_weight_per_unit()*0.001)));
rail_engine = vehicle_search( track_wt, total_weight/1000, best_rail_speed, NULL, wayobj_t::default_oberleitung!=NULL);
if( rail_engine!=NULL ) {
best_rail_speed = min(rail_engine->get_topspeed(),rail_vehicle->get_topspeed());
// find cheapest track with that speed (and no monorail/elevated/tram tracks, please)
rail_weg = way_builder_t::weg_search( track_wt, best_rail_speed, welt->get_timeline_year_month(),type_flat );
if( rail_weg!=NULL ) {
if( best_rail_speed>rail_weg->get_topspeed() ) {
best_rail_speed = rail_weg->get_topspeed();
}
// no train can have more than 15 cars
count_rail = min( 22, (3*prod*dist) / (rail_vehicle->get_total_capacity()*best_rail_speed*2) );
// if engine too week, reduce number of cars
if( count_rail*80*64>(int)(rail_engine->get_power()*rail_engine->get_gear()) ) {
count_rail = rail_engine->get_power()*rail_engine->get_gear()/(80*64);
}
count_rail = ((count_rail+1)&0x0FE)+1;
DBG_MESSAGE("ai_goods_t::do_ki()","Engine %s guess to need %d rail cars %s for route (%s)", rail_engine->get_name(), count_rail, rail_vehicle->get_name(), rail_weg->get_name() );
}
}
if( rail_engine==NULL || rail_weg==NULL ) {
// no rail transport possible
DBG_MESSAGE("ai_goods_t::do_ki()","No railway possible.");
rail_vehicle = NULL;
count_rail = 255;
}
}
INT_CHECK("simplay 1265");
DBG_MESSAGE("do_ki()","check railway");
/* calculate number of cars for road; much easier */
count_road=255; // no cars yet
if( road_vehicle!=NULL ) {
best_road_speed = road_vehicle->get_topspeed();
// find cheapest road
road_weg = way_builder_t::weg_search(road_wt, best_road_speed, road_vehicle->get_weight(), welt->get_timeline_year_month(), type_flat, 1);
if( road_weg!=NULL ) {
if( best_road_speed>road_weg->get_topspeed() ) {
best_road_speed = road_weg->get_topspeed();
}
// minimum vehicle is 1, maximum vehicle is 48, more just result in congestion
count_road = min( 254, (prod*dist) / (road_vehicle->get_total_capacity()*best_road_speed*2)+2 );
DBG_MESSAGE("ai_goods_t::do_ki()","guess to need %d road cars %s for route %s", count_road, road_vehicle->get_name(), road_weg->get_name() );
}
else {
// no roads there !?!
DBG_MESSAGE("ai_goods_t::do_ki()","No roadway possible.");
}
}
// The logic above here is hopelessly broken for Extended for multiple reasons
// (meters_per_tile, bits_per_month, and pay by average speed)
// and appears to have been wrong even in standard.
// Maybe try to clean it up sometime. --neroden
// find the cheapest transport ...
// assume maximum cost
int cost_rail=0x7FFFFFFF, cost_road=0x7FFFFFFF;
// calculate cost for rail
if( count_rail<255 ) {
// calculated here, since the above number was based on production
// only uneven number of cars bigger than 3 makes sense ...
count_rail = max( 3, count_rail );
// Guess that average speed is half of "best" speed
const uint32 average_speed = best_rail_speed / 2;
const sint64 freight_revenue_per_trip = freight->get_total_fare(distance_meters) * rail_vehicle->get_capacity() * count_rail / 3000;
const sint64 freight_cost_per_trip
= ( (sint64) rail_vehicle->get_running_cost(welt) * count_rail
+ rail_engine->get_running_cost(welt)