# Custom Extra Scripts (TeaScript)

This is a list of useful custom scripts made for the TeaScript language. Feel free to use any of the scripts you find here, and contribute any script that you may find useful!

For an explanation on how to create a custom script, check here.

## Adding Your Own Script

This is a template so you can add your own functions to this list.

### Name of Script

Created by: user

Requisites: optional

Here is an explanation or notes of the function. Make sure to underline number values, and italicize string values.

__returnval__ = funcname(__number__, *string)*

```
script funcname(a as double, b as string, return double)
end script
```

## Conversions

### rad2deg

Created by: SetaYoshi

Requisites: None

Converts a radian value to degrees

__degrees__ = rad2deg(__radians__*)*

```
script rad2deg(r as double, return double)
return r*180/pi
end script
```

### deg2rad

Created by: SetaYoshi

Requisites: None

Converts a degree value to radians

__radians__ = deg2rad(__degrees__*)*

```
script deg2rad(r as double, return double)
return r*pi/180
end script
```

### rec2polr

Created by: SetaYoshi

Requisites: None

Returns the magnitude in polar form of rectangular numbers

__r__ = rec2polr(__x__, __y__*)*

```
script rec2polr(x as double, y as double, return double)
return sqr(x^2 + y^2)
end script
```

### rec2pola

Created by: SetaYoshi

Requisites: None

Returns the angle in polar form of rectangular numbers

__a__ = rec2pola(__x__, __y__*)*

```
script rec2pola(x as double, y as double, return double)
return 2*pi*getangle(x, y)
end script
```

### rec2polad

Created by: SetaYoshi

Requisites: None

Returns the angle in degrees in polar form of rectangular numbers

__a__ = rec2polad(__x__, __y__*)*

```
script rec2polad(x as double, y as double, return double)
return 360*getangle(x, y)
end script
```

### pol2recx

Created by: SetaYoshi

Requisites: None

Returns the x component in rectangular form from polar numbers

__x__ = pol2recx(__r__, __a__*)*

```
script pol2recx(r as double, a as double, return double)
return r*cos(a)
end script
```

### pol2recy

Created by: SetaYoshi

Requisites: None

Returns the x component in rectangular form from polar numbers

__y__ = pol2recy(__r__, __a__*)*

```
script pol2recy(r as double, a as double, return double)
return r*sin(a)
end script
```

### podl2recx

Created by: SetaYoshi

Requisites: None

Returns the x component in rectangular form from polar numbers in degrees

__x__ = pold2recx(__r__, __a__*)*

```
script pold2recx(r as double, a as double, return double)
return r*cos(a*180/pi)
end script
```

### pold2recy

Created by: SetaYoshi

Requisites: None

Returns the x component in rectangular form from polar numbers

__y__ = pold2recy(__r__, __a__*)*

```
script pol2recy(r as double, a as double, return double)
return r*sin(a*180/pi)
end script
```

### tolowercase

Created by: SetaYoshi

Requisites: An user-variable called *tmp*

Converts all letters to lower case.

*lowertext* `= tolowercase(`

*text)*

```
script toLowerCase(inp as string, return string)
str(tmp) = ""
dim i as integer
for i = 1 to len(inp) step 1
if asc(mid(inp, i, 1)) >= 65 and asc(mid(inp, i, 1)) <= 90 then
str(tmp) = str(tmp) & chr(asc(mid(inp, i, 1)) + 32)
else
str(tmp) = str(tmp) & mid(inp, i, 1)
end if
next
return str(tmp)
end script
```

### touppercase

Created by: SetaYoshi

Requisites: An user-variable called *tmp*

Converts all letters to uppercase.

*uppertext* `= touppercase(`

*text)*

```
script toUpperCase(inp as string, return string)
str(tmp) = ""
dim i as integer
for i = 1 to len(inp) step 1
if asc(mid(inp, i, 1)) >= 97 and asc(mid(inp, i, 1)) <= 122 then
str(tmp) = str(tmp) & chr(asc(mid(inp, i, 1)) - 32)
else
str(tmp) = str(tmp) & mid(inp, i, 1)
end if
next
return str(tmp)
end script
```

### getColorC

Created by: SetaYoshi

Requisites: none

Converts a SMBX Color and returns a component.

For component type use:

- 1: alpha value
- 2: red value
- 3: green value
- 4: blue value

__component__`= getColorC(`

__SMBX Color__, __component type__*)*

