# Math¶

Supporting math functions.

```procedure LoadCoSineArrays;
```

Load default sin/cos values. Used internally for SRL

Note

by Mutant Squirrle

Example:

## CreateTPAFromText¶

```function CreateTPAFromText(Txt : string; Chars : string) : TPointArray;
```

Returns the TPointArray of the inputted Text.

Note

by MastaRaymond

Example:

## RBoolEx¶

```function RBoolEx(chance: integer): boolean;
```

Returns a boolean, with a chance of 1 / chance

Note

by Rasta Magician

Example:

## RBool¶

```function RBool: boolean;
```

Returns a boolean, with a 50% chance

Note

by Rasta Magician

Example:

```function FixRad(rad: Extended): Extended;
```

Returns the periodic rad in the range (0,2PI].

Note

By Nava2

Example:

## AngleBetween¶

```function AngleBetween(v1, v2: TVector): Extended;
```

Calculates the angle between two TVector records. This is returned in radians (normalized between (0, 2Pi]).

Note

Example:

## Normalize¶

```procedure Normalize(var vect: TVector);
```

Converts a TVector to normal TVector (|v| = 1).

Note

Example:

## CreateVector¶

```function CreateVector(Origin, vPoint: TPoint; NormalizeV: Boolean): TVector;
```

Creates a TVector object. If NormalzseV is true, will set |v| = 1.

Note

Example:

## GetSplitPt¶

```function GetSplinePt(Points: TPointArray; Theta: Extended): TPoint;
```

Returns the point on a spline, defined by control points Points, at Theta

Note

by BenLand100

Example:

## srl_GaussRand¶

This is a functional pointer. It behaves transparently as a regular function would. This function will behave as so: it will return a random Extended normally distributed with a mean of 0 and a standard deviation of 1.

If you need more flexibility in this (ex. mean of 2), please use GaussRandom which converts between the Standard Normal and a custom version.

Note

by: SRL Development Team

Example:

## n2_gauss¶

```function n2_gauss(): Extended;
```

Calculates a standard normal value (N(0, 1)).

By definition, 68.2% of points will lie within +/- 1*sigma, 95.4% within +/- 2.

Note

by: Nava2

## GaussMarsPolar¶

```function GaussMarsPolar(): Extended;
```

Calculates a standard normal value (N(0, 1]).

By definition, 68.2% of points will lie within +/- 1*sigma, 95.4% within +/- 2.

Note

Please use srl_GaussRand instead of this function directly, feel free to set srl_GaussRand to this function if you choose. Currently, this is the default.

Note

by: BenLand100

## SetupGauss¶

```procedure setupGauss();
```

Sets up the necessary constants for the gaussian functions used in the Gaussian math functions internally to SRL.

Note

by: SRL Development Team

## GaussRand¶

..code-block:: pascal

function GaussRand(mean, dev: Extended): Extended;

Calculates a normally distributed point around mean (u) with a standard deviation of dev (o).

..note:

```by: Nava2
```

Example:

..code-block:: pascal

// shooting arrows simulation: pt := Point(GaussRand(center.x, 0.5), GaussRand(center.y, 0.5)); WriteLn(Format(‘Hit the target at (%d, %d).’, [pt.x, pt.y]));

## Gauss_Point¶

```procedure Gauss_Point(cX, cY: Integer; sAngle, eAngle: Extended; maxRad: Integer; out oX, oY: Integer);
```

Calculates a point about cX, cY with radius maxRad. The sangle and eangle are in radians. The point is returned in the reference parameters oX, oY.

Note

by: Nava2

Example:

```var
gX, gY: Integer;
// get a gaussian pt in a circle:
Gauss_Point(MMCX, MMCY, 0, math_2PI, MMX2-MMCX, gX, gY);
WriteLn(Format("gX, gY -> %d, %d", gX, gY);
```

## GaussRange¶

```function GaussRange(minI, maxI: Extended): Extended;
```

Returns a number between minI and maxI that is normally distributed in the range.

Note

by: Nava2

Example:

```randMean := GaussRange(0, 20);
```

## iGaussRange¶

```function iGaussRange(minI, maxI: Integer): Integer;
```

See GaussRange for more detail. Same function, but with integers.

Note

by: Nava2

## GaussRandom¶

```function GaussRandom(maxExt: Integer): Integer;
```

