Panda3D
 All Classes Functions Variables Enumerations
aiWorld.cxx
00001 ////////////////////////////////////////////////////////////////////////
00002 // Filename    : aiWorld.cxx
00003 // Created by  : Deepak, John, Navin
00004 // Date        :  8 Sep 09
00005 ////////////////////////////////////////////////////////////////////
00006 //
00007 // PANDA 3D SOFTWARE
00008 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00009 //
00010 // All use of this software is subject to the terms of the revised BSD
00011 // license.  You should have received a copy of this license along
00012 // with this source code in a file named "LICENSE."
00013 //
00014 ////////////////////////////////////////////////////////////////////
00015 
00016 #include "aiWorld.h"
00017 
00018 AIWorld::AIWorld(NodePath render) {
00019   _ai_char_pool = new AICharPool();
00020   _render = render;
00021 }
00022 
00023 AIWorld::~AIWorld() {
00024 }
00025 
00026 void AIWorld::add_ai_char(AICharacter *ai_char) {
00027   _ai_char_pool->append(ai_char);
00028   ai_char->_window_render = _render;
00029   ai_char->_world = this;
00030 }
00031 
00032 void AIWorld::remove_ai_char(string name) {
00033   _ai_char_pool->del(name);
00034   remove_ai_char_from_flock(name);
00035 }
00036 
00037 void AIWorld::remove_ai_char_from_flock(string name) {
00038   AICharPool::node *ai_pool;
00039   ai_pool = _ai_char_pool->_head;
00040   while((ai_pool) != NULL) {
00041     for(unsigned int i = 0; i < _flock_pool.size(); ++i) {
00042       if(ai_pool->_ai_char->_ai_char_flock_id == _flock_pool[i]->get_id()) {
00043         for(unsigned int j = 0; j<_flock_pool[i]->_ai_char_list.size(); ++j) {
00044           if(_flock_pool[i]->_ai_char_list[j]->_name == name) {
00045             _flock_pool[i]->_ai_char_list.erase(_flock_pool[i]->_ai_char_list.begin() + j);
00046             return;
00047           }
00048         }
00049       }
00050     }
00051     ai_pool = ai_pool->_next;
00052   }
00053 }
00054 
00055 void AIWorld::print_list() {
00056   _ai_char_pool->print_list();
00057 }
00058 
00059 ////////////////////////////////////////////////////////////////////////
00060 // Function : update
00061 // Description : The AIWorld update function calls the update function of all the
00062 //                AI characters which have been added to the AIWorld.
00063 
00064 /////////////////////////////////////////////////////////////////////////////////
00065 
00066 void AIWorld::update() {
00067   AICharPool::node *ai_pool;
00068   ai_pool = _ai_char_pool->_head;
00069 
00070   while((ai_pool)!=NULL) {
00071     ai_pool->_ai_char->update();
00072     ai_pool = ai_pool->_next;
00073   }
00074 }
00075 
00076 /////////////////////////////////////////////////////////////////////////////////
00077 //
00078 // Function : add_flock
00079 // Description : This function adds all the AI characters in the Flock object to
00080 //                the AICharPool. This function allows adding the AI characetrs as
00081 //                part of a flock.
00082 
00083 /////////////////////////////////////////////////////////////////////////////////
00084 
00085 void AIWorld::add_flock(Flock *flock) {
00086   // Add all the ai_characters in the flock to the AIWorld.
00087   for(unsigned int i = 0; i < flock->_ai_char_list.size(); ++i) {
00088     this->add_ai_char(flock->_ai_char_list[i]);
00089   }
00090   // Add the flock to the flock pool.
00091   _flock_pool.push_back(flock);
00092 }
00093 
00094 /////////////////////////////////////////////////////////////////////////////////
00095 //
00096 // Function : get_flock
00097 // Description : This function returns a handle to the Flock whose id is passed.
00098 
00099 /////////////////////////////////////////////////////////////////////////////////
00100 
00101 Flock AIWorld::get_flock(unsigned int flock_id) {
00102   for(unsigned int i=0; i < _flock_pool.size(); ++i) {
00103     if(_flock_pool[i]->get_id() == flock_id) {
00104       return *_flock_pool[i];
00105     }
00106   }
00107   Flock *null_flock = NULL;
00108   return *null_flock;
00109 }
00110 
00111 /////////////////////////////////////////////////////////////////////////////////
00112 //
00113 // Function : remove_flock
00114 // Description : This function removes the flock behavior completely.
00115 
00116 /////////////////////////////////////////////////////////////////////////////////
00117 
00118 void AIWorld::remove_flock(unsigned int flock_id) {
00119   for(unsigned int i = 0; i < _flock_pool.size(); ++i) {
00120     if(_flock_pool[i]->get_id() == flock_id) {
00121        for(unsigned int j = 0; j < _flock_pool[i]->_ai_char_list.size(); ++j) {
00122          _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock_activate");
00123          _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock");
00124          _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->_flock_group = NULL;
00125        }
00126        _flock_pool.erase(_flock_pool.begin() + i);
00127        break;
00128     }
00129   }
00130 }
00131 
00132 /////////////////////////////////////////////////////////////////////////////////
00133 //
00134 // Function : flock_off
00135 // Description : This function turns off the flock behavior temporarily. Similar to
00136 //                pausing the behavior.
00137 
00138 /////////////////////////////////////////////////////////////////////////////////
00139 
00140 void AIWorld::flock_off(unsigned int flock_id) {
00141   for(unsigned int i = 0; i < _flock_pool.size(); ++i) {
00142     if(_flock_pool[i]->get_id() == flock_id) {
00143        for(unsigned int j = 0; j < _flock_pool[i]->_ai_char_list.size(); ++j) {
00144          _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock_activate");
00145          _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_off("flock");
00146        }
00147        break;
00148     }
00149   }
00150 }
00151 
00152 /////////////////////////////////////////////////////////////////////////////////
00153 //
00154 // Function : flock_on
00155 // Description : This function turns on the flock behavior.
00156 
00157 /////////////////////////////////////////////////////////////////////////////////
00158 
00159 void AIWorld::flock_on(unsigned int flock_id) {
00160   for(unsigned int i = 0; i < _flock_pool.size(); ++i) {
00161     if(_flock_pool[i]->get_id() == flock_id) {
00162        for(unsigned int j = 0; j < _flock_pool[i]->_ai_char_list.size(); ++j) {
00163          _flock_pool[i]->_ai_char_list[j]->get_ai_behaviors()->turn_on("flock_activate");
00164        }
00165        break;
00166     }
00167   }
00168 }
00169 
00170 AICharPool::AICharPool() {
00171   _head = NULL;
00172 }
00173 
00174 AICharPool::~AICharPool() {
00175 }
00176 
00177 void AICharPool::append(AICharacter *ai_ch) {
00178   node *q;
00179   node *t;
00180 
00181   if(_head == NULL) {
00182     q = new node();
00183     q->_ai_char = ai_ch;
00184     q->_next = NULL;
00185     _head = q;
00186   }
00187   else {
00188     q = _head;
00189     while( q->_next != NULL) {
00190       q = q->_next;
00191     }
00192 
00193     t = new node();
00194     t->_ai_char = ai_ch;
00195     t->_next = NULL;
00196     q->_next = t;
00197   }
00198 }
00199 
00200 void AICharPool::del(string name) {
00201   node *q;
00202   node *r;
00203   q = _head;
00204 
00205   if(_head==NULL) {
00206     return;
00207   }
00208 
00209   // Only one node in the linked list
00210   if(q->_next == NULL) {
00211     if(q->_ai_char->_name == name) {
00212       _head = NULL;
00213       delete q;
00214     }
00215     return;
00216   }
00217 
00218   r = q;
00219   while( q != NULL) {
00220     if( q->_ai_char->_name == name) {
00221       // Special case
00222       if(q == _head) {
00223         _head = q->_next;
00224         delete q;
00225         return;
00226       }
00227 
00228       r->_next = q->_next;
00229       delete q;
00230       return;
00231     }
00232     r = q;
00233     q = q->_next;
00234   }
00235 }
00236 
00237 /////////////////////////////////////////////////////////////////////////////////
00238 //
00239 // Function : print_list
00240 // Description : This function prints the ai characters in the AICharPool. Used for
00241 //                debugging purposes.
00242 
00243 /////////////////////////////////////////////////////////////////////////////////
00244 
00245 void AICharPool::print_list() {
00246   node* q;
00247   q = _head;
00248   while(q != NULL) {
00249     cout<<q->_ai_char->_name<<endl;
00250     q = q->_next;
00251   }
00252 }
00253 
00254 /////////////////////////////////////////////////////////////////////////////////
00255 //
00256 // Function : add_obstacle
00257 // Description : This function adds the nodepath as an obstacle that is needed
00258 //                by the obstacle avoidance behavior.
00259 
00260 /////////////////////////////////////////////////////////////////////////////////
00261 
00262 void AIWorld::add_obstacle(NodePath obstacle) {
00263   _obstacles.push_back(obstacle);
00264 }
00265 
00266 /////////////////////////////////////////////////////////////////////////////////
00267 //
00268 // Function : remove_obstacle
00269 // Description : This function removes the nodepath from the obstacles list that is needed
00270 //                by the obstacle avoidance behavior.
00271 
00272 /////////////////////////////////////////////////////////////////////////////////
00273 
00274 void AIWorld::remove_obstacle(NodePath obstacle) {
00275   for(unsigned int i = 0; i <= _obstacles.size(); ++i) {
00276     if(_obstacles[i] == obstacle) {
00277       _obstacles.erase(_obstacles.begin() + i);
00278     }
00279   }
00280 }
 All Classes Functions Variables Enumerations