forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simfab.cc
3254 lines (2853 loc) · 102 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 "simware.h"
#include "simworld.h"
#include "descriptor/building_desc.h"
#include "descriptor/goods_desc.h"
#include "descriptor/sound_desc.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 "descriptor/factory_desc.h"
#include "bauer/hausbauer.h"
#include "bauer/goods_manager.h"
#include "bauer/fabrikbauer.h"
#include "gui/fabrik_info.h"
#include "utils/simrandom.h"
#include "utils/cbuffer_t.h"
#include "gui/simwin.h"
#include "display/simgraph.h"
// Fabrik_t
static const int FAB_MAX_INPUT = 15000;
// Half a display unit (0.5).
static const sint64 FAB_DISPLAY_UNIT_HALF = ((sint64)1 << (fabrik_t::precision_bits + DEFAULT_PRODUCTION_FACTOR_BITS - 1));
// Half a production factor unit (0.5).
static const sint32 FAB_PRODFACT_UNIT_HALF = ((sint32)1 << (DEFAULT_PRODUCTION_FACTOR_BITS - 1));
karte_ptr_t fabrik_t::welt;
/**
* Convert internal values to displayed values
*/
sint64 convert_goods(sint64 value) { return ((value + FAB_DISPLAY_UNIT_HALF) >> (fabrik_t::precision_bits + DEFAULT_PRODUCTION_FACTOR_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);
}
};
/**
* Produce a scaled production amount from a production amount and work factor.
*/
sint32 work_scale_production(sint64 prod, sint64 work){
// compute scaled production, rounding up
return ((prod * work) + (1 << WORK_BITS) - 1) >> WORK_BITS;
}
/**
* Produce a work factor from a production amount and scaled production amount.
*/
sint32 work_from_production(sint64 prod, sint64 scaled){
// compute work, rounding up
return prod ? ((scaled << WORK_BITS) + prod - 1) / prod : 0;
}
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;
}
void ware_production_t::roll_stats(uint32 factor, 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( (sint64)menge * (sint64)factor, FAB_GOODS_STORAGE );
}
void ware_production_t::rdwr(loadsave_t *file)
{
if( file->is_loading() ) {
init_stats();
}
// we use a temporary variable to save/load old data correctly
sint64 statistics_buf[MAX_MONTH][MAX_FAB_GOODS_STAT];
memcpy( statistics_buf, statistics, sizeof(statistics_buf) );
if( file->is_saving() && file->is_version_less(120, 1) ) {
for( int m=0; m<MAX_MONTH; ++m ) {
statistics_buf[m][0] = (statistics[m][FAB_GOODS_STORAGE] >> DEFAULT_PRODUCTION_FACTOR_BITS);
statistics_buf[m][2] = (statistics[m][2] >> DEFAULT_PRODUCTION_FACTOR_BITS);
}
}
if( file->is_version_atleast(112, 1) ) {
for( int s=0; s<MAX_FAB_GOODS_STAT; ++s ) {
for( int m=0; m<MAX_MONTH; ++m ) {
file->rdwr_longlong( statistics_buf[m][s] );
}
}
file->rdwr_longlong( weighted_sum_storage );
}
else if( file->is_version_atleast(110, 5) ) {
// save/load statistics
for( int s=0; s<3; ++s ) {
for( int m=0; m<MAX_MONTH; ++m ) {
file->rdwr_longlong( statistics_buf[m][s] );
}
}
file->rdwr_longlong( weighted_sum_storage );
}
if( file->is_loading() ) {
memcpy( statistics, statistics_buf, sizeof(statistics_buf) );
// Apply correction for output production graphs which have had their precision changed for factory normalization.
// Also apply a fix for corrupted in-transit values caused by a logical error.
if(file->is_version_less(120, 1)){
for( int m=0; m<MAX_MONTH; ++m ) {
statistics[m][0] = (statistics[m][FAB_GOODS_STORAGE] & 0xffffffff) << DEFAULT_PRODUCTION_FACTOR_BITS;
statistics[m][2] = (statistics[m][2] & 0xffffffff) << DEFAULT_PRODUCTION_FACTOR_BITS;
}
}
// recalc transit always on load
statistics[0][FAB_GOODS_TRANSIT] = 0;
}
}
void ware_production_t::book_weighted_sum_storage(uint32 factor, sint64 delta_time)
{
const sint64 amount = (sint64)menge * (sint64)factor;
weighted_sum_storage += amount * delta_time;
set_stat( amount, FAB_GOODS_STORAGE );
}
sint32 ware_production_t::calculate_output_production_rate() const {
return fabrik_t::calculate_work_rate_ramp(menge, min_shipment * OUTPUT_SCALE_RAMPDOWN_MULTIPLYER, max);
}
sint32 ware_production_t::calculate_demand_production_rate() const {
return fabrik_t::calculate_work_rate_ramp(demand_buffer, max / 2, max);
}
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->is_version_atleast(110, 5) ) {
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 > goods_manager_t::INDEX_NONE ) {
// only for freights
fabrik_t *fab = get_fab( ware->get_zielpos() );
if( fab ) {
fab->update_transit_intern( ware, add );
}
}
}
void fabrik_t::apply_transit( const ware_t *ware )
{
if( ware->index > goods_manager_t::INDEX_NONE ) {
// only for freights
fabrik_t *fab = get_fab( ware->get_zielpos() );
if( fab ) {
for( uint32 input = 0; input < fab->input.get_count(); input++ ){
ware_production_t& w = fab->input[input];
if( w.get_typ()->get_index() == ware->index ) {
// It is now in transit.
w.book_stat((sint64)ware->menge, FAB_GOODS_TRANSIT );
// If using JIT2, must decrement demand buffers, activating if required.
if( welt->get_settings().get_just_in_time() >= 2 ){
const uint32 prod_factor = fab->desc->get_supplier(input)->get_consumption();
const sint32 prod_delta = (sint32)((((sint64)(ware->menge) << (DEFAULT_PRODUCTION_FACTOR_BITS + precision_bits)) + (sint64)(prod_factor - 1)) / (sint64)prod_factor);
const sint32 demand = w.demand_buffer;
w.demand_buffer -= prod_delta;
if( demand >= w.max && w.demand_buffer < w.max ) {
fab->inactive_demands --;
}
}
// ours is on its way, no need to handle the other
return;
}
}
}
}
}
// just for simplicity ...
void fabrik_t::update_transit_intern( const ware_t *ware, bool add )
{
FOR( array_tpl<ware_production_t>, &w, input ) {
if( w.get_typ()->get_index() == ware->index ) {
w.book_stat(add ? (sint64)ware->menge : -(sint64)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( uint32 in = 0; in < input.get_count(); in++ ){
input[in].book_weighted_sum_storage(desc->get_supplier(in)->get_consumption(), delta_time);
}
for( uint32 out = 0; out < output.get_count(); out++ ){
output[out].book_weighted_sum_storage(desc->get_product(out)->get_factor(), 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
sint64 power = get_power();
weighted_sum_power += power * delta_time;
set_stat( power, FAB_POWER );
}
void fabrik_t::update_scaled_electric_demand()
{
if( desc->get_electric_demand()==65535 ) {
// demand not specified in pak, use old fixed demands
scaled_electric_demand = prodbase * PRODUCTION_DELTA_T;
if( desc->is_electricity_producer() ) {
scaled_electric_demand *= 4;
}
return;
}
const sint64 prod = desc->get_productivity();
scaled_electric_demand = (uint32)( (( (sint64)(desc->get_electric_demand()) * (sint64)prodbase + (prod >> 1) ) / prod) << POWER_TO_MW );
if( scaled_electric_demand == 0 ) {
prodfactor_electric = 0;
}
}
void fabrik_t::update_scaled_pax_demand()
{
// first, scaling based on current production base
const sint64 prod = desc->get_productivity();
const sint64 desc_pax_demand = ( desc->get_pax_demand()==65535 ? desc->get_pax_level() : desc->get_pax_demand() );
// formula : desc_pax_demand * (current_production_base / desc_production_base); (prod >> 1) is for rounding
const uint32 pax_demand = (uint32)( ( desc_pax_demand * (sint64)prodbase + (prod >> 1) ) / prod );
// then, scaling based on month length
scaled_pax_demand = (uint32)welt->scale_with_month_length(pax_demand);
if( scaled_pax_demand == 0 && desc_pax_demand > 0 ) {
scaled_pax_demand = 1; // since desc 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 = desc->get_productivity();
const sint64 desc_mail_demand = ( desc->get_mail_demand()==65535 ? (desc->get_pax_level()>>2) : desc->get_mail_demand() );
// formula : desc_mail_demand * (current_production_base / desc_production_base); (prod >> 1) is for rounding
const uint32 mail_demand = (uint32)( ( desc_mail_demand * (sint64)prodbase + (prod >> 1) ) / prod );
// then, scaling based on month length
scaled_mail_demand = (uint32)welt->scale_with_month_length(mail_demand);
if( scaled_mail_demand == 0 && desc_mail_demand > 0 ) {
scaled_mail_demand = 1; // since desc 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 || desc->get_pax_boost()==0 ) {
prodfactor_pax = 0;
}
else if( pax_arrived>=pax_demand ) {
// maximum boost
prodfactor_pax = desc->get_pax_boost();
}
else {
// pro-rata boost : (pax_arrived / pax_demand) * desc_pax_boost; (pax_demand >> 1) is for rounding
prodfactor_pax = (sint32)( ( (sint64)pax_arrived * (sint64)(desc->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 || desc->get_mail_boost()==0 ) {
prodfactor_mail = 0;
}
else if( mail_arrived>=mail_demand ) {
// maximum boost
prodfactor_mail = desc->get_mail_boost();
}
else {
// pro-rata boost : (mail_arrived / mail_demand) * desc_mail_boost; (mail_demand >> 1) is for rounding
prodfactor_mail = (sint32)( ( (sint64)mail_arrived * (sint64)(desc->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( desc->get_field_group() ) {
// with fields -> calculate based on capacities contributed by fields
const uint32 ware_types = input.get_count() + output.get_count();
if( ware_types>0 ) {
// calculate total storage capacity contributed by fields
const field_group_desc_t *const field_group = desc->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, input) {
for( int b=0; b<desc->get_supplier_count(); ++b ) {
const factory_supplier_desc_t *const input = desc->get_supplier(b);
if (g.get_typ() == input->get_input_type()) {
// Inputs are now normalized to factory production.
uint32 prod_factor = input->get_consumption();
g.max = (sint32)((((sint64)((input->get_capacity() << precision_bits) + share) << DEFAULT_PRODUCTION_FACTOR_BITS) + (sint64)(prod_factor - 1)) / (sint64)prod_factor);
}
}
}
// then, for output goods
FOR(array_tpl<ware_production_t>, & g, output) {
for( uint b=0; b<desc->get_product_count(); ++b ) {
const factory_product_desc_t *const output = desc->get_product(b);
if (g.get_typ() == output->get_output_type()) {
// Outputs are now normalized to factory production.
uint32 prod_factor = output->get_factor();
g.max = (sint32)((((sint64)((output->get_capacity() << precision_bits) + share) << DEFAULT_PRODUCTION_FACTOR_BITS) + (sint64)(prod_factor - 1)) / (sint64)prod_factor);
}
}
}
}
}
else {
// without fields -> scaling based on prodbase
// first, for input goods
FOR(array_tpl<ware_production_t>, & g, input) {
for( int b=0; b<desc->get_supplier_count(); ++b ) {
const factory_supplier_desc_t *const input = desc->get_supplier(b);
if (g.get_typ() == input->get_input_type()) {
// Inputs are now normalized to factory production.
uint32 prod_factor = input->get_consumption();
g.max = (sint32)(((((sint64)input->get_capacity() * (sint64)prodbase) << (precision_bits + DEFAULT_PRODUCTION_FACTOR_BITS)) + (sint64)(prod_factor - 1)) / ((sint64)desc->get_productivity() * (sint64)prod_factor));
}
}
}
// then, for output goods
FOR(array_tpl<ware_production_t>, & g, output) {
for( uint b=0; b<desc->get_product_count(); ++b ) {
const factory_product_desc_t *const output = desc->get_product(b);
if (g.get_typ() == output->get_output_type()) {
// Outputs are now normalized to factory production.
uint32 prod_factor = output->get_factor();
g.max = (sint32)(((((sint64)output->get_capacity() * (sint64)prodbase) << (precision_bits + DEFAULT_PRODUCTION_FACTOR_BITS)) + (sint64)(prod_factor - 1)) / ((sint64)desc->get_productivity() * (sint64)prod_factor));
}
}
}
}
// Now that the maximum is known, work out the recommended shipment size for outputs in normalized units.
for( uint32 out = 0; out < output.get_count(); out++ ){
const uint32 prod_factor = desc->get_product(out)->get_factor();
// Determine the maximum number of whole units the out can store.
const uint32 unit_size = (uint32)(((sint64)output[out].max * (sint64)prod_factor) >> ( precision_bits + DEFAULT_PRODUCTION_FACTOR_BITS ));
// Determine the number of units to ship. Prefer 10 units although in future a more dynamic choice may be appropiate.
uint32 shipment_size;
// Maximum shipment size.
if( unit_size >= SHIPMENT_MAX_SIZE * SHIPMENT_NUM_MIN ) {
shipment_size = SHIPMENT_MAX_SIZE;
}
// Dynamic shipment size.
else if( unit_size > SHIPMENT_NUM_MIN ) {
shipment_size = unit_size / SHIPMENT_NUM_MIN;
}
// Minimum shipment size.
else {
shipment_size = 1;
}
// Now convert it into the prefered shipment size. Always round up to prevent "off by 1" error.
output[out].min_shipment = (sint32)((((sint64)shipment_size << (precision_bits + DEFAULT_PRODUCTION_FACTOR_BITS)) + (sint64)(prod_factor - 1)) / (sint64)prod_factor);
}
if( welt->get_settings().get_just_in_time() >= 2 ) {
rebuild_inactive_cache();
}
}
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_demand();
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)
{
owner = NULL;
prodfactor_electric = 0;
lieferziele_active_last_month = 0;
pos = koord3d::invalid;
rdwr(file);
if( desc == 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() );
desc = NULL; // to get rid of this broken factory later...
}
else {
build(rotate, false, false);
// now get rid of construction image
for( sint16 y=0; y<desc->get_building()->get_y(rotate); y++ ) {
for( sint16 x=0; x<desc->get_building()->get_x(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 = NULL;
last_sound_ms = welt->get_ticks();
}
fabrik_t::fabrik_t(koord3d pos_, player_t* owner, const factory_desc_t* factory_desc, sint32 initial_prod_base) :
desc(factory_desc),
pos(pos_)
{
this->pos.z = welt->max_hgt(pos.get_2d());
pos_origin = pos;
this->owner = owner;
prodfactor_electric = 0;
prodfactor_pax = 0;
prodfactor_mail = 0;
if (initial_prod_base < 0) {
prodbase = desc->get_productivity() + simrand(desc->get_range());
}
else {
prodbase = initial_prod_base;
}
delta_sum = 0;
delta_menge = 0;
menge_remainder = 0;
activity_count = 0;
currently_producing = false;
transformer = NULL;
total_input = total_transit = total_output = 0;
status = nothing;
lieferziele_active_last_month = 0;
// create input information
input.resize( factory_desc->get_supplier_count() );
for( int g=0; g<factory_desc->get_supplier_count(); ++g ) {
const factory_supplier_desc_t *const supp = factory_desc->get_supplier(g);
input[g].set_typ( supp->get_input_type() );
}
// create output information
output.resize( factory_desc->get_product_count() );
for( uint g=0; g<factory_desc->get_product_count(); ++g ) {
const factory_product_desc_t *const product = factory_desc->get_product(g);
output[g].set_typ( product->get_output_type() );
}
recalc_storage_capacities();
if( welt->get_settings().get_just_in_time() >= 2 ){
inactive_inputs = inactive_outputs = inactive_demands = 0;
if( input.empty() ){
// All sources start out with maximum product.
for( uint32 out = 0; out < output.get_count(); out++ ){
output[out].menge = output[out].max;
inactive_outputs ++;
}
}
else {
for( uint32 out = 0; out < output.get_count(); out++ ){
output[out].menge = 0;
}
// A consumer of sorts so output and input starts out empty but with a full demand buffer.
for( uint32 in = 0; in < input.get_count(); in++ ){
input[in].menge = 0;
input[in].demand_buffer = input[in].max;
inactive_inputs++;
inactive_demands++;
}
}
}
else {
if( input.empty() ) {
FOR( array_tpl<ware_production_t>, & g, output ) {
if( g.max > 0 ) {
// if source then start with full storage, so that AI will build line(s) immediately
g.menge = g.max - 1;
}
}
}
}
last_sound_ms = welt->get_ticks();
init_stats();
arrival_stats_pax.init();
arrival_stats_mail.init();
delta_slot = 0;
times_expanded = 0;
update_scaled_electric_demand();
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(), slope_t::flat ) );
plan->get_kartenboden()->calc_image();
continue;
}
}
fields.pop_back();
}
// destroy chart window, if present
destroy_win((ptrdiff_t)this);
}
void fabrik_t::build(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::build(owner, pos_origin, rotate, desc->get_building(), this);
pos = gb->get_pos();
pos_origin.z = pos.z;
if(desc->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(), owner, desc->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 = desc->get_field_group()->get_min_fields() + simrand( desc->get_field_group()->get_start_fields()-desc->get_field_group()->get_min_fields() );
while( fields.get_count() < spawn_fields && add_random_field(10000u) ) {
if (fields.get_count() > desc->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) );
}
}
}
else {
fields.clear();
}
/// Determine control logic
if( welt->get_settings().get_just_in_time() >= 2 ) {
// Does it both consume and produce?
if( !output.empty() && !input.empty() ) {
control_type = CL_FACT_MANY;
}
// Does it produce?
else if( !output.empty() ) {
control_type = CL_PROD_MANY;
}
// Does it consume?
else if( !input.empty() ) {
control_type = CL_CONS_MANY;
}
// No I/O?
else {
control_type = desc->is_electricity_producer() ? CL_ELEC_PROD : CL_NONE;
}
}
else{
// Classic logic.
if( !output.empty() && !input.empty() ) {
control_type = CL_FACT_CLASSIC;
}
else if( !output.empty() ) {
control_type = CL_PROD_CLASSIC;
}
else if( !input.empty() ) {
control_type = CL_CONS_CLASSIC;
}
else {
control_type = CL_ELEC_CLASSIC;
}
}
// Boost logic determines what factors boost factory production.
if( welt->get_settings().get_just_in_time() >= 2 ) {
if( !desc->is_electricity_producer() && desc->get_electric_demand() > 0 ) {
boost_type = BL_POWER;
}
else if( desc->get_pax_demand() || desc->get_mail_demand() ) {
boost_type = BL_PAXM;
}
else {
boost_type = BL_NONE;
}
}
else {
boost_type = BL_CLASSIC;
}
if( welt->get_settings().get_just_in_time() >= 2 ) {
if( input.empty() ) {
demand_type = DL_NONE;
}
else if( output.empty() ) {
demand_type = DL_ASYNC;
}
else {
demand_type = DL_SYNC;
}
}
else {
demand_type = input.empty() ? DL_NONE : DL_OLD;
}
}
/* field generation code
* @author Kieron Green
*/
bool fabrik_t::add_random_field(uint16 probability)
{
// has fields, and not yet too many?
const field_group_desc_t *fd = desc->get_field_group();
if(fd==NULL || fd->get_max_fields() <= fields.get_count()) {