Esempio n. 1
0
/**
 * @brief Gets a system.
 *
 * Behaves differently depending on what you pass as param: <br/>
 *    - string : Gets the system by name. <br/>
 *    - planet : Gets the system by planet. <br/>
 *
 * @usage sys = system.get( p ) -- Gets system where planet 'p' is located.
 * @usage sys = system.get( "Gamma Polaris" ) -- Gets the system by name.
 *
 *    @luatparam string|Planet param Read description for details.
 *    @luatreturn System System matching param.
 * @luafunc get( param )
 */
static int systemL_get( lua_State *L )
{
   StarSystem *ss;
   Planet *pnt;

   /* Passing a string (systemname) */
   if (lua_isstring(L,1)) {
      ss = system_get( lua_tostring(L,1) );
   }
   /* Passing a planet */
   else if (lua_isplanet(L,1)) {
      pnt = luaL_validplanet(L,1);
      ss = system_get( planet_getSystem( pnt->name ) );
   }
   else NLUA_INVALID_PARAMETER(L);

   /* Error checking. */
   if (ss == NULL) {
      NLUA_ERROR(L, "No matching systems found.");
      return 0;
   }

   /* return the system */
   lua_pushsystem(L,system_index(ss));
   return 1;
}
Esempio n. 2
0
/**
 * @brief Gets the current planet - MUST BE LANDED.
 *
 * @usage p,s = planet.cur() -- Gets current planet (assuming landed)
 *
 *    @luatreturn Planet The planet the player is landed on.
 *    @luatreturn System The system it is in.
 * @luafunc cur()
 */
static int planetL_cur( lua_State *L )
{
   LuaSystem sys;
   if (land_planet == NULL) {
      NLUA_ERROR(L,_("Attempting to get landed planet when player not landed."));
      return 0; /* Not landed. */
   }
   lua_pushplanet(L,planet_index(land_planet));
   sys = system_index( system_get( planet_getSystem(land_planet->name) ) );
   lua_pushsystem(L,sys);
   return 2;
}
Esempio n. 3
0
/**
 * @brief Gets a system.
 *
 * Behaves differently depending on what you pass as param:
 *    - nil : -OBSOLETE- Gets the current system. Use system.cur() instead.
 *    - string : Gets the system by name.
 *    - planet : Gets the system by planet.
 *
 * @usage sys = system.get( p ) -- Gets system where planet 'p' is located.
 * @usage sys = system.get( "Gamma Polaris" ) -- Gets the system by name.
 *
 *    @luaparam param Read description for details.
 *    @luareturn System metatable matching param.
 * @luafunc get( param )
 */
static int systemL_get( lua_State *L )
{
    LuaSystem sys;
    StarSystem *ss;
    Planet *pnt;

    /* Invalid by default. */
    sys.id = -1;

    /* Get current system with no parameters */
    if (lua_gettop(L) == 0) {
        sys.id = system_index( cur_system );
    }
    /* Passing a string (systemname) */
    else if (lua_isstring(L,1)) {
        ss = system_get( lua_tostring(L,1) );
        if (ss != NULL)
            sys.id = system_index( ss );
    }
    /* Passing a planet */
    else if (lua_isplanet(L,1)) {
        pnt = luaL_validplanet(L,1);
        ss = system_get( planet_getSystem( pnt->name ) );
        if (ss != NULL)
            sys.id = system_index( ss );
    }
    else NLUA_INVALID_PARAMETER(L);

    /* Error checking. */
    if (sys.id < 0) {
        NLUA_ERROR(L, "No matching systems found.");
        return 0;
    }

    /* return the system */
    lua_pushsystem(L,sys);
    return 1;
}
Esempio n. 4
0
/**
 * @brief Gets the player's long term autonav destination.
 *
 * @usage sys = player.autonavDest()
 *
 *    @luareturn The system the player wants to get to or nil if none selected.
 * @luafunc autonavDest()
 */
static int playerL_autonavDest( lua_State *L )
{
   LuaSystem ls;
   StarSystem *dest;

   /* Get destination. */
   dest = map_getDestination();
   if (dest == NULL)
      return 0;

   ls.id = system_index( dest );
   lua_pushsystem( L, ls );
   return 1;
}
Esempio n. 5
0
/**
 * @brief Gets the system corresponding to a planet.
 *    @luatparam Planet p Planet to get system of.
 *    @luatreturn System|nil The system to which the planet belongs or nil if it has none.
 * @luafunc system( p )
 */
