|
@@ -32,80 +32,84 @@
|
|
#include <stdarg.h>
|
|
#include <stdarg.h>
|
|
#include <time.h>
|
|
#include <time.h>
|
|
|
|
|
|
-
|
|
|
|
-struct s_quest_db quest_db[MAX_QUEST_DB];
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-int quest_search_db(int quest_id)
|
|
|
|
-{
|
|
|
|
- int i;
|
|
|
|
-
|
|
|
|
- ARR_FIND(0, MAX_QUEST_DB,i,quest_id == quest_db[i].id);
|
|
|
|
- if( i == MAX_QUEST_DB )
|
|
|
|
- return -1;
|
|
|
|
-
|
|
|
|
- return i;
|
|
|
|
|
|
+/**
|
|
|
|
+ * Searches a quest by ID.
|
|
|
|
+ *
|
|
|
|
+ * @param quest_id ID to lookup
|
|
|
|
+ * @return Quest entry (equals to &quest_dummy if the ID is invalid)
|
|
|
|
+ */
|
|
|
|
+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];
|
|
}
|
|
}
|
|
|
|
|
|
-//Send quest info on login
|
|
|
|
-int quest_pc_login(TBL_PC * sd)
|
|
|
|
-{
|
|
|
|
|
|
+/**
|
|
|
|
+ * 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)
|
|
|
|
+ */
|
|
|
|
+int quest_pc_login(TBL_PC *sd) {
|
|
int i;
|
|
int i;
|
|
|
|
|
|
- if(sd->avail_quests == 0)
|
|
|
|
|
|
+ if( sd->avail_quests == 0 )
|
|
return 1;
|
|
return 1;
|
|
|
|
|
|
clif_quest_send_list(sd);
|
|
clif_quest_send_list(sd);
|
|
clif_quest_send_mission(sd);
|
|
clif_quest_send_mission(sd);
|
|
-
|
|
|
|
- for( i = 0; i < sd->avail_quests; i++ ){
|
|
|
|
- clif_quest_update_objective(sd, &sd->quest_log[i], sd->quest_index[i]);
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ //@TODO[Haru]: Is this necessary? Does quest_send_mission not take care of this?
|
|
|
|
+ for( i = 0; i < sd->avail_quests; i++ )
|
|
|
|
+ clif_quest_update_objective(sd, &sd->quest_log[i]);
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
-int quest_add(TBL_PC * sd, int quest_id)
|
|
|
|
-{
|
|
|
|
-
|
|
|
|
- int i, j;
|
|
|
|
-
|
|
|
|
- if( sd->num_quests >= MAX_QUEST_DB )
|
|
|
|
- {
|
|
|
|
- ShowError("quest_add: Character %d has got all the quests.(max quests: %d)\n", sd->status.char_id, MAX_QUEST_DB);
|
|
|
|
- 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);
|
|
|
|
|
|
+/**
|
|
|
|
+ * 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.
|
|
|
|
+ * @return 0 in case of success, nonzero otherwise
|
|
|
|
+ */
|
|
|
|
+int quest_add(TBL_PC *sd, int quest_id) {
|
|
|
|
+ int n;
|
|
|
|
+ struct quest_db *qi = quest_db(quest_id);
|
|
|
|
+
|
|
|
|
+ if( qi == &quest_dummy ) {
|
|
|
|
+ ShowError("quest_add: quest %d not found in DB.\n", quest_id);
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
- if( (j = quest_search_db(quest_id)) < 0 )
|
|
|
|
- {
|
|
|
|
- ShowError("quest_add: quest %d not found in DB.\n", quest_id);
|
|
|
|
|
|
+ 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;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
- i = sd->avail_quests;
|
|
|
|
- memmove(&sd->quest_log[i+1], &sd->quest_log[i], sizeof(struct quest)*(sd->num_quests-sd->avail_quests));
|
|
|
|
- memmove(sd->quest_index+i+1, sd->quest_index+i, sizeof(int)*(sd->num_quests-sd->avail_quests));
|
|
|
|
-
|
|
|
|
- memset(&sd->quest_log[i], 0, sizeof(struct quest));
|
|
|
|
- sd->quest_log[i].quest_id = quest_db[j].id;
|
|
|
|
- if( quest_db[j].time )
|
|
|
|
- sd->quest_log[i].time = (unsigned int)(time(NULL) + quest_db[j].time);
|
|
|
|
- sd->quest_log[i].state = Q_ACTIVE;
|
|
|
|
|
|
+ n = sd->avail_quests; //Insertion point
|
|
|
|
|
|
- sd->quest_index[i] = j;
|
|
|
|
sd->num_quests++;
|
|
sd->num_quests++;
|
|
sd->avail_quests++;
|
|
sd->avail_quests++;
|
|
- sd->save_quest = true;
|
|
|
|
|
|
+ RECREATE(sd->quest_log, struct quest, sd->num_quests);
|
|
|
|
+
|
|
|
|
+ //The character has some completed quests, make room before them so that they will stay at the end of the array
|
|
|
|
+ if( sd->avail_quests != sd->num_quests )
|
|
|
|
+ memmove(&sd->quest_log[n + 1], &sd->quest_log[n], sizeof(struct quest) * (sd->num_quests-sd->avail_quests));
|
|
|
|
|
|
- clif_quest_add(sd, &sd->quest_log[i], sd->quest_index[i]);
|
|
|
|
|
|
+ memset(&sd->quest_log[n], 0, sizeof(struct quest));
|
|
|
|
+
|
|
|
|
+ sd->quest_log[n].quest_id = qi->id;
|
|
|
|
+ if( qi->time )
|
|
|
|
+ sd->quest_log[n].time = (unsigned int)(time(NULL) + qi->time);
|
|
|
|
+ sd->quest_log[n].state = Q_ACTIVE;
|
|
|
|
+
|
|
|
|
+ sd->save_quest = true;
|
|
|
|
|
|
- clif_quest_update_objective(sd, &sd->quest_log[i], sd->quest_index[i]);
|
|
|
|
|
|
+ clif_quest_add(sd, &sd->quest_log[n]);
|
|
|
|
+ clif_quest_update_objective(sd, &sd->quest_log[n]);
|
|
|
|
|
|
if( save_settings&64 )
|
|
if( save_settings&64 )
|
|
chrif_save(sd,0);
|
|
chrif_save(sd,0);
|
|
@@ -113,49 +117,50 @@ int quest_add(TBL_PC * sd, int quest_id)
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
-int quest_change(TBL_PC * sd, int qid1, int qid2)
|
|
|
|
-{
|
|
|
|
-
|
|
|
|
- int i, j;
|
|
|
|
|
|
+/**
|
|
|
|
+ * 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
|
|
|
|
+ */
|
|
|
|
+int quest_change(TBL_PC *sd, int qid1, int qid2) {
|
|
|
|
+ int i;
|
|
|
|
+ struct quest_db *qi = quest_db(qid2);
|
|
|
|
|
|
- if( quest_check(sd, qid2, HAVEQUEST) >= 0 )
|
|
|
|
- {
|
|
|
|
- ShowError("quest_change: Character %d already has quest %d.\n", sd->status.char_id, qid2);
|
|
|
|
|
|
+ if( qi == &quest_dummy ) {
|
|
|
|
+ ShowError("quest_change: quest %d not found in DB.\n", qid2);
|
|
return -1;
|
|
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);
|
|
|
|
|
|
+ if( quest_check(sd, qid2, HAVEQUEST) >= 0 ) {
|
|
|
|
+ ShowError("quest_change: Character %d already has quest %d.\n", sd->status.char_id, qid2);
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
- if( (j = quest_search_db(qid2)) < 0 )
|
|
|
|
- {
|
|
|
|
- ShowError("quest_change: quest %d not found in DB.\n",qid2);
|
|
|
|
|
|
+ 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;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
ARR_FIND(0, sd->avail_quests, i, sd->quest_log[i].quest_id == qid1);
|
|
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 quests %d.\n", sd->status.char_id, qid1);
|
|
|
|
|
|
+ if( i == sd->avail_quests ) {
|
|
|
|
+ ShowError("quest_change: Character %d has completed quest %d.\n", sd->status.char_id, qid1);
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
memset(&sd->quest_log[i], 0, sizeof(struct quest));
|
|
memset(&sd->quest_log[i], 0, sizeof(struct quest));
|
|
- sd->quest_log[i].quest_id = quest_db[j].id;
|
|
|
|
- if( quest_db[j].time )
|
|
|
|
- sd->quest_log[i].time = (unsigned int)(time(NULL) + quest_db[j].time);
|
|
|
|
|
|
+ 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->quest_log[i].state = Q_ACTIVE;
|
|
|
|
|
|
- sd->quest_index[i] = j;
|
|
|
|
sd->save_quest = true;
|
|
sd->save_quest = true;
|
|
|
|
|
|
clif_quest_delete(sd, qid1);
|
|
clif_quest_delete(sd, qid1);
|
|
- clif_quest_add(sd, &sd->quest_log[i], sd->quest_index[i]);
|
|
|
|
-
|
|
|
|
- clif_quest_update_objective(sd, &sd->quest_log[i], sd->quest_index[i]);
|
|
|
|
|
|
+ clif_quest_add(sd, &sd->quest_log[i]);
|
|
|
|
+ clif_quest_update_objective(sd, &sd->quest_log[i]);
|
|
|
|
|
|
if( save_settings&64 )
|
|
if( save_settings&64 )
|
|
chrif_save(sd,0);
|
|
chrif_save(sd,0);
|
|
@@ -163,27 +168,31 @@ int quest_change(TBL_PC * sd, int qid1, int qid2)
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
-int quest_delete(TBL_PC * sd, int quest_id)
|
|
|
|
-{
|
|
|
|
|
|
+/**
|
|
|
|
+ * 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
|
|
|
|
+ */
|
|
|
|
+int quest_delete(TBL_PC *sd, int quest_id) {
|
|
int i;
|
|
int i;
|
|
|
|
|
|
//Search for quest
|
|
//Search for quest
|
|
ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].quest_id == quest_id);
|
|
ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].quest_id == quest_id);
|
|
- if(i == sd->num_quests)
|
|
|
|
- {
|
|
|
|
|
|
+ if( i == sd->num_quests ) {
|
|
ShowError("quest_delete: Character %d doesn't have quest %d.\n", sd->status.char_id, quest_id);
|
|
ShowError("quest_delete: Character %d doesn't have quest %d.\n", sd->status.char_id, quest_id);
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
-
|
|
|
|
if( sd->quest_log[i].state != Q_COMPLETE )
|
|
if( sd->quest_log[i].state != Q_COMPLETE )
|
|
sd->avail_quests--;
|
|
sd->avail_quests--;
|
|
- if( sd->num_quests-- < MAX_QUEST_DB && sd->quest_log[i+1].quest_id )
|
|
|
|
- {
|
|
|
|
- memmove(&sd->quest_log[i], &sd->quest_log[i+1], sizeof(struct quest)*(sd->num_quests-i));
|
|
|
|
- memmove(sd->quest_index+i, sd->quest_index+i+1, sizeof(int)*(sd->num_quests-i));
|
|
|
|
- }
|
|
|
|
- memset(&sd->quest_log[sd->num_quests], 0, sizeof(struct quest));
|
|
|
|
- sd->quest_index[sd->num_quests] = 0;
|
|
|
|
|
|
+ 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;
|
|
sd->save_quest = true;
|
|
|
|
|
|
clif_quest_delete(sd, quest_id);
|
|
clif_quest_delete(sd, quest_id);
|
|
@@ -194,9 +203,16 @@ int quest_delete(TBL_PC * sd, int quest_id)
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
-int quest_update_objective_sub(struct block_list *bl, va_list ap)
|
|
|
|
-{
|
|
|
|
- struct map_session_data * sd;
|
|
|
|
|
|
+/**
|
|
|
|
+ * 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
|
|
|
|
+ */
|
|
|
|
+int quest_update_objective_sub(struct block_list *bl, va_list ap) {
|
|
|
|
+ struct map_session_data *sd;
|
|
int mob, party;
|
|
int mob, party;
|
|
|
|
|
|
nullpo_ret(bl);
|
|
nullpo_ret(bl);
|
|
@@ -215,29 +231,48 @@ int quest_update_objective_sub(struct block_list *bl, va_list ap)
|
|
return 1;
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-void quest_update_objective(TBL_PC * sd, int mob) {
|
|
|
|
- int i,j;
|
|
|
|
|
|
+/**
|
|
|
|
+ * Updates the quest objectives for a character after killing a monster.
|
|
|
|
+ *
|
|
|
|
+ * @param sd Character's data
|
|
|
|
+ * @param mob_id Monster ID
|
|
|
|
+ */
|
|
|
|
+void quest_update_objective(TBL_PC *sd, int mob) {
|
|
|
|
+ int i, j;
|
|
|
|
|
|
for( i = 0; i < sd->avail_quests; i++ ) {
|
|
for( i = 0; i < sd->avail_quests; i++ ) {
|
|
- if( sd->quest_log[i].state != Q_ACTIVE )
|
|
|
|
|
|
+ struct quest_db *qi = NULL;
|
|
|
|
+
|
|
|
|
+ if( sd->quest_log[i].state != Q_ACTIVE ) // Skip inactive quests
|
|
continue;
|
|
continue;
|
|
|
|
|
|
- for( j = 0; j < MAX_QUEST_OBJECTIVES; j++ )
|
|
|
|
- if( quest_db[sd->quest_index[i]].mob[j] == mob && sd->quest_log[i].count[j] < quest_db[sd->quest_index[i]].count[j] ) {
|
|
|
|
|
|
+ qi = quest_db(sd->quest_log[i].quest_id);
|
|
|
|
+
|
|
|
|
+ for( j = 0; j < qi->num_objectives; j++ ) {
|
|
|
|
+ if( qi->mob[j] == mob && sd->quest_log[i].count[j] < qi->count[j] ) {
|
|
sd->quest_log[i].count[j]++;
|
|
sd->quest_log[i].count[j]++;
|
|
sd->save_quest = true;
|
|
sd->save_quest = true;
|
|
- clif_quest_update_objective(sd,&sd->quest_log[i],sd->quest_index[i]);
|
|
|
|
|
|
+ clif_quest_update_objective(sd, &sd->quest_log[i]);
|
|
}
|
|
}
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-int quest_update_status(TBL_PC * sd, int quest_id, quest_state status) {
|
|
|
|
|
|
+/**
|
|
|
|
+ * 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
|
|
|
|
+ */
|
|
|
|
+int quest_update_status(TBL_PC *sd, int quest_id, enum quest_state status) {
|
|
int i;
|
|
int i;
|
|
|
|
|
|
- //Only status of active and inactive quests can be updated. Completed quests can't (for now). [Inkfish]
|
|
|
|
ARR_FIND(0, sd->avail_quests, i, sd->quest_log[i].quest_id == quest_id);
|
|
ARR_FIND(0, sd->avail_quests, i, sd->quest_log[i].quest_id == quest_id);
|
|
- if(i == sd->avail_quests) {
|
|
|
|
|
|
+ if( i == sd->avail_quests ) {
|
|
ShowError("quest_update_status: Character %d doesn't have quest %d.\n", sd->status.char_id, quest_id);
|
|
ShowError("quest_update_status: Character %d doesn't have quest %d.\n", sd->status.char_id, quest_id);
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
@@ -246,15 +281,17 @@ int quest_update_status(TBL_PC * sd, int quest_id, quest_state status) {
|
|
sd->save_quest = true;
|
|
sd->save_quest = true;
|
|
|
|
|
|
if( status < Q_COMPLETE ) {
|
|
if( status < Q_COMPLETE ) {
|
|
- clif_quest_update_status(sd, quest_id, (bool)status);
|
|
|
|
|
|
+ clif_quest_update_status(sd, quest_id, status == Q_ACTIVE ? true : false);
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
- if( i != (--sd->avail_quests) ) {
|
|
|
|
|
|
+ // 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;
|
|
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));
|
|
|
|
|
|
+
|
|
|
|
+ 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);
|
|
clif_quest_delete(sd, quest_id);
|
|
@@ -265,7 +302,22 @@ int quest_update_status(TBL_PC * sd, int quest_id, quest_state status) {
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
-int quest_check(TBL_PC * sd, int quest_id, quest_check_type type) {
|
|
|
|
|
|
+/**
|
|
|
|
+ * 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
|
|
|
|
+ */
|
|
|
|
+int quest_check(TBL_PC *sd, int quest_id, enum quest_check_type type) {
|
|
int i;
|
|
int i;
|
|
|
|
|
|
ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].quest_id == quest_id);
|
|
ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].quest_id == quest_id);
|
|
@@ -277,18 +329,18 @@ int quest_check(TBL_PC * sd, int quest_id, quest_check_type type) {
|
|
return sd->quest_log[i].state;
|
|
return sd->quest_log[i].state;
|
|
case PLAYTIME:
|
|
case PLAYTIME:
|
|
return (sd->quest_log[i].time < (unsigned int)time(NULL) ? 2 : sd->quest_log[i].state == Q_COMPLETE ? 1 : 0);
|
|
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 == 0 || sd->quest_log[i].state == 1 ) {
|
|
|
|
- int j;
|
|
|
|
- ARR_FIND(0, MAX_QUEST_OBJECTIVES, j, sd->quest_log[i].count[j] < quest_db[sd->quest_index[i]].count[j]);
|
|
|
|
- if( j == MAX_QUEST_OBJECTIVES )
|
|
|
|
- return 2;
|
|
|
|
- if( sd->quest_log[i].time > 0 && sd->quest_log[i].time < (unsigned int)time(NULL) )
|
|
|
|
- return 1;
|
|
|
|
- return 0;
|
|
|
|
- } else
|
|
|
|
- return 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, MAX_QUEST_OBJECTIVES, j, sd->quest_log[i].count[j] < qi->count[j]);
|
|
|
|
+ if( j == MAX_QUEST_OBJECTIVES )
|
|
|
|
+ return 2;
|
|
|
|
+ if( sd->quest_log[i].time < (unsigned int)time(NULL) )
|
|
|
|
+ return 1;
|
|
}
|
|
}
|
|
|
|
+ return 0;
|
|
default:
|
|
default:
|
|
ShowError("quest_check_quest: Unknown parameter %d",type);
|
|
ShowError("quest_check_quest: Unknown parameter %d",type);
|
|
break;
|
|
break;
|
|
@@ -297,79 +349,156 @@ int quest_check(TBL_PC * sd, int quest_id, quest_check_type type) {
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * Loads quests from the quest db.
|
|
|
|
+ *
|
|
|
|
+ * @return Number of loaded quests, or -1 if the file couldn't be read.
|
|
|
|
+ */
|
|
int quest_read_db(void) {
|
|
int quest_read_db(void) {
|
|
- const char* dbsubpath[] = {
|
|
|
|
- "",
|
|
|
|
- DBIMPORT"/",
|
|
|
|
- };
|
|
|
|
- int f;
|
|
|
|
-
|
|
|
|
- for(f=0; f<ARRAYLENGTH(dbsubpath); f++){
|
|
|
|
- FILE *fp;
|
|
|
|
- char line[1024];
|
|
|
|
- int i,j,k = 0;
|
|
|
|
- char *str[20],*p,*np;
|
|
|
|
- char filename[256];
|
|
|
|
-
|
|
|
|
- sprintf(filename, "%s/%s%s", db_path,dbsubpath[f],"quest_db.txt");
|
|
|
|
- if( (fp=fopen(filename,"r"))==NULL ){
|
|
|
|
- if(f==0) ShowError("can't read %s\n", filename);
|
|
|
|
- return -1;
|
|
|
|
- }
|
|
|
|
|
|
+ //@TODO[Haru]: This duplicates some sv_readdb functionalities, and it would be
|
|
|
|
+ //nice if it could be replaced by it. The reason why it wasn't is probably
|
|
|
|
+ //because we need to accept commas (which is also used as delimiter) in the
|
|
|
|
+ //last field (quest name), and sv_readdb isn't capable of doing so.
|
|
|
|
+ FILE *fp;
|
|
|
|
+ char line[1024];
|
|
|
|
+ int i, count = 0;
|
|
|
|
+ char *str[20], *p, *np;
|
|
|
|
+ struct quest_db entry;
|
|
|
|
+
|
|
|
|
+ sprintf(line, "%s/quest_db.txt", db_path);
|
|
|
|
+ if( (fp = fopen(line, "r")) == NULL ) {
|
|
|
|
+ ShowError("can't read %s\n", line);
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
|
|
- while(fgets(line, sizeof(line), fp)) {
|
|
|
|
- if (k == MAX_QUEST_DB) {
|
|
|
|
- ShowError("quest_read_db: Too many entries specified in %s/quest_db.txt!\n", db_path);
|
|
|
|
|
|
+ while( fgets(line, sizeof(line), fp) ) {
|
|
|
|
+ if( line[0] == '/' && line[1] == '/' )
|
|
|
|
+ continue;
|
|
|
|
+ memset(str, 0, sizeof(str));
|
|
|
|
+
|
|
|
|
+ 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;
|
|
break;
|
|
- }
|
|
|
|
- if(line[0]=='/' && line[1]=='/')
|
|
|
|
|
|
+ else {
|
|
|
|
+ ShowError("quest_read_db: insufficient columns in line %s\n", line);
|
|
continue;
|
|
continue;
|
|
- memset(str,0,sizeof(str));
|
|
|
|
-
|
|
|
|
- for( j = 0, p = line; j < 8; j++ ) {
|
|
|
|
- if( ( np = strchr(p,',') ) != NULL ) {
|
|
|
|
- str[j] = p;
|
|
|
|
- *np = 0;
|
|
|
|
- p = np + 1;
|
|
|
|
- }
|
|
|
|
- else if (str[0] == NULL)
|
|
|
|
- continue;
|
|
|
|
- else {
|
|
|
|
- ShowError("quest_read_db: insufficient columns in line %s\n", line);
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
- if(str[0]==NULL)
|
|
|
|
- continue;
|
|
|
|
|
|
+ }
|
|
|
|
+ if( str[0] == NULL )
|
|
|
|
+ continue;
|
|
|
|
|
|
- memset(&quest_db[k], 0, sizeof(quest_db[0]));
|
|
|
|
|
|
+ memset(&entry, 0, sizeof(entry));
|
|
|
|
|
|
- quest_db[k].id = atoi(str[0]);
|
|
|
|
- quest_db[k].time = atoi(str[1]);
|
|
|
|
|
|
+ entry.id = atoi(str[0]);
|
|
|
|
|
|
- for( i = 0; i < MAX_QUEST_OBJECTIVES; i++ ) {
|
|
|
|
- quest_db[k].mob[i] = atoi(str[2*i+2]);
|
|
|
|
- quest_db[k].count[i] = atoi(str[2*i+3]);
|
|
|
|
|
|
+ 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( !quest_db[k].mob[i] || !quest_db[k].count[i] )
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
|
|
+ entry.time = atoi(str[1]);
|
|
|
|
|
|
- quest_db[k].num_objectives = i;
|
|
|
|
|
|
+ for( i = 0; i < MAX_QUEST_OBJECTIVES; i++ ) {
|
|
|
|
+ entry.mob[i] = atoi(str[2 * i + 2]);
|
|
|
|
+ entry.count[i] = atoi(str[2 * i + 3]);
|
|
|
|
|
|
- k++;
|
|
|
|
|
|
+ if( !entry.mob[i] || !entry.count[i] )
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
- fclose(fp);
|
|
|
|
- ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", k, filename);
|
|
|
|
|
|
+
|
|
|
|
+ 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");
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * 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
|
|
|
|
+ */
|
|
|
|
+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.
|
|
|
|
+ */
|
|
|
|
+void quest_clear_db(void) {
|
|
|
|
+ int i;
|
|
|
|
+
|
|
|
|
+ for( i = 0; i < MAX_QUEST_DB; i++ ) {
|
|
|
|
+ if( quest_db_data[i] ) {
|
|
|
|
+ aFree(quest_db_data[i]);
|
|
|
|
+ quest_db_data[i] = NULL;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Initializes the quest interface.
|
|
|
|
+ */
|
|
void do_init_quest(void) {
|
|
void do_init_quest(void) {
|
|
quest_read_db();
|
|
quest_read_db();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * Finalizes the quest interface before shutdown.
|
|
|
|
+ */
|
|
|
|
+void do_final_quest(void) {
|
|
|
|
+ memset(&quest_dummy, 0, sizeof(quest_dummy));
|
|
|
|
+
|
|
|
|
+ quest_clear_db();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Reloads the quest database.
|
|
|
|
+ */
|
|
void do_reload_quest(void) {
|
|
void do_reload_quest(void) {
|
|
- memset(&quest_db, 0, sizeof(quest_db));
|
|
|
|
|
|
+ memset(&quest_dummy, 0, sizeof(quest_dummy));
|
|
|
|
+
|
|
|
|
+ quest_clear_db();
|
|
|
|
+
|
|
quest_read_db();
|
|
quest_read_db();
|
|
|
|
+
|
|
|
|
+ //Update quest data for players, to ensure no entries about removed quests are left over.
|
|
|
|
+ map_foreachpc(&quest_reload_check_sub);
|
|
}
|
|
}
|