Panda3D
aiWorld.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file aiWorld.cxx
10  * @author Deepak, John, Navin
11  * @date 2009-09-08
12  */
13 
14 #include "aiWorld.h"
15 
16 AIWorld::AIWorld(NodePath render) {
17  _render = std::move(render);
18 }
19 
20 AIWorld::~AIWorld() {
21 }
22 
23 void AIWorld::add_ai_char(AICharacter *ai_char) {
24  _ai_char_pool.push_back(ai_char);
25  ai_char->_window_render = _render;
26  ai_char->_world = this;
27 }
28 
29 void AIWorld::remove_ai_char(std::string name) {
30  AICharPool::iterator it;
31  for (it = _ai_char_pool.begin(); it != _ai_char_pool.end(); ++it) {
32  AICharacter *ai_char = *it;
33  if (ai_char->_name == name) {
34  nassertv(ai_char->_world == this);
35  ai_char->_world = nullptr;
36  _ai_char_pool.erase(it);
37  break;
38  }
39  }
40 
41  remove_ai_char_from_flock(std::move(name));
42 }
43 
44 void AIWorld::remove_ai_char_from_flock(std::string name) {
45  for (AICharacter *ai_char : _ai_char_pool) {
46  for (Flock *flock : _flock_pool) {
47  if (ai_char->_ai_char_flock_id == flock->get_id()) {
48  for (size_t j = 0; j < flock->_ai_char_list.size(); ++j) {
49  if (flock->_ai_char_list[j]->_name == name) {
50  flock->_ai_char_list.erase(flock->_ai_char_list.begin() + j);
51  return;
52  }
53  }
54  }
55  }
56  }
57 }
58 
59 /**
60  * This function prints the names of the AI characters that have been added to
61  * the AIWorld. Useful for debugging purposes.
62  */
64  for (AICharacter *ai_char : _ai_char_pool) {
65  std::cout << ai_char->_name << std::endl;
66  }
67 }
68 
69 /**
70  * The AIWorld update function calls the update function of all the AI
71  * characters which have been added to the AIWorld.
72  */
74  for (AICharacter *ai_char : _ai_char_pool) {
75  ai_char->update();
76  }
77 }
78 
79 /**
80  * This function adds all the AI characters in the Flock object to the
81  * AICharPool. This function allows adding the AI characetrs as part of a
82  * flock.
83  */
84 void AIWorld::add_flock(Flock *flock) {
85  // Add all the ai_characters in the flock to the AIWorld.
86  for(unsigned int i = 0; i < flock->_ai_char_list.size(); ++i) {
87  this->add_ai_char(flock->_ai_char_list[i]);
88  }
89  // Add the flock to the flock pool.
90  _flock_pool.push_back(flock);
91 }
92 
93 /**
94  * This function returns a handle to the Flock whose id is passed.
95  */
96 Flock AIWorld::get_flock(unsigned int flock_id) {
97  for(unsigned int i=0; i < _flock_pool.size(); ++i) {
98  if(_flock_pool[i]->get_id() == flock_id) {
99  return *_flock_pool[i];
100  }
101  }
102  Flock *null_flock = nullptr;
103  return *null_flock;
104 }
105 
106 /**
107  * This function removes the flock behavior completely.
108  */
109 void AIWorld::remove_flock(unsigned int flock_id) {
110  for(unsigned int i = 0; i < _flock_pool.size(); ++i) {
111  if(_flock_pool[i]->get_id() == flock_id) {
112  for(unsigned int j = 0; j < _flock_pool[i]->_ai_char_list.size(); ++j) {
113  _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock_activate");
114  _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock");
115  _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->_flock_group = nullptr;
116  }
117  _flock_pool.erase(_flock_pool.begin() + i);
118  break;
119  }
120  }
121 }
122 
123 /**
124  * This function turns off the flock behavior temporarily. Similar to pausing
125  * the behavior.
126  */
127 void AIWorld::flock_off(unsigned int flock_id) {
128  for(unsigned int i = 0; i < _flock_pool.size(); ++i) {
129  if(_flock_pool[i]->get_id() == flock_id) {
130  for(unsigned int j = 0; j < _flock_pool[i]->_ai_char_list.size(); ++j) {
131  _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock_activate");
132  _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock");
133  }
134  break;
135  }
136  }
137 }
138 
139 /**
140  * This function turns on the flock behavior.
141  */
142 void AIWorld::flock_on(unsigned int flock_id) {
143  for(unsigned int i = 0; i < _flock_pool.size(); ++i) {
144  if(_flock_pool[i]->get_id() == flock_id) {
145  for(unsigned int j = 0; j < _flock_pool[i]->_ai_char_list.size(); ++j) {
146  _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_on("flock_activate");
147  }
148  break;
149  }
150  }
151 }
152 
153 /**
154  * This function adds the nodepath as an obstacle that is needed by the
155  * obstacle avoidance behavior.
156  */
158  _obstacles.push_back(obstacle);
159 }
160 
161 /**
162  * This function removes the nodepath from the obstacles list that is needed
163  * by the obstacle avoidance behavior.
164  */
166  for(unsigned int i = 0; i <= _obstacles.size(); ++i) {
167  if(_obstacles[i] == obstacle) {
168  _obstacles.erase(_obstacles.begin() + i);
169  }
170  }
171 }
void flock_on(unsigned int flock_id)
This function turns on the flock behavior.
Definition: aiWorld.cxx:142
void remove_flock(unsigned int flock_id)
This function removes the flock behavior completely.
Definition: aiWorld.cxx:109
void add_obstacle(NodePath obstacle)
This function adds the nodepath as an obstacle that is needed by the obstacle avoidance behavior.
Definition: aiWorld.cxx:157
void update()
The AIWorld update function calls the update function of all the AI characters which have been added ...
Definition: aiWorld.cxx:73
void print_list()
This function prints the names of the AI characters that have been added to the AIWorld.
Definition: aiWorld.cxx:63
void add_flock(Flock *flock)
This function adds all the AI characters in the Flock object to the AICharPool.
Definition: aiWorld.cxx:84
This class is used to define the flock attributes and the AI characters which are part of the flock.
Definition: flock.h:26
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void flock_off(unsigned int flock_id)
This function turns off the flock behavior temporarily.
Definition: aiWorld.cxx:127
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:165
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:161
void update()
Each character's update will update its AI and physics based on his resultant steering force.
Definition: aiCharacter.cxx:45
Flock get_flock(unsigned int flock_id)
This function returns a handle to the Flock whose id is passed.
Definition: aiWorld.cxx:96