34#include "gatherable.h"
40#include "nlua_pilot.h"
49typedef struct HookQueue_s {
62typedef enum HookType_e {
104 int (*func)(
void* );
122static int hooks_executeParam(
const char* stack,
const HookParam *param );
244 while (param[n].type != HOOK_PARAM_SENTINEL) {
245 switch (param[n].type) {
247 lua_pushnil( naevL );
249 case HOOK_PARAM_NUMBER:
250 lua_pushnumber( naevL, param[n].u.num );
252 case HOOK_PARAM_STRING:
253 lua_pushstring( naevL, param[n].u.str );
255 case HOOK_PARAM_BOOL:
256 lua_pushboolean( naevL, param[n].u.b );
258 case HOOK_PARAM_PILOT:
261 case HOOK_PARAM_FACTION:
264 case HOOK_PARAM_SPOB:
267 case HOOK_PARAM_JUMP:
271 lua_rawgeti( naevL, LUA_REGISTRYINDEX, param[n].u.ref );
275 WARN( _(
"Unknown Lua parameter type.") );
276 lua_pushnil( naevL );
304 WARN(_(
"Trying to run hook with nonexistent parent: deleting"));
312 WARN(_(
"Trying to run hook with parent not in player mission stack: deleting"));
335 WARN(_(
"Hook [%s] '%d' -> '%s' failed"), hook->
stack,
368 WARN(_(
"Hook [%s] '%d' -> '%s' failed, event does not exist. Deleting hook."), hook->
stack,
384 WARN(_(
"Hook [%s] '%d' -> '%s' failed"), hook->
stack,
412 switch (hook->
type) {
430 WARN(_(
"Invalid hook type '%d', deleting."), hook->
type);
469 Hook *new_hook = calloc( 1,
sizeof(
Hook) );
479 new_hook->
type = type;
481 new_hook->
stack = strdup(stack);
485 if (strcmp(stack,
"safe")==0)
499unsigned int hook_addMisn(
unsigned int parent,
const char *func,
const char *stack )
519unsigned int hook_addEvent(
unsigned int parent,
const char *func,
const char *stack )
582unsigned int hook_addFunc(
int (*func)(
void*),
void *data,
const char *stack )
587 new_hook->
u.
func.func = func;
588 new_hook->
u.
func.data = data;
647 if ((
player.
p == NULL) || player_isFlag(PLAYER_CREATING))
656 for (
int j=1; j>=0; j--) {
689unsigned int hook_addDateMisn(
unsigned int parent,
const char *func, ntime_t resolution )
700 new_hook->
res = resolution;
706unsigned int hook_addDateEvt(
unsigned int parent,
const char *func, ntime_t resolution )
717 new_hook->
res = resolution;
731 if ((
player.
p == NULL) || player_isFlag(PLAYER_CREATING) || player_isFlag(PLAYER_DESTROYED))
739 for (
int j=1; j>=0; j--) {
860static int hooks_executeParam(
const char* stack,
const HookParam *param )
865 if ((
player.
p == NULL) || player_isFlag(PLAYER_DESTROYED))
870 if (strcmp(stack, h->stack)==0) {
877 for (
int j=1; j>=0; j--) {
889 if (strcmp(stack, h->stack) != 0)
908 while (param[n].type != HOOK_PARAM_SENTINEL) {
909 switch (param[n].type) {
911 luaL_unref( naevL, LUA_REGISTRYINDEX, param[n].u.ref );
941 if ((
player.
p == NULL) || player_isFlag(PLAYER_DESTROYED))
945 hq->
stack = strdup(stack);
948 for (; param[i].type != HOOK_PARAM_SENTINEL; i++)
952 if (i >= HOOK_MAX_PARAM)
953 WARN( _(
"HOOK_MAX_PARAM is set too low (%d), need at least %d!"), HOOK_MAX_PARAM, i );
970 if ((
player.
p == NULL) || player_isFlag(PLAYER_DESTROYED))
978 return hooks_executeParam( stack, param );
1046 if ((
player.
p == NULL) || player_isFlag(PLAYER_DESTROYED))
1052 WARN(_(
"Attempting to run hook of id '%d' which is not in the stack"),
id);
1129 const char *nosave[] = {
1130 "p_death",
"p_board",
"p_disable",
"p_jump",
"p_attacked",
"p_idle",
1147 for (
int i=0; strcmp(nosave[i],
"end") != 0; i++)
1148 if (strcmp(nosave[i],h->
stack)==0)
return 0;
1161 xmlw_startElem(writer,
"hooks");
1167 xmlw_startElem(writer,
"hook");
1171 xmlw_attr(writer,
"type",
"misn");
1172 xmlw_elem(writer,
"parent",
"%u",h->u.misn.parent);
1173 xmlw_elem(writer,
"func",
"%s",h->u.misn.func);
1177 xmlw_attr(writer,
"type",
"event");
1178 xmlw_elem(writer,
"parent",
"%u",h->u.event.parent);
1179 xmlw_elem(writer,
"func",
"%s",h->u.event.func);
1183 WARN(_(
"Something has gone screwy here..."));
1188 xmlw_elem(writer,
"id",
"%u",h->id);
1189 xmlw_elem(writer,
"stack",
"%s",h->stack);
1193 xmlw_elem(writer,
"resolution",
"%"PRId64,h->res);
1195 xmlw_endElem(writer);
1197 xmlw_endElem(writer);
1215 node = parent->xmlChildrenNode;
1217 if (xml_isNode(node,
"hooks"))
1219 }
while (xml_nextNode(node));
1239 xmlNodePtr node, cur;
1240 char *func, *stack, *stype;
1241 unsigned int parent, id, new_id;
1249 node = base->xmlChildrenNode;
1251 if (xml_isNode(node,
"hook")) {
1259 xmlr_attr_strd(node,
"type", stype);
1264 else if (strcmp(stype,
"misn") == 0) {
1269 else if (strcmp(stype,
"event") == 0) {
1275 WARN(_(
"Hook of unknown type '%s' found, skipping."), stype);
1281 cur = node->xmlChildrenNode;
1286 xmlr_long(cur,
"id",
id);
1289 xmlr_str(cur,
"stack", stack);
1293 xmlr_uint(cur,
"parent", parent);
1294 xmlr_str(cur,
"func", func);
1298 if (xml_isNode( cur,
"resolution" )) {
1300 res = xml_getLong( cur );
1304 WARN(_(
"Save has unknown hook node '%s'."), cur->name);
1305 }
while (xml_nextNode(cur));
1308 if ((parent == 0) || (func == NULL) || (stack == NULL)) {
1309 WARN(_(
"Invalid hook."));
1322 WARN(_(
"Save has unsupported hook type."));
1338 }
while (xml_nextNode(node));
Provides macros to work with dynamic arrays.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
int claim_testSys(const Claim_t *claim, int sys)
Tests to see if a system is claimed by a system claim.
void claim_activateAll(void)
Activates all the claims.
Event_t * event_get(unsigned int eventid)
Gets an event.
int event_save(unsigned int eventid)
Checks to see if an event should be saved.
int event_testClaims(unsigned int eventid, int sys)
Tests to see if an event has claimed a system.
static unsigned int hook_id
static int hook_runningstack
static ntime_t hook_time_accum
unsigned int hook_addTimerEvt(unsigned int parent, const char *func, double ms)
Adds a new event type hook timer.
int hook_runIDparam(unsigned int id, const HookParam *param)
Runs a single hook by id.
static int hook_parseParam(const HookParam *param)
Parses hook parameters.
static Hook * hook_new(HookType_t type, const char *stack)
Generates and allocates a new hook.
int hooks_runParam(const char *stack, const HookParam *param)
Runs all the hooks of stack.
static int hook_runMisn(Hook *hook, const HookParam *param, int claims)
Runs a mission hook.
void hook_rmMisnParent(unsigned int parent)
Removes all hooks belonging to parent mission.
static void hq_free(HookQueue_t *hq)
Frees a queued hook.
static int hook_loadingstack
static void hq_clear(void)
Clears the queued hooks.
int hook_load(xmlNodePtr parent)
Loads hooks for a player.
static Hook * hook_get(unsigned int id)
Gets a hook by ID.
void hooks_update(double dt)
Updates all the hook timer related stuff.
int hook_runID(unsigned int id)
Runs a single hook by id.
int hook_hasEventParent(unsigned int parent)
Checks to see how many hooks there are with the same event parent.
static void hooks_updateDateExecute(ntime_t change)
Updates date hooks and runs them if necessary.
static void hooks_purgeList(void)
Purges the list of deletable hooks.
static int hq_add(HookQueue_t *hq)
static HookQueue_t * hook_queue
void hook_cleanup(void)
Gets rid of all current hooks.
int hooks_runParamDeferred(const char *stack, const HookParam *param)
Runs all the hooks of stack in the next frame. Does not trigger right away.
static int hook_runEvent(Hook *hook, const HookParam *param, int claims)
Runs a Event function hook.
nlua_env hook_env(unsigned int hook)
Gets the lua env for a hook.
void hooks_updateDate(ntime_t change)
Updates the time to see if it should be updated.
static int hook_run(Hook *hook, const HookParam *param, int claims)
Runs a hook.
static Mission * hook_getMission(Hook *hook)
Gets the mission of a hook.
unsigned int hook_addEvent(unsigned int parent, const char *func, const char *stack)
Adds a new event type hook.
static void hook_free(Hook *h)
Frees a hook.
void hook_rm(unsigned int id)
Removes a hook.
static int hook_needSave(Hook *h)
Checks if a hook needs to be saved.
void hook_exclusionEnd(double dt)
Ends exclusion zone and runs all the queued hooks.
int hooks_run(const char *stack)
Runs all the hooks of stack.
void hook_rmEventParent(unsigned int parent)
Removes all hooks belonging to parent event.
unsigned int hook_addFunc(int(*func)(void *), void *data, const char *stack)
Adds a function hook to be run.
static unsigned int hook_genID(void)
Generates a new hook id.
int hook_hasMisnParent(unsigned int parent)
Checks to see how many hooks there are with the same mission parent.
void hook_exclusionStart(void)
Starts the hook exclusion zone, this makes hooks queue until exclusion is done.
unsigned int hook_addTimerMisn(unsigned int parent, const char *func, double ms)
Adds a new mission type hook timer hook.
static int hook_parse(xmlNodePtr base)
Parses an individual hook.
unsigned int hook_addMisn(unsigned int parent, const char *func, const char *stack)
Adds a new mission type hook.
static void hook_rmRaw(Hook *h)
Removes a hook.
int hook_save(xmlTextWriterPtr writer)
Saves all the hooks.
Mission ** player_missions
Header file with generic functions and naev-specifics.
void event_runStart(unsigned int eventid, const char *func)
Starts running a function, allows programmer to set up arguments.
int event_runFunc(unsigned int eventid, const char *func, int nargs)
Runs a function previously set up with event_runStart.
LuaFaction * lua_pushfaction(lua_State *L, LuaFaction faction)
Pushes a faction on the stack.
void hookL_unsetarg(unsigned int hook)
Unsets a Lua argument.
int hookL_getarg(unsigned int hook)
Gets a Lua argument for a hook.
LuaJump * lua_pushjump(lua_State *L, LuaJump jump)
Pushes a jump on the stack.
void misn_runStart(Mission *misn, const char *func)
Sets up the mission to run misn_runFunc.
int misn_runFunc(Mission *misn, const char *func, int nargs)
Runs a mission set up with misn_runStart.
LuaPilot * lua_pushpilot(lua_State *L, LuaPilot pilot)
Pushes a pilot on the stack.
LuaSpob * lua_pushspob(lua_State *L, LuaSpob spob)
Pushes a spob on the stack.
void pilots_rmHook(unsigned int hook)
Removes a hook from all the pilots.
void player_runHooks(void)
Runs hooks for the player.
Activated event structure.
The actual hook parameter.
Hook queue to delay execution.
struct HookQueue_s * next
HookParam hparam[HOOK_MAX_PARAM]
Internal representation of a hook.
struct Hook::@9::@10 misn
struct Hook::@9::@11 event
Represents an active mission.