1 /***************************************************************************
2  *      Mechanized Assault and Exploration Reloaded Projectfile            *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 
20 #ifndef game_data_base_baseH
21 #define game_data_base_baseH
22 
23 #include <vector>
24 #include "game/data/units/unitdata.h"
25 
26 class cBuilding;
27 class cMap;
28 class cNetMessage;
29 class cPlayer;
30 class cServer;
31 class cPosition;
32 
33 struct sSubBase
34 {
35 public:
36 	explicit sSubBase (cPlayer* owner_);
37 	sSubBase (const sSubBase& other);
38 	~sSubBase();
39 
40 	/**
41 	* integrates all building of the given subbase in the own one
42 	* @author eiko
43 	*/
44 	void merge (sSubBase* sb);
45 
46 	/**
47 	* returns an unique number to identify the subbase
48 	* @author eiko
49 	*/
50 	int getID() const;
51 
52 	void addBuilding (cBuilding* b);
53 
54 	/**
55 	* adds/subtracts a resource to/from the subbase
56 	* @author eiko
57 	*/
58 	void addMetal (cServer& server, int value);
59 	void addOil (cServer& server, int value);
60 	void addGold (cServer& server, int value);
61 
62 	/**
63 	* recalculates the values of all subbases
64 	* @author eiko
65 	*/
66 	void refresh();
67 
68 	/**
69 	* inreases the energy production of the subbase by
70 	* starting offline generators/stations
71 	* @author eiko
72 	*/
73 	bool increaseEnergyProd (cServer& server, int value);
74 
75 	/**
76 	 * Checks, if there are consumers, that have to be shut down,
77 	 * due to a lack of a resources
78 	 */
79 	bool checkTurnEnd (cServer& server);
80 
81 	/**
82 	 * Produces resources, builds units and repairs/reloads units at turn start.
83 	 */
84 	void makeTurnStart (cServer& server);
85 
86 	//------------------------------------
87 	//resource management:
88 
89 	/** returns the maximum production of a resource */
90 	int getMaxMetalProd() const;
91 	int getMaxGoldProd() const;
92 	int getMaxOilProd() const;
93 
94 	/** returns the maximum allowed production
95 	 * (without decreasing one of the other ones) of a resource */
96 	int getMaxAllowedMetalProd() const;
97 	int getMaxAllowedGoldProd() const;
98 	int getMaxAllowedOilProd() const;
99 
100 	/** returns the current production of a resource */
101 	int getMetalProd() const;
102 	int getGoldProd() const;
103 	int getOilProd() const;
104 
105 	/** sets the production of a resource.
106 	 * If value is bigger then maxAllowed,
107 	 * it will be reduced to the maximum allowed value */
108 	void setMetalProd (int value);
109 	void setGoldProd (int value);
110 	void setOilProd (int value);
111 
112 	int getMetal() const;
113 	void setMetal (int value);
114 
115 	int getOil() const;
116 	void setOil (int value);
117 
118 	int getGold() const;
119 	void setGold (int value);
120 
121 	/** changes the production of a resource by value. */
122 	void changeMetalProd (int value);
123 	void changeGoldProd (int value);
124 	void changeOilProd (int value);
125 
126 	bool isDitributionMaximized() const;
127 
128 	void pushInto (cNetMessage& message) const;
129 	void popFrom (cNetMessage& message);
130 
131 	mutable cSignal<void ()> destroyed;
132 
133 	mutable cSignal<void ()> metalChanged;
134 	mutable cSignal<void ()> oilChanged;
135 	mutable cSignal<void ()> goldChanged;
136 private:
137 
138 	//-----------------------------------
139 	//turn end management:
140 
141 	/**
142 	 * checks if consumers have to be switched off, due to a lack of resources
143 	 * @return returns true, if consumers have been shut down
144 	 * @author eiko
145 	 */
146 	bool checkGoldConsumer (cServer& server);
147 	bool checkHumanConsumer (cServer& server);
148 	bool checkMetalConsumer (cServer& server);
149 	/**
150 	 * - switches off unneeded fuel consumers(=energy producers)
151 	 * - sets the optimal amount of generators and stations
152 	 *   to minimize fuel consumption
153 	 * - increases oil production, if necessary
154 	 * - switches off oil consumers, if too few oil is available
155 	 * @return: returns true, if oil consumers have been shut down,
156 	 *          due to a lack of oil
157 	 * @author eiko
158 	 */
159 	bool checkOil (cServer& server);
160 	/**
161 	 * switches off energy consumers, if necessary
162 	 * @return returns true, if a energy consumers have been shut down
163 	 * @author eiko
164 	 */
165 	bool checkEnergy (cServer& server);
166 
167 	void makeTurnStartRepairs (cServer& server, cBuilding& building);
168 	void makeTurnStartReload (cServer& server, cBuilding& building);
169 	void makeTurnStartBuild (cServer& server, cBuilding& building);
170 
171 
172 	/**
173 	* calcs the maximum allowed production of a resource,
174 	* without decreasing the production of the other two
175 	* @author eiko
176 	*/
177 	int calcMaxAllowedProd (int ressourceType) const;
178 	/**
179 	* calcs the maximum possible production of a resource
180 	* @author eiko
181 	*/
182 	int calcMaxProd (int ressourceType) const;
183 	/**
184 	* adds/subtracts resources of the type storeResType to/from the subbase
185 	* @author eiko
186 	*/
187 	void addRessouce (cServer& server, sUnitData::eStorageResType storeResType, int value);
188 
189 	int getResource (sUnitData::eStorageResType storeResType) const;
190 	void setResource (sUnitData::eStorageResType storeResType, int value);
191 public:
192 	//private:
193 	//	friend class cBase;
194 	std::vector<cBuilding*> buildings;
195 	cPlayer* owner;
196 
197 	int MaxMetal;
198 	int MaxOil;
199 	int MaxGold;
200 
201 	int MaxEnergyProd;
202 	int EnergyProd;
203 	int MaxEnergyNeed;
204 	int EnergyNeed;
205 	int MetalNeed;
206 	int OilNeed;
207 	int GoldNeed;
208 	int MaxMetalNeed;
209 	int MaxOilNeed;
210 	int MaxGoldNeed;
211 
212 	int HumanProd;
213 	int HumanNeed;
214 	int MaxHumanNeed;
215 
216 	int MetalProd;
217 	int OilProd;
218 	int GoldProd;
219 
220 private:
221 	int metal;
222 	int oil;
223 	int gold;
224 };
225 
226 class cBase
227 {
228 public:
229 	cBase();
230 	~cBase();
231 
232 	/**
233 	* adds a building to the base and updates the subbase structures
234 	* @param building the building, that is added to the base
235 	* @param server when not null, the resulting subbase values are sent to the client
236 	* @author eiko
237 	*/
238 	void addBuilding (cBuilding* building, cServer* server);
239 	/**
240 	* deletes a building from the base and updates the subbase structures
241 	* @param building the building, that is deleted to the base
242 	* @param server when not null, the resulting subbase values are sent to the client
243 	* @author eiko
244 	*/
245 	void deleteBuilding (cBuilding* building, cServer* server);
246 
247 	bool checkTurnEnd (cServer& server);
248 
249 	/**
250 	 * Handles the turn start for all sub bases.
251 	 *
252 	 * This produces resources, builds units and repairs/reloads units.
253 	 */
254 	void makeTurnStart (cServer& server);
255 
256 	/**
257 	* recalculates the values of all subbases
258 	*@author eiko
259 	*/
260 	void refreshSubbases();
261 
262 	sSubBase* checkNeighbour (const cPosition& position, const cBuilding& Building);
263 
264 public:
265 	std::vector<sSubBase*> SubBases;
266 	cMap* map;
267 };
268 
269 #endif // game_data_base_baseH
270