QuickAvatarMoverProcess.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2002-2005 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 #include "QuickAvatarMoverProcess.h"
00021 
00022 #include "MainActor.h"
00023 #include "World.h"
00024 #include "CurrentMap.h"
00025 #include "Kernel.h"
00026 #include "GUIApp.h"
00027 #include "ShapeInfo.h"
00028 #include "getObject.h"
00029 
00030 #include "IDataSource.h"
00031 #include "ODataSource.h"
00032 
00033 // p_dynamic_cast stuff
00034 DEFINE_RUNTIME_CLASSTYPE_CODE(QuickAvatarMoverProcess,Process);
00035 
00036 ProcId QuickAvatarMoverProcess::amp[6] = { 0, 0, 0, 0, 0, 0 };
00037 bool QuickAvatarMoverProcess::clipping = false;
00038 bool QuickAvatarMoverProcess::quarter = false;
00039 
00040 QuickAvatarMoverProcess::QuickAvatarMoverProcess() : Process(1), dx(0), dy(0), dz(0), dir(0)
00041 {
00042 }
00043 
00044 QuickAvatarMoverProcess::QuickAvatarMoverProcess(int x, int y, int z, int _dir) : Process(1), dx(x), dy(y), dz(z), dir(_dir)
00045 {
00046         QuickAvatarMoverProcess::terminateMover(dir);
00047         assert(dir < 6);
00048         amp[dir] = getPid();
00049 }
00050 
00051 QuickAvatarMoverProcess::~QuickAvatarMoverProcess()
00052 {
00053 }
00054 
00055 bool QuickAvatarMoverProcess::run(const uint32 /*framenum*/)
00056 {
00057         if (GUIApp::get_instance()->isAvatarInStasis())
00058         {
00059                 terminate();
00060                 return false;
00061         }
00062 
00063         MainActor * avatar = getMainActor();
00064         sint32 x,y,z;
00065         avatar->getLocation(x,y,z);
00066         sint32 ixd,iyd,izd;
00067         avatar->getFootpadWorld(ixd, iyd, izd);
00068 
00069         CurrentMap* cm = World::get_instance()->getCurrentMap();
00070 
00071         sint32 dx = this->dx;
00072         sint32 dy = this->dy;
00073         sint32 dz = this->dz;
00074 
00075         for (int j = 0; j < 3; j++)
00076         {
00077                 dx = this->dx;
00078                 dy = this->dy;
00079                 dz = this->dz;
00080 
00081                 if (j == 1) dx = 0;
00082                 else if (j == 2) dy = 0;
00083 
00084                 if (quarter)
00085                 {
00086                         dx /= 4;
00087                         dy /= 4;
00088                         dz /= 4;
00089                 }
00090 
00091                 bool ok = false;
00092 
00093                 while (dx || dy || dz) {
00094                         uint32 flags = avatar->getShapeInfo()->flags;
00095 
00096                         if (!clipping || cm->isValidPosition(x+dx,y+dy,z+dz,ixd,iyd,izd,flags,1,0,0))
00097                         {
00098                                 if (clipping && !dz)
00099                                 {
00100                                         if (cm->isValidPosition(x+dx,y+dy,z-8,ixd,iyd,izd,flags,1,0,0) &&
00101                                                         !cm->isValidPosition(x,y,z-8,ixd,iyd,izd,flags,1,0,0))
00102                                         {
00103                                                 dz = -8;
00104                                         }
00105                                         else if (cm->isValidPosition(x+dx,y+dy,z-16,ixd,iyd,izd,flags,1,0,0) &&
00106                                                         !cm->isValidPosition(x,y,z-16,ixd,iyd,izd,flags,1,0,0))
00107                                         {
00108                                                 dz = -16;
00109                                         }
00110                                         else if (cm->isValidPosition(x+dx,y+dy,z-24,ixd,iyd,izd,flags,1,0,0) &&
00111                                                         !cm->isValidPosition(x,y,z-24,ixd,iyd,izd,flags,1,0,0))
00112                                         {
00113                                                 dz = -24;
00114                                         }
00115                                         else if (cm->isValidPosition(x+dx,y+dy,z-32,ixd,iyd,izd,flags,1,0,0) &&
00116                                                         !cm->isValidPosition(x,y,z-32,ixd,iyd,izd,flags,1,0,0))
00117                                         {
00118                                                 dz = -32;
00119                                         }
00120                                 }
00121                                 ok = true;
00122                                 break;
00123                         }
00124                         else if (cm->isValidPosition(x+dx,y+dy,z+dz+8,ixd,iyd,izd,flags,1,0,0))
00125                         {
00126                                 dz+=8;
00127                                 ok = true;
00128                                 break;
00129                         }
00130                         dx/=2;
00131                         dy/=2;
00132                         dz/=2;
00133                 }
00134                 if (ok) break;
00135         }
00136 
00137         // Yes, i know, not entirely correct
00138         avatar->collideMove(x+dx,y+dy,z+dz, false, true);
00139         return true;
00140 }
00141 
00142 void QuickAvatarMoverProcess::terminate()
00143 {
00144         Process::terminate();
00145         amp[dir] = 0;
00146 }
00147 
00148 void QuickAvatarMoverProcess::terminateMover(int _dir)
00149 {
00150         assert(_dir < 6);
00151 
00152         Kernel * kernel = Kernel::get_instance();
00153 
00154         QuickAvatarMoverProcess * p =
00155                 p_dynamic_cast<QuickAvatarMoverProcess *>(kernel->getProcess(amp[_dir]));
00156 
00157         if (p && !p->is_terminated())
00158                 p->terminate();
00159 }
00160 
00161 void QuickAvatarMoverProcess::startMover(int x, int y, int z, int _dir)
00162 {
00163         GUIApp * g = GUIApp::get_instance();
00164         if (! g->isAvatarInStasis())
00165         {
00166                 Process *p = new QuickAvatarMoverProcess(x,y,z,_dir);
00167                 Kernel::get_instance()->addProcess(p);
00168         } else {
00169                 pout << "Can't: avatarInStasis" << std::endl;
00170         }
00171 }
00172 
00173 void QuickAvatarMoverProcess::saveData(ODataSource* ods)
00174 {
00175         Process::saveData(ods);
00176 
00177         ods->write4(dir);
00178         // don't save more information. We plan to terminate upon load
00179 }
00180 
00181 bool QuickAvatarMoverProcess::loadData(IDataSource* ids, uint32 version)
00182 {
00183         if (!Process::loadData(ids, version)) return false;
00184 
00185         // small safety precaution
00186         dir = ids->read4();
00187         if (dir < 6)
00188                 amp[dir] = 0;
00189         else
00190                 return false;
00191 
00192         terminateDeferred(); // Don't allow this process to continue
00193         return true;
00194 }
00195 
00196 void QuickAvatarMoverProcess::ConCmd_startMoveUp(const Console::ArgvType &argv)
00197 {
00198         QuickAvatarMoverProcess::startMover(-64,-64,0,0);
00199 }
00200 
00201 void QuickAvatarMoverProcess::ConCmd_startMoveDown(const Console::ArgvType &argv)
00202 {
00203         QuickAvatarMoverProcess::startMover(+64,+64,0,1);
00204 }
00205 
00206 void QuickAvatarMoverProcess::ConCmd_startMoveLeft(const Console::ArgvType &argv)
00207 {
00208         QuickAvatarMoverProcess::startMover(-64,+64,0,2);
00209 }
00210 
00211 void QuickAvatarMoverProcess::ConCmd_startMoveRight(const Console::ArgvType &argv)
00212 {
00213         QuickAvatarMoverProcess::startMover(+64,-64,0,3);
00214 }
00215 
00216 void QuickAvatarMoverProcess::ConCmd_startAscend(const Console::ArgvType &argv)
00217 {
00218         QuickAvatarMoverProcess::startMover(0,0,8,4);
00219 }
00220 
00221 void QuickAvatarMoverProcess::ConCmd_startDescend(const Console::ArgvType &argv){
00222         QuickAvatarMoverProcess::startMover(0,0,-8,5);
00223 }
00224 
00225 void QuickAvatarMoverProcess::ConCmd_stopMoveUp(const Console::ArgvType &argv)
00226 {
00227         QuickAvatarMoverProcess::terminateMover(0);
00228 }
00229 
00230 void QuickAvatarMoverProcess::ConCmd_stopMoveDown(const Console::ArgvType &argv){
00231         QuickAvatarMoverProcess::terminateMover(1);
00232 }
00233 
00234 void QuickAvatarMoverProcess::ConCmd_stopMoveLeft(const Console::ArgvType &argv)
00235 {
00236         QuickAvatarMoverProcess::terminateMover(2);
00237 }
00238 
00239 void QuickAvatarMoverProcess::ConCmd_stopMoveRight(const Console::ArgvType &argv)
00240 {
00241         QuickAvatarMoverProcess::terminateMover(3);
00242 }
00243 
00244 void QuickAvatarMoverProcess::ConCmd_stopAscend(const Console::ArgvType &argv)
00245 {
00246         QuickAvatarMoverProcess::terminateMover(4);
00247 }
00248 
00249 void QuickAvatarMoverProcess::ConCmd_stopDescend(const Console::ArgvType &argv)
00250 {
00251         QuickAvatarMoverProcess::terminateMover(5);
00252 }
00253 
00254 void QuickAvatarMoverProcess::ConCmd_toggleQuarterSpeed(const Console::ArgvType &argv)
00255 {
00256         QuickAvatarMoverProcess::setQuarterSpeed(!QuickAvatarMoverProcess::isQuarterSpeed());
00257 }
00258 
00259 void QuickAvatarMoverProcess::ConCmd_toggleClipping(const Console::ArgvType &argv)
00260 {
00261         QuickAvatarMoverProcess::toggleClipping();
00262         pout << "QuickAvatarMoverProcess::clipping = " << QuickAvatarMoverProcess::isClipping() << std::endl;
00263 }
00264 

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