summaryrefslogtreecommitdiff
path: root/src/q_poison.cc
blob: a0d9e025dbf09345c4b1799f098ec1f21f0637f5 (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
#include "q_poison.hpp"

#include "cave.hpp"
#include "cave_type.hpp"
#include "game.hpp"
#include "hook_chardump_in.hpp"
#include "hook_drop_in.hpp"
#include "hook_init_quest_in.hpp"
#include "hook_quest_finish_in.hpp"
#include "hooks.hpp"
#include "messages.hpp"
#include "monster2.hpp"
#include "monster_race.hpp"
#include "monster_race_flag.hpp"
#include "monster_spell_flag.hpp"
#include "monster_type.hpp"
#include "object2.hpp"
#include "player_type.hpp"
#include "tables.hpp"
#include "util.hpp"
#include "variable.hpp"
#include "z-rand.hpp"

#define cquest (quest[QUEST_POISON])

static int wild_locs[4][2] =
{
	{ 32, 49, },
	{ 32, 48, },
	{ 33, 48, },
	{ 34, 48, },
};

static bool_ create_molds_hook(int r_idx)
{
	auto const &r_info = game->edit_data.r_info;

	auto r_ptr = &r_info[r_idx];

	if (r_ptr->spells & SF_MULTIPLY) return FALSE;

	if (r_ptr->d_char == 'm') return TRUE;
	else if (r_ptr->d_char == ',') return TRUE;
	else if (r_ptr->d_char == 'e') return TRUE;
	else return FALSE;
}

static bool quest_poison_gen_hook(void *, void *, void *)
{
	int cy = 1, cx = 1, x, y, tries = 10000, r_idx;
	bool_ (*old_get_mon_num_hook)(int r_idx);

	if (cquest.status != QUEST_STATUS_TAKEN)
	{
		return false;
	}
	if (p_ptr->wilderness_y != wild_locs[cquest.data[0]][0])
	{
		return false;
	}
	if (p_ptr->wilderness_x != wild_locs[cquest.data[0]][1])
	{
		return false;
	}
	if (p_ptr->wild_mode)
	{
		return false;
	}

	/* Find a good position */
	while (tries)
	{
		/* Get a random spot */
		cy = randint(cur_hgt - 24) + 22;
		cx = randint(cur_wid - 34) + 32;

		/* Is it a good spot ? */
		if (cave_empty_bold(cy, cx))
		{
			break;
		}

		/* One less try */
		tries--;
	}

	/* Place the baddies */

	/* Backup the old hook */
	old_get_mon_num_hook = get_mon_num_hook;

	/* Require "okay" monsters */
	get_mon_num_hook = create_molds_hook;

	/* Prepare allocation table */
	get_mon_num_prep();

	/* Pick a monster, using the level calculation */
	for (x = cx - 25; x <= cx + 25; x++)
	{
		for (y = cy - 25; y <= cy + 25; y++)
		{
			if (!in_bounds(y, x))
			{
				continue;
			}

			if (distance(cy, cx, y, x) > 25)
			{
				continue;
			}

			if (magik(80) && ((cave[y][x].feat == FEAT_DEEP_WATER) || (cave[y][x].feat == FEAT_SHAL_WATER)))
			{
				cave_set_feat(y, x, FEAT_TAINTED_WATER);
			}

			if (distance(cy, cx, y, x) > 10)
			{
				continue;
			}

			if (magik(60))
			{
				int m_idx;

				r_idx = get_mon_num(30);
				m_idx = place_monster_one(y, x, r_idx, 0, FALSE, MSTATUS_ENEMY);
				if (m_idx) m_list[m_idx].mflag |= MFLAG_QUEST;

				/* Sometimes make it up some levels */
				if (magik(80) && m_idx)
				{
					monster_type *m_ptr = &m_list[m_idx];

					if (m_ptr->level < p_ptr->lev)
					{
						m_ptr->exp = monster_exp(m_ptr->level + randint(p_ptr->lev - m_ptr->level));
						monster_check_experience(m_idx, TRUE);
					}
				}
			}
		}
	}

	/* Reset restriction */
	get_mon_num_hook = old_get_mon_num_hook;

	/* Prepare allocation table */
	get_mon_num_prep();

	return false;
}

static bool quest_poison_finish_hook(void *, void *in_, void *)
{
	struct hook_quest_finish_in *in = static_cast<struct hook_quest_finish_in *>(in_);
	s32b q_idx = in->q_idx;
	object_type forge, *q_ptr;

	if (q_idx != QUEST_POISON)
	{
		return false;
	}

	c_put_str(TERM_YELLOW, "The water is clean again! Thank you so much.", 8, 0);
	c_put_str(TERM_YELLOW, "The beautiful Mallorns are safe. Take this as a proof of our gratitude.", 9, 0);

	q_ptr = &forge;
	object_prep(q_ptr, lookup_kind(TV_DRAG_ARMOR, SV_DRAGON_BLUE));
	q_ptr->found = OBJ_FOUND_REWARD;
	q_ptr->number = 1;
	q_ptr->name2 = EGO_ELVENKIND;
	apply_magic(q_ptr, 1, FALSE, FALSE, FALSE);
	object_aware(q_ptr);
	object_known(q_ptr);
	q_ptr->ident |= IDENT_STOREB;
	inven_carry(q_ptr, FALSE);

	/* Continue the plot */
	*(quest[q_idx].plot) = QUEST_NULL;

	del_hook_new(HOOK_QUEST_FINISH, quest_poison_finish_hook);
	process_hooks_restart = TRUE;

	return true;
}

static bool quest_poison_dump_hook(void *, void *in_, void *)
{
	hook_chardump_in *in = static_cast<struct hook_chardump_in *>(in_);
	FILE *f = in->file;

	if (cquest.status >= QUEST_STATUS_COMPLETED)
	{
		fprintf(f, "\n You saved the beautiful Mallorns of Lothlorien.");
	}
	return false;
}

static bool quest_poison_quest_hook(void *, void *in_, void *)
{
	struct hook_init_quest_in *in = static_cast<struct hook_init_quest_in *>(in_);
	s32b q_idx = in->q_idx;
	object_type forge, *q_ptr;

	if (q_idx != QUEST_POISON)
	{
		return false;
	}

	q_ptr = &forge;
	object_prep(q_ptr, lookup_kind(TV_POTION2, SV_POTION2_CURE_WATER));
	q_ptr->number = 99;
	object_aware(q_ptr);
	object_known(q_ptr);
	q_ptr->ident |= IDENT_STOREB;
	q_ptr->inscription = "quest";

	inven_carry(q_ptr, FALSE);

	del_hook_new(HOOK_INIT_QUEST, quest_poison_quest_hook);
	process_hooks_restart = TRUE;

	return false;
}

static bool quest_poison_drop_hook(void *, void *in_, void *)
{
	struct hook_drop_in *in = static_cast<struct hook_drop_in *>(in_);
	s32b mcnt = 0, i, x, y;
	s32b o_idx = in->o_idx;
	object_type *o_ptr = &p_ptr->inventory[o_idx];

	if (cquest.status != QUEST_STATUS_TAKEN)
	{
		return false;
	}
	if (p_ptr->wilderness_y != wild_locs[cquest.data[0]][0])
	{
		return false;
	}
	if (p_ptr->wilderness_x != wild_locs[cquest.data[0]][1])
	{
		return false;
	}
	if (p_ptr->wild_mode)
	{
		return false;
	}

	if (o_ptr->tval != TV_POTION2)
	{
		return false;
	}
	if (o_ptr->sval != SV_POTION2_CURE_WATER)
	{
		return false;
	}

	for (i = m_max - 1; i >= 1; i--)
	{
		/* Access the monster */
		monster_type *m_ptr = &m_list[i];

		/* Ignore "dead" monsters */
		if (!m_ptr->r_idx)
		{
			continue;
		}

		if (m_ptr->status <= MSTATUS_NEUTRAL)
		{
			mcnt++;
		}
	}

	if (mcnt < 10)
	{
		for (x = 1; x < cur_wid - 1; x++)
		{
			for (y = 1; y < cur_hgt - 1; y++)
			{
				if (!in_bounds(y, x))
				{
					continue;
				}

				if (cave[y][x].feat == FEAT_TAINTED_WATER)
				{
					cave_set_feat(y, x, FEAT_SHAL_WATER);
				}
			}
		}

		cmsg_print(TERM_YELLOW, "Well done! The water seems to be clean now.");

		cquest.status = QUEST_STATUS_COMPLETED;

		del_hook_new(HOOK_DROP, quest_poison_drop_hook);
		process_hooks_restart = TRUE;

		return false;
	}
	else
	{
		msg_print("There are too many monsters left to cure the water.");
		return true;
	}
	return false;
}

void quest_poison_init_hook()
{
	/* Get a place to place the poison */
	if (!cquest.data[1])
	{
		cquest.data[1] = TRUE;
		cquest.data[0] = rand_int(4);
		if (wizard) message_add(format("Wilderness poison %d, %d", wild_locs[cquest.data[0]][0], wild_locs[cquest.data[0]][1]), TERM_BLUE);
	}

	if ((cquest.status >= QUEST_STATUS_TAKEN) && (cquest.status < QUEST_STATUS_FINISHED))
	{
		add_hook_new(HOOK_DROP,         quest_poison_drop_hook,   "poison_drop",   NULL);
		add_hook_new(HOOK_WILD_GEN,     quest_poison_gen_hook,    "poison_gen",    NULL);
		add_hook_new(HOOK_QUEST_FINISH, quest_poison_finish_hook, "poison_finish", NULL);
	}
	if (cquest.status < QUEST_STATUS_COMPLETED)
	{
		add_hook_new(HOOK_INIT_QUEST, quest_poison_quest_hook, "poison_iquest", NULL);
	}
	add_hook_new(HOOK_CHAR_DUMP, quest_poison_dump_hook, "poison_dump", NULL);
}