Example #1
0
/**
 * @brief Gets the presence in the system.
 *
 * Possible parameters are besides a faction:<br/>
 *  - "all": Gets the sum of all the presences.<br />
 *  - "friendly": Gets the sum of all the friendly presences.<br />
 *  - "hostile": Gets the sum of all the hostile presences.<br />
 *  - "neutral": Gets the sum of all the neutral presences.<br />
 *
 * @usage p = sys:presence( f ) -- Gets the presence of a faction f
 * @usage p = sys:presence( "all" ) -- Gets the sum of all the presences
 * @usage if sys:presence("friendly") > sys:presence("hostile") then -- Checks to see if the system is dominantly friendly
 *
 *    @luatparam System s System to get presence level of.
 *    @luatreturn number The presence level in sys (absolute value).
 * @luafunc presence( s )
 */
static int systemL_presence( lua_State *L )
{
   StarSystem *sys;
   int *fct;
   int nfct;
   double presence, v;
   int i, f, used;
   const char *cmd;

   /* Get parameters. */
   sys = luaL_validsystem(L, 1);

   /* Allow fall-through. */
   used = 0;

   /* Get the second parameter. */
   if (lua_isstring(L, 2)) {
      /* A string command has been given. */
      cmd  = lua_tostring(L, 2);
      nfct = 0;
      used = 1;

      /* Check the command string and get the appropriate faction group.*/
      if(strcmp(cmd, "all") == 0)
         fct = faction_getGroup(&nfct, 0);
      else if(strcmp(cmd, "friendly") == 0)
         fct = faction_getGroup(&nfct, 1);
      else if(strcmp(cmd, "hostile") == 0)
         fct = faction_getGroup(&nfct, 3);
      else if(strcmp(cmd, "neutral") == 0)
         fct = faction_getGroup(&nfct, 2);
      else /* Invalid command string. */
         used = 0;
   }

   if (!used) {
      /* A faction id was given. */
      f      = luaL_validfaction(L, 2);
      nfct   = 1;
      fct    = malloc(sizeof(int));
      fct[0] = f;
   }

   /* Add up the presence values. */
   presence = 0;
   for(i=0; i<nfct; i++) {
      /* Only count positive presences. */
      v = system_getPresence( sys, fct[i] );
      if (v > 0)
         presence += v;
   }

   /* Clean up after ourselves. */
   free(fct);

   /* Push it back to Lua. */
   lua_pushnumber(L, presence);
   return 1;
}
Example #2
0
/**
 * @brief Gets the presence in the system.
 *
 * Possible parameters are besides a faction:<br/>
 *  - "all": Gets the sum of all the presences.<br />
 *  - "friendly": Gets the sum of all the friendly presences.<br />
 *  - "hostile": Gets the sum of all the hostile presences.<br />
 *  - "neutral": Gets the sum of all the neutral presences.<br />
 *
 * @usage p = sys:presence( f ) -- Gets the presence of a faction f
 * @usage p = sys:presence( "all" ) -- Gets the sum of all the presences
 * @usage if sys:presence("friendly") > sys:presence("hostile") then -- Checks to see if the system is dominantly friendly
 *
 *    @luaparam s System to get presence level of.
 *    @luareturn The presence level in sys (absolute value).
 * @luafunc presence( s )
 */
static int systemL_presence( lua_State *L )
{
    StarSystem *sys;
    LuaFaction *lf;
    int *fct;
    int nfct;
    double presence;
    int i;
    const char *cmd;

    /* Get parameters. */
    sys = luaL_validsystem(L, 1);

    /* Get the second parameter. */
    if (lua_isstring(L, 2)) {
        /* A string command has been given. */
        cmd  = lua_tostring(L, 2);
        nfct = 0;

        /* Check the command string and get the appropriate faction group.*/
        if(strcmp(cmd, "all") == 0)
            fct = faction_getGroup(&nfct, 0);
        else if(strcmp(cmd, "friendly") == 0)
            fct = faction_getGroup(&nfct, 1);
        else if(strcmp(cmd, "hostile") == 0)
            fct = faction_getGroup(&nfct, 3);
        else if(strcmp(cmd, "neutral") == 0)
            fct = faction_getGroup(&nfct, 2);
        else /* Invalid command string. */
            NLUA_INVALID_PARAMETER(L);
    }
    else if (lua_isfaction(L, 2)) {
        /* A faction id was given. */
        lf     = lua_tofaction(L, 2);
        nfct   = 1;
        fct    = malloc(sizeof(int) * nfct);
        fct[0] = lf->f;
    }
    else NLUA_INVALID_PARAMETER(L);

    /* Add up the presence values. */
    presence = 0;
    for(i=0; i<nfct; i++)
        presence += system_getPresence( sys, fct[i] );

    /* Clean up after ourselves. */
    free(fct);

    /* Push it back to Lua. */
    lua_pushnumber(L, presence);
    return 1;
}