00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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();
00033
00034
00035 this->SetGraphicCode( OC_VEHICLE_PICKUP );
00036 }
00037
00038
00039
00040 AgentDemonstrator::~AgentDemonstrator()
00041 {
00042 if (m_agent_state != AGENT_DIE)
00043 this->die();
00044 }
00045
00046
00047
00048 void AgentDemonstrator::processMessage()
00049 {
00050 static Message msg;
00051
00052
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;
00068 processMessage();
00069 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
00079 processMessage();
00080
00081
00082
00083
00084 if (m_agent_state != AGENT_LIVE)
00085 return;
00086
00087
00088 unsigned int m_sleep = 3;
00089 if ((m_kernel.getStep() % m_sleep) != 0)
00090 return;
00091
00092
00093 if (!randomMove()) {
00094 m_ttl--;
00095
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