summaryrefslogtreecommitdiff
path: root/src/map/quest.c
blob: 7a216095e45a4d85e9f4763e5d4c212855b75db1 (plain) (blame)
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
/**
 * This file is part of Hercules.
 * http://herc.ws - http://github.com/HerculesWS/Hercules
 *
 * Copyright (C) 2012-2018  Hercules Dev Team
 * Copyright (C)  Athena Dev Teams
 *
 * Hercules is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#define HERCULES_CORE

#include "quest.h"

#include "map/battle.h"
#include "map/chrif.h"
#include "map/clif.h"
#include "map/homunculus.h"
#include "map/intif.h"
#include "map/itemdb.h"
#include "map/log.h"
#include "map/map.h"
#include "map/mercenary.h"
#include "map/mob.h"
#include "map/npc.h"
#include "map/party.h"
#include "map/pc.h"
#include "map/script.h"
#include "map/unit.h"
#include "common/cbasetypes.h"
#include "common/conf.h"
#include "common/memmgr.h"
#include "common/nullpo.h"
#include "common/random.h"
#include "common/showmsg.h"
#include "common/socket.h"
#include "common/strlib.h"
#include "common/timer.h"
#include "common/utils.h"

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

static struct quest_interface quest_s;
static struct quest_db *db_data[MAX_QUEST_DB]; ///< Quest database

struct quest_interface *quest;

/**
 * Searches a quest by ID.
 *
 * @param quest_id ID to lookup
 * @return Quest entry (equals to &quest->dummy if the ID is invalid)
 */
static struct quest_db *quest_db(int quest_id)
{
	if (quest_id < 0 || quest_id >= MAX_QUEST_DB || quest->db_data[quest_id] == NULL)
		return &quest->dummy;
	return quest->db_data[quest_id];
}

/**
 * Sends quest info to the player on login.
 *
 * @param sd Player's data
 * @return 0 in case of success, nonzero otherwise (i.e. the player has no quests)
 */
static int quest_pc_login(struct map_session_data *sd)
{
#if PACKETVER < 20141022
	int i;
#endif

	nullpo_retr(1, sd);
	if(sd->avail_quests == 0)
		return 1;

	clif->quest_send_list(sd);

#if PACKETVER < 20141022
	clif->quest_send_mission(sd);
	for( i = 0; i < sd->avail_quests; i++ ) {
		// TODO[Haru]: is this necessary? Does quest_send_mission not take care of this?
		clif->quest_update_objective(sd, &sd->quest_log[i]);
	}
#endif

	return 0;
}

/**
 * Adds a quest to the player's list.
 *
 * New quest will be added as Q_ACTIVE.
 *
 * @param sd         Player's data
 * @param quest_id   ID of the quest to add.
 * @param time_limit Custom time, in UNIX epoch, for this quest
 * @return 0 in case of success, nonzero otherwise
 */
static int quest_add(struct map_session_data *sd, int quest_id, unsigned int time_limit)
{
	int n;
	struct quest_db *qi = quest->db(quest_id);

	nullpo_retr(-1, sd);
	if (qi == &quest->dummy) {
		ShowError("quest_add: quest %d not found in DB.\n", quest_id);
		return -1;
	}

	if (quest->check(sd, quest_id, HAVEQUEST) >= 0) {
		ShowError("quest_add: Character %d already has quest %d.\n", sd->status.char_id, quest_id);
		return -1;
	}

	n = sd->avail_quests; // Insertion point
	Assert_retr(-1, sd->avail_quests <= sd->num_quests);

	sd->num_quests++;
	sd->avail_quests++;
	RECREATE(sd->quest_log, struct quest, sd->num_quests);

	if (sd->avail_quests != sd->num_quests) {
		// The character has some completed quests, make room before them so that they will stay at the end of the array
		memmove(&sd->quest_log[n+1], &sd->quest_log[n], sizeof(struct quest)*(sd->num_quests-sd->avail_quests));
	}

	memset(&sd->quest_log[n], 0, sizeof(struct quest));

	sd->quest_log[n].quest_id = qi->id;
	if (time_limit != 0)
		sd->quest_log[n].time = time_limit;
	else if (qi->time != 0)
		sd->quest_log[n].time = (unsigned int)(time(NULL) + qi->time);
	sd->quest_log[n].state = Q_ACTIVE;

	sd->save_quest = true;

	clif->quest_add(sd, &sd->quest_log[n]);
#if PACKETVER >= 20150513
	clif->quest_notify_objective(sd, &sd->quest_log[n]);
#else
	clif->quest_update_objective(sd, &sd->quest_log[n]);
#endif
	quest->questinfo_refresh(sd);

	if ((map->save_settings & 64) != 0)
		chrif->save(sd, 0);

	return 0;
}

