13
« on: March 25, 2007, 12:35:27 pm »
For some reason I cant attach files so i have to post my whole code. Here is the pet_engine file:
<?php
/**********************************************************************************
* pet_engine.php                                                                  *
* Backend engnie for pets                                                        *
***********************************************************************************
* SMFPets: Pets MOD for Daniel15's Shop Mod for Simple Machines Forum            *
* =============================================================================== *
* Software Version:          SMFPets 1.0 (Build 1)                              *
* $Date:: 2007-02-10 8:33 PM EST (Sat, 10 Feb 2007)                            $ *
* $Id:: pet_engine.php 79 2007-01-18 08:26:55Z basilbeard                      $ *
* Software by:                Basilbeard                                          *
* Copyright      2007 by:    Basilbeard                                          *
* Support, News, Updates at:  http://www.daniel15.com/forum                      *
*                                                                                *
* Forum software by:          Simple Machines (http://www.simplemachines.org)    *
* Copyright 2006-2007 by:    Simple Machines LLC (http://www.simplemachines.org) *
*          2001-2006 by:    Lewis Media (http://www.lewismedia.com)            *
***********************************************************************************
* This program is free software; you may redistribute it and/or modify it under  *
* the terms of the provided license as published by Simple Machines LLC.          *
*                                                                                *
* This program is distributed in the hope that it is and will be useful, but      *
* WITHOUT ANY WARRANTIES; without even any implied warranty of MERCHANTABILITY    *
* or FITNESS FOR A PARTICULAR PURPOSE.                                            *
*                                                                                *
* See the "license.txt" file for details of the Simple Machines license.          *
* The latest version of the license can always be found at                        *
* http://www.simplemachines.org.                                                  *
**********************************************************************************/
// Basically, this is a basic pet. All the defaults are defined here.
// So, if anything isn't defined by the pet, the defaults are inherited from here
if (!class_exists('petTemplate')) {
class petTemplate
{
/*
* All the below fields aren't in the breed files themselves. They're just defaults
* The correct values are filled in with other functions.
* These values are used as fallovers. If any of the variables aren't defined in the item,
* it will use the defaults from here.
*/
//These vars depend on the pet and are set by the owner of the pet.
var $name = 'No Name'; // The name of the pet
var $desc = 'No Desc'; //The description the user has given.
var $level = 0; //The level of the pet;
var $curap = 0; //The current number of skill points the pet has
var $maxap = 0; //The max number of skill points the pet has
var $age = 0; //How old(in days) the pet is
var $happy = 0; //Pets Happiness
var $hunger = 0; //Pets Hunger
var $id = 0; //Pet ID
var $training = 0; //Is the pet training ?
var $trainingend = '00000000'; //When does training end?
var $ownerid = 1; //Owner of the pet
//These vars depend on the breed and are loaded up in the free file.
var $breed = 'No Breed'; //The breed of the pet
var $breed_desc = 'No Breed Desc'; //The description of the breed.
    var $price = 700; //The price to purchase one of these pets
    var $breed_img = 'blank.jpg'; //The img file of the breed
    var $skills_cnt = 0; //Number of skills owned by the pet.
// -----------------------------------------------------------------------
//Lots of functions to make life easier everywhere else
//The view function that people see on your pets profile
function view_Pet() {
global $txt, $sourcesdir;
switch ($this->happy) {
case 0: $happy = "Very Depressed"; break;
case 1: $happy = "Sad"; break;
case 2: $happy = "Sad"; break;
case 3: $happy = "Gloomy"; break;
case 4: $happy = "Gloomy"; break;
case 5: $happy = "Content"; break;
case 6: $happy = "Content"; break;
case 7: $happy = "Happy"; break;
case 8: $happy = "Happy"; break;
case 9: $happy = "Joyous"; break;
case 10: $happy = "Joyous"; break;
case 11: $happy = "Joyous"; break;
case 12: $happy = "Gleeful"; break;
case 13: $happy = "Gleeful"; break;
case 14: $happy = "Gleeful"; break;
default: $happy = "Excstatic"; break;
}
switch ($this->hunger) {
case 0: $hunger = "Starving"; break;
case 1: $hunger = "Very Hungry"; break;
case 2: $hunger = "Very Hungry"; break;
case 3: $hunger = "Hungry"; break;
case 4: $hunger = "Hungry"; break;
case 5: $hunger = "Content"; break;
case 6: $hunger = "Content"; break;
case 7: $hunger = "Happy Tummy"; break;
case 8: $hunger = "Happy Tummy"; break;
case 9: $hunger = "Full"; break;
case 10: $hunger = "Full"; break;
case 11: $hunger = "Full"; break;
case 12: $hunger = "Very Full"; break;
case 13: $hunger = "Very Full"; break;
case 14: $hunger = "Very Full"; break;
default: $hunger = "Bloated"; break;
}
return "
<table width='85%' cellpadding='5' cellspacing='0' border='1' align='center'>
<tr class = 'titlebg2'><th colspan='6'>".$this->name." the ".$this->breed."</th></tr>
<tr class = 'middletext'><td colspan='1' rowspan='3' width='10%'><img border='0' src='".$sourcesdir."/forum/Sources/shop/pet_images/".$this->breed_img."' alt='".$this->breed."' />
    <th colspan='1' width='20%'>".$txt['pets_name']."</th>
    <td colspan='1'>".$this->name."</td>
    <th colspan='1' width='20%'>".$txt['pets_breed']."</th>
<td colspan='2'>".$this->breed."</td></tr>
<tr><th colspan='1' width='10%'>".$txt['pets_happy']."</th>
<td colspan='1'>".$happy."</td>
<th colspan='1'>".$txt['pets_hunger']."</th>
<td colspan='1'>".$hunger."</td></tr>
<tr><th colspan='1' width='20%'>".$txt['pets_level']."</th>
<td colspan='1'>".$this->level."</td>
<th colspan='1' width='20%'>".$txt['pets_age']."</th>
<td colspan='2'>".sprintf($txt['pets_days'], $this->age)."</td></tr>
<tr><th colspan='1'>".$txt['pets_description']."</th>
<td colspan='5'>".$this->desc."</td></tr>";
}
//The view function that you see on your pets menu. Allows for skills and such.
function view_Pet_owner() {
global $txt, $sourcesdir;
return "
<table width='75%' cellpadding='5' cellspacing='0' border='1' align='center'>
<tr 'titlebg2'><th colspan='6'>".$this->name." the ".$this->breed."</th></tr>
<tr><td colspan='1' rowspan='3' width='10%'><img border='0' src='".$sourcesdir."/forum/Sources/shop/pet_images/".$this->breed_img."' alt='".$this->breed."' />
    <th colspan='1' width='20%'>".$txt['pets_name']."</th>
    <td colspan='1'>".$this->name."</td>
    <th colspan='1' width='20%'>".$txt['pets_breed']."</th>
<td colspan='2'>".$this->breed."</td></tr>
<tr><th colspan='1' width='10%'>".$txt['pets_happy']."</th>
<td colspan='1'>".$this->happy."</td>
<th colspan='1'>".$txt['pets_hunger']."</th>
<td colspan='1'>".$this->hunger."</td></tr>
<tr><th colspan='1' width='20%'>".$txt['pets_level']."</th>
<td colspan='1'>".$this->level."</td>
<th colspan='1' width='20%'>".$txt['pets_age']."</th>
<td colspan='2'>".sprintf($txt['pets_days'], $this->age)."</td></tr>
<tr><th colspan='1'>".$txt['pets_description']."</th>
<td colspan='5'>".$this->desc."</td></tr>
<tr><th colspan='6'>".$txt['pets_do_stuff'].$this->curap."/".$this->maxap.")</th></tr>
<tr><th colspan='1' width='10%'>".$txt['pets_use_skill']."</th>
<td colspan='2'>".$this->skillsMenu()."</td>
<th colspan='1' width='10%'>".$txt['pets_train']."</th>
<td colspan='2'>".$this->trainMenu()."</td></tr>
<tr><th colspan='1' width='10%'>".$txt['pets_rename']."</th>
<td colspan='2'>".$this->renameMenu()."</td>
<th colspan='1' width='10%'>".$txt['pets_describe']."</th>
<td colspan='2'>".$this->describeMenu()."</td></tr></table><br />";
}
//Skills menu. Called by avove function. Used to make things nicer.
function skillsMenu() {
global $txt, $scripturl;
$this->load_breed();
$returnstring = "<form method='post' action='".$scripturl."?action=shop;do=pets;type=skills;pet=".$this->id."'><select name='skill'>";
$cnt = 0;
for ($i = 0; $i < $this->skills_cnt; $i++) {
if ($this->skill[$i]->level_learn <= $this->level && $this->skill[$i]->cost <= $this->curap) {
$cnt++;
$returnstring .= "<option value=".$i." selected = 'selected'>".$this->skill[$i]->name." (Cost: ".$this->skill[$i]->cost."SP)</option>";
}
}
if ($cnt == 0) {
$returnstring = $txt['pets_no_skills'];
}
else {
$returnstring .= "<input type='submit' value='{$txt['pets_use_skill']}'></form>";
}
}
//Train menu. Called by avove function. Used to make things nicer.
function trainMenu() {
global $txt, $scripturl;
  $date = date('z');
  if ($date < 10) {
  $date = '00'.$date;
  }
  elseif ($date < 100) {
  $date = '0'.$date;
  }
  $date = date('y').$date.date('His');
if ($this->training == 1) {
if ($date > $this->trainingend){
$middlestring = $txt['pets_course_finish2'];
$command = $txt['pets_train_pet'];
$returnstring = "<form method='post' action='".$scripturl."?action=shop;do=pets;type=train;pet=".$this->id."'> ";
$returnstring .= $middlestring;
$returnstring .= " <input type='submit' value='{$command}'></form>";
}
else{
$middlestring = sprintf($txt['pets_course_taking'], $this->training_date($this->trainingend));
$command = $txt['pets_train_pet'];
$returnstring = "<form method='post' action='".$scripturl."?action=shop;do=pets;type=train;pet=".$this->id."'> ";
$returnstring .= $middlestring;
}
}
else {
$middlestring = $txt['pets_train_cost'].formatMoney($this->calcTrainCost());
$command = $txt['pets_train'];
$returnstring = "<form method='post' action='".$scripturl."?action=shop;do=pets;type=train;pet=".$this->id."'> ";
$returnstring .= $middlestring;
$returnstring .= " <input type='submit' value='{$command}'></form>";
}
return $returnstring;
}
//Rename. Called by avove function. Used to make things nicer.
function renameMenu() {
global $txt, $scripturl;
$returnstring = "<form method='post' action='".$scripturl."?action=shop;do=pets;type=rename;pet=".$this->id."'>";
$returnstring .= "<input type='text' name='newname' value='{$this->name}'>";
$returnstring .= "<input type='submit' value='{$txt['pets_rename']}'></form>";
return $returnstring;
}
//Describe menu. Called by avove function. Used to make things nicer.
function describeMenu() {
global $txt, $scripturl;
$returnstring = "<form method='post' action='".$scripturl."?action=shop;do=pets;type=describe;pet=".$this->id."'>";
$returnstring .= "<input type='text' name='newname' value='{$this->desc}'>";
$returnstring .= "<input type='submit' value='{$txt['pets_describe']}'></form>";
return $returnstring;
}
//Training cost. Set to n * (n+1)*5 where n is the current level.
function calcTrainCost() {
return ($this->level) * ($this->level + 1) * 4;
}
//Using a skill
function use_skill($skillid = 0) {
global $txt;
$this->load_breed();
$min_level = $this->skill[$skillid]->level_learn;
$max = $this->skill[$skillid]->level_max;
$cost = $this->skill[$skillid]->cost;
if ($cost > $this->curap) {
return $txt['pets_skill_no_power'];
}
elseif ($this->hunger < 5) {
return $txt['pets_skill_hunger'];
}
elseif ($this->happy < 5) {
return $txt['pets_skill_happy'];
}
else {
if ($this->level < $min_level) $chance = 0;
elseif ($this->level < $max) $chance = floor(100 * ($this->level - $min_level) / ($max - $min_level));
else $chance = 100;
$rand = mt_rand(1,100);
if ($rand > $chance) {
$this->curap -= $cost;
return $txt['pets_skill_fail'];
}
else {
$this->curap -= $cost;
eval('$return = $this->use_skill_' . $skillid . '();');
return $return;
}
}
}
//Change a pets name
function rename_pet($newname = 'No Name') {
$this->name = $newname;
}
//Change a pets description
function redesc_pet($newdesc = 'No Desc') {
$this->desc = $newdesc;
}
//Store the values back into the database(very important)
function store_values() {
global $db_prefix;
$result = db_query("UPDATE {$db_prefix}shop_pets
SET name = '{$this->name}',
    level = {$this->level},
    curap = {$this->curap},
    maxap = {$this->maxap},
    happy = {$this->happy},
    `desc` = '{$this->desc}',
    hunger = {$this->hunger},
    age = {$this->age},
    training = {$this->training},
    trainingend = '{$this->trainingend}',
    ownerid = {$this->ownerid}
    WHERE pet_id = {$this->id}", __FILE__, __LINE__);
}
//Load the values from the database(also important)
function load_values($id = 0) {
global $db_prefix;
if ($id != 0) {
$result = db_query("SELECT * FROM {$db_prefix}shop_pets
WHERE pet_id = {$id}", __FILE__, __LINE__);
$row = mysql_fetch_assoc($result);
$this->name = $row['name'];
$this->breed = $row['breed'];
$this->desc = $row['desc'];
$this->level = $row['level'];
$this->curap = $row['curap'];
$this->maxap = $row['maxap'];
$this->age = $row['age'];
$this->happy = $row['happy'];
$this->hunger = $row['hunger'];
$this->training = $row['training'];
$this->trainingend = $row['trainingend'];
$this->ownerid = $row['ownerid'];
$this->id = $row['pet_id'];
$this->load_breed();
}
}
//Advance a day(yay)
function advance_day() {
global $db_prefix;
$happy_loss = mt_rand(1,4) - 1;
$hunger_loss = mt_rand(1,2);
if ($this->happy - $happy_loss < 0) $happy_loss = $this->happy;
if ($this->hunger - $hunger_loss < 0) $hunger_loss = $this->hunger;
$this->happy = $this->happy - $happy_loss;
$this->hunger = $this->hunger - $hunger_loss;
$this->age++;
$this->curap = $this->maxap;
}
 
  //Training a pet
  function train_pet() {
global $db_prefix, $ID_MEMBER, $txt;
  $date = date('z');
  if ($date < 10) {
  $date = '00'.$date;
  }
  elseif ($date < 100) {
  $date = '0'.$date;
  }
  $date = date('y').$date.date('His');
  if ($this->training != 1) {
  $cost = $this->calcTrainCost();
  $result = db_query("SELECT * from {$db_prefix}members WHERE ID_MEMBER = {$ID_MEMBER}", __FILE__, __LINE__);
  $row = mysql_fetch_assoc($result);
  if ($row['money'] < $cost) {
  return $txt['pets_no_train_money'];
  }
  else {
$result = db_query("
UPDATE {$db_prefix}members
SET money = money - {$cost}
WHERE ID_MEMBER = {$ID_MEMBER}", __FILE__, __LINE__);
  $this->training = 1;
  $this->trainingend = $this->training_end($date);
  return sprintf($txt['pets_place_course'], $this->training_date($this->trainingend));
  }
  }
  else {
  if ($date > $this->trainingend) {
  $this->level++;
  if ($this->level == 10) $this->maxap++;
  if ($this->level == 30) $this->maxap++;
  if ($this->level == 60) $this->maxap++;
  if ($this->level == 100) $this->maxap++;
  $this->training = 0;
  return sprintf($txt['pets_course_finish'], $this->level);
  }
  else {
  return sprintf($txt['pets_course_taking'], $this->training_date($this->trainingend));
  }
  }
}
//Finding the ending training date
function training_end($date = '00000000000') {
$train_hours = $this->level * 4;
$dates = preg_split('//', $date);
$cur_year = $dates[1].$dates[2];
$cur_date = $dates[3].$dates[4].$dates[5];
$cur_hour = $dates[6].$dates[7];
$cur_minsec = $dates[8].$dates[9].$dates[10].$dates[11];
$hour = $cur_hour + $train_hours;
$extra_days = 0;
while ($hour >= 24) {
$hour = $hour - 24;
$extra_days++;
}
if ($hour < 10) {
$hour = "0".$hour;
}
$cur_date += $extra_days;
if (date('L') == 0 && $cur_date >= 365) {
$cur_year++;
$cur_date = $cur_date - 365;
}
elseif (date('L') == 1 && $cur_date >= 366) {
$cur_year++;
$cur_date = $cur_date - 366;
}
if ($cur_date < 10) {
$cur_date = '00'.$cur_date;
}
elseif ($cur_date < 100) {
$cur_date = '0'.$cur_date;
}
return $cur_year.$cur_date.$hour.$cur_minsec;
}
function training_date($date = '00000000000') {
$dates = preg_split('//', $date);
$cur_year = $dates[1].$dates[2];
$cur_date = $dates[3].$dates[4].$dates[5];
$cur_hour = $dates[6].$dates[7];
$cur_min = $dates[8].$dates[9];
$cur_sec = $dates[10].$dates[11];
$cur_hour++;
$month = 1;
$cur_date++;
if ($cur_date > 31) {
$cur_date = $cur_date - 31;
$month++;
if ($cur_date > 28 && date('L') == 0) {
$cur_date = $cur_date - 28;
$month++;
if ($cur_date > 29 && date('L') == 1) {
$cur_date = $cur_date - 29;
$month++;
if ($cur_date > 31) {
$cur_date = $cur_date - 30;
$month++;
if ($cur_date > 30) {
$cur_date = $cur_date - 31;
$month++;
if ($cur_date > 31) {
$cur_date = $cur_date - 30;
$month++;
if ($cur_date > 30) {
$cur_date = $cur_date - 31;
$month++;
if ($cur_date > 31) {
$cur_date = $cur_date - 30;
$month++;
if ($cur_date > 31) {
$cur_date = $cur_date - 31;
$month++;
if ($cur_date > 30) {
$cur_date = $cur_date - 30;
$month++;
if ($cur_date > 31) {
$cur_date = $cur_date - 31;
$month++;
if ($cur_date > 30) {
$cur_date = $cur_date - 30;
$month++;
}
}
}
}
}
}
}
}
}
}
}
}
return date("H:i:s, F jS Y", mktime($cur_hour, 0, 0, $month, $cur_date, $cur_year));
}
}
}
//Simple class to handle skills
if (!class_exists('skill')) {
class skill {
var $level_learn = 0;
var $level_max = 0;
var $cost = 1;
var $name = 'Skill';
var $code = 'return "This skill does nothing"';
}
}
?>