d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > [php] Need An Idea Of Item Generation ;d
Add Reply New Topic New Poll
Member
Posts: 5,193
Joined: Mar 13 2008
Gold: 1.00
Dec 21 2016 05:08pm
I'm not really pro in php, however, sometimes i like to do small projects.
Currently im trying to program random item generation and i have no idea how to do so.
I want all items to have armor value and some of stats.
Rarity of item may be just rng like green (uncommon), blue (rare), yellow (legendary).
Some of mods i want to be prefixes, some i want to be suffixes which, green item (2 mods) cant roll 2 prefix or 2 suffix.
Mostly the problem is, how to construct database, and im looking for an ideas ;D
Maybe there is any opensource script that i can look for and learn from it ? :D

Regards, Ashcan.

P.s. Any examples would be nice ;D

This post was edited by Ashcan on Dec 21 2016 05:08pm
Member
Posts: 6,874
Joined: Jul 10 2008
Gold: 3.00
Dec 22 2016 04:00am
Unlikely you will find an OS solution for that.

You generally have 2 parts of your project:

1) Database for storage of generated items + structure of the item entity
2) Script to generate random items based on given logic (luck in this case)

1. If you decide to use Relational DB (MySQL), the structure could be like:

item:
-id - PK
-rarity (enum: 1-green, 2-blue, 3-yellow) //although i suggest you stick to the blizzard colors, they are much intuitive for most gamers nowadays
-quality (int 2) //0-20
-generated_at (datetime)

item_property:
-item_id (foreign key pointing to item.id)
-property_id (foreign key pointing to property.id)

property:
-id - PK
-name (string) // example: artisan's
-attribute_id (foreign key pointing to attribute.id) //ex. strength
-value (integer) // example: 10
-type: (smallint) // 1-prefix, 2-suffix, 3-base or whatever you want it to be

attribute:
-id - PK
-name (string) //example: strength

Note this is very very basic structure, you can introduce skills, character-based properties, set items etc etc.. but that is a topic of another discussion.

2. You make the script to roll a random rarity, then create an item based on that. The logic here again can be very simple and very complicated (for example to roll for specific character class specific properties only).

The code itself is different based on whether you want to use a framework or not. I personally havent coded pure php in a while, and find it ugly right now. It is much better to define entities and hydrate them with random data.

For the logic itself:
1) $rarity = rand(1,100); //generate random number
2) check $rarity // 1-60 - normal, 60-90 -rare, 90-100 -legendary (or any other way you decide)
3) based on rarity (from item entity in the DB) you generate the accurate prefix/suffix mode and depending on whether you want to have always one suffix / one prefix or can be random you populate the data. You can get random skill from the skills entity and assign to it random value, depending on your requirements again.

;)


Member
Posts: 5,193
Joined: Mar 13 2008
Gold: 1.00
Dec 22 2016 04:04am
Quote (netwarrior @ 22 Dec 2016 12:00)


3) based on rarity (from item entity in the DB) you generate the accurate prefix/suffix mode and depending on whether you want to have always one suffix / one prefix or can be random you populate the data. You can get random skill from the skills entity and assign to it random value, depending on your requirements again.



Can you explain this a bit more?
Like any pseudo code would be good ;D



I had an idea to store prefixes and suffixes in 2 tables among with stats and rolls + something like "quality of item required" so i could add higher rolls of mods depending on quality of item ;D
Member
Posts: 6,874
Joined: Jul 10 2008
Gold: 3.00
Dec 22 2016 06:33am
Quote (Ashcan @ Dec 22 2016 01:04pm)
Can you explain this a bit more?
Like any pseudo code would be good ;D



I had an idea to store prefixes and suffixes in 2 tables among with stats and rolls + something like "quality of item required" so i could add higher rolls of mods depending on quality of item ;D


Well, no I can't write the code for you because I am not sure what tools you will be using to build it with, and I don't feel like writing pure php right now for sake of validation / accuracy.

The idea is:

After you get the rarity, you get for example rare, this means rare item can have for example 3 rolls, you take ALL prefixes available, ALL suffixes available and put them all in one array, randomize it and take the first 3 elements (if you dont want to have at least one of each). Then random roll their properties and insert them into the database.
Member
Posts: 3,197
Joined: May 4 2013
Gold: 1,457.00
Dec 23 2016 08:51am
Something i found recently: https://github.com/v03/rpg Here is almost complete example of item generation where items were taken from Diablo II and there is working dumb irc bot to "give" people random generated items

Not in php obviously. Clone the repo, bundle install, ruby items.rb

output will be something like


Quote
You found Shocking Charm of Perfection. But it requires a higher level (70) than you have!
You found Russet Charm of Atlas. But it requires a higher level (66) than you have!
You found Foul Ring of Ox. But it requires a higher level (6) than you have!
You found Russet Spiked Shield of Giant. But it is too heavy for you! You toss it aside.
You found Iron Chain Boots of Squid (dmg: 0 | def: 9) with magic enchanting: 30 Attack Rating | 66 Vitality and equip it immediately.


Only has magic items and only of "normal" quality (no exceptional/elite versions) but is fairly complete otherwise and its easy to add more.

If you want to use it as irc bot you need to edit app.rb

Also good chance for you to stop using piece of shit called php and use real language :)

