From b1508637e414820d615888a11baed170392154d7 Mon Sep 17 00:00:00 2001 From: ultramage Date: Thu, 1 May 2008 13:56:24 +0000 Subject: Replaced the integers+checking approach in r12679 with usage of floating point arithmetic. Applied search&replace to use the new name of the function. git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@12680 54d463be-8e91-2dee-dedb-b68131a5f0ec --- src/common/utils.c | 22 +++++++++++++++------- src/map/clif.c | 4 ++-- src/map/mercenary.c | 3 ++- src/map/mob.c | 12 ++++++------ src/map/pc.c | 2 +- src/map/pet.c | 10 +++++----- src/map/skill.c | 2 +- 7 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/common/utils.c b/src/common/utils.c index a9ce1389f..1e77f461a 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -11,6 +11,7 @@ #include #include #include +#include // floor() #ifdef WIN32 #include @@ -224,16 +225,23 @@ uint32 MakeDWord(uint16 word0, uint16 word1) /// calculates the value of A / B, in percent (rounded down) -unsigned int percent(const unsigned int A, const unsigned int B) +unsigned int get_percentage(const unsigned int A, const unsigned int B) { + double result; + if( B == 0 ) { - ShowError("percent(): divison by zero!\n"); + ShowError("get_percentage(): divison by zero! (A=%u,B=%u)\n", A, B); return -1; } - if( A < UINT_MAX/100 ) - return 100*A/B; - else - return A/(B/100); -} \ No newline at end of file + result = 100 * ((double)A / (double)B); + + if( result > UINT_MAX ) + { + ShowError("get_percentage(): result percentage too high! (A=%u,B=%u,result=%g)", A, B, result); + return UINT_MAX; + } + + return (unsigned int)floor(result); +} diff --git a/src/map/clif.c b/src/map/clif.c index 6d902f14d..9e197a945 100644 --- a/src/map/clif.c +++ b/src/map/clif.c @@ -7131,7 +7131,7 @@ int clif_charnameack (int fd, struct block_list *bl) if (battle_config.show_mob_info&1) str_p += sprintf(str_p, "HP: %u/%u | ", md->status.hp, md->status.max_hp); if (battle_config.show_mob_info&2) - str_p += sprintf(str_p, "HP: %d%% | ", percent(md->status.hp, md->status.max_hp)); + str_p += sprintf(str_p, "HP: %d%% | ", get_percentage(md->status.hp, md->status.max_hp)); //Even thought mobhp ain't a name, we send it as one so the client //can parse it. [Skotlex] if (str_p != mobhp) { @@ -8184,7 +8184,7 @@ void clif_parse_GlobalMessage(int fd, struct map_session_data* sd) { unsigned int next = pc_nextbaseexp(sd); if( next == 0 ) next = pc_thisbaseexp(sd); - if( percent(sd->status.base_exp, next)% 10 == 0 ) // 0%, 10%, 20%, ... + if( get_percentage(sd->status.base_exp, next)% 10 == 0 ) // 0%, 10%, 20%, ... { switch (sd->state.snovice_call_flag) { case 0: diff --git a/src/map/mercenary.c b/src/map/mercenary.c index 0d5fb328d..05dcfa4c7 100644 --- a/src/map/mercenary.c +++ b/src/map/mercenary.c @@ -8,6 +8,7 @@ #include "../common/nullpo.h" #include "../common/mmo.h" #include "../common/showmsg.h" +#include "../common/utils.h" #include "log.h" #include "clif.h" @@ -95,7 +96,7 @@ int merc_hom_vaporize(struct map_session_data *sd, int flag) if (status_isdead(&hd->bl)) return 0; //Can't vaporize a dead homun. - if (flag && percent(hd->battle_status.hp, hd->battle_status.max_hp) < 80) + if (flag && get_percentage(hd->battle_status.hp, hd->battle_status.max_hp) < 80) return 0; hd->regen.state.block = 3; //Block regen while vaporized. diff --git a/src/map/mob.c b/src/map/mob.c index 7278ea043..69db21af3 100644 --- a/src/map/mob.c +++ b/src/map/mob.c @@ -2688,7 +2688,7 @@ int mob_class_change (struct mob_data *md, int class_) if (md->class_ == class_) return 0; //Nothing to change. - hp_rate = percent(md->status.hp, md->status.max_hp); + hp_rate = get_percentage(md->status.hp, md->status.max_hp); md->class_ = class_; md->db = mob_db(class_); if (battle_config.override_mob_names==1) @@ -2823,7 +2823,7 @@ int mob_summonslave(struct mob_data *md2,int *value,int amount,int skill_id) if (!battle_config.monster_class_change_recover && (skill_id == NPC_TRANSFORMATION || skill_id == NPC_METAMORPHOSIS)) - hp_rate = percent(md2->status.hp, md2->status.max_hp); + hp_rate = get_percentage(md2->status.hp, md2->status.max_hp); for(;kbl,bl,BCT_ENEMY)>0) return 0; - rate = percent(status_get_hp(bl), status_get_max_hp(bl)); + rate = get_percentage(status_get_hp(bl), status_get_max_hp(bl)); if (rate >= min_rate && rate <= max_rate) (*fr) = bl; @@ -2954,7 +2954,7 @@ struct block_list *mob_getmasterhpltmaxrate(struct mob_data *md,int rate) if( md && md->master_id > 0 ) { struct block_list *bl = map_id2bl(md->master_id); - if( bl && percent(status_get_hp(bl), status_get_max_hp(bl)) < rate ) + if( bl && get_percentage(status_get_hp(bl), status_get_max_hp(bl)) < rate ) return bl; } @@ -3060,11 +3060,11 @@ int mobskill_use(struct mob_data *md, unsigned int tick, int event) case MSC_ALWAYS: flag = 1; break; case MSC_MYHPLTMAXRATE: // HP< maxhp% - flag = percent(md->status.hp, md->status.max_hp); + flag = get_percentage(md->status.hp, md->status.max_hp); flag = (flag <= c2); break; case MSC_MYHPINRATE: - flag = percent(md->status.hp, md->status.max_hp); + flag = get_percentage(md->status.hp, md->status.max_hp); flag = (flag >= c2 && flag <= ms[i].val[0]); break; case MSC_MYSTATUSON: // status[num] on diff --git a/src/map/pc.c b/src/map/pc.c index a5cea128d..04b1ea0ef 100644 --- a/src/map/pc.c +++ b/src/map/pc.c @@ -5084,7 +5084,7 @@ int pc_dead(struct map_session_data *sd,struct block_list *src) { unsigned int next = pc_nextbaseexp(sd); if( next == 0 ) next = pc_thisbaseexp(sd); - if( percent(sd->status.base_exp,next) >= 99 && !map_flag_gvg(sd->bl.m) ) + if( get_percentage(sd->status.base_exp,next) >= 99 && !map_flag_gvg(sd->bl.m) ) sd->state.snovice_dead_flag = 1; } diff --git a/src/map/pet.c b/src/map/pet.c index d06155b54..b8a61e093 100644 --- a/src/map/pet.c +++ b/src/map/pet.c @@ -554,7 +554,7 @@ int pet_catch_process2(struct map_session_data* sd, int target_id) return 1; } - pet_catch_rate = (pet_db[i].capture + (sd->status.base_level - md->level)*30 + sd->battle_status.luk*20)*(200 - percent(md->status.hp, md->status.max_hp))/100; + 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) @@ -1162,8 +1162,8 @@ int pet_heal_timer(int tid, unsigned int tick, int id, intptr data) status = status_get_status_data(&sd->bl); if(pc_isdead(sd) || - (rate = percent(status->sp, status->max_sp)) > pd->s_skill->sp || - (rate = percent(status->hp, status->max_hp)) > pd->s_skill->hp || + (rate = get_percentage(status->sp, status->max_sp)) > pd->s_skill->sp || + (rate = get_percentage(status->hp, status->max_hp)) > pd->s_skill->hp || (rate = (pd->ud.skilltimer != -1)) //Another skill is in effect ) { //Wait (how long? 1 sec for every 10% of remaining) pd->s_skill->timer=add_timer(gettick()+(rate>10?rate:10)*100,pet_heal_timer,sd->bl.id,0); @@ -1205,8 +1205,8 @@ int pet_skill_support_timer(int tid, unsigned int tick, int id, intptr data) } if(pc_isdead(sd) || - (rate = percent(status->sp, status->max_sp)) > pd->s_skill->sp || - (rate = percent(status->hp, status->max_hp)) > pd->s_skill->hp || + (rate = get_percentage(status->sp, status->max_sp)) > pd->s_skill->sp || + (rate = get_percentage(status->hp, status->max_hp)) > pd->s_skill->hp || (rate = (pd->ud.skilltimer != -1)) //Another skill is in effect ) { //Wait (how long? 1 sec for every 10% of remaining) pd->s_skill->timer=add_timer(tick+(rate>10?rate:10)*100,pet_skill_support_timer,sd->bl.id,0); diff --git a/src/map/skill.c b/src/map/skill.c index f8f50e3aa..7222f618e 100644 --- a/src/map/skill.c +++ b/src/map/skill.c @@ -7582,7 +7582,7 @@ int skill_check_condition(struct map_session_data* sd, short skill, short lv, in itemid[i] = skill_db[j].itemid[i]; amount[i] = skill_db[j].amount[i]; } - if(mhp > 0 && percent(status->hp, status->max_hp) > mhp) { + if(mhp > 0 && get_percentage(status->hp, status->max_hp) > mhp) { //mhp is the max-hp-requirement, that is, //you must have this % or less of HP to cast it. clif_skill_fail(sd,skill,2,0); -- cgit v1.2.3-70-g09d2