```
script getColorC(n as double, t as double, return double)
return int(n/(256^(4 - t)) - 256*int(n/(256^(4 - t + 1))))
end script
```

### name2key

Created by: SetaYoshi

Requisites: none

Converts the name of a key to a number to be used by the `keypress`

function . Use the player value to choose what player (1: player 1, 2:player 2)

__keypressvalue =__`name2key(`

*name*, __player__*)*

```
script name2key(x as string, p as double, return double)
select case x
case "pause" return -10 - 10*(p - 1)
case "right" return -11 - 10*(p - 1)
case "left" return -12 - 10*(p - 1)
case "down" return -13 - 10*(p - 1)
case "up" return -14 - 10*(p - 1)
case "altjump" return -15 - 10*(p - 1)
case "jump" return -16 - 10*(p - 1)
case "run" return -17 - 10*(p - 1)
case "altrun" return -18 - 10*(p - 1)
case "select" return -19 - 10*(p - 1)
end select
end script
```

### convertify

Created by: _FyreNova

Requisites: none

Converts units. If you use `"convert"`

, it will convert **from** frames **to** the unit. If you use `"deconvert"`

, it will convert **from** the unit **to** frames. Units can be seconds, milliseconds, and minutes.

__secondtimer__ = `convertify(3,"seconds","convert")`

```
script convertify(a as double,b as string,c as string,return double)
select case c
case "convert"
select case b
case "seconds"
return round(a*65,0)
case "minutes"
return round(a*65*60,0)
case "milliseconds"
return round(a*65*.001,0)
end select
case "deconvert"
select case b
case "seconds"
return round(a/65,0)
case "minutes"
return round(a/65/60,0)
case "milliseconds"
return round(a/65/.001,0)
end select
end select
end script
```

## Math Functions

### logn

Created by: SetaYoshi

Requisites: None

Does a log operation at any base

__y__ = logn(__x__, __base__*)*

```
script logn(x as double, b as double, return double)
return log(x)/log(b)
end script
```

### sqrn

Created by: SetaYoshi

Requisites: None

Does a root at any base

__y__ = sqrn(__x__, __base__*)*

```
script sqrn(x as double, n as double, return double)
return x^(1/n)
end script
```

### factorial

Created by: SetaYoshi

Requisites: None

Performs a factorial operation

__y__ = factorial(__x__*)*

```
script factorial(x as double, return double)
dim i as integer
dim v as double = 1
for i = 1 to x
v = v*i
next
return v
end script
```

### modul

Created by: SetaYoshi

Requisites: None

A more accurate modulus operator

__y__ = modul(__x__, __n__*)*

```
script modul(x as double, n as double, return double)
if n = 0 then return 0
if n > 0 and x > 0 then return x - n*fix(x/n)
if n > 0 then return n - abs(x - n*fix(x/n))
if x > 0 then return x + n*(fix(x/abs(n)) + 1)
return abs(n*fix(x/n)) + x
end script
```

### lin_interp

Created by: SetaYoshi

Requisites: None

Performs a linear interpolation. Given 2 pairs of points, it will find a missing y with a given x

__yn__ = lin_interp(__x1__, __y1__, __x2__, __y2__, __xn__*)*

```
script lin_interp(x1 as double, y1 as double, x2 as double, y2 as double, x as double, return double)
return y1 + (x - x1)*(y2 - y1)/(x2 - x1)
end script
```

### distance

Created by: SetaYoshi

Requisites: None

Finds the distance between 2 given points

__r__ = distance(__x1__, __y1__, __x2__, __y2__*)*

```
script distance(x1 as double, y1 as double, x2 as double, y2 as double, return double)
return sqr((x1 - x2)^2 + (y1 - y2)^2)
end script
```

### min

Created by: SetaYoshi

Requisites: None

Returns the smaller number

__y__ = min(__a__, __b__*)*

```
script min(a as double, b as double, return double)
if a < b then return a
return b
end script
```

### max

Created by: SetaYoshi

Requisites: None

Returns the larger number

__y__ = max(__a__, __b__*)*

```
script max(a as double, b as double, return double)
if a > b then return a
return b
end script
```

### midd

Created by: SetaYoshi

Requisites: None

Returns the middle number

__y__ = midd(__a__, __b__, __c__*)*

```
script midd(a as double, b as double, c as double, return double)
if (a > b and b > c) or (c > b and b > a) then return b
if (a > c and c > b) or (b > c and c > a) then return c
return a
end script
```

### isInteger

Created by: SetaYoshi

Requisites: None

Returns the middle number

