Question about Events

Post all your tuts or request for tuts here.
Post Reply
Gheeda
Posts: 43
Joined: Fri Sep 03, 2010 12:12 am

Question about Events

Post by Gheeda »

Any ideas on where to start in regards to adding events to my random combat encounters? I'd like to have certain 'creatures', that cant be attacked/wont attack,pop up to say a line or two of dialogue along with an item or stat boost for the player.

Im also having issues with passwords, any password including numbers wont work. thoughts?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Question about Events

Post by Jackolantern »

To be clear, you want to have random battle events triggered depending on the mob you are fighting? If so, you could create a few extra fields to each mob in the database: 1event, 2event, 3event, 4event, 5event (name them something more descriptive if you wish). They should be left blank (null) if that mob does not have that event registered. Then you could have something like this in each combat cycle:

Code: Select all

/*After you pull up the mob's info for combat, save the variable you are using that holds the mob's data. We will call it "$mobData". Note: This code is to be performed at the time the mob variables are being set, before the mob resource handle is destroyed, and only once per battle. This block of code is *not* called every combat cycle.*/

$mob1Event = (isset($mobData['1event'])) ? true : false;
$mob2Event = (isset($mobData['2event'])) ? true : false;
$mob3Event = (isset($mobData['3event'])) ? true : false;
$mob4Event = (isset($mobData['4event'])) ? true : false;
$mob5Event = (isset($mobData['5event'])) ? true : false;

/*This code will be performed at the time you want the events to be checked in each cycle. This larger block need to be called *every* combat cycle. This block assumes you have the mob ID stored in a variable called "$monsterID".*/

$randomResult = rand(1, 100);
if ($randomResult <= 100 && $randomResult >= 80 && $mob5Event == true) {
    $event = $getMobEvent($monsterID, 5);
    $doMobEvent($event);
} else if ($randomResult <= 79 && $randomResult >= 60 && $mob5Event == true) {
    $event = $getMobEvent($monsterID, 4);
    $doMobEvent($event);
} else if ($randomResult <= 59 && $randomResult >= 40 && $mob3Event == true) {
    $event = $getMobEvent($monsterID, 3);
    $doMobEvent($event);
} else if ($randomResult <= 39 && $randomResult >= 20 && $mob2Event == true) {
    $event = $getMobEvent($monsterID, 2);
    $doMobEvent($event);
} else if ($randomResult <= 19 && $randomResult >= 1 && $mob1Event == true) {
    $event = $getMobEvent($monsterID, 1);
    $doMobEvent($event);
}

/*This is the function for actually pulling up the event. The function returns a string with the name of the event to fire. Since these next 2 code blocks are functions, they need to be *outside* of the combat cycle. You could give them their own files, and then include them with include_once('getmobevent.php');, or something like that*/

function $getMobEvent($mobIn, $eventSlot) {
    //pull up the mob's event being called. These lines assume you named the event fields as outlines above, assumes
    //...all monsters are in a table named "mobs", assumes you have the mob ID in a field named "$monsterID", and 
    //...assumes the field in the "mobs" table storing the mobID is called "mobID". Adjust accordingly.
    $eventHolder = mysql_fetch_assoc(mysql_query("SELECT ".$eventSlot."event FROM mobs WHERE mobID = ".$monsterID));
    $eventToFire = $eventHolder[$eventSlot.'event'];
    return $eventToFire
}

/*This is the function that handles firing the event. It is mostly going to be made up of your event logic. You would likely just want to make this an interface-type function, where it calls other event-specific functions that contain the actual event code. If you store all the event code in this function, it will become a huge, unmaintainable wall of code. This function could also use a large switch statement instead of the if/else construct.*/

