tModLoader v2024.11
A mod to make and play Terraria mods
Utils Class Reference

Classes

struct  ChaseResults
 

Public Member Functions

delegate Color ColorLerpMethod (float percent)
 
delegate void LaserLineFraming (int stage, Vector2 currentPosition, float distanceLeft, Rectangle lastFrame, out float distanceCovered, out Rectangle frame, out Vector2 origin, out Color color)
 
delegate bool TileActionAttempt (int x, int y)
 

Static Public Member Functions

static float AngleFrom (this Vector2 Origin, Vector2 Target)
 
static float AngleLerp (this float curAngle, float targetAngle, float amount)
 
static float AngleTo (this Vector2 Origin, Vector2 Target)
 
static float AngleTowards (this float curAngle, float targetAngle, float maxChange)
 
static bool Between (this Vector2 vec, Vector2 minimum, Vector2 maximum)
 
static Vector2 Bottom (this Rectangle r)
 
static Vector2 BottomLeft (this Rectangle r)
 
static Vector2 BottomRight (this Rectangle r)
 
static Vector2 Center (this Rectangle r)
 
static Rectangle CenteredRectangle (Vector2 center, Vector2 size)
 
static T Clamp< T > (T value, T min, T max)
 
static void ClampWithinWorld (ref int minX, ref int minY, ref int maxX, ref int maxY, bool lastValuesInclusiveToIteration=false, int fluffX=0, int fluffY=0)
 
static Vector2 ClosestPointInRect (this Rectangle r, Vector2 point)
 
static Vector2 ClosestPointOnLine (this Vector2 P, Vector2 A, Vector2 B)
 Returns the closest point on a line segment from A to B to another point P . The resulting point will be on the line and not extend past A or B.
 
static long CoinsCombineStacks (out bool overFlowing, params long[] coinCounts)
 
static long CoinsCount (out bool overFlowing, Item[] inv, params int[] ignoreSlots)
 
static int[] CoinsSplit (long count)
 Converts a coin value into an array of individual coin currency item counts. The result is a 4 element array containing [Copper, Silver, Gold, Platinum] coin counts totaling to the coin value provided.
 
static Color ColorLerp_BlackToWhite (float percent)
 
static string[] ConvertMonoArgsToDotNet (string[] brokenArgs)
 
static int Count< T > (this T[] arr, T value)
 
static bool deepCompare (this int[] firstArray, int[] secondArray)
 
static Vector2 DirectionFrom (this Vector2 Origin, Vector2 Target)
 
static Vector2 DirectionTo (this Vector2 Origin, Vector2 Target)
 
static float Distance (this Rectangle r, Vector2 point)
 
static float Distance (this Vector2 Origin, Vector2 Target)
 
static float DistanceSQ (this Vector2 Origin, Vector2 Target)
 
static bool DoesFitInCone (Vector2 point, Vector2 coneCenter, float coneLength, float coneRotation, float maximumAngle)
 
static Vector2 DrawBorderString (SpriteBatch sb, string text, Vector2 pos, Color color, float scale=1f, float anchorx=0f, float anchory=0f, int maxCharactersDisplayed=-1)
 
static Vector2 DrawBorderStringBig (SpriteBatch spriteBatch, string text, Vector2 pos, Color color, float scale=1f, float anchorx=0f, float anchory=0f, int maxCharactersDisplayed=-1)
 
static void DrawBorderStringFourWay (SpriteBatch sb, DynamicSpriteFont font, string text, float x, float y, Color textColor, Color borderColor, Vector2 origin, float scale=1f)
 
static void DrawCursorSingle (SpriteBatch sb, Color color, float rot=float.NaN, float scale=1f, Vector2 manualPosition=default(Vector2), int cursorSlot=0, int specialMode=0)
 
static void DrawInvBG (SpriteBatch sb, float x, float y, float w, float h, Color c=default(Color))
 
static void DrawInvBG (SpriteBatch sb, int x, int y, int w, int h, Color c=default(Color))
 
static void DrawInvBG (SpriteBatch sb, Rectangle R, Color c=default(Color))
 
