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 object_all_del (lua_State *L) { GObjMap m = objs; for (GObjMap::iterator i=m.begin(), i_=m.end() ; i != i_ ; ++i) { object_del(L, i->second); } }
GritObjectPtr object_add (lua_State *L, std::string name, GritClass *grit_class) { bool anonymous = false; if (name=="") { anonymous = true; do { std::stringstream ss; ss << "Unnamed:" << grit_class->name << ":" << name_generation_counter++; name = ss.str(); } while (objs.find(name) != objs.end()); } GObjMap::iterator i = objs.find(name); if (i != objs.end()) { object_del(L, i->second); } GritObjectPtr self = GritObjectPtr(new GritObject(name, grit_class)); self->anonymous = anonymous; objs[name] = self; streamer_list(self); return self; }
// destroy every object in the list, including the header void list_destroy(){ Object *p, *o; for(p = &object_head; (p != NULL); p = p->next) { } while(p != NULL){ p = object_del(p); } }
static int gritobj_destroy (lua_State *L) { TRY_START check_args(L,1); GET_UD_MACRO(GritObjectPtr,self,1,GRITOBJ_TAG); object_del(L,self); return 0; TRY_END }
// reset the main variables, delete the enemies... void game_reset(){ Object *p; PLAYER_COUNT = 0; SCORE = 0; for(p = &object_head; (p != NULL); p = p->next) { if(enemy_all(p->type)){ p = object_del(p); } } }
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; }
static void frt_bv_free(void *p) { object_del(p); bv_destroy((BitVector *)p); }
//Collide players with enemies and enemies with bullets void object_colision(bool* playHit) { Object *p; Object *ob; int x, y; int top, bottom, left, right; int bullet_life; for(p = &object_head; (p != NULL); p = p->next) { test: switch(p->type) { case player: case player2: if (p->y <= 0) { p->y = 0; //keys[KEY_UP] = false; } else if (p->y >= DISPLAY_H - p->height) { p->y = DISPLAY_H - p->height; //keys[KEY_DOWN] = false; } if (p->x <= 25) { p->x = 25; //keys[KEY_LEFT] = false; } else if (p->x >= DISPLAY_W - p->width- 25) { p->x = DISPLAY_W - p->width -25; //keys[KEY_RIGHT] = false; } if (p->life <= 0) { p = object_del(p); } break; case bullet: if ((p->y < 0) || p->y > DISPLAY_H + p->height) { p = object_del(p); goto test; } for(ob = &object_head; (ob != NULL); ob = ob->next) { if (enemy_all(ob->type)) { top = (p->y > ob->y) ? p->y : ob->y; bottom = (p->y + p->height < ob->y + p->height) ? p->y + p->height : ob->y + ob->height; left = (p->x > ob->x) ? p->x : ob->x; right = (p->x + p->width < ob->x + ob->width) ? p->x + p->width : ob->x + ob->width; for(x = left; x < right; x++) { for(y = top; y < bottom; y++) { if ((masks[p->type][p->img_i])->bits[(int) (x - p->x)][(int) (y - p->y)] == 1 && (masks[ob->type][ob->img_i])->bits[(int) (x - ob->x)][(int) (y - ob->y)] == 1) { bullet_life = p->life; bullet_life += ob->life; ob->life += p->life; p->life = bullet_life; if (ob->life < 0) { ob = object_del(ob); SCORE++; } if (p->life >= 0) { p = object_del(p); } goto test; } } } } } break; case_enemy_all if(p->y > DISPLAY_H + 50) { p = object_del(p); goto test; } for(ob = &object_head; (ob != NULL); ob = ob->next) { if ((ob->type == player) || (ob->type == player2)) { top = (p->y > ob->y) ? p->y : ob->y; bottom = (p->y + p->height < ob->y + p->height) ? p->y + p->height : ob->y + ob->height; left = (p->x > ob->x) ? p->x : ob->x; right = (p->x + p->width < ob->x + ob->width) ? p->x + p->width : ob->x + ob->width; for(x = left; x < right; x++) { for(y = top; y < bottom; y++) { if ((masks[p->type][p->img_i])->bits[(int) (x - p->x)][(int) (y - p->y)] == 1 && (masks[ob->type][ob->img_i])->bits[(int) (x - ob->x)][(int) (y - ob->y)] == 1) { p = object_del(p); ob->life += -1; *playHit = true; goto test; } } } } } break; } } }
static void frt_qp_free(void *p) { object_del(p); qp_destroy((QParser *)p); }
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; }