Supercell.GUT/Supercell.GUT.Titan/Logic/Math/LogicMath.cs
BreadDEV 8c6a533918 [v0.0.2] you can enter menu now. but still early state
todo: improve code and finish base structures
2024-03-05 17:37:18 +07:00

151 lines
5 KiB
C#

namespace Supercell.GUT.Titan.Logic.Math;
public static class LogicMath
{
public static readonly int[] DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30];
public static readonly int[] SQRT_TABLE = [
0x00, 0x10, 0x16, 0x1B, 0x20, 0x23, 0x27, 0x2A, 0x2D,
0x30, 0x32, 0x35, 0x37, 0x39, 0x3B, 0x3D, 0x40, 0x41,
0x43, 0x45, 0x47, 0x49, 0x4B, 0x4C, 0x4E, 0x50, 0x51,
0x53, 0x54, 0x56, 0x57, 0x59, 0x5A, 0x5B, 0x5D, 0x5E,
0x60, 0x61, 0x62, 0x63, 0x65, 0x66, 0x67, 0x68, 0x6A,
0x6B, 0x6C, 0x6D, 0x6E, 0x70, 0x71, 0x72, 0x73, 0x74,
0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D,
0x7E, 0x80, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86,
0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
0x90, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x96,
0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9B, 0x9C, 0x9D, 0x9E,
0x9F, 0xA0, 0xA0, 0xA1, 0xA2, 0xA3, 0xA3, 0xA4, 0xA5,
0xA6, 0xA7, 0xA7, 0xA8, 0xA9, 0xAA, 0xAA, 0xAB, 0xAC,
0xAD, 0xAD, 0xAE, 0xAF, 0xB0, 0xB0, 0xB1, 0xB2, 0xB2,
0xB3, 0xB4, 0xB5, 0xB5, 0xB6, 0xB7, 0xB7, 0xB8, 0xB9,
0xB9, 0xBA, 0xBB, 0xBB, 0xBC, 0xBD, 0xBD, 0xBE, 0xBF,
0xC0, 0xC0, 0xC1, 0xC1, 0xC2, 0xC3, 0xC3, 0xC4, 0xC5,
0xC5, 0xC6, 0xC7, 0xC7, 0xC8, 0xC9, 0xC9, 0xCA, 0xCB,
0xCB, 0xCC, 0xCC, 0xCD, 0xCE, 0xCE, 0xCF, 0xD0, 0xD0,
0xD1, 0xD1, 0xD2, 0xD3, 0xD3, 0xD4, 0xD4, 0xD5, 0xD6,
0xD6, 0xD7, 0xD7, 0xD8, 0xD9, 0xD9, 0xDA, 0xDA, 0xDB,
0xDB, 0xDC, 0xDD, 0xDD, 0xDE, 0xDE, 0xDF, 0xE0, 0xE0,
0xE1, 0xE1, 0xE2, 0xE2, 0xE3, 0xE3, 0xE4, 0xE5, 0xE5,
0xE6, 0xE6, 0xE7, 0xE7, 0xE8, 0xE8, 0xE9, 0xEA, 0xEA,
0xEB, 0xEB, 0xEC, 0xEC, 0xED, 0xED, 0xEE, 0xEE, 0xEF,
0xF0, 0xF0, 0xF1, 0xF1, 0xF2, 0xF2, 0xF3, 0xF3, 0xF4,
0xF4, 0xF5, 0xF5, 0xF6, 0xF6, 0xF7, 0xF7, 0xF8, 0xF8,
0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC, 0xFD,
0xFD, 0xFE, 0xFE, 0xFF, 0x00, 0x00, 0x00
];
public static int Max(int a1, int a2)
{
return a1 > a2 ? a1 : a2;
}
public static int Min(int a1, int a2)
{
return a1 < a2 ? a1 : a2;
}
public static int GetDaysInMonth(int month, bool isLeapYear)
{
if (month != 1)
return LogicMath.DAYS_IN_MONTH[month];
if (!isLeapYear)
return LogicMath.DAYS_IN_MONTH[month];
return 29;
}
public static int Sqrt(int value)
{
int result;
int v4;
int v5;
int v6;
int v7;
if (value < 0x10000)
{
if (value < 256)
{
result = -1;
if (value >= 0)
return LogicMath.SQRT_TABLE[value] >> 4;
}
else
{
if (value < 4096)
{
if (value < 1024)
v7 = LogicMath.SQRT_TABLE[value & 0xFFFFFFFC] >> 3;
else
v7 = LogicMath.SQRT_TABLE[value >> 4] >> 2;
}
else if (value < 0x4000)
{
v7 = LogicMath.SQRT_TABLE[value >> 6] >> 1;
}
else
{
v7 = LogicMath.SQRT_TABLE[value >> 8];
}
result = v7 + 1;
if (result * result > value)
return v7;
}
}
else
{
if (value < 0x1000000)
{
if (value < 0x100000)
{
if (value < 0x40000)
v6 = 2 * LogicMath.SQRT_TABLE[value >> 0xA];
else
v6 = 4 * LogicMath.SQRT_TABLE[value >> 0xC];
}
else if (value < 0x400000)
{
v6 = 8 * LogicMath.SQRT_TABLE[value >> 0xE];
}
else
{
v6 = 16 * LogicMath.SQRT_TABLE[value >> 0x10];
}
v5 = value / v6 + (v6 | 1);
}
else
{
if (value < 0x10000000)
{
if (value < 0x4000000)
v4 = 32 * LogicMath.SQRT_TABLE[value >> 0x12];
else
v4 = LogicMath.SQRT_TABLE[value >> 0x14] << 6;
}
else if (value < 0x40000000)
{
v4 = LogicMath.SQRT_TABLE[value >> 0x16] << 7;
}
else
{
result = 0xFFFF;
if (value == 0x7FFFFFFF)
return result;
v4 = LogicMath.SQRT_TABLE[value >> 0x18] << 8;
}
v5 = value / (((v4 | 1) + value / v4) >> 1) + (((v4 | 1) + value / v4) >> 1) + 1;
}
return (v5 >> 1) - ((v5 >> 1) * (v5 >> 1) > value ? 1 : 0);
}
return result;
}
}