Debian Codegraph @

src/simulator/trafficsim.cpp

raw symbols SCIP
1 /***************************************************************************
2 trafficsim.cpp - description
3 -------------------
4 begin : may 1st, 2004
5 copyright : (C) 2003-2010 by Duong Khang NGUYEN
6 email : neoneurone @ gmail com
7
8 $Id: trafficsim.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 "trafficsim.h"
26 #include "buildinglayer.h"
27 #include "../map.h" // We want our own map.h
28 #include "pathstructure.h"
29 #include "pathfinder.h"
30 #include "movementmanager.h"
31 #include "vehicle.h"
32
33
34 /*=====================================================================*/
35 TrafficSim::TrafficSim(
36 SDL_mutex* mutex,
37 BuildingLayer* pblayer,
38 Map* pmap,
39 PathFinder* pf,
40 MovementManager* mm ):
41 Simulator( mutex, pblayer, pmap ),
42 _pPathFinder( pf ),
43 _pMovementManager( mm )
44 {
45 OPENCITY_DEBUG( "TSim param ctor" );
46 }
47
48
49 /*=====================================================================*/
50 TrafficSim::~TrafficSim()
51 {
52 OPENCITY_DEBUG( "TSim dtor" );
53 }
54
55
56 /*=====================================================================*/
57 int
58 TrafficSim::Main()
59 {
60 static uint w, h;
61 static uint w1, h1, w2, h2;
62 static uint startW, startH;
63 static Structure* pstruct;
64 static PathStructure* ppathstruct;
65 static int iTrafficValue;
66 static int iNumberPath;
67
68
69 if (_eSimState != SIMULATOR_RUNNING)
70 return 0;
71
72 // get a random road structure
73 pstruct = _pBuildLayer->GetRandomStructure(w, h, OC_STRUCTURE_ROAD );
74 if (pstruct == NULL)
75 return 0;
76
77 // Try to lock the mutex to prevent the others from deleting the structure
78 // pointed by "pstruct" while we're playing with
79 SDL_LockMutex( _pMutexMain );
80
81 // Save the starting point for pathfinding
82 startW = w; startH = h;
83
84 // Convert the pstruct to the correct structure
85 ppathstruct = (PathStructure*)pstruct;
86
87 // Get the surface around the structure we have
88 w1 = w; h1 = h;
89 _pMapCity->GetPossibleWH(
90 w1, h1, -OC_P_RCIP_RANGE, -OC_P_RCIP_RANGE );
91 w2 = w; h2 = h;
92 _pMapCity->GetPossibleWH(
93 w2, h2, OC_P_RCIP_RANGE, OC_P_RCIP_RANGE );
94
95 // Calculate the traffic
96 iTrafficValue = 0;
97 iNumberPath = 0; // this one is always > 0
98 for ( w = w1; w <= w2; w++ ) {
99 for ( h = h1; h <= h2; h++ ) {
100 // we reuse the "pstruct" variable here
101 pstruct = _pBuildLayer->GetStructure( w, h );
102 if (pstruct != NULL)
103 switch (pstruct->GetCode()) {
104 case OC_STRUCTURE_ROAD:
105 iNumberPath++;
106 break;
107 case OC_STRUCTURE_RES:
108 case OC_STRUCTURE_COM:
109 case OC_STRUCTURE_IND:
110 iTrafficValue += pstruct->GetLevel();
111 break;
112
113 default:
114 // keep gcc happy
115 break;
116 }
117 } // for h
118 } // for w
119
120 // Since "ubNumberPath" is always > 0
121 iTrafficValue = iTrafficValue / iNumberPath;
122
123 // give the current path structure the traffic value
124 // it's always >= 0 since
125 // since the structure's level is always >= 0
126 ppathstruct->SetTraffic((OC_BYTE)iTrafficValue );
127
128 // let the others run !
129 SDL_UnlockMutex( _pMutexMain );
130
131 // WARNING: the pathfinderShortestPath() need the unlocked mutex !
132 // Are we going to create a new vehicle ?
133 if ((iTrafficValue > OC_TSIM_TRAFFIC_MIN) and ((rand() % 100) < OC_TSIM_VEHICLE_CHANCE )) {
134 this->CreateNewVehicle(startW, startH);
135 _tiVariation[Simulator::OC_TRAFFIC]--;
136 }
137
138 return 0;
139 }
140
141
142 /*=====================================================================*/
143 void TrafficSim::CreateNewVehicle(
144 const uint& w,
145 const uint& h )
146 {
147 vector<Destination> vdest;
148 PathStructure* pstruct;
149 uint w2, h2;
150 int iRandom;
151 Vehicle* pvehicle;
152
153 // IF the manager is full THEN
154 if (_pMovementManager->IsFull())
155 return;
156
157 // try to get a destination
158 // NOTE: here we are sure that GetRandomStructure() returns
159 // a PathStructure* or NULL
160 // That's why whe doesn't need to use dynamic_cast<>
161 pstruct = (PathStructure*)_pBuildLayer->GetRandomStructure(w2, h2, OC_STRUCTURE_ROAD );
162 if (pstruct == NULL)
163 return;
164
165 // buses prefer short distance
166 iRandom = rand() % Vehicle::VEHICLE_NUMBER;
167 if ( iRandom == Vehicle::VEHICLE_BUS) {
168 _pPathFinder->findShortestPath(
169 w, h, w2, h2,
170 vdest,
171 PathFinder::OC_DISTANCE );
172 }
173 // sport vehicle prefer less traffic
174 else if ( (iRandom == Vehicle::VEHICLE_SPORT) || (iRandom == Vehicle::VEHICLE_STD) ) {
175 _pPathFinder->findShortestPath(
176 w, h, w2, h2,
177 vdest,
178 PathFinder::OC_TRAFFIC );
179 }
180
181 // now create the new vehicle if a path was found
182 if ( vdest.size() > 0 ) {
183 pvehicle = new Vehicle((Vehicle::VEHICLE_TYPE)iRandom );
184 pvehicle->SetPath( vdest ); // path init
185 pvehicle->Start(); // vehicle init
186 if (_pMovementManager->Add( pvehicle ) < 0) {
187 OPENCITY_DEBUG("WARNING: The movement manager is full");
188 delete pvehicle;
189 }
190 }
191 }