/** * Expose map_location::vector_sum to lua */ int intf_vector_sum(lua_State* L) { map_location l1, l2; if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) { lua_pushstring(L, "vector_sum: requires two locations"); return lua_error(L); } luaW_pushlocation(L, l1.vector_sum_assign(l2)); return 1; }
/** * Expose map_location get_relative_dir */ int intf_get_relative_dir(lua_State* L) { map_location l1, l2; if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) { lua_pushstring(L, "get_relative_dir: requires two locations"); return lua_error(L); } lua_pushinteger(L, l1.get_relative_dir(l2)); return 1; }
/** * Expose map_location distance_between */ int intf_distance_between(lua_State* L) { map_location l1, l2; if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) { lua_pushstring(L, "distance_between: requires two locations"); return lua_error(L); } lua_pushinteger(L, distance_between(l1,l2)); return 1; }
/** * Expose map_location tiles_adjacent */ int intf_tiles_adjacent(lua_State* L) { map_location l1, l2; if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) { lua_pushstring(L, "vector_sum: requires two locations"); return lua_error(L); } lua_pushboolean(L, tiles_adjacent(l1,l2)); return 1; }
/** * Expose map_location::rotate_right_around_center to lua */ int intf_rotate_right_around_center(lua_State* L) { int k = luaL_checkint(L, -1); lua_pop(L,1); map_location center, loc; if(!luaW_tolocation(L, 1, loc) || !luaW_tolocation(L, 2, center)) { lua_pushstring(L, "rotate_right_around_center: requires two locations"); return lua_error(L); } luaW_pushlocation(L, loc.rotate_right_around_center(center, k)); return 1; }
map_location luaW_checklocation(lua_State *L, int index) { map_location result; if (!luaW_tolocation(L, index, result)) luaW_type_error(L, index, "location"); return result; }
/** * Expose map_location::vector_negation to lua */ int intf_vector_negation(lua_State* L) { map_location l1; if(!luaW_tolocation(L, 1, l1)) { return luaL_argerror(L, 1, "expected a location"); } luaW_pushlocation(L, l1.vector_negation()); return 1; }
/** * Expose map_location get_in_basis_N_NE */ int intf_get_in_basis_N_NE(lua_State* L) { map_location l1; if(!luaW_tolocation(L, 1, l1)) { return luaL_argerror(L, 1, "expected a location"); } std::pair<int, int> r = l1.get_in_basis_N_NE(); lua_pushinteger(L, r.first); lua_pushinteger(L, r.second); return 2; }
/** * Expose map_location get_adjacent_tiles */ int intf_get_adjacent_tiles(lua_State* L) { map_location l1; if(!luaW_tolocation(L, 1, l1)) { return luaL_argerror(L, 1, "expected a location"); } map_location locs[6]; get_adjacent_tiles(l1, locs); for (int i = 0; i < 6; ++i) { luaW_pushlocation(L, locs[i]); } return 6; }
/** * Expose map_location::get_direction function to lua * Arg 1: a location * Arg 2: a direction * Arg 3: number of steps */ int intf_get_direction(lua_State* L) { map_location l; if(!luaW_tolocation(L, 1, l)) { return luaL_argerror(L, 1, "get_direction: first argument(S) must be a location"); } int nargs = lua_gettop(L); if (nargs != 2 and nargs != 3) { std::string msg("get_direction: must pass 2 or 3 args, found "); msg += std::to_string(nargs); luaL_error(L, msg.c_str()); return 0; } int n = 1; if (nargs == 3) { n = luaL_checkint(L, -1); lua_pop(L,1); } map_location::DIRECTION d; if (lua_isnumber(L, -1)) { d = map_location::rotate_right(map_location::NORTH, luaL_checkint(L, -1)); //easiest way to correctly convert int to direction lua_pop(L,1); } else if (lua_isstring(L, -1)) { d = map_location::parse_direction(luaL_checkstring(L,-1)); lua_pop(L,1); } else { std::string msg("get_direction: second argument should be a direction, either a string or an integer, instead found a "); msg += lua_typename(L, lua_type(L, -1)); return luaL_argerror(L, -1, msg.c_str()); } map_location result = l.get_direction(d, n); luaW_pushlocation(L, result); return 1; }