I'm a really beginning coder who wants to port Dungeons & Dragons OGL mechanics to my Unity3D project.
Now, I want to have modifiers for basic Character Attributes in C#.
These are based around number 10/11, which are the "middle" results on 20-sided dice.
Base Characters Attributes are for example Strength, Intelligence, Charisma etc.
The higher your stats are above 10, the higher stat modifier you get. The lower from 10, the lower your modifier (negative one).
So, for 10 and 11 you have +0 (no modifer). Now, for:
12, 13 -> you get +1
14, 15 = +2
16, 17 = +3
18, 19 = +4
20, 21 = +5
22, 22 = +6 and so on (Character Attributes can go up to 40 and as low as 0 I guees)
For attributes below average, so below 10:
8, 9 = -1
6, 7 = -2
4, 5 = -3
2, 3 = -4
0, 1 = -5
Now, I could hardcode it on ifs or like one folk, on switches. This is part of his code:
private string DetermineModNum(string value)
{
int score; //convert to int.
TryParse(value, NumberStyles.Integer, null, out score);
if (score > 10)
{
if (score % 2 == 0) //can be divided by 2
{
switch (score)
{
case 12:
return "1";
case 14:
return "2";
case 16:
return "3";
case 18:
return "4";
case 20:
return "5";
case 22:
return "6";
case 24:
return "7";
case 26:
return "8";
case 28:
return "9";
case 30:
return "10";
}
}
else
{
switch (score)
{
case 11:
return "0";
case 13:
return "1";
case 15:
return "2";
case 17:
return "3";
case 19:
return "4";
}
}
}
else
{
if (score % 2 == 0) //can be divided by 2
{
switch (score)
{
case 10:
return "0";
case 8:
return "-1";
}
}
else
{
switch (score)
{
case 9:
return "-1";
case 7:
return "-2";
}
}
}
return null;
}
This is incomplete and just part of his character generator. But the other way to do it would be probably to divide the Attribute by 4. So instead of writing it all, we could divide our stats by 4.
For example:
Strength: 7 (modifier would be 7 / 4 = 1,75, so we should round it up to 2 and change it to -2)
Intelligence: 18 (modfier would be 18 / 4 = 4,5, so that's just int 4)
Which approach would be faster in a Unity3D game?
Also, if second solution is better, could you help me showing how to construct it best in C#? I'm just learning, could really use some good example here. I need modifiers below 10 to be - and above to be +, with a range of attributes at about 0 - 40 at least (will check max Attribute in SRD soon).
I will need a lot of modifiers based on different things. Some will be easy and regular like above, some have other patterns of modifiers, like every 2nd level, but some other things, like class abilities, will have custom, but sometimes still repetitive patterns.
All mechanics must be as fast as possible because during combat all is calculated real-time and I want to have best performance possible.
Integer arithmetic is very fast on modern CPUs. On the other hand conditional jumps can empty the execution pipeline and slow down the execution. So doing math can possibly be faster than a lengthy switch statement. Math will also be much more concise and less error prone than 2 screen full of case statements.
But I guess that you will barely notice any difference between the different approaches. And don't forget: premature optimization is the root of all evil.
So
int modNum = (score / 2) - 5;
is clearly to be preferred. Note that here an integer division is performed, which automatically truncates the result. E.g. 13 / 2 ==> 6
.
short
's (16-bit), int
's (32-bit) and long
s (64-bit) all store integer, i.e. whole numbers having no fractional part.