The Battle for Wesnoth  1.17.0-dev
lua_terrainfilter.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
16 
17 #include "formatter.hpp"
18 #include "global.hpp"
19 #include "log.hpp"
20 #include "map/location.hpp"
21 #include "map/map.hpp"
22 #include "pathutils_impl.hpp"
23 #include "scripting/lua_common.hpp"
24 #include "scripting/push_check.hpp"
27 
28 #include "formula/callable_objects.hpp"
29 #include "formula/formula.hpp"
30 #include "formula/string_utils.hpp"
31 
32 #include <boost/dynamic_bitset.hpp>
33 #include <boost/range/iterator_range.hpp>
34 #include <unordered_map>
35 #include "lua/lauxlib.h"
36 #include "lua/lua.h"
37 
38 static lg::log_domain log_scripting_lua_mapgen("scripting/lua/mapgen");
39 #define LOG_LMG LOG_STREAM(info, log_scripting_lua_mapgen)
40 #define ERR_LMG LOG_STREAM(err, log_scripting_lua_mapgen)
41 //general helper functions for parsing
42 
43 struct invalid_lua_argument : public std::exception
44 {
45  explicit invalid_lua_argument(const std::string& msg) : errormessage_(msg) {}
46  const char* what() const noexcept { return errormessage_.c_str(); }
47 
48 private:
49  std::string errormessage_;
50 };
51 
52 using knows_sets_t = std::map<std::string, std::set<map_location>>;
53 using offset_list_t = std::vector<std::pair<int, int>>;
54 using std::string_view;
55 using dynamic_bitset = boost::dynamic_bitset<>;
56 using location_set = std::set<map_location>;
57 
58 static const char terrinfilterKey[] = "terrainfilter";
59 #define LOG_MATCHES(NAME) \
60 LOG_LMG << #NAME << ":matches(" << l << ") line:" << __LINE__ << "\n";
61 
62 //helper functions for parsing
63 namespace {
64  int atoi(string_view s)
65  {
66  if(s.empty()) {
67  return 0;
68  }
69 
70  char** end = 0;
71  int res = strtol(&s[0], end, 10);
72  return res;
73  }
74 
75  std::pair<int, int> parse_single_range(string_view s)
76  {
77  int dash_pos = s.find('-');
78  if(dash_pos == int(string_view::npos)) {
79  int res = atoi(s);
80  return {res, res};
81  }
82  else {
83  string_view first = s.substr(0, dash_pos);
84  string_view second = s.substr(dash_pos + 1);
85  return {atoi(first), atoi(second)};
86  }
87  }
88 
89  dynamic_bitset parse_range(string_view s)
90  {
91  dynamic_bitset res;
92  utils::split_foreach(s, ',', utils::STRIP_SPACES, [&](string_view part){
93  auto pair = parse_single_range(part);
94  int m = std::max(pair.first, pair.second);
95  if(m >= int(res.size())) {
96  res.resize(m + 1);
97  for(int i = pair.first; i <= pair.second; ++i) {
98  res[i] = true;
99  }
100  }
101  });
102  return res;
103  }
104  void parse_rel(string_view str, offset_list_t& even, offset_list_t& odd)
105  {
106  //sw = 1*s -1*se
107  //nw = -1*se
108  //ne = 1*se - 1*s
109  int s = 0;
110  int se = 0;
111  bool last_was_n = false;
112  while(!str.empty()) {
113  switch(str.front()) {
114  case 'n':
115  --s;
116  last_was_n = true;
117  break;
118  case 's':
119  ++s;
120  last_was_n = false;
121  break;
122  case 'e':
123  ++se;
124  if(!last_was_n) {
125  --s;
126  }
127  break;
128  case 'w':
129  --se;
130  if(last_was_n) {
131  ++s;
132  }
133  break;
134  default:
135  break;
136  }
137  str.remove_prefix(1);
138  }
139  if((se & 2) == 0) {
140  odd.emplace_back(se, s + se/2);
141  even.emplace_back(se, s + se/2);
142  }
143  else {
144  odd.emplace_back(se, s + (se - 1)/2);
145  even.emplace_back(se, s + (se + 1)/2);
146  }
147  }
148 
149  void parse_rel_sequence(string_view s, offset_list_t& even, offset_list_t& odd)
150  {
151  utils::split_foreach(s, ',', utils::STRIP_SPACES, [&](string_view part){
152  parse_rel(part, even, odd);
153  });
154  }
155  /**
156  * TODO: move to a template header.
157  * Function that will add to @a result all elements of @a locs, plus all
158  * on-board locations matching @a pred that are connected to elements of
159  * locs by a chain of at most @a radius tiles, each of which matches @a pred.
160  * @a add_result a function that takes a location_range
161 */
162 
163 } //end namespace
164 
165 static std::set<map_location> luaW_to_locationset(lua_State* L, int index)
166 {
167  std::set<map_location> res;
168  map_location single;
169  if(luaW_tolocation(L, index, single)) {
170  res.insert(single);
171  return res;
172  }
173  lua_pushvalue(L, index);
174  size_t len = lua_rawlen(L, -1);
175  for(size_t i = 0; i != len; ++i) {
176  lua_geti(L, -1, i + 1);
177  res.insert(luaW_checklocation(L, -1));
178  lua_pop(L, 1);
179  }
180  lua_pop(L, 1);
181  return res;
182 }
183 
185 {
186 public:
188  virtual bool matches(const gamemap_base& m, map_location l) = 0;
189  virtual ~filter_impl() {};
190 };
191 
192 //build_filter impl
193 namespace {
194 
195 std::unique_ptr<filter_impl> build_filter(lua_State* L, int res_index, knows_sets_t& ks);
196 
197 class con_filter : public filter_impl
198 {
199 public:
200  con_filter(lua_State* L, int res_index, knows_sets_t& ks)
201  :list_()
202  {
203  LOG_LMG << "creating con filter\n";
204  size_t len = lua_rawlen(L, -1);
205  for(size_t i = 1; i != len; ++i) {
206  lua_geti(L, -1, i + 1);
207  list_.emplace_back(build_filter(L, res_index, ks));
208  lua_pop(L, 1);
209  }
210  }
211  std::vector<std::unique_ptr<filter_impl>> list_;
212 };
213 
214 class and_filter : public con_filter
215 {
216 public:
217  and_filter(lua_State* L, int res_index, knows_sets_t& ks)
218  : con_filter(L, res_index, ks)
219  {
220  LOG_LMG << "created and filter\n";
221  }
222 
223  bool matches(const gamemap_base& m, map_location l) override
224  {
225  LOG_MATCHES(and);
226  for(const auto& pfilter : list_) {
227  if(!pfilter->matches(m, l)) {
228  return false;
229  }
230  }
231  return true;
232  }
233 };
234 
235 class or_filter : public con_filter
236 {
237 public:
238  or_filter(lua_State* L, int res_index, knows_sets_t& ks)
239  : con_filter(L, res_index, ks)
240  {
241  LOG_LMG << "created or filter\n";
242  }
243 
244  bool matches(const gamemap_base& m, map_location l) override
245  {
246  LOG_MATCHES(or);
247  for(const auto& pfilter : list_) {
248  if(pfilter->matches(m, l)) {
249  return true;
250  }
251  }
252  return false;
253  }
254 };
255 
256 class nand_filter : public con_filter
257 {
258 public:
259  nand_filter(lua_State* L, int res_index, knows_sets_t& ks)
260  : con_filter(L, res_index, ks)
261  {
262  LOG_LMG << "created nand filter\n";
263  }
264 
265  bool matches(const gamemap_base& m, map_location l) override
266  {
267  LOG_MATCHES(nand);
268  for(const auto& pfilter : list_) {
269  if(!pfilter->matches(m, l)) {
270  return true;
271  }
272  }
273  return false;
274  }
275 };
276 
277 class nor_filter : public con_filter
278 {
279 public:
280  nor_filter(lua_State* L, int res_index, knows_sets_t& ks)
281  : con_filter(L, res_index, ks)
282  {
283  LOG_LMG << "created nor filter\n";
284  }
285 
286  bool matches(const gamemap_base& m, map_location l) override
287  {
288  LOG_MATCHES(nor);
289  for(const auto& pfilter : list_) {
290  if(pfilter->matches(m, l)) {
291  return false;
292  }
293  }
294  return true;
295  }
296 };
297 
298 class cached_filter : public filter_impl
299 {
300 public:
301  cached_filter(lua_State* L, int res_index, knows_sets_t& ks)
302  : filter_()
303  , cache_()
304  {
305  LOG_LMG << "creating cached filter\n";
306  lua_geti(L, -1, 2);
307  filter_ = build_filter(L, res_index, ks);
308  lua_pop(L, 1);
309  }
310 
311  bool matches(const gamemap_base& m, map_location l) override
312  {
313  LOG_MATCHES(cached);
314  int cache_size = 2 * m.total_width() * m.total_height();
315  int loc_index = 2 * (l.wml_x() + l.wml_y() * m.total_width());
316 
317  if(int(cache_.size()) != cache_size) {
318  cache_ = dynamic_bitset(cache_size);
319  }
320  if(cache_[loc_index]) {
321  return cache_[loc_index + 1];
322  }
323  else {
324  bool res = filter_->matches(m, l);
325  cache_[loc_index] = true;
326  cache_[loc_index + 1] = res;
327  return res;
328  }
329  }
330 
331  std::unique_ptr<filter_impl> filter_;
332  mutable dynamic_bitset cache_;
333 };
334 
335 class x_filter : public filter_impl
336 {
337 public:
338  x_filter(lua_State* L, int /*res_index*/, knows_sets_t&)
339  : filter_()
340  {
341  LOG_LMG << "creating x filter\n";
342  lua_geti(L, -1, 2);
343  filter_ = parse_range(luaW_tostring(L, -1));
344  lua_pop(L, 1);
345  }
346  bool matches(const gamemap_base&, map_location l) override
347  {
348  LOG_MATCHES(x);
349  return l.x >= 0 && l.x < int(filter_.size()) && filter_[l.x];
350  }
351  dynamic_bitset filter_;
352 };
353 
354 class y_filter : public filter_impl
355 {
356 public:
357  y_filter(lua_State* L, int /*res_index*/, knows_sets_t&)
358  : filter_()
359  {
360  LOG_LMG << "creating y filter\n";
361  lua_geti(L, -1, 2);
362  filter_ = parse_range(luaW_tostring(L, -1));
363  lua_pop(L, 1);
364  }
365 
366  bool matches(const gamemap_base&, map_location l) override
367  {
368  LOG_MATCHES(y);
369  return l.y >= 0 && l.y < int(filter_.size()) && filter_[l.y];
370  }
371 
372  dynamic_bitset filter_;
373 };
374 
375 class onborder_filter : public filter_impl
376 {
377 public:
378  onborder_filter(lua_State*, int /*res_index*/, knows_sets_t&)
379  {
380  LOG_LMG << "creating onborder filter\n";
381  }
382 
383  bool matches(const gamemap_base& m, map_location l) override
384  {
385  LOG_MATCHES(onborder);
386  return !m.on_board(l);
387  }
388 };
389 
390 class terrain_filter : public filter_impl
391 {
392 public:
393  terrain_filter(lua_State* L, int /*res_index*/, knows_sets_t&)
394  : filter_()
395  {
396  LOG_LMG << "creating terrain filter\n";
397  lua_geti(L, -1, 2);
398  //fixme: use string_view
399  filter_ = t_translation::ter_match(luaW_tostring(L, -1));
400  lua_pop(L, 1);
401  }
402 
403  bool matches(const gamemap_base& m, map_location l) override
404  {
405  LOG_MATCHES(terrain);
406  const t_translation::terrain_code letter = m.get_terrain(l);
407  return t_translation::terrain_matches(letter, filter_);
408  }
409 
410  t_translation::ter_match filter_;
411 };
412 
413 static const offset_list_t even_offsets_default = {{1 , 0}, {1 , 1}, {0 , 1}, {-1 , 1}, {-1 , 0}, {0, -1}};
414 static const offset_list_t odd_offsets_default = {{1 , -1}, {1 , 0}, {0 , 1}, {-1 , 0}, {-1 , -1}, {0, -1}};
415 
416 class adjacent_filter : public filter_impl
417 {
418 public:
419  adjacent_filter(lua_State* L, int res_index, knows_sets_t& ks)
420  : filter_()
421  {
422  LOG_LMG << "creating adjacent filter\n";
423  if(luaW_tableget(L, -1, "adjacent")) {
424  parse_rel_sequence(luaW_tostring(L, -1), even_offsets_, odd_offsets_);
425  lua_pop(L, 1);
426  }
427  else {
428  even_offsets_ = even_offsets_default;
429  odd_offsets_ = odd_offsets_default;
430  }
431  if(luaW_tableget(L, -1, "count")) {
432  accepted_counts_ = parse_range(luaW_tostring(L, -1));
433  lua_pop(L, 1);
434  }
435  lua_geti(L, -1, 2);
436  filter_ = build_filter(L, res_index, ks);
437  lua_pop(L, 1);
438  }
439 
440  bool matches(const gamemap_base& m, map_location l) override
441  {
442  LOG_MATCHES(adjacent);
443  int count = 0;
444  // is_odd == is_even in wml coordinates.
445  offset_list_t& offsets = (l.wml_x() & 1) ? odd_offsets_ : even_offsets_;
446  for(const auto& offset : offsets) {
447  map_location ad = {l.x + offset.first, l.y + offset.second};
448  if(m.on_board_with_border(ad) && filter_->matches(m, ad)) {
449  if(accepted_counts_.size() == 0) {
450  return true;
451  }
452  ++count;
453  }
454  }
455  return int(accepted_counts_.size()) > count && accepted_counts_[count];
456  }
457  offset_list_t even_offsets_;
458  offset_list_t odd_offsets_;
459  dynamic_bitset accepted_counts_;
460  std::unique_ptr<filter_impl> filter_;
461 };
462 
463 class findin_filter : public filter_impl
464 {
465 public:
466  findin_filter(lua_State* L, int res_index, knows_sets_t& ks)
467  : set_(nullptr)
468  {
469  LOG_LMG << "creating findin filter\n";
470  lua_geti(L, -1, 2);
471  std::string id = std::string(luaW_tostring(L, -1));
472  lua_pop(L, 1);
473 
474  //TODO: c++14: use heterogenous lookup.
475  auto insert_res = ks.insert(knows_sets_t::value_type{id, {}});
476  if(insert_res.second && res_index > 0) {
477  // istable(L, res_index) was already checked.
478  if(luaW_tableget(L, res_index, id.c_str())) {
479  insert_res.first->second = luaW_to_locationset(L, -1);
480  lua_pop(L, 1);
481  }
482  }
483  set_ = &insert_res.first->second;
484  }
485  bool matches(const gamemap_base&, map_location l) override
486  {
487  LOG_MATCHES(findin);
488  if(set_) {
489  return set_->find(l) != set_->end();
490  }
491  return false;
492  }
493  const location_set* set_;
494 };
495 
496 class radius_filter : public filter_impl
497 {
498 public:
499 
500  radius_filter(lua_State* L, int res_index, knows_sets_t& ks)
501  : radius_()
502  , filter_radius_()
503  , filter_()
504  {
505  LOG_LMG << "creating radius filter\n";
506  if(luaW_tableget(L, -1, "filter_radius")) {
507  filter_radius_ = build_filter(L, res_index, ks);
508  lua_pop(L, 1);
509  }
510  lua_geti(L, -1, 2);
511  radius_ = lua_tointeger(L, -1);
512  lua_pop(L, 1);
513  lua_geti(L, -1, 3);
514  filter_ = build_filter(L, res_index, ks);
515  lua_pop(L, 1);
516  }
517 
518  bool matches(const gamemap_base& m, map_location l) override
519  {
520  LOG_MATCHES(radius);
521  std::set<map_location> result;
522 
523  get_tiles_radius({{ l }}, radius_, result,
524  [&](const map_location& l) {
525  return m.on_board_with_border(l);
526  },
527  [&](const map_location& l) {
528  return !filter_radius_ || filter_radius_->matches(m, l);
529  }
530  );
531 
532  for (map_location lr : result) {
533  if(!filter_ || filter_->matches(m, lr)) {
534  return true;
535  }
536  }
537  return false;
538  }
539 
540  int radius_;
541  std::unique_ptr<filter_impl> filter_radius_;
542  std::unique_ptr<filter_impl> filter_;
543 };
544 
545 class formula_filter : public filter_impl
546 {
547 public:
548  formula_filter(lua_State* L, int, knows_sets_t&)
549  : formula_()
550  {
551  LOG_LMG << "creating formula filter\n";
552  lua_geti(L, -1, 2);
553  std::string code = std::string(luaW_tostring(L, -1));
554  lua_pop(L, 1);
555 
556  try {
557  formula_ = std::make_unique<wfl::formula>(code);
558  } catch(const wfl::formula_error& e) {
559  ERR_LMG << "formula error" << e.what() << "\n";
560  }
561  }
562  bool matches(const gamemap_base&, map_location l) override
563  {
564  LOG_MATCHES(formula);
565  try {
566  const wfl::location_callable callable1(l);
567  wfl::map_formula_callable callable(callable1.fake_ptr());
568  return (formula_.get() != nullptr) && formula_->evaluate(callable).as_bool();
569  } catch(const wfl::formula_error& e) {
570  ERR_LMG << "Formula error: " << e.type << " at " << e.filename << ':' << e.line << ")\n";
571  return false;
572  }
573  }
574  std::unique_ptr<wfl::formula> formula_;
575 };
576 
577 // todo: maybe invent a gerneral macro for this string_switch implementation.
578 enum filter_keys { F_AND, F_OR, F_NAND, F_NOR, F_X, F_Y, F_FIND_IN, F_ADJACENT, F_TERRAIN, F_RADIUS, F_FORMULA, F_CACHED };
579 //todoc++14: std::unordered_map doesn'tsupport herterogrnous lookup.
580 //todo consider renaming and -> all ,or ->any, nor -> none, nand -> notall
581 static const std::unordered_map<std::string, filter_keys> keys {
582  { "all", F_AND },
583  { "any", F_OR },
584  { "not_all", F_NAND },
585  { "none", F_NOR },
586  { "x", F_X },
587  { "y", F_Y },
588  { "find_in", F_FIND_IN },
589  { "adjacent", F_ADJACENT },
590  { "terrain", F_TERRAIN },
591  { "cached", F_CACHED },
592  { "formula", F_FORMULA },
593  { "radius", F_RADIUS }
594 };
595 
596 std::unique_ptr<filter_impl> build_filter(lua_State* L, int res_index, knows_sets_t& ks)
597 {
598  LOG_LMG << "buildfilter: start\n";
599  if(!lua_istable(L, -1)) {
600  throw invalid_lua_argument("buildfilter: expected table");
601  }
602  lua_rawgeti(L, -1, 1);
603  std::string s = std::string(luaW_tostring(L, -1));
604  LOG_LMG << "buildfilter: got: " << s << "\n";
605  auto it = keys.find(s);
606  if(it == keys.end()) {
607  //fixme use proper exception type.
608  throw invalid_lua_argument(std::string("buildfilter: invalid filter type ") + s);
609  }
610  auto key = it->second;
611  lua_pop(L, 1);
612  switch(key)
613  {
614  case F_AND:
615  return std::make_unique<and_filter>(L, res_index, ks);
616  case F_OR:
617  return std::make_unique<or_filter>(L, res_index, ks);
618  case F_NAND:
619  return std::make_unique<nand_filter>(L, res_index, ks);
620  case F_NOR:
621  return std::make_unique<nor_filter>(L, res_index, ks);
622  case F_X:
623  return std::make_unique<x_filter>(L, res_index, ks);
624  case F_Y:
625  return std::make_unique<y_filter>(L, res_index, ks);
626  case F_FIND_IN:
627  return std::make_unique<findin_filter>(L, res_index, ks);
628  case F_ADJACENT:
629  return std::make_unique<adjacent_filter>(L, res_index, ks);
630  case F_TERRAIN:
631  return std::make_unique<terrain_filter>(L, res_index, ks);
632  case F_RADIUS:
633  return std::make_unique<radius_filter>(L, res_index, ks);
634  case F_CACHED:
635  return std::make_unique<cached_filter>(L, res_index, ks);
636  case F_FORMULA:
637  return std::make_unique<formula_filter>(L, res_index, ks);
638  default:
639  throw "invalid filter key enum";
640  }
641 }
642 }
643 
644 //////////////// PUBLIC API ////////////////
645 
646 namespace lua_mapgen {
647 /**
648  * @param L the pointer to the lua interpreter.
649  * @param data_index a index to the lua stack pointing to the lua table that describes the filter.
650  * @param res_index a _positive_ index to the lua stack pointing to the lua table that describes the filter resources.
651  */
652 filter::filter(lua_State* L, int data_index, int res_index)
653 {
654  LOG_LMG << "creating filter object\n";
655  lua_pushvalue (L, data_index);
656  impl_ = build_filter(L, res_index, known_sets_);
657  lua_pop(L, 1);
658  LOG_LMG << "finished creating filter object\n";
659 }
660 
661 bool filter::matches(const gamemap_base& m, map_location l)
662 {
663  log_scope("filter::matches");
664  return impl_->matches(m, l);
665 }
666 
667 filter::~filter()
668 {
669 
670 }
671 
672 }
673 
675 {
676  location_set res;
677  LOG_LMG << "map:get_locations vaidargs\n";
678  if(lua_istable(L, 3)) {
679  LOG_LMG << "map:get_locations some locations\n";
681  LOG_LMG << "map:get_locations #args = " << s.size() << "\n";
682  for (const map_location& l : s) {
683  if(f.matches(m, l)) {
684  res.insert(l);
685  }
686  }
687  }
688  else {
689  LOG_LMG << "map:get_locations all locations\n";
690  m.for_each_loc([&](map_location l) {
691  if(f.matches(m, l)) {
692  res.insert(l);
693  }
694  });
695  }
696  LOG_LMG << "map:get_locations #res = " << res.size() << "\n";
697  luaW_push_locationset(L, res);
698  LOG_LMG << "map:get_locations end\n";
699  return 1;
700 
701 }
703 {
704  //todo: create filter form table if needed
705  LOG_LMG << "map:get_locations\n";
707  if(luaW_is_mgfilter(L, 2)) {
709  return intf_mg_get_locations_part2(L, m, f);
710  }
711  else if (lua_istable(L, 2)) {
712  lua_mapgen::filter f(L, 2, 0);
713  return intf_mg_get_locations_part2(L, m, f);
714  }
715  else {
716  return luaW_type_error(L, 2, "terrainfilter");
717  }
718 }
719 
721 {
724  int r = luaL_checkinteger(L, 3);
726  location_set res;
727  get_tiles_radius(std::move(s), r, res,
728  [&](const map_location& l) {
729  return m.on_board_with_border(l);
730  },
731  [&](const map_location& l) {
732  return f.matches(m, l);
733  }
734  );
735  luaW_push_locationset(L, res);
736  return 1;
737 }
738 
740 {
741  return luaL_testudata(L, index, terrinfilterKey) != nullptr;
742 }
743 
744 
746 {
747  if(luaW_is_mgfilter(L, index)) {
748  return static_cast<lua_mapgen::filter*>(lua_touserdata(L, index));
749  }
750  return nullptr;
751 }
752 
754 {
755  if(luaW_is_mgfilter(L, index)) {
756  return *static_cast<lua_mapgen::filter*>(lua_touserdata(L, index));
757  }
758  luaW_type_error(L, index, "terrainfilter");
759  throw "luaW_type_error didn't throw";
760 }
761 
763 {
765 }
766 
767 template<typename... T>
769 {
770  LOG_LMG << "luaW_push_mgfilter\n";
771  lua_mapgen::filter* res = new(L) lua_mapgen::filter(std::forward<T>(params)...);
773  return res;
774 }
775 
776 /**
777  * Create a filter.
778 */
780 {
781  try {
782  int res_index = 0;
783  if(!lua_istable(L, 1)) {
784  return luaL_argerror(L, 1, "table expected");
785  }
786  if(lua_istable(L, 2)) {
787  res_index = 2;
788  }
789  lua_mapgen::filter res(L, 1, res_index);
790  luaW_push_mgfilter(L, std::move(res));
791  return 1;
792  }
793  catch(const invalid_lua_argument& e) {
794  return luaL_argerror(L, 1, e.what());
795  }
796 }
797 
798 
799 /**
800  * Gets some data on a filter (__index metamethod).
801  * - Arg 1: full userdata containing the filter.
802  * - Arg 2: string containing the name of the property.
803  * - Ret 1: something containing the attribute.
804  */
806 {
808  UNUSED(f);
809  return 0;
810 }
811 
812 /**
813  * Sets some data on a filter (__newindex metamethod).
814  * - Arg 1: full userdata containing the filter.
815  * - Arg 2: string containing the name of the property.
816  * - Arg 3: something containing the attribute.
817  */
819 {
821  UNUSED(f);
822  char const *m = luaL_checkstring(L, 2);
823  std::string err_msg = "unknown modifiable property of map: ";
824  err_msg += m;
825  return luaL_argerror(L, 2, err_msg.c_str());
826 }
827 
828 
829 /**
830  * Clears the cache of a filter.
831  */
833 {
835  UNUSED(f);
836  return 0;
837 }
838 /**
839  * Destroys a map object before it is collected (__gc metamethod).
840  */
842 {
844  f.~filter();
845  return 0;
846 }
847 
848 
849 namespace lua_terrainfilter {
851  {
852  std::ostringstream cmd_out;
853 
854  cmd_out << "Adding terrainmamap metatable...\n";
855 
858  lua_setfield(L, -2, "__gc");
860  lua_setfield(L, -2, "__index");
862  lua_setfield(L, -2, "__newindex");
863  lua_pushstring(L, "terrain_filter");
864  lua_setfield(L, -2, "__metatable");
865  // terrainmap methods
867  lua_setfield(L, -2, "clear_cache");
868 
869  return cmd_out.str();
870  }
871 }
bool luaW_tableget(lua_State *L, int index, const char *key)
bool matches(const gamemap_base &m, map_location l)
#define lua_pushcfunction(L, f)
Definition: lua.h:370
std::pair< int, int > parse_range(const std::string &str)
std::map< std::string, std::set< map_location > > knows_sets_t
int luaW_type_error(lua_State *L, int narg, const char *tname)
static const char terrinfilterKey[]
LUA_API int lua_rawgeti(lua_State *L, int idx, lua_Integer n)
Definition: lapi.cpp:710
lua_mapgen::filter * luaW_to_mgfilter(lua_State *L, int index)
bool terrain_matches(const terrain_code &src, const terrain_code &dest)
Tests whether a specific terrain matches an expression, for matching rules see above.
std::vector< std::pair< int, int > > offset_list_t
std::string filename
Definition: formula.hpp:107
gamemap_base & luaW_checkterrainmap(lua_State *L, int index)
const char * what() const noexcept
static int impl_terrainfilter_get(lua_State *L)
Gets some data on a filter (__index metamethod).
#define lua_tointeger(L, i)
Definition: lua.h:362
int wml_x() const
Definition: location.hpp:152
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:49
std::string register_metatables(lua_State *L)
void for_each_loc(const F &f) const
Definition: map.hpp:135
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname)
Definition: lauxlib.cpp:324
map_location luaW_checklocation(lua_State *L, int index)
Converts an optional table or pair of integers to a map location object.
Definition: lua_common.cpp:771
int luaW_push_locationset(lua_State *L, const std::set< map_location > &locs)
Converts a set of map locations to a Lua table pushed at the top of the stack.
Definition: lua_common.cpp:779
lua_mapgen::filter & luaW_check_mgfilter(lua_State *L, int index)
#define lua_pop(L, n)
Definition: lua.h:364
LUALIB_API int luaL_argerror(lua_State *L, int arg, const char *extramsg)
Definition: lauxlib.cpp:175
static int intf_clearcache(lua_State *L)
Clears the cache of a filter.
static lua_mapgen::filter * luaW_push_mgfilter(lua_State *L, T &&... params)
int intf_mg_get_tiles_radius(lua_State *L)
terrain_code get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
Definition: map.cpp:301
int wml_y() const
Definition: location.hpp:153
bool on_board_with_border(const map_location &loc) const
Definition: map.cpp:389
std::string type
Definition: formula.hpp:105
boost::dynamic_bitset<> dynamic_bitset
int intf_terrainfilter_create(lua_State *L)
Create a filter.
#define LOG_MATCHES(NAME)
static lg::log_domain log_scripting_lua_mapgen("scripting/lua/mapgen")
LUALIB_API void * luaL_testudata(lua_State *L, int ud, const char *tname)
Definition: lauxlib.cpp:330
const char * what() const noexcept
Definition: exceptions.hpp:35
static map_location::DIRECTION se
Encapsulates the map of the game.
Definition: location.hpp:37
int total_width() const
Real width of the map, including borders.
Definition: map.hpp:58
#define UNUSED(x)
Definition: global.hpp:30
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
Definition: lauxlib.cpp:311
LUA_API void * lua_touserdata(lua_State *L, int idx)
Definition: lapi.cpp:432
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:214
bool luaW_is_mgfilter(lua_State *L, int index)
static std::set< map_location > luaW_to_locationset(lua_State *L, int index)
#define ERR_LMG
std::size_t i
Definition: function.cpp:940
formula_callable_ptr fake_ptr()
Definition: callable.hpp:41
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int arg)
Definition: lauxlib.cpp:442
void lua_mgfilter_setmetatable(lua_State *L)
static int impl_terrainfilter_set(lua_State *L)
Sets some data on a filter (__newindex metamethod).
static map_location::DIRECTION s
void get_tiles_radius(const map_location &center, std::size_t radius, std::set< map_location > &result)
Function that will add to result all locations within radius tiles of center (including center itself...
Definition: pathutils.cpp:69
pump_impl & impl_
Definition: pump.cpp:134
#define log_scope(description)
Definition: log.hpp:206
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:384
LUA_API void lua_pushvalue(lua_State *L, int idx)
Definition: lapi.cpp:246
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
int total_height() const
Real height of the map, including borders.
Definition: map.hpp:61
REMOVE_EMPTY: remove empty elements.
void split_foreach(std::string_view s, char sep, const int flags, const F &f)
std::set< map_location > location_set
LUA_API lua_Unsigned lua_rawlen(lua_State *L, int idx)
Definition: lapi.cpp:402
#define f
virtual ~filter_impl()
#define lua_istable(L, n)
Definition: lua.h:373
std::string_view luaW_tostring(lua_State *L, int index)
Standard logging facilities (interface).
LUA_API int lua_geti(lua_State *L, int idx, lua_Integer n)
Definition: lapi.cpp:661
#define e
This structure can be used for matching terrain strings.
invalid_lua_argument(const std::string &msg)
static int impl_terrainfilter_collect(lua_State *L)
Destroys a map object before it is collected (__gc metamethod).
int intf_mg_get_locations(lua_State *L)
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:514
static int intf_mg_get_locations_part2(lua_State *L, gamemap_base &m, lua_mapgen::filter &f)
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:837
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:722
#define LOG_LMG
#define luaL_checkstring(L, n)
Definition: lauxlib.h:138