summaryrefslogtreecommitdiff
path: root/src/map
diff options
context:
space:
mode:
Diffstat (limited to 'src/map')
-rw-r--r--src/map/HPMmap.c1
-rw-r--r--src/map/Makefile.in4
-rw-r--r--src/map/achievement.c1983
-rw-r--r--src/map/achievement.h289
-rw-r--r--src/map/atcommand.c6
-rw-r--r--src/map/chat.c5
-rw-r--r--src/map/chrif.c2
-rw-r--r--src/map/clif.c357
-rw-r--r--src/map/clif.h8
-rw-r--r--src/map/intif.c103
-rw-r--r--src/map/intif.h5
-rw-r--r--src/map/map.c4
-rw-r--r--src/map/map.h11
-rw-r--r--src/map/mob.c7
-rw-r--r--src/map/npc.c4
-rw-r--r--src/map/packets.h4
-rw-r--r--src/map/packets_struct.h66
-rw-r--r--src/map/party.c4
-rw-r--r--src/map/pc.c48
-rw-r--r--src/map/pc.h8
-rw-r--r--src/map/pet.c7
-rw-r--r--src/map/script.c86
-rw-r--r--src/map/unit.c2
23 files changed, 2941 insertions, 73 deletions
diff --git a/src/map/HPMmap.c b/src/map/HPMmap.c
index e4640d09d..091a53311 100644
--- a/src/map/HPMmap.c
+++ b/src/map/HPMmap.c
@@ -47,6 +47,7 @@
#include "common/sysinfo.h"
#include "common/timer.h"
#include "common/utils.h"
+#include "map/achievement.h"
#include "map/atcommand.h"
#include "map/battle.h"
#include "map/battleground.h"
diff --git a/src/map/Makefile.in b/src/map/Makefile.in
index 7c04a4f37..1bef380e1 100644
--- a/src/map/Makefile.in
+++ b/src/map/Makefile.in
@@ -40,14 +40,14 @@ MT19937AR_D = $(THIRDPARTY_D)/mt19937ar
MT19937AR_OBJ = $(MT19937AR_D)/mt19937ar.o
MT19937AR_H = $(MT19937AR_D)/mt19937ar.h
-MAP_C = atcommand.c battle.c battleground.c buyingstore.c channel.c chat.c \
+MAP_C = achievement.c atcommand.c battle.c battleground.c buyingstore.c channel.c chat.c \
chrif.c clan.c clif.c date.c duel.c elemental.c guild.c homunculus.c HPMmap.c \
instance.c intif.c irc-bot.c itemdb.c log.c mail.c map.c mapreg_sql.c \
mercenary.c mob.c npc.c npc_chat.c party.c path.c pc.c pc_groups.c \
pet.c quest.c rodex.c script.c searchstore.c skill.c status.c storage.c \
trade.c unit.c vending.c
MAP_OBJ = $(addprefix obj_sql/, $(patsubst %c,%o,$(MAP_C)))
-MAP_H = atcommand.h battle.h battleground.h buyingstore.h channel.h chat.h \
+MAP_H = achievement.h atcommand.h battle.h battleground.h buyingstore.h channel.h chat.h \
chrif.h clan.h clif.h date.h duel.h elemental.h guild.h homunculus.h HPMmap.h \
instance.h intif.h irc-bot.h itemdb.h log.h mail.h map.h mapreg.h \
mercenary.h messages.h messages_main.h messages_re.h messages_zero.h \
diff --git a/src/map/achievement.c b/src/map/achievement.c
new file mode 100644
index 000000000..0369b0fb5
--- /dev/null
+++ b/src/map/achievement.c
@@ -0,0 +1,1983 @@
+/**
+* This file is part of Hercules.
+* http://herc.ws - http://github.com/HerculesWS/Hercules
+*
+* Copyright (C) 2017 Hercules Dev Team
+* Copyright (C) Smokexyz
+* Copyright (C) Dastgir
+*
+* Hercules is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+#define HERCULES_CORE
+
+#include "map/achievement.h"
+
+#include "map/itemdb.h"
+#include "map/mob.h"
+#include "map/party.h"
+#include "map/pc.h"
+#include "map/script.h"
+
+#include "common/conf.h"
+#include "common/db.h"
+#include "common/memmgr.h"
+#include "common/nullpo.h"
+#include "common/showmsg.h"
+#include "common/strlib.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static struct achievement_interface achievement_s;
+struct achievement_interface *achievement;
+
+/**
+ * Retrieve an achievement via it's ID.
+ * @param aid as the achievement ID.
+ * @return NULL or pointer to the achievement data.
+ */
+static const struct achievement_data *achievement_get(int aid)
+{
+ return (struct achievement_data *) idb_get(achievement->db, aid);
+}
+
+/**
+ * Searches the provided achievement data for an achievement,
+ * optionally creates a new one if no key exists.
+ * @param[in] sd a pointer to map_session_data.
+ * @param[in] aid ID of the achievement provided as key.
+ * @param[in] create new key creation flag.
+ * @return pointer to the session's achievement data.
+ */
+static struct achievement *achievement_ensure(struct map_session_data *sd, const struct achievement_data *ad)
+{
+ struct achievement *s_ad = NULL;
+ int i = 0;
+
+ nullpo_retr(NULL, sd);
+ nullpo_retr(NULL, ad);
+
+ /* Lookup for achievement entry */
+ ARR_FIND(0, VECTOR_LENGTH(sd->achievement), i, (s_ad = &VECTOR_INDEX(sd->achievement, i)) && s_ad->id == ad->id);
+
+ if (i == VECTOR_LENGTH(sd->achievement)) {
+ struct achievement ta = { 0 };
+ ta.id = ad->id;
+
+ VECTOR_ENSURE(sd->achievement, 1, 1);
+ VECTOR_PUSH(sd->achievement, ta);
+
+ s_ad = &VECTOR_LAST(sd->achievement);
+ }
+
+ return s_ad;
+}
+
+/**
+ * Calculates the achievement's totals via reference.
+ * @param[in] sd pointer to map_session_data
+ * @param[out] tota_points pointer to total points var
+ * @param[out] completed pointer to total var
+ * @param[out] rank pointer to completed var
+ * @param[out] curr_rank_points pointer to achievement rank var
+ */
+static void achievement_calculate_totals(const struct map_session_data *sd, int *total_points, int *completed, int *rank, int *curr_rank_points)
+{
+ const struct achievement *a = NULL;
+ const struct achievement_data *ad = NULL;
+ int tmp_curr_points = 0;
+ int tmp_total_points = 0;
+ int tmp_total_completed = 0;
+ int tmp_rank = 0;
+ int i = 0;
+
+ nullpo_retv(sd);
+
+ for (i = 0; i < VECTOR_LENGTH(sd->achievement); i++) {
+ a = &VECTOR_INDEX(sd->achievement, i);
+
+ if ((ad = achievement->get(a->id)) == NULL)
+ continue;
+
+ if (a->completed_at != 0) {
+ tmp_total_points += ad->points;
+ tmp_total_completed++;
+ }
+ }
+
+ if (tmp_total_points > 0) {
+ tmp_curr_points = tmp_total_points;
+ for (i = 0; i < MAX_ACHIEVEMENT_RANKS
+ && tmp_curr_points >= VECTOR_INDEX(achievement->rank_exp, i)
+ && i < VECTOR_LENGTH(achievement->rank_exp); i++) {
+ tmp_curr_points -= VECTOR_INDEX(achievement->rank_exp, i);
+ tmp_rank++;
+ }
+ }
+
+ if (total_points != NULL)
+ *total_points = tmp_total_points;
+
+ if (completed != NULL)
+ *completed = tmp_total_completed;
+
+ if (rank != NULL)
+ *rank = tmp_rank;
+
+ if (curr_rank_points != NULL)
+ *curr_rank_points = tmp_curr_points;
+}
+
+/**
+ * Checks whether all objectives of the achievement are completed.
+ * @param[in] sd as the map_session_data pointer
+ * @param[in] ad as the achievement_data pointer
+ * @return true if complete, false if not.
+ */
+static bool achievement_check_complete(struct map_session_data *sd, const struct achievement_data *ad)
+{
+ int i;
+ struct achievement *ach = NULL;
+
+ nullpo_retr(false, sd);
+ nullpo_retr(false, ad);
+
+ if ((ach = achievement->ensure(sd, ad)) == NULL)
+ return false;
+ for (i = 0; i < VECTOR_LENGTH(ad->objective); i++)
+ if (ach->objective[i] < VECTOR_INDEX(ad->objective, i).goal)
+ return false;
+
+ return true;
+}
+
+/**
+ * Compares the progress of an objective against it's goal.
+ * Increments the progress of the objective by the specified amount, towards the goal.
+ * @param sd [in] as a pointer to map_session_data
+ * @param ad [in] as a pointer to the achievement_data
+ * @param obj_idx [in] as the index of the objective.
+ * @param progress [in] as the progress of the objective to be added.
+ */
+static void achievement_progress_add(struct map_session_data *sd, const struct achievement_data *ad, unsigned int obj_idx, int progress)
+{
+ struct achievement *ach = NULL;
+
+ nullpo_retv(sd);
+ nullpo_retv(ad);
+
+ Assert_retv(progress != 0);
+ Assert_retv(obj_idx < VECTOR_LENGTH(ad->objective));
+
+ if ((ach = achievement->ensure(sd, ad)) == NULL)
+ return;
+
+ if (ach->completed_at)
+ return; // ignore the call if the achievement is completed.
+
+ // Check and increment the objective count.
+ if (!ach->objective[obj_idx] || ach->objective[obj_idx] < VECTOR_INDEX(ad->objective, obj_idx).goal) {
+ ach->objective[obj_idx] = min(progress + ach->objective[obj_idx], VECTOR_INDEX(ad->objective, obj_idx).goal);
+
+ // Check if the Achievement is complete.
+ if (achievement->check_complete(sd, ad)) {
+ achievement->validate_achieve(sd, ad->id);
+ ach->completed_at = time(NULL);
+ }
+
+ // update client.
+ clif->achievement_send_update(sd->fd, sd, ad);
+ }
+}
+
+/**
+ * Compare an absolute progress value against the goal of an objective.
+ * Does not add/increase progress.
+ * @param sd [in] pointer to map-server session data.
+ * @param ad [in] pointer to achievement data.
+ * @param obj_idx [in] index of the objective in question.
+ * @param progress progress of the objective in question.
+ */
+static void achievement_progress_set(struct map_session_data *sd, const struct achievement_data *ad, unsigned int obj_idx, int progress)
+{
+ struct achievement *ach = NULL;
+
+ nullpo_retv(sd);
+ nullpo_retv(ad);
+
+ Assert_retv(progress != 0);
+ Assert_retv(obj_idx < VECTOR_LENGTH(ad->objective));
+
+ if (progress >= VECTOR_INDEX(ad->objective, obj_idx).goal) {
+
+ if ((ach = achievement->ensure(sd, ad)) == NULL)
+ return;
+
+ if (ach->completed_at)
+ return;
+
+ ach->objective[obj_idx] = VECTOR_INDEX(ad->objective, obj_idx).goal;
+
+ if (achievement->check_complete(sd, ad)) {
+ achievement->validate_achieve(sd, ad->id);
+ ach->completed_at = time(NULL);
+ }
+
+ clif->achievement_send_update(sd->fd, sd, ad);
+ }
+}
+
+/**
+* Checks if the given criteria satisfies the achievement's objective.
+* @param objective [in] pointer to the achievement's objectives data.
+* @param criteria [in] pointer to the current session's criteria as a comparand.
+* @return true if all criteria are satisfied, else false.
+*/
+static bool achievement_check_criteria(const struct achievement_objective *objective, const struct achievement_objective *criteria)
+{
+ int i = 0, j = 0;
+
+ nullpo_retr(false, objective);
+ nullpo_retr(false, criteria);
+
+ /* Item Id */
+ if (objective->unique_type == CRITERIA_UNIQUE_ITEM_ID && objective->unique.itemid != criteria->unique.itemid)
+ return false;
+ /* Weapon Level */
+ else if (objective->unique_type == CRITERIA_UNIQUE_WEAPON_LV && objective->unique.weapon_lv != criteria->unique.weapon_lv)
+ return false;
+ /* Status Types */
+ else if (objective->unique_type == CRITERIA_UNIQUE_STATUS_TYPE && objective->unique.status_type != criteria->unique.status_type)
+ return false;
+ /* Achievement Id */
+ else if (objective->unique_type == CRITERIA_UNIQUE_ACHIEVE_ID && objective->unique.achieve_id != criteria->unique.achieve_id)
+ return false;
+
+ /* Monster Id */
+ if (objective->mobid > 0 && objective->mobid != criteria->mobid)
+ return false;
+
+ /* Item Type */
+ if (objective->item_type > 0 && (objective->item_type & (2 << criteria->item_type)) == 0)
+ return false;
+
+ /* Job Ids */
+ for (i = 0; i < VECTOR_LENGTH(objective->jobid); i++) {
+ ARR_FIND(0, VECTOR_LENGTH(criteria->jobid), j, VECTOR_INDEX(criteria->jobid, j) != VECTOR_INDEX(objective->jobid, i));
+ if (j < VECTOR_LENGTH(criteria->jobid))
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * Validates an Achievement Objective of similar types.
+ * @param[in] sd as a pointer to the map session data.
+ * @param[in] type as the type of the achievement.
+ * @param[in] criteria as the criteria of the objective (mob id, job id etc.. 0 for no criteria).
+ * @param[in] progress as the current progress of the objective.
+ * @return total number of updated achievements on success, 0 on failure.
+ */
+static int achievement_validate_type(struct map_session_data *sd, enum achievement_types type, const struct achievement_objective *criteria, bool additive)
+{
+ int i = 0, total = 0;
+ struct achievement *ach = NULL;
+
+ nullpo_ret(sd);
+ nullpo_ret(criteria);
+
+ Assert_ret(criteria->goal != 0);
+
+ if (type == ACH_QUEST) {
+ ShowError("achievement_validate_type: ACH_QUEST is not handled by this function. (use achievement_validate())\n");
+ return 0;
+ } else if (type >= ACH_TYPE_MAX) {
+ ShowError("achievement_validate_type: Invalid Achievement Type %d! (min: %d, max: %d)\n", (int)type, (int)ACH_QUEST, (int)ACH_TYPE_MAX - 1);
+ return 0;
+ }
+
+ /* Loop through all achievements of the type, checking for possible matches. */
+ for (i = 0; i < VECTOR_LENGTH(achievement->category[type]); i++) {
+ int j = 0;
+ bool updated = false;
+ const struct achievement_data *ad = NULL;
+
+ if ((ad = achievement->get(VECTOR_INDEX(achievement->category[type], i))) == NULL)
+ continue;
+
+ for (j = 0; j < VECTOR_LENGTH(ad->objective); j++) {
+ // Check if objective criteria matches.
+ if (achievement->check_criteria(&VECTOR_INDEX(ad->objective, j), criteria) == false)
+ continue;
+ // Ensure availability of the achievement.
+ if ((ach = achievement->ensure(sd, ad)) == NULL)
+ return false;
+ // Criteria passed, check if not completed and update progress.
+ if ((ach->completed_at == 0 && ach->objective[j] < VECTOR_INDEX(ad->objective, j).goal)) {
+ if (additive == true)
+ achievement->progress_add(sd, ad, j, criteria->goal);
+ else
+ achievement->progress_set(sd, ad, j, criteria->goal);
+ updated = true;
+ }
+ }
+
+ if (updated == true)
+ total++;
+ }
+
+ return total;
+}
+
+/**
+ * Validates any achievement's specific objective index.
+ * @param[in] sd pointer to the session data.
+ * @param[in] aid ID of the achievement.
+ * @param[in] index index of the objective.
+ * @param[in] progress progress to be added towards the goal.
+ */
+static bool achievement_validate(struct map_session_data *sd, int aid, unsigned int obj_idx, int progress, bool additive)
+{
+ const struct achievement_data *ad = NULL;
+ struct achievement *ach = NULL;
+
+ nullpo_retr(false, sd);
+ Assert_retr(false, progress > 0);
+ Assert_retr(false, obj_idx < MAX_ACHIEVEMENT_OBJECTIVES);
+
+ if ((ad = achievement->get(aid)) == NULL) {
+ ShowError("achievement_validate: Invalid Achievement %d provided.", aid);
+ return false;
+ }
+
+ // Ensure availability of the achievement.
+ if ((ach = achievement->ensure(sd, ad)) == NULL)
+ return false;
+
+ // Check if not completed and update progress.
+ if ((!ach->completed_at && ach->objective[obj_idx] < VECTOR_INDEX(ad->objective, obj_idx).goal)) {
+ if (additive == true)
+ achievement->progress_add(sd, ad, obj_idx, progress);
+ else
+ achievement->progress_set(sd, ad, obj_idx, progress);
+ }
+
+ return true;
+}
+
+/**
+ * Validates monster kill type objectives.
+ * @type ACH_KILL_MOB_CLASS
+ * @param[in] sd pointer to session data.
+ * @param[in] mob_id (criteria) class of the monster checked for.
+ * @param[in] progress (goal) progress to be added.
+ * @see achievement_vaildate_type()
+ */
+static void achievement_validate_mob_kill(struct map_session_data *sd, int mob_id)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+ Assert_retv(mob_id > 0 && mob->db(mob_id) != NULL);
+
+ if (sd->achievements_received == false)
+ return;
+
+ criteria.mobid = mob_id;
+ criteria.goal = 1;
+
+ achievement->validate_type(sd, ACH_KILL_MOB_CLASS, &criteria, true);
+}
+
+/**
+ * Validate monster damage type objectives.
+ * @types ACH_DAMAGE_MOB_REC_MAX
+ * ACH_DAMAGE_MOB_REC_TOTAL
+ * @param[in] sd pointer to session data.
+ * @param[in] damage amount of damage received/dealt.
+ * @param received received/dealt boolean switch.
+ */
+static void achievement_validate_mob_damage(struct map_session_data *sd, unsigned int damage, bool received)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+ Assert_retv(damage > 0);
+
+ if (sd->achievements_received == false)
+ return;
+
+ criteria.goal = (int) damage;
+
+ if (received) {
+ achievement->validate_type(sd, ACH_DAMAGE_MOB_REC_MAX, &criteria, false);
+ achievement->validate_type(sd, ACH_DAMAGE_MOB_REC_TOTAL, &criteria, true);
+ } else {
+ achievement->validate_type(sd, ACH_DAMAGE_MOB_MAX, &criteria, false);
+ achievement->validate_type(sd, ACH_DAMAGE_MOB_TOTAL, &criteria, true);
+ }
+
+}
+
+/**
+ * Validate player kill (PVP) achievements.
+ * @types ACH_KILL_PC_TOTAL
+ * ACH_KILL_PC_JOB
+ * ACH_KILL_PC_JOBTYPE
+ * @param[in] sd pointer to killed player's session data.
+ * @param[in] dstsd pointer to killer's session data.
+ */
+static void achievement_validate_pc_kill(struct map_session_data *sd, struct map_session_data *dstsd)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+ nullpo_retv(dstsd);
+
+ if (sd->achievements_received == false)
+ return;
+
+ criteria.goal = 1;
+
+ /* */
+ achievement->validate_type(sd, ACH_KILL_PC_TOTAL, &criteria, true);
+
+ /* */
+ VECTOR_INIT(criteria.jobid);
+ VECTOR_ENSURE(criteria.jobid, 1, 1);
+ VECTOR_PUSH(criteria.jobid, dstsd->status.class);
+
+ /* Job class */
+ achievement->validate_type(sd, ACH_KILL_PC_JOB, &criteria, true);
+ /* Job Type */
+ achievement->validate_type(sd, ACH_KILL_PC_JOBTYPE, &criteria, true);
+
+ VECTOR_CLEAR(criteria.jobid);
+}
+
+/**
+ * Validate player kill (PVP) achievements.
+ * @types ACH_DAMAGE_PC_MAX
+ * ACH_DAMAGE_PC_TOTAL
+ * ACH_DAMAGE_PC_REC_MAX
+ * ACH_DAMAGE_PC_REC_TOTAL
+ * @param[in] sd pointer to source player's session data.
+ * @param[in] dstsd pointer to target player's session data.
+ * @param[in] damage amount of damage dealt / received.
+ */
+static void achievement_validate_pc_damage(struct map_session_data *sd, struct map_session_data *dstsd, unsigned int damage)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+
+ if (sd->achievements_received == false)
+ return;
+
+ if (damage == 0)
+ return;
+
+ criteria.goal = (int) damage;
+
+ /* */
+ achievement->validate_type(sd, ACH_DAMAGE_PC_MAX, &criteria, false);
+ achievement->validate_type(sd, ACH_DAMAGE_PC_TOTAL, &criteria, true);
+
+ /* */
+ achievement->validate_type(dstsd, ACH_DAMAGE_PC_REC_MAX, &criteria, false);
+ achievement->validate_type(dstsd, ACH_DAMAGE_PC_REC_TOTAL, &criteria, true);
+}
+
+/**
+ * Validates job change objectives.
+ * @type ACH_JOB_CHANGE
+ * @param[in] sd pointer to session data.
+ * @see achivement_validate_type()
+ */
+static void achievement_validate_jobchange(struct map_session_data *sd)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+
+ if (sd->achievements_received == false)
+ return;
+
+ VECTOR_INIT(criteria.jobid);
+ VECTOR_ENSURE(criteria.jobid, 1, 1);
+ VECTOR_PUSH(criteria.jobid, sd->status.class);
+
+ criteria.goal = 1;
+
+ achievement->validate_type(sd, ACH_JOB_CHANGE, &criteria, false);
+
+ VECTOR_CLEAR(criteria.jobid);
+}
+
+/**
+ * Validates stat type objectives.
+ * @types ACH_STATUS
+ * ACH_STATUS_BY_JOB
+ * ACH_STATUS_BY_JOBTYPE
+ * @param[in] sd pointer to session data.
+ * @param[in] stat_type (criteria) status point type. (see status_point_types)
+ * @param[in] progress (goal) amount of absolute progress to check.
+ * @see achievement_validate_type()
+ */
+static void achievement_validate_stats(struct map_session_data *sd, enum status_point_types stat_type, int progress)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+ Assert_retv(progress > 0);
+
+ if (sd->achievements_received == false)
+ return;
+
+ if (!achievement_valid_status_types(stat_type)) {
+ ShowError("achievement_validate_stats: Invalid status type %d given.\n", (int) stat_type);
+ return;
+ }
+
+ criteria.unique.status_type = stat_type;
+
+ criteria.goal = progress;
+
+ achievement->validate_type(sd, ACH_STATUS, &criteria, false);
+
+ VECTOR_INIT(criteria.jobid);
+ VECTOR_ENSURE(criteria.jobid, 1, 1);
+ VECTOR_PUSH(criteria.jobid, sd->status.class);
+
+ /* Stat and Job class */
+ achievement->validate_type(sd, ACH_STATUS_BY_JOB, &criteria, false);
+
+ /* Stat and Job Type */
+ achievement->validate_type(sd, ACH_STATUS_BY_JOBTYPE, &criteria, false);
+
+ VECTOR_CLEAR(criteria.jobid);
+}
+
+/**
+ * Validates chatroom creation type objectives.
+ * @types ACH_CHATROOM_CREATE
+ * ACH_CHATROOM_CREATE_DEAD
+ * @param[in] sd pointer to session data.
+ * @see achievement_validate_type()
+ */
+static void achievement_validate_chatroom_create(struct map_session_data *sd)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+
+ if (sd->achievements_received == false)
+ return;
+
+ criteria.goal = 1;
+
+ if (pc_isdead(sd)) {
+ achievement->validate_type(sd, ACH_CHATROOM_CREATE_DEAD, &criteria, true);
+ return;
+ }
+
+ achievement->validate_type(sd, ACH_CHATROOM_CREATE, &criteria, true);
+}
+
+/**
+ * Validates chatroom member count type objectives.
+ * @type ACH_CHATROOM_MEMBERS
+ * @param[in] sd pointer to session data.
+ * @param[in] progress (goal) amount of progress to be added.
+ * @see achievement_validate_type()
+ */
+static void achievement_validate_chatroom_members(struct map_session_data *sd, int progress)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+
+ if (sd->achievements_received == false)
+ return;
+
+ Assert_retv(progress > 0);
+
+ criteria.goal = progress;
+
+ achievement->validate_type(sd, ACH_CHATROOM_MEMBERS, &criteria, false);
+}
+
+/**
+ * Validates friend add type objectives.
+ * @type ACH_FRIEND_ADD
+ * @param[in] sd pointer to session data.
+ * @see achievement_validate_type()
+ */
+static void achievement_validate_friend_add(struct map_session_data *sd)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+
+ if (sd->achievements_received == false)
+ return;
+
+ criteria.goal = 1;
+
+ achievement->validate_type(sd, ACH_FRIEND_ADD, &criteria, true);
+}
+
+/**
+ * Validates party creation type objectives.
+ * @type ACH_PARTY_CREATE
+ * @param[in] sd pointer to session data.
+ * @see achievement_validate_type()
+ */
+static void achievement_validate_party_create(struct map_session_data *sd)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+
+ if (sd->achievements_received == false)
+ return;
+
+ criteria.goal = 1;
+ achievement->validate_type(sd, ACH_PARTY_CREATE, &criteria, true);
+}
+
+/**
+ * Validates marriage type objectives.
+ * @type ACH_MARRY
+ * @param[in] sd pointer to session data.
+ * @see achievement_validate_type()
+ */
+static void achievement_validate_marry(struct map_session_data *sd)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+
+ if (sd->achievements_received == false)
+ return;
+
+ criteria.goal = 1;
+
+ achievement->validate_type(sd, ACH_MARRY, &criteria, true);
+}
+
+/**
+ * Validates adoption type objectives.
+ * @types ACH_ADOPT_PARENT
+ * ACH_ADOPT_BABY
+ * @param[in] sd pointer to session data.
+ * @param[in] parent (type) boolean value to indicate if parent (true) or baby (false).
+ * @see achievement_validate_type()
+ */
+static void achievement_validate_adopt(struct map_session_data *sd, bool parent)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+
+ if (sd->achievements_received == false)
+ return;
+
+ criteria.goal = 1;
+
+ if (parent)
+ achievement->validate_type(sd, ACH_ADOPT_PARENT, &criteria, true);
+ else
+ achievement->validate_type(sd, ACH_ADOPT_BABY, &criteria, true);
+}
+
+/**
+ * Validates zeny type objectives.
+ * @types ACH_ZENY_HOLD
+ * ACH_ZENY_GET_ONCE
+ * ACH_ZENY_GET_TOTAL
+ * ACH_ZENY_SPEND_ONCE
+ * ACH_ZENY_SPEND_TOTAL
+ * @param[in] sd pointer to session data.
+ * @param[in] amount (goal) amount of zeny earned or spent.
+ * @see achievement_validate_type()
+ */
+static void achievement_validate_zeny(struct map_session_data *sd, int amount)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+
+ if (sd->achievements_received == false)
+ return;
+
+ Assert_retv(amount != 0);
+
+ if (amount > 0) {
+ criteria.goal = sd->status.zeny;
+ achievement->validate_type(sd, ACH_ZENY_HOLD, &criteria, false);
+ criteria.goal = amount;
+ achievement->validate_type(sd, ACH_ZENY_GET_ONCE, &criteria, false);
+ achievement->validate_type(sd, ACH_ZENY_GET_TOTAL, &criteria, true);
+ } else {
+ criteria.goal = amount;
+ achievement->validate_type(sd, ACH_ZENY_SPEND_ONCE, &criteria, false);
+ achievement->validate_type(sd, ACH_ZENY_SPEND_TOTAL, &criteria, true);
+ }
+}
+
+/**
+ * Validates equipment refinement type objectives.
+ * @types ACH_EQUIP_REFINE_SUCCESS
+ * ACH_EQUIP_REFINE_FAILURE
+ * ACH_EQUIP_REFINE_SUCCESS_TOTAL
+ * ACH_EQUIP_REFINE_FAILURE_TOTAL
+ * ACH_EQUIP_REFINE_SUCCESS_WLV
+ * ACH_EQUIP_REFINE_FAILURE_WLV
+ * ACH_EQUIP_REFINE_SUCCESS_ID
+ * ACH_EQUIP_REFINE_FAILURE_ID
+ * @param[in] sd pointer to session data.
+ * @param[in] idx Inventory index of the item.
+ * @param[in] success (type) boolean switch for failure / success.
+ * @see achievement_validate_type()
+ */
+static void achievement_validate_refine(struct map_session_data *sd, unsigned int idx, bool success)
+{
+ struct achievement_objective criteria = { 0 };
+ struct item_data *id = NULL;
+
+ nullpo_retv(sd);
+ Assert_retv(idx < MAX_INVENTORY);
+
+ id = itemdb->exists(sd->status.inventory[idx].nameid);
+
+ if (sd->achievements_received == false)
+ return;
+
+ Assert_retv(idx < MAX_INVENTORY);
+ Assert_retv(id != NULL);
+
+ criteria.goal = sd->status.inventory[idx].refine;
+
+ /* Universal */
+ achievement->validate_type(sd,
+ success ? ACH_EQUIP_REFINE_SUCCESS : ACH_EQUIP_REFINE_FAILURE,
+ &criteria, false);
+
+ /* Total */
+ criteria.goal = 1;
+ achievement->validate_type(sd,
+ success ? ACH_EQUIP_REFINE_SUCCESS_TOTAL : ACH_EQUIP_REFINE_FAILURE_TOTAL,
+ &criteria, true);
+
+ /* By Weapon Level */
+ if (id->type == IT_WEAPON) {
+ criteria.item_type = id->type;
+ criteria.unique.weapon_lv = id->wlv;
+ criteria.goal = sd->status.inventory[idx].refine;
+ achievement->validate_type(sd,
+ success ? ACH_EQUIP_REFINE_SUCCESS_WLV : ACH_EQUIP_REFINE_FAILURE_WLV,
+ &criteria, false);
+ criteria.item_type = 0;
+ criteria.unique.weapon_lv = 0; // cleanup
+ }
+
+ /* By NameId */
+ criteria.unique.itemid = id->nameid;
+ criteria.goal = sd->status.inventory[idx].refine;
+ achievement->validate_type(sd,
+ success ? ACH_EQUIP_REFINE_SUCCESS_ID : ACH_EQUIP_REFINE_FAILURE_ID,
+ &criteria, false);
+ criteria.unique.itemid = 0; // cleanup
+}
+
+/**
+ * Validates item received type objectives.
+ * @types ACH_ITEM_GET_COUNT
+ * ACH_ITEM_GET_WORTH
+ * ACH_ITEM_GET_COUNT_ITEMTYPE
+ * @param[in] sd pointer to session data.
+ * @param[in] nameid (criteria) ID of the item.
+ * @param[in] amount (goal) amount of the item collected.
+ */
+static void achievement_validate_item_get(struct map_session_data *sd, int nameid, int amount)
+{
+ struct item_data *it = itemdb->exists(nameid);
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+
+ if (sd->achievements_received == false)
+ return;
+
+ Assert_retv(amount > 0);
+ nullpo_retv(it);
+
+ criteria.unique.itemid = it->nameid;
+ criteria.goal = amount;
+ achievement->validate_type(sd, ACH_ITEM_GET_COUNT, &criteria, false);
+ criteria.unique.itemid = 0; // cleanup
+
+ /* Item Buy Value*/
+ criteria.goal = max(it->value_buy, 1);
+ achievement->validate_type(sd, ACH_ITEM_GET_WORTH, &criteria, false);
+
+ /* Item Type */
+ criteria.item_type = it->type;
+ criteria.goal = 1;
+ achievement->validate_type(sd, ACH_ITEM_GET_COUNT_ITEMTYPE, &criteria, false);
+ criteria.item_type = 0; // cleanup
+}
+
+/**
+ * Validates item sold type objectives.
+ * @type ACH_ITEM_SELL_WORTH
+ * @param[in] sd pointer to session data.
+ * @param[in] nameid Item Id in question.
+ * @param[in] amount amount of item in question.
+ */
+static void achievement_validate_item_sell(struct map_session_data *sd, int nameid, int amount)
+{
+ struct item_data *it = itemdb->exists(nameid);
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+
+ if (sd->achievements_received == false)
+ return;
+
+ Assert_retv(amount > 0);
+ nullpo_retv(it);
+
+ criteria.unique.itemid = it->nameid;
+
+ criteria.goal = max(it->value_sell, 1);
+
+ achievement->validate_type(sd, ACH_ITEM_SELL_WORTH, &criteria, false);
+}
+
+/**
+ * Validates achievement type objectives.
+ * @type ACH_ACHIEVE
+ * @param[in] sd pointer to session data.
+ * @param[in] achid (criteria) achievement id.
+ */
+static void achievement_validate_achieve(struct map_session_data *sd, int achid)
+{
+ const struct achievement_data *ad = achievement->get(achid);
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+ nullpo_retv(ad);
+
+ if (sd->achievements_received == false)
+ return;
+
+ Assert_retv(achid > 0);
+
+ criteria.unique.achieve_id = ad->id;
+
+ criteria.goal = 1;
+
+ achievement->validate_type(sd, ACH_ACHIEVE, &criteria, false);
+}
+
+/**
+ * Validates taming type objectives.
+ * @type ACH_PET_CREATE
+ * @param[in] sd pointer to session data.
+ * @param[in] class (criteria) class of the monster tamed.
+ */
+static void achievement_validate_taming(struct map_session_data *sd, int class)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+
+ if (sd->achievements_received == false)
+ return;
+
+ Assert_retv(class > 0);
+ Assert_retv(mob->db(class) != mob->dummy);
+
+ criteria.mobid = class;
+ criteria.goal = 1;
+
+ achievement->validate_type(sd, ACH_PET_CREATE, &criteria, true);
+}
+
+/**
+ * Validated achievement rank type objectives.
+ * @type ACH_ACHIEVEMENT_RANK
+ * @param[in] sd pointer to session data.
+ * @param[in] rank (goal) rank of earned.
+ */
+static void achievement_validate_achievement_rank(struct map_session_data *sd, int rank)
+{
+ struct achievement_objective criteria = { 0 };
+
+ nullpo_retv(sd);
+
+ if (sd->achievements_received == false)
+ return;
+
+ Assert_retv(rank >= 0 && rank <= VECTOR_LENGTH(achievement->rank_exp));
+
+ criteria.goal = 1;
+
+ achievement->validate_type(sd, ACH_ACHIEVEMENT_RANK, &criteria, false);
+}
+
+/**
+ * Verifies if an achievement type requires a criteria field.
+ * @param[in] type achievement type in question.
+ * @return true if required, false if not.
+ */
+static bool achievement_type_requires_criteria(enum achievement_types type)
+{
+ if (type == ACH_KILL_PC_JOB
+ || type == ACH_KILL_PC_JOBTYPE
+ || type == ACH_KILL_MOB_CLASS
+ || type == ACH_JOB_CHANGE
+ || type == ACH_STATUS
+ || type == ACH_STATUS_BY_JOB
+ || type == ACH_STATUS_BY_JOBTYPE
+ || type == ACH_EQUIP_REFINE_SUCCESS_WLV
+ || type == ACH_EQUIP_REFINE_FAILURE_WLV
+ || type == ACH_EQUIP_REFINE_SUCCESS_ID
+ || type == ACH_EQUIP_REFINE_FAILURE_ID
+ || type == ACH_ITEM_GET_COUNT
+ || type == ACH_PET_CREATE
+ || type == ACH_ACHIEVE)
+ return true;
+
+ return false;
+}
+
+/**
+ * Stores all the title ID that has been earned by player
+ * @param[in] sd pointer to session data.
+ */
+static void achievement_init_titles(struct map_session_data *sd)
+{
+ int i;
+ nullpo_retv(sd);
+
+ VECTOR_INIT(sd->title_ids);
+ /* Browse through the session's achievement list and gather their values. */
+ for (i = 0; i < VECTOR_LENGTH(sd->achievement); i++) {
+ struct achievement *a = &VECTOR_INDEX(sd->achievement, i);
+ const struct achievement_data *ad = NULL;
+
+ /* Sanity check for nonull pointers. */
+ if (a == NULL || (ad = achievement->get(a->id)) == NULL)
+ continue;
+
+ if (a->completed_at > 0 && a->rewarded_at > 0 && ad->rewards.title_id > 0) {
+ VECTOR_ENSURE(sd->title_ids, 1, 1);
+ VECTOR_PUSH(sd->title_ids, ad->rewards.title_id);
+ }
+ }
+}
+
+/**
+ * Validates whether player has earned the title.
+ * @param[in] sd pointer to session data.
+ * @param[in] title_id Title ID
+ * @return true, if title has been earned, else false
+ */
+static bool achievement_check_title(struct map_session_data *sd, int title_id) {
+ int i;
+
+ nullpo_retr(false, sd);
+
+ if (title_id == 0)
+ return true;
+
+ for (i = 0; i < VECTOR_LENGTH(sd->title_ids); i++) {
+ if (VECTOR_INDEX(sd->title_ids, i) == title_id) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+/**
+ * Achievement rewards are given to player
+ * @param sd session data
+ * @param ad achievement data
+ */
+static void achievement_get_rewards(struct map_session_data *sd, const struct achievement_data *ad) {
+ int i = 0;
+ struct achievement *ach = NULL;
+
+ nullpo_retv(sd);
+ nullpo_retv(ad);
+
+ if ((ach = achievement->ensure(sd, ad)) == NULL)
+ return;
+
+ /* Buff */
+ if (ad->rewards.bonus != NULL)
+ script->run(ad->rewards.bonus, 0, sd->bl.id, 0);
+
+ /* Give Items */
+ for (i = 0; i < VECTOR_LENGTH(ad->rewards.item); i++) {
+ struct item it = { 0 };
+ int total = 0;
+
+ it.nameid = VECTOR_INDEX(ad->rewards.item, i).id;
+ total = VECTOR_INDEX(ad->rewards.item, i).amount;
+
+ it.identify = 1;
+
+ //Check if it's stackable.
+ if (!itemdb->isstackable(it.nameid)) {
+ int j = 0;
+ for (j = 0; j < total; ++j)
+ pc->additem(sd, &it, (it.amount = 1), LOG_TYPE_SCRIPT);
+ } else {
+ pc->additem(sd, &it, (it.amount = total), LOG_TYPE_SCRIPT);
+ }
+ }
+
+ ach->rewarded_at = time(NULL);
+
+ if (ad->rewards.title_id > 0) { // Add Title
+ VECTOR_ENSURE(sd->title_ids, 1, 1);
+ VECTOR_PUSH(sd->title_ids, ad->rewards.title_id);
+ clif->achievement_send_list(sd->fd, sd);
+ } else {
+ clif->achievement_reward_ack(sd->fd, sd, ad);
+ clif->achievement_send_update(sd->fd, sd, ad); // send update.
+ }
+}
+
+/**
+ * Parses the Achievement Ranks.
+ * @read db/achievement_rank_db.conf
+ */
+static void achievement_readdb_ranks(void)
+{
+ const char *filename = "db/achievement_rank_db.conf";
+ struct config_t ar_conf = { 0 };
+ struct config_setting_t *ardb = NULL, *conf = NULL;
+ int entry = 0;
+
+ if (!libconfig->load_file(&ar_conf, filename))
+ return; // report error.
+
+ if (!(ardb = libconfig->setting_get_member(ar_conf.root, "achievement_rank_db"))) {
+ ShowError("achievement_readdb_ranks: Could not process contents of file '%s', skipping...\n", filename);
+ libconfig->destroy(&ar_conf);
+ return;
+ }
+
+ while (entry < libconfig->setting_length(ardb) && entry < MAX_ACHIEVEMENT_RANKS) {
+ char rank[8];
+
+ if (!(conf = libconfig->setting_get_elem(ardb, entry))) {
+ ShowError("achievement_readdb_ranks: Could not read value for entry %d, skipping...\n", entry+1);
+ continue;
+ }
+
+ entry++; // Rank counter;
+
+ sprintf(rank, "Rank%d", entry);
+
+ if (strcmp(rank, config_setting_name(conf)) == 0) {
+ int exp = 0;
+
+ if ((exp = libconfig->setting_get_int(conf)) <= 0) {
+ ShowError("achievement_readdb_ranks: Invalid value provided for %s in '%s'.\n", rank, filename);
+ continue;
+ }
+
+ VECTOR_ENSURE(achievement->rank_exp, 1, 1);
+ VECTOR_PUSH(achievement->rank_exp, exp);
+ } else {
+ ShowWarning("achievement_readdb_ranks: Ranks are not in order! Ignoring all ranks after Rank %d...\n", entry);
+ break; // break if elements are not in order or rank doesn't exist.
+ }
+ }
+
+ if (libconfig->setting_length(ardb) > MAX_ACHIEVEMENT_RANKS)
+ ShowWarning("achievement_rankdb_ranks: Maximum number of achievement ranks exceeded. Skipping all after entry %d...\n", entry);
+
+ libconfig->destroy(&ar_conf);
+
+ if (!entry) {
+ ShowError("achievement_readdb_ranks: No ranks provided in '%s'!\n", filename);
+ return;
+ }
+
+ ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entry, filename);
+}
+
+/**
+ * Validates Mob Id criteria of an objective while parsing an achievement entry.
+ * @param[in] t pointer to the config setting.
+ * @param[out] obj pointer to the achievement objective entry being parsed.
+ * @param[in] type Type of the achievement being parsed.
+ * @param[in] entry_id Id of the entry being parsed.
+ * @param[in] obj_idx Index of the objective entry being parsed.
+ * @return false on failure, true on success
+ */
+static bool achievement_readdb_validate_criteria_mobid(const struct config_setting_t *t, struct achievement_objective *obj, enum achievement_types type, int entry_id, int obj_idx)
+{
+ int val = 0;
+ const char *string = NULL;
+
+ nullpo_retr(false, t);
+ nullpo_retr(false, obj);
+
+ if (libconfig->setting_lookup_int(t, "MobId", &val)) {
+ if (mob->db_checkid(val) == 0) {
+ ShowError("achievement_readdb_validate_criteria_mobid: Non-existant monster with ID %id provided (Achievement: %d, Objective: %d). Skipping...\n", val, entry_id, obj_idx);
+ return false;
+ }
+
+ obj->mobid = val;
+ } else if (libconfig->setting_lookup_string(t, "MobId", &string)) {
+ if (!script->get_constant(string, &val)) {
+ ShowError("achievement_readdb_validate_criteria_mobid: Non-existant constant %s provided (Achievement: %d, Objective: %d). Skipping...\n", string, entry_id, obj_idx);
+ return false;
+ }
+
+ obj->mobid = val;
+ } else if (achievement_criteria_mobid(type)) {
+ ShowError("achievement_readdb_validate_criteria_mobid: Achievement type of ID %d requires MobId as objective criteria, setting not provided. Skipping...\n", entry_id);
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * Validates Job Id criteria of an objective while parsing an achievement entry.
+ * @param[in] t pointer to the config setting.
+ * @param[out] obj pointer to the achievement objective entry being parsed.
+ * @param[in] type Type of the achievement being parsed.
+ * @param[in] entry_id Id of the entry being parsed.
+ * @param[in] obj_idx Index of the objective entry being parsed.
+ * @return false on failure, true on success.
+ */
+static bool achievement_readdb_validate_criteria_jobid(const struct config_setting_t *t, struct achievement_objective *obj, enum achievement_types type, int entry_id, int obj_idx)
+{
+ int job_id = 0;
+ const char *string = NULL;
+ struct config_setting_t *tt = NULL;
+
+ nullpo_retr(false, t);
+ nullpo_retr(false, obj);
+
+ // initialize the buffered objective jobid vector.
+ VECTOR_INIT(obj->jobid);
+
+ if (libconfig->setting_lookup_int(t, "JobId", &job_id)) {
+ if (pc->jobid2mapid(job_id) == -1) {
+ ShowError("achievement_readdb_validate_criteria_jobid: Invalid JobId %d provided (Achievement: %d, Objective: %d). Skipping...\n", job_id, entry_id, obj_idx);
+ return false;
+ }
+
+ VECTOR_ENSURE(obj->jobid, 1, 1);
+ VECTOR_PUSH(obj->jobid, job_id);
+ } else if (libconfig->setting_lookup_string(t, "JobId", &string)) {
+ if (script->get_constant(string, &job_id) == false) {
+ ShowError("achievement_readdb_validate_criteria_jobid: Invalid JobId %d provided (Achievement: %d, Objective: %d). Skipping...\n", job_id, entry_id, obj_idx);
+ return false;
+ }
+
+ VECTOR_ENSURE(obj->jobid, 1, 1);
+ VECTOR_PUSH(obj->jobid, job_id);
+ } else if ((tt = libconfig->setting_get_member(t, "JobId")) && config_setting_is_array(tt)) {
+ int j = 0;
+
+ while (j < libconfig->setting_length(tt)) {
+ if ((job_id = libconfig->setting_get_int_elem(tt, j)) == 0) {
+ if ((string = libconfig->setting_get_string_elem(tt, j)) != NULL && script->get_constant(string, &job_id) == false) {
+ ShowError("achievement_readdb_validate_criteria_jobid: Invalid JobId provided at index %d (Achievement: %d, Objective: %d). Skipping...\n", j, entry_id, obj_idx);
+ continue;
+ }
+ }
+
+ /* Ensure size and allocation */
+ VECTOR_ENSURE(obj->jobid, 1, 1);
+ /* push buffer */
+ VECTOR_PUSH(obj->jobid, job_id);
+ j++;
+ }
+ } else if (achievement_criteria_jobid(type)) {
+ ShowError("achievement_readdb_validate_criteria_jobid: Achievement type of ID %d requires a JobId field in the objective criteria, setting not provided. Skipping...\n", entry_id);
+ return false;
+ }
+
+ return true;
+
+}
+
+/**
+ * Validates Item Id criteria of an objective while parsing an achievement entry.
+ * @param[in] t pointer to the config setting.
+ * @param[out] obj pointer to the achievement objective entry being parsed.
+ * @param[in] type Type of the achievement being parsed.
+ * @param[in] entry_id Id of the entry being parsed.
+ * @param[in] obj_idx Index of the objective entry being parsed.
+ * @return false on failure, true on success.
+ */
+static bool achievement_readdb_validate_criteria_itemid(const struct config_setting_t *t, struct achievement_objective *obj, enum achievement_types type, int entry_id, int obj_idx)
+{
+ int val = 0;
+ const char *string = NULL;
+
+ nullpo_retr(false, t);
+ nullpo_retr(false, obj);
+
+
+ if (libconfig->setting_lookup_int(t, "ItemId", &val)) {
+ if (itemdb->exists(val) == NULL) {
+ ShowError("achievement_readdb_validate_criteria_itemid: Invalid ItemID %d provided (Achievement: %d, Objective: %d). Skipping...\n", val, entry_id, obj_idx);
+ return false;
+ } else if (obj->unique_type != CRITERIA_UNIQUE_NONE) {
+ ShowError("achievement_readdb_validate_criteria_itemid: Unique criteria has already been set to type %d. (Achievement: %d, Objective: %d). Skipping...\n", (int) obj->unique_type, entry_id, obj_idx);
+ return false;
+ }
+
+ obj->unique.itemid = val;
+ obj->unique_type = CRITERIA_UNIQUE_ITEM_ID;
+ } else if (libconfig->setting_lookup_string(t, "ItemId", &string)) {
+ if (script->get_constant(string, &val) == false) {
+ ShowError("achievement_readdb_validate_criteria_itemid: Invalid ItemID %d provided (Achievement: %d, Objective: %d). Skipping...\n", val, entry_id, obj_idx);
+ return false;
+ } else if (obj->unique_type != CRITERIA_UNIQUE_NONE) {
+ ShowError("achievement_readdb_validate_criteria_itemid: Unique criteria has already been set to type %d. (Achievement: %d, Objective: %d). Skipping...\n", (int) obj->unique_type, entry_id, obj_idx);
+ return false;
+ }
+
+ obj->unique.itemid = val;
+ obj->unique_type = CRITERIA_UNIQUE_ITEM_ID;
+ } else if (achievement_criteria_itemid(type)) {
+ ShowError("achievement_readdb_validate_criteria_itemid: Criteria requires a ItemId field (Achievement: %d, Objective: %d). Skipping...\n", entry_id, obj_idx);
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * Validates Status Type criteria of an objective while parsing an achievement entry.
+ * @param[in] t pointer to the config setting.
+ * @param[out] obj pointer to the achievement objective entry being parsed.
+ * @param[in] type Type of the achievement being parsed.
+ * @param[in] entry_id Id of the entry being parsed.
+ * @param[in] obj_idx Index of the objective entry being parsed.
+ * @return false on failure, true on success.
+ */
+static bool achievement_readdb_validate_criteria_statustype(const struct config_setting_t *t, struct achievement_objective *obj, enum achievement_types type, int entry_id, int obj_idx)
+{
+ int val = 0;
+ const char *string = NULL;
+
+ nullpo_retr(false, t);
+ nullpo_retr(false, obj);
+
+ if (libconfig->setting_lookup_int(t, "StatusType", &val)) {
+ if (!achievement_valid_status_types(val)) {
+ ShowError("achievement_readdb_validate_criteria_statustype: Invalid StatusType %d provided (Achievement: %d, Objective: %d). Skipping...\n", val, entry_id, obj_idx);
+ return false;
+ } else if (obj->unique_type != CRITERIA_UNIQUE_NONE) {
+ ShowError("achievement_readdb_validate_criteria_statustype: Unique criteria has already been set to type %d. (Achievement: %d, Objective: %d). Skipping...\n", (int) obj->unique_type, entry_id, obj_idx);
+ return false;
+ }
+
+ obj->unique.status_type = (enum status_point_types) val;
+ obj->unique_type = CRITERIA_UNIQUE_STATUS_TYPE;
+ } else if (libconfig->setting_lookup_string(t, "StatusType", &string)) {
+ if (strcmp(string, "SP_STR") == 0) val = SP_STR;
+ else if (strcmp(string, "SP_AGI") == 0) val = SP_AGI;
+ else if (strcmp(string, "SP_VIT") == 0) val = SP_VIT;
+ else if (strcmp(string, "SP_INT") == 0) val = SP_INT;
+ else if (strcmp(string, "SP_DEX") == 0) val = SP_DEX;
+ else if (strcmp(string, "SP_LUK") == 0) val = SP_LUK;
+ else if (strcmp(string, "SP_BASELEVEL") == 0) val = SP_BASELEVEL;
+ else if (strcmp(string, "SP_JOBLEVEL") == 0) val = SP_JOBLEVEL;
+ else val = SP_NONE;
+
+ if (!achievement_valid_status_types(val)) {
+ ShowError("achievement_readdb_validate_criteria_statustype: Invalid StatusType %s provided (Achievement: %d, Objective: %d). Skipping...\n", string, entry_id, obj_idx);
+ return false;
+ } else if (obj->unique_type != CRITERIA_UNIQUE_NONE) {
+ ShowError("achievement_readdb_validate_criteria_statustype: Unique criteria has already been set to type %d. (Achievement: %d, Objective: %d). Skipping...\n", (int) obj->unique_type, entry_id, obj_idx);
+ return false;
+ }
+
+ obj->unique.status_type = (enum status_point_types) val;
+ obj->unique_type = CRITERIA_UNIQUE_STATUS_TYPE;
+ } else if (achievement_criteria_stattype(type)) {
+ ShowError("achievement_readdb_validate_criteria_statustype: Criteria requires a StatusType field (Achievement: %d, Objective: %d). Skipping...\n", entry_id, obj_idx);
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * Validates Item Type criteria of an objective while parsing an achievement entry.
+ * @param[in] t pointer to the config setting.
+ * @param[out] obj pointer to the achievement objective entry being parsed.
+ * @param[in] type Type of the achievement being parsed.
+ * @param[in] entry_id Id of the entry being parsed.
+ * @param[in] obj_idx Index of the objective entry being parsed.
+ * @return false on failure, true on success.
+ */
+static bool achievement_readdb_validate_criteria_itemtype(const struct config_setting_t *t, struct achievement_objective *obj, enum achievement_types type, int entry_id, int obj_idx)
+{
+ int val = 0;
+ const char *string = NULL;
+ struct config_setting_t *tt = NULL;
+
+ nullpo_retr(false, t);
+ nullpo_retr(false, obj);
+
+ if (libconfig->setting_lookup_int(t, "ItemType", &val)) {
+ if (val < IT_HEALING || val > IT_MAX) {
+ ShowError("achievement_readdb_validate_criteria_itemtype: Invalid ItemType %d provided (Achievement: %d, Objective: %d). Skipping...\n", val, entry_id, obj_idx);
+ return false;
+ }
+
+ if (val == IT_MAX) {
+ obj->item_type |= (2 << val) - 1;
+ } else {
+ obj->item_type |= (2 << val);
+ }
+
+ } else if (libconfig->setting_lookup_string(t, "ItemType", &string)) {
+ if (!script->get_constant(string, &val) || val < IT_HEALING || val > IT_MAX) {
+ ShowError("achievement_readdb_validate_criteria_itemtype: Invalid ItemType %d provided (Achievement: %d, Objective: %d). Skipping...\n", val, entry_id, obj_idx);
+ return false;
+ }
+
+ if (val == IT_MAX) {
+ obj->item_type |= (2 << val) - 1;
+ } else {
+ obj->item_type |= (2 << val);
+ }
+ } else if ((tt = libconfig->setting_get_member(t, "ItemType")) && config_setting_is_array(tt)) {
+ int j = 0;
+ uint32 it_type = 0;
+
+ while (j < libconfig->setting_length(tt)) {
+ if ((val = libconfig->setting_get_int_elem(tt, j))) {
+ if (val < IT_HEALING || val >= IT_MAX) {
+ ShowError("achievement_readdb_validate_criteria_itemtype: Invalid ItemType %d provided (Achievement: %d, Objective: %d). Skipping...\n", val, entry_id, obj_idx);
+ continue;
+ }
+ if (val == IT_MAX) {
+ obj->item_type |= (2 << val) - 1;
+ } else {
+ obj->item_type |= (2 << val);
+ }
+ } else if ((string = libconfig->setting_get_string_elem(tt, j))) {
+ if (!script->get_constant(string, &val)) {
+ ShowError("achievement_readdb_validate_criteria_itemtype: Invalid ItemType %s provided (Achievement: %d, Objective: %d). Skipping...\n", string, entry_id, obj_idx);
+ continue;
+ }
+
+ if (val == IT_MAX) {
+ obj->item_type |= (2 << val) - 1;
+ } else {
+ obj->item_type |= (2 << val);
+ }
+ }
+ j++;
+ }
+
+ obj->item_type = it_type;
+ } else if (achievement_criteria_itemtype(type)) {
+ ShowError("achievement_readdb_validate_criteria_itemtype: Criteria requires a ItemType field (Achievement: %d, Objective: %d). Skipping...\n", entry_id, obj_idx);
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * Validates Weapon Level criteria of an objective while parsing an achievement entry.
+ * @param[in] t pointer to the config setting.
+ * @param[out] obj pointer to the achievement objective entry being parsed.
+ * @param[in] type Type of the achievement being parsed.
+ * @param[in] entry_id Id of the entry being parsed.
+ * @param[in] obj_idx Index of the objective entry being parsed.
+ * @return false on failure, true on success.
+ */
+static bool achievement_readdb_validate_criteria_weaponlv(const struct config_setting_t *t, struct achievement_objective *obj, enum achievement_types type, int entry_id, int obj_idx)
+{
+ int val = 0;
+
+ nullpo_retr(false, t);
+ nullpo_retr(false, obj);
+
+ if (libconfig->setting_lookup_int(t, "WeaponLevel", &val)) {
+ if (val < 1 || val > 4) {
+ ShowError("achievement_readdb_validate_criteria_weaponlv: Invalid WeaponLevel %d provided (Achievement: %d, Objective: %d). Skipping...\n", val, entry_id, obj_idx);
+ return false;
+ } else if (obj->unique_type != CRITERIA_UNIQUE_NONE) {
+ ShowError("achievement_readdb_validate_criteria_weaponlv: Unique criteria has already been set to type %d. (Achievement: %d, Objective: %d). Skipping...\n", (int) obj->unique_type, entry_id, obj_idx);
+ return false;
+ }
+
+ obj->unique.weapon_lv = val;
+ obj->unique_type = CRITERIA_UNIQUE_WEAPON_LV;
+ } else if (achievement_criteria_weaponlv(type)) {
+ ShowError("achievement_readdb_validate_criteria_weaponlv: Criteria requires a WeaponType field. (Achievement: %d, Objective: %d). Skipping...\n", entry_id, obj_idx);
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * Validates achievement Id criteria of an objective while parsing an achievement entry.
+ * @param[in] t pointer to the config setting.
+ * @param[out] obj pointer to the achievement objective entry being parsed.
+ * @param[in] type Type of the achievement being parsed.
+ * @param[in] entry_id Id of the entry being parsed.
+ * @param[in] obj_idx Index of the objective entry being parsed.
+ * @return false on failure, true on success.
+ */
+static bool achievement_readdb_validate_criteria_achievement(const struct config_setting_t *t, struct achievement_objective *obj, enum achievement_types type, int entry_id, int obj_idx)
+{
+ int val = 0;
+
+ nullpo_retr(false, t);
+ nullpo_retr(false, obj);
+
+ if (libconfig->setting_lookup_int(t, "Achieve", &val)) {
+ if (achievement->get(val) == NULL) {
+ ShowError("achievement_readdb_validate_criteria_achievement: Invalid Achievement %d provided as objective (Achievement %d, Objective %d). Skipping...\n", val, entry_id, obj_idx);
+ return false;
+ } else if (obj->unique_type != CRITERIA_UNIQUE_NONE) {
+ ShowError("achievement_readdb_validate_criteria_achievement: Unique criteria has already been set to type %d. (Achievement: %d, Objective: %d). Skipping...\n", (int) obj->unique_type, entry_id, obj_idx);
+ return false;
+ }
+
+ obj->unique.achieve_id = val;
+ obj->unique_type = CRITERIA_UNIQUE_ACHIEVE_ID;
+ } else if (type == ACH_ACHIEVE) {
+ ShowError("achievement_readdb_validate_criteria_achievement: Achievement type of ID %d requires an Achieve field in the objective criteria, setting not provided. Skipping...\n", entry_id);
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * Read a single objective entry of an achievement.
+ * @param[in] conf config pointer.
+ * @param[in] index Index of the objective being in the objective list being parsed.
+ * @param[out] entry pointer to the achievement db entry being parsed.
+ * @return false on failure, true on success.
+ */
+static bool achievement_readdb_objective_sub(const struct config_setting_t *conf, int index, struct achievement_data *entry)
+{
+ struct config_setting_t *tt = NULL;
+ char objnum[12];
+
+ nullpo_retr(false, conf);
+ nullpo_retr(false, entry);
+
+ sprintf(objnum, "*%d", index); // Search Objective 1..MAX
+ if ((tt = libconfig->setting_get_member(conf, objnum)) && config_setting_is_group(tt)) {
+ struct achievement_objective obj = { 0 };
+ struct config_setting_t *c = NULL;
+
+ /* Description */
+ if (libconfig->setting_lookup_mutable_string(tt, "Description", obj.description, OBJECTIVE_DESCRIPTION_LENGTH) == 0) {
+ ShowError("achievement_readdb_objective_sub: Objective %d has no description for Achievement %d, skipping...\n", index, entry->id);
+ return false;
+ }
+
+ /* Criteria */
+ if ((c = libconfig->setting_get_member(tt, "Criteria")) && config_setting_is_group(tt)) {
+ /* MobId */
+ if (achievement->readdb_validate_criteria_mobid(c, &obj, entry->type, entry->id, index) == false)
+ return false;
+
+ /* ItemId */
+ if (achievement->readdb_validate_criteria_itemid(c, &obj, entry->type, entry->id, index) == false)
+ return false;
+
+ /* StatusType */
+ if (achievement->readdb_validate_criteria_statustype(c, &obj, entry->type, entry->id, index) == false)
+ return false;
+
+ /* ItemType */
+ if (achievement->readdb_validate_criteria_itemtype(c, &obj, entry->type, entry->id, index) == false)
+ return false;
+
+ /* WeaponLevel */
+ if (achievement->readdb_validate_criteria_weaponlv(c, &obj, entry->type, entry->id, index) == false)
+ return false;
+
+ /* Achievement */
+ if (achievement->readdb_validate_criteria_achievement(c, &obj, entry->type, entry->id, index) == false)
+ return false;
+
+ /**
+ * Vectors are read last to avoid memory leaks if either of the above break, in cases where they are stacked with other criteria.
+ * Note to future editors - be sure to cleanup previous vectors before breaks.
+ */
+ /* JobId */
+ if (achievement->readdb_validate_criteria_jobid(c, &obj, entry->type, entry->id, index) == false)
+ return false;
+ } else if (achievement->type_requires_criteria(entry->type)) {
+ ShowError("achievement_readdb_objective_sub: No criteria field added (Achievement: %d, Objective: %d)! Skipping...\n", entry->id, index);
+ return false;
+ }
+
+ /* Goal */
+ if (libconfig->setting_lookup_int(tt, "Goal", &obj.goal) <= 0)
+ obj.goal = 1; // 1 count by default.
+
+ /* Ensure size and allocation */
+ VECTOR_ENSURE(entry->objective, 1, 1);
+
+ /* Push buffer */
+ VECTOR_PUSH(entry->objective, obj);
+ } else { // Break if not in order, to comply with the client's objective order.
+ ShowWarning("achievement_readdb_objective_sub: Objectives for Achievement %d are not in order (starting from *%d). Remaining objectives will be skipped.\n", entry->id, index);
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * Parses achievement objective entries of the current achievement db entry being parsed.
+ * @param[in] conf config pointer.
+ * @param[out] entry pointer to the achievement db entry being parsed.
+ * @return false on failure, true on success.
+ */
+static bool achievement_readdb_objectives(const struct config_setting_t *conf, struct achievement_data *entry)
+{
+ struct config_setting_t *t = NULL;
+
+ nullpo_retr(false, conf);
+ nullpo_retr(false, entry);
+
+ if ((t = libconfig->setting_get_member(conf, "Objectives")) && config_setting_is_group(t)) {
+ int i = 0;
+ // Initialize the buffer objective vector.
+ VECTOR_INIT(entry->objective);
+
+ for (i = 0; i < libconfig->setting_length(t) && i < MAX_ACHIEVEMENT_OBJECTIVES; i++)
+ if (achievement_readdb_objective_sub(t, i + 1, entry) == false)
+ break;
+
+ // Assess total objectives.
+ if (libconfig->setting_length(t) > MAX_ACHIEVEMENT_OBJECTIVES)
+ ShowWarning("achievement_readdb_objectives: Exceeded maximum number of objectives (%d) for Achievement %d. Remaining objectives will be skipped.\n", MAX_ACHIEVEMENT_OBJECTIVES, entry->id);
+ if (i == 0) {
+ ShowError("achievement_readdb_objectives: No Objectives provided for Achievement %d, skipping...\n", entry->id);
+ return false;
+ }
+ } else {
+ ShowError("achievement_readdb_objectives: No Objectives provided for Achievement %d, skipping...\n", entry->id);
+ return false;
+ } // end of "Objectives"
+
+ return true;
+}
+
+/**
+ * Validates a single reward item in the reward item list of an achievement.
+ * @param[in] t pointer to the config setting.
+ * @param[in] index Index of the item in the reward list.
+ * @param[out] entry pointer to the achievement entry being parsed.
+ * @return false on failure, true on success.
+ */
+static bool achievement_readdb_validate_reward_item_sub(const struct config_setting_t *t, int index, struct achievement_data *entry)
+{
+ struct config_setting_t *it = NULL;
+ struct achievement_reward_item item = { 0 };
+ const char *name = NULL;
+ int amount = 0;
+ int val = 0;
+
+ nullpo_retr(false, t);
+ nullpo_retr(false, entry);
+
+ if ((it = libconfig->setting_get_elem(t, index)) == NULL)
+ return false;
+
+ name = config_setting_name(it);
+ amount = libconfig->setting_get_int(it);
+
+ if (name[0] == 'I' && name[1] == 'D' && itemdb->exists(atoi(name+2))) {
+ val = atoi(name);
+ } else if (!script->get_constant(name, &val)) {
+ ShowWarning("achievement_readdb_validate_reward_item_sub: Non existant Item %s provided as a reward in Achievement %d, skipping...\n", name, entry->id);
+ return false;
+ }
+
+ if (amount <= 0) {
+ ShowWarning("achievement_readdb_validate_reward_item_sub: No amount provided for Item %s as a reward in Achievement %d, skipping...\n", name, entry->id);
+ return false;
+ }
+
+ /* Ensure size and allocation */
+ VECTOR_ENSURE(entry->rewards.item, 1, 1);
+
+ item.id = val;
+ item.amount = amount;
+
+ /* push buffer */
+ VECTOR_PUSH(entry->rewards.item, item);
+
+ return true;
+}
+
+/**
+ * Validates the reward items when parsing an achievement db entry.
+ * @param[in] t pointer to the config setting.
+ * @param[out] entry pointer to the achievement entry being parsed.
+ */
+static void achievement_readdb_validate_reward_items(const struct config_setting_t *t, struct achievement_data *entry)
+{
+ struct config_setting_t *tt = NULL;
+ int i = 0;
+
+ nullpo_retv(t);
+ nullpo_retv(entry);
+
+ if ((tt = libconfig->setting_get_member(t, "Items")) && config_setting_is_group(t)) {
+ for (i = 0; i < libconfig->setting_length(tt) && i < MAX_ACHIEVEMENT_ITEM_REWARDS; i++)
+ if (achievement->readdb_validate_reward_item_sub(tt, i, entry) == false)
+ continue;
+
+ if (libconfig->setting_length(tt) > MAX_ACHIEVEMENT_ITEM_REWARDS)
+ ShowError("achievement_readdb_validate_reward_items: Maximum amount of item rewards (%d) exceeded for Achievement %d. Remaining items will be skipped.\n", MAX_ACHIEVEMENT_ITEM_REWARDS, entry->id);
+ }
+}
+
+/**
+ * Validates the reward Bonus script when parsing an achievement db entry.
+ * @param[in] t pointer to the config setting.
+ * @param[out] entry pointer to the achievement entry being parsed.
+ * @param[in] source pointer to the source file name.
+ */
+static void achievement_readdb_validate_reward_bonus(const struct config_setting_t *t, struct achievement_data *entry, const char *source)
+{
+ const char *string = NULL;
+
+ nullpo_retv(source);
+ nullpo_retv(t);
+ nullpo_retv(entry);
+
+ if (libconfig->setting_lookup_string(t, "Bonus", &string))
+ entry->rewards.bonus = string ? script->parse(string, source, 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS, NULL) : NULL;
+}
+
+/**
+ * Validates the reward Title Id when parsing an achievement entry.
+ * @param[in] t pointer to the config setting.
+ * @param[out] entry pointer to the entry.
+ */
+static void achievement_readdb_validate_reward_titleid(const struct config_setting_t *t, struct achievement_data *entry)
+{
+ nullpo_retv(t);
+ nullpo_retv(entry);
+
+ libconfig->setting_lookup_int(t, "TitleId", &entry->rewards.title_id);
+}
+
+/**
+ * Validates the reward Bonus script when parsing an achievement db entry.
+ * @param[in] conf pointer to the config setting.
+ * @param[out] entry pointer to the achievement entry being parsed.
+ * @param[in] source pointer to the source file name.
+ */
+static bool achievement_readdb_rewards(const struct config_setting_t *conf, struct achievement_data *entry, const char *source)
+{
+ struct config_setting_t *t = NULL;
+
+ nullpo_retr(false, conf);
+ nullpo_retr(false, entry);
+ nullpo_retr(false, source);
+
+ VECTOR_INIT(entry->rewards.item);
+
+ if ((t = libconfig->setting_get_member(conf, "Rewards")) && config_setting_is_group(t)) {
+ /* Items */
+ achievement->readdb_validate_reward_items(t, entry);
+
+ /* Buff/Bonus Script Code */
+ achievement->readdb_validate_reward_bonus(t, entry, source);
+
+ /* Title Id */
+ // @TODO Check Title ID against title DB!
+ achievement->readdb_validate_reward_titleid(t, entry);
+
+ }
+
+ return true;
+}
+
+/**
+ * Validates the reward Bonus script when parsing an achievement db entry.
+ * @param[in] conf pointer to the config setting.
+ * @param[out] entry pointer to the achievement entry being parsed.
+ * @param[in] source pointer to the source file name.
+ */
+static void achievement_readdb_additional_fields(const struct config_setting_t *conf, struct achievement_data *entry, const char *source)
+{
+ // plugins do their own thing.
+}
+
+/**
+ * Parses the Achievement DB.
+ * @read achievement_db.conf
+ */
+static void achievement_readb(void)
+{
+ const char *filename = "db/"DBPATH"achievement_db.conf";
+ struct config_t ach_conf = { 0 };
+ struct config_setting_t *achdb = NULL, *conf = NULL;
+ int entry = 0, count = 0;
+ VECTOR_DECL(int) duplicate;
+
+ if (!libconfig->load_file(&ach_conf, filename))
+ return; // report error.
+
+ if (!(achdb = libconfig->setting_get_member(ach_conf.root, "achievement_db"))) {
+ ShowError("achievement_readdb: Could not process contents of file '%s', skipping...\n", filename);
+ libconfig->destroy(&ach_conf);
+ return;
+ }
+
+ VECTOR_INIT(duplicate);
+
+ while ((conf = libconfig->setting_get_elem(achdb, entry++))) {
+ const char *string = NULL;
+ int val = 0, i = 0;
+ struct achievement_data t_ad = { 0 }, *p_ad = NULL;
+
+ /* Achievement ID */
+ if (libconfig->setting_lookup_int(conf, "Id", &t_ad.id) == 0) {
+ ShowError("achievement_readdb: Id field for entry %d is not provided! Skipping...\n", entry);
+ continue;
+ } else if (t_ad.id <= 0 || t_ad.id > INT32_MAX) {
+ ShowError("achievement_readdb: Invalid Id %d for entry %d. Skipping...\n", t_ad.id, entry);
+ continue;
+ }
+
+ ARR_FIND(0, VECTOR_LENGTH(duplicate), i, VECTOR_INDEX(duplicate, i) == t_ad.id);
+ if (i != VECTOR_LENGTH(duplicate)) {
+ ShowError("achievement_readdb: Duplicate Id %d for entry %d. Skipping...\n", t_ad.id, entry);
+ continue;
+ }
+
+ /* Achievement Name */
+ if (libconfig->setting_lookup_mutable_string(conf, "Name", t_ad.name, ACHIEVEMENT_NAME_LENGTH) == 0) {
+ ShowError("achievement_readdb: Name field not provided for Achievement %d!\n", t_ad.id);
+ continue;
+ }
+
+ /* Achievement Type*/
+ if (libconfig->setting_lookup_string(conf, "Type", &string) == 0) {
+ ShowError("achievement_readdb: Type field not provided for Achievement %d! Skipping...\n", t_ad.id);
+ continue;
+ } else if (!script->get_constant(string, &val)) {
+ ShowError("achievement_readdb: Invalid constant %s provided as type for Achievement %d! Skipping...\n", string, t_ad.id);
+ continue;
+ }
+
+ t_ad.type = (enum achievement_types) val;
+
+ /* Objectives */
+ achievement->readdb_objectives(conf, &t_ad);
+
+ /* Rewards */
+ achievement->readdb_rewards(conf, &t_ad, filename);
+
+ /* Achievement Points */
+ libconfig->setting_lookup_int(conf, "Points", &t_ad.points);
+
+ /* Additional Fields */
+ achievement->readdb_additional_fields(conf, &t_ad, filename);
+
+ /* Allocate memory for data. */
+ CREATE(p_ad, struct achievement_data, 1);
+ *p_ad = t_ad;
+
+ /* Place in the database. */
+ idb_put(achievement->db, p_ad->id, p_ad);
+
+ /* Put achievement key in categories. */
+ VECTOR_ENSURE(achievement->category[p_ad->type], 1, 1);
+ VECTOR_PUSH(achievement->category[p_ad->type], p_ad->id);
+
+ /* Qualify for duplicate Id checks */
+ VECTOR_ENSURE(duplicate, 1, 1);
+ VECTOR_PUSH(duplicate, t_ad.id);
+
+ count++;
+ } // end of achievement_db parsing.
+
+ /* Destroy the buffer */
+ libconfig->destroy(&ach_conf);
+
+ VECTOR_CLEAR(duplicate);
+
+ ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, filename);
+}
+
+/**
+ * On server initiation.
+ * @param[in] minimal ignores alocating/reading databases.
+ */
+static void do_init_achievement(bool minimal)
+{
+ int i = 0;
+
+ if (minimal)
+ return;
+
+ /* DB Initialization */
+ achievement->db = idb_alloc(DB_OPT_RELEASE_DATA);
+
+ for (i = 0; i < ACH_TYPE_MAX; i++)
+ VECTOR_INIT(achievement->category[i]);
+
+ VECTOR_INIT(achievement->rank_exp);
+
+ /* Read LibConfig Files */
+ achievement->readdb();
+ achievement->readdb_ranks();
+}
+
+/**
+ * Cleaning function called through achievement->db->destroy()
+ */
+static int achievement_db_finalize(union DBKey key, struct DBData *data, va_list args)
+{
+ int i = 0;
+ struct achievement_data *ad = DB->data2ptr(data);
+
+ for(i = 0; i < VECTOR_LENGTH(ad->objective); i++)
+ VECTOR_CLEAR(VECTOR_INDEX(ad->objective, i).jobid);
+
+ VECTOR_CLEAR(ad->objective);
+ VECTOR_CLEAR(ad->rewards.item);
+
+ // Free the script
+ if (ad->rewards.bonus != NULL) {
+ script->free_code(ad->rewards.bonus);
+ ad->rewards.bonus = NULL;
+ }
+
+ return 0;
+}
+
+/**
+ * On server finalizing.
+ */
+static void do_final_achievement(void)
+{
+ int i = 0;
+
+ achievement->db->destroy(achievement->db, achievement->db_finalize);
+
+ for (i = 0; i < ACH_TYPE_MAX; i++)
+ VECTOR_CLEAR(achievement->category[i]);
+
+ VECTOR_CLEAR(achievement->rank_exp);
+}
+
+/**
+ * Achievement Interface
+ */
+void achievement_defaults(void)
+{
+ achievement = &achievement_s;
+ /* */
+ achievement->init = do_init_achievement;
+ achievement->final = do_final_achievement;
+ /* */
+ achievement->db_finalize = achievement_db_finalize;
+ /* */
+ achievement->readdb = achievement_readb;
+ /* */
+ achievement->readdb_objectives_sub = achievement_readdb_objective_sub;
+ achievement->readdb_objectives = achievement_readdb_objectives;
+ /* */
+ achievement->readdb_validate_criteria_mobid = achievement_readdb_validate_criteria_mobid;
+ achievement->readdb_validate_criteria_jobid = achievement_readdb_validate_criteria_jobid;
+ achievement->readdb_validate_criteria_itemid = achievement_readdb_validate_criteria_itemid;
+ achievement->readdb_validate_criteria_statustype = achievement_readdb_validate_criteria_statustype;
+ achievement->readdb_validate_criteria_itemtype = achievement_readdb_validate_criteria_itemtype;
+ achievement->readdb_validate_criteria_weaponlv = achievement_readdb_validate_criteria_weaponlv;
+ achievement->readdb_validate_criteria_achievement = achievement_readdb_validate_criteria_achievement;
+ /* */
+ achievement->readdb_rewards = achievement_readdb_rewards;
+ achievement->readdb_validate_reward_items = achievement_readdb_validate_reward_items;
+ achievement->readdb_validate_reward_item_sub = achievement_readdb_validate_reward_item_sub;
+ achievement->readdb_validate_reward_bonus = achievement_readdb_validate_reward_bonus;
+ achievement->readdb_validate_reward_titleid = achievement_readdb_validate_reward_titleid;
+ /* */
+ achievement->readdb_additional_fields = achievement_readdb_additional_fields;
+ /* */
+ achievement->readdb_ranks = achievement_readdb_ranks;
+ /* */
+ achievement->get = achievement_get;
+ achievement->ensure = achievement_ensure;
+ /* */
+ achievement->calculate_totals = achievement_calculate_totals;
+ achievement->check_complete = achievement_check_complete;
+ achievement->progress_add = achievement_progress_add;
+ achievement->progress_set = achievement_progress_set;
+ achievement->check_criteria = achievement_check_criteria;
+ /* */
+ achievement->validate = achievement_validate;
+ achievement->validate_type = achievement_validate_type;
+ /* */
+ achievement->validate_mob_kill = achievement_validate_mob_kill;
+ achievement->validate_mob_damage = achievement_validate_mob_damage;
+ achievement->validate_pc_kill = achievement_validate_pc_kill;
+ achievement->validate_pc_damage = achievement_validate_pc_damage;
+ achievement->validate_jobchange = achievement_validate_jobchange;
+ achievement->validate_stats = achievement_validate_stats;
+ achievement->validate_chatroom_create = achievement_validate_chatroom_create;
+ achievement->validate_chatroom_members = achievement_validate_chatroom_members;
+ achievement->validate_friend_add = achievement_validate_friend_add;
+ achievement->validate_party_create = achievement_validate_party_create;
+ achievement->validate_marry = achievement_validate_marry;
+ achievement->validate_adopt = achievement_validate_adopt;
+ achievement->validate_zeny = achievement_validate_zeny;
+ achievement->validate_refine = achievement_validate_refine;
+ achievement->validate_item_get = achievement_validate_item_get;
+ achievement->validate_item_sell = achievement_validate_item_sell;
+ achievement->validate_achieve = achievement_validate_achieve;
+ achievement->validate_taming = achievement_validate_taming;
+ achievement->validate_achievement_rank = achievement_validate_achievement_rank;
+ /* */
+ achievement->type_requires_criteria = achievement_type_requires_criteria;
+ /* */
+ achievement->init_titles = achievement_init_titles;
+ achievement->check_title = achievement_check_title;
+ achievement->get_rewards = achievement_get_rewards;
+}
diff --git a/src/map/achievement.h b/src/map/achievement.h
new file mode 100644
index 000000000..beba120a2
--- /dev/null
+++ b/src/map/achievement.h
@@ -0,0 +1,289 @@
+/**
+* This file is part of Hercules.
+* http://herc.ws - http://github.com/HerculesWS/Hercules
+*
+* Copyright (C) 2018 Hercules Dev Team
+* Copyright (C) Smokexyz
+*
+* Hercules is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+#ifndef MAP_ACHIEVEMENT_H
+#define MAP_ACHIEVEMENT_H
+
+#include "common/hercules.h"
+#include "common/db.h"
+#include "map/map.h" // enum status_point_types
+
+#define ACHIEVEMENT_NAME_LENGTH 50
+#define OBJECTIVE_DESCRIPTION_LENGTH 100
+
+struct achievement;
+struct map_session_data;
+struct char_achievements;
+
+/**
+ * Achievement Types
+ */
+enum achievement_types {
+ // Quest
+ ACH_QUEST, // achievement and objective specific
+ // PC Kills
+ ACH_KILL_PC_TOTAL,
+ ACH_KILL_PC_JOB,
+ ACH_KILL_PC_JOBTYPE,
+ // Mob Kills
+ ACH_KILL_MOB_CLASS,
+ // PC Damage
+ ACH_DAMAGE_PC_MAX,
+ ACH_DAMAGE_PC_TOTAL,
+ ACH_DAMAGE_PC_REC_MAX,
+ ACH_DAMAGE_PC_REC_TOTAL,
+ // Mob Damage
+ ACH_DAMAGE_MOB_MAX,
+ ACH_DAMAGE_MOB_TOTAL,
+ ACH_DAMAGE_MOB_REC_MAX,
+ ACH_DAMAGE_MOB_REC_TOTAL,
+ // Job
+ ACH_JOB_CHANGE,
+ // Status
+ ACH_STATUS,
+ ACH_STATUS_BY_JOB,
+ ACH_STATUS_BY_JOBTYPE,
+ // Chatroom
+ ACH_CHATROOM_CREATE_DEAD,
+ ACH_CHATROOM_CREATE,
+ ACH_CHATROOM_MEMBERS,
+ // Friend
+ ACH_FRIEND_ADD,
+ // Party
+ ACH_PARTY_CREATE,
+ ACH_PARTY_JOIN,
+ // Marriage
+ ACH_MARRY,
+ // Adoption
+ ACH_ADOPT_BABY,
+ ACH_ADOPT_PARENT,
+ // Zeny
+ ACH_ZENY_HOLD,
+ ACH_ZENY_GET_ONCE,
+ ACH_ZENY_GET_TOTAL,
+ ACH_ZENY_SPEND_ONCE,
+ ACH_ZENY_SPEND_TOTAL,
+ // Equipment
+ ACH_EQUIP_REFINE_SUCCESS,
+ ACH_EQUIP_REFINE_FAILURE,
+ ACH_EQUIP_REFINE_SUCCESS_TOTAL,
+ ACH_EQUIP_REFINE_FAILURE_TOTAL,
+ ACH_EQUIP_REFINE_SUCCESS_WLV,
+ ACH_EQUIP_REFINE_FAILURE_WLV,
+ ACH_EQUIP_REFINE_SUCCESS_ID,
+ ACH_EQUIP_REFINE_FAILURE_ID,
+ // Items
+ ACH_ITEM_GET_COUNT,
+ ACH_ITEM_GET_COUNT_ITEMTYPE,
+ ACH_ITEM_GET_WORTH,
+ ACH_ITEM_SELL_WORTH,
+ // Monsters
+ ACH_PET_CREATE,
+ // Achievement
+ ACH_ACHIEVE,
+ ACH_ACHIEVEMENT_RANK,
+ ACH_TYPE_MAX
+};
+
+enum unique_criteria_types {
+ CRITERIA_UNIQUE_NONE,
+ CRITERIA_UNIQUE_ACHIEVE_ID,
+ CRITERIA_UNIQUE_ITEM_ID,
+ CRITERIA_UNIQUE_STATUS_TYPE,
+ CRITERIA_UNIQUE_WEAPON_LV
+};
+
+/**
+ * Achievement Objective Structure
+ *
+ * @see achievement_type_requires_criteria()
+ * @see achievement_criteria_mobid()
+ * @see achievement_criteria_jobid()
+ * @see achievement_criteria_itemid()
+ * @see achievement_criteria_stattype()
+ * @see achievement_criteria_itemtype()
+ * @see achievement_criteria_weaponlv()
+ */
+struct achievement_objective {
+ int goal;
+ char description[OBJECTIVE_DESCRIPTION_LENGTH];
+ /**
+ * Those criteria that do not make sense if stacked together.
+ * union identifier is set in unique_type. (@see unique_criteria_type)
+ */
+ union {
+ int achieve_id;
+ int itemid;
+ enum status_point_types status_type;
+ int weapon_lv;
+ } unique;
+ enum unique_criteria_types unique_type;
+ /* */
+ uint32 item_type;
+ int mobid;
+ VECTOR_DECL(int) jobid;
+};
+
+struct achievement_reward_item {
+ int id, amount;
+};
+
+struct achievement_rewards {
+ int title_id;
+ struct script_code *bonus;
+ VECTOR_DECL(struct achievement_reward_item) item;
+};
+
+/**
+ * Achievement Data Structure
+ */
+struct achievement_data {
+ int id;
+ char name[ACHIEVEMENT_NAME_LENGTH];
+ enum achievement_types type;
+ int points;
+ VECTOR_DECL(struct achievement_objective) objective;
+ struct achievement_rewards rewards;
+};
+
+// Achievements types that use Mob ID as criteria.
+#define achievement_criteria_mobid(t) ( \
+ (t) == ACH_KILL_MOB_CLASS \
+ || (t) == ACH_PET_CREATE )
+
+// Achievements types that use JobID vector as criteria.
+#define achievement_criteria_jobid(t) ( \
+ (t) == ACH_KILL_PC_JOB \
+ || (t) == ACH_KILL_PC_JOBTYPE \
+ || (t) == ACH_JOB_CHANGE \
+ || (t) == ACH_STATUS_BY_JOB \
+ || (t) == ACH_STATUS_BY_JOBTYPE )
+
+// Achievements types that use Item ID as criteria.
+#define achievement_criteria_itemid(t) ( \
+ (t) == ACH_ITEM_GET_COUNT \
+ || (t) == ACH_EQUIP_REFINE_SUCCESS_ID \
+ || (t) == ACH_EQUIP_REFINE_FAILURE_ID )
+
+// Achievements types that use status type parameter as criteria.
+#define achievement_criteria_stattype(t) ( \
+ (t) == ACH_STATUS \
+ || (t) == ACH_STATUS_BY_JOB \
+ || (t) == ACH_STATUS_BY_JOBTYPE )
+
+// Achievements types that use item type mask as criteria.
+#define achievement_criteria_itemtype(t) ( \
+ (t) == ACH_ITEM_GET_COUNT_ITEMTYPE )
+
+// Achievements types that use weapon level as criteria.
+#define achievement_criteria_weaponlv(t) ( \
+ (t) == ACH_EQUIP_REFINE_SUCCESS_WLV \
+ || (t) == ACH_EQUIP_REFINE_FAILURE_WLV )
+
+// Valid status types for objective criteria.
+#define achievement_valid_status_types(s) ( \
+ (s) == SP_STR \
+ || (s) == SP_AGI \
+ || (s) == SP_VIT \
+ || (s) == SP_INT \
+ || (s) == SP_DEX \
+ || (s) == SP_LUK \
+ || (s) == SP_BASELEVEL || (s) == SP_JOBLEVEL )
+
+struct achievement_interface {
+ struct DBMap *db; // int id -> struct achievement_data *
+ /* */
+ VECTOR_DECL(int) rank_exp; // Achievement Rank Exp Requirements
+ VECTOR_DECL(int) category[ACH_TYPE_MAX]; /* A collection of Ids per type for faster processing. */
+ /* */
+ void (*init) (bool minimal);
+ void (*final) (void);
+ /* */
+ int (*db_finalize) (union DBKey key, struct DBData *data, va_list args);
+ /* */
+ void (*readdb)(void);
+ /* */
+ bool (*readdb_objectives_sub) (const struct config_setting_t *conf, int index, struct achievement_data *entry);
+ bool (*readdb_objectives) (const struct config_setting_t *conf, struct achievement_data *entry);
+ /* */
+ bool (*readdb_validate_criteria_mobid) (const struct config_setting_t *t, struct achievement_objective *obj, enum achievement_types type, int entry_id, int obj_idx);
+ bool (*readdb_validate_criteria_jobid) (const struct config_setting_t *t, struct achievement_objective *obj, enum achievement_types type, int entry_id, int obj_idx);
+ bool (*readdb_validate_criteria_itemid) (const struct config_setting_t *t, struct achievement_objective *obj, enum achievement_types type, int entry_id, int obj_idx);
+ bool (*readdb_validate_criteria_statustype) (const struct config_setting_t *t, struct achievement_objective *obj, enum achievement_types type, int entry_id, int obj_idx);
+ bool (*readdb_validate_criteria_itemtype) (const struct config_setting_t *t, struct achievement_objective *obj, enum achievement_types type, int entry_id, int obj_idx);
+ bool (*readdb_validate_criteria_weaponlv) (const struct config_setting_t *t, struct achievement_objective *obj, enum achievement_types type, int entry_id, int obj_idx);
+ bool (*readdb_validate_criteria_achievement) (const struct config_setting_t *t, struct achievement_objective *obj, enum achievement_types type, int entry_id, int obj_idx);
+ /* */
+ bool (*readdb_rewards) (const struct config_setting_t *conf, struct achievement_data *entry, const char *source);
+ void (*readdb_validate_reward_items) (const struct config_setting_t *t, struct achievement_data *entry);
+ bool (*readdb_validate_reward_item_sub) (const struct config_setting_t *t, int element, struct achievement_data *entry);
+ void (*readdb_validate_reward_bonus) (const struct config_setting_t *t, struct achievement_data *entry, const char *source);
+ void (*readdb_validate_reward_titleid) (const struct config_setting_t *t, struct achievement_data *entry);
+ /* */
+ void (*readdb_additional_fields) (const struct config_setting_t *conf, struct achievement_data *entry, const char *source);
+ /* */
+ void (*readdb_ranks) (void);
+ /* */
+ const struct achievement_data *(*get) (int aid);
+ struct achievement *(*ensure) (struct map_session_data *sd, const struct achievement_data *ad);
+ /* */
+ void (*calculate_totals) (const struct map_session_data *sd, int *points, int *completed, int *rank, int *curr_rank_points);
+ bool (*check_complete) (struct map_session_data *sd, const struct achievement_data *ad);
+ void (*progress_add) (struct map_session_data *sd, const struct achievement_data *ad, unsigned int obj_idx, int progress);
+ void (*progress_set) (struct map_session_data *sd, const struct achievement_data *ad, unsigned int obj_idx, int progress);
+ bool (*check_criteria) (const struct achievement_objective *objective, const struct achievement_objective *criteria);
+ /* */
+ bool (*validate) (struct map_session_data *sd, int aid, unsigned int obj_idx, int progress, bool additive);
+ int (*validate_type) (struct map_session_data *sd, enum achievement_types type, const struct achievement_objective *criteria, bool additive);
+ /* */
+ void (*validate_mob_kill) (struct map_session_data *sd, int mob_id);
+ void (*validate_mob_damage) (struct map_session_data *sd, unsigned int damage, bool received);
+ void (*validate_pc_kill) (struct map_session_data *sd, struct map_session_data *dstsd);
+ void (*validate_pc_damage) (struct map_session_data *sd, struct map_session_data *dstsd, unsigned int damage);
+ void (*validate_jobchange) (struct map_session_data *sd);
+ void (*validate_stats) (struct map_session_data *sd, enum status_point_types stat_type, int progress);
+ void (*validate_chatroom_create) (struct map_session_data *sd);
+ void (*validate_chatroom_members) (struct map_session_data *sd, int progress);
+ void (*validate_friend_add) (struct map_session_data *sd);
+ void (*validate_party_create) (struct map_session_data *sd);
+ void (*validate_marry) (struct map_session_data *sd);
+ void (*validate_adopt) (struct map_session_data *sd, bool parent);
+ void (*validate_zeny) (struct map_session_data *sd, int amount);
+ void (*validate_refine) (struct map_session_data *sd, unsigned int idx, bool success);
+ void (*validate_item_get) (struct map_session_data *sd, int nameid, int amount);
+ void (*validate_item_sell) (struct map_session_data *sd, int nameid, int amount);
+ void (*validate_achieve) (struct map_session_data *sd, int achid);
+ void (*validate_taming) (struct map_session_data *sd, int class);
+ void (*validate_achievement_rank) (struct map_session_data *sd, int rank);
+ /* */
+ bool (*type_requires_criteria) (enum achievement_types type);
+ /* */
+ void (*init_titles) (struct map_session_data *sd);
+ bool (*check_title) (struct map_session_data *sd, int title_id);
+ void (*get_rewards) (struct map_session_data *sd, const struct achievement_data *ad);
+};
+
+#ifdef HERCULES_CORE
+void achievement_defaults(void);
+#endif // HERCULES_CORE
+
+HPShared struct achievement_interface *achievement;
+
+#endif // MAP_ACHIEVEMENT_H
diff --git a/src/map/atcommand.c b/src/map/atcommand.c
index 3ec709b57..52bf64e87 100644
--- a/src/map/atcommand.c
+++ b/src/map/atcommand.c
@@ -55,6 +55,7 @@
#include "map/storage.h"
#include "map/trade.h"
#include "map/unit.h"
+#include "map/achievement.h"
#include "common/cbasetypes.h"
#include "common/conf.h"
#include "common/core.h"
@@ -1421,6 +1422,10 @@ ACMD(baselevelup)
clif->updatestatus(sd, SP_BASEEXP);
clif->updatestatus(sd, SP_NEXTBASEEXP);
pc->baselevelchanged(sd);
+
+ // achievements
+ achievement->validate_stats(sd, SP_BASELEVEL, sd->status.base_level);
+
if(sd->status.party_id)
party->send_levelup(sd);
@@ -2526,6 +2531,7 @@ ACMD(param)
clif->updatestatus(sd, SP_USTR + i);
status_calc_pc(sd, SCO_FORCE);
clif->message(fd, msg_fd(fd,42)); // Stat changed.
+ achievement->validate_stats(sd, SP_STR + i, new_value); // Achievements [Smokexyz/Hercules]
} else {
if (value < 0)
clif->message(fd, msg_fd(fd,41)); // Unable to decrease the number/value.
diff --git a/src/map/chat.c b/src/map/chat.c
index 9852131be..d9b642219 100644
--- a/src/map/chat.c
+++ b/src/map/chat.c
@@ -29,6 +29,7 @@
#include "map/npc.h" // npc_event_do()
#include "map/pc.h"
#include "map/skill.h" // ext_skill_unit_onplace()
+#include "map/achievement.h"
#include "common/cbasetypes.h"
#include "common/memmgr.h"
#include "common/mmo.h"
@@ -126,6 +127,7 @@ static bool chat_createpcchat(struct map_session_data *sd, const char *title, co
pc_stop_attack(sd);
clif->createchat(sd,0); // 0 = success
clif->dispchat(cd,0);
+ achievement->validate_chatroom_create(sd); // Achievements [Smokexyz/Hercules]
return true;
}
clif->createchat(sd,1); // 1 = Room limit exceeded
@@ -181,6 +183,9 @@ static bool chat_joinchat(struct map_session_data *sd, int chatid, const char *p
cd->usersd[cd->users] = sd;
cd->users++;
+ if (cd->owner->type == BL_PC)
+ achievement->validate_chatroom_members(BL_UCAST(BL_PC, cd->owner), cd->users);
+
pc_setchatid(sd,cd->bl.id);
clif->joinchatok(sd, cd); //To the person who newly joined the list of all
diff --git a/src/map/chrif.c b/src/map/chrif.c
index cd24c5fea..609c856c6 100644
--- a/src/map/chrif.c
+++ b/src/map/chrif.c
@@ -336,6 +336,8 @@ static bool chrif_save(struct map_session_data *sd, int flag)
elemental->save(sd->ed);
if( sd->save_quest )
intif->quest_save(sd);
+ if (VECTOR_LENGTH(sd->achievement) > 0)
+ intif->achievements_save(sd);
if (sd->storage.received == true && sd->storage.save == true)
intif->send_account_storage(sd);
diff --git a/src/map/clif.c b/src/map/clif.c
index 82507a84a..80cd44aaf 100644
--- a/src/map/clif.c
+++ b/src/map/clif.c
@@ -56,13 +56,14 @@
#include "map/trade.h"
#include "map/unit.h"
#include "map/vending.h"
+#include "map/achievement.h"
#include "common/HPM.h"
#include "common/cbasetypes.h"
#include "common/conf.h"
#include "common/ers.h"
#include "common/grfio.h"
#include "common/memmgr.h"
-#include "common/mmo.h" // NEW_CARTS
+#include "common/mmo.h" // NEW_CARTS, char_achievements
#include "common/nullpo.h"
#include "common/random.h"
#include "common/showmsg.h"
@@ -8816,17 +8817,18 @@ static void clif_refresh(struct map_session_data *sd)
/// Updates the object's (bl) name on client.
/// 0095 <id>.L <char name>.24B (ZC_ACK_REQNAME)
/// 0195 <id>.L <char name>.24B <party name>.24B <guild name>.24B <position name>.24B (ZC_ACK_REQNAMEALL)
+/// 0A30 <id>.L <char name>.24B <party name>.24B <guild name>.24B <position name>.24B <title id>.L (ZC_ACK_REQNAMEALL2)
static void clif_charnameack(int fd, struct block_list *bl)
{
- unsigned char buf[103];
- int cmd = 0x95;
+ struct packet_reqnameall_ack packet = { 0 };
+ int len = sizeof(struct packet_reqnameall_ack);
nullpo_retv(bl);
- WBUFW(buf,0) = cmd;
- WBUFL(buf,2) = bl->id;
+ packet.packet_id = reqName;
+ packet.gid = bl->id;
- switch( bl->type ) {
+ switch(bl->type) {
case BL_PC:
{
const struct map_session_data *ssd = BL_UCCAST(BL_PC, bl);
@@ -8834,17 +8836,28 @@ static void clif_charnameack(int fd, struct block_list *bl)
const struct guild *g = NULL;
int ps = -1;
+ if (ssd->fakename[0] != '\0' || ssd->status.guild_id > 0 || ssd->status.party_id > 0 || ssd->status.title_id > 0) {
+ packet.packet_id = reqNameAllType;
+ }
+
//Requesting your own "shadow" name. [Skotlex]
- if (ssd->fd == fd && ssd->disguise != -1)
- WBUFL(buf,2) = -bl->id;
+ if (ssd->fd == fd && ssd->disguise != -1) {
+ packet.gid = -bl->id;
+ }
if (ssd->fakename[0] != '\0') {
- WBUFW(buf, 0) = cmd = 0x195;
- memcpy(WBUFP(buf,6), ssd->fakename, NAME_LENGTH);
- WBUFB(buf,30) = WBUFB(buf,54) = WBUFB(buf,78) = 0;
+ memcpy(packet.name, ssd->fakename, NAME_LENGTH);
break;
}
- memcpy(WBUFP(buf,6), ssd->status.name, NAME_LENGTH);
+
+#if PACKETVER >= 20150503
+ // Title System [Dastgir/Hercules]
+ if (ssd->status.title_id > 0) {
+ packet.title_id = ssd->status.title_id;
+ }
+#endif
+
+ memcpy(packet.name, ssd->status.name, NAME_LENGTH);
if (ssd->status.party_id != 0) {
p = party->search(ssd->status.party_id);
@@ -8866,47 +8879,41 @@ static void clif_charnameack(int fd, struct block_list *bl)
if (p == NULL && g == NULL)
break;
- WBUFW(buf, 0) = cmd = 0x195;
- if (p != NULL)
- memcpy(WBUFP(buf,30), p->party.name, NAME_LENGTH);
- else
- WBUFB(buf,30) = 0;
+ if (p != NULL) {
+ memcpy(packet.party_name, p->party.name, NAME_LENGTH);
+ }
if (g != NULL && ps >= 0 && ps < MAX_GUILDPOSITION) {
- memcpy(WBUFP(buf,54), g->name,NAME_LENGTH);
- memcpy(WBUFP(buf,78), g->position[ps].name, NAME_LENGTH);
- } else { //Assume no guild.
- WBUFB(buf,54) = 0;
- WBUFB(buf,78) = 0;
+ memcpy(packet.guild_name, g->name,NAME_LENGTH);
+ memcpy(packet.position_name, g->position[ps].name, NAME_LENGTH);
}
}
break;
//[blackhole89]
case BL_HOM:
- memcpy(WBUFP(buf,6), BL_UCCAST(BL_HOM, bl)->homunculus.name, NAME_LENGTH);
+ memcpy(packet.name, BL_UCCAST(BL_HOM, bl)->homunculus.name, NAME_LENGTH);
break;
case BL_MER:
- memcpy(WBUFP(buf,6), BL_UCCAST(BL_MER, bl)->db->name, NAME_LENGTH);
+ memcpy(packet.name, BL_UCCAST(BL_MER, bl)->db->name, NAME_LENGTH);
break;
case BL_PET:
- memcpy(WBUFP(buf,6), BL_UCCAST(BL_PET, bl)->pet.name, NAME_LENGTH);
+ memcpy(packet.name, BL_UCCAST(BL_PET, bl)->pet.name, NAME_LENGTH);
break;
case BL_NPC:
- memcpy(WBUFP(buf,6), BL_UCCAST(BL_NPC, bl)->name, NAME_LENGTH);
+ memcpy(packet.name, BL_UCCAST(BL_NPC, bl)->name, NAME_LENGTH);
break;
case BL_MOB:
{
const struct mob_data *md = BL_UCCAST(BL_MOB, bl);
- memcpy(WBUFP(buf,6), md->name, NAME_LENGTH);
+ memcpy(packet.name, md->name, NAME_LENGTH);
if (md->guardian_data && md->guardian_data->g) {
- WBUFW(buf, 0) = cmd = 0x195;
- WBUFB(buf,30) = 0;
- memcpy(WBUFP(buf,54), md->guardian_data->g->name, NAME_LENGTH);
- memcpy(WBUFP(buf,78), md->guardian_data->castle->castle_name, NAME_LENGTH);
+ packet.packet_id = reqNameAllType;
+ memcpy(packet.guild_name, md->guardian_data->g->name, NAME_LENGTH);
+ memcpy(packet.position_name, md->guardian_data->castle->castle_name, NAME_LENGTH);
} else if (battle_config.show_mob_info) {
char mobhp[50], *str_p = mobhp;
- WBUFW(buf, 0) = cmd = 0x195;
+ packet.packet_id = reqNameAllType;
if (battle_config.show_mob_info&4)
str_p += sprintf(str_p, "Lv. %d | ", md->level);
if (battle_config.show_mob_info&1)
@@ -8917,34 +8924,39 @@ static void clif_charnameack(int fd, struct block_list *bl)
//can parse it. [Skotlex]
if (str_p != mobhp) {
*(str_p-3) = '\0'; //Remove trailing space + pipe.
- memcpy(WBUFP(buf,30), mobhp, NAME_LENGTH);
- WBUFB(buf,54) = 0;
- WBUFB(buf,78) = 0;
+ memcpy(packet.party_name, mobhp, NAME_LENGTH);
}
}
}
break;
case BL_CHAT:
#if 0 //FIXME: Clients DO request this... what should be done about it? The chat's title may not fit... [Skotlex]
- memcpy(WBUFP(buf,6), BL_UCCAST(BL_CHAT, bl)->title, NAME_LENGTH);
+ memcpy(packet.name, BL_UCCAST(BL_CHAT, bl)->title, NAME_LENGTH);
break;
#endif
return;
case BL_ELEM:
- memcpy(WBUFP(buf,6), BL_UCCAST(BL_ELEM, bl)->db->name, NAME_LENGTH);
+ memcpy(packet.name, BL_UCCAST(BL_ELEM, bl)->db->name, NAME_LENGTH);
break;
default:
ShowError("clif_charnameack: bad type %u(%d)\n", bl->type, bl->id);
return;
}
+ if (packet.packet_id == reqName) {
+ len = sizeof(struct packet_reqname_ack);
+ }
+ // if no recipient specified just update nearby clients
// if no recipient specified just update nearby clients
if (fd == 0) {
- clif->send(buf, packet_len(cmd), bl, AREA);
+ clif->send(&packet, len, bl, AREA);
} else {
- WFIFOHEAD(fd, packet_len(cmd));
- memcpy(WFIFOP(fd, 0), buf, packet_len(cmd));
- WFIFOSET(fd, packet_len(cmd));
+ struct map_session_data *sd = sockt->session_is_valid(fd) ? sockt->session[fd]->session_data : NULL;
+ if (sd != NULL) {
+ clif->send(&packet, len, &sd->bl, SELF);
+ } else {
+ clif->send(&packet, len, bl, SELF);
+ }
}
}
@@ -8952,54 +8964,52 @@ static void clif_charnameack(int fd, struct block_list *bl)
//Needed because when you send a 0x95 packet, the client will not remove the cached party/guild info that is not sent.
static void clif_charnameupdate(struct map_session_data *ssd)
{
- unsigned char buf[103];
- int cmd = 0x195, ps = -1;
+ int ps = -1;
struct party_data *p = NULL;
struct guild *g = NULL;
+ struct packet_reqnameall_ack packet = { 0 };
nullpo_retv(ssd);
- if( ssd->fakename[0] )
+ if (ssd->fakename[0])
return; //No need to update as the party/guild was not displayed anyway.
- WBUFW(buf,0) = cmd;
- WBUFL(buf,2) = ssd->bl.id;
+ packet.packet_id = reqNameAllType;
+ packet.gid = ssd->bl.id;
- memcpy(WBUFP(buf,6), ssd->status.name, NAME_LENGTH);
+ memcpy(packet.name, ssd->status.name, NAME_LENGTH);
if (!battle_config.display_party_name) {
if (ssd->status.party_id > 0 && ssd->status.guild_id > 0 && (g = ssd->guild) != NULL)
p = party->search(ssd->status.party_id);
- }else{
+ } else {
if (ssd->status.party_id > 0)
p = party->search(ssd->status.party_id);
}
- if( ssd->status.guild_id > 0 && (g = ssd->guild) != NULL )
- {
+ if (ssd->status.guild_id > 0 && (g = ssd->guild) != NULL) {
int i;
ARR_FIND(0, g->max_member, i, g->member[i].account_id == ssd->status.account_id && g->member[i].char_id == ssd->status.char_id);
if( i < g->max_member ) ps = g->member[i].position;
}
- if( p )
- memcpy(WBUFP(buf,30), p->party.name, NAME_LENGTH);
- else
- WBUFB(buf,30) = 0;
+ if (p != NULL)
+ memcpy(packet.party_name, p->party.name, NAME_LENGTH);
- if( g && ps >= 0 && ps < MAX_GUILDPOSITION )
- {
- memcpy(WBUFP(buf,54), g->name,NAME_LENGTH);
- memcpy(WBUFP(buf,78), g->position[ps].name, NAME_LENGTH);
+ if (g != NULL && ps >= 0 && ps < MAX_GUILDPOSITION) {
+ memcpy(packet.guild_name, g->name,NAME_LENGTH);
+ memcpy(packet.position_name, g->position[ps].name, NAME_LENGTH);
}
- else
- {
- WBUFB(buf,54) = 0;
- WBUFB(buf,78) = 0;
+
+#if PACKETVER >= 20150503
+ // Achievement System [Dastgir/Hercules]
+ if (ssd->status.title_id > 0) {
+ packet.title_id = ssd->status.title_id;
}
+#endif
// Update nearby clients
- clif->send(buf, packet_len(cmd), &ssd->bl, AREA);
+ clif->send(&packet, sizeof(packet), &ssd->bl, AREA);
}
/// Taekwon Jump (TK_HIGHJUMP) effect (ZC_HIGHJUMP).
@@ -14787,6 +14797,7 @@ static void clif_parse_FriendsListReply(int fd, struct map_session_data *sd)
f_sd->status.friends[i].char_id = sd->status.char_id;
memcpy(f_sd->status.friends[i].name, sd->status.name, NAME_LENGTH);
clif->friendslist_reqack(f_sd, sd, 0);
+ achievement->validate_friend_add(f_sd); // Achievements [Smokexyz/Hercules]
if (battle_config.friend_auto_add) {
// Also add f_sd to sd's friendlist.
@@ -14805,6 +14816,7 @@ static void clif_parse_FriendsListReply(int fd, struct map_session_data *sd)
sd->status.friends[i].char_id = f_sd->status.char_id;
memcpy(sd->status.friends[i].name, f_sd->status.name, NAME_LENGTH);
clif->friendslist_reqack(sd, f_sd, 0);
+ achievement->validate_friend_add(sd); // Achievements [Smokexyz/Hercules]
}
}
}
@@ -20224,6 +20236,216 @@ static unsigned short clif_parse_cmd_optional(int fd, struct map_session_data *s
return cmd;
}
+/**
+ * Send all of a session's achievement information to client.
+ * Called only once on login/char-loading. (PACKET_ZC_ALL_ACH_LIST)
+ * @packet [out] 0x0A23 <ID>.W <Length>.W <ach_count>.L <total_points>.L <rank>.W <current_rank_points>.L <next_rank_points>.L <struct ach_list_info *[]>.P
+ * @param fd socket descriptor
+ * @param sd pointer to map_session_data
+ */
+static void clif_achievement_send_list(int fd, struct map_session_data *sd)
+{
+#if PACKETVER >= 20141016
+ int i = 0, count = 0, curr_exp_tmp = 0;
+ struct packet_achievement_list p = { 0 };
+
+ nullpo_retv(sd);
+
+ /* Browse through the session's achievement list and gather their values. */
+ for (i = 0; i < VECTOR_LENGTH(sd->achievement); i++) {
+ int j = 0;
+ struct achievement *a = &VECTOR_INDEX(sd->achievement, i);
+ const struct achievement_data *ad = NULL;
+
+ /* Sanity check for nonull pointers. */
+ if (a == NULL || (ad = achievement->get(a->id)) == NULL)
+ continue;
+
+ p.ach[count].ach_id = a->id;
+
+ for (j = 0; j < VECTOR_LENGTH(ad->objective); j++)
+ p.ach[count].objective[j] = a->objective[j];
+
+ if (a->completed_at) {
+ p.ach[count].completed = 1;
+ p.ach[count].completed_at = (uint32) a->completed_at;
+ p.ach[count].reward = a->rewarded_at ? 1 : 0;
+ p.total_points += ad->points;
+ }
+
+ count++;
+ }
+
+ p.packet_id = achievementListType;
+ p.packet_len = sizeof(struct ach_list_info) * count + 22;
+ p.total_achievements = count;
+ /* Determine Achievement Rank and current exp */
+ curr_exp_tmp = p.total_points;
+ for (i = 0; curr_exp_tmp && i < MAX_ACHIEVEMENT_RANKS && i < VECTOR_LENGTH(achievement->rank_exp); i++) {
+ if (curr_exp_tmp >= VECTOR_INDEX(achievement->rank_exp, i)) {
+ curr_exp_tmp -= VECTOR_INDEX(achievement->rank_exp, i);
+ p.rank++;
+
+ // Validate achievement rank type achievements.
+ achievement->validate_achievement_rank(sd, p.rank);
+ }
+ }
+ /* Determine Achievement Rank Exp */
+ p.current_rank_points = curr_exp_tmp;
+ p.next_rank_points = VECTOR_INDEX(achievement->rank_exp, p.rank);
+ /* Send payload */
+ clif->send(&p, p.packet_len, &sd->bl, SELF);
+#endif // PACKETVER >= 20141016
+}
+
+/**
+ * Sends achievement information for a single achievement.
+ * Called on objective progress updates/completion. (PACKET_ZC_ACH_UPDATE)
+ * @packet [out] 0x0A24 <ID>.W <total_points>.L <rank>.W <current_rank_points>.L <next_rank_points>.L <struct ach_list_info *>.P
+ * @param fd socket descriptor
+ * @param sd pointer to struct map_session_data
+ * @param ad const pointer to struct achievement_data from the achievement db.
+ */
+static void clif_achievement_send_update(int fd, struct map_session_data *sd, const struct achievement_data *ad)
+{
+#if PACKETVER >= 20141016
+ struct packet_achievement_update p = { 0 };
+ struct achievement *a = NULL;
+ int i = 0, points = 0, rank = 0, curr_rank_points = 0;
+
+ nullpo_retv(sd);
+ nullpo_retv(ad);
+
+ /* Get Session Achievement Data */
+ if ((a = achievement->ensure(sd, ad)) == NULL)
+ return;
+
+ /* Get total points, current rank and current rank points from the session. */
+ achievement->calculate_totals(sd, &points, NULL, &rank, &curr_rank_points);
+
+ p.packet_id = achievementUpdateType;
+
+ /* Determine Total Achievement Points */
+ p.total_points = points;
+
+ /* Determine Achievement Rank */
+ p.rank = rank;
+
+ /* Determine Achievement Rank Exp */
+ p.current_rank_points = curr_rank_points;
+ p.next_rank_points = VECTOR_INDEX(achievement->rank_exp, p.rank);
+
+ p.ach.ach_id = ad->id;
+ p.ach.completed = (uint8) achievement->check_complete(sd, ad);
+
+ for (i = 0; i < VECTOR_LENGTH(ad->objective); i++)
+ p.ach.objective[i] = a->objective[i];
+
+ p.ach.completed_at = (uint32) a->completed_at;
+
+ p.ach.reward = a->rewarded_at ? 1 : 0;
+
+ clif->send(&p, packet_len(achievementUpdateType), &sd->bl, SELF);
+
+ /* Validate rank-related achievements */
+ achievement->validate_achievement_rank(sd, rank);
+
+#endif // PACKETVER >= 20141016
+}
+
+/**
+ * Parses achievement reward packet from session.
+ * @packet [in] 0x0A25 <ach_id>.L
+ * @param fd socket descriptor.
+ * @param sd ptr to session data.
+ */
+static void clif_parse_achievement_get_reward(int fd, struct map_session_data *sd) __attribute__((nonnull(2)));
+static void clif_parse_achievement_get_reward(int fd, struct map_session_data *sd)
+{
+#if PACKETVER >= 20141016
+ int ach_id = RFIFOL(fd, 2);
+ const struct achievement_data *ad = NULL;
+ struct achievement *ach = NULL;
+
+ if (ach_id <= 0 || (ad = achievement->get(ach_id)) == NULL)
+ return;
+
+ if ((ach = achievement->ensure(sd, ad)) == NULL)
+ return;
+
+ if (achievement->check_complete(sd, ad) && ach->completed_at && ach->rewarded_at == 0) {
+ achievement->get_rewards(sd, ad);
+ }
+#endif // PACKETVER >= 20141016
+}
+
+/**
+ * Sends achievement reward collection acknowledgement to the client.
+ * @packet [out] 0x0A26 <packet_id>.W <received
+ */
+static void clif_achievement_reward_ack(int fd, struct map_session_data *sd, const struct achievement_data *ad)
+{
+#if PACKETVER >= 20141016
+ struct packet_achievement_reward_ack p = { 0 };
+
+ nullpo_retv(sd);
+ nullpo_retv(ad);
+
+ p.packet_id = achievementRewardAckType;
+ p.received = 1;
+ p.ach_id = ad->id;
+
+ clif->send(&p, packet_len(achievementRewardAckType), &sd->bl, SELF);
+#endif // PACKETVER >= 20141016
+}
+
+/**
+ * Sends achievement reward collection acknowledgement to the client.
+ * @packet[in] 0x0A2E <packet_id>.W <title_id>.L
+ */
+static void clif_parse_change_title(int fd, struct map_session_data *sd) __attribute__((nonnull(2)));
+static void clif_parse_change_title(int fd, struct map_session_data *sd)
+{
+ int title_id = RFIFOL(fd, 2);
+
+ if (title_id == sd->status.title_id) { // Same Title
+ return;
+ } else if (title_id < 0) {
+ title_id = 0;
+ }
+
+ clif->change_title_ack(fd, sd, title_id);
+}
+
+/**
+ * [clif_change_title_ack description]
+ * @packet [out] 0x0A2F <packet_id>.W <Result>.B <title_id>.L
+ */
+static void clif_change_title_ack(int fd, struct map_session_data *sd, int title_id)
+{
+#if PACKETVER >= 20141016
+ unsigned char failed = 0;
+
+ if (!achievement->check_title(sd, title_id)) {
+ clif->message(fd, "Title is not yet earned.");
+ failed = 1;
+ }
+
+ sd->status.title_id = title_id;
+
+ WFIFOHEAD(fd, packet_len(0xa2f));
+ WFIFOW(fd, 0) = 0xa2f;
+ WFIFOB(fd, 2) = failed;
+ WFIFOL(fd, 3) = sd->status.title_id;
+ WFIFOSET(fd, packet_len(0xa2f));
+
+ // Update names
+ clif->charnameack(fd, &sd->bl);
+ clif->charnameack(0, &sd->bl);
+#endif
+}
+// End of Achievement System
+
/*==========================================
* RoDEX
*------------------------------------------*/
@@ -22166,6 +22388,14 @@ void clif_defaults(void)
clif->isdisguised = clif_isdisguised;
clif->navigate_to = clif_navigate_to;
clif->bl_type = clif_bl_type;
+ /* Achievement System */
+ clif->achievement_send_list = clif_achievement_send_list;
+ clif->achievement_send_update = clif_achievement_send_update;
+ clif->pAchievementGetReward = clif_parse_achievement_get_reward;
+ clif->achievement_reward_ack = clif_achievement_reward_ack;
+ /* Title */
+ clif->change_title_ack = clif_change_title_ack;
+ clif->pChangeTitle = clif_parse_change_title;
/*------------------------
*- Parse Incoming Packet
@@ -22413,6 +22643,7 @@ void clif_defaults(void)
clif->pHotkeyRowShift = clif_parse_HotkeyRowShift;
clif->dressroom_open = clif_dressroom_open;
clif->pOneClick_ItemIdentify = clif_parse_OneClick_ItemIdentify;
+ /* Achievements [Smokexyz/Hercules] */
clif->get_bl_name = clif_get_bl_name;
/* RODEX */
clif->pRodexOpenWriteMail = clif_parse_rodex_open_write_mail;
diff --git a/src/map/clif.h b/src/map/clif.h
index 0077566b5..8cff8a21e 100644
--- a/src/map/clif.h
+++ b/src/map/clif.h
@@ -54,6 +54,7 @@ struct skill_cd;
struct skill_unit;
struct unit_data;
struct view_data;
+struct achievement_data; // map/achievement.h
enum clif_messages;
enum rodex_add_item;
@@ -1197,6 +1198,13 @@ struct clif_interface {
bool (*isdisguised) (struct block_list* bl);
void (*navigate_to) (struct map_session_data *sd, const char* mapname, uint16 x, uint16 y, uint8 flag, bool hideWindow, uint16 mob_id);
unsigned char (*bl_type) (struct block_list *bl);
+ /* Achievement System */
+ void (*achievement_send_list) (int fd, struct map_session_data *sd);
+ void (*achievement_send_update) (int fd, struct map_session_data *sd, const struct achievement_data *ad);
+ void (*pAchievementGetReward) (int fd, struct map_session_data *sd);
+ void (*achievement_reward_ack) (int fd, struct map_session_data *sd, const struct achievement_data *ad);
+ void (*change_title_ack) (int fd, struct map_session_data *sd, int title_id);
+ void (*pChangeTitle) (int fd, struct map_session_data *sd);
/*------------------------
*- Parse Incoming Packet
*------------------------*/
diff --git a/src/map/intif.c b/src/map/intif.c
index 393058a8a..5fafc0913 100644
--- a/src/map/intif.c
+++ b/src/map/intif.c
@@ -41,6 +41,8 @@
#include "map/quest.h"
#include "map/rodex.h"
#include "map/storage.h"
+#include "map/achievement.h"
+
#include "common/memmgr.h"
#include "common/nullpo.h"
#include "common/showmsg.h"
@@ -1730,6 +1732,99 @@ static void intif_parse_DeleteHomunculusOk(int fd)
ShowError("Homunculus data delete failure\n");
}
+/***************************************
+ * ACHIEVEMENT SYSTEM FUNCTIONS
+ ***************************************/
+/**
+ * Sends a request to the inter-server to load
+ * and send a character's achievement data.
+ * @packet [out] 0x3012 <char_id>.L
+ * @param sd pointer to map_session_data.
+ */
+static void intif_achievements_request(struct map_session_data *sd)
+{
+ nullpo_retv(sd);
+
+ if (intif->CheckForCharServer())
+ return;
+
+ WFIFOHEAD(inter_fd, 6);
+ WFIFOW(inter_fd, 0) = 0x3012;
+ WFIFOL(inter_fd, 2) = sd->status.char_id;
+ WFIFOSET(inter_fd, 6);
+}
+
+/**
+ * Handles reception of achievement data for a character from the inter-server.
+ * @packet [in] 0x3810 <packet_len>.W <char_id>.L <char_achievements[]>.P
+ * @param fd socket descriptor.
+ */
+static void intif_parse_achievements_load(int fd)
+{
+ int size = RFIFOW(fd, 2);
+ int char_id = RFIFOL(fd, 4);
+ int payload_count = (size - 8) / sizeof(struct achievement);
+ struct map_session_data *sd = map->charid2sd(char_id);
+ int i = 0;
+
+ if (sd == NULL) {
+ ShowError("intif_parse_achievements_load: Parse request for achievements received but character is offline!\n");
+ return;
+ }
+
+ if (VECTOR_LENGTH(sd->achievement) > 0) {
+ ShowError("intif_parse_achievements_load: Achievements already loaded! Possible multiple calls from the inter-server received.\n");
+ return;
+ }
+
+ VECTOR_ENSURE(sd->achievement, payload_count, 1);
+
+ for (i = 0; i < payload_count; i++) {
+ struct achievement t_ach = { 0 };
+
+ memcpy(&t_ach, RFIFOP(fd, 8 + i * sizeof(struct achievement)), sizeof(struct achievement));
+
+ if (achievement->get(t_ach.id) == NULL) {
+ ShowError("intif_parse_achievements_load: Invalid Achievement %d received from character %d. Ignoring...\n", t_ach.id, char_id);
+ continue;
+ }
+
+ VECTOR_PUSH(sd->achievement, t_ach);
+ }
+
+ achievement->init_titles(sd);
+ clif->achievement_send_list(fd, sd);
+ sd->achievements_received = true;
+}
+
+/**
+ * Send character's achievement data to the inter-server.
+ * @packet 0x3013 <packet_len>.W <char_id>.L <achievements[]>.P
+ * @param sd pointer to map session data.
+ */
+static void intif_achievements_save(struct map_session_data *sd)
+{
+ int packet_len = 0, payload_size = 0, i = 0;
+
+ nullpo_retv(sd);
+ /* check for character server. */
+ if (intif->CheckForCharServer())
+ return;
+ /* Return if no data. */
+ if (!(payload_size = VECTOR_LENGTH(sd->achievement) * sizeof(struct achievement)))
+ return;
+
+ packet_len = payload_size + 8;
+
+ WFIFOHEAD(inter_fd, packet_len);
+ WFIFOW(inter_fd, 0) = 0x3013;
+ WFIFOW(inter_fd, 2) = packet_len;
+ WFIFOL(inter_fd, 4) = sd->status.char_id;
+ for (i = 0; i < VECTOR_LENGTH(sd->achievement); i++)
+ memcpy(WFIFOP(inter_fd, 8 + i * sizeof(struct achievement)), &VECTOR_INDEX(sd->achievement, i), sizeof(struct achievement));
+ WFIFOSET(inter_fd, packet_len);
+}
+
/**************************************
* QUESTLOG SYSTEM FUNCTIONS *
**************************************/
@@ -2799,6 +2894,7 @@ static int intif_parse(int fd)
case 0x3806: intif->pChangeNameOk(fd); break;
case 0x3807: intif->pMessageToFD(fd); break;
case 0x3808: intif->pAccountStorageSaveAck(fd); break;
+ case 0x3810: intif->pAchievementsLoad(fd); break;
case 0x3818: intif->pLoadGuildStorage(fd); break;
case 0x3819: intif->pSaveGuildStorage(fd); break;
case 0x3820: intif->pPartyCreated(fd); break;
@@ -2896,7 +2992,7 @@ void intif_defaults(void)
{
const int packet_len_table [INTIF_PACKET_LEN_TABLE_SIZE] = {
-1,-1,27,-1, -1,-1,37,-1, 7, 0, 0, 0, 0, 0, 0, 0, //0x3800-0x380f
- 0, 0, 0, 0, 0, 0, 0, 0, -1,11, 0, 0, 0, 0, 0, 0, //0x3810
+ -1, 0, 0, 0, 0, 0, 0, 0, -1,11, 0, 0, 0, 0, 0, 0, //0x3810 Achievements [Smokexyz/Hercules]
39,-1,15,15, 14,19, 7,-1, 0, 0, 0, 0, 0, 0, 0, 0, //0x3820
10,-1,15, 0, 79,23, 7,-1, 0,-1,-1,-1, 14,67,186,-1, //0x3830
-1, 0, 0,14, 0, 0, 0, 0, -1,74,-1,11, 11,-1, 0, 0, //0x3840
@@ -3001,6 +3097,9 @@ void intif_defaults(void)
intif->CheckForCharServer = CheckForCharServer;
/* */
intif->itembound_req = intif_itembound_req;
+ /* Achievement System */
+ intif->achievements_request = intif_achievements_request;
+ intif->achievements_save = intif_achievements_save;
/* parse functions */
intif->pWisMessage = intif_parse_WisMessage;
intif->pWisEnd = intif_parse_WisEnd;
@@ -3073,4 +3172,6 @@ void intif_defaults(void)
intif->pRodexCheckName = intif_parse_RodexCheckName;
/* Clan System */
intif->pRecvClanMemberAction = intif_parse_RecvClanMemberAction;
+ /* Achievement System */
+ intif->pAchievementsLoad = intif_parse_achievements_load;
}
diff --git a/src/map/intif.h b/src/map/intif.h
index c75b93201..1e98d11f8 100644
--- a/src/map/intif.h
+++ b/src/map/intif.h
@@ -145,6 +145,9 @@ struct intif_interface {
void (*request_accinfo) (int u_fd, int aid, int group_lv, char* query);
/* */
int (*CheckForCharServer) (void);
+ /* Achievement System [Smokexyz/Hercules] */
+ void(*achievements_request) (struct map_session_data *sd);
+ void(*achievements_save) (struct map_session_data *sd);
/* */
void (*pWisMessage) (int fd);
void (*pWisEnd) (int fd);
@@ -217,6 +220,8 @@ struct intif_interface {
void(*pRodexCheckName) (int fd);
/* Clan System */
void (*pRecvClanMemberAction) (int fd);
+ /* Achievements */
+ void (*pAchievementsLoad) (int fd);
};
#ifdef HERCULES_CORE
diff --git a/src/map/map.c b/src/map/map.c
index 0124a3035..ce8f4cdf5 100644
--- a/src/map/map.c
+++ b/src/map/map.c
@@ -59,6 +59,7 @@
#include "map/rodex.h"
#include "map/trade.h"
#include "map/unit.h"
+#include "map/achievement.h"
#include "common/HPM.h"
#include "common/cbasetypes.h"
#include "common/conf.h"
@@ -6134,6 +6135,7 @@ int do_final(void)
map->list_final();
vending->final();
rodex->final();
+ achievement->final();
HPM_map_do_final();
@@ -6338,6 +6340,7 @@ static void map_load_defaults(void)
pet_defaults();
path_defaults();
quest_defaults();
+ achievement_defaults();
npc_chat_defaults();
rodex_defaults();
}
@@ -6653,6 +6656,7 @@ int do_init(int argc, char *argv[])
mercenary->init(minimal);
elemental->init(minimal);
quest->init(minimal);
+ achievement->init(minimal);
npc->init(minimal);
unit->init(minimal);
bg->init(minimal);
diff --git a/src/map/map.h b/src/map/map.h
index 04525b4d5..207fef2ce 100644
--- a/src/map/map.h
+++ b/src/map/map.h
@@ -66,6 +66,16 @@ enum E_MAPSERVER_ST {
#define block_free_max 1048576
#define BL_LIST_MAX 1048576
+// The following system marks a different job ID system used by the map server,
+// which makes a lot more sense than the normal one. [Skotlex]
+// These marks the "level" of the job.
+#define JOBL_2_1 0x0100
+#define JOBL_2_2 0x0200
+#define JOBL_2 0x0300 // JOBL_2_1 | JOBL_2_2
+#define JOBL_UPPER 0x1000
+#define JOBL_BABY 0x2000
+#define JOBL_THIRD 0x4000
+
// For filtering and quick checking.
#define MAPID_BASEMASK 0x00ff
#define MAPID_UPPERMASK 0x0fff
@@ -537,6 +547,7 @@ struct flooritem_data {
};
enum status_point_types { //we better clean up this enum and change it name [Hemagx]
+ SP_NONE = -1,
SP_SPEED,SP_BASEEXP,SP_JOBEXP,SP_KARMA,SP_MANNER,SP_HP,SP_MAXHP,SP_SP, // 0-7
SP_MAXSP,SP_STATUSPOINT,SP_0a,SP_BASELEVEL,SP_SKILLPOINT,SP_STR,SP_AGI,SP_VIT, // 8-15
SP_INT,SP_DEX,SP_LUK,SP_CLASS,SP_ZENY,SP_SEX,SP_NEXTBASEEXP,SP_NEXTJOBEXP, // 16-23
diff --git a/src/map/mob.c b/src/map/mob.c
index 0dbff9211..27039490c 100644
--- a/src/map/mob.c
+++ b/src/map/mob.c
@@ -44,6 +44,7 @@
#include "map/script.h"
#include "map/skill.h"
#include "map/status.h"
+#include "map/achievement.h"
#include "common/HPM.h"
#include "common/cbasetypes.h"
#include "common/conf.h"
@@ -2188,6 +2189,10 @@ static void mob_damage(struct mob_data *md, struct block_list *src, int damage)
if (src)
mob->log_damage(md, src, damage);
md->dmgtick = timer->gettick();
+
+ // Achievements [Smokexyz/Hercules]
+ if (src != NULL && src->type == BL_PC)
+ achievement->validate_mob_damage(BL_UCAST(BL_PC, src), damage, false);
}
if (battle_config.show_mob_info&3)
@@ -2420,6 +2425,8 @@ static int mob_dead(struct mob_data *md, struct block_list *src, int type)
}
if(zeny) // zeny from mobs [Valaris]
pc->getzeny(tmpsd[i], zeny, LOG_TYPE_PICKDROP_MONSTER, NULL);
+
+ achievement->validate_mob_kill(tmpsd[i], md->db->mob_id); // Achievements [Smokexyz/Hercules]
}
}
diff --git a/src/map/npc.c b/src/map/npc.c
index 383558eaf..3a00b38ea 100644
--- a/src/map/npc.c
+++ b/src/map/npc.c
@@ -40,6 +40,7 @@
#include "map/skill.h"
#include "map/status.h"
#include "map/unit.h"
+#include "map/achievement.h"
#include "common/HPM.h"
#include "common/cbasetypes.h"
#include "common/db.h"
@@ -2272,6 +2273,9 @@ static int npc_selllist(struct map_session_data *sd, struct itemlist *item_list)
}
pc->delitem(sd, idx, entry->amount, 0, DELITEM_SOLD, LOG_TYPE_NPC);
+
+ // Achievements [Smokexyz/Hercules]
+ achievement->validate_item_sell(sd, sd->status.inventory[idx].nameid, entry->amount);
}
if (z + sd->status.zeny > MAX_ZENY && nd->master_nd == NULL)
diff --git a/src/map/packets.h b/src/map/packets.h
index 8ef011d08..61bf625cf 100644
--- a/src/map/packets.h
+++ b/src/map/packets.h
@@ -3209,7 +3209,7 @@ packet(0x96e,-1,clif->ackmergeitems);
packet(0x0a22,3); // ZC_RECV_ROULETTE_ITEM
packet(0x0a23,-1); // ZC_ALL_ACH_LIST
packet(0x0a24,35); // ZC_ACH_UPDATE
- packet(0x0a25,6,clif->pDull/*,XXX*/); // CZ_REQ_ACH_REWARD
+ packet(0x0a25,6,clif->pAchievementGetReward, 2); // CZ_REQ_ACH_REWARD
packet(0x0a26,7); // ZC_REQ_ACH_REWARD_ACK
// changed packet sizes
packet(0x0a18,14); // ZC_ACCEPT_ENTER3
@@ -3266,7 +3266,7 @@ packet(0x96e,-1,clif->ackmergeitems);
// 2014-09-03aRagexeRE
#if PACKETVER >= 20140903
// new packets
- packet(0x0a2e,6,clif->pDull/*,XXX*/); // CZ_REQ_CHANGE_TITLE
+ packet(0x0a2e,6,clif->pChangeTitle); // CZ_REQ_CHANGE_TITLE
packet(0x0a2f,7); // ZC_ACK_CHANGE_TITLE
// changed packet sizes
#endif
diff --git a/src/map/packets_struct.h b/src/map/packets_struct.h
index e8207d29c..5f6443ef3 100644
--- a/src/map/packets_struct.h
+++ b/src/map/packets_struct.h
@@ -302,6 +302,11 @@ enum packet_headers {
rouletteinfoackType = 0xa1c,
roulettgenerateackType = 0xa20,
roulettercvitemackType = 0xa22,
+#if PACKETVER >= 20141016
+ achievementListType = 0xa23,
+ achievementUpdateType = 0xa24,
+ achievementRewardAckType = 0xa26,
+#endif // PACKETVER >= 20141016
#if PACKETVER >= 20150513 // [4144] 0x09f8 handling in client from 2014-10-29aRagexe and 2014-03-26cRagexeRE
questListType = 0x9f8, ///< ZC_ALL_QUEST_LIST3
#elif PACKETVER >= 20141022
@@ -401,6 +406,12 @@ enum packet_headers {
#else
hominfoType = 0x22e,
#endif
+ reqName = 0x95,
+#if PACKETVER >= 20150503 // Confirm this?
+ reqNameAllType = 0xA30,
+#else
+ reqNameAllType = 0x195,
+#endif
};
#if !defined(sun) && (!defined(__NETBSD__) || __NetBSD_Version__ >= 600000000) // NetBSD 5 and Solaris don't like pragma pack but accept the packed attribute
@@ -2589,6 +2600,61 @@ struct PACKET_ZC_SEARCH_STORE_INFO_ACK {
struct PACKET_ZC_SEARCH_STORE_INFO_ACK_sub items[];
} __attribute__((packed));
+/* Achievement System */
+struct ach_list_info {
+ uint32 ach_id;
+ uint8 completed;
+ uint32 objective[MAX_ACHIEVEMENT_OBJECTIVES];
+ uint32 completed_at;
+ uint8 reward;
+} __attribute__((packed));
+
+struct packet_achievement_list {
+ uint16 packet_id;
+ uint16 packet_len;
+ uint32 total_achievements;
+ uint32 total_points;
+ uint16 rank;
+ uint32 current_rank_points;
+ uint32 next_rank_points;
+ struct ach_list_info ach[MAX_ACHIEVEMENT_DB];
+} __attribute__((packed));
+
+struct packet_achievement_update {
+ uint16 packet_id;
+ uint32 total_points;
+ uint16 rank;
+ uint32 current_rank_points;
+ uint32 next_rank_points;
+ struct ach_list_info ach;
+} __attribute__((packed));
+
+struct packet_achievement_reward_ack {
+ uint16 packet_id;
+ uint8 received;
+ uint32 ach_id;
+} __attribute__((packed));
+
+// Name Packet ZC_ACK_REQNAME
+struct packet_reqname_ack {
+ uint16 packet_id;
+ int32 gid;
+ char name[NAME_LENGTH];
+} __attribute__((packed));
+
+// ZC_ACK_REQNAMEALL / ZC_ACK_REQNAMEALL2
+struct packet_reqnameall_ack {
+ uint16 packet_id;
+ int32 gid;
+ char name[NAME_LENGTH];
+ char party_name[NAME_LENGTH];
+ char guild_name[NAME_LENGTH];
+ char position_name[NAME_LENGTH];
+#if PACKETVER >= 20150503 // Confirm this?
+ int32 title_id; // Achievement Title
+#endif
+} __attribute__((packed));
+
#if !defined(sun) && (!defined(__NETBSD__) || __NetBSD_Version__ >= 600000000) // NetBSD 5 and Solaris don't like pragma pack but accept the packed attribute
#pragma pack(pop)
#endif // not NetBSD < 6 / Solaris
diff --git a/src/map/party.c b/src/map/party.c
index 9024b1c19..e4fb18c23 100644
--- a/src/map/party.c
+++ b/src/map/party.c
@@ -36,6 +36,7 @@
#include "map/pc.h"
#include "map/skill.h"
#include "map/status.h"
+#include "map/achievement.h"
#include "common/HPM.h"
#include "common/cbasetypes.h"
#include "common/memmgr.h"
@@ -189,6 +190,9 @@ static int party_create(struct map_session_data *sd, const char *name, int item,
party->fill_member(&leader, sd, 1);
intif->create_party(&leader,name,item,item2);
+
+ achievement->validate_party_create(sd); //Achievements (Smokexyz)
+
return 0;
}
diff --git a/src/map/pc.c b/src/map/pc.c
index d8a1c0b0e..01bae5e14 100644
--- a/src/map/pc.c
+++ b/src/map/pc.c
@@ -55,6 +55,7 @@
#include "map/skill.h"
#include "map/status.h" // struct status_data
#include "map/storage.h"
+#include "map/achievement.h"
#include "common/cbasetypes.h"
#include "common/conf.h"
#include "common/core.h" // get_svn_revision()
@@ -1053,6 +1054,11 @@ static bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data
pc->skill(p1_sd, WE_CALLBABY, 1, SKILL_GRANT_PERMANENT);
pc->skill(p2_sd, WE_CALLBABY, 1, SKILL_GRANT_PERMANENT);
+ // Achievements [Smokexyz/Hercules]
+ achievement->validate_adopt(p1_sd, true); // Parent 1
+ achievement->validate_adopt(p2_sd, true); // Parent 2
+ achievement->validate_adopt(b_sd, false); // Baby
+
return true;
}
@@ -1325,6 +1331,7 @@ static bool pc_authok(struct map_session_data *sd, int login_id2, time_t expirat
sd->bg_queue.type = 0;
VECTOR_INIT(sd->script_queues);
+ VECTOR_INIT(sd->achievement); // Achievements [Smokexyz/Hercules]
VECTOR_INIT(sd->storage.item); // initialize storage item vector.
VECTOR_INIT(sd->hatEffectId);
@@ -1384,6 +1391,7 @@ static bool pc_authok(struct map_session_data *sd, int login_id2, time_t expirat
" Group '"CL_WHITE"%d"CL_RESET"').\n",
sd->status.name, sd->status.account_id, sd->status.char_id,
CONVIP(ip), sd->group_id);
+
// Send friends list
clif->friendslist_send(sd);
@@ -1464,7 +1472,6 @@ static int pc_reg_received(struct map_session_data *sd)
nullpo_ret(sd);
sd->vars_ok = true;
-
sd->change_level_2nd = pc_readglobalreg(sd,script->add_str("jobchange_level"));
sd->change_level_3rd = pc_readglobalreg(sd,script->add_str("jobchange_level_3rd"));
sd->die_counter = pc_readglobalreg(sd,script->add_str("PC_DIE_COUNTER"));
@@ -1587,6 +1594,9 @@ static int pc_reg_received(struct map_session_data *sd)
if( npc->motd ) /* [Ind/Hercules] */
script->run(npc->motd->u.scr.script, 0, sd->bl.id, npc->fake_nd->bl.id);
+ // Achievements [Smokexyz/Hercules]
+ intif->achievements_request(sd);
+
return 1;
}
@@ -4494,6 +4504,8 @@ static int pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_typ
sd->status.zeny -= zeny;
clif->updatestatus(sd,SP_ZENY);
+ achievement->validate_zeny(sd, -zeny); // Achievements [Smokexyz/Hercules]
+
if(!tsd) tsd = sd;
logs->zeny(sd, type, tsd, -zeny);
if( zeny > 0 && sd->state.showzeny ) {
@@ -4630,6 +4642,8 @@ static int pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_typ
sd->status.zeny += zeny;
clif->updatestatus(sd,SP_ZENY);
+ achievement->validate_zeny(sd, zeny); // Achievements [Smokexyz/Hercules]
+
if(!tsd) tsd = sd;
logs->zeny(sd, type, tsd, zeny);
if( zeny > 0 && sd->state.showzeny ) {
@@ -4754,6 +4768,7 @@ static int pc_additem(struct map_session_data *sd, struct item *item_data, int a
sd->status.inventory[i].amount = amount;
sd->inventory_data[i] = data;
clif->additem(sd,i,amount,0);
+
}
if( ( !itemdb->isstackable2(data) || data->flag.force_serial || data->type == IT_CASH) && !item_data->unique_id )
@@ -4761,6 +4776,8 @@ static int pc_additem(struct map_session_data *sd, struct item *item_data, int a
logs->pick_pc(sd, log_type, amount, &sd->status.inventory[i],sd->inventory_data[i]);
+ achievement->validate_item_get(sd, sd->status.inventory[i].nameid, sd->status.inventory[i].amount); // Achievements [Smokexyz/Hercules]
+
sd->weight += w;
clif->updatestatus(sd,SP_WEIGHT);
//Auto-equip
@@ -6873,11 +6890,15 @@ static int pc_checkbaselevelup(struct map_session_data *sd)
clif->misceffect(&sd->bl,0);
npc->script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
- if(sd->status.party_id)
+ if (sd->status.party_id)
party->send_levelup(sd);
pc->baselevelchanged(sd);
+
quest->questinfo_refresh(sd);
+
+ achievement->validate_stats(sd, SP_BASELEVEL, sd->status.base_level);
+
return 1;
}
@@ -6940,7 +6961,11 @@ static int pc_checkjoblevelup(struct map_session_data *sd)
clif->status_change(&sd->bl,SI_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
npc->script_event(sd, NPCE_JOBLVUP);
+
quest->questinfo_refresh(sd);
+
+ achievement->validate_stats(sd, SP_BASELEVEL, sd->status.job_level);
+
return 1;
}
@@ -7237,6 +7262,8 @@ static int pc_setstat(struct map_session_data *sd, int type, int val)
return -1;
}
+ achievement->validate_stats(sd, type, val); // Achievements [Smokexyz/Hercules]
+
return val;
}
@@ -7968,6 +7995,14 @@ static void pc_damage(struct map_session_data *sd, struct block_list *src, unsig
if (battle_config.prevent_logout_trigger & PLT_DAMAGE)
sd->canlog_tick = timer->gettick();
+
+ // Achievements [Smokexyz/Hercules]
+ if (src != NULL) {
+ if (src->type == BL_PC)
+ achievement->validate_pc_damage(BL_UCAST(BL_PC, src), sd, hp);
+ else if (src->type == BL_MOB)
+ achievement->validate_mob_damage(sd, hp, true);
+ }
}
/*==========================================
@@ -8118,6 +8153,8 @@ static int pc_dead(struct map_session_data *sd, struct block_list *src)
pc->setparam(ssd, SP_KILLEDRID, sd->bl.id);
npc->script_event(ssd, NPCE_KILLPC);
+ achievement->validate_pc_kill(ssd, sd); // Achievements [Smokexyz/Hercules]
+
if (battle_config.pk_mode&2) {
ssd->status.manner -= 5;
if(ssd->status.manner < 0)
@@ -9036,6 +9073,8 @@ static int pc_jobchange(struct map_session_data *sd, int class, int upper)
}
quest->questinfo_refresh(sd);
+ achievement->validate_jobchange(sd); // Achievements [Smokexyz/Hercules]
+
return 0;
}
@@ -10676,6 +10715,11 @@ static int pc_marriage(struct map_session_data *sd, struct map_session_data *dst
return -1;
sd->status.partner_id = dstsd->status.char_id;
dstsd->status.partner_id = sd->status.char_id;
+
+ // Achievements [Smokexyz/Hercules]
+ achievement->validate_marry(sd);
+ achievement->validate_marry(dstsd);
+
return 0;
}
diff --git a/src/map/pc.h b/src/map/pc.h
index 0781fe801..17a9b8200 100644
--- a/src/map/pc.h
+++ b/src/map/pc.h
@@ -37,7 +37,7 @@
#include "common/db.h"
#include "common/ers.h" // struct eri
#include "common/hercules.h"
-#include "common/mmo.h" // JOB_*, MAX_FAME_LIST, struct fame_list, struct mmo_charstatus, NEW_CARTS
+#include "common/mmo.h" // JOB_*, MAX_FAME_LIST, struct fame_list, struct mmo_charstatus, NEW_CARTS, struct s_achievement
/**
* Defines
@@ -631,6 +631,12 @@ END_ZEROED_BLOCK;
unsigned sitstand : 1;
unsigned commands : 1;
} block_action;
+
+ /* Achievement System */
+ struct char_achievements achievement;
+ bool achievements_received;
+ // Title
+ VECTOR_DECL(int) title_ids;
};
#define EQP_WEAPON EQP_HAND_R
diff --git a/src/map/pet.c b/src/map/pet.c
index d1d8a5a33..cfb3e8253 100644
--- a/src/map/pet.c
+++ b/src/map/pet.c
@@ -23,6 +23,7 @@
#include "config/core.h" // DBPATH
#include "pet.h"
+#include "map/achievement.h"
#include "map/atcommand.h" // msg_txt()
#include "map/battle.h"
#include "map/chrif.h"
@@ -582,8 +583,8 @@ static int pet_catch_process2(struct map_session_data *sd, int target_id)
pet_catch_rate = (pet->db[i].capture + (sd->status.base_level - md->level)*30 + sd->battle_status.luk*20)*(200 - get_percentage(md->status.hp, md->status.max_hp))/100;
if(pet_catch_rate < 1) pet_catch_rate = 1;
- if(battle_config.pet_catch_rate != 100)
- pet_catch_rate = (pet_catch_rate*battle_config.pet_catch_rate)/100;
+ if(battle->bc->pet_catch_rate != 100)
+ pet_catch_rate = (pet_catch_rate*battle->bc->pet_catch_rate)/100;
if(rnd()%10000 < pet_catch_rate)
{
@@ -592,6 +593,8 @@ static int pet_catch_process2(struct map_session_data *sd, int target_id)
clif->pet_roulette(sd,1);
intif->create_pet(sd->status.account_id,sd->status.char_id,pet->db[i].class_,mob->db(pet->db[i].class_)->lv,
pet->db[i].EggID,0,pet->db[i].intimate,100,0,1,pet->db[i].jname);
+
+ achievement->validate_taming(sd, pet->db[i].class_);
}
else
{
diff --git a/src/map/script.c b/src/map/script.c
index 24d03bbe2..e38aae562 100644
--- a/src/map/script.c
+++ b/src/map/script.c
@@ -57,6 +57,7 @@
#include "map/status.h"
#include "map/storage.h"
#include "map/unit.h"
+#include "map/achievement.h"
#include "common/cbasetypes.h"
#include "common/conf.h"
#include "common/db.h"
@@ -9582,6 +9583,11 @@ static BUILDIN(successrefitem)
clif->additem(sd,i,1,0);
pc->equipitem(sd,i,ep);
clif->misceffect(&sd->bl,3);
+
+ achievement->validate_refine(sd, i, true); // Achievements [Smokexyz/Hercules]
+
+ /* The following check is exclusive to characters (possibly only whitesmiths)
+ * that create equipments and refine them to level 10. */
if(sd->status.inventory[i].refine == 10 &&
sd->status.inventory[i].card[0] == CARD0_FORGE &&
sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
@@ -9619,6 +9625,9 @@ static BUILDIN(failedrefitem)
if (num > 0 && num <= ARRAYLENGTH(script->equip))
i=pc->checkequip(sd,script->equip[num-1]);
if(i >= 0) {
+ // Call before changing refine to 0.
+ achievement->validate_refine(sd, i, false);
+
sd->status.inventory[i].refine = 0;
pc->unequipitem(sd, i, PCUNEQUIPITEM_RECALC|PCUNEQUIPITEM_FORCE); //recalculate bonus
clif->refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure
@@ -9666,6 +9675,9 @@ static BUILDIN(downrefitem)
clif->additem(sd,i,1,0);
pc->equipitem(sd,i,ep);
+
+ achievement->validate_refine(sd, i, false); // Achievements [Smokexyz/Hercules]
+
clif->misceffect(&sd->bl,2);
}
@@ -21254,6 +21266,78 @@ static BUILDIN(showevent)
}
/*==========================================
+ * Achievement System [Smokexyz/Hercules]
+ *-----------------------------------------*/
+/**
+ * Validates an objective index for the given achievement.
+ * Can be used for any achievement type.
+ * @command achievement_progress(<ach_id>,<obj_idx>,<progress>,<incremental?>{,<char_id>});
+ * @param aid - achievement ID
+ * @param obj_idx - achievement objective index.
+ * @param progress - objective progress towards goal.
+ * @Param incremental - (boolean) true to add the progress towards the goal,
+ * false to use the progress only as a comparand.
+ * @param account_id - (optional) character ID to perform on.
+ * @return true on success, false on failure.
+ * @push 1 on success, 0 on failure.
+ */
+static BUILDIN(achievement_progress)
+{
+ struct map_session_data *sd = script->rid2sd(st);
+ int aid = script_getnum(st, 2);
+ int obj_idx = script_getnum(st, 3);
+ int progress = script_getnum(st, 4);
+ int incremental = script_getnum(st, 5);
+ int account_id = script_hasdata(st, 6) ? script_getnum(st, 6) : 0;
+ const struct achievement_data *ad = NULL;
+
+ if ((ad = achievement->get(aid)) == NULL) {
+ ShowError("buildin_achievement_progress: Invalid achievement ID %d received.\n", aid);
+ script_pushint(st, 0);
+ return false;
+ }
+
+ if (obj_idx <= 0 || obj_idx > VECTOR_LENGTH(ad->objective)) {
+ ShowError("buildin_achievement_progress: Invalid objective index %d received. (min: %d, max: %d)\n", obj_idx, 0, VECTOR_LENGTH(ad->objective));
+ script_pushint(st, 0);
+ return false;
+ }
+
+ obj_idx--; // convert to array index.
+
+ if (progress <= 0 || progress > VECTOR_INDEX(ad->objective, obj_idx).goal) {
+ ShowError("buildin_achievement_progress: Progress exceeds goal limit for achievement id %d.\n", aid);
+ script_pushint(st, 0);
+ return false;
+ }
+
+ if (incremental < 0 || incremental > 1) {
+ ShowError("buildin_achievement_progress: Argument 4 expects boolean (0/1). provided value: %d\n", incremental);
+ script_pushint(st, 0);
+ return false;
+ }
+
+ if (script_hasdata(st, 6)) {
+ if (account_id <= 0) {
+ ShowError("buildin_achievement_progress: Invalid Account id %d provided.\n", account_id);
+ script_pushint(st, 0);
+ return false;
+ } else if ((sd = map->id2sd(account_id)) == NULL) {
+ ShowError("buildin_achievement_progress: Account with id %d was not found.\n", account_id);
+ script_pushint(st, 0);
+ return false;
+ }
+ }
+
+ if (achievement->validate(sd, aid, obj_idx, progress, incremental ? true : false))
+ script_pushint(st, progress);
+ else
+ script_pushint(st, 0);
+
+ return true;
+}
+
+/*==========================================
* BattleGround System
*------------------------------------------*/
static BUILDIN(waitingroom2bg)
@@ -25132,6 +25216,8 @@ static void script_parse_builtin(void)
BUILDIN_DEF(agitstart2,""),
BUILDIN_DEF(agitend2,""),
BUILDIN_DEF(agitcheck2,""),
+ // Achievements [Smokexyz/Hercules]
+ BUILDIN_DEF(achievement_progress, "iiii?"),
// BattleGround
BUILDIN_DEF(waitingroom2bg,"siiss?"),
BUILDIN_DEF(waitingroom2bg_single,"isiis"),
diff --git a/src/map/unit.c b/src/map/unit.c
index 2fd8d6efb..9174bdccd 100644
--- a/src/map/unit.c
+++ b/src/map/unit.c
@@ -2765,8 +2765,10 @@ static int unit_free(struct block_list *bl, clr_type clrtype)
sd->instance = NULL;
}
VECTOR_CLEAR(sd->script_queues);
+ VECTOR_CLEAR(sd->achievement); // Achievement [Smokexyz/Hercules]
VECTOR_CLEAR(sd->storage.item);
VECTOR_CLEAR(sd->hatEffectId);
+ VECTOR_CLEAR(sd->title_ids); // Title [Dastgir/Hercules]
sd->storage.received = false;
if( sd->quest_log != NULL ) {
aFree(sd->quest_log);