1 /*
2 * See Licensing and Copyright notice in naev.h
3 */
4
5 /**
6 * @file nlua_system.c
7 *
8 * @brief Lua system module.
9 */
10
11 #include "nlua_system.h"
12
13 #include "naev.h"
14
15 #include <lauxlib.h>
16
17 #include "nluadef.h"
18 #include "nlua_faction.h"
19 #include "nlua_vec2.h"
20 #include "nlua_planet.h"
21 #include "nlua_jump.h"
22 #include "log.h"
23 #include "rng.h"
24 #include "land.h"
25 #include "land_outfits.h"
26 #include "map.h"
27 #include "map_overlay.h"
28 #include "space.h"
29
30
31 /* System metatable methods */
32 static int systemL_cur( lua_State *L );
33 static int systemL_get( lua_State *L );
34 static int systemL_getAll( lua_State *L );
35 static int systemL_eq( lua_State *L );
36 static int systemL_name( lua_State *L );
37 static int systemL_faction( lua_State *L );
38 static int systemL_nebula( lua_State *L );
39 static int systemL_jumpdistance( lua_State *L );
40 static int systemL_jumpPath( lua_State *L );
41 static int systemL_adjacent( lua_State *L );
42 static int systemL_jumps( lua_State *L );
43 static int systemL_presences( lua_State *L );
44 static int systemL_planets( lua_State *L );
45 static int systemL_presence( lua_State *L );
46 static int systemL_radius( lua_State *L );
47 static int systemL_isknown( lua_State *L );
48 static int systemL_setknown( lua_State *L );
49 static int systemL_mrkClear( lua_State *L );
50 static int systemL_mrkAdd( lua_State *L );
51 static int systemL_mrkRm( lua_State *L );
52 static const luaL_reg system_methods[] = {
53 { "cur", systemL_cur },
54 { "get", systemL_get },
55 { "getAll", systemL_getAll },
56 { "__eq", systemL_eq },
57 { "__tostring", systemL_name },
58 { "name", systemL_name },
59 { "faction", systemL_faction },
60 { "nebula", systemL_nebula },
61 { "jumpDist", systemL_jumpdistance },
62 { "jumpPath", systemL_jumpPath },
63 { "adjacentSystems", systemL_adjacent },
64 { "jumps", systemL_jumps },
65 { "presences", systemL_presences },
66 { "planets", systemL_planets },
67 { "presence", systemL_presence },
68 { "radius", systemL_radius },
69 { "known", systemL_isknown },
70 { "setKnown", systemL_setknown },
71 { "mrkClear", systemL_mrkClear },
72 { "mrkAdd", systemL_mrkAdd },
73 { "mrkRm", systemL_mrkRm },
74 {0,0}
75 }; /**< System metatable methods. */
76
77
78 /**
79 * @brief Loads the system library.
80 *
81 * @param env Environment to load system library into.
82 * @return 0 on success.
83 */
nlua_loadSystem(nlua_env env)84 int nlua_loadSystem( nlua_env env )
85 {
86 nlua_register(env, SYSTEM_METATABLE, system_methods, 1);
87 return 0; /* No error */
88 }
89
90
91 /**
92 * @brief Lua system module.
93 *
94 * This module allows you to use the Star Systems from Lua.
95 *
96 * Typical example would be something like:
97 * @code
98 * cur = system.get() -- Gets current system
99 * sys = system.get( "Gamma Polaris" )
100 * @endcode
101 *
102 * @luamod system
103 */
104 /**
105 * @brief Gets system at index.
106 *
107 * @param L Lua state to get system from.
108 * @param ind Index position of system.
109 * @return The LuaSystem at ind.
110 */
lua_tosystem(lua_State * L,int ind)111 LuaSystem lua_tosystem( lua_State *L, int ind )
112 {
113 return *((LuaSystem*) lua_touserdata(L,ind));
114 }
115 /**
116 * @brief Gets system at index raising an error if type doesn't match.
117 *
118 * @param L Lua state to get system from.
119 * @param ind Index position of system.
120 * @return The LuaSystem at ind.
121 */
luaL_checksystem(lua_State * L,int ind)122 LuaSystem luaL_checksystem( lua_State *L, int ind )
123 {
124 if (lua_issystem(L,ind))
125 return lua_tosystem(L,ind);
126 luaL_typerror(L, ind, SYSTEM_METATABLE);
127 return 0;
128 }
129
130 /**
131 * @brief Gets system (or system name) at index raising an error if type doesn't match.
132 *
133 * @param L Lua state to get system from.
134 * @param ind Index position of system.
135 * @return The System at ind.
136 */
luaL_validsystem(lua_State * L,int ind)137 StarSystem* luaL_validsystem( lua_State *L, int ind )
138 {
139 LuaSystem ls;
140 StarSystem *s;
141
142 if (lua_issystem(L, ind)) {
143 ls = luaL_checksystem(L, ind);
144 s = system_getIndex( ls );
145 }
146 else if (lua_isstring(L, ind))
147 s = system_get( lua_tostring(L, ind) );
148 else {
149 luaL_typerror(L, ind, SYSTEM_METATABLE);
150 return NULL;
151 }
152
153 if (s == NULL)
154 NLUA_ERROR(L, "System is invalid");
155
156 return s;
157 }
158
159 /**
160 * @brief Pushes a system on the stack.
161 *
162 * @param L Lua state to push system onto.
163 * @param sys System to push.
164 * @return System just pushed.
165 */
lua_pushsystem(lua_State * L,LuaSystem sys)166 LuaSystem* lua_pushsystem( lua_State *L, LuaSystem sys )
167 {
168 LuaSystem *s;
169 s = (LuaSystem*) lua_newuserdata(L, sizeof(LuaSystem));
170 *s = sys;
171 luaL_getmetatable(L, SYSTEM_METATABLE);
172 lua_setmetatable(L, -2);
173 return s;
174 }
175
176 /**
177 * @brief Checks to see if ind is a system.
178 *
179 * @param L Lua state to check.
180 * @param ind Index position to check.
181 * @return 1 if there is a system at index position.
182 */
lua_issystem(lua_State * L,int ind)183 int lua_issystem( lua_State *L, int ind )
184 {
185 int ret;
186
187 if (lua_getmetatable(L,ind)==0)
188 return 0;
189 lua_getfield(L, LUA_REGISTRYINDEX, SYSTEM_METATABLE);
190
191 ret = 0;
192 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
193 ret = 1;
194
195 lua_pop(L, 2); /* remove both metatables */
196 return ret;
197 }
198
199
200 /**
201 * @brief Gets the current system.
202 *
203 * @usage sys = system.cur() -- Gets the current system
204 *
205 * @luatreturn System Current system.
206 * @luafunc cur()
207 */
systemL_cur(lua_State * L)208 static int systemL_cur( lua_State *L )
209 {
210 lua_pushsystem(L,system_index( cur_system ));
211 return 1;
212 }
213
214
215 /**
216 * @brief Gets a system.
217 *
218 * Behaves differently depending on what you pass as param: <br/>
219 * - string : Gets the system by name. <br/>
220 * - planet : Gets the system by planet. <br/>
221 *
222 * @usage sys = system.get( p ) -- Gets system where planet 'p' is located.
223 * @usage sys = system.get( "Gamma Polaris" ) -- Gets the system by name.
224 *
225 * @luatparam string|Planet param Read description for details.
226 * @luatreturn System System matching param.
227 * @luafunc get( param )
228 */
systemL_get(lua_State * L)229 static int systemL_get( lua_State *L )
230 {
231 StarSystem *ss;
232 Planet *pnt;
233
234 /* Passing a string (systemname) */
235 if (lua_isstring(L,1)) {
236 ss = system_get( lua_tostring(L,1) );
237 }
238 /* Passing a planet */
239 else if (lua_isplanet(L,1)) {
240 pnt = luaL_validplanet(L,1);
241 ss = system_get( planet_getSystem( pnt->name ) );
242 }
243 else NLUA_INVALID_PARAMETER(L);
244
245 /* Error checking. */
246 if (ss == NULL) {
247 NLUA_ERROR(L, "No matching systems found.");
248 return 0;
249 }
250
251 /* return the system */
252 lua_pushsystem(L,system_index(ss));
253 return 1;
254 }
255
256 /**
257 * @brief Gets all the systems.
258 * @luatreturn {System,...} A list of all the systems.
259 * @luafunc getAll()
260 */
systemL_getAll(lua_State * L)261 static int systemL_getAll( lua_State *L )
262 {
263 StarSystem *sys;
264 int i, ind, n;
265
266 lua_newtable(L);
267 sys = system_getAll( &n );
268
269 ind = 1;
270 for (i=0; i<n; i++) {
271 lua_pushnumber( L, ind++ );
272 lua_pushsystem( L, system_index( &sys[i] ) );
273 lua_settable( L, -3 );
274 }
275 return 1;
276 }
277
278 /**
279 * @brief Check systems for equality.
280 *
281 * Allows you to use the '==' operator in Lua with systems.
282 *
283 * @usage if sys == system.get( "Draygar" ) then -- Do something
284 *
285 * @luatparam System s System comparing.
286 * @luatparam System comp System to compare against.
287 * @luatreturn boolean true if both systems are the same.
288 * @luafunc __eq( s, comp )
289 */
systemL_eq(lua_State * L)290 static int systemL_eq( lua_State *L )
291 {
292 LuaSystem a, b;
293 a = luaL_checksystem(L,1);
294 b = luaL_checksystem(L,2);
295 if (a == b)
296 lua_pushboolean(L,1);
297 else
298 lua_pushboolean(L,0);
299 return 1;
300 }
301
302 /**
303 * @brief Returns the system's name.
304 *
305 * @usage name = sys:name()
306 *
307 * @luatparam System s System to get name of.
308 * @luatreturn string The name of the system.
309 * @luafunc name( s )
310 */
systemL_name(lua_State * L)311 static int systemL_name( lua_State *L )
312 {
313 StarSystem *sys;
314 sys = luaL_validsystem(L,1);
315 lua_pushstring(L, sys->name);
316 return 1;
317 }
318
319 /**
320 * @brief Gets system faction.
321 *
322 * @luatparam System s System to get the faction of.
323 * @luatreturn Faction The dominant faction in the system.
324 * @luafunc faction( s )
325 */
systemL_faction(lua_State * L)326 static int systemL_faction( lua_State *L )
327 {
328 StarSystem *s;
329
330 s = luaL_validsystem(L,1);
331
332 if (s->faction == -1)
333 return 0;
334
335 lua_pushfaction(L,s->faction);
336 return 1;
337
338 }
339
340
341 /**
342 * @brief Gets the system's nebula parameters.
343 *
344 * @usage density, volatility = sys:nebula()
345 *
346 * @luatparam System s System to get nebula parameters from.
347 * @luatreturn number The density of the system.
348 * @luatreturn number The volatility of the system.
349 * @luafunc nebula( s )
350 */
systemL_nebula(lua_State * L)351 static int systemL_nebula( lua_State *L )
352 {
353 StarSystem *s;
354
355 s = luaL_validsystem(L,1);
356
357 /* Push the density and volatility. */
358 lua_pushnumber(L, s->nebu_density);
359 lua_pushnumber(L, s->nebu_volatility);
360
361 return 2;
362 }
363
364
365 /**
366 * @brief Gets jump distance from current system, or to another.
367 *
368 * Does different things depending on the parameter type:
369 * - nil : Gets distance from current system.
370 * - string : Gets distance from system matching name.
371 * - system : Gets distance from system
372 *
373 * @usage d = sys:jumpDist() -- Distance from current system.
374 * @usage d = sys:jumpDist( "Draygar" ) -- Distance from system Draygar.
375 * @usage d = sys:jumpDist( another_sys ) -- Distance from system another_sys.
376 *
377 * @luatparam System s System to get distance from.
378 * @luatparam nil|string|System param See description.
379 * @luatparam[opt=false] boolean hidden Whether or not to consider hidden jumps.
380 * @luatparam[opt=false] boolean known Whether or not to consider only jumps known by the player.
381 * @luatreturn number Number of jumps to system.
382 * @luafunc jumpDist( s, param, hidden, known )
383 */
systemL_jumpdistance(lua_State * L)384 static int systemL_jumpdistance( lua_State *L )
385 {
386 StarSystem *sys, *sysp;
387 StarSystem **s;
388 int jumps;
389 const char *start, *goal;
390 int h, k;
391
392 sys = luaL_validsystem(L,1);
393 start = sys->name;
394 h = lua_toboolean(L,3);
395 k = !lua_toboolean(L,4);
396
397 if (lua_gettop(L) > 1) {
398 if (lua_isstring(L,2))
399 goal = lua_tostring(L,2);
400 else if (lua_issystem(L,2)) {
401 sysp = luaL_validsystem(L,2);
402 goal = sysp->name;
403 }
404 else NLUA_INVALID_PARAMETER(L);
405 }
406 else
407 goal = cur_system->name;
408
409 s = map_getJumpPath( &jumps, start, goal, k, h, NULL );
410 free(s);
411
412 lua_pushnumber(L,jumps);
413 return 1;
414 }
415
416
417 /**
418 * @brief Gets jump path from current system, or to another.
419 *
420 * Does different things depending on the parameter type:
421 * - nil : Gets path from current system.
422 * - string : Gets path from system matching name.
423 * - system : Gets path from system
424 *
425 * @usage jumps = sys:jumpPath() -- Path to current system.
426 * @usage jumps = sys:jumpPath( "Draygar" ) -- Path from current sys to Draygar.
427 * @usage jumps = system.jumpPath( "Draygar", another_sys ) -- Path from Draygar to another_sys.
428 *
429 * @luatparam System s System to get path from.
430 * @luatparam nil|string|System param See description.
431 * @luatparam[opt=false] boolean hidden Whether or not to consider hidden jumps.
432 * @luatreturn {Jump,...} Table of jumps.
433 * @luafunc jumpPath( s, param, hidden )
434 */
systemL_jumpPath(lua_State * L)435 static int systemL_jumpPath( lua_State *L )
436 {
437 LuaJump lj;
438 StarSystem *sys, *sysp;
439 StarSystem **s;
440 int i, sid, jumps, pushed, h;
441 const char *start, *goal;
442
443 h = lua_toboolean(L,3);
444
445 /* Foo to Bar */
446 if (lua_gettop(L) > 1) {
447 sys = luaL_validsystem(L,1);
448 start = sys->name;
449 sid = sys->id;
450
451 if (lua_isstring(L,2))
452 goal = lua_tostring(L,2);
453 else if (lua_issystem(L,2)) {
454 sysp = luaL_validsystem(L,2);
455 goal = sysp->name;
456 }
457 else NLUA_INVALID_PARAMETER(L);
458 }
459 /* Current to Foo */
460 else {
461 start = cur_system->name;
462 sid = cur_system->id;
463 sys = luaL_validsystem(L,1);
464 goal = sys->name;
465 }
466
467 s = map_getJumpPath( &jumps, start, goal, 1, h, NULL );
468 if (s == NULL)
469 return 0;
470
471 /* Create the jump table. */
472 lua_newtable(L);
473 pushed = 0;
474
475 /* Map path doesn't contain the start system, push it manually. */
476 lj.srcid = sid;
477 lj.destid = s[0]->id;
478
479 lua_pushnumber(L, ++pushed); /* key. */
480 lua_pushjump(L, lj); /* value. */
481 lua_rawset(L, -3);
482
483 for (i=0; i<(jumps - 1); i++) {
484 lj.srcid = s[i]->id;
485 lj.destid = s[i+1]->id;
486
487 lua_pushnumber(L, ++pushed); /* key. */
488 lua_pushjump(L, lj); /* value. */
489 lua_rawset(L, -3);
490 }
491 free(s);
492
493 return 1;
494 }
495
496
497 /**
498 * @brief Gets all the adjacent systems to a system.
499 *
500 * @usage for _,s in ipairs( sys:adjacentSystems() ) do -- Iterate over adjacent systems.
501 *
502 * @luatparam System s System to get adjacent systems of.
503 * @luatparam[opt=false] boolean hidden Whether or not to show hidden jumps also.
504 * @luatreturn {System,...} An ordered table with all the adjacent systems.
505 * @luafunc adjacentSystems( s, hidden )
506 */
systemL_adjacent(lua_State * L)507 static int systemL_adjacent( lua_State *L )
508 {
509 int i, id, h;
510 LuaSystem sysp;
511 StarSystem *s;
512
513 id = 1;
514 s = luaL_validsystem(L,1);
515 h = lua_toboolean(L,2);
516
517 /* Push all adjacent systems. */
518 lua_newtable(L);
519 for (i=0; i<s->njumps; i++) {
520 if (jp_isFlag(&s->jumps[i], JP_EXITONLY ))
521 continue;
522 if (!h && jp_isFlag(&s->jumps[i], JP_HIDDEN))
523 continue;
524 sysp = system_index( s->jumps[i].target );
525 lua_pushnumber(L, id); /* key. */
526 lua_pushsystem(L, sysp); /* value. */
527 lua_rawset(L,-3);
528
529 id++;
530 }
531
532 return 1;
533 }
534
535
536 /**
537 * @brief Gets all the jumps in a system.
538 *
539 * @usage for _,s in ipairs( sys:jumps() ) do -- Iterate over jumps.
540 *
541 * @luatparam System s System to get the jumps of.
542 * @luatparam[opt=false] boolean exitonly Whether to exclude exit-only jumps.
543 * @luatreturn {Jump,...} An ordered table with all the jumps.
544 * @luafunc jumps( s, exitonly )
545 */
systemL_jumps(lua_State * L)546 static int systemL_jumps( lua_State *L )
547 {
548 int i, exitonly, pushed;
549 LuaJump lj;
550 StarSystem *s;
551
552 s = luaL_validsystem(L,1);
553 exitonly = lua_toboolean(L,2);
554 pushed = 0;
555
556 /* Push all jumps. */
557 lua_newtable(L);
558 for (i=0; i<s->njumps; i++) {
559 /* Skip exit-only jumps if requested. */
560 if ((exitonly) && (jp_isFlag( jump_getTarget( s->jumps[i].target, s ),
561 JP_EXITONLY)))
562 continue;
563
564 lj.srcid = s->id;
565 lj.destid = s->jumps[i].targetid;
566 lua_pushnumber(L,++pushed); /* key. */
567 lua_pushjump(L,lj); /* value. */
568 lua_rawset(L,-3);
569 }
570
571 return 1;
572 }
573
574
575 /**
576 * @brief Returns the factions that have presence in a system and their respective presence values.
577 *
578 * @usage if sys:presences()["Empire"] then -- Checks to see if Empire has ships in the system
579 * @usage if sys:presences()[faction.get("Pirate")] then -- Checks to see if the Pirates have ships in the system
580 *
581 * @luatparam System s System to get the factional presences of.
582 * @luatreturn {Faction,...} A table with the factions that have presence in the system.
583 * @luafunc presences( s )
584 */
systemL_presences(lua_State * L)585 static int systemL_presences( lua_State *L )
586 {
587 StarSystem *s;
588 int i;
589
590 s = luaL_validsystem(L,1);
591
592 /* Return result in table */
593 lua_newtable(L);
594 for (i=0; i<s->npresence; i++) {
595 /* Only return positive presences. */
596 if (s->presence[i].value <= 0)
597 continue;
598
599 lua_pushstring( L, faction_name(s->presence[i].faction) ); /* t, k */
600 lua_pushnumber(L,s->presence[i].value); /* t, k, v */
601 lua_settable(L,-3); /* t */
602 /* allows syntax foo = system.presences(); if foo["bar"] then ... end */
603 }
604 return 1;
605 }
606
607
608 /**
609 * @brief Gets the planets in a system.
610 *
611 * @usage for key, planet in ipairs( sys:planets() ) do -- Iterate over planets in system
612 * @usage if #sys:planets() > 0 then -- System has planets
613 *
614 * @luatparam System s System to get planets of
615 * @luatreturn {Planet,...} A table with all the planets
616 * @luafunc planets( s )
617 */
systemL_planets(lua_State * L)618 static int systemL_planets( lua_State *L )
619 {
620 int i, key;
621 StarSystem *s;
622
623 s = luaL_validsystem(L,1);
624
625 /* Push all planets. */
626 lua_newtable(L);
627 key = 0;
628 for (i=0; i<s->nplanets; i++) {
629 if(s->planets[i]->real == ASSET_REAL) {
630 key++;
631 lua_pushnumber(L,key); /* key */
632 lua_pushplanet(L,planet_index( s->planets[i] )); /* value */
633 lua_rawset(L,-3);
634 }
635 }
636
637 return 1;
638 }
639
640
641 /**
642 * @brief Gets the presence in the system.
643 *
644 * Possible parameters are besides a faction:<br/>
645 * - "all": Gets the sum of all the presences.<br />
646 * - "friendly": Gets the sum of all the friendly presences.<br />
647 * - "hostile": Gets the sum of all the hostile presences.<br />
648 * - "neutral": Gets the sum of all the neutral presences.<br />
649 *
650 * @usage p = sys:presence( f ) -- Gets the presence of a faction f
651 * @usage p = sys:presence( "all" ) -- Gets the sum of all the presences
652 * @usage if sys:presence("friendly") > sys:presence("hostile") then -- Checks to see if the system is dominantly friendly
653 *
654 * @luatparam System s System to get presence level of.
655 * @luatreturn number The presence level in sys (absolute value).
656 * @luafunc presence( s )
657 */
systemL_presence(lua_State * L)658 static int systemL_presence( lua_State *L )
659 {
660 StarSystem *sys;
661 int *fct;
662 int nfct;
663 double presence, v;
664 int i, f, used;
665 const char *cmd;
666
667 /* Get parameters. */
668 sys = luaL_validsystem(L, 1);
669
670 /* Allow fall-through. */
671 used = 0;
672
673 /* Get the second parameter. */
674 if (lua_isstring(L, 2)) {
675 /* A string command has been given. */
676 cmd = lua_tostring(L, 2);
677 nfct = 0;
678 used = 1;
679
680 /* Check the command string and get the appropriate faction group.*/
681 if(strcmp(cmd, "all") == 0)
682 fct = faction_getGroup(&nfct, 0);
683 else if(strcmp(cmd, "friendly") == 0)
684 fct = faction_getGroup(&nfct, 1);
685 else if(strcmp(cmd, "hostile") == 0)
686 fct = faction_getGroup(&nfct, 3);
687 else if(strcmp(cmd, "neutral") == 0)
688 fct = faction_getGroup(&nfct, 2);
689 else /* Invalid command string. */
690 used = 0;
691 }
692
693 if (!used) {
694 /* A faction id was given. */
695 f = luaL_validfaction(L, 2);
696 nfct = 1;
697 fct = malloc(sizeof(int));
698 fct[0] = f;
699 }
700
701 /* Add up the presence values. */
702 presence = 0;
703 for(i=0; i<nfct; i++) {
704 /* Only count positive presences. */
705 v = system_getPresence( sys, fct[i] );
706 if (v > 0)
707 presence += v;
708 }
709
710 /* Clean up after ourselves. */
711 free(fct);
712
713 /* Push it back to Lua. */
714 lua_pushnumber(L, presence);
715 return 1;
716 }
717
718
719 /**
720 * @brief Gets the radius of the system.
721 *
722 * This is the radius of the circle which all the default jumps will be on.
723 *
724 * @usage r = s:radius()
725 *
726 * @luatparam System s System to get the radius of.
727 * @luatreturn number The radius of the system.
728 * @luafunc radius( s )
729 */
systemL_radius(lua_State * L)730 static int systemL_radius( lua_State *L )
731 {
732 StarSystem *sys;
733
734 /* Get parameters. */
735 sys = luaL_validsystem(L, 1);
736
737 lua_pushnumber( L, sys->radius );
738 return 1;
739 }
740
741
742 /**
743 * @brief Checks to see if a system is known by the player.
744 *
745 * @usage b = s:known()
746 *
747 * @luatparam System s System to check if the player knows.
748 * @luatreturn boolean true if the player knows the system.
749 * @luafunc known( s )
750 */
systemL_isknown(lua_State * L)751 static int systemL_isknown( lua_State *L )
752 {
753 StarSystem *sys = luaL_validsystem(L, 1);
754 lua_pushboolean(L, sys_isKnown(sys));
755 return 1;
756 }
757
758
759 /**
760 * @brief Sets a system's known state.
761 *
762 * @usage s:setKnown( false ) -- Makes system unknown.
763 * @luatparam System s System to set known.
764 * @luatparam[opt=false] boolean b Whether or not to set as known.
765 * @luatparam[opt=false] boolean r Whether or not to iterate over the system's assets and jump points.
766 * @luafunc setKnown( s, b, r )
767 */
systemL_setknown(lua_State * L)768 static int systemL_setknown( lua_State *L )
769 {
770 int b, r, i;
771 StarSystem *sys;
772
773 NLUA_CHECKRW(L);
774
775 r = 0;
776 sys = luaL_validsystem(L, 1);
777 b = lua_toboolean(L, 2);
778 if (lua_gettop(L) > 2)
779 r = lua_toboolean(L, 3);
780
781 if (b)
782 sys_setFlag( sys, SYSTEM_KNOWN );
783 else
784 sys_rmFlag( sys, SYSTEM_KNOWN );
785
786 if (r) {
787 if (b) {
788 for (i=0; i < sys->nplanets; i++)
789 planet_setKnown( sys->planets[i] );
790 for (i=0; i < sys->njumps; i++)
791 jp_setFlag( &sys->jumps[i], JP_KNOWN );
792 }
793 else {
794 for (i=0; i < sys->nplanets; i++)
795 planet_rmFlag( sys->planets[i], PLANET_KNOWN );
796 for (i=0; i < sys->njumps; i++)
797 jp_rmFlag( &sys->jumps[i], JP_KNOWN );
798 }
799 }
800
801 /* Update outfits image array. */
802 outfits_updateEquipmentOutfits();
803
804 return 0;
805 }
806
807
808 /**
809 * @brief Clears the system markers.
810 *
811 * This can be dangerous and clash with other missions, do not try this at home kids.
812 *
813 * @usage system.mrkClear()
814 *
815 * @luafunc mrkClear()
816 */
systemL_mrkClear(lua_State * L)817 static int systemL_mrkClear( lua_State *L )
818 {
819 (void) L;
820 NLUA_CHECKRW(L);
821 ovr_mrkClear();
822 return 0;
823 }
824
825
826 /**
827 * @brief Adds a system marker.
828 *
829 * @usage mrk_id = system.mrkAdd( "Hello", vec2.new( 50, 30 ) ) -- Creates a marker at (50,30)
830 *
831 * @luatparam string str String to display next to marker.
832 * @luatparam Vec2 v Position to display marker at.
833 * @luatreturn number The id of the marker.
834 * @luafunc mrkAdd( str, v )
835 */
systemL_mrkAdd(lua_State * L)836 static int systemL_mrkAdd( lua_State *L )
837 {
838 const char *str;
839 Vector2d *vec;
840 unsigned int id;
841
842 NLUA_CHECKRW(L);
843
844 /* Handle parameters. */
845 str = luaL_checkstring( L, 1 );
846 vec = luaL_checkvector( L, 2 );
847
848 /* Create marker. */
849 id = ovr_mrkAddPoint( str, vec->x, vec->y );
850 lua_pushnumber( L, id );
851 return 1;
852 }
853
854
855 /**
856 * @brief Removes a system marker.
857 *
858 * @usage system.mrkRm( mrk_id ) -- Removes a marker by mrk_id
859 *
860 * @luatparam number id ID of the marker to remove.
861 * @luafunc mrkRm( id )
862 */
systemL_mrkRm(lua_State * L)863 static int systemL_mrkRm( lua_State *L )
864 {
865 unsigned int id;
866 NLUA_CHECKRW(L);
867 id = luaL_checklong( L, 1 );
868 ovr_mrkRm( id );
869 return 0;
870 }
871
872