__y__ = midd(__a__, __b__, __c__*)*

```
script midd(a as double, b as double, c as double, return double)
if (a > b and b > c) or (c > b and b > a) then return b
if (a > c and c > b) or (b > c and c > a) then return c
return a
end script
```

## RNG Functions

### rand

Created by: SetaYoshi

Requisites: None

Returns a random number between 2 numbers

__x__ = rand(__min__, __max__*)*

```
script rand(min as double, max as double, return double)
return rnd*(max - min) + min
end script
```

### randInt

Created by: SetaYoshi

Requisites: None

Returns a random integer between 2 numbers

__x__ = randInt(__min__, __max__*)*

```
script randInt(min as double, max as double, return double)
return round(rnd*(max - min) + min, 0)
end script
```

### randSub

Created by: SetaYoshi

Requisites: None

Returns a random character in a string

*char* `= randSub(`

*text)*

```
script randSub(inp as string, return string)
return mid(inp, randInt(1, len(inp)), 1)
end script
```

### randBool

Created by: SetaYoshi

Requisites: None

Randomly returns -1 or 0

*char* `= randBool(`

*text)*

```
script randBool(return string)
return rnd <= 0.5
end script
```

## Range Functions

### coil

Created by: SetaYoshi

Requisites: None

Forces a number to loop between a range

__y__ = coil(__min__, __max__, __n__*)*

```
script coil(a as double, b as double, n as double, return double)
return ((a - b) mod (n - b + a)) + b
end script
```

### wrap

Created by: SetaYoshi

Requisites: None

A modified version of coil that is more practical for counting

__y__ = coil(__min__, __max__, __n__*)*

```
script coil(a as double, b as double, n as double, return double)
return ((a - b - 1) mod (n - b + a)) + b
end script
```

### tie

Created by: SetaYoshi

Requisites: None

A less flexible form of coil

__y__ = tie(__min__, __max__, __n__*)*

```
script tie(a as double, b as double, n as double, return double)
if n < a then return b - (a - n)
if n > b then return a - (b - n)
return n
end script
```

### knot

Created by: SetaYoshi

Requisites: None

An alternate form of tie that is more practical for counting.

__y__ = knot(__min__, __max__, __n__*)*

```
script knot(a as double, b as double, n as double, return double)
if n < a then return b - (a - n - 1)
if n > b then return a - (b - n + 1)
return n
end script
```

### brace

Created by: SetaYoshi

Requisites: None

When the number overflows, it will snap to the opposite boundary

__y__ = brace(__min__, __max__, __n__*)*

```
script brace(a as double, b as double, n as double, return double)
if n < a then return b
if n > b then return a
return n
end script
```

### clamp

Created by: SetaYoshi

Requisites: None

When the number overflows, it will snap to the current boundary

`y = clamp(min, max, n`

*)*

```
script clamp(a as double, b as double, n as double, return double)
if n < a then return a
if n > b then return b
return n
end script
```

### tween

Created by: _FyreNova

Requisites: None

Returns the tweened number between __ x__ and

__y__

, using __as its percentage.__

`z`

__value__ = tween(number1,number2,decimal)

```
script tween(x as double, y as double, z as double, return double)
if x < y
return ((y-x)*z)+x
else
return ((x-y)*z)+y
end if
end script
```

## Trig

### Basic

#### c_sin

Created by: SetaYoshi

Requisites: **modul, factorial**

Using taylor expansion to calculate sin

__y__ = c_sin(__x__*)*

```
script c_sin(x as double, return double)
dim n as integer
dim v as double
dim z as double = modul(x, 2*pi)
for n = 0 to 10
v = v + (-1)^n/factorial(2*n + 1)*z^(2*n + 1)
next
return v
end script
```

#### c_cos

Created by: SetaYoshi

Requisites: **modul, factorial**

Using taylor expansion to calculate cos

__y__ = c_cos(__x__*)*

```
script c_cos(x as double, return double)
dim n as integer
dim v as double
dim z as double = modul(x, 2*pi)
for n = 0 to 10
v = v + (-1)^n/factorial(2*n)*z^(2*n)
next
return v
end script
```

#### c_cos

Created by: SetaYoshi

Requisites: **modul, factorial**

Using taylor expansion to calculate cos

__y__ = c_cos(__x__*)*

```
script c_cos(x as double, return double)
dim n as integer
dim v as double
dim z as double = modul(x, 2*pi)
for n = 0 to 10
v = v + (-1)^n/factorial(2*n)*z^(2*n)
next
return v
end script
```

