1. ## Gem calculation formulas.

I've gotten a few requests for how the wiki calculators work, so I figured I'd post it here.

Gem expenditures are based on a series of plateaus. At each plateau the number of resources per gem increases. Between plateaus the number of resources per gem is constant.

This means that the gem calculations are based on the slope between the plateaus.

For example the plateaus for gold/elixir are:

100 - 1 gem
1000 - 5 gems
10000 - 25 gems
100000 - 125 gems
1000000 - 600 gems
10000000 - 3000 gems

So let's say you wanted to buy 5000 gold or elixir. You look at the plateaus and see that 5000 falls between the 1000 and 10000 resource plateaus.

We know the first 1000 resources costs 5 gems. The calculations for the rest are based on the slope of the line between the 1000 and 10000 gem resource plateaus.

That yields the following formula:

(5000 - 1000)/((10000 - 1000)/(25 - 5)) + 5
= 4000/(9000/20) + 5
= 4000/450 + 5
~14 gems

There are two corner cases; the gem amounts for resources lower than the first plateau and gem amounts for resources greater than the max you can store. These are easily handled. For the first case, any resources less than 100 will also cost 1 gem. For the second case you simply make multiple purchases until you reach the desired amount.

Dark Elixir works similarly, but costs 100 times as much.

1 - 1 gem
10 - 5 gems
100 - 25 gems
1000 - 125 gems
10000 - 600 gems
100000 - 3000 gems

Other than the cost, the one difference is that the max plateau is lower than the max you can store. In this case the calculations just continue using the slope for the highest range.

For example if you wanted to buy 150,000 DE, that would yield the following formula:

(150000 - 10000)/((100000 - 10000)/(3000-600)) + 600
= 140000/(90000/2400) + 600
= 140000/37.5 + 600
~4333 gems

Alternatively you could calculate it this way:
(150000 - 100000)/((100000 - 10000)/(3000-600)) + 3000
= 50000/37.5 + 3000
~4333 gems

Here are the plateaus for time:

60 seconds (1 min) - 1 gem
3600 seconds (1 hour) - 20 gems
86400 seconds (1 day) - 260 gems
604800 seconds (1 week) - 1000 gems

The one difference is that gem costs for time are always rounded down. So if you start an upgrade for a level 11 archer tower which takes 7 days and wait one second the gem cost will go from 1000 to 999 immediately.

The following is the javascript I use on the wiki for the resource cost calculations:

Code:
```function doCalcResourceToGems (resources) {
var ranges = [100,1000,10000,100000,1000000,10000000];
var gems = [1,5,25,125,600,3000];
var storagemax = 8001000;

if (isNaN(resources)) return("???");

if (resources < 0) return("???");
else if (resources == 0) return(0);
else if (resources <= ranges) return(gems);

for (var i = 1; i < ranges.length-1; i++)
if (resources <= ranges[i])
return(Math.round((resources - ranges[i-1])/((ranges[i] - ranges[i-1])/(gems[i] - gems[i-1])) + gems[i-1]));

if (resources <= storagemax)
return(Math.round((resources - ranges[ranges.length-2])/((ranges[ranges.length-1] - ranges[ranges.length-2])/(gems[gems.length-1] - gems[gems.length-2])) + gems[gems.length-2]));
else
return(doCalcResourceToGems(resources % storagemax) + Math.floor(resources/storagemax)*doCalcResourceToGems(storagemax));

return("???");
}```  Reply With Quote

2. Thanks for sharing this.  Reply With Quote

3. Very cool. Did you get the info from the game files? observation? Or did someone from SC share it at some point?

~Jeff  Reply With Quote

4. Originally Posted by GameShaman Very cool. Did you get the info from the game files? observation? Or did someone from SC share it at some point?

~Jeff
The plateaus are from the in game files. The formulas were worked out via observation. SC seldom shares anything specific about internal numbers.  Reply With Quote

5. thanks for this, been looking everywhere for the time formula   Reply With Quote

6. If you plot those plateaus you get these nice graphs (attached below for DE and Time) and a VERY good formula for calculating Gem cost.
Gem = 1,0098*DE^0,6949 (R²=1, perfect)
Gem = 0,0443*Time^0,7558 (R²=0,9995)  Reply With Quote

7. Great work!!!! Awesome!!

Thanks for sharing!  Reply With Quote

8. This makes me feel so stupid.

Nice job!  Reply With Quote

9. This is my java code, just var-->type conversions from your code.

Code:
```public static double doCalcResourceToGems(double resources) {
double[] ranges ={100, 1000, 10000, 100000, 1000000, 10000000};
double[] gems ={1, 5, 25, 125, 600, 3000};
double storagemax = 8001000;

if (resources <= 0) return (0);
else if (resources <= ranges) return (gems);

for (int i = 1; i < ranges.length - 1; i++)
if (resources <= ranges[i])
return (Math.round((resources - ranges[i - 1]) / ((ranges[i] - ranges[i - 1]) / (gems[i] - gems[i - 1])) + gems[i - 1]));

if (resources <= storagemax){
System.out.println("2"+ranges.length);
return (Math.round((resources - ranges[ranges.length - 2]) / ((ranges[ranges.length - 1] - ranges[ranges.length - 2]) / (gems[gems.length - 1] - gems[gems.length - 2])) + gems[gems.length - 2]));}
else{
System.out.println("3");
return(doCalcResourceToGems(resources % storagemax) + Math.floor(resources/storagemax)*doCalcResourceToGems(storagemax));}

}```
But now when condition resource greater than storagemax, my results an the COC.wikia results doesn't match
any idea??

i made some mistakes in conversion now they work fine  Reply With Quote

10. ## DarkElixir and Time max elements

storagemax for darkelixir =??
and is there any max for time, if not what we have to do ??

and in game files where did you found the plateaus, so we can access them if any changes occured in future  Reply With Quote