static int planetL_system( lua_State *L )
{
   LuaSystem sys;
   Planet *p;
   const char *sysname;
   /* Arguments. */
   p = luaL_validplanet(L,1);
   sysname = planet_getSystem( p->name );
   if (sysname == NULL)
      return 0;
   sys = system_index( system_get( sysname ) );
   lua_pushsystem( L, sys );
   return 1;
}
Esempio n. 6
0
/**
 * @brief Gets all the systems.
 *    @luatreturn {System,...} A list of all the systems.
 * @luafunc getAll()
 */
static int systemL_getAll( lua_State *L )
{
   StarSystem *sys;
   int i, ind, n;

   lua_newtable(L);
   sys = system_getAll( &n );

   ind = 1;
   for (i=0; i<n; i++) {
      lua_pushnumber( L, ind++ );
      lua_pushsystem( L, system_index( &sys[i] ) );
      lua_settable(   L, -3 );
   }
   return 1;
}
Esempio n. 7
0
/**
 * @brief Gets all the adjacent systems to a system.
 *
 * @usage for _,s in ipairs( sys:adjacentSystems() ) do -- Iterate over adjacent systems.
 *
 *    @luaparam s System to get adjacent systems of.
 *    @luareturn An ordered table with all the adjacent systems.
 * @luafunc adjacentSystems( s )
 */
static int systemL_adjacent( lua_State *L )
{
    int i;
    LuaSystem sysp;
    StarSystem *s;

    s = luaL_validsystem(L,1);

    /* Push all adjacent systems. */
    lua_newtable(L);
    for (i=0; i<s->njumps; i++) {
        sysp.id = system_index( s->jumps[i].target );
        lua_pushnumber(L,i+1); /* key. */
        lua_pushsystem(L,sysp); /* value. */
        lua_rawset(L,-3);
    }

    return 1;
}
Esempio n. 8
0
/**
 * @brief Gets all the adjacent systems to a system.
 *
 * @usage for _,s in ipairs( sys:adjacentSystems() ) do -- Iterate over adjacent systems.
 *
 *    @luaparam s System to get adjacent systems of.
 *    @luaparam hidden Whether or not to show hidden jumps also.
 *    @luareturn An ordered table with all the adjacent systems.
 * @luafunc adjacentSystems( s, hidden )
 */
static int systemL_adjacent( lua_State *L )
{
   int i, h;
   LuaSystem sysp;
   StarSystem *s;

   s = luaL_validsystem(L,1);
   h = lua_toboolean(L,2);

   /* Push all adjacent systems. */
   lua_newtable(L);
   for (i=0; i<s->njumps; i++) {
      if (jp_isFlag(&s->jumps[i], JP_EXITONLY ))
         continue;
      if (!h && jp_isFlag(&s->jumps[i], JP_HIDDEN))
         continue;
      sysp.id = system_index( s->jumps[i].target );
      lua_pushnumber(L,i+1); /* key. */
      lua_pushsystem(L,sysp); /* value. */
      lua_rawset(L,-3);
   }

   return 1;
}
Esempio n. 9
0
/**
 * @brief Gets a planet.
 *
 * Possible values of param:
 *    - nil : -OBSOLETE- Gets the current landed planet or nil if there is none. Use planet.cur() instead.
 *    - bool : Gets a random planet.
 *    - faction : Gets random planet belonging to faction matching the number.
 *    - string : Gets the planet by name.
 *    - table : Gets random planet belonging to any of the factions in the
 *               table.
 *
 * @usage p,s = planet.get( "Anecu" ) -- Gets planet by name
 * @usage p,s = planet.get( faction.get( "Empire" ) ) -- Gets random Empire planet
 * @usage p,s = planet.get(true) -- Gets completely random planet
 * @usage p,s = planet.get( { faction.get("Empire"), faction.get("Dvaered") } ) -- Random planet belonging to Empire or Dvaered
 *    @luaparam param See description.
 *    @luareturn Returns the planet and the system it belongs to.
 * @luafunc get( param )
 */
