Follow us on...
Follow us on Twitter Follow us on Facebook
Register

View RSS Feed

ThomasMosey

Battle System Algorithm

Rate this Entry
Hey there,

In this semi-tutorial, I will be teaching you how Maths CAN be useful in programming (Although, it is majorly important in programming anyway!) by teaching you how to write simple algorithms that can create a Fighting System for games (This can also be used in other programming languages if changed, as the Maths will always be the same)

I have recently been developing my PHP, text-based Game, Legend of Kyden. I started it quite some time ago, but lost the project when I started developing different things. And recently picked it up again and made it 100 times better than it was, but in doing so, the parts that were already complete became*obsolete and old, so I thought I would give them a recode. One of these was the PvP Fighting system, which I wanted to be random, not just, for example "My Defence is 1 more than your Attack, so you'll never hit me!". So that lead me to finding other ways to do it.

I will not be posting this tutorial in any specific programming language, but just as*pseudo code so it can be easily integrated in to your own programming language. This will also not be a complete tutorial, I will just be showing you how to calculate different things to make the fight more fair.

So, on with the tutorial!

Step 1. Finding the specific skills to use.

For the purpose of this Tutorial, lets assume that we have 4 different skills that we want to use. These are:
  • Strength
  • Defence
  • Agility
  • Accuracy

You can find these however you want, for example, fetching them from a database or file. I will be assigning them values in the code, to avoid any confusion. So lets start off with this:
Code:
PLAYER_STR = 5;
PLAYER_DEF = 5;
PLAYER_AGI = 5;
PLAYER_ACC = 5;
As you can see, I have assigned each of the skills the number 5. Next, we will want to find and assign the Opponents Skills. Making this:
Code:
PLAYER_STR = 5;
PLAYER_DEF = 5;
PLAYER_AGI = 5;
PLAYER_ACC = 5;

OP_STR = 5;
OP_DEF = 5;
OP_AGI = 5;
OP_ACC = 5;
Again, these will just be the number 5 for me.

Step 2. Can you throw a punch, or can't you?

The next step we are going to take is calculating, using the skills provided, whether or not the Player has a chance to hit the Opponent. I'm going to try and keep this as simple as possible, so here's the code we are going to use:
Code:
PLAYER_HIT_CHANCE = (PLAYER_ACC / OP_AGI) * 100
if PLAYER_HIT_CHANCE < 0
 	PLAYER_HIT_CHANCE = 0
else if PLAYER_HIT_CHANCE > 100
	PLAYER_HIT_CHANCE = 100
else
	PLAYER_HIT_CHANCE = round_down(PLAYER_HIT_CHANCE)
Now, let me explain what each of these parts is doing.
Code:
PLAYER_HIT_CHANCE = (PLAYER_ACC / OP_AGI) * 100
This piece of code is taking the Player's Accuracy, and then dividing it by the Opponent's Agility, and then multiplies it by 100. This gets the percentage chance that the Player can actually hit the Opponent, before we even calculate how much the Player can hit by.
Code:
if PLAYER_HIT_CHANCE < 0
	PLAYER_HIT_CHANCE = 0
This code is to clean things up a bit, so if the Player has less than a 0% chance of hitting the Opponent, just assign it to the value of 0.
Code:
else if PLAYER_HIT_CHANCE > 100
	PLAYER_HIT_CHANCE = 100
This code is pretty much the same as above, but the other way round, so that if the Player has over a 100% chance of hitting the Opponent, we will just assign it as 100.
Code:
else
	PLAYER_HIT_CHANCE = round_down(PLAYER_HIT_CHANCE)
Now here, we are saying that if the PLAYER_HIT_CHANCE is not 0% or 100%, we will round it down to stop any confusion or the numbers getting too complicated further on. You can round this up if you like, but I like to round it down.

Now that we have the percentage chance that the Player will be able to hit the Opponent, we want to use that to calculate whether or not the Player WILL actually hit the Opponent. There are plenty of ways to do this, but to keep it clean and simple, I'm going to be using the following code:

Code:
PLAYER_HIT_RANDOM = random(0, 100)
if PLAYER_HIT_RANDOM <= PLAYER_HIT_CHANCE
	PLAYER_HIT = TRUE
else
	PLAYER_HIT = FALSE
Now to explain the code:

Code:
PLAYER_HIT_RANDOM = random(0, 100)
This is just getting a random number from the given range, and as we are using percentages, we will be using the range 0 - 100.

Code:
if PLAYER_HIT_RANDOM <= PLAYER_HIT_CHANCE
	PLAYER_HIT = TRUE
