forked from ForsakenX/forsaken
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_collision.c
101 lines (87 loc) · 2.43 KB
/
lua_collision.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
/* Lua bindings for collision detection functions.
*
* collided, endpos, endgroup, normal, newtarget =
* bgcollide(startpos, startgroup, move_off, bgcol)
*
* me = Ships[WhoIAm()]
* meobj = me.Object
* if wouldcollide(meobj, meobj.Mat * vec(0, 0, 100)) then
* print("I would collide if I moved forward 100 units")
* end
*
* see also collision.h
*/
#include <lua.h>
#include <lauxlib.h>
#include "object.h" /* FIXME: needed by collision.h */
#include "collision.h"
#include "ships.h" /* for SHIP_RADIUS */
#include "lua_vecmat.h"
extern MCLOADHEADER MCloadheadert0;
extern MLOADHEADER Mloadheader;
/* [-0, +1, m] */
#define NEWOBJ(L, objtype, var) do { \
var = lua_newuserdata(L, sizeof(objtype)); \
luaL_getmetatable(L, #objtype); \
lua_setmetatable(L, -2); \
} while (0)
static int luacoll_bgcollide(lua_State *L)
{
VECTOR *startpos, *move_off, *endpos, *normal, *newtarget;
u_int16_t startgroup, endgroup;
bool bgcol;
/* get input parameters */
startpos = luaL_checkudata(L, 1, "VECTOR");
startgroup = luaL_checkint(L, 2);
move_off = luaL_checkudata(L, 3, "VECTOR");
(void) luaL_checkint(L, 4);
bgcol = lua_toboolean(L, 4);
/* initialize userdata memory for output values */
NEWOBJ(L, VECTOR, endpos);
NEWOBJ(L, VECTOR, normal);
NEWOBJ(L, VECTOR, newtarget);
/* call collision check function and push result as first output value */
lua_pushboolean(L,
BackgroundCollide(
&MCloadheadert0, &Mloadheader, /* fixed arguments */
startpos, startgroup, move_off, /* inputs */
endpos, &endgroup, (NORMAL *) normal, newtarget, /* outputs */
bgcol, NULL
)
);
/* finally, push the other outputs */
lua_pushvector(L, endpos);
lua_pushinteger(L, endgroup);
lua_pushvector(L, normal);
lua_pushvector(L, newtarget);
return 5;
}
static int luacoll_wouldcollide(lua_State *L)
{
OBJECT *obj;
VECTOR *move_off;
float radius;
obj = luaL_checkudata(L, 1, "OBJECT");
move_off = luaL_checkudata(L, 2, "VECTOR");
if (lua_isnoneornil(L, 3))
radius = SHIP_RADIUS;
else
radius = luaL_checknumber(L, 3);
lua_pushboolean(L, WouldObjectCollide(obj, move_off, radius, NULL));
return 1;
}
int luaopen_collision(lua_State *L)
{
static const luaL_Reg funcs[] = {
{ "bgcollide", luacoll_bgcollide },
{ "wouldcollide", luacoll_wouldcollide },
{ NULL, NULL }
};
int i;
for (i=0; funcs[i].name; i++)
{
lua_pushcfunction(L, funcs[i].func);
lua_setglobal(L, funcs[i].name);
}
return 0;
}