summaryrefslogtreecommitdiff
path: root/src/modules.cc
blob: a001450238f5a1024433419379b2640f1f0348d2 (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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
/*
 * Copyright (c) 2003 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 "modules.hpp"

#include "birth.hpp"
#include "cave.hpp"
#include "cave_type.hpp"
#include "corrupt.hpp"
#include "files.hpp"
#include "hook_eat_in.hpp"
#include "hook_give_in.hpp"
#include "hook_move_in.hpp"
#include "hook_stair_in.hpp"
#include "hook_stair_out.hpp"
#include "hook_new_monster_end_in.hpp"
#include "hooks.hpp"
#include "joke.hpp"
#include "lua_bind.hpp"
#include "monster2.hpp"
#include "monster_race.hpp"
#include "monster_type.hpp"
#include "object2.hpp"
#include "object_type.hpp"
#include "player_type.hpp"
#include "spells2.hpp"
#include "stats.hpp"
#include "tables.hpp"
#include "util.hpp"
#include "util.h"
#include "variable.h"
#include "variable.hpp"

#include <cassert>
#include <chrono>
#include <thread>

using std::this_thread::sleep_for;
using std::chrono::milliseconds;

/*
 * Check and create if needed the directory dirpath
 */
bool_ private_check_user_directory(cptr dirpath)
{
	/* Is this used anywhere else in *bands? */
	struct stat stat_buf;

	int ret;

	/* See if it already exists */
	ret = stat(dirpath, &stat_buf);

	/* It does */
	if (ret == 0)
	{
		/* Now we see if it's a directory */
		if ((stat_buf.st_mode & S_IFMT) == S_IFDIR) return (TRUE);

		/*
		 * Something prevents us from create a directory with
		 * the same pathname
		 */
		return (FALSE);
	}

	/* No - this maybe the first time. Try to create a directory */
	else
	{
		/* Create the ~/.ToME directory */
		ret = mkdir(dirpath, 0700);

		/* An error occured */
		if (ret == -1) return (FALSE);

		/* Success */
		return (TRUE);
	}
}

static void module_reset_dir_aux(char **dir, cptr new_path)
{
	char buf[1024];

	/* Build the new path */
	strnfmt(buf, sizeof (buf), "%s%s%s", *dir, PATH_SEP, new_path);

	free(*dir);
	*dir = strdup(buf);

	/* Make it if needed */
	if (!private_check_user_directory(*dir))
		quit(format("Unable to create module dir %s\n", *dir));
}

static void module_reset_dir(cptr dir, cptr new_path)
{
	char **d = 0;
	char buf[1025];

	if (!strcmp(dir, "core")) d = &ANGBAND_DIR_CORE;
	if (!strcmp(dir, "dngn")) d = &ANGBAND_DIR_DNGN;
	if (!strcmp(dir, "data")) d = &ANGBAND_DIR_DATA;
	if (!strcmp(dir, "edit")) d = &ANGBAND_DIR_EDIT;
	if (!strcmp(dir, "file")) d = &ANGBAND_DIR_FILE;
	if (!strcmp(dir, "help")) d = &ANGBAND_DIR_HELP;
	if (!strcmp(dir, "info")) d = &ANGBAND_DIR_INFO;
	if (!strcmp(dir, "pref")) d = &ANGBAND_DIR_PREF;
	if (!strcmp(dir, "xtra")) d = &ANGBAND_DIR_XTRA;
	if (!strcmp(dir, "user")) d = &ANGBAND_DIR_USER;
	if (!strcmp(dir, "note")) d = &ANGBAND_DIR_NOTE;

	if (
	    !strcmp(dir, "user") ||
	    !strcmp(dir, "note"))
	{
		char user_path[1024];
		/* copied from init_file_paths */
		path_parse(user_path, 1024, PRIVATE_USER_PATH);
		strcat(user_path, USER_PATH_VERSION);
		strnfmt(buf, 1024, "%s%s%s", user_path, PATH_SEP, new_path);

		free(*d);
		*d = strdup(buf);

		// Make it if needed */
		if (!private_check_user_directory(*d))
		{
			quit(format("Unable to create module dir %s\n", *d));
		}
	}
	else if (!strcmp(dir, "save"))
	{
		module_reset_dir_aux(&ANGBAND_DIR_SAVE, new_path);
	}
	else
	{
		/* Build the new path */
		strnfmt(buf, 1024, "%s%s%s%s%s", ANGBAND_DIR_MODULES, PATH_SEP, new_path, PATH_SEP, dir);

		free(*d);
		*d = strdup(buf);
	}
}

static void dump_modules(int sel, int max)
{
	int i;

	char buf[40], pre = ' ', post = ')';

	char ind;


	for (i = 0; i < max; i++)
	{
		ind = I2A(i % 26);
		if (i >= 26) ind = toupper(ind);

		if (sel == i)
		{
			pre = '[';
			post = ']';
		}
		else
		{
			pre = ' ';
			post = ')';
		}

		strnfmt(buf, 40, "%c%c%c %s", pre, ind, post, modules[i].meta.name);

		if (sel == i)
		{
			print_desc_aux(modules[i].meta.desc, 5, 0);

			c_put_str(TERM_L_BLUE, buf, 10 + (i / 4), 20 * (i % 4));
		}
		else
			put_str(buf, 10 + (i / 4), 20 * (i % 4));
	}
}

static void activate_module(int module_idx)
{
	module_type *module_ptr = &modules[module_idx];

	/* Initialize the module table */
	game_module_idx = module_idx;

	/* Do misc inits  */
	max_plev = module_ptr->max_plev;

	RANDART_WEAPON = module_ptr->randarts.weapon_chance;
	RANDART_ARMOR = module_ptr->randarts.armor_chance;
	RANDART_JEWEL = module_ptr->randarts.jewelry_chance;

	VERSION_MAJOR = module_ptr->meta.version.major;
	VERSION_MINOR = module_ptr->meta.version.minor;
	VERSION_PATCH = module_ptr->meta.version.patch;
	version_major = VERSION_MAJOR;
	version_minor = VERSION_MINOR;
	version_patch = VERSION_PATCH;

	/* Change window name if needed */
	if (strcmp(game_module, "ToME"))
	{
		strnfmt(angband_term_name[0], 79, "T-Engine: %s", game_module);
		Term_xtra(TERM_XTRA_RENAME_MAIN_WIN, 0);
	}

	/* Reprocess the player name, just in case */
	process_player_base();
}

static void init_module(module_type *module_ptr)
{
	/* Set up module directories? */
	cptr dir = module_ptr->meta.module_dir;
	if (dir) {
		module_reset_dir("core", dir);
		module_reset_dir("data", dir);
		module_reset_dir("dngn", dir);
		module_reset_dir("edit", dir); 
		module_reset_dir("file", dir);
		module_reset_dir("help", dir);
		module_reset_dir("note", dir);
		module_reset_dir("save", dir);
		module_reset_dir("user", dir);
		module_reset_dir("pref", dir);
	}
}

bool_ module_savefile_loadable(cptr savefile_mod)
{
	return (strcmp(savefile_mod, modules[game_module_idx].meta.save_file_tag) == 0);
}

/* Did the player force a module on command line */
cptr force_module = NULL;

/* Find module index by name. Returns -1 if matching module not found */
int find_module(cptr name)
{
	int i = 0;

	for (i=0; i<MAX_MODULES; i++)
	{
		if (streq(name, modules[i].meta.name))
		{
			return i;
		}
	}

	return -1;
}

/* Display possible modules and select one */
bool_ select_module()
{
	s32b k, sel, max;

	/* How many modules? */
	max = MAX_MODULES;

	/* No need to bother the player if there is only one module */
	sel = -1;
	if (force_module) {
		/* Find module by name */
		sel = find_module(force_module);
	}
	/* Only a single choice */
	if (max == 1) {
		sel = 0;
	}
	/* No module selected */
	if (sel != -1)
	{
		/* Process the module */
		init_module(&modules[sel]);

		game_module = modules[sel].meta.name;

		activate_module(sel);

		return FALSE;
	}

	sel = 0;

	/* Preprocess the basic prefs, we need them to have movement keys */
	process_pref_file("pref.prf");

	while (TRUE)
	{
		/* Clear screen */
		Term_clear();

		/* Let the user choose */
		c_put_str(TERM_YELLOW, "Welcome to ToME, you must select a module to play,", 1, 12);
		c_put_str(TERM_YELLOW, "either ToME official module or third party ones.", 2, 13);
		put_str("Press 8/2/4/6 to move, Return to select and Esc to quit.", 4, 3);

		dump_modules(sel, max);

		k = inkey();

		if (k == ESCAPE)
		{
			quit(NULL);
		}
		if (k == '6')
		{
			sel++;
			if (sel >= max) sel = 0;
			continue;
		}
		else if (k == '4')
		{
			sel--;
			if (sel < 0) sel = max - 1;
			continue;
		}
		else if (k == '2')
		{
			sel += 4;
			if (sel >= max) sel = sel % max;
			continue;
		}
		else if (k == '8')
		{
			sel -= 4;
			if (sel < 0) sel = (sel + max - 1) % max;
			continue;
		}
		else if (k == '\r')
		{
			if (sel < 26) k = I2A(sel);
			else k = toupper(I2A(sel));
		}

		{
			int x;

			if (islower(k)) x = A2I(k);
			else x = A2I(tolower(k)) + 26;

			if ((x < 0) || (x >= max)) continue;

			/* Process the module */
			init_module(&modules[x]);

			game_module = modules[x].meta.name;

			activate_module(x);

			return (FALSE);
		}
	}

	/* Shouldnt happen */
	return (FALSE);
}

static bool_ dleft(byte c, cptr str, int y, int o)
{
	int i = strlen(str);
	int x = 39 - (strlen(str) / 2) + o;
	while (i > 0)
	{
		int a = 0;
		int time = 0;

		if (str[i-1] != ' ')
		{
			while (a < x + i - 1)
			{
				Term_putch(a - 1, y, c, 32);
				Term_putch(a, y, c, str[i-1]);
				time = time + 1;
				if (time >= 4)
				{
					sleep_for(milliseconds(1));
					time = 0;
				}
				Term_redraw_section(a - 1, y, a, y);
				a = a + 1;

				if (inkey_scan()) {
					return TRUE;
				}
			}
		}

		i = i - 1;
	}
	return FALSE;
}

static bool_ dright(byte c, cptr str, int y, int o)
{
	int n = strlen(str); // Conversion to int to avoid warnings
	int x = 39 - (n / 2) + o;
	for (int i = 1; i <= n; i++)
	{
		int a = 79;
		int time = 0;

		if (str[i-1] != ' ') {
			while (a >= x + i - 1)
			{
				Term_putch(a + 1, y, c, 32);
				Term_putch(a, y, c, str[i-1]);
				time = time + 1;
				if (time >= 4) {
					sleep_for(milliseconds(1));
					time = 0;
				}
				Term_redraw_section(a, y, a + 1, y);
				a = a - 1;

				if (inkey_scan()) {
					return TRUE;
				}
			}
		}
	}
	return FALSE;
}

typedef struct intro_text intro_text;
struct intro_text
{
	bool_ (*drop_func)(byte, cptr, int, int);
	byte color;
	cptr text;
	int y0;
	int x0;
};

static bool_ show_intro(intro_text intro_texts[])
{
	int i = 0;

	Term_clear();
	for (i = 0; ; i++)
	{
		intro_text *it = &intro_texts[i];
		if (it->drop_func == NULL)
		{
			break;
		}
		else if (it->drop_func(it->color, it->text, it->y0, it->x0))
		{
			/* Abort */
			return TRUE;
		}
	}

	/* Wait for key */
	Term_putch(0, 0, TERM_DARK, 32);
	inkey();

	/* Continue */
	return FALSE;
}

void tome_intro()
{
	intro_text intro1[] =
	{
		{ dleft , TERM_L_BLUE, "Art thou an adventurer,", 10, 0, },
		{ dright, TERM_L_BLUE, "One who passes through the waterfalls we call danger", 11, -1, },
		{ dleft , TERM_L_BLUE, "to find the true nature of the legends beyond them?", 12, 0, },
		{ dright, TERM_L_BLUE, "If this is so, then seeketh me.", 13, -1, },
		{ dleft , TERM_WHITE , "[Press any key to continue]", 23, -1, },
		{ NULL  , TERM_WHITE , NULL, 0, 0, }
	};
	intro_text intro2[] =
	{
		{ dleft , TERM_L_BLUE , "DarkGod", 8, 0, },
		{ dright, TERM_WHITE  , "in collaboration with", 9, -1, },
		{ dleft , TERM_L_GREEN, "Eru Iluvatar,", 10, 0, },
		{ dright, TERM_L_GREEN, "Manwe", 11, -1, },
		{ dleft , TERM_WHITE  , "and", 12, 0, },
		{ dright, TERM_L_GREEN, "All the T.o.M.E. contributors(see credits.txt)", 13, -1, },
		{ dleft , TERM_WHITE  , "present", 15, 1, },
		{ dright, TERM_YELLOW , "T.o.M.E.", 16, 0, },
		{ dleft , TERM_WHITE  , "[Press any key to continue]", 23, -1, },
		{ NULL  , TERM_WHITE  , NULL, 0, 0, }
	};

	screen_save();

	/* Intro 1 */
	if (show_intro(intro1))
	{
		goto exit;
	}

	/* Intro 2 */
	if (show_intro(intro2))
	{
		goto exit;
	}

exit:
	screen_load();
}

void theme_intro()
{
	struct intro_text intro1[] =
	{
		{ dleft , TERM_L_BLUE , "Three Rings for the Elven-kings under the sky,", 10, 0, },
		{ dright, TERM_L_BLUE , "Seven for the Dwarf-lords in their halls of stone,", 11, -1, },
		{ dleft , TERM_L_BLUE , "Nine for Mortal Men doomed to die,", 12, 0, },
		{ dright, TERM_L_BLUE , "One for the Dark Lord on his dark throne", 13, -1, },
		{ dleft , TERM_L_BLUE , "In the land of Mordor, where the Shadows lie.", 14, 0, },
		{ dright, TERM_L_BLUE , "One Ring to rule them all, One Ring to find them,", 15, -1, },
		{ dleft , TERM_L_BLUE , "One Ring to bring them all and in the darkness bind them", 16, 0, },
		{ dright, TERM_L_BLUE , "In the land of Mordor, where the Shadows lie.", 17, -1, },
		{ dright, TERM_L_GREEN, "--J.R.R. Tolkien", 18, 0, },
		{ dleft , TERM_WHITE  , "[Press any key to continue]", 23, -1, },
		{ NULL  , TERM_WHITE  , NULL, 0, 0, },
	};
	struct intro_text intro2[] =
	{
		{ dleft , TERM_L_BLUE , "furiosity", 8, 0, },
		{ dright, TERM_WHITE  , "in collaboration with", 9, -1, },
		{ dleft , TERM_L_GREEN, "DarkGod and all the ToME contributors,", 10, 0, },
		{ dright, TERM_L_GREEN, "module creators, t-o-m-e.net forum posters,", 11, -1, },
		{ dleft , TERM_WHITE  , "and", 12, 0, },
		{ dright, TERM_L_GREEN, "by the grace of the Valar", 13, -1, },
		{ dleft , TERM_WHITE  , "present", 15, 1, },
		{ dright, TERM_YELLOW , "Theme (a module for ToME)", 16, 0, },
		{ dleft , TERM_WHITE  , "[Press any key to continue]", 23, -1, },
		{ NULL  , TERM_WHITE  , NULL, 0, 0, },
	};
	
       	screen_save();
 
	/* Intro 1 */
	if (show_intro(intro1))
	{
		goto exit;
	}

	/* Intro 2 */
	if (show_intro(intro2))
	{
		goto exit;
	}

exit:
	screen_load();
}

static bool_ auto_stat_gain_hook(void *data, void *in, void *out)
{
	while (p_ptr->last_rewarded_level * 5 <= p_ptr->lev)
	{
		do_inc_stat(A_STR);
		do_inc_stat(A_INT);
		do_inc_stat(A_WIS);
		do_inc_stat(A_DEX);
		do_inc_stat(A_CON);
		do_inc_stat(A_CHR);

		p_ptr->last_rewarded_level += 1;
	}

	return FALSE;
}

static bool_ drunk_takes_wine(void *data, void *in_, void *out)
{
	hook_give_in *in = (hook_give_in *) in_;
	monster_type *m_ptr = &m_list[in->m_idx];
	object_type *o_ptr = get_object(in->item);

	if ((m_ptr->r_idx == test_monster_name("Singing, happy drunk")) &&
	    (o_ptr->tval == TV_FOOD) &&
	    ((o_ptr->sval == 38) ||
	     (o_ptr->sval == 39)))
	{
		cmsg_print(TERM_YELLOW, "'Hic!'");

		/* Destroy item */
		inc_stack_size_ex(in->item, -1, OPTIMIZE, NO_DESCRIBE);

		/* Create empty bottle */
		{
			object_type forge;
			object_prep(&forge, lookup_kind(TV_BOTTLE,1));
			drop_near(&forge, 50, p_ptr->py, p_ptr->px);
			return TRUE;
		}
	}
	else
	{
		return FALSE;
	}
}

static bool_ hobbit_food(void *data, void *in_, void *out)
{
	hook_give_in *in = (hook_give_in *) in_;
	monster_type *m_ptr = &m_list[in->m_idx];
	object_type *o_ptr = get_object(in->item);

	if ((m_ptr->r_idx == test_monster_name("Scruffy-looking hobbit")) &&
	    (o_ptr->tval == TV_FOOD))
	{
		cmsg_print(TERM_YELLOW, "'Yum!'");

		inc_stack_size_ex(in->item, -1, OPTIMIZE, NO_DESCRIBE);

		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

static bool_ smeagol_ring(void *data, void *in_, void *out)
{
	hook_give_in *in = (hook_give_in *) in_;
	monster_type *m_ptr = &m_list[in->m_idx];
	object_type *o_ptr = get_object(in->item);

	if ((m_ptr->r_idx == test_monster_name("Smeagol")) &&
	    (o_ptr->tval == TV_RING))
	{
		cmsg_print(TERM_YELLOW, "'MY... PRECIOUSSSSS!!!'");

		inc_stack_size_ex(in->item, -1, OPTIMIZE, NO_DESCRIBE);

		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

static bool_ longbottom_leaf(void *data, void *in_, void *out_)
{
	hook_eat_in *in = (hook_eat_in *) in_;

	if ((in->o_ptr->tval == TV_FOOD) &&
	    (in->o_ptr->sval == 45))
	{
		msg_print("What a stress reliever!");
		heal_insanity(1000);
		return TRUE;
	}

	return FALSE;
}

static bool_ food_vessel(void *data, void *in_, void *out)
{
	hook_eat_in *in = (hook_eat_in *) in_;

	if (((in->o_ptr->tval == TV_FOOD) && (in->o_ptr->sval == 43)) ||
	    ((in->o_ptr->tval == TV_FOOD) && (in->o_ptr->sval == 44)))
	{
		object_type forge;

		object_prep(&forge, lookup_kind(TV_JUNK, 3));

		forge.ident |= IDENT_MENTAL | IDENT_KNOWN;
		inven_carry(&forge, FALSE);

		return TRUE;
	}

	return FALSE;
}

/*
 * Player must have appropriate keys to enter Erebor.
 */
static bool_ erebor_stair(void *data, void *in_, void *out_)
{
	hook_stair_in *in = (hook_stair_in *) in_;
	hook_stair_out *out = (hook_stair_out *) out_;

	if ((dungeon_type == 20) &&
	    (dun_level == 60) &&
	    (in->direction == STAIRS_DOWN))
	{
		int i, keys;

		keys = 0;
		for (i = 0; i < INVEN_TOTAL - 1; i++)
		{
			if ((p_ptr->inventory[i].name1 == 209) ||
			    (p_ptr->inventory[i].name1 == 210))
			{
				keys += 1;
			}
		}

		if (keys >= 2)
		{
			msg_print("The moon-letters on the map show you "
				  "the keyhole! You use the key to enter.");
			out->allow = TRUE;
		}
		else
		{
			msg_print("You have found a door, but you cannot "
				  "find a way to enter. Ask in Dale, perhaps?");
			out->allow = FALSE;
		}
	}

	return FALSE;
}

/*
 * Orthanc requires a key.
 */
static bool_ orthanc_stair(void *data, void *in_, void *out_)
{
	hook_stair_in *in = (hook_stair_in *) in_;
	hook_stair_out *out = (hook_stair_out *) out_;

	if ((dungeon_type == 36) &&
	    (dun_level == 39) &&
	    (in->direction == STAIRS_DOWN))
	{
		int i, keys;

		keys = 0;
		for (i = 0; i < INVEN_TOTAL - 1; i++)
		{
			if (p_ptr->inventory[i].name1 == 15)
			{
				keys += 1;
			}
		}

		if (keys >= 1)
		{
			msg_print("#BYou have the key to the tower of Orthanc! You may proceed.#w");
			out->allow = TRUE;
		}
		else
		{
			msg_print("#yYou may not enter Orthanc without the key to the gates!#w Rumours say the key was lost in the Mines of Moria...");
			out->allow = FALSE;
		}
	}

	return FALSE;
}

/*
 * Movement from Theme
 */
static bool_ theme_push_past(void *data, void *in_, void *out_)
{
	hook_move_in *p = (hook_move_in *) in_;
	cave_type *c_ptr = &cave[p->y][p->x];

	if (c_ptr->m_idx > 0)
	{
		monster_type *m_ptr = &m_list[c_ptr->m_idx];
		auto const mr_ptr = m_ptr->race();

		if (m_ptr->status >= MSTATUS_NEUTRAL)
		{
			if (cave_floor_bold(p->y, p->x) ||
			    (mr_ptr->flags2 == RF2_PASS_WALL))
			{
				char buf[128];

				monster_desc(buf, m_ptr, 0);
				msg_print(format("You push past %s.", buf));

				m_ptr->fy = p_ptr->py;
				m_ptr->fx = p_ptr->px;
				cave[p_ptr->py][p_ptr->px].m_idx = c_ptr->m_idx;
				c_ptr->m_idx = 0;
			}
			else
			{
				char buf[128];

				monster_desc(buf, m_ptr, 0);
				msg_print(format("%s is in your way!", buf));
				energy_use = 0;

				return TRUE;
			}
		}
	}

	return FALSE;
}

/*
 * Check if monster race is in list. The list is terminated
 * with a -1.
 */
static bool_ race_in_list(int r_idx, int race_idxs[])
{
	int i;

	for (i = 0; race_idxs[i] >= 0; i++)
	{
		if (r_idx == race_idxs[i])
		{
			return TRUE;
		}
	}

	return FALSE;
}

/*
 * Monster racial alignment from Theme.
 */
s16b *theme_race_status(int r_idx)
{
	static s16b FRIEND_ = MSTATUS_FRIEND;
	static s16b *FRIEND = &FRIEND_;
	static s16b NEUTRAL_ = MSTATUS_NEUTRAL;
	static s16b *NEUTRAL = &NEUTRAL_;

	object_type *o_ptr = NULL;

	switch (p_ptr->prace)
	{
	case RACE_MAIA:
	{
		int good_race_idxs[] = {
			25, 29, 45, 97, 109,
			147, 225, 335, 346, 443,
			581, 629, 699, 853, 984,
			1007, 1017, -1
		};

		if (!(player_has_corruption(CORRUPT_BALROG_AURA)) &&
		    !(player_has_corruption(CORRUPT_BALROG_WINGS)) &&
		    !(player_has_corruption(CORRUPT_BALROG_STRENGTH)) &&
		    !(player_has_corruption(CORRUPT_BALROG_FORM)) &&
		    race_in_list(r_idx, good_race_idxs))
		{
			/* Good beings (except swans, GWoPs, Wyrm
			 * Spirits, and some joke uniques) are
			 * coaligned with Maiar */
			return FRIEND;
		}

		break;
	}

	case RACE_HUMAN:
	case RACE_DUNADAN:
	case RACE_DRUADAN:
	case RACE_ROHANKNIGHT:
	{
		int nonevil_humanoid_race_idxs[] = {
			43, 45, 46, 83, 93,
			97, 109, 110, 142, 147,
			216, 225, 293, 345, 346,
			693, 699, 937, 988, 997,
			998, 1000, -1
		};
		
		if (race_in_list(r_idx, nonevil_humanoid_race_idxs))
		{
			return NEUTRAL;
		}

		break;
	}

	case RACE_ELF:
	case RACE_HOBBIT:
	case RACE_WOOD_ELF:
	{
		int nonevil_sentient_race_idxs[] = {
			43, 45, 46, 83, 93,
			97, 109, 110, 142, 147,
			216, 225, 293, 345, 346,
			693, 699, 937, 988, 997,
			998, 1000, 74, 103, 882,
			1017, -1
		};

		if (race_in_list(r_idx, nonevil_sentient_race_idxs))
		{
			return NEUTRAL;
		}

		break;
	}

	case RACE_GNOME:
	{
		int gnomish_race_idxs[] = {
			103, 281, 680, 984, 1001,
			1003, 1007, 1011, 1014, 1016,
			-1
		};

		if (race_in_list(r_idx, gnomish_race_idxs))
		{
			return NEUTRAL;
		}

		break;
	}

	case RACE_DWARF:
	case RACE_PETTY_DWARF:
	{
		int dwarvish_race_idxs[] = {
			111, 112, 179, 180, 181,
			182, -1
		};

		if (race_in_list(r_idx, dwarvish_race_idxs))
		{
			return NEUTRAL;
		}

		break;
	}

	case RACE_ORC:
	{
		int low_orc_race_idxs[] = {
			87, 118, 126, 149, 244,
			251, 264, -1
		};

		if ((p_ptr->pgod == GOD_MELKOR) &&
		    race_in_list(r_idx, low_orc_race_idxs))
		{
			return FRIEND;
		}

		break;
	}

	case RACE_TROLL:
	{
		int low_troll_race_idxs[] = {
			297, 401, 403, 424, 454,
			491, 496, 509, 538, -1
		};

		if ((p_ptr->pgod == GOD_MELKOR) &&
		    race_in_list(r_idx, low_troll_race_idxs))
		{
			return NEUTRAL;
		}

		break;
	}

	case RACE_HALF_OGRE:
	{
		int ogre_race_idxs[] = {
			262, 285, 415, 430, 479,
			745, 918, -1
		};

		if (race_in_list(r_idx, ogre_race_idxs))
		{
			return NEUTRAL;
		}

		break;
	}

	case RACE_BEORNING:
	{
		/* Bears; not werebears. */
		int bear_race_idxs[] = {
			160, 173, 191, 854,
			855, 867, 873, -1
		};

		if (race_in_list(r_idx, bear_race_idxs))
		{
			return NEUTRAL;
		}

		break;
	}

	case RACE_DARK_ELF:
	{
		int dark_elven_race_idxs[] = {
			122, 178, 183, 226, 348,
			375, 400, 657, -1
		};

		if (race_in_list(r_idx, dark_elven_race_idxs))
		{
			return FRIEND;
		}

		break;
	}

	case RACE_ENT:
	{
		int plant_race_idxs[] = {
			248, 266, 317, 329, 396,
			-1
		};

		if (race_in_list(r_idx, plant_race_idxs))
		{
			return FRIEND;
		}

		/* And since the above is largely useless except out
		   in the wild...  If an Ent worships Yavanna,
		   lower-level animals are coaligned should make the
		   early game a bit easier for Ents. */

		if (p_ptr->pgod == GOD_YAVANNA)
		{
			int lower_animal_race_idxs[] = {
				 21,  23,  24,  25,  26,
				 27,  28,  29,  30,  31,
				 33,  35,  36,  37,  38,
				 39,  41,  49,  50,  52,
				 56,  57,  58,  59,  60,
				 61,  62,  69,  70,  75,
				 77,  78,  79,  86,  88,
				 89,  90,  95,  96, 105,
				106, 114, 119, 120, 121,
				123, 127, 134, 141, 143,
				151, 154, 155, 156, 160,
				161, 168, 171, 173, 174,
				175, 176, 187, 191, 196,
				197, 198, 210, 211, 213,
				230, 236, 250, 259, -1
			};

			if (race_in_list(r_idx, lower_animal_race_idxs))
			{
				return FRIEND;
			}
		}

		break;
	}

	case RACE_EAGLE:
	{
		int nonevil_nonneurtal_bird_race_idxs[] = {
			61, 141, 151, 279, -1
		};

		if (race_in_list(r_idx, nonevil_nonneurtal_bird_race_idxs))
		{
			return FRIEND;
		}

		break;
	}

	case RACE_DRAGON:
	{
		int hatchling_dragon_race_idxs[] = {
			163, 164, 165, 166, 167,
			204, 218, 219, 911, -1
		};

		if (race_in_list(r_idx, hatchling_dragon_race_idxs))
		{
			return FRIEND;
		}

		break;
	}

	case RACE_YEEK:
	{
		int yeek_race_idxs[] = {
			580, 583, 594, 653, 655,
			659, 661, -1
		};

		if (race_in_list(r_idx, yeek_race_idxs))
		{
			return NEUTRAL;
		}

		break;
	}

	};

	/* Oathbreakers are coaligned if player is wielding Anduril.
	   It's dirty, but it works, and it doesn't bother checking
	   demons and the races who can't wield weapons. */
	o_ptr = get_object(INVEN_WIELD);
	if (o_ptr != NULL &&
	    o_ptr->name1 == ART_ANDURIL)
	{
		switch (p_ptr->prace)
		{
		case RACE_HUMAN:
		case RACE_HALF_ELF:
		case RACE_ELF:
		case RACE_HOBBIT:
		case RACE_GNOME:
		case RACE_DWARF:
		case RACE_ORC:
		case RACE_TROLL:
		case RACE_DUNADAN:
		case RACE_HIGH_ELF:
		case RACE_HALF_OGRE:
		case RACE_BEORNING:
		case RACE_DRUADAN:
		case RACE_PETTY_DWARF:
		case RACE_DARK_ELF:
		case RACE_ENT:
		case RACE_ROHANKNIGHT:
		case RACE_YEEK:
		case RACE_WOOD_ELF:
		case RACE_MAIA:
		case RACE_EASTERLING:
		case RACE_DEMON:
		{
			int oathbreaker_race_idxs[] = {
				731, -1
			};

			if (race_in_list(r_idx, oathbreaker_race_idxs))
			{
				return FRIEND;
			}

			break;
		}
		}
	}

	/* No status override */
	return NULL;
}

static bool_ theme_level_end_gen(void *data, void *in, void *out)
{
	int i = 0;

	for (i = 0; i < m_max; i++)
	{
		monster_type *m_ptr = &m_list[i];
		int r_idx = m_ptr->r_idx;
		s16b *status = theme_race_status(r_idx);
		if (status)
		{
			m_ptr->status = *status;
		}
	}

	return FALSE;
}

static bool_ theme_new_monster_end(void *data, void *in_, void *out)
{
	hook_new_monster_end_in *in = (hook_new_monster_end_in *) in_;
	s16b *status = theme_race_status(in->m_ptr->r_idx);

	if (status)
	{
		in->m_ptr->status = *status;
	}

	return FALSE;
}

void init_hooks_module()
{
	/*
	 * Common hooks
	 */
	add_hook_new(HOOK_GIVE,
		     drunk_takes_wine,
		     "drunk_takes_wine",
		     NULL);

	add_hook_new(HOOK_LEVEL_END_GEN,
		     gen_joke_monsters,
		     "gen_joke_monsters",
		     NULL);

	/*
	 * Module-specific hooks
	 */
	switch (game_module_idx)
	{
	case MODULE_TOME:
	{
		break;
	}

	case MODULE_THEME:
	{
		timer_aggravate_evil_enable();

		add_hook_new(HOOK_PLAYER_LEVEL,
			     auto_stat_gain_hook,
			     "auto_stat_gain",
			     NULL);

		add_hook_new(HOOK_GIVE,
			     hobbit_food,
			     "hobbit_food",
			     NULL);

		add_hook_new(HOOK_GIVE,
			     smeagol_ring,
			     "smeagol_ring",
			     NULL);

		add_hook_new(HOOK_EAT,
			     longbottom_leaf,
			     "longbottom_leaf",
			     NULL);

		add_hook_new(HOOK_EAT,
			     food_vessel,
			     "food_vessel",
			     NULL);

		add_hook_new(HOOK_STAIR,
			     erebor_stair,
			     "erebor_stair",
			     NULL);

		add_hook_new(HOOK_STAIR,
			     orthanc_stair,
			     "orthanc_stair",
			     NULL);

		add_hook_new(HOOK_MOVE,
			     theme_push_past,
			     "__hook_push_past",
			     NULL);

		add_hook_new(HOOK_LEVEL_END_GEN,
			     theme_level_end_gen,
			     "theme_level_end_gen",
			     NULL);

		add_hook_new(HOOK_NEW_MONSTER_END,
			     theme_new_monster_end,
			     "theme_new_monster_end",
			     NULL);

		break;
	}

	default:
		assert(FALSE);
	}
}