mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-10 13:31:49 +00:00
Merge branch 'main' into animation
This commit is contained in:
commit
1186011dcf
23 changed files with 346 additions and 119 deletions
|
|
@ -1,20 +1,42 @@
|
|||
local util = {}
|
||||
|
||||
local DROP_FORCE = 8
|
||||
local DROP_INIT_VEL = {0, 3, 0}
|
||||
util.DROP_FORCE = 8
|
||||
util.DROP_INIT_VEL = { 0, 3, 0 }
|
||||
util.DROP_MAX_ITEM_DIR_SHIFT_DEGREES = 65
|
||||
|
||||
---@param mode integer 0 - whole stack, 1 - single item, 2 - dupe whole stack
|
||||
function util.drop_from_slot(invid, slot, mode)
|
||||
if invid == 0 then
|
||||
return
|
||||
---@param dir_shift? vec2 direction shift for drop; x and y must be clamped to [-0.5, 0.5]
|
||||
local function calculate_item_drop_dir(pid, dir_shift)
|
||||
if not dir_shift then
|
||||
dir_shift = { 0, 0 }
|
||||
end
|
||||
local yaw, pitch = player.get_rot(pid)
|
||||
local q_yaw = quat.from_euler({ 0, yaw, 0 })
|
||||
local q_pitch = quat.from_euler({ pitch, 0, 0 })
|
||||
local q_rotation = quat.mul(q_yaw, q_pitch)
|
||||
|
||||
local q_shift = quat.from_euler({
|
||||
-dir_shift[2] * util.DROP_MAX_ITEM_DIR_SHIFT_DEGREES * 2,
|
||||
-dir_shift[1] * util.DROP_MAX_ITEM_DIR_SHIFT_DEGREES * 2,
|
||||
0,
|
||||
})
|
||||
quat.mul(q_rotation, q_shift, q_rotation)
|
||||
return quat.mul_vec3(q_rotation, { 0, 0, -1 })
|
||||
end
|
||||
|
||||
--- Create a drop from slot, set it velocity, and return it
|
||||
---@param mode integer 0 - whole stack, 1 - single item, 2 - dupe whole stack
|
||||
---@param dir_shift? vec2 direction shift for drop; x and y must be clamped to [-0.5, 0.5]
|
||||
---@return table? entity, string? error
|
||||
function util.drop_from_slot(pid, invid, slot, mode, dir_shift)
|
||||
if invid == 0 then
|
||||
return nil, "invid cannot be 0"
|
||||
end
|
||||
local pid = hud.get_player()
|
||||
if mode == 2 and not player.is_infinite_items(pid) then
|
||||
return
|
||||
return nil, "no permission to dupe items"
|
||||
end
|
||||
local itemid, itemcount = inventory.get(invid, slot)
|
||||
if itemid == 0 then
|
||||
return
|
||||
return nil, "no item in slot"
|
||||
end
|
||||
|
||||
local drop_itemcount = 1
|
||||
|
|
@ -30,25 +52,30 @@ function util.drop_from_slot(invid, slot, mode)
|
|||
local data = inventory.get_all_data(invid, slot)
|
||||
local pvel = { player.get_vel(pid) }
|
||||
local ppos = vec3.add({ player.get_pos(pid) }, { 0, 0.7, 0 })
|
||||
local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE)
|
||||
local drop = util.drop(ppos, itemid, drop_itemcount, data, 1.5)
|
||||
local dir = calculate_item_drop_dir(pid, dir_shift)
|
||||
local throw_force = vec3.mul(dir, util.DROP_FORCE)
|
||||
local drop, err = util.drop(ppos, itemid, drop_itemcount, data, 1.5)
|
||||
if not drop then
|
||||
return
|
||||
return nil, err
|
||||
end
|
||||
local velocity = vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))
|
||||
local velocity = vec3.add(throw_force, vec3.add(pvel, util.DROP_INIT_VEL))
|
||||
drop.rigidbody:set_vel(velocity)
|
||||
return drop
|
||||
end
|
||||
|
||||
---@return table? entity, string? error
|
||||
function util.drop(ppos, itemid, count, data, pickup_delay)
|
||||
if itemid == 0 or not itemid then
|
||||
return nil
|
||||
return nil, "item is empty"
|
||||
end
|
||||
return entities.spawn("base:drop", ppos, {base__drop={
|
||||
id=itemid,
|
||||
count=count,
|
||||
data=data,
|
||||
pickup_delay=pickup_delay
|
||||
}})
|
||||
return entities.spawn("base:drop", ppos, {
|
||||
base__drop = {
|
||||
id = itemid,
|
||||
count = count,
|
||||
data = data,
|
||||
pickup_delay = pickup_delay
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
function util.calc_loot(loot_table)
|
||||
|
|
@ -66,7 +93,9 @@ function util.calc_loot(loot_table)
|
|||
if count == 0 then
|
||||
goto continue
|
||||
end
|
||||
table.insert(results, {item=item.index(loot.item), count=count})
|
||||
table.insert(results, {
|
||||
item = item.index(loot.item), count = count
|
||||
})
|
||||
end
|
||||
::continue::
|
||||
end
|
||||
|
|
@ -78,7 +107,7 @@ function util.block_loot(blockid)
|
|||
if lootscheme then
|
||||
return util.calc_loot(lootscheme)
|
||||
end
|
||||
return {{item=block.get_picking_item(blockid), count=1}}
|
||||
return { { item = block.get_picking_item(blockid), count = 1 } }
|
||||
end
|
||||
|
||||
return util
|
||||
|
|
|
|||
|
|
@ -2,8 +2,13 @@ local base_util = require "util"
|
|||
|
||||
function on_hud_open()
|
||||
events.on("core:drop_outside_inventory", function(mode)
|
||||
local pid = hud.get_player()
|
||||
local invid = hud.get_exchange_inventory()
|
||||
base_util.drop_from_slot(invid, 0, mode)
|
||||
local vp = gui.get_viewport()
|
||||
local dir_shift = table.map(input.get_mouse_pos(), function(i, v)
|
||||
return v / vp[i] - 0.5
|
||||
end)
|
||||
base_util.drop_from_slot(pid, invid, 0, mode, dir_shift)
|
||||
end)
|
||||
input.add_callback("player.drop", function()
|
||||
if hud.is_paused() or hud.is_inventory_open() then
|
||||
|
|
@ -13,9 +18,9 @@ function on_hud_open()
|
|||
local invid, slot = player.get_inventory(pid)
|
||||
local mode = 1
|
||||
if input.is_pressed("key:left-ctrl") or input.is_pressed("key:right-ctrl") then
|
||||
mode = 0
|
||||
mode = 0
|
||||
end
|
||||
base_util.drop_from_slot(invid, slot, mode)
|
||||
base_util.drop_from_slot(pid, invid, slot, mode)
|
||||
end)
|
||||
rules.create("do-loot-non-player", true)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
color="#FFFFFF80" size="16" pos="4,6"
|
||||
hover-color="#1080FF"></image>
|
||||
<panel orientation="horizontal" gravity="top-right"
|
||||
size="76,24" padding="8" interval="8" color="0">
|
||||
size="76,24" padding="8,6,8,2" interval="8" color="0">
|
||||
<image id="saveIcon" src="gui/save" tooltip="@Save"
|
||||
enabled="false" interactive="true"
|
||||
hover-color="#1080FF"
|
||||
|
|
@ -31,6 +31,12 @@
|
|||
hover-color="#1080FF"
|
||||
onclick="run_current_file()"
|
||||
color="#FFFFFF80" size="16"></image>
|
||||
<!-- TODO: remove after 0.32-->
|
||||
<image src="gui/item" size="16"
|
||||
enabled="true" interactive="true"
|
||||
hover-color="#1080FF"
|
||||
tooltip="Fix old VCM regions (0.31 bug)"
|
||||
onclick="fix_vcm_regions()"></image>
|
||||
</panel>
|
||||
<label id="title" pos="26,8"></label>
|
||||
</container>
|
||||
|
|
|
|||
|
|
@ -113,6 +113,78 @@ local function reload_model(filename, name)
|
|||
assets.parse_model(file.ext(filename), document.editor.text, name)
|
||||
end
|
||||
|
||||
-- === TODO: remove after 0.32 ===
|
||||
local pattern =
|
||||
"region %(%s*"
|
||||
.. "([+-]?%d+%.?%d*[eE]?[+-]?%d*)%s*,%s*"
|
||||
.. "([+-]?%d+%.?%d*[eE]?[+-]?%d*)%s*,%s*"
|
||||
.. "([+-]?%d+%.?%d*[eE]?[+-]?%d*)%s*,%s*"
|
||||
.. "([+-]?%d+%.?%d*[eE]?[+-]?%d*)"
|
||||
.. "%s*%)"
|
||||
|
||||
local function replace1(x1, y1, x2, y2)
|
||||
return string.format("region (%s, %s, %s, %s)", x2, y2, x1, y1)
|
||||
end
|
||||
|
||||
local function replace2(x1, y1, x2, y2)
|
||||
return string.format("region (%s, %s, %s, %s)", x2, y1, x1, y2)
|
||||
end
|
||||
|
||||
function convert_vcm(text)
|
||||
local lines = {}
|
||||
local zpairs = {
|
||||
{"%(north%)", "%(south%)", "(TMPS)", "(TMPN)"},
|
||||
{"%,north", "%,south", ",TMPS", ",TMPN"},
|
||||
{"north%,", "south%,", "TMPS,", "TMPN,"},
|
||||
}
|
||||
local xpairs = {
|
||||
{"%(west%)", "%(east%)", "(TMPE)", "(TMPW)"},
|
||||
{"%,west", "%,east", ",TMPE", ",TMPW"},
|
||||
{"west%,", "east%,", "TMPE,", "TMPW,"},
|
||||
}
|
||||
|
||||
for i, pair in ipairs(zpairs) do
|
||||
text = text:gsub(pair[1], pair[3])
|
||||
text = text:gsub(pair[2], pair[4])
|
||||
end
|
||||
text = text:gsub("TMPS", "south")
|
||||
text = text:gsub("TMPN", "north")
|
||||
|
||||
for i, pair in ipairs(xpairs) do
|
||||
text = text:gsub(pair[1], pair[3])
|
||||
text = text:gsub(pair[2], pair[4])
|
||||
end
|
||||
text = text:gsub("TMPW", "west")
|
||||
text = text:gsub("TMPE", "east")
|
||||
|
||||
for line in text:gmatch("[^\n]*\n?") do
|
||||
if line == "" then
|
||||
break
|
||||
end
|
||||
|
||||
if line:find("(top)", 1, true) then
|
||||
line = line:gsub(pattern, replace1)
|
||||
end
|
||||
|
||||
if line:find("(bottom)", 1, true)
|
||||
or line:find("(east)", 1, true) then
|
||||
line = line:gsub(pattern, replace2)
|
||||
end
|
||||
|
||||
table.insert(lines, line)
|
||||
end
|
||||
|
||||
return table.concat(lines)
|
||||
end
|
||||
|
||||
function fix_vcm_regions()
|
||||
if not current_file.filename then
|
||||
return
|
||||
end
|
||||
document.editor.text = convert_vcm(document.editor.text)
|
||||
end
|
||||
-- ===============================
|
||||
|
||||
function run_current_file()
|
||||
if not current_file.filename then
|
||||
return
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
<inventory>
|
||||
<slots-grid pos="0, 164" rows="1" count="10" sharefunc="inventory_share_func"/>
|
||||
<slots-grid rows="3" start-index="10" count="30" sharefunc="inventory_share_func"/>
|
||||
<slots-grid pos="0, 164" rows="1" count="10" sharefunc="share_func"/>
|
||||
<slots-grid rows="3" start-index="10" count="30" sharefunc="share_func"/>
|
||||
</inventory>
|
||||
|
|
|
|||
|
|
@ -1,12 +1 @@
|
|||
function inventory_share_func(invid, slotid)
|
||||
local blockinv = hud.get_block_inventory()
|
||||
if blockinv ~= 0 then
|
||||
inventory.move(invid, slotid, blockinv)
|
||||
elseif rules.get("allow-content-access") then
|
||||
inventory.set(invid, slotid, 0, 0)
|
||||
elseif slotid < 10 then
|
||||
inventory.move_range(invid, slotid, invid, 10)
|
||||
else
|
||||
inventory.move_range(invid, slotid, invid, 0, 9)
|
||||
end
|
||||
end
|
||||
share_func = require("core:inventory_utils").share_default_func
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
@box from (0,0,0) to (1,0.5,1) delete (top,south)
|
||||
@box from (0,0.5,0) to (1,1,0.5) delete (bottom,south)
|
||||
@box from (0,0,0) to (1,0.5,1) delete (top,north)
|
||||
@box from (0,0.5,0) to (1,1,0.5) delete (bottom,north)
|
||||
@rect from (0,0.5,0.5) right (1,0,0) up (0,0,0.5) texture "$2"
|
||||
@rect from (0,0,0) right (1,0,0) up (0,1,0) texture "$1"
|
||||
|
|
|
|||
21
res/modules/inventory_utils.lua
Normal file
21
res/modules/inventory_utils.lua
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
local M = {}
|
||||
|
||||
function M.share_default_func(invid, slotid)
|
||||
local blockinv = hud.get_block_inventory()
|
||||
local playerinv = player.get_inventory(hud.get_player())
|
||||
if blockinv ~= 0 then
|
||||
if invid == blockinv then
|
||||
inventory.move(blockinv, slotid, playerinv)
|
||||
else
|
||||
inventory.move(invid, slotid, blockinv)
|
||||
end
|
||||
elseif rules.get("allow-content-access") then
|
||||
inventory.set(invid, slotid, 0, 0)
|
||||
elseif slotid < 10 then
|
||||
inventory.move_range(invid, slotid, invid, 10)
|
||||
else
|
||||
inventory.move_range(invid, slotid, invid, 0, 9)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
Loading…
Add table
Add a link
Reference in a new issue