Files
claudeandClaude Opus 5.5 25a06e8517
test / test (push) Successful in 57s
World item drops stay on the plane and fly to the avatar
- New strata_player/pickup.lua re-registers __builtin:item with a
  wrapped on_step: items are kept on z = PLANE_Z, and the nearest avatar
  within 3 nodes whose inventory has room pulls them in (physics off)
  and takes them within 0.6 nodes, with a pickup sound. What doesn't fit
  stays in the world and falls again.
- core.item_drop throws items out of the avatar in its facing direction
  instead of from the camera, with a 1.5 s pickup delay.
- 5 selftest cases in tests/pickup.lua. Item entities are static-saved,
  so the test forceloads its blocks (a headless server has no player to
  keep them active).

Closes #17

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 12:10:01 -05:00

145 lines
4.8 KiB
Lua

-- Strata item pickup: dropped items stay on the plane and fly to avatars.
--
-- The engine's item entity (__builtin:item) is only picked up by punching
-- it, and core.item_drop throws items from the hidden player body at the
-- camera. Here the item entity is re-registered with a wrapped on_step:
-- * items are kept on the plane (z = PLANE_Z), so drops from the wall
-- layer or the camera land where they can be reached;
-- * an avatar within MAGNET_RADIUS whose inventory has room pulls the item
-- in, with physics off, and takes it once it is within PICKUP_RADIUS;
-- * items a player drops come out of their avatar and are ignored by the
-- magnet for DROP_DELAY, so they don't jump straight back.
local PLANE_Z = strata.PLANE_Z
local MAGNET_RADIUS = 3 -- nodes from the avatar's centre
local PICKUP_RADIUS = 0.6
local MAGNET_ACCEL = 40 -- nodes/s² towards the avatar
local MAGNET_MAX_SPEED = 12
local DROP_DELAY = 1.5 -- seconds before a thrown item can be picked up
local THROW_SPEED = 3.5 -- sideways speed of a thrown item
local THROW_UP = 3
local THROW_HEIGHT = 0.4 -- above the avatar's centre
local PICKUP_GAIN = 0.4
strata.pickup = {
MAGNET_RADIUS = MAGNET_RADIUS,
PICKUP_RADIUS = PICKUP_RADIUS,
DROP_DELAY = DROP_DELAY,
}
--- The inventory an avatar picks items up into: its player's, or the
-- _test_inv of an ownerless selftest avatar. nil if it can't pick up now.
local function avatar_inventory(name, obj)
local player = core.get_player_by_name(name)
if player then
return player:get_hp() > 0 and player:get_inventory() or nil
end
local ent = obj:get_luaentity()
return ent and ent._test_inv
end
--- The nearest avatar within MAGNET_RADIUS of pos that has room for at
-- least one of stack. Returns name, avatar position and inventory.
function strata.item_magnet_target(pos, stack)
local one = ItemStack(stack)
one:set_count(1)
local best_name, best_pos, best_inv, best_d
for name, obj in pairs(strata.avatars) do
local apos = obj:get_pos()
local d = apos and vector.distance(pos, apos)
if d and d <= MAGNET_RADIUS and (not best_d or d < best_d) then
local inv = avatar_inventory(name, obj)
if inv and inv:room_for_item("main", one) then
best_name, best_pos, best_inv, best_d = name, apos, inv, d
end
end
end
return best_name, best_pos, best_inv
end
local builtin = core.registered_entities["__builtin:item"]
local item = {}
for k, v in pairs(builtin) do
item[k] = v
end
local function give(self, name, inv)
local left = inv:add_item("main", ItemStack(self.itemstring))
if core.get_player_by_name(name) then
core.sound_play("strata_pickup", {to_player = name, gain = PICKUP_GAIN}, true)
end
if left:is_empty() then
self.itemstring = ""
self.object:remove()
return true
end
self:set_item(left)
return false
end
function item:on_step(dtime, moveresult)
local obj = self.object
local pos = obj:get_pos()
if pos.z ~= PLANE_Z and not obj:get_attach() then
pos.z = PLANE_Z
obj:set_pos(pos)
local vel = obj:get_velocity()
vel.z = 0
obj:set_velocity(vel)
end
self._pickup_delay = math.max(0, (self._pickup_delay or 0) - dtime)
local name, apos, inv
if self._pickup_delay == 0 and self.itemstring ~= "" then
name, apos, inv = strata.item_magnet_target(pos, self.itemstring)
end
if name then
if vector.distance(pos, apos) <= PICKUP_RADIUS and give(self, name, inv) then
return
end
if not self._magnet then
self._magnet = true
self.force_out = nil
self:disable_physics()
end
self.age = 0
local dir = vector.direction(pos, apos)
local vel = vector.add(obj:get_velocity(), vector.multiply(dir, MAGNET_ACCEL * dtime))
-- only the component towards the avatar survives, so it doesn't orbit
local speed = math.min(math.max(vector.dot(vel, dir), 0), MAGNET_MAX_SPEED)
obj:set_velocity(vector.multiply(dir, speed))
return
end
if self._magnet then
-- lost the target: fall again from the next step (no moveresult now)
self._magnet = nil
self:enable_physics()
return
end
return builtin.on_step(self, dtime, moveresult)
end
core.register_entity(":__builtin:item", item)
--- Throw an item out of an avatar: dropping (Q, or out of the inventory)
-- would otherwise spawn it at the camera.
local builtin_item_drop = core.item_drop
function core.item_drop(itemstack, dropper, pos)
local name = dropper and dropper:is_player() and dropper:get_player_name()
local avatar = name and strata.get_avatar(name)
if not avatar then
return builtin_item_drop(itemstack, dropper, pos)
end
local apos = avatar:get_pos()
local facing = avatar:get_luaentity()._facing or 1
local obj = core.add_item(vector.offset(apos, 0, THROW_HEIGHT, 0), itemstack:take_item(itemstack:get_count()))
if not obj then
return
end
obj:set_velocity({x = facing * THROW_SPEED, y = THROW_UP, z = 0})
local ent = obj:get_luaentity()
ent.dropped_by = name
ent._pickup_delay = DROP_DELAY
return itemstack
end