diff options
author | Inkfish <Inkfish@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2009-07-25 20:55:35 +0000 |
---|---|---|
committer | Inkfish <Inkfish@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2009-07-25 20:55:35 +0000 |
commit | 34b37179c0895f0b0acc4377bf2bfe5edfcff9ce (patch) | |
tree | d650d9bc108404fa0949d5ec2754d96cde31926b /src/map/quest.c | |
parent | a93b8b9c2537bd8359d647ae23e90d084d019535 (diff) | |
download | hercules-34b37179c0895f0b0acc4377bf2bfe5edfcff9ce.tar.gz hercules-34b37179c0895f0b0acc4377bf2bfe5edfcff9ce.tar.bz2 hercules-34b37179c0895f0b0acc4377bf2bfe5edfcff9ce.tar.xz hercules-34b37179c0895f0b0acc4377bf2bfe5edfcff9ce.zip |
Rewrote Quest Log system.
git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@13959 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/map/quest.c')
-rw-r--r-- | src/map/quest.c | 303 |
1 files changed, 236 insertions, 67 deletions
diff --git a/src/map/quest.c b/src/map/quest.c index 057c46d7c..ccde85a07 100644 --- a/src/map/quest.c +++ b/src/map/quest.c @@ -33,46 +33,61 @@ #include <stdarg.h> #include <time.h> +#define MAX_QUEST 25 + //Send quest info on login int quest_pc_login(TBL_PC * sd) { - if(sd->num_quests == 0) return 1; clif_send_questlog(sd); clif_send_questlog_info(sd); + return 0; } -int quest_add(TBL_PC * sd, struct quest * qd) +int quest_add(TBL_PC * sd, int quest_id) { - int i; + int i, j, count; - //Search to see if this quest exists - ARR_FIND(0, MAX_QUEST, i, sd->quest_log[i].quest_id == qd->quest_id); + if( quest_check_quest(sd, quest_id, HAVEQUEST) >= 0 ) + { + ShowError("quest_add: you already have quest %d.\n",quest_id); + return -1; + } - //Already have this quest - if(i!=MAX_QUEST) - return 1; + if( (j = quest_search_db(quest_id)) < 0 ) + { + ShowError("quest_add: quest %d not found in DB.\n",quest_id); + return -1; + } - //Find empty quest log spot - ARR_FIND(0, MAX_QUEST, i, sd->quest_log[i].quest_id == 0); + if( sd->num_quests >= MAX_QUEST_DB || sd->avail_quests >= MAX_QUEST ) + { + ShowError("quest_add: your quest log is full.(max quests: %d, max incompleted quests: %d)\n", MAX_QUEST_DB, MAX_QUEST); + return 1; + } - //Quest log is full - if(i == MAX_QUEST) - 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)); + 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; + for( count = 0; count < MAX_QUEST_OBJECTIVES && quest_db[j].mob[count]; count++ ) + sd->quest_log[i].mob[count] = quest_db[j].mob[count]; + sd->quest_log[i].num_objectives = count; - //Copy over quest data - memcpy(&sd->quest_log[i], qd, sizeof(struct quest)); sd->num_quests++; + sd->avail_quests++; //Notify inter server - intif_quest_add(sd->status.char_id, qd); + intif_quest_add(sd->status.char_id, &sd->quest_log[i]); return 0; - } int quest_add_ack(int char_id, int quest_id, int success) @@ -83,26 +98,21 @@ int quest_add_ack(int char_id, int quest_id, int success) ///Player no longer on map if(!sd) return -1; + + ARR_FIND(0, sd->avail_quests, i, sd->quest_log[i].quest_id == quest_id); + if( i == sd->avail_quests ) + return -1; //Shouldn't happen! - //Search for quest - ARR_FIND(0, MAX_QUEST, i, sd->quest_log[i].quest_id == quest_id); - - //Quest not found, shouldn't happen? - if(i == MAX_QUEST) - return -1; - - if(success) - { - //Notify client - clif_send_quest_info(sd, &sd->quest_log[i]); - } + if( success ) + clif_send_quest_info(sd, &sd->quest_log[i]); //Notify client else { + 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)); + memset(&sd->quest_log[--sd->num_quests], 0, sizeof(struct quest)); ShowError("Quest %d for character %d could not be added!\n", quest_id, char_id); - - //Zero quest - memset(&sd->quest_log[i], 0, sizeof(struct quest)); - sd->num_quests--; + return 1; } return 0; @@ -110,25 +120,20 @@ int quest_add_ack(int char_id, int quest_id, int success) int quest_delete(TBL_PC * sd, int quest_id) { - - int i; - //Search for quest - ARR_FIND(0, MAX_QUEST, i, sd->quest_log[i].quest_id == quest_id); - - //Quest not found - if(i == MAX_QUEST) + if( quest_check_quest(sd, quest_id, HAVEQUEST) < 0 ) //Quest not found + { + ShowError("quest_delete: quest %d not found in your quest log.\n",quest_id); return -1; + } intif_quest_delete(sd->status.char_id, quest_id); return 0; - } int quest_delete_ack(int char_id, int quest_id, int success) { - int i; TBL_PC * sd = map_charid2sd(char_id); @@ -137,65 +142,229 @@ int quest_delete_ack(int char_id, int quest_id, int success) return -1; //Search for quest - ARR_FIND(0, MAX_QUEST, i, sd->quest_log[i].quest_id == quest_id); - - //Quest not found - if(i == MAX_QUEST) + ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].quest_id == quest_id); + if(i == sd->num_quests) //Shouldn't happen! return -1; if(success) { + if( sd->quest_log[i].state != Q_COMPLETE ) + 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)); + memset(&sd->quest_log[--sd->num_quests], 0, sizeof(struct quest)); + clif_send_quest_delete(sd, quest_id); + } + else + { + ShowError("Quest %d for character %d could not be deleted!\n", quest_id, char_id); + return 1; + } - //Zero quest - memset(&sd->quest_log[i], 0, sizeof(struct quest)); - sd->num_quests--; + return 0; +} - //Notify client - clif_send_quest_delete(sd, quest_id); +void quest_update_objective(TBL_PC * sd, int mob) +{ + int i,j; - return 1; + for( i = 0; i < sd->avail_quests; i++ ) + { + if( sd->quest_log[i].state != Q_ACTIVE ) + continue; + + for( j = 0; j < MAX_QUEST_OBJECTIVES; j++ ) + if( sd->quest_log[i].mob[j] == mob ) + { + sd->quest_log[i].count[j]++; + sd->save_quest[i] = true; + //clif_send_quest_info(sd, &sd->quest_log[i]); //TODO: Figure out the real packet [Inkfish] + //break; + } + } +} + +int quest_update_status(TBL_PC * sd, int quest_id, int status) +{ + int i; + //Search for quest + ARR_FIND(0, sd->avail_quests, i, sd->quest_log[i].quest_id == quest_id); // Only status of active and inactive quests can't be updated and completed quests can't [Inkfish] + if(i == sd->avail_quests) //Quest not found + { + ShowError("Quest %d not found in your quest log!\n", quest_id); + return -1; + } + + sd->quest_log[i].state = (quest_state)status; + if( status != Q_COMPLETE ) + { + clif_send_quest_status(sd, quest_id, (bool)status); + sd->save_quest[i] = true; } else - ShowError("Quest %d for character %d could not be deleted!\n", quest_id, char_id); + { + struct quest tmp_quest; + + clif_send_quest_delete(sd, quest_id); + sd->avail_quests--; + + 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)); + + sd->save_quest[sd->avail_quests] = true; + } return 0; +} + +int quest_save(TBL_PC * sd) +{ + int i; + for( i = 0; i < sd->num_quests; i++ ) + if( sd->save_quest[i] ) + intif_quest_save(sd->status.char_id, &sd->quest_log[i]); + + return 0; } -int quest_update_objective(TBL_PC * sd, int quest_id, int objective_num, const char * name, int count) +int quest_save_ack(int char_id, int quest_id, int success) { int i; + TBL_PC * sd = map_charid2sd(char_id); - //Search for quest - ARR_FIND(0, MAX_QUEST, i, sd->quest_log[i].quest_id == quest_id); - - //Quest not found - if(i == MAX_QUEST) + ///Player no longer on map + if(!sd) return -1; - memcpy(&sd->quest_log[i].objectives[objective_num].name, name, NAME_LENGTH); - sd->quest_log[i].objectives[objective_num].count = count; + //Search for quest + ARR_FIND(0, sd->num_quests, i, sd->quest_log[i].quest_id == quest_id); + if(i == sd->num_quests) //Quest not found + return -1; - //Notify client - clif_send_quest_info(sd, &sd->quest_log[i]); + if(success) + sd->save_quest[i] = false; + else + { + ShowError("Quest %d for character %d could not be saved!\n", quest_id, char_id); + return 1; + } return 0; } -bool quest_has_quest(TBL_PC * sd, int quest_id) +int quest_check_quest(TBL_PC * sd, int quest_id, quest_check_type type) { int i; - ARR_FIND(0, MAX_QUEST, 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) + 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: + { + int j = quest_search_db(quest_id); + + if( j < 0 ) + { + ShowError("quest_check_quest: quest not found in DB\n"); + return -1; + } + + if( sd->quest_log[i].count[0] < quest_db[j].count[0] || sd->quest_log[i].count[1] < quest_db[j].count[1] || sd->quest_log[i].count[2] < quest_db[j].count[2] ) + { + if( sd->quest_log[i].time < (unsigned int)time(NULL) ) + return 1; + + return 0; + } + + return 2; + } + default: + ShowError("quest_check_quest: Unknown parameter %d",type); + break; + } - return (i != MAX_QUEST); + return -1; } -int quest_update_status(TBL_PC * sd, int quest_id, bool status) +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; +} + +int quest_read_db(void) +{ + FILE *fp; + char line[1024]; + int j,k = 0; + char *str[20],*p,*np; + + 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(line[0]=='/' && line[1]=='/') + 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 + { + ShowError("quest_read_db: insufficient columes in line %s\n", line); + continue; + } + } + if(str[0]==NULL) + continue; + + memset(&quest_db[k], 0, sizeof(quest_db[0])); + + quest_db[k].id = atoi(str[0]); + quest_db[k].time = atoi(str[1]); + quest_db[k].mob[0] = atoi(str[2]); + quest_db[k].count[0] = atoi(str[3]); + quest_db[k].mob[1] = atoi(str[4]); + quest_db[k].count[1] = atoi(str[5]); + quest_db[k].mob[2] = atoi(str[6]); + quest_db[k].count[2] = atoi(str[7]); + //memcpy(quest_db[k].name, str[8], sizeof(str[8])); + k++; + } + fclose(fp); + ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n","quest_db.txt"); return 0; } + +void do_init_quest(void) +{ + quest_read_db(); +} |