Debian Codegraph @

src/simulator/residentialsim.cpp

raw symbols SCIP
1 /***************************************************************************
2 residentialsim.cpp - description
3 -------------------
4 begin : sep 21th, 2003
5 copyright : (C) 2003-2010 by Duong Khang NGUYEN
6 email : neoneurone @ gmail com
7
8 $Id: residentialsim.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_structure_type.h"
22
23 // OpenCity headers
24 #include "residentialsim.h"
25 #include "buildinglayer.h"
26 #include "structure.h"
27
28
29 /*=====================================================================*/
30 ResidentialSim::ResidentialSim(
31 SDL_mutex* mutex,
32 BuildingLayer* pblayer,
33 Map* pmap ):
34 Simulator( mutex, pblayer, pmap )
35 {
36 OPENCITY_DEBUG( "RSim param ctor" );
37 }
38
39
40 /*=====================================================================*/
41 ResidentialSim::~ResidentialSim()
42 {
43 OPENCITY_DEBUG( "RSim dtor" );
44 }
45
46
47 //========================================================================
48 // int Main() const
49 //
50 // description: simulate the growth of a Residential square
51 // algorithm :
52 // 1. get a random Residential structure
53 // 2. look around for necessary structures
54 // 3. IF there's all needed structures around THEN
55 // IF chance < OC_SIMULATOR_UP THEN
56 // levelUp()
57 // ELSE
58 // levelDown()
59 // FI
60 // ELSE
61 // IF chance < OC_SIMULATOR_DOWN THEN
62 // levelDown()
63 // FI
64 // FI
65 //========================================================================
66 int
67 ResidentialSim::Main()
68 {
69 static uint w, l;
70 static Structure* pstruct;
71 static bool boolLevelUp;
72 static int iRandom;
73 static OPENCITY_GRAPHIC_CODE oldGC;
74
75
76 if (_eSimState != SIMULATOR_RUNNING)
77 return 0;
78
79 // Get a random residential structure
80 pstruct = _pBuildLayer->GetRandomStructure(w, l, OC_STRUCTURE_RES );
81 if (pstruct == NULL)
82 return 0;
83
84 boolLevelUp = false;
85 // OPENCITY_DEBUG( "Begin - ResidentialSim - w/l: " << w << "/" << l );
86
87 // Try to lock the mutex to prevent the others from deleting the structure
88 // pointed by "pstruct" while we're playing with
89 SDL_LockMutex( _pMutexMain );
90
91 pstruct->Unset(
92 OC_STRUCTURE_W | OC_STRUCTURE_G |
93 OC_STRUCTURE_R | OC_STRUCTURE_C | OC_STRUCTURE_I |
94 OC_STRUCTURE_P );
95 pstruct->Set( OC_STRUCTURE_R );
96
97 // IF there's a P in range THEN
98 if (CheckRange(w, l, OC_R_P_RANGE, OC_STRUCTURE_ROAD) == true)
99 pstruct->Set( OC_STRUCTURE_P );
100 // ELSE we need more traffic
101 else
102 _tiVariation[Simulator::OC_TRAFFIC]++;
103
104 // Is there a C in range ?
105 if (CheckRange(w, l, OC_R_C_RANGE, OC_STRUCTURE_COM) == true)
106 pstruct->Set( OC_STRUCTURE_C );
107 else
108 _tiVariation[Simulator::OC_COMMERCIAL]++;
109
110 // Is there a I in range ?
111 if (CheckRange(w, l, OC_R_I_RANGE, OC_STRUCTURE_IND) == true)
112 pstruct->Set( OC_STRUCTURE_I );
113 else
114 _tiVariation[Simulator::OC_INDUSTRIAL]++;
115
116 if (pstruct->IsSet(
117 OC_STRUCTURE_E |
118 OC_STRUCTURE_R | OC_STRUCTURE_C | OC_STRUCTURE_I |
119 OC_STRUCTURE_P ) == true )
120 boolLevelUp = true;
121
122 iRandom = rand() % 100;
123 oldGC = pstruct->GetGraphicCode();
124 if (boolLevelUp == true) {
125 // Really levelup ?
126 if (iRandom < OC_SIMULATOR_UP) {
127 if ((this->CheckLevelUp(w, l, pstruct) == true)
128 and (pstruct->LevelUp() == true)) {
129 _pBuildLayer->ResizeStructure( w, l, oldGC );
130 _iValue++;
131 _tiVariation[Simulator::OC_RESIDENTIAL]--;
132 }
133 }
134 } // end if levelup
135 else {
136 // Really level down ?
137 if (iRandom < OC_SIMULATOR_DOWN)
138 if ((this->CheckLevelDown(w, l, pstruct) == true)
139 && (pstruct->LevelDown() == true)) {
140 _pBuildLayer->ResizeStructure( w, l, oldGC );
141 _iValue--;
142 _tiVariation[Simulator::OC_RESIDENTIAL]++;
143 }
144 }
145
146 // Let the other simulators run
147 SDL_UnlockMutex( _pMutexMain );
148
149 // OPENCITY_DEBUG( "End - ResidentialSim - w/l: " << w << "/" << l );
150
151 return 0;
152 }
153
154
155 /*=====================================================================*/
156 void
157 ResidentialSim::RemoveStructure(
158 const uint & w1,
159 const uint & h1,
160 const uint & w2,
161 const uint & h2 )
162 {
163 Structure* pstruct = _pBuildLayer->GetStructure( w1, h1 );
164
165 // if this is a R zone
166 // and it has a positive value according to its level
167 // then we remove its value from the sim.
168 if (pstruct != NULL)
169 if (pstruct->GetCode() == OC_STRUCTURE_RES)
170 if (pstruct->GetLevel() - 1 > 0)
171 _iValue -= pstruct->GetLevel()-1;
172 }