naev 0.10.4
nlua_ship.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <lauxlib.h>
11
12#include "naev.h"
15#include "nlua_ship.h"
16
17#include "array.h"
18#include "log.h"
19#include "nlua_outfit.h"
20#include "nlua_tex.h"
21#include "nluadef.h"
22#include "rng.h"
23#include "slots.h"
24
25/*
26 * Prototypes.
27 */
28static const ShipOutfitSlot* ship_outfitSlotFromID( const Ship *s, int id );
29
30/* Ship metatable methods. */
31static int shipL_eq( lua_State *L );
32static int shipL_get( lua_State *L );
33static int shipL_getAll( lua_State *L );
34static int shipL_name( lua_State *L );
35static int shipL_nameRaw( lua_State *L );
36static int shipL_baseType( lua_State *L );
37static int shipL_class( lua_State *L );
38static int shipL_classDisplay( lua_State *L );
39static int shipL_getPoints( lua_State *L );
40static int shipL_slots( lua_State *L );
41static int shipL_getSlots( lua_State *L );
42static int shipL_fitsSlot( lua_State *L );
43static int shipL_CPU( lua_State *L );
44static int shipL_gfxComm( lua_State *L );
45static int shipL_gfxTarget( lua_State *L );
46static int shipL_gfx( lua_State *L );
47static int shipL_price( lua_State *L );
48static int shipL_time_mod( lua_State *L );
49static int shipL_getSize( lua_State *L );
50static int shipL_description( lua_State *L );
51static int shipL_getShipStat( lua_State *L );
52static int shipL_getShipStatDesc( lua_State *L );
53static int shipL_tags( lua_State *L );
54static const luaL_Reg shipL_methods[] = {
55 { "__tostring", shipL_name },
56 { "__eq", shipL_eq },
57 { "get", shipL_get },
58 { "getAll", shipL_getAll },
59 { "name", shipL_name },
60 { "nameRaw", shipL_nameRaw },
61 { "baseType", shipL_baseType },
62 { "class", shipL_class },
63 { "classDisplay", shipL_classDisplay },
64 { "points", shipL_getPoints },
65 { "slots", shipL_slots },
66 { "getSlots", shipL_getSlots },
67 { "fitsSlot", shipL_fitsSlot },
68 { "cpu", shipL_CPU },
69 { "price", shipL_price },
70 { "time_mod", shipL_time_mod },
71 { "size", shipL_getSize },
72 { "gfxComm", shipL_gfxComm },
73 { "gfxTarget", shipL_gfxTarget },
74 { "gfx", shipL_gfx },
75 { "description", shipL_description },
76 { "shipstat", shipL_getShipStat },
77 { "shipstatDesc", shipL_getShipStatDesc },
78 { "tags", shipL_tags },
79 {0,0}
80};
88int nlua_loadShip( nlua_env env )
89{
90 nlua_register(env, SHIP_METATABLE, shipL_methods, 1);
91 return 0;
92}
93
114const Ship* lua_toship( lua_State *L, int ind )
115{
116 return *((Ship**) lua_touserdata(L,ind));
117}
125const Ship* luaL_checkship( lua_State *L, int ind )
126{
127 if (lua_isship(L,ind))
128 return lua_toship(L,ind);
129 luaL_typerror(L, ind, SHIP_METATABLE);
130 return NULL;
131}
139const Ship* luaL_validship( lua_State *L, int ind )
140{
141 const Ship *s;
142
143 if (lua_isship(L, ind))
144 s = luaL_checkship(L,ind);
145 else if (lua_isstring(L, ind))
146 s = ship_get( lua_tostring(L, ind) );
147 else {
148 luaL_typerror(L, ind, SHIP_METATABLE);
149 return NULL;
150 }
151
152 if (s == NULL)
153 NLUA_ERROR(L, _("Ship is invalid."));
154
155 return s;
156}
164const Ship** lua_pushship( lua_State *L, const Ship *ship )
165{
166 const Ship **p;
167 p = (const Ship**) lua_newuserdata(L, sizeof(Ship*));
168 *p = ship;
169 luaL_getmetatable(L, SHIP_METATABLE);
170 lua_setmetatable(L, -2);
171 return p;
172}
180int lua_isship( lua_State *L, int ind )
181{
182 int ret;
183
184 if (lua_getmetatable(L,ind)==0)
185 return 0;
186 lua_getfield(L, LUA_REGISTRYINDEX, SHIP_METATABLE);
187
188 ret = 0;
189 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
190 ret = 1;
191
192 lua_pop(L, 2); /* remove both metatables */
193 return ret;
194}
195
206static int shipL_eq( lua_State *L )
207{
208 const Ship *a, *b;
209 a = luaL_checkship(L,1);
210 b = luaL_checkship(L,2);
211 if (a == b)
212 lua_pushboolean(L,1);
213 else
214 lua_pushboolean(L,0);
215 return 1;
216}
217
227static int shipL_get( lua_State *L )
228{
229 const Ship *ship = luaL_validship(L,1);
230 lua_pushship(L, ship);
231 return 1;
232}
233
240static int shipL_getAll( lua_State *L )
241{
242 const Ship *ships = ship_getAll();
243 lua_newtable(L); /* t */
244 for (int i=0; i<array_size(ships); i++) {
245 lua_pushship( L, &ships[i] );
246 lua_rawseti( L, -2, i+1 );
247 }
248 return 1;
249}
250
264static int shipL_name( lua_State *L )
265{
266 const Ship *s = luaL_validship(L,1);
267 lua_pushstring(L, _(s->name));
268 return 1;
269}
270
284static int shipL_nameRaw( lua_State *L )
285{
286 const Ship *s = luaL_validship(L,1);
287 lua_pushstring(L, s->name);
288 return 1;
289}
290
302static int shipL_baseType( lua_State *L )
303{
304 const Ship *s = luaL_validship(L,1);
305 lua_pushstring(L, s->base_type);
306 return 1;
307}
308
318static int shipL_class( lua_State *L )
319{
320 const Ship *s = luaL_validship(L,1);
321 lua_pushstring(L, ship_class(s));
322 return 1;
323}
324
334static int shipL_classDisplay( lua_State *L )
335{
336 const Ship *s = luaL_validship(L,1);
337 lua_pushstring(L, ship_classDisplay(s));
338 return 1;
339}
340
350static int shipL_getPoints( lua_State *L )
351{
352 const Ship *s = luaL_validship(L,1);
353 lua_pushinteger(L, s->points);
354 return 1;
355}
356
368static int shipL_slots( lua_State *L )
369{
370 const Ship *s = luaL_validship(L,1);
371 /* Push slot numbers. */
372 lua_pushnumber(L, array_size(s->outfit_weapon));
373 lua_pushnumber(L, array_size(s->outfit_utility));
374 lua_pushnumber(L, array_size(s->outfit_structure));
375 return 3;
376}
377
389static int shipL_getSlots( lua_State *L )
390{
391 int k;
392 const Ship *s = luaL_validship(L,1);
393 int ignore_locked = lua_toboolean(L,2);
394 char *outfit_types[] = {"Structure", "Utility", "Weapon"};
395 const ShipOutfitSlot *outfit_arrays[] = {
398 s->outfit_weapon };
399
400 lua_newtable(L);
401 k=1;
402 for (int i=0; i<3 ; i++) {
403 for (int j=0; j<array_size(outfit_arrays[i]) ; j++) {
404 const OutfitSlot *slot = &outfit_arrays[i][j].slot;
405 const ShipOutfitSlot *sslot = &outfit_arrays[i][j];
406
407 /* Skip locked if necessary. */
408 if (ignore_locked && sslot->locked)
409 continue;
410
411 /* make the slot table and put it in */
412 lua_newtable(L);
413
414 lua_pushstring(L, "type"); /* key */
415 lua_pushstring(L, outfit_types[i]); /* value */
416 lua_rawset(L, -3); /* table[key = value ]*/
417
418 lua_pushstring(L, "size"); /* key */
419 lua_pushstring(L, slotSize(slot->size) );
420 lua_rawset(L, -3); /* table[key] = value */
421
422 lua_pushstring(L, "property"); /* key */
423 lua_pushstring( L, sp_display(slot->spid)); /* value */
424 lua_rawset(L, -3); /* table[key] = value */
425
426 lua_pushstring(L, "required"); /* key */
427 lua_pushboolean( L, sslot->required); /* value */
428 lua_rawset(L, -3); /* table[key] = value */
429
430 lua_pushstring(L, "exclusive"); /* key */
431 lua_pushboolean( L, sslot->exclusive); /* value */
432 lua_rawset(L, -3); /* table[key] = value */
433
434 lua_pushstring(L, "locked"); /* key */
435 lua_pushboolean( L, sslot->locked); /* value */
436 lua_rawset(L, -3); /* table[key] = value */
437
438 if (sslot->data != NULL) {
439 lua_pushstring(L, "outfit"); /* key */
440 lua_pushoutfit(L, sslot->data); /* value*/
441 lua_rawset(L, -3); /* table[key] = value */
442 }
443
444 lua_rawseti(L, -2, k++); /* put the slot table in */
445 }
446 }
447
448 return 1;
449}
450
457static const ShipOutfitSlot* ship_outfitSlotFromID( const Ship *s, int id )
458{
459 const ShipOutfitSlot *outfit_arrays[] = {
462 s->outfit_weapon };
463
464 for (int i=0; i<3 ; i++) {
465 int n = array_size(outfit_arrays[i]);
466 if (id <= n)
467 return &outfit_arrays[i][ id-1 ];
468 id -= n;
469 }
470 return NULL;
471}
472
482static int shipL_fitsSlot( lua_State *L )
483{
484 const Ship *s = luaL_validship(L,1);
485 int id = luaL_checkinteger(L,2);
486 const Outfit *o= luaL_validoutfit(L,3);
487 const ShipOutfitSlot *ss = ship_outfitSlotFromID( s, id );
488 if (ss->locked) {
489 lua_pushboolean(L,0);
490 return 1;
491 }
492 lua_pushboolean( L, outfit_fitsSlot( o, &ss->slot ) );
493 return 1;
494}
495
505static int shipL_CPU( lua_State *L )
506{
507 const Ship *s = luaL_validship(L,1);
508 lua_pushnumber(L, s->cpu);
509 return 1;
510}
511
522static int shipL_price( lua_State *L )
523{
524 const Ship *s = luaL_validship(L,1);
525 lua_pushnumber(L, ship_buyPrice(s));
526 lua_pushnumber(L, ship_basePrice(s));
527 return 2;
528}
529
537static int shipL_time_mod( lua_State *L )
538{
539 const Ship *s = luaL_validship(L,1);
540 lua_pushnumber(L, s->dt_default );
541 return 1;
542}
543
551static int shipL_getSize( lua_State *L )
552{
553 const Ship *s = luaL_validship(L,1);
554 lua_pushinteger(L, ship_size(s) );
555 return 1;
556}
557
569static int shipL_gfxComm( lua_State *L )
570{
571 const Ship *s = luaL_validship(L,1);
572 glTexture *tex = ship_loadCommGFX( s );
573 if (tex == NULL) {
574 WARN(_("Unable to get ship comm graphic for '%s'."), s->name);
575 return 0;
576 }
577 lua_pushtex( L, tex );
578 return 1;
579}
580
592static int shipL_gfxTarget( lua_State *L )
593{
594 const Ship *s = luaL_validship(L,1);
596 if (tex == NULL) {
597 WARN(_("Unable to get ship target graphic for '%s'."), s->name);
598 return 0;
599 }
600 lua_pushtex( L, tex );
601 return 1;
602}
603
615static int shipL_gfx( lua_State *L )
616{
617 const Ship *s = luaL_validship(L,1);
618 glTexture *tex = gl_dupTexture( s->gfx_space );
619 if (tex == NULL) {
620 WARN(_("Unable to get ship graphic for '%s'."), s->name);
621 return 0;
622 }
623 lua_pushtex( L, tex );
624 return 1;
625}
626
636static int shipL_description( lua_State *L )
637{
638 const Ship *s = luaL_validship(L,1);
639 lua_pushstring(L, _(s->description));
640 return 1;
641}
642
652static int shipL_getShipStat( lua_State *L )
653{
654 const Ship *s = luaL_validship(L,1);
655 const char *str = luaL_optstring(L,2,NULL);
656 int internal = lua_toboolean(L,3);
657 ss_statsGetLua( L, &s->stats_array, str, internal );
658 return 1;
659}
660
668static int shipL_getShipStatDesc( lua_State *L )
669{
670 char buf[STRMAX];
671 const Ship *s = luaL_validship(L,1);
672 ss_statsDesc( &s->stats_array, buf, sizeof(buf), 0 );
673 lua_pushstring(L,buf);
674 return 1;
675}
676
686static int shipL_tags( lua_State *L )
687{
688 const Ship *s = luaL_validship(L,1);
689 lua_newtable(L);
690 for (int i=0; i<array_size(s->tags); i++) {
691 lua_pushstring(L,s->tags[i]);
692 lua_pushboolean(L,1);
693 lua_rawset(L,-3);
694 }
695 return 1;
696}
Provides macros to work with dynamic arrays.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition: array.h:168
Header file with generic functions and naev-specifics.
const Outfit * luaL_validoutfit(lua_State *L, int ind)
Makes sure the outfit is valid or raises a Lua error.
Definition: nlua_outfit.c:135
const Outfit ** lua_pushoutfit(lua_State *L, const Outfit *outfit)
Pushes a outfit on the stack.
Definition: nlua_outfit.c:160
static int shipL_tags(lua_State *L)
Gets the ship tags.
Definition: nlua_ship.c:686
static int shipL_getSize(lua_State *L)
Gets the ship's size. Ultra-light is 1, light is 2, medium is 3, heavy-medium is 4,...
Definition: nlua_ship.c:551
static int shipL_getShipStatDesc(lua_State *L)
Gets the ship stats description for a ship.
Definition: nlua_ship.c:668
static int shipL_getShipStat(lua_State *L)
Gets a shipstat from an Ship by name, or a table containing all the ship stats if not specified.
Definition: nlua_ship.c:652
static int shipL_price(lua_State *L)
Gets the ship's price, with and without default outfits.
Definition: nlua_ship.c:522
const Ship ** lua_pushship(lua_State *L, const Ship *ship)
Pushes a ship on the stack.
Definition: nlua_ship.c:164
int nlua_loadShip(nlua_env env)
Loads the ship library.
Definition: nlua_ship.c:88
static int shipL_class(lua_State *L)
Gets the raw (untranslated) name of the ship's class.
Definition: nlua_ship.c:318
int lua_isship(lua_State *L, int ind)
Checks to see if ind is a ship.
Definition: nlua_ship.c:180
static int shipL_time_mod(lua_State *L)
Gets the ship's time_mod.
Definition: nlua_ship.c:537
const Ship * luaL_validship(lua_State *L, int ind)
Makes sure the ship is valid or raises a Lua error.
Definition: nlua_ship.c:139
const Ship * luaL_checkship(lua_State *L, int ind)
Gets ship at index or raises error if there is no ship at index.
Definition: nlua_ship.c:125
static int shipL_eq(lua_State *L)
Checks to see if two ships are the same.
Definition: nlua_ship.c:206
static int shipL_getSlots(lua_State *L)
Get a table of slots of a ship, where a slot is a table with a string size, type, and property.
Definition: nlua_ship.c:389
static int shipL_CPU(lua_State *L)
Gets the ship available CPU.
Definition: nlua_ship.c:505
static int shipL_gfxComm(lua_State *L)
Gets the ship's comm graphics.
Definition: nlua_ship.c:569
const Ship * lua_toship(lua_State *L, int ind)
Lua bindings to interact with ships.
Definition: nlua_ship.c:114
static int shipL_baseType(lua_State *L)
Gets the raw (untranslated) name of the ship's base type.
Definition: nlua_ship.c:302
static int shipL_nameRaw(lua_State *L)
Gets the raw (untranslated) name of the ship.
Definition: nlua_ship.c:284
static const luaL_Reg shipL_methods[]
Definition: nlua_ship.c:54
static int shipL_slots(lua_State *L)
Gets the amount of the ship's slots.
Definition: nlua_ship.c:368
static int shipL_description(lua_State *L)
Gets the description of the ship (translated).
Definition: nlua_ship.c:636
static int shipL_classDisplay(lua_State *L)
Gets the raw (untranslated) display name of the ship's class (not ship's base class).
Definition: nlua_ship.c:334
static const ShipOutfitSlot * ship_outfitSlotFromID(const Ship *s, int id)
Gets an outfit slot from ID.
Definition: nlua_ship.c:457
static int shipL_getAll(lua_State *L)
Gets a table containing all the ships.
Definition: nlua_ship.c:240
static int shipL_name(lua_State *L)
Gets the translated name of the ship.
Definition: nlua_ship.c:264
static int shipL_get(lua_State *L)
Gets a ship.
Definition: nlua_ship.c:227
static int shipL_gfx(lua_State *L)
Gets the ship's graphics.
Definition: nlua_ship.c:615
static int shipL_getPoints(lua_State *L)
Gets the point value of a ship. Used for comparing relative ship strengths (minus outfits).
Definition: nlua_ship.c:350
static int shipL_fitsSlot(lua_State *L)
Checks to see if an outfit fits a ship slot.
Definition: nlua_ship.c:482
static int shipL_gfxTarget(lua_State *L)
Gets the ship's target graphics.
Definition: nlua_ship.c:592
glTexture ** lua_pushtex(lua_State *L, glTexture *texture)
Pushes a texture on the stack.
Definition: nlua_tex.c:130
glTexture * gl_dupTexture(const glTexture *texture)
Duplicates a texture.
Definition: opengl_tex.c:809
int outfit_fitsSlot(const Outfit *o, const OutfitSlot *s)
Checks to see if an outfit fits a slot.
Definition: outfit.c:981
const char * slotSize(const OutfitSlotSize o)
Gets the slot size as a string.
Definition: outfit.c:308
static const double a[]
Definition: rng.c:247
static const double b[]
Definition: rng.c:256
int ship_size(const Ship *s)
Gets the size of the ship.
Definition: ship.c:302
const char * ship_class(const Ship *s)
Gets the ship's class name in human readable form.
Definition: ship.c:151
const char * ship_classDisplay(const Ship *s)
Gets the ship's display class in human readable form.
Definition: ship.c:162
credits_t ship_basePrice(const Ship *s)
Gets the ship's base price (no outfits).
Definition: ship.c:252
credits_t ship_buyPrice(const Ship *s)
The ship buy price, includes default outfits.
Definition: ship.c:260
const Ship * ship_getAll(void)
Gets the array (array.h) of all ships.
Definition: ship.c:107
glTexture * ship_loadCommGFX(const Ship *s)
Loads the ship's comm graphic.
Definition: ship.c:289
const Ship * ship_get(const char *name)
Gets a ship based on its name.
Definition: ship.c:73
int ss_statsGetLua(lua_State *L, const ShipStats *s, const char *name, int internal)
Gets a ship stat value by name and pushes it to Lua.
Definition: shipstats.c:967
int ss_statsDesc(const ShipStats *s, char *buf, int len, int newline)
Writes the ship statistics description.
Definition: shipstats.c:744
const char * sp_display(unsigned int spid)
Gets the display name of a slot property (in English).
Definition: slots.c:150
Pilot slot that can contain outfits.
Definition: outfit.h:101
OutfitSlotSize size
Definition: outfit.h:105
unsigned int spid
Definition: outfit.h:102
A ship outfit, depends radically on the type.
Definition: outfit.h:304
Ship outfit slot.
Definition: ship.h:70
int exclusive
Definition: ship.h:73
int required
Definition: ship.h:74
const Outfit * data
Definition: ship.h:76
OutfitSlot slot
Definition: ship.h:71
int locked
Definition: ship.h:75
Represents a space ship.
Definition: ship.h:94
double dt_default
Definition: ship.h:123
glTexture * gfx_space
Definition: ship.h:137
ShipStats stats_array
Definition: ship.h:166
char * name
Definition: ship.h:95
char * description
Definition: ship.h:109
char * base_type
Definition: ship.h:96
glTexture * gfx_target
Definition: ship.h:139
int points
Definition: ship.h:99
double cpu
Definition: ship.h:119
ShipOutfitSlot * outfit_utility
Definition: ship.h:157
char ** tags
Definition: ship.h:169
ShipOutfitSlot * outfit_weapon
Definition: ship.h:158
ShipOutfitSlot * outfit_structure
Definition: ship.h:156
Abstraction for rendering sprite sheets.
Definition: opengl_tex.h:34