Now we are using the variable from the previous code we made. Lets say the random number generated is 72, and the Player's successful hit percentage is 75, this would assign the variable PLAYER_HIT as TRUE, meaning the player can successfully hit the Opponent.

Code:
else
	PLAYER_HIT = FALSE
And this is just saying if the above IF statement didn't meet our requirements, do this, which assigns PLAYER_HIT as FALSE, so the player did not successfully hit the Opponent.

Now that we have found out whether or not the Player can hit the Opponent or not, we need to do the same for the Opponent. I will just compile all of the code we currently have in the tutorial, and the Opponents code for this step, below, as I have no need to explain it as it's the same as for the Player.

Code:
PLAYER_STR = 5;
PLAYER_DEF = 5;
PLAYER_AGI = 5;
PLAYER_ACC = 5;

OP_STR = 5;
OP_DEF = 5;
OP_AGI = 5;
OP_ACC = 5;

PLAYER_HIT_CHANCE = (PLAYER_ACC / OP_AGI) * 100
if PLAYER_HIT_CHANCE < 0
 	PLAYER_HIT_CHANCE = 0
else if PLAYER_HIT_CHANCE > 100
	PLAYER_HIT_CHANCE = 100
else
	PLAYER_HIT_CHANCE = round_down(PLAYER_HIT_CHANCE)

PLAYER_HIT_RANDOM = random(0, 100)
if PLAYER_HIT_RANDOM <= PLAYER_HIT_CHANCE
	PLAYER_HIT = TRUE
else
	PLAYER_HIT = FALSE

OP_HIT_CHANCE = (OP_ACC / PLAYER_AGI) * 100
if OP_HIT_CHANCE < 0
 	OP_HIT_CHANCE = 0
else if OP_HIT_CHANCE > 100
	OP_HIT_CHANCE = 100
else
	OP_HIT_CHANCE = round_down(OP_HIT_CHANCE)

OP_HIT_RANDOM = random(0, 100)
if OP_HIT_RANDOM <= OP_HIT_CHANCE
	OP_HIT = TRUE
else
	OP_HIT = FALSE
And that's it for this step of the tutorial. On to the next!

Step 3. Meet my Metal!

In this Step of the Tutorial, we will be looking at the amount the Player/Opponent can attack by, depending on both their own Strength and the Enemies Defence. This is generally quite easy, and won't be as long as the last step.

First of all, lets find the amount the Player will initially hit the Opponent for:

Code:
POINTS = 0
if PLAYER_HIT == TRUE
	POINTS = random(0, PLAYER_STR)
I have no need to break down this part of the code, as it is small enough for me to explain. First of all, we set the initial attack points, just in case the Player is to miss the attack leading the next IF statement to not be executed. Then, we find out whether or not the Player missed the Attack using the code in the last step, and if the Player is to hit the Opponent, we set the variable POINTS to a random number between the range 0 - PLAYER_STR, this way it will be a nice and more equal and random game throughout.

Now that we have that sorted, we want to bring the Opponents Defence in to effect. We can do this by adding the following code:

Code:
OP_DEFENCE = random(0, OP_DEF)
POINTS = POINTS - OP_DEFENCE
if POINTS < 0
	POINTS = 0
Again, I will not split up this code, as it's small enough already. So what this does it firstly finds a random number between the range 0 - OP_DEF, to again make this a more random outcome. It then takes away this new Defence variable from the Player's attack points, to find the real amount the Player will now hit on to the Opponent. And I then added a quick check to stop any errors and making the algorithm more realistic, which will basically assign the Points to hit as 0, if the Points to hit is already under 0. The reason for this is later on when you come to actually using this tutorial, if you have an extremely low leveled Player attacking a high leveled Opponent, this would go in to a minus state (E.g. -10), so if you then use that to minus from their health, it will instead add to their health (GCSE Maths shows, for example, 100--10 to make 100+10)

Now that we have this code for the Player, lets again use it for the Opponent. I will compile all of the code we have so far below:

Code:
PLAYER_STR = 5;
PLAYER_DEF = 5;
PLAYER_AGI = 5;
PLAYER_ACC = 5;

OP_STR = 5;
OP_DEF = 5;
OP_AGI = 5;
OP_ACC = 5;

PLAYER_HIT_CHANCE = (PLAYER_ACC / OP_AGI) * 100
if PLAYER_HIT_CHANCE < 0
 	PLAYER_HIT_CHANCE = 0
else if PLAYER_HIT_CHANCE > 100
	PLAYER_HIT_CHANCE = 100
