summaryrefslogtreecommitdiff
path: root/src/map/quest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/map/quest.c')
-rw-r--r--src/map/quest.c156
1 files changed, 100 insertions, 56 deletions
diff --git a/src/map/quest.c b/src/map/quest.c
index a65e08a..aef112a 100644
--- a/src/map/quest.c
+++ b/src/map/quest.c
@@ -10,75 +10,119 @@
#include "../../../common/mmo.h"
#include "../../../common/socket.h"
#include "../../../common/strlib.h"
+#include "../../../map/itemdb.h"
#include "../../../map/quest.h"
#include "map/quest.h"
-int equest_read_db(void)
+/**
+ * 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.
+ */
+struct quest_db *equest_read_db_sub(config_setting_t *cs, int *nPtr, const char *source)
{
- FILE *fp;
- char line[1024];
- int i, count = 0;
- char *str[20], *p, *np;
- struct quest_db entry;
+ struct quest_db *entry = NULL;
+ config_setting_t *t = NULL;
+ int i32 = 0, quest_id;
+ const char *str = NULL;
+ const int n = *nPtr;
- sprintf(line, "%s/quest_db.txt", map->db_path);
- if ((fp = fopen(line, "r")) == NULL)
- {
- ShowError("can't read %s\n", line);
- hookStop();
- return -1;
- }
+ /*
+ * 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)
+ * )
+ */
- while (fgets(line, sizeof(line), fp))
- {
- if (line[0]=='/' && line[1]=='/')
- continue;
- memset(str,0,sizeof(str));
+ 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;
+ }
- for (i = 0, p = line; i < 8; i++) {
- if (( np = strchr(p,',') ) != NULL) {
- str[i] = p;
- *np = 0;
- p = np + 1;
- } else if (str[0] == NULL) {
- break;
- } else {
- ShowError("quest_read_db: insufficient columns in line %s\n", line);
- continue;
- }
- }
- if (str[0] == NULL)
- continue;
+ 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;
+ }
- memset(&entry, 0, sizeof(entry));
+ CREATE(entry, struct quest_db, 1);
+ entry->id = quest_id;
+ //safestrncpy(entry->name, str, sizeof(entry->name));
- entry.id = atoi(str[0]);
+ if (libconfig->setting_lookup_int(cs, "TimeLimit", &i32)) // This is an unsigned value, do not check for >= 0
+ entry->time = (unsigned int)i32;
- if (entry.id < 0 || entry.id >= MAX_QUEST_DB) {
- ShowError("quest_read_db: Invalid quest ID '%d' in line '%s' (min: 0, max: %d.)\n", entry.id, line, MAX_QUEST_DB);
- continue;
+ 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.
+ 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;
}
+*/
+ entry->objectives_count = 1;
+ RECREATE(entry->objectives, struct quest_objective, 1);
+ entry->objectives[0].mob = 1;
+ entry->objectives[0].count = 0;
+ }
- entry.time = atoi(str[1]);
-
-// for (i = 0; i < MAX_QUEST_OBJECTIVES; i++) {
- for (i = 0; i < 1; i++) {
-// entry.mob[i] = atoi(str[2*i+2]);
- entry.mob[i] = 1;
- entry.count[i] = atoi(str[2*i+3]);
+ 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++) {
+ 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;
}
-
- entry.num_objectives = i;
-
- if (quest->db_data[entry.id] == NULL)
- quest->db_data[entry.id] = aMalloc(sizeof(struct quest_db));
-
- memcpy(quest->db_data[entry.id], &entry, sizeof(struct quest_db));
- count++;
}
- fclose(fp);
- ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, "quest_db.txt");
- hookStop();
- return 0;
+ return entry;
}