summaryrefslogtreecommitdiff
path: root/src/plots.c
blob: f48d56588662a5f55660545e9b4df15db158aa89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* File: plots.c */

/* Purpose: plots & quests */

/*
 * Copyright (c) 2001 James E. Wilson, Robert A. Koeneke, DarkGod
 *
 * This software may be copied and distributed for educational, research, and
 * not for profit purposes provided that this copyright and statement are
 * included in all such copies.
 */

#include "angband.h"

#include "lua/lua.h"
#include "tolua.h"
extern lua_State* L;

/* #define DEBUG_HOOK */

/******** Hooks stuff *********/
FILE *hook_file;

#define MAX_ARGS        50

static hooks_chain *hooks_heads[MAX_HOOKS];

/* Wipe hooks and init them with quest hooks */
void wipe_hooks()
{
	int i;

	for (i = 0; i < MAX_HOOKS; i++)
	{
		hooks_heads[i] = NULL;
	}
}
void init_hooks()
{
	int i;

	for (i = 0; i < MAX_Q_IDX_INIT; i++)
	{
		if ((quest[i].type == HOOK_TYPE_C) && (quest[i].init != NULL)) quest[i].init(i);
	}
}

void dump_hooks(int h_idx)
{
	int min = 0, max = MAX_HOOKS, i;

	if (h_idx != -1)
	{
		min = h_idx;
		max = h_idx + 1;
	}

	for (i = min; i < max; i++)
	{
		hooks_chain *c = hooks_heads[i];

		/* Find it */
		while (c != NULL)
		{
			msg_format("%s(%s)", c->name, (c->type == HOOK_TYPE_C) ? "C" : "Lua");

			c = c->next;
		}
	}
}

/* Check a hook */
bool_ check_hook(int h_idx)
{
	hooks_chain *c = hooks_heads[h_idx];

	return (c != NULL);
}

/* Add a hook */
hooks_chain* add_hook(int h_idx, hook_type hook, cptr name)
{
	hooks_chain *new, *c = hooks_heads[h_idx];

	/* Find it */
	while ((c != NULL) && (strcmp(c->name, name)))
	{
		c = c->next;
	}

	/* If not already in the list, add it */
	if (c == NULL)
	{
		MAKE(new, hooks_chain);
		new->hook = hook;
		sprintf(new->name, "%s", name);
#ifdef DEBUG_HOOK
		if (wizard) cmsg_format(TERM_VIOLET, "HOOK ADD: %s", name);
		if (take_notes) add_note(format("HOOK ADD: %s", name), 'D');
#endif
		new->next = hooks_heads[h_idx];
		hooks_heads[h_idx] = new;
		return (new);
	}
	else return (c);
}

void add_hook_script(int h_idx, char *script, cptr name)
{
	hooks_chain *c = add_hook(h_idx, NULL, name);
#ifdef DEBUG_HOOK
	if (wizard) cmsg_format(TERM_VIOLET, "HOOK LUA ADD: %s : %s", name, script);
#endif
	sprintf(c->script, "%s", script);
	c->type = HOOK_TYPE_LUA;
}

/* Remove a hook */
void del_hook(int h_idx, hook_type hook)
{
	hooks_chain *c = hooks_heads[h_idx], *p = NULL;

	/* Find it */
	while ((c != NULL) && (c->hook != hook))
	{
		p = c;
		c = c->next;
	}

	/* Remove it */
	if (c != NULL)
	{
		if (p == NULL)
		{
#ifdef DEBUG_HOOK
			if (wizard) cmsg_format(TERM_VIOLET, "HOOK DEL: %s", c->name);
			if (take_notes) add_note(format("HOOK DEL: %s", c->name), 'D');
#endif
			hooks_heads[h_idx] = c->next;
			FREE(c, hooks_chain);
		}
		else
		{
#ifdef DEBUG_HOOK
			if (wizard) cmsg_format(TERM_VIOLET, "HOOK DEL: %s", c->name);
			if (take_notes) add_note(format("HOOK DEL: %s", c->name), 'D');
#endif
			p->next = c->next;
			FREE(c, hooks_chain);
		}
	}
}