#### c_tan

Created by: SetaYoshi

Requisites: **modul, factorial**

Using taylor expansion to calculate tan

__y__ = c_tan(__x__*)*

```
script c_tan(x as double, return double)
dim n as integer
dim z as double = modul(x, 2*pi)
dim c as double
dim s as double
for n = 0 to 10
c = c + (-1)^n/factorial(2*n)*z^(2*n)
next
if c = 0 then return 0
for n = 0 to 10
s = s + (-1)^n/factorial(2*n + 1)*z^(2*n + 1)
next
return s/c
end script
```

#### c_csc

Created by: SetaYoshi

Requisites: **modul, factorial**

Using taylor expansion to calculate csc

__y__ = c_csc(__x__*)*

```
script c_csc(x as double, return double)
dim n as integer
dim v as double
dim z as double = modul(x, 2*pi)
for n = 0 to 10
v = v + (-1)^n/factorial(2*n + 1)*z^(2*n + 1)
next
if v = 0 then return 0
return 1/v
end script
```

#### c_sec

Created by: SetaYoshi

Requisites: **modul, factorial**

Using taylor expansion to calculate sec

__y__ = c_sec(__x__*)*

```
script c_sec(x as double, return double)
dim n as integer
dim v as double
dim z as double = modul(x, 2*pi)
for n = 0 to 10
v = v + (-1)^n/factorial(2*n)*z^(2*n)
next
if v = 0 then return 0
return 1/v
end script
```

#### c_cot

Created by: SetaYoshi

Requisites: **modul, factorial**

Using taylor expansion to calculate sec

__y__ = c_cot(__x__*)*

```
script c_cot(x as double, return double)
dim n as integer
dim z as double = modul(x, 2*pi)
dim c as double
dim s as double
for n = 0 to 10
s = s + (-1)^n/factorial(2*n + 1)*z^(2*n + 1)
next
if s = 0 then return 0
for n = 0 to 10
c = c + (-1)^n/factorial(2*n)*z^(2*n)
next
return c/s
end script
```

### Inverse Trig

#### arcsin

Created by: SetaYoshi

Requisites: **factorial**

Using taylor expansion to calculate arcsin

__y__ = arcsin(__x__*)*

```
script c_arcsin(x as double, return double)
dim n as integer
dim v as double
if abs(x) > 1 then return 0
for n = 0 to 10
v = v + factorial(2*n)/(4^n*factorial(n)^2*(2*n + 1))*x^(2*n + 1)
next
return v
end script
```

#### arccos

Created by: SetaYoshi

Requisites: **factorial**

Using taylor expansion to calculate sec

__y__ = arccos(__x__*)*

```
script c_arccos(x as double, return double)
dim n as integer
dim v as double
if abs(x) > 1 then return 0
for n = 0 to 10
v = v + factorial(2*n)/(4^n*factorial(n)^2*(2*n + 1))*x^(2*n + 1)
next
return pi/2 - v
end script
```

#### arctan

Created by: SetaYoshi

Requisites: **factorial**

Using taylor expansion to calculate sec

__y__ = arctan(__x__*)*

```
script arctan(x as double, return double)
dim n as integer
dim v as double
dim z as double = modul(x, 2*pi)
for n = 0 to 10
v = v + (-1)^n/(2*n + 1)*z^(2*n + 1)
next
return v
end script
```

#### arccsc

Created by: SetaYoshi

Requisites: **factorial**

Using taylor expansion to calculate sec

__y__ = arccsc(__x__*)*

```
script c_arccsc(x as double, return double)
dim n as integer
dim v as double
if abs(x) > 1 then return 0
dim z as double = 1/x
for n = 0 to 10
v = v + factorial(2*n)/(4^n*factorial(n)^2*(2*n + 1))*z^(2*n + 1)
next
return v
end script
```

#### arcsec

Created by: SetaYoshi

Requisites: **factorial**

Using taylor expansion to calculate sec

__y__ = arcsec(__x__*)*

```
script arcsec(x as double, return double)
dim n as integer
dim v as double
if abs(x) > 1 then return 0
dim z as double = 1/x
for n = 0 to 10
v = v + factorial(2*n)/(4^n*factorial(n)^2*(2*n + 1))*z^(2*n + 1)
next
return v
end script
```

#### arccot

Created by: SetaYoshi

Requisites: **factorial**

Using taylor expansion to calculate sec

__y__ = arccot(__x__*)*