/**
 * Replaces a quest in a player's list with another one.
 *
 * @param sd   Player's data
 * @param qid1 Current quest to replace
 * @param qid2 New quest to add
 * @return 0 in case of success, nonzero otherwise
 */
static int quest_change(struct map_session_data *sd, int qid1, int qid2)
{
	int i;
	struct quest_db *qi = quest->db(qid2);

	nullpo_retr(-1, sd);
	if( qi == &quest->dummy ) {
		ShowError("quest_change: quest %d not found in DB.\n", qid2);
		return -1;
	}

	if( quest->check(sd, qid2, HAVEQUEST) >= 0 ) {
		ShowError("quest_change: Character %d already has quest %d.\n", sd->status.char_id, qid2);
		return -1;
	}

	if( quest->check(sd, qid1, HAVEQUEST) < 0 ) {
		ShowError("quest_change: Character %d doesn't have quest %d.\n", sd->status.char_id, qid1);
		return -1;
	}

	ARR_FIND(0, sd->avail_quests, i, sd->quest_log[i].quest_id == qid1);
	if( i == sd->avail_quests ) {
		ShowError("quest_change: Character %d has completed quest %d.\n", sd->status.char_id, qid1);
		return -1;
	}

	memset(&sd->quest_log[i], 0, sizeof(struct quest));
	sd->quest_log[i].quest_id = qi->id;
	if( qi->time )
		sd->quest_log[i].time = (unsigned int)(time(NULL) + qi->time);
	sd->quest_log[i].state = Q_ACTIVE;

	sd->save_quest = true;

	clif->quest_delete(sd, qid1);
	clif->quest_add(sd, &sd->quest_log[i]);
#if PACKETVER >= 20150513
	clif->quest_notify_objective(sd, &sd->quest_log[i]);
#else
	clif->quest_update_objective(sd, &sd->quest_log[i]);
#endif
	quest->questinfo_refresh(sd);

	if( map->save_settings&64 )
		chrif->save(sd,0);
	return 0;
}

/**
 * Removes a quest from a player's list
 *
 * @param sd       Player's data
 * @param quest_id ID of the quest to remove
 * @return 0 in case of success, nonzero otherwise
 */
static int quest_delete(struct map_session_data *sd, int quest_id)
{
	int i;

	nullpo_retr(-1, sd);
	//Search for quest
	ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].quest_id == quest_id);

	if(i == sd->num_quests) {
		ShowError("quest_delete: Character %d doesn't have quest %d.\n", sd->status.char_id, quest_id);
		return -1;
	}

	if( sd->quest_log[i].state != Q_COMPLETE )
		sd->avail_quests--;

	if( i < --sd->num_quests ) {
		// Compact the array
		memmove(&sd->quest_log[i], &sd->quest_log[i+1], sizeof(struct quest)*(sd->num_quests-i));
	}
	if( sd->num_quests == 0 ) {
		aFree(sd->quest_log);
		sd->quest_log = NULL;
	} else {
		RECREATE(sd->quest_log, struct quest, sd->num_quests);
	}
	sd->save_quest = true;

	clif->quest_delete(sd, quest_id);
	quest->questinfo_refresh(sd);

	if( map->save_settings&64 )
		chrif->save(sd,0);

	return 0;
}

/**
 * Map iterator subroutine to update quest objectives for a party after killing a monster.
 *
 * @see map_foreachinrange
 * @param ap Argument list, expecting:
 *           int Party ID
 *           int Mob ID
 */
static int quest_update_objective_sub(struct block_list *bl, va_list ap)
{
	struct map_session_data *sd = NULL;
	int party_id = va_arg(ap, int);
	int mob_id = va_arg(ap, int);

	nullpo_ret(bl);
	Assert_ret(bl->type == BL_PC);
	sd = BL_UCAST(BL_PC, bl);

	if( !sd->avail_quests )
		return 0;
	if( sd->status.party_id != party_id )
		return 0;

	quest->update_objective(sd, mob_id);

	return 1;
}


/**
 * Updates the quest objectives for a character after killing a monster, including the handling of quest-granted drops.
 *
 * @param sd     Character's data
 * @param mob_id Monster ID
 */
static void quest_update_objective(struct map_session_data *sd, int mob_id)
{
	int i,j;

	nullpo_retv(sd);
	for (i = 0; i < sd->avail_quests; i++) {
		struct quest_db *qi = NULL;

		if (sd->quest_log[i].state != Q_ACTIVE) // Skip inactive quests
			continue;

		qi = quest->db(sd->quest_log[i].quest_id);

		for (j = 0; j < qi->objectives_count; j++) {
			if (qi->objectives[j].mob == mob_id && sd->quest_log[i].count[j] < qi->objectives[j].count) {
				sd->quest_log[i].count[j]++;
				sd->save_quest = true;
				clif->quest_update_objective(sd, &sd->quest_log[i]);
			}
		}

		// process quest-granted extra drop bonuses
		for (j = 0; j < qi->dropitem_count; j++) {
			struct quest_dropitem *dropitem = &qi->dropitem[j];
			struct item item;
			struct item_data *data = NULL;
			int temp;
			if (dropitem->mob_id != 0 && dropitem->mob_id != mob_id)
				continue;
			// TODO: Should this be affected by server rates?
			if (rnd()%10000 >= dropitem->rate)
				continue;
			if (!(data = itemdb->exists(dropitem->nameid)))
				continue;
			memset(&item,0,sizeof(item));
			item.nameid = dropitem->nameid;
			item.identify = itemdb->isidentified2(data);
			item.amount = 1;
			if((temp = pc->additem(sd, &item, 1, LOG_TYPE_QUEST)) != 0) { // TODO: We might want a new log type here?
				// Failed to obtain the item
				clif->additem(sd, 0, 0, temp);
			}
		}
	}
}

/**
 * Updates a quest's state.
 *
 * Only status of active and inactive quests can be updated. Completed quests can't (for now). [Inkfish]
 *
 * @param sd       Character's data
 * @param quest_id Quest ID to update
 * @param qs       New quest state
 * @return 0 in case of success, nonzero otherwise
 */
static int quest_update_status(struct map_session_data *sd, int quest_id, enum quest_state qs)
{
	int i;

	nullpo_retr(-1, sd);
	ARR_FIND(0, sd->avail_quests, i, sd->quest_log[i].quest_id == quest_id);
	if( i == sd->avail_quests ) {
		ShowError("quest_update_status: Character %d doesn't have quest %d.\n", sd->status.char_id, quest_id);
		return -1;
	}

	sd->quest_log[i].state = qs;
	sd->save_quest = true;

	if( qs < Q_COMPLETE ) {
		clif->quest_update_status(sd, quest_id, qs == Q_ACTIVE ? true : false);
		return 0;
	}

	// The quest is complete, so it needs to be moved to the completed quests block at the end of the array.

	if( i < (--sd->avail_quests) ) {
		struct quest tmp_quest;
		memcpy(&tmp_quest, &sd->quest_log[i],sizeof(struct quest));
		memcpy(&sd->quest_log[i], &sd->quest_log[sd->avail_quests],sizeof(struct quest));
		memcpy(&sd->quest_log[sd->avail_quests], &tmp_quest,sizeof(struct quest));
	}

	clif->quest_delete(sd, quest_id);
	quest->questinfo_refresh(sd);

	if( map->save_settings&64 )
		chrif->save(sd,0);

	return 0;
}

/**
 * Queries quest information for a character.
 *
 * @param sd       Character's data
 * @param quest_id Quest ID
 * @param type     Check type
 * @return -1 if the quest was not found, otherwise it depends on the type:
 *         HAVEQUEST: The quest's state
 *         PLAYTIME:  2 if the quest's timeout has expired
 *                    1 if the quest was completed
 *                    0 otherwise
 *         HUNTING:   2 if the quest has not been marked as completed yet, and its objectives have been fulfilled
 *                    1 if the quest's timeout has expired
 *                    0 otherwise
 */
static int quest_check(struct map_session_data *sd, int quest_id, enum quest_check_type type)
{
	int i;

	nullpo_retr(-1, sd);
	ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].quest_id == quest_id);
	if (i == sd->num_quests)
		return -1;

	switch (type) {
		case HAVEQUEST:
			return sd->quest_log[i].state;
		case PLAYTIME:
			return (sd->quest_log[i].time < (unsigned int)time(NULL) ? 2 : sd->quest_log[i].state == Q_COMPLETE ? 1 : 0);
		case HUNTING:
			if( sd->quest_log[i].state == Q_INACTIVE || sd->quest_log[i].state == Q_ACTIVE ) {
				int j;
				struct quest_db *qi = quest->db(sd->quest_log[i].quest_id);
				ARR_FIND(0, qi->objectives_count, j, sd->quest_log[i].count[j] < qi->objectives[j].count);
				if (j == qi->objectives_count)
					return 2;
				if (sd->quest_log[i].time < (unsigned int)time(NULL))
					return 1;
			}
			return 0;
		default:
			ShowError("quest_check_quest: Unknown parameter %u", type);
			break;
	}

	return -1;
}

/**
 * Reads and parses an entry from the quest_db.
 *
 * @param cs     The config setting containing the entry.
 * @param n      The sequential index of the current config setting.
 * @param source The source configuration file.
 * @return The parsed quest entry.
 * @retval NULL in case of errors.
 */
static struct quest_db *quest_read_db_sub(struct config_setting_t *cs, int n, const char *source)
{
	struct quest_db *entry = NULL;
	struct config_setting_t *t = NULL;
	int i32 = 0, quest_id;
	const char *str = NULL;
	nullpo_retr(NULL, cs);
	/*
	 * Id: Quest ID                    [int]
	 * Name: Quest Name                [string]
	 * TimeLimit: Time Limit (seconds) [int, optional]
	 * Targets: (                      [array, optional]
	 *     {
	 *         MobId: Mob ID           [int]
	 *         Count:                  [int]
	 *     },
	 *     ... (can repeated up to MAX_QUEST_OBJECTIVES times)
	 * )
	 * Drops: (
	 *     {
	 *         ItemId: Item ID to drop [int]
	 *         Rate: Drop rate         [int]
	 *         MobId: Mob ID to match  [int, optional]
	 *     },
	 *     ... (can be repeated)
	 * )
	 */
	if (!libconfig->setting_lookup_int(cs, "Id", &quest_id)) {
		ShowWarning("quest_read_db: Missing id in \"%s\", entry #%d, skipping.\n", source, n);
		return NULL;
	}
	if (quest_id < 0 || quest_id >= MAX_QUEST_DB) {
		ShowWarning("quest_read_db: Invalid quest ID '%d' in \"%s\", entry #%d (min: 0, max: %d), skipping.\n", quest_id, source, n, MAX_QUEST_DB);
		return NULL;
	}

	if (!libconfig->setting_lookup_string(cs, "Name", &str) || !*str) {
		ShowWarning("quest_read_db_sub: Missing Name in quest %d of \"%s\", skipping.\n", quest_id, source);
		return NULL;
	}

	CREATE(entry, struct quest_db, 1);
	entry->id = quest_id;
	//safestrncpy(entry->name, str, sizeof(entry->name));

	if (libconfig->setting_lookup_int(cs, "TimeLimit", &i32)) // This is an unsigned value, do not check for >= 0
		entry->time = (unsigned int)i32;

