naev 0.12.5
pilot_hook.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <stdlib.h>
12
13#include "pilot_hook.h"
14
15#include "array.h"
16#include "claim.h"
17#include "hook.h"
18#include "log.h"
19
21 NULL;
22static int pilot_hookCleanup = 0;
23
33int pilot_runHookParam( Pilot *p, int hook_type, const HookParam *param,
34 int nparam )
35{
36 int run;
37 HookParam hstaparam[5], *hdynparam, *hparam;
38
39 /* Set up hook parameters. */
40 if ( nparam <= 3 ) {
41 int n = 1;
42 hstaparam[0].type = HOOK_PARAM_PILOT;
43 hstaparam[0].u.lp = p->id;
44 if ( nparam != 0 )
45 memcpy( &hstaparam[n], param, sizeof( HookParam ) * nparam );
46 n += nparam;
47 hstaparam[n].type = HOOK_PARAM_SENTINEL;
48 hdynparam = NULL;
49 hparam = hstaparam;
50 } else {
51 hdynparam = malloc( sizeof( HookParam ) * ( nparam + 2 ) );
52 hdynparam[0].type = HOOK_PARAM_PILOT;
53 hdynparam[0].u.lp = p->id;
54 memcpy( &hdynparam[1], param, sizeof( HookParam ) * nparam );
55 hdynparam[nparam + 1].type = HOOK_PARAM_SENTINEL;
56 hparam = hdynparam;
57 }
58
59 /* Run pilot specific hooks. */
60 run = 0;
61 for ( int i = 0; i < array_size( p->hooks ); i++ ) {
62 int ret;
63 if ( p->hooks[i].type != hook_type )
64 continue;
65
66 ret = hook_runIDparam( p->hooks[i].id, hparam );
67 if ( ret )
68 WARN( _( "Pilot '%s' failed to run hook type %d" ), p->name,
69 hook_type );
70 else
71 run++;
72 }
73
74 /* Run global hooks. */
75 for ( int i = 0; i < array_size( pilot_globalHooks ); i++ ) {
76 int ret;
77 if ( pilot_globalHooks[i].type != hook_type )
78 continue;
79
80 ret = hook_runIDparam( pilot_globalHooks[i].id, hparam );
81 if ( ret )
82 WARN( _( "Pilot '%s' failed to run hook type %d" ), p->name,
83 hook_type );
84 else
85 run++;
86 }
87
88 /* Clean up. */
89 free( hdynparam );
90
91 if ( run > 0 )
92 claim_activateAll(); /* Reset claims. */
93
94 return run;
95}
96
104int pilot_runHook( Pilot *p, int hook_type )
105{
106 return pilot_runHookParam( p, hook_type, NULL, 0 );
107}
108
116void pilot_addHook( Pilot *pilot, int type, unsigned int hook )
117{
118 PilotHook *phook;
119
120 /* Don't crash if pilot is NULL. */
121 if ( pilot == NULL ) {
122 WARN( _( "Trying to set hook on NULL pilot!" ) );
123 return;
124 }
125
126 /* Allocate memory. */
127 if ( pilot->hooks == NULL )
128 pilot->hooks = array_create( PilotHook );
129
130 /* Create the new hook. */
131 phook = &array_grow( &pilot->hooks );
132 phook->type = type;
133 phook->id = hook;
134}
135
139void pilots_addGlobalHook( int type, unsigned int hook )
140{
141 PilotHook phook;
142
143 /* Allocate memory. */
144 if ( pilot_globalHooks == NULL )
146
147 /* Create the new hook. */
148 phook.type = type;
149 phook.id = hook;
151}
152
156void pilots_rmGlobalHook( unsigned int hook )
157{
158 for ( int i = 0; i < array_size( pilot_globalHooks ); i++ ) {
159 if ( pilot_globalHooks[i].id != hook )
160 continue;
162 &pilot_globalHooks[i + 1] );
163 return;
164 }
165}
166
175
181void pilots_rmHook( unsigned int hook )
182{
183 Pilot *const *plist;
184
185 /* Cleaning up a pilot's hooks. */
186 if ( pilot_hookCleanup )
187 return;
188
189 /* Remove global hook first. */
190 pilots_rmGlobalHook( hook );
191
192 plist = pilot_getAll();
193 for ( int i = 0; i < array_size( plist ); i++ ) {
194 Pilot *p = plist[i];
195
196 for ( int j = 0; j < array_size( p->hooks ); j++ ) {
197 /* Hook not found. */
198 if ( p->hooks[j].id != hook )
199 continue;
200
201 array_erase( &p->hooks, &p->hooks[j], &p->hooks[j + 1] );
202 j--; /* Dun like it but we have to keep iterator safe. */
203 }
204 }
205}
206
213{
214 /* Remove the hooks. */
216 for ( int i = 0; i < array_size( p->hooks ); i++ )
217 hook_rm( p->hooks[i].id );
219
220 array_free( p->hooks );
221 p->hooks = NULL;
222}
223
228{
229 /* Clear global hooks. */
230 if ( pilot_globalHooks != NULL ) {
233 pilot_globalHooks = NULL;
234 }
235}
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition array.h:170
#define array_end(array)
Returns a pointer to the end of the reserved memory space.
Definition array.h:214
#define array_erase(ptr_array, first, last)
Erases elements in interval [first, last).
Definition array.h:148
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:179
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
Definition array.h:122
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
Definition array.h:134
#define array_begin(array)
Returns a pointer to the beginning of the reserved memory space.
Definition array.h:206
#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:240
int hook_runIDparam(unsigned int id, const HookParam *param)
Runs a single hook by id.
Definition hook.c:1103
void hook_rm(unsigned int id)
Removes a hook.
Definition hook.c:846
Pilot *const * pilot_getAll(void)
Gets the pilot stack.
Definition pilot.c:93
void pilots_clearGlobalHooks(void)
Removes all the pilot global hooks.
Definition pilot_hook.c:170
static PilotHook * pilot_globalHooks
Definition pilot_hook.c:20
void pilots_addGlobalHook(int type, unsigned int hook)
Adds a pilot global hook.
Definition pilot_hook.c:139
void pilots_rmHook(unsigned int hook)
Removes a hook from all the pilots.
Definition pilot_hook.c:181
static int pilot_hookCleanup
Definition pilot_hook.c:22
void pilot_clearHooks(Pilot *p)
Clears the pilots hooks.
Definition pilot_hook.c:212
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:33
void pilot_freeGlobalHooks(void)
Clears global pilot hooks.
Definition pilot_hook.c:227
int pilot_runHook(Pilot *p, int hook_type)
Tries to run a pilot hook if he has it.
Definition pilot_hook.c:104
void pilot_addHook(Pilot *pilot, int type, unsigned int hook)
Adds a hook to the pilot.
Definition pilot_hook.c:116
void pilots_rmGlobalHook(unsigned int hook)
Removes a pilot global hook.
Definition pilot_hook.c:156
The actual hook parameter.
Definition hook.h:40
LuaPilot lp
Definition hook.h:46
HookParamType type
Definition hook.h:41
union HookParam::@325330313164266255110350307176363262300250041145 u
A wrapper for pilot hooks.
Definition pilot.h:232
unsigned int id
Definition pilot.h:234
int type
Definition pilot.h:233
The representation of an in-game pilot.
Definition pilot.h:263
PilotHook * hooks
Definition pilot.h:387