objectcontroller_L.cpp

Go to the documentation of this file.
00001 #include "lugre_prefix.h"
00002 #include "lugre_luabind.h"
00003 #include "object.h"
00004 #include "objectcontroller.h"
00005 
00006 using namespace Lugre;
00007 
00008 /*
00009 #include <assert.h>
00010 #include <stdarg.h>
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <map>
00014 #include "gfx3D.h"
00015 #include "net.h"
00016 #include "fifo.h"
00017 #include "player.h"
00018 #include "game.h"
00019 #include "client.h"
00020 #include "server.h"
00021 #include "smartptr.h"
00022 //#include "config.h"
00023 #include "scripting.h"
00024 #include "input.h"
00025 #include "sound.h"
00026 #include "robstring1.2.h"
00027 #include "location.h"
00028 */
00029 
00030 extern "C" {
00031     #include "lua.h"
00032     #include "lauxlib.h"
00033     #include "lualib.h"
00034 }
00035 
00036 class cObjectController_L : public cLuaBind<cObjectController> { public:
00038         virtual void RegisterMethods    (lua_State *L) { PROFILE
00039             lua_register(L,"CreateObjectController",        &cObjectController_L::CreateObjectController);
00040 
00041             #define REGISTER_METHOD(methodname) mlMethod.push_back(make_luaL_reg(#methodname,&cObjectController_L::methodname));
00042             REGISTER_METHOD(Destroy);
00043             REGISTER_METHOD(SetApproachObject);
00044             REGISTER_METHOD(SetApproachPosition);
00045             REGISTER_METHOD(IsTargetAlive);
00046             REGISTER_METHOD(DistanceToTarget);          
00047         }
00048 
00050         virtual void RegisterMembers    () { PROFILE
00051             cObjectController* prototype = new cObjectController(); // memory leak : never deleted, but better than side effects
00052             cMemberVar_REGISTER(prototype,  kVarType_Float,         mfMaxAccel,         0);
00053             cMemberVar_REGISTER(prototype,  kVarType_Float,         mfApproachMinDist,  0);
00054             cMemberVar_REGISTER(prototype,  kVarType_bool,          mbStupid,           0);
00055             cMemberVar_REGISTER(prototype,  kVarType_Float,         mfMaxSpeed,         0);
00056             //~ cMemberVar_REGISTER(prototype,  kVarType_int,           miLastChangeTime,       0);
00057             //~ cMemberVar_REGISTER(prototype,  kVarType_bool,          mbResynced,     0);
00058             //~ cMemberVar_REGISTER(prototype,  kVarType_Vector3,       mvPos,          0);
00059             //~ cMemberVar_REGISTER(prototype,  kVarType_Quaternion,    mqTurn,         0);
00060         }
00061 
00062     // static methods exported to lua
00063 
00065         static int                          CreateObjectController      (lua_State *L) { PROFILE 
00066             return CreateUData(L,new cObjectController()); 
00067         }
00068 
00069     // object methods exported to lua
00070         
00071         static int  Destroy             (lua_State *L) { PROFILE 
00072             delete checkudata_alive(L);
00073             return 0; 
00074         }
00075         
00077         static int          SetApproachObject   (lua_State *L) { PROFILE 
00078             cObject* pTarget = (lua_gettop(L) >= 2 && !lua_isnil(L,2)) ? cLuaBind<cObject>::checkudata(L,2) : 0;
00079             checkudata_alive(L)->SetTarget(pTarget ? (new cObjectContollerTargetObject(pTarget)) : 0);
00080             return 0; 
00081         }
00082         
00084         static int          SetApproachPosition (lua_State *L) { PROFILE 
00085             checkudata_alive(L)->SetTarget(new cObjectContollerTargetPosition(
00086                     Ogre::Vector3(
00087                         luaL_checknumber(L,2),
00088                         luaL_checknumber(L,3),
00089                         luaL_checknumber(L,4)
00090                     )
00091             ));
00092             return 0; 
00093         }
00094 
00096         // only call if target is alive
00097         static int          DistanceToTarget    (lua_State *L) { PROFILE 
00098             cObjectController*p = checkudata_alive(L);
00099             if(p->mpApproachTarget && p->mpApproachTarget->IsAlive()){
00100                 Vector3 other(
00101                         luaL_checknumber(L,2),
00102                         luaL_checknumber(L,3),
00103                         luaL_checknumber(L,4));
00104                 lua_pushnumber(L,p->mpApproachTarget->GetPosition().distance(other));
00105             } else {
00106                 lua_pushnumber(L,0);
00107             }
00108             return 1;
00109         }
00110         
00112         static int          IsTargetAlive   (lua_State *L) { PROFILE 
00113             cObjectController*p = checkudata_alive(L);
00114             if(p->mpApproachTarget){
00115                 lua_pushboolean(L,p->mpApproachTarget->IsAlive());
00116             } else {
00117                 lua_pushboolean(L,false);
00118             }
00119             return 1;
00120         }
00121         
00122         virtual const char* GetLuaTypeName () { return "sfz.objcontroller"; }
00123 };
00124 
00125 
00127 void    cObjectController::LuaRegister  (lua_State *L) { PROFILE
00128     cLuaBind<cObjectController>::GetSingletonPtr(new cObjectController_L())->LuaRegister(L);
00129 }
00130 
00131 
00132 
00133 
00134 #if 0
00135 using namespace Ogre;
00136 
00137 class cObject;
00138 class lua_State;
00139 
00140 class cObject_L : public cLuaBind<cObject> { public:
00142         virtual void RegisterMethods    (lua_State *L) { PROFILE
00143             lua_register(L,"RayQuery",          &cObject_L::RayQuery);
00144             lua_register(L,"GetIntersecting",   &cObject_L::GetIntersecting);
00145         }
00146 
00147     // object methods exported to lua
00148         
00149     // static methods exported to lua
00150 
00153         static int      RayQuery        (lua_State *L) { PROFILE
00154             // TODO : repair (location system)... currently unused though
00155             /*
00156             std::vector<std::pair<Real,cObject*> > resultset;
00157             cGame::GetSingleton().RayQuery(luaSFZ_checkVector3(L,1),luaSFZ_checkVector3(L,4),resultset);
00158 
00159             // push 2 values for each hit : objid,dist
00160             int nres = 0;
00161             for (std::vector<std::pair<Real,cObject*> >::iterator itor=resultset.begin();itor!=resultset.end();++itor) {
00162                 lua_pushnumber(L,(*itor).second->miID);
00163                 lua_pushnumber(L,(*itor).first);
00164                 luaL_checkstack(L, 2, "too many results");
00165                 nres += 2;
00166             }
00167             return nres;
00168             */
00169         }
00170 
00173         static int      GetIntersecting (lua_State *L) { PROFILE
00174             std::set<size_t> resultset;
00175             
00176             // TODO : repair (location system)... currently unused though
00177             // cGame::GetSingleton().GetIntersectingIDs(luaSFZ_checkVector3(L,1),luaL_checknumber(L,4),resultset);
00178 
00179             // push 1 value for each hit : objid
00180             int nres = 0;
00181             /*
00182             for (std::set<size_t>::iterator itor=resultset.begin();itor!=resultset.end();++itor) {
00183                 lua_pushnumber(L,(*itor));
00184                 luaL_checkstack(L, 1, "too many results");
00185                 ++nres;
00186             }*/
00187             return nres;
00188         }
00189 
00190         virtual const char* GetLuaTypeName () { return "sfz.objcontroller"; }
00191 };
00192 #endif

Generated on Wed May 23 06:00:15 2012 for cpp by  doxygen 1.5.6