-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlua_ships.c
315 lines (302 loc) · 8.49 KB
/
lua_ships.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
/* Functions for accessing ship data from Lua.
*
* me = Ships[WhoIAm()]
* print(me.Primary.name)
* t = me:table()
*
* No support for modifying (yet?)
*
* see also networking.h
*/
#include <lua.h>
#include <lauxlib.h>
#include "main.h"
#include "new3d.h"
#include "object.h"
#include "networking.h"
#include "2dtextures.h" /* FIXME -- needed for primary.h and secondary.h */
#include "primary.h"
#include "secondary.h"
#include "lua_vecmat.h"
#include "lua_weapons.h"
extern BYTE WhoIAm;
extern BYTE GameStatus[MAX_PLAYERS+1];
extern int8_t PrimaryToFireLookup[ MAXPRIMARYWEAPONS ];
extern int8_t SecondaryToFireLookup[ MAXSECONDARYWEAPONS ];
static void lua_pushobjptr(lua_State *L, void *v, const char *type)
{
void **vp = lua_newuserdata(L, sizeof(void *));
*vp = v;
luaL_getmetatable(L, type);
lua_setmetatable(L, -2);
}
#define GETTABLEFORPRIM(num) do { \
lua_getglobal(L, "weapons"); \
lua_pushinteger(L, PrimaryToFireLookup[num] + 1); \
lua_gettable(L, -2); \
lua_remove(L, -2); \
} while (0)
#define GETTABLEFORSEC(num) do { \
lua_getglobal(L, "weapons"); \
lua_pushinteger(L, SecondaryToFireLookup[num] + 1 + SECOFFSET); \
lua_gettable(L, -2); \
lua_remove(L, -2); \
} while (0)
#define FIELD(f, type) do { \
lua_push ## type(L, ship->f); \
lua_setfield(L, -2, #f); \
} while (0)
#define FIELDPTR(f, type) do { \
lua_push ## type(L, &ship->f); \
lua_setfield(L, -2, #f); \
} while (0)
static int luaship_table(lua_State *L)
{
int shipidx;
GLOBALSHIP *ship;
shipidx = *((int *) luaL_checkudata(L, 1, "GLOBALSHIPIDX"));
ship = &Ships[shipidx];
lua_createtable(L, 0, 43);
/* Get a table for the Object field */
/* This should work but apparently doesn't :-/ --
lua_pushobjptr(L, &ship->Object, "OBJECTPTR");
lua_getfield(L, -1, "table");
lua_pushvalue(L, -2);
lua_call(L, 1, 1);
*/
/* Get the C function that builds the table for Object... */
luaL_getmetatable(L, "OBJECTPTR");
lua_getfield(L, -1, "__index");
lua_pushobjptr(L, &ship->Object, "OBJECTPTR");
lua_pushliteral(L, "table");
lua_call(L, 2, 1);
/* ...and call it with the Object pointer */
lua_pushobjptr(L, &ship->Object, "OBJECTPTR");
lua_call(L, 1, 1);
lua_setfield(L, -3, "Object");
lua_pop(L, 1); /* Get rid of the metatable */
/* Add index field */
lua_pushinteger(L, shipidx);
lua_setfield(L, -2, "index");
/* Add status field */
lua_pushinteger(L, GameStatus[shipidx]);
lua_setfield(L, -2, "status");
FIELD(enable, integer);
FIELD(ShipThatLastKilledMe, integer);
FIELD(ShipThatLastHitMe, integer);
FIELD(NumMultiples, integer);
FIELD(StealthTime, number);
FIELD(Timer, number);
FIELD(InvulTimer, number);
FIELD(Invul, boolean);
FIELDPTR(LastAngle, vector);
FIELD(PrimBullIdCount, integer);
FIELD(SecBullIdCount, integer);
FIELD(PickupIdCount, integer);
FIELD(Damage, number);
/* FIELD(Primary, integer); -- no longer used */
/* FIELD(Secondary, integer); -- no longer used */
GETTABLEFORPRIM(ship->Primary);
lua_setfield(L, -2, "Primary");
GETTABLEFORSEC(ship->Secondary);
lua_setfield(L, -2, "Secondary");
FIELD(ModelNum, integer);
FIELD(Pickups, integer);
FIELD(RegenSlots, integer);
FIELD(Mines, integer);
FIELD(JustRecievedPacket, boolean);
FIELDPTR(LastMove, vector);
FIELDPTR(Move_Off, vector);
/* TODO: OrbModels (u_int16_t array) */
/* TODO: OrbAmmo (float array) */
FIELD(PrimPowerLevel, number);
FIELD(PrimID, integer);
FIELD(SecID, integer);
FIELD(SecWeapon, integer);
FIELD(headlights, boolean);
FIELD(DemoInterpolate, boolean);
FIELDPTR(OldPos, vector);
FIELDPTR(NextPos, vector);
/* TODO: FIELDPTR(OldQuat, quat); */
/* TODO: FIELDPTR(NextQuat, quat); */
FIELD(OldBank, number);
FIELD(NextBank, number);
FIELD(SuperNashramTimer, number);
/* TODO: TempLines? (u_int16_t array) */
FIELD(ShakeTimer, number);
FIELD(ShakeDirTimer, number);
FIELD(ShakeForce, number);
FIELD(PacketDelay, number);
FIELD(ShieldHullCount, integer);
FIELDPTR(RealPos, vector);
FIELD(RealGroup, integer);
return 1;
}
#undef FIELDPTR
#undef FIELD
#define FIELD(f, type) do { \
if (!strcmp(name, #f)) \
{ \
lua_push ## type(L, ship->f); \
return 1; \
} \
} while (0)
#define FIELDPTR(f, type) do { \
if (!strcmp(name, #f)) \
{ \
lua_push ## type(L, &ship->f); \
return 1; \
} \
} while (0)
static int luaship_index(lua_State *L)
{
GLOBALSHIP *ship;
const char *name;
int shipidx = *((int *) luaL_checkudata(L, 1, "GLOBALSHIPIDX"));
ship = &Ships[shipidx];
name = luaL_checkstring(L, 2);
if (!strcmp(name, "table"))
{
lua_pushcfunction(L, luaship_table);
return 1;
}
else if (!strcmp(name, "index"))
{
lua_pushinteger(L, shipidx);
return 1;
}
else if (!strcmp(name, "Object"))
{
lua_pushobjptr(L, &ship->Object, "OBJECTPTR");
return 1;
}
else if (!strcmp(name, "status"))
{
lua_pushinteger(L, GameStatus[shipidx]);
return 1;
}
FIELD(enable, integer);
FIELD(ShipThatLastKilledMe, integer);
FIELD(ShipThatLastHitMe, integer);
FIELD(NumMultiples, integer);
FIELD(StealthTime, number);
FIELD(Timer, number);
FIELD(InvulTimer, number);
FIELD(Invul, boolean);
FIELDPTR(LastAngle, vector);
FIELD(PrimBullIdCount, integer);
FIELD(SecBullIdCount, integer);
FIELD(PickupIdCount, integer);
FIELD(Damage, number);
/* FIELD(Primary, integer); -- no longer used */
/* FIELD(Secondary, integer); -- no longer used */
if (!strcmp(name, "Primary"))
{
GETTABLEFORPRIM(ship->Primary);
return 1;
}
if (!strcmp(name, "Secondary"))
{
GETTABLEFORSEC(ship->Secondary);
return 1;
}
FIELD(ModelNum, integer);
FIELD(Pickups, integer);
FIELD(RegenSlots, integer);
FIELD(Mines, integer);
FIELD(JustRecievedPacket, boolean);
FIELDPTR(LastMove, vector);
FIELDPTR(Move_Off, vector);
/* TODO: OrbModels (u_int16_t array) */
/* TODO: OrbAmmo (float array) */
FIELD(PrimPowerLevel, number);
FIELD(PrimID, integer);
FIELD(SecID, integer);
FIELD(SecWeapon, integer);
FIELD(headlights, boolean);
FIELD(DemoInterpolate, boolean);
FIELDPTR(OldPos, vector);
FIELDPTR(NextPos, vector);
/* TODO: FIELDPTR(OldQuat, quat); */
/* TODO: FIELDPTR(NextQuat, quat); */
FIELD(OldBank, number);
FIELD(NextBank, number);
FIELD(SuperNashramTimer, number);
/* TODO: TempLines? (u_int16_t array) */
FIELD(ShakeTimer, number);
FIELD(ShakeDirTimer, number);
return luaL_argerror(L, 2, "unknown field name");
}
#undef FIELDPTR
#undef FIELD
/* TODO: move to a common function file and export */
static bool isudatatype(lua_State *L, int index, const char *mt)
{
bool ret;
if (!lua_getmetatable(L, index))
return false;
lua_getfield(L, LUA_REGISTRYINDEX, mt);
ret = lua_rawequal(L, -1, -2);
lua_pop(L, 2);
return ret;
}
static int luaship_equals(lua_State *L)
{
lua_pushboolean(L,
isudatatype(L, 1, "GLOBALSHIPIDX") &&
isudatatype(L, 2, "GLOBALSHIPIDX") &&
*((int *) lua_touserdata(L, 1)) == *((int *) lua_touserdata(L, 2))
);
return 1;
}
static int luaship_arrayindex(lua_State *L)
{
GLOBALSHIP *ships;
int *shipidx;
int i;
ships = *((GLOBALSHIP **) luaL_checkudata(L, 1, "GLOBALSHIPARRAYPTR"));
i = luaL_checkint(L, 2);
if (i < 0 || i >= MAX_PLAYERS)
return luaL_argerror(L, 2, "invalid ship index");
shipidx = lua_newuserdata(L, sizeof(int));
*shipidx = i;
luaL_getmetatable(L, "GLOBALSHIPIDX");
lua_setmetatable(L, -2);
return 1;
}
static int luaship_arraylength(lua_State *L)
{
(void) luaL_checkudata(L, 1, "GLOBALSHIPARRAYPTR");
lua_pushinteger(L, sizeof(Ships)/sizeof(Ships[0]));
return 1;
}
static int luaship_whoiam(lua_State *L)
{
lua_pushinteger(L, WhoIAm);
return 1;
}
int luaopen_ships(lua_State *L)
{
static const luaL_Reg shiparrmt[] = {
{ "__index", luaship_arrayindex },
{ "__len", luaship_arraylength },
{ NULL, NULL }
};
static const luaL_Reg shipmt[] = {
{ "__index", luaship_index },
{ "__eq", luaship_equals },
{ NULL, NULL }
};
GLOBALSHIP **ships = lua_newuserdata(L, sizeof(void *));
*ships = Ships;
luaL_newmetatable(L, "GLOBALSHIPARRAYPTR");
luaL_register(L, NULL, shiparrmt);
lua_setmetatable(L, -2);
luaL_newmetatable(L, "GLOBALSHIPIDX");
luaL_register(L, NULL, shipmt);
lua_pop(L, 1);
lua_setglobal(L, "Ships");
lua_pushcfunction(L, luaship_whoiam);
lua_setglobal(L, "WhoIAm");
return 0;
}