Diablo II/III Style Random Loot System

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Verahta
Posts: 440
Joined: Wed Aug 24, 2011 1:50 am

Diablo II/III Style Random Loot System

Post by Verahta »

Is there a tutorial that shows how to create a random loot generator system like the ones in Diablo II and such games? I love how varied and different all the loot is in those games, especially how some are common, uncommon, and rare versions/combinations.
"In order to understand recursion, one must first understand recursion".
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Diablo II/III Style Random Loot System

Post by Jackolantern »

Its not that hard. You will have several master item tables (separated by function, such as a master weapon table, master armor table, etc.). These will hold the full details of each item when they are made. Then, when you want to generate a new item, you could so something like this:

Code: Select all

$itemChance = rand(1, 1000);

//a list of your stats, as they are listed as the column names in your master item table
$stats = array("strength", "intelligence", "endurance", "agility", "dexterity", "speed");

//a list of possible item types
$itemType = array("sword", "dagger", "club", "staff");

//a list of possible item names
$itemName = array("heretic", "monk's", "sparkling");

//a list of possible adjectives
$itemAdj = array("furious", "flawless", "bloodletting");

//rarest item generation
if ($itemChance > 950) {   

   //shuffle the stats so we can choose randomly, but will only add each one once
   shuffle($stats);
   //shuffle name elements
   shuffle($itemType);
   shuffle($itemName);
   shuffle($itemAdj);

   //create an array to hold the stat types we come up with
   $statsList = array();
   //create an array to hold the stat values we come up with
   $statValues = array();
   //create a variable to hold the damage value
   $damage = 0;
   //create the variable to hold the final name
   $finalName = "";

   //loop to create several stats to add, in this case 4 stats
   for ($x = 0; $x < 4; $x++) {
       //determine what stat to affect
       $statsList[$x] = $stats[$x]
       //create the value for that stat. The range in rand() will be the range of the stat value
       $statValues[$x] = rand(20, 30);

       //now create the name
       $finalName = $itemAdj[0]." ".$itemName[0]." ".$itemType;
       //upper-case the first letters of each word in the name
       $finalName = ucwords($finalName);

       //create random damage value
       $damage = rand(12, 24);

       //now add the new item to the database, making up a random name. 
       //not worrying about duplicate names, as Diablo II has duplicate items with differing stats.
       $sql = "INSERT INTO masterweapons (name, damage, $statsList[0], $statsList[1], $statsList[2], $statsList[3]) VALUES ('$finalName', $damage, $statValues[0], $statValues[1], $statValues[2], $statValues[3])";
       mysql_query($sql) or die("Could not create a new item");
} else if ($itemChance > 800) {
    //basically the same thing, but scaling back number of stats to add to item (if you want)
    //...and lowing the possible values of everything
   shuffle($stats);
   shuffle($itemType);
   shuffle($itemName);
   shuffle($itemAdj);

   $statsList = array();
   $statValues = array();
   $damage = 0;
   $finalName = "";

   //changed it to add only 3 stats, although in production code, 
   //...this would still be a pretty rare item
   for ($x = 0; $x < 3; $x++) {
       $statsList[$x] = $stats[$x]
       $statValues[$x] = rand(20, 30);

       $finalName = $itemAdj[0]." ".$itemName[0]." ".$itemType;
       $finalName = ucwords($finalName);
       
       //lower damage
       $damage = rand(8, 19);
       
       //remove the stat in the sql query that we aren't adding in this item insertion
       $sql = "INSERT INTO masterweapons (name, damage, $statsList[0], $statsList[1], $statsList[2]) VALUES ('$finalName', $damage, $statValues[0], $statValues[1], $statValues[2])";
       mysql_query($sql) or die("Could not create a new item");
} else if ($itemChance > 650) {
       //...and on until you you cover all chances...
I have not tested this code, but you should be able to see the general idea we are using here. Hope this helps!
The indelible lord of tl;dr
User avatar
Verahta
Posts: 440
Joined: Wed Aug 24, 2011 1:50 am

Re: Diablo II/III Style Random Loot System

Post by Verahta »

Very cool Jack, thank you. This helps a ton and points me in the right direction.
"In order to understand recursion, one must first understand recursion".
Post Reply

Return to “Advanced Help and Support”