1 /* File: quest.c */
2 
3 /* Purpose: Quest code */
4 
5 /*
6  * Copyright (c) 1989, 2003 James E. Wilson, Robert A. Koeneke,
7  *                          Robert Ruehlmann, Steven Fuerst
8  *
9  * This software may be copied and distributed for educational, research, and
10  * not for profit purposes provided that this copyright and statement are
11  * included in all such copies.
12  */
13 
14 #include "angband.h"
15 
16 #include "wild.h"
17 
18 /*
19  * Maximum number of tries for selection of a proper quest monster
20  */
21 #define MAX_TRIES 100
22 
23 
24 /*
25  * Wipe a quest
26  */
quest_wipe(int i)27 static void quest_wipe(int i)
28 {
29 	quest_type *q_ptr = &quest[i];
30 
31 	q_ptr->status = QUEST_STATUS_UNTAKEN;
32 	q_ptr->flags = 0x00;
33 	q_ptr->type = QUEST_TYPE_NONE;
34 
35 	/* No artificial quest item */
36 	q_ptr->item = 0;
37 
38 	/* No quest-giver */
39 	q_ptr->place = 0;
40 	q_ptr->shop = 0;
41 
42 	/* No reward */
43 	q_ptr->reward = 0;
44 
45 	/* Types of creation and trigger hooks */
46 	q_ptr->c_type = QC_NONE;
47 	q_ptr->x_type = QX_NONE;
48 
49 	/* Timeout */
50 	q_ptr->timeout = 0;
51 
52 	/* No name */
53 	q_ptr->name[0] = '\0';
54 
55 	/*
56 	 * Do not need to clear the extra data
57 	 * - it is ignored since q_ptr->type = QUEST_TYPE_NONE
58 	 */
59 }
60 
61 /* Current location in scan for completed quests */
62 static s16b q_cnt = 0;
63 
64 /*
65  * Acquires and returns the index of a "free" quest.
66  *
67  * This routine should almost never fail, but in case it does,
68  * we must be sure to handle "failure" of this routine.
69  */
q_pop(void)70 u16b q_pop(void)
71 {
72 	int i;
73 
74 	/* Initial allocation */
75 	if (q_max < z_info->q_max)
76 	{
77 		/* Get next space */
78 		i = q_max;
79 
80 		/* Count quests */
81 		q_max++;
82 
83 		/* Use this quest */
84 		return (i);
85 	}
86 
87 	/* Recycle finished quests */
88 	for (i = 1; i < q_max; i++)
89 	{
90 		quest_type *q_ptr;
91 
92 		/* Make sure we have a linear algorithm */
93 		q_cnt++;
94 
95 		/* loop back to start */
96 		if (q_cnt >= q_max) q_cnt = 0;
97 
98 		/* Acquire quest */
99 		q_ptr = &quest[q_cnt];
100 
101 		/* Skip live quests */
102 		if (q_ptr->status != QUEST_STATUS_FINISHED) continue;
103 
104 		/* Skip find_place quests as these can be completed as a group */
105 		if (q_ptr->type == QUEST_TYPE_FIND_PLACE) continue;
106 
107 		/* Clear the old data */
108 		quest_wipe(q_cnt);
109 
110 		/* Use this field */
111 		return (q_cnt);
112 	}
113 
114 	/* Warn the player */
115 	msgf("Too many quests!");
116 
117 	/* Oops */
118 	return (0);
119 }
120 
121 
122 /* See if this quest is a wild quest and activate it if necessary */
discover_wild_quest(int q_num)123 void discover_wild_quest(int q_num)
124 {
125 	/* Is there a quest here? */
126 	if (!q_num) return;
127 
128 	/* Is it a wild quest */
129 	if (quest[q_num].type != QUEST_TYPE_WILD) return;
130 
131 	/* Was this a taken quest? */
132 	if (quest[q_num].status == QUEST_STATUS_UNTAKEN)
133 	{
134 		/* Now we take it */
135 		quest[q_num].status = QUEST_STATUS_TAKEN;
136 
137 		/* Hack -- make him active to make the discovery */
138 		quest[q_num].flags |= QUEST_FLAG_ACTIVE;
139 
140 		/* Announce */
141 		quest_discovery();
142 	}
143 }
144 
145 
146 /*
147  * Make a quest for killing n monsters of a certain type on a certain level
148  */
insert_dungeon_monster_quest(u16b r_idx,u16b num,u16b level)149 u16b insert_dungeon_monster_quest(u16b r_idx, u16b num, u16b level)
150 {
151 	int q_num;
152 	quest_type *q_ptr;
153 	monster_race *r_ptr = &r_info[r_idx];
154 
155 	/* get a new quest */
156 	q_num = q_pop();
157 
158 	/* Paranoia */
159 	if (!q_num) return (0);
160 
161 	q_ptr = &quest[q_num];
162 
163 	/* Store in information */
164 	q_ptr->type = QUEST_TYPE_DUNGEON;
165 
166 	/* We need to place the monster(s) when the dungeon is made */
167 	q_ptr->c_type = QC_DUN_MONST;
168 
169 	/* We need to trigger when the monsters are killed */
170 	if (FLAG(r_ptr, RF_UNIQUE))
171 	{
172 		q_ptr->x_type = QX_KILL_UNIQUE;
173 	}
174 	else
175 	{
176 		q_ptr->x_type = QX_KILL_MONST;
177 	}
178 
179 	if (num != 1)
180 	{
181 		char buf[80];
182 		strcpy(buf, mon_race_name(r_ptr));
183 		plural_aux(buf);
184 
185 		/* XXX XXX Create quest name */
186 		(void)strnfmt(q_ptr->name, 128, "Kill %d %s.", (int)num, buf);
187 	}
188 	else
189 	{
190 		/* XXX XXX Create quest name */
191 		(void)strnfmt(q_ptr->name, 128, "Kill %s.", mon_race_name(r_ptr));
192 	}
193 
194 	/* Save the quest data */
195 	q_ptr->data.dun.r_idx = r_idx;
196 	q_ptr->data.dun.level = level;
197 	q_ptr->data.dun.cur_num = 0;
198 	q_ptr->data.dun.max_num = num;
199 	q_ptr->data.dun.num_mon = 0;
200 
201 	/* Return number of quest */
202 	return (q_num);
203 }
204 
205 
206 /*
207  * Create the quests for the Serpent and Oberon
208  */
insert_winner_quest(u16b r_idx,u16b num,u16b level)209 static void insert_winner_quest(u16b r_idx, u16b num, u16b level)
210 {
211 	/* Normal monster quest */
212 	u16b q_idx = insert_dungeon_monster_quest(r_idx, num, level);
213 
214 	/* Winner result of quest */
215 	quest[q_idx].x_type = QX_KILL_WINNER;
216 	quest[q_idx].flags |= QUEST_FLAG_KNOWN;
217 	quest[q_idx].status = QUEST_STATUS_TAKEN;
218 }
219 
220 
221 /*
222  * Look for an appropriate dungeon for a given level
223  */
find_good_dungeon(int level,bool repeat)224 static u16b find_good_dungeon(int level, bool repeat)
225 {
226 	int i;
227 
228 	int score, best_score = 0;
229 
230 	int best_place = 0;
231 
232 	place_type *pl_ptr;
233 
234 	for (i = 0; i < place_count; i++)
235 	{
236 		pl_ptr = &place[i];
237 
238 		/* Want dungeons */
239 		if (pl_ptr->type != TOWN_DUNGEON) continue;
240 
241 		/* Reuse this dungeon? (relevant for quest_find_place) */
242 		if (!repeat && pl_ptr->quest_num) continue;
243 
244 		/* Get difference in levels */
245 		score = ABS(pl_ptr->dungeon->max_level - level);
246 
247 		/* The bigger the difference, the less likely a high score is */
248 		score = randint1(127 - score);
249 
250 		if (score > best_score)
251 		{
252 			best_score = score;
253 			best_place = i;
254 		}
255 	}
256 
257 	/* Best match to reward level */
258 	return (best_place);
259 }
260 
261 /*
262  * Look for an appropriate town with a distance appropriate
263  * for the given level.
264  */
find_good_town(int * dist)265 static u16b find_good_town(int *dist)
266 {
267 	int i;
268 
269 	int score, best_score = 0;
270 
271 	int best_place = 0;
272 
273 	place_type *pl_ptr;
274 
275 	for (i = 0; i < place_count; i++)
276 	{
277 		/* Not current town */
278 		if (i == p_ptr->place_num) continue;
279 
280 		pl_ptr = &place[i];
281 
282 		/* Want towns with buildings */
283 		if (!pl_ptr->numstores) continue;
284 
285 		/* Get difference of distance in wilderness blocks and difficulty level */
286 		score = abs(distance(pl_ptr->x, pl_ptr->y, p_ptr->px / 16, p_ptr->py / 16) - *dist);
287 
288 		/* The bigger the difference, the less likely a high score is */
289 		score = randint1(WILD_SIZE - score);
290 
291 		if (score > best_score)
292 		{
293 			best_score = score;
294 			best_place = i;
295 		}
296 	}
297 
298 	/* Save distance to best town */
299 	pl_ptr = &place[best_place];
300 	*dist = distance(pl_ptr->x, pl_ptr->y, p_ptr->px / 16, p_ptr->py / 16);
301 
302 	/* Best match to reward level */
303 	return (best_place);
304 }
305 
306 /*
307  * This function returns the closest name of the closest town and the
308  * direction to that town.  If known == TRUE then the player must have
309  * seen the closest town too, in order not to give away clues to the map.
310  */
describe_quest_location(cptr * dirn,int x,int y,bool known)311 cptr describe_quest_location(cptr * dirn, int x, int y, bool known)
312 {
313 	int i;
314 	int dx, dy;
315 
316 	/* Find the nearest town */
317 	int best_dist = 99999;
318 	int best_town = 0;
319 
320 	for (i = 0; i < place_count; i++)
321 	{
322 		bool visit = FALSE;
323 		int d;
324 
325 		wild_done_type *w_ptr;
326 
327 		/* Only real towns */
328 		if (place[i].type != TOWN_FRACT) continue;
329 
330 		/* Should this be a known town? */
331 		if (known)
332 		{
333 			for (dx = 0; dx < 8 && !visit; dx++)
334 			{
335 				for (dy = 0; dy < 8 && !visit; dy++)
336 				{
337 					/* Get wilderness square where the town could be */
338 					w_ptr = &wild[place[i].y + dy][place[i].x + dx].done;
339 
340 					/* Is this a town square */
341 					if (w_ptr->place != i) continue;
342 
343 					/* Has the player visited this square? */
344 					visit |= (w_ptr->info & WILD_INFO_SEEN);
345 				}
346 			}
347 
348 			/* Unmapped town */
349 			if (!visit) continue;
350 		}
351 
352 		/* Find closest town */
353 		d = distance(x, y, place[i].x, place[i].y);
354 
355 		/* Keep track of the best town */
356 		if (d < best_dist)
357 		{
358 			best_dist = d;
359 			best_town = i;
360 		}
361 	}
362 
363 
364 	dx = x - place[best_town].x;
365 	dy = y - place[best_town].y;
366 
367 	if (ABS(dy) > ABS(dx) * 3)
368 	{
369 		if (dy > 0)
370 			*dirn = "south";
371 		else
372 			*dirn = "north";
373 	}
374 	else if (ABS(dx) > ABS(dy) * 3)
375 	{
376 		if (dx > 0)
377 			*dirn = "east";
378 		else
379 			*dirn = "west";
380 	}
381 	else if (dx > 0)
382 	{
383 		if (dy > 0)
384 			*dirn = "south-east";
385 		else
386 			*dirn = "north-east";
387 	}
388 	else
389 	{
390 		if (dy > 0)
391 			*dirn = "south-west";
392 		else
393 			*dirn = "north-west";
394 	}
395 
396 	return (place[best_town].name);
397 }
398 
399 
400 /*
401  * Initialise the quests
402  */
init_quests(void)403 errr init_quests(void)
404 {
405 	int i;
406 
407 	/* Make the quest array */
408 	C_MAKE(quest, z_info->q_max, quest_type);
409 
410 	/* Reset number of quests */
411 	q_max = 1;
412 
413 	/* Wipe the quests */
414 	for (i = 0; i < z_info->q_max; i++)
415 	{
416 		quest_wipe(i);
417 	}
418 
419 	return (0);
420 }
421 
422 
423 
424 /*
425  * Quests
426  *
427  */
init_player_quests(void)428 void init_player_quests(void)
429 {
430 	int i;
431 
432 	/* Reset number of quests */
433 	q_max = 1;
434 
435 	/* Clear all the quests */
436 	for (i = 0; i < z_info->q_max; i++)
437 	{
438 		quest_wipe(i);
439 	}
440 
441 	/* Add the winner quests */
442 
443 	/* Hack XXX XXX Oberon, hard coded */
444 	insert_winner_quest(QW_OBERON, 1, 99);
445 
446 	/* Hack XXX XXX Serpent, hard coded */
447 	insert_winner_quest(QW_SERPENT, 1, 100);
448 }
449 
450 
451 /* Array of places to find an inscription */
452 static cptr find_quest[] =
453 {
454 	"You find the following inscription in the floor",
455 	"You see a message inscribed in the wall",
456 	"There is a sign saying",
457 	"Something is written on the staircase",
458 	"You find a scroll with the following message",
459 	"You hear",
460 };
461 
462 
463 /*
464  * Discover quests on this level
465  */
quest_discovery(void)466 void quest_discovery(void)
467 {
468 	int i;
469 
470 	quest_type *q_ptr;
471 
472 	char name[80];
473 
474 	for (i = 0; i < q_max; i++)
475 	{
476 		q_ptr = &quest[i];
477 
478 		/* Quest needs to be taken. */
479 		if (q_ptr->status != QUEST_STATUS_TAKEN) continue;
480 
481 		/* Is the quest active? */
482 		if (!(q_ptr->flags & QUEST_FLAG_ACTIVE)) continue;
483 
484 		/* Is the quest known already? */
485 		if (q_ptr->flags & QUEST_FLAG_KNOWN) continue;
486 
487 		/* Hack - The quest is now known */
488 		q_ptr->flags |= QUEST_FLAG_KNOWN;
489 
490 		/* See what type of quest it is */
491 		switch (q_ptr->type)
492 		{
493 			case QUEST_TYPE_NONE:
494 			{
495 				/* Paranoia */
496 				continue;
497 			}
498 
499 			case QUEST_TYPE_BOUNTY:
500 			{
501 
502 				/* Paranoia */
503 				continue;
504 			}
505 
506 			case QUEST_TYPE_DUNGEON:
507 			{
508 				monster_race *r_ptr = &r_info[q_ptr->data.dun.r_idx];
509 				int q_num = q_ptr->data.dun.max_num - q_ptr->data.dun.cur_num;
510 
511 				/* Assume the quest is a 'kill n monsters quest' for now. */
512 				strcpy(name, mon_race_name(r_ptr));
513 
514 				if (FLAG(r_ptr, RF_UNIQUE))
515 				{
516 					/* Unique */
517 					msgf("%s: Beware, this level is protected by %s!",
518 							   find_quest[rand_range(0, 5)], name);
519 				}
520 				else
521 				{
522 					/* Normal monsters */
523 					if (q_num > 1) plural_aux(name);
524 
525 					msgf("%s: Be warned, this level is guarded by %d %s!",
526 							   find_quest[rand_range(0, 5)], q_num, name);
527 				}
528 
529 				/* Disturb */
530 				disturb(FALSE);
531 
532 				continue;
533 			}
534 
535 			case QUEST_TYPE_WILD:
536 			{
537 				msgf("You discover something unusual in the wilderness.");
538 
539 				/* Disturb */
540 				disturb(FALSE);
541 
542 				/* Paranoia */
543 				continue;
544 			}
545 
546 			case QUEST_TYPE_FIND_ITEM:
547 			{
548 				msgf("You feel there is something interesting about this level.");
549 
550 				/* Disturb */
551 				disturb(FALSE);
552 
553 				/* Paranoia */
554 				continue;
555 
556 			}
557 
558 			default:
559 			{
560 				/* Paranoia */
561 				continue;
562 			}
563 		}
564 	}
565 }
566 
567 
568 /*
569  * Is this dungeon level a special (winner) quest level? */
is_special_level(int level)570 bool is_special_level(int level)
571 {
572 	int i;
573 
574 	quest_type *q_ptr;
575 
576 	for (i = 0; i < q_max; i++)
577 	{
578 		q_ptr = &quest[i];
579 
580 		/* Must be dungeon quest */
581 		if (q_ptr->type != QUEST_TYPE_DUNGEON) continue;
582 
583 		/* Must be winner quest */
584 		if (q_ptr->x_type != QX_KILL_WINNER) continue;
585 
586 		/* Is the quest still there? */
587 		if (q_ptr->status > QUEST_STATUS_TAKEN) continue;
588 
589 		/* Does the level match? */
590 		if (q_ptr->data.dun.level == level) return (TRUE);
591 	}
592 
593 	return (FALSE);
594 }
595 
596 /*
597  * Activate quests valid for this level
598  */
activate_quests(int level)599 void activate_quests(int level)
600 {
601 	int i;
602 
603 	quest_type *q_ptr;
604 
605 	for (i = 0; i < q_max; i++)
606 	{
607 		q_ptr = &quest[i];
608 
609 		/* Is the quest still there? */
610 		if (q_ptr->status > QUEST_STATUS_TAKEN) continue;
611 
612 		/* Assume no longer active */
613 		q_ptr->flags &= ~(QUEST_FLAG_ACTIVE);
614 
615 		/* Is the quest relevant? */
616 		switch (q_ptr->type)
617 		{
618 			case QUEST_TYPE_DUNGEON:
619 			{
620 				/* Correct dungeon level? */
621 				if (q_ptr->data.dun.level != level) break;
622 
623 				/* Hack - toggle QUESTOR flag */
624 				SET_FLAG(&r_info[q_ptr->data.dun.r_idx], RF_QUESTOR);
625 
626 				/* Activate the quest */
627 				q_ptr->flags |= QUEST_FLAG_ACTIVE;
628 
629 				break;
630 			}
631 
632 			case QUEST_TYPE_BOUNTY:
633 			{
634 				dun_type *d_ptr = dungeon();
635 				monster_race *r_ptr = &r_info[q_ptr->data.bnt.r_idx];
636 
637 				/* Hack - toggle QUESTOR flag */
638 				SET_FLAG(r_ptr, RF_QUESTOR);
639 
640 				/* Is the player inside the right sort of dungeon? */
641 				if (level &&
642 					r_ptr->flags[7] & d_ptr->habitat)
643 				{
644 					/* Create a starting level */
645 					int min_level = d_ptr->min_level;
646 
647 					/* How deep is the dungeon? */
648 					int depth = MIN(d_ptr->max_level, 99) - min_level;
649 
650 					/* How many have been killed? */
651 					int cur_num = q_ptr->data.bnt.cur_num;
652 
653 					/* Top out at how many monsters? */
654 					int max_num = q_ptr->data.bnt.max_num;
655 
656 					/* Spread the monsters evenly throughout the dungeon */
657 					if (level == min_level + (depth * (cur_num + 1)) / (max_num + 1))
658 					{
659 						/* Activate the quest */
660 						q_ptr->flags |= QUEST_FLAG_ACTIVE;
661 					}
662 				}
663 
664 				break;
665 			}
666 
667 			case QUEST_TYPE_WILD:
668 			{
669 				/* In Wilderness? */
670 				if (!level) q_ptr->flags |= QUEST_FLAG_ACTIVE;
671 				break;
672 			}
673 
674 
675 			case QUEST_TYPE_FIND_ITEM:
676 			{
677 				/* Always active until the relic has been id'd */
678 				q_ptr->flags |= QUEST_FLAG_ACTIVE;
679 			}
680 
681 			case QUEST_TYPE_MESSAGE:
682 			{
683 				int place_num = q_ptr->data.msg.place;
684 
685 				place_type *pl_ptr;
686 
687 				/* Not correct place? */
688 				if (place_num != p_ptr->place_num) break;
689 
690 				pl_ptr = &place[place_num];
691 
692 				/* Need to be on the surface */
693 				if (p_ptr->depth) break;
694 
695 				q_ptr->flags |= QUEST_FLAG_ACTIVE;
696 			}
697 
698 			case QUEST_TYPE_FIND_PLACE:
699 			{
700 				int place_num = q_ptr->data.fpl.place;
701 
702 				place_type *pl_ptr;
703 
704 				/* Not correct place? */
705 				if (place_num != p_ptr->place_num) break;
706 
707 				pl_ptr = &place[place_num];
708 
709 				/* Need to be on the surface */
710 				if (p_ptr->depth) break;
711 
712 				q_ptr->flags |= QUEST_FLAG_ACTIVE;
713 			}
714 		}
715 	}
716 }
717 
718 
719 /*
720  * Create a magical staircase
721  */
create_stairs(int x,int y)722 static void create_stairs(int x, int y)
723 {
724 	int i = 0;
725 
726 	int ny, nx;
727 
728 	cave_type *c_ptr = area(x, y);
729 
730 	/* Paranoia - not on deepest dungeon level */
731 	if (p_ptr->depth == dungeon()->max_level) return;
732 
733 	/* Stagger around */
734 	while ((cave_perma_grid(c_ptr) || c_ptr->o_idx) && !(i > 100))
735 	{
736 		/* Pick a location */
737 		scatter(&nx, &ny, x, y, 1);
738 
739 		/* Stagger */
740 		y = ny;
741 		x = nx;
742 
743 		/* paranoia - increment counter */
744 		i++;
745 
746 		/* paranoia */
747 		if (!in_bounds2(x, y)) continue;
748 
749 		c_ptr = area(x, y);
750 	}
751 
752 	/* Explain the staircase */
753 	msgf("A magical staircase appears...");
754 
755 	/* Destroy the fields on the square */
756 	delete_field(x, y);
757 
758 	/* Create stairs down */
759 	cave_set_feat(x, y, FEAT_MORE);
760 }
761 
762 
display_monster_quest(quest_type * q_ptr)763 static void display_monster_quest(quest_type *q_ptr)
764 {
765 	int j, k;
766 	cave_type *c_ptr;
767 
768 	int x, y;
769 
770 	monster_race *r_ptr = &r_info[q_ptr->data.dun.r_idx];
771 
772 	/* Hack -- "unique" monsters must be "unique" */
773 	if ((FLAG(r_ptr, RF_UNIQUE)) &&
774 		(r_ptr->cur_num >= r_ptr->max_num))
775 	{
776 		/* Hack - the unique is already dead */
777 		q_ptr->status = QUEST_STATUS_FINISHED;
778 	}
779 	else
780 	{
781 		bool group;
782 		int number, r_idx;
783 
784 		/* Create a number of monsters depending on quest_type */
785 		if (q_ptr->type == QUEST_TYPE_BOUNTY)
786 		{
787 			/* One at a time */
788 			number = 1;
789 
790 			/* Which monster? */
791 			r_idx = q_ptr->data.bnt.r_idx;
792 		}
793 		else
794 		{
795 			/* All remaining at once */
796 			number = q_ptr->data.dun.max_num - q_ptr->data.dun.cur_num;
797 
798 			/* Which monster? */
799 			r_idx = q_ptr->data.dun.r_idx;
800 		}
801 
802 		for (j = 0; j < number; j++)
803 		{
804 			for (k = 0; k < 5000; k++)
805 			{
806 				/* Find an empty grid */
807 				while (TRUE)
808 				{
809 					y = rand_range(p_ptr->min_hgt + 1,
810 								   p_ptr->max_hgt - 2);
811 					x = rand_range(p_ptr->min_wid + 1,
812 								   p_ptr->max_wid - 2);
813 
814 					/* Access the grid */
815 					c_ptr = area(x, y);
816 
817 					if (!cave_naked_grid(c_ptr)) continue;
818 					if (distance(x, y, p_ptr->px, p_ptr->py) < 10)
819 						continue;
820 					else
821 						break;
822 				}
823 
824 				if (FLAG(r_ptr, RF_FRIENDS))
825 					group = FALSE;
826 				else
827 					group = TRUE;
828 
829 				/* Try to place the monster */
830 				if (place_monster_aux
831 					(x, y, r_idx, FALSE, group, FALSE, FALSE, TRUE))
832 				{
833 					/* Success */
834 					break;
835 				}
836 				else
837 				{
838 					/* Failure - Try again */
839 					continue;
840 				}
841 			}
842 		}
843 	}
844 }
845 
display_artifact_quest(quest_type * q_ptr)846 static void display_artifact_quest(quest_type *q_ptr)
847 {
848 	int	x = rand_range(p_ptr->min_wid + 1, p_ptr->max_wid - 2);
849 	int y = rand_range(p_ptr->min_hgt + 1, p_ptr->max_hgt - 2);
850 
851 	/* Drop artifact in dungeon */
852 	create_named_art(q_ptr->data.fit.a_idx, x, y);
853 }
854 
855 
856 /*
857  * Test each quest to see which ones are created
858  */
trigger_quest_create(byte c_type,vptr data)859 void trigger_quest_create(byte c_type, vptr data)
860 {
861 	int i;
862 	quest_type *q_ptr;
863 
864 	/* Ignore data - it may be unused */
865 	(void)data;
866 
867 	for (i = 0; i < q_max; i++)
868 	{
869 		q_ptr = &quest[i];
870 
871 		/* Quest must be chosen */
872 		if (q_ptr->status != QUEST_STATUS_TAKEN) continue;
873 
874 		/* Quest must be active */
875 		if (!(q_ptr->flags & QUEST_FLAG_ACTIVE)) continue;
876 
877 		/* Must be relevant */
878 		if (q_ptr->c_type != c_type) continue;
879 
880 		/* Handle the trigger */
881 		switch (c_type)
882 		{
883 			case QC_NONE:
884 			{
885 				/* Paranoia */
886 				continue;
887 			}
888 
889 			case QC_DUN_MONST:
890 			{
891 				display_monster_quest(q_ptr);
892 				continue;
893 			}
894 
895 			case QC_DUN_ARTIFACT:
896 			{
897 				int place_num = q_ptr->data.fit.place;
898 
899 				place_type *pl_ptr;
900 
901 				/* Not correct place? */
902 				if (place_num != p_ptr->place_num) continue;
903 
904 				pl_ptr = &place[place_num];
905 
906 				/* Need to be in the dungeon */
907 				if (!pl_ptr->dungeon) continue;
908 
909 				/* Correct dungeon level? */
910 				if (p_ptr->depth != pl_ptr->dungeon->max_level) continue;
911 
912 				display_artifact_quest(q_ptr);
913 
914 				continue;
915 			}
916 		}
917 	}
918 }
919 
920 
921 /*
922  * Test each quest to see if they are completed
923  */
trigger_quest_complete(byte x_type,vptr data)924 void trigger_quest_complete(byte x_type, vptr data)
925 {
926 	int i;
927 	quest_type *q_ptr;
928 
929 	for (i = 0; i < q_max; i++)
930 	{
931 		q_ptr = &quest[i];
932 
933 		/* Quest must be chosen */
934 		if (q_ptr->status != QUEST_STATUS_TAKEN) continue;
935 
936 		/* Quest must be active */
937 		if (!(q_ptr->flags & QUEST_FLAG_ACTIVE)) continue;
938 
939 		/* Must be relevant */
940 		if (q_ptr->x_type != x_type) continue;
941 
942 		/* Handle the trigger */
943 		switch (x_type)
944 		{
945 			case QX_NONE:
946 			{
947 				/* Paranoia */
948 				break;
949 			}
950 
951 			case QX_KILL_MONST:
952 			case QX_KILL_UNIQUE:
953 			{
954 				monster_type *m_ptr = ((monster_type *)data);
955 
956 				if (q_ptr->data.bnt.r_idx == m_ptr->r_idx)
957 				{
958 					/* Don't count clones */
959 					if (m_ptr->smart & SM_CLONED) break;
960 
961 					/* Increment number killed */
962 					q_ptr->data.bnt.cur_num++;
963 
964 					if (q_ptr->data.bnt.cur_num >= q_ptr->data.bnt.max_num)
965 					{
966 						/* Complete the quest */
967 						q_ptr->status = QUEST_STATUS_COMPLETED;
968 
969 						/* Monster is no longer 'QUESTOR' */
970 						r_info[q_ptr->data.bnt.r_idx].flags[0] &= ~(RF0_QUESTOR);
971 					}
972 				}
973 
974 				break;
975 			}
976 
977 			case QX_KILL_WINNER:
978 			{
979 				monster_type *m_ptr = ((monster_type *)data);
980 				monster_race *r_ptr = &r_info[m_ptr->r_idx];
981 
982 				if (q_ptr->data.dun.r_idx == m_ptr->r_idx)
983 				{
984 					/* Winner? */
985 					if (mon_name_cont(r_ptr, "Serpent of Chaos"))
986 					{
987 						/* Total winner */
988 						p_ptr->state.total_winner = TRUE;
989 
990 						/* Redraw the "title" */
991 						p_ptr->redraw |= (PR_TITLE);
992 
993 						/* Congratulations */
994 						msgf("*** CONGRATULATIONS ***");
995 						msgf("You have won the game!");
996 						msgf
997 							("You may retire (commit suicide) when you are ready.");
998 					}
999 					else
1000 					{
1001 						/* Oberon */
1002 
1003 						/* A message */
1004 						msgf("Well done!");
1005 						msgf("You have beaten Oberon.");
1006 						msgf
1007 							("You now can meet the final challenge of the Serpent of Chaos.");
1008 					}
1009 
1010 					/* Complete the quest */
1011 					q_ptr->status = QUEST_STATUS_COMPLETED;
1012 
1013 					/* Mega-hack */
1014 					create_stairs(m_ptr->fx, m_ptr->fy);
1015 				}
1016 
1017 				break;
1018 			}
1019 
1020 			case QX_WILD_ENTER:
1021 			{
1022 				place_type *pl_ptr;
1023 
1024 				/* Only trigger for the correct quest */
1025 				if (q_ptr != data) continue;
1026 
1027 				pl_ptr = &place[p_ptr->place_num];
1028 
1029 				/* Wilderness quests turn off the monsters */
1030 				if (q_ptr->type == QUEST_TYPE_WILD)
1031 				{
1032 
1033 					/* Unlink location from wilderness */
1034 					int x = ((u16b)p_ptr->wilderness_x / WILD_BLOCK_SIZE);
1035 					int y = ((u16b)p_ptr->wilderness_y / WILD_BLOCK_SIZE);
1036 
1037 					wild_done_type *w_ptr = &wild[y][x].done;
1038 
1039 					/* No more place here */
1040 					w_ptr->place = 0;
1041 
1042 					/* Decrement active block counter */
1043 					pl_ptr->data--;
1044 
1045 					/* Are we done yet? */
1046 					if (pl_ptr->data) break;
1047 
1048 					/* Finish the quest */
1049 					q_ptr->status = QUEST_STATUS_FINISHED;
1050 				}
1051 
1052 				if (q_ptr->type == QUEST_TYPE_FIND_PLACE)
1053 				{
1054 					msgf("You find the ruin you were looking for.");
1055 
1056 					/* Complete the quest */
1057 					q_ptr->status = QUEST_STATUS_COMPLETED;
1058 				}
1059 
1060 				break;
1061 			}
1062 
1063 			case QX_KNOW_ARTIFACT:
1064 			{
1065 				if (*((int *) data) == q_ptr->data.fit.a_idx)
1066 				{
1067 					msgf("You find the relic you were looking for.");
1068 
1069 					/* Complete the quest */
1070 					q_ptr->status = QUEST_STATUS_COMPLETED;
1071 
1072 					break;
1073 				}
1074 			}
1075 
1076 			case QX_FIND_SHOP:
1077 			{
1078 				place_type *pl_ptr;
1079 
1080 				/* Towns must match */
1081 				if (p_ptr->place_num != q_ptr->data.msg.place) continue;
1082 
1083 				pl_ptr = &place[p_ptr->place_num];
1084 
1085 				/* Do the stores match? */
1086 				if ((store_type *) data != &pl_ptr->store[q_ptr->data.msg.shop]) continue;
1087 
1088 				/* Complete the quest */
1089 				q_ptr->status = QUEST_STATUS_COMPLETED;
1090 
1091 				msgf("You have found the place you were looking for and you deliver the message!");
1092 				message_flush();
1093 
1094 				break;
1095 			}
1096 		}
1097 
1098 		/* Finished the quest? */
1099 		if ((q_ptr->status == QUEST_STATUS_FINISHED) ||
1100 			(q_ptr->status == QUEST_STATUS_COMPLETED))
1101 		{
1102 			msgf("You just completed your quest!");
1103 		}
1104 	}
1105 }
1106 
1107 
1108 /*
1109  * Look up an open quest that corresponds to the given building.
1110  *
1111  * If there is none, return NULL.
1112  *
1113  * (This assumes one quest per building.)
1114  */
lookup_quest_building(const store_type * b_ptr)1115 quest_type *lookup_quest_building(const store_type *b_ptr)
1116 {
1117 	int i;
1118 	quest_type *q_ptr;
1119 
1120 	place_type *pl_ptr = &place[p_ptr->place_num];
1121 
1122 	for (i = 0; i < q_max; i++)
1123 	{
1124 		q_ptr = &quest[i];
1125 
1126 		if (q_ptr->place != p_ptr->place_num) continue;
1127 
1128 		/* Bounds checking */
1129 		if (q_ptr->shop >= pl_ptr->numstores) continue;
1130 
1131 		/* Disregard finished quests */
1132 		if (q_ptr->status == QUEST_STATUS_FINISHED) continue;
1133 
1134 		/* A match? */
1135 		if (&pl_ptr->store[q_ptr->shop] == b_ptr)
1136 		{
1137 
1138 			return (q_ptr);
1139 		}
1140 	}
1141 
1142 	/* No match */
1143 	return (NULL);
1144 }
1145 
1146 
reward_quest(quest_type * q_ptr)1147 void reward_quest(quest_type *q_ptr)
1148 {
1149 	switch (q_ptr->type)
1150 	{
1151 		case QUEST_TYPE_FIND_ITEM:
1152 		{
1153 			if (q_ptr->status == QUEST_STATUS_COMPLETED)
1154 			{
1155 				msgf("You can keep it if you like.");
1156 
1157 				/* Allow this quest to be deleted if needed */
1158 				q_ptr->status = QUEST_STATUS_FINISHED;
1159 
1160 				/* Take note */
1161 				if (auto_notes)
1162 				{
1163 					add_note('Q', "Finished quest: %s", q_ptr->name);
1164 				}
1165 			}
1166 			else
1167 			{
1168 				msgf("%s", q_ptr->name);
1169 				msgf("Still looking?");
1170 			}
1171 
1172 			break;
1173 		}
1174 
1175 		case QUEST_TYPE_BOUNTY:
1176 		{
1177 			if (q_ptr->status == QUEST_STATUS_COMPLETED)
1178 			{
1179 				/* Give to player */
1180 				p_ptr->au += q_ptr->reward;
1181 
1182 				/* And tell him */
1183 				msgf("You are given %d gold pieces for your efforts.",
1184 					q_ptr->reward);
1185 
1186 				/* Allow this quest to be deleted if needed */
1187 				q_ptr->status = QUEST_STATUS_FINISHED;
1188 
1189 				/* Take note */
1190 				if (auto_notes)
1191 				{
1192 					add_note('Q', "Finished quest: %s", q_ptr->name);
1193 				}
1194 			}
1195 			else
1196 			{
1197 				/* Remind what the quest is */
1198 				msgf("%s", q_ptr->name);
1199 
1200 				/* If you have killed all but one monster */
1201 				if (q_ptr->data.bnt.max_num - q_ptr->data.bnt.cur_num == 1)
1202 				{
1203 					monster_race *r_ptr = &r_info[q_ptr->data.bnt.r_idx];
1204 
1205 					if (FLAG(r_ptr, RF_MALE))
1206 					{
1207 						/* Male reference */
1208 						msgf("Still looking for him?");
1209 					}
1210 					else if (FLAG(r_ptr, RF_FEMALE))
1211 					{
1212 						/* Female reference */
1213 						msgf("Still looking for her?");
1214 					}
1215 					else
1216 					{
1217 						/* Neuter reference */
1218 						msgf("Still looking for it?");
1219 					}
1220 				}
1221 				else
1222 				{
1223 					/* More than one monster */
1224 					msgf("Still looking for them?");
1225 				}
1226 			}
1227 
1228 			break;
1229 		}
1230 
1231 		case QUEST_TYPE_MESSAGE:
1232 		{
1233 			if (q_ptr->status == QUEST_STATUS_COMPLETED)
1234 			{
1235 				/* Give to player */
1236 				p_ptr->au += q_ptr->reward;
1237 
1238 				msgf("You are given %d gold pieces for your efforts.",
1239 					q_ptr->reward);
1240 
1241 				/* Allow this quest to be deleted if needed */
1242 				q_ptr->status = QUEST_STATUS_FINISHED;
1243 
1244 				/* Take note */
1245 				if (auto_notes)
1246 				{
1247 					add_note('Q', "Finished quest: %s", q_ptr->name);
1248 				}
1249 			}
1250 			else
1251 			{
1252 				/* Tell the player what he was trying */
1253 				msgf("%s", q_ptr->name);
1254 				msgf("Please deliver the message as soon as possible!");
1255 			}
1256 
1257 			break;
1258 		}
1259 
1260 		case QUEST_TYPE_FIND_PLACE:
1261 		{
1262 			if (q_ptr->status == QUEST_STATUS_COMPLETED)
1263 			{
1264 				/* Give to player */
1265 				p_ptr->au += q_ptr->reward;
1266 
1267 				msgf("You are given %d gold pieces for your efforts.",
1268 					q_ptr->reward);
1269 
1270 				/* Break the link between place and quest */
1271 				place[q_ptr->data.fpl.place].quest_num = z_info->q_max;
1272 
1273 				/* Allow this quest to be deleted if needed */
1274 				q_ptr->status = QUEST_STATUS_FINISHED;
1275 
1276 				/* Take note */
1277 				if (auto_notes)
1278 				{
1279 					add_note('Q', "Finished quest: %s", q_ptr->name);
1280 				}
1281 			}
1282 			else
1283 			{
1284 				/* Remind the player what he was doing */
1285 				msgf("%s", q_ptr->name);
1286 				msgf("Please find the ruin as soon as possible!");
1287 			}
1288 
1289 			break;
1290 		}
1291 
1292 
1293 		default:
1294 		{
1295 
1296 		}
1297 	}
1298 
1299 	message_flush();
1300 }
1301 
1302 static const store_type *curr_build;
1303 static int curr_scale;
1304 
1305 /* Save the quest giver (current town + building) */
set_quest_giver(quest_type * q_ptr)1306 static void set_quest_giver(quest_type *q_ptr)
1307 {
1308 	place_type *pl_ptr = &place[p_ptr->place_num];
1309 
1310 	/* Remember quest giver for later */
1311 	q_ptr->place = p_ptr->place_num;
1312 	q_ptr->shop = GET_ARRAY_INDEX(pl_ptr->store, curr_build);
1313 
1314 	/* We know of this quest */
1315 	q_ptr->flags |= QUEST_FLAG_KNOWN;
1316 }
1317 
1318 
insert_artifact_quest(u16b a_idx)1319 static quest_type *insert_artifact_quest(u16b a_idx)
1320 {
1321 	artifact_type *a_ptr = &a_info[a_idx];
1322 
1323 	place_type *pl_ptr;
1324 
1325 	cptr town_name, town_dir;
1326 
1327 	quest_type *q_ptr;
1328 
1329 	int q_num;
1330 
1331 	/* Skip "empty" artifacts */
1332 	if (!a_ptr->name) return (NULL);
1333 
1334 	/* Cannot make an artifact twice */
1335 	if (a_ptr->cur_num) return (NULL);
1336 
1337 	/* No quest items */
1338 	if (FLAG(a_ptr, TR_QUESTITEM)) return (NULL);
1339 
1340 	/* get a new quest */
1341 	q_num = q_pop();
1342 
1343 	/* Paranoia */
1344 	if (!q_num) return (NULL);
1345 
1346 	q_ptr = &quest[q_num];
1347 
1348 	/* Store in information */
1349 	q_ptr->type = QUEST_TYPE_FIND_ITEM;
1350 
1351 	/* We have taken the quest */
1352 	q_ptr->status = QUEST_STATUS_TAKEN;
1353 
1354 	/* We need to place the artifact in the dungeon */
1355 	q_ptr->c_type = QC_DUN_ARTIFACT;
1356 
1357 	/* Finished when the player identifies it */
1358 	q_ptr->x_type = QX_KNOW_ARTIFACT;
1359 
1360 	/* Find an available dungeon to place it in */
1361 
1362 	/* Save the quest data */
1363 	q_ptr->data.fit.a_idx = a_idx;
1364 
1365 	/* Boost the level to make this quest harder. */
1366 	q_ptr->data.fit.place = find_good_dungeon(a_ptr->level * MAX_DEPTH / 100, TRUE);
1367 
1368 	/* Where is it? */
1369 	pl_ptr = &place[q_ptr->data.fit.place];
1370 
1371 	/* Get name of closest town + direction away from it */
1372 	town_name = describe_quest_location(&town_dir, pl_ptr->x, pl_ptr->y, FALSE);
1373 
1374 	/* XXX XXX Create quest name */
1375 	(void)strnfmt(q_ptr->name, 128, "Find the relic %s, which is hidden %s of %s.",
1376 				  a_name + a_ptr->name, town_dir, town_name);
1377 
1378 	/* Artifact is now a quest item */
1379 	SET_FLAG(a_ptr, TR_QUESTITEM);
1380 
1381 	/* Done */
1382 	return (q_ptr);
1383 }
1384 
1385 
1386 
1387 
1388 /* This function returns TRUE if nr1 and nr2 share a divider > 1 */
share_divider(int nr1,int nr2)1389 static bool share_divider(int nr1, int nr2)
1390 {
1391 	int i;
1392 
1393 	/* make sure that nr2 is larger/equal to nr1 */
1394 	if (nr1 > nr2)
1395 	{
1396 		/* swap them */
1397 		i = nr1;
1398 		nr1 = nr2;
1399 		nr2 = i;
1400 	}
1401 
1402 	/* Try all the numbers between 2 and nr1 */
1403 	for (i = 2; i <= nr1; i++)
1404 	{
1405 		/* if i can divide both nr1 and nr2 they share a divider */
1406 		if (!(nr1 % i) && !(nr2 % i)) return (TRUE);
1407 	}
1408 
1409 	/* The greatest common denominator of nr1 and nr2 is 1. */
1410 	return (FALSE);
1411 }
1412 
1413 
1414 /*
1415  * Supply an artifact-idx that has not been found yet.  Do this on a weighted
1416  * basis.  The product of the artifact's depth and level should be lower than
1417  * some random number.  This ensures that low depth/level artifacts are more
1418  * easily chosen
1419  */
find_random_artifact(void)1420 static u16b find_random_artifact(void)
1421 {
1422 	u16b a_idx;
1423 	int min = 999999, max = 0;
1424 	int rand, step;
1425 
1426 	artifact_type *a_ptr;
1427 
1428 	/* Loop through the artifacts */
1429 	for (a_idx = 0; a_idx < z_info->a_max; a_idx++)
1430 	{
1431 		a_ptr = &a_info[a_idx];
1432 
1433 		/* Skip "empty" artifacts */
1434 		if (!a_ptr->name) continue;
1435 
1436 		/* Cannot make an artifact twice */
1437 		if (a_ptr->cur_num) continue;
1438 
1439 		/* No quest items */
1440 		if (FLAG(a_ptr, TR_QUESTITEM)) continue;
1441 
1442 		/* keep track of the lowest level * rarity */
1443 		min = MIN(min, a_ptr->level * a_ptr->rarity);
1444 
1445 		/* keep track of the highest level * rarity */
1446 		max = MAX(max, a_ptr->level * a_ptr->rarity);
1447 	}
1448 
1449 	/* All the artifacts have been found! */
1450 	if (!max) return (0);
1451 
1452 	/* Find the selection condition */
1453 	rand = rand_range(min, max);
1454 
1455 	/*
1456 	 * Select a step to go through the artifact array. That step should not
1457 	 * share a divider with z_info->a_max to ensure that all artifacts are
1458 	 * tried.  (basic group theory)
1459 	 */
1460 	do step = randint(z_info->a_max);
1461 	while (share_divider(step, z_info->a_max));
1462 
1463 	/* Randomly loop through the artifacts */
1464 	do
1465 	{
1466 		/* Finf the next artifact */
1467 		a_idx = (a_idx + step) % z_info->a_max;
1468 
1469 		/* Make a pointer to the artifact */
1470 		a_ptr = &a_info[a_idx];
1471 
1472 		/* Skip "empty" artifacts */
1473 		if (!a_ptr->name) continue;
1474 
1475 		/* Cannot make an artifact twice */
1476 		if (a_ptr->cur_num) continue;
1477 
1478 		/* No quest items */
1479 		if (FLAG(a_ptr, TR_QUESTITEM)) continue;
1480 
1481 		/* deliver this artifact maybe */
1482 		if (a_ptr->level * a_ptr->rarity <= rand) return (a_idx);
1483 	}
1484 	/* No termnation needed because there is a garantee to find an artifact */
1485 	while (TRUE);
1486 }
1487 
request_find_item(int dummy)1488 static bool request_find_item(int dummy)
1489 {
1490 	quest_type *q_ptr;
1491 
1492 	/* Hack - ignore parameter */
1493 	(void) dummy;
1494 
1495 	/* Try to find a artifact to quest for */
1496 	q_ptr = insert_artifact_quest(find_random_artifact());
1497 
1498 	if (!q_ptr)
1499 	{
1500 		msgf("You have found all the relics and still want more?  Impossible.");
1501 
1502 		message_flush();
1503 
1504 		/* No available quests, unfortunately. */
1505 		return (FALSE);
1506 	}
1507 
1508 	/* Display a helpful message. */
1509 	msgf("%s", q_ptr->name);
1510 
1511 	message_flush();
1512 
1513 	/* Remember who gave us the quest */
1514 	set_quest_giver(q_ptr);
1515 
1516 	/* Exit */
1517 	return (TRUE);
1518 }
1519 
1520 
monster_quest(const monster_race * r_ptr)1521 static bool monster_quest(const monster_race *r_ptr)
1522 {
1523 	int i;
1524 
1525 	/* No bounty quests for multiplying monsters */
1526 	if (FLAG(r_ptr, RF_MULTIPLY)) return (FALSE);
1527 
1528 	/* No bounty to kill friendly monsters */
1529 	if (FLAG(r_ptr, RF_FRIENDLY)) return (FALSE);
1530 
1531 	/* Allow silly monsters only if the silly_monster flag is set */
1532 	if (!silly_monsters && FLAG(r_ptr, RF_SILLY)) return (FALSE);
1533 
1534 	/* Only "hard" monsters for quests */
1535 	if (FLAG(r_ptr, RF_NEVER_MOVE) || FLAG(r_ptr, RF_FRIENDS)) return (FALSE);
1536 
1537 	/* No uniques that are already dead */
1538 	if ((FLAG(r_ptr, RF_UNIQUE) || FLAG(r_ptr, RF_UNIQUE_7))
1539 			&& (r_ptr->cur_num >= r_ptr->max_num))
1540 		{
1541 			return (FALSE);
1542 		}
1543 
1544 	/* For all the quests */
1545 	for (i = 0; i < q_max; i++)
1546 	{
1547 		/* If there is already a bounty quest for this monster then give up */
1548 		if (quest[i].type == QUEST_TYPE_BOUNTY &&
1549 			&r_info[quest[i].data.bnt.r_idx] == r_ptr) return (FALSE);
1550 
1551 		/* Don't accidentally quest for Oberon or the Serpent */
1552 		if (quest[i].x_type == QX_KILL_WINNER &&
1553 			&r_info[quest[i].data.dun.r_idx] == r_ptr) return (FALSE);
1554 	}
1555 
1556 	return (TRUE);
1557 }
1558 
1559 
insert_bounty_quest(u16b r_idx,u16b num)1560 static quest_type *insert_bounty_quest(u16b r_idx, u16b num)
1561 {
1562 	quest_type *q_ptr;
1563 
1564 	int q_num;
1565 
1566 	monster_race *r_ptr = &r_info[r_idx];
1567 
1568 	/* get a new quest */
1569 	q_num = q_pop();
1570 
1571 	/* Paranoia */
1572 	if (!q_num) return (NULL);
1573 
1574 	q_ptr = &quest[q_num];
1575 
1576 	/* Bounty quest */
1577 	q_ptr->type = QUEST_TYPE_BOUNTY;
1578 
1579 	/* We have taken the quest */
1580 	q_ptr->status = QUEST_STATUS_TAKEN;
1581 
1582 	if (num != 1)
1583 	{
1584 		char buf[80];
1585 		strcpy(buf, mon_race_name(r_ptr));
1586 		plural_aux(buf);
1587 
1588 		/* XXX XXX Create quest name */
1589 		(void)strnfmt(q_ptr->name, 128, "Kill %d %s.", num, buf);
1590 	}
1591 	else
1592 	{
1593 		/* XXX XXX Create quest name */
1594 		(void)strnfmt(q_ptr->name, 128, "Kill %s.", mon_race_name(r_ptr));
1595 	}
1596 
1597 	/* We need to place the monster(s) when the dungeon is made */
1598 	q_ptr->c_type = QC_DUN_MONST;
1599 
1600 	/* We need to trigger when the monsters are killed */
1601 	if (FLAG(r_ptr, RF_UNIQUE))
1602 	{
1603 		q_ptr->x_type = QX_KILL_UNIQUE;
1604 	}
1605 	else
1606 	{
1607 		q_ptr->x_type = QX_KILL_MONST;
1608 	}
1609 
1610 	/* Save the quest data */
1611 	q_ptr->data.bnt.r_idx = r_idx;
1612 	q_ptr->data.bnt.cur_num = 0;
1613 	q_ptr->data.bnt.max_num = num;
1614 	q_ptr->reward = r_ptr->level * r_ptr->level * num * 2;
1615 
1616 	/* bonus reward for uniques */
1617 	if (num == 1) q_ptr->reward *= 10;
1618 
1619 	/* Done */
1620 	return (q_ptr);
1621 }
1622 
request_bounty(int dummy)1623 static bool request_bounty(int dummy)
1624 {
1625 	int i;
1626 
1627 	u16b num;
1628 	u16b best_r_idx = 1;
1629 	int best_level = 1;
1630 
1631 	int r_idx;
1632 	monster_race *r_ptr;
1633 
1634 	quest_type *q_ptr;
1635 
1636 	/* Hack - ignore parameter */
1637 	(void) dummy;
1638 
1639 	/* Get monster */
1640 	for (i = 0; i < MAX_TRIES; i++)
1641 	{
1642 		/*
1643 		 * Random monster out of depth
1644 		 * (depending on level + number of quests)
1645 		 */
1646 		r_idx = get_mon_num(p_ptr->max_lev + curr_scale);
1647 
1648 		r_ptr = &r_info[r_idx];
1649 
1650 		/* Look at the monster - only "hard" monsters for quests */
1651 		if (!monster_quest(r_ptr)) continue;
1652 
1653 		/* Save the index if the monster is deeper than current monster */
1654 		if (!best_r_idx || (r_info[r_idx].level > best_level))
1655 		{
1656 			best_r_idx = r_idx;
1657 			best_level = r_info[r_idx].level;
1658 		}
1659 
1660 		/* Accept monsters that are a few levels out of depth */
1661 		if (best_level > p_ptr->max_lev * 2) break;
1662 	}
1663 
1664 	r_ptr = &r_info[best_r_idx];
1665 
1666 	/* Get the number of monsters */
1667 	if (FLAG(r_ptr, RF_UNIQUE))
1668 	{
1669 		num = 1;
1670 	}
1671 	else if (FLAG(r_ptr, RF_UNIQUE_7))
1672 	{
1673 		num = randint1(r_ptr->max_num - r_ptr->cur_num);
1674 	}
1675 	else
1676 	{
1677 		num = 5 + randint0(p_ptr->max_lev / 2) / r_ptr->rarity;
1678 	}
1679 
1680 	/* Generate the quest */
1681 	q_ptr = insert_bounty_quest(best_r_idx, num);
1682 
1683 	if (!q_ptr)
1684 	{
1685 		msgf("Sorry, I don't need any bounties today.");
1686 
1687 		message_flush();
1688 
1689 		/* No available quests, unfortunately. */
1690 		return (FALSE);
1691 	}
1692 
1693 	/* Show it on the screen? */
1694 
1695 
1696 	/* Display a helpful message. */
1697 	msgf("%s", q_ptr->name);
1698 
1699 	message_flush();
1700 
1701 	/* Remember who gave us the quest */
1702 	set_quest_giver(q_ptr);
1703 
1704 	/* Exit */
1705 	return (TRUE);
1706 }
1707 
1708 
insert_message_quest(int dist)1709 static quest_type *insert_message_quest(int dist)
1710 {
1711 	place_type *pl_ptr;
1712 
1713 	quest_type *q_ptr;
1714 	store_type *st_ptr;
1715 
1716 	int store;
1717 	u16b place_num;
1718 
1719 	/* Get a new quest */
1720 	int q_num = q_pop();
1721 
1722 	/* Paranoia */
1723 	if (!q_num) return (NULL);
1724 
1725 	q_ptr = &quest[q_num];
1726 
1727 	/* Store in information */
1728 	q_ptr->type = QUEST_TYPE_MESSAGE;
1729 
1730 	/* We have taken the quest */
1731 	q_ptr->status = QUEST_STATUS_TAKEN;
1732 
1733 	/* We don't need any special creation operation */
1734 	q_ptr->c_type = QC_NONE;
1735 
1736 	/* Finished when the player finds it */
1737 	q_ptr->x_type = QX_FIND_SHOP;
1738 
1739 	/* Find a town that is roughly dist wilderness blocks away */
1740 	place_num = find_good_town(&dist);
1741 
1742 	/* Get the place */
1743 	pl_ptr = &place[place_num];
1744 
1745 	/* Find a store at that town */
1746 	do
1747 	{
1748 		store = randint0(pl_ptr->numstores);
1749 
1750 		st_ptr = &pl_ptr->store[store];
1751 
1752 		/* Want a store with an owner */
1753 	}
1754 	while (st_ptr->type == BUILD_NONE ||
1755 		   st_ptr->type == BUILD_STAIRS ||
1756 		   st_ptr->type == BUILD_BLANK ||
1757 		   st_ptr->type == BUILD_STORE_HOME);
1758 
1759 	/* XXX XXX Create quest name */
1760 	(void)strnfmt(q_ptr->name, 128, "Carry a message to %s in %s.",
1761 					quark_str(st_ptr->owner_name),
1762 					pl_ptr->name);
1763 
1764 
1765 	/* Save the quest data */
1766 	q_ptr->data.msg.shop = store;
1767 	q_ptr->data.msg.place = place_num;
1768 
1769 	/* Set the reward level */
1770 	q_ptr->reward = dist * 100;
1771 
1772 	/* Done */
1773 	return (q_ptr);
1774 }
1775 
request_message(int dummy)1776 static bool request_message(int dummy)
1777 {
1778 	quest_type *q_ptr;
1779 
1780 	/* Hack - ignore parameter */
1781 	(void) dummy;
1782 
1783 	/*
1784 	 * Generate a quest to send a message to a town
1785 	 * roughly 20 to 50 wilderness squares away.
1786 	 */
1787 	q_ptr = insert_message_quest(curr_scale * 2);
1788 
1789 	if (!q_ptr)
1790 	{
1791 		msgf("Sorry, I don't need any to send any messages today.");
1792 
1793 		message_flush();
1794 
1795 		/* No available quests, unfortunately. */
1796 		return (FALSE);
1797 	}
1798 
1799 	/* Show it on the screen? */
1800 
1801 	/* Display a helpful message. */
1802 	msgf("%s", q_ptr->name);
1803 
1804 	message_flush();
1805 
1806 	/* Remember who gave us the quest */
1807 	set_quest_giver(q_ptr);
1808 
1809 	/* Exit */
1810 	return (TRUE);
1811 }
1812 
insert_find_place_quest(void)1813 static quest_type *insert_find_place_quest(void)
1814 {
1815 	place_type *pl_ptr;
1816 
1817 	quest_type *q_ptr;
1818 
1819 	cptr town_name, town_dir;
1820 
1821 	u16b place_num;
1822 
1823 	int q_num;
1824 
1825 	/* Find a dungeon appropriate for this player */
1826 	place_num = find_good_dungeon(2 * p_ptr->lev, FALSE);
1827 
1828 	/* All dungeons have been found */
1829 	if (!place_num)	return (NULL);
1830 
1831 	/* Get a new quest */
1832 	q_num = q_pop();
1833 
1834 	/* Paranoia */
1835 	if (!q_num) return (NULL);
1836 
1837 	q_ptr = &quest[q_num];
1838 
1839 	/* Store in information */
1840 	q_ptr->type = QUEST_TYPE_FIND_PLACE;
1841 
1842 	/* We have taken the quest */
1843 	q_ptr->status = QUEST_STATUS_TAKEN;
1844 
1845 	/* We don't need any special creation operation */
1846 	q_ptr->c_type = QC_NONE;
1847 
1848 	/* Finished when the player finds it */
1849 	q_ptr->x_type = QX_WILD_ENTER;
1850 
1851 	/* Get the place */
1852 	pl_ptr = &place[place_num];
1853 
1854 	pl_ptr->quest_num = q_num;
1855 
1856 	/* Get name of closest town + direction away from it */
1857 	town_name = describe_quest_location(&town_dir, pl_ptr->x, pl_ptr->y, FALSE);
1858 
1859 	/* XXX XXX Create quest name */
1860 	(void)strnfmt(q_ptr->name, 128, "Find a certain lost ruin, which is hidden %s of %s.",
1861 				  town_dir, town_name);
1862 
1863 	q_ptr->data.fpl.place = place_num;
1864 
1865 	/* Set the reward level */
1866 	q_ptr->reward = 100 * distance(pl_ptr->x, pl_ptr->y, p_ptr->wilderness_x / 16,
1867 					p_ptr->wilderness_y / 16);
1868 
1869 	/* Done */
1870 	return (q_ptr);
1871 }
1872 
request_find_place(int dummy)1873 static bool request_find_place(int dummy)
1874 {
1875 	quest_type *q_ptr;
1876 
1877 	/* Hack - ignore parameter */
1878 	(void) dummy;
1879 
1880 	/*Generate a quest to find an unknown dungeon */
1881 	q_ptr = insert_find_place_quest();
1882 
1883 	if (!q_ptr)
1884 	{
1885 		msgf("You've found all the ruins that I know of.");
1886 
1887 		message_flush();
1888 
1889 		/* No available quests, unfortunately. */
1890 		return (FALSE);
1891 	}
1892 
1893 	/* Show it on the screen? */
1894 
1895 	/* Display a helpful message. */
1896 	msgf("%s", q_ptr->name);
1897 
1898 	message_flush();
1899 
1900 	/* Remember who gave us the quest */
1901 	set_quest_giver(q_ptr);
1902 
1903 	/* Exit */
1904 	return (TRUE);
1905 }
1906 
1907 
1908 
1909 #define QUEST_MENU_MAX		7
1910 #define QUEST_MENU_RELIC	3
1911 
1912 /* The quest selection menu */
1913 static menu_type quest_menu[QUEST_MENU_MAX] =
1914 {
1915 	{"To hunt down a bounty of monsters", NULL, request_bounty, MN_ACTIVE},
1916 	{"To send a message to someone far away", NULL, request_message, MN_ACTIVE},
1917 	{"To find a lost ruin", NULL, request_find_place, MN_ACTIVE},
1918 	MENU_END,
1919 	MENU_END,
1920 	{"To find a lost relic", NULL, request_find_item, MN_ACTIVE},
1921 	MENU_END
1922 };
1923 
request_quest(const store_type * b_ptr,int scale)1924 void request_quest(const store_type *b_ptr, int scale)
1925 {
1926 	/* Save building so we can remember the quest giver */
1927 	curr_build = b_ptr;
1928 
1929 	/* Save scale so we can work out how hard to make the quest */
1930 	curr_scale = scale;
1931 
1932 	/* Only allow artifact quests from large castles */
1933 	if (scale >= 20)
1934 	{
1935 		/* Copy this quest into the menu */
1936 		quest_menu[QUEST_MENU_RELIC] = quest_menu[QUEST_MENU_RELIC + 2];
1937 	}
1938 
1939 	display_menu(quest_menu, -1, FALSE, NULL, "What type of quest would you like?");
1940 
1941 
1942 	/* Remove the artifact quest from the menu */
1943 	quest_menu[QUEST_MENU_RELIC] = quest_menu[QUEST_MENU_RELIC + 1];
1944 }
1945 
1946 /* Show the quest status as a string */
quest_status_string(quest_type * q_ptr)1947 static cptr quest_status_string(quest_type *q_ptr)
1948 {
1949 	int monst_num = 0;
1950 	int max_num = 0;
1951 
1952 	/* Just checking */
1953 	if (!q_ptr) return (NULL);
1954 
1955 	/* Surely you jest? */
1956 	if (q_ptr->type == QUEST_TYPE_NONE) return (NULL);
1957 
1958 	/* Check the various statuses */
1959 	switch (q_ptr->status)
1960 	{
1961 		/* Unknown quest */
1962 		case QUEST_STATUS_UNTAKEN: return (NULL);
1963 
1964 		/* Underway */
1965 		case QUEST_STATUS_TAKEN:
1966 		{
1967 			/* Count the bounty monsters */
1968 			if (q_ptr->type == QUEST_TYPE_BOUNTY)
1969 			{
1970 				monst_num = q_ptr->data.bnt.cur_num;
1971 				max_num = q_ptr->data.bnt.max_num;
1972 			}
1973 
1974 			/* Count the dungeon monsters */
1975 			if (q_ptr->type == QUEST_TYPE_DUNGEON)
1976 			{
1977 				monst_num = q_ptr->data.dun.cur_num;
1978 				max_num = q_ptr->data.dun.max_num;
1979 			}
1980 
1981 			/* Don't show the count for zero or one monsters */
1982 			if (max_num <= 1) return ("\n\n");
1983 
1984 			/* Tell the world */
1985 			return (format("You have killed %d.\n\n", monst_num));
1986 		}
1987 
1988 		/* Report back to quest_giver */
1989 		case QUEST_STATUS_COMPLETED:
1990 		{
1991 			/* All done killing */
1992 			if (q_ptr->type == QUEST_TYPE_BOUNTY) return ("(Killed)\n\n");
1993 
1994 			/* All done killing */
1995 			if (q_ptr->type == QUEST_TYPE_DUNGEON) return ("(Killed)\n\n");
1996 
1997 			/* All done defeating */
1998 			if (q_ptr->type == QUEST_TYPE_WILD) return ("(Completed)\n");
1999 
2000 			/* All done delivering */
2001 			if (q_ptr->type == QUEST_TYPE_MESSAGE) return ("(Delivered)\n\n");
2002 
2003 			/* All done finding */
2004 			if (q_ptr->type == QUEST_TYPE_FIND_ITEM) return ("(Found)\n\n");
2005 
2006 			/* All done finding */
2007 			if (q_ptr->type == QUEST_TYPE_FIND_PLACE) return ("(Found)\n\n");
2008 		}
2009 
2010 		/* Finnished */
2011 		case QUEST_STATUS_FINISHED: return ("(Completed)\n");
2012 
2013 		default: return ("(BUG!)\n");
2014 	}
2015 }
2016 
2017 
2018 /*
2019  * Print quest status of all active quests
2020  */
do_cmd_knowledge_quests(int dummy)2021 bool do_cmd_knowledge_quests(int dummy)
2022 {
2023 	FILE *fff;
2024 	char file_name[1024];
2025 	char tmp_str[256];
2026 
2027 	quest_type *q_ptr;
2028 	int i;
2029 
2030 	/* Hack - ignore parameter */
2031 	(void) dummy;
2032 
2033 	/* Open a temporary file */
2034 	fff = my_fopen_temp(file_name, 1024);
2035 
2036 	/* Failure */
2037 	if (!fff) return (FALSE);
2038 
2039 	for (i = 0; i < q_max; i++)
2040 	{
2041 		q_ptr = &quest[i];
2042 
2043 		/* Do we know about it? */
2044 		if (!(q_ptr->flags & QUEST_FLAG_KNOWN)) continue;
2045 
2046 		/* See what type of quest it is */
2047 		switch (q_ptr->type)
2048 		{
2049 			case QUEST_TYPE_NONE:
2050 			{
2051 				/* Paranoia */
2052 				continue;
2053 			}
2054 
2055 			case QUEST_TYPE_DUNGEON:
2056 			{
2057 				char level[20];
2058 
2059 				/* In feet, or in levels */
2060 				if (depth_in_feet)
2061 				{
2062 					strnfmt(level, 20, "%4dft",
2063 							(int)q_ptr->data.dun.level * 50);
2064 				}
2065 				else
2066 				{
2067 					strnfmt(level, 20, "%3d", (int)q_ptr->data.dun.level);
2068 				}
2069 
2070 				/* Hack - assume kill n monsters of type m */
2071 				strnfmt(tmp_str, 256,
2072 						"%s (Dungeon level: %s)  %s",
2073 						q_ptr->name, level,
2074 						quest_status_string(q_ptr));
2075 
2076 				break;
2077 			}
2078 
2079 			case QUEST_TYPE_BOUNTY:
2080 			case QUEST_TYPE_MESSAGE:
2081 			case QUEST_TYPE_FIND_ITEM:
2082 			case QUEST_TYPE_FIND_PLACE:
2083 			case QUEST_TYPE_WILD:
2084 			{
2085 				/* Hack - this is simple */
2086 				strnfmt(tmp_str, 256, "%s  %s",
2087 					q_ptr->name, quest_status_string(q_ptr));
2088 
2089 				break;
2090 			}
2091 
2092 			default:
2093 			{
2094 				/* Paranoia */
2095 				strnfmt(tmp_str, 256, "Invalid quest type!");
2096 			}
2097 		}
2098 
2099 		/* Copy to the file */
2100 		froff(fff, "%s", tmp_str);
2101 	}
2102 
2103 	/* Close the file */
2104 	my_fclose(fff);
2105 
2106 	/* Display the file contents */
2107 	(void)show_file(file_name, "Quest status", 0, 0);
2108 
2109 	/* Remove the file */
2110 	(void)fd_kill(file_name);
2111 
2112 	return (FALSE);
2113 }
2114 
2115 
2116 /* Dump the quests related to this town into fff, only when display is set */
dump_castle_info(FILE * fff,int town)2117 void dump_castle_info(FILE *fff, int town)
2118 {
2119 	int i;
2120 	bool quest_in_town = FALSE;
2121 
2122 	quest_type *q_ptr;
2123 
2124 	/* Loop through the quests */
2125 	for (i = 0; i < z_info->q_max; i++)
2126 	{
2127 		/* Find a quest */
2128 		q_ptr = &quest[i];
2129 
2130 		/* Is it from this town? */
2131 		if (town != q_ptr->place) continue;
2132 
2133 		/* There is a quest */
2134 		quest_in_town = TRUE;
2135 
2136 		/* Show it */
2137 		froff(fff, "%s  %s", q_ptr->name, quest_status_string(q_ptr));
2138 	}
2139 
2140 	/* If no quest was issued  */
2141 	if (!quest_in_town)
2142 	{
2143 		/* Say so */
2144 		froff(fff, "No quest was issued in this town.\n");
2145 	}
2146 }
2147 
2148 /*
2149  * The following functions are used to determine if the given monster
2150  * is appropriate for inclusion in a quest of the given type.
2151  *
2152  * The general selections are not allowed to include "unique" monsters.
2153  */
2154 
2155 
2156 /*
2157  * Hack - Monster validation macro
2158  *
2159  * Line 1 -- forbid town monsters
2160  * Line 2 -- forbid uniques
2161  * Line 3 -- forbid aquatic monsters
2162  */
2163 #define quest_monster_okay(I) \
2164 	(!FLAG(&r_info[I], RF_WILD_TOWN) && \
2165 	 !FLAG(&r_info[I], RF_UNIQUE) && \
2166 	 !FLAG(&r_info[I], RF_AQUATIC))
2167 
2168 
2169 #ifdef UNUSED_FUNC
2170 /*
2171  * Helper monster selection function
2172  */
quest_aux_simple(int r_idx)2173 static bool quest_aux_simple(int r_idx)
2174 {
2175 	/* Okay */
2176 	return (quest_monster_okay(r_idx));
2177 }
2178 
2179 #endif /* UNUSED_FUNC */
2180 
2181 
2182 /*
2183  * Helper function for selecting undead
2184  */
quest_aux_undead(int r_idx)2185 static bool quest_aux_undead(int r_idx)
2186 {
2187 	monster_race *r_ptr = &r_info[r_idx];
2188 
2189 	/* Validate the monster */
2190 	if (!quest_monster_okay(r_idx)) return (FALSE);
2191 
2192 	/* Require Undead */
2193 	if (!FLAG(r_ptr, RF_UNDEAD)) return (FALSE);
2194 
2195 	/* Okay */
2196 	return (TRUE);
2197 }
2198 
2199 
2200 /*
2201  * Helper function for selecting orcs
2202  */
quest_aux_orc(int r_idx)2203 static bool quest_aux_orc(int r_idx)
2204 {
2205 	monster_race *r_ptr = &r_info[r_idx];
2206 
2207 	/* Validate the monster */
2208 	if (!quest_monster_okay(r_idx)) return (FALSE);
2209 
2210 	/* Require orc */
2211 	if (!(FLAG(r_ptr, RF_ORC))) return (FALSE);
2212 
2213 	/* Decline undead */
2214 	if (FLAG(r_ptr, RF_UNDEAD)) return (FALSE);
2215 
2216 	/* Okay */
2217 	return (TRUE);
2218 }
2219 
2220 
2221 /*
2222  * Helper function for selecting trolls
2223  */
quest_aux_troll(int r_idx)2224 static bool quest_aux_troll(int r_idx)
2225 {
2226 	monster_race *r_ptr = &r_info[r_idx];
2227 
2228 	/* Validate the monster */
2229 	if (!quest_monster_okay(r_idx)) return (FALSE);
2230 
2231 	/* Require troll */
2232 	if (!(FLAG(r_ptr, RF_TROLL))) return (FALSE);
2233 
2234 	/* Decline undead */
2235 	if (FLAG(r_ptr, RF_UNDEAD)) return (FALSE);
2236 
2237 	/* Okay */
2238 	return (TRUE);
2239 }
2240 
2241 
2242 /*
2243  * Helper function for selecting giants
2244  */
quest_aux_giant(int r_idx)2245 static bool quest_aux_giant(int r_idx)
2246 {
2247 	monster_race *r_ptr = &r_info[r_idx];
2248 
2249 	/* Validate the monster */
2250 	if (!quest_monster_okay(r_idx)) return (FALSE);
2251 
2252 	/* Require giant */
2253 	if (!(FLAG(r_ptr, RF_GIANT))) return (FALSE);
2254 
2255 	/* Decline undead */
2256 	if (FLAG(r_ptr, RF_UNDEAD)) return (FALSE);
2257 
2258 	/* Okay */
2259 	return (TRUE);
2260 }
2261 
2262 
2263 /*
2264  * Helper function for selecting dragons
2265  */
quest_aux_dragon(int r_idx)2266 static bool quest_aux_dragon(int r_idx)
2267 {
2268 	monster_race *r_ptr = &r_info[r_idx];
2269 
2270 	/* Validate the monster */
2271 	if (!quest_monster_okay(r_idx)) return (FALSE);
2272 
2273 	/* Require dragon */
2274 	if (!(FLAG(r_ptr, RF_DRAGON))) return (FALSE);
2275 
2276 	/* Decline undead */
2277 	if (FLAG(r_ptr, RF_UNDEAD)) return (FALSE);
2278 
2279 	/* Okay */
2280 	return (TRUE);
2281 }
2282 
pick_quest_type(quest_aux_type * l_ptr,int level)2283 static int pick_quest_type(quest_aux_type *l_ptr, int level)
2284 {
2285 	int tmp, total;
2286 
2287 	quest_aux_type *n_ptr;
2288 
2289 	int i;
2290 
2291 	/* Calculate the total possibilities */
2292 	for (i = 0, total = 0; TRUE; i++)
2293 	{
2294 		n_ptr = &l_ptr[i];
2295 
2296 		/* Note end */
2297 		if (!n_ptr->hook_func) break;
2298 
2299 		/* Ignore excessive depth */
2300 		if (n_ptr->level > level) continue;
2301 
2302 		/* Count this possibility */
2303 		total += n_ptr->chance * MAX_DEPTH / (level - n_ptr->level + 5);
2304 	}
2305 
2306 	if (!total) return (-1);
2307 
2308 	/* Pick a random type */
2309 	tmp = randint0(total);
2310 
2311 	/* Find this type */
2312 	for (i = 0, total = 0; TRUE; i++)
2313 	{
2314 		n_ptr = &l_ptr[i];
2315 
2316 		/* Note end */
2317 		if (!n_ptr->hook_func) break;
2318 
2319 		/* Ignore excessive depth */
2320 		if (n_ptr->level > level) continue;
2321 
2322 		/* Count this possibility */
2323 		total += n_ptr->chance * MAX_DEPTH / (level - n_ptr->level + 5);
2324 
2325 		/* Found the type */
2326 		if (tmp < total) break;
2327 	}
2328 
2329 	return (i);
2330 }
2331 
2332 
2333 
2334 static quest_aux_type camp_types[] =
2335 {
2336 	{quest_aux_undead, 10, 1, "undead"},
2337 	{quest_aux_orc, 20, 2, "orc"},
2338 	{quest_aux_troll, 40, 4, "troll"},
2339 	{quest_aux_giant, 60, 1, "giant"},
2340 	{quest_aux_dragon, 80, 1, "dragon"},
2341 	{NULL, 0, 0, NULL},
2342 };
2343 
2344 
2345 
2346 /*
2347  * Pick a quest to use
2348  */
pick_wild_quest(int * xsize,int * ysize,byte * flags)2349 void pick_wild_quest(int *xsize, int *ysize, byte *flags)
2350 {
2351 	/* Hack - don't worry too much now, we only have one type of quest */
2352 
2353 	/* Random size */
2354 	*xsize = randint1(5);
2355 	*ysize = randint1(5);
2356 
2357 	/* On normal terrain */
2358 	*flags = Q_GEN_PICKY;
2359 }
2360 
2361 /*
2362  * Look to see if a wilderness block is able to have
2363  * a quest overlayed on top.
2364  */
quest_blank(int x,int y,int xsize,int ysize,int place_num,byte flags)2365 bool quest_blank(int x, int y, int xsize, int ysize, int place_num, byte flags)
2366 {
2367 	int i, j;
2368 	wild_gen2_type *w_ptr;
2369 	place_type *pl_ptr = &place[place_num];
2370 
2371 	/* Hack - Population check */
2372 	if (randint0(256) > wild[y][x].trans.pop_map) return (FALSE);
2373 
2374 	for (i = x - 1; i < x + xsize + 2; i++)
2375 	{
2376 		for (j = y - 1; j < y + ysize + 2; j++)
2377 		{
2378 			/* Hack - Not next to boundary */
2379 			if ((i <= 0) || (i >= max_wild - 1) ||
2380 				(j <= 0) || (j >= max_wild - 1))
2381 			{
2382 				return (FALSE);
2383 			}
2384 
2385 			w_ptr = &wild[j][i].trans;
2386 
2387 			/* No place already */
2388 			if (w_ptr->place) return (FALSE);
2389 
2390 			/* Picky quests require "normal terrain" */
2391 			if (flags & Q_GEN_PICKY)
2392 			{
2393 				/* No water or lava or acid */
2394 				if (w_ptr->
2395 					info & (WILD_INFO_WATER | WILD_INFO_LAVA | WILD_INFO_ACID))
2396 				{
2397 					return (FALSE);
2398 				}
2399 			}
2400 
2401 			/* Ocean quests must be on water */
2402 			if (flags & Q_GEN_OCEAN)
2403 			{
2404 				/* Not on Ocean? */
2405 				if (w_ptr->hgt_map >= (256 / SEA_FRACTION)) return (FALSE);
2406 			}
2407 			else
2408 			{
2409 				/* Otherwise, Ocean is not allowed */
2410 				if (w_ptr->hgt_map < (256 / SEA_FRACTION)) return (FALSE);
2411 			}
2412 		}
2413 	}
2414 
2415 	/* Look to see if another town / quest is too close */
2416 	for (i = 1; i < place_num; i++)
2417 	{
2418 		if (distance(place[i].x, place[i].y, x, y) < MIN_DIST_QUEST)
2419 		{
2420 			/* Too close? */
2421 			return (FALSE);
2422 		}
2423 	}
2424 
2425 	/* Save size */
2426 	pl_ptr->xsize = xsize;
2427 	pl_ptr->ysize = ysize;
2428 
2429 	/* Ok then */
2430 	return (TRUE);
2431 }
2432 
2433 
2434 /*
2435  * Create a quest in the wilderness
2436  */
create_quest(int x,int y,int place_num)2437 bool create_quest(int x, int y, int place_num)
2438 {
2439 	int i, j;
2440 	int q_num, qtype;
2441 	cptr town_name, town_dir;
2442 
2443 	wild_type *w_ptr = &wild[y][x];
2444 
2445 	place_type *pl_ptr = &place[place_num];
2446 
2447 	quest_type *q_ptr;
2448 
2449 	/* Select type of monster to place in the camp */
2450 	qtype = pick_quest_type(camp_types, (255 - w_ptr->trans.law_map) / 3);
2451 
2452 	/* Is the area too easy for the quests? */
2453 	if (qtype == -1) return (FALSE);
2454 
2455 	/* Get a new quest */
2456 	q_num = q_pop();
2457 
2458 	/* Paranoia */
2459 	if (!q_num) return (FALSE);
2460 
2461 	/* Get a random seed for later */
2462 	pl_ptr->seed = randint0(0x10000000);
2463 
2464 	/* Quest */
2465 	pl_ptr->type = TOWN_QUEST;
2466 	pl_ptr->monst_type = TOWN_MONST_MONST;
2467 	pl_ptr->x = x;
2468 	pl_ptr->y = y;
2469 	pl_ptr->quest_num = q_num;
2470 
2471 	/* Data value is used as a counter of "active" blocks */
2472 	pl_ptr->data = 0;
2473 
2474 	if ((!pl_ptr->xsize) || (!pl_ptr->ysize)) quit("Zero quest size");
2475 
2476 	/* Link wilderness to quest */
2477 	for (i = 0; i < pl_ptr->xsize; i++)
2478 	{
2479 		for (j = 0; j < pl_ptr->ysize; j++)
2480 		{
2481 			w_ptr = &wild[y + j][x + i];
2482 
2483 			/*
2484 			 * Add quest to wilderness
2485 			 * Note: only 255 can be stored currently.
2486 			 */
2487 			w_ptr->trans.place = (byte)place_num;
2488 
2489 			/* Increment "active block" counter */
2490 			pl_ptr->data++;
2491 		}
2492 	}
2493 
2494 	/* Set up quest */
2495 	q_ptr = &quest[q_num];
2496 
2497 	/* Store in information */
2498 	q_ptr->type = QUEST_TYPE_WILD;
2499 
2500 	/* We don't need a special generator */
2501 	q_ptr->c_type = QC_NONE;
2502 
2503 	/* We need to trigger when the player enters the wilderness block */
2504 	q_ptr->x_type = QX_WILD_ENTER;
2505 
2506 	/* Get name and direction of closest town to quest */
2507 	town_name = describe_quest_location(&town_dir, pl_ptr->x, pl_ptr->y, FALSE);
2508 
2509 	/* Create quest name */
2510 	(void)strnfmt(q_ptr->name, 128, "Defeat the %s camp %s of %s.",
2511 				  camp_types[qtype].name, town_dir, town_name);
2512 
2513 	/* Save the quest data */
2514 	q_ptr->data.wld.place = place_num;
2515 	q_ptr->data.wld.data = qtype;
2516 	/* q_ptr->data.wld.depth = (255 - w_ptr->trans.law_map) / 3; */
2517 
2518 	return (TRUE);
2519 }
2520 
2521 
2522 /*
2523  * Draw the quest onto its region
2524  */
draw_quest(place_type * pl_ptr)2525 void draw_quest(place_type *pl_ptr)
2526 {
2527 	int x, y, n;
2528 	int i, j;
2529 
2530 	wild_type *w_ptr = &wild[pl_ptr->y][pl_ptr->x];
2531 
2532 	quest_type *q_ptr = &quest[pl_ptr->quest_num];
2533 
2534 	cave_type *c_ptr;
2535 
2536 	/* Object theme */
2537 	obj_theme theme;
2538 
2539 	int depth = w_ptr->done.mon_gen;
2540 
2541 	/* Paranoia */
2542 	if (pl_ptr->region) quit("Quest already has region during creation.");
2543 
2544 	/* Get region */
2545 	create_region(pl_ptr, pl_ptr->xsize * WILD_BLOCK_SIZE,
2546 						 pl_ptr->ysize * WILD_BLOCK_SIZE,
2547 						 REGION_OVER);
2548 
2549 	/* Hack - do not increment refcount here - let allocate_block do that */
2550 
2551 	/* Hack -- Use the "simple" RNG */
2552 	Rand_quick = TRUE;
2553 
2554 	/* Hack -- Induce consistant quest layout */
2555 	Rand_value = pl_ptr->seed;
2556 
2557 	/* Apply the monster restriction */
2558 	get_mon_num_prep(camp_types[q_ptr->data.wld.data].hook_func);
2559 
2560 	/* Set theme for weapons / armour */
2561 	theme.treasure = 0;
2562 	theme.combat = 100;
2563 	theme.magic = 0;
2564 	theme.tools = 0;
2565 
2566 	init_match_theme(theme);
2567 
2568 	/* Prepare allocation table */
2569 	get_obj_num_prep(kind_is_theme);
2570 
2571 	/* Pick number random spots within region */
2572 	n = (pl_ptr->xsize * pl_ptr->ysize) / 4;
2573 
2574 	while (n != 0)
2575 	{
2576 		/* Decrement counter */
2577 		n--;
2578 
2579 		/* Get spot */
2580 		x = randint0(pl_ptr->xsize * 2);
2581 		y = randint0(pl_ptr->ysize * 2);
2582 
2583 		/* Place ground */
2584 		for (i = 0; i < 8; i++)
2585 		{
2586 			for (j = 0; j < 8; j++)
2587 			{
2588 				/* Get location */
2589 				c_ptr = cave_p(x * 8 + i, y * 8 + j);
2590 
2591 				/* Draw a roughly circular blob */
2592 				if (randint0(distance(0, 0, i, j)) < 4)
2593 				{
2594 					if (one_in_(3))
2595 					{
2596 						c_ptr->feat = FEAT_PEBBLES;
2597 					}
2598 					else
2599 					{
2600 						c_ptr->feat = FEAT_DIRT;
2601 					}
2602 
2603 					/* Place monsters on spots */
2604 					if (one_in_(QUEST_CAMP_MON))
2605 					{
2606 						/* Pick a race to clone */
2607 						c_ptr->m_idx = get_mon_num(depth);
2608 					}
2609 
2610 					/* Place weapons + armour around the spots */
2611 					if (one_in_(QUEST_CAMP_OBJ))
2612 					{
2613 						c_ptr->o_idx = get_obj_num(depth, depth / 3);
2614 					}
2615 				}
2616 			}
2617 		}
2618 	}
2619 
2620 
2621 	/* Set theme for junk */
2622 	theme.treasure = 5;
2623 	theme.combat = 0;
2624 	theme.magic = 0;
2625 	theme.tools = 5;
2626 
2627 	init_match_theme(theme);
2628 
2629 	/* Clear allocation table */
2630 	get_obj_num_prep(kind_is_theme);
2631 
2632 	/* Scatter stuff over the region */
2633 	for (i = 0; i < pl_ptr->xsize * WILD_BLOCK_SIZE; i++)
2634 	{
2635 		for (j = 0; j < pl_ptr->ysize * WILD_BLOCK_SIZE; j++)
2636 		{
2637 			/* Only on some squares */
2638 			if (!one_in_(QUEST_CAMP_SCATTER)) continue;
2639 
2640 			/* Get location */
2641 			c_ptr = cave_p(i, j);
2642 
2643 			/* Not on allocated squares */
2644 			if (c_ptr->feat) continue;
2645 
2646 			if (one_in_(3))
2647 			{
2648 				c_ptr->feat = FEAT_PEBBLES;
2649 			}
2650 			else
2651 			{
2652 				c_ptr->feat = FEAT_DIRT;
2653 			}
2654 
2655 			/* Place monsters on spots */
2656 			if (one_in_(QUEST_CAMP_MON))
2657 			{
2658 				/* Pick a race to clone */
2659 				c_ptr->m_idx = get_mon_num(depth);
2660 
2661 				/* Place junk under monsters */
2662 				if (one_in_(QUEST_CAMP_OBJ))
2663 				{
2664 					c_ptr->o_idx = get_obj_num(depth, 0);
2665 				}
2666 			}
2667 		}
2668 	}
2669 
2670 	/* Activate quest + we know about the quest */
2671 	q_ptr->flags |= (QUEST_FLAG_ACTIVE);
2672 
2673 	/* Hack - we now take this quest */
2674 	if (q_ptr->status == QUEST_STATUS_UNTAKEN)
2675 	{
2676 		q_ptr->status = QUEST_STATUS_TAKEN;
2677 	}
2678 
2679 	/* Mega-hack Give a message if we "discover" it */
2680 	quest_discovery();
2681 
2682 	/* We know about it now */
2683 	q_ptr->flags |= QUEST_FLAG_KNOWN;
2684 
2685 	/* Hack XXX XXX (No quest-giving store yet) */
2686 
2687 	/* Hack -- use the "complex" RNG */
2688 	Rand_quick = FALSE;
2689 
2690 	/* Remove the monster restriction */
2691 	get_mon_num_prep(NULL);
2692 }
2693