forked from ForsakenX/forsaken
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_bullets.c
384 lines (369 loc) · 9.78 KB
/
lua_bullets.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
/* Functions for accessing bullet data from Lua.
*
* OWNER_SHIP = 1
*
* for i, b in ipairs(bullets) do
* if b.Used then
* if b.OwnerType == OWNER_SHIP and b.Owner == me then
* print("Bullet " .. i .. " is mine")
* else
* print("Bullet " .. i .. " is NOT mine")
* end
* end
* end
*
* see also primary.h and secondary.h
*/
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include "main.h" /* FIXME -- needed for primary.h and secondary.h */
#include "2dtextures.h" /* FIXME -- needed for primary.h and secondary.h */
#include "new3d.h" /* FIXME -- needed for primary.h and secondary.h */
#include "object.h" /* FIXME -- needed for networking.h */
#include "networking.h" /* FIXME -- needed for primary.h and secondary.h */
#include "primary.h"
#include "secondary.h"
#include "lua_vecmat.h"
#include "lua_weapons.h"
extern PRIMARYWEAPONBULLET PrimBulls[MAXPRIMARYWEAPONBULLETS];
extern SECONDARYWEAPONBULLET SecBulls[MAXSECONDARYWEAPONBULLETS];
static void pushprimbull(lua_State *L, u_int16_t index)
{
int *id = lua_newuserdata(L, sizeof(int));
*id = (int) index;
luaL_getmetatable(L, "PRIMARYWEAPONBULLETIDX");
lua_setmetatable(L, -2);
}
static void pushsecbull(lua_State *L, u_int16_t index)
{
int *id = lua_newuserdata(L, sizeof(int));
*id = (int) index;
luaL_getmetatable(L, "SECONDARYWEAPONBULLETIDX");
lua_setmetatable(L, -2);
}
static int pushentity(lua_State *L, u_int16_t type, u_int16_t index)
{
const char *mt;
int *id;
switch (type)
{
case OWNER_SHIP: mt = "GLOBALSHIPIDX"; break;
case OWNER_ENEMY: mt = "ENEMYIDX"; break;
case OWNER_MINE: mt = "SECONDARYWEAPONBULLETIDX"; break;
default:
lua_pushnil(L);
lua_pushliteral(L, "unknown target type");
return 2;
}
id = lua_newuserdata(L, sizeof(int));
*id = index;
luaL_getmetatable(L, mt);
lua_setmetatable(L, -2);
return 1;
}
#define GETTABLEFORPRIM(index) do { \
lua_getglobal(L, "weapons"); \
lua_pushinteger(L, index + 1); \
lua_gettable(L, -2); \
lua_remove(L, -2); \
} while (0)
#define FIELD(f, type) do { \
if (!strcmp(name, #f)) \
{ \
lua_push ## type(L, bullet->f); \
return 1; \
} \
} while (0)
#define FIELDPTR(f, type) do { \
if (!strcmp(name, #f)) \
{ \
lua_push ## type(L, &bullet->f); \
return 1; \
} \
} while (0)
static int luaprimbull_index(lua_State *L)
{
PRIMARYWEAPONBULLET *bullet;
const char *name;
int *id;
int bullidx = *((int *) luaL_checkudata(L, 1, "PRIMARYWEAPONBULLETIDX"));
bullet = &PrimBulls[bullidx];
name = luaL_checkstring(L, 2);
/*if (!strcmp(name, "table")) -- TODO
{
lua_pushcfunction(L, luaprimbull_table);
return 1;
}
else*/ if (!strcmp(name, "index") || !strcmp(name, "Index"))
{
lua_pushinteger(L, bullidx + 1);
return 1;
}
else if (!strcmp(name, "category"))
{
lua_pushliteral(L, "primary");
return 1;
}
FIELD(Used, boolean);
if (!strcmp(name, "Next") && bullet->Next != (u_int16_t) -1)
{
pushprimbull(L, bullet->Next);
return 1;
}
else if (!strcmp(name, "Prev") && bullet->Prev != (u_int16_t) -1)
{
pushprimbull(L, bullet->Prev);
return 1;
}
else if (!strcmp(name, "Type"))
{
GETTABLEFORPRIM(bullet->Type);
return 1;
}
FIELD(OwnerType, integer);
if (!strcmp(name, "Owner"))
return pushentity(L, bullet->OwnerType, bullet->Owner);
/* FIELD(EnemyGun, ??); -- TODO */
if (!strcmp(name, "Weapon"))
{
GETTABLEFORPRIM(bullet->Weapon);
return 1;
}
FIELD(PowerLevel, integer);
FIELD(TrojPower, number);
FIELD(LifeCount, number);
FIELD(Speed, number);
FIELD(ColRadius, number);
FIELD(ColType, integer);
FIELDPTR(Offset, vector);
FIELDPTR(Pos, vector);
FIELDPTR(Dir, vector);
FIELDPTR(LocalDir, vector);
FIELDPTR(UpVector, vector);
FIELDPTR(ColStart, vector);
FIELD(ColDist, number);
FIELD(ColFlag, integer);
FIELDPTR(ColPoint, vector); /* VERT */
FIELDPTR(ColPointNormal, vector); /* NORMAL */
FIELD(ColGroup, integer);
FIELD(GroupImIn, integer);
FIELDPTR(Mat, matrix);
FIELD(line, integer);
FIELD(fmpoly, integer);
FIELD(numfmpolys, integer);
FIELD(poly, integer);
FIELD(numpolys, integer);
FIELD(light, integer);
FIELD(lightsize, number);
FIELD(r, number);
FIELD(g, number);
FIELD(b, number);
FIELD(Bounces, integer);
FIELD(TimeInterval, number);
FIELD(TimeCount, number);
FIELD(FirePoint, integer);
FIELD(SpotFX, integer);
FIELD(FramelagAddition, number);
return luaL_argerror(L, 2, "unknown field name");
}
#define GETTABLEFORSEC(index) do { \
lua_getglobal(L, "weapons"); \
lua_pushinteger(L, index + 1 + SECOFFSET); \
lua_gettable(L, -2); \
lua_remove(L, -2); \
} while (0)
#define SECFLAG(name) do { \
lua_pushboolean(L, bullet->Flags & SECFLAGS_ ## name); \
lua_setfield(L, -2, #name); \
} while (0)
static int luasecbull_index(lua_State *L)
{
SECONDARYWEAPONBULLET *bullet;
const char *name;
int *id;
int bullidx = *((int *) luaL_checkudata(L, 1, "SECONDARYWEAPONBULLETIDX"));
bullet = &SecBulls[bullidx];
name = luaL_checkstring(L, 2);
/*if (!strcmp(name, "table")) -- TODO
{
lua_pushcfunction(L, luasecbull_table);
return 1;
}
else*/ if (!strcmp(name, "index") || !strcmp(name, "Index"))
{
lua_pushinteger(L, bullidx + 1 + MAXPRIMARYWEAPONBULLETS);
return 1;
}
else if (!strcmp(name, "category"))
{
lua_pushliteral(L, "secondary");
return 1;
}
FIELD(Used, boolean);
if (!strcmp(name, "Next") && bullet->Next != (u_int16_t) -1)
{
pushsecbull(L, bullet->Next);
return 1;
}
else if (!strcmp(name, "Prev") && bullet->Prev != (u_int16_t) -1)
{
pushsecbull(L, bullet->Prev);
return 1;
}
else if (!strcmp(name, "NextInGroup") && bullet->NextInGroup != NULL)
{
pushsecbull(L, bullet->NextInGroup - SecBulls);
return 1;
}
else if (!strcmp(name, "PrevInGroup") && bullet->PrevInGroup != NULL)
{
pushsecbull(L, bullet->PrevInGroup - SecBulls);
return 1;
}
else if (!strcmp(name, "Flags"))
{
/* FIXME: not really consistent */
lua_createtable(L, 0, 3);
SECFLAG(CanShoot);
SECFLAG(CanTarget);
SECFLAG(NoIncoming);
return 1;
}
FIELD(SecType, integer);
if (!strcmp(name, "Type"))
{
GETTABLEFORSEC(bullet->Type);
return 1;
}
FIELD(MoveType, integer);
FIELD(State, integer);
FIELD(OwnerType, integer);
if (!strcmp(name, "Owner"))
return pushentity(L, bullet->OwnerType, bullet->Owner);
FIELD(ID, integer);
if (!strcmp(name, "Weapon"))
{
GETTABLEFORSEC(bullet->Weapon);
return 1;
}
FIELD(Lensflare, boolean);
FIELD(LifeCount, number);
FIELD(LifeSpan, number);
FIELD(SpeedInc, number);
FIELD(SpeedWanted, number);
FIELD(Speed, number);
FIELD(DropCount, number);
FIELDPTR(DropVector, vector);
FIELD(Size, number);
FIELDPTR(Pos, vector);
FIELDPTR(Offset, vector);
FIELDPTR(StartPos, vector);
FIELDPTR(StartDir, vector);
FIELDPTR(StartMat, matrix);
FIELDPTR(ColStart, vector);
FIELD(ColDist, number);
FIELD(ColFlag, integer);
FIELD(ColGroup, integer);
FIELDPTR(ColPoint, vector); /* VERT */
FIELDPTR(ColPointNormal, vector); /* NORMAL */
FIELD(GroupImIn, integer);
FIELD(ModelNum, integer);
FIELD(ModelIndex, integer);
FIELDPTR(Mat, matrix);
FIELD(fmpoly, integer);
FIELD(numfmpolys, integer);
FIELD(poly, integer);
FIELD(numpolys, integer);
FIELD(xsize, number);
FIELD(ysize, number);
FIELD(light, integer);
FIELD(lightsize, number);
FIELD(r, number);
FIELD(g, number);
FIELD(b, number);
FIELD(TurnSpeed, number);
FIELD(ViewCone, number);
FIELD(TargetType, integer);
if (!strcmp(name, "Target"))
return pushentity(L, bullet->TargetType, bullet->Target);
/* FIELDPTR(DirQuat, quat); -- TODO */
FIELDPTR(DirVector, vector);
FIELDPTR(UpVector, vector);
FIELD(Shield, number);
FIELD(Damage, number);
FIELD(Ammo, integer);
FIELD(NumBounces, integer);
FIELD(NumOldPos, integer);
FIELD(RetractPos, number);
FIELD(ColRadius, number);
FIELD(Interval, number);
FIELD(Time, number);
FIELD(FramelagAddition, 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 luaprimbull_equals(lua_State *L)
{
lua_pushboolean(L,
isudatatype(L, 1, "PRIMARYWEAPONBULLETIDX") &&
isudatatype(L, 2, "PRIMARYWEAPONBULLETIDX") &&
*((int *) lua_touserdata(L, 1)) == *((int *) lua_touserdata(L, 2))
);
return 1;
}
static int luasecbull_equals(lua_State *L)
{
lua_pushboolean(L,
isudatatype(L, 1, "SECONDARYWEAPONBULLETIDX") &&
isudatatype(L, 2, "SECONDARYWEAPONBULLETIDX") &&
*((int *) lua_touserdata(L, 1)) == *((int *) lua_touserdata(L, 2))
);
return 1;
}
int luaopen_bullets(lua_State *L)
{
static const luaL_Reg primbullmt[] = {
{ "__index", luaprimbull_index },
{ "__eq", luaprimbull_equals },
{ NULL, NULL }
}, secbullmt[] = {
{ "__index", luasecbull_index },
{ "__eq", luasecbull_equals },
{ NULL, NULL }
};
int i;
luaL_newmetatable(L, "PRIMARYWEAPONBULLETIDX");
luaL_register(L, NULL, primbullmt);
lua_pop(L, 1);
luaL_newmetatable(L, "SECONDARYWEAPONBULLETIDX");
luaL_register(L, NULL, secbullmt);
lua_pop(L, 1);
lua_createtable(L, MAXPRIMARYWEAPONBULLETS + MAXSECONDARYWEAPONBULLETS, 0);
for (i=0; i<MAXPRIMARYWEAPONBULLETS; i++)
{
lua_pushinteger(L, i+1);
pushprimbull(L, i);
lua_settable(L, -3);
}
for (i=0; i<MAXSECONDARYWEAPONBULLETS; i++)
{
lua_pushinteger(L, i+1 + MAXPRIMARYWEAPONBULLETS);
pushsecbull(L, i);
lua_settable(L, -3);
}
lua_setglobal(L, "bullets");
return 0;
}