Beispiel #1
0
/**
 * @brief Initializes a mission.
 *
 *    @param mission Mission to initialize.
 *    @param misn Data to use.
 *    @param genid 1 if should generate id, 0 otherwise.
 *    @param create 1 if should run create function, 0 otherwise.
 *    @param[out] id ID of the newly created mission.
 *    @return 0 on success.
 */
static int mission_init( Mission* mission, MissionData* misn, int genid, int create, unsigned int *id )
{
   char *buf;
   uint32_t bufsize;
   int ret;

   /* clear the mission */
   memset( mission, 0, sizeof(Mission) );

   /* Create id if needed. */
   mission->id    = (genid) ? mission_genID() : 0;
   if (id != NULL)
      *id         = mission->id;
   mission->data  = misn;
   if (create) {
      mission->title = strdup(misn->name);
      mission->desc  = strdup("No description.");
   }

   /* init Lua */
   mission->L = nlua_newState();
   if (mission->L == NULL) {
      WARN("Unable to create a new Lua state.");
      return -1;
   }
   nlua_loadBasic( mission->L ); /* pairs and such */
   misn_loadLibs( mission->L ); /* load our custom libraries */

   /* load the file */
   buf = ndata_read( misn->lua, &bufsize );
   if (buf == NULL) {
      WARN("Mission '%s' Lua script not found.", misn->lua );
      return -1;
   }
   if (luaL_dobuffer(mission->L, buf, bufsize, misn->lua) != 0) {
      WARN("Error loading mission file: %s\n"
          "%s\n"
          "Most likely Lua file has improper syntax, please check",
            misn->lua, lua_tostring(mission->L,-1));
      free(buf);
      return -1;
   }
   free(buf);

   /* run create function */
   if (create) {
      /* Failed to create. */
      ret = misn_run( mission, "create");
      if (ret) {
         mission_cleanup(mission);
         return ret;
      }
   }

   return 0;
}
Beispiel #2
0
/**
 * @brief Initializes the conditional subsystem.
 */
int cond_init (void)
{
   if (cond_L != NULL)
      return 0;

   cond_L = nlua_newState();
   if (nlua_loadStandard(cond_L,1)) {
      WARN("Failed to load standard Lua libraries.");
      return -1;
   }

   return 0;
}
Beispiel #3
0
/**
 * @brief Runs the rescue script if players are stuck.
 */
static void land_stranded (void)
{
   char *buf;
   uint32_t bufsize;
   const char *file = "dat/rescue.lua";
   int errf;
   lua_State *L;

   /* Nothing to do if there's no rescue script. */
   if (!ndata_exists(file))
      return;

   if (rescue_L == NULL) {
      rescue_L = nlua_newState();
      nlua_loadStandard( rescue_L, 0 );
      nlua_loadTk( rescue_L );

      L = rescue_L;

      buf = ndata_read( file, &bufsize );
      if (luaL_dobuffer(L, buf, bufsize, file) != 0) {
         WARN("Error loading file: %s\n"
             "%s\n"
             "Most likely Lua file has improper syntax, please check",
               file, lua_tostring(L,-1));
         free(buf);
         return;
      }
      free(buf);
   }
   else
      L = rescue_L;

#if DEBUGGING
   lua_pushcfunction(L, nlua_errTrace);
   errf = -2;
#else /* DEBUGGING */
   errf = 0;
#endif /* DEBUGGING */


   /* Run Lua. */
   lua_getglobal(L,"rescue");
   if (lua_pcall(L, 0, 0, errf)) { /* error has occurred */
      WARN("Rescue: 'rescue' : '%s'", lua_tostring(L,-1));
      lua_pop(L,1);
   }
#if DEBUGGING
   lua_pop(L,1);
#endif
}
Beispiel #4
0
/**
 * @brief Creates an event.
 *
 *    @param data Data to base event off of.
 */
