-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua-gridaoi.c
143 lines (128 loc) · 3.75 KB
/
lua-gridaoi.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
#include "gridaoi/grid-aoi.h"
#include <lua.h>
#include <lauxlib.h>
int
_aoi_new(lua_State *L) {
int realwidth = luaL_checkinteger(L, 1);
int realhigh = luaL_checkinteger(L, 2);
int tile_len = luaL_checkinteger(L, 3);
int view_range = luaL_checkinteger(L, 4);
aoi_contextptr aoictx = aoi_new(realwidth, realhigh, tile_len, view_range);
if (aoictx == NULL) {
luaL_error(L,"[_aoi_new] realwidth:%d, realhigh:%d, tile_len:%d, view_range:%d", realwidth, realhigh, tile_len, view_range);
}
lua_pushlightuserdata(L, aoictx);
return 1;
}
int
_aoi_delete(lua_State *L) {
aoi_contextptr aoictx = lua_touserdata(L, 1);
aoi_delete(aoictx);
return 0;
}
int
_aoi_enter(lua_State *L) {
aoi_contextptr aoictx = lua_touserdata(L, 1);
int id = luaL_checkinteger(L, 2);
float curx = luaL_checknumber(L, 3);
float cury = luaL_checknumber(L, 4);
int layer = luaL_checkinteger(L, 5);
int viewLayer = luaL_checkinteger(L, 6);
struct int_valuevec enter_self, enter_other;
valuevec_init(&enter_self);
valuevec_init(&enter_other);
if (aoi_enter(aoictx, id, curx, cury, (short)layer,
(short)viewLayer, &enter_self, &enter_other) != 0) {
luaL_error(L, "[_aoi_enter] id:%d,x:%f,y:%f", id, curx, cury);
}
lua_createtable(L, valuevec_count(&enter_self), 0);
for (size_t i=0; i<valuevec_count(&enter_self); i++) {
lua_pushinteger(L, valuevec_get(&enter_self, i));
lua_rawseti(L, -2, i+1);
}
lua_createtable(L, valuevec_count(&enter_other), 0);
for (size_t i=0; i<valuevec_count(&enter_other); i++) {
lua_pushinteger(L, valuevec_get(&enter_other, i));
lua_rawseti(L, -2, i+1);
}
valuevec_free(&enter_self);
valuevec_free(&enter_other);
return 2;
}
int
_aoi_leave(lua_State *L) {
aoi_contextptr aoictx = lua_touserdata(L, 1);
int id = lua_tointeger(L, 2);
struct int_valuevec leave_other;
valuevec_init(&leave_other);
if (aoi_leave(aoictx, id, &leave_other) != 0) {
luaL_error(L, "[_aoi_leave] id:%d", id);
}
lua_createtable(L, valuevec_count(&leave_other), 0);
for (size_t i=0; i<valuevec_count(&leave_other); i++) {
lua_pushinteger(L, valuevec_get(&leave_other, i));
lua_rawseti(L, -2, i+1);
}
valuevec_free(&leave_other);
return 1;
}
int
_aoi_update(lua_State *L) {
aoi_contextptr aoictx = lua_touserdata(L, 1);
int id = lua_tointeger(L, 2);
float curx = luaL_checknumber(L, 3);
float cury = luaL_checknumber(L, 4);
struct int_valuevec enter, leave;
valuevec_init(&enter);
valuevec_init(&leave);
if (aoi_move(aoictx, id, curx, cury, &enter, &leave) != 0) {
luaL_error(L, "[_aoi_update] id:%d", id);
}
lua_createtable(L, valuevec_count(&enter), 0);
for (size_t i=0; i<valuevec_count(&enter); i++) {
lua_pushinteger(L, valuevec_get(&enter, i));
lua_rawseti(L, -2, i+1);
}
lua_createtable(L, valuevec_count(&leave), 0);
for (size_t i=0; i<valuevec_count(&leave); i++) {
lua_pushinteger(L, valuevec_get(&leave, i));
lua_rawseti(L, -2, i+1);
}
valuevec_free(&enter);
valuevec_free(&leave);
return 2;
}
int
_aoi_viewlist(lua_State *L) {
aoi_contextptr aoictx = lua_touserdata(L, 1);
int id = lua_tointeger(L, 2);
struct int_valuevec viewed;
valuevec_init(&viewed);
if (aoi_viewlist(aoictx, id, &viewed) != 0) {
luaL_error(L, "[_aoi_viewlist] id:%d", id);
}
lua_createtable(L, valuevec_count(&viewed), 0);
for (size_t i=0; i<valuevec_count(&viewed); i++) {
lua_pushinteger(L, valuevec_get(&viewed, i));
lua_rawseti(L, -2, i+1);
}
valuevec_free(&viewed);
return 1;
}
int
luaopen_gridaoi_c(lua_State *L)
{
luaL_checkversion(L);
luaL_Reg l[] = {
{ "aoi_new", _aoi_new},
{ "aoi_delete", _aoi_delete},
{ "aoi_enter", _aoi_enter},
{ "aoi_leave", _aoi_leave},
{ "aoi_update", _aoi_update},
{ "aoi_viewlist", _aoi_viewlist},
{ NULL, NULL },
};
lua_createtable(L, 0, (sizeof(l)) / sizeof(luaL_Reg) - 1);
luaL_setfuncs(L, l, 0);
return 1;
}