static int planetL_get( lua_State *L )
{
   int i;
   int *factions;
   int nfactions;
   char **planets;
   int nplanets;
   const char *rndplanet;
   LuaPlanet planet;
   LuaSystem luasys;
   LuaFaction *f;
   Planet *pnt;
   StarSystem *sys;
   char *sysname;

   rndplanet = NULL;
   planets   = NULL;
   nplanets  = 0;

   /* Get the landed planet */
   if (lua_gettop(L) == 0) {
      if (land_planet != NULL) {
         planet.id = planet_index( land_planet );
         lua_pushplanet(L,planet);
         luasys.id = system_index( system_get( planet_getSystem(land_planet->name) ) );
         lua_pushsystem(L,luasys);
         return 2;
      }
      NLUA_ERROR(L,"Attempting to get landed planet when player not landed.");
      return 0; /* Not landed. */
   }

   /* If boolean return random. */
   else if (lua_isboolean(L,1)) {
      pnt = planet_get( space_getRndPlanet() );
      planet.id    = planet_index( pnt );
      lua_pushplanet(L,planet);
      luasys.id      = system_index( system_get( planet_getSystem(pnt->name) ) );
      lua_pushsystem(L,luasys);
      return 2;
   }

   /* Get a planet by faction */
   else if (lua_isfaction(L,1)) {
      f = lua_tofaction(L,1);
      planets = space_getFactionPlanet( &nplanets, &f->f, 1 );
   }

   /* Get a planet by name */
   else if (lua_isstring(L,1)) {
      rndplanet = lua_tostring(L,1);
   }

   /* Get a planet from faction list */
   else if (lua_istable(L,1)) {
      /* Get table length and preallocate. */
      nfactions = (int) lua_objlen(L,1);
      factions = malloc( sizeof(int) * nfactions );
      /* Load up the table. */
      lua_pushnil(L);
      i = 0;
      while (lua_next(L, -2) != 0) {
         if (lua_isfaction(L, -1)) {
            f = lua_tofaction(L, -1);
            factions[i++] = f->f;
         }
         lua_pop(L,1);
      }

      /* get the planets */
      planets = space_getFactionPlanet( &nplanets, factions, nfactions );
      free(factions);
   }
   else 
      NLUA_INVALID_PARAMETER(L); /* Bad Parameter */

   /* No suitable planet found */
   if ((rndplanet == NULL) && ((planets == NULL) || nplanets == 0))
      return 0;
   /* Pick random planet */
   else if (rndplanet == NULL) {
      rndplanet = planets[RNG(0,nplanets-1)];
      free(planets);
   }

   /* Push the planet */
   pnt = planet_get(rndplanet); /* The real planet */
   if (pnt == NULL) {
      NLUA_ERROR(L, "Planet '%s' not found in stack", rndplanet);
      return 0;
   }
   sysname = planet_getSystem(rndplanet);
   if (sysname == NULL) {
      NLUA_ERROR(L, "Planet '%s' is not placed in a system", rndplanet);
      return 0;
   }
   sys = system_get( sysname );
   if (sys == NULL) {
      NLUA_ERROR(L, "Planet '%s' can't find system '%s'", rndplanet, sysname); 
      return 0;
   }
   planet.id = planet_index( pnt );
   lua_pushplanet(L,planet);
   luasys.id = system_index( sys );
   lua_pushsystem(L,luasys);
   return 2;
}
Esempio n. 10
0
/**
 * @brief Gets the current system.
 *
 * @usage sys = system.cur() -- Gets the current system
 *
 *    @luatreturn System Current system.
 * @luafunc cur()
 */
