Scenario of Web Game. We have two connected buildings. The second one is something like upgrade but not exactly, never mind this is not important.
Every building have experience level and this level its dependеnt of produced materials from the building. When we produce materials we have calculation between levels of the buildings. Example: we produce 2000 gold, every building produce the gold like:
Building B level - Building A level x Produced Gold
notes:
Always Building B(Upgrade building) have bigger priority so its: level B - level A x Gold
Always the produced gold is the same on both buildings
$building_A = array(4=>2000);
$building_B = array(5=>2000);
Building A Level 4 -> 2000 Gold
Building B Level 5 -> 2000 Gold
Calculation is : 5 - 4 x 2000 gold = 1 x 2000
Variant 0.1: Its possible Base building to Level UP and second building to be the same level: Array:
$buildingA = array(4=>1000, 5=>1000);
$buildingB = array(5=>2000);
Building А
Building B:
Calculation:
5 - 4 x 1000 = 1 x 1000
5 - 5 x 1000 = 0 x 1000
Variant 0.2: Its possible and opposite situation: Array:
$buildingA = array(4=>2000);
$buildingB = array(4=>700,5=>1300);
Building A:
Building B:
Calculation:
5-4 x 700 = 1 x 700
6-4 x 1300 = 1 x 1300
Here is the code of variants 0.1 & 0.2
http://sandbox.onlinephpfunctions.com/code/ef0adce304554392266b8193b292051c0d98bae8
//v.01
$buildingA = array(4=>1000, 5=>1000);
$buildingB = array(5=>2000);
//v.02
//$buildingA = array(4=>2000);
//$buildingB = array(4=>700,5=>1300);
if(sizeof($buildingB) == 1)
{
foreach($buildingA as $level_a => $gold)
{
$level_b = array_keys($buildingB);
$level_b = $level_b[0];
$level_diff = $level_b - $level_a;
$tmp['calc'] = $level_b . ' - ' .$level_a.' = '.$level_diff . ' x ' . $gold;
$x[] = $tmp;
}
}
elseif(sizeof($buildingA) == 1)
{
foreach($buildingB as $level_b => $gold)
{
$level_a = array_keys($buildingA);
$level_a = $level_a[0];
$level_diff = $level_b - $level_a;
$tmp['calc'] = $level_b . ' - ' .$level_a.' = '.$level_diff . ' x ' . $gold;
$x[] = $tmp;
}
}
Everything is good to here, but have and turd variant when the both buildings increase his level. I make several code's for this variant but non of them looks and work good. Can somebody with more clear mind give some suggestion how easy to calculate difference.
v.1
$buildingA = array(4=>500, 5=>1500);
$buildingB = array(4=>1000, 5=>1000);
Calculation: ()
v.2
$buildingA = array(4=>1500, 5=>500);
$buildingB = array(4=>1000, 5=>1000);
Calculation: (B-A)
v.3
$buildingA = array(4=>1000, 5=>1000);
$buildingB = array(4=>500, 5=>1500);
Calculation: (B-A)
V.4
$buildingA = array(4=>1400, 5=>600);
$buildingB = array(5=>700, 6=>1300);
Calculation: (B-A)
V.5
$buildingA = array(1=>300, 2=>700, 3=>1000);
$buildingB = array(5=>500, 6=>1500);
Calculation: (B-A)
Lets try explan with v.3:
$buildingA = array(4=>1000, 5=>1000);
$buildingB = array(4=>500, 5=>1500);
Building A will produce 1000 gold in level 4 then level up and the rest is on level 5
Building B will produce 500 gold in level 4 then will level up and the rest is on level 5
So how we calculate:
Here i make some test script, but the code not look very well and its not work with v.04: http://sandbox.onlinephpfunctions.com/code/6f12fda4877ad781da52f908f3cba79aa0c69f33
I need to calculate how match gold will produce the buildings together when both level UP. I not see easy way to calc this. Any suggestions ?
I am out of ideas how to make that. its hard to explain it good, but i hope its writen clear - please do not judge me about that :)
One a genius colleague provided me easy mathematical solution:
function f ($x1, $x2)
{
foreach ($x2 as $k2 => $v2)
{
if ($v2 == 0)
continue;
foreach ($x1 as $k1 => $v1)
{
if ($v2 == 0 || $v1 == 0)
continue;
$m = ($v2 >= $v1) ? $v1 : $v2;
$v2 -= $m;
$v1 -= $m;
$x2[$k2] = $v2;
$x1[$k1] = $v1;
echo '(' . $k2 . ' - ' . $k1 . ') = ' . ($m) . '<br />';
}
}
}
f($buildingA, $buildingB);