	if ((t=libconfig->setting_get_member(cs, "Targets")) && config_setting_is_list(t)) {
		int i, len = libconfig->setting_length(t);
		for (i = 0; i < len && entry->objectives_count < MAX_QUEST_OBJECTIVES; i++) {
			// Note: We ensure that objectives_count < MAX_QUEST_OBJECTIVES because
			//       quest_log (as well as the client) expect this maximum size.
			struct config_setting_t *tt = libconfig->setting_get_elem(t, i);
			int mob_id = 0, count = 0;
			if (!tt)
				break;
			if (!config_setting_is_group(tt))
				continue;
			if (!libconfig->setting_lookup_int(tt, "MobId", &mob_id) || mob_id <= 0)
				continue;
			if (!libconfig->setting_lookup_int(tt, "Count", &count) || count <= 0)
				continue;
			RECREATE(entry->objectives, struct quest_objective, ++entry->objectives_count);
			entry->objectives[entry->objectives_count-1].mob = mob_id;
			entry->objectives[entry->objectives_count-1].count = count;
		}
	}

	if ((t=libconfig->setting_get_member(cs, "Drops")) && config_setting_is_list(t)) {
		int i, len = libconfig->setting_length(t);
		for (i = 0; i < len; i++) {
			struct config_setting_t *tt = libconfig->setting_get_elem(t, i);
			int mob_id = 0, nameid = 0, rate = 0;
			if (!tt)
				break;
			if (!config_setting_is_group(tt))
				continue;
			if (!libconfig->setting_lookup_int(tt, "MobId", &mob_id))
				mob_id = 0; // Zero = any monster
			if (mob_id < 0)
				continue;
			if (!libconfig->setting_lookup_int(tt, "ItemId", &nameid) || !itemdb->exists(nameid))
				continue;
			if (!libconfig->setting_lookup_int(tt, "Rate", &rate) || rate <= 0)
				continue;
			RECREATE(entry->dropitem, struct quest_dropitem, ++entry->dropitem_count);
			entry->dropitem[entry->dropitem_count-1].mob_id = mob_id;
			entry->dropitem[entry->dropitem_count-1].nameid = nameid;
			entry->dropitem[entry->dropitem_count-1].rate = rate;
		}
	}
	return entry;
}

/**
 * Loads quests from the quest db.
 *
 * @return Number of loaded quests, or -1 if the file couldn't be read.
 */
static int quest_read_db(void)
{
	char filepath[256];
	struct config_t quest_db_conf;
	struct config_setting_t *qdb = NULL, *q = NULL;
	int i = 0, count = 0;
	const char *filename = "quest_db.conf";

	safesnprintf(filepath, 256, "%s/%s", map->db_path, filename);
	if (!libconfig->load_file(&quest_db_conf, filepath))
		return -1;

	if ((qdb = libconfig->setting_get_member(quest_db_conf.root, "quest_db")) == NULL) {
		ShowError("can't read %s\n", filepath);
		return -1;
	}

	while ((q = libconfig->setting_get_elem(qdb, i++))) {
		struct quest_db *entry = quest->read_db_sub(q, i-1, filepath);
		if (!entry)
			continue;

		if (quest->db_data[entry->id] != NULL) {
			ShowWarning("quest_read_db: Duplicate quest %d.\n", entry->id);
			if (quest->db_data[entry->id]->dropitem)
				aFree(quest->db_data[entry->id]->dropitem);
			if (quest->db_data[entry->id]->objectives)
				aFree(quest->db_data[entry->id]->objectives);
			aFree(quest->db_data[entry->id]);
		}
		quest->db_data[entry->id] = entry;

		count++;
	}
	libconfig->destroy(&quest_db_conf);
	ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, filename);
	return count;
}

/**
 * Map iterator to ensures a player has no invalid quest log entries.
 *
 * Any entries that are no longer in the db are removed.
 *
 * @see map->foreachpc
 * @param ap Ignored
 */
static int quest_reload_check_sub(struct map_session_data *sd, va_list ap)
{
	int i, j;

	nullpo_ret(sd);

	j = 0;
	for (i = 0; i < sd->num_quests; i++) {
		struct quest_db *qi = quest->db(sd->quest_log[i].quest_id);
		if (qi == &quest->dummy) { // Remove no longer existing entries
			if (sd->quest_log[i].state != Q_COMPLETE) // And inform the client if necessary
				clif->quest_delete(sd, sd->quest_log[i].quest_id);
			continue;
		}
		if (i != j) {
			// Move entries if there's a gap to fill
			memcpy(&sd->quest_log[j], &sd->quest_log[i], sizeof(struct quest));
		}
		j++;
	}
	sd->num_quests = j;
	ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].state == Q_COMPLETE);
	sd->avail_quests = i;

	return 1;
}

