Math in Video Games: Rounding, Probabilities, Averages, Et Cetera

Discussion in 'Gaming' started by Amaury, Mar 6, 2014.

  1. Amaury Legendary Hero

    Joined:
    Jan 15, 2007
    Gender:
    Male
    Location:
    Ellensburg, WA
    1,693
    When rounding, if the number is four or less, it stays the same; if the number is five or more, it's rounded up, but it seems games don't round up. Why is this?

    For example, this isn't my video, but if you watch from 7:32 to 7:47 here, you'll see that Marcello's "Pray to the Heavens" attack did an average of 130 points of damage. To find the average, we must first add up the damage done to each individual character, which, from left to right, was 136, 135, 142, and 110. The total damage dealt on the party was 523. Now, take that 523 and divide it by how many numbers there are -- in this case four. 523 / 4 = 130.75, and look at what we have: .75, which means we'd round up to 131. 130.75 > 130.8 > 131. So why are they saying the average was 130 points of damage?



    Another example is Arc the Lad: Twilight of the Spirits. Just using the magic side, there are three healing spells: Cure, which recovers 25% of the max HP to one person, Healing Rain, which recovers 50% of the max HP to everyone, and Vital Energy, which recovers 75% of the max HP to everyone.

    Using Delma, for example, an ally on the Deimos side, at level 20, she has a total of 237 HP. At 237 max HP, Cure heals her 59 HP, Healing Rain heals her 118 HP, and Vital Energy heals her 177 HP.

    To figure out how to get these numbers, you would do this, where n is the number we need to figure out:

    Math 1.png

    Math 2.png

    Going by the game's amounts, the number for Cure is right. However, the numbers for Healing Rain and Vital Energy are technically wrong. They're staying the same and rounding down instead of rounding up. Why?

    Just as an end note, it's thanks to the instructors I had when I was studying for my GED that I know all this math.
     
  2. Patman Bof

    Joined:
    Oct 19, 2010
    Gender:
    Male
    Location:
    France
    672
    It' d be a pain in the ass ?
    Not only would it be more time-consuming to write the code, it would also eat more storage space and add stuff for the CPU to compute. Not a big strain, but still, reducing the number of computations required to perform a task is a huge aspect of programing. It' s called code optimization. The simpler the better.
     
  3. Graxe King's Apprentice

    Joined:
    Dec 15, 2007
    69
    449
    To add, sometimes arithmetic operators in programming languages automatically and just take the integer/non-decimal value(s) for the output, hence the rounding down.
     
  4. Amaury Legendary Hero

    Joined:
    Jan 15, 2007
    Gender:
    Male
    Location:
    Ellensburg, WA
    1,693
    I think you made a typo. Automatically what?

    Although I understand the gist of it.
     
  5. Patman Bof

    Joined:
    Oct 19, 2010
    Gender:
    Male
    Location:
    France
    672
    He meant they do round, but always down. A 6.9 will be rounded down to a 6. Which is just as arbitrary as rounding up above .5 and down below .5

    Say Sora has 679 HP and equips a HP+10% accessory. The line of code will look something like :

    If accessory=HP+10 then : HPSora=HPSora+(HPSora/10)

    Sora' s HP would now be 679+67

    If you wanted to round things so that Sora would gain 68 HP instead of 67 you' d have to add several lines of codes to deal with it. But since it works just as fine that way why bother ...
     
  6. Amaury Legendary Hero

    Joined:
    Jan 15, 2007
    Gender:
    Male
    Location:
    Ellensburg, WA
    1,693
    So using Arc the Lad: Twilight of the Spirits again, there are a total of five healing abilities:

    Special Moves:
    • First Aid
      - Restores HP
    • Medical Machine
      - Restores HP thru the power of science

    Magic:
    • Cure
      - Restores 25% of an ally's HP
    • Healing Rain
      - Restores 50% of an ally's HP
    • Vital Energy
      - Restores 75% of an ally's HP

    We already know how Cure, Healing Rain, and Vital Energy work from my previous post, and the only reason I knew the percentages was because of their descriptions in-game, as seen above. However, as you can see, the descriptions for First Aid and Medical Machine are ambiguous, so we're missing information. With the healing magic, we already had all the information we needed: the number of HP restored (all you have to do is watch how much HP is restored in-game), the maximum HP, and the percentage of HP it restored. It was just a matter of figuring out how they got that number and putting the information together in the right order, something I wouldn't have known if it weren't for my awesome GED instructors. However, for First Aid and Medical Machine, we can't do that because we're missing the percentage.

    Medical Machine completely restores HP to everyone, so that doesn't really need any more explanation; however, First Aid restores a little more than Cure, but a little less than Healing Rain, but we don't know the percentage, so we need to figure that out. The math would be opposite of what was done to find the number of HP restored.

    Using a level 20 Delma with a total of 237 HP again, First Aid restores 82 HP to her. The math would look like this:
    Math 3.png

    I believe my math is right, but let me know. Anyway, going by the game's programming, however, it would actually be that First Aid restores 34% of the max HP to a single person, not 35%, correct? However, maybe not. Maybe it is 35%, because look: if I reversed that and did it like I was trying to find the number of HP restored, here are my answers using 34% and 35%:

    Math 4.png

    At 237 max HP, 82 HP is restored. However, if we say it's 34%, then it's saying that 80 HP is restored, which is wrong, so it would be 35% because it will restore 82 HP. Does rounding work differently when dealing with percentages?
     
    Last edited: Mar 6, 2014
  7. Patman Bof

    Joined:
    Oct 19, 2010
    Gender:
    Male
    Location:
    France
    672
    It' s the hardware that always rounds down. As for the exemple you mentionned, I could try and make and educated guess but I won' t bother.

    In the case of healing spells that don' t clearly state out loud a fixed percentage the algorithm used could be a lot of things. Could be a fixed number, could be a fixed percentage, could be either of those with modifiers applied (caster Lv, caster mag stat, target mag stat etc ...).

    The only way to know for sure would be to either make a lot of tests to retro-engineer the algorithm (count me out lol) or to look directly at the code.

    If you' re that curious about it check Gamefaqs, sometimes they do have files breaking down the algorithms used.
     
  8. Amaury Legendary Hero

    Joined:
    Jan 15, 2007
    Gender:
    Male
    Location:
    Ellensburg, WA
    1,693
    Now that I think about it, though, even though it's rounding up -- and maybe they do round up percentages -- it makes sense. Cure restores 25% of the max HP, Healing Rain restores 50% of the max HP, and Vital Energy restores 75% of the max HP, and notice how they end in two fives and a zero, so it'd make sense that it'd be 35% and not 34%.

    Uh, I believe it would always be the same.

    Maximum HP times the percentage divided by 100% = Number of HP restored. So, using Cure and Delma's maximum HP at level 20, as I posted before: 237 * 25 / 100 = HP restored

    I'll make a video later so you can see it in action. It might give you and others a better understanding, too.
     
  9. Patman Bof

    Joined:
    Oct 19, 2010
    Gender:
    Male
    Location:
    France
    672
    I was talking about the spells that don' t tell you how they work, like first aid. And I forgot to mention retro-engineering might be doomed to fail because some modifiers are invisible to the user. For instance the timer is often associated with the luck stat to randomize the end result.

    And even when they do tell you how they work, sometimes they lie (I' m looking at you, FF IV insane drop rates).
     
    Last edited: Mar 6, 2014
  10. Amaury Legendary Hero

    Joined:
    Jan 15, 2007
    Gender:
    Male
    Location:
    Ellensburg, WA
    1,693
    @Patman and anyone else that's been watching this thread, here is the video, which may simplify things a bit since you're not looking at a lot of math:

     
  11. Patman Bof

    Joined:
    Oct 19, 2010
    Gender:
    Male
    Location:
    France
    672
    What I was trying to explain to you is that guessing the general rule is trickier than you might think. Let' s say first aid did indeed restore 34% HP just this once, does it still restore 34% HP when you change the character stats or equipment ? Would it have restored 34% HP had you pressed the button a second later ? Here' s a short vid showing how trying to figure out a rule, even a simple one, can be trickier than you might think at first :

     
  12. Amaury Legendary Hero

    Joined:
    Jan 15, 2007
    Gender:
    Male
    Location:
    Ellensburg, WA
    1,693
    Yes, in this particular game, the amounts are fixed and will always be the same. With 237 max HP, for example, Cure will always heal 59 HPs.

    Now, since you brought up the subject of things being different, I'll change the topic of this thread and make it a general math in videos games thread.

    We'll be using Dragon Quest VIII: Journey of the Cursed King for this example. In this game, there are several healing abilities:

    Spells:
    • Heal
      - Restores at least 30 HPs to a single ally, but can restore up to 40 HPs
    • Midheal
      - Restores at least 75 HPs to a single ally, but can restore up to 100 HPs
    • Fullheal
      - Fully restores HPs to a single ally
    • Multiheal
      - Restores at least 100 HPs to the entire party, but can restore up to 120 HPs
    • Omniheal
      - Fully restores HPs to the entire party

    Abilities:
    • Hustle Dance
      - Restores at least 70 HPs to the entire party, but can restore up to 100 HPs

    These are my own descriptions, just to make a note.

    So my guess here is that we're working with probabilities, correct? Using Midheal, for example, with the game's programming, what determines whether it will heal 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, or 100 HPs?
     
  13. Patman Bof

    Joined:
    Oct 19, 2010
    Gender:
    Male
    Location:
    France
    672
    Probably something related to the timer. See, randomness is utterly impossible to program. Can' t do that. What we can do is write an algorithm whose entry variable changes very often (the timer is ideal for that since it changes a lot of times in a single second) and whose result will, in the case of hustle dance, always be somewhere between 0 and 30. In other words you can try to emulate randomness but you' ll never truly get it. Actually, some scientists wonder if there is even such a thing as randomness in nature, maybe there' s always a rule and we just fail to see it.

    To complicate things further, the timer might not be the only entry variable. As I said earlier luck is often included so that the higher your luck the higher the odds for you to get a high result.