2014-03-15 16:59:03 +00:00
|
|
|
// SONIC ROBO BLAST 2
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-05-18 00:42:11 +00:00
|
|
|
// Copyright (C) 2012-2016 by John "JTE" Muniz.
|
2023-03-31 12:53:31 +00:00
|
|
|
// Copyright (C) 2012-2023 by Sonic Team Junior.
|
2014-03-15 16:59:03 +00:00
|
|
|
//
|
|
|
|
// This program is free software distributed under the
|
|
|
|
// terms of the GNU General Public License, version 2.
|
|
|
|
// See the 'LICENSE' file for more details.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
/// \file lua_mathlib.c
|
|
|
|
/// \brief basic math library for Lua scripting
|
|
|
|
|
|
|
|
#include "doomdef.h"
|
|
|
|
//#include "fastcmp.h"
|
|
|
|
#include "tables.h"
|
|
|
|
#include "p_local.h"
|
|
|
|
#include "doomstat.h" // for ALL7EMERALDS
|
2021-02-13 17:04:12 +00:00
|
|
|
#include "r_main.h" // for R_PointToDist2
|
2021-04-20 00:42:00 +00:00
|
|
|
#include "m_easing.h"
|
2014-03-15 16:59:03 +00:00
|
|
|
|
|
|
|
#include "lua_script.h"
|
|
|
|
#include "lua_libs.h"
|
|
|
|
|
|
|
|
// General math
|
|
|
|
//////////////////
|
|
|
|
|
|
|
|
static int lib_abs(lua_State *L)
|
|
|
|
{
|
|
|
|
int a = (int)luaL_checkinteger(L, 1);
|
|
|
|
lua_pushinteger(L, abs(a));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_min(lua_State *L)
|
|
|
|
{
|
2016-05-06 02:23:46 +00:00
|
|
|
int a = luaL_checkinteger(L, 1);
|
|
|
|
int b = luaL_checkinteger(L, 2);
|
|
|
|
lua_pushinteger(L, min(a,b));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_max(lua_State *L)
|
|
|
|
{
|
2016-05-06 02:23:46 +00:00
|
|
|
int a = luaL_checkinteger(L, 1);
|
|
|
|
int b = luaL_checkinteger(L, 2);
|
|
|
|
lua_pushinteger(L, max(a,b));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Angle math
|
|
|
|
////////////////
|
|
|
|
|
|
|
|
static int lib_fixedangle(lua_State *L)
|
|
|
|
{
|
2015-05-21 03:54:04 +00:00
|
|
|
lua_pushangle(L, FixedAngle(luaL_checkfixed(L, 1)));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_anglefixed(lua_State *L)
|
|
|
|
{
|
2015-05-21 03:54:04 +00:00
|
|
|
lua_pushfixed(L, AngleFixed(luaL_checkangle(L, 1)));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_invangle(lua_State *L)
|
|
|
|
{
|
2015-05-21 03:54:04 +00:00
|
|
|
lua_pushangle(L, InvAngle(luaL_checkangle(L, 1)));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_finesine(lua_State *L)
|
|
|
|
{
|
2015-05-21 03:54:04 +00:00
|
|
|
lua_pushfixed(L, FINESINE((luaL_checkangle(L, 1)>>ANGLETOFINESHIFT) & FINEMASK));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_finecosine(lua_State *L)
|
|
|
|
{
|
2015-05-21 03:54:04 +00:00
|
|
|
lua_pushfixed(L, FINECOSINE((luaL_checkangle(L, 1)>>ANGLETOFINESHIFT) & FINEMASK));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_finetangent(lua_State *L)
|
|
|
|
{
|
2015-10-10 20:21:16 +00:00
|
|
|
// HACK: add ANGLE_90 to make tan() in Lua start at 0 like it should
|
|
|
|
// use & 4095 instead of & FINEMASK (8191), so it doesn't go out of the array's bounds
|
|
|
|
lua_pushfixed(L, FINETANGENT(((luaL_checkangle(L, 1)+ANGLE_90)>>ANGLETOFINESHIFT) & 4095));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-22 06:38:37 +00:00
|
|
|
static int lib_fixedasin(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_pushangle(L, -FixedAcos(luaL_checkfixed(L, 1)) + ANGLE_90);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-04-11 23:33:11 +00:00
|
|
|
static int lib_fixedacos(lua_State *L)
|
2021-04-11 23:29:14 +00:00
|
|
|
{
|
|
|
|
lua_pushangle(L, FixedAcos(luaL_checkfixed(L, 1)));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-03-15 16:59:03 +00:00
|
|
|
// Fixed math
|
|
|
|
////////////////
|
|
|
|
|
|
|
|
static int lib_fixedmul(lua_State *L)
|
|
|
|
{
|
2015-05-21 03:54:04 +00:00
|
|
|
lua_pushfixed(L, FixedMul(luaL_checkfixed(L, 1), luaL_checkfixed(L, 2)));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_fixedint(lua_State *L)
|
|
|
|
{
|
2015-05-21 03:54:04 +00:00
|
|
|
lua_pushinteger(L, FixedInt(luaL_checkfixed(L, 1)));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_fixeddiv(lua_State *L)
|
|
|
|
{
|
2016-04-18 21:50:15 +00:00
|
|
|
fixed_t i = luaL_checkfixed(L, 1);
|
|
|
|
fixed_t j = luaL_checkfixed(L, 2);
|
|
|
|
if (j == 0)
|
|
|
|
return luaL_error(L, "divide by zero");
|
|
|
|
lua_pushfixed(L, FixedDiv(i, j));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-28 13:19:35 +00:00
|
|
|
// TODO: 2.3: Delete
|
2014-03-15 16:59:03 +00:00
|
|
|
static int lib_fixedrem(lua_State *L)
|
|
|
|
{
|
2020-05-18 16:29:56 +00:00
|
|
|
LUA_Deprecated(L, "FixedRem(a, b)", "a % b");
|
|
|
|
lua_pushfixed(L, luaL_checkfixed(L, 1) % luaL_checkfixed(L, 2));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_fixedsqrt(lua_State *L)
|
|
|
|
{
|
2016-04-18 21:50:15 +00:00
|
|
|
fixed_t i = luaL_checkfixed(L, 1);
|
|
|
|
if (i < 0)
|
2016-04-19 04:59:33 +00:00
|
|
|
return luaL_error(L, "square root domain error");
|
2016-04-18 21:50:15 +00:00
|
|
|
lua_pushfixed(L, FixedSqrt(i));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_fixedhypot(lua_State *L)
|
|
|
|
{
|
2021-02-13 17:04:12 +00:00
|
|
|
lua_pushfixed(L, R_PointToDist2(0, 0, luaL_checkfixed(L, 1), luaL_checkfixed(L, 2)));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_fixedfloor(lua_State *L)
|
|
|
|
{
|
2015-05-21 03:54:04 +00:00
|
|
|
lua_pushfixed(L, FixedFloor(luaL_checkfixed(L, 1)));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_fixedtrunc(lua_State *L)
|
|
|
|
{
|
2015-05-21 03:54:04 +00:00
|
|
|
lua_pushfixed(L, FixedTrunc(luaL_checkfixed(L, 1)));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_fixedceil(lua_State *L)
|
|
|
|
{
|
2015-05-21 03:54:04 +00:00
|
|
|
lua_pushfixed(L, FixedCeil(luaL_checkfixed(L, 1)));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_fixedround(lua_State *L)
|
|
|
|
{
|
2015-05-21 03:54:04 +00:00
|
|
|
lua_pushfixed(L, FixedRound(luaL_checkfixed(L, 1)));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Misc. math
|
|
|
|
// (aka extra little funcs that don't quite fit in baselib)
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static int lib_getsecspecial(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_pushinteger(L, GETSECSPECIAL(luaL_checkinteger(L, 1), luaL_checkinteger(L, 2)));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lib_all7emeralds(lua_State *L)
|
|
|
|
{
|
2015-05-20 21:29:32 +00:00
|
|
|
lua_pushboolean(L, ALL7EMERALDS(luaL_checkinteger(L, 1)));
|
2014-03-15 16:59:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:00:26 +00:00
|
|
|
// Returns both color and signpost shade numbers!
|
2014-03-15 16:59:03 +00:00
|
|
|
static int lib_coloropposite(lua_State *L)
|
|
|
|
{
|
2020-05-24 00:29:07 +00:00
|
|
|
UINT16 colornum = (UINT16)luaL_checkinteger(L, 1);
|
2020-02-15 08:18:41 +00:00
|
|
|
if (!colornum || colornum >= numskincolors)
|
|
|
|
return luaL_error(L, "skincolor %d out of range (1 - %d).", colornum, numskincolors-1);
|
|
|
|
lua_pushinteger(L, skincolors[colornum].invcolor); // push color
|
|
|
|
lua_pushinteger(L, skincolors[colornum].invshade); // push sign shade index, 0-15
|
2014-03-15 16:59:03 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2021-04-20 00:42:00 +00:00
|
|
|
static luaL_Reg lib_math[] = {
|
2014-03-15 16:59:03 +00:00
|
|
|
{"abs", lib_abs},
|
|
|
|
{"min", lib_min},
|
|
|
|
{"max", lib_max},
|
|
|
|
{"sin", lib_finesine},
|
|
|
|
{"cos", lib_finecosine},
|
|
|
|
{"tan", lib_finetangent},
|
2021-06-22 06:38:37 +00:00
|
|
|
{"asin", lib_fixedasin},
|
2021-04-11 23:33:11 +00:00
|
|
|
{"acos", lib_fixedacos},
|
2014-03-15 16:59:03 +00:00
|
|
|
{"FixedAngle", lib_fixedangle},
|
2020-11-12 20:25:31 +00:00
|
|
|
{"fixangle" , lib_fixedangle},
|
2014-03-15 16:59:03 +00:00
|
|
|
{"AngleFixed", lib_anglefixed},
|
2020-11-12 20:25:31 +00:00
|
|
|
{"anglefix" , lib_anglefixed},
|
2014-03-15 16:59:03 +00:00
|
|
|
{"InvAngle", lib_invangle},
|
|
|
|
{"FixedMul", lib_fixedmul},
|
2020-11-12 20:25:31 +00:00
|
|
|
{"fixmul" , lib_fixedmul},
|
2014-03-15 16:59:03 +00:00
|
|
|
{"FixedInt", lib_fixedint},
|
2020-11-12 20:25:31 +00:00
|
|
|
{"fixint" , lib_fixedint},
|
2014-03-15 16:59:03 +00:00
|
|
|
{"FixedDiv", lib_fixeddiv},
|
2020-11-12 20:25:31 +00:00
|
|
|
{"fixdiv" , lib_fixeddiv},
|
2014-03-15 16:59:03 +00:00
|
|
|
{"FixedRem", lib_fixedrem},
|
2020-11-12 20:25:31 +00:00
|
|
|
{"fixrem" , lib_fixedrem},
|
2014-03-15 16:59:03 +00:00
|
|
|
{"FixedSqrt", lib_fixedsqrt},
|
2020-11-12 20:25:31 +00:00
|
|
|
{"fixsqrt" , lib_fixedsqrt},
|
2014-03-15 16:59:03 +00:00
|
|
|
{"FixedHypot", lib_fixedhypot},
|
2020-11-12 20:25:31 +00:00
|
|
|
{"fixhypot" , lib_fixedhypot},
|
2014-03-15 16:59:03 +00:00
|
|
|
{"FixedFloor", lib_fixedfloor},
|
2020-11-12 20:25:31 +00:00
|
|
|
{"fixfloor" , lib_fixedfloor},
|
2014-03-15 16:59:03 +00:00
|
|
|
{"FixedTrunc", lib_fixedtrunc},
|
2020-11-12 20:25:31 +00:00
|
|
|
{"fixtrunc" , lib_fixedtrunc},
|
2014-03-15 16:59:03 +00:00
|
|
|
{"FixedCeil", lib_fixedceil},
|
2020-11-12 20:25:31 +00:00
|
|
|
{"fixceil" , lib_fixedceil},
|
2014-03-15 16:59:03 +00:00
|
|
|
{"FixedRound", lib_fixedround},
|
2020-11-12 20:25:31 +00:00
|
|
|
{"fixround" , lib_fixedround},
|
2014-03-15 16:59:03 +00:00
|
|
|
{"GetSecSpecial", lib_getsecspecial},
|
|
|
|
{"All7Emeralds", lib_all7emeralds},
|
|
|
|
{"ColorOpposite", lib_coloropposite},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2021-04-20 00:42:00 +00:00
|
|
|
//
|
|
|
|
// Easing functions
|
|
|
|
//
|
|
|
|
|
|
|
|
#define EASINGFUNC(easetype) \
|
|
|
|
{ \
|
|
|
|
fixed_t start = 0; \
|
|
|
|
fixed_t end = FRACUNIT; \
|
|
|
|
fixed_t t = luaL_checkfixed(L, 1); \
|
|
|
|
int n = lua_gettop(L); \
|
|
|
|
if (n == 2) \
|
|
|
|
end = luaL_checkfixed(L, 2); \
|
|
|
|
else if (n >= 3) \
|
|
|
|
{ \
|
|
|
|
start = luaL_checkfixed(L, 2); \
|
|
|
|
end = luaL_checkfixed(L, 3); \
|
|
|
|
} \
|
|
|
|
lua_pushfixed(L, (Easing_ ## easetype)(t, start, end)); \
|
|
|
|
return 1; \
|
|
|
|
} \
|
|
|
|
|
|
|
|
static int lib_easelinear(lua_State *L) { EASINGFUNC(Linear) }
|
|
|
|
|
|
|
|
static int lib_easeinsine(lua_State *L) { EASINGFUNC(InSine) }
|
|
|
|
static int lib_easeoutsine(lua_State *L) { EASINGFUNC(OutSine) }
|
|
|
|
static int lib_easeinoutsine(lua_State *L) { EASINGFUNC(InOutSine) }
|
|
|
|
|
|
|
|
static int lib_easeinquad(lua_State *L) { EASINGFUNC(InQuad) }
|
|
|
|
static int lib_easeoutquad(lua_State *L) { EASINGFUNC(OutQuad) }
|
|
|
|
static int lib_easeinoutquad(lua_State *L) { EASINGFUNC(InOutQuad) }
|
|
|
|
|
|
|
|
static int lib_easeincubic(lua_State *L) { EASINGFUNC(InCubic) }
|
|
|
|
static int lib_easeoutcubic(lua_State *L) { EASINGFUNC(OutCubic) }
|
|
|
|
static int lib_easeinoutcubic(lua_State *L) { EASINGFUNC(InOutCubic) }
|
|
|
|
|
|
|
|
static int lib_easeinquart(lua_State *L) { EASINGFUNC(InQuart) }
|
|
|
|
static int lib_easeoutquart(lua_State *L) { EASINGFUNC(OutQuart) }
|
|
|
|
static int lib_easeinoutquart(lua_State *L) { EASINGFUNC(InOutQuart) }
|
|
|
|
|
|
|
|
static int lib_easeinquint(lua_State *L) { EASINGFUNC(InQuint) }
|
|
|
|
static int lib_easeoutquint(lua_State *L) { EASINGFUNC(OutQuint) }
|
|
|
|
static int lib_easeinoutquint(lua_State *L) { EASINGFUNC(InOutQuint) }
|
|
|
|
|
|
|
|
static int lib_easeinexpo(lua_State *L) { EASINGFUNC(InExpo) }
|
|
|
|
static int lib_easeoutexpo(lua_State *L) { EASINGFUNC(OutExpo) }
|
|
|
|
static int lib_easeinoutexpo(lua_State *L) { EASINGFUNC(InOutExpo) }
|
|
|
|
|
|
|
|
#undef EASINGFUNC
|
|
|
|
|
|
|
|
#define EASINGFUNC(easetype) \
|
|
|
|
{ \
|
|
|
|
boolean useparam = false; \
|
|
|
|
fixed_t param = 0; \
|
|
|
|
fixed_t start = 0; \
|
|
|
|
fixed_t end = FRACUNIT; \
|
|
|
|
fixed_t t = luaL_checkfixed(L, 1); \
|
|
|
|
int n = lua_gettop(L); \
|
|
|
|
if (n == 2) \
|
|
|
|
end = luaL_checkfixed(L, 2); \
|
|
|
|
else if (n >= 3) \
|
|
|
|
{ \
|
|
|
|
start = (fixed_t)luaL_optinteger(L, 2, start); \
|
|
|
|
end = (fixed_t)luaL_optinteger(L, 3, end); \
|
|
|
|
if ((n >= 4) && (useparam = (!lua_isnil(L, 4)))) \
|
|
|
|
param = luaL_checkfixed(L, 4); \
|
|
|
|
} \
|
|
|
|
if (useparam) \
|
|
|
|
lua_pushfixed(L, (Easing_ ## easetype ## Parameterized)(t, start, end, param)); \
|
|
|
|
else \
|
|
|
|
lua_pushfixed(L, (Easing_ ## easetype)(t, start, end)); \
|
|
|
|
return 1; \
|
|
|
|
} \
|
|
|
|
|
|
|
|
static int lib_easeinback(lua_State *L) { EASINGFUNC(InBack) }
|
|
|
|
static int lib_easeoutback(lua_State *L) { EASINGFUNC(OutBack) }
|
|
|
|
static int lib_easeinoutback(lua_State *L) { EASINGFUNC(InOutBack) }
|
|
|
|
|
|
|
|
#undef EASINGFUNC
|
|
|
|
|
|
|
|
static luaL_Reg lib_ease[] = {
|
|
|
|
{"linear", lib_easelinear},
|
|
|
|
|
|
|
|
{"insine", lib_easeinsine},
|
|
|
|
{"outsine", lib_easeoutsine},
|
|
|
|
{"inoutsine", lib_easeinoutsine},
|
|
|
|
|
|
|
|
{"inquad", lib_easeinquad},
|
|
|
|
{"outquad", lib_easeoutquad},
|
|
|
|
{"inoutquad", lib_easeinoutquad},
|
|
|
|
|
|
|
|
{"incubic", lib_easeincubic},
|
|
|
|
{"outcubic", lib_easeoutcubic},
|
|
|
|
{"inoutcubic", lib_easeinoutcubic},
|
|
|
|
|
|
|
|
{"inquart", lib_easeinquart},
|
|
|
|
{"outquart", lib_easeoutquart},
|
|
|
|
{"inoutquart", lib_easeinoutquart},
|
|
|
|
|
|
|
|
{"inquint", lib_easeinquint},
|
|
|
|
{"outquint", lib_easeoutquint},
|
|
|
|
{"inoutquint", lib_easeinoutquint},
|
|
|
|
|
|
|
|
{"inexpo", lib_easeinexpo},
|
|
|
|
{"outexpo", lib_easeoutexpo},
|
|
|
|
{"inoutexpo", lib_easeinoutexpo},
|
|
|
|
|
|
|
|
{"inback", lib_easeinback},
|
|
|
|
{"outback", lib_easeoutback},
|
|
|
|
{"inoutback", lib_easeinoutback},
|
|
|
|
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2014-03-15 16:59:03 +00:00
|
|
|
int LUA_MathLib(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
2021-04-20 00:42:00 +00:00
|
|
|
luaL_register(L, NULL, lib_math);
|
|
|
|
luaL_register(L, "ease", lib_ease);
|
2014-03-15 16:59:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|