function $doMobEvent($eventIn) {
    if ($eventIn == "Lightning Arrows") {
        lightningArrows();
        return;
    } else if ($eventIn == "Divine Faith") {
        divineFaith();
        return;
    }...and so on...
}
The reason for creating all of the $mob1event = true/false variables is so that there could be a check on each event check to make sure the mob actually has the event. It would be inefficient and would cause many unneeded MySQL queries if you had to run a query on every single combat cycle, when it is likely that most mobs won't have all events set. Since you have to pull up the mob's info to start combat, you can just run the event check at that time and fill out all of the boolean (true/false) values there. Make sure that part is only done once as you are filling out the mob's variables for battle. The rest of the code would need to be performed with every combat cycle.

Hope this helps!

EDIT: As far as your issue with passwords, we would need to see your login processing script to see what the problem is.
The indelible lord of tl;dr
Gheeda
Posts: 43
Joined: Fri Sep 03, 2010 12:12 am

Re: Question about Events

Post by Gheeda »

login.php

Code: Select all

<form method="POST" action="authenticate.php">
User Name <input type="text" name="player" size="21">
Password <input type="password" name="password" size="21" mask="x">
<br>
<input type="submit" value="Login" name="submit">

<br><br>Not Registered? <a href='register.php'>Register
authenticate.php

Code: Select all

<?php
include_once 'connect.php';
session_start();

if (isset($_POST['submit']))
{
  $player=$_POST['player'];
  $password=$_POST['password'];
  $player=strip_tags($player);
  $password=strip_tags($password);
  $password=md5($password);

  $query = "select name,password from players where name='$player' and '$password'";
  $result = mysql_query($query) or die("Could not query players");
  $result2 = mysql_fetch_array($result);
  if ($result2)
  { 
    $_SESSION['player']=$player;
    
    echo "<big>Logged in successfully<br>";
    echo "<A href='index.php'>Continue</a></big>";
  }
  else
  {
   echo "<big>Wrong username or password.<A href='login.php'>Try Again</a></big>";
  }
}
?>
About the events, I apologize for being unclear although what you came up with does interest me. I was wondering how to combine two tables of like data into a pool of random encounters/events. I was also curious about the coding differences between a combat encounter...which would prompt the attack.php ( which is where my damage rolls & exp/gold allocating happens)..and an event encounter that instantly adds shit. Best example I can think of is The Kingdom of Loathing.


Edit: I got as far as to think up a database that had event 'setpieces' be it a character or an image of an item. In this database I'd have forms that, if applicable, would be filled in with stat bonuses. I could call on the setpiece and echo a stat bonus, sure... But I dont know how to implement this into a pre-existing combat system, the nessasary broadness of the $updateplayer escapes me as well as its IF Command to remain out of sight until prompted....also, the issue of distributing items tied to events..
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Question about Events

Post by Jackolantern »

Your authenticate script needs a bit of updating:

Code: Select all

<?php
include_once 'connect.php';
session_start();

if (isset($_POST['submit']))
{
  $player=$_POST['player'];
  $password=$_POST['password'];
  $player=strip_tags($player);
  $password=strip_tags($password);
  $password=md5($password);

  //Removed the password segment from this query, as it is a SQL injection risk. 
  //It was also incorrect in the tutorial, and missed the "...and password = '$password'" 
  //...part which may have caused your error.

  $query = "select name,password from players where name='$player'";
  $result = mysql_query($query) or die("Could not query players");
  $result2 = mysql_fetch_array($result);
  if ($result2)
  { 
    //Adding password check here
    if ($password == $result2['password']) {
         $_SESSION['player']=$player;
    
         echo "<big>Logged in successfully<br>";
         echo "<A href='index.php'>Continue</a></big>";
         exit();
     } else {
         echo "<big>Wrong username or password.<A href='login.php'>Try Again</a></big>";
         exit();
     }
  }
  else
  {
   echo "<big>Wrong username or password.<A href='login.php'>Try Again</a></big>";
  }
}
?>
Also, ensure you have made your password field long enough to handle an MD5 password. MD5 requires a Char 32 field.

