Panda3D
aiWorld.cxx
1 ////////////////////////////////////////////////////////////////////////
2 // Filename : aiWorld.cxx
3 // Created by : Deepak, John, Navin
4 // Date : 8 Sep 09
5 ////////////////////////////////////////////////////////////////////
6 //
7 // PANDA 3D SOFTWARE
8 // Copyright (c) Carnegie Mellon University. All rights reserved.
9 //
10 // All use of this software is subject to the terms of the revised BSD
11 // license. You should have received a copy of this license along
12 // with this source code in a file named "LICENSE."
13 //
14 ////////////////////////////////////////////////////////////////////
15 
16 #include "aiWorld.h"
17 
18 AIWorld::AIWorld(NodePath render) {
19  _ai_char_pool = new AICharPool();
20  _render = render;
21 }
22 
23 AIWorld::~AIWorld() {
24 }
25 
26 void AIWorld::add_ai_char(AICharacter *ai_char) {
27  _ai_char_pool->append(ai_char);
28  ai_char->_window_render = _render;
29  ai_char->_world = this;
30 }
31 
32 void AIWorld::remove_ai_char(string name) {
33  _ai_char_pool->del(name);
34  remove_ai_char_from_flock(name);
35 }
36 
37 void AIWorld::remove_ai_char_from_flock(string name) {
38  AICharPool::node *ai_pool;
39  ai_pool = _ai_char_pool->_head;
40  while((ai_pool) != NULL) {
41  for(unsigned int i = 0; i < _flock_pool.size(); ++i) {
42  if(ai_pool->_ai_char->_ai_char_flock_id == _flock_pool[i]->get_id()) {
43  for(unsigned int j = 0; j<_flock_pool[i]->_ai_char_list.size(); ++j) {
44  if(_flock_pool[i]->_ai_char_list[j]->_name == name) {
45  _flock_pool[i]->_ai_char_list.erase(_flock_pool[i]->_ai_char_list.begin() + j);
46  return;
47  }
48  }
49  }
50  }
51  ai_pool = ai_pool->_next;
52  }
53 }
54 
55 void AIWorld::print_list() {
56  _ai_char_pool->print_list();
57 }
58 
59 ////////////////////////////////////////////////////////////////////////
60 // Function : update
61 // Description : The AIWorld update function calls the update function of all the
62 // AI characters which have been added to the AIWorld.
63 
64 /////////////////////////////////////////////////////////////////////////////////
65 
67  AICharPool::node *ai_pool;
68  ai_pool = _ai_char_pool->_head;
69 
70  while((ai_pool)!=NULL) {
71  ai_pool->_ai_char->update();
72  ai_pool = ai_pool->_next;
73  }
74 }
75 
76 /////////////////////////////////////////////////////////////////////////////////
77 //
78 // Function : add_flock
79 // Description : This function adds all the AI characters in the Flock object to
80 // the AICharPool. This function allows adding the AI characetrs as
81 // part of a flock.
82 
83 /////////////////////////////////////////////////////////////////////////////////
84 
85 void AIWorld::add_flock(Flock *flock) {
86  // Add all the ai_characters in the flock to the AIWorld.
87  for(unsigned int i = 0; i < flock->_ai_char_list.size(); ++i) {
88  this->add_ai_char(flock->_ai_char_list[i]);
89  }
90  // Add the flock to the flock pool.
91  _flock_pool.push_back(flock);
92 }
93 
94 /////////////////////////////////////////////////////////////////////////////////
95 //
96 // Function : get_flock
97 // Description : This function returns a handle to the Flock whose id is passed.
98 
99 /////////////////////////////////////////////////////////////////////////////////
100 
101 Flock AIWorld::get_flock(unsigned int flock_id) {
102  for(unsigned int i=0; i < _flock_pool.size(); ++i) {
103  if(_flock_pool[i]->get_id() == flock_id) {
104  return *_flock_pool[i];
105  }
106  }
107  Flock *null_flock = NULL;
108  return *null_flock;
109 }
110 
111 /////////////////////////////////////////////////////////////////////////////////
112 //
113 // Function : remove_flock
114 // Description : This function removes the flock behavior completely.
115 
116 /////////////////////////////////////////////////////////////////////////////////
117 
118 void AIWorld::remove_flock(unsigned int flock_id) {
119  for(unsigned int i = 0; i < _flock_pool.size(); ++i) {
120  if(_flock_pool[i]->get_id() == flock_id) {
121  for(unsigned int j = 0; j < _flock_pool[i]->_ai_char_list.size(); ++j) {
122  _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock_activate");
123  _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock");
124  _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->_flock_group = NULL;
125  }
126  _flock_pool.erase(_flock_pool.begin() + i);
127  break;
128  }
129  }
130 }
131 
132 /////////////////////////////////////////////////////////////////////////////////
133 //
134 // Function : flock_off
135 // Description : This function turns off the flock behavior temporarily. Similar to
136 // pausing the behavior.
137 
138 /////////////////////////////////////////////////////////////////////////////////
139 
140 void AIWorld::flock_off(unsigned int flock_id) {
141  for(unsigned int i = 0; i < _flock_pool.size(); ++i) {
142  if(_flock_pool[i]->get_id() == flock_id) {
143  for(unsigned int j = 0; j < _flock_pool[i]->_ai_char_list.size(); ++j) {
144  _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock_activate");
145  _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock");
146  }
147  break;
148  }
149  }
150 }
151 
152 /////////////////////////////////////////////////////////////////////////////////
153 //
154 // Function : flock_on
155 // Description : This function turns on the flock behavior.
156 
157 /////////////////////////////////////////////////////////////////////////////////
158 
159 void AIWorld::flock_on(unsigned int flock_id) {
160  for(unsigned int i = 0; i < _flock_pool.size(); ++i) {
161  if(_flock_pool[i]->get_id() == flock_id) {
162  for(unsigned int j = 0; j < _flock_pool[i]->_ai_char_list.size(); ++j) {
163  _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_on("flock_activate");
164  }
165  break;
166  }
167  }
168 }
169 
170 AICharPool::AICharPool() {
171  _head = NULL;
172 }
173 
174 AICharPool::~AICharPool() {
175 }
176 
177 void AICharPool::append(AICharacter *ai_ch) {
178  node *q;
179  node *t;
180 
181  if(_head == NULL) {
182  q = new node();
183  q->_ai_char = ai_ch;
184  q->_next = NULL;
185  _head = q;
186  }
187  else {
188  q = _head;
189  while( q->_next != NULL) {
190  q = q->_next;
191  }
192 
193  t = new node();
194  t->_ai_char = ai_ch;
195  t->_next = NULL;
196  q->_next = t;
197  }
198 }
199 
200 void AICharPool::del(string name) {
201  node *q;
202  node *r;
203  q = _head;
204 
205  if(_head==NULL) {
206  return;
207  }
208 
209  // Only one node in the linked list
210  if(q->_next == NULL) {
211  if(q->_ai_char->_name == name) {
212  _head = NULL;
213  delete q;
214  }
215  return;
216  }
217 
218  r = q;
219  while( q != NULL) {
220  if( q->_ai_char->_name == name) {
221  // Special case
222  if(q == _head) {
223  _head = q->_next;
224  delete q;
225  return;
226  }
227 
228  r->_next = q->_next;
229  delete q;
230  return;
231  }
232  r = q;
233  q = q->_next;
234  }
235 }
236 
237 /////////////////////////////////////////////////////////////////////////////////
238 //
239 // Function : print_list
240 // Description : This function prints the ai characters in the AICharPool. Used for
241 // debugging purposes.
242 
243 /////////////////////////////////////////////////////////////////////////////////
244 
246  node* q;
247  q = _head;
248  while(q != NULL) {
249  cout<<q->_ai_char->_name<<endl;
250  q = q->_next;
251  }
252 }
253 
254 /////////////////////////////////////////////////////////////////////////////////
255 //
256 // Function : add_obstacle
257 // Description : This function adds the nodepath as an obstacle that is needed
258 // by the obstacle avoidance behavior.
259 
260 /////////////////////////////////////////////////////////////////////////////////
261 
263  _obstacles.push_back(obstacle);
264 }
265 
266 /////////////////////////////////////////////////////////////////////////////////
267 //
268 // Function : remove_obstacle
269 // Description : This function removes the nodepath from the obstacles list that is needed
270 // by the obstacle avoidance behavior.
271 
272 /////////////////////////////////////////////////////////////////////////////////
273 
275  for(unsigned int i = 0; i <= _obstacles.size(); ++i) {
276  if(_obstacles[i] == obstacle) {
277  _obstacles.erase(_obstacles.begin() + i);
278  }
279  }
280 }
void flock_on(unsigned int flock_id)
This function turns on the flock behavior.
Definition: aiWorld.cxx:159
void remove_flock(unsigned int flock_id)
This function removes the flock behavior completely.
Definition: aiWorld.cxx:118
void add_obstacle(NodePath obstacle)
This function adds the nodepath as an obstacle that is needed by the obstacle avoidance behavior...
Definition: aiWorld.cxx:262
void update()
The AIWorld update function calls the update function of all the AI characters which have been added ...
Definition: aiWorld.cxx:66
void add_flock(Flock *flock)
This function adds all the AI characters in the Flock object to the AICharPool.
Definition: aiWorld.cxx:85
This class is used to define the flock attributes and the AI characters which are part of the flock...
Definition: flock.h:32
void print_list()
This function prints the ai characters in the AICharPool.
Definition: aiWorld.cxx:245
void flock_off(unsigned int flock_id)
This function turns off the flock behavior temporarily.
Definition: aiWorld.cxx:140
void remove_obstacle(NodePath obstacle)
This function removes the nodepath from the obstacles list that is needed by the obstacle avoidance b...
Definition: aiWorld.cxx:274
This class implements a linked list of AI Characters allowing the user to add and delete characters f...
Definition: aiWorld.h:41
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
void update()
Each character&#39;s update will update its ai and physics based on his resultant steering force...
Definition: aiCharacter.cxx:47
Flock get_flock(unsigned int flock_id)
This function returns a handle to the Flock whose id is passed.
Definition: aiWorld.cxx:101