예제 #1
0
void GritObject::notifyFade (lua_State *L,
                             const GritObjectPtr &self,
                             const float fade)
{
    if (gritClass==NULL) GRIT_EXCEPT("Object destroyed");

    if (!isActivated()) return;

    STACK_BASE;
    //stack is empty

    push_cfunction(L, my_lua_error_handler);
    int error_handler = lua_gettop(L);

    //stack: err

    // call into lua...
    //stack: err
    getField(L, "setFade");
    //stack: err, class, callback
    if (lua_isnil(L, -1)) {
        // TODO(dcunnin): We should add needsFadeCallbacks.
        // This might be part of a genreal overhaul of lod levels, etc.

        // no setFade function, do nothing
        lua_pop(L, 2);
        //stack is empty
        STACK_CHECK;
        return;
    }

    //stack: err, callback
    // we now have the callback to play with
    
    push_gritobj(L, self); // persistent grit obj
    lua_pushnumber(L, fade); // fade
    //stack: err, callback, persistent, fade
    int status = lua_pcall(L, 2, 0, error_handler);
    if (status) {
        //stack: err, msg
        // pop the error message since the error handler will
        // have already printed it out
        lua_pop(L, 1);
        object_del(L, self);
        //stack: err
    }
    //stack: err

    lua_pop(L, 1);
    //stack is empty
    STACK_CHECK;

}
예제 #2
0
void GritObject::init (lua_State *L, const GritObjectPtr &self)
{
    if (gritClass==NULL) GRIT_EXCEPT("Object destroyed");

    STACK_BASE;
    //stack is empty

    push_cfunction(L, my_lua_error_handler);
    int error_handler = lua_gettop(L);

    //stack: err

    //stack: err
    getField(L, "init");
    //stack: err, callback
    if (lua_isnil(L, -1)) {
        lua_pop(L, 2);
        //stack is empty
        STACK_CHECK;
        CERR << "initializing object: \""<<name<<"\": "
             << "class \""<<gritClass->name<<"\" "
             << "does not have init function" << std::endl;
        object_del(L, self);
        return;
    }

    // Call the callback.

    lua_checkstack(L, 2);
    push_gritobj(L, self); // persistent grit obj
    //stack: err, callback, persistent
    int status = lua_pcall(L, 1, 0, error_handler);
    if (status) {
        //stack: err, msg
        // pop the error message since the error handler will
        // have already printed it out
        lua_pop(L, 1);
        CERR << "Object: \"" << name << "\" raised an error on initialization, so destroying it."
             << std::endl;
        // will deactivate us
        object_del(L, self);
        //stack: err
    }
    //stack: err

    lua_pop(L, 1);
    //stack is empty
    STACK_CHECK;

}
예제 #3
0
bool GritObject::stepCallback (lua_State *L, const GritObjectPtr &self, float elapsed)
{
    if (gritClass==NULL) GRIT_EXCEPT("Object destroyed");

    STACK_BASE;
    //stack is empty

    push_cfunction(L, my_lua_error_handler);
    int error_handler = lua_gettop(L);

    //stack: err

    getField(L, "stepCallback");
    //stack: err, callback
    if (lua_isnil(L, -1)) {
        lua_pop(L, 2);
        //stack is empty
        STACK_CHECK;
        return false;
    }

    // Call the callback.

    lua_checkstack(L, 2);
    push_gritobj(L, self); // persistent grit obj
    lua_pushnumber(L, elapsed); // time since last frame
    //stack: err, callback, instance, elapsed
    int status = lua_pcall(L, 2, 0, error_handler);
    if (status) {
        //stack: err, msg
        // pop the error message since the error handler will
        // have already printed it out
        lua_pop(L, 1);
        //stack: err
    }
    //stack: err

    lua_pop(L, 1);
    //stack is empty
    STACK_CHECK;

    return status == 0;
}
static int rbody_index (lua_State *L)
{
TRY_START
        check_args(L, 2);
        GET_UD_MACRO(RigidBody, self, 1, RBODY_TAG);
        const char *key = luaL_checkstring(L, 2);
        if (!::strcmp(key, "force")) {
                push_cfunction(L, rbody_force);
        } else if (!::strcmp(key, "impulse")) {
                push_cfunction(L, rbody_impulse);
        } else if (!::strcmp(key, "torque")) {
                push_cfunction(L, rbody_torque);
        } else if (!::strcmp(key, "torqueImpulse")) {
                push_cfunction(L, rbody_torque_impulse);

        } else if (!::strcmp(key, "procObjMaterials")) {
                std::vector<int> mats;
                self.colMesh->getProcObjMaterials(mats);
                lua_createtable(L, mats.size(), 0);
                for (size_t j=0 ; j<mats.size(); ++j) {
                        lua_pushstring(L, phys_mats.getMaterial(mats[j])->name.c_str());
                        lua_rawseti(L, -2, j+1);
                }
        } else if (!::strcmp(key, "scatter")) {
                push_cfunction(L, rbody_scatter);
        } else if (!::strcmp(key, "rangedScatter")) {
                push_cfunction(L, rbody_ranged_scatter);

        } else if (!::strcmp(key, "activate")) {
                push_cfunction(L, rbody_activate);
        } else if (!::strcmp(key, "deactivate")) {
                push_cfunction(L, rbody_deactivate);
        } else if (!::strcmp(key, "destroy")) {
                push_cfunction(L, rbody_destroy);

        } else if (!::strcmp(key, "linearSleepThreshold")) {
                lua_pushnumber(L, self.getLinearSleepThreshold());
        } else if (!::strcmp(key, "angularSleepThreshold")) {
                lua_pushnumber(L, self.getAngularSleepThreshold());

        } else if (!::strcmp(key, "worldPosition")) {
                push_v3(L, self.getPosition());
        } else if (!::strcmp(key, "worldOrientation")) {
                push_quat(L, self.getOrientation());

        } else if (!::strcmp(key, "localToWorld")) {
                push_cfunction(L, rbody_local_to_world);
        } else if (!::strcmp(key, "worldToLocal")) {
                push_cfunction(L, rbody_world_to_local);

        } else if (!::strcmp(key, "linearVelocity")) {
                push_v3(L, self.getLinearVelocity());
        } else if (!::strcmp(key, "angularVelocity")) {
                push_v3(L, self.getAngularVelocity());
        } else if (!::strcmp(key, "getLocalVelocity")) {
                push_cfunction(L, rbody_local_vel);

        } else if (!::strcmp(key, "contactProcessingThreshold")) {
                lua_pushnumber(L, self.getContactProcessingThreshold());

        } else if (!::strcmp(key, "linearDamping")) {
                lua_pushnumber(L, self.getLinearDamping());
        } else if (!::strcmp(key, "angularDamping")) {
                lua_pushnumber(L, self.getAngularDamping());

        } else if (!::strcmp(key, "mass")) {
                lua_pushnumber(L, self.getMass());
        } else if (!::strcmp(key, "inertia")) {
                push_v3(L, self.getInertia());
        } else if (!::strcmp(key, "ghost")) {
                lua_pushboolean(L, self.getGhost());

        } else if (!::strcmp(key, "numParts")) {
                lua_pushnumber(L, self.getNumElements());
        } else if (!::strcmp(key, "getPartEnabled")) {
                push_cfunction(L, rbody_get_part_enabled);
        } else if (!::strcmp(key, "setPartEnabled")) {
                push_cfunction(L, rbody_set_part_enabled);
        } else if (!::strcmp(key, "getPartPositionInitial")) {
                push_cfunction(L, rbody_get_part_position_initial);
        } else if (!::strcmp(key, "getPartPositionOffset")) {
                push_cfunction(L, rbody_get_part_position_offset);
        } else if (!::strcmp(key, "setPartPositionOffset")) {
                push_cfunction(L, rbody_set_part_position_offset);
        } else if (!::strcmp(key, "getPartOrientationInitial")) {
                push_cfunction(L, rbody_get_part_orientation_initial);
        } else if (!::strcmp(key, "getPartOrientationOffset")) {
                push_cfunction(L, rbody_get_part_orientation_offset);
        } else if (!::strcmp(key, "setPartOrientationOffset")) {
                push_cfunction(L, rbody_set_part_orientation_offset);

        } else if (!::strcmp(key, "meshName")) {
                push_string(L, self.colMesh->getName());

        } else if (!::strcmp(key, "owner")) {
                if (self.owner.isNull()) {
                        lua_pushnil(L);
                } else {
                        push_gritobj(L, self.owner);
                }

        } else if (!::strcmp(key, "updateCallback")) {
                self.updateCallbackPtr.push(L);
        } else if (!::strcmp(key, "stepCallback")) {
                self.stepCallbackPtr.push(L);
        } else if (!::strcmp(key, "collisionCallback")) {
                self.collisionCallbackPtr.push(L);
        } else if (!::strcmp(key, "stabiliseCallback")) {
                self.stabiliseCallbackPtr.push(L);
        } else {
                my_lua_error(L, "Not a readable RigidBody member: "+std::string(key));
        }
        return 1;
TRY_END
}
예제 #5
0
bool GritObject::deactivate (lua_State *L, const GritObjectPtr &self)
{
    if (gritClass==NULL) GRIT_EXCEPT("Object destroyed");

    if (!isActivated()) return false;

    bool killme = false;

    streamer_unlist_as_activated(self);

    STACK_BASE;
    //stack is empty

    push_cfunction(L, my_lua_error_handler);
    int error_handler = lua_gettop(L);

    //stack: err

    //stack: err
    getField(L, "deactivate");
    //stack: err, callback
    if (lua_isnil(L, -1)) {
        lua_pop(L, 2);
        //stack is empty
        CERR << "deactivating object: \""<<name<<"\": "
             << "class \""<<gritClass->name<<"\" "
             << "does not have deactivate function" << std::endl;
        luaL_unref(L, LUA_REGISTRYINDEX, lua);
        lua = LUA_NOREF;

        STACK_CHECK;
        // returning true indicates the object will be erased
        // to prevent the error reoccuring
        return true;
    }

    //stack: err, callback
    // Make the call.

    push_gritobj(L, self); // persistent grit obj
    //stack: err, callback, self
    int status = lua_pcall(L, 1, 1, error_handler);
    if (status) {
        //stack: err, msg
        // pop the error message since the error handler will
        // have already printed it out
        lua_pop(L, 1);
        killme = true;
        //stack: err
    } else {
        //stack: err, killme
        killme = 0 != lua_toboolean(L, -1);
        lua_pop(L, 1);
        //stack: err
    }
    //stack: err

    luaL_unref(L, LUA_REGISTRYINDEX, lua);
    lua = LUA_NOREF;

    lua_pop(L, 1);
    //stack is empty
    STACK_CHECK;

    return killme;
}
예제 #6
0
void GritObject::activate (lua_State *L,
                           const GritObjectPtr &self)
{
    if (isActivated()) return;

    // can call in from lua after destroyed by deleteObject
    if (gritClass==NULL) GRIT_EXCEPT("Object destroyed");

    if (!demand.loaded()) {
        // If it's not loaded yet then we must have been activated explicitly
        // i.e. not via the streamer, which waits until the demand is loaded.
        // Since it's an explicit activation, we better make sure it will work.
        try {
            demand.immediateLoad();
        } catch (Exception &e) {
            CERR << e << std::endl;
            CERR << "Object: \"" << name << "\" raised an error on activation, so destroying it."
                 << std::endl;
            // will deactivate us
            object_del(L, self);
            return;
        }

    }

    STACK_BASE;
    //stack is empty

    // error handler in case there is a problem during 
    // the callback
    push_cfunction(L, my_lua_error_handler);
    int error_handler = lua_gettop(L);

    //stack: err

    //stack: err
    getField(L, "activate");
    //stack: err, callback
    if (lua_isnil(L, -1)) {
        // don't activate it as class does not have activate function
        // pop both the error handler and the nil activate function
        // and the table
        lua_pop(L, 3);
        CERR << "activating object: \""<<name<<"\": "
             << "class \""<<gritClass->name<<"\" "
             << "does not have activate function" << std::endl;
        object_del(L, self);
        STACK_CHECK;
        return;
    }

    //stack: err, callback
    STACK_CHECK_N(2);

    // Call activate callback:

    // push 4 args
    lua_checkstack(L, 5);
    //stack: err, callback
    push_gritobj(L, self); // persistent
    //stack: err, callback, persistent
    lua_newtable(L); // instance
    //stack: err, callback, persistent, instance
    lua_pushvalue(L, -1);
    //stack: err, callback, persistent, instance, instance
    lua = luaL_ref(L, LUA_REGISTRYINDEX); // set up the lua ref to the new instance
    //stack: err, callback, persistent, instance
    STACK_CHECK_N(4);

    // call (2 args), pops function too
    int status = lua_pcall(L, 2, 0, error_handler);
    if (status) {
        STACK_CHECK_N(2);
        //stack: err, error
        // pop the error message since the error handler will
        // have already printed it out
        lua_pop(L, 1);
        CERR << "Object: \"" << name << "\" raised an error on activation, so destroying it."
             << std::endl;
        // will deactivate us
        object_del(L, self);
        //stack: err
        STACK_CHECK_N(1);
    } else {
        STACK_CHECK_N(1);
        //stack: err
        streamer_list_as_activated(self);
        lastFade = -1;
    }
    //stack: err

    STACK_CHECK_N(1);
    lua_pop(L, 1);
    //stack is empty
    STACK_CHECK;
}
static int gritobj_index (lua_State *L)
{
TRY_START
        check_args(L,2);
        GET_UD_MACRO(GritObjectPtr,self,1,GRITOBJ_TAG);
        std::string key = check_string(L,2);
        if (key=="destroy") {
                push_cfunction(L,gritobj_destroy);
        } else if (key=="activated") {
                lua_pushboolean(L,self->isActivated());
        } else if (key=="near") {
                push_gritobj(L,self->getNearObj());
        } else if (key=="far") {
                push_gritobj(L,self->getFarObj());
        } else if (key=="fade") {
                lua_pushnumber(L,self->getFade());
        } else if (key=="updateSphere") {
                push_cfunction(L,gritobj_update_sphere);
        } else if (key=="pos") {
                push_v3(L, self->getPos());
        } else if (key=="renderingDistance") {
                lua_pushnumber(L, self->getR());
        } else if (key=="deactivate") {
                push_cfunction(L,gritobj_deactivate);
        } else if (key=="activate") {
                push_cfunction(L,gritobj_activate);
        } else if (key=="instance") {
                self->pushLuaTable(L);
        } else if (key=="addDiskResource") {
                push_cfunction(L,gritobj_add_disk_resource);
        } else if (key=="reloadDiskResources") {
                push_cfunction(L,gritobj_reload_disk_resource);
/*
        } else if (key=="getAdvancePrepareHints") {
                push_cfunction(L,gritobj_get_advance_prepare_hints);
*/
        } else if (key=="destroyed") {
                lua_pushboolean(L,self->getClass()==NULL);
        } else if (key=="class") {
                GritClass *c = self->getClass();
                if (c==NULL) my_lua_error(L,"GritObject destroyed");
                push_gritcls(L,c);
        } else if (key=="className") {
                GritClass *c = self->getClass();
                if (c==NULL) my_lua_error(L,"GritObject destroyed");
                lua_pushstring(L,c->name.c_str());
        } else if (key=="name") {
                lua_pushstring(L,self->name.c_str());
        } else if (key=="needsFrameCallbacks") {
                lua_pushboolean(L,self->getNeedsFrameCallbacks());
        } else if (key=="needsStepCallbacks") {
                lua_pushboolean(L,self->getNeedsStepCallbacks());
        } else if (key=="dump") {
                self->userValues.dump(L);
        } else {
                GritClass *c = self->getClass();
                if (c==NULL) my_lua_error(L,"GritObject destroyed");
                const char *err = self->userValues.luaGet(L);
                if (err) my_lua_error(L, err);
                if (!lua_isnil(L,-1)) return 1;
                lua_pop(L,1);
                // try class instead
                c->get(L,key);
        }
        return 1;
TRY_END
}