Divergent/mods/Weapon Cover Tilt/gamedata/scripts/demonized_randomizing_funct...

316 lines
7.0 KiB
Plaintext
Raw Permalink Normal View History

2024-03-17 20:18:03 -04:00
-- Library for picking random values between two numbers in various ways
-- Contains easing functions based on https://github.com/bbor/stingray-easing-functions/blob/master/engine/easing.c
-- Rewritten in Lua by demonized
-- Usage
--[[
local random_funcs = demonized_randomizing_functions
local a = random_funcs.get_random_value(10, 30, 2)
local b = random_funcs.get_random_value(30, 52.69, random_funcs.QuadraticEaseInOut)
--]]
local sin = math.sin
local cos = math.cos
local sqrt = math.sqrt
local random = math.random
local M_PI = math.pi
local M_PI_2 = M_PI / 2
-- Pick a random float between min_cond and max_cond
-- If magnitude is number and > 1, then random values will be closer to min_cond
-- If magnitude is number and < 1, then random values will be closer to max_cond
-- If magnitude is easing function, then random value will be picked according to function graph
-- For more info on easing function, google easing or cubic-bezier functions or look here: https://github.com/bbor/stingray-easing-functions
function get_random_value(min_cond, max_cond, magnitude)
local min_cond = min_cond or 0
local max_cond = max_cond or 1
local magnitude = magnitude or 1
local rand = 1
if type(magnitude) == "function" then
rand = magnitude(random())
else
rand = random() ^ magnitude
end
if min_cond > max_cond then
max_cond, min_cond = min_cond, max_cond
end
local a = max_cond - min_cond
local b = a * rand
local c = min_cond + b
return c
end
-- Easing functions, you can use them for third argument in get_random_value or directlyin your script
-- Modeled after the line y = x
function LinearInterpolation(p)
return p
end
-- Modeled after the parabola y = x^2
function QuadraticEaseIn(p)
return p * p
end
-- Modeled after the parabola y = -x^2 + 2x
function QuadraticEaseOut(p)
return -(p * (p - 2))
end
-- Modeled after the piecewise quadratic
-- y = (1/2)((2x)^2) [0, 0.5)
-- y = -(1/2)((2x-1)*(2x-3) - 1) [0.5, 1]
function QuadraticEaseInOut(p)
if(p < 0.5) then
return 2 * p * p
else
return (-2 * p * p) + (4 * p) - 1
end
end
-- Modeled after the cubic y = x^3
function CubicEaseIn(p)
return p * p * p
end
-- Modeled after the cubic y = (x - 1)^3 + 1
function CubicEaseOut(p)
local f = (p - 1)
return f * f * f + 1
end
-- Modeled after the piecewise cubic
-- y = (1/2)((2x)^3) [0, 0.5)
-- y = (1/2)((2x-2)^3 + 2) [0.5, 1]
function CubicEaseInOut(p)
if(p < 0.5) then
return 4 * p * p * p
else
local f = ((2 * p) - 2)
return 0.5 * f * f * f + 1
end
end
-- Modeled after the quartic x^4
function QuarticEaseIn(p)
return p * p * p * p
end
-- Modeled after the quartic y = 1 - (x - 1)^4
function QuarticEaseOut(p)
local f = (p - 1)
return f * f * f * (1 - p) + 1
end
-- Modeled after the piecewise quartic
-- y = (1/2)((2x)^4) [0, 0.5)
-- y = -(1/2)((2x-2)^4 - 2) [0.5, 1]
function QuarticEaseInOut(p)
if(p < 0.5) then
return 8 * p * p * p * p
else
local f = (p - 1)
return -8 * f * f * f * f + 1
end
end
-- Modeled after the quintic y = x^5
function QuinticEaseIn(p)
return p * p * p * p * p
end
-- Modeled after the quintic y = (x - 1)^5 + 1
function QuinticEaseOut(p)
local f = (p - 1)
return f * f * f * f * f + 1
end
-- Modeled after the piecewise quintic
-- y = (1/2)((2x)^5) [0, 0.5)
-- y = (1/2)((2x-2)^5 + 2) [0.5, 1]
function QuinticEaseInOut(p)
if(p < 0.5) then
return 16 * p * p * p * p * p
else
local f = ((2 * p) - 2)
return 0.5 * f * f * f * f * f + 1
end
end
-- Modeled after quarter-cycle of sine wave
function SineEaseIn(p)
return sin((p - 1) * M_PI_2) + 1
end
-- Modeled after quarter-cycle of sine wave (different phase)
function SineEaseOut(p)
return sin(p * M_PI_2)
end
-- Modeled after half sine wave
function SineEaseInOut(p)
return 0.5 * (1 - cos(p * M_PI))
end
-- Modeled after shifted quadrant IV of unit circle
function CircularEaseIn(p)
return 1 - sqrt(1 - (p * p))
end
-- Modeled after shifted quadrant II of unit circle
function CircularEaseOut(p)
return sqrt((2 - p) * p)
end
-- CircularEaseOut with adjustable power
function CircularEaseOutPowered(p, power)
local power = power or 0.5
return ((2 - p) * p) ^ power
end
-- Modeled after the piecewise circular function
-- y = (1/2)(1 - sqrt(1 - 4x^2)) [0, 0.5)
-- y = (1/2)(sqrt(-(2x - 3)*(2x - 1)) + 1) [0.5, 1]
function CircularEaseInOut(p)
if(p < 0.5) then
return 0.5 * (1 - sqrt(1 - 4 * (p * p)))
else
return 0.5 * (sqrt(-((2 * p) - 3) * ((2 * p) - 1)) + 1)
end
end
-- Modeled after the exponential function y = 2^(10(x - 1))
function ExponentialEaseIn(p)
return (p == 0.0) and p or 2 ^ (10 * (p - 1))
end
-- Modeled after the exponential function y = -2^(-10x) + 1
function ExponentialEaseOut(p)
return (p == 1.0) and p or 1 - 2 ^ (-10 * p)
end
-- Modeled after the piecewise exponential
-- y = (1/2)2^(10(2x - 1)) [0,0.5)
-- y = -(1/2)*2^(-10(2x - 1))) + 1 [0.5,1]
function ExponentialEaseInOut(p)
if(p == 0.0 or p == 1.0) then
return p
end
if(p < 0.5) then
return 0.5 * 2^( (20 * p) - 10)
else
return -0.5 * 2^( (-20 * p) + 10) + 1
end
end
-- Modeled after the damped sine wave y = sin(13pi/2*x)*pow(2, 10 * (x - 1))
function ElasticEaseIn(p)
return sin(13 * M_PI_2 * p) * 2^( 10 * (p - 1))
end
-- Modeled after the damped sine wave y = sin(-13pi/2*(x + 1))*pow(2, -10x) + 1
function ElasticEaseOut(p)
return sin(-13 * M_PI_2 * (p + 1)) * 2^( -10 * p) + 1
end
-- Modeled after the piecewise exponentially-damped sine wave:
-- y = (1/2)*sin(13pi/2*(2*x))*pow(2, 10 * ((2*x) - 1)) [0,0.5)
-- y = (1/2)*(sin(-13pi/2*((2x-1)+1))*pow(2,-10(2*x-1)) + 2) [0.5, 1]
function ElasticEaseInOut(p)
if(p < 0.5) then
return 0.5 * sin(13 * M_PI_2 * (2 * p)) * 2^(10 * ((2 * p) - 1))
else
return 0.5 * (sin(-13 * M_PI_2 * ((2 * p - 1) + 1)) * 2^(-10 * (2 * p - 1)) + 2)
end
end
-- Modeled after the overshooting cubic y = x^3-x*sin(x*pi)
function BackEaseIn(p)
return p * p * p - p * sin(p * M_PI)
end
-- Modeled after overshooting cubic y = 1-((1-x)^3-(1-x)*sin((1-x)*pi))
function BackEaseOut(p)
local f = (1 - p)
return 1 - (f * f * f - f * sin(f * M_PI))
end
function BackEaseOutQuadratic(p)
local f = (1 - p)
return 1 - (f * f - f * sin(f * M_PI))
end
-- Modeled after the piecewise overshooting cubic function:
-- y = (1/2)*((2x)^3-(2x)*sin(2*x*pi)) [0, 0.5)
-- y = (1/2)*(1-((1-x)^3-(1-x)*sin((1-x)*pi))+1) [0.5, 1]
function BackEaseInOut(p)
if(p < 0.5) then
local f = 2 * p
return 0.5 * (f * f * f - f * sin(f * M_PI))
else
local f = (1 - (2*p - 1))
return 0.5 * (1 - (f * f * f - f * sin(f * M_PI))) + 0.5
end
end
function BounceEaseIn(p)
return 1 - BounceEaseOut(1 - p)
end
function BounceEaseOut(p)
if(p < 4/11.0) then
return (121 * p * p)/16.0
elseif(p < 8/11.0) then
return (363/40.0 * p * p) - (99/10.0 * p) + 17/5.0
elseif(p < 9/10.0) then
return (4356/361.0 * p * p) - (35442/1805.0 * p) + 16061/1805.0
else
return (54/5.0 * p * p) - (513/25.0 * p) + 268/25.0
end
end
function BounceEaseInOut(p)
if(p < 0.5) then
return 0.5 * BounceEaseIn(p*2)
else
return 0.5 * BounceEaseOut(p * 2 - 1) + 0.5
end
end