HP damage based on percentage

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
JayBlue
Regular
Posts: 86
Joined: Fri Aug 26, 2016 7:10 pm
Location: Space
Contact:

HP damage based on percentage

#1 Post by JayBlue »

I'm trying to work on a battle system where the attacks are based on percentage.



Example:
You choose a skill
Skill says it does +110% damage based on a character's STR stat.
STR stat is = 85

Calculation:
STRstat / 10 = 8.5
8.5 + STRstat = 93.5

The attack should do 93.5 damage.

Now my question is, how would the code for something like this look like?
And how would I round the number 93.5 up to 94?

Any help would be appreciated :)
If an Owl hoots in a forest, does it make a sound?

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2402
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: HP damage based on percentage

#2 Post by Ocelot »

Code: Select all

label do_some_calculations:
    $ skill_percentage = 110
    $ STRstat = 85

    $ damage = int(ceil( STRstat * percentage / 100 ))
< < insert Rick Cook quote here > >

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: HP damage based on percentage

#3 Post by xavimat »

I'm making a game with this kind of things and simply use:

Code: Select all

$ damage = STRstat * skill_percentage / 100
without the int() and ceil().
Ocelot: Is there a real difference?
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2402
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: HP damage based on percentage

#4 Post by Ocelot »

Well, OP did ask "And how would I round the number 93.5 up to 94". int() was a reflex, when I saw 94 and not 94.0

Now I noticed that choice of number left certain ambiguity in whether rounding up or proper arithmetic rounding is needed. If latter, then it should be 'round', not 'ceil'
< < insert Rick Cook quote here > >

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: HP damage based on percentage

#5 Post by xavimat »

Ocelot wrote:Well, OP did ask "And how would I round the number 93.5 up to 94". int() was a reflex, when I saw 94 and not 94.0
Yeah, that's correct :D :D
However, to use that functions "import math" is needed.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
JayBlue
Regular
Posts: 86
Joined: Fri Aug 26, 2016 7:10 pm
Location: Space
Contact:

Re: HP damage based on percentage

#6 Post by JayBlue »

Thank you guys, (Ocelot, xavimat) for answering.

Yes, i want it to work with proper arithmatic so that the numbers round up or down when they're supposed to.

This is what I'm currently trying to do.

Code: Select all

$ damage = int(round( STRstat * skill_percentage / 100))
The 93.5 isn't rounding up. I've tried changing the skill_percentage to other numbers and it always rounds down.

Also, what is the import math function? I'm assuming it's code that would help.
If an Owl hoots in a forest, does it make a sound?

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: HP damage based on percentage

#7 Post by xavimat »

- import math is only needed if you want to use "ceil" (that you don't).

- Somethings needed to know: In python, integers are always rounded down:

Code: Select all

85 / 10 = 8
19 / 10 = 1
20 / 10 = 2
- so, in your code, there are only integers, so the round() function does nothing, neither the int().

Code: Select all

str = 85
skill = 110

str * skill = 85 * 110 = 9350
9350 / 100 = 93
int(round(93)) <-- rounding an integer (not needed) and then converting that integer into an integer (not needed).
SOLUTIONS:
1. If you really, really, really want to use decimals, then tell python that one of the numbers is a float (only need one), then, all the calculations from that number will be floats:

Code: Select all

$ damage = str * skill / 100.0   <--- The period in "100.0" tells python that the number is a float (you could also use "100.")
85 * 110 / 100.0 = 93.5
round(93.5) = 94.0  <-- python adds the ".0" part to remember that is a float
int(94.0) = 94  <-- And you have now an integer again, rounded and pretty.
2. But, I would advise you to use simply integers and not to worry about the decimals lost. I don't know if in your gameplay it will be a great deal to do 93 of damage or to do 94, but in the game I'm making it simply doesn't matter, and the code is much, much simpler.
The problem comes only when using really low numbers (str of 3, skill of 101%), but, you can start at least with 40 or 50 in the lower levels. When leveling up the decimals will be less significant.

Also, the decimals lost happen when the player attacks and when the enemy attacks, so, both of them are treated the same way.
Look at the simple combat in the Dungeon Crawl (in the cookbook section), the decimals are ignored and there is no problem.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

Post Reply

Who is online

Users browsing this forum: Google [Bot], Yone28