-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
lua_terrainmap.cpp
479 lines (416 loc) · 12.1 KB
/
lua_terrainmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
Copyright (C) 2018 the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "scripting/lua_terrainmap.hpp"
#include "scripting/lua_terrainfilter.hpp"
#include "formatter.hpp"
#include "log.hpp"
#include "map/location.hpp"
#include "map/map.hpp"
#include "scripting/lua_common.hpp"
#include "scripting/push_check.hpp"
#include "scripting/game_lua_kernel.hpp"
#include "lua/lauxlib.h"
#include "lua/lua.h"
static lg::log_domain log_scripting_lua("scripting/lua");
#define LOG_LUA LOG_STREAM(info, log_scripting_lua)
#define ERR_LUA LOG_STREAM(err, log_scripting_lua)
static const char terrinmapKey[] = "terrainmap";
static const char maplocationKey[] = "special_locations";
using utils::string_view;
//////// SPECIAL LOCATION ////////
bool luaW_isslocs(lua_State* L, int index)
{
return luaL_testudata(L, index, maplocationKey) != nullptr;
}
mapgen_gamemap* luaW_toslocs(lua_State *L, int index)
{
if(luaW_isterrainmap(L, index)) {
lua_rawgeti(L, index, 1);
mapgen_gamemap* m = luaW_toterrainmap(L, -1);
lua_pop(L, 1);
return m;
}
return nullptr;
}
mapgen_gamemap& luaW_check_slocs(lua_State *L, int index)
{
if(mapgen_gamemap* m = luaW_toslocs(L, index)) {
return *m;
}
luaW_type_error(L, index, "terrainmap");
throw "luaW_type_error didn't thow.";
}
void lua_slocs_setmetatable(lua_State *L)
{
luaL_setmetatable(L, maplocationKey);
}
/**
* @a index the index of the map object.
*/
void luaW_pushslocs(lua_State *L, int index)
{
lua_pushvalue(L, index);
//stack: map
lua_createtable(L, 1, 0);
//stack: map, slocs
lua_pushvalue(L, -2);
//stack: map, slocs, map
lua_rawseti(L, -2, 1);
//stack: map, slocs
luaL_setmetatable(L, maplocationKey);
//stack: map, slocs
lua_remove(L, -2);
//stack: slocs
}
int impl_slocs_get(lua_State* L)
{
mapgen_gamemap& m = luaW_check_slocs(L, 1);
string_view id = luaL_checkstring(L, 2);
auto res = m.special_location(std::string(id));
if(res.wml_x() >= 0) {
luaW_pushlocation(L, res);
}
else {
//functions with variable return numbers have been causing problem in the past
lua_pushnil(L);
}
return 1;
}
int impl_slocs_set(lua_State* L)
{
mapgen_gamemap& m = luaW_check_slocs(L, 1);
string_view id = luaL_checkstring(L, 2);
map_location loc = luaW_checklocation(L, 3);
m.set_special_location(std::string(id), loc);
return 0;
}
//////// MAP ////////
mapgen_gamemap::mapgen_gamemap(string_view s)
: tiles_()
, starting_positions_()
{
if(s.empty()) {
return;
}
//throws t_translation::error
//todo: make read_game_map take a string_view
tiles_ = t_translation::read_game_map(s, starting_positions_, t_translation::coordinate{ 1, 1 });
}
mapgen_gamemap::mapgen_gamemap(int w, int h, terrain_code t)
: tiles_(w, h, t)
, starting_positions_()
{
}
std::string mapgen_gamemap::to_string() const
{
return t_translation::write_game_map(tiles_, starting_positions_, { 1, 1 }) + "\n";
}
void mapgen_gamemap::set_terrain(const map_location& loc, const terrain_code & terrain, const terrain_type_data::merge_mode mode)
{
terrain_code& t = (*this)[loc];
terrain_code old = t;
t = terrain;
simplemerge(old, t, mode);
}
void mapgen_gamemap::simplemerge(terrain_code old_t, terrain_code& new_t, const terrain_type_data::merge_mode mode)
{
if(mode == terrain_type_data::OVERLAY) {
new_t = t_translation::terrain_code(old_t.base, new_t.overlay);
}
if(mode == terrain_type_data::BASE) {
new_t = t_translation::terrain_code(new_t.base, old_t.overlay);
}
}
void mapgen_gamemap::set_special_location(const std::string& id, const map_location& loc)
{
bool valid = loc.valid();
auto it_left = starting_positions_.left.find(id);
if (it_left != starting_positions_.left.end()) {
if (valid) {
starting_positions_.left.replace_data(it_left, loc);
}
else {
starting_positions_.left.erase(it_left);
}
}
else {
starting_positions_.left.insert(it_left, std::make_pair(id, loc));
}
}
map_location mapgen_gamemap::special_location(const std::string& id) const
{
auto it = starting_positions_.left.find(id);
if (it != starting_positions_.left.end()) {
auto& coordinate = it->second;
return map_location(coordinate.x, coordinate.y);
}
else {
return map_location();
}
}
bool luaW_isterrainmap(lua_State* L, int index)
{
return luaL_testudata(L, index, terrinmapKey) != nullptr;
}
mapgen_gamemap* luaW_toterrainmap(lua_State *L, int index)
{
if(luaW_isterrainmap(L, index)) {
return static_cast<mapgen_gamemap*>(lua_touserdata(L, index));
}
return nullptr;
}
mapgen_gamemap& luaW_checkterrainmap(lua_State *L, int index)
{
if(luaW_isterrainmap(L, index)) {
return *static_cast<mapgen_gamemap*>(lua_touserdata(L, index));
}
luaW_type_error(L, index, "terrainmap");
throw "luaW_type_error didn't throw";
}
void lua_terrainmap_setmetatable(lua_State *L)
{
luaL_setmetatable(L, terrinmapKey);
}
template<typename... T>
mapgen_gamemap* luaW_pushmap(lua_State *L, T&&... params)
{
mapgen_gamemap* res = new(L) mapgen_gamemap(std::forward<T>(params)...);
lua_terrainmap_setmetatable(L);
return res;
}
/**
* Create a map.
* - Arg 1: string descripbing the map data.
* - or:
* - Arg 1: int, width
* - Arg 2: int, height
* - Arg 3: string, terrain
*/
int intf_terainmap_create(lua_State *L)
{
if(lua_isnumber(L, 1) && lua_isnumber(L, 2)) {
int w = lua_tonumber(L, 1);
int h = lua_tonumber(L, 2);
auto terrain = t_translation::read_terrain_code(luaL_checkstring(L, 3));
luaW_pushmap(L, w, h, terrain);
return 1;
}
else {
string_view data_str = luaL_checkstring(L, 1);
luaW_pushmap(L, data_str);
return 1;
}
}
/**
* Destroys a map object before it is collected (__gc metamethod).
*/
static int impl_terainmap_collect(lua_State *L)
{
mapgen_gamemap *u = static_cast<mapgen_gamemap*>(lua_touserdata(L, 1));
u->mapgen_gamemap::~mapgen_gamemap();
return 0;
}
/**
* Gets some data on a map (__index metamethod).
* - Arg 1: full userdata containing the map.
* - Arg 2: string containing the name of the property.
* - Ret 1: something containing the attribute.
*/
static int impl_terainmap_get(lua_State *L)
{
mapgen_gamemap& tm = luaW_checkterrainmap(L, 1);
char const *m = luaL_checkstring(L, 2);
// Find the corresponding attribute.
return_int_attrib("width", tm.total_width());
return_int_attrib("height", tm.total_height());
return_string_attrib("data", tm.to_string());
if(strcmp(m, "special_locations") == 0) {
luaW_pushslocs(L, 1);
return 1;
}
if(luaW_getmetafield(L, 1, m)) {
return 1;
}
return 0;
}
/**
* Sets some data on a map (__newindex metamethod).
* - Arg 1: full userdata containing the map.
* - Arg 2: string containing the name of the property.
* - Arg 3: something containing the attribute.
*/
static int impl_terainmap_set(lua_State *L)
{
mapgen_gamemap& tm = luaW_checkterrainmap(L, 1);
UNUSED(tm);
char const *m = luaL_checkstring(L, 2);
std::string err_msg = "unknown modifiable property of map: ";
err_msg += m;
return luaL_argerror(L, 2, err_msg.c_str());
}
/**
* Sets a terrain code.
* - Arg 1: map location.
* - Arg 2: terrain code string.
* - Arg 3: layer: (overlay|base|both, default=both)
*/
static int intf_set_terrain(lua_State *L)
{
mapgen_gamemap& tm = luaW_checkterrainmap(L, 1);
map_location loc = luaW_checklocation(L, 2);
string_view t_str = luaL_checkstring(L, 3);
auto terrain = t_translation::read_terrain_code(t_str);
auto mode = terrain_type_data::BOTH;
if(!lua_isnoneornil(L, 4)) {
string_view mode_str = luaL_checkstring(L, 4);
if(mode_str == "base") {
mode = terrain_type_data::BASE;
}
else if(mode_str == "overlay") {
mode = terrain_type_data::OVERLAY;
}
}
tm.set_terrain(loc, terrain, mode);
return 0;
}
/**
* Gets a terrain code.
* - Arg 1: map location.
* - Ret 1: string.
*/
static int intf_get_terrain(lua_State *L)
{
mapgen_gamemap& tm = luaW_checkterrainmap(L, 1);
map_location loc = luaW_checklocation(L, 2);
auto t = tm[loc];
lua_pushstring(L, t_translation::write_terrain_code(t).c_str());
return 1;
}
static std::vector<gamemap::overlay_rule> read_rules_vector(lua_State *L, int index)
{
std::vector<gamemap::overlay_rule> rules;
for (int i = 1, i_end = lua_rawlen(L, index); i <= i_end; ++i)
{
lua_rawgeti(L, index, i);
if(!lua_istable(L, -1)) {
luaL_argerror(L, index, "rules must be a table of tables");
}
rules.push_back(gamemap::overlay_rule());
auto& rule = rules.back();
if(luaW_tableget(L, -1, "old")) {
rule.old_ = t_translation::read_list(luaW_tostring(L, -1));
lua_pop(L, 1);
}
if(luaW_tableget(L, -1, "new")) {
rule.new_ = t_translation::read_list(luaW_tostring(L, -1));
lua_pop(L, 1);
}
if(luaW_tableget(L, -1, "mode")) {
auto str = luaW_tostring(L, -1);
rule.mode_ = str == "base" ? terrain_type_data::BASE : (str == "overlay" ? terrain_type_data::OVERLAY : terrain_type_data::BOTH);
lua_pop(L, 1);
}
if(luaW_tableget(L, -1, "terrain")) {
const t_translation::ter_list terrain = t_translation::read_list(luaW_tostring(L, -1));
if(!terrain.empty()) {
rule.terrain_ = terrain[0];
}
lua_pop(L, 1);
}
if(luaW_tableget(L, -1, "use_old")) {
rule.use_old_ = luaW_toboolean(L, -1);
lua_pop(L, 1);
}
if(luaW_tableget(L, -1, "replace_if_failed")) {
rule.replace_if_failed_ = luaW_toboolean(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 1);
}
return rules;
}
/**
* Reaplces part of the map.
* - Arg 1: map location.
* - Arg 2: map data string.
* - Arg 3: table for optional named arguments
* - is_odd: boolen, if Arg2 has the odd mapo format (as if it was cut from a odd map location)
* - ignore_special_locations: boolean
* - rules: table of tables
*/
int mapgen_gamemap::intf_mg_terrain_mask(lua_State *L)
{
mapgen_gamemap& tm1 = luaW_checkterrainmap(L, 1);
map_location loc = luaW_checklocation(L, 2);
mapgen_gamemap& tm2 = luaW_checkterrainmap(L, 3);
bool is_odd = false;
bool ignore_special_locations = false;
std::vector<gamemap::overlay_rule> rules;
if(lua_istable(L, 4)) {
is_odd = luaW_table_get_def<bool>(L, 4, "is_odd", false);
ignore_special_locations = luaW_table_get_def<bool>(L, 4, "ignore_special_locations", false);
if(luaW_tableget(L, 4, "rules")) {
if(!lua_istable(L, -1)) {
return luaL_argerror(L, 4, "rules must be a table");
}
rules = read_rules_vector(L, -1);
lua_pop(L, 1);
}
}
gamemap::overlay_impl(
tm1.tiles_,
tm1.starting_positions_,
tm2.tiles_,
tm2.starting_positions_,
[&](const map_location& loc, const t_translation::terrain_code& t, terrain_type_data::merge_mode mode, bool) { tm1.set_terrain(loc, t, mode); },
loc,
rules,
is_odd,
ignore_special_locations
);
return 0;
}
namespace lua_terrainmap {
std::string register_metatables(lua_State* L)
{
std::ostringstream cmd_out;
cmd_out << "Adding terrainmamap metatable...\n";
luaL_newmetatable(L, terrinmapKey);
lua_pushcfunction(L, impl_terainmap_collect);
lua_setfield(L, -2, "__gc");
lua_pushcfunction(L, impl_terainmap_get);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, impl_terainmap_set);
lua_setfield(L, -2, "__newindex");
lua_pushstring(L, "terainmap");
lua_setfield(L, -2, "__metatable");
// terainmap methods
lua_pushcfunction(L, intf_set_terrain);
lua_setfield(L, -2, "set_terrain");
lua_pushcfunction(L, intf_get_terrain);
lua_setfield(L, -2, "get_terrain");
lua_pushcfunction(L, intf_mg_get_locations);
lua_setfield(L, -2, "get_locations");
lua_pushcfunction(L, intf_mg_get_tiles_radius);
lua_setfield(L, -2, "get_tiles_radius");
lua_pushcfunction(L, &mapgen_gamemap::intf_mg_terrain_mask);
lua_setfield(L, -2, "terrain_mask");
cmd_out << "Adding terrainmamap2 metatable...\n";
luaL_newmetatable(L, maplocationKey);
lua_pushcfunction(L, impl_slocs_get);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, impl_slocs_set);
lua_setfield(L, -2, "__newindex");
lua_pushstring(L, "special_locations");
lua_setfield(L, -2, "__metatable");
return cmd_out.str();
}
}