Author Topic: Turn-Based Combat  (Read 1213 times)

Offline Glenugie

  • Level 10
  • *
  • Posts: 64
  • Reputation: +0/-0
    • View Profile
    • The Land of Ennui
Turn-Based Combat
« on: July 11, 2009, 11:40:19 AM »
The time I've spent here I have seen many of these threads crop up, mostly people who haven't made any attempt and want a way to make turn based combat, or in their best desires, to have it made for them.

I've been trying to tweak the combat code from here for a few days now, it's one of the few remaining things I need to fix to have my BBG fully operational (It's in closed beta right now)

Anyway, back to the request at hand. What I've assumed is required to make this work, is to have a while loop utilised so that the combat's initial while loop, for either one of the HP's to hit 0 or less must wait for the user to click a button to continue.

Basically, so I can allow the user to better see the combat system, to present a blow-by-blow account of combat if you will. My most recent attempts have been the most successful:
Code: [Select]
   $ButtonShowed="False";
   while (!isset($_POST['Continue']) and $ButtonShowed!="True" and $player['curhp'] > 0 and $monster['curhp'] > 0) {
          ?><form name=ContinueFighting action="<?php echo $_SERVER['PHP_SELF']?>" method=Post>
<input type=button class=button name=Continue value="Attack Again">
  </form><?
          $ButtonShowed="True";
}
The problem with this being that combat shows all the buttons at the top of the screen, but continues to run despite. I'm pretty sure that it's due to the $ButtonShowed being made True and thus cancelling the loop. But if I leave it out, it merely goes into an infinite loop, not allowing the user to click the button, or for combat to continue.

I'm positive it isn't the combat script merely being unresolved causing the infinite loop, as I limit the turns to 30 with this:
Code: [Select]
if($turns > 30) {
   //If the number of turns exceeds 30, stop combat
  die ("Stale mate! You did not kill the monster, and it did not kill you within the 30 turn combat limit.<br><a href=main.php>Return to Main Map</a>");}

Anyway, I wonder if someone here might be able to point me in the right direction to speed up my endeavour. At any rate, any help or advice you give will be most appreciated :)

Thanks in Advance,

Glenugie

Offline Barrikor

  • Level 21
  • *
  • Posts: 248
  • Reputation: +3/-0
    • View Profile
Re: Turn-Based Combat
« Reply #1 on: July 11, 2009, 03:56:41 PM »
Maybe it's scrambling the while statement....?

Code: [Select]
<?php

$ButtonShowed 
false ;
while ((( ! isset( 
$_POST['Continue'] ) && ! $ButtonShowed ) && ( $player['curhp'] > ) ) && ($monster['curhp'] > 0) ) 
{
?>

<form name=ContinueFighting action="<?php echo $_SERVER['PHP_SELF'] ; ?>" method=Post>
<input type=button class=button name=Continue value="Attack Again">
</form>
<?php
$ButtonShowed true ;
}
?>

« Last Edit: July 11, 2009, 04:34:44 PM by Barrikor »
Projects: Pith Framework (at 0.5), CactusGUI (at 0.3)

Offline Glenugie

  • Level 10
  • *
  • Posts: 64
  • Reputation: +0/-0
    • View Profile
    • The Land of Ennui
Re: Turn-Based Combat
« Reply #2 on: July 12, 2009, 03:08:51 AM »
Regretably that hasn't helped, it runs it's code as it should but only once, moving combat along despite the button not being pressed. I've tried various echo's to see if any of the other variables are picking up incorrect values but they all seem to be fine.

Leaving me at my earlier conclusion that it's the $ButtonShowed being included in the while loop's structure incorrectly. Unfortunately, that hasn't led me to any answers.

Are there any more suggestions?

Thanks for the reply though Barrikor,

Glenugie

Offline Nox

  • Level 35
  • **
  • Posts: 768
  • Reputation: +12/-2
    • View Profile
Re: Turn-Based Combat
« Reply #3 on: July 12, 2009, 03:31:10 AM »
I'm afraid I don't understand what you try to achieve
If you want to continue from turn to turn by pressing button then there is no need for while, because the whole while is calculated on (before) every page load so "if" would be enough
And you can't order PHP parsing to continue once the page is loaded, PHP is all parsed before you receive it, so just calculate current round and then simply display the continue button without a cycle...
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Glenugie

  • Level 10
  • *
  • Posts: 64
  • Reputation: +0/-0
    • View Profile
    • The Land of Ennui