Computes a normally distributed number in [0, maxExt). Thus, it behaves like Random() would be expected to. But normally distributed about maxExt/2.

It is a wrapper for iGaussRange.

Note

by Nava2

Example:

## GaussBox¶

```procedure GaussBox(out px, py: Integer; x1, y1, x2, y2: Integer);
```

Calculates a point normally distributed from the center of the passed box. It checks for outlying points and will regenerate points if necessary.

Note

by: Nava2

Example:

```var
gX, gY: Integer;

// get a gaussian pt in a circle:
Gauss_Box(gX, gY, MMX1, MMY1, MMX2, MMY2);
WriteLn(Format("gX, gY -> %d, %d", gX, gY);
```

## MakeSplinePath¶

```function MakeSplinePath(Points: TPointArray; ThetaInc: Extended): TPointArray;
```

Returns a spline, defined by control points Points, incrementing theta by ThetaInc

Note

by BenLand100

Example:

## MidPoints¶

```function MidPoints(Path: TPointArray; MaxDist: Integer): TPointArray;
```

Adds midpoints to Path so no distance on it is greater than MaxDist

Note

by BenLand100

Example:

## InAbstractBox¶

```function InAbstractBox(x1, y1, x2, y2, x3, y3, x4, y4: Integer; x, y: Integer): Boolean;
```

Returns true if point x, y is in an abstract box defined by x1, y1, x2, y2, x3, y3, x4, y4.

``` x1, y1     x2, y2
+--------+
\       /
\     /
+--+
x4, y4   x3, y3
```

Note

by BenLand100

Example:

## Sine¶

```function Sine(degrees: Integer): Extended;
```

Return the sin of degrees.

Note

by Mutant Squirrle

Example:

```if (sine(0) = 0) then
```

## Cose¶

```function Cose(degrees: Integer): Extended;
```

Return the cos of degrees.

Note

by Mutant Squirrle

Example:

```if (Cose(0) = 1) then
```

## GridCoords16Ex¶

```Function GridCoords16Ex(Xmod, Ymod: integer; Grid16: TPoint): TPoint;
```

Using a grid system based off MiniMap size to return grid position on MainScreen. This is used in MMtoMS as well as other Grid functions. Why the number 16? Original grid is based off a tile, and this splits one tile into a 4x4 hence 16.

Note

by Narcle

Example:

```Example: see MMtoMSEx
```

## GetGridAbstractBox¶

```Function GetGridAbstractBox(Grid: TPoint): TPointArray;
```

Returns TPointArray of points for grid tile to use InAbstractBox in GetGridPos

Note

by Narcle

Example:

## GetGridPos¶

```function GetGridPos(MS: TPoint): TPoint;
```

Given a MainScreen point it’ll return the GridTile position the point is in. If it fails result is Point(-100, -100), because it can return negative grid points.

Note

by Narcle

Example:

## GridCoords¶

```function GridCoords(GridPoint: TPoint): TPoint;
```

Returns grid point Main Screen position. (middle of tile)

Note

by Narcle

Example:

## MMToMSEx¶

```Function MMtoMSEx(Xmod, Ymod: integer; MM: TPoint): TPoint;
```

Turns a Minimap point into a close MS point. New one compensates for angles. Xmod and Ymod change the starting point on MainScreen. Ymod = -30 would raise the points on screen by 30 pixels. This is good if you know the Height of the npc or object your looking for on MS. Formula in GridCoords16Ex now.

Note

by Narcle

Example:

```FindTreeOnMinimap(x, y);
msPoint := MMToMSEx(0, -20, Point(x,y));
MMouse(msPoint.x, msPoint.y, 0, 0);
//...
```

## MMToMS¶

```function MMToMS(MM: TPoint): TPoint;
```

Refer to MMtoMSEx - Ymod shift required to be backward compatible

Note

by Narcle

Example:

## DeleteValueInStrArray¶

```procedure DeleteValueInStrArray(var Arr: TStringArray; ValuePosition: Integer);
```

Deletes value with ValuePosition in “Arr” string array. Maintains current order.

Note

by EvilChicken!

Example:

## DeleteValueInIntArray¶

```procedure DeleteValueInIntArray(var Arr: TIntegerArray; ValuePosition: Integer);
```

Deletes value with ValuePosition in “Arr” Integer array.

