Debian Codegraph @

src/simulator/electricitysim.cpp

raw symbols SCIP
1 /***************************************************************************
2 electricitysim.cpp - description
3 -------------------
4 begin : march 2nd, 2004
5 copyright : (C) 2004-2010 by Duong Khang NGUYEN
6 email : neoneurone @ gmail com
7
8 $Id: electricitysim.cpp 450 2010-11-21 19:11:43Z neoneurone $
9 ***************************************************************************/
10
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * any later version. *
17 * *
18 ***************************************************************************/
19
20 // Useful enumerations
21 #include "opencity_direction.h"
22 #include "opencity_structure_type.h"
23
24 // OpenCity headers
25 #include "electricitysim.h"
26 #include "buildinglayer.h"
27 #include "structure.h"
28
29 // Global settings
30 #include "globalvar.h"
31 extern GlobalVar gVars;
32
33 // Standard headers
34 #include <algorithm>
35
36 using namespace std;
37
38
39 /*=====================================================================*/
40 ElectricitySim::ElectricitySim(
41 SDL_mutex* mutex,
42 BuildingLayer* pblayer,
43 Map* pmap ):
44 Simulator( mutex, pblayer, pmap ),
45 _uiNumberEPlant( 0 ),
46 _iValueMax( 0 )
47 {
48 OPENCITY_DEBUG( "ESim param ctor" );
49 }
50
51
52 /*=====================================================================*/
53 ElectricitySim::~ElectricitySim()
54 {
55 OPENCITY_DEBUG( "ESim dtor" );
56 }
57
58
59 /*=====================================================================*/
60 void
61 ElectricitySim::SaveTo( std::fstream& rfs )
62 {
63 // Call the base class method
64 Simulator::SaveTo( rfs );
65 }
66
67
68 /*=====================================================================*/
69 void
70 ElectricitySim::LoadFrom( std::fstream& rfs )
71 {
72 // Call the base class method
73 Simulator::LoadFrom( rfs );
74
75 // Member variable reinitialization
76 _uiNumberEPlant = 0;
77 _iValueMax = 0;
78 _vectorpairuiEPlant.clear();
79 }
80
81
82 //========================================================================
83 // int simulatorMain()
84 //
85 // description: simulate the transmission of the electricity
86 //
87 // algorithm : it is inpired by my GRAPH knownledge
88 // (which is quite limited). In few words, it is
89 // breadth-first search algorithm
90 //
91 // note : (28 jun 04) Currently, RCISims unset the Electricity bit
92 // then set it again if there's an electric structure around
93 // the under processing structure. I don't think that is a
94 // good idea. We should let the electricity sim do its job.
95 //========================================================================
96 int
97 ElectricitySim::Main()
98 {
99 static Structure* pstruct;
100 static vector< pair<uint, uint> >::iterator iter;
101 static pair<uint, uint> pairstructWH;
102 static uint nW, nH;
103
104 // IF the simulators are not running THEN return
105 if (_eSimState != SIMULATOR_RUNNING)
106 return 0;
107
108 SDL_LockMutex( _pMutexMain );
109
110 // clear the mark and E bit of ALL structure
111 _pBuildLayer->StructureUnset( (OC_BYTE)( OC_STRUCTURE_MARK | OC_STRUCTURE_E ) );
112
113 // for each EPLANT do
114 iter = _vectorpairuiEPlant.begin();
115 while ( iter != _vectorpairuiEPlant.end() ) {
116 pairstructWH = *iter;
117 pstruct = _pBuildLayer->GetStructure( pairstructWH.first, pairstructWH.second );
118
119 // only process the EPLANT if it is not marked yet
120 if (pstruct != NULL)
121 if (pstruct->IsSet( OC_STRUCTURE_MARK ) == false )
122 _dequepairui.push_back( pairstructWH );
123
124 // now process the deque
125 while ( !_dequepairui.empty() ) {
126 pairstructWH = _dequepairui.front();
127 _dequepairui.pop_front();
128 // process the front structure
129 pstruct = _pBuildLayer->GetStructure( pairstructWH.first, pairstructWH.second );
130 // WARNING: pstruct is never NULL here !
131 switch (pstruct->GetCode()) {
132 // turn the E bit of the following structures on
133 case OC_STRUCTURE_EPLANT_COAL:
134 case OC_STRUCTURE_EPLANT_NUCLEAR:
135 pstruct->Set(OC_STRUCTURE_E);
136 break;
137
138 // the RCI zones need nearby electric zone
139 // in order to be connected to the E system
140 case OC_STRUCTURE_RES:
141 case OC_STRUCTURE_COM:
142 case OC_STRUCTURE_IND:
143 case OC_STRUCTURE_PART:
144 case OC_STRUCTURE_FIREDEPT:
145 case OC_STRUCTURE_POLICEDEPT:
146 case OC_STRUCTURE_EDUCATIONDEPT:
147 case OC_STRUCTURE_HOSPITALDEPT:
148 if (CheckRange(
149 pairstructWH.first, pairstructWH.second,
150 OC_E_RCI_RANGE, OC_STRUCTURE_ELECTRIC ) == true) {
151 pstruct->Set(OC_STRUCTURE_E);
152 }
153 break;
154
155 // the ELINE structure have a shorter range
156 case OC_STRUCTURE_ELINE:
157 if (CheckRange(
158 pairstructWH.first, pairstructWH.second,
159 OC_E_E_RANGE, OC_STRUCTURE_ELECTRIC ) == true) {
160 pstruct->Set(OC_STRUCTURE_E);
161 }
162 break;
163
164 // Ignored structures
165 case OC_STRUCTURE_PARK:
166 case OC_STRUCTURE_FLORA:
167 case OC_STRUCTURE_ROAD:
168 case OC_STRUCTURE_TEST: // Development test feature
169 break;
170
171 // What's the heck ?
172 case OC_STRUCTURE_UNDEFINED:
173 case OC_STRUCTURE_ANY:
174 case OC_STRUCTURE_ELECTRIC:
175
176 // Future asserts
177 case OC_STRUCTURE_MILITARYDEPT:
178
179 // What's the heck ?
180 default:
181 OPENCITY_DEBUG( "What is this structure code: " << pstruct->GetCode() );
182 assert( 0 );
183 break;
184 } // switch
185
186 // Mark the structure we have just processed
187 pstruct->Set( OC_STRUCTURE_MARK );
188
189 // Put all the neighbour into the deque if they are not yet marked (processed)
190 // look for the neighour in the NORTH
191 if ((_pMapCity->GetNeighbourWH(
192 pairstructWH.first, pairstructWH.second,
193 nW, nH, OC_DIR_O_N ) == true )
194 and (pstruct = _pBuildLayer->GetStructure( nW, nH ))
195 and (pstruct->IsSet(OC_STRUCTURE_MARK) == false )
196 and (this->DequeContain(make_pair(nW, nH)) == false))
197 _dequepairui.push_back( pair<uint,uint>( nW, nH) );
198
199 // look for the neighour in the EAST
200 if ((_pMapCity->GetNeighbourWH(
201 pairstructWH.first, pairstructWH.second,
202 nW, nH, OC_DIR_O_E ) == true )
203 and (pstruct = _pBuildLayer->GetStructure( nW, nH ))
204 and (pstruct->IsSet(OC_STRUCTURE_MARK) == false )
205 and (this->DequeContain(make_pair(nW, nH)) == false))
206 _dequepairui.push_back( pair<uint,uint>( nW, nH) );
207
208 // look for the neighour in the SOUTH
209 if ((_pMapCity->GetNeighbourWH(
210 pairstructWH.first, pairstructWH.second,
211 nW, nH, OC_DIR_O_S ) == true )
212 and (pstruct = _pBuildLayer->GetStructure( nW, nH ))
213 and (pstruct->IsSet(OC_STRUCTURE_MARK) == false )
214 and (this->DequeContain(make_pair(nW, nH)) == false))
215 _dequepairui.push_back( pair<uint,uint>( nW, nH) );
216
217 // look for the neighour in the WEST
218 if ((_pMapCity->GetNeighbourWH(
219 pairstructWH.first, pairstructWH.second,
220 nW, nH, OC_DIR_O_W ) == true )
221 and (pstruct = _pBuildLayer->GetStructure( nW, nH ))
222 and (pstruct->IsSet(OC_STRUCTURE_MARK) == false )
223 and (this->DequeContain(make_pair(nW, nH)) == false))
224 _dequepairui.push_back( pair<uint,uint>( nW, nH) );
225 } // while
226
227 // next EPLANT
228 iter++;
229 } // while _vectorpairuiEPlant
230
231 SDL_UnlockMutex( _pMutexMain );
232
233 return 0;
234 }
235
236
237
238 /*=====================================================================*/
239 void
240 ElectricitySim::AddStructure(
241 const uint & w1,
242 const uint & h1,
243 const uint & w2,
244 const uint & h2 )
245 {
246 Structure* pstruct;
247
248 pstruct = _pBuildLayer->GetStructure( w1, h1 );
249 if ( pstruct == NULL )
250 return;
251
252 switch( pstruct->GetCode() ) {
253 case OC_STRUCTURE_EPLANT_COAL:
254 _iValueMax += OC_EPLANT_COAL_POWER;
255 _uiNumberEPlant++;
256 _vectorpairuiEPlant.push_back( pair<uint, uint>( w1, h1 ) );
257 break;
258
259 case OC_STRUCTURE_EPLANT_NUCLEAR:
260 _iValueMax += OC_EPLANT_NUCLEAR_POWER;
261 _uiNumberEPlant++;
262 _vectorpairuiEPlant.push_back( pair<uint, uint>( w1, h1 ) );
263 break;
264
265 default: // keep gcc happy
266 break;
267 } // switch
268 }
269
270
271 /*=====================================================================*/
272 void
273 ElectricitySim::RemoveStructure(
274 const uint & w1,
275 const uint & h1,
276 const uint & w2,
277 const uint & h2 )
278 {
279 Structure* pstruct;
280 OPENCITY_STRUCTURE_CODE enumStructCode;
281 uint mainW1 = w1, mainH1 = h1, mainW2 = w1, mainH2 = h1;
282 uint mainW = w1, mainH = h1;
283 bool boolFound = false;
284 uint sw, sl, sh; // Structure's width, length and height
285
286 // Remove the w, h of the main structure
287 vector< pair<uint, uint> >::iterator iter;
288
289 pstruct = _pBuildLayer->GetStructure( mainW, mainH );
290 if ( pstruct == NULL )
291 return;
292
293 // Normal structure codes
294 enumStructCode = pstruct->GetCode();
295 switch ( enumStructCode ) {
296 case OC_STRUCTURE_PART:
297 // get the main structure pointer, and code
298 pstruct = pstruct->GetMain();
299 enumStructCode = pstruct->GetCode();
300
301 // Calculate the structure's range
302 gVars.gpPropertyMgr->GetWLH( pstruct->GetGraphicCode(), sw, 4, sl, 4, sh, 1 );
303 sw--; sl--;
304 // now look for the main w, h
305 _pMapCity->GetPossibleWH( mainW1, mainH1, -sw, -sl );
306 _pMapCity->GetPossibleWH( mainW2, mainH2, sw, sl );
307 mainH = mainH1;
308 while ( (boolFound == false) and (mainH <= mainH2) ) {
309 mainW = mainW1;
310 while ( (boolFound == false) and (mainW <= mainW2) ) {
311 // IF the current structure at mainW, mainH
312 // is the main structure THEN we found it
313 if ( _pBuildLayer->GetStructure( mainW, mainH ) == pstruct )
314 boolFound = true;
315 else
316 mainW++;
317 }
318 mainH++;
319 }
320 break;
321
322 case OC_STRUCTURE_RES:
323 case OC_STRUCTURE_COM:
324 case OC_STRUCTURE_IND:
325 case OC_STRUCTURE_FIREDEPT:
326 case OC_STRUCTURE_POLICEDEPT:
327 case OC_STRUCTURE_EDUCATIONDEPT:
328 break;
329
330 default: // keep gcc happy
331 break;
332 }
333
334 // Power plant structure codes
335 switch ( enumStructCode ) {
336 case OC_STRUCTURE_EPLANT_COAL:
337 _iValueMax -= OC_EPLANT_COAL_POWER;
338 _uiNumberEPlant--;
339
340 // Search for the pair of mainW, mainH in the "_vectorpairuiEPlant"
341 // and delete it if found
342 iter = find(
343 _vectorpairuiEPlant.begin(),
344 _vectorpairuiEPlant.end(),
345 make_pair( mainW, mainH )
346 );
347 if ( iter != _vectorpairuiEPlant.end() )
348 _vectorpairuiEPlant.erase( iter );
349 break;
350
351 case OC_STRUCTURE_EPLANT_NUCLEAR:
352 _iValueMax -= OC_EPLANT_NUCLEAR_POWER;
353 _uiNumberEPlant--;
354
355 // search for the pair of mainW, mainH in the "_vectorpairuiEPlant"
356 // and delete it if found
357 iter = find(
358 _vectorpairuiEPlant.begin(),
359 _vectorpairuiEPlant.end(),
360 make_pair( mainW, mainH )
361 );
362 if ( iter != _vectorpairuiEPlant.end() )
363 _vectorpairuiEPlant.erase( iter );
364 break;
365
366 default: // keep gcc happy
367 break;
368 }
369 }
370
371
372 /*=====================================================================*/
373 const int
374 ElectricitySim::GetMaxValue() const
375 {
376 return _iValueMax;
377 }
378
379
380 /*=====================================================================*/
381 bool
382 ElectricitySim::DequeContain( const pair<uint, uint> & pairui )
383 {
384 if (find( _dequepairui.begin(), _dequepairui.end(), pairui )
385 != _dequepairui.end())
386 return true;
387 else
388 return false;
389 }