Creating an items table

Today I tried to do a little work on my game but I got stuck on a table which I have been pondering about for a little while now but today it stopped me so I need to address it.

In the game I am working on, players will have items, and will be able to use these items on themselves or others. And these items must be able to have an equation that manipulates the users and the effected players stats. This equation should be able to manipulate multiple stats of each player. I will eventually want to add splash damage but let’s start out easy.

So, you have the user who we will call player 1 (p1) and the player that is going to be effected by this item; let’s call them player 2 (p2).

Based on this idea I have been able to think of two solutions, one which I ditched almost immediately . But I don’t know if the general idea behind this one is any good. So take a look and let me know what you think.

Items table

  • id – 1
  • name – bardic’s staff of unimaginable penetration
  • equation – eq{target:10*(p1.str+p1.dex) / p2.def-50}
  • target – 0 // 0 = enemy , 1 = allies, 2 = all

As you can see, I am storing the equation in a JSON like object and would simply do a search on the search and replace the bold variables with player 1’s and player 2′ actually stats.  This method I think would allow me to easily keep the equation dynamic and clean.

Now lets look at something like area effects (splash damage, group shields) and how I would go about store such an equation in the table. This example has 3 equations in the equation object but only would be be used at most. I would either use target and others or all but I put all three is just for example.

  • equation – eq{target:10*(p1.str+p1.dex) / p2.def-50, others:10*(p1.str+p1.dex) / p2.def-20, all: 10*(p1.str+p1.dex) / p2.def-20}

With this setup I can have multiple equations and multiple stats in either set up. I did have an option 2 which was to bust out all the stats into their own tables but the more I thought about it in comparison to this it just seemed clunky and overly complex for something that shouldn’t be that crazy.

Now, to prove to myself that I could actually do this I wrote some a simple script to do such a thing. Right now the script is messier than it needs to be but I wrote it kinda quick just to prove that I can do that I’m talking about here. I will eventually clean it up when I have some time.

But the gist of the script is it hide’s the p1.stats and replaces them with actual numbers. This would naturally all come from a database but for the example I just have everything there.

I’d like to apologize in advanced for the lack of formating. I’ve tried three different highlighters and they break things worse than how they appear here. So till I find a better… sorry >.<

$eq = 10*5 / p1.str + p1.dex / p1.chr;


function getHolders($str)
$statArr = array();
$pos = 0;
$i = 0;
$len = strlen($str);
$i != strlen($str))
$pos = stripos($str,"p1",$pos);
$pos += 6;
$i = $len;

function createStatArr($statArr)
$i = 0; $i < count($statArr);$i++)
$statArr[$i] = array("str"=>10);
$statArr[$i] = array("dex"=>7);
$statArr[$i] = array("chr"=>2);

return $statArr;

function replaceHoldersWithVals($statArr, $str)
$pos = 0;
$len = strlen($str);
$i = 0; $i < count($statArr);$i++)
$str = str_ireplace("p1.".key($statArr[$i]),$statArr[$i][key($statArr[$i])],$str);
"\$e = $str;");


  1. I was working with a company last year where we did something very similar, we stored equations and parsed them on the fly with regex, replacing the variables with the appropriate values.

  2. Why can you not simply use a general equation and then store a modifier in the items table that will be used to give the different items their power.

    For example:

    base equation could be: 10 * (p1.str + p1.dex) / p2.def

    Then for the different items you would have modifiers like:

    Gun = 0.8
    Nuclear Bomb = 10.0

    Then multiply the base equation by the modifier.

    I don't know what other equations you have in mind but I think it would be better to keep the logic out of the database and in the PHP.

  3. Why would you not want to have equations in the db @Rhinos?

    My logic is that if I put the equations in the db I can easily make a system to handle 'dynamic' equations which allows me to create admin tools that I can hand of to my friends that aren't coders but helping with the game.

    Otherwise I could either have to a large structure in my code to handle each item individually or have a few template equations they need to fit into.

    Or so I think lol. I could be utterly wrong. I am more than welcome to other idea's though 🙂

  4. I think first you need to come up with the equations for the different items to see if that is what you really need in your system. You don't want to make it more complicated than it has to be.

    In the admin tools that your friends would use it would be easier for them just to think of a modifier number that will determine the strength/skill of the item rather than using new or existing equations.

    I'm not saying that your idea is wrong, but I think you need to think "Is it worth the time and effort developing this idea" if all the items will just have the same equations with only slight +/- modifications that could have been done by just using a modifier number.

    Let me know what you think.

  5. Since I've made this post I've been pondering if this was the best solution for what I'm trying to achieve. I do think this is the best path for my system though. It allows me to avoid several problems I've seen with items in games and gives me a good solution to my next game which would need such a solution more.

    Thanks for your comments. Like I said, I do see your point, that is solution is a bit complex for items if all weapons were to share a few base equations. You have also given me an idea for a new post, which is one of the reasons I have chosen the direction I'm going with this 🙂

  6. Hello fellow PBBG developer 🙂

    I'm just initiating myself in the browser game dev field and unfortunately there isn't too much info about this kind of development around. But I'm glad I have found at least another one.

    Well, I'm developing the battle system of my game right now, and reading your posts had shed light on some of the issues I have been pondering on (lots of pondering because there is not much info on this, as I said and you have to come up with everything by yourself). For example, I'm trying to make a game were cooperation can be done, for questing and combat. I'm trying to design a weapon system too, easy on designers (so other people can help without knowing about programming).

    I'm using Ruby & Rails for development, and would like to recommend you to take a look into it. I was a PHP developer for several years… but I'm not going back. And you won't either, once you get the gist of Rails. 🙂

    So, even if you aren't convinced, or don't feel like it, or think you don't have time, or maybe feel lazy because you believe you will lose a lot of time restarting all the work you have already done… do it, it IS worth it. It is THAT good. 😀

    You can get so much work out of the way with Rails, and you get a lot of goodies. If you want code samples, or have questions about it (anything) you can ask, I'll be glad to help. I'm not an expert (yet), but trying to get there.

    I'll be around, there is a lot of things I'd like to discuss with someone that's into PBBG dev. Mostly design ones.

    By the way, for the syntax highlighter I recommend:
    And you use it like this:

  7. How does your method handle weapons that don't do direct damage? A net slows the target and makes certain weapons ineffective? A pot of glue that sticks the target to a particular square?

    Why aren't you doing this with objects? Your database table for the item should have a "type" field. You use that type to instantiate a PHP class instance. The class has a function called "use". Thus all melee weapons use the same formula with the coefficients taken from the database when the object is loaded. "Special" melee weapons would have a different type (probably a subtype of the MeleeWeapon class which only varies in its use() method). So after you load the object you call the item's use function:

    $player.inventory[$currentitem].use($player, $target, $location)

    The benefits are no calls to eval (which are slow and cannot be cached by the interpreter), no impossible to trace errors because of typos in the database, ease of maintenance and new object creation. use() could use $location to handle splash damage.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.