naev 0.11.5
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_dims( lua_State *L );
48static int shipL_price( lua_State *L );
49static int shipL_time_mod( lua_State *L );
50static int shipL_getSize( lua_State *L );
51static int shipL_description( lua_State *L );
52static int shipL_getShipStat( lua_State *L );
53static int shipL_getShipStatDesc( lua_State *L );
54static int shipL_tags( lua_State *L );
55static const luaL_Reg shipL_methods[] = {
56 { "__tostring", shipL_name },
57 { "__eq", shipL_eq },
58 { "get", shipL_get },
59 { "getAll", shipL_getAll },
60 { "name", shipL_name },
61 { "nameRaw", shipL_nameRaw },
62 { "baseType", shipL_baseType },
63 { "class", shipL_class },
64 { "classDisplay", shipL_classDisplay },
65 { "points", shipL_getPoints },
66 { "slots", shipL_slots },
67 { "getSlots", shipL_getSlots },
68 { "fitsSlot", shipL_fitsSlot },
69 { "cpu", shipL_CPU },
70 { "price", shipL_price },
71 { "time_mod", shipL_time_mod },
72 { "size", shipL_getSize },
73 { "gfxComm", shipL_gfxComm },
74 { "gfxTarget", shipL_gfxTarget },
75 { "gfx", shipL_gfx },
76 { "dims", shipL_dims },
77 { "description", shipL_description },
78 { "shipstat", shipL_getShipStat },
79 { "shipstatDesc", shipL_getShipStatDesc },
80 { "tags", shipL_tags },
81 {0,0}
82};
90int nlua_loadShip( nlua_env env )
91{
92 nlua_register(env, SHIP_METATABLE, shipL_methods, 1);
93 return 0;
94}
95
116const Ship* lua_toship( lua_State *L, int ind )
117{
118 return *((Ship**) lua_touserdata(L,ind));
119}
127const Ship* luaL_checkship( lua_State *L, int ind )
128{
129 if (lua_isship(L,ind))
130 return lua_toship(L,ind);
131 luaL_typerror(L, ind, SHIP_METATABLE);
132 return NULL;
133}
141const Ship* luaL_validship( lua_State *L, int ind )
142{
143 const Ship *s;
144
145 if (lua_isship(L, ind))
146 s = luaL_checkship(L,ind);
147 else if (lua_isstring(L, ind))
148 s = ship_get( lua_tostring(L, ind) );
149 else {
150 luaL_typerror(L, ind, SHIP_METATABLE);
151 return NULL;
152 }
153
154 if (s == NULL)
155 NLUA_ERROR(L, _("Ship is invalid."));
156
157 return s;
158}
166const Ship** lua_pushship( lua_State *L, const Ship *ship )
167{
168 const Ship **p;
169 p = (const Ship**) lua_newuserdata(L, sizeof(Ship*));
170 *p = ship;
171 luaL_getmetatable(L, SHIP_METATABLE);
172 lua_setmetatable(L, -2);
173 return p;
174}
182int lua_isship( lua_State *L, int ind )
183{
184 int ret;
185
186 if (lua_getmetatable(L,ind)==0)
187 return 0;
188 lua_getfield(L, LUA_REGISTRYINDEX, SHIP_METATABLE);
189
190 ret = 0;
191 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
192 ret = 1;
193
194 lua_pop(L, 2); /* remove both metatables */
195 return ret;
196}
197
208static int shipL_eq( lua_State *L )
209{
210 const Ship *a, *b;
211 a = luaL_checkship(L,1);
212 b = luaL_checkship(L,2);
213 if (a == b)
214 lua_pushboolean(L,1);
215 else
216 lua_pushboolean(L,0);
217 return 1;
218}
219
229static int shipL_get( lua_State *L )
230{
231 const Ship *ship = luaL_validship(L,1);
232 lua_pushship(L, ship);
233 return 1;
234}
235
242static int shipL_getAll( lua_State *L )
243{
244 const Ship *ships = ship_getAll();
245 lua_newtable(L); /* t */
246 for (int i=0; i<array_size(ships); i++) {
247 lua_pushship( L, &ships[i] );
248 lua_rawseti( L, -2, i+1 );
249 }
250 return 1;
251}
252
266static int shipL_name( lua_State *L )
267{
268 const Ship *s = luaL_validship(L,1);
269 lua_pushstring(L, _(s->name));
270 return 1;
271}
272
286static int shipL_nameRaw( lua_State *L )
287{
288 const Ship *s = luaL_validship(L,1);
289 lua_pushstring(L, s->name);
290 return 1;
291}
292
304static int shipL_baseType( lua_State *L )
305{
306 const Ship *s = luaL_validship(L,1);
307 lua_pushstring(L, s->base_type);
308 return 1;
309}
310
320static int shipL_class( lua_State *L )
321{
322 const Ship *s = luaL_validship(L,1);
323 lua_pushstring(L, ship_class(s));
324 return 1;
325}
326
336static int shipL_classDisplay( lua_State *L )
337{
338 const Ship *s = luaL_validship(L,1);
339 lua_pushstring(L, ship_classDisplay(s));
340 return 1;
341}
342
352static int shipL_getPoints( lua_State *L )
353{
354 const Ship *s = luaL_validship(L,1);
355 lua_pushinteger(L, s->points);
356 return 1;
357}
358
370static int shipL_slots( lua_State *L )
371{
372 const Ship *s = luaL_validship(L,1);
373 /* Push slot numbers. */
374 lua_pushnumber(L, array_size(s->outfit_weapon));
375 lua_pushnumber(L, array_size(s->outfit_utility));
376 lua_pushnumber(L, array_size(s->outfit_structure));
377 return 3;
378}
379
391static int shipL_getSlots( lua_State *L )
392{
393 int k;
394 const Ship *s = luaL_validship(L,1);
395 int ignore_locked = lua_toboolean(L,2);
396 const ShipOutfitSlot *outfit_arrays[] = {
399 s->outfit_weapon };
400
401 lua_newtable(L);
402 k=1;
403 for (int i=0; i<3 ; i++) {
404 for (int j=0; j<array_size(outfit_arrays[i]) ; j++) {
405 const OutfitSlot *slot = &outfit_arrays[i][j].slot;
406 const ShipOutfitSlot *sslot = &outfit_arrays[i][j];
407
408 /* Skip locked if necessary. */
409 if (ignore_locked && sslot->locked)
410 continue;
411
412 /* make the slot table and put it in */
413 lua_newtable(L);
414
415 /* Index can be used as an ID (at least for now...) */
416#if 0
417 lua_pushstring(L, "id" ); /* key */
418 lua_pushinteger(L, k); /* value */
419 lua_rawset(L, -3); /* table[key] = value*/
420#endif
421
422 lua_pushstring(L, "type"); /* key */
423 lua_pushstring(L, slotName(slot->type)); /* value */
424 lua_rawset(L, -3); /* table[key] = value*/
425
426 lua_pushstring(L, "size"); /* key */
427 lua_pushstring(L, slotSize(slot->size) );
428 lua_rawset(L, -3); /* table[key] = value */
429
430 lua_pushstring(L, "property"); /* key */
431 lua_pushstring( L, sp_display(slot->spid)); /* value */
432 lua_rawset(L, -3); /* table[key] = value */
433
434 lua_pushstring(L, "required"); /* key */
435 lua_pushboolean( L, sslot->required); /* value */
436 lua_rawset(L, -3); /* table[key] = value */
437
438 lua_pushstring(L, "exclusive"); /* key */
439 lua_pushboolean( L, sslot->exclusive); /* value */
440 lua_rawset(L, -3); /* table[key] = value */
441
442 lua_pushstring(L, "locked"); /* key */
443 lua_pushboolean( L, sslot->locked); /* value */
444 lua_rawset(L, -3); /* table[key] = value */
445
446 if (sslot->data != NULL) {
447 lua_pushstring(L, "outfit"); /* key */
448 lua_pushoutfit(L, sslot->data); /* value*/
449 lua_rawset(L, -3); /* table[key] = value */
450 }
451
452 lua_rawseti(L, -2, k++); /* put the slot table in */
453 }
454 }
455
456 return 1;
457}
458
465static const ShipOutfitSlot* ship_outfitSlotFromID( const Ship *s, int id )
466{
467 const ShipOutfitSlot *outfit_arrays[] = {
470 s->outfit_weapon };
471
472 for (int i=0; i<3 ; i++) {
473 int n = array_size(outfit_arrays[i]);
474 if (id <= n)
475 return &outfit_arrays[i][ id-1 ];
476 id -= n;
477 }
478 return NULL;
479}
480
490static int shipL_fitsSlot( lua_State *L )
491{
492 const Ship *s = luaL_validship(L,1);
493 int id = luaL_checkinteger(L,2);
494 const Outfit *o= luaL_validoutfit(L,3);
495 const ShipOutfitSlot *ss = ship_outfitSlotFromID( s, id );
496 if (ss->locked) {
497 lua_pushboolean(L,0);
498 return 1;
499 }
500 lua_pushboolean( L, outfit_fitsSlot( o, &ss->slot ) );
501 return 1;
502}
503
513static int shipL_CPU( lua_State *L )
514{
515 const Ship *s = luaL_validship(L,1);
516 lua_pushnumber(L, s->cpu);
517 return 1;
518}
519
530static int shipL_price( lua_State *L )
531{
532 const Ship *s = luaL_validship(L,1);
533 lua_pushnumber(L, ship_buyPrice(s));
534 lua_pushnumber(L, ship_basePrice(s));
535 return 2;
536}
537
545static int shipL_time_mod( lua_State *L )
546{
547 const Ship *s = luaL_validship(L,1);
548 lua_pushnumber(L, s->dt_default );
549 return 1;
550}
551
559static int shipL_getSize( lua_State *L )
560{
561 const Ship *s = luaL_validship(L,1);
562 lua_pushinteger(L, ship_size(s) );
563 return 1;
564}
565
577static int shipL_gfxComm( lua_State *L )
578{
579 const Ship *s = luaL_validship(L,1);
580 glTexture *tex = ship_loadCommGFX( s );
581 if (tex == NULL) {
582 WARN(_("Unable to get ship comm graphic for '%s'."), s->name);
583 return 0;
584 }
585 lua_pushtex( L, tex );
586 return 1;
587}
588
600static int shipL_gfxTarget( lua_State *L )
601{
602 const Ship *s = luaL_validship(L,1);
604 if (tex == NULL) {
605 WARN(_("Unable to get ship target graphic for '%s'."), s->name);
606 return 0;
607 }
608 lua_pushtex( L, tex );
609 return 1;
610}
611
623static int shipL_gfx( lua_State *L )
624{
625 const Ship *s = luaL_validship(L,1);
626 glTexture *tex = gl_dupTexture( s->gfx_space );
627 if (tex == NULL) {
628 WARN(_("Unable to get ship graphic for '%s'."), s->name);
629 return 0;
630 }
631 lua_pushtex( L, tex );
632 return 1;
633}
634
642static int shipL_dims( lua_State *L )
643{
644 const Ship *s = luaL_validship(L,1);
645 lua_pushnumber( L, s->gfx_space->sw );
646 lua_pushnumber( L, s->gfx_space->sh );
647 return 2;
648}
649
659static int shipL_description( lua_State *L )
660{
661 const Ship *s = luaL_validship(L,1);
662 lua_pushstring(L, _(s->description));
663 return 1;
664}
665
675static int shipL_getShipStat( lua_State *L )
676{
677 const Ship *s = luaL_validship(L,1);
678 const char *str = luaL_optstring(L,2,NULL);
679 int internal = lua_toboolean(L,3);
680 ss_statsGetLua( L, &s->stats_array, str, internal );
681 return 1;
682}
683
691static int shipL_getShipStatDesc( lua_State *L )
692{
693 char buf[STRMAX];
694 const Ship *s = luaL_validship(L,1);
695 ss_statsDesc( &s->stats_array, buf, sizeof(buf), 0 );
696 lua_pushstring(L,buf);
697 return 1;
698}
699
709static int shipL_tags( lua_State *L )
710{
711 const Ship *s = luaL_validship(L,1);
712 lua_newtable(L);
713 for (int i=0; i<array_size(s->tags); i++) {
714 lua_pushstring(L,s->tags[i]);
715 lua_pushboolean(L,1);
716 lua_rawset(L,-3);
717 }
718 return 1;
719}
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.
const Outfit ** lua_pushoutfit(lua_State *L, const Outfit *outfit)
Pushes a outfit on the stack.
static int shipL_tags(lua_State *L)
Gets the ship tags.
Definition nlua_ship.c:709
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:559
static int shipL_getShipStatDesc(lua_State *L)
Gets the ship stats description for a ship.
Definition nlua_ship.c:691
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:675
static int shipL_price(lua_State *L)
Gets the ship's price, with and without default outfits.
Definition nlua_ship.c:530
const Ship ** lua_pushship(lua_State *L, const Ship *ship)
Pushes a ship on the stack.
Definition nlua_ship.c:166
int nlua_loadShip(nlua_env env)
Loads the ship library.
Definition nlua_ship.c:90
static int shipL_class(lua_State *L)
Gets the raw (untranslated) name of the ship's class.
Definition nlua_ship.c:320
int lua_isship(lua_State *L, int ind)
Checks to see if ind is a ship.
Definition nlua_ship.c:182
static int shipL_dims(lua_State *L)
Gets the onscreen dimensions of the ship.
Definition nlua_ship.c:642
static int shipL_time_mod(lua_State *L)
Gets the ship's time_mod.
Definition nlua_ship.c:545
const Ship * luaL_validship(lua_State *L, int ind)
Makes sure the ship is valid or raises a Lua error.
Definition nlua_ship.c:141
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:127
static int shipL_eq(lua_State *L)
Checks to see if two ships are the same.
Definition nlua_ship.c:208
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:391
static int shipL_CPU(lua_State *L)
Gets the ship available CPU.
Definition nlua_ship.c:513
static int shipL_gfxComm(lua_State *L)
Gets the ship's comm graphics.
Definition nlua_ship.c:577
const Ship * lua_toship(lua_State *L, int ind)
Lua bindings to interact with ships.
Definition nlua_ship.c:116
static int shipL_baseType(lua_State *L)
Gets the raw (untranslated) name of the ship's base type.
Definition nlua_ship.c:304
static int shipL_nameRaw(lua_State *L)
Gets the raw (untranslated) name of the ship.
Definition nlua_ship.c:286
static const luaL_Reg shipL_methods[]
Definition nlua_ship.c:55
static int shipL_slots(lua_State *L)
Gets the amount of the ship's slots.
Definition nlua_ship.c:370
static int shipL_description(lua_State *L)
Gets the description of the ship (translated).
Definition nlua_ship.c:659
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:336
static const ShipOutfitSlot * ship_outfitSlotFromID(const Ship *s, int id)
Gets an outfit slot from ID.
Definition nlua_ship.c:465
static int shipL_getAll(lua_State *L)
Gets a table containing all the ships.
Definition nlua_ship.c:242
static int shipL_name(lua_State *L)
Gets the translated name of the ship.
Definition nlua_ship.c:266
static int shipL_get(lua_State *L)
Gets a ship.
Definition nlua_ship.c:229
static int shipL_gfx(lua_State *L)
Gets the ship's graphics.
Definition nlua_ship.c:623
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:352
static int shipL_fitsSlot(lua_State *L)
Checks to see if an outfit fits a ship slot.
Definition nlua_ship.c:490
static int shipL_gfxTarget(lua_State *L)
Gets the ship's target graphics.
Definition nlua_ship.c:600
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:917
int outfit_fitsSlot(const Outfit *o, const OutfitSlot *s)
Checks to see if an outfit fits a slot.
Definition outfit.c:1047
const char * slotName(const OutfitSlotType type)
Definition outfit.c:335
const char * slotSize(const OutfitSlotSize o)
Gets the slot size as a string.
Definition outfit.c:358
int ship_size(const Ship *s)
Gets the size of the ship.
Definition ship.c:316
const char * ship_class(const Ship *s)
Gets the ship's class name in human readable form.
Definition ship.c:165
const char * ship_classDisplay(const Ship *s)
Gets the ship's display class in human readable form.
Definition ship.c:176
credits_t ship_basePrice(const Ship *s)
Gets the ship's base price (no outfits).
Definition ship.c:266
credits_t ship_buyPrice(const Ship *s)
The ship buy price, includes default outfits.
Definition ship.c:274
const Ship * ship_getAll(void)
Gets the array (array.h) of all ships.
Definition ship.c:121
glTexture * ship_loadCommGFX(const Ship *s)
Loads the ship's comm graphic.
Definition ship.c:303
const Ship * ship_get(const char *name)
Gets a ship based on its name.
Definition ship.c:87
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:1054
int ss_statsDesc(const ShipStats *s, char *buf, int len, int newline)
Writes the ship statistics description.
Definition shipstats.c:824
const char * sp_display(unsigned int spid)
Gets the display name of a slot property (in English).
Definition slots.c:159
Pilot slot that can contain outfits.
Definition outfit.h:109
OutfitSlotSize size
Definition outfit.h:113
unsigned int spid
Definition outfit.h:110
OutfitSlotType type
Definition outfit.h:112
A ship outfit, depends radically on the type.
Definition outfit.h:328
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:124
glTexture * gfx_space
Definition ship.h:138
ShipStats stats_array
Definition ship.h:165
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:140
int points
Definition ship.h:99
double cpu
Definition ship.h:120
ShipOutfitSlot * outfit_utility
Definition ship.h:155
char ** tags
Definition ship.h:168
ShipOutfitSlot * outfit_weapon
Definition ship.h:156
ShipOutfitSlot * outfit_structure
Definition ship.h:154
Abstraction for rendering sprite sheets.
Definition opengl_tex.h:36
double sw
Definition opengl_tex.h:46
double sh
Definition opengl_tex.h:47