| 1 | /*************************************************************************** |
| 2 | agent.cpp - description |
| 3 | ------------------- |
| 4 | begin : nov 29th 2005 |
| 5 | copyright : (C) 2005-2006 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | author : Victor STINNER |
| 8 | |
| 9 | $Id: agent.cpp 375 2008-10-28 14:47:15Z 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 enumerations |
| 22 | #include "opencity_structure_type.h" |
| 23 | |
| 24 | // MAS headers |
| 25 | #include "agent.h" |
| 26 | #include "kernel.h" |
| 27 | #include "environment.h" |
| 28 | #include "message.h" |
| 29 | |
| 30 | // OpenCity headers |
| 31 | #include "structure.h" ///< OpenCity's specifics |
| 32 | |
| 33 | // Standard headers |
| 34 | #include <cassert> |
| 35 | |
| 36 | |
| 37 | /*=====================================================================*/ |
| 38 | Agent::Agent(Kernel& kernel, Environment &env, int x, int y, MAS_ROLE role): |
| 39 | m_kernel(kernel), |
| 40 | m_environment(env), |
| 41 | m_x(x), |
| 42 | m_y(y), |
| 43 | m_role(role), |
| 44 | m_enumGC( OC_EMPTY ), // OpenCity's specifics |
| 45 | m_id(kernel.getAgentUniqId()) |
| 46 | { |
| 47 | MAS_DEBUG( "ctor " << *this ); |
| 48 | |
| 49 | m_move_speed = 1; |
| 50 | m_direction = (MAS_DIRECTION)randomInt(0, 3); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | /*=====================================================================*/ |
| 55 | Agent::~Agent() |
| 56 | { |
| 57 | MAS_DEBUG( "dtor " << *this ); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /*=====================================================================*/ |
| 62 | MAS_ROLE Agent::getRole() const |
| 63 | { |
| 64 | return m_role; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /*=====================================================================*/ |
| 69 | void Agent::receiveMessage(const Message& msg) |
| 70 | { |
| 71 | MAS_DEBUG( *this << " receive " << msg ); |
| 72 | |
| 73 | m_messages.push_back(msg); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /*=====================================================================*/ |
| 78 | void Agent::processMessage() |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /*=====================================================================*/ |
| 84 | void Agent::sendMessage(MAS_ROLE role, const Message& msg) |
| 85 | { |
| 86 | m_kernel.sendMessage(role, msg); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /*=====================================================================*/ |
| 91 | void Agent::sendMessageToAgent(AgentID_t agent, const Message& msg) |
| 92 | { |
| 93 | m_kernel.sendMessageToAgent(agent, msg); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /*=====================================================================*/ |
| 98 | Agent* Agent::lookForAgent(MAS_DIRECTION dir, unsigned long max_distance) |
| 99 | { |
| 100 | int x = m_x, y = m_y; |
| 101 | int dx=0, dy=0; |
| 102 | switch (dir) |
| 103 | { |
| 104 | case NORTH: dy = 1; break; |
| 105 | case SOUTH: dy = -1; break; |
| 106 | case EAST: dx = 1; break; |
| 107 | case WEST: dx = -1; break; |
| 108 | } |
| 109 | |
| 110 | uint step; |
| 111 | for (step=1; step<=max_distance; step++) |
| 112 | { |
| 113 | x += dx; |
| 114 | y += dy; |
| 115 | if (x < 0 || y < 0 || (int)m_environment.getWidth() <= x || (int)m_environment.getHeight() <= y) |
| 116 | { |
| 117 | break; |
| 118 | } |
| 119 | if (m_environment.hasAgentAt(this, x, y)) |
| 120 | { |
| 121 | return m_environment.getAgentAt(x,y); |
| 122 | } |
| 123 | } |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /*=====================================================================*/ |
| 129 | bool Agent::randomMove(int turn_percent) |
| 130 | { |
| 131 | bool ok = doRandomMove(turn_percent); |
| 132 | if (ok) |
| 133 | { |
| 134 | ok = moveDirection(); |
| 135 | // debug |
| 136 | // std::cerr << "Agent " << getId() << ", move to direction " << m_direction |
| 137 | // << " at x: " << x << " / y: " << y << std::endl; |
| 138 | } |
| 139 | return ok; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /*=====================================================================*/ |
| 144 | bool Agent::doRandomMove(int turn_percent) |
| 145 | { |
| 146 | bool ok = canMove(m_direction); |
| 147 | if (ok) { |
| 148 | if (turn_percent < randomInt(0,99)) |
| 149 | return true; |
| 150 | |
| 151 | // Try left/right |
| 152 | MAS_DIRECTION dir; |
| 153 | unsigned char rotate = randomBool(); |
| 154 | if (rotate) |
| 155 | dir = rotateLeft(m_direction); |
| 156 | else |
| 157 | dir = rotateRight(m_direction); |
| 158 | if (canMove(dir)) |
| 159 | { |
| 160 | m_direction = dir; |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | // Try right/left (the opposite) |
| 165 | if (rotate) |
| 166 | dir = rotateRight(m_direction); |
| 167 | else |
| 168 | dir = rotateLeft(m_direction); |
| 169 | if (canMove(dir)) |
| 170 | m_direction = dir; |
| 171 | return true; |
| 172 | } |
| 173 | else { |
| 174 | // Try left/right |
| 175 | unsigned char rotate = randomBool(); |
| 176 | MAS_DIRECTION dir; |
| 177 | if (rotate) |
| 178 | dir = rotateLeft(m_direction); |
| 179 | else |
| 180 | dir = rotateRight(m_direction); |
| 181 | if (canMove(dir)) |
| 182 | { |
| 183 | m_direction = dir; |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | // Try right/left (the opposite) |
| 188 | if (rotate) |
| 189 | dir = rotateRight(m_direction); |
| 190 | else |
| 191 | dir = rotateLeft(m_direction); |
| 192 | if (canMove(dir)) |
| 193 | { |
| 194 | m_direction = dir; |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | // try turn backward |
| 199 | if (rotate) |
| 200 | m_direction = rotate180(m_direction); |
| 201 | else |
| 202 | m_direction = rotate180(m_direction); |
| 203 | return canMove(m_direction); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /*=====================================================================*/ |
| 209 | void Agent::born() |
| 210 | { |
| 211 | m_agent_state = AGENT_BORN; |
| 212 | m_kernel.registerAgent(this); |
| 213 | m_environment.registerAgent(this, m_x, m_y); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | /*=====================================================================*/ |
| 218 | void Agent::live() |
| 219 | { |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /*=====================================================================*/ |
| 224 | void Agent::die() |
| 225 | { |
| 226 | m_agent_state = AGENT_DIE; |
| 227 | m_kernel.unregisterAgent(this); |
| 228 | m_environment.unregisterAgent(this); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | /*=====================================================================*/ |
| 233 | AgentID_t Agent::getId() const |
| 234 | { |
| 235 | return m_id; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /*=====================================================================*/ |
| 240 | unsigned int Agent::getX() const |
| 241 | { |
| 242 | return m_x; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | /*=====================================================================*/ |
| 247 | unsigned int Agent::getY() const |
| 248 | { |
| 249 | return m_y; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | /*=====================================================================*/ |
| 254 | void Agent::output (std::ostream& os) const |
| 255 | { |
| 256 | os << "Agent " << m_id; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | /*=====================================================================*/ |
| 261 | std::ostream& operator<< (std::ostream& os, const Agent &agent) |
| 262 | { |
| 263 | agent.output(os); |
| 264 | return os; |
| 265 | } |
| 266 | |
| 267 | |
| 268 | /*=====================================================================*/ |
| 269 | const OPENCITY_GRAPHIC_CODE |
| 270 | Agent::GetGraphicCode() const |
| 271 | { |
| 272 | return m_enumGC; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | /*=====================================================================*/ |
| 277 | bool Agent::move(int x, int y) |
| 278 | { |
| 279 | assert (0 <= x && 0 <= y && x < (int)m_environment.getWidth() && y < (int)m_environment.getHeight()); |
| 280 | if (m_environment.moveAgent(this, x, y)) |
| 281 | { |
| 282 | m_x = static_cast<unsigned int>(x); |
| 283 | m_y = static_cast<unsigned int>(y); |
| 284 | return true; |
| 285 | } else { |
| 286 | return false; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | |
| 291 | /*=====================================================================*/ |
| 292 | bool |
| 293 | Agent::moveDirection() |
| 294 | { |
| 295 | int x, y; |
| 296 | return (canMove(m_direction, x, y) and move(x,y)); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | /*=====================================================================*/ |
| 301 | bool |
| 302 | Agent::canMove(int x, int y) const |
| 303 | { |
| 304 | if ( x < 0 |
| 305 | or y < 0 |
| 306 | or (int)m_environment.getWidth() <= x |
| 307 | or(int)m_environment.getHeight() <= y ) |
| 308 | return false; |
| 309 | |
| 310 | Structure* building = m_environment.getBuildingXY(x, y); |
| 311 | if (building == NULL) |
| 312 | return false; |
| 313 | |
| 314 | bool ok = building->GetCode() == OC_STRUCTURE_ROAD; |
| 315 | if (ok) { |
| 316 | ok = !m_environment.hasAgentAt(this, x, y); |
| 317 | } |
| 318 | return ok; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /*=====================================================================*/ |
| 323 | bool |
| 324 | Agent::canMove(MAS_DIRECTION dir) const |
| 325 | { |
| 326 | int x, y; |
| 327 | return canMove(dir, x, y); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | /*=====================================================================*/ |
| 332 | bool |
| 333 | Agent::canMove(MAS_DIRECTION dir, int &x, int &y) const |
| 334 | { |
| 335 | x = static_cast<int>(m_x); |
| 336 | y = static_cast<int>(m_y); |
| 337 | switch (dir) { |
| 338 | case NORTH: y += m_move_speed; break; |
| 339 | case SOUTH: y -= m_move_speed; break; |
| 340 | case EAST: x += m_move_speed; break; |
| 341 | case WEST: x -= m_move_speed; break; |
| 342 | } |
| 343 | return canMove(x, y); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /*=====================================================================*/ |
| 348 | void |
| 349 | Agent::SetGraphicCode( const OPENCITY_GRAPHIC_CODE gc ) |
| 350 | { |
| 351 | m_enumGC = gc; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 |