diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt index d690156a..70d68420 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt @@ -393,6 +393,11 @@ class Starbound : ISBFileLocator { 1 } + state.setTableFunction("imageSpaces", this) { args -> + // List root.imageSpaces(String imagePath, Vec2F worldPosition, float spaceScan, bool flip) + TODO() + } + state.pop() state.load(polyfill, "@starbound.jar!/scripts/polyfill.lua") diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/JsonFunction.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/JsonFunction.kt index 0508a039..cee00c41 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/JsonFunction.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/JsonFunction.kt @@ -83,8 +83,10 @@ class JsonFunction( when (interpolation) { JsonFunctionInterpolation.LINEAR -> { - if (x < ranges[0].x) { + if (x <= lowerBound) { return ranges[0].y + } else if (x >= upperBound) { + return ranges.last().y } for (i in 0 until ranges.size - 1) { diff --git a/src/test/java/ru/dbotthepony/kstarbound/test/JsonFunctionTest.java b/src/test/java/ru/dbotthepony/kstarbound/test/JsonFunctionTest.java new file mode 100644 index 00000000..80d09c46 --- /dev/null +++ b/src/test/java/ru/dbotthepony/kstarbound/test/JsonFunctionTest.java @@ -0,0 +1,306 @@ +package ru.dbotthepony.kstarbound.test; + +import com.google.gson.JsonObject; +import com.google.gson.internal.bind.JsonTreeReader; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import ru.dbotthepony.kstarbound.Starbound; +import ru.dbotthepony.kstarbound.defs.JsonFunction; + +import java.util.HashMap; +import java.util.Locale; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class JsonFunctionTest { + private double[] drawData() { + return new double[] { + 0.000000, 0.000000, + 0.000500, 0.005000, + 0.001000, 0.010000, + 0.001500, 0.015000, + 0.002000, 0.020000, + 0.002500, 0.025000, + 0.003000, 0.030000, + 0.003500, 0.035000, + 0.004000, 0.040000, + 0.004500, 0.045000, + 0.005000, 0.050000, + 0.005500, 0.055000, + 0.006000, 0.060000, + 0.006500, 0.065000, + 0.007000, 0.070000, + 0.007500, 0.075000, + 0.008000, 0.080000, + 0.008500, 0.085000, + 0.009000, 0.090000, + 0.009500, 0.095000, + 0.010000, 0.100000, + 0.022000, 0.105000, + 0.034000, 0.110000, + 0.046000, 0.115000, + 0.058000, 0.120000, + 0.070000, 0.125000, + 0.082000, 0.130000, + 0.094000, 0.135000, + 0.106000, 0.140000, + 0.118000, 0.145000, + 0.130000, 0.150000, + 0.142000, 0.155000, + 0.154000, 0.160000, + 0.166000, 0.165000, + 0.178000, 0.170000, + 0.190000, 0.175000, + 0.202000, 0.180000, + 0.214000, 0.185000, + 0.226000, 0.190000, + 0.238000, 0.195000, + 0.250000, 0.200000, + 0.255435, 0.205000, + 0.260870, 0.210000, + 0.266304, 0.215000, + 0.271739, 0.220000, + 0.277174, 0.225000, + 0.282609, 0.230000, + 0.288043, 0.235000, + 0.293478, 0.240000, + 0.298913, 0.245000, + 0.304348, 0.250000, + 0.309783, 0.255000, + 0.315217, 0.260000, + 0.320652, 0.265000, + 0.326087, 0.270000, + 0.331522, 0.275000, + 0.336957, 0.280000, + 0.342391, 0.285000, + 0.347826, 0.290000, + 0.353261, 0.295000, + 0.358696, 0.300000, + 0.364130, 0.305000, + 0.369565, 0.310000, + 0.375000, 0.315000, + 0.380435, 0.320000, + 0.385870, 0.325000, + 0.391304, 0.330000, + 0.396739, 0.335000, + 0.402174, 0.340000, + 0.407609, 0.345000, + 0.413043, 0.350000, + 0.418478, 0.355000, + 0.423913, 0.360000, + 0.429348, 0.365000, + 0.434783, 0.370000, + 0.440217, 0.375000, + 0.445652, 0.380000, + 0.451087, 0.385000, + 0.456522, 0.390000, + 0.461957, 0.395000, + 0.467391, 0.400000, + 0.472826, 0.405000, + 0.478261, 0.410000, + 0.483696, 0.415000, + 0.489130, 0.420000, + 0.494565, 0.425000, + 0.500000, 0.430000, + 0.505435, 0.435000, + 0.510870, 0.440000, + 0.516304, 0.445000, + 0.521739, 0.450000, + 0.527174, 0.455000, + 0.532609, 0.460000, + 0.538043, 0.465000, + 0.543478, 0.470000, + 0.548913, 0.475000, + 0.554348, 0.480000, + 0.559783, 0.485000, + 0.565217, 0.490000, + 0.570652, 0.495000, + 0.576087, 0.500000, + 0.581522, 0.505000, + 0.586957, 0.510000, + 0.592391, 0.515000, + 0.597826, 0.520000, + 0.603261, 0.525000, + 0.608696, 0.530000, + 0.614130, 0.535000, + 0.619565, 0.540000, + 0.625000, 0.545000, + 0.630435, 0.550000, + 0.635870, 0.555000, + 0.641304, 0.560000, + 0.646739, 0.565000, + 0.652174, 0.570000, + 0.657609, 0.575000, + 0.663043, 0.580000, + 0.668478, 0.585000, + 0.673913, 0.590000, + 0.679348, 0.595000, + 0.684783, 0.600000, + 0.690217, 0.605000, + 0.695652, 0.610000, + 0.701087, 0.615000, + 0.706522, 0.620000, + 0.711957, 0.625000, + 0.717391, 0.630000, + 0.722826, 0.635000, + 0.728261, 0.640000, + 0.733696, 0.645000, + 0.739130, 0.650000, + 0.744565, 0.655000, + 0.750000, 0.660000, + 0.755435, 0.665000, + 0.760870, 0.670000, + 0.766304, 0.675000, + 0.771739, 0.680000, + 0.777174, 0.685000, + 0.782609, 0.690000, + 0.788043, 0.695000, + 0.793478, 0.700000, + 0.798913, 0.705000, + 0.804348, 0.710000, + 0.809783, 0.715000, + 0.815217, 0.720000, + 0.820652, 0.725000, + 0.826087, 0.730000, + 0.831522, 0.735000, + 0.836957, 0.740000, + 0.842391, 0.745000, + 0.847826, 0.750000, + 0.853261, 0.755000, + 0.858696, 0.760000, + 0.864130, 0.765000, + 0.869565, 0.770000, + 0.875000, 0.775000, + 0.880435, 0.780000, + 0.885870, 0.785000, + 0.891304, 0.790000, + 0.896739, 0.795000, + 0.902174, 0.800000, + 0.907609, 0.805000, + 0.913043, 0.810000, + 0.918478, 0.815000, + 0.923913, 0.820000, + 0.929348, 0.825000, + 0.934783, 0.830000, + 0.940217, 0.835000, + 0.945652, 0.840000, + 0.951087, 0.845000, + 0.956522, 0.850000, + 0.961957, 0.855000, + 0.967391, 0.860000, + 0.972826, 0.865000, + 0.978261, 0.870000, + 0.983696, 0.875000, + 0.989130, 0.880000, + 0.994565, 0.885000, + 1.000000, 0.890000, + 1.125000, 0.895000, + 1.250000, 0.900000, + 1.250000, 0.905000, + 1.250000, 0.910000, + 1.250000, 0.915000, + 1.250000, 0.920000, + 1.250000, 0.925000, + 1.250000, 0.930000, + 1.250000, 0.935000, + 1.250000, 0.940000, + 1.250000, 0.945000, + 1.250000, 0.950000, + 1.250000, 0.955000, + 1.250000, 0.960000, + 1.250000, 0.965000, + 1.250000, 0.970000, + 1.250000, 0.975000, + 1.250000, 0.980000, + 1.250000, 0.985000, + 1.250000, 0.990000, + 1.250000, 0.995000, + 1.250000, 1.000000, + 1.250000, 1.005000, + 1.250000, 1.010000, + 1.250000, 1.015000, + 1.250000, 1.020000, + 1.250000, 1.025000, + 1.250000, 1.030000, + 1.250000, 1.035000, + 1.250000, 1.040000, + 1.250000, 1.045000, + 1.250000, 1.050000, + 1.250000, 1.055000, + 1.250000, 1.060000, + 1.250000, 1.065000, + 1.250000, 1.070000, + 1.250000, 1.075000, + 1.250000, 1.080000, + 1.250000, 1.085000, + 1.250000, 1.090000, + 1.250000, 1.095000, + 1.250000, 1.100000, + 1.125000, 1.105000, + 1.000000, 1.110000, + 1.000000, 1.115000, + 1.000000, 1.120000, + 1.000000, 1.125000, + 1.000000, 1.130000, + 1.000000, 1.135000, + 1.000000, 1.140000, + 1.000000, 1.145000, + 1.000000, 1.150000, + 1.000000, 1.155000, + 1.000000, 1.160000, + 1.000000, 1.165000, + 1.000000, 1.170000, + 1.000000, 1.175000, + 1.000000, 1.180000, + 1.000000, 1.185000, + 1.000000, 1.190000, + 1.000000, 1.195000, + 1.000000, 1.200000, + }; + } + + @Test + @DisplayName("Json functions") + public void test() { + var sb = new Starbound(); + + var fnData = """ + { + "drawTimeMultiplier" : [ "linear", "clamp", [0, 0], [0.1, 0.01], [0.2, 0.25], [0.89, 1.0], [0.9, 1.25], [1.1, 1.25], [1.11, 1.0], [1.2, 1.0] ], + "normalizedDrawTimeMultiplier" : [ "linear", "clamp", [0, 0], [0.1, 0.01], [0.2, 0.25], [0.89, 0.9], [0.9, 1.0] ], + "bowImageSelector" : [ "linear", "clamp", [0, 0], [0.9, 4.0], [1.1, 4.0], [1.11, 3.9], [1.2, 3.9] ], + "bowImageSelectorNoPower" : [ "linear", "clamp", [0, 0], [0.9, 3.0] ], + "poisonBowImageSelector" : [ "linear", "clamp", [0, 0], [0.9, 4.0], [1.1, 4.0], [1.11, 5.0], [1.2, 5.0] ], + "poisonBowImageSelectorNoPower" : [ "linear", "clamp", [0, 0], [0.9, 3.0], [0.9001, 5.0] ], + "constant" : [ "linear", "clamp", [0, 1.0] ] + } + """; + + var data = sb.getGson().fromJson(fnData, JsonObject.class); + + var funcs = new HashMap(); + + for (var entry : data.entrySet()) { + funcs.put(entry.getKey(), sb.getGson().fromJson(new JsonTreeReader(entry.getValue()), JsonFunction.class)); + } + + var constant = funcs.get("constant"); + + for (int i = -10; i <= 10; i++) { + assertEquals(1.0, constant.evaluate(i)); + } + + var drawTimeMultiplier = funcs.get("drawTimeMultiplier"); + var drawTimeMultiplierData = drawData(); + + for (int i = 0; i < drawTimeMultiplierData.length; i += 2) { + double result = drawTimeMultiplierData[i]; + double x = drawTimeMultiplierData[i + 1]; + double evaluated = drawTimeMultiplier.evaluate(x); + + if (!String.format(Locale.ROOT, "%.4f", result).equals(String.format(Locale.ROOT, "%.4f", evaluated))) { + throw new RuntimeException("Evaluation of function returned " + String.format(Locale.ROOT, "%.4f", evaluated) + " while " + String.format(Locale.ROOT, "%.4f", result) + " was expected for input " + x); + } + } + } +}