• Hi all. We have had reports of member's signatures being edited to include malicious content. You can rest assured this wasn't done by staff and we can find no indication that the forums themselves have been compromised.

    However, remember to keep your passwords secure. If you use similar logins on multiple sites, people and even bots may be able to access your account.

    We always recommend using unique passwords and enable two-factor authentication if possible. Make sure you are secure.
  • Be sure to join the discussion on our discord at: Discord.gg/serebii
  • If you're still waiting for the e-mail, be sure to check your junk/spam e-mail folders

Implement Battle system in PHP

Well, I am trying to make an online RPG on Pokemon, but I am stuck at the Battle system!
I have seen the formula for the *Damage* on this website, but unable to think of a proper mechanism to implement it!

If anyone is having any idea about it! please help me out!
 

Rezzo

Occasionally
Just make a function which does the calculation, and call the calculation wherever it is needed. For the attack power and the stats of the Pokémon, make a parent function which collect these and give them to the calculation function. I might have made that a bit unclear so let me know if you want me to expand a bit more

Edit: I've decided to expand my answer regardless, seeing as my previous answer was rubbish.

First, you will want a parent class (We'll call it Stats) which collects the stats that the damage calculation needs. After this, you will need to build a child class (call it Damage) which extends the parent class, gathers each stat which should be saved inside of a variable, and does the calculation. After this, you can create an if statement which checks if a Pokémon has fainted or not by comparing the current HP stat to the damage done (yes, that means you should also collect the receiving Pokémon's HP stat too!).

There are many other variables to think about, such as how you will want to implement the chances of a status occurring, recoil damage, weather and item buffs... but as long as you have this fundamental working you can build upon it.
 
Last edited:
Thats nice! but I have already stored everything in my database!
Now I just required to somehow make it function!... I am able to understand all the formula and calculation where and when should I do!! But implementing that in PHP is a bit challenging stuff for me! :\
 

Playful Latios

@Soul Dew
You should probably make it AJAX based. That's what my game is. Remember that every function you'll need to globalize certain variables, especially if you are using a class for database. Also, I'm going to recommend having mod1, mod2, and mod3 be separate functions so it is easier to read the code.

If you do make it AJAX based, I can show you an example of the code I use.

PHP:
function battlemenu()
{
	global $plugins, $pid, $username, $wid, $x, $y, $id, $hash, $location;
	$battlemenucontent = "<div id=\"battlemenu\"><div class=\"fight\" onclick=\"loadScript('fight&wid=$wid&pid=$pid')\">Fight</div><!--End Fight Div-->
	<div class=\"switch\" onclick=\"loadScript('switchpokemon&wid=$wid&inbattle=1&pid=$pid&username=$username')\">Switch</div><!--End Switch Div-->
	<div class=\"item\" onclick=\"loadScript('item&wid=$wid&inbattle=1&pid=$pid')\">Items</div><!--End Items div-->
	<div class='run'><a href='?location=$location&x=$x&y=$y&id=$id&unique=$hash&run=1&inbattle=1&wid=$wid'>Run Away</a></div><!--End Run Div-->";
	$plugincontent = $plugins->execute_code("battle_menu", "menu", "end");
	echo $battlemenucontent . $plugincontent . "</div><!--End Battle Menu Div-->";
}

loadScript is my javascript function that makes AJAX requests. The first parameter is what function on the AJAX page it should call. The reason you'll see wid=$wid and pid=$pid is because otherwise it will lose that data which results in the page breaking.
 
Last edited:
Actually my game till now is PHP based! coz m not use to AJAX properly... But surely I understand it requirement and would work on it!
I would need it for LIVE Battle System!
 
Top