forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simfab.cc
2268 lines (1993 loc) · 67.8 KB
/
simfab.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
/*
* Fabrikfunktionen und Fabrikbau
*
* Hansjörg Malthaner
*
*
* 25.03.00 Anpassung der Lagerkapazitäten: min. 5 normale Lieferungen
* sollten an Lager gehalten werden.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "simdebug.h"
#include "display/simimg.h"
#include "simcolor.h"
#include "boden/grund.h"
#include "boden/boden.h"
#include "boden/fundament.h"
#include "simfab.h"
#include "simcity.h"
#include "simhalt.h"
#include "simtools.h"
#include "simware.h"
#include "simworld.h"
#include "besch/haus_besch.h"
#include "besch/ware_besch.h"
#include "player/simplay.h"
#include "simintr.h"
#include "obj/wolke.h"
#include "obj/gebaeude.h"
#include "obj/field.h"
#include "obj/leitung2.h"
#include "dataobj/settings.h"
#include "dataobj/environment.h"
#include "dataobj/translator.h"
#include "dataobj/loadsave.h"
#include "besch/fabrik_besch.h"
#include "bauer/hausbauer.h"
#include "bauer/warenbauer.h"
#include "bauer/fabrikbauer.h"
#include "gui/fabrik_info.h"
#include "utils/cbuffer_t.h"
#include "gui/simwin.h"
#include "display/simgraph.h"
// Fabrik_t
static const int FAB_MAX_INPUT = 15000;
karte_ptr_t fabrik_t::welt;
/**
* Convert internal values to displayed values
*/
sint64 convert_goods(sint64 value) { return ( (value + (1<<(fabrik_t::precision_bits-1))) >> fabrik_t::precision_bits ); }
sint64 convert_power(sint64 value) { return ( value >> POWER_TO_MW ); }
sint64 convert_boost(sint64 value) { return ( (value * 100 + (DEFAULT_PRODUCTION_FACTOR>>1)) >> DEFAULT_PRODUCTION_FACTOR_BITS ); }
/**
* Ordering based on relative distance to a fixed point `origin'.
*/
class RelativeDistanceOrdering
{
private:
const koord m_origin;
public:
RelativeDistanceOrdering(const koord& origin)
: m_origin(origin)
{ /* nothing */ }
/**
* Returns true if `a' is closer to the origin than `b', otherwise false.
*/
bool operator()(const koord& a, const koord& b) const
{
return koord_distance(m_origin, a) < koord_distance(m_origin, b);
}
};
void ware_production_t::init_stats()
{
for( int m=0; m<MAX_MONTH; ++m ) {
for( int s=0; s<MAX_FAB_GOODS_STAT; ++s ) {
statistics[m][s] = 0;
}
}
weighted_sum_storage = 0;
max_transit = 0;
}
void ware_production_t::roll_stats(sint64 aggregate_weight)
{
// calculate weighted average storage first
if( aggregate_weight>0 ) {
set_stat( weighted_sum_storage / aggregate_weight, FAB_GOODS_STORAGE );
}
for( int s=0; s<MAX_FAB_GOODS_STAT; ++s ) {
for( int m=MAX_MONTH-1; m>0; --m ) {
statistics[m][s] = statistics[m-1][s];
}
if( s==FAB_GOODS_TRANSIT ) {
// keep the current amount in transit
statistics[0][s] = statistics[1][s];
}
else {
statistics[0][s] = 0;
}
}
weighted_sum_storage = 0;
// restore current storage level
set_stat( menge, FAB_GOODS_STORAGE );
}
void ware_production_t::rdwr(loadsave_t *file)
{
if( file->is_loading() ) {
init_stats();
}
if( file->get_version()>112000 ) {
for( int s=0; s<MAX_FAB_GOODS_STAT; ++s ) {
for( int m=0; m<MAX_MONTH; ++m ) {
file->rdwr_longlong( statistics[m][s] );
}
}
file->rdwr_longlong( weighted_sum_storage );
}
else if( file->get_version()>=110005 ) {
// save/load statistics
for( int s=0; s<3; ++s ) {
for( int m=0; m<MAX_MONTH; ++m ) {
file->rdwr_longlong( statistics[m][s] );
}
}
file->rdwr_longlong( weighted_sum_storage );
}
if( file->is_loading() ) {
// recalc transit always on load
set_stat(0, FAB_GOODS_TRANSIT);
}
}
void ware_production_t::book_weighted_sum_storage(sint64 delta_time)
{
weighted_sum_storage += (sint64)menge * delta_time;
set_stat( menge, FAB_GOODS_STORAGE );
}
void fabrik_t::arrival_statistics_t::init()
{
for( uint32 s=0; s<SLOT_COUNT; ++s ) {
slots[s] = 0;
}
current_slot = 0;
active_slots = 0;
aggregate_arrival = 0;
scaled_demand = 0;
}
void fabrik_t::arrival_statistics_t::rdwr(loadsave_t *file)
{
if( file->get_version()>=110005 ) {
if( file->is_loading() ) {
aggregate_arrival = 0;
for( uint32 s=0; s<SLOT_COUNT; ++s ) {
file->rdwr_short( slots[s] );
aggregate_arrival += slots[s];
}
scaled_demand = 0;
}
else {
for( uint32 s=0; s<SLOT_COUNT; ++s ) {
file->rdwr_short( slots[s] );
}
}
file->rdwr_short( current_slot );
file->rdwr_short( active_slots );
}
else if( file->is_loading() ) {
init();
}
}
sint32 fabrik_t::arrival_statistics_t::advance_slot()
{
sint32 result = 0;
// advance to the next slot
++current_slot;
if( current_slot>=SLOT_COUNT ) {
current_slot = 0;
}
// handle expiration of past arrivals and reset slot to 0
if( slots[current_slot]>0 ) {
aggregate_arrival -= slots[current_slot];
slots[current_slot] = 0;
if( aggregate_arrival==0 ) {
// reset slot count to 0 as all previous arrivals have expired
active_slots = 0;
}
result |= ARRIVALS_CHANGED;
}
// count the number of slots covered since aggregate arrival last increased from 0 to +ve
if( active_slots>0 && active_slots<SLOT_COUNT ) {
++active_slots;
result |= ACTIVE_SLOTS_INCREASED;
}
return result;
}
void fabrik_t::arrival_statistics_t::book_arrival(const uint16 amount)
{
if( aggregate_arrival==0 ) {
// new arrival after complete inactivity -> start counting slots
active_slots = 1;
}
// increment current slot and aggregate arrival
slots[current_slot] += amount;
aggregate_arrival += amount;
}
void fabrik_t::update_transit( const ware_t *ware, bool add )
{
if( ware->index > warenbauer_t::INDEX_NONE ) {
// only for freights
fabrik_t *fab = get_fab( ware->get_zielpos() );
if( fab ) {
fab->update_transit_intern( ware, add );
}
}
}
// just for simplicity ...
void fabrik_t::update_transit_intern( const ware_t *ware, bool add )
{
FOR( array_tpl<ware_production_t>, &w, eingang ) {
if( w.get_typ()->get_index() == ware->index ) {
w.book_stat(add ? ware->menge : -ware->menge, FAB_GOODS_TRANSIT );
return;
}
}
}
void fabrik_t::init_stats()
{
for( int m=0; m<MAX_MONTH; ++m ) {
for( int s=0; s<MAX_FAB_STAT; ++s ) {
statistics[m][s] = 0;
}
}
weighted_sum_production = 0;
weighted_sum_boost_electric = 0;
weighted_sum_boost_pax = 0;
weighted_sum_boost_mail = 0;
weighted_sum_power = 0;
aggregate_weight = 0;
}
void fabrik_t::book_weighted_sums(sint64 delta_time)
{
aggregate_weight += delta_time;
// storage level of input/output stores
FOR(array_tpl<ware_production_t>, & g, eingang) {
g.book_weighted_sum_storage(delta_time);
}
FOR(array_tpl<ware_production_t>, & g, ausgang) {
g.book_weighted_sum_storage(delta_time);
}
// production level
const sint32 current_prod = get_current_production();
weighted_sum_production += current_prod * delta_time;
set_stat( current_prod, FAB_PRODUCTION );
// electricity, pax and mail boosts
weighted_sum_boost_electric += prodfactor_electric * delta_time;
set_stat( prodfactor_electric, FAB_BOOST_ELECTRIC );
weighted_sum_boost_pax += prodfactor_pax * delta_time;
weighted_sum_boost_mail += prodfactor_mail * delta_time;
// power produced or consumed
weighted_sum_power += power * delta_time;
set_stat( power, FAB_POWER );
}
void fabrik_t::update_scaled_electric_amount()
{
if( besch->get_electric_amount()==65535 ) {
// demand not specified in pak, use old fixed demands
scaled_electric_amount = prodbase * PRODUCTION_DELTA_T;
if( besch->is_electricity_producer() ) {
scaled_electric_amount *= 4;
}
return;
}
const sint64 prod = besch->get_produktivitaet();
scaled_electric_amount = (uint32)( (( (sint64)(besch->get_electric_amount()) * (sint64)prodbase + (prod >> 1) ) / prod) << POWER_TO_MW );
if( scaled_electric_amount == 0 ) {
prodfactor_electric = 0;
}
}
void fabrik_t::update_scaled_pax_demand()
{
// first, scaling based on current production base
const sint64 prod = besch->get_produktivitaet();
const sint64 besch_pax_demand = ( besch->get_pax_demand()==65535 ? besch->get_pax_level() : besch->get_pax_demand() );
// formula : besch_pax_demand * (current_production_base / besch_production_base); (prod >> 1) is for rounding
const uint32 pax_demand = (uint32)( ( besch_pax_demand * (sint64)prodbase + (prod >> 1) ) / prod );
// then, scaling based on month length
scaled_pax_demand = welt->scale_with_month_length(pax_demand);
if( scaled_pax_demand==0 && besch_pax_demand>0 ) {
scaled_pax_demand = 1; // since besch pax demand > 0 -> ensure no less than 1
}
// pax demand for fixed period length
arrival_stats_pax.set_scaled_demand( pax_demand );
}
void fabrik_t::update_scaled_mail_demand()
{
// first, scaling based on current production base
const sint64 prod = besch->get_produktivitaet();
const sint64 besch_mail_demand = ( besch->get_mail_demand()==65535 ? (besch->get_pax_level()>>2) : besch->get_mail_demand() );
// formula : besch_mail_demand * (current_production_base / besch_production_base); (prod >> 1) is for rounding
const uint32 mail_demand = (uint32)( ( besch_mail_demand * (sint64)prodbase + (prod >> 1) ) / prod );
// then, scaling based on month length
scaled_mail_demand = welt->scale_with_month_length(mail_demand);
if( scaled_mail_demand==0 && besch_mail_demand>0 ) {
scaled_mail_demand = 1; // since besch mail demand > 0 -> ensure no less than 1
}
// mail demand for fixed period length
arrival_stats_mail.set_scaled_demand( mail_demand );
}
void fabrik_t::update_prodfactor_pax()
{
// calculate pax boost based on arrival data and demand of the fixed-length period
const uint32 periods = welt->get_settings().get_factory_arrival_periods();
const uint32 slots = arrival_stats_pax.get_active_slots();
const uint32 pax_demand = ( periods==1 || slots*periods<=(uint32)SLOT_COUNT ?
arrival_stats_pax.get_scaled_demand() :
( slots==(uint32)SLOT_COUNT ?
arrival_stats_pax.get_scaled_demand() * periods :
(arrival_stats_pax.get_scaled_demand() * periods * slots) >> SLOT_BITS ) );
const uint32 pax_arrived = arrival_stats_pax.get_aggregate_arrival();
if( pax_demand==0 || pax_arrived==0 || besch->get_pax_boost()==0 ) {
prodfactor_pax = 0;
}
else if( pax_arrived>=pax_demand ) {
// maximum boost
prodfactor_pax = besch->get_pax_boost();
}
else {
// pro-rata boost : (pax_arrived / pax_demand) * besch_pax_boost; (pax_demand >> 1) is for rounding
prodfactor_pax = (sint32)( ( (sint64)pax_arrived * (sint64)(besch->get_pax_boost()) + (sint64)(pax_demand >> 1) ) / (sint64)pax_demand );
}
set_stat(prodfactor_pax, FAB_BOOST_PAX);
}
void fabrik_t::update_prodfactor_mail()
{
// calculate mail boost based on arrival data and demand of the fixed-length period
const uint32 periods = welt->get_settings().get_factory_arrival_periods();
const uint32 slots = arrival_stats_mail.get_active_slots();
const uint32 mail_demand = ( periods==1 || slots*periods<=(uint32)SLOT_COUNT ?
arrival_stats_mail.get_scaled_demand() :
( slots==(uint32)SLOT_COUNT ?
arrival_stats_mail.get_scaled_demand() * periods :
(arrival_stats_mail.get_scaled_demand() * periods * slots) >> SLOT_BITS ) );
const uint32 mail_arrived = arrival_stats_mail.get_aggregate_arrival();
if( mail_demand==0 || mail_arrived==0 || besch->get_mail_boost()==0 ) {
prodfactor_mail = 0;
}
else if( mail_arrived>=mail_demand ) {
// maximum boost
prodfactor_mail = besch->get_mail_boost();
}
else {
// pro-rata boost : (mail_arrived / mail_demand) * besch_mail_boost; (mail_demand >> 1) is for rounding
prodfactor_mail = (sint32)( ( (sint64)mail_arrived * (sint64)(besch->get_mail_boost()) + (sint64)(mail_demand >> 1) ) / (sint64)mail_demand );
}
set_stat(prodfactor_mail, FAB_BOOST_MAIL);
}
void fabrik_t::recalc_demands_at_target_cities()
{
if (!welt->get_settings().get_factory_enforce_demand()) {
// demand not enforced -> no splitting of demands
FOR(vector_tpl<stadt_t*>, const c, target_cities) {
c->access_target_factories_for_pax().update_factory( this, scaled_pax_demand << DEMAND_BITS);
c->access_target_factories_for_mail().update_factory(this, scaled_mail_demand << DEMAND_BITS);
}
return;
}
if (target_cities.empty()) {
// nothing to do
return;
}
else if( target_cities.get_count()==1 ) {
// only 1 target city -> no need to apportion pax/mail demand
target_cities[0]->access_target_factories_for_pax().update_factory(this, (scaled_pax_demand << DEMAND_BITS));
target_cities[0]->access_target_factories_for_mail().update_factory(this, (scaled_mail_demand << DEMAND_BITS));
}
else {
// more than 1 target cities -> need to apportion pax/mail demand among the cities
static vector_tpl<uint32> weights(8);
weights.clear();
uint32 sum_of_weights = 0;
// first, calculate the weights
for( uint32 c=0; c<target_cities.get_count(); ++c ) {
weights.append( weight_by_distance( target_cities[c]->get_einwohner(), shortest_distance( get_pos().get_2d(), target_cities[c]->get_center() ) ) );
sum_of_weights += weights[c];
}
// finally, apportion the pax/mail demand; formula : demand * (city_weight / aggregate_city_weight); (sum_of_weights >> 1) is for rounding
for( uint32 c=0; c<target_cities.get_count(); ++c ) {
const uint32 pax_amount = (uint32)(( (sint64)(scaled_pax_demand << DEMAND_BITS) * (sint64)weights[c] + (sint64)(sum_of_weights >> 1) ) / (sint64)sum_of_weights);
target_cities[c]->access_target_factories_for_pax().update_factory(this, pax_amount);
const uint32 mail_amount = (uint32)(( (sint64)(scaled_mail_demand << DEMAND_BITS) * (sint64)weights[c] + (sint64)(sum_of_weights >> 1) ) / (sint64)sum_of_weights);
target_cities[c]->access_target_factories_for_mail().update_factory(this, mail_amount);
}
}
}
void fabrik_t::recalc_storage_capacities()
{
if( besch->get_field_group() ) {
// with fields -> calculate based on capacities contributed by fields
const uint32 ware_types = eingang.get_count() + ausgang.get_count();
if( ware_types>0 ) {
// calculate total storage capacity contributed by fields
const field_group_besch_t *const field_group = besch->get_field_group();
sint32 field_capacities = 0;
FOR(vector_tpl<field_data_t>, const& f, fields) {
field_capacities += field_group->get_field_class(f.field_class_index)->get_storage_capacity();
}
const sint32 share = (sint32)( ( (sint64)field_capacities << precision_bits ) / (sint64)ware_types );
// first, for input goods
FOR(array_tpl<ware_production_t>, & g, eingang) {
for( int b=0; b<besch->get_lieferanten(); ++b ) {
const fabrik_lieferant_besch_t *const input = besch->get_lieferant(b);
if (g.get_typ() == input->get_ware()) {
g.max = (input->get_kapazitaet() << precision_bits) + share;
}
}
}
// then, for output goods
FOR(array_tpl<ware_production_t>, & g, ausgang) {
for( uint b=0; b<besch->get_produkte(); ++b ) {
const fabrik_produkt_besch_t *const output = besch->get_produkt(b);
if (g.get_typ() == output->get_ware()) {
g.max = (output->get_kapazitaet() << precision_bits) + share;
}
}
}
}
}
else {
// without fields -> scaling based on prodbase
// first, for input goods
FOR(array_tpl<ware_production_t>, & g, eingang) {
for( int b=0; b<besch->get_lieferanten(); ++b ) {
const fabrik_lieferant_besch_t *const input = besch->get_lieferant(b);
if (g.get_typ() == input->get_ware()) {
g.max = (sint32)((sint64)(input->get_kapazitaet() << precision_bits) * (sint64)prodbase / (sint64)besch->get_produktivitaet());
}
}
}
// then, for output goods
FOR(array_tpl<ware_production_t>, & g, ausgang) {
for( uint b=0; b<besch->get_produkte(); ++b ) {
const fabrik_produkt_besch_t *const output = besch->get_produkt(b);
if (g.get_typ() == output->get_ware()) {
g.max = (sint32)((sint64)(output->get_kapazitaet() << precision_bits) * (sint64)prodbase / (sint64)besch->get_produktivitaet());
}
}
}
}
}
void fabrik_t::add_target_city(stadt_t *const city)
{
if( target_cities.append_unique(city) ) {
recalc_demands_at_target_cities();
}
}
void fabrik_t::remove_target_city(stadt_t *const city)
{
if( target_cities.is_contained(city) ) {
target_cities.remove(city);
city->access_target_factories_for_pax().remove_factory(this);
city->access_target_factories_for_mail().remove_factory(this);
recalc_demands_at_target_cities();
}
}
void fabrik_t::clear_target_cities()
{
FOR(vector_tpl<stadt_t*>, const c, target_cities) {
c->access_target_factories_for_pax().remove_factory(this);
c->access_target_factories_for_mail().remove_factory(this);
}
target_cities.clear();
}
void fabrik_t::set_base_production(sint32 p)
{
prodbase = p;
recalc_storage_capacities();
update_scaled_electric_amount();
update_scaled_pax_demand();
update_scaled_mail_demand();
update_prodfactor_pax();
update_prodfactor_mail();
recalc_demands_at_target_cities();
}
fabrik_t *fabrik_t::get_fab(const koord &pos)
{
const grund_t *gr = welt->lookup_kartenboden(pos);
if(gr) {
gebaeude_t *gb = gr->find<gebaeude_t>();
if(gb) {
return gb->get_fabrik();
}
}
return NULL;
}
void fabrik_t::link_halt(halthandle_t halt)
{
welt->access(pos.get_2d())->add_to_haltlist(halt);
}
void fabrik_t::unlink_halt(halthandle_t halt)
{
planquadrat_t *plan=welt->access(pos.get_2d());
if(plan) {
plan->remove_from_haltlist(halt);
}
}
void fabrik_t::add_lieferziel(koord ziel)
{
if( !lieferziele.is_contained(ziel) ) {
lieferziele.insert_ordered( ziel, RelativeDistanceOrdering(pos.get_2d()) );
// now tell factory too
fabrik_t * fab = fabrik_t::get_fab(ziel);
if (fab) {
fab->add_supplier(get_pos().get_2d());
}
}
}
void fabrik_t::rem_lieferziel(koord ziel)
{
lieferziele.remove(ziel);
}
fabrik_t::fabrik_t(loadsave_t* file)
{
besitzer_p = NULL;
power = 0;
power_demand = 0;
prodfactor_electric = 0;
lieferziele_active_last_month = 0;
pos = koord3d::invalid;
rdwr(file);
if( besch == NULL ) {
dbg->warning( "fabrik_t::fabrik_t()", "No pak-file for factory at (%s) - will not be built!", pos_origin.get_str() );
return;
}
else if( !welt->is_within_limits(pos_origin.get_2d()) ) {
dbg->warning( "fabrik_t::fabrik_t()", "%s is not a valid position! (Will not be built!)", pos_origin.get_str() );
besch = NULL; // to get rid of this broken factory later...
}
else {
baue(rotate, false, false);
// now get rid of construction image
for( sint16 y=0; y<besch->get_haus()->get_h(rotate); y++ ) {
for( sint16 x=0; x<besch->get_haus()->get_b(rotate); x++ ) {
gebaeude_t *gb = welt->lookup_kartenboden( pos_origin.get_2d()+koord(x,y) )->find<gebaeude_t>();
if( gb ) {
gb->add_alter(10000);
}
}
}
}
delta_sum = 0;
delta_menge = 0;
menge_remainder = 0;
total_input = total_transit = total_output = 0;
status = nothing;
currently_producing = false;
transformer_connected = false;
}
fabrik_t::fabrik_t(koord3d pos_, spieler_t* spieler, const fabrik_besch_t* fabesch, sint32 initial_prod_base) :
besch(fabesch),
pos(pos_)
{
this->pos.z = welt->max_hgt(pos.get_2d());
pos_origin = pos;
besitzer_p = spieler;
prodfactor_electric = 0;
prodfactor_pax = 0;
prodfactor_mail = 0;
if (initial_prod_base < 0) {
prodbase = besch->get_produktivitaet() + simrand(besch->get_bereich());
}
else {
prodbase = initial_prod_base;
}
delta_sum = 0;
delta_menge = 0;
menge_remainder = 0;
activity_count = 0;
currently_producing = false;
transformer_connected = false;
power = 0;
power_demand = 0;
total_input = total_transit = total_output = 0;
status = nothing;
lieferziele_active_last_month = 0;
// create input information
eingang.resize( fabesch->get_lieferanten() );
for( int g=0; g<fabesch->get_lieferanten(); ++g ) {
const fabrik_lieferant_besch_t *const input = fabesch->get_lieferant(g);
eingang[g].set_typ( input->get_ware() );
}
// create output information
ausgang.resize( fabesch->get_produkte() );
for( uint g=0; g<fabesch->get_produkte(); ++g ) {
const fabrik_produkt_besch_t *const product = fabesch->get_produkt(g);
ausgang[g].set_typ( product->get_ware() );
}
recalc_storage_capacities();
if (eingang.empty()) {
FOR(array_tpl<ware_production_t>, & g, ausgang) {
if (g.max > 0) {
// if source then start with full storage, so that AI will build line(s) immediately
g.menge = g.max - 1;
}
}
}
init_stats();
arrival_stats_pax.init();
arrival_stats_mail.init();
delta_slot = 0;
times_expanded = 0;
update_scaled_electric_amount();
update_scaled_pax_demand();
update_scaled_mail_demand();
}
fabrik_t::~fabrik_t()
{
while(!fields.empty()) {
planquadrat_t *plan = welt->access( fields.back().location );
// if destructor is called when world is destroyed, plan is already invalid
if (plan) {
grund_t *gr = plan->get_kartenboden();
if (field_t* f = gr->find<field_t>()) {
delete f; // implicitly removes the field from fields
plan->boden_ersetzen( gr, new boden_t(gr->get_pos(), hang_t::flach ) );
plan->get_kartenboden()->calc_bild();
continue;
}
}
fields.pop_back();
}
// destroy chart window, if present
destroy_win((ptrdiff_t)this);
}
void fabrik_t::baue(sint32 rotate, bool build_fields, bool force_initial_prodbase)
{
this->rotate = rotate;
pos_origin = welt->lookup_kartenboden(pos_origin.get_2d())->get_pos();
gebaeude_t *gb = hausbauer_t::baue(besitzer_p, pos_origin, rotate, besch->get_haus(), this);
pos = gb->get_pos();
pos_origin.z = pos.z;
if(besch->get_field_group()) {
// if there are fields
if( !fields.empty() ) {
for( uint16 i=0; i<fields.get_count(); i++ ) {
const koord k = fields[i].location;
grund_t *gr=welt->lookup_kartenboden(k);
if( gr->ist_natur() ) {
// first make foundation below
grund_t *gr2 = new fundament_t(gr->get_pos(), gr->get_grund_hang());
welt->access(k)->boden_ersetzen(gr, gr2);
gr2->obj_add( new field_t(gr2->get_pos(), besitzer_p, besch->get_field_group()->get_field_class( fields[i].field_class_index ), this ) );
}
else {
// there was already a building at this position => do not restore!
fields.remove_at(i);
i--;
}
}
}
else if( build_fields ) {
// make sure not to exceed initial prodbase too much
sint32 org_prodbase = prodbase;
// we will start with a minimum number and try to get closer to start_fields
const uint16 spawn_fields = besch->get_field_group()->get_min_fields() + simrand( besch->get_field_group()->get_start_fields()-besch->get_field_group()->get_min_fields() );
while( fields.get_count() < spawn_fields && add_random_field(10000u) ) {
if (fields.get_count() > besch->get_field_group()->get_min_fields() && prodbase >= 2*org_prodbase) {
// too much productivity, no more fields needed
break;
}
}
sint32 field_prod = prodbase - org_prodbase;
// adjust prodbase
if (force_initial_prodbase) {
set_base_production( max(field_prod, org_prodbase) );
}
}
}
}
/* field generation code
* @author Kieron Green
*/
bool fabrik_t::add_random_field(uint16 probability)
{
// has fields, and not yet too many?
const field_group_besch_t *fb = besch->get_field_group();
if(fb==NULL || fb->get_max_fields() <= fields.get_count()) {
return false;
}
// we are lucky and are allowed to generate a field
if( simrand(10000)>=probability ) {
return false;
}
// we start closest to the factory, and check for valid tiles as we move out
uint8 radius = 1;
// pick a coordinate to use - create a list of valid locations and choose a random one
slist_tpl<grund_t *> build_locations;
do {
for(sint32 xoff = -radius; xoff < radius + get_besch()->get_haus()->get_groesse().x ; xoff++) {
for(sint32 yoff =-radius ; yoff < radius + get_besch()->get_haus()->get_groesse().y; yoff++) {
// if we can build on this tile then add it to the list
grund_t *gr = welt->lookup_kartenboden(pos.get_2d()+koord(xoff,yoff));
if (gr != NULL &&
gr->get_typ() == grund_t::boden &&
gr->get_hoehe() == pos.z &&
gr->get_grund_hang() == hang_t::flach &&
gr->ist_natur() &&
(gr->find<leitung_t>() || gr->kann_alle_obj_entfernen(NULL) == NULL)) {
// only on same height => climate will match!
build_locations.append(gr);
assert(gr->find<field_t>() == NULL);
}
// skip inside of rectange (already checked earlier)
if(radius > 1 && yoff == -radius && (xoff > -radius && xoff < radius + get_besch()->get_haus()->get_groesse().x - 1)) {
yoff = radius + get_besch()->get_haus()->get_groesse().y - 2;
}
}
}
if (build_locations.empty()) {
radius++;
}
} while (radius < 10 && build_locations.empty());
// built on one of the positions
if (!build_locations.empty()) {
grund_t *gr = build_locations.at(simrand(build_locations.get_count()));
leitung_t* lt = gr->find<leitung_t>();
if(lt) {
gr->obj_remove(lt);
}
gr->obj_loesche_alle(NULL);
// first make foundation below
const koord k = gr->get_pos().get_2d();
field_data_t new_field(k);
assert(!fields.is_contained(new_field));
// Knightly : fetch a random field class besch based on spawn weights
const weighted_vector_tpl<uint16> &field_class_indices = fb->get_field_class_indices();
new_field.field_class_index = pick_any_weighted(field_class_indices);
const field_class_besch_t *const field_class = fb->get_field_class( new_field.field_class_index );
fields.append(new_field);
grund_t *gr2 = new fundament_t(gr->get_pos(), gr->get_grund_hang());
welt->access(k)->boden_ersetzen(gr, gr2);
gr2->obj_add( new field_t(gr2->get_pos(), besitzer_p, field_class, this ) );
// Knightly : adjust production base and storage capacities
set_base_production( prodbase + field_class->get_field_production() );
if(lt) {
gr2->obj_add( lt );
}
gr2->calc_bild();
return true;
}
return false;
}
void fabrik_t::remove_field_at(koord pos)
{
field_data_t field(pos);
assert(fields.is_contained( field ));
field = fields[ fields.index_of(field) ];
const field_class_besch_t *const field_class = besch->get_field_group()->get_field_class( field.field_class_index );
fields.remove(field);
// Knightly : revert the field's effect on production base and storage capacities
set_base_production( prodbase - field_class->get_field_production() );
}
vector_tpl<fabrik_t *> &fabrik_t::sind_da_welche(koord min_pos, koord max_pos)
{
static vector_tpl <fabrik_t*> fablist(16);
fablist.clear();
for(int y=min_pos.y; y<=max_pos.y; y++) {
for(int x=min_pos.x; x<=max_pos.x; x++) {
fabrik_t *fab=get_fab(koord(x,y));
if(fab) {
if (fablist.append_unique(fab)) {
//DBG_MESSAGE("fabrik_t::sind_da_welche()","appended factory %s at (%i,%i)",gr->first_obj()->get_fabrik()->get_besch()->get_name(),x,y);
}
}
}
}
return fablist;
}
/**
* if name==NULL translate besch factory name in game language
*/
char const* fabrik_t::get_name() const
{
return name ? name.c_str() : translator::translate(besch->get_name(), welt->get_settings().get_name_language_id());
}
void fabrik_t::set_name(const char *new_name)
{
if(new_name==NULL || strcmp(new_name, translator::translate(besch->get_name(), welt->get_settings().get_name_language_id()))==0) {
// new name is equal to name given by besch/translation -> set name to NULL
name = NULL;
} else {
name = new_name;
}
fabrik_info_t *win = dynamic_cast<fabrik_info_t*>(win_get_magic((ptrdiff_t)this));
if (win) {
win->update_info();
}
}
void fabrik_t::rdwr(loadsave_t *file)
{
xml_tag_t f( file, "fabrik_t" );
sint32 i;
sint32 spieler_n;
sint32 eingang_count;
sint32 ausgang_count;
sint32 anz_lieferziele;
if( file->is_saving() ) {
eingang_count = eingang.get_count();
ausgang_count = ausgang.get_count();
anz_lieferziele = lieferziele.get_count();
const char *s = besch->get_name();
file->rdwr_str(s);
}
else {
char s[256];
file->rdwr_str(s, lengthof(s));
DBG_DEBUG("fabrik_t::rdwr()","loading factory '%s'",s);
besch = fabrikbauer_t::get_fabesch(s);
if( besch==NULL ) {
// maybe it was only renamed?
besch = fabrikbauer_t::get_fabesch(translator::compatibility_name(s));
}
if( besch==NULL ) {
dbg->warning( "fabrik_t::rdwr()", "Pak-file for factory '%s' missing!", s );
// we continue loading even if besch==NULL
welt->add_missing_paks( s, karte_t::MISSING_FACTORY );
}
}
pos_origin.rdwr(file);
// pos will be assigned after call to hausbauer_t::baue
file->rdwr_byte(rotate);
// now rebuilt information for received goods
file->rdwr_long(eingang_count);
if( file->is_loading() ) {
eingang.resize( eingang_count );
}
for( i=0; i<eingang_count; ++i ) {
ware_production_t &ware = eingang[i];
const char *ware_name = NULL;
if( file->is_saving() ) {
ware_name = ware.get_typ()->get_name();
}
file->rdwr_str(ware_name);
file->rdwr_long(ware.menge);
if( file->get_version()<110005 ) {
// max storage is only loaded/saved for older versions
file->rdwr_long(ware.max);
}
ware.rdwr( file );
if( file->is_loading() ) {
ware.set_typ( warenbauer_t::get_info(ware_name) );
guarded_free(const_cast<char *>(ware_name));
// Hajo: repair files that have 'insane' values
if( ware.menge<0 ) {
ware.menge = 0;
}
if( ware.menge>(FAB_MAX_INPUT<<precision_bits) ) {
ware.menge = (FAB_MAX_INPUT << precision_bits);
}
}
}
// now rebuilt information for produced goods
file->rdwr_long(ausgang_count);
if( file->is_loading() ) {
ausgang.resize( ausgang_count );
}
for( i=0; i<ausgang_count; ++i ) {
ware_production_t &ware = ausgang[i];
const char *ware_name = NULL;
if( file->is_saving() ) {
ware_name = ware.get_typ()->get_name();
}
file->rdwr_str(ware_name);
file->rdwr_long(ware.menge);
if( file->get_version()<110005 ) {
// max storage is only loaded/saved for older versions
file->rdwr_long(ware.max);