location_L.cpp

Go to the documentation of this file.
00001 #include "lugre_prefix.h"
00002 #include "lugre_luabind.h"
00003 #include "lugre_ogrewrapper.h"
00004 #include "object.h"
00005 #include "location.h"
00006 #include <Ogre.h>
00007 
00008 extern "C" {
00009     #include "lua.h"
00010     #include "lauxlib.h"
00011     #include "lualib.h"
00012 }
00013 
00014 using namespace Lugre;
00015 
00016 
00017 
00018 class cLocation_L : public cLuaBind<cLocation> { public:
00020         virtual void RegisterMethods    (lua_State *L) { PROFILE
00021             lua_register(L,"CreateLocation",        &cLocation_L::CreateLocation);
00022 
00023             #define REGISTER_METHOD(methodname) mlMethod.push_back(make_luaL_reg(#methodname,&cLocation_L::methodname));
00024             REGISTER_METHOD(Destroy);
00025             REGISTER_METHOD(GetBoundRad);
00026             REGISTER_METHOD(SetBoundRad);
00027             REGISTER_METHOD(StepAllObjects);
00028             REGISTER_METHOD(DeleteAllObjects);
00029             REGISTER_METHOD(SendResyncs);
00030             REGISTER_METHOD(StoreResyncs);
00031             REGISTER_METHOD(IntersectSphere);
00032             REGISTER_METHOD(Intersect2DRect); 
00033         }
00034 
00035     // static methods exported to lua
00036 
00038         static int                  CreateLocation      (lua_State *L) { PROFILE 
00039             return CreateUData(L,new cLocation()); 
00040         }
00041 
00042     // object methods exported to lua
00043         
00044         static int  Destroy             (lua_State *L) { PROFILE 
00045             delete checkudata_alive(L);
00046             return 0; 
00047         }
00048         
00050         static int          GetBoundRad (lua_State *L) { PROFILE 
00051             lua_pushnumber(L,checkudata_alive(L)->mfBoundingRad);
00052             return 1; 
00053         }
00054         
00056         static int          SetBoundRad (lua_State *L) { PROFILE 
00057             checkudata_alive(L)->mfBoundingRad = luaL_checknumber(L,2);
00058             return 0; 
00059         }
00060         
00062         static int          DeleteAllObjects    (lua_State *L) { PROFILE 
00063             checkudata_alive(L)->DeleteAllObjects();
00064             return 0; 
00065         }
00066         
00067         
00069         static int          StepAllObjects  (lua_State *L) { PROFILE 
00070             bool bIncResyncCounterLow = (lua_gettop(L) >= 4 && !lua_isnil(L,4)) ? (lua_isboolean(L,4) ? lua_toboolean(L,4) : luaL_checkint(L,4)) : false;
00071             checkudata_alive(L)->StepAllObjects(luaL_checkint(L,2),luaL_checknumber(L,3),bIncResyncCounterLow);
00072             return 0; 
00073         }
00074         
00075         
00079         static int          SendResyncs     (lua_State *L) { PROFILE 
00080             cUDP_SendSocket*    pUDP_SendSocket     = cLuaBind<cUDP_SendSocket>::checkudata_alive(L,2);
00081             
00082             uint32              iAddr               = (uint32)(long)(lua_touserdata(L,3)); 
00083             // iAddr is not really a pointer, but lua has problems encoding full 32bit integers in it's number type (float)
00084             
00085             int                 iPort               = luaL_checkint(L,4);
00086             int                 iMinLastChangeTime  = luaL_checkint(L,5);
00087             float               fRandomResyncProb   = luaL_checknumber(L,6);
00088             lua_pushnumber(L,checkudata_alive(L)->SendResyncs(*pUDP_SendSocket,iAddr,iPort,iMinLastChangeTime,fRandomResyncProb));
00089             return 1; 
00090         }
00091         
00095         static int          StoreResyncs        (lua_State *L) { PROFILE 
00096             cFIFO*      pFIFO               = cLuaBind<cFIFO>::checkudata_alive(L,2);
00097             int         iMinLastChangeTime  = luaL_checkint(L,3);
00098             float       fRandomResyncProb   = luaL_checknumber(L,4);
00099             lua_pushnumber(L,checkudata_alive(L)->StoreResyncs(*pFIFO,iMinLastChangeTime,fRandomResyncProb));
00100             return 1; 
00101         }
00102         
00108         static int                  IntersectSphere     (lua_State *L) { PROFILE 
00109             cLocation*      pLocation = checkudata_alive(L);
00110             Ogre::Vector3   vPos = luaSFZ_checkVector3(L,2);
00111             float           fRad = luaL_checknumber(L,5);
00112             
00113             // check early out
00114             if (lua_gettop(L) >= 6 && !lua_isnil(L,6)) {
00115                 float fEarlyOutRad = luaL_checknumber(L,6);
00116                 if (fEarlyOutRad < 0) fEarlyOutRad = pLocation->mfBoundingRad;
00117                 if (vPos.squaredLength() > mysquare(fRad + fEarlyOutRad)) return 0;
00118             }
00119             
00120             // construct result table
00121             lua_newtable(L);
00122             int i=0;
00123             cObject* pObj;
00124             for (std::list<cObject*>::iterator itor=pLocation->mlObjects.begin();itor!=pLocation->mlObjects.end();++itor) {
00125                 pObj = (*itor);
00126                 if ((vPos - pObj->mvPos).squaredLength() <= mysquare(fRad + pObj->mfBoundingRad)) {
00127                     lua_pushnumber( L, pObj->GetID() );
00128                     lua_rawseti( L, -2, ++i );
00129                 }
00130             }
00131             return 1;
00132         }
00133         
00137         static int                  Intersect2DRect     (lua_State *L) { PROFILE 
00138             // mpGfx3D 
00139             cLocation*      pLocation = checkudata_alive(L);
00140             float           x0 = luaL_checknumber(L,2);
00141             float           y0 = luaL_checknumber(L,3);
00142             float           x1 = luaL_checknumber(L,4);
00143             float           y1 = luaL_checknumber(L,5);
00144             float           fMaxDist = (lua_gettop(L) >= 6 && !lua_isnil(L,6)) ? luaL_checknumber(L,6) : 0;
00145             
00146             // construct result table
00147             lua_newtable(L);
00148             cObject*        pObj;
00149             cOgreWrapper&   ogrewrapper = cOgreWrapper::GetSingleton();
00150             Ogre::Real      x,y,cx,cy;
00151             Ogre::Real sw = Ogre::Real(ogrewrapper.GetViewportWidth());
00152             Ogre::Real sh = Ogre::Real(ogrewrapper.GetViewportHeight());
00153             for (std::list<cObject*>::iterator itor=pLocation->mlObjects.begin();itor!=pLocation->mlObjects.end();++itor) {
00154                 pObj = (*itor);
00155                 if (ogrewrapper.ProjectSizeAndPos(pObj->mvPos,x,y,pObj->mfBoundingRad,cx,cy)) { // if bIsOnScreen
00156                     x = (x + 1.0) * 0.5 * sw;
00157                     y = (-y + 1.0) * 0.5 * sh;
00158                     cx *= sw;
00159                     cy *= sh;
00160                     float fDist = mymax(0.0f,   mymax(  mymax(-(x0 - (x-cx/2)),-(y0 - (y-cy/2))),
00161                                                         mymax( (x0 - (x+cx/2)), (y0 - (y+cy/2))) ));
00162                     
00163                     //if (x >= x0-cx/2 && x <= x1+cx/2 && y >= y0-cy/2 && y <= y1+cy/2) {
00164                     if (fDist <= fMaxDist) {
00165                         lua_pushnumber( L, fDist );
00166                         lua_rawseti( L, -2, pObj->GetID() );
00167                     }
00168                 }
00169             }
00170             return 1;
00171         }
00172 
00173         virtual const char* GetLuaTypeName () { return "sfz.location"; }
00174 };
00175 
00177 void    cLocation::LuaRegister  (lua_State *L) { PROFILE
00178     cLuaBind<cLocation>::GetSingletonPtr(new cLocation_L())->LuaRegister(L);
00179 }

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