Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

environment.cpp

00001 /***************************************************************************
00002                         environment.h  -  description
00003                             -------------------
00004     begin                : dec 11th 2005
00005     copyright            : (C) 2005 by Duong-Khang NGUYEN
00006     email                : neoneurone @ users sourceforge net
00007     author               : Victor STINNER
00008 
00009     $Id: environment.cpp 63 2006-10-17 20:45:12Z neoneurone $
00010 ***************************************************************************/
00011 
00012 /***************************************************************************
00013 *                                                                         *
00014 *   This program is free software; you can redistribute it and/or modify  *
00015 *   it under the terms of the GNU General Public License as published by  *
00016 *   the Free Software Foundation; either version 2 of the License, or     *
00017 *   any later version.                                                    *
00018 *                                                                         *
00019 ***************************************************************************/
00020 
00021 
00022 #include "environment.h" 
00023 #include "structure.h"
00024 #include "layer.h"
00025 #include "kernel.h"
00026 #include "graphicmanager.h"
00027 #include "pathfinder.h"
00028 
00029 #include "globalvar.h"
00030 extern GlobalVar gVars;
00031 
00032 
00033 /*=====================================================================*/
00034 unsigned char randomBool()
00035 {
00036     return rand() <= (RAND_MAX / 2);
00037 }
00038 
00039 
00040 /*=====================================================================*/
00041 int randomInt(int min, int max)
00042 {
00043     unsigned int width = (max - min + 1);
00044     return static_cast<int> (min + (double)rand() * width / RAND_MAX);
00045 }
00046 
00047 
00048     /*=====================================================================*/
00049 direction_t rotate180(direction_t dir)
00050 {
00051     switch (dir)
00052     {
00053         case NORTH: return SOUTH; 
00054         case EAST:  return WEST;
00055         case SOUTH: return NORTH;
00056         case WEST:  return EAST;
00057     }
00058 
00059 // We should never reach here
00060     assert(0);
00061     return NORTH;
00062 }
00063 
00064 
00065     /*=====================================================================*/
00066 direction_t rotateLeft(direction_t dir)
00067 {
00068     switch (dir)
00069     {
00070         case NORTH: return WEST; 
00071         case EAST:  return NORTH;
00072         case SOUTH: return EAST;
00073         case WEST:  return SOUTH;
00074     }
00075 
00076 // We should never reach here
00077     assert(0);
00078     return NORTH;
00079 }
00080 
00081 
00082     /*=====================================================================*/
00083 direction_t rotateRight(direction_t dir)
00084 {
00085     switch (dir)
00086     {
00087         case NORTH: return EAST; 
00088         case EAST:  return SOUTH;
00089         case SOUTH: return WEST;
00090         case WEST:  return NORTH;
00091     }
00092 
00093 // We should never reach here
00094     assert(0);
00095     return NORTH;
00096 }
00097 
00098     /*=====================================================================*/
00099 Environment::Environment(
00100     unsigned int width,
00101     unsigned int height,
00102     Layer* pBL,
00103     Kernel *kernel):
00104 m_width(width),
00105 m_height(height),
00106 m_kernel(kernel),
00107 m_pBuildingLayer(pBL)
00108 {
00109     m_vector.assign(m_width * m_height, NULL);
00110 }
00111 
00112 
00113 /*=====================================================================*/
00114 void Environment::displayAgent()
00115 {
00116     assert( gVars.gpGraphicMgr != NULL );
00117 
00118     m_vector_cit it=m_vector.begin(), end=m_vector.end();
00119     for (; it != end; ++it) {
00120         if (*it) {
00121             Agent *agent = *it;
00122             float x = agent->getX();
00123             float y = agent->getY();
00124             m_last_pos_it last = m_last_pos.find(agent);
00125             if (last != m_last_pos.end()) {
00126                 unsigned int dt = m_kernel->getStep() - last->second.step;
00127                 if (dt  < 3) {
00128                     int lastx = last->second.x;
00129                     int lasty = last->second.y;
00130                     x = x + (x - lastx) * dt / 3;
00131                     y = y + (y - lasty) * dt / 3;
00132                 }
00133                 else {
00134                     m_last_pos.erase(last);
00135                 }
00136             }
00137             gVars.gpGraphicMgr->DisplayAgent(x, y, agent);
00138 // debug
00139 //          cout << "Registered agent with GC : " << (*it)->GetGraphicCode() << endl;
00140         }
00141     }
00142 }
00143 
00144 
00145 /*=====================================================================*/
00146 unsigned int Environment::getHeight() const
00147 {
00148     return m_width;
00149 }
00150 
00151 
00152 /*=====================================================================*/
00153 unsigned int Environment::getWidth() const
00154 {
00155     return m_height;
00156 }
00157 
00158 
00159 /*=====================================================================*/
00160 void Environment::registerAgent(Agent* agent, int x, int y)
00161 {
00162     unsigned int pos = agent->getY()*m_width + agent->getX();
00163     if (m_vector[pos] != NULL) {
00164         cerr << "There's already one agent at x: " << x << " / y: " << y << endl;
00165         return;
00166     }
00167     m_vector[pos] = agent;
00168 }
00169 
00170 
00171 /*=====================================================================*/
00172 void Environment::unregisterAgent(Agent* agent)
00173 {
00174     unsigned int pos = agent->getY()*m_width + agent->getX();
00175     assert(m_vector[pos] != NULL);
00176     m_vector[pos] = NULL;
00177 }
00178 
00179 
00180 /*=====================================================================*/
00181 bool Environment::hasAgentAt(const Agent *agent, int x, int y) const
00182 {
00183     return (m_vector.at(y*m_width + x) != NULL);
00184 }
00185 
00186 
00187 /*=====================================================================*/
00188 Agent* Environment::getAgentAt(int x, int y)
00189 {
00190     if (x < 0 || (int)m_width <= x) return NULL;
00191     if (y < 0 || (int)m_height <= y) return NULL;
00192     return m_vector.at(y*m_width + x);
00193 }
00194 
00195 
00196 /*=====================================================================*/
00197 bool Environment::moveAgent(Agent* agent, int x, int y)
00198 {
00199     unsigned int pos = y*m_width + x;
00200     unsigned int oldpos = agent->getY()*m_width + agent->getX();
00201     if (m_vector[pos] != NULL) return false;
00202     m_last_pos[agent] = AgentPosition(agent->getX(),agent->getY(),m_kernel->getStep());
00203     m_vector[pos] = agent;
00204     m_vector[oldpos] = NULL;
00205     return true;
00206 }
00207 
00208 
00209 /*=====================================================================*/
00210 const bool
00211 Environment::findShortestPath(
00212     unsigned int x1, unsigned int y1,
00213     unsigned int x2, unsigned int y2,
00214     std::vector<Destination> & rvdest )
00215 {
00216     assert(gVars.gpPathFinder != NULL);
00217 
00218     return gVars.gpPathFinder->findShortestPath(
00219         x1, y1, x2, y2, rvdest,
00220         PathFinder::OC_DISTANCE, MAX_PATH_LENGTH  );
00221 }
00222 
00223 
00224 /*=====================================================================*/
00225 Structure*
00226 Environment::getBuildingXY(
00227     unsigned int x,
00228     unsigned int y)
00229 {
00230     return m_pBuildingLayer->GetStructure( x, y );
00231 }
00232 
00233 
00234 /*=====================================================================*/
00235 /*                         STATIC     METHODS                          */
00236 /*=====================================================================*/
00237 unsigned int
00238 Environment::toSquareDistance(
00239     unsigned int x1, unsigned int y1,
00240     unsigned int x2, unsigned int y2)
00241 {
00242     return ((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
00243 }
00244 
00245 
00246 
00247 
00248 
00249 
00250 
00251 
00252 
00253 
00254 
00255 
00256 
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00266 
00267 
00268 
00269 
00270 

Generated on Sat Nov 11 10:21:09 2006 for OpenCity by  doxygen 1.4.2