Skip to content

Commit

Permalink
Quest DB update (core)
Browse files Browse the repository at this point in the history
* Change quest_db_data[] with fixed array to DBMap *questdb

Signed-off-by: Cydh Ramdh <house.bad@gmail.com>
  • Loading branch information
cydh committed Jan 24, 2015
1 parent 707403d commit 1d27526
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 48 deletions.
6 changes: 3 additions & 3 deletions src/map/clif.c
Original file line number Diff line number Diff line change
Expand Up @@ -15398,7 +15398,7 @@ void clif_quest_send_mission(struct map_session_data * sd)
WFIFOL(fd, 4) = sd->avail_quests;

for( i = 0; i < sd->avail_quests; i++ ) {
struct quest_db *qi = quest_db(sd->quest_log[i].quest_id);
struct quest_db *qi = quest_search(sd->quest_log[i].quest_id);

WFIFOL(fd, i*104+8) = sd->quest_log[i].quest_id;
WFIFOL(fd, i*104+12) = sd->quest_log[i].time - qi->time;
Expand All @@ -15423,7 +15423,7 @@ void clif_quest_add(struct map_session_data * sd, struct quest * qd)
{
int fd = sd->fd;
int i;
struct quest_db *qi = quest_db(qd->quest_id);
struct quest_db *qi = quest_search(qd->quest_id);

WFIFOHEAD(fd, packet_len(0x2b3));
WFIFOW(fd, 0) = 0x2b3;
Expand Down Expand Up @@ -15464,7 +15464,7 @@ void clif_quest_update_objective(struct map_session_data * sd, struct quest * qd
{
int fd = sd->fd;
int i;
struct quest_db *qi = quest_db(qd->quest_id);
struct quest_db *qi = quest_search(qd->quest_id);
int len = qi->num_objectives * 12 + 6;

WFIFOHEAD(fd, len);
Expand Down
2 changes: 1 addition & 1 deletion src/map/intif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1910,7 +1910,7 @@ void intif_parse_questlog(int fd)
CREATE(sd->quest_log, struct quest, num_received);

for(i = 0; i < num_received; i++) {
if(quest_db(received[i].quest_id) == &quest_dummy) {
if(quest_search(received[i].quest_id) == &quest_dummy) {
ShowError("intif_parse_QuestLog: quest %d not found in DB.\n", received[i].quest_id);
continue;
}
Expand Down
81 changes: 44 additions & 37 deletions src/map/quest.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@

#include <stdlib.h>

static DBMap *questdb;

/**
* 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)
struct quest_db *quest_search(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];
struct quest_db *quest = (struct quest_db *)idb_get(questdb, quest_id);
if (!quest)
return &quest_dummy;
return quest;
}

/**
Expand Down Expand Up @@ -61,7 +63,7 @@ int quest_pc_login(TBL_PC *sd)
int quest_add(TBL_PC *sd, int quest_id)
{
int n;
struct quest_db *qi = quest_db(quest_id);
struct quest_db *qi = quest_search(quest_id);

if( qi == &quest_dummy ) {
ShowError("quest_add: quest %d not found in DB.\n", quest_id);
Expand Down Expand Up @@ -111,7 +113,7 @@ int quest_add(TBL_PC *sd, int quest_id)
int quest_change(TBL_PC *sd, int qid1, int qid2)
{
int i;
struct quest_db *qi = quest_db(qid2);
struct quest_db *qi = quest_search(qid2);

if( qi == &quest_dummy ) {
ShowError("quest_change: quest %d not found in DB.\n", qid2);
Expand Down Expand Up @@ -236,7 +238,7 @@ void quest_update_objective(TBL_PC *sd, int mob)
if( sd->quest_log[i].state != Q_ACTIVE ) // Skip inactive quests
continue;

qi = quest_db(sd->quest_log[i].quest_id);
qi = quest_search(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] ) {
Expand Down Expand Up @@ -322,7 +324,7 @@ int quest_check(TBL_PC *sd, int quest_id, enum quest_check_type type)
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);
struct quest_db *qi = quest_search(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 )
Expand All @@ -349,15 +351,13 @@ int quest_read_db(void)
"",
DBIMPORT"/",
};
int f;
uint8 f;

for (f = 0; f < ARRAYLENGTH(dbsubpath); f++) {
FILE *fp;
char line[1024];
int i, count = 0;
char *str[20], *p, *np;
uint32 count = 0;
char filename[256];
struct quest_db entry;

sprintf(filename, "%s/%s%s", db_path, dbsubpath[f], "quest_db.txt");
if( (fp = fopen(filename, "r")) == NULL ) {
Expand All @@ -368,6 +368,10 @@ int quest_read_db(void)
}

while( fgets(line, sizeof(line), fp) ) {
struct quest_db *quest = NULL, entry;
char *str[9], *p, *np;
uint8 i;

if( line[0] == '/' && line[1] == '/' )
continue;

Expand All @@ -389,31 +393,36 @@ int quest_read_db(void)
if( str[0] == NULL )
continue;

memset(&entry, 0, sizeof(entry));

memset(&entry, 0, sizeof(struct quest_db));
entry.id = atoi(str[0]);

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);
if( entry.id < 0 || entry.id >= INT_MAX ) {
ShowError("quest_read_db: Invalid quest ID '%d' in line '%s' (min: 0, max: %d.)\n", entry.id, line, INT_MAX);
continue;
}

if (!(quest = (struct quest_db *)idb_get(questdb, entry.id))) {
CREATE(quest, struct quest_db, 1);
}

entry.time = atoi(str[1]);

for( i = 0; i < MAX_QUEST_OBJECTIVES; i++ ) {
entry.mob[i] = atoi(str[2 * i + 2]);
entry.count[i] = atoi(str[2 * i + 3]);
entry.mob[i] = (uint16)atoi(str[2 * i + 2]);
entry.count[i] = (uint16)atoi(str[2 * i + 3]);

if( !entry.mob[i] || !entry.count[i] )
break;
}

entry.num_objectives = i;

if( quest_db_data[entry.id] == NULL )
quest_db_data[entry.id] = aMalloc(sizeof(struct quest_db));
//StringBuf_Init(&entry.name);
//StringBuf_Printf(&entry.name, "%s", str[7]);

memcpy(quest_db_data[entry.id], &entry, sizeof(struct quest_db));
if (!quest->id) {
memcpy(quest, &entry, sizeof(entry));
idb_put(questdb, quest->id, quest);
}
count++;
}

Expand All @@ -439,7 +448,7 @@ int quest_reload_check_sub(struct map_session_data *sd, va_list ap)

j = 0;
for( i = 0; i < sd->num_quests; i++ ) {
struct quest_db *qi = quest_db(sd->quest_log[i].quest_id);
struct quest_db *qi = quest_search(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
Expand All @@ -465,23 +474,23 @@ int quest_reload_check_sub(struct map_session_data *sd, va_list ap)
/**
* 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;
}
}
static int questdb_free(DBKey key, DBData *data, va_list ap) {
struct quest_db *quest = db_data2ptr(data);
if (!quest)
return 0;
//if (&quest->name)
// StringBuf_Destroy(&quest->name);
aFree(quest);
return 1;
}

/**
* Initializes the quest interface.
*/
void do_init_quest(void)
{
questdb = idb_alloc(DB_OPT_BASE);
quest_read_db();
}

Expand All @@ -491,8 +500,7 @@ void do_init_quest(void)
void do_final_quest(void)
{
memset(&quest_dummy, 0, sizeof(quest_dummy));

quest_clear_db();
questdb->destroy(questdb, questdb_free);
}

/**
Expand All @@ -501,8 +509,7 @@ void do_final_quest(void)
void do_reload_quest(void)
{
memset(&quest_dummy, 0, sizeof(quest_dummy));

quest_clear_db();
questdb->clear(questdb, questdb_free);

quest_read_db();

Expand Down
13 changes: 6 additions & 7 deletions src/map/quest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
struct quest_db {
int id;
unsigned int time;
int mob[MAX_QUEST_OBJECTIVES];
int count[MAX_QUEST_OBJECTIVES];
int num_objectives;
//char name[NAME_LENGTH];
uint16 mob[MAX_QUEST_OBJECTIVES];
uint16 count[MAX_QUEST_OBJECTIVES];
uint8 num_objectives;
//StringBuf name;
};

struct quest_db *quest_db_data[MAX_QUEST_DB]; ///< Quest database
struct quest_db quest_dummy; ///< Dummy entry for invalid quest lookups
struct quest_db quest_dummy; ///< Dummy entry for invalid quest lookups

// Questlog check types
enum quest_check_type {
Expand All @@ -36,7 +35,7 @@ int quest_update_status(TBL_PC * sd, int quest_id, enum quest_state status);
int quest_check(TBL_PC * sd, int quest_id, enum quest_check_type type);
void quest_clear(void);

struct quest_db *quest_db(int quest_id);
struct quest_db *quest_search(int quest_id);

void do_init_quest(void);
void do_final_quest(void);
Expand Down

1 comment on commit 1d27526

@Lemongrass3110
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just add a comment with:
// ignore dummy name
or something like that and drop the whole commented name things.

The rest really looks nice. /slur

Please sign in to comment.