LoiterProcess.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2004-2007 The Pentagram team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #include "pent_include.h"
00020 
00021 #include "LoiterProcess.h"
00022 #include "Actor.h"
00023 #include "PathfinderProcess.h"
00024 #include "Kernel.h"
00025 #include "DelayProcess.h"
00026 #include "getObject.h"
00027 
00028 #include "IDataSource.h"
00029 #include "ODataSource.h"
00030 
00031 // p_dynamic_cast stuff
00032 DEFINE_RUNTIME_CLASSTYPE_CODE(LoiterProcess,Process);
00033 
00034 LoiterProcess::LoiterProcess() : Process()
00035 {
00036 
00037 }
00038 
00039 LoiterProcess::LoiterProcess(Actor* actor_, sint32 c)
00040 {
00041         assert(actor_);
00042         item_num = actor_->getObjId();
00043         count = c;
00044 
00045         type = 0x205; // CONSTANT!
00046 }
00047 
00048 bool LoiterProcess::run(const uint32 /*framenum*/)
00049 {
00050         if (!count) {
00051                 terminate();
00052                 return false;
00053         }
00054         if (count > 0)
00055                 count--;
00056 
00057         Actor *a = getActor(item_num);
00058 
00059         if (!a || a->isDead()) {
00060                 // dead?
00061                 terminate();
00062                 return false;
00063         }
00064 
00065         sint32 x,y,z;
00066         a->getLocation(x,y,z);
00067 
00068         x += 32 * ((std::rand() % 20) - 10);
00069         y += 32 * ((std::rand() % 20) - 10);
00070 
00071         PathfinderProcess* pfp = new PathfinderProcess(a,x,y,z);
00072         Kernel::get_instance()->addProcess(pfp);
00073 
00074         bool hasidle1 = a->hasAnim(Animation::idle1);
00075         bool hasidle2 = a->hasAnim(Animation::idle2);
00076 
00077         if ((hasidle1 || hasidle2) && ((std::rand()%3) == 0))
00078         {
00079                 Animation::Sequence idleanim;
00080 
00081                 if (!hasidle1) {
00082                         idleanim = Animation::idle2;
00083                 } else if (!hasidle2) {
00084                         idleanim = Animation::idle1;
00085                 } else {
00086                         if (std::rand()%2)
00087                                 idleanim = Animation::idle1;
00088                         else
00089                                 idleanim = Animation::idle2;
00090                 }
00091                 uint16 idlepid = a->doAnim(idleanim, 8);
00092                 Process* idlep = Kernel::get_instance()->getProcess(idlepid);
00093                 idlep->waitFor(pfp);
00094 
00095                 waitFor(idlep);
00096 
00097         } else {
00098                 // wait 4-7 sec
00099                 DelayProcess* dp = new DelayProcess(30 * (4 + (std::rand()%3)));
00100                 Kernel::get_instance()->addProcess(dp);
00101                 dp->waitFor(pfp);
00102 
00103                 waitFor(dp);
00104         }
00105         
00106 
00107         return false;
00108 }
00109 
00110 void LoiterProcess::saveData(ODataSource* ods)
00111 {
00112         Process::saveData(ods);
00113 
00114         ods->write4(count);
00115 }
00116 
00117 bool LoiterProcess::loadData(IDataSource* ids, uint32 version)
00118 {
00119         if (!Process::loadData(ids, version)) return false;
00120 
00121         if (version >= 3)
00122                 count = ids->read4();
00123         else
00124                 count = 0; // default to loitering indefinitely
00125 
00126         return true;
00127 }

Generated on Fri Jul 27 22:27:23 2007 for pentagram by  doxygen 1.4.7