static int event_create( int dataid )
{
   lua_State *L;
   uint32_t bufsize;
   char *buf;
   Event_t *ev;
   EventData_t *data;

   /* Create the event. */
   event_nactive++;
   if (event_nactive > event_mactive) {
      event_mactive += EVENT_CHUNK;
      event_active = realloc( event_active, sizeof(Event_t) * event_mactive );
   }
   ev = &event_active[ event_nactive-1 ];
   memset( ev, 0, sizeof(Event_t) );
   ev->id = ++event_genid; /* Create unique ID. */

   /* Add the data. */
   ev->data = dataid;
   data = &event_data[dataid];

   /* Open the new state. */
   ev->L = nlua_newState();
   L = ev->L;
   nlua_loadStandard(L,0);
   nlua_loadEvt(L);
   nlua_loadHook(L);
   nlua_loadTk(L);

   /* Load file. */
   buf = ndata_read( data->lua, &bufsize );
   if (buf == NULL) {
      WARN("Event '%s' Lua script not found.", data->lua );
      return -1;
   }
   if (luaL_dobuffer(L, buf, bufsize, data->lua) != 0) {
      WARN("Error loading event file: %s\n"
            "%s\n"
            "Most likely Lua file has improper syntax, please check",
            data->lua, lua_tostring(L,-1));
      return -1;
   }
   free(buf);

   /* Run Lua. */
   event_runLua( ev, "create" );

   return 0;
}
Beispiel #5
0
/**
 * @brief Initializes the CLI environment.
 */
int cli_init (void)
{
   /* Already loaded. */
   if (cli_state != NULL)
      return 0;

   /* Calculate size. */
   cli_width   = SCREEN_W - 100;
   cli_height  = SCREEN_H - 100;

   /* Create the state. */
   cli_state   = nlua_newState();
   nlua_loadStandard( cli_state, 0 );
   nlua_loadCol( cli_state, 0 );
   nlua_loadTex( cli_state, 0 );
   nlua_loadBackground( cli_state, 0 );
   nlua_loadCamera( cli_state, 0 );
   nlua_loadTk( cli_state );
   nlua_loadCLI( cli_state );
   nlua_loadMusic( cli_state, 0 );
   luaL_register( cli_state, "_G", cli_methods );
   lua_settop( cli_state, 0 );

   /* Mark as console. */
   lua_pushboolean( cli_state, 1 );
   lua_setglobal( cli_state, "__cli" );

   /* Set the font. */
   cli_font    = malloc( sizeof(glFont) );
   gl_fontInit( cli_font, "dat/mono.ttf", CONSOLE_FONT_SIZE );

   /* Clear the buffer. */
   memset( cli_buffer, 0, sizeof(cli_buffer) );

   /* Put a friendly message at first. */
   cli_addMessage( "Welcome to the Lua console!" );
   cli_addMessage( "" );

   return 0;
}
Beispiel #6
0
/**
 * @brief Creates a background Lua state from a script.
 */
static lua_State* background_create( const char *name )
{
   uint32_t bufsize;
   char path[PATH_MAX];
   char *buf;
   lua_State *L;

   /* Create file name. */
   snprintf( path, sizeof(path), "dat/bkg/%s.lua", name );

   /* Create the Lua state. */
   L = nlua_newState();
   nlua_loadStandard(L,1);
   nlua_loadTex(L,0);
   nlua_loadCol(L,0);
   nlua_loadBackground(L,0);

   /* Open file. */
   buf = ndata_read( path, &bufsize );
   if (buf == NULL) {
      WARN("Default background script '%s' not found.", path);
      lua_close(L);
      return NULL;
   }

   /* Load file. */
   if (luaL_dobuffer(L, buf, bufsize, path) != 0) {
      WARN("Error loading background file: %s\n"
            "%s\n"
            "Most likely Lua file has improper syntax, please check",
            path, lua_tostring(L,-1));
      free(buf);
      lua_close(L);
      return NULL;
   }
   free(buf);

   return L;
}
Beispiel #7
0
/**
 * @brief Parses a single faction, but doesn't set the allies/enemies bit.
 *
 *    @param temp Faction to load data into.
 *    @param parent Parent node to extract faction from.
 *    @return Faction created from parent node.
 */
static int faction_parse( Faction* temp, xmlNodePtr parent )
{
   xmlNodePtr node;
   int player;
   char buf[PATH_MAX], *dat;
   uint32_t ndat;

   /* Clear memory. */
   memset( temp, 0, sizeof(Faction) );

   temp->name = xml_nodeProp(parent,"name");
   if (temp->name == NULL)
      WARN("Faction from "FACTION_DATA_PATH" has invalid or no name");

   player = 0;
   node = parent->xmlChildrenNode;
   do {

      /* Only care about nodes. */
      xml_onlyNodes(node);

      /* Can be 0 or negative, so we have to take that into account. */
      if (xml_isNode(node,"player")) {
         temp->player_def = xml_getFloat(node);
         player = 1;
         continue;
      }

      xmlr_strd(node,"longname",temp->longname);
      xmlr_strd(node,"display",temp->displayname);
      if (xml_isNode(node, "colour")) {
         temp->colour = col_fromName(xml_raw(node));
         continue;
      }

      if (xml_isNode(node, "spawn")) {
         if (temp->sched_state != NULL)
            WARN("Faction '%s' has duplicate 'spawn' tag.", temp->name);
         nsnprintf( buf, sizeof(buf), "dat/factions/spawn/%s.lua", xml_raw(node) );
         temp->sched_state = nlua_newState();
         nlua_loadStandard( temp->sched_state, 0 );
         dat = ndata_read( buf, &ndat );
         if (luaL_dobuffer(temp->sched_state, dat, ndat, buf) != 0) {
            WARN("Failed to run spawn script: %s\n"
                  "%s\n"
                  "Most likely Lua file has improper syntax, please check",
                  buf, lua_tostring(temp->sched_state,-1));
            lua_close( temp->sched_state );
            temp->sched_state = NULL;
         }
         free(dat);
         continue;
      }

      if (xml_isNode(node, "standing")) {
         if (temp->state != NULL)
            WARN("Faction '%s' has duplicate 'standing' tag.", temp->name);
         nsnprintf( buf, sizeof(buf), "dat/factions/standing/%s.lua", xml_raw(node) );
         temp->state = nlua_newState();
         nlua_loadStandard( temp->state, 0 );
         dat = ndata_read( buf, &ndat );
         if (luaL_dobuffer(temp->state, dat, ndat, buf) != 0) {
            WARN("Failed to run standing script: %s\n"
                  "%s\n"
                  "Most likely Lua file has improper syntax, please check",
                  buf, lua_tostring(temp->state,-1));
            lua_close( temp->state );
            temp->state = NULL;
         }
         free(dat);
         continue;
      }

      if (xml_isNode(node, "known")) {
         faction_setFlag(temp, FACTION_KNOWN);
         continue;
      }

      if (xml_isNode(node, "equip")) {
         if (temp->equip_state != NULL)
            WARN("Faction '%s' has duplicate 'equip' tag.", temp->name);
         nsnprintf( buf, sizeof(buf), "dat/factions/equip/%s.lua", xml_raw(node) );
         temp->equip_state = nlua_newState();
         nlua_loadStandard( temp->equip_state, 0 );
         dat = ndata_read( buf, &ndat );
         if (luaL_dobuffer(temp->equip_state, dat, ndat, buf) != 0) {
            WARN("Failed to run equip script: %s\n"
                  "%s\n"
                  "Most likely Lua file has improper syntax, please check",
                  buf, lua_tostring(temp->equip_state,-1));
            lua_close( temp->equip_state );
            temp->equip_state = NULL;
         }
         free(dat);
         continue;
      }

      if (xml_isNode(node,"logo")) {
         if (temp->logo_small != NULL)
            WARN("Faction '%s' has duplicate 'logo' tag.", temp->name);
         nsnprintf( buf, PATH_MAX, FACTION_LOGO_PATH"%s_small.png", xml_get(node));
         temp->logo_small = gl_newImage(buf, 0);
         nsnprintf( buf, PATH_MAX, FACTION_LOGO_PATH"%s_tiny.png", xml_get(node));
         temp->logo_tiny = gl_newImage(buf, 0);
         continue;
      }

      if (xml_isNode(node,"static")) {
         faction_setFlag(temp, FACTION_STATIC);
         continue;
      }

      if (xml_isNode(node,"invisible")) {
         faction_setFlag(temp, FACTION_INVISIBLE);
         continue;
      }

      /* Avoid warnings. */
      if (xml_isNode(node,"allies") || xml_isNode(node,"enemies"))
         continue;

      DEBUG("Unknown node '%s' in faction '%s'",node->name,temp->name);
   } while (xml_nextNode(node));

   if (player==0)
      DEBUG("Faction '%s' missing player tag.", temp->name);
   if ((temp->state!=NULL) && faction_isFlag( temp, FACTION_STATIC ))
      WARN("Faction '%s' has Lua and is static!", temp->name);
   if ((temp->state==NULL) && !faction_isFlag( temp, FACTION_STATIC ))
      WARN("Faction '%s' has no Lua and isn't static!", temp->name);

   return 0;
}