Blog > Blog > Quick look (not really) at Battle.lua

Quick look (not really) at Battle.lua

Posted by Hamcha on August 27, 2011


So, Revision 9 introduced a new file: Battle.lua

This file manages the most important things about the combat system: Damage Calculation, how damage is dealt to platoons and how the A.I. behaves.

This is a request for help, actually, I would like to hear YOUR opinion on things, how would you do those calculations and why mine are wrong (or what I can do to improve them).

I'll explain everything after the break.

...


Damage Calculation

Damage Calculation is probably the easiest part so I'll be quick and I think that if you did something like that it'll probably be similar to how I did it.

function CalculateDamage (aATK,aDEX,dDEF,dDEX)
     Damage = aATK
     EnemyDef = 2*dDEF

     Damage = aATK
     CriticalPos = aDEX/5
     DodgePos = dDEX/10

     -- Base multiplier (None)
     Multiplier = 1

     if CriticalPos > math.random(100) then
          Multiplier = 1.5
     end

     if DodgePos > math.random(100) then
          Multiplier = 0
     end

     -- This is how the enemy DEF affects the whole thing
     -- This is also why I will kill you if you set DEF to 0.
     DefEffect = Damage/EnemyDef

     Damage = Damage * Multiplier * DefEffect
     return Damage
end

If you are a programmer, it's likely that you can understand most of it (if not everything) without having to read here, but I'll explain it anyway, but before, some info:

  • What is used to calculate the damage is the Attacker's ATK (Attack) and DEX (Dexterity) and the Defender's DEF (Defense) and DEX (Dexterity)
  • DEX is being used for Critical chances (Attacker's) and possible Dodging of attacks (Defender's)

That said, here's how damage is calculated:

  1. For starters, we assume that the Damage is the ATK of the attacker
  2. We then create two random numbers and test them against DEX values (both Attacker and Defender's) and see whether the attack does Critical Damage (Damage 1.5x) or does nothing because the Defender dodges it (Zero Damage)
  3. Then we create a new variable which is nothing more than ATK/DEF ratio and then multiply the Damage by it.

That's it, simple and straight-forward. Still, I'm not really happy how the Defender's DEF actually influences it, but I'll stick to it until I'll need to change it for balancing reasons.

Let's move to the next argument of this article.


Artificial Intelligence

This is the big deal or it could appear to be so.

There's really no big strategy in a J-RPG combat system, so it's not really complicated to do the A.I. code.
Still, I think that the actual code I wrote it's too simple and I'll appreciate every single suggestion about how could I improve it.

Here's the code:

function AI_Attack (_atkPow,_EnemyPlatoons,_pCount,_type)
     if _pCount < 1 then
          return 0
     end

     MaxAtk = Platoon.getStat(_EnemyPlatoons[0],"ATK",_type)
     MaxID = 0

     for i=1,_pCount-1 do
          TempAtk = Platoon.getStat(_EnemyPlatoons[i],"ATK",_type)
          if TempAtk > MaxAtk then
               MaxAtk = TempAtk
               MaxID = i
          end
     end

     return MaxID
end

So, what does it do?

As I said, it's really simple:

  1. Examine all possible targets 
  2. Attack the strongest one

Stop, it can't get simpler than that.

Brief explanation why I kept it so simple:
The attack of a platoon depends on how many units are alive, so three weak platoons do less damage than if they were two with full health.
So the most logical thing is to attack the strongest enemies, so they do less damage to you when it's their turn. 


Dealing damage to units

You're tired, aren't you?
Well, good news is that I won't post the code because it doesn't work yet.

So, problem is:
When someone attacks, it attacks a Platoon, not single units, so how I distribute this damage to units?

The method I used is pretty simple and it should work fine.
The explanation is pretty simple too, just like the other ones:

  • I select a random squadron in the attacked platoon
  • Then I select a random unit in the selected squadron
  • I take a random part of the damage I have to distribute and deal it to the chosen unit
  • If the unit dies, I take the damage that exceeds and sum it back to the Remaining Damage I still have to deal
    (if the Unit has 8 HP and I have to deal 10 of Damage, I take 2, that is 10-8)
  • Until the Remaining Damage is 0 (or until there's still one unit alive) I repeat the whole process.

About this, I still don't know if it will ever work, and I think I'll probably stick to a less random-based approach.


That's it, for now

So again, let me know your opinion about AI, about Damage Calculation... about everything!
I'll be happy to reply!

See you~

PS: Grammar Nazis are welcome, so I can blame my co-worker for being a bad Spellchecker.

Grammar Nazis are welcome

English is not my native language and I'm looking forward to improve so if you happen to find some mistakes please point them out!

Comments:

hvbksqh

J1FGMd , [url=http://mufxsphxmzet.com/]mufxsphxmzet[/url], [link=http://qgdngfgypfhq.com/]qgdngfgypfhq[/link], http://fzqddpagngbs.com/
dytiqjepikj

7rJck2 , [url=http://hkhhpxeulsgm.com/]hkhhpxeulsgm[/url], [link=http://abjfwwupnpki.com/]abjfwwupnpki[/link], http://jnhepdknfosc.com/
Leave a Reply



(Your email will not be publicly displayed.)


Captcha Code

Click the image to see another captcha.