From bfb0760ef1b1efd52d38531866fd158cd27f2c9e Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 17 Sep 2010 14:46:54 +0000 Subject: [PATCH] added ag::pickOne() git-svn-id: svn://anubis/anaglym/trunk@310 99a6e188-d820-4881-8870-2d33a10e2619 --- ag.cc | 162 +++++++++++++++++++++++++++++++++++++++------------------- ag.h | 1 + 2 files changed, 111 insertions(+), 52 deletions(-) diff --git a/ag.cc b/ag.cc index 7430879..1ccafa3 100644 --- a/ag.cc +++ b/ag.cc @@ -55,6 +55,7 @@ namespace ag { "loadTexture", loadTexture }, { "next", next }, { "pickObjects", pickObjects }, + { "pickOne", pickOne }, { "print", print }, { "println", println }, { "registerEventHandler", registerEventHandler }, @@ -93,61 +94,118 @@ namespace ag int pickObjects(lua_State * L) { int argc = lua_gettop(L); - if (argc == 2 && lua_isnumber(L, 1) && lua_isnumber(L, 2)) - { - lua_newtable(L); - int next_index = 1; - refptr< vector > objects = - g_engine->pickObjects(lua_tointeger(L, 1), lua_tointeger(L, 2)); - for (vector::const_iterator it - = objects->begin(); - it != objects->end(); - it++) - { - lua_getfield(L, LUA_GLOBALSINDEX, "ag");/* {ag} */ - lua_getfield(L, -1, "_objects"); /* {ag,_objects} */ - lua_pushinteger(L, (*it)->id); /* {ag,_objects,id} */ - lua_gettable(L, -2); /* {ag,_objects,obj} */ - lua_remove(L, -3); /* {_objects,obj} */ - lua_remove(L, -2); /* {obj} */ - if (!lua_isnil(L, -1)) - { - lua_pushinteger(L, next_index++); /* {obj,index} */ - lua_pushvalue(L, -2); /* {obj,index,obj} */ - lua_settable(L, argc + 1); /* {obj} */ - } - lua_pushstring(L, "pick_dist"); /* {obj,"pick_dist"} */ - lua_pushnumber(L, (*it)->dist); /* {obj,"pick_dist",dist} */ - lua_settable(L, -3); /* {obj} */ - lua_createtable(L, 3, 0); /* {obj,arr} */ - for (int i = 0; i < 3; i++) - { - lua_pushinteger(L, i + 1); /* {obj,arr,index} */ - lua_pushnumber(L, (*it)->pos[i]);/* {obj,arr,index,pos_x} */ - lua_settable(L, -3); /* {obj,arr} */ - } - lua_pushstring(L, "pick_pos"); /* {obj,arr,"pick_pos"} */ - lua_pushvalue(L, -2); /* {obj,arr,"pick_pos",arr} */ - lua_settable(L, -4); /* {obj,arr} */ - lua_pop(L, 1); /* {obj} */ - lua_createtable(L, 3, 0); /* {obj,arr} */ - for (int i = 0; i < 3; i++) - { - lua_pushinteger(L, i + 1); /* {obj,arr,index} */ - lua_pushnumber(L, (*it)->normal[i]);/* {obj,arr,index,n} */ - lua_settable(L, -3); /* {obj,arr} */ - } - lua_pushstring(L, "pick_normal"); /* {obj,arr,"pick_normal"} */ - lua_pushvalue(L, -2); /* {obj,arr,"pick_normal",arr} */ - lua_settable(L, -4); /* {obj,arr} */ - lua_pop(L, 1); /* {obj} */ - lua_pop(L, 1); /* {} */ - } - } - else + if (!(argc == 2 && lua_isnumber(L, 1) && lua_isnumber(L, 2))) { lua_pushnil(L); + return 1; } + lua_newtable(L); + int next_index = 1; + refptr< vector > objects = + g_engine->pickObjects(lua_tointeger(L, 1), lua_tointeger(L, 2)); + for (vector::const_iterator it + = objects->begin(); + it != objects->end(); + it++) + { + lua_getfield(L, LUA_GLOBALSINDEX, "ag");/* {ag} */ + lua_getfield(L, -1, "_objects"); /* {ag,_objects} */ + lua_pushinteger(L, (*it)->id); /* {ag,_objects,id} */ + lua_gettable(L, -2); /* {ag,_objects,obj} */ + lua_remove(L, -3); /* {_objects,obj} */ + lua_remove(L, -2); /* {obj} */ + + lua_pushinteger(L, next_index++); /* {obj,index} */ + lua_pushvalue(L, -2); /* {obj,index,obj} */ + lua_settable(L, argc + 1); /* {obj} */ + + lua_pushstring(L, "pick_dist"); /* {obj,"pick_dist"} */ + lua_pushnumber(L, (*it)->dist); /* {obj,"pick_dist",dist} */ + lua_settable(L, -3); /* {obj} */ + lua_createtable(L, 3, 0); /* {obj,arr} */ + for (int i = 0; i < 3; i++) + { + lua_pushinteger(L, i + 1); /* {obj,arr,index} */ + lua_pushnumber(L, (*it)->pos[i]); /* {obj,arr,index,pos_x} */ + lua_settable(L, -3); /* {obj,arr} */ + } + lua_pushstring(L, "pick_pos"); /* {obj,arr,"pick_pos"} */ + lua_pushvalue(L, -2); /* {obj,arr,"pick_pos",arr} */ + lua_settable(L, -4); /* {obj,arr} */ + lua_pop(L, 1); /* {obj} */ + lua_createtable(L, 3, 0); /* {obj,arr} */ + for (int i = 0; i < 3; i++) + { + lua_pushinteger(L, i + 1); /* {obj,arr,index} */ + lua_pushnumber(L, (*it)->normal[i]);/* {obj,arr,index,n} */ + lua_settable(L, -3); /* {obj,arr} */ + } + lua_pushstring(L, "pick_normal"); /* {obj,arr,"pick_normal"} */ + lua_pushvalue(L, -2); /* {obj,arr,"pick_normal",arr} */ + lua_settable(L, -4); /* {obj,arr} */ + lua_pop(L, 1); /* {obj} */ + lua_pop(L, 1); /* {} */ + } + + return 1; + } + + int pickOne(lua_State * L) + { + int argc = lua_gettop(L); + Engine::Object *obj; + Engine::PickedObjectRef por; + + if (!(argc == 3 + && lua_isnumber(L, 1) + && lua_isnumber(L, 2) + && lua_istable(L, 3))) + goto fail; + obj = object::getObject(L, 3); + if (obj == NULL) + goto fail; + por = g_engine->pickOne(lua_tointeger(L, 1), lua_tointeger(L, 2), obj); + if (por.isNull()) + goto fail; + lua_getfield(L, LUA_GLOBALSINDEX, "ag");/* {ag} */ + lua_getfield(L, -1, "_objects"); /* {ag,_objects} */ + lua_pushinteger(L, por->id); /* {ag,_objects,id} */ + lua_gettable(L, -2); /* {ag,_objects,obj} */ + lua_remove(L, -3); /* {_objects,obj} */ + lua_remove(L, -2); /* {obj} */ + + lua_pushstring(L, "pick_dist"); /* {obj,"pick_dist"} */ + lua_pushnumber(L, por->dist); /* {obj,"pick_dist",dist} */ + lua_settable(L, -3); /* {obj} */ + lua_createtable(L, 3, 0); /* {obj,arr} */ + for (int i = 0; i < 3; i++) + { + lua_pushinteger(L, i + 1); /* {obj,arr,index} */ + lua_pushnumber(L, por->pos[i]); /* {obj,arr,index,pos_x} */ + lua_settable(L, -3); /* {obj,arr} */ + } + lua_pushstring(L, "pick_pos"); /* {obj,arr,"pick_pos"} */ + lua_pushvalue(L, -2); /* {obj,arr,"pick_pos",arr} */ + lua_settable(L, -4); /* {obj,arr} */ + lua_pop(L, 1); /* {obj} */ + lua_createtable(L, 3, 0); /* {obj,arr} */ + for (int i = 0; i < 3; i++) + { + lua_pushinteger(L, i + 1); /* {obj,arr,index} */ + lua_pushnumber(L, por->normal[i]); /* {obj,arr,index,n} */ + lua_settable(L, -3); /* {obj,arr} */ + } + lua_pushstring(L, "pick_normal"); /* {obj,arr,"pick_normal"} */ + lua_pushvalue(L, -2); /* {obj,arr,"pick_normal",arr} */ + lua_settable(L, -4); /* {obj,arr} */ + lua_pop(L, 1); /* {obj} */ + lua_pop(L, 1); /* {} */ + + lua_pushboolean(L, true); + return 1; + +fail: + lua_pushboolean(L, false); return 1; } diff --git a/ag.h b/ag.h index f59e52b..0a08ec1 100644 --- a/ag.h +++ b/ag.h @@ -30,6 +30,7 @@ namespace ag int loadTexture(lua_State * L); int next(lua_State * L); int pickObjects(lua_State * L); + int pickOne(lua_State * L); int print(lua_State * L); int println(lua_State * L); int registerEventHandler(lua_State * L);