1 /*
2  * Copyright (c) 1997 - 2003 Hansj�rg Malthaner
3  *
4  * This file is part of the Simutrans project under the artistic licence.
5  * (see licence.txt)
6  */
7 
8 /** @file powernet.h Data structure to manage a net of powerlines - a powernet */
9 
10 #ifndef powernet_t_h
11 #define powernet_t_h
12 
13 #include "../simtypes.h"
14 #include "../tpl/slist_tpl.h"
15 
16 /**
17  * Data class for power networks. A two phase queue to store
18  * and hand out power.
19  * @author Hj. Malthaner
20  */
21 class powernet_t
22 {
23 public:
24 	/**
25 	 * Max power capacity of each network.
26 	 * Avoids possible overflows while providing a human friendly number.
27 	 */
28 	static const uint64 max_capacity;
29 
30 	// number of fractional bits for network load values
31 	static const uint8 FRACTION_PRECISION;
32 
33 	/**
34 	 * Must be called when a new map is started or loaded. Clears the table of networks.
35 	 * @author Hj. Malthaner
36 	 */
37 	static void new_world();
38 
39 	/// Steps all powernets
40 	static void step_all(uint32 delta_t);
41 
42 private:
43 	static slist_tpl<powernet_t *> powernet_list;
44 
45 	// Network power supply.
46 	uint64 power_supply;
47 	// Network power demand.
48 	uint64 power_demand;
49 
50 	// Computed normalized demand.
51 	sint32 norm_demand;
52 	// Computed normalized supply.
53 	sint32 norm_supply;
54 
55 	// Just transfers power demand and supply to current step
56 	void step(uint32 delta_t);
57 
58 public:
59 	powernet_t();
60 	~powernet_t();
61 
get_max_capacity()62 	uint64 get_max_capacity() const { return max_capacity; }
63 
64 	/**
65 	 * Add power supply for next step.
66 	 */
add_supply(const uint32 p)67 	void add_supply(const uint32 p) { power_supply += (uint64)p; }
68 
69 	/**
70 	 * Subtract power supply for next step.
71 	 */
sub_supply(const uint32 p)72 	void sub_supply(const uint32 p) { power_supply -= (uint64)p; }
73 
74 	/**
75 	 * Get the total power supply of the network.
76 	 */
77 	uint64 get_supply() const;
78 
79 	/**
80 	 * Add power demand for next step.
81 	 */
add_demand(const uint32 p)82 	void add_demand(const uint32 p) { power_demand += (uint64)p; }
83 
84 	/**
85 	 * Subtract power demand for next step.
86 	 */
sub_demand(const uint32 p)87 	void sub_demand(const uint32 p) { power_demand -= (uint64)p; }
88 
89 	/**
90 	 * Get the total power demand of the network.
91 	 */
92 	uint64 get_demand() const;
93 
94 	/**
95 	 * Return the normalized value of demand in the network.
96 	 * Will have a logical value between 0 (no demand) and 1 (all supply consumed).
97 	 * Will have a logical value of 1 when no supply is present.
98 	 * Return value is fixed point with FRACTION_PRECISION fractional bits.
99 	 */
get_normal_demand()100 	sint32 get_normal_demand() const { return norm_demand; }
101 
102 	/**
103 	 * Return the normalized value of supply in the network.
104 	 * Will have a logical value between 0 (no supply) and 1 (all demand supplied).
105 	 * Will have a logical value of 1 when no demand is present.
106 	 * Return value is fixed point with FRACTION_PRECISION fractional bits.
107 	 */
get_normal_supply()108 	sint32 get_normal_supply() const { return norm_supply; }
109 };
110 
111 #endif
112