added ag::pickOne()

git-svn-id: svn://anubis/anaglym/trunk@310 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2010-09-17 14:46:54 +00:00
parent a43935fdd6
commit bfb0760ef1
2 changed files with 111 additions and 52 deletions

162
ag.cc
View File

@ -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<Engine::PickedObjectRef> > objects =
g_engine->pickObjects(lua_tointeger(L, 1), lua_tointeger(L, 2));
for (vector<Engine::PickedObjectRef>::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<Engine::PickedObjectRef> > objects =
g_engine->pickObjects(lua_tointeger(L, 1), lua_tointeger(L, 2));
for (vector<Engine::PickedObjectRef>::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;
}

1
ag.h
View File

@ -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);