/
nlua_vec2.c
521 lines (457 loc) · 11.9 KB
/
nlua_vec2.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
* See Licensing and Copyright notice in naev.h
*/
/**
* @file nlua_space.c
*
* @brief Handles the Lua space bindings.
*
* These bindings control the planets and systems.
*/
#include "nlua_vec2.h"
#include "naev.h"
#include <lauxlib.h>
#include "nlua.h"
#include "nluadef.h"
#include "log.h"
/* Vector metatable methods */
static int vectorL_new( lua_State *L );
static int vectorL_add( lua_State *L );
static int vectorL_sub( lua_State *L );
static int vectorL_mul( lua_State *L );
static int vectorL_div( lua_State *L );
static int vectorL_get( lua_State *L );
static int vectorL_polar( lua_State *L );
static int vectorL_set( lua_State *L );
static int vectorL_setP( lua_State *L );
static int vectorL_distance( lua_State *L );
static int vectorL_distance2( lua_State *L );
static int vectorL_mod( lua_State *L );
static const luaL_reg vector_methods[] = {
{ "new", vectorL_new },
{ "__add", vectorL_add },
{ "add", vectorL_add },
{ "__sub", vectorL_sub },
{ "sub", vectorL_sub },
{ "__mul", vectorL_mul },
{ "mul", vectorL_mul },
{ "__div", vectorL_div },
{ "div", vectorL_div },
{ "get", vectorL_get },
{ "polar", vectorL_polar },
{ "setP", vectorL_set },
{ "setP", vectorL_setP },
{ "dist", vectorL_distance },
{ "dist2", vectorL_distance2 },
{ "mod", vectorL_mod },
{0,0}
}; /**< Vector metatable methods. */
/**
* @brief Loads the vector metatable.
*
* @param L State to load the vector metatable into.
* @return 0 on success.
*/
int nlua_loadVector( lua_State *L )
{
/* Create the metatable */
luaL_newmetatable(L, VECTOR_METATABLE);
/* Create the access table */
lua_pushvalue(L,-1);
lua_setfield(L,-2,"__index");
/* Register the values */
luaL_register(L, NULL, vector_methods);
/* Clean up. */
lua_setfield(L, LUA_GLOBALSINDEX, VECTOR_METATABLE);
return 0;
}
/**
* @brief Represents a 2D vector in Lua.
*
* This module allows you to manipulate 2D vectors. Usage is generally as follows:
*
* @code
* my_vec = vec2.new( 3, 2 ) -- my_vec is now (3,2)
* my_vec:add( 5, 3 ) -- my_vec is now (8,5)
* my_vec = my_vec * 3 -- my_vec is now (24,15)
* your_vec = vec2.new( 5, 2 ) -- your_vec is now (5,2)
* my_vec = my_vec - your_vec -- my_vec is now (19,13)
* @endcode
*
* @luamod vec2
*
* To call members of the metatable always use:
* @code
* vector:function( param )
* @endcode
*/
/**
* @brief Gets vector at index.
*
* @param L Lua state to get vector from.
* @param ind Index position of vector.
* @return The LuaVector at ind.
*/
LuaVector* lua_tovector( lua_State *L, int ind )
{
return (LuaVector*) lua_touserdata(L,ind);
}
/**
* @brief Gets vector at index making sure type is valid.
*
* @param L Lua state to get vector from.
* @param ind Index position of vector.
* @return The LuaVector at ind.
*/
LuaVector* luaL_checkvector( lua_State *L, int ind )
{
if (lua_isvector(L,ind))
return (LuaVector*) lua_touserdata(L,ind);
luaL_typerror(L, ind, VECTOR_METATABLE);
return NULL;
}
/**
* @brief Pushes a vector on the stack.
*
* @param L Lua state to push vector onto.
* @param vec Vector to push.
* @return Vector just pushed.
*/
LuaVector* lua_pushvector( lua_State *L, LuaVector vec )
{
LuaVector *v;
v = (LuaVector*) lua_newuserdata(L, sizeof(LuaVector));
*v = vec;
luaL_getmetatable(L, VECTOR_METATABLE);
lua_setmetatable(L, -2);
return v;
}
/**
* @brief Checks to see if ind is a vector.
*
* @param L Lua state to check.
* @param ind Index position to check.
* @return 1 if there is a vector at index position.
*/
int lua_isvector( lua_State *L, int ind )
{
int ret;
if (lua_getmetatable(L,ind)==0)
return 0;
lua_getfield(L, LUA_REGISTRYINDEX, VECTOR_METATABLE);
ret = 0;
if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
ret = 1;
lua_pop(L, 2); /* remove both metatables */
return ret;
}
/**
* @brief Creates a new vector.
*
* @usage vec2.new( 5, 3 ) -- creates a vector at (5,3)
* @usage vec2.new() -- creates a vector at (0,0)
*
* @luaparam x If set, the X value for the new vector.
* @luaparam y If set, the Y value for the new vector.
* @luareturn The new vector.
* @luafunc new( x, y )
*/
static int vectorL_new( lua_State *L )
{
LuaVector v;
double x, y;
if ((lua_gettop(L) > 1) && lua_isnumber(L,1) && lua_isnumber(L,2)) {
x = lua_tonumber(L,1);
y = lua_tonumber(L,2);
}
else {
x = 0.;
y = 0.;
}
vect_cset( &v.vec, x, y );
lua_pushvector(L, v);
return 1;
}
/**
* @brief Adds two vectors or a vector and some cartesian coordinates.
*
* If x is a vector it adds both vectors, otherwise it adds cartesian coordinates
* to the vector.
*
* @usage my_vec = my_vec + your_vec
* @usage my_vec:add( your_vec )
* @usage my_vec:add( 5, 3 )
*
* @luaparam v Vector getting stuff subtracted from.
* @luaparam x X coordinate or vector to add to.
* @luaparam y Y coordinate or nil to add to.
* @luareturn The result of the vector operation.
* @luafunc add( v, x, y )
*/
static int vectorL_add( lua_State *L )
{
LuaVector vout, *v1, *v2;
double x, y;
/* Get self. */
v1 = luaL_checkvector(L,1);
/* Get rest of parameters. */
v2 = NULL;
if (lua_isvector(L,2)) {
v2 = lua_tovector(L,2);
x = v2->vec.x;
y = v2->vec.y;
}
else if ((lua_gettop(L) > 2) && lua_isnumber(L,2) && lua_isnumber(L,3)) {
x = lua_tonumber(L,2);
y = lua_tonumber(L,3);
}
else {
NLUA_INVALID_PARAMETER(L);
return 0;
}
/* Actually add it */
vect_cset( &vout.vec, v1->vec.x + x, v1->vec.y + y );
lua_pushvector( L, vout );
return 1;
}
/**
* @brief Subtracts two vectors or a vector and some cartesian coordinates.
*
* If x is a vector it subtracts both vectors, otherwise it subtracts cartesian
* coordinates to the vector.
*
* @usage my_vec = my_vec - your_vec
* @usage my_vec:sub( your_vec )
* @usage my_vec:sub( 5, 3 )
*
* @luaparam v Vector getting stuff subtracted from.
* @luaparam x X coordinate or vector to subtract.
* @luaparam y Y coordinate or nil to subtract.
* @luareturn The result of the vector operation.
* @luafunc sub( v, x, y )
*/
static int vectorL_sub( lua_State *L )
{
LuaVector vout, *v1, *v2;
double x, y;
/* Get self. */
v1 = luaL_checkvector(L,1);
/* Get rest of parameters. */
v2 = NULL;
if (lua_isvector(L,2)) {
v2 = lua_tovector(L,2);
x = v2->vec.x;
y = v2->vec.y;
}
else if ((lua_gettop(L) > 2) && lua_isnumber(L,2) && lua_isnumber(L,3)) {
x = lua_tonumber(L,2);
y = lua_tonumber(L,3);
}
else NLUA_INVALID_PARAMETER(L);
/* Actually add it */
vect_cset( &vout.vec, v1->vec.x - x, v1->vec.y - y );
lua_pushvector( L, vout );
return 1;
}
/**
* @brief Multiplies a vector by a number.
*
* @usage my_vec = my_vec * 3
* @usage my_vec:mul( 3 )
*
* @luaparam v Vector to multiply.
* @luaparam mod Amount to multiply by.
* @luareturn The result of the vector operation.
* @luafunc mul( v, mod )
*/
static int vectorL_mul( lua_State *L )
{
LuaVector vout, *v1;
double mod;
/* Get parameters. */
v1 = luaL_checkvector(L,1);
mod = luaL_checknumber(L,2);
/* Actually add it */
vect_cset( &vout.vec, v1->vec.x * mod, v1->vec.y * mod );
lua_pushvector( L, vout );
return 1;
}
/**
* @brief Divides a vector by a number.
*
* @usage my_vec = my_vec / 3
* @usage my_vec:div(3)
*
* @luaparam v Vector to divide.
* @luaparam mod Amount to divide by.
* @luareturn The result of the vector operation.
* @luafunc div( v, mod )
*/
static int vectorL_div( lua_State *L )
{
LuaVector vout, *v1;
double mod;
/* Get parameters. */
v1 = luaL_checkvector(L,1);
mod = luaL_checknumber(L,2);
/* Actually add it */
vect_cset( &vout.vec, v1->vec.x / mod, v1->vec.y / mod );
lua_pushvector( L, vout );
return 1;
}
/**
* @brief Gets the cartesian positions of the vector.
*
* @usage x,y = my_vec:get()
*
* @luaparam v Vector to get position of.
* @luareturn X and Y position of the vector.
* @luafunc get(v)
*/
static int vectorL_get( lua_State *L )
{
LuaVector *v1;
/* Get self. */
v1 = luaL_checkvector(L,1);
/* Push the vector. */
lua_pushnumber(L, v1->vec.x);
lua_pushnumber(L, v1->vec.y);
return 2;
}
/**
* @brief Gets polar coordinates of a vector.
*
* The angle is in degrees, not radians.
*
* @usage modulus, angle = my_vec:polar()
*
* @luaparam v Vector to get polar coordinates of.
* @luareturn The modulus and angle of the vector.
* @luafunc polar(v)
*/
static int vectorL_polar( lua_State *L )
{
LuaVector *v1;
/* Get self. */
v1 = luaL_checkvector(L,1);
lua_pushnumber(L, VMOD(v1->vec));
lua_pushnumber(L, VANGLE(v1->vec)*180./M_PI);
return 2;
}
/**
* @brief Sets the vector by cartesian coordinates.
*
* @usage my_vec:set(5, 3) -- my_vec is now (5,3)
*
* @luaparam v Vector to set coordinates of.
* @luaparam x X coordinate to set.
* @luaparam y Y coordinate to set.
* @luafunc set( v, x, y )
*/
static int vectorL_set( lua_State *L )
{
LuaVector *v1;
double x, y;
/* Get parameters. */
v1 = luaL_checkvector(L,1);
x = luaL_checknumber(L,2);
y = luaL_checknumber(L,3);
vect_cset( &v1->vec, x, y );
return 0;
}
/**
* @brief Sets the vector by cartesian coordinates.
*
* @usage my_vec:setP( 1, math.pi ) -- my_vec is now (0,1)
*
* @luaparam v Vector to set coordinates of.
* @luaparam m Modulus to set.
* @luaparam a Angle to set.
* @luafunc setP( v, m, a )
*/
static int vectorL_setP( lua_State *L )
{
LuaVector *v1;
double m, a;
/* Get parameters. */
v1 = luaL_checkvector(L,1);
m = luaL_checknumber(L,2);
a = luaL_checknumber(L,3)/180.*M_PI;
vect_pset( &v1->vec, m, a );
return 0;
}
/**
* @brief Gets the distance from the Vec2.
*
* @usage my_vec:dist() -- Gets length of the vector (distance from origin).
* @usage my_vec:dist( your_vec ) -- Gets distance from both vectors (your_vec - my_vec).
*
* @luaparam v Vector to act as origin.
* @luaparam v2 Vector to get distance from, uses origin (0,0) if not set.
* @luareturn The distance calculated.
* @luafunc dist( v, v2 )
*/
static int vectorL_distance( lua_State *L )
{
LuaVector *v1, *v2;
double dist;
/* Get self. */
v1 = luaL_checkvector(L,1);
/* Get rest of parameters. */
v2 = NULL;
if (lua_gettop(L) > 1) {
v2 = luaL_checkvector(L,2);
}
/* Get distance. */
if (v2 == NULL)
dist = vect_odist(&v1->vec);
else
dist = vect_dist(&v1->vec, &v2->vec);
/* Return the distance. */
lua_pushnumber(L, dist);
return 1;
}
/**
* @brief Gets the squared distance from the Vec2 (saves a sqrt())
*
* @usage my_vec:dist2() -- Gets squared length of the vector (distance squared from origin).
* @usage my_vec:dist2( your_vec ) -- Gets squared distance from both vectors (your_vec - my_vec)^2.
*
* @luaparam v Vector to act as origin.
* @luaparam v2 Vector to get squared distance from, uses origin (0,0) if not set.
* @luareturn The distance calculated.
* @luafunc dist2( v, v2 )
*/
static int vectorL_distance2( lua_State *L )
{
LuaVector *v1, *v2;
double dist2;
/* Get self. */
v1 = luaL_checkvector(L,1);
/* Get rest of parameters. */
v2 = NULL;
if (lua_gettop(L) > 1) {
v2 = luaL_checkvector(L,2);
}
/* Get distance. */
if (v2 == NULL)
dist2 = vect_odist2(&v1->vec);
else
dist2 = vect_dist2(&v1->vec, &v2->vec);
/* Return the distance. */
lua_pushnumber(L, dist2);
return 1;
}
/**
* @brief Gets the modulus of the vector.
* @luaparam v Vector to get modulus of.
* @luareturn The modulus of the vector.
* @luafunc mod(v)
*/
static int vectorL_mod( lua_State *L )
{
LuaVector *v;
v = luaL_checkvector(L,1);
lua_pushnumber(L, VMOD(v->vec));
return 1;
}