Panda3D
Loading...
Searching...
No Matches
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
16AIWorld::AIWorld(NodePath render) {
17 _render = std::move(render);
18}
19
20AIWorld::~AIWorld() {
21}
22
23void 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
29void 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
44void 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 */
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 */
96Flock 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 static Flock null_flock(0, 0.0, 0.0, 0, 0, 0);
103 nassertr(false, null_flock);
104 return null_flock;
105}
106
107/**
108 * This function removes the flock behavior completely.
109 */
110void AIWorld::remove_flock(unsigned int flock_id) {
111 for(unsigned int i = 0; i < _flock_pool.size(); ++i) {
112 if(_flock_pool[i]->get_id() == flock_id) {
113 for(unsigned int j = 0; j < _flock_pool[i]->_ai_char_list.size(); ++j) {
114 _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock_activate");
115 _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock");
116 _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->_flock_group = nullptr;
117 }
118 _flock_pool.erase(_flock_pool.begin() + i);
119 break;
120 }
121 }
122}
123
124/**
125 * This function turns off the flock behavior temporarily. Similar to pausing
126 * the behavior.
127 */
128void AIWorld::flock_off(unsigned int flock_id) {
129 for(unsigned int i = 0; i < _flock_pool.size(); ++i) {
130 if(_flock_pool[i]->get_id() == flock_id) {
131 for(unsigned int j = 0; j < _flock_pool[i]->_ai_char_list.size(); ++j) {
132 _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock_activate");
133 _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock");
134 }
135 break;
136 }
137 }
138}
139
140/**
141 * This function turns on the flock behavior.
142 */
143void AIWorld::flock_on(unsigned int flock_id) {
144 for(unsigned int i = 0; i < _flock_pool.size(); ++i) {
145 if(_flock_pool[i]->get_id() == flock_id) {
146 for(unsigned int j = 0; j < _flock_pool[i]->_ai_char_list.size(); ++j) {
147 _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_on("flock_activate");
148 }
149 break;
150 }
151 }
152}
153
154/**
155 * This function adds the nodepath as an obstacle that is needed by the
156 * obstacle avoidance behavior.
157 */
159 _obstacles.push_back(obstacle);
160}
161
162/**
163 * This function removes the nodepath from the obstacles list that is needed
164 * by the obstacle avoidance behavior.
165 */
167 for(unsigned int i = 0; i <= _obstacles.size(); ++i) {
168 if(_obstacles[i] == obstacle) {
169 _obstacles.erase(_obstacles.begin() + i);
170 }
171 }
172}
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void update()
Each character's update will update its AI and physics based on his resultant steering force.
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
void update()
The AIWorld update function calls the update function of all the AI characters which have been added ...
Definition aiWorld.cxx:73
void remove_flock(unsigned int flock_id)
This function removes the flock behavior completely.
Definition aiWorld.cxx:110
Flock get_flock(unsigned int flock_id)
This function returns a handle to the Flock whose id is passed.
Definition aiWorld.cxx:96
void flock_off(unsigned int flock_id)
This function turns off the flock behavior temporarily.
Definition aiWorld.cxx:128
void flock_on(unsigned int flock_id)
This function turns on the flock behavior.
Definition aiWorld.cxx:143
void add_obstacle(NodePath obstacle)
This function adds the nodepath as an obstacle that is needed by the obstacle avoidance behavior.
Definition aiWorld.cxx:158
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:166
This class is used to define the flock attributes and the AI characters which are part of the flock.
Definition flock.h:26
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition nodePath.h:159