summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBardur Arantsson <bardur@scientician.net>2012-06-07 19:42:13 +0200
committerBardur Arantsson <bardur@scientician.net>2012-06-08 06:18:08 +0200
commit6c7a878b3a522f554ec7b11cb1cad929676d97ff (patch)
tree5fc0664acdf65998c703e3388e1be55c292922c5
parent3756182bf99335d02f7872c105b5ae5f9b5c4e57 (diff)
Lua: Add new-style hooks.
-rw-r--r--src/defines.h1
-rw-r--r--src/externs.h2
-rw-r--r--src/plots.c49
-rw-r--r--src/types.h2
4 files changed, 54 insertions, 0 deletions
diff --git a/src/defines.h b/src/defines.h
index 9c0ba654..306ace92 100644
--- a/src/defines.h
+++ b/src/defines.h
@@ -4524,6 +4524,7 @@
#define HOOK_TYPE_C 0
#define HOOK_TYPE_LUA 1
+#define HOOK_TYPE_NEW 2
/*
* Defines for loadsave.c
diff --git a/src/externs.h b/src/externs.h
index 95a91f05..3c345051 100644
--- a/src/externs.h
+++ b/src/externs.h
@@ -603,6 +603,7 @@ extern void wipe_hooks(void);
extern void dump_hooks(int h_idx);
extern void init_hooks(void);
extern hooks_chain* add_hook(int h_idx, hook_type hook, cptr name);
+extern void add_hook_new(int h_idx, bool_ (*hook_f)(void *, void *, void *), cptr name, void *data);
extern void add_hook_script(int h_idx, char *script, cptr name);
extern void del_hook(int h_idx, hook_type hook);
extern void del_hook_name(int h_idx, cptr name);
@@ -611,6 +612,7 @@ extern int process_hooks_restart;
extern hook_return process_hooks_return[20];
extern bool_ process_hooks_ret(int h_idx, char *ret, char *fmt, ...);
extern bool_ process_hooks(int h_idx, char *fmt, ...);
+extern bool_ process_hooks_new(int h_idx, void *in, void *out);
extern void initialize_bookable_spells();
diff --git a/src/plots.c b/src/plots.c
index 6015420a..2fb592b0 100644
--- a/src/plots.c
+++ b/src/plots.c
@@ -109,6 +109,14 @@ hooks_chain* add_hook(int h_idx, hook_type hook, cptr name)
else return (c);
}
+void add_hook_new(int h_idx, bool_ (*hook_f)(void *, void *, void *), cptr name, void *data)
+{
+ hooks_chain *c = add_hook(h_idx, NULL, name);
+ c->hook_f = hook_f;
+ c->hook_data = data;
+ c->type = HOOK_TYPE_NEW;
+}
+
void add_hook_script(int h_idx, char *script, cptr name)
{
hooks_chain *c = add_hook(h_idx, NULL, name);
@@ -387,9 +395,15 @@ static bool_ vprocess_hooks_return (int h_idx, char *ret, char *fmt, va_list *ap
c = c->next;
lua_settop(L, oldtop);
}
+ else if (c->type == HOOK_TYPE_NEW)
+ {
+ /* Skip; handled in process_hooks_new */
+ c = c->next;
+ }
else
{
msg_format("Unkown hook type %d, name %s", c->type, c->name);
+ c = c->next;
}
}
@@ -418,6 +432,41 @@ bool_ process_hooks(int h_idx, char *fmt, ...)
return (ret);
}
+bool_ process_hooks_new(int h_idx, void *in, void *out)
+{
+ hooks_chain *c = hooks_heads[h_idx];
+
+ while (c != NULL)
+ {
+ /* Only new-style hooks; skip the rest. */
+ if (c->type != HOOK_TYPE_NEW)
+ {
+ c = c->next;
+ continue;
+ }
+
+ /* Invoke hook function; stop processing if
+ the hook returns TRUE */
+ if (c->hook_f(c->hook_data, in, out))
+ {
+ return TRUE;
+ }
+
+ /* Should we restart processing at the beginning? */
+ if (process_hooks_restart)
+ {
+ c = hooks_heads[h_idx];
+ process_hooks_restart = FALSE;
+ }
+ else
+ {
+ c = c->next;
+ }
+ }
+
+ return FALSE;
+}
+
/******** Plots & Quest stuff ********/
static void quest_describe(int q_idx)
diff --git a/src/types.h b/src/types.h
index 5173a89c..da1debf9 100644
--- a/src/types.h
+++ b/src/types.h
@@ -2363,6 +2363,8 @@ typedef struct hooks_chain hooks_chain;
struct hooks_chain
{
hook_type hook;
+ bool_ (*hook_f)(void *, void *, void *);
+ void *hook_data;
char name[40];
char script[40];
byte type;