From d431f126db1496d8db791fc7427eaec487852319 Mon Sep 17 00:00:00 2001 From: "Guilherme G. Menaldo" Date: Thu, 17 Aug 2023 23:44:39 -0300 Subject: [PATCH] refactor SA_AUTOSPELL selected skill processing - replace if chain with array lookup/config file - renamed skill_autospell to skill_autospell_spell_selected --- src/map/clif.c | 2 +- src/map/skill.c | 59 +++++++++---------- src/map/skill.h | 2 +- src/plugins/HPMHooking/HPMHooking.Defs.inc | 4 +- .../HPMHooking_map.HPMHooksCore.inc | 8 +-- .../HPMHooking_map.HookingPoints.inc | 2 +- .../HPMHooking/HPMHooking_map.Hooks.inc | 16 ++--- 7 files changed, 46 insertions(+), 47 deletions(-) diff --git a/src/map/clif.c b/src/map/clif.c index b1c845baaef..b5f39b82a02 100644 --- a/src/map/clif.c +++ b/src/map/clif.c @@ -14187,7 +14187,7 @@ static void clif_parse_AutoSpell(int fd, struct map_session_data *sd) if( !skill_id ) return; - skill->autospell(sd, skill_id); + skill->autospell_spell_selected(sd, skill_id); clif_menuskill_clear(sd); } diff --git a/src/map/skill.c b/src/map/skill.c index 9e78e4bcf5d..cdf2d7026ec 100644 --- a/src/map/skill.c +++ b/src/map/skill.c @@ -17873,42 +17873,41 @@ static void skill_autospell_select_spell(struct block_list *bl, int skill_lv) skill->get_time(SA_AUTOSPELL, skill_lv), SA_AUTOSPELL); } -static int skill_autospell(struct map_session_data *sd, uint16 skill_id) +/** + * Initiates AutoSpell effect on player based on the skill they chose. + * + * // @FIXME: Why this always returns 0? Does it even make sense? + * @param sd player casting the skill + * @param skill_id selected skill + * @returns always returns 0 + */ +static int skill_autospell_spell_selected(struct map_session_data *sd, uint16 skill_id) { - uint16 skill_lv; - int maxlv=1,lv; - nullpo_ret(sd); - skill_lv = sd->menuskill_val; - lv=pc->checkskill(sd,skill_id); + uint16 autospell_lv = sd->menuskill_val; + int skill_lv = pc->checkskill(sd, skill_id); - if(!skill_lv || !lv) return 0; // Player must learn the skill before doing auto-spell [Lance] + if(autospell_lv == 0 || skill_lv == 0) + return 0; // Player must learn the skill before doing auto-spell [Lance] - if(skill_id==MG_NAPALMBEAT) maxlv=3; - else if(skill_id==MG_COLDBOLT || skill_id==MG_FIREBOLT || skill_id==MG_LIGHTNINGBOLT){ - if (sd->sc.data[SC_SOULLINK] && sd->sc.data[SC_SOULLINK]->val2 == SL_SAGE) - maxlv =10; //Soul Linker bonus. [Skotlex] - else if(skill_lv==2) maxlv=1; - else if(skill_lv==3) maxlv=2; - else if(skill_lv>=4) maxlv=3; - } - else if(skill_id==MG_SOULSTRIKE){ - if(skill_lv==5) maxlv=1; - else if(skill_lv==6) maxlv=2; - else if(skill_lv>=7) maxlv=3; - } - else if(skill_id==MG_FIREBALL){ - if(skill_lv==8) maxlv=1; - else if(skill_lv>=9) maxlv=2; - } - else if(skill_id==MG_FROSTDIVER) maxlv=1; - else return 0; + int skill_idx; + ARR_FIND(0, MAX_AUTOSPELL_DB, skill_idx, skill->dbs->autospell_db[skill_idx].skill_id == skill_id); + if (skill_idx == MAX_AUTOSPELL_DB) + return 0; // Not an AutoSpell skill (exploit attempt?) + + const struct s_autospell_db *sk = &skill->dbs->autospell_db[skill_idx]; + if (sk->autospell_level > autospell_lv) + return 0; // Don't have enough level to use + + int max_lv = sk->skill_lv[autospell_lv - 1]; + if (sk->spirit_boost && sd->sc.data[SC_SOULLINK] != NULL && sd->sc.data[SC_SOULLINK]->val2 == SL_SAGE) + max_lv = skill->dbs->db[skill->get_index(skill_id)].max; // Soul Linker bonus. [Skotlex] - if(maxlv > lv) - maxlv = lv; + if (max_lv > skill_lv) + max_lv = skill_lv; - sc_start4(&sd->bl,&sd->bl,SC_AUTOSPELL,100,skill_lv,skill_id,maxlv,0, + sc_start4(&sd->bl, &sd->bl, SC_AUTOSPELL, 100, skill_lv, skill_id, max_lv, 0, skill->get_time(SA_AUTOSPELL, skill_lv), SA_AUTOSPELL); return 0; } @@ -25281,7 +25280,7 @@ void skill_defaults(void) skill->weaponrefine = skill_weaponrefine; skill->autospell_select_spell = skill_autospell_select_spell; skill->autospell_select_spell_pc = skill_autospell_select_spell_pc; - skill->autospell = skill_autospell; + skill->autospell_spell_selected = skill_autospell_spell_selected; skill->calc_heal = skill_calc_heal; skill->check_cloaking = skill_check_cloaking; skill->check_cloaking_end = skill_check_cloaking_end; diff --git a/src/map/skill.h b/src/map/skill.h index a61f12feed6..5002617d1b1 100644 --- a/src/map/skill.h +++ b/src/map/skill.h @@ -2170,7 +2170,7 @@ struct skill_interface { void (*weaponrefine) (struct map_session_data *sd,int idx); void (*autospell_select_spell) (struct block_list *bl, int skill_lv); void (*autospell_select_spell_pc) (struct map_session_data *sd, int skill_lv); - int (*autospell) (struct map_session_data *md,uint16 skill_id); + int (*autospell_spell_selected) (struct map_session_data *md, uint16 skill_id); int (*calc_heal) (struct block_list *src, struct block_list *target, uint16 skill_id, uint16 skill_lv, bool heal); bool (*check_cloaking) (struct block_list *bl, struct status_change_entry *sce); int (*check_cloaking_end) (struct block_list *bl, va_list ap); diff --git a/src/plugins/HPMHooking/HPMHooking.Defs.inc b/src/plugins/HPMHooking/HPMHooking.Defs.inc index 9ed280799b1..6952773ff29 100644 --- a/src/plugins/HPMHooking/HPMHooking.Defs.inc +++ b/src/plugins/HPMHooking/HPMHooking.Defs.inc @@ -8470,8 +8470,8 @@ typedef void (*HPMHOOK_pre_skill_autospell_select_spell) (struct block_list **bl typedef void (*HPMHOOK_post_skill_autospell_select_spell) (struct block_list *bl, int skill_lv); typedef void (*HPMHOOK_pre_skill_autospell_select_spell_pc) (struct map_session_data **sd, int *skill_lv); typedef void (*HPMHOOK_post_skill_autospell_select_spell_pc) (struct map_session_data *sd, int skill_lv); -typedef int (*HPMHOOK_pre_skill_autospell) (struct map_session_data **md, uint16 *skill_id); -typedef int (*HPMHOOK_post_skill_autospell) (int retVal___, struct map_session_data *md, uint16 skill_id); +typedef int (*HPMHOOK_pre_skill_autospell_spell_selected) (struct map_session_data **md, uint16 *skill_id); +typedef int (*HPMHOOK_post_skill_autospell_spell_selected) (int retVal___, struct map_session_data *md, uint16 skill_id); typedef int (*HPMHOOK_pre_skill_calc_heal) (struct block_list **src, struct block_list **target, uint16 *skill_id, uint16 *skill_lv, bool *heal); typedef int (*HPMHOOK_post_skill_calc_heal) (int retVal___, struct block_list *src, struct block_list *target, uint16 skill_id, uint16 skill_lv, bool heal); typedef bool (*HPMHOOK_pre_skill_check_cloaking) (struct block_list **bl, struct status_change_entry **sce); diff --git a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc index 64aac6c2442..82f18257dad 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc @@ -6394,8 +6394,8 @@ struct { struct HPMHookPoint *HP_skill_autospell_select_spell_post; struct HPMHookPoint *HP_skill_autospell_select_spell_pc_pre; struct HPMHookPoint *HP_skill_autospell_select_spell_pc_post; - struct HPMHookPoint *HP_skill_autospell_pre; - struct HPMHookPoint *HP_skill_autospell_post; + struct HPMHookPoint *HP_skill_autospell_spell_selected_pre; + struct HPMHookPoint *HP_skill_autospell_spell_selected_post; struct HPMHookPoint *HP_skill_calc_heal_pre; struct HPMHookPoint *HP_skill_calc_heal_post; struct HPMHookPoint *HP_skill_check_cloaking_pre; @@ -13939,8 +13939,8 @@ struct { int HP_skill_autospell_select_spell_post; int HP_skill_autospell_select_spell_pc_pre; int HP_skill_autospell_select_spell_pc_post; - int HP_skill_autospell_pre; - int HP_skill_autospell_post; + int HP_skill_autospell_spell_selected_pre; + int HP_skill_autospell_spell_selected_post; int HP_skill_calc_heal_pre; int HP_skill_calc_heal_post; int HP_skill_check_cloaking_pre; diff --git a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc index f0f89781ee6..306c75604ef 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc @@ -3272,7 +3272,7 @@ struct HookingPointData HookingPoints[] = { { HP_POP(skill->weaponrefine, HP_skill_weaponrefine) }, { HP_POP(skill->autospell_select_spell, HP_skill_autospell_select_spell) }, { HP_POP(skill->autospell_select_spell_pc, HP_skill_autospell_select_spell_pc) }, - { HP_POP(skill->autospell, HP_skill_autospell) }, + { HP_POP(skill->autospell_spell_selected, HP_skill_autospell_spell_selected) }, { HP_POP(skill->calc_heal, HP_skill_calc_heal) }, { HP_POP(skill->check_cloaking, HP_skill_check_cloaking) }, { HP_POP(skill->check_cloaking_end, HP_skill_check_cloaking_end) }, diff --git a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc index f210b4fa2fc..085f974c27c 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc @@ -85308,14 +85308,14 @@ void HP_skill_autospell_select_spell_pc(struct map_session_data *sd, int skill_l } return; } -int HP_skill_autospell(struct map_session_data *md, uint16 skill_id) { +int HP_skill_autospell_spell_selected(struct map_session_data *md, uint16 skill_id) { int hIndex = 0; int retVal___ = 0; - if (HPMHooks.count.HP_skill_autospell_pre > 0) { + if (HPMHooks.count.HP_skill_autospell_spell_selected_pre > 0) { int (*preHookFunc) (struct map_session_data **md, uint16 *skill_id); *HPMforce_return = false; - for (hIndex = 0; hIndex < HPMHooks.count.HP_skill_autospell_pre; hIndex++) { - preHookFunc = HPMHooks.list.HP_skill_autospell_pre[hIndex].func; + for (hIndex = 0; hIndex < HPMHooks.count.HP_skill_autospell_spell_selected_pre; hIndex++) { + preHookFunc = HPMHooks.list.HP_skill_autospell_spell_selected_pre[hIndex].func; retVal___ = preHookFunc(&md, &skill_id); } if (*HPMforce_return) { @@ -85324,12 +85324,12 @@ int HP_skill_autospell(struct map_session_data *md, uint16 skill_id) { } } { - retVal___ = HPMHooks.source.skill.autospell(md, skill_id); + retVal___ = HPMHooks.source.skill.autospell_spell_selected(md, skill_id); } - if (HPMHooks.count.HP_skill_autospell_post > 0) { + if (HPMHooks.count.HP_skill_autospell_spell_selected_post > 0) { int (*postHookFunc) (int retVal___, struct map_session_data *md, uint16 skill_id); - for (hIndex = 0; hIndex < HPMHooks.count.HP_skill_autospell_post; hIndex++) { - postHookFunc = HPMHooks.list.HP_skill_autospell_post[hIndex].func; + for (hIndex = 0; hIndex < HPMHooks.count.HP_skill_autospell_spell_selected_post; hIndex++) { + postHookFunc = HPMHooks.list.HP_skill_autospell_spell_selected_post[hIndex].func; retVal___ = postHookFunc(retVal___, md, skill_id); } }