Re: Turn-Based Combat
« Reply #4 on: July 12, 2009, 03:45:12 AM »
Alright, that would make sense I suppose. I'm working on it now, but to be clear, I would assume that the  button should act as a submit button and cause the page to reload, with the values for the current turns, stats and the like being written to session variables that would be destroyed at the end of combat?

Or am I overcomplicating things, again?

Thanks for taking the time to respond,

Glenugie

Offline Nox

  • Level 35
  • **
  • Posts: 768
  • Reputation: +12/-2
    • View Profile
Re: Turn-Based Combat
« Reply #5 on: July 12, 2009, 04:41:45 AM »
Yes, that's right...or you can use AJAX (use the almighty Google)
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Glenugie

  • Level 10
  • *
  • Posts: 64
  • Reputation: +0/-0
    • View Profile
    • The Land of Ennui
Re: Turn-Based Combat
« Reply #6 on: July 12, 2009, 04:54:39 AM »
For now I've got the solution involving session variables up and working, but I'll take a look into AJAX later in the project and see if I can improve on what I've got.

Thanks to everyone for all the help :)

Offline Nox

  • Level 35
  • **
  • Posts: 768
  • Reputation: +12/-2
    • View Profile
Re: Turn-Based Combat
« Reply #7 on: July 12, 2009, 08:01:08 AM »
Um, wait, I didn't read it carefuly enough...writing things into sessions...might help the database (actually recently someone thought about this), but remember that sessions expire after some time, so it might be necessary to store it into database...rather search for it a bit to see if you can use them, sorry, I didn't realise it before, hope you didn't code too much yet
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Glenugie

  • Level 10
  • *
  • Posts: 64
  • Reputation: +0/-0
    • View Profile
    • The Land of Ennui
Re: Turn-Based Combat
« Reply #8 on: July 12, 2009, 11:08:29 AM »
I'll monitor it with my beta testers, but it should be alright I think. I'll see I guess. Thanks for the heads up, but I already finished it :P

Offline Nox

  • Level 35
  • **
  • Posts: 768
  • Reputation: +12/-2
    • View Profile
Re: Turn-Based Combat
« Reply #9 on: July 12, 2009, 02:09:51 PM »
Just one of possible situations to think about - player is close to death, next round he would die, he closes browser, goes to watch bedtime story, after some time the fight session expires and data are gone...
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Hypemaster

  • Level 16
  • *
  • Posts: 145
  • Reputation: +3/-0
    • View Profile
    • FusionBroz.com
Re: Turn-Based Combat
« Reply #10 on: July 12, 2009, 06:14:33 PM »
You should store every turn in database. Sessions can be destroyed, if the losing they just quit browser and try again like nox said.
To be safe its best to store outcome of every turn into the database, thats how i would do it.
Or you could just do the battle completly in background store every step in database, and when player click attack again you just show next record/phase of battle. (only works if you only us attack again and not any special attacks).

Offline shoespeak

  • Level 11
  • *
  • Posts: 75
  • Reputation: +3/-0
    • View Profile
Re: Turn-Based Combat
« Reply #11 on: July 13, 2009, 09:42:41 AM »
Quote
The problem with this being that combat shows all the buttons at the top of the screen, but continues to run despite. I'm pretty sure that it's due to the $ButtonShowed being made True and thus cancelling the loop.
You are right about that.
Quote
But if I leave it out, it merely goes into an infinite loop, not allowing the user to click the button, or for combat to continue.
This is because there will be nothing canceling the loop if nobody dies. Change while to if and it will work.

Or, instead of using 'ButtonShowed' you can have a set a flag in the database like in_battle. Before every page, check to see if in_battle is set, and if it is you pull up the battle screen and let them finish dukeing it out.

Also I see while(!isset($_POST['Continue']) hopefully you are not doing everything like this (filter incoming data!)

My last thing to say is that I am not sure what you mean by 'storing turns in the database.' If after every turn you go through and update things like curhp, curmp, ammo, etc., then yes, I think that is what a lot of people do to ensure that the data just doesn't disappear. That's what I have always done...if you store these variables in the session and only update the database when the battle is over....well it would make for a pretty cool 'temporary battle' system (but I think it'd be too easy to game the system)

Offline Glenugie

  • Level 10
  • *
  • Posts: 64
  • Reputation: +0/-0
    • View Profile
    • The Land of Ennui
Re: Turn-Based Combat
« Reply #12 on: July 13, 2009, 04:17:37 PM »
I'm planning on setting up the system to store a DB value for if the user is in combat, meaning it will resume the battle, even if they close the browser.

And so you rest easy shoespeak, that is the only time to my knowledge I haven't filtered the incoming data :)

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal