Skip to content

Commit ba7a196

Browse files
Merge pull request #399 from SWG-Source/fix/randbell-deviation-clamp
Clamp bell curve rolls to three deviations
2 parents b7eb755 + ed15ffc commit ba7a196

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

sku.0/sys.server/compiled/game/script/library/space_crafting.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package script.library;
22

33
import script.*;
4+
import script.library.utils;
45

56
import java.util.Vector;
67

@@ -72,27 +73,47 @@ public space_crafting()
7273
public static final String STARSHIP_DROID_TABLE = "datatables/space_combat/ship_droid_assignments.iff";
7374
public static final String SHIP_COMPONENT_TABLE = "datatables/space/ship_components.iff";
7475
public static final String SHIP_WEAPON_TABLE = "datatables/ship/components/weapon.iff";
76+
7577
public static double randBell(double avg, double var)
7678
{
79+
80+
//This looks like the Marsagila polar form of a Box-Muller
81+
//Distributes a value over a bell curve, giving a result in standard deviations
82+
//Needs to be clamped to avoid insane values
83+
84+
7785
var = var / 2;
7886
double r, v1, v2;
87+
88+
7989
do
8090
{
8191
v1 = 2.0 * rand() - 1.0;
8292
v2 = 2.0 * rand() - 1.0;
8393
r = v1 * v1 + v2 * v2;
8494
} while (r >= 1.0 || r == 0.0);
95+
96+
8597
double fac = Math.sqrt(-2.0 * StrictMath.log(r) / r);
86-
double value = (avg + (v1 * fac) * (avg * var));
87-
if (value < 0)
88-
{
89-
return 0;
90-
}
91-
else
92-
{
93-
return value;
94-
}
98+
99+
double deviations = (v1 * fac);
100+
101+
102+
//Now we are going to clamp to +/- 3 standard deviations
103+
104+
deviations = utils.clamp(deviations, -3.0, 3.0);
105+
106+
double value = (avg + deviations * (avg * var));
107+
if (value < 0)
108+
{
109+
return 0;
110+
}
111+
else
112+
{
113+
return value;
114+
}
95115
}
116+
96117
public static float getBellValue(float fltValue, float fltModifier)
97118
{
98119
return (float)(randBell(fltValue, fltModifier));

sku.0/sys.server/compiled/game/script/library/utils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,21 @@ public static attrib_mod[] addMindAttribToStim(int power) throws InterruptedExce
117117
}
118118
return am;
119119
}
120+
121+
public static double clamp(double value, double min, double max)
122+
{
123+
if (value < min)
124+
{
125+
return min;
126+
}
127+
else if (value > max)
128+
{
129+
return max;
130+
}
131+
132+
return value;
133+
}
134+
120135
public static int randix(float[] fArray) throws InterruptedException
121136
{
122137
if (fArray.length > 1)

0 commit comments

Comments
 (0)