Diablo II/III Style Random Loot System
Diablo II/III Style Random Loot System
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".
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Diablo II/III Style Random Loot System
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:
I have not tested this code, but you should be able to see the general idea we are using here. Hope this helps!
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...
The indelible lord of tl;dr
Re: Diablo II/III Style Random Loot System
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".