naev 0.10.4
nlua_vec2.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
12#include <lauxlib.h>
13
14#include "naev.h"
17#include "nlua_vec2.h"
18
19#include "log.h"
20#include "nluadef.h"
21#include "collision.h"
22
23/* Vector metatable methods */
24static int vectorL_new( lua_State *L );
25static int vectorL_newP( lua_State *L );
26static int vectorL_tostring( lua_State *L );
27static int vectorL_add__( lua_State *L );
28static int vectorL_add( lua_State *L );
29static int vectorL_sub__( lua_State *L );
30static int vectorL_sub( lua_State *L );
31static int vectorL_mul__( lua_State *L );
32static int vectorL_mul( lua_State *L );
33static int vectorL_div__( lua_State *L );
34static int vectorL_div( lua_State *L );
35static int vectorL_dot( lua_State *L );
36static int vectorL_get( lua_State *L );
37static int vectorL_polar( lua_State *L );
38static int vectorL_set( lua_State *L );
39static int vectorL_setP( lua_State *L );
40static int vectorL_distance( lua_State *L );
41static int vectorL_distance2( lua_State *L );
42static int vectorL_mod( lua_State *L );
43static int vectorL_angle( lua_State *L );
44static int vectorL_normalize( lua_State *L );
45static int vectorL_collideLineLine( lua_State *L );
46static int vectorL_collideCircleLine( lua_State *L );
47static const luaL_Reg vector_methods[] = {
48 { "new", vectorL_new },
49 { "newP", vectorL_newP },
50 { "__tostring", vectorL_tostring },
51 { "__add", vectorL_add },
52 { "add", vectorL_add__ },
53 { "__sub", vectorL_sub },
54 { "sub", vectorL_sub__ },
55 { "__mul", vectorL_mul },
56 { "mul", vectorL_mul__ },
57 { "__div", vectorL_div },
58 { "div", vectorL_div__ },
59 { "dot", vectorL_dot },
60 { "get", vectorL_get },
61 { "polar", vectorL_polar },
62 { "set", vectorL_set },
63 { "setP", vectorL_setP },
64 { "dist", vectorL_distance },
65 { "dist2", vectorL_distance2 },
66 { "mod", vectorL_mod },
67 { "angle", vectorL_angle },
68 { "normalize", vectorL_normalize },
69 { "collideLineLine", vectorL_collideLineLine },
70 { "collideCircleLine", vectorL_collideCircleLine },
71 {0,0}
72};
80int nlua_loadVector( nlua_env env )
81{
82 nlua_register(env, VECTOR_METATABLE, vector_methods, 1);
83 return 0;
84}
85
113vec2* lua_tovector( lua_State *L, int ind )
114{
115 return (vec2*) lua_touserdata(L,ind);
116}
124vec2* luaL_checkvector( lua_State *L, int ind )
125{
126 if (lua_isvector(L,ind))
127 return (vec2*) lua_touserdata(L,ind);
128 luaL_typerror(L, ind, VECTOR_METATABLE);
129 return NULL;
130}
131
139vec2* lua_pushvector( lua_State *L, vec2 vec )
140{
141 vec2 *v = (vec2*) lua_newuserdata(L, sizeof(vec2));
142 *v = vec;
143 luaL_getmetatable(L, VECTOR_METATABLE);
144 lua_setmetatable(L, -2);
145 return v;
146}
147
155int lua_isvector( lua_State *L, int ind )
156{
157 int ret;
158
159 if (lua_getmetatable(L,ind)==0)
160 return 0;
161 lua_getfield(L, LUA_REGISTRYINDEX, VECTOR_METATABLE);
162
163 ret = 0;
164 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
165 ret = 1;
166
167 lua_pop(L, 2); /* remove both metatables */
168 return ret;
169}
170
182static int vectorL_new( lua_State *L )
183{
184 vec2 v;
185 double x, y;
186
187 if (!lua_isnoneornil(L,1))
188 x = luaL_checknumber(L,1);
189 else
190 x = 0.;
191
192 if (!lua_isnoneornil(L,2))
193 y = luaL_checknumber(L,2);
194 else
195 y = x;
196
197 vec2_cset( &v, x, y );
198 lua_pushvector(L, v);
199 return 1;
200}
201
213static int vectorL_newP( lua_State *L )
214{
215 vec2 v;
216 double m, a;
217
218 if (!lua_isnoneornil(L,1))
219 m = luaL_checknumber(L, 1);
220 else
221 m = 0.;
222
223 if (!lua_isnoneornil(L,2))
224 a = luaL_checknumber(L, 2);
225 else
226 a = 0.;
227
228 vec2_pset( &v, m, a );
229 lua_pushvector(L, v);
230 return 1;
231}
232
240static int vectorL_tostring( lua_State *L )
241{
242 char buf[STRMAX_SHORT];
243 vec2 *v = luaL_checkvector(L,1);
244 snprintf( buf, sizeof(buf), "vec2( %g, %g )", v->x, v->y );
245 lua_pushstring(L, buf);
246 return 1;
247}
248
265static int vectorL_add( lua_State *L )
266{
267 vec2 vout, *v1;
268 double x, y;
269
270 if (lua_isnumber(L,1)) {
271 x = y = lua_tonumber(L,1);
272 v1 = luaL_checkvector(L,2);
273 }
274 else {
275 /* Get self. */
276 v1 = luaL_checkvector(L,1);
277
278 /* Get rest of parameters. */
279 if (lua_isvector(L,2)) {
280 vec2 *v2 = lua_tovector(L,2);
281 x = v2->x;
282 y = v2->y;
283 }
284 else {
285 x = luaL_checknumber(L,2);
286 if (!lua_isnoneornil(L,3))
287 y = luaL_checknumber(L,3);
288 else
289 y = x;
290 }
291 }
292
293 /* Actually add it */
294 vec2_cset( &vout, v1->x + x, v1->y + y );
295 lua_pushvector( L, vout );
296
297 return 1;
298}
299static int vectorL_add__( lua_State *L )
300{
301 vec2 *v1;
302 double x, y;
303
304 /* Get self. */
305 v1 = luaL_checkvector(L,1);
306
307 /* Get rest of parameters. */
308 if (lua_isvector(L,2)) {
309 vec2 *v2 = lua_tovector(L,2);
310 x = v2->x;
311 y = v2->y;
312 }
313 else {
314 x = luaL_checknumber(L,2);
315 if (!lua_isnoneornil(L,3))
316 y = luaL_checknumber(L,3);
317 else
318 y = x;
319 }
320
321 /* Actually add it */
322 vec2_cset( v1, v1->x + x, v1->y + y );
323 lua_pushvector( L, *v1 );
324
325 return 1;
326}
327
344static int vectorL_sub( lua_State *L )
345{
346 vec2 vout, *v1;
347 double x, y;
348
349 /* Get self. */
350 v1 = luaL_checkvector(L,1);
351
352 /* Get rest of parameters. */
353 if (lua_isvector(L,2)) {
354 vec2 *v2 = lua_tovector(L,2);
355 x = v2->x;
356 y = v2->y;
357 }
358 else {
359 x = luaL_checknumber(L,2);
360 if (!lua_isnoneornil(L,3))
361 y = luaL_checknumber(L,3);
362 else
363 y = x;
364 }
365
366 /* Actually add it */
367 vec2_cset( &vout, v1->x - x, v1->y - y );
368 lua_pushvector( L, vout );
369 return 1;
370}
371static int vectorL_sub__( lua_State *L )
372{
373 vec2 *v1;
374 double x, y;
375
376 /* Get self. */
377 v1 = luaL_checkvector(L,1);
378
379 /* Get rest of parameters. */
380 if (lua_isvector(L,2)) {
381 vec2 *v2 = lua_tovector(L,2);
382 x = v2->x;
383 y = v2->y;
384 }
385 else {
386 x = luaL_checknumber(L,2);
387 if (!lua_isnoneornil(L,3))
388 y = luaL_checknumber(L,3);
389 else
390 y = x;
391 }
392
393 /* Actually add it */
394 vec2_cset( v1, v1->x - x, v1->y - y );
395 lua_pushvector( L, *v1 );
396 return 1;
397}
398
410static int vectorL_mul( lua_State *L )
411{
412 vec2 vout;
413
414 if (lua_isnumber(L,1)) {
415 double d = lua_tonumber(L,1);
416 vec2 *v = luaL_checkvector(L,2);
417 vec2_cset( &vout, v->x * d, v->y * d );
418 }
419 else {
420 if (lua_isnumber(L,2)) {
421 vec2 *v = luaL_checkvector(L,1);
422 double d = lua_tonumber(L,2);
423 vec2_cset( &vout, v->x * d, v->y * d );
424 }
425 else {
426 vec2 *v1 = luaL_checkvector(L,1);
427 vec2 *v2 = luaL_checkvector(L,2);
428 vec2_cset( &vout, v1->x * v2->x, v1->y * v2->y );
429 }
430 }
431
432 /* Actually add it */
433 lua_pushvector( L, vout );
434 return 1;
435}
436static int vectorL_mul__( lua_State *L )
437{
438 vec2 *v1;
439
440 /* Get parameters. */
441 v1 = luaL_checkvector(L,1);
442 if (lua_isnumber(L,2)) {
443 double mod = luaL_checknumber(L,2);
444 vec2_cset( v1, v1->x * mod, v1->y * mod );
445 }
446 else {
447 vec2 *v2 = luaL_checkvector(L,2);
448 vec2_cset( v1, v1->x * v2->x, v1->y * v2->y );
449 }
450
451 /* Actually add it */
452 lua_pushvector( L, *v1 );
453 return 1;
454}
455
467static int vectorL_div( lua_State *L )
468{
469 vec2 vout, *v1;
470
471 /* Get parameters. */
472 v1 = luaL_checkvector(L,1);
473 if (lua_isnumber(L,2)) {
474 double mod = lua_tonumber(L,2);
475 vec2_cset( &vout, v1->x / mod, v1->y / mod );
476 }
477 else {
478 vec2 *v2 = luaL_checkvector(L,2);
479 vec2_cset( &vout, v1->x / v2->x, v1->y / v2->y );
480 }
481
482 lua_pushvector( L, vout );
483 return 1;
484}
485static int vectorL_div__( lua_State *L )
486{
487 vec2 *v1;
488
489 /* Get parameters. */
490 v1 = luaL_checkvector(L,1);
491 if (lua_isnumber(L,2)) {
492 double mod = lua_tonumber(L,2);
493 vec2_cset( v1, v1->x / mod, v1->y / mod );
494 }
495 else {
496 vec2 *v2 = luaL_checkvector(L,2);
497 vec2_cset( v1, v1->x / v2->x, v1->y / v2->y );
498 }
499
500 lua_pushvector( L, *v1 );
501 return 1;
502}
503
512static int vectorL_dot( lua_State *L )
513{
514 vec2 *a = luaL_checkvector(L,1);
515 vec2 *b = luaL_checkvector(L,2);
516 lua_pushnumber( L, a->x*b->x + a->y*b->y );
517 return 1;
518}
519
530static int vectorL_get( lua_State *L )
531{
532 vec2 *v1 = luaL_checkvector(L,1);
533
534 /* Push the vector. */
535 lua_pushnumber(L, v1->x);
536 lua_pushnumber(L, v1->y);
537 return 2;
538}
539
552static int vectorL_polar( lua_State *L )
553{
554 vec2 *v1 = luaL_checkvector(L,1);
555 lua_pushnumber(L, VMOD(*v1));
556 lua_pushnumber(L, VANGLE(*v1));
557 return 2;
558}
559
570static int vectorL_set( lua_State *L )
571{
572 vec2 *v1;
573 double x, y;
574
575 /* Get parameters. */
576 v1 = luaL_checkvector(L,1);
577 x = luaL_checknumber(L,2);
578 y = luaL_checknumber(L,3);
579
580 vec2_cset( v1, x, y );
581 return 0;
582}
583
594static int vectorL_setP( lua_State *L )
595{
596 vec2 *v1;
597 double m, a;
598
599 /* Get parameters. */
600 v1 = luaL_checkvector(L,1);
601 m = luaL_checknumber(L,2);
602 a = luaL_checknumber(L,3);
603
604 vec2_pset( v1, m, a );
605 return 0;
606}
607
619static int vectorL_distance( lua_State *L )
620{
621 vec2 *v1, *v2;
622 double dist;
623
624 /* Get self. */
625 v1 = luaL_checkvector(L,1);
626
627 /* Get rest of parameters. */
628 if (!lua_isnoneornil(L,2))
629 v2 = luaL_checkvector(L,2);
630 else
631 v2 = NULL;
632
633 /* Get distance. */
634 if (v2 == NULL)
635 dist = vec2_odist(v1);
636 else
637 dist = vec2_dist(v1, v2);
638
639 /* Return the distance. */
640 lua_pushnumber(L, dist);
641 return 1;
642}
643
655static int vectorL_distance2( lua_State *L )
656{
657 vec2 *v1, *v2;
658 double dist2;
659
660 /* Get self. */
661 v1 = luaL_checkvector(L,1);
662
663 /* Get rest of parameters. */
664 if (!lua_isnoneornil(L,2))
665 v2 = luaL_checkvector(L,2);
666 else
667 v2 = NULL;
668
669 /* Get distance. */
670 if (v2 == NULL)
671 dist2 = vec2_odist2(v1);
672 else
673 dist2 = vec2_dist2(v1, v2);
674
675 /* Return the distance. */
676 lua_pushnumber(L, dist2);
677 return 1;
678}
679
686static int vectorL_mod( lua_State *L )
687{
688 vec2 *v = luaL_checkvector(L,1);
689 lua_pushnumber(L, VMOD(*v));
690 return 1;
691}
692
699static int vectorL_angle( lua_State *L )
700{
701 vec2 *v = luaL_checkvector(L,1);
702 lua_pushnumber(L, VANGLE(*v));
703 return 1;
704}
705
712static int vectorL_normalize( lua_State *L )
713{
714 vec2 *v = luaL_checkvector(L,1);
715 double m = VMOD(*v);
716 v->x /= m;
717 v->y /= m;
718 lua_pushvector(L, *v);
719 return 1;
720}
721
732static int vectorL_collideLineLine( lua_State *L )
733{
734 vec2 *s1 = luaL_checkvector(L,1);
735 vec2 *e1 = luaL_checkvector(L,2);
736 vec2 *s2 = luaL_checkvector(L,3);
737 vec2 *e2 = luaL_checkvector(L,4);
738 vec2 crash;
739 int ret = CollideLineLine( s1->x, s1->y, e1->x, e1->y, s2->x, s2->y, e2->x, e2->y, &crash );
740 lua_pushinteger( L, ret );
741 lua_pushvector( L, crash );
742 return 2;
743}
744
756static int vectorL_collideCircleLine( lua_State *L )
757{
758 vec2 *center, *p1, *p2, crash[2];
759 double radius;
760
761 center = luaL_checkvector( L, 1 );
762 radius = luaL_checknumber( L, 2 );
763 p1 = luaL_checkvector( L, 3 );
764 p2 = luaL_checkvector( L, 4 );
765
766 int cnt = CollideLineCircle( p1, p2, center, radius, crash );
767 if (cnt>0)
768 lua_pushvector( L, crash[0] );
769 if (cnt>1)
770 lua_pushvector( L, crash[1] );
771
772 return cnt;
773}
int CollideLineCircle(const vec2 *p1, const vec2 *p2, const vec2 *cc, double cr, vec2 crash[2])
Checks to see if a line collides with a circle.
Definition: collision.c:786
int CollideLineLine(double s1x, double s1y, double e1x, double e1y, double s2x, double s2y, double e2x, double e2y, vec2 *crash)
Checks to see if two lines collide.
Definition: collision.c:448
Header file with generic functions and naev-specifics.
static int vectorL_distance2(lua_State *L)
Gets the squared distance from the Vec2 (saves a sqrt())
Definition: nlua_vec2.c:655
static int vectorL_collideCircleLine(lua_State *L)
Computes the intersection of a line segment and a circle.
Definition: nlua_vec2.c:756
static int vectorL_collideLineLine(lua_State *L)
Sees if two line segments collide.
Definition: nlua_vec2.c:732
static const luaL_Reg vector_methods[]
Definition: nlua_vec2.c:47
static int vectorL_mod(lua_State *L)
Gets the modulus of the vector. Lua function parameter: Vec2 v Vector to get modulus of....
Definition: nlua_vec2.c:686
static int vectorL_dot(lua_State *L)
Dot product of two vectors.
Definition: nlua_vec2.c:512
static int vectorL_set(lua_State *L)
Sets the vector by cartesian coordinates.
Definition: nlua_vec2.c:570
static int vectorL_div(lua_State *L)
Divides a vector by a number.
Definition: nlua_vec2.c:467
static int vectorL_setP(lua_State *L)
Sets the vector by polar coordinates.
Definition: nlua_vec2.c:594
static int vectorL_angle(lua_State *L)
Gets the angle of the vector. Lua function parameter: Vec2 v Vector to get angle of....
Definition: nlua_vec2.c:699
static int vectorL_normalize(lua_State *L)
Normalizes a vector. Lua function parameter: Vec2 v Vector to normalize. Lua return parameter: Vec2 N...
Definition: nlua_vec2.c:712
static int vectorL_mul(lua_State *L)
Multiplies a vector by a number.
Definition: nlua_vec2.c:410
int nlua_loadVector(nlua_env env)
Loads the vector metatable.
Definition: nlua_vec2.c:80
int lua_isvector(lua_State *L, int ind)
Checks to see if ind is a vector.
Definition: nlua_vec2.c:155
static int vectorL_add(lua_State *L)
Adds two vectors or a vector and some cartesian coordinates.
Definition: nlua_vec2.c:265
static int vectorL_newP(lua_State *L)
Creates a new vector using polar coordinates.
Definition: nlua_vec2.c:213
static int vectorL_tostring(lua_State *L)
Converts a vector to a string.
Definition: nlua_vec2.c:240
static int vectorL_get(lua_State *L)
Gets the cartesian positions of the vector.
Definition: nlua_vec2.c:530
vec2 * luaL_checkvector(lua_State *L, int ind)
Gets vector at index making sure type is valid.
Definition: nlua_vec2.c:124
static int vectorL_sub(lua_State *L)
Subtracts two vectors or a vector and some cartesian coordinates.
Definition: nlua_vec2.c:344
static int vectorL_distance(lua_State *L)
Gets the distance from the Vec2.
Definition: nlua_vec2.c:619
static int vectorL_polar(lua_State *L)
Gets polar coordinates of a vector.
Definition: nlua_vec2.c:552
vec2 * lua_tovector(lua_State *L, int ind)
Represents a 2D vector in Lua.
Definition: nlua_vec2.c:113
vec2 * lua_pushvector(lua_State *L, vec2 vec)
Pushes a vector on the stack.
Definition: nlua_vec2.c:139
static int vectorL_new(lua_State *L)
Creates a new vector.
Definition: nlua_vec2.c:182
static const double a[]
Definition: rng.c:247
static const double d[]
Definition: rng.c:273
Represents a 2d vector.
Definition: vec2.h:32
double y
Definition: vec2.h:34
double x
Definition: vec2.h:33