-
Notifications
You must be signed in to change notification settings - Fork 1
/
lua-map.c
461 lines (438 loc) · 9.72 KB
/
lua-map.c
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
/**
* author: xingshuo
* date: 2017-12-25
*/
#include "config.h"
#include "map.h"
#include "pathfind.h"
#include "lua.h"
#include "lauxlib.h"
#define MAP_METATABLE "map_meta"
#define check_map(L, idx)\
*(Map**)luaL_checkudata(L, idx, MAP_METATABLE)
#define push_pos(L, row, col)\
do { \
lua_newtable(L); \
lua_pushstring(L, "row"); \
lua_pushinteger(L, row); \
lua_rawset(L,-3); \
lua_pushstring(L, "col"); \
lua_pushinteger(L, col); \
lua_rawset(L,-3); \
}while (0);
/* new_map
*
* create a new Map userdata.
*
* Arguments:
* L Lua state
*
* Lua Stack:
* 1 path of map file
* 2 whether open pathfinding cache
*
* Lua Returns:
* +1 Map userdata
*/
static int
new_map(lua_State *L) {
const char* path = lua_tostring(L, 1);
if (path == NULL) {
return luaL_argerror(L, 1, "load map path err");
}
bool open_cache = lua_toboolean(L, 2);
Map* m = load_map(path, open_cache);
*(Map**)lua_newuserdata(L, sizeof(void*)) = m;
luaL_getmetatable(L, MAP_METATABLE);
lua_setmetatable(L, -2);
return 1;
}
/* _delete_map
*
* __gc metamethod for Map userdata.
*
* Arguments:
* L Lua State
*
* Lua Stack:
* 1 Map userdata
*/
static int
_delete_map(lua_State *L) {
Map* m = check_map(L, 1);
delete_map(m);
return 0;
}
/* _tostring
*
* __tostring metamethod for Map userdata.
*
* Arguments:
* L Lua State
*
* Lua Stack:
* 1 Map userdata
*
* Lua Returns:
* +1 string representation of Map userdata
*/
static int
_tostring(lua_State *L) {
Map* m = check_map(L, 1);
lua_pushfstring(L, "[cmap: %p]", m);
return 1;
}
/* lset_block
*
* set dynamic blocking, if Map block changes, pathfinding cache cleared
*
* Arguments:
* L Lua state
*
* Lua Stack:
* 1 Map userdata
* 2 row num
* 3 col num
* 4 block mark (see the enum in config.h)
*/
static int
lset_block(lua_State *L) {
Map* m = check_map(L, 1);
int row = luaL_checknumber(L, 2);
int col = luaL_checknumber(L, 3);
Tile tile = luaL_checknumber(L, 4);
if (is_valid_grid(m, row, col) && m->tiles[row*m->max_col + col] != tile) {
m->tiles[row*m->max_col + col] = tile;
PathFinder* pf = m->pf;
if (pf->open_cache) {
clear_cache(pf);
}
}
return 0;
}
/* lget_block
*
* get the block mark of pos
*
* Arguments:
* L Lua state
*
* Lua Stack:
* 1 Map userdata
* 2 row num
* 3 col num
*
* Lua Returns:
* +1 the block mark, if the pos is valid, nil otherwise.
*/
static int
lget_block(lua_State *L) {
Map* m = check_map(L, 1);
int row = luaL_checknumber(L, 2);
int col = luaL_checknumber(L, 3);
if (is_valid_grid(m, row, col)) {
lua_pushinteger(L, m->tiles[row*m->max_col + col]);
return 1;
}else{
return 0;
}
}
/* lfind_path
*
* do pathfinding
*
* Arguments:
* L Lua state
*
* Lua Stack:
* 1 Map userdata
* 2 start row num
* 3 start col num
* 4 end row num
* 5 end col num
*
* Lua Returns:
* +1 true + path, if find the path, false otherwise.
*/
static int
lfind_path(lua_State *L) {
Map* m = check_map(L, 1);
int srow = luaL_checknumber(L, 2);
int scol = luaL_checknumber(L, 3);
int erow = luaL_checknumber(L, 4);
int ecol = luaL_checknumber(L, 5);
if (!is_valid_grid(m, srow, scol) || is_block_grid(m, srow, scol)) {
lua_pushboolean(L, 0);
return 1;
}
if (!is_valid_grid(m, erow, ecol) || is_block_grid(m, erow, ecol)) {
lua_pushboolean(L, 0);
return 1;
}
bool find = find_path(m->pf, srow, scol, erow, ecol);
if (find) {
lua_pushboolean(L, 1);
lua_newtable(L);
int idx = 0;
PathNode* node = ((PathFinder*)(m->pf))->path;
while (node) {
lua_pushinteger(L, ++idx);
push_pos(L, node->row, node->col);
lua_rawset(L,-3);
node = node->next;
}
delete_path(m->pf);
return 2;
}else {
lua_pushboolean(L, 0);
return 1;
}
}
/* lget_noblocking_pos
*
* get a nonblocking point randomly
*
* Arguments:
* L Lua state
*
* Lua Stack:
* 1 Map userdata
*
* Lua Returns:
* +1 a table of noblocking pos, if find, nil otherwise.
*/
static int
lget_noblocking_pos(lua_State *L) {
Map* m = check_map(L, 1);
int length = m->max_row*m->max_col;
int rnd_pos = rand()%length;
int i;
for (i=rnd_pos; i<length; i++) {
int r = i/m->max_col;
int c = i%m->max_col;
if (!is_block_grid(m, r, c)) {
push_pos(L, r, c);
return 1;
}
}
for (i=rnd_pos-1; i>=0; i--) {
int r = i/m->max_col;
int c = i%m->max_col;
if (!is_block_grid(m, r, c)) {
push_pos(L, r, c);
return 1;
}
}
return 0;
}
/* lis_block
*
* check pos whether block
*
* Arguments:
* L Lua state
*
* Lua Stack:
* 1 Map userdata
* 2 row num
* 3 col num
*
* Lua Returns:
* +1 true, if pos is block or not a valid pos, false otherwise.
*/
static int
lis_block(lua_State *L) {
Map* m = check_map(L, 1);
int row = luaL_checknumber(L, 2);
int col = luaL_checknumber(L, 3);
if (!is_valid_grid(m, row, col))
lua_pushboolean(L, 1);
else
lua_pushboolean(L, is_block_grid(m, row, col));
return 1;
}
/* lset_opencache
*
* open or close pathfinding cache, anyway, the cache will be cleared.
*
* Arguments:
* L Lua state
*
* Lua Stack:
* 1 Map userdata
* 2 open or close flag
*/
static int
lset_opencache(lua_State *L) {
Map* m = check_map(L, 1);
bool open = lua_toboolean(L, 2);
PathFinder* pf = m->pf;
pf->open_cache = open;
clear_cache(pf);
return 0;
}
/* lis_opencache
*
* check whether open pathfinding cache
*
* Arguments:
* L Lua state
*
* Lua Stack:
* 1 Map userdata
*
* Lua Returns:
* +1 true, if opened, false otherwise.
*/
static int
lis_opencache(lua_State *L) {
Map* m = check_map(L, 1);
PathFinder* pf = m->pf;
lua_pushboolean(L, pf->open_cache);
return 1;
}
/* linfo
*
* query info data
*
* Arguments:
* L Lua state
*
* Lua Stack:
* 1 Map userdata
* 2 query key
*
* Lua Returns:
* +1 a table of info data, if query key is nil, the value query key correspond otherwise.
*/
static int
linfo(lua_State *L) {
Map* m = check_map(L, 1);
const char *which = luaL_optstring(L, 2, NULL);
if (which == NULL) {
lua_newtable(L);
lua_pushstring(L, "max_row");
lua_pushinteger(L, m->max_row);
lua_rawset(L,-3);
lua_pushstring(L, "max_col");
lua_pushinteger(L, m->max_col);
lua_rawset(L,-3);
lua_pushstring(L, "grid_size");
lua_pushinteger(L, m->grid_size);
lua_rawset(L,-3);
lua_pushstring(L, "half_grid_flag");
lua_pushinteger(L, m->half_grid_flag);
lua_rawset(L,-3);
return 1;
}else {
if (!strcasecmp(which, "max_row")) {
lua_pushinteger(L, m->max_row);
return 1;
}else if(!strcasecmp(which, "max_col")) {
lua_pushinteger(L, m->max_col);
return 1;
}else if(!strcasecmp(which, "grid_size")) {
lua_pushinteger(L, m->grid_size);
return 1;
}else if(!strcasecmp(which, "half_grid_flag")) {
lua_pushinteger(L, m->half_grid_flag);
return 1;
}else {
return 0;
}
}
}
/* lweight
*
* query grid weight
*
* Arguments:
* L Lua state
*
* Lua Stack:
* 1 Map userdata
* 2 query row
* 3 query col
*
* Lua Returns:
* +3 x,y,z value of grid weight, if query pos is valid, nil otherwise.
*/
static int
lweight(lua_State *L) {
Map* m = check_map(L, 1);
int row = luaL_checknumber(L, 2);
int col = luaL_checknumber(L, 3);
if (!is_valid_grid(m, row, col)) {
return 0;
}
int wx = m->wt[row*m->max_col + col].x;
int wy = m->wt[row*m->max_col + col].y;
int wz = m->wt[row*m->max_col + col].z;
lua_pushinteger(L, wx);
lua_pushinteger(L, wy);
lua_pushinteger(L, wz);
return 3;
}
/* ldist
*
* cal shortest distance of two grids
*
* Arguments:
* L Lua state
*
* Lua Stack:
* 1 Map userdata
* 2 query start row
* 3 query start col
* 4 query end row
* 5 query end col
*
* Lua Returns:
* +1 shortest distance of two grids, if query two pos are valid, nil otherwise.
*/
static int
ldist(lua_State *L) {
Map* m = check_map(L, 1);
int srow = luaL_checknumber(L, 2);
int scol = luaL_checknumber(L, 3);
int erow = luaL_checknumber(L, 4);
int ecol = luaL_checknumber(L, 5);
if (!is_valid_grid(m, srow, scol) || !is_valid_grid(m, erow, ecol)) {
return 0;
}
int dis;
cal_grids_dist(m, srow, scol, erow, ecol, dis);
lua_pushinteger(L, dis);
return 1;
}
int
luaopen_cmap(lua_State* L) {
srand(time(NULL));
luaL_checkversion(L);
luaL_Reg l1[] = {
{"create", new_map},
{NULL, NULL},
};
luaL_Reg l2[] = {
{"find_path", lfind_path},
{"set_block", lset_block},
{"get_block", lget_block},
{"get_noblocking_pos", lget_noblocking_pos},
{"is_block", lis_block},
{"set_opencache", lset_opencache},
{"is_opencache", lis_opencache},
{"info", linfo},
{"weight", lweight},
{"dist", ldist},
{NULL,NULL},
};
luaL_newmetatable(L, MAP_METATABLE);
luaL_newlib(L, l2);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, _delete_map);
lua_setfield(L, -2, "__gc");
lua_pushcfunction(L, _tostring);
lua_setfield(L, -2, "__tostring");
luaL_newlib(L, l1);
return 1;
}