void del_hook_name(int h_idx, cptr name)
{
	hooks_chain *c = hooks_heads[h_idx], *p = NULL;

	/* Find it */
	while ((c != NULL) && (strcmp(c->name, name)))
	{
		p = c;
		c = c->next;
	}

	/* Remove it */
	if (c != NULL)
	{
		if (p == NULL)
		{
#ifdef DEBUG_HOOK
			if (wizard) cmsg_format(TERM_VIOLET, "HOOK DEL: %s", c->name);
			if (take_notes) add_note(format("HOOK DEL: %s", c->name), 'D');
#endif
			hooks_heads[h_idx] = c->next;
			FREE(c, hooks_chain);
		}
		else
		{
#ifdef DEBUG_HOOK
			if (wizard) cmsg_format(TERM_VIOLET, "HOOK DEL: %s", c->name);
			if (take_notes) add_note(format("HOOK DEL: %s", c->name), 'D');
#endif
			p->next = c->next;
			FREE(c, hooks_chain);
		}
	}
}

/* get the next argument */
static hook_return param_pile[MAX_ARGS];
static int get_next_arg_pos = 0;
static int get_next_arg_pile_pos = 0;
s32b get_next_arg(char *fmt)
{
	while (TRUE)
	{
		switch (fmt[get_next_arg_pos++])
		{
		case 'd':
		case 'l':
			return (param_pile[get_next_arg_pile_pos++].num);
		case ')':
			get_next_arg_pos--;
			return 0;
		case '(':
		case ',':
			break;
		}
	}
}
char* get_next_arg_str(char *fmt)
{
	while (TRUE)
	{
		switch (fmt[get_next_arg_pos++])
		{
		case 's':
			return (char*)(param_pile[get_next_arg_pile_pos++].str);
		case ')':
			get_next_arg_pos--;
			return 0;
		case '(':
		case ',':
			break;
		}
	}
}

/* Actually process the hooks */
int process_hooks_restart = FALSE;
hook_return process_hooks_return[20];
static bool_ vprocess_hooks_return (int h_idx, char *ret, char *fmt, va_list *ap)
{
	hooks_chain *c = hooks_heads[h_idx];
	va_list real_ap;

	while (c != NULL)
	{
#ifdef DEBUG_HOOK
		if (wizard) cmsg_format(TERM_VIOLET, "HOOK: %s", c->name);
		if (take_notes) add_note(format("HOOK PROCESS: %s", c->name), 'D');
#endif
		if (c->type == HOOK_TYPE_C)
		{
			int i = 0, nb = 0;

			/* Push all args in the pile */
			i = 0;
			COPY(&real_ap, ap, va_list);
			while (fmt[i])
			{
				switch (fmt[i])
				{
				case 'O':
					param_pile[nb++].o_ptr = va_arg(real_ap, object_type *);
					break;
				case 's':
					param_pile[nb++].str = va_arg(real_ap, char *);
					break;
				case 'd':
				case 'l':
					param_pile[nb++].num = va_arg(real_ap, s32b);
					break;
				case '(':
				case ')':
				case ',':
					break;
				}
				i++;
			}

			get_next_arg_pos = 0;
			get_next_arg_pile_pos = 0;
			if (c->hook(fmt))
			{
				return TRUE;
			}

			/* Should we restart ? */
			if (process_hooks_restart)
			{
				c = hooks_heads[h_idx];
				process_hooks_restart = FALSE;
			}
			else
			{
				c = c->next;
			}
		}
		else if (c->type == HOOK_TYPE_LUA)
		{
			int i = 0, nb = 0, nbr = 1;
			int oldtop = lua_gettop(L), size;

			/* Push the function */
			lua_getglobal(L, c->script);

			/* Push and count the arguments */
			COPY(&real_ap, ap, va_list);
			while (fmt[i])
			{
				switch (fmt[i++])
				{
				case 'd':
				case 'l':
					tolua_pushnumber(L, va_arg(real_ap, s32b));
					nb++;
					break;
				case 's':
					tolua_pushstring(L, va_arg(real_ap, char*));
					nb++;
					break;
				case 'O':
					tolua_pushusertype(L, (void*)va_arg(real_ap, object_type*), tolua_tag(L, "object_type"));
					nb++;
					break;
				case 'M':
					tolua_pushusertype(L, (void*)va_arg(real_ap, monster_type*), tolua_tag(L, "monster_type"));
					nb++;
					break;
				case '(':
				case ')':
				case ',':
					break;
				}
			}

			/* Count returns */
			nbr += strlen(ret);

			/* Call the function */
			if (lua_call(L, nb, nbr))
			{
				cmsg_format(TERM_VIOLET, "ERROR in lua_call while calling '%s' lua hook script. Breaking the hook chain now.", c->script);
				return FALSE;
			}

			/* Number of returned values, SHOULD be the same as nbr, but I'm paranoid */
			size = lua_gettop(L) - oldtop;

			/* get the extra returns if needed */
			for (i = 0; i < nbr - 1; i++)
			{
				if ((ret[i] == 'd') || (ret[i] == 'l'))
				{
					if (lua_isnumber(L, ( -size) + 1 + i)) process_hooks_return[i].num = tolua_getnumber(L, ( -size) + 1 + i, 0);
					else process_hooks_return[i].num = 0;
				}
				else if (ret[i] == 's')
				{
					if (lua_isstring(L, ( -size) + 1 + i)) process_hooks_return[i].str = tolua_getstring(L, ( -size) + 1 + i, "");
					else process_hooks_return[i].str = NULL;
				}
				else if (ret[i] == 'O')
				{
					if (tolua_istype(L, ( -size) + 1 + i, tolua_tag(L, "object_type"), 0))
						process_hooks_return[i].o_ptr = (object_type*)tolua_getuserdata(L, ( -size) + 1 + i, NULL);
					else
						process_hooks_return[i].o_ptr = NULL;
				}
				else if (ret[i] == 'M')
				{
					if (tolua_istype(L, ( -size) + 1 + i, tolua_tag(L, "monster_type"), 0))
						process_hooks_return[i].m_ptr = (monster_type*)tolua_getuserdata(L, ( -size) + 1 + i, NULL);
					else
						process_hooks_return[i].m_ptr = NULL;
				}
				else process_hooks_return[i].num = 0;
			}

			/* Get the basic return(continue or stop the hook chain) */
			if (tolua_getnumber(L, -size, 0))
			{
				lua_settop(L, oldtop);
				return (TRUE);
			}
			if (process_hooks_restart)
			{
				c = hooks_heads[h_idx];
				process_hooks_restart = FALSE;
			}
			else
				c = c->next;
			lua_settop(L, oldtop);
		}
		else
		{
			msg_format("Unkown hook type %d, name %s", c->type, c->name);
		}
	}

	return FALSE;
}

