[Add] Two new hooks: Board and disable. Also made lua debug more verbose.
This commit is contained in:
parent
6099849f4f
commit
dcf093b13e
@ -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
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -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) \
|
||||
|
@ -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 }
|
||||
};
|
||||
|
||||
@ -683,6 +683,10 @@ static unsigned int hook_generic(lua_State* L, char* stack) {
|
||||
|
||||
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++)
|
||||
if(player_missions[i].id == cur_mission->id)
|
||||
@ -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;
|
||||
}
|
||||
|
15
src/pilot.c
15
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));
|
||||
|
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user