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;
replaceHoldersWithVals(createStatArr(getHolders($eq)),$eq);
function getHolders($str)
{
$statArr = array();
$pos = 0;
$i = 0;
$len = strlen($str);
while($i != strlen($str))
{
if(stripos($str,"p1",$pos))
{
$pos = stripos($str,"p1",$pos);
array_push($statArr,substr($str,$pos+3,3));
$pos += 6;
}else{
$i = $len;
}
}
return $statArr;
}
function createStatArr($statArr)
{
for($i = 0; $i < count($statArr);$i++)
{
switch($statArr[$i])
{
case "str":
{
$statArr[$i] = array("str"=>10);
break;
}
case "dex":
{
$statArr[$i] = array("dex"=>7);
break;
}
case "chr":
{
$statArr[$i] = array("chr"=>2);
break;
}
}
}
return $statArr;
}
function replaceHoldersWithVals($statArr, $str)
{
$pos = 0;
$len = strlen($str);
for($i = 0; $i < count($statArr);$i++)
{
$str = str_ireplace("p1.".key($statArr[$i]),$statArr[$i][key($statArr[$i])],$str);
}
eval("\$e = $str;");
echo $e;
}