Permalink
Browse files

added "ship.getSlots" lua function

  • Loading branch information...
1 parent cb602d0 commit 5181772becefc63b59f99187a8cb8cf77136e7b0 Barium Blue committed Jan 30, 2013
Showing with 84 additions and 3 deletions.
  1. +3 −3 src/nlua_news.c
  2. +63 −0 src/nlua_ship.c
  3. +17 −0 src/outfit.c
  4. +1 −0 src/outfit.h
View
@@ -125,10 +125,10 @@ int newsL_add( lua_State *L ){
{
lua_pushnil(L);
- //traverse table
+ /* traverse table */
while (lua_next(L, -2)) {
- //traverse sub table
+ /* traverse sub table */
if (lua_istable(L,-1)){
lua_pushnil(L);
@@ -539,7 +539,7 @@ int newsL_bind( lua_State *L )
lua_pushnil(L);
- //traverse table
+ /* traverse table */
while (lua_next(L, -2)) {
if (!(a = luaL_validarticle( L,-1 ))){
WARN("Bad argument to news.date(), must be article or a table of articles");
View
@@ -9,6 +9,7 @@
*/
#include "nlua_ship.h"
+#include "slots.h"
#include "naev.h"
@@ -28,6 +29,7 @@ static int shipL_name( lua_State *L );
static int shipL_baseType( lua_State *L );
static int shipL_class( lua_State *L );
static int shipL_slots( lua_State *L );
+static int shipL_getSlots( lua_State *L );
static int shipL_CPU( lua_State *L );
static int shipL_outfitCPU( lua_State *L );
static int shipL_gfxTarget( lua_State *L );
@@ -40,6 +42,7 @@ static const luaL_reg shipL_methods[] = {
{ "baseType", shipL_baseType },
{ "class", shipL_class },
{ "slots", shipL_slots },
+ { "getSlots", shipL_getSlots },
{ "cpu", shipL_CPU },
{ "outfitCPU", shipL_outfitCPU },
{ "gfxTarget", shipL_gfxTarget },
@@ -325,6 +328,66 @@ static int shipL_slots( lua_State *L )
/**
+ * @brief Get a table of slots of a ship, where a slot is a table with a string size, type, and property
+ *
+ * @usage for _,v in ipairs( ship.getSlots( ship.get("Llama") ) ) do print(v["type"]) end
+ *
+ * @luaparam s Ship to get slots of
+ * @luareturn A table of tables with slot properties string "size", string "type", and string "property"
+ */
+static int shipL_getSlots( lua_State *L )
+{
+ int i, k;
+ Ship *s;
+ OutfitSlot *slot;
+ int outfit_type = 0;
+ char *outfit_types[] = {"structure", "utility", "weapon"};
+
+ s = luaL_validship(L,1);
+
+ lua_newtable(L);
+ k=1;
+ for (i=0; i < s->outfit_nstructure + s->outfit_nutility + s->outfit_nweapon ; i++){
+
+ /* get the slot */
+ if (i < s->outfit_nstructure){
+ slot = &s->outfit_structure[i].slot;
+ outfit_type = 0;
+ }
+ else if (i < s->outfit_nstructure + s->outfit_nutility){
+ slot = &s->outfit_utility[i].slot;
+ outfit_type = 1;
+ }
+ else{
+ slot = &s->outfit_weapon[i].slot;
+ outfit_type = 2;
+ }
+
+ /* make the slot table and put it in */
+ lua_pushnumber(L, k++); /* slot table key */
+ lua_newtable(L);
+
+ lua_pushstring(L, "type"); /* key */
+ lua_pushstring(L, outfit_types[outfit_type]); /* value */
+ lua_rawset(L, -3); /* table[key = value ]*/
+
+ lua_pushstring(L, "size"); /* key */
+ lua_pushstring(L, slotSize( slot->size) );
+ lua_rawset(L, -3); /* table[key] = value */
+
+ lua_pushstring(L, "property"); /* key */
+ lua_pushstring( L, sp_display(slot->spid)); /* value */ /* some spids seem to be random values... */
+ lua_rawset(L, -3); /* table[key] = value */
+
+ lua_rawset(L, -3); /* put the slot table in */
+ }
+
+ return 1;
+
+}
+
+
+/**
* @brief Gets the ship available CPU.
*
* @usage cpu_left = s:cpu()
View
@@ -269,6 +269,23 @@ const char *outfit_slotSize( const Outfit* o )
}
}
+const char *slotSize( const OutfitSlotSize o )
+{
+ switch( o ) {
+ case OUTFIT_SLOT_SIZE_NA:
+ return "NA";
+ case OUTFIT_SLOT_SIZE_LIGHT:
+ return "Small";
+ case OUTFIT_SLOT_SIZE_MEDIUM:
+ return "Medium";
+ case OUTFIT_SLOT_SIZE_HEAVY:
+ return "Large";
+ default:
+ return "Unknown";
+ }
+}
+
+
/**
* @brief Gets the slot size colour for an outfit slot.
View
@@ -390,6 +390,7 @@ char **outfit_searchFuzzyCase( const char* name, int *n );
*/
const char *outfit_slotName( const Outfit* o );
const char *outfit_slotSize( const Outfit* o );
+const char *slotSize( const OutfitSlotSize o );
const glColour *outfit_slotSizeColour( const OutfitSlot* os );
OutfitSlotSize outfit_toSlotSize( const char *s );
glTexture* outfit_gfx( const Outfit* o );

0 comments on commit 5181772

Please sign in to comment.