-
Notifications
You must be signed in to change notification settings - Fork 30
/
lua_controls.c
153 lines (148 loc) · 3.96 KB
/
lua_controls.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
/* Functions for accessing ship controls from Lua.
*
* print(controls.pitch)
* controls.yaw = 1
* t = controls.table()
*
* see also controls.h
*/
#include <lua.h>
#include <lauxlib.h>
#include "controls.h"
extern SHIPCONTROL control;
#define FIELD(f, type) do { \
lua_push ## type(L, ctrl->f); \
lua_setfield(L, -2, #f); \
} while (0)
static int luactrl_table(lua_State *L)
{
SHIPCONTROL *ctrl;
ctrl = *((SHIPCONTROL **) luaL_checkudata(L, 1, "SHIPCONTROLPTR"));
lua_createtable(L, 0, 24);
FIELD(pitch, number);
FIELD(yaw, number);
FIELD(roll, number);
FIELD(bank, number);
FIELD(right, number);
FIELD(up, number);
FIELD(forward, number);
FIELD(cruise_control, integer);
FIELD(turbo, integer);
FIELD(slide_mode, integer);
FIELD(roll_mode, integer);
FIELD(fire_primary, integer);
FIELD(fire_secondary, integer);
FIELD(fire_mine, integer);
FIELD(select_primary, integer);
FIELD(select_secondary, integer);
FIELD(select_next_primary, integer);
FIELD(select_prev_primary, integer);
FIELD(select_next_secondary, integer);
FIELD(select_prev_secondary, integer);
FIELD(drop_primary, integer);
FIELD(drop_secondary, integer);
FIELD(drop_shield, integer);
FIELD(drop_ammo, integer);
return 1;
}
#undef FIELD
#define FIELD(f, type) do { \
if (!strcmp(name, #f)) \
{ \
lua_push ## type(L, ctrl->f); \
return 1; \
} \
} while (0)
static int luactrl_index(lua_State *L)
{
SHIPCONTROL *ctrl;
const char *name;
ctrl = *((SHIPCONTROL **) luaL_checkudata(L, 1, "SHIPCONTROLPTR"));
name = luaL_checkstring(L, 2);
if (!strcmp(name, "table"))
{
lua_pushcfunction(L, luactrl_table);
return 1;
}
FIELD(pitch, number);
FIELD(yaw, number);
FIELD(roll, number);
FIELD(bank, number);
FIELD(right, number);
FIELD(up, number);
FIELD(forward, number);
FIELD(cruise_control, integer);
FIELD(turbo, integer);
FIELD(slide_mode, integer);
FIELD(roll_mode, integer);
FIELD(fire_primary, integer);
FIELD(fire_secondary, integer);
FIELD(fire_mine, integer);
FIELD(select_primary, integer);
FIELD(select_secondary, integer);
FIELD(select_next_primary, integer);
FIELD(select_prev_primary, integer);
FIELD(select_next_secondary, integer);
FIELD(select_prev_secondary, integer);
FIELD(drop_primary, integer);
FIELD(drop_secondary, integer);
FIELD(drop_shield, integer);
FIELD(drop_ammo, integer);
return 1;
}
#undef FIELD
#define FIELD(f, type) do { \
if (!strcmp(name, #f)) \
{ \
ctrl->f = luaL_check ## type(L, 3); \
return 0; \
} \
} while (0)
static int luactrl_newindex(lua_State *L)
{
SHIPCONTROL *ctrl;
const char *name;
ctrl = *((SHIPCONTROL **) luaL_checkudata(L, 1, "SHIPCONTROLPTR"));
name = luaL_checkstring(L, 2);
FIELD(pitch, number);
FIELD(yaw, number);
FIELD(roll, number);
FIELD(bank, number);
FIELD(right, number);
FIELD(up, number);
FIELD(forward, number);
FIELD(cruise_control, integer);
FIELD(turbo, integer);
FIELD(slide_mode, integer);
FIELD(roll_mode, integer);
FIELD(fire_primary, integer);
FIELD(fire_secondary, integer);
FIELD(fire_mine, integer);
FIELD(select_primary, integer);
FIELD(select_secondary, integer);
FIELD(select_next_primary, integer);
FIELD(select_prev_primary, integer);
FIELD(select_next_secondary, integer);
FIELD(select_prev_secondary, integer);
FIELD(drop_primary, integer);
FIELD(drop_secondary, integer);
FIELD(drop_shield, integer);
FIELD(drop_ammo, integer);
return 1;
}
#undef FIELD
int luaopen_controls(lua_State *L)
{
static const luaL_Reg ctrlmt[] = {
{ "__index", luactrl_index },
{ "__newindex", luactrl_newindex },
{ NULL, NULL }
};
SHIPCONTROL **ctrl = lua_newuserdata(L, sizeof(void *));
*ctrl = &control;
luaL_newmetatable(L, "SHIPCONTROLPTR");
luaL_register(L, NULL, ctrlmt);
lua_setmetatable(L, -2);
lua_setglobal(L, "control");
return 0;
}