static void DrawLaser (SpriteBatch sb, Texture2D tex, Vector2 start, Vector2 end, Vector2 scale, LaserLineFraming framing)
 
static void DrawLine (SpriteBatch spriteBatch, Point start, Point end, Color color)
 
static void DrawLine (SpriteBatch spriteBatch, Vector2 start, Vector2 end, Color color)
 
static void DrawLine (SpriteBatch spriteBatch, Vector2 start, Vector2 end, Color colorStart, Color colorEnd, float width)
 
static void DrawPanel (Texture2D texture, int edgeWidth, int edgeShove, SpriteBatch spriteBatch, Vector2 position, float width, Color color)
 
static void DrawRect (SpriteBatch spriteBatch, Rectangle rect, Color color)
 
static void DrawRect (SpriteBatch spriteBatch, Vector2 start, Vector2 end, Color color)
 
static void DrawRect (SpriteBatch spriteBatch, Vector2 topLeft, Vector2 topRight, Vector2 bottomRight, Vector2 bottomLeft, Color color)
 
static void DrawRectangle (SpriteBatch sb, Vector2 start, Vector2 end, Color colorStart, Color colorEnd, float width)
 
static void DrawRectForTilesInWorld (SpriteBatch spriteBatch, Point start, Point end, Color color)
 
static void DrawRectForTilesInWorld (SpriteBatch spriteBatch, Rectangle rect, Color color)
 
static void DrawSettings2Panel (SpriteBatch spriteBatch, Vector2 position, float width, Color color)
 
static void DrawSettingsPanel (SpriteBatch spriteBatch, Vector2 position, float width, Color color)
 
static void DrawSplicedPanel (SpriteBatch sb, Texture2D texture, int x, int y, int w, int h, int leftEnd, int rightEnd, int topEnd, int bottomEnd, Color c)
 
static Vector2 FactorAcceleration (Vector2 currentVelocity, float timeToInterception, Vector2 descendOfProjectile, int framesOfLenience)
 
static bool FloatIntersect (float r1StartX, float r1StartY, float r1Width, float r1Height, float r2StartX, float r2StartY, float r2Width, float r2Height)
 
static Vector2 Floor (this Vector2 vec)
 
static string FormatWith (string original, object obj)
 
static Rectangle Frame (this Asset< Texture2D > tex, int horizontalFrames=1, int verticalFrames=1, int frameX=0, int frameY=0, int sizeOffsetX=0, int sizeOffsetY=0)
 
