Simple NPC Battle V.1.0
Hello!
This mod will cover several topics:
-assigning stats to adoptables (and making them inheritable through breeding)
-training adoptables to increase stats
-competing against randomly generated NPCs
-earning trophies from competing
This mod was specifically coded for and tested with Mysidia 1.3.4. While it may be adaptable to other versions of Mysidia it would take quite a bit of tweaking.
Note: the battleview.php code is based on Kyttias's explore code, I have permission from her to use and share it with you all :)
Demo:
P.1: Stats
The code I will provide uses four stats: Sense, Speed, Strength, and Stamina. In this section I will show you how to add these four, but with some tweaking you can honestly use/add any sort of stats you'd like.
The first step is adding these stats to the database. In phpMyAdmin go to the table prefix_owned_adoptables, then go to the structure tab and scroll down to add four columns, naming them sense, speed, strength, and stamina. Type should be varchar and you shouldn't need more than 5. I set the default value as 10. (
screenshot)
Once that is done, it's time to make it so that when a user adopts a new pet that pet has random stats. Go into the file adopt.php and find this line:
PHP Code:
$mysidia->db->insert("owned_adoptables"...
Right above that insert this chunk of code:
PHP Code:
$sense = rand(10,100);
$speed = rand(10,100);
$strength = rand(10,100);
$stamina = rand(10,100);
Now add this to the end of the insert string, make sure it is BEFORE the ending ));, and that there is a comma between the last thing in there and the new stuff. So right after
"lastbred" => 0 add a comma, and then add this before the ));
PHP Code:
"sense" => $sense, "speed" => $speed, "strength" => $strength, "stamina" => $stamina
Now we need to do the same thing in the files class_promocode.php and class_stockadopt.php, just follow the above steps for these two files.
Now breeding is a little bit more tricky since we want bred adoptables to inherit their parent's stats. So go into class_breeding.php and find the insert statement ($mysidia->db->insert("owned_adoptables"...). Right above it add this lovely chunk of code:
PHP Code:
$mother_se = $this->female->sense;
$father_se = $this->male->sense;
$mother_sp = $this->female->speed;
$father_sp = $this->male->speed;
$mother_str = $this->female->strength;
$father_str = $this->male->strength;
$mother_st = $this->female->stamina;
$father_st = $this->male->stamina;
$parent_se = $mother_se + $father_se;
$parent_sp = $mother_sp + $father_sp;
$parent_str = $mother_str + $father_str;
$parent_st = $mother_st + $father_st;
$sense = $parent_se / 2;
$speed = $parent_sp / 2;
$strength = $parent_str / 2;
$stamina = $parent_st / 2;
What this does is retrieve all of the parent stats, add each stat together (mother's sense + father's sense, etc), and then finds the average between them. So if the mother has 32 sense and the father has 64, the babies will have 48.
After you add that, add this in just like we did for the other files:
PHP Code:
"sense" => $sense, "speed" => $speed, "strength" => $strength, "stamina" => $stamina
Cool, now that's it for stats! Our battle page will display the pet's stats anyways, but if you want to display it on the level up pages and such check these threads out:
Restyling the Manage Page
Public Pet Profiles
P.2: Training
This section will cover how to train pets in order to increase stats!
So, we are going to need two files. First create a file called train.php and paste this in it:
PHP Code:
<?php
class TrainController extends AppController{
public function __construct(){
parent::__construct("member");
}
public function index(){
$mysidia = Registry::get("mysidia");
}
}
?>
That goes in your root folder and it is good to go. Close it and make a new file, this one called trainview.php. Now copy and paste this code into it:
PHP Code:
<?php
class TrainView extends View{
public function index(){
$mysidia = Registry::get("mysidia");
$document = $this->document;
$document->setTitle("Stat Training");
$document->add(new Comment("Welcome to the Gym. People from all across the land come here to train their pets and prep them for battle.<br>
<center><b>Each training session costs 30 CURRENCY.</b></center><br><br>
To train your pet it must be an adult, and it must be set as your assigned companion.", FALSE));
$profile = $mysidia->user->getprofile();
$mysidia->user->getprofile();
if ((int)$profile->getFavpetID() == 0) {
$document->addLangVar('<br><br>It seems you do not yet have not assigned a companion! Assign one and then come back.');
return;
}
$favpet = new OwnedAdoptable($profile->getFavpetID());
if ($favpet->currentlevel < 3){
$document->add(new Comment("<br><br>Woah, sorry there! Your pet isn't old enough to train, yet! Come back with an adult if you want to train!", FALSE));
return;
}
if($favpet->currentlevel = 3){
$document->add(new Comment("To train, select the stat that you would like to work on.<br><br>
<center><b>{$favpet->name}'s Stats</b><br>
<u>Se | Sp | Str | Stm</u><br>
{$favpet->sense} | {$favpet->speed} | {$favpet->strength} | {$favpet->stamina}<br>", FALSE));
$this->exploreButton("Train_Sense", FALSE, "Train Sense");
$this->exploreButton("Train_Speed", FALSE, "Train Speed");
$this->exploreButton("Train_Strength", FALSE, "Train Strength");
$this->exploreButton("Train_Stamina", FALSE, "Train Stamina");
} # END no area selected
/* If an area is selected: */
if($mysidia->input->post("area")){
$area = $mysidia->input->post("area"); // This has apostrophes as *s instead, just like the exploreButtons above!
$str = str_replace("_", " ", $area);
$areaname = str_replace("*", "'", $str); // This one has apostrophes instead of asterisks.
$document->setTitle("{$areaname}");
$increase = rand(1,5);
$sense = $favpet->sense;
$newsense = $sense + $increase;
$speed = $favpet->speed;
$newspeed = $speed + $increase;
$strength = $favpet->strength;
$newstrength = $strength + $increase;
$stamina = $favpet->stamina;
$newstamina = $stamina + $increase;
switch ($area){
# First one is the default response that will happen unless something specific is specified:
case $areaname: $document->add(new Comment("<br><br>Oops, something has gone wrong.", FALSE));
break;
case "Train_Sense":
$currentcash = $mysidia->user->money;
$newmoney = $currentcash - 30;
$document->add(new Comment("<br><b>The Session:</b><br> Write something here about the Sense training.<br><br>
{$favpet->name} gained {$increase} Sense from this session!", FALSE));
$mysidia->db->update("users", array("money" => $newmoney), "username='{$mysidia->user->username}'");
$mysidia->db->update("owned_adoptables", array("sense" => $newsense), "aid='{$profile->getFavpetID()}'");
break;
case "Train_Speed":
$currentcash = $mysidia->user->money;
$newmoney = $currentcash - 30;
$document->add(new Comment("<br><b>The Session:</b><br> Write something here about the Speed training.<br><br>
{$favpet->name} gained {$increase} Speed from this session!", FALSE));
$mysidia->db->update("users", array("money" => $newmoney), "username='{$mysidia->user->username}'");
$mysidia->db->update("owned_adoptables", array("speed" => $newspeed), "aid='{$profile->getFavpetID()}'");
break;
case "Train_Strength":
$currentcash = $mysidia->user->money;
$newmoney = $currentcash - 30;
$document->add(new Comment("<br><b>The Session:</b><br> Write something here about the Strength training.<br><br>
{$favpet->name} gained {$increase} Strength from this session!", FALSE));
$mysidia->db->update("users", array("money" => $newmoney), "username='{$mysidia->user->username}'");
$mysidia->db->update("owned_adoptables", array("strength" => $newstrength), "aid='{$profile->getFavpetID()}'");
break;
case "Train_Stamina":
$currentcash = $mysidia->user->money;
$newmoney = $currentcash - 30;
$document->add(new Comment("<br><b>The Session:</b><br> Write something here about the training.<br><br>
{$favpet->name} gained {$increase} Stamina from this session!", FALSE));
$mysidia->db->update("users", array("money" => $newmoney), "username='{$mysidia->user->username}'");
$mysidia->db->update("owned_adoptables", array("stamina" => $newstamina), "aid='{$profile->getFavpetID()}'");
break;
}
return;
}
}
public function exploreButton($areaname, $image_link = "", $customtext = "", $customimg = ""){
$document = $this->document;
if ($image_link){ /* Image Links */
if (!$customimg){ /* Area Logo Image */
$imgname = str_replace("*", "'", $areaname);
$document->add(new Comment("
<form id='exploreform' action='train' name='exploreform' method='post' role='form'>
<input id='area' name='area' type='hidden' value='{$areaname}'>
<button id='{$areaname}' class='btn-text btn-sm' value='train' name='{$areaname}' type='submit'>
<img src=\"./images/{$imgname}_logo.png\"/>
</button>
</form>
", FALSE));
}
else { /* Custom Link Image */
$imgname = str_replace("*", "'", $customimg);
$document->add(new Comment("
<form id='exploreform' action='train' name='exploreform' method='post' role='form'>
<input id='area' name='area' type='hidden' value='{$areaname}'>
<button id='{$areaname}' class='btn-text btn-sm' value='train' name='{$areaname}' type='submit'>
<img src=\"./images/{$imgname}\"/>
</button>
</form>
", FALSE));
}
}
else { /* Text-Only Links */
if (!$customtext){ /* Area Name Button */
$str = str_replace("_", " ", $areaname); $btn_name = str_replace("*", "'", $str);
$document->add(new Comment("
<form id='exploreform' action='train' name='exploreform' method='post' role='form'>
<input id='area' name='area' type='hidden' value='{$areaname}'>
<button id='{$areaname}' class='btn-violet btn-sm' value='train' name='{$areaname}' type='submit'>
{$btn_name}
</button>
</form>
", FALSE));
}
else { /* Custom Link Text */
$customtext = str_replace("*", "'", $customtext);
$document->add(new Comment("
<form id='exploreform' action='train' name='exploreform' method='post' role='form'>
<input id='area' name='area' type='hidden' value='{$areaname}'>
<button id='{$areaname}' class='btn-violet btn-sm' style='display: inline;' value='train' name='{$areaname}' type='submit'>
{$customtext}
</button>
</form>
", FALSE));
}
}
return;
}
}
?>
First, customize the currency name around line 9 where it just says CURRENCY. Also change the price so that it reflects whatever you want to charge for each training session.
Then, to change the charge, scroll to where you see the line
PHP Code:
$newmoney = $currentcash - 30;
and just change 30 to whatever the cost should be.
Each type of training has a section for you to write about the training session or add images, etc. Just fill it in to your pleasing!
P.3: Battle
This is the major focus of this mod! Creating a page that allows the user's fave pet to battle!
This requires two new files:
battle.php
This goes in the root folder where the files like adopt.php and account.php are.
PHP Code:
<?php
class BattleController extends AppController{
public function __construct(){
parent::__construct("member");
}
public function index(){
$mysidia = Registry::get("mysidia");
}
}
?>
battleview.php
This goes in the view folder.
PHP Code:
<?php
/* This is the Battle Mod view page. This page is created based on the explore script created by Kyttias, so credit for the base of that goes to her. The ability to select and use the favepet was from RestlessThoughts, as posted on this thread; http://mysidiaadoptables.com/forum/showthread.php?t=4827&page=3
*This mod is entirely a collection of what Abronsyth has learned from messing around with mods created by wonderful users such as the two mentioned above.
*/
class BattleView extends View{
public function index(){
$mysidia = Registry::get("mysidia");
$document = $this->document;
$document->setTitle("Battle");
$document->add(new Comment("<br>", FALSE));
$profile = $mysidia->user->getprofile();
$mysidia->user->getprofile();
if ((int)$profile->getFavpetID() == 0) {
$document->addLangVar('<br><br>It seems you do not yet have not assigned a companion! Assign one and then come back.');
return;
}
$favpet = new OwnedAdoptable($profile->getFavpetID());
if ($favpet->currentlevel < 3){
$document->add(new Comment("<br><br>Woah, sorry there! Your companion isn't old enough to battle, yet! Come back with an adult companion if you want to compete!", FALSE));
return;
}
if($favpet->currentlevel = 3){
/*Random Opponent Stats*/
$opsense = rand(1,100);
$opstamina = rand(1,100);
$opstrength = rand(1,100);
$opspeed = rand(1,100);
/*Below determines how many trophies the pet earns by winning a batttle.*/
$trophies = $favpet->trophies;
$newtrophy = $trophies + 1;
$document->add(new Comment("You enter the battle arena with {$favpet->name} at your side. Waiting at the opposite end of the arena is your opponent!<br>
To engage in battle, select an attack from below. If you would not like to battle, you may simply leave this page.<br>", FALSE));
$this->exploreButton("Use_Bite", FALSE, "Bite");
$this->exploreButton("Use_Tackle", FALSE, "Tackle");
$this->exploreButton("Use_Trick", FALSE, "Trick");
}
if($mysidia->input->post("area")){
$area = $mysidia->input->post("area");
$document->setTitle("Battle");
switch ($area){
case "Use_Bite":
$opbite = $opspeed + $opstrength;
$bite = $favpet->speed + $favpet->strength;
$prize = rand(25,50);
$currentcash = $mysidia->user->money;
$newmoney = $prize + $currentcash;
if($bite > $opbite){
$document->add(new Comment("<br><br><table align='center' border='0px'>
<tr>
<td width='40%' style='text-align:left;vertical-align:bottom;'>
<i>{$favpet->name}</i><br>
<b>Sense:</b> {$favpet->sense}<br>
<b>Speed:</b> {$favpet->speed}<br>
<b>Strength:</b> {$favpet->strength}<br>
<b>Stamina:</b> {$favpet->stamina}<br>
</td>
<td width='20%'>
<center><b>VS</b></center>
</td>
<td width='40%' style='text-align:right;vertical-align:bottom'>
<i>Opponent</i><br>
<b>Sense:</b> {$opsense}<br>
<b>Speed:</b> {$opspeed}<br>
<b>Strength:</b> {$opstrength}<br>
<b>Stamina:</b> {$opstamina}<br>
</td>
</tr>
</table><br><center>Your pet dives in and attacks your opponent with a bite dealing {$bite} damage.
<br>The opponent counters the attack with {$opbite} damage, but it isn't enough!<br>
Your pet has won, and brought you {$prize} currency! Furthermore, {$favpet->name} has won a trophy!</center><br><br>", FALSE));
$mysidia->db->update("users", array("money" => $newmoney), "username='{$mysidia->user->username}'");
$mysidia->db->update("owned_adoptables", array("trophies" => $newtrophy), "aid='{$profile->getFavpetID()}'");
}
else{
$document->add(new Comment("<br><br><table align='center' border='0px'>
<tr>
<td width='40%' style='text-align:left;vertical-align:bottom;'>
<i>{$favpet->name}</i><br>
<b>Sense:</b> {$favpet->sense}<br>
<b>Speed:</b> {$favpet->speed}<br>
<b>Strength:</b> {$favpet->strength}<br>
<b>Stamina:</b> {$favpet->stamina}<br>
</td>
<td width='20%'>
<center><b>VS</b></center>
</td>
<td width='40%' style='text-align:right;vertical-align:bottom'>
<i>Opponent</i><br>
<b>Sense:</b> {$opsense}<br>
<b>Speed:</b> {$opspeed}<br>
<b>Strength:</b> {$opstrength}<br>
<b>Stamina:</b> {$opstamina}<br>
</td>
</tr>
</table><br><center>Your pet dives in and attacks your opponent with a bite dealing {$bite} damage.
<br>The opponent counters the attack with {$opbite} damage, and manages to over power {$favpet->name}!<br>
Unfortunately your pet lost...better luck next time!</center><br><br>", FALSE));
}
break;
case "Use_Tackle":
$optackle = $opstrength + $opstamina;
$tackle = $favpet->strength + $favpet->stamina;
$prize = rand(25,50);
$currentcash = $mysidia->user->money;
$newmoney = $prize + $currentcash;
if($tackle > $optackle){
$document->add(new Comment("<br><br>
<table align='center' border='0px'>
<tr>
<td width='40%' style='text-align:left;vertical-align:bottom;'>
<i>{$favpet->name}</i><br>
<b>Sense:</b> {$favpet->sense}<br>
<b>Speed:</b> {$favpet->speed}<br>
<b>Strength:</b> {$favpet->strength}<br>
<b>Stamina:</b> {$favpet->stamina}<br>
</td>
<td width='20%'>
<center><b>VS</b></center>
</td>
<td width='40%' style='text-align:right;vertical-align:bottom'>
<i>Opponent</i><br>
<b>Sense:</b> {$opsense}<br>
<b>Speed:</b> {$opspeed}<br>
<b>Strength:</b> {$opstrength}<br>
<b>Stamina:</b> {$opstamina}<br>
</td>
</tr>
</table><br><center>Your pet launches forward and slams into the opponent, dealing {$tackle} damage!
<br>The opponent counters the attack with {$optackle} damage, but it isn't enough!<br>
Your pet has won, and brought you {$prize} currency! Furthermore, {$favpet->name} has won a trophy!</center><br><br>", FALSE));
$mysidia->db->update("users", array("money" => $newmoney), "username='{$mysidia->user->username}'");
$mysidia->db->update("owned_adoptables", array("trophies" => $newtrophy), "aid='{$profile->getFavpetID()}'");
}
else{
$document->add(new Comment("<br><br>
<table align='center' border='0px'>
<tr>
<td width='40%' style='text-align:left;vertical-align:bottom;'>
<i>{$favpet->name}</i><br>
<b>Sense:</b> {$favpet->sense}<br>
<b>Speed:</b> {$favpet->speed}<br>
<b>Strength:</b> {$favpet->strength}<br>
<b>Stamina:</b> {$favpet->stamina}<br>
</td>
<td width='20%'>
<center><b>VS</b></center>
</td>
<td width='40%' style='text-align:right;vertical-align:bottom'>
<i>Opponent</i><br>
<b>Sense:</b> {$opsense}<br>
<b>Speed:</b> {$opspeed}<br>
<b>Strength:</b> {$opstrength}<br>
<b>Stamina:</b> {$opstamina}<br>
</td>
</tr>
</table><br><center>Your pet launches forward and slams into the opponent, dealing {$tackle} damage!
<br>The opponent counters the attack with {$opbite} damage, and manages to over power {$favpet->name}!<br>
Unfortunately your pet lost...better luck next time!</center><br><br>", FALSE));
}
break;
case "Use_Trick":
$optrick = $opsense + $opspeed;
$trick = $favpet->sense + $favpet->speed;
$prize = rand(25,50);
$currentcash = $mysidia->user->money;
$newmoney = $prize + $currentcash;
if($trick > $optrick){
$document->add(new Comment("<br><br>
<table align='center' border='0px'>
<tr>
<td width='40%' style='text-align:left;vertical-align:bottom;'>
<i>{$favpet->name}</i><br>
<b>Sense:</b> {$favpet->sense}<br>
<b>Speed:</b> {$favpet->speed}<br>
<b>Strength:</b> {$favpet->strength}<br>
<b>Stamina:</b> {$favpet->stamina}<br>
</td>
<td width='20%'>
<center><b>VS</b></center>
</td>
<td width='40%' style='text-align:right;vertical-align:bottom'>
<i>Opponent</i><br>
<b>Sense:</b> {$opsense}<br>
<b>Speed:</b> {$opspeed}<br>
<b>Strength:</b> {$opstrength}<br>
<b>Stamina:</b> {$opstamina}<br>
</td>
</tr>
</table><br><center>Your pet taunts the opponent and dashes out of the way at the last minute, tricking the opponent into running at the wall, dealing {$trick} damage!
<br>The opponent counters the attack with {$optrick} damage, but it isn't enough!<br>
Your pet has won, and brought you {$prize} currency! Furthermore, {$favpet->name} has won a trophy!</center><br><br>", FALSE));
$mysidia->db->update("users", array("money" => $newmoney), "username='{$mysidia->user->username}'");
$mysidia->db->update("owned_adoptables", array("trophies" => $newtrophy), "aid='{$profile->getFavpetID()}'");
}
else{
$document->add(new Comment("<br><br>
<table align='center' border='0px'>
<tr>
<td width='40%' style='text-align:left;vertical-align:bottom;'>
<i>{$favpet->name}</i><br>
<b>Sense:</b> {$favpet->sense}<br>
<b>Speed:</b> {$favpet->speed}<br>
<b>Strength:</b> {$favpet->strength}<br>
<b>Stamina:</b> {$favpet->stamina}<br>
</td>
<td width='20%'>
<center><b>VS</b></center>
</td>
<td width='40%' style='text-align:right;vertical-align:bottom'>
<i>Opponent</i><br>
<b>Sense:</b> {$opsense}<br>
<b>Speed:</b> {$opspeed}<br>
<b>Strength:</b> {$opstrength}<br>
<b>Stamina:</b> {$opstamina}<br>
</td>
</tr>
</table><br><center>Your pet taunts the opponent and dashes out of the way at the last minute, tricking the opponent into running at the wall, dealing {$trick} damage!
<br>The opponent counters the attack with {$optrick} damage, and manages to over power {$favpet->name}!<br>
Unfortunately your pet lost...better luck next time!</center><br><br>", FALSE));
}
break;
}
return;
}
}
public function exploreButton($areaname, $image_link = "", $customtext = "", $customimg = ""){
$document = $this->document;
if ($image_link){ /* Image Links */
if (!$customimg){ /* Area Logo Image */
$imgname = str_replace("*", "'", $areaname);
$document->add(new Comment("
<form id='exploreform' action='battle' name='exploreform' method='post' role='form'>
<input id='area' name='area' type='hidden' value='{$areaname}'>
<button id='{$areaname}' class='btn-text btn-sm' value='battle' name='{$areaname}' type='submit'>
<img src=\"./images/{$imgname}_logo.png\"/>
</button>
</form>
", FALSE));
}
else { /* Custom Link Image */
$imgname = str_replace("*", "'", $customimg);
$document->add(new Comment("
<form id='exploreform' action='battle' name='exploreform' method='post' role='form'>
<input id='area' name='area' type='hidden' value='{$areaname}'>
<button id='{$areaname}' class='btn-text btn-sm' value='battle' name='{$areaname}' type='submit'>
<img src=\"./images/{$imgname}\"/>
</button>
</form>
", FALSE));
}
}
else { /* Text-Only Links */
if (!$customtext){ /* Area Name Button */
$str = str_replace("_", " ", $areaname); $btn_name = str_replace("*", "'", $str);
$document->add(new Comment("
<form id='exploreform' action='battle' name='exploreform' method='post' role='form'>
<input id='area' name='area' type='hidden' value='{$areaname}'>
<button id='{$areaname}' class='btn-violet btn-sm' value='battle' name='{$areaname}' type='submit'>
{$btn_name}
</button>
</form>
", FALSE));
}
else { /* Custom Link Text */
$customtext = str_replace("*", "'", $customtext);
$document->add(new Comment("
<form id='exploreform' action='battle' name='exploreform' method='post' role='form'>
<input id='area' name='area' type='hidden' value='{$areaname}'>
<button id='{$areaname}' class='btn-violet btn-sm' style='display: inline;' value='battle' name='{$areaname}' type='submit'>
{$customtext}
</button>
</form>
", FALSE));
}
}
return;
}
}
?>
Note lines 20 and 24:
PHP Code:
if ($favpet->currentlevel < 3)
PHP Code:
if($favpet->currentlevel = 3)
These make it so that if the user's assigned fave pet must be level 3 in order to battle. Change this number to whichever suits your needs (and remember the levels start at 0 for the egg).
Also note lines 49, 108, and 169:
PHP Code:
$prize = rand(25,50);
This determines how much the user will be awarded each time their pet wins. The first number (25) is the minimum amount, and the second number (50) is the maximum amount.
There are many places in the code where you can alter and customize the text to your liking. For example, each attack uses two different stats, combines them to get a total, and uses that total against a similar total for the opponent.
Ask about it if you have questions!
Continue reading below for a guide on adding a simply trophy system!