Merge branch 'main' into animation

This commit is contained in:
MihailRis 2026-07-22 23:56:17 +03:00
commit 7c06a74244
67 changed files with 1231 additions and 260 deletions

View file

@ -1,8 +1,77 @@
local this = {}
local app = __vc_app
function this.equals(expected, fact)
local function diff_canvas(expected, fact)
this.equals(expected.width, fact.width, "image width")
this.equals(expected.width, fact.width, "image height")
local diff = Canvas({expected.width, expected.height})
local mismatches = 0
for y = 0, expected.height - 1 do
for x = 0, expected.width - 1 do
local ca = expected:at(x, y)
local cb = fact:at(x, y)
if ca == cb then
diff:set(x, y, ca)
else
diff:set(x, y, 0xff0000ff)
mismatches = mismatches + 1
end
end
end
return diff, mismatches
end
local TEST_ARTIFACTS_PATH = "export:test-results"
local function get_test_artifact_path(name)
local path = TEST_ARTIFACTS_PATH
if not file.isdir(path) then
file.mkdirs(path)
end
return file.join(path, name)
end
function this.equals(expected, fact, prefix)
local mt = getmetatable(expected)
if mt == Canvas then
local diff, mismatches = diff_canvas(expected, fact)
if mismatches > 0 then
prefix = prefix or app.script or base64.encode_urlsafe(random.bytes(6))
local path_diff = get_test_artifact_path(
string.format("%s.diff.png", prefix)
)
local path_fact = get_test_artifact_path(
string.format("%s.fact.png", prefix)
)
local path_expected = get_test_artifact_path(
string.format("%s.expected.png", prefix)
)
file.write_bytes(path_diff, diff:encode('png'))
file.write_bytes(path_fact, fact:encode('png'))
file.write_bytes(path_expected, expected:encode('png'))
local total_pixels = expected.width * expected.height
assert(mismatches == 0, string.format(
"%s pixels of %s (%s%%) are different, diff saved as %s",
mismatches,
total_pixels,
math.round(mismatches / total_pixels * 100, 2),
string.escape(path_diff))
)
end
return
end
if prefix then
prefix = prefix .. " "
else
prefix = ""
end
assert(fact == expected, string.format(
"(fact == expected) assertion failed\n Expected: %s\n Fact: %s",
prefix .. "(fact == expected) assertion failed\n Expected: %s\n Fact: %s",
expected, fact
))
end

View file

@ -12,11 +12,30 @@ FFI.cdef[[
} bytearray_t;
]]
local malloc = FFI.C.malloc
local free = FFI.C.free
local FFIBytearray
local bytearray_type
local allocated_bytes = 0
local GC_THRESHOLD = 50 * 1024 * 1024
local function malloc(size)
local raw = FFI.C.malloc(size)
if raw == nil then
return nil
end
allocated_bytes = allocated_bytes + size
if allocated_bytes >= GC_THRESHOLD then
collectgarbage("step", 200)
allocated_bytes = 0
end
return raw
end
local function grow_buffer(self, elems)
local new_capacity = math.ceil(self.capacity / 0.75 + elems)
local prev = self.bytes

View file

@ -35,3 +35,7 @@ function math.sum(...)
return sum
end
function math.sign(x)
return (x > 0) and 1 or (x < 0 and -1 or 0)
end

View file

@ -5,4 +5,25 @@ function util.create_demo_world(generator)
app.new_world("demo", "2019", generator or "core:default")
end
function util.check_vec3(expected, fact, name, delta)
delta = delta or 1e-4
for i=1,3 do
assert(math.abs(fact[i] - expected[i]) < delta,
name.."["..i.."] expected: "..expected[i]..", fact: "..fact[i])
end
end
function util.check_float(expected, fact, name, delta)
delta = delta or 1e-4
local actual_delta = math.abs(fact - expected)
assert(actual_delta < delta,
name.." expected: "..expected..", fact: "..fact..
" (delta: "..actual_delta.." > "..delta..")")
end
function util.check_int(expected, fact, name)
assert(fact == expected,
name.." expected: "..expected..", fact: "..fact)
end
return util

View file

@ -139,14 +139,16 @@ function get_dir() return mob_dir end
function is_flight() return flight end
function set_flight(flag) flight = flag end
function set_flight(flag)
flight = flag
body:set_gravity_scale(flight and 0.0 or props.gravity_scale)
end
local prev_angle = (vec2.angle({mob_dir[3], mob_dir[1]})) % 360
function on_physics_update(delta)
local grounded = body:is_grounded()
body:set_vdamping(flight)
body:set_gravity_scale(flight and 0.0 or props.gravity_scale)
body:set_linear_damping(
(flight or not grounded) and props.air_damping or props.ground_damping
)

View file

@ -58,16 +58,12 @@ local function process_player_inputs(pid, rot, delta)
end
end
local prepared = false
body:set_enabled(false)
mob.set_flight(true)
function on_player_set(pid)
mob.set_flight(player.is_flight(pid))
body:set_body_type(player.is_noclip(pid) and "kinematic" or "dynamic")
end
function on_physics_update(delta)
if not prepared then
prepared = true
body:set_enabled(true)
end
local pid = entity:get_player()
if pid == -1 then
return

View file

@ -312,6 +312,8 @@ entities.get_all = function(uids)
return stdcomp.get_all(uids)
end
end
world.raycast = entities.__world_raycast
entities.__world_raycast = nil
__vc_scripts_registry = require "core:internal/scripts_registry"