2020-01-05 15:07:40 -05:00
|
|
|
-- LUALOCALS < ---------------------------------------------------------
|
2020-06-25 07:50:34 -04:00
|
|
|
local ItemStack, minetest, nodecore, pairs, vector
|
|
|
|
= ItemStack, minetest, nodecore, pairs, vector
|
2020-01-05 15:07:40 -05:00
|
|
|
-- LUALOCALS > ---------------------------------------------------------
|
|
|
|
|
2020-06-25 07:50:34 -04:00
|
|
|
local function addtodict(dict, key, item)
|
|
|
|
local entry = dict[key]
|
|
|
|
if not entry then
|
|
|
|
entry = {}
|
|
|
|
dict[key] = entry
|
|
|
|
end
|
|
|
|
entry[#entry + 1] = item
|
|
|
|
end
|
2020-01-05 15:07:40 -05:00
|
|
|
|
2020-06-25 07:50:34 -04:00
|
|
|
local function removesingles(dict)
|
|
|
|
local t = {}
|
|
|
|
for k, v in pairs(dict) do
|
|
|
|
if #v > 1 then t[k] = v end
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
2020-01-05 15:07:40 -05:00
|
|
|
|
2020-06-25 07:50:34 -04:00
|
|
|
minetest.register_globalstep(function()
|
|
|
|
local gethash = minetest.hash_node_position
|
|
|
|
local round = vector.round
|
2020-01-05 15:07:40 -05:00
|
|
|
|
2020-06-26 06:11:19 -04:00
|
|
|
local entpos = {}
|
|
|
|
local entvel = {}
|
|
|
|
|
2020-06-25 07:50:34 -04:00
|
|
|
local dict = {}
|
|
|
|
for _, ent in pairs(minetest.luaentities) do
|
|
|
|
if ent.name == "__builtin:item" then
|
|
|
|
local pos = ent.object:get_pos()
|
2020-06-26 06:11:19 -04:00
|
|
|
local vel = ent.object:get_velocity()
|
|
|
|
if pos and vel and vector.dot(vel, vel) < 4 then
|
|
|
|
entpos[ent] = pos
|
|
|
|
entvel[ent] = vel
|
|
|
|
addtodict(dict, gethash(round(pos)), ent)
|
|
|
|
end
|
2020-01-05 15:07:40 -05:00
|
|
|
end
|
|
|
|
end
|
2020-06-25 07:50:34 -04:00
|
|
|
dict = removesingles(dict)
|
|
|
|
|
|
|
|
for _, ents in pairs(dict) do
|
|
|
|
local groups = {}
|
|
|
|
for _, ent in pairs(ents) do
|
|
|
|
addtodict(groups, nodecore.stack_family(ent.itemstring), ent)
|
|
|
|
end
|
|
|
|
groups = removesingles(groups)
|
|
|
|
for _, grp in pairs(groups) do
|
2020-06-25 23:03:56 -04:00
|
|
|
local newpos = {x = 0, y = 0, z = 0}
|
|
|
|
local newvel = {x = 0, y = 0, z = 0}
|
|
|
|
local samples = 0
|
2020-06-25 07:50:34 -04:00
|
|
|
local max = ItemStack(grp[1].itemstring):get_stack_max()
|
|
|
|
local stacks = {}
|
|
|
|
local partial
|
|
|
|
local pqty = 0
|
|
|
|
for _, ent in pairs(grp) do
|
2020-06-25 23:03:56 -04:00
|
|
|
local stack = ItemStack(ent.itemstring)
|
|
|
|
local iqty = stack:get_count()
|
|
|
|
newpos = vector.add(newpos, vector.multiply(
|
2020-06-26 06:11:19 -04:00
|
|
|
entpos[ent], iqty))
|
2020-06-25 23:03:56 -04:00
|
|
|
newvel = vector.add(newvel, vector.multiply(
|
2020-06-26 06:11:19 -04:00
|
|
|
entvel[ent], iqty))
|
2020-06-25 23:03:56 -04:00
|
|
|
samples = samples + iqty
|
2020-06-25 07:50:34 -04:00
|
|
|
if not partial then
|
2020-06-25 23:03:56 -04:00
|
|
|
pqty = stack:get_count()
|
|
|
|
stack:set_count(max)
|
|
|
|
partial = stack:to_string()
|
2020-01-05 15:07:40 -05:00
|
|
|
else
|
2020-06-25 23:03:56 -04:00
|
|
|
pqty = pqty + iqty
|
2020-06-25 22:46:03 -04:00
|
|
|
while pqty >= max do
|
2020-06-25 07:50:34 -04:00
|
|
|
stacks[#stacks + 1] = partial
|
|
|
|
pqty = pqty - max
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if pqty > 0 then
|
2020-06-25 22:46:03 -04:00
|
|
|
partial = ItemStack(partial)
|
2020-06-25 07:50:34 -04:00
|
|
|
partial:set_count(pqty)
|
2020-06-25 22:46:03 -04:00
|
|
|
stacks[#stacks + 1] = partial:to_string()
|
2020-06-25 07:50:34 -04:00
|
|
|
end
|
2020-06-25 23:03:56 -04:00
|
|
|
newpos = vector.multiply(newpos, 1 / samples)
|
|
|
|
newvel = vector.multiply(newvel, 1 / samples)
|
2020-06-25 07:50:34 -04:00
|
|
|
for i = 1, #grp do
|
|
|
|
local stack = stacks[i]
|
2020-06-25 23:03:56 -04:00
|
|
|
local ent = grp[i]
|
2020-06-25 07:50:34 -04:00
|
|
|
if stack then
|
2020-06-25 23:03:56 -04:00
|
|
|
ent.itemstring = stack
|
|
|
|
ent.object:set_pos(newpos)
|
|
|
|
ent.object:set_velocity(newvel)
|
2020-06-25 07:50:34 -04:00
|
|
|
else
|
2020-06-25 23:03:56 -04:00
|
|
|
ent.itemstring = ""
|
|
|
|
ent.object:remove()
|
2020-01-05 15:07:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-06-25 07:50:34 -04:00
|
|
|
end)
|