static Rectangle Frame (this Texture2D tex, int horizontalFrames=1, int verticalFrames=1, int frameX=0, int frameY=0, int sizeOffsetX=0, int sizeOffsetY=0)
 Returns a Rectangle region ("frame") of the Texture2D corresponding to the layout provided. Can be used to easily retrieve coordinates for drawing a specific frame of a texture. For example texture.Frame(1, 3, 0, 2) indicates that the texture has 1 column and 3 rows and returns the coordinates of the bottom (index #2, or 3rd) frame of the only column.
 
static ChaseResults GetChaseResults (Vector2 chaserPosition, float chaserSpeed, Vector2 runnerPosition, Vector2 runnerVelocity)
 
static float GetDayTimeAs24FloatStartingFromMidnight ()
 Returns the current world time in a 24 hour clock representation. This value is derived from Main.dayTime and Main.time. For example Utils.GetDayTimeAs24FloatStartingFromMidnight() < 6.50f would check if the time is before 6:30 AM.
 
static Vector2 GetDayTimeAsDirectionIn24HClock ()
 
static Vector2 GetDayTimeAsDirectionIn24HClock (float timeFrom0To24)
 
static double GetLerpValue (double from, double to, double t, bool clamped=false)
 
static float GetLerpValue (float from, float to, float t, bool clamped=false)
 
static List< int > GetTrueIndexes (params bool[][] arrays)
 
static List< int > GetTrueIndexes (this bool[] array)
 
static bool HasNaNs (this Vector2 vec)
 
static int Height (this Asset< Texture2D > asset)
 
static string Hex3 (this Color color)
 
static string Hex4 (this Color color)
 
static bool IndexInRange< T > (this List< T > t, int index)
 
static bool IndexInRange< T > (this T[] t, int index)
 
static bool IntersectsConeFastInaccurate (this Rectangle targetRect, Vector2 coneCenter, float coneLength, float coneRotation, float maximumAngle)
 
static bool IntersectsConeSlowMoreAccurate (this Rectangle targetRect, Vector2 coneCenter, float coneLength, float coneRotation, float maximumAngle)
 
static bool IsPowerOfTwo (int x)
 
static Vector2 Left (this Rectangle r)
 
static double Lerp (double value1, double value2, double amount)
 
static float LineRectangleDistance (Rectangle rect, Vector2 lineStart, Vector2 lineEnd)
 
static void LogAndChatAndConsoleInfoMessage (string message)
 
static void LogAndConsoleErrorMessage (string message)
 
static void LogAndConsoleInfoMessage (string message)
 
static void LogAndConsoleInfoMessageFormat (string format, params object[] args)
 
static Version MajorMinor (this Version v)
 
static Version MajorMinorBuild (this Version v)
 
static R[] MapArray< T, R > (T[] array, Func< T, R > mapper)
 
static T Max< T > (params T[] args)
 
static Rectangle Modified (this Rectangle r, int x, int y, int w, int h)
 
static int ModulusPositive (this int myInteger, int modulusNumber)
 
static Vector2 MoveTowards (this Vector2 currentPosition, Vector2 targetPosition, float maxAmountAllowedToMove)
 
static float MultiLerp (float percent, params float[] floats)
 Linearly interpolates between several values denoting a continuous piecewise linear function (Piecewise linear function wikipedia page). This can be used to interpolate between several values, providing finer control over the transition between values. More...
 
static Color MultiplyRGB (this Color firstColor, Color secondColor)
 
static Color MultiplyRGBA (this Color firstColor, Color secondColor)
 
static int Next (this UnifiedRandom random, IntRange range)
 
static T Next< T > (this UnifiedRandom r, IList< T > list)
 Returns a random element from the provided list. More...
 
static T Next< T > (this UnifiedRandom r, T[] array)
 Returns a random element from the provided array. More...
 
static bool NextBool (this UnifiedRandom r)
 Returns true or false randomly with equal chance. More...
 
static bool NextBool (this UnifiedRandom r, int antecedent, int consequent)
 Returns true X out of Y times.
 
static bool NextBool (this UnifiedRandom r, int consequent)
 Returns true 1 out of X times.
 
static T NextEnum< T > (this T src)
 
static float NextFloat (this UnifiedRandom r)
 Generates a random value between 0f (inclusive) and 1f (exclusive).
It will not return 1f. More...
 
static float NextFloat (this UnifiedRandom r, float maxValue)
 Generates a random value between 0f (inclusive) and maxValue (exclusive).
It will not return maxValue . More...
 
static float NextFloat (this UnifiedRandom r, float minValue, float maxValue)
 Generates a random value between minValue (inclusive) and maxValue (exclusive).
It will not return maxValue . More...
 
static float NextFloat (this UnifiedRandom random, FloatRange range)
 
static float NextFloatDirection (this UnifiedRandom r)
 
static T NextFromCollection< T > (this UnifiedRandom random, List< T > objs)
 
static T NextFromList< T > (this UnifiedRandom random, params T[] objs)
 
static Vector2 NextVector2Circular (this UnifiedRandom r, float circleHalfWidth, float circleHalfHeight)
 
static Vector2 NextVector2CircularEdge (this UnifiedRandom r, float circleHalfWidth, float circleHalfHeight)
 
static Vector2 NextVector2FromRectangle (this UnifiedRandom r, Rectangle rect)
 
static Vector2 NextVector2Square (this UnifiedRandom r, float min, float max)
 
static Vector2 NextVector2Unit (this UnifiedRandom r, float startRotation=0f, float rotationRange=(float) Math.PI *2f)
 
static Rectangle OffsetSize (this Rectangle rect, int xSize, int ySize)
 
static void OpenFolder (string folderPath)
 
static void OpenToURL (string url)
 
static Vector2 OriginFlip (this Rectangle rect, Vector2 origin, SpriteEffects effects)
 
static Dictionary< string, string > ParseArguements (string[] args)
 
static float PingPongFrom01To010 (float value)
 Remaps a provided value from 0->1 to 0->1->0, essentially turning a sawtooth pattern into a triangle wave pattern: More...
 
static bool PlotLine (Point p0, Point p1, TileActionAttempt plot, bool jump=true)
 
static bool PlotLine (Point16 p0, Point16 p1, TileActionAttempt plot, bool jump=true)
 
static bool PlotTileArea (int x, int y, TileActionAttempt plot)
 
static bool PlotTileLine (Vector2 start, Vector2 end, float width, TileActionAttempt plot)
 
static bool PlotTileLine (Vector2D start, Vector2D end, double width, TileActionAttempt plot)
 
static bool PlotTileTale (Vector2D start, Vector2D end, double width, TileActionAttempt plot)
 
static void PoofOfSmoke (Vector2 position)
 
static bool PressingControl (this KeyboardState kb)
 
static bool PressingShift (this KeyboardState kb)
 
static string PrettifyPercentDisplay (float percent, string originalFormat)
 
static T PreviousEnum< T > (this T src)
 
static int RandomConsecutive (double random, int odds)
 
static float RandomFloat (ref ulong seed)
 
static int RandomInt (ref ulong seed, int max)
 
static int RandomInt (ref ulong seed, int min, int max)
 
static int RandomNext (ref ulong seed, int bits)
 
static ulong RandomNextSeed (ulong seed)
 
static Vector2 RandomVector2 (UnifiedRandom random, float min, float max)
 
static Vector2D RandomVector2D (UnifiedRandom random, double min, double max)
 
static string ReadEmbeddedResource (string path)
 
static Vector2 ReadPackedVector2 (this BinaryReader bb)
 
static Color ReadRGB (this BinaryReader bb)
 
static Vector2 ReadVector2 (this BinaryReader bb)
 
static BitArray ReceiveBitArray (int BitArrLength, BinaryReader reader)
 Receives the result of SendBitArray, and returns the corresponding BitArray
 
static bool RectangleLineCollision (Vector2 rectTopLeft, Vector2 rectBottomRight, Vector2 lineStart, Vector2 lineEnd)
 
static float Remap (float fromValue, float fromMin, float fromMax, float toMin, float toMax, bool clamped=true)
 
static int Repeat (int value, int length)
 
static Vector2 Right (this Rectangle r)
 
static Vector2 RotatedBy (this Vector2 spinningpoint, double radians, Vector2 center=default(Vector2))
 
static Vector2D RotatedBy (this Vector2D spinningpoint, double radians, Vector2D center=default(Vector2D))
 
static Vector2 RotatedByRandom (this Vector2 spinninpoint, double maxRadians)
 
static Vector2 RotateRandom (this Vector2 spinninpoint, double maxRadians)
 
static Vector2 rotateTowards (Vector2 currentPosition, Vector2 currentVelocity, Vector2 targetPosition, float maxChange)
 
static Vector2 Round (Vector2 input)
 
static Vector2 SafeNormalize (this Vector2 v, Vector2 defaultValue)
 If this Vector2 can't be normalized, either because it is a 0 length vector or has not a number values, defaultValue will be returned instead as a fallback. It will not be normalized so using Vector2.UnitX or Vector2.UnitY is appropriate. Vector2.Zero can also be appropriate if the surrounding logic expects it. Using SafeNormalize instead of Normalize avoids many difficult to anticipate issues that would cause bugs.
 
static T SelectRandom< T > (UnifiedRandom random, params T[] choices)
 
static void SendBitArray (BitArray arr, BinaryWriter writer)
 Bit packs a BitArray into a Byte Array and then sends the byte array
 
static void ShowFancyErrorMessage (string message, int returnToMenu, UIState returnToState=null)
 
static Vector2 Size (this Asset< Texture2D > tex)
 
static Vector2 Size (this Rectangle r)
 
static Vector2 Size (this Texture2D tex)
 
static double SmoothStep (double min, double max, double x)
 
static float SmoothStep (float min, float max, float x)
 
static bool SolveQuadratic (float a, float b, float c, out float result1, out float result2)
 
static void Swap< T > (ref T t1, ref T t2)
 
static byte[] ToByteArray (this string str)
 
static int ToDirectionInt (this bool value)
 Converts a bool to 1 if true, -1 if false. Suitable for working with Entity.direction and Projectile.spriteDirection, or anything else related to geometry.
 
static int ToInt (this bool value)
 Converts a bool to 1 if true, 0 if false.
 
static Vector2 Top (this Rectangle r)
 
static Vector2 TopLeft (this Rectangle r)
 
static Point ToPoint (this Point16 p)
 
static Point ToPoint (this Vector2 v)
 
static Point ToPoint (this Vector2D v)
 
static Point16 ToPoint16 (this Vector2 v)
 Converts this Vector2 to a Point16, resulting in X and Y values rounded towards 0. If the intention is to convert to Tile coordinates from World coordinates, use ToTileCoordinates16(Vector2) instead.
 
static Vector2 TopRight (this Rectangle r)
 
static float ToRotation (this Vector2 v)
 
static Vector2 ToRotationVector2 (this float f)
 
static Vector2 ToScreenPosition (this Vector2 worldPosition)
 
static Point ToTileCoordinates (this Vector2 vec)
 
static Point ToTileCoordinates (this Vector2D vec)
 
static Point16 ToTileCoordinates16 (this Vector2 vec)
 
static Point16 ToTileCoordinates16 (this Vector2D vec)
 
static Vector2 ToVector2 (this Point p)
 
static Vector2 ToVector2 (this Point16 p)
 
static Vector2D ToVector2D (this Point p)
 
static Vector2D ToVector2D (this Point16 p)
 
static Vector2D ToVector2D (this Vector2 v)
 
static Vector2 ToWorldCoordinates (this Point p, float autoAddX=8f, float autoAddY=8f)
 
static Vector2 ToWorldCoordinates (this Point p, Vector2 autoAddXY)
 
static Vector2 ToWorldCoordinates (this Point16 p, float autoAddX=8f, float autoAddY=8f)
 
static Vector2 ToWorldCoordinates (this Point16 p, Vector2 autoAddXY)
 
static Vector2 ToWorldCoordinates (this Vector2 v, float autoAddX=8f, float autoAddY=8f)
 
static Vector2 ToWorldCoordinates (this Vector2 v, Vector2 autoAddXY)
 
static void TrimTextIfNeeded (ref string text, DynamicSpriteFont font, float scale, float maxWidth)
 
static bool TryCreatingDirectory (string folderPath)
 
static float Turn01ToCyclic010 (float value)
 Remaps a provided value from 0->1 to a cosine curve. Provides a method of turning an input sawtooth pattern into a smooth cyclic curve. This is useful when using repeating timers to drive effects that should return smoothly to their initial value before repeating rather than reset abruptly. More...
 
static double UnclampedSmoothStep (double min, double max, double x)
 
static float UnclampedSmoothStep (float min, float max, float x)
 
static DateTime UnixTimeStampToDateTime (long unixTimeStamp)
 
static Vector2 Vector2FromElipse (Vector2 angleVector, Vector2 elipseSizes)
 
static int Width (this Asset< Texture2D > asset)
 
static bool WithinRange (this Vector2 Origin, Vector2 Target, float MaxRange)
 Returns true if the Target is within MaxRange world coordinate units of Origin .
 
static string[] WordwrapString (string text, DynamicSpriteFont font, int maxWidth, int maxLines, out int lineAmount)
 
static List< List< TextSnippet > > WordwrapStringSmart (string text, Color c, DynamicSpriteFont font, int maxWidth, int maxLines)
 
static float WrappedLerp (float value1, float value2, float percent)
 
static void WritePackedVector2 (this BinaryWriter bb, Vector2 v)
 
static void WriteRGB (this BinaryWriter bb, Color c)
 
static void WriteVector2 (this BinaryWriter bb, Vector2 v)
 
static Vector2 XY (this Vector4 vec)
 
static Vector3 XZW (this Vector4 vec)
 
static Vector3 YZW (this Vector4 vec)
 
static Vector2 ZW (this Vector4 vec)
 

Static Public Attributes

static Dictionary< DynamicSpriteFont, float[]> charLengths = new Dictionary<DynamicSpriteFont, float[]>()
 
const long MaxCoins = 999999999L
 

Member Function Documentation

◆ Clamp< T >()

static T Utils.Clamp< T > ( value,
min,
max 
)
static
Type Constraints
T :IComparable<T> 

◆ Max< T >()

static T Utils.Max< T > ( params T[]  args)
static
Type Constraints
T :IComparable 

◆ MultiLerp()

static float Utils.MultiLerp ( float  percent,
params float[]  floats 
)
static

Linearly interpolates between several values denoting a continuous piecewise linear function (Piecewise linear function wikipedia page). This can be used to interpolate between several values, providing finer control over the transition between values.

floats parameter→ 0 4 1 Range Values ↓ / \ 3
/ \ 2
/ 1 / 0 percent input→ 0 .25 .5 .75 1 output→ 0 2 4 3 2

Parameters
percent
floats
Returns

◆ Next< T >() [1/2]

static T Utils.Next< T > ( this UnifiedRandom  r,
IList< T >  list 
)
static

Returns a random element from the provided list.

Template Parameters
T
Parameters
r
list
Returns

◆ Next< T >() [2/2]

static T Utils.Next< T > ( this UnifiedRandom  r,
T[]  array 
)
static

Returns a random element from the provided array.

Template Parameters
T
Parameters
r
array
Returns

◆ NextBool()

static bool Utils.NextBool ( this UnifiedRandom  r)
static

Returns true or false randomly with equal chance.

Parameters
r
Returns

◆ NextEnum< T >()

static T Utils.NextEnum< T > ( this T  src)
static
Type Constraints
T :struct 

◆ NextFloat() [1/3]

static float Utils.NextFloat ( this UnifiedRandom  r)
static

Generates a random value between 0f (inclusive) and 1f (exclusive).
It will not return 1f.

Parameters
r
Returns

◆ NextFloat() [2/3]

static float Utils.NextFloat ( this UnifiedRandom  r,
float  maxValue 
)
static

Generates a random value between 0f (inclusive) and maxValue (exclusive).
It will not return maxValue .

Parameters
r
maxValue
Returns

◆ NextFloat() [3/3]

static float Utils.NextFloat ( this UnifiedRandom  r,
float  minValue,
float  maxValue 
)
static

Generates a random value between minValue (inclusive) and maxValue (exclusive).
It will not return maxValue .

Parameters
r
minValue
maxValue
Returns

◆ PingPongFrom01To010()

static float Utils.PingPongFrom01To010 ( float  value)
static

Remaps a provided value from 0->1 to 0->1->0, essentially turning a sawtooth pattern into a triangle wave pattern:

Original: Remap: Range Values ↓
/ / \ 1
/ / \ 0.5
/ / \ 0
Input→ 0 .5 1 0 .5 1 Output→ 0 .5 1 0 1 0
This is useful when using repeating timers to drive effects that should return linearly to their initial value before repeating rather than reset abruptly.

Parameters
value
Returns

◆ PreviousEnum< T >()

static T Utils.PreviousEnum< T > ( this T  src)
static
Type Constraints
T :struct 

◆ Turn01ToCyclic010()

static float Utils.Turn01ToCyclic010 ( float  value)
static

Remaps a provided value from 0->1 to a cosine curve. Provides a method of turning an input sawtooth pattern into a smooth cyclic curve. This is useful when using repeating timers to drive effects that should return smoothly to their initial value before repeating rather than reset abruptly.

Parameters
value
Returns