else
	PLAYER_HIT_CHANCE = round_down(PLAYER_HIT_CHANCE)

PLAYER_HIT_RANDOM = random(0, 100)
if PLAYER_HIT_RANDOM <= PLAYER_HIT_CHANCE
	PLAYER_HIT = TRUE
else
	PLAYER_HIT = FALSE

OP_HIT_CHANCE = (OP_ACC / PLAYER_AGI) * 100
if OP_HIT_CHANCE < 0
 	OP_HIT_CHANCE = 0
else if OP_HIT_CHANCE > 100
	OP_HIT_CHANCE = 100
else
	OP_HIT_CHANCE = round_down(OP_HIT_CHANCE)

OP_HIT_RANDOM = random(0, 100)
if OP_HIT_RANDOM <= OP_HIT_CHANCE
	OP_HIT = TRUE
else
	OP_HIT = FALSE

POINTS = 0
if PLAYER_HIT == TRUE
	POINTS = random(0, PLAYER_STR)

OP_DEFENCE = random(0, OP_DEF)
POINTS = POINTS - OP_DEFENCE
if POINTS < 0
	POINTS = 0

OP_POINTS = 0
if OP_HIT == TRUE
	OP_POINTS = random(0, OP_STR)

PLAYER_DEFENCE = random(0, PLAYER_DEF)
OP_POINTS = OP_POINTS - PLAYER_DEFENCE
if OP_POINTS < 0
	OP_POINTS = 0
...And there we have it! This is only the start - it is extremely easy to expand on Mathematical systems like these.

Submit "Battle System Algorithm" to Digg Submit "Battle System Algorithm" to StumbleUpon Submit "Battle System Algorithm" to Google Submit "Battle System Algorithm" to del.icio.us

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments

  1. Dawnshadow's Avatar
    Wow, I actually understood that. (maybe I should look into acquiring some knowledge in coding)

    I enjoyed the randomness this code allows for. It makes every battle different and less repetitive (which is awesome because repetition can bore quickly). Thanks~ :3
  2. ThomasMosey's Avatar
    Quote Originally Posted by Dawnshadow
    Wow, I actually understood that. (maybe I should look into acquiring some knowledge in coding)

    I enjoyed the randomness this code allows for. It makes every battle different and less repetitive (which is awesome because repetition can bore quickly). Thanks~ :3
    That was the purpose of me making the tutorial, I've seen many sites where the outcome was always the same, it was originally on my own Blog, but it shut down many months ago.

    I'm glad you enjoyed it!
  3. Hituro's Avatar
    The most interesting bit of this is the one bit you leave out, why you chose to use Accuracy / Agility as the base for your to-hit roll. Everything else is just implementing that.

    For example you've chosen that for characters of equal ability (Acc 5 v.s Ag 5) you will hit 100% of the time. That means there is no advantage to being better, only as good. On the other hand your ability to hit fairly rapidly falls as your opponent's Agility exceeds your accuracy (Acc 5 vs. Ag 6 = 83% hit, Acc 5 vs. Ag 7 = 71% hit), while never reaching zero (though you do round down). This means that an outmatched opponent will never land a blow. Also this is not liner. At higher stat levels the same stat difference has a lower effect (Acc 50 vs. Ag 51 = 98% hit), which means in higher level battles most blows will land all the time. You might have, for example, thought about a minimum and maximum chance to hit as well.
  4. ThomasMosey's Avatar
    Quote Originally Posted by Hituro
    The most interesting bit of this is the one bit you leave out, why you chose to use Accuracy / Agility as the base for your to-hit roll. Everything else is just implementing that.

    For example you've chosen that for characters of equal ability (Acc 5 v.s Ag 5) you will hit 100% of the time. That means there is no advantage to being better, only as good. On the other hand your ability to hit fairly rapidly falls as your opponent's Agility exceeds your accuracy (Acc 5 vs. Ag 6 = 83% hit, Acc 5 vs. Ag 7 = 71% hit), while never reaching zero (though you do round down). This means that an outmatched opponent will never land a blow. Also this is not liner. At higher stat levels the same stat difference has a lower effect (Acc 50 vs. Ag 51 = 98% hit), which means in higher level battles most blows will land all the time. You might have, for example, thought about a minimum and maximum chance to hit as well.
    Thank you for your input, but my purpose in writing this tutorial is a nice, simple, minimalistic approach to alternative, more random, algorithms for Battle Systems.

Trackbacks

Total Trackbacks 0
Trackback URL: