naev 0.11.5
pilot_hook.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <limits.h>
11#include <math.h>
12#include <stdlib.h>
13
14#include "naev.h"
17#include "pilot_hook.h"
18
19#include "array.h"
20#include "hook.h"
21#include "log.h"
22#include "nstring.h"
23#include "nxml.h"
24
26static int pilot_hookCleanup = 0;
37int pilot_runHookParam( Pilot* p, int hook_type, const HookParam* param, int nparam )
38{
39 int n, run;
40 HookParam hstaparam[5], *hdynparam, *hparam;
41
42 /* Set up hook parameters. */
43 if (nparam <= 3) {
44 hstaparam[0].type = HOOK_PARAM_PILOT;
45 hstaparam[0].u.lp = p->id;
46 n = 1;
47 if (nparam != 0)
48 memcpy( &hstaparam[n], param, sizeof(HookParam)*nparam );
49 n += nparam;
50 hstaparam[n].type = HOOK_PARAM_SENTINEL;
51 hdynparam = NULL;
52 hparam = hstaparam;
53 }
54 else {
55 hdynparam = malloc( sizeof(HookParam) * (nparam+2) );
56 hdynparam[0].type = HOOK_PARAM_PILOT;
57 hdynparam[0].u.lp = p->id;
58 memcpy( &hdynparam[1], param, sizeof(HookParam)*nparam );
59 hdynparam[nparam+1].type = HOOK_PARAM_SENTINEL;
60 hparam = hdynparam;
61 }
62
63 /* Run pilot specific hooks. */
64 run = 0;
65 for (int i=0; i<array_size(p->hooks); i++) {
66 int ret;
67 if (p->hooks[i].type != hook_type)
68 continue;
69
70 ret = hook_runIDparam( p->hooks[i].id, hparam );
71 if (ret)
72 WARN(_("Pilot '%s' failed to run hook type %d"), p->name, hook_type);
73 else
74 run++;
75 }
76
77 /* Run global hooks. */
78 for (int i=0; i<array_size(pilot_globalHooks); i++) {
79 int ret;
80 if (pilot_globalHooks[i].type != hook_type)
81 continue;
82
83 ret = hook_runIDparam( pilot_globalHooks[i].id, hparam );
84 if (ret)
85 WARN(_("Pilot '%s' failed to run hook type %d"), p->name, hook_type);
86 else
87 run++;
88 }
89
90 /* Clean up. */
91 free( hdynparam );
92
93 if (run > 0)
94 claim_activateAll(); /* Reset claims. */
95
96 return run;
97}
98
106int pilot_runHook( Pilot* p, int hook_type )
107{
108 return pilot_runHookParam( p, hook_type, NULL, 0 );
109}
110
118void pilot_addHook( Pilot *pilot, int type, unsigned int hook )
119{
120 PilotHook *phook;
121
122 /* Allocate memory. */
123 if (pilot->hooks == NULL)
124 pilot->hooks = array_create( PilotHook );
125
126 /* Create the new hook. */
127 phook = &array_grow( &pilot->hooks );
128 phook->type = type;
129 phook->id = hook;
130}
131
135void pilots_addGlobalHook( int type, unsigned int hook )
136{
137 PilotHook phook;
138
139 /* Allocate memory. */
140 if (pilot_globalHooks == NULL)
142
143 /* Create the new hook. */
144 phook.type = type;
145 phook.id = hook;
147}
148
152void pilots_rmGlobalHook( unsigned int hook )
153{
154 for (int i=0; i<array_size(pilot_globalHooks); i++) {
155 if (pilot_globalHooks[i].id != hook)
156 continue;
158 return;
159 }
160}
161
169
175void pilots_rmHook( unsigned int hook )
176{
177 Pilot *const*plist;
178
179 /* Cleaning up a pilot's hooks. */
181 return;
182
183 /* Remove global hook first. */
184 pilots_rmGlobalHook( hook );
185
186 plist = pilot_getAll();
187 for (int i=0; i<array_size(plist); i++) {
188 Pilot *p = plist[i];
189
190 for (int j=0; j<array_size(p->hooks); j++) {
191 /* Hook not found. */
192 if (p->hooks[j].id != hook)
193 continue;
194
195 array_erase( &p->hooks, &p->hooks[j], &p->hooks[j+1] );
196 j--; /* Dun like it but we have to keep iterator safe. */
197 }
198 }
199}
200
207{
208 /* Remove the hooks. */
210 for (int i=0; i<array_size(p->hooks); i++)
211 hook_rm( p->hooks[i].id );
213
214 array_free(p->hooks);
215 p->hooks = NULL;
216}
217
222{
223 /* Clear global hooks. */
224 if (pilot_globalHooks != NULL) {
227 pilot_globalHooks = NULL;
228 }
229}
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition array.h:158
#define array_end(array)
Returns a pointer to the end of the reserved memory space.
Definition array.h:202
#define array_erase(ptr_array, first, last)
Erases elements in interval [first, last).
Definition array.h:140
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:168
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
Definition array.h:119
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
Definition array.h:129
#define array_begin(array)
Returns a pointer to the beginning of the reserved memory space.
Definition array.h:194
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
Definition array.h:93
void claim_activateAll(void)
Activates all the claims.
Definition claim.c:239
int hook_runIDparam(unsigned int id, const HookParam *param)
Runs a single hook by id.
Definition hook.c:1053
void hook_rm(unsigned int id)
Removes a hook.
Definition hook.c:798
Header file with generic functions and naev-specifics.
Pilot *const * pilot_getAll(void)
Gets the pilot stack.
Definition pilot.c:94
void pilots_clearGlobalHooks(void)
Removes all the pilot global hooks.
Definition pilot_hook.c:165
static PilotHook * pilot_globalHooks
Definition pilot_hook.c:25
void pilots_addGlobalHook(int type, unsigned int hook)
Adds a pilot global hook.
Definition pilot_hook.c:135
void pilots_rmHook(unsigned int hook)
Removes a hook from all the pilots.
Definition pilot_hook.c:175
static int pilot_hookCleanup
Definition pilot_hook.c:26
void pilot_clearHooks(Pilot *p)
Clears the pilots hooks.
Definition pilot_hook.c:206
int pilot_runHookParam(Pilot *p, int hook_type, const HookParam *param, int nparam)
Tries to run a pilot hook if he has it.
Definition pilot_hook.c:37
void pilot_freeGlobalHooks(void)
Clears global pilot hooks.
Definition pilot_hook.c:221
int pilot_runHook(Pilot *p, int hook_type)
Tries to run a pilot hook if he has it.
Definition pilot_hook.c:106
void pilot_addHook(Pilot *pilot, int type, unsigned int hook)
Adds a hook to the pilot.
Definition pilot_hook.c:118
void pilots_rmGlobalHook(unsigned int hook)
Removes a pilot global hook.
Definition pilot_hook.c:152
The actual hook parameter.
Definition hook.h:38
LuaPilot lp
Definition hook.h:44
HookParamType type
Definition hook.h:39
union HookParam::@25 u
A wrapper for pilot hooks.
Definition pilot.h:188
unsigned int id
Definition pilot.h:190
int type
Definition pilot.h:189
The representation of an in-game pilot.
Definition pilot.h:217
PilotHook * hooks
Definition pilot.h:330