The Battle for Wesnoth  1.15.0-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 
18 #include "map/location.hpp"
19 
20 #include <string>
21 #include <utility>
22 #include <ciso646> // for and
23 
24 #include "lua/lua.h"
25 #include "lua/lauxlib.h"
26 
27 namespace lua_map_location {
28 
29 /**
30  * Expose map_location::get_direction function to lua
31  * Arg 1: a location
32  * Arg 2: a direction
33  * Arg 3: (optional) number of steps
34  */
36 {
37  map_location l;
38  if(!luaW_tolocation(L, 1, l)) {
39  return luaL_argerror(L, 1, "get_direction: first argument(S) must be a location");
40  }
41  int nargs = lua_gettop(L);
42  if (nargs < 2) {
43  luaL_error(L, "get_direction: not missing direction argument");
44  return 0;
45  }
46 
47  int n = 1;
48  if (nargs == 3) {
49  n = luaL_checkinteger(L, -1);
50  lua_pop(L,1);
51  }
52 
54  if (lua_isstring(L, -1)) {
56  lua_pop(L,1);
57  } else {
58  std::string msg("get_direction: second argument should be a direction string, instead found a ");
59  msg += lua_typename(L, lua_type(L, -1));
60  return luaL_argerror(L, -1, msg.c_str());
61  }
62 
63  map_location result = l.get_direction(d, n);
64  luaW_pushlocation(L, result);
65  return 1;
66 }
67 
68 /**
69  * Expose map_location::vector_sum to lua
70  */
72 {
73  map_location l1, l2;
74  if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) {
75  lua_pushstring(L, "vector_sum: requires two locations");
76  return lua_error(L);
77  }
78 
80  return 1;
81 }
82 
83 /**
84  * Expose map_location::vector_difference to lua
85  */
87 {
88  map_location l1, l2;
89  if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) {
90  lua_pushstring(L, "vector_sum: requires two locations");
91  return lua_error(L);
92  }
93 
95  return 1;
96 }
97 
98 /**
99  * Expose map_location::vector_negation to lua
100  * - Arg 1: Location
101  * - Ret: Negated vector
102  */
104 {
105  map_location l1;
106  if(!luaW_tolocation(L, 1, l1)) {
107  return luaL_argerror(L, 1, "expected a location");
108  }
109 
111  return 1;
112 }
113 
114 /**
115  * Expose map_location::rotate_right_around_center to lua
116  */
118 {
119  int k = luaL_checkinteger(L, -1);
120  lua_pop(L,1);
121  map_location center, loc;
122  if(!luaW_tolocation(L, 1, loc) || !luaW_tolocation(L, 2, center)) {
123  lua_pushstring(L, "rotate_right_around_center: requires two locations");
124  return lua_error(L);
125  }
126 
128  return 1;
129 }
130 
131 /**
132  * Expose map_location tiles_adjacent
133  * - Args 1, 2: Two locations
134  * - Ret: True if the locations are adjacent
135  */
137 {
138  map_location l1, l2;
139  if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) {
140  lua_pushstring(L, "vector_sum: requires two locations");
141  return lua_error(L);
142  }
143 
144  lua_pushboolean(L, tiles_adjacent(l1,l2));
145  return 1;
146 }
147 
148 /**
149  * Expose map_location get_adjacent_tiles
150  * - Arg 1: A location
151  * - Ret 1 - 6: The adjacent locations
152  */
154 {
155  map_location l1;
156  if(!luaW_tolocation(L, 1, l1)) {
157  return luaL_argerror(L, 1, "expected a location");
158  }
159 
160  map_location locs[6];
161  get_adjacent_tiles(l1, locs);
162 
163  for (int i = 0; i < 6; ++i) {
164  luaW_pushlocation(L, locs[i]);
165  }
166 
167  return 6;
168 }
169 
170 /**
171  * Expose map_location distance_between
172  * - Args 1, 2: Two locations
173  * - Ret: The distance between the two locations
174  */
176 {
177  map_location l1, l2;
178  if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) {
179  lua_pushstring(L, "distance_between: requires two locations");
180  return lua_error(L);
181  }
182 
184  return 1;
185 }
186 
187 /**
188  * Expose map_location get_in_basis_N_NE
189  */
191 {
192  map_location l1;
193  if(!luaW_tolocation(L, 1, l1)) {
194  return luaL_argerror(L, 1, "expected a location");
195  }
196 
197  std::pair<int, int> r = l1.get_in_basis_N_NE();
198  lua_pushinteger(L, r.first);
199  lua_pushinteger(L, r.second);
200  return 2;
201 }
202 
203 /**
204  * Expose map_location get_relative_dir
205  * - Args 1, 2: Two locations
206  * - Ret: The direction of location 2 from location 1
207  */
209 {
210  map_location l1, l2;
211  if(!luaW_tolocation(L, 1, l1) || !luaW_tolocation(L, 2, l2)) {
212  lua_pushstring(L, "get_relative_dir: requires two locations");
213  return lua_error(L);
214  }
215 
216  const std::string dir = map_location::write_direction(l1.get_relative_dir(l2));
217  lua_pushlstring(L, dir.c_str(), dir.length());
218  return 1;
219 }
220 
221 } // end namespace lua_map_location
map_location & vector_sum_assign(const map_location &a)
Definition: location.hpp:125
static DIRECTION parse_direction(const std::string &str)
Definition: location.cpp:64
map_location & vector_difference_assign(const map_location &a)
Definition: location.hpp:133
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:517
DIRECTION get_relative_dir(const map_location &loc, map_location::RELATIVE_DIR_MODE mode) const
Definition: location.cpp:225
LUA_API int lua_type(lua_State *L, int idx)
Definition: lapi.cpp:251
std::pair< int, int > get_in_basis_N_NE() const
Definition: location.cpp:286
LUA_API void lua_pushboolean(lua_State *L, int b)
Definition: lapi.cpp:557
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:167
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:108
map_location vector_negation() const
Definition: location.hpp:115
#define d
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:344
map_location get_direction(DIRECTION dir, unsigned int n=1u) const
Definition: location.cpp:402
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:164
LUA_API int lua_isstring(lua_State *L, int idx)
Definition: lapi.cpp:283
map_location rotate_right_around_center(const map_location &center, int k) const
Definition: location.cpp:305
LUA_API const char * lua_pushlstring(lua_State *L, const char *s, size_t len)
Definition: lapi.cpp:479
int intf_tiles_adjacent(lua_State *L)
Expose map_location tiles_adjacent.
Encapsulates the map of the game.
Definition: location.hpp:42
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:557
std::size_t i
Definition: function.cpp:933
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int arg)
Definition: lauxlib.cpp:430
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:44
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:223
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:600
LUA_API int lua_error(lua_State *L)
Definition: lapi.cpp:1114
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:644
static map_location::DIRECTION n
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:466
static std::string write_direction(DIRECTION dir)
Definition: location.cpp:139
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:491
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:655
LUA_API const char * lua_typename(lua_State *L, int t)
Definition: lapi.cpp:257
#define luaL_checkstring(L, n)
Definition: lauxlib.h:124