```
script arccot(x as double, return double)
dim n as integer
dim v as double
dim z as double = modul(x, 2*pi)
for n = 0 to 10
v = v + (-1)^n/(2*n + 1)*z^(2*n + 1)
next
return pi/2 - v
end script
```

### Hyperbolic

#### sinh

Created by: SetaYoshi

Requisites: none

The hyperbolic form of sin

__y__ = sinh(__x__*)*

```
script c_sinh(x as double, return double)
return 0.5*(exp(x) - exp(-x))
end script
```

#### cosh

Created by: SetaYoshi

Requisites: none

The hyperbolic form of cos

__y__ = cosh(__x__*)*

```
script cosh(x as double, return double)
return 0.5*(exp(x) + exp(-x))
end script
```

#### tanh

Created by: SetaYoshi

Requisites: none

The hyperbolic form of tan

__y__ = tan(__x__*)*

```
script tanh(x as double, return double)
return (exp(x) - exp(-x))/(exp(x) - exp(-x))
end script
```

#### csch

Created by: SetaYoshi

Requisites: none

The hyperbolic form of csc

__y__ = csc(__x__*)*

```
script csch(x as double, return double)
return 2/(exp(x) - exp(-x))
end script
```

#### sech

Created by: SetaYoshi

Requisites: none

The hyperbolic form of sec

__y__ = sech(__x__*)*

```
script sech(x as double, return double)
return 2/(exp(x) + exp(-x))
end script
```

#### coth

Created by: SetaYoshi

Requisites: none

The hyperbolic form of sec

__y__ = coth(__x__*)*

```
script coth(x as double, return double)
return (exp(x) - exp(-x))/(exp(x) - exp(-x))
end script
```

### Hyperbolic Inverse

#### arcsinh

Created by: SetaYoshi

Requisites: none

The hyperbolic inverse form of sin

__y__ = arcsinh(__x__*)*

```
script arcsinh(x as double, return double)
return log(x + sqr(x^2 + 1))
end script
```

#### arccosh

Created by: SetaYoshi

Requisites: none

The hyperbolic inverse form of cos

__y__ = arccosh(__x__*)*

```
script arccosh(x as double, return double)
return log(x + sqr(x^2 - 1))
end script
```

#### arctanh

Created by: SetaYoshi

Requisites: none

The hyperbolic inverse form of tan

__y__ = arctanh(__x__*)*

```
script arctanh(x as double, return double)
return 0.5*log((1 + x)/(1 - x))
end script
```

#### arccsch

Created by: SetaYoshi

Requisites: none

The hyperbolic inverse form of csc

__y__ = arcscsh(__x__*)*

```
script arccsch(x as double, return double)
if x = 0 then return 0
return log((1 + sqr(1 + x^2))/x)
end script
```

#### arcsech

Created by: SetaYoshi

Requisites: none

The hyperbolic inverse form of sec

__y__ = arcsech(__x__*)*

```
script c_arcsech(x as double, return double)
if x = 0 then return 0
return log((1 + sqr(1 - x^2))/x)
end script
```

#### arccoth

Created by: SetaYoshi

Requisites: none

The hyperbolic inverse form of cot

__y__ = arccoth(__x__*)*

```
script c_arccoth(x as double, return double)
if x >= -1 and x <= 1 then return 0
return 0.5*log((x + 1)/(x - 1))
end script
```

## Detectors

### onscreen

Created by: YvajekK

Based on SetaYoshi's script

Requisites: None

Detects if the object is on the screen.
Returns:

- 0: when the object isn't visible
- 1: when the object is visible and the screen isn't splitted
- 2: when the object is visible on the second player's screen

__isvisible__ = onscreen(__x__, __y__, __width__, __height__)

```
script onscreen(x as double, y as double, w as double, h as double, return integer)
dim scrw as integer
dim scrh as integer
if sysval(scrsplitstyle) = 0 then
scrw = 800
scrh = 600
elseif sysval(scrsplitstyle) = 1 or sysval(scrsplitstyle) = 2 then
scrw = 800
scrh = 300
else
scrw = 400
scrh = 600
end if
if x <= sysval(player1scrx) + scrw and x + w >= sysval(player1scrx) and y <= sysval(player1scry) + scrh and y + h >= sysval(player1scry) then
return 1
elseif x <= sysval(player2scrx) + scrw and x + w >= sysval(player2scrx) and y <= sysval(player2scry) + scrh and y + h >= sysval(player2scry) then
return 2
end if
return 0
end script
```