//获取从源到目标的一条路径,如果不能通达返回空表,否则返回一条路径表 int luaGetPath(lua_State *L){ struct battlemap *battlemap = (struct battlemap *)lua_touserdata(L,-1); int to_x = (int)lua_tonumber(L,-2); int to_y = (int)lua_tonumber(L,-3); int from_x = (int)lua_tonumber(L,-4); int from_y = (int)lua_tonumber(L,-5); struct map_node *from = (struct map_node*)get_node_by_xy(battlemap->map,from_x,from_y); struct map_node *to = (struct map_node*)get_node_by_xy(battlemap->map,to_x,to_y); struct path_node *path = find_path(battlemap->astar,from,to); if(!path) { PUSH_NIL(L); return 1; } lua_newtable(L); int i = 1; while(path) { struct node *mnode = (struct node*)path->_map_node; PUSH_TABLE2(L,lua_pushnumber(L,mnode->x),lua_pushnumber(L,mnode->y)); lua_rawseti(L,-2,i++); path = path->parent; } return 1; }
int main(int argc,char **argv) { lua_State *L = luaL_newstate(); luaL_openlibs(L); if (luaL_dofile(L,"node_echo.lua")) { const char * error = lua_tostring(L, -1); lua_pop(L,1); printf("%s\n",error); } CALL_LUA_FUNC1(L,"main",0, PUSH_TABLE2(L,PUSH_STRING(L,argv[1]), PUSH_NUMBER(L,atoi(argv[2]))) ); printf("end here\n"); return 0; }