Page 1 of 1
Re: battle turn formula
Posted: Thu Oct 18, 2012 1:25 pm
by Ark
You could make something like this:
Code: Select all
$Acounter = 0;
$Bcounter = 0;
$Ccounter = 0;
...
// when a round ends
...
$Acounter++;
$Bcounter++;
$Ccounter++;
if($Acounter == 2){
// attack!
$Acounter = 0;
}
if($Bcounter == 3){
// attack!
$Bcounter = 0;
}
...
Re: battle turn formula
Posted: Thu Oct 18, 2012 4:43 pm
by Jackolantern
I would probably do it in more of a generic turn loop, and then inject the character's turns into it. Say for example the speed stat is inverted, where the lower the better. That may seem limiting, but if you keep the stat high enough it gives room to grow. I am not sure how you register more or less party members in your game, so I am just assuming they are in an array called $partyMembers (I also don't know if you wanted multiple turns on different pages, or all figured at once. This is the latte):
Code: Select all
<?php
//skipping any other setup you need in the page
//string for outputting results of battle:
$result = "";
//boolean flag to cut off battle when done
$done = false;
//loop for incrementing turns
for ($x = 0; !$done; $x++) {
checkPlayerTurn($x);
checkMonsterTurn($x);
checkIfBattleOver();
}
function checkPlayerTurn($x) {
for ($y = 0; y < count($partyMembers); y++) {
/*Probably needs some explaining here. I am assuming you have an object for each player stored in the
* $partyMembers array with a stat called "speed" that can be checked. The modulus operator "%" returns the
* remainder of division (remember the way you learned division in 3rd grade or so, where you didn't deal with
* decimal places?). This way you can see if the character's speed stat is completely divisible by the turn number.
* If someone's speed was 1, they would act every iteration of the loop. If 4, it would be every 4 iterations of the loop.
*/
if ($x % $partyMembers[$y]->$speed == 0) {
playerAttack($partyMembers[$y]);
}
}
}
function playerAttack($player) {
//Custom battle logic put here using $player->attack and other stats for battle.
//Prepare the HTML list items for output to the player to show results of battle
$result += "<li>" + $attackResultString + "</li>";
}
function checkIfBattleOver() {
//Custom logic to see if battle is over. If it is....
$done = true;
//....to stop the main loop.
}
function checkMonsterTurn($x) {
//basically the same as checkPlayerTurn(), but
// ...with an array of monsters the player is attacking
}
function monsterAttack($monster) {
//called by checkMonsterTurn. Basically the same as playerAttack()
}
?>
<!-- skipping head of HTML file and any other banners or anything needed in HTML. Basically just getting to the battle results list -->
<ul>
<?php echo $result; ?>
</ul>
Re: battle turn formula
Posted: Thu Oct 18, 2012 9:46 pm
by Jackolantern
Yeah, you could definitely combine them together. I just figured they would be separate in your code, but it doesn't really matter much. The real key to keeping this code clean and from blowing up into an unmaintainable mess would be to create an entity class that can be both a player character and a monster, and then sub-class those into their respective final class (player_character and monster, for example). Then keep as much personal data and routines in there. For example, who they are hitting, the attack they are using (if not generic), their stats (including speed), etc. should all be kept in the parent class of Entity (well, the attack they are using may need to be a different implementation between Player Characters and Mobs) to keep the logic clean of the implementation.

Re: battle turn formula
Posted: Fri Oct 19, 2012 12:44 am
by Jackolantern
$x++ is just part of the FOR loop. That is called the "increment clause" of the FOR loop, and its whole purpose is to ensure you don't create an infinite loop. Each time the body of the FOR loop occurs, it will add 1 to the $x variable. I will admit that is kind of a strange FOR loop because instead of having the typical conditional comparison as the 2nd clause in the FOR loop, I only have a negated Boolean variable (!$done). I did this so that the way to exit the loop would be obvious, but it could have just as easily been done with a WHILE loop.
I have to say I am not exactly sure how the spreadsheet comes in. However, however you want to implement those stats would probably work fine in my code. The code I wrote is
not ready for use, and instead just shows how to create speed-based turns that are resolved on one page load.
If you want misses, that would just be an IF/ELSE statement inside the playerAttack() function. You would just test whatever the condition of a miss is (probably some random number generation calculated with a stat(s)), and if the miss condition is true, inside the IF statement you just do:
Code: Select all
$result += "<li>" + $player->name + " missed " + $player->target + "!</li>";
...where $player is the passed-in parameter that is an object with its name and attack target stored in it.
Then the ELSE segment of the same IF statement will be for when the player hits. That way, only one or the other will happen.
Does this make sense?
Re: battle turn formula
Posted: Fri Oct 19, 2012 1:02 am
by Jackolantern
Oops!
I have been spending too much time in Java and Javascript lands lol! I forgot (as I often do) that you can't concatenate strings with + in PHP, but rather, with a period ('.').
So, all of the
...needs to be
And the line of code I wrote in the last post needs to swap the "+" for "."

Re: battle turn formula
Posted: Fri Oct 19, 2012 2:22 am
by Callan S.
I think what I'd do is divide the speed of the character over 100 or such. Then figure each increment for it. Like a speed 4 character would go at 25, 50, 75 and 100. A speed ten character would go at 10, 20, 30, etc.
Once you've figured out each participants increment, you just have the program count down from 100 one at a time - anyone who has a matching speed increment to the current number gets an attack/turn.
Re: battle turn formula
Posted: Fri Oct 19, 2012 3:44 am
by Jackolantern
That is pretty much what mine does except it goes until it is signaled to quit, and the division to make higher numbers better for speed (which is probably the way it should be).
You did want all turns to occur on the same page, right?
Re: battle turn formula
Posted: Fri Oct 19, 2012 4:33 pm
by Jackolantern
Just change the FOR loop to this:
Code: Select all
for ($x = 100; $x > 0; $x--) {
//code here
}
And get rid of the $done variable, as it would not be needed. The attack condition IF statement should still work just fine since it just checks if the player's speed stat is divisible by the turn number.
Re: battle turn formula
Posted: Fri Oct 19, 2012 9:30 pm
by Jackolantern
No problem! Hope it helped

Re: battle turn formula
Posted: Fri Oct 19, 2012 11:58 pm
by Callan S.
Oroton wrote:Yes, how would you do a count down
It seems if your points were 25, you'd go on turn 100, 75, 50, and 25. But if your score was 20 you'd go on 100, 80 60 40 and 20, so again lower numbers would be better.. How would u invert that.
I think you're looking at the wrong number. If you have a speed of 4, then you divide 100 by that and get 25 for you're attack increment position. The speed of 4 matters and the higher it is, the better off you are.
Haven't tested this code, but
Code: Select all
$participantsinbattle=2;
$playerspeed[1]=4;
$playerspeed[2]=10;
for ($x = 100; $x > 0; $x--)
{
for ($check = 1; $check <= $participantsinbattle; $check++)
{
$speedtocheck=$playerspeed[$check];
if (floor($x/$speedtocheck)==$x/$speedtocheck) echo "Player ".$check." has a turn at position: ".$x."<br>";
}
}
Jack probably knows a more efficient/elegant method. But there's a base.