Jelajahi Sumber

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://svn.code.sf.net/p/rathena/svn/trunk@12680 54d463be-8e91-2dee-dedb-b68131a5f0ec

ultramage 17 tahun lalu
induk
melakukan
6559c24882
7 mengubah file dengan 32 tambahan dan 23 penghapusan
  1. 15 7
      src/common/utils.c
  2. 2 2
      src/map/clif.c
  3. 2 1
      src/map/mercenary.c
  4. 6 6
      src/map/mob.c
  5. 1 1
      src/map/pc.c
  6. 5 5
      src/map/pet.c
  7. 1 1
      src/map/skill.c

+ 15 - 7
src/common/utils.c

@@ -11,6 +11,7 @@
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
+#include <math.h> // floor()
 
 #ifdef WIN32
 	#include <windows.h>
@@ -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);
-}
+	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);
+}

+ 2 - 2
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:

+ 2 - 1
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.

+ 6 - 6
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(;k<amount;k++) {
 		short x,y;
@@ -2927,7 +2927,7 @@ int mob_getfriendhprate_sub(struct block_list *bl,va_list ap)
 	if (battle_check_target(&md->bl,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

+ 1 - 1
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;
 	}
 

+ 5 - 5
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);

+ 1 - 1
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);