Debian Codegraph @

src/mas/environment.cpp

raw symbols SCIP
1 /***************************************************************************
2 environment.h - description
3 -------------------
4 begin : dec 11th, 2005
5 copyright : (C) 2005-2010 by Duong Khang NGUYEN
6 email : neoneurone @ gmail com
7 author : Victor STINNER
8
9 $Id: environment.cpp 448 2010-05-09 15:18:32Z neoneurone $
10 ***************************************************************************/
11
12 /***************************************************************************
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * any later version. *
18 * *
19 ***************************************************************************/
20
21 // Useful enumeration
22 #include "opencity_direction.h"
23 #include "opencity_structure_type.h"
24
25 // MAS headers
26 #include "environment.h"
27 #include "kernel.h"
28
29 // OpenCity headers
30 #include "structure.h"
31 #include "layer.h"
32 #include "graphicmanager.h"
33 #include "pathfinder.h"
34
35 // Global settings
36 #include "globalvar.h"
37 extern GlobalVar gVars;
38
39
40 /*=====================================================================*/
41 unsigned char randomBool()
42 {
43 return rand() <= (RAND_MAX / 2);
44 }
45
46
47 /*=====================================================================*/
48 int randomInt(int min, int max)
49 {
50 unsigned int width = (max - min + 1);
51 return static_cast<int> (min + (double)rand() * width / RAND_MAX);
52 }
53
54
55 /*=====================================================================*/
56 MAS_DIRECTION rotate180(MAS_DIRECTION dir)
57 {
58 switch (dir)
59 {
60 case NORTH: return SOUTH;
61 case EAST: return WEST;
62 case SOUTH: return NORTH;
63 case WEST: return EAST;
64 }
65
66 // We should never reach here
67 assert(0);
68 return NORTH;
69 }
70
71
72 /*=====================================================================*/
73 MAS_DIRECTION rotateLeft(MAS_DIRECTION dir)
74 {
75 switch (dir)
76 {
77 case NORTH: return WEST;
78 case EAST: return NORTH;
79 case SOUTH: return EAST;
80 case WEST: return SOUTH;
81 }
82
83 // We should never reach here
84 assert(0);
85 return NORTH;
86 }
87
88
89 /*=====================================================================*/
90 MAS_DIRECTION rotateRight(MAS_DIRECTION dir)
91 {
92 switch (dir)
93 {
94 case NORTH: return EAST;
95 case EAST: return SOUTH;
96 case SOUTH: return WEST;
97 case WEST: return NORTH;
98 }
99
100 // We should never reach here
101 assert(0);
102 return NORTH;
103 }
104
105 /*=====================================================================*/
106 Environment::Environment(
107 unsigned int width,
108 unsigned int height,
109 Layer* pBL,
110 Kernel *kernel):
111 _width(width),
112 _height(height),
113 _pKernel(kernel),
114 _pBuildingLayer(pBL)
115 {
116 _vector.assign(_width * _height, NULL);
117 }
118
119
120 /*=====================================================================*/
121 void Environment::displayAgent()
122 {
123 assert( gVars.gpGraphicMgr != NULL );
124
125 _vector_cit it=_vector.begin(), end=_vector.end();
126 for (; it != end; ++it) {
127 if (*it) {
128 Agent *agent = *it;
129 float x = agent->getX();
130 float y = agent->getY();
131 _last_pos_it last = _last_pos.find(agent);
132 if (last != _last_pos.end()) {
133 unsigned int dt = _pKernel->getStep() - last->second.step;
134 if (dt < 3) {
135 int lastx = last->second.x;
136 int lasty = last->second.y;
137 x = x + (x - lastx) * dt / 3;
138 y = y + (y - lasty) * dt / 3;
139 }
140 else {
141 _last_pos.erase(last);
142 }
143 }
144 gVars.gpGraphicMgr->DisplayAgent(x, y, agent);
145 // debug
146 // cout << "Registered agent with GC : " << (*it)->GetGraphicCode() << endl;
147 }
148 }
149 }
150
151
152 /*=====================================================================*/
153 unsigned int Environment::getHeight() const
154 {
155 return _width;
156 }
157
158
159 /*=====================================================================*/
160 unsigned int Environment::getWidth() const
161 {
162 return _height;
163 }
164
165
166 /*=====================================================================*/
167 void Environment::registerAgent(Agent* agent, int x, int y)
168 {
169 unsigned int pos = agent->getY()*_width + agent->getX();
170 if (_vector[pos] != NULL) {
171 cerr << "There's already one agent at x: " << x << " / y: " << y << endl;
172 return;
173 }
174 _vector[pos] = agent;
175 }
176
177
178 /*=====================================================================*/
179 void Environment::unregisterAgent(Agent* agent)
180 {
181 unsigned int pos = agent->getY()*_width + agent->getX();
182 assert(_vector[pos] != NULL);
183 _vector[pos] = NULL;
184 }
185
186
187 /*=====================================================================*/
188 bool Environment::hasAgentAt(const Agent *agent, int x, int y) const
189 {
190 return (_vector.at(y*_width + x) != NULL);
191 }
192
193
194 /*=====================================================================*/
195 Agent* Environment::getAgentAt(int x, int y)
196 {
197 if (x < 0 || (int)_width <= x) return NULL;
198 if (y < 0 || (int)_height <= y) return NULL;
199 return _vector.at(y*_width + x);
200 }
201
202
203 /*=====================================================================*/
204 bool Environment::moveAgent(Agent* agent, int x, int y)
205 {
206 unsigned int pos = y*_width + x;
207 unsigned int oldpos = agent->getY()*_width + agent->getX();
208 if (_vector[pos] != NULL) return false;
209 _last_pos[agent] = AgentPosition(agent->getX(),agent->getY(),_pKernel->getStep());
210 _vector[pos] = agent;
211 _vector[oldpos] = NULL;
212 return true;
213 }
214
215
216 /*=====================================================================*/
217 const bool
218 Environment::findShortestPath(
219 unsigned int x1, unsigned int y1,
220 unsigned int x2, unsigned int y2,
221 std::vector<Destination> & rvdest )
222 {
223 assert(gVars.gpPathFinder != NULL);
224
225 return gVars.gpPathFinder->findShortestPath(
226 x1, y1, x2, y2, rvdest,
227 PathFinder::OC_DISTANCE, MAX_PATH_LENGTH );
228 }
229
230
231 /*=====================================================================*/
232 Structure*
233 Environment::getBuildingXY(
234 unsigned int x,
235 unsigned int y)
236 {
237 return _pBuildingLayer->GetStructure( x, y );
238 }
239
240
241 /*=====================================================================*/
242 /* STATIC METHODS */
243 /*=====================================================================*/
244 unsigned int
245 Environment::toSquareDistance(
246 unsigned int x1, unsigned int y1,
247 unsigned int x2, unsigned int y2)
248 {
249 return ((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
250 }