Note

by EvilChicken!

Example:

## DeleteValueInFloatArray¶

```procedure DeleteValueInFloatArray(var Arr: TExtendedArray; ValuePosition: Integer);
```

Deletes value with ValuePosition in “Arr” Float array.

Note

by EvilChicken!

Example:

## DeleteValueInBoolArray¶

```procedure DeleteValueInBoolArray(var Arr: TBooleanArray; ValuePosition: Integer);
```

Deletes value with ValuePosition in “Arr” Boolean array.

Note

by EvilChicken!

Example:

## DeleteValueInTPA¶

```procedure DeleteValueInTPA(var Arr: TPointArray; ValuePosition: Integer);
```

Deletes value with ValuePosition in “Arr” TPointArray.

Note

by EvilChicken! & Coh3n

Example:

## DeleteValueInATPA¶

```procedure DeleteValueInATPA(var Arr: T2DPointArray; ValuePosition: Integer);
```

Deletes value with ValuePosition in “Arr” T2DPointArray.

Note

by EvilChicken! & Coh3n

Example:

## BoxClosestPoint¶

```function BoxClosestPoint(p : TPoint; b : TBox) : TPoint;
```

There are ‘nine’ places where the point can be (relative to a box).. The middle, 5, is the box itself. The rest looks like this:

1 | 2 | 3
———
4 | 5 | 6
———
7 | 8 | 9

Place 9 means it’s past the b.x2, and past the b.y2, therefore the smallest dist between the point and the box is the distance between the point and the box’s x2,y2 coordinate.

Note

by Raym0nd

Example:

## RandomPointBoxEx¶

```function RandomPointBoxEx(p : TPoint; b : TBox; extraDist : integer) : TPoint;
```

Returns a point in the box which is ‘aimed’ near the TPoint given.. Basically it only allows points that lay within the extradist from the closest point to be returned. Practical example:

Inventory Box.. When your mouse is on the right of an inv item, you don’t want to click on the left of the box.

Note

by Raym0nd

Example:

## MiddleBox¶

```function MiddleBox(b : TBox) : TPoint;
```

Returns the middle of the box.

Note

by Raym0nd

Example:

```midPt := MiddleBox(TBox(MMX1, MMY1, MMX2, MMY2));
// midPt == Point(MMCX, MMCY)!
```

## GroupDigits¶

```function GroupDigits(n: integer; token: String): String;
```

Nicely formats the number passed by placing token in between groups of three numbers.

Note

by PriSoner and Nava2

Example:

```foo := GroupDigits(1000, ','); // foo -> '1,000'
bar := GroupDigits(1500423, ' '); // bar -> '1 500 423'
```

## CashStrToInt¶

```function CondensedStrToInt(Number: string): Integer;
```

Converts between ‘100k’ representation to 1000000.

Note

by Nava2

Example:

```foo := CashStrToInt('2b');
```

## GridBox¶

```function GridBox(slot, columns, rows, w, h, diffX, diffY: integer; starTPoint: TPoint): TBox;
```

Returns a TBox of the slot ‘slot’ in a grid created by the other parameters. Slots start with 1 being the top left of the grid, and counting accross.

• columns: number of columns in the grid
• rows: number of rows in the grid
• w: the width of each box in the grid
• h: the height of each box in the grid
• diffX: pixels from center to center (x)
• diffY: pixels from center to center (y)
• starTPoint: the center point of the top left box in the grid

Note

Author: Coh3n

Example:

```result := GridBox(Slot, 4, 2, 95, 80, 0, 150, point(110, 100));
```

## Grid¶

```function Grid(columns, rows, w, h, diffX, diffY: integer; starTPoint: TPoint): TBoxArray;
```

Returns a grid of TBoxes (see GridBox for parameters).

Note

Author: Coh3n

Example:

```SMART_DrawBoxes(false, Grid(5, 10, 20, 20, 25, 25, point(30, 30)), clYellow);
```

## TPABetweenPoints¶

```function TPABetweenPoints(P1, P2: TPoint; Interval, Rand: Integer): TPointArray;
```

Generates a TPA between P1 and P2, Interval between points; Rand randomness on each point. Very useful for generating a random walking path between the player’s position and the ending position.

Note

Author: Nava2
```newTPA := TPABetweenPoints(PlayerPos, EndPos, 25, 10);