Battle System Algorithm
by , 07-16-2012 at 12:58 PM (791 Views)
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:
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;
Again, these will just be the number 5 for me.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;
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:
Now, let me explain what each of these parts is doing.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)
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:PLAYER_HIT_CHANCE = (PLAYER_ACC / OP_AGI) * 100
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:if PLAYER_HIT_CHANCE < 0 PLAYER_HIT_CHANCE = 0
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 if PLAYER_HIT_CHANCE > 100 PLAYER_HIT_CHANCE = 100
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.Code:else PLAYER_HIT_CHANCE = round_down(PLAYER_HIT_CHANCE)
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:
Now to explain the code:Code:PLAYER_HIT_RANDOM = random(0, 100) if PLAYER_HIT_RANDOM <= PLAYER_HIT_CHANCE PLAYER_HIT = TRUE else PLAYER_HIT = FALSE
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:PLAYER_HIT_RANDOM = random(0, 100)
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:if PLAYER_HIT_RANDOM <= PLAYER_HIT_CHANCE PLAYER_HIT = TRUE
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.Code:else PLAYER_HIT = FALSE
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.
And that's it for this step of the tutorial. On to the next!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
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:
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.Code:POINTS = 0 if PLAYER_HIT == TRUE POINTS = random(0, PLAYER_STR)
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:
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)Code:OP_DEFENCE = random(0, OP_DEF) POINTS = POINTS - OP_DEFENCE if POINTS < 0 POINTS = 0
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:
...And there we have it! This is only the start - it is extremely easy to expand on Mathematical systems like these.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










Email Blog Entry
