Beispiel #1
0
/**
 * @brief Adds an NPC.
 *
 * @usage npc_id = evt.npcAdd( "my_func", "Mr. Test", "none", "A test." ) -- Creates an NPC.
 *
 *    @luaparam func Name of the function to run when approaching, gets passed the npc_id when called.
 *    @luaparam name Name of the NPC
 *    @luaparam portrait Portrait to use for the NPC (from GFX_PATH/portraits/).
 *    @luaparam desc Description associated to the NPC.
 *    @luaparam priority Optional priority argument (defaults to 5, highest is 0, lowest is 10).
 *    @luareturn The ID of the NPC to pass to npcRm.
 * @luafunc npcAdd( func, name, portrait, desc, priority )
 */
static int evt_npcAdd( lua_State *L )
{
   unsigned int id;
   int priority;
   const char *func, *name, *gfx, *desc;
   char portrait[PATH_MAX];
   Event_t *cur_event;

   /* Handle parameters. */
   func = luaL_checkstring(L, 1);
   name = luaL_checkstring(L, 2);
   gfx  = luaL_checkstring(L, 3);
   desc = luaL_checkstring(L, 4);

   /* Optional priority. */
   if (lua_gettop(L) > 4)
      priority = luaL_checkint( L, 5 );
   else
      priority = 5;

   /* Set path. */
   nsnprintf( portrait, PATH_MAX, GFX_PATH"portraits/%s.png", gfx );

   cur_event = event_getFromLua(L);

   /* Add npc. */
   id = npc_add_event( cur_event->id, func, name, priority, portrait, desc );

   /* Return ID. */
   if (id > 0) {
      lua_pushnumber( L, id );
      return 1;
   }
   return 0;
}
Beispiel #2
0
/**
 * @brief Saves an event.
 *
 * @usage evt.save() -- Saves an event, which is by default disabled.
 *
 *    @luaparam enable If true or nil sets the event to save, otherwise tells the event to not save.
 * @luafunc save( enable )
 */
static int evt_save( lua_State *L )
{
   int b;
   Event_t *cur_event;
   if (lua_gettop(L)==0)
      b = 1;
   else
      b = lua_toboolean(L,1);
   cur_event = event_getFromLua(L);
   cur_event->save = b;
   return 0;
}
Beispiel #3
0
/**
 * @brief Creates a mission hook to a certain stack.
 *
 * Basically a generic approach to hooking.
 *
 *    @param L Lua state.
 *    @param stack Stack to put the hook in.
 *    @param ms Milliseconds to delay (pass stack as NULL to set as timer).
 *    @param pos Position in the stack of the function name.
 *    @return The hook ID or 0 on error.
 */
static unsigned int hook_generic( lua_State *L, const char* stack, double ms, int pos, ntime_t date )
{
   int i;
   const char *func;
   unsigned int h;
   Event_t *running_event;
   Mission *running_mission;

   /* Last parameter must be function to hook */
   func = luaL_checkstring(L,pos);

   /* Get stuff. */
   running_event = event_getFromLua(L);
   running_mission = misn_getFromLua(L);

   h = 0;
   if (running_mission != NULL) {
      /* make sure mission is a player mission */
      for (i=0; i<MISSION_MAX; i++)
         if (player_missions[i].id == running_mission->id)
            break;
      if (i>=MISSION_MAX) {
         WARN("Mission not in stack trying to hook, forgot to run misn.accept()?");
         return 0;
      }

      if (stack != NULL)
         h = hook_addMisn( running_mission->id, func, stack );
      else if (date != 0)
         h = hook_addDateMisn( running_mission->id, func, date );
      else
         h = hook_addTimerMisn( running_mission->id, func, ms );
   }
   else if (running_event != NULL) {
      if (stack != NULL)
         h = hook_addEvent( running_event->id, func, stack );
      else if (date != 0)
         h = hook_addDateEvt( running_event->id, func, date );
      else
         h = hook_addTimerEvt( running_event->id, func, ms );
   }

   if (h == 0) {
      NLUA_ERROR(L,"No hook target was set.");
      return 0;
   }

   /* Check parameter. */
   if (!lua_isnil(L,pos+1))
      hookL_setarg( L, h, pos+1 );

   return h;
}
Beispiel #4
0
/**
 * @brief Tries to claim systems.
 *
 * Claiming systems is a way to avoid mission/event collisions preemptively.
 *
 * Note it does not actually claim the systems if it fails to claim. It also
 *  does not work more then once.
 *
 * @usage if not evt.claim( { system.get("Gamma Polaris") } ) then evt.finish( false ) end
 * @usage if not evt.claim( system.get("Gamma Polaris") ) then evt.finish( false ) end
 *
 *    @luaparam systems Table of systems to claim or a single system.
 *    @luareturn true if was able to claim, false otherwise.
 * @luafunc claim( systems )
 */
static int evt_claim( lua_State *L )
{
    LuaSystem *ls;
    SysClaim_t *claim;
    Event_t *cur_event;

    /* Get current event. */
    cur_event = event_getFromLua(L);

    /* Check to see if already claimed. */
    if (cur_event->claims != NULL) {
        NLUA_ERROR(L, "Event trying to claim but already has.");
        return 0;
    }

    /* Create the claim. */
    claim = claim_create();

    /* Handle parameters. */
    if (lua_istable(L,1)) {
        /* Iterate over table. */
        lua_pushnil(L);
        while (lua_next(L, 1) != 0) {
            if (lua_issystem(L,-1)) {
                ls = lua_tosystem( L, -1 );
                claim_add( claim, ls->id );
            }
            lua_pop(L,1);
        }
    }
    else if (lua_issystem(L, 1)) {
        ls = lua_tosystem( L, 1 );
        claim_add( claim, ls->id );
    }
    else
        NLUA_INVALID_PARAMETER(L);

    /* Test claim. */
    if (claim_test( claim )) {
        claim_destroy( claim );
        lua_pushboolean(L,0);
        return 1;
    }

    /* Set the claim. */
    cur_event->claims = claim;
    claim_activate( claim );
    lua_pushboolean(L,1);
    return 1;
}
Beispiel #5
0
/**
 * @brief Removes an NPC.
 *
 * @usage evt.npcRm( npc_id )
 *
 *    @luaparam id ID of the NPC to remove.
 * @luafunc npcRm( id )
 */
static int evt_npcRm( lua_State *L )
{
   unsigned int id;
   int ret;
   Event_t *cur_event;

   id = luaL_checklong(L, 1);

   cur_event = event_getFromLua(L);
   ret = npc_rm_event( id, cur_event->id );

   if (ret != 0)
      NLUA_ERROR(L, "Invalid NPC ID!");
   return 0;
}
Beispiel #6
0
/**
 * @brief Finishes the event.
 *
 *    @luaparam properly If true and the event is unique it marks the event
 *                     as completed.  If false or nil it deletes the event but
 *                     doesn't mark it as completed.
 * @luafunc finish( properly )
 */
static int evt_finish( lua_State *L )
{
   int b;
   Event_t *cur_event;

   b = lua_toboolean(L,1);
   lua_pushboolean( L, 1 );
   lua_setglobal( L, "__evt_delete" );

   cur_event = event_getFromLua(L);
   if (b && event_isUnique(cur_event->id))
      player_eventFinished( cur_event->data );

   lua_pushstring(L, NLUA_DONE);
   lua_error(L); /* shouldn't return */

   return 0;
}