static int systemL_cur( lua_State *L )
{
   lua_pushsystem(L,system_index( cur_system ));
   return 1;
}
Esempio n. 11
0
static int planetL_getBackend( lua_State *L, int landable )
{
   int i;
   int *factions;
   int nfactions;
   char **planets;
   int nplanets;
   const char *rndplanet;
   LuaSystem luasys;
   LuaFaction f;
   Planet *pnt;
   StarSystem *sys;
   char *sysname;

   rndplanet = NULL;
   planets   = NULL;
   nplanets  = 0;

   /* If boolean return random. */
   if (lua_isboolean(L,1)) {
      pnt            = planet_get( space_getRndPlanet(landable, 0, NULL) );
      lua_pushplanet(L,planet_index( pnt ));
      luasys         = system_index( system_get( planet_getSystem(pnt->name) ) );
      lua_pushsystem(L,luasys);
      return 2;
   }

   /* Get a planet by faction */
   else if (lua_isfaction(L,1)) {
      f        = lua_tofaction(L,1);
      planets  = space_getFactionPlanet( &nplanets, &f, 1, landable );
   }

   /* Get a planet by name */
   else if (lua_isstring(L,1)) {
      rndplanet = lua_tostring(L,1);

      if (landable) {
         pnt = planet_get( rndplanet );
         if (pnt == NULL) {
            NLUA_ERROR(L, _("Planet '%s' not found in stack"), rndplanet);
            return 0;
         }

         /* Check if can land. */
         planet_updateLand( pnt );
         if (!pnt->can_land)
            return 0;
      }
   }

   /* Get a planet from faction list */
   else if (lua_istable(L,1)) {
      /* Get table length and preallocate. */
      nfactions = (int) lua_objlen(L,1);
      factions = malloc( sizeof(int) * nfactions );
      /* Load up the table. */
      lua_pushnil(L);
      i = 0;
      while (lua_next(L, -2) != 0) {
         if (lua_isfaction(L, -1))
            factions[i++] = lua_tofaction(L, -1);
         lua_pop(L,1);
      }

      /* get the planets */
      planets = space_getFactionPlanet( &nplanets, factions, nfactions, landable );
      free(factions);
   }
   else
      NLUA_INVALID_PARAMETER(L); /* Bad Parameter */

   /* No suitable planet found */
   if ((rndplanet == NULL) && ((planets == NULL) || nplanets == 0))
      return 0;
   /* Pick random planet */
   else if (rndplanet == NULL) {
      planets = (char**) arrayShuffle( (void**)planets, nplanets );

      for (i=0; i<nplanets; i++) {
         if (landable) {
            /* Check landing. */
            pnt = planet_get( planets[i] );
            if (pnt == NULL)
               continue;

            planet_updateLand( pnt );
            if (!pnt->can_land)
               continue;
         }

         rndplanet = planets[i];
         break;
      }
      free(planets);
   }

   /* Push the planet */
   pnt = planet_get(rndplanet); /* The real planet */
   if (pnt == NULL) {
      NLUA_ERROR(L, _("Planet '%s' not found in stack"), rndplanet);
      return 0;
   }
   sysname = planet_getSystem(rndplanet);
   if (sysname == NULL) {
      NLUA_ERROR(L, _("Planet '%s' is not placed in a system"), rndplanet);
      return 0;
   }
   sys = system_get( sysname );
   if (sys == NULL) {
      NLUA_ERROR(L, _("Planet '%s' can't find system '%s'"), rndplanet, sysname);
      return 0;
   }
   lua_pushplanet(L,planet_index( pnt ));
   luasys = system_index( sys );
   lua_pushsystem(L,luasys);
   return 2;
}
Esempio n. 12
0
/**
 * @brief Parses the actual individual mission nodes.
 *
 *    @param parent Parent node to parse.
 *    @return 0 on success.
 */
static int missions_parseActive( xmlNodePtr parent )
{
   Mission *misn;
   MissionData *data;
   int m, i;
   char *buf;
   char *title;
   const char **items;
   int nitems;
   int id, sys, type;
   StarSystem *ssys;

   xmlNodePtr node, cur, nest;

   m = 0; /* start with mission 0 */
   node = parent->xmlChildrenNode;
   do {
      if (xml_isNode(node,"mission")) {
         misn = player_missions[m];

         /* process the attributes to create the mission */
         xmlr_attr(node,"data",buf);
         data = mission_get(mission_getID(buf));
         if (data == NULL) {
            WARN("Mission '%s' from savegame not found in game - ignoring.", buf);
            free(buf);
            continue;
         }
         else {
            if (mission_init( misn, data, 0, 0, NULL )) {
               WARN("Mission '%s' from savegame failed to load properly - ignoring.", buf);
               free(buf);
               continue;
            }
            misn->accepted = 1;
         }
         free(buf);

         /* this will orphan an identifier */
         xmlr_attr(node,"id",buf);
         misn->id = atol(buf);
         free(buf);

         cur = node->xmlChildrenNode;
         do {

            xmlr_strd(cur,"title",misn->title);
            xmlr_strd(cur,"desc",misn->desc);
            xmlr_strd(cur,"reward",misn->reward);

            /* Get the markers. */
            if (xml_isNode(cur,"markers")) {
               nest = cur->xmlChildrenNode;
               do {
                  if (xml_isNode(nest,"marker")) {
                     /* Get ID. */
                     xmlr_attr(nest,"id",buf);
                     id = (buf != NULL) ? atoi(buf) : -1;
                     if (buf != NULL)
                        free(buf);
                     /* Get type. */
                     xmlr_attr(nest,"type",buf);
                     type = (buf != NULL) ? atoi(buf) : -1;
                     if (buf != NULL)
                        free(buf);
                     /* Get system. */
                     ssys = system_get( xml_get( nest ));
                     if (ssys == NULL) {
                        WARN( "System Marker to '%s' does not exist", xml_get( nest ) );
                        continue;
                     }
                     sys = system_index( ssys );
                     mission_addMarker( misn, id, sys, type );
                  }
               } while (xml_nextNode(nest));
            }

            /* Cargo. */
            if (xml_isNode(cur,"cargos")) {
               nest = cur->xmlChildrenNode;
               do {
                  if (xml_isNode(nest,"cargo"))
                     mission_linkCargo( misn, xml_getLong(nest) );
               } while (xml_nextNode(nest));
            }

            /* OSD. */
            if (xml_isNode(cur,"osd")) {
               xmlr_attr(cur,"nitems",buf);
               if (buf != NULL) {
                  nitems = atoi(buf);
                  free(buf);
               }
               else
                  continue;
               xmlr_attr(cur,"title",title);
               items = malloc( nitems * sizeof(char*) );
               i = 0;
               nest = cur->xmlChildrenNode;
               do {
                  if (xml_isNode(nest,"msg")) {
                     if (i > nitems) {
                        WARN("Inconsistency with 'nitems' in savefile.");
                        break;
                     }
                     items[i] = xml_get(nest);
                     i++;
                  }
               } while (xml_nextNode(nest));

               /* Create the osd. */
               misn->osd = osd_create( title, nitems, items, data->avail.priority );
               free(items);
               free(title);

               /* Set active. */
               xmlr_attr(cur,"active",buf);
               if (buf != NULL) {
                  osd_active( misn->osd, atoi(buf) );
                  free(buf);
               }
            }

            /* Claims. */
            if (xml_isNode(cur,"claims"))
               misn->claims = claim_xmlLoad( cur );

            if (xml_isNode(cur,"lua"))
               /* start the unpersist routine */
               nxml_unpersistLua( misn->L, cur );

         } while (xml_nextNode(cur));



         m++; /* next mission */
         if (m >= MISSION_MAX) break; /* full of missions, must be an error */
      }
   } while (xml_nextNode(node));

   return 0;
}
Esempio n. 13
0
/**
 * @brief Unpersists Lua data.
 *
 *    @param L State to unperisist data into.
 *    @param parent Node containing all the Lua persisted data.
 *    @return 0 on success.
 */
