| 1 | /*************************************************************************** |
| 2 | agentpolice.cpp - description |
| 3 | ------------------- |
| 4 | begin : nov 29th 2005 |
| 5 | copyright : (C) 2005-2008 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | author : Victor STINNER |
| 8 | |
| 9 | $Id: agentpolice.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 enumeration |
| 22 | #include "opencity_direction.h" |
| 23 | |
| 24 | // MAS headers |
| 25 | #include "agentpolice.h" |
| 26 | #include "kernel.h" |
| 27 | #include "environment.h" |
| 28 | #include "message.h" |
| 29 | |
| 30 | // OpenCity header |
| 31 | #include "destination.h" |
| 32 | |
| 33 | // Standard header |
| 34 | #include <cstdlib> |
| 35 | |
| 36 | // The max square distance for collaboration |
| 37 | #define MAX_DISTANCE 200 |
| 38 | #define MAX_PERCEPTION_RANGE 10 |
| 39 | |
| 40 | |
| 41 | /*=====================================================================*/ |
| 42 | AgentPolice::AgentPolice |
| 43 | ( |
| 44 | Kernel& kernel, |
| 45 | Environment& env, |
| 46 | int x, int y |
| 47 | ): |
| 48 | Agent(kernel, env, x, y, ROLE_POLICE), |
| 49 | m_state( POLICE_LOOK ) |
| 50 | { |
| 51 | m_sleep = 3; |
| 52 | SetState( POLICE_LOOK ); |
| 53 | |
| 54 | this->born(); // Warning: see superclass |
| 55 | |
| 56 | // OpenCity's specifics |
| 57 | this->SetGraphicCode( OC_VEHICLE_POLICE ); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /*=====================================================================*/ |
| 62 | AgentPolice::~AgentPolice() |
| 63 | { |
| 64 | if (m_agent_state != AGENT_DIE) |
| 65 | this->die(); // Warning: see superclass |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /*=====================================================================*/ |
| 70 | Agent* |
| 71 | AgentPolice::SeeBadGuy( MAS_DIRECTION dir ) |
| 72 | { |
| 73 | Agent* agent = lookForAgent(dir, MAX_PERCEPTION_RANGE); |
| 74 | if (agent == NULL) |
| 75 | return agent; |
| 76 | if (agent->getRole() == ROLE_POLICE) |
| 77 | return NULL; |
| 78 | return agent; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /*=====================================================================*/ |
| 83 | void AgentPolice::SetState(police_state_t state) |
| 84 | { |
| 85 | m_index = 0; |
| 86 | m_path.clear(); |
| 87 | |
| 88 | if (state == m_state) return; |
| 89 | |
| 90 | m_state = state; |
| 91 | switch(m_state) |
| 92 | { |
| 93 | case POLICE_COLLABORATE: |
| 94 | break; |
| 95 | case POLICE_PURSUE: |
| 96 | m_pursue = 0; |
| 97 | break; |
| 98 | case POLICE_LOST_PURSUE: |
| 99 | m_lost_pursue = 0; |
| 100 | break; |
| 101 | default: |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /*=====================================================================*/ |
| 108 | void |
| 109 | AgentPolice::Look() |
| 110 | { |
| 111 | MAS_DEBUG( "(look)" ); |
| 112 | |
| 113 | Agent* agent = FindBadGuy(); |
| 114 | if (agent == NULL) { |
| 115 | randomMove(); |
| 116 | return; |
| 117 | } |
| 118 | MAS_DEBUG( "Look -> Pursue agent " << agent->getId() ); |
| 119 | |
| 120 | moveDirection(); |
| 121 | NoticePursue(agent); |
| 122 | SetState(POLICE_PURSUE); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /*=====================================================================*/ |
| 127 | void |
| 128 | AgentPolice::NoticePursue(Agent *agent) |
| 129 | { |
| 130 | // Create a new alert message for other police agents |
| 131 | MAS_MESSAGE_TYPE msgt = MSG_UNDEFINED; |
| 132 | switch (agent->getRole()) { |
| 133 | case ROLE_DEMONSTRATOR: |
| 134 | msgt = MSG_NEW_DEMONSTRATOR; |
| 135 | break; |
| 136 | |
| 137 | case ROLE_ROBBER: |
| 138 | msgt = MSG_NEW_ROBBER; |
| 139 | break; |
| 140 | |
| 141 | default: // Keep GCC happy |
| 142 | break; |
| 143 | } |
| 144 | Message msg = Message(msgt, this) << agent->getX() << agent->getY(); |
| 145 | m_kernel.sendMessage( ROLE_POLICE, msg ); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | /*=====================================================================*/ |
| 151 | Agent* |
| 152 | AgentPolice::IsNearBadGuy() |
| 153 | { |
| 154 | Agent* agent = m_environment.getAgentAt(m_x-1, m_y); |
| 155 | |
| 156 | if (agent != NULL) |
| 157 | return agent; |
| 158 | |
| 159 | agent = m_environment.getAgentAt(m_x+1, m_y); |
| 160 | if (agent != NULL) |
| 161 | return agent; |
| 162 | |
| 163 | agent = m_environment.getAgentAt(m_x, m_y-1); |
| 164 | if (agent != NULL) |
| 165 | return agent; |
| 166 | |
| 167 | agent = m_environment.getAgentAt(m_x, m_y+1); |
| 168 | if (agent != NULL) |
| 169 | return agent; |
| 170 | |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | /*=====================================================================*/ |
| 175 | void AgentPolice::Pursue() |
| 176 | { |
| 177 | moveDirection(); |
| 178 | Agent* agent = SeeBadGuy(m_direction); |
| 179 | |
| 180 | if (agent != NULL) { |
| 181 | MAS_DEBUG( *this << "(is pursuing agent " << agent->getId() << ")" ); |
| 182 | m_lost_pursue = 0; |
| 183 | |
| 184 | // Notice pursue if needed |
| 185 | m_pursue++; |
| 186 | if (1<m_pursue and (m_pursue % 5) == 0) |
| 187 | { |
| 188 | NoticePursue(agent); |
| 189 | } |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | agent = FindBadGuy(false); |
| 194 | if (agent == NULL) { |
| 195 | MAS_DEBUG( "Pursue -> Lost pursue" ); |
| 196 | |
| 197 | SetState(POLICE_LOST_PURSUE); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /*=====================================================================*/ |
| 203 | Agent* |
| 204 | AgentPolice::FindBadGuy(bool test_backward) |
| 205 | { |
| 206 | Agent* agent = SeeBadGuy(m_direction); |
| 207 | if (agent != NULL) |
| 208 | return agent; |
| 209 | |
| 210 | MAS_DIRECTION dir; |
| 211 | unsigned char rotate = randomBool(); |
| 212 | if (rotate) |
| 213 | dir = rotateLeft(m_direction); |
| 214 | else |
| 215 | dir = rotateRight(m_direction); |
| 216 | agent = SeeBadGuy(dir); |
| 217 | |
| 218 | if (agent != NULL) { |
| 219 | m_direction = dir; |
| 220 | return agent; |
| 221 | } |
| 222 | |
| 223 | if (rotate) |
| 224 | dir = rotateRight(m_direction); |
| 225 | else |
| 226 | dir = rotateLeft(m_direction); |
| 227 | agent = SeeBadGuy(dir); |
| 228 | |
| 229 | if (agent != NULL) { |
| 230 | m_direction = dir; |
| 231 | return agent; |
| 232 | } |
| 233 | |
| 234 | if (test_backward) { |
| 235 | dir = rotate180(m_direction); |
| 236 | agent = SeeBadGuy(dir); |
| 237 | if (agent != NULL) { |
| 238 | m_direction = dir; |
| 239 | return agent; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /*=====================================================================*/ |
| 248 | void AgentPolice::LostPursue() |
| 249 | { |
| 250 | MAS_DEBUG( "(did lost agent)" ); |
| 251 | |
| 252 | Agent* agent = FindBadGuy(); |
| 253 | if (agent != NULL) { |
| 254 | MAS_DEBUG( "Lost pursue -> Pursue " << agent->getId() ); |
| 255 | |
| 256 | moveDirection(); |
| 257 | NoticePursue(agent); |
| 258 | SetState(POLICE_PURSUE); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | m_lost_pursue++; |
| 263 | if (7 < m_lost_pursue) { |
| 264 | MAS_DEBUG( "Lost pursue -> Look" ); |
| 265 | |
| 266 | SetState(POLICE_LOOK); |
| 267 | } |
| 268 | randomMove(30); |
| 269 | |
| 270 | agent = FindBadGuy(); |
| 271 | if (agent != NULL) { |
| 272 | MAS_DEBUG( "Lost pursue -> Pursue (after moving)" << agent->getId() ); |
| 273 | |
| 274 | NoticePursue(agent); |
| 275 | SetState(POLICE_PURSUE); |
| 276 | return; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /*=====================================================================*/ |
| 282 | void |
| 283 | AgentPolice::processMessage() |
| 284 | { |
| 285 | static Message msg; |
| 286 | |
| 287 | // Process all the messages |
| 288 | while (!m_messages.empty()) { |
| 289 | msg = m_messages.front(); |
| 290 | |
| 291 | switch (msg.getType()) { |
| 292 | case MSG_NEW_DEMONSTRATOR: |
| 293 | // IF this is not our own message THEN |
| 294 | if ((msg.getSender() != this) and (m_state == POLICE_LOST_PURSUE || m_state == POLICE_LOOK)) { |
| 295 | // Check the square distance of the target |
| 296 | unsigned int ax = msg[0].getUInt(); |
| 297 | unsigned int ay = msg[1].getUInt(); |
| 298 | |
| 299 | if (Environment::toSquareDistance(m_x, m_y, ax, ay) > MAX_DISTANCE) |
| 300 | break; |
| 301 | |
| 302 | m_environment.findShortestPath(m_x, m_y, ax, ay, m_path); |
| 303 | if (m_path.size() > 1) { |
| 304 | MAS_DEBUG( "-> Collaborate (path size=" << m_path.size() << ")" ); |
| 305 | |
| 306 | // Avoid the first x, y which are equal to m_x, m_y |
| 307 | m_index = 1; |
| 308 | SetState(POLICE_COLLABORATE); |
| 309 | } |
| 310 | else { |
| 311 | m_path.clear(); |
| 312 | m_index = 0; |
| 313 | |
| 314 | Agent* agent = FindBadGuy(); |
| 315 | if (agent == NULL) |
| 316 | break; |
| 317 | |
| 318 | MAS_DEBUG( "(collaborate) Pursue agent " << agent->getId() ); |
| 319 | NoticePursue(agent); |
| 320 | SetState(POLICE_PURSUE); |
| 321 | } |
| 322 | } |
| 323 | break; |
| 324 | |
| 325 | case MSG_AGENT_DIE: |
| 326 | m_agent_state = AGENT_DIE; |
| 327 | break; |
| 328 | |
| 329 | default: |
| 330 | MAS_DEBUG( "Received unknown message" ); |
| 331 | } |
| 332 | |
| 333 | m_messages.pop_front(); |
| 334 | } // while |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /*=====================================================================*/ |
| 339 | void |
| 340 | AgentPolice::born() |
| 341 | { |
| 342 | Agent::born(); |
| 343 | m_kernel.registerRole(this, ROLE_POLICE); |
| 344 | m_agent_state = AGENT_LIVE; |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /*=====================================================================*/ |
| 349 | void |
| 350 | AgentPolice::Collaborate() |
| 351 | { |
| 352 | Agent* agent = IsNearBadGuy(); |
| 353 | |
| 354 | if (agent != NULL) { |
| 355 | // NoticePursue(agent); |
| 356 | randomMove(-1); |
| 357 | SetState(POLICE_PURSUE); |
| 358 | return; |
| 359 | } |
| 360 | MAS_DEBUG( "(collaborate)" ); |
| 361 | |
| 362 | followPath(); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | /*=====================================================================*/ |
| 367 | void |
| 368 | AgentPolice::live() |
| 369 | { |
| 370 | if (m_agent_state != AGENT_LIVE) |
| 371 | return; |
| 372 | |
| 373 | if (m_sleep != 0 and (m_kernel.getStep() % m_sleep) != 0) |
| 374 | return; |
| 375 | |
| 376 | processMessage(); |
| 377 | |
| 378 | switch (m_state) { |
| 379 | case POLICE_LOOK: |
| 380 | Look(); |
| 381 | break; |
| 382 | |
| 383 | case POLICE_PURSUE: |
| 384 | Pursue(); |
| 385 | break; |
| 386 | |
| 387 | case POLICE_LOST_PURSUE: |
| 388 | LostPursue(); |
| 389 | break; |
| 390 | |
| 391 | case POLICE_COLLABORATE: |
| 392 | Collaborate(); |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | |
| 398 | /*=====================================================================*/ |
| 399 | void |
| 400 | AgentPolice::die() |
| 401 | { |
| 402 | processMessage(); |
| 403 | Agent::die(); |
| 404 | m_kernel.unregisterRole(this, this->getRole()); |
| 405 | } |
| 406 | |
| 407 | |
| 408 | /*=====================================================================*/ |
| 409 | void |
| 410 | AgentPolice::output (std::ostream& os) const |
| 411 | { |
| 412 | os << "AgentPolice " << getId(); |
| 413 | } |
| 414 | |
| 415 | |
| 416 | /*=====================================================================*/ |
| 417 | /* PRIVATE METHODS */ |
| 418 | /*=====================================================================*/ |
| 419 | void |
| 420 | AgentPolice::followPath() |
| 421 | { |
| 422 | int x, y; |
| 423 | bool ok = false; |
| 424 | |
| 425 | // move to the indicated direction |
| 426 | if (m_path[m_index]._uiTime) { |
| 427 | x = m_path[m_index]._uiW; |
| 428 | y = m_path[m_index]._uiL; |
| 429 | if (canMove(x, y) and move(x, y)) { |
| 430 | ok = true; |
| 431 | } |
| 432 | else { |
| 433 | m_index = 0; |
| 434 | m_path.clear(); |
| 435 | |
| 436 | Agent* agent = m_environment.getAgentAt(x,y); |
| 437 | if (agent != NULL and agent->getRole() != ROLE_POLICE) { |
| 438 | MAS_DEBUG( "Collaborate -> Pursue (blocked)" << agent->getId() ); |
| 439 | |
| 440 | NoticePursue(agent); |
| 441 | SetState (POLICE_PURSUE); |
| 442 | return; |
| 443 | } |
| 444 | else { |
| 445 | randomMove(); |
| 446 | MAS_DEBUG( "Collaborate -> Look (abort)" << agent->getId() ); |
| 447 | |
| 448 | SetState (POLICE_LOOK); |
| 449 | return; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | if (--m_path[m_index]._uiTime == 0) { |
| 455 | if (++m_index >= m_path.size()) { |
| 456 | Agent* agent = SeeBadGuy(m_direction); |
| 457 | if (agent != NULL) { |
| 458 | MAS_DEBUG( "Collaborate -> Pursue (path ended)" << agent->getId() ); |
| 459 | |
| 460 | NoticePursue(agent); |
| 461 | SetState (POLICE_PURSUE); |
| 462 | } |
| 463 | else { |
| 464 | MAS_DEBUG( "Collaborate -> Look (path ended)" << agent->getId() ); |
| 465 | |
| 466 | SetState (POLICE_LOOK); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | |
| 473 | /*=====================================================================*/ |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 |