Debian Codegraph @

src/mas/agentdemonstrator.cpp

raw symbols SCIP
1 /***************************************************************************
2 agentdemonstrator.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: agentdemonstrator.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
22 #include "agentdemonstrator.h"
23 #include "kernel.h"
24 #include "message.h"
25
26 #define MAX_TTL 15 ///< Maximum time to live
27
28 /*=====================================================================*/
29 AgentDemonstrator::AgentDemonstrator
30 (
31 Kernel& kernel,
32 Environment& env,
33 int x, int y
34 ):
35 Agent(kernel, env, x, y, ROLE_DEMONSTRATOR)
36 {
37 this->born(); // Warning: see superclass
38
39 // OpenCity's specifics
40 this->SetGraphicCode( OC_VEHICLE_PICKUP );
41 }
42
43
44 /*=====================================================================*/
45 AgentDemonstrator::~AgentDemonstrator()
46 {
47 if (m_agent_state != AGENT_DIE)
48 this->die(); // Warning: see superclass
49 }
50
51
52 /*=====================================================================*/
53 void
54 AgentDemonstrator::processMessage()
55 {
56 static Message msg;
57
58 // Process all the messages
59 while (!m_messages.empty()) {
60 msg = m_messages.front();
61 MAS_DEBUG( *this << " processes " << msg );
62
63 if (msg.getType() == MSG_AGENT_DIE)
64 m_agent_state = AGENT_DIE;
65 m_messages.pop_front();
66 }
67 }
68
69
70 /*=====================================================================*/
71 void
72 AgentDemonstrator::born()
73 {
74 m_ttl = MAX_TTL; // Set m_ttl to maximum time to live
75 processMessage();
76 Agent::born(); // This set m_agent_state to AGENT_BORN
77 m_kernel.registerRole(this, ROLE_DEMONSTRATOR);
78 m_agent_state = AGENT_LIVE;
79 }
80
81
82 /*=====================================================================*/
83 void
84 AgentDemonstrator::live()
85 {
86 // Process all the received messages
87 processMessage();
88
89 // Do we need this ?
90 // Agent::live();
91
92 if (m_agent_state != AGENT_LIVE)
93 return;
94
95 // Little sleep()
96 unsigned int m_sleep = 3;
97 if ((m_kernel.getStep() % m_sleep) != 0)
98 return;
99
100 // IF we can not move in more than ttl steps THEN die
101 if (!randomMove()) {
102 m_ttl--; // Decrease Time To Live
103 //debug cout << "Agent: " << getId() << " / TTL: " << m_ttl << endl;
104 if (!m_ttl) {
105 m_kernel.killAgent(this);
106 }
107 }
108 else {
109 m_ttl = MAX_TTL;
110 }
111 }
112
113
114 /*=====================================================================*/
115 void
116 AgentDemonstrator::die()
117 {
118 processMessage();
119 Agent::die();
120 m_kernel.unregisterRole(this, getRole());
121 }
122
123
124 /*=====================================================================*/
125 void
126 AgentDemonstrator::output(std::ostream& os) const
127 {
128 os << "AgentDemonstrator " << getId();
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162