The Battle for Wesnoth  1.15.12+dev
lua_map_location_ops.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
16 #include "scripting/lua_common.hpp"
17 #include "scripting/push_check.hpp"
18 
19 #include "map/location.hpp"
20 #include "pathutils.hpp"
21 
22 #include <string>
23 #include <utility>
24 #include <ciso646> // for and
25 
26 #include "lua/lua.h"
27 #include "lua/lauxlib.h"
28 
29 namespace lua_map_location {
30 
31 /**
32  * Expose map_location::get_direction function to lua
33  * Arg 1: a location
34  * Arg 2: a direction
35  * Arg 3: (optional) number of steps
36  */
38 {
39  map_location l;
40  if(!luaW_tolocation(L, 1, l)) {
41  return luaL_argerror(L, 1, "get_direction: first argument(S) must be a location");
42  }
43  int nargs = lua_gettop(L);
44  if (nargs < 2) {
45  luaL_error(L, "get_direction: not missing direction argument");
46  return 0;
47  }
48 
49  int n = 1;
50  if (nargs == 3) {
51  n = luaL_checkinteger(L, -1);
52  lua_pop(L,1);
53  }
54 
56  if (lua_isstring(L, -1)) {
58  lua_pop(L,1);
59  } else {
60  std::string msg("get_direction: second argument should be a direction string, instead found a ");
61  msg += lua_typename(L, lua_type(L, -1));
62  return luaL_argerror(L, -1, msg.c_str());
63  }
64 
65  map_location result = l.get_direction(d, n);
66  luaW_pushlocation(L, result);
67  return 1;
68 }
69 
70 /**
71  * Expose map_location::vector_sum to lua
72  */
74 {
75  map_location l1, l2;
76  if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) {
77  lua_pushstring(L, "vector_sum: requires two locations");
78  return lua_error(L);
79  }
80 
82  return 1;
83 }
84 
85 /**
86  * Expose map_location::vector_difference to lua
87  */
89 {
90  map_location l1, l2;
91  if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) {
92  lua_pushstring(L, "vector_diff: requires two locations");
93  return lua_error(L);
94  }
95 
97  return 1;
98 }
99 
100 /**
101  * Expose map_location::vector_negation to lua
102  * - Arg 1: Location
103  * - Ret: Negated vector
104  */
106 {
107  map_location l1;
108  if(!luaW_tolocation(L, 1, l1)) {
109  return luaL_argerror(L, 1, "expected a location");
110  }
111 
113  return 1;
114 }
115 
116 /**
117  * Expose map_location::rotate_right_around_center to lua
118  */
120 {
121  int k = luaL_checkinteger(L, -1);
122  lua_pop(L,1);
123  map_location center, loc;
124  if(!luaW_tolocation(L, 1, loc) || !luaW_tolocation(L, 2, center)) {
125  lua_pushstring(L, "rotate_right_around_center: requires two locations");
126  return lua_error(L);
127  }
128 
130  return 1;
131 }
132 
133 /**
134  * Expose map_location tiles_adjacent
135  * - Args 1, 2: Two locations
136  * - Ret: True if the locations are adjacent
137  */
139 {
140  map_location l1, l2;
141  if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) {
142  lua_pushstring(L, "tiles_adjacent: requires two locations");
143  return lua_error(L);
144  }
145 
146  lua_pushboolean(L, tiles_adjacent(l1,l2));
147  return 1;
148 }
149 
150 /**
151  * Expose map_location get_adjacent_tiles
152  * - Arg 1: A location
153  * - Ret 1 - 6: The adjacent locations
154  */
156 {
157  map_location l1;
158  if(!luaW_tolocation(L, 1, l1)) {
159  return luaL_argerror(L, 1, "expected a location");
160  }
161 
162  for(const map_location& adj : get_adjacent_tiles(l1)) {
163  luaW_pushlocation(L, adj);
164  }
165 
166  return 6;
167 }
168 
169 /**
170  * Expose map_location get_tiles_in_radius
171  * - Arg 1: A location
172  * - Ret: The locations
173  */
175 {
176  map_location l1;
177  if(!luaW_tolocation(L, 1, l1)) {
178  return luaL_argerror(L, 1, "expected a location");
179  }
180  int radius = luaL_checkinteger(L, 2);
181 
182  std::vector<map_location> locs;
183  get_tiles_in_radius(l1, radius, locs);
184  lua_push(L, locs);
185 
186  return 1;
187 }
188 
189 /**
190  * Expose map_location distance_between
191  * - Args 1, 2: Two locations
192  * - Ret: The distance between the two locations
193  */
195 {
196  map_location l1, l2;
197  if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) {
198  lua_pushstring(L, "distance_between: requires two locations");
199  return lua_error(L);
200  }
201 
203  return 1;
204 }
205 
206 /**
207  * Expose map_location get_in_basis_N_NE
208  */
210 {
211  map_location l1;
212  if(!luaW_tolocation(L, 1, l1)) {
213  return luaL_argerror(L, 1, "expected a location");
214  }
215 
216  std::pair<int, int> r = l1.get_in_basis_N_NE();
217  lua_pushinteger(L, r.first);
218  lua_pushinteger(L, r.second);
219  return 2;
220 }
221 
222 /**
223  * Expose map_location get_relative_dir
224  * - Args 1, 2: Two locations
225  * - Ret: The direction of location 2 from location 1
226  */
228 {
229  map_location l1, l2;
230  if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) {
231  lua_pushstring(L, "get_relative_dir: requires two locations");
232  return lua_error(L);
233  }
234 
235  const std::string dir = map_location::write_direction(l1.get_relative_dir(l2));
236  lua_pushlstring(L, dir.c_str(), dir.length());
237  return 1;
238 }
239 
240 } // end namespace lua_map_location
map_location & vector_sum_assign(const map_location &a)
Definition: location.hpp:120
static DIRECTION parse_direction(const std::string &str)
Definition: location.cpp:65
map_location & vector_difference_assign(const map_location &a)
Definition: location.hpp:128
void get_adjacent_tiles(const map_location &a, map_location *res)
Function which, given a location, will place all adjacent locations in res.
Definition: location.cpp:474
DIRECTION get_relative_dir(const map_location &loc, map_location::RELATIVE_DIR_MODE mode) const
Definition: location.cpp:226
LUA_API int lua_type(lua_State *L, int idx)
Definition: lapi.cpp:260
std::pair< int, int > get_in_basis_N_NE() const
Definition: location.cpp:287
LUA_API void lua_pushboolean(lua_State *L, int b)
Definition: lapi.cpp:581
void get_tiles_in_radius(const map_location &center, const int radius, std::vector< map_location > &result)
Function that will add to result all locations within radius tiles of center (excluding center itself...
Definition: pathutils.cpp:55
int intf_get_direction(lua_State *L)
Expose map_location::get_direction function to lua Arg 1: a location Arg 2: a direction Arg 3: (optio...
LUA_API int lua_gettop(lua_State *L)
Definition: lapi.cpp:168
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
map_location vector_negation() const
Definition: location.hpp:110
#define d
void lua_push(lua_State *L, const T &val)
Definition: push_check.hpp:384
int intf_vector_diff(lua_State *L)
Expose map_location::vector_difference to lua.
int intf_distance_between(lua_State *L)
Expose map_location distance_between.
#define lua_pop(L, n)
Definition: lua.h:364
map_location get_direction(DIRECTION dir, unsigned int n=1u) const
Definition: location.cpp:359
int intf_rotate_right_around_center(lua_State *L)
Expose map_location::rotate_right_around_center to lua.
int intf_get_relative_dir(lua_State *L)
Expose map_location get_relative_dir.
LUALIB_API int luaL_argerror(lua_State *L, int arg, const char *extramsg)
Definition: lauxlib.cpp:175
LUA_API int lua_isstring(lua_State *L, int idx)
Definition: lapi.cpp:292
map_location rotate_right_around_center(const map_location &center, int k) const
Definition: location.cpp:306
LUA_API const char * lua_pushlstring(lua_State *L, const char *s, size_t len)
Definition: lapi.cpp:502
int intf_tiles_adjacent(lua_State *L)
Expose map_location tiles_adjacent.
Encapsulates the map of the game.
Definition: location.hpp:37
int intf_vector_negation(lua_State *L)
Expose map_location::vector_negation to lua.
bool tiles_adjacent(const map_location &a, const map_location &b)
Function which tells if two locations are adjacent.
Definition: location.cpp:502
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int arg)
Definition: lauxlib.cpp:442
int intf_get_in_basis_N_NE(lua_State *L)
Expose map_location get_in_basis_N_NE.
DIRECTION
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:39
int intf_get_adjacent_tiles(lua_State *L)
Expose map_location get_adjacent_tiles.
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
Definition: lauxlib.cpp:234
int intf_vector_sum(lua_State *L)
Expose map_location::vector_sum to lua.
std::size_t distance_between(const map_location &a, const map_location &b)
Function which gives the number of hexes between two tiles (i.e.
Definition: location.cpp:545
LUA_API int lua_error(lua_State *L)
Definition: lapi.cpp:1205
void luaW_pushlocation(lua_State *L, const map_location &ml)
Converts a map location object to a Lua table pushed at the top of the stack.
Definition: lua_common.cpp:665
int intf_get_tiles_in_radius(lua_State *L)
Expose map_location get_tiles_in_radius.
static map_location::DIRECTION n
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:489
static std::string write_direction(DIRECTION dir)
Definition: location.cpp:140
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:514
bool luaW_tolocation(lua_State *L, int index, map_location &loc)
Converts an optional table or pair of integers to a map location object.
Definition: lua_common.cpp:676
LUA_API const char * lua_typename(lua_State *L, int t)
Definition: lapi.cpp:266
#define luaL_checkstring(L, n)
Definition: lauxlib.h:138