summaryrefslogtreecommitdiff
path: root/src/map/intif.c
diff options
context:
space:
mode:
authorHaru <haru@dotalux.com>2013-11-27 03:21:45 +0100
committerHaru <haru@dotalux.com>2013-12-03 16:28:08 +0100
commit6f55c00e72ca6db130a84fe92218f73a777428f4 (patch)
treee3f25d1ad5b3dbb06371941fc9fd8cb66a5411a9 /src/map/intif.c
parent470ab15023f09dc823e8ce8ac818e0bbe8a95aef (diff)
downloadhercules-6f55c00e72ca6db130a84fe92218f73a777428f4.tar.gz
hercules-6f55c00e72ca6db130a84fe92218f73a777428f4.tar.bz2
hercules-6f55c00e72ca6db130a84fe92218f73a777428f4.tar.xz
hercules-6f55c00e72ca6db130a84fe92218f73a777428f4.zip
Questlog fixes
- Improved memory usage of the quest log system. (saves up to 75kB per online character). Fixes issue #133. - Fixed various issues with quest entries disappearing from characters without an apparent reason, or monster kill counters getting stuck - the issues were caused by a de-synchronization between the two parallel questlog arrays in map_session_data. - Added some code documentation. - Thanks to Ind. Signed-off-by: Haru <haru@dotalux.com>
Diffstat (limited to 'src/map/intif.c')
-rw-r--r--src/map/intif.c100
1 files changed, 66 insertions, 34 deletions
diff --git a/src/map/intif.c b/src/map/intif.c
index 36ae753db..8fdc8e3a3 100644
--- a/src/map/intif.c
+++ b/src/map/intif.c
@@ -1311,50 +1311,79 @@ QUESTLOG SYSTEM FUNCTIONS
***************************************/
-int intif_request_questlog(TBL_PC *sd)
-{
+/**
+ * Requests a character's quest log entries to the inter server.
+ *
+ * @param sd Character's data
+ */
+void intif_request_questlog(TBL_PC *sd) {
WFIFOHEAD(inter_fd,6);
WFIFOW(inter_fd,0) = 0x3060;
WFIFOL(inter_fd,2) = sd->status.char_id;
WFIFOSET(inter_fd,6);
- return 0;
}
+/**
+ * Parses the received quest log entries for a character from the inter server.
+ *
+ * Received in reply to the requests made by intif_request_questlog.
+ *
+ * @see intif_parse
+ */
void intif_parse_QuestLog(int fd) {
- int char_id = RFIFOL(fd, 4);
- int i;
- TBL_PC * sd = map->charid2sd(char_id);
+ int char_id = RFIFOL(fd, 4), num_received = (RFIFOW(fd, 2)-8)/sizeof(struct quest);
+ TBL_PC *sd = map->charid2sd(char_id);
- //User not online anymore
- if(!sd)
+ if (!sd) // User not online anymore
return;
- sd->avail_quests = sd->num_quests = (RFIFOW(fd, 2)-8)/sizeof(struct quest);
-
- memset(&sd->quest_log, 0, sizeof(sd->quest_log));
-
- for( i = 0; i < sd->num_quests; i++ )
- {
- memcpy(&sd->quest_log[i], RFIFOP(fd, i*sizeof(struct quest)+8), sizeof(struct quest));
-
- sd->quest_index[i] = quest->search_db(sd->quest_log[i].quest_id);
-
- if( sd->quest_index[i] < 0 )
- {
- ShowError("intif_parse_QuestLog: quest %d not found in DB.\n",sd->quest_log[i].quest_id);
- sd->avail_quests--;
- sd->num_quests--;
- i--;
- continue;
+ if (num_received == 0) {
+ if (sd->quest_log) {
+ aFree(sd->quest_log);
+ sd->quest_log = NULL;
+ }
+ sd->num_quests = sd->avail_quests = 0;
+ } else {
+ struct quest *received = (struct quest *)RFIFOP(fd, 8);
+ int i, k = num_received;
+ if (sd->quest_log) {
+ RECREATE(sd->quest_log, struct quest, num_received);
+ } else {
+ CREATE(sd->quest_log, struct quest, num_received);
}
- if( sd->quest_log[i].state == Q_COMPLETE )
- sd->avail_quests--;
+ for (i = 0; i < num_received; i++) {
+ if( quest->db(received[i].quest_id) == &quest->dummy ) {
+ ShowError("intif_parse_QuestLog: quest %d not found in DB.\n", received[i].quest_id);
+ continue;
+ }
+ if (received->state != Q_COMPLETE) {
+ // Insert at the beginning
+ memcpy(&sd->quest_log[sd->avail_quests++], &received[i], sizeof(struct quest));
+ } else {
+ // Insert at the end
+ memcpy(&sd->quest_log[--k], &received[i], sizeof(struct quest));
+ }
+ sd->num_quests++;
+ }
+ if (sd->avail_quests < k) {
+ // sd->avail_quests and k didn't meet in the middle: some entries were skipped
+ if (k < num_received) // Move the entries at the end to fill the gap
+ memmove(&sd->quest_log[k], &sd->quest_log[sd->avail_quests], sizeof(struct quest)*(num_received - k));
+ sd->quest_log = aRealloc(sd->quest_log, sizeof(struct quest)*sd->num_quests);
+ }
}
quest->pc_login(sd);
}
+/**
+ * Parses the quest log save ack for a character from the inter server.
+ *
+ * Received in reply to the requests made by intif_quest_save.
+ *
+ * @see intif_parse
+ */
void intif_parse_QuestSave(int fd) {
int cid = RFIFOL(fd, 2);
TBL_PC *sd = map->id2sd(cid);
@@ -1365,21 +1394,24 @@ void intif_parse_QuestSave(int fd) {
sd->save_quest = false;
}
-int intif_quest_save(TBL_PC *sd)
-{
- int len;
+/**
+ * Requests to the inter server to save a character's quest log entries.
+ *
+ * @param sd Character's data
+ * @return 0 in case of success, nonzero otherwise
+ */
+int intif_quest_save(TBL_PC *sd) {
+ int len = sizeof(struct quest)*sd->num_quests + 8;
if(intif->CheckForCharServer())
- return 0;
-
- len = sizeof(struct quest)*sd->num_quests + 8;
+ return 1;
WFIFOHEAD(inter_fd, len);
WFIFOW(inter_fd,0) = 0x3061;
WFIFOW(inter_fd,2) = len;
WFIFOL(inter_fd,4) = sd->status.char_id;
if( sd->num_quests )
- memcpy(WFIFOP(inter_fd,8), &sd->quest_log, sizeof(struct quest)*sd->num_quests);
+ memcpy(WFIFOP(inter_fd,8), sd->quest_log, sizeof(struct quest)*sd->num_quests);
WFIFOSET(inter_fd, len);
return 0;