As for the events, you will need to explain exactly what you want to happen so that I can know the logic of it. When do events happen? Why do they happen? What data are they using? What occurs when they happen? I need a bit more than "an event encounter that instantly adds $#%!".
The indelible lord of tl;dr
Gheeda
Posts: 43
Joined: Fri Sep 03, 2010 12:12 am

Re: Question about Events

Post by Gheeda »

Place X is clickable. Clicking place X would result in one of two things. 1. A creature wants to kill you..you kill it...renders reward. or 2. You discover an item/person who wants to give you an item or stat bonus...no attacking, stats/reward given immediately...

Fixing my login stuffs now.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Question about Events

Post by Jackolantern »

If the chances are the same across the board, a simple rand() call will work. If it is different depending on the thing being clicked, the code I gave you above still works. Just store the fields with the chances (field names 1event, 2event, etc.) as part of the things being clicked (places, chests, caves, etc.). Just make sure you are pulling data from the right place (for example, change $monsterID to $placeID, and update the queries to pull from the correct fields you want to check in the "places" table), but all the logic is still the same.
The indelible lord of tl;dr
Gheeda
Posts: 43
Joined: Fri Sep 03, 2010 12:12 am

Re: Question about Events

Post by Gheeda »

Password fixes worked a treat..And I appreciate you pointing out the security risk in my coding since I do have intentions of publishing this project eventually.

Still fiddling with the code you supplied. The events happen but no data involving the player sticks. Surely ill have it working in no time. Thanks bunches for your help :)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Question about Events

Post by Jackolantern »

What do you mean "no data involving the player sticks"? Where is it failing? How is the data stored that you are needing? Maybe I can help.
The indelible lord of tl;dr
Gheeda
Posts: 43
Joined: Fri Sep 03, 2010 12:12 am

Re: Question about Events

Post by Gheeda »

It was echoing that the player was receiving stat increments but wasn't actually increasing the stats. That has since been fixed :)

But if you dont mind, you could look over my code and tell me where I went wrong.

I coded a simple highscore.php that (in a perfect world) would list all players (those capable of being in the runnings...just a simple 1 to a 'highscoreform' toggle on register does this) along with their level and their score. The score is simply Exp gained + level + gold.

Currently it lists only one character, and if it helps, the character isn't the logged in character viewing the list.

Code: Select all

<?php
include_once 'connect.php';
  session_start();
  include_once 'logo.php';
?>
 
  <link href="style.css" rel="stylesheet" type="text/css" />
<div id="login1" div align="center">

<?php
if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='login.php'>Login</a>";
  exit;
}
?>
</div>
<?php
$check = 1 ;
$charinfo="SELECT * from players where hs= 1";
      $charinfo2=mysql_query($charinfo) or die("Could not load player db.");
      $charinfo3=mysql_fetch_array($charinfo2) or die ("Could not array.");
      print "<table border='0' bordercolor='000000' bgcolor='#ffffff'>";
      print "<tr><td>Name<font color='ffffff'>________________</td><td>Level<font color='ffffff'>__</td><td>Score<font color='ffffff'>______</td></tr>";
      $score = $charinfo3['gold'] + $charinfo3['level'];
      $score2 = $score + $charinfo3['exper'];
      $name = $charinfo3['name'];
      $level = $charinfo3['level'];
      print "<tr><td>$name</td><td>$level</td><td>$score2</td></tr>";
      ?>
      <a href='index.php'>Go back to town</a>
edit: the only reason $check = 1 was in there was to see if results varied when switching the 1 in hs= 1
I have a feeling I'm not asking for the data properly.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Question about Events

Post by Jackolantern »

What is going wrong on your highscrore.php script? Is it not pulling up the one player it should be? No huge errors are jumping out at me, so I would need to know more about how it is not functioning properly.
The indelible lord of tl;dr
Post Reply

Return to “Tutorials”