bool_ process_hooks_ret(int h_idx, char *ret, char *fmt, ...)
{
	va_list ap;
	bool_ r;

	va_start(ap, fmt);
	r = vprocess_hooks_return (h_idx, ret, fmt, &ap);
	va_end(ap);
	return (r);
}

bool_ process_hooks(int h_idx, char *fmt, ...)
{
	va_list ap;
	bool_ ret;

	va_start(ap, fmt);
	ret = vprocess_hooks_return (h_idx, "", fmt, &ap);
	va_end(ap);
	return (ret);
}

/******** Plots & Quest stuff ********/

static void quest_describe(int q_idx)
{
	int i = 0;

	while ((i < 10) && (quest[q_idx].desc[i][0] != '\0'))
	{
		cmsg_print(TERM_YELLOW, quest[q_idx].desc[i++]);
	}
}

/* Catch-all quest hook */
bool_ quest_null_hook(int q)
{
	/* Do nothing */
	return (FALSE);
}

/************************** Random Quests *************************/
#include "q_rand.c"

/**************************** Main plot ***************************/
#include "q_main.c"
#include "q_one.c"
#include "q_ultrag.c"
#include "q_ultrae.c"

/**************************** Bree plot ***************************/
#include "q_thief.c"
#include "q_hobbit.c"
#include "q_troll.c"
#include "q_wight.c"
#include "q_nazgul.c"
#include "q_shroom.c"

/*************************** Lorien plot **************************/
#include "q_wolves.c"
#include "q_spider.c"
#include "q_poison.c"

/************************** Gondolin plot *************************/
#include "q_dragons.c"
#include "q_eol.c"
#include "q_nirna.c"
#include "q_invas.c"

/************************* Minas Anor plot ************************/
#include "q_haunted.c"
#include "q_betwen.c"

/************************* Khazad-dum plot ************************/
#include "q_evil.c"

/*************************** Other plot ***************************/
#include "q_narsil.c"
#include "q_thrain.c"