This post was edited by nuvo on Dec 23 2016 08:54am
Member
Posts: 5,193
Joined: Mar 13 2008
Gold: 1.00
Dec 23 2016 06:43pm
Quote (nuvo @ 23 Dec 2016 16:51)
Something i found recently: https://github.com/v03/rpg Here is almost complete example of item generation where items were taken from Diablo II and there is working dumb irc bot to "give" people random generated items

Not in php obviously. Clone the repo, bundle install, ruby items.rb

output will be something like




Only has magic items and only of "normal" quality (no exceptional/elite versions) but is fairly complete otherwise and its easy to add more.

If you want to use it as irc bot you need to edit app.rb

Also good chance for you to stop using piece of shit called php and use real language :)


Yeah example is nice, but i guess im still kinda lost with how to create table in mysql properly ;D
I'm writing plugin/mod for csgo (sourcemod) and im making random item drops in game with sending request to site to create it, among with web interface to manage that stuff ;D (thats why i use php)

So yeah, problem with prefix/suffix persists since if i do it that way, i wont be able to create tier list based on item quality, or at least i have no idea how to do it ^^

This post was edited by Ashcan on Dec 23 2016 06:49pm
Member
Posts: 29,723
Joined: Jun 11 2007
Gold: 279.52
Dec 25 2016 12:15pm
heres a quick idea for the code (not using a db atm), i would suggest expand on it and use objects.

Code
<h2>item gen 1.0</h2>

<?php

//random attributes

$str = "str = ".rand(1,20);
$dex = "dex = ".rand(1,20);
$life = "life = ".rand(5,15);
$mana = "mana = ".rand(10,25);
$res = "res = ".rand(1,5);
$damage = "dmg = ".rand(10,50);


$stat_array = array($str, $dex, $life, $mana, $res, $damage);



$item = '';
$rarity = rand(1,1000);

echo "item level = ".$rarity . "<br/>";


if($rarity <= 549){
$itemrarity = 'common';
$itemstat = array_rand($stat_array,1);
echo $itemrarity . "<br/>";
echo $stat_array[$itemstat] . "<br/>";
}

if($rarity >= 550 && $rarity < 849){
$itemrarity = 'uncommon';
$itemstat = array_rand($stat_array,2);
echo $itemrarity . "<br/>";
echo $stat_array[$itemstat[0]] . "<br/>";
echo $stat_array[$itemstat[1]] . "<br/>";
}

if($rarity >= 850 && $rarity < 975){
$itemrarity = 'rare';
$itemstat = array_rand($stat_array,3);
echo $itemrarity . "<br/>";
echo $stat_array[$itemstat[0]] . "<br/>";
echo $stat_array[$itemstat[1]] . "<br/>";
echo $stat_array[$itemstat[2]] . "<br/>";
}

if($rarity > 975){
$itemrarity = 'legendary';
$itemstat = array_rand($stat_array,4);
echo $itemrarity . "<br/>";
echo $stat_array[$itemstat[0]] . "<br/>";
echo $stat_array[$itemstat[1]] . "<br/>";
echo $stat_array[$itemstat[2]] . "<br/>";
echo $stat_array[$itemstat[3]] . "<br/>";

}

?>



for a db, it should be pretty simple


youll need the following tables:

id
item name
item rarity
item level (optional )
all the stats you plan to use seperately (so it you want strength and dex and life, then you will need to make a column for each one, this will need to be an integer so you can store the value of said item

instead of using eachos like im doing, you would he using an insert of that value into the associated column

Member
Posts: 5,193
Joined: Mar 13 2008
Gold: 1.00
Dec 26 2016 08:16pm
I actually did it but
about that:
Code
array_rand

xDDD
Need to rewrite my code now cuz earlier it was like:
Code
$rows = mysqli_fetch_assoc($conn->query("SELECT * FROM mods WHERE prefix=1"));
$rng = rand(1,count($rows));
$mods = array();
foreach($rows as $row){
$mods[] = $row['id'];
}
echo $mods[$rng];


That function (which i didnt know exist) kinda helps xD
Member
Posts: 5,167
Joined: Nov 23 2006
Gold: 11.01
Jan 23 2017 11:13am
Ideally you would have an "Item" interface which a class such as "GenerateItem" would implement. This "GenerateItem.php" would then implement the getter/setters for the item (since all items will have the same attributes). You could then have EpicItem.php that inherits from GenerateItem.php which would define the attributes of an "Epic item" ie: generating it's strength, armor, etc and then returns the item.

That way your parent class (GenerateItem.php) can just have a function that determines what tyep of item to generate and call that specific item's class which will define/return an item and anything above that class doesn't care about the implementation of what defines an "Epic" item.

Your classes could be as simple as something like this:

Code
class EpicItem extends Item {

public function __construct($item) {
$item->armor = $this->setArmor();
$item->rarity = 'Epic';
}

private function setArmor() {
//Use whatever you want to determine an appropriate armor value for an "Epic" item.
return rand(70, 100) * 1.3; //Generates a random int between 70-100 and then multiples it by the "Epic multiplier"
}
} //end EpicItem class.


There are so so so many ways to do this in a clean fashion to where you just say "Give me an item" it will be able to generate an item without needing to know about how those items value's are determined.

This post was edited by PuppyMonkeyBaby on Jan 23 2017 11:29am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll