Revision: 1530
          http://rigsofrods.svn.sourceforge.net/rigsofrods/?rev=1530&view=rev
Author:   rorthomas
Date:     2010-08-23 01:43:26 +0000 (Mon, 23 Aug 2010)

Log Message:
-----------
improved scripting capabilities: callbacks if all wheels of a truck are in a 
certain box

Modified Paths:
--------------
    trunk/source/main/Beam.cpp
    trunk/source/main/BeamData.h
    trunk/source/main/RoRFrameListener.cpp
    trunk/source/main/ScriptEngine.cpp
    trunk/source/main/ScriptEngine.h
    trunk/source/main/collisions.cpp
    trunk/source/main/collisions.h

Modified: trunk/source/main/Beam.cpp
===================================================================
--- trunk/source/main/Beam.cpp  2010-08-22 22:17:15 UTC (rev 1529)
+++ trunk/source/main/Beam.cpp  2010-08-23 01:43:26 UTC (rev 1530)
@@ -8075,9 +8075,10 @@
                                ground_model_t *gm = 0; // this is used as 
result storage, so we can use it later on
                                int contacted = 
collisions->groundCollision(&nodes[i], nodes[i].colltesttimer, &gm, &ns);
 
+                               int handlernum = 0;
                                //TODO is this supposed to be a binary 
operator, or a logical operator?
                                if ( contacted |
-                                       collisions->nodeCollision(&nodes[i], 
i==cinecameranodepos[currentcamera], contacted, nodes[i].colltesttimer, &ns, 
&gm))
+                                       collisions->nodeCollision(&nodes[i], 
i==cinecameranodepos[currentcamera], contacted, nodes[i].colltesttimer, &ns, 
&gm, &handlernum))
                                {
                                        //FX
                                        if (gm && doUpdate && dustp)
@@ -8122,6 +8123,8 @@
                                                
wheels[nodes[i].wheelid].lastGroundModel = gm;
                                        }
 
+                                       
wheels[nodes[i].wheelid].lastEventHandler = handlernum;
+
                                        // note last ground model
                                        lastFuzzyGroundModel = gm;
                                }

Modified: trunk/source/main/BeamData.h
===================================================================
--- trunk/source/main/BeamData.h        2010-08-22 22:17:15 UTC (rev 1529)
+++ trunk/source/main/BeamData.h        2010-08-23 01:43:26 UTC (rev 1530)
@@ -424,6 +424,9 @@
        float lastSlip;
        int lastContactType;
        ground_model_t *lastGroundModel;
+
+       // for improved collision code
+       int lastEventHandler;
 };
 
 struct vwheel

Modified: trunk/source/main/RoRFrameListener.cpp
===================================================================
--- trunk/source/main/RoRFrameListener.cpp      2010-08-22 22:17:15 UTC (rev 
1529)
+++ trunk/source/main/RoRFrameListener.cpp      2010-08-23 01:43:26 UTC (rev 
1530)
@@ -800,7 +800,7 @@
 
 
 #ifdef USE_ANGELSCRIPT
-       new ScriptEngine(this);
+       new ScriptEngine(this, 0);
        exploreScripts();
 #endif
 
@@ -4573,6 +4573,7 @@
 #endif
 
 #ifdef USE_ANGELSCRIPT
+       ScriptEngine::getSingleton().setCollisions(collisions);
        if(!netmode)
        {
                LogManager::getSingleton().logMessage("Loading Angelscript 
Script engine." );
@@ -7253,8 +7254,11 @@
        }
 
 
+       // TODO: check if all wheels are on a certain event id
+       // wheels[nodes[i].wheelid].lastEventHandler
+
 #ifdef USE_ANGELSCRIPT
-       ScriptEngine::getSingleton().framestep(dt);
+       ScriptEngine::getSingleton().framestep(dt, trucks, free_truck);
 #endif
 
        // update network labels

Modified: trunk/source/main/ScriptEngine.cpp
===================================================================
--- trunk/source/main/ScriptEngine.cpp  2010-08-22 22:17:15 UTC (rev 1529)
+++ trunk/source/main/ScriptEngine.cpp  2010-08-23 01:43:26 UTC (rev 1530)
@@ -35,6 +35,7 @@
 #include "as_ogre.h"
 #include "SelectorWindow.h"
 #include "sha1.h"
+#include "collisions.h"
 
 using namespace Ogre;
 using namespace std;
@@ -42,7 +43,7 @@
 
 template<> ScriptEngine *Ogre::Singleton<ScriptEngine>::ms_Singleton=0;
 
-ScriptEngine::ScriptEngine(RoRFrameListener *efl) : mefl(efl), engine(0), 
context(0), frameStepFunctionPtr(-1), eventMask(0)
+ScriptEngine::ScriptEngine(RoRFrameListener *efl, Collisions *_coll) : 
mefl(efl), coll(_coll), engine(0), context(0), frameStepFunctionPtr(-1), 
wheelEventFunctionPtr(-1), eventMask(0)
 {
        init();
 
@@ -112,6 +113,8 @@
 
        // get some other optional functions
        frameStepFunctionPtr = mod->GetFunctionIdByDecl("void 
frameStep(float)");
+       wheelEventFunctionPtr = mod->GetFunctionIdByDecl("void wheelEvents(int, 
string, string)");
+       
        eventCallbackFunctionPtr = mod->GetFunctionIdByDecl("void 
eventCallback(int, int)");
 
        // Create our context, prepare it, and then execute
@@ -587,8 +590,57 @@
        return 1;
 }
 
-int ScriptEngine::framestep(Ogre::Real dt)
+int ScriptEngine::framestep(Ogre::Real dt, Beam **trucks, int free_truck)
 {
+       // check for all truck wheels
+       if(coll && wheelEventFunctionPtr != -1)
+       {
+               for(int t = 0; t < free_truck; t++)
+               {
+                       Beam *b = trucks[t];
+                       if (!b || b->state == SLEEPING || b->state == NETWORKED 
|| b->state == RECYCLE)
+                               continue;
+
+                       bool allwheels=true;
+                       int handlerid=-1;
+                       for(int w = 0; w < b->free_wheel; w++)
+                       {
+                               if(handlerid == -1 && 
b->wheels[w].lastEventHandler != -1)
+                               {
+                                       handlerid = 
b->wheels[w].lastEventHandler;
+                                       continue;
+                               }else if(b->wheels[w].lastEventHandler != 
handlerid)
+                               {
+                                       allwheels=false;
+                                       break;
+                               }
+                       }
+
+                       if(handlerid >= 0 && handlerid < MAX_EVENTSOURCE)
+                       {
+                               eventsource_t *source = 
coll->getEvent(handlerid);
+                               if(!engine) return 0;
+                               if(!context) context = engine->CreateContext();
+                               context->Prepare(wheelEventFunctionPtr);
+
+                               // Set the function arguments
+                               context->SetArgFloat (0, t);
+                               context->SetArgObject(1, 
&std::string(source->instancename));
+                               context->SetArgObject(2, 
&std::string(source->boxname));
+
+                               //LogManager::getSingleton().logMessage("SE| 
Executing framestep()");
+                               int r = context->Execute();
+                               if( r == asEXECUTION_FINISHED )
+                               {
+                                 // The return value is only valid if the 
execution finished successfully
+                                 asDWORD ret = context->GetReturnDWord();
+                               }                               
+                       }
+               }
+       }
+
+
+       // framestep stuff below
        if(frameStepFunctionPtr<0) return 1;
        if(!engine) return 0;
        if(!context) context = engine->CreateContext();
@@ -607,7 +659,8 @@
        return 0;
 }
 
-int ScriptEngine::envokeCallback(int functionPtr, eventsource_t *source)
+
+int ScriptEngine::envokeCallback(int functionPtr, eventsource_t *source, 
node_t *node)
 {
        if(functionPtr<=0) return 1;
        if(!engine) return 0;
@@ -617,6 +670,10 @@
        // Set the function arguments
        context->SetArgObject(0, &std::string(source->instancename));
        context->SetArgObject(1, &std::string(source->boxname));
+       if(node)
+               context->SetArgDWord (2, node->id);
+       else
+               context->SetArgDWord (2, -1);
 
        //LogManager::getSingleton().logMessage("SE| Executing framestep()");
        int r = context->Execute();

Modified: trunk/source/main/ScriptEngine.h
===================================================================
--- trunk/source/main/ScriptEngine.h    2010-08-22 22:17:15 UTC (rev 1529)
+++ trunk/source/main/ScriptEngine.h    2010-08-23 01:43:26 UTC (rev 1530)
@@ -54,9 +54,10 @@
 class ScriptEngine : public Ogre::Singleton<ScriptEngine>
 {
 public:
-       ScriptEngine(RoRFrameListener *efl);
+       ScriptEngine(RoRFrameListener *efl, Collisions *_coll);
        ~ScriptEngine();
 
+       void setCollisions(Collisions *_coll) { coll=_coll; };
 
        int loadScript(Ogre::String scriptname);
 
@@ -72,7 +73,7 @@
         * @param dt time passed since the last call to this function in seconds
         * @return 0 on success, everything else on error
         */
-       int framestep(Ogre::Real dt);
+       int framestep(Ogre::Real dt, Beam **trucks, int free_truck);
 
        /**
         * This enum describes what events are existing. The script can 
register to receive events.
@@ -121,15 +122,17 @@
         */
        int executeString(Ogre::String command);
 
-       int envokeCallback(int functionPtr, eventsource_t *source);
+       int envokeCallback(int functionPtr, eventsource_t *source, node_t 
*node);
 
        AngelScript::asIScriptEngine *getEngine() { return engine; };
 
 protected:
     RoRFrameListener *mefl;             //!< local RoRFrameListener instance, 
used as proxy for many functions
+       Collisions *coll;
     AngelScript::asIScriptEngine *engine;                //!< instance of the 
scripting engine
        AngelScript::asIScriptContext *context;              //!< context in 
which all scripting happens
        int frameStepFunctionPtr;               //!< script function pointer to 
the frameStep function
+       int wheelEventFunctionPtr;               //!< script function pointer
        int eventCallbackFunctionPtr;           //!< script function pointer to 
the event callback function
        std::map <std::string , std::vector<int> > callbacks;
 

Modified: trunk/source/main/collisions.cpp
===================================================================
--- trunk/source/main/collisions.cpp    2010-08-22 22:17:15 UTC (rev 1529)
+++ trunk/source/main/collisions.cpp    2010-08-23 01:43:26 UTC (rev 1530)
@@ -745,7 +745,7 @@
                                                                // prefer AS to 
LUA
                                                                bool handled = 
false;
 #ifdef USE_ANGELSCRIPT
-                                                               int ret = 
ScriptEngine::getSingleton().envokeCallback(eventsources[cbox->eventsourcenum].luahandler,
 &eventsources[cbox->eventsourcenum]);
+                                                               int ret = 
ScriptEngine::getSingleton().envokeCallback(eventsources[cbox->eventsourcenum].luahandler,
 &eventsources[cbox->eventsourcenum], 0);
                                                                if(ret == 0)
                                                                        
handled=true;
 #endif
@@ -806,7 +806,7 @@
                                                        // prefer AS to LUA
                                                        bool handled = false;
 #ifdef USE_ANGELSCRIPT
-                                                       int ret = 
ScriptEngine::getSingleton().envokeCallback(eventsources[cbox->eventsourcenum].luahandler,
 &eventsources[cbox->eventsourcenum]);
+                                                       int ret = 
ScriptEngine::getSingleton().envokeCallback(eventsources[cbox->eventsourcenum].luahandler,
 &eventsources[cbox->eventsourcenum], 0);
                                                        if(ret == 0)
                                                                handled=true;
 #endif
@@ -914,7 +914,7 @@
        return 0;
 }
 
-bool Collisions::nodeCollision(node_t *node, bool iscinecam, int contacted, 
float dt, float* nso, ground_model_t** ogm)
+bool Collisions::nodeCollision(node_t *node, bool iscinecam, int contacted, 
float dt, float* nso, ground_model_t** ogm, int *handlernum)
 {
        bool smoky=false;
        //float corrf=1.0;
@@ -959,8 +959,9 @@
                                                        {
                                                                // prefer AS to 
LUA
                                                                bool handled = 
false;
+                                                               if(handlernum) 
*handlernum = cbox->eventsourcenum;
 #ifdef USE_ANGELSCRIPT
-                                                               int ret = 
ScriptEngine::getSingleton().envokeCallback(eventsources[cbox->eventsourcenum].luahandler,
 &eventsources[cbox->eventsourcenum]);
+                                                               int ret = 
ScriptEngine::getSingleton().envokeCallback(eventsources[cbox->eventsourcenum].luahandler,
 &eventsources[cbox->eventsourcenum], node);
                                                                if(ret == 0)
                                                                        
handled=true;
 #endif
@@ -1015,8 +1016,9 @@
                                                {
                                                        // prefer AS to LUA
                                                        bool handled = false;
+                                                       if(handlernum) 
*handlernum = cbox->eventsourcenum;
 #ifdef USE_ANGELSCRIPT
-                                                       int ret = 
ScriptEngine::getSingleton().envokeCallback(eventsources[cbox->eventsourcenum].luahandler,
 &eventsources[cbox->eventsourcenum]);
+                                                       int ret = 
ScriptEngine::getSingleton().envokeCallback(eventsources[cbox->eventsourcenum].luahandler,
 &eventsources[cbox->eventsourcenum], node);
                                                        if(ret == 0)
                                                                handled=true;
 #endif

Modified: trunk/source/main/collisions.h
===================================================================
--- trunk/source/main/collisions.h      2010-08-22 22:17:15 UTC (rev 1529)
+++ trunk/source/main/collisions.h      2010-08-23 01:43:26 UTC (rev 1530)
@@ -149,6 +149,8 @@
        Vector3 forcecampos;
        ground_model_t *defaultgm, *defaultgroundgm;
 
+       eventsource_t *getEvent(int eventID) { return &eventsources[eventID]; };
+
        Collisions() {}; // for wrapper, DO NOT USE!
 
   Collisions(
@@ -160,7 +162,7 @@
        void addCollisionBox(SceneNode *tenode, bool rotating, bool virt, float 
px, float py, float pz, float rx, float ry, float rz, float lx,float hx,float 
ly,float hy,float lz,float hz,float srx,float sry,float srz, const char* 
eventname, const char* instancename, bool forcecam, Vector3 campos, float 
scx=1.0, float scy=1.0, float scz=1.0, float drx=0.0, float dry=0.0, float 
drz=0.0, int event_filter=EVENT_ALL, int luahandler=-1);
        int addCollisionTri(Vector3 p1, Vector3 p2, Vector3 p3, ground_model_t* 
gm);
        bool collisionCorrect(Vector3 *refpos);
-       bool nodeCollision(node_t *node, bool iscinecam, int contacted, float 
dt, float* nso, ground_model_t** ogm);
+       bool nodeCollision(node_t *node, bool iscinecam, int contacted, float 
dt, float* nso, ground_model_t** ogm, int *handlernum=0);
        Vector3 getPosition(char* instance, char* box);
        Quaternion getDirection(char* instance, char* box);
        collision_box_t *getBox(char* instance, char* box);


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
Rigsofrods-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rigsofrods-devel

Reply via email to