static int stringdb_index(lua_State *L) { TRY_START check_args(L,2); GET_UD_MACRO(StringDB,self,1,STRINGDB_TAG); if (lua_type(L,2)==LUA_TNUMBER) { if (self.size()==0) { my_lua_error(L,"Empty stringdb"); } unsigned short index=check_t<unsigned short>(L,2,1,self.size()); lua_pushstring(L,self[index-1].c_str()); } else { std::string key = luaL_checkstring(L,2); if (key=="add") { push_cfunction(L,stringdb_add); } else if (key=="clear") { push_cfunction(L,stringdb_clear); } else if (key=="table") { lua_createtable(L, self.size(), 0); for (unsigned int i=0 ; i<self.size() ; i++) { lua_pushnumber(L,i+1); lua_pushstring(L,self[i].c_str()); lua_settable(L,-3); } } else { my_lua_error(L,"Not a readable StringDB member: "+key); } } return 1; TRY_END }
static int global_physics_sweep_col_mesh (lua_State *L) { TRY_START int base_line = 7; check_args_min(L, base_line); std::string col_mesh_name = check_path(L, 1); DiskResource *col_mesh_ = disk_resource_get_or_make(col_mesh_name); CollisionMesh *col_mesh = dynamic_cast<CollisionMesh*>(col_mesh_); Quaternion q = check_quat(L, 2); Vector3 start = check_v3(L, 3); Vector3 ray = check_v3(L, 4); bool nearest_only = check_bool(L, 5); unsigned long flags = check_t<unsigned long>(L, 6); if (lua_type(L, 7) != LUA_TFUNCTION) my_lua_error(L, "Parameter 5 should be a function."); LuaSweepCallback lcb(nearest_only, flags); init_cast_blacklist(L, base_line, lcb); physics_sweep_col_mesh(start, q, start + ray, q, lcb, col_mesh); push_cfunction(L, my_lua_error_handler); int error_handler = lua_gettop(L); lcb.pushResults(L, 7, error_handler); return 0; TRY_END }
static int global_physics_test (lua_State *L) { TRY_START LuaTestCallback lcb; push_cfunction(L, my_lua_error_handler); int error_handler = lua_gettop(L); if (lua_gettop(L)==5) { float radius = lua_tonumber(L, 1); Vector3 pos = check_v3(L, 2); bool only_dyn = check_bool(L, 3); if (lua_type(L, 4) != LUA_TFUNCTION) my_lua_error(L, "Parameter 4 should be a function."); physics_test_sphere(radius, pos, only_dyn, lcb); lcb.pushResults(L, 4, error_handler); } else { check_args(L, 6); std::string col_mesh_name = check_path(L, 1); DiskResource *col_mesh_ = disk_resource_get_or_make(col_mesh_name); CollisionMesh *col_mesh = dynamic_cast<CollisionMesh*>(col_mesh_); if (col_mesh==NULL) my_lua_error(L, "Not a collision mesh: \""+col_mesh_name+"\""); if (!col_mesh->isLoaded()) my_lua_error(L, "Not loaded: \""+col_mesh_name+"\""); Vector3 pos = check_v3(L, 2); Quaternion quat = check_quat(L, 3); bool only_dyn = check_bool(L, 4); if (lua_type(L, 5) != LUA_TFUNCTION) my_lua_error(L, "Parameter 5 should be a function."); physics_test(col_mesh, pos, quat, only_dyn, lcb); lcb.pushResults(L, 5, error_handler); } lua_pop(L, 1); // error handler return 0; TRY_END }
void InputFilter::triggerMouseMove (lua_State *L, const Vector2 &rel) { ensureAlive(); 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 // get the function mouseMoveCallback.push(L); //stack: err, callback if (lua_isnil(L, -1)) { lua_pop(L, 2); STACK_CHECK; CERR << "InputFilter \"" << description << "\" " << "has no mouseMoveCallback function, releasing mouse." << std::endl; setMouseCapture(L, false); return; } //stack: err, callback STACK_CHECK_N(2); push_v2(L, rel); //stack: err, callback, rel STACK_CHECK_N(3); // call (1 arg), pops function too int status = lua_pcall(L, 1, 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, 2); STACK_CHECK; CERR << "InputFilter \"" << description << "\" " << "raised an error on mouseMoveCallback, disabling mouse capture." << std::endl; setMouseCapture(L, false); //stack is empty } else { //stack: err STACK_CHECK_N(1); lua_pop(L, 1); //stack is empty } //stack is empty STACK_CHECK; }
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; }
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; }
void InputFilter::triggerFunc (lua_State *L, const std::string &bind, const LuaPtr &func) { ensureAlive(); APP_ASSERT(!func.isNil()); 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 // get the function func.push(L); //stack: err, callback STACK_CHECK_N(2); // call (1 arg), pops function too int status = lua_pcall(L, 0, 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, 2); STACK_CHECK; CERR << "InputFilter \"" << description << "\" " << "raised an error on bind "<<bind<<"." << std::endl; //stack is empty } else { //stack: err STACK_CHECK_N(1); lua_pop(L, 1); //stack is empty } //stack is empty STACK_CHECK; }
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 }
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; }
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 }