static int nxml_unpersistDataNode( lua_State *L, xmlNodePtr parent )
{
   LuaPlanet p;
   LuaSystem s;
   LuaFaction f;
   LuaShip sh;
   LuaTime lt;
   Planet *pnt;
   StarSystem *ss;
   xmlNodePtr node;
   char *name, *type, *buf, *num;
   int keynum;

   node = parent->xmlChildrenNode;
   do {
      if (xml_isNode(node,"data")) {
         /* Get general info. */
         xmlr_attr(node,"name",name);
         xmlr_attr(node,"type",type);
         /* Check to see if key is a number. */
         xmlr_attr(node,"keynum",num);
         if (num != NULL) {
            keynum = 1;
            lua_pushnumber(L, atof(name));
            free(num);
         }
         else
            lua_pushstring(L, name);

         /* handle data types */
         /* Recursive tables. */
         if (strcmp(type,"table")==0) {
            xmlr_attr(node,"name",buf);
            /* Create new table. */
            lua_newtable(L);
            /* Save data. */
            nxml_unpersistDataNode(L,node);
            /* Set table. */
            free(buf);
         }
         else if (strcmp(type,"number")==0)
            lua_pushnumber(L,xml_getFloat(node));
         else if (strcmp(type,"bool")==0)
            lua_pushboolean(L,xml_getInt(node));
         else if (strcmp(type,"string")==0)
            lua_pushstring(L,xml_get(node));
         else if (strcmp(type,"planet")==0) {
            pnt = planet_get(xml_get(node));
            if (pnt != NULL) {
               p.id = planet_index(pnt);
               lua_pushplanet(L,p);
            }
            else
               WARN("Failed to load unexistent planet '%s'", xml_get(node));
         }
         else if (strcmp(type,"system")==0) {
            ss = system_get(xml_get(node));
            if (ss != NULL) {
               s.id = system_index( ss );
               lua_pushsystem(L,s);
            }
            else
               WARN("Failed to load unexistent system '%s'", xml_get(node));
         }
         else if (strcmp(type,"faction")==0) {
            f.f = faction_get(xml_get(node));
            lua_pushfaction(L,f);
         }
         else if (strcmp(type,"ship")==0) {
            sh.ship = ship_get(xml_get(node));
            lua_pushship(L,sh);
         }
         else if (strcmp(type,"time")==0) {
            lt.t = xml_getLong(node);
            lua_pushtime(L,lt);
         }
         else {
            WARN("Unknown lua data type!");
            lua_pop(L,1);
            return -1;
         }

         /* Set field. */
         lua_settable(L, -3);

         /* cleanup */
         free(type);
         free(name);
      }
   } while (xml_nextNode(node));

   return 0;
}