/**
 * Clears the quest database for shutdown or reload.
 */
static void quest_clear_db(void)
{
	int i;

	for (i = 0; i < MAX_QUEST_DB; i++) {
		if (quest->db_data[i]) {
			if (quest->db_data[i]->objectives)
				aFree(quest->db_data[i]->objectives);
			if (quest->db_data[i]->dropitem)
				aFree(quest->db_data[i]->dropitem);
			aFree(quest->db_data[i]);
			quest->db_data[i] = NULL;
		}
	}
}

/*
* Limit the questinfo icon id to avoid client problems
*/
static int quest_questinfo_validate_icon(int icon)
{
#if PACKETVER >= 20170315
	if (icon < 0 || (icon > 10 && icon != 9999))
		icon = 9999;
#elif PACKETVER >= 20120410
	if (icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
		icon = 9999; // Default to nothing if icon id is invalid.
#else
	if (icon < 0 || icon > 7)
		icon = 0;
	else
		icon = icon + 1;
#endif
	return icon;
}

/**
 * Refresh the questinfo bubbles on the player map.
 *
 * @param sd session data.
 * @param qi questinfo data.
 *
 */
static void quest_questinfo_refresh(struct map_session_data *sd)
{
	int i;

	nullpo_retv(sd);

	for (i = 0; i < VECTOR_LENGTH(map->list[sd->bl.m].qi_data); i++) {
		struct questinfo *qi = &VECTOR_INDEX(map->list[sd->bl.m].qi_data, i);
		// Remove the bubbles if one of the conditions is no longer valid.
		if (quest->questinfo_validate(sd, qi) == false) {
#if PACKETVER >= 20120410
			clif->quest_show_event(sd, &qi->nd->bl, 9999, 0);
#else
			clif->quest_show_event(sd, &qi->nd->bl, 0, 0);
#endif
		} else {
			clif->quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
		}
	}
}

/**
 * Validate all possible conditions required for the questinfo
 *
 * @param sd session data.
 * @param qi questinfo data.
 *
 * @retval true if conditions are correct.
 * @retval false if one condition or more are in-correct.
 */
static bool quest_questinfo_validate(struct map_session_data *sd, struct questinfo *qi)
{
	nullpo_retr(false, sd);
	nullpo_retr(false, qi);
	if (qi->hasJob && quest->questinfo_validate_job(sd, qi) == false)
		return false;
	if (qi->sex_enabled && quest->questinfo_validate_sex(sd, qi) == false)
		return false;
	if ((qi->base_level.min != 0 || qi->base_level.max != 0) && quest->questinfo_validate_baselevel(sd, qi) == false)
		return false;
	if ((qi->job_level.min != 0 || qi->job_level.max != 0) && quest->questinfo_validate_joblevel(sd, qi) == false)
		return false;
	if (VECTOR_LENGTH(qi->items) > 0 && quest->questinfo_validate_items(sd, qi) == false)
		return false;
	if (qi->homunculus.level != 0 && quest->questinfo_validate_homunculus_level(sd, qi) == false)
		return false;
	if (qi->homunculus.class_ != 0 && quest->questinfo_validate_homunculus_type(sd, qi) == false)
		return false;
	if (VECTOR_LENGTH(qi->quest_requirement) > 0 && quest->questinfo_validate_quests(sd, qi) == false)
		return false;
	if (qi->mercenary_class != 0 && quest->questinfo_validate_mercenary_class(sd, qi) == false)
		return false;
	return true;
}

/**
 * Validate job required for the questinfo
 *
 * @param sd session data.
 * @param qi questinfo data.
 *
 * @retval true if player job is matching the required.
 * @retval false if player job is NOT matching the required.
 */
static bool quest_questinfo_validate_job(struct map_session_data *sd, struct questinfo *qi)
{
	nullpo_retr(false, sd);
	nullpo_retr(false, qi);
	if (sd->status.class == qi->job)
		return true;
	return false;
}

/**
 * Validate sex required for the questinfo
 *
 * @param sd session data.
 * @param qi questinfo data.
 *
 * @retval true if player sex is matching the required.
 * @retval false if player sex is NOT matching the required.
 */
static bool quest_questinfo_validate_sex(struct map_session_data *sd, struct questinfo *qi)
{
	nullpo_retr(false, sd);
	nullpo_retr(false, qi);
	if (sd->status.sex == qi->sex)
		return true;
	return false;
}

/**
 * Validate base level required for the questinfo
 *
 * @param sd session data.
 * @param qi questinfo data.
 *
 * @retval true if player base level is included in the required level range.
 * @retval false if player base level is NOT included in the required level range.
 */
static bool quest_questinfo_validate_baselevel(struct map_session_data *sd, struct questinfo *qi)
{
	nullpo_retr(false, sd);
	nullpo_retr(false, qi);
	if (sd->status.base_level >= qi->base_level.min && sd->status.base_level <= qi->base_level.max)
		return true;
	return false;
}

/**
 * Validate job level required for the questinfo
 *
 * @param sd session data.
 * @param qi questinfo data.
 *
 * @retval true if player job level is included in the required level range.
 * @retval false if player job level is NOT included in the required level range.
 */
static bool quest_questinfo_validate_joblevel(struct map_session_data *sd, struct questinfo *qi)
{
	nullpo_retr(false, sd);
	nullpo_retr(false, qi);
	if (sd->status.job_level >= qi->job_level.min && sd->status.job_level <= qi->job_level.max)
		return true;
	return false;
}

/**
 * Validate items list required for the questinfo
 *
 * @param sd session data.
 * @param qi questinfo data.
 *
 * @retval true if player have all the items required.
 * @retval false if player is missing one or more of the items required.
 */
static bool quest_questinfo_validate_items(struct map_session_data *sd, struct questinfo *qi)
{
	nullpo_retr(false, sd);
	nullpo_retr(false, qi);

	for (int i = 0; i < VECTOR_LENGTH(qi->items); i++) {
		struct questinfo_itemreq *item = &VECTOR_INDEX(qi->items, i);
		int count = 0;
		for (int j = 0; j < sd->status.inventorySize; j++) {
			if (sd->status.inventory[j].nameid == item->nameid)
				count += sd->status.inventory[j].amount;
		}
		if (count < item->min || count > item->max)
			return false;
	}

	return true;
}

/**
 * Validate minimal homunculus level required for the questinfo
 *
 * @param sd session data.
 * @param qi questinfo data.
 *
 * @retval true if homunculus level >= the required value.
 * @retval false if homunculus level smaller than the required value.
 */
static bool quest_questinfo_validate_homunculus_level(struct map_session_data *sd, struct questinfo *qi)
{
	nullpo_retr(false, sd);
	nullpo_retr(false, qi);

	if (sd->hd == NULL)
		return false;
	if (!homun_alive(sd->hd))
		return false;
	if (sd->hd->homunculus.level < qi->homunculus.level)
		return false;
	return true;
}

/**
 * Validate homunculus type required for the questinfo
 *
 * @param sd session data.
 * @param qi questinfo data.
 *
 * @retval true if player's homunculus is matching the required.
 * @retval false if player's homunculus is NOT matching the required.
 */
static bool quest_questinfo_validate_homunculus_type(struct map_session_data *sd, struct questinfo *qi)
{
	nullpo_retr(false, sd);
	nullpo_retr(false, qi);

	if (sd->hd == NULL)
		return false;
	if (!homun_alive(sd->hd))
		return false;
	if (homun->class2type(sd->hd->homunculus.class_) != qi->homunculus_type)
		return false;
	return true;
}

/**
 * Validate quest list required for the questinfo
 *
 * @param sd session data.
 * @param qi questinfo data.
 *
 * @retval true if player have all the quests required.
 * @retval false if player is missing one or more of the quests required.
 */
static bool quest_questinfo_validate_quests(struct map_session_data *sd, struct questinfo *qi)
{
	int i;

	nullpo_retr(false, sd);
	nullpo_retr(false, qi);
	
	for (i = 0; i < VECTOR_LENGTH(qi->quest_requirement); i++) {
		struct questinfo_qreq *quest_requirement = &VECTOR_INDEX(qi->quest_requirement, i);
		int quest_progress = quest->check(sd, quest_requirement->id, HAVEQUEST);
		if (quest_progress == -1)
			quest_progress = 0;
		else if (quest_progress == 0 || quest_progress == 1)
			quest_progress = 1;
		else
			quest_progress = 2;
		if (quest_progress != quest_requirement->state)
			return false;
	}

	return true;
}

/**
 * Validate mercenary class required for the questinfo
 *
 * @param sd session data.
 * @param qi questinfo data.
 *
 * @retval true if player have a mercenary with the given class.
 * @retval false if player does NOT have a mercenary with the given class.
 */
static bool quest_questinfo_validate_mercenary_class(struct map_session_data *sd, struct questinfo *qi)
{
	nullpo_retr(false, sd);
	nullpo_retr(false, qi);

	if (sd->md == NULL)
		return false;

	if (sd->md->mercenary.class_ != qi->mercenary_class)
		return false;

	return true;
}

/**
 * Clears the questinfo data vector
 *
 * @param m mapindex.
 *
 */
static void quest_questinfo_vector_clear(int m)
{
	int i;

	Assert_retv(m >= 0 && m < map->count);

	for (i = 0; i < VECTOR_LENGTH(map->list[m].qi_data); i++) {
		struct questinfo *qi_data = &VECTOR_INDEX(map->list[m].qi_data, i);
		VECTOR_CLEAR(qi_data->items);
		VECTOR_CLEAR(qi_data->quest_requirement);
	}
	VECTOR_CLEAR(map->list[m].qi_data);
}

/**
 * Initializes the quest interface.
 *
 * @param minimal Run in minimal mode (skips most of the loading)
 */
static void do_init_quest(bool minimal)
{
	if (minimal)
		return;

	quest->read_db();
}

/**
 * Finalizes the quest interface before shutdown.
 */
static void do_final_quest(void)
{
	quest->clear();
}

/**
 * Reloads the quest database.
 */
static void do_reload_quest(void)
{
	quest->clear();

	quest->read_db();

	// Update quest data for players, to ensure no entries about removed quests are left over.
	map->foreachpc(&quest_reload_check_sub);
}

/**
 * Initializes default values for the quest interface.
 */
void quest_defaults(void)
{
	quest = &quest_s;
	quest->db_data = db_data;

	memset(&quest->db, 0, sizeof(quest->db));
	memset(&quest->dummy, 0, sizeof(quest->dummy));
	/* */
	quest->init = do_init_quest;
	quest->final = do_final_quest;
	quest->reload = do_reload_quest;
	/* */
	quest->db = quest_db;
	quest->pc_login = quest_pc_login;
	quest->add = quest_add;
	quest->change = quest_change;
	quest->delete = quest_delete;
	quest->update_objective_sub = quest_update_objective_sub;
	quest->update_objective = quest_update_objective;
	quest->update_status = quest_update_status;
	quest->check = quest_check;
	quest->clear = quest_clear_db;
	quest->read_db = quest_read_db;
	quest->read_db_sub = quest_read_db_sub;

	quest->questinfo_validate_icon = quest_questinfo_validate_icon;
	quest->questinfo_refresh = quest_questinfo_refresh;
	quest->questinfo_validate = quest_questinfo_validate;
	quest->questinfo_validate_job = quest_questinfo_validate_job;
	quest->questinfo_validate_sex = quest_questinfo_validate_sex;
	quest->questinfo_validate_baselevel = quest_questinfo_validate_baselevel;
	quest->questinfo_validate_joblevel = quest_questinfo_validate_joblevel;
	quest->questinfo_validate_items = quest_questinfo_validate_items;
	quest->questinfo_validate_homunculus_level = quest_questinfo_validate_homunculus_level;
	quest->questinfo_validate_homunculus_type = quest_questinfo_validate_homunculus_type;
	quest->questinfo_validate_quests = quest_questinfo_validate_quests;
	quest->questinfo_validate_mercenary_class = quest_questinfo_validate_mercenary_class;
	quest->questinfo_vector_clear = quest_questinfo_vector_clear;
}