From dcf093b13edb4b65f85ad2a6e37ed46da7392d90 Mon Sep 17 00:00:00 2001 From: Allanis Date: Tue, 9 Jul 2013 16:11:14 +0100 Subject: [PATCH] [Add] Two new hooks: Board and disable. Also made lua debug more verbose. --- dat/missions/emp_scout00.lua | 2 +- src/board.c | 5 +++++ src/hook.c | 4 +++- src/lluadef.h | 9 ++++++-- src/misn_lua.c | 40 +++++++++++++++++++++++++----------- src/pilot.c | 15 ++++++++++---- src/pilot.h | 2 ++ 7 files changed, 57 insertions(+), 20 deletions(-) diff --git a/dat/missions/emp_scout00.lua b/dat/missions/emp_scout00.lua index db9f469..42ce37b 100644 --- a/dat/missions/emp_scout00.lua +++ b/dat/missions/emp_scout00.lua @@ -63,7 +63,7 @@ function enter() elseif sys == misn_target then p = pilot.add("Collective Drone", "scout") for k,v in pairs(p) do - hook.pilotDeath(v, "kill") + hook.pilot(v, "death", "kill") end end diff --git a/src/board.c b/src/board.c index 7482aa8..5312439 100644 --- a/src/board.c +++ b/src/board.c @@ -6,6 +6,7 @@ #include "space.h" #include "rng.h" #include "economy.h" +#include "hook.h" #include "board.h" #define BOARDING_WIDTH 300 @@ -77,6 +78,10 @@ void player_board(void) { "Leave", board_exit); board_update(); + + /* Run hook if needed. */ + if(p->hook_type == PILOT_HOOK_BOARD) + hook_runID(p->hook); } static void board_exit(char* str) { diff --git a/src/hook.c b/src/hook.c index b9e19e9..09002f2 100644 --- a/src/hook.c +++ b/src/hook.c @@ -174,7 +174,9 @@ void hook_cleanup(void) { /* Save the hooks. */ static int hook_needSave(Hook* h) { int i; - char* nosave[] = { "death", "end" }; + char* nosave[] = { + "death", "board", "disable", /* Pilot hooks. */ + "end" }; for(i = 0; strcmp(nosave[i], "end") != 0; i++) if(strcmp(nosave[i], h->stack)==0) return 0; diff --git a/src/lluadef.h b/src/lluadef.h index 0f89e04..a3e8506 100644 --- a/src/lluadef.h +++ b/src/lluadef.h @@ -5,19 +5,24 @@ #include "lualib.h" /* Debug stuff. */ +#ifdef DEBUG #define LLUA_DEBUG(str, args...) \ (fprintf(stdout, "Lua: "str"\n", ## args)) #define LLUA_INVALID_PARAMETER() { \ - LLUA_DEBUG("[%s] Invalid parameter", __func__); \ + LLUA_DEBUG("[%s] Invalid parameter (%s:%d)", __func__, __FILE__, __LINE__); \ return 0; \ } #define LLUA_MIN_ARGS(n) \ if(lua_gettop(L) < n) { \ - LLUA_DEBUG("[%s] Too few arguments", __func__); \ + LLUA_DEBUG("[%s] Too few arguments (%s:%d)", __func__, __FILE__, __LINE__); \ return 0; \ } +#else /* DEBUG. */ +#define LLUA_DEBUG(str, args...) do {;} while(0) +#define LLUA_MIN_ARGS(n) do {;} while(0) +#endif /* DEBUG. */ /* Comfortability macros. */ #define luaL_dobuffer(L, b, n, s) \ diff --git a/src/misn_lua.c b/src/misn_lua.c index a48ded9..17b2abb 100644 --- a/src/misn_lua.c +++ b/src/misn_lua.c @@ -123,13 +123,13 @@ static int hook_land(lua_State* L); static int hook_takeoff(lua_State* L); static int hook_time(lua_State* L); static int hook_enter(lua_State* L); -static int hook_pilotDeath(lua_State* L); +static int hook_pilot(lua_State* L); static const luaL_reg hook_methods[] = { { "land", hook_land }, { "takeoff", hook_takeoff }, { "time", hook_time }, { "enter", hook_enter }, - { "pilotDeath", hook_pilotDeath }, + { "pilot", hook_pilot }, { 0, 0 } }; @@ -682,6 +682,10 @@ static unsigned int hook_generic(lua_State* L, char* stack) { char* func; LLUA_MIN_ARGS(1); + + /* Last parameter must be function to hook. */ + if(lua_isstring(L, -1)) func = (char*)lua_tostring(L, -1); + else LLUA_INVALID_PARAMETER(); /* Make sure mission is a player mission. */ for(i = 0; i < MISSION_MAX; i++) @@ -692,12 +696,6 @@ static unsigned int hook_generic(lua_State* L, char* stack) { return 0; } - if(lua_isstring(L, -1)) func = (char*)lua_tostring(L, -1); - else { - WARN("Mission '%s': trying to push non-valid function hook", - cur_mission->data->name); - return 0; - } return hook_add(cur_mission->id, func, stack); } @@ -721,15 +719,33 @@ static int hook_enter(lua_State* L) { return 0; } -static int hook_pilotDeath(lua_State* L) { +static int hook_pilot(lua_State* L) { LLUA_MIN_ARGS(2); unsigned int h, p; + int type; + char* hook_type; - if(lua_isnumber(L, -2)) p = (unsigned int) lua_tonumber(L, -2); + /* First parameter - pilot to hook. */ + if(lua_isnumber(L, 1)) p = (unsigned int)lua_tonumber(L, 1); else LLUA_INVALID_PARAMETER(); - h = hook_generic(L, "death"); /* We won't actually call the death stack directly. */ - pilot_addHook(pilot_get(p), PILOT_HOOK_DEATH, h); + /* Second parameer - Hook name. */ + if(lua_isstring(L, 2)) hook_type = (char*) lua_tostring(L, 2); + else LLUA_INVALID_PARAMETER(); + + /* Check to see if hook_type is valid. */ + if(strcmp(hook_type, "death")==0) type = PILOT_HOOK_DEATH; + else if(strcmp(hook_type, "board")==0) type = PILOT_HOOK_BOARD; + else if(strcmp(hook_type, "disable")==0) type = PILOT_HOOK_DISABLE; + else { /* Hook type not valid. */ + LLUA_DEBUG("Invalid pilot hook type: '%s'", hook_type); + return 0; + } + + + /* Actually add the hook. */ + h = hook_generic(L, hook_type); + pilot_addHook(pilot_get(p), type, h); return 0; } diff --git a/src/pilot.c b/src/pilot.c index 7fb0239..4b0f991 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -424,10 +424,17 @@ static void pilot_update(Pilot* pilot, const double dt) { else if(pilot->armour <= 0.) /* PWNED! */ pilot_dead(pilot); /* Start death stuff. */ - /* Pupose fallthrough to get the movement similar to disabled. */ - if(pilot != player && pilot->armour < PILOT_DISABLED_ARMOUR * pilot->armour_max) { - /* We are disabled. */ - pilot_setFlag(pilot, PILOT_DISABLED); + /* Purpose fallthrough to get the movement similar to disabled. */ + if((pilot != player) && + (pilot->armour < PILOT_DISABLED_ARMOUR*pilot->armour_max)) { /* Disabled. */ + /* First time pilot is disabled. */ + if(!pilot_isFlag(pilot, PILOT_DISABLED)) { + pilot_setFlag(pilot, PILOT_DISABLED); /* Set as disabled. */ + /* Run hook. */ + if(pilot->hook_type == PILOT_HOOK_DISABLE) + hook_runID(pilot->hook); + } + /* Come to a halt slowly. */ vect_pset(&pilot->solid->vel, VMOD(pilot->solid->vel) * (1. - dt*0.10), VANGLE(pilot->solid->vel)); diff --git a/src/pilot.h b/src/pilot.h index 63fc7bc..52cb153 100644 --- a/src/pilot.h +++ b/src/pilot.h @@ -23,6 +23,8 @@ /* Hooks. */ #define PILOT_HOOK_NONE 0 /* No hook. */ #define PILOT_HOOK_DEATH 1 /* Pilot died. */ +#define PILOT_HOOK_BOARD 2 /* Pilot got boarded. */ +#define PILOT_HOOK_DISABLE 3 /* Pilot got disabled. */ /* Flags. */ #define pilot_isFlag(p,f) (p->flags & (f))