The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lua_map_location_ops.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 
15 #include "lua_map_location_ops.hpp"
16 #include "lua_common.hpp"
17 
18 #include "map/location.hpp"
19 #include "util.hpp"
20 
21 #include <string>
22 #include <utility>
23 #include <ciso646> // for and
24 
25 #include "lua/lua.h"
26 #include "lua/lauxlib.h"
27 
28 namespace lua_map_location {
29 
30 /**
31  * Expose map_location::get_direction function to lua
32  * Arg 1: a location
33  * Arg 2: a direction
34  * Arg 3: number of steps
35  */
37 {
39  if(!luaW_tolocation(L, 1, l)) {
40  return luaL_argerror(L, 1, "get_direction: first argument(S) must be a location");
41  }
42  int nargs = lua_gettop(L);
43  if (nargs != 2 and nargs != 3) {
44  std::string msg("get_direction: must pass 2 or 3 args, found ");
45  msg += std::to_string(nargs);
46  luaL_error(L, msg.c_str());
47  return 0;
48  }
49 
50  int n = 1;
51  if (nargs == 3) {
52  n = luaL_checkint(L, -1);
53  lua_pop(L,1);
54  }
55 
57  if (lua_isnumber(L, -1)) {
58  d = map_location::rotate_right(map_location::NORTH, luaL_checkint(L, -1)); //easiest way to correctly convert int to direction
59  lua_pop(L,1);
60  } else if (lua_isstring(L, -1)) {
62  lua_pop(L,1);
63  } else {
64  std::string msg("get_direction: second argument should be a direction, either a string or an integer, instead found a ");
65  msg += lua_typename(L, lua_type(L, -1));
66  return luaL_argerror(L, -1, msg.c_str());
67  }
68 
70  luaW_pushlocation(L, result);
71  return 1;
72 }
73 
74 /**
75  * Expose map_location::vector_sum to lua
76  */
78 {
79  map_location l1, l2;
80  if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) {
81  lua_pushstring(L, "vector_sum: requires two locations");
82  return lua_error(L);
83  }
84 
86  return 1;
87 }
88 
89 /**
90  * Expose map_location::vector_negation to lua
91  */
93 {
94  map_location l1;
95  if(!luaW_tolocation(L, 1, l1)) {
96  return luaL_argerror(L, 1, "expected a location");
97  }
98 
100  return 1;
101 }
102 
103 /**
104  * Expose map_location::ZERO to lua
105  */
107 {
109  return 1;
110 }
111 
112 /**
113  * Expose map_location::rotate_right_around_center to lua
114  */
116 {
117  int k = luaL_checkint(L, -1);
118  lua_pop(L,1);
119  map_location center, loc;
120  if(!luaW_tolocation(L, 1, loc) || !luaW_tolocation(L, 2, center)) {
121  lua_pushstring(L, "rotate_right_around_center: requires two locations");
122  return lua_error(L);
123  }
124 
126  return 1;
127 }
128 
129 /**
130  * Expose map_location tiles_adjacent
131  */
133 {
134  map_location l1, l2;
135  if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) {
136  lua_pushstring(L, "vector_sum: requires two locations");
137  return lua_error(L);
138  }
139 
140  lua_pushboolean(L, tiles_adjacent(l1,l2));
141  return 1;
142 }
143 
144 /**
145  * Expose map_location get_adjacent_tiles
146  */
148 {
149  map_location l1;
150  if(!luaW_tolocation(L, 1, l1)) {
151  return luaL_argerror(L, 1, "expected a location");
152  }
153 
154  map_location locs[6];
155  get_adjacent_tiles(l1, locs);
156 
157  for (int i = 0; i < 6; ++i) {
158  luaW_pushlocation(L, locs[i]);
159  }
160 
161  return 6;
162 }
163 
164 /**
165  * Expose map_location distance_between
166  */
168 {
169  map_location l1, l2;
170  if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) {
171  lua_pushstring(L, "distance_between: requires two locations");
172  return lua_error(L);
173  }
174 
176  return 1;
177 }
178 
179 /**
180  * Expose map_location get_in_basis_N_NE
181  */
183 {
184  map_location l1;
185  if(!luaW_tolocation(L, 1, l1)) {
186  return luaL_argerror(L, 1, "expected a location");
187  }
188 
189  std::pair<int, int> r = l1.get_in_basis_N_NE();
190  lua_pushinteger(L, r.first);
191  lua_pushinteger(L, r.second);
192  return 2;
193 }
194 
195 /**
196  * Expose map_location get_relative_dir
197  */
199 {
200  map_location l1, l2;
201  if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) {
202  lua_pushstring(L, "get_relative_dir: requires two locations");
203  return lua_error(L);
204  }
205 
207  return 1;
208 }
209 
210 /**
211  * Expose map_location parse_direction
212  */
214 {
215  std::string str = luaL_checkstring(L, -1);
217  if (d == map_location::NDIRECTIONS) {
218  luaL_argerror(L, -1, "error: not a direction string");
219  return 0;
220  } else {
221  lua_pushinteger(L, d);
222  return 1;
223  }
224 }
225 
226 /**
227  * Expose map_location write_direction
228  */
230 {
231  int d = luaL_checkint(L, -1);
232  if (d >= 0 && d < map_location::NDIRECTIONS) {
233  lua_pushstring(L, map_location::write_direction(static_cast<map_location::DIRECTION>(d)).c_str());
234  return 1;
235  } else {
236  luaL_argerror(L, -1, "error: must be an integer from 0 to 5");
237  return 0;
238  }
239 }
240 
241 } // end namespace lua_map_location
static DIRECTION parse_direction(const std::string &str)
Definition: location.cpp:69
int intf_vector_zero(lua_State *L)
Expose map_location::ZERO to lua.
LUA_API int lua_type(lua_State *L, int idx)
Definition: lapi.cpp:243
LUA_API void lua_pushboolean(lua_State *L, int b)
Definition: lapi.cpp:571
map_location rotate_right_around_center(const map_location &center, int k) const
Definition: location.cpp:300
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.hpp:274
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: number...
LUA_API int lua_gettop(lua_State *L)
Definition: lapi.cpp:154
static const map_location & ZERO()
Old implementation:
Definition: location.hpp:190
DIRECTION get_relative_dir(const map_location &loc, map_location::RELATIVE_DIR_MODE mode) const
Definition: location.cpp:220
#define d
int intf_distance_between(lua_State *L)
Expose map_location distance_between.
#define lua_pop(L, n)
Definition: lua.h:322
GLdouble l
Definition: glew.h:6966
int intf_rotate_right_around_center(lua_State *L)
Expose map_location::rotate_right_around_center to lua.
int intf_parse_direction(lua_State *L)
Expose map_location parse_direction.
int intf_get_relative_dir(lua_State *L)
Expose map_location get_relative_dir.
GLuint64EXT * result
Definition: glew.h:10727
bool tiles_adjacent(const map_location &a, const map_location &b)
Function which tells if two locations are adjacent.
Definition: location.hpp:314
LUA_API int lua_isstring(lua_State *L, int idx)
Definition: lapi.cpp:268
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.hpp:357
Templates and utility-routines for strings and numbers.
int intf_tiles_adjacent(lua_State *L)
Expose map_location tiles_adjacent.
Encapsulates the map of the game.
Definition: location.hpp:38
int intf_vector_negation(lua_State *L)
Expose map_location::vector_negation to lua.
int intf_write_direction(lua_State *L)
Expose map_location write_direction.
static DIRECTION rotate_right(DIRECTION d, unsigned int k=1u)
Inlined bodies.
Definition: location.hpp:155
int intf_get_in_basis_N_NE(lua_State *L)
Expose map_location get_in_basis_N_NE.
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
Definition: lauxlib.cpp:152
size_t i
Definition: function.cpp:1057
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
DIRECTION
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:40
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
int intf_get_adjacent_tiles(lua_State *L)
Expose map_location get_adjacent_tiles.
LUA_API int lua_isnumber(lua_State *L, int idx)
Definition: lapi.cpp:261
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
Definition: lauxlib.cpp:198
map_location & vector_sum_assign(const map_location &a)
Definition: location.hpp:211
int intf_vector_sum(lua_State *L)
Expose map_location::vector_sum to lua.
GLclampd n
Definition: glew.h:5903
#define luaL_checkint(L, n)
Definition: lauxlib.h:117
LUA_API int lua_error(lua_State *L)
Definition: lapi.cpp:1099
std::pair< int, int > get_in_basis_N_NE() const
Definition: location.cpp:281
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:593
map_location get_direction(DIRECTION d, unsigned int n=1u) const
Definition: location.hpp:232
map_location vector_negation() const
Inline vector ops.
Definition: location.hpp:201
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:477
static std::string write_direction(DIRECTION dir)
Definition: location.cpp:144
GLsizei const GLcharARB ** string
Definition: glew.h:4503
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:507
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:606
LUA_API const char * lua_typename(lua_State *L, int t)
Definition: lapi.cpp:249
#define luaL_checkstring(L, n)
Definition: lauxlib.h:115