forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simfab.h
616 lines (499 loc) · 17.2 KB
/
simfab.h
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
/*
* Copyright (c) 1997 - 2001 Hansjörg Malthaner
*
* This file is part of the Simutrans project under the artistic license.
* (see license.txt)
*/
#ifndef simfab_h
#define simfab_h
#include "dataobj/koord3d.h"
#include "dataobj/translator.h"
#include "tpl/slist_tpl.h"
#include "tpl/vector_tpl.h"
#include "tpl/array_tpl.h"
#include "besch/fabrik_besch.h"
#include "halthandle_t.h"
#include "simworld.h"
#include "utils/plainstring.h"
class spieler_t;
class stadt_t;
class ware_t;
/**
* Factory statistics
* @author Knightly
*/
#define MAX_MONTH (12)
#define FAB_PRODUCTION (0)
#define FAB_POWER (1)
#define FAB_BOOST_ELECTRIC (2)
#define FAB_BOOST_PAX (3)
#define FAB_BOOST_MAIL (4)
#define FAB_PAX_GENERATED (5)
#define FAB_PAX_DEPARTED (6)
#define FAB_PAX_ARRIVED (7)
#define FAB_MAIL_GENERATED (8)
#define FAB_MAIL_DEPARTED (9)
#define FAB_MAIL_ARRIVED (10)
#define MAX_FAB_STAT (11)
// reference lines
#define FAB_REF_MAX_BOOST_ELECTRIC (0)
#define FAB_REF_MAX_BOOST_PAX (1)
#define FAB_REF_MAX_BOOST_MAIL (2)
#define FAB_REF_DEMAND_ELECTRIC (3)
#define FAB_REF_DEMAND_PAX (4)
#define FAB_REF_DEMAND_MAIL (5)
#define MAX_FAB_REF_LINE (6)
// statistics for goods
#define MAX_FAB_GOODS_STAT (4)
// common to both input and output goods
#define FAB_GOODS_STORAGE (0)
// input goods
#define FAB_GOODS_RECEIVED (1)
#define FAB_GOODS_CONSUMED (2)
#define FAB_GOODS_TRANSIT (3)
// output goods
#define FAB_GOODS_DELIVERED (1)
#define FAB_GOODS_PRODUCED (2)
// production happens in every second
#define PRODUCTION_DELTA_T (1024)
// default production factor
#define DEFAULT_PRODUCTION_FACTOR_BITS (8)
#define DEFAULT_PRODUCTION_FACTOR (1 << DEFAULT_PRODUCTION_FACTOR_BITS)
// precision of apportioned demand (i.e. weights of factories at target cities)
#define DEMAND_BITS (4)
/**
* Convert internal values to displayed values
*/
sint64 convert_goods(sint64 value);
sint64 convert_power(sint64 value);
sint64 convert_boost(sint64 value);
class ware_production_t
{
private:
const ware_besch_t *type;
// Knightly : statistics for each goods
sint64 statistics[MAX_MONTH][MAX_FAB_GOODS_STAT];
sint64 weighted_sum_storage;
/// clears statistics, transit, and weighted_sum_storage
void init_stats();
public:
ware_production_t() : type(NULL), menge(0), max(0)/*, transit(statistics[0][FAB_GOODS_TRANSIT])*/, max_transit(0), index_offset(0)
{
#ifdef TRANSIT_DISTANCE
count_suppliers = 0;
#endif
init_stats();
}
const ware_besch_t* get_typ() const { return type; }
void set_typ(const ware_besch_t *t) { type=t; }
// Knightly : functions for manipulating goods statistics
void roll_stats(sint64 aggregate_weight);
void rdwr(loadsave_t *file);
const sint64* get_stats() const { return *statistics; }
void book_stat(sint64 value, int stat_type) { assert(stat_type<MAX_FAB_GOODS_STAT); statistics[0][stat_type] += value; }
void set_stat(sint64 value, int stat_type) { assert(stat_type<MAX_FAB_GOODS_STAT); statistics[0][stat_type] = value; }
sint64 get_stat(int month, int stat_type) const { assert(stat_type<MAX_FAB_GOODS_STAT); return statistics[month][stat_type]; }
/**
* convert internal units to displayed values
*/
sint64 get_stat_converted(int month, int stat_type) const {
assert(stat_type<MAX_FAB_GOODS_STAT);
sint64 value = statistics[month][stat_type];
if (stat_type==FAB_GOODS_STORAGE || stat_type==FAB_GOODS_CONSUMED) {
value = convert_goods(value);
}
return value;
}
void book_weighted_sum_storage(sint64 delta_time);
sint32 menge; // in internal units shifted by precision_bits (see step)
sint32 max;
/// Cargo currently in transit from/to this slot. Equivalent to statistics[0][FAB_GOODS_TRANSIT].
const sint32 get_in_transit() const { return statistics[0][FAB_GOODS_TRANSIT]; }
/// Current limit on cargo in transit, depending on suppliers mean distance.
sint32 max_transit;
#ifdef TRANSIT_DISTANCE
sint32 count_suppliers; // only needed for averaging
#endif
uint32 index_offset; // used for haltlist and lieferziele searches in verteile_waren to produce round robin results
};
/**
* Eine Klasse für Fabriken in Simutrans. Fabriken produzieren und
* verbrauchen Waren und beliefern nahe Haltestellen.
*
* @date 1998
* @see haltestelle_t
* @author Hj. Malthaner
*/
class fabrik_t
{
public:
/**
* Konstanten
* @author Hj. Malthaner
*/
enum { precision_bits = 10, old_precision_bits = 10, precision_mask = 1023 };
private:
/**
* Factory statistics
* @author Knightly
*/
sint64 statistics[MAX_MONTH][MAX_FAB_STAT];
sint64 weighted_sum_production;
sint64 weighted_sum_boost_electric;
sint64 weighted_sum_boost_pax;
sint64 weighted_sum_boost_mail;
sint64 weighted_sum_power;
sint64 aggregate_weight;
// Knightly : Functions for manipulating factory statistics
void init_stats();
void set_stat(sint64 value, int stat_type) { assert(stat_type<MAX_FAB_STAT); statistics[0][stat_type] = value; }
// Knightly : For accumulating weighted sums for average statistics
void book_weighted_sums(sint64 delta_time);
/**
* Die möglichen Lieferziele
* @author Hj. Malthaner
*/
vector_tpl <koord> lieferziele;
uint32 lieferziele_active_last_month;
/**
* suppliers to this factry
* @author hsiegeln
*/
vector_tpl <koord> suppliers;
/**
* fields of this factory (only for farms etc.)
* @author prissi/Knightly
*/
struct field_data_t
{
koord location;
uint16 field_class_index;
field_data_t() : field_class_index(0) {}
explicit field_data_t(const koord loc) : location(loc), field_class_index(0) {}
field_data_t(const koord loc, const uint16 class_index) : location(loc), field_class_index(class_index) {}
bool operator==(const field_data_t &other) const { return location==other.location; }
};
vector_tpl <field_data_t> fields;
/**
* Die erzeugten waren auf die Haltestellen verteilen
* @author Hj. Malthaner
*/
void verteile_waren(const uint32 produkt);
// List of target cities
vector_tpl<stadt_t *> target_cities;
spieler_t *besitzer_p;
static karte_ptr_t welt;
const fabrik_besch_t *besch;
/**
* Bauposition gedreht?
* @author V.Meyer
*/
uint8 rotate;
/**
* produktionsgrundmenge
* @author Hj. Malthaner
*/
sint32 prodbase;
/**
* multiplikator für die Produktionsgrundmenge
* @author Hj. Malthaner
*/
sint32 prodfactor_electric;
sint32 prodfactor_pax;
sint32 prodfactor_mail;
array_tpl<ware_production_t> eingang; ///< array for input/consumed goods
array_tpl<ware_production_t> ausgang; ///< array for output/produced goods
/**
* Zeitakkumulator für Produktion
* @author Hj. Malthaner
*/
sint32 delta_sum;
uint32 delta_menge;
// production remainder when scaled to PRODUCTION_DELTA_T. added back next step to eliminate cumulative error
uint32 menge_remainder;
// Knightly : number of rounds where there is active production or consumption
uint8 activity_count;
// true if the factory has a transformer adjacent
bool transformer_connected;
// true, if the factory did produce enough in the last step to require power
bool currently_producing;
// power that can be currently drawn from this station (or the amount delivered)
uint32 power;
// power requested for next step
uint32 power_demand;
uint32 total_input, total_transit, total_output;
uint8 status;
/// Position of a building of the factory.
koord3d pos;
/// Position of the nw-corner tile of the factory.
koord3d pos_origin;
/**
* Number of times the factory has expanded so far
* Only for factories without fields
* @author Knightly
*/
uint16 times_expanded;
/**
* Electricity amount scaled with prodbase
* @author TurfIt
*/
uint32 scaled_electric_amount;
/**
* Pax/mail demand scaled with prodbase and month length
* @author Knightly
*/
uint32 scaled_pax_demand;
uint32 scaled_mail_demand;
/**
* Update scaled electricity amount
* @author TurfIt
*/
void update_scaled_electric_amount();
/**
* Update scaled pax/mail demand
* @author Knightly
*/
void update_scaled_pax_demand();
void update_scaled_mail_demand();
/**
* Update production multipliers for pax and mail
* @author Knightly
*/
void update_prodfactor_pax();
void update_prodfactor_mail();
/**
* Re-calculate the pax/mail demands of factory at target cities
* @author Knightly
*/
void recalc_demands_at_target_cities();
/**
* Recalculate storage capacities based on prodbase or capacities contributed by fields
* @author Knightly
*/
void recalc_storage_capacities();
/**
* Class for collecting arrival data and calculating pax/mail boost with fixed period length
* @author Knightly
*/
#define PERIOD_BITS (18) // determines period length on which boost calculation is based
#define SLOT_BITS (6) // determines the number of time slots available
#define SLOT_COUNT (1<<SLOT_BITS) // number of time slots for accumulating arrived pax/mail
class arrival_statistics_t
{
private:
uint16 slots[SLOT_COUNT];
uint16 current_slot;
uint16 active_slots; // number of slots covered since aggregate arrival last increased from 0 to +ve
uint32 aggregate_arrival;
uint32 scaled_demand;
public:
void init();
void rdwr(loadsave_t *file);
void set_scaled_demand(const uint32 demand) { scaled_demand = demand; }
#define ARRIVALS_CHANGED (1)
#define ACTIVE_SLOTS_INCREASED (2)
sint32 advance_slot();
void book_arrival(const uint16 amount);
uint16 get_active_slots() const { return active_slots; }
uint32 get_aggregate_arrival() const { return aggregate_arrival; }
uint32 get_scaled_demand() const { return scaled_demand; }
};
void update_transit_intern( const ware_t *ware, bool add );
/**
* Arrival data for calculating pax/mail boost
* @author Knightly
*/
arrival_statistics_t arrival_stats_pax;
arrival_statistics_t arrival_stats_mail;
plainstring name;
/**
* For advancement of slots for boost calculation
* @author Knightly
*/
sint32 delta_slot;
void recalc_factory_status();
// create some smoke on the map
void smoke() const;
// scales the amount of production based on the amount already in storage
uint32 scale_output_production(const uint32 product, uint32 menge) const;
public:
fabrik_t(loadsave_t *file);
fabrik_t(koord3d pos, spieler_t* sp, const fabrik_besch_t* fabesch, sint32 initial_prod_base);
~fabrik_t();
/**
* Return/book statistics
* @author Knightly
*/
const sint64* get_stats() const { return *statistics; }
sint64 get_stat(int month, int stat_type) const { assert(stat_type<MAX_FAB_STAT); return statistics[month][stat_type]; }
void book_stat(sint64 value, int stat_type) { assert(stat_type<MAX_FAB_STAT); statistics[0][stat_type] += value; }
static void update_transit( const ware_t *ware, bool add );
/**
* convert internal units to displayed values
*/
sint64 get_stat_converted(int month, int stat_type) const {
assert(stat_type<MAX_FAB_STAT);
sint64 value = statistics[month][stat_type];
switch(stat_type) {
case FAB_POWER:
value = convert_power(value);
break;
case FAB_BOOST_ELECTRIC:
case FAB_BOOST_PAX:
case FAB_BOOST_MAIL:
value = convert_boost(value);
break;
default: ;
}
return value;
}
static fabrik_t * get_fab(const koord &pos);
/**
* @return vehicle description object
* @author Hj. Malthaner
*/
const fabrik_besch_t *get_besch() const {return besch; }
void laden_abschliessen();
/// gets position of a building belonging to factory
koord3d get_pos() const { return pos; }
void rotate90( const sint16 y_size );
void link_halt(halthandle_t halt);
void unlink_halt(halthandle_t halt);
const vector_tpl<koord>& get_lieferziele() const { return lieferziele; }
bool is_active_lieferziel( koord k ) const;
const vector_tpl<koord>& get_suppliers() const { return suppliers; }
/**
* Functions for manipulating the list of connected cities
* @author Hj. Malthaner/prissi/Knightly
*/
void add_target_city(stadt_t *const city);
void remove_target_city(stadt_t *const city);
void clear_target_cities();
const vector_tpl<stadt_t *>& get_target_cities() const { return target_cities; }
/**
* Fügt ein neues Lieferziel hinzu
* @author Hj. Malthaner
*/
void add_lieferziel(koord ziel);
void rem_lieferziel(koord pos);
/**
* adds a supplier
* @author Hj. Malthaner
*/
void add_supplier(koord pos);
void rem_supplier(koord pos);
/**
* @return menge der ware typ
* -1 wenn typ nicht produziert wird
* sonst die gelagerte menge
*/
sint32 input_vorrat_an(const ware_besch_t *ware); // Vorrat von Warentyp
sint32 vorrat_an(const ware_besch_t *ware); // Vorrat von Warentyp
// returns all power and consume it to prevent multiple pumpes
uint32 get_power() { uint32 p=power; power=0; return p; }
// returns power wanted by the factory for next step and sets to 0 to prevent multiple senkes on same powernet
uint32 get_power_demand() { uint32 p=power_demand; power_demand=0; return p; }
// give power to the factory to consume ...
void add_power(uint32 p) { power += p; }
// senkes give back wanted power they can't supply such that a senke on a different powernet can try suppling
// WARNING: senke stepping order can vary between ingame construction and savegame loading => different results after saveing/loading the game
void add_power_demand(uint32 p) { power_demand +=p; }
// true, if there was production requiring power in the last step
bool is_currently_producing() const { return currently_producing; }
// used to limit transformers to 1 per factory
bool is_transformer_connected() const { return transformer_connected; }
void set_transformer_connected(bool connected) { transformer_connected = connected; }
/**
* @return 1 wenn verbrauch,
* 0 wenn Produktionsstopp,
* -1 wenn Ware nicht verarbeitet wird
*/
sint8 is_needed(const ware_besch_t *) const;
sint32 liefere_an(const ware_besch_t *, sint32 menge);
void step(uint32 delta_t); // fabrik muss auch arbeiten
void neuer_monat();
char const* get_name() const;
void set_name( const char *name );
sint32 get_kennfarbe() const { return besch->get_kennfarbe(); }
spieler_t *get_besitzer() const
{
grund_t const* const p = welt->lookup(pos);
return p ? p->first_obj()->get_besitzer() : 0;
}
void zeige_info();
// infostring on production
void info_prod(cbuffer_t& buf) const;
// infostring on targets/sources
void info_conn(cbuffer_t& buf) const;
void rdwr(loadsave_t *file);
/*
* Fills the vector with the koords of the tiles.
*/
void get_tile_list( vector_tpl<koord> &tile_list ) const;
/**
* gibt eine NULL-Terminierte Liste von Fabrikpointern zurück
*
* @author Hj. Malthaner
*/
static vector_tpl<fabrik_t *> & sind_da_welche(koord min, koord max);
// hier die methoden zum parametrisieren der Fabrik
/**
* Baut die Gebäude für die Fabrik
*
* @author Hj. Malthaner, V. Meyer
*/
void baue(sint32 rotate, bool build_fields, bool force_initial_prodbase);
sint16 get_rotate() const { return rotate; }
/* field generation code
* spawns a field for sure if probability>=1000
* @author Kieron Green
*/
bool add_random_field(uint16 probability);
void remove_field_at(koord pos);
uint32 get_field_count() const { return fields.get_count(); }
/**
* total and current procduction/storage values
* @author Hj. Malthaner
*/
const array_tpl<ware_production_t>& get_eingang() const { return eingang; }
const array_tpl<ware_production_t>& get_ausgang() const { return ausgang; }
/**
* Production multipliers
* @author Hj. Malthaner
*/
sint32 get_prodfactor_electric() const { return prodfactor_electric; }
sint32 get_prodfactor_pax() const { return prodfactor_pax; }
sint32 get_prodfactor_mail() const { return prodfactor_mail; }
sint32 get_prodfactor() const { return DEFAULT_PRODUCTION_FACTOR + prodfactor_electric + prodfactor_pax + prodfactor_mail; }
/* does not takes month length into account */
sint32 get_base_production() const { return prodbase; }
void set_base_production(sint32 p);
sint32 get_current_production() const { return (sint32)welt->scale_with_month_length( ((sint64)prodbase * (sint64)get_prodfactor())>>8 ); }
/* prissi: returns the status of the current factory, as well as output */
enum { bad, medium, good, inactive, nothing };
static unsigned status_to_color[5];
uint8 get_status() const { return status; }
uint32 get_total_in() const { return total_input; }
uint32 get_total_transit() const { return total_transit; }
uint32 get_total_out() const { return total_output; }
/**
* Crossconnects all factories
* @author prissi
*/
void add_all_suppliers();
/* adds a new supplier to this factory
* fails if no matching goods are there
*/
bool add_supplier(fabrik_t* fab);
/**
* Return the scaled electricity amount and pax/mail demand
* @author Knightly
*/
uint32 get_scaled_electric_amount() const { return scaled_electric_amount; }
uint32 get_scaled_pax_demand() const { return scaled_pax_demand; }
uint32 get_scaled_mail_demand() const { return scaled_mail_demand; }
bool is_end_consumer() const { return (ausgang.empty() && !besch->is_electricity_producer()); }
// Returns a list of goods produced by this factory.
slist_tpl<const ware_besch_t*> *get_produced_goods() const;
};
#endif