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

agentdemonstrator.cpp

00001 /***************************************************************************
00002                     agentdemonstrator.cpp  -  description
00003                         -------------------
00004     begin                : nov 29th 2005
00005     copyright            : (C) 2005-2006 by Duong-Khang NGUYEN
00006     email                : neoneurone @ users sourceforge net
00007     author               : Victor STINNER
00008     
00009     $Id: agentdemonstrator.cpp 32 2006-08-06 18:59:25Z 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 "agentdemonstrator.h"
00023 #include "kernel.h"
00024 #include "message.h"
00025 
00026 #define MAX_TTL 15                  
00027 
00028    /*=====================================================================*/
00029 AgentDemonstrator::AgentDemonstrator(Kernel& kernel, Environment& env, int x, int y):
00030 Agent(kernel, env, x, y, ROLE_DEMONSTRATOR)
00031 {
00032     this->born();       // Warning: see superclass
00033 
00034 // OpenCity's specifics
00035     this->SetGraphicCode( OC_VEHICLE_PICKUP );
00036 }
00037 
00038 
00039    /*=====================================================================*/
00040 AgentDemonstrator::~AgentDemonstrator()
00041 {
00042     if (m_agent_state != AGENT_DIE)
00043         this->die();        // Warning: see superclass
00044 }
00045 
00046 
00047    /*=====================================================================*/
00048 void AgentDemonstrator::processMessage()
00049 {
00050     static Message msg;
00051 
00052 // Process all the messages
00053     while (!m_messages.empty()) {
00054         msg = m_messages.front();
00055         MAS_DEBUG( *this << " processes " << msg );
00056 
00057         if (msg.getType() == Message::MSG_AGENT_DIE)
00058             m_agent_state = AGENT_DIE;
00059         m_messages.pop_front();
00060     }
00061 }
00062 
00063 
00064    /*=====================================================================*/
00065 void AgentDemonstrator::born()
00066 {
00067     m_ttl = MAX_TTL;            // Set m_ttl to maximum time to live
00068     processMessage();
00069     Agent::born();              // This set m_agent_state to AGENT_BORN
00070     m_kernel.registerRole(this, ROLE_DEMONSTRATOR);
00071     m_agent_state = AGENT_LIVE;
00072 }
00073 
00074 
00075    /*=====================================================================*/
00076 void AgentDemonstrator::live()
00077 {
00078 // Process all the received messages
00079     processMessage();
00080 
00081 // Do we need this ?
00082 //  Agent::live();
00083 
00084     if (m_agent_state != AGENT_LIVE)
00085         return;
00086 
00087     // Little sleep()
00088     unsigned int m_sleep = 3;
00089     if ((m_kernel.getStep() % m_sleep) != 0)
00090         return;
00091 
00092 // IF we can not move in more than ttl steps THEN die
00093     if (!randomMove()) {
00094         m_ttl--;        // Decrease Time To Live
00095 //debug cout << "Agent: " << getId() << " / TTL: " << m_ttl << endl;
00096         if (!m_ttl) {
00097             m_kernel.killAgent(this);
00098         }
00099     }
00100     else {
00101         m_ttl = MAX_TTL;
00102     }
00103 }
00104 
00105 
00106    /*=====================================================================*/
00107 void AgentDemonstrator::die()
00108 {
00109     processMessage();
00110     Agent::die();
00111     m_kernel.unregisterRole(this, getRole());
00112 }
00113 
00114 
00115    /*=====================================================================*/
00116 void AgentDemonstrator::output(std::ostream& os) const
00117 {
00118     os << "AgentDemonstrator " << getId();
00119 }
00120 
00121 
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 
00130 
00131 
00132 
00133 
00134 
00135 
00136 
00137 
00138 
00139 
00140 
00141 
00142 
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 

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