34#include "gatherable.h"
40#include "nlua_commodity.h"
41#include "nlua_pilot.h"
42#include "nlua_outfit.h"
52typedef struct HookQueue_s {
65typedef enum HookType_e {
107 int (*func)(
void* );
125static int hooks_executeParam(
const char* stack,
const HookParam *param );
247 while (param[n].type != HOOK_PARAM_SENTINEL) {
248 switch (param[n].type) {
250 lua_pushnil( naevL );
252 case HOOK_PARAM_NUMBER:
253 lua_pushnumber( naevL, param[n].u.num );
255 case HOOK_PARAM_STRING:
256 lua_pushstring( naevL, param[n].u.str );
258 case HOOK_PARAM_BOOL:
259 lua_pushboolean( naevL, param[n].u.b );
261 case HOOK_PARAM_PILOT:
264 case HOOK_PARAM_SHIP:
267 case HOOK_PARAM_OUTFIT:
270 case HOOK_PARAM_COMMODITY:
273 case HOOK_PARAM_FACTION:
276 case HOOK_PARAM_SPOB:
279 case HOOK_PARAM_JUMP:
283 lua_rawgeti( naevL, LUA_REGISTRYINDEX, param[n].u.ref );
287 WARN( _(
"Unknown Lua parameter type.") );
288 lua_pushnil( naevL );
316 WARN(_(
"Trying to run hook with nonexistent parent: deleting"));
324 WARN(_(
"Trying to run hook with parent not in player mission stack: deleting"));
338 misn_runStart( misn, hook->
u.
misn.
func );
346 if (misn_runFunc( misn, hook->
u.
misn.
func, n ) < 0) {
347 WARN(_(
"Hook [%s] '%d' -> '%s' failed"), hook->
stack,
380 WARN(_(
"Hook [%s] '%d' -> '%s' failed, event does not exist. Deleting hook."), hook->
stack,
396 WARN(_(
"Hook [%s] '%d' -> '%s' failed"), hook->
stack,
424 switch (hook->
type) {
442 WARN(_(
"Invalid hook type '%d', deleting."), hook->
type);
481 Hook *new_hook = calloc( 1,
sizeof(
Hook) );
491 new_hook->
type = type;
493 new_hook->
stack = strdup(stack);
497 if (strcmp(stack,
"safe")==0)
511unsigned int hook_addMisn(
unsigned int parent,
const char *func,
const char *stack )
531unsigned int hook_addEvent(
unsigned int parent,
const char *func,
const char *stack )
594unsigned int hook_addFunc(
int (*func)(
void*),
void *data,
const char *stack )
599 new_hook->
u.
func.func = func;
600 new_hook->
u.
func.data = data;
659 if ((
player.
p == NULL) || player_isFlag(PLAYER_CREATING))
668 for (
int j=1; j>=0; j--) {
701unsigned int hook_addDateMisn(
unsigned int parent,
const char *func, ntime_t resolution )
712 new_hook->
res = resolution;
718unsigned int hook_addDateEvt(
unsigned int parent,
const char *func, ntime_t resolution )
729 new_hook->
res = resolution;
743 if ((
player.
p == NULL) || player_isFlag(PLAYER_CREATING) || player_isFlag(PLAYER_DESTROYED))
751 for (
int j=1; j>=0; j--) {
872static int hooks_executeParam(
const char* stack,
const HookParam *param )
877 if ((
player.
p == NULL) || player_isFlag(PLAYER_DESTROYED))
882 if (strcmp(stack, h->stack)==0) {
889 for (
int j=1; j>=0; j--) {
901 if (strcmp(stack, h->stack) != 0)
920 while (param[n].type != HOOK_PARAM_SENTINEL) {
921 switch (param[n].type) {
923 luaL_unref( naevL, LUA_REGISTRYINDEX, param[n].u.ref );
953 if ((
player.
p == NULL) || player_isFlag(PLAYER_DESTROYED))
957 hq->
stack = strdup(stack);
960 for (; param[i].type != HOOK_PARAM_SENTINEL; i++)
964 if (i >= HOOK_MAX_PARAM)
965 WARN( _(
"HOOK_MAX_PARAM is set too low (%d), need at least %d!"), HOOK_MAX_PARAM, i );
982 if ((
player.
p == NULL) || player_isFlag(PLAYER_DESTROYED))
990 return hooks_executeParam( stack, param );
1058 if ((
player.
p == NULL) || player_isFlag(PLAYER_DESTROYED))
1064 WARN(_(
"Attempting to run hook of id '%d' which is not in the stack"),
id);
1153 const char *nosave[] = {
1154 "p_death",
"p_board",
"p_disable",
"p_jump",
"p_attacked",
"p_idle",
1171 for (
int i=0; strcmp(nosave[i],
"end") != 0; i++)
1172 if (strcmp(nosave[i],h->
stack)==0)
return 0;
1185 xmlw_startElem(writer,
"hooks");
1191 xmlw_startElem(writer,
"hook");
1195 xmlw_attr(writer,
"type",
"misn");
1196 xmlw_elem(writer,
"parent",
"%u",h->u.misn.parent);
1197 xmlw_elem(writer,
"func",
"%s",h->u.misn.func);
1201 xmlw_attr(writer,
"type",
"event");
1202 xmlw_elem(writer,
"parent",
"%u",h->u.event.parent);
1203 xmlw_elem(writer,
"func",
"%s",h->u.event.func);
1207 WARN(_(
"Something has gone screwy here..."));
1212 xmlw_elem(writer,
"id",
"%u",h->id);
1213 xmlw_elem(writer,
"stack",
"%s",h->stack);
1217 xmlw_elem(writer,
"resolution",
"%"PRId64,h->res);
1219 xmlw_endElem(writer);
1221 xmlw_endElem(writer);
1239 node = parent->xmlChildrenNode;
1241 if (xml_isNode(node,
"hooks"))
1243 }
while (xml_nextNode(node));
1263 xmlNodePtr node, cur;
1264 char *func, *stack, *stype;
1265 unsigned int parent, id, new_id;
1271 node = base->xmlChildrenNode;
1273 if (xml_isNode(node,
"hook")) {
1281 xmlr_attr_strd(node,
"type", stype);
1286 else if (strcmp(stype,
"misn") == 0) {
1291 else if (strcmp(stype,
"event") == 0) {
1297 WARN(_(
"Hook of unknown type '%s' found, skipping."), stype);
1303 cur = node->xmlChildrenNode;
1308 xmlr_long(cur,
"id",
id);
1311 xmlr_str(cur,
"stack", stack);
1315 xmlr_uint(cur,
"parent", parent);
1316 xmlr_str(cur,
"func", func);
1320 if (xml_isNode( cur,
"resolution" )) {
1322 res = xml_getLong( cur );
1326 WARN(_(
"Save has unknown hook node '%s'."), cur->name);
1327 }
while (xml_nextNode(cur));
1330 if ((parent == 0) || (func == NULL) || (stack == NULL)) {
1331 WARN(_(
"Invalid hook."));
1344 WARN(_(
"Save has unsupported hook type."));
1360 }
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)
void hook_clear(void)
Clears the hooks.
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.
Commodity ** lua_pushcommodity(lua_State *L, Commodity *commodity)
Pushes a commodity on the stack.
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.
const Outfit ** lua_pushoutfit(lua_State *L, const Outfit *outfit)
Pushes a outfit on the stack.
LuaPilot * lua_pushpilot(lua_State *L, LuaPilot pilot)
Pushes a pilot on the stack.
const Ship ** lua_pushship(lua_State *L, const Ship *ship)
Pushes a ship 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::@32::@34 event
struct Hook::@32::@33 misn
Represents an active mission.