This commit is contained in:
BuckarooBanzay 2023-06-20 14:48:30 +02:00
commit 3e95d5830e
12 changed files with 230 additions and 0 deletions

18
.github/workflows/luacheck.yml vendored Normal file
View File

@ -0,0 +1,18 @@
name: luacheck
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v2.3.4
- name: apt
run: sudo apt-get install -y luarocks
- name: luacheck install
run: luarocks install --local luacheck
- name: luacheck run
run: $HOME/.luarocks/bin/luacheck ./

15
.luacheckrc Normal file
View File

@ -0,0 +1,15 @@
globals = {
"pick_and_place"
}
read_globals = {
-- Stdlib
string = {fields = {"split", "trim"}},
table = {fields = {"copy", "getn"}},
-- Minetest
"vector", "ItemStack",
"dump", "dump2",
"VoxelArea",
"minetest"
}

3
common.lua Normal file
View File

@ -0,0 +1,3 @@
function pick_and_place.rotate_size(size, rotation)
end

32
entity.lua Normal file
View File

@ -0,0 +1,32 @@
-- id -> true
local active_entities = {}
minetest.register_entity("pick_and_place:display", {
initial_properties = {
physical = false,
static_save = false,
collisionbox = {0, 0, 0, 0, 0, 0},
visual = "upright_sprite",
visual_size = {x=10, y=10},
glow = 10
},
on_step = function(self)
if not active_entities[self.id] then
-- not valid anymore
self.object:remove()
end
end
})
function pick_and_place.add_entity(pos, id)
active_entities[id] = true
local ent = minetest.add_entity(pos, "pick_and_place:display")
local luaent = ent:get_luaentity()
luaent.id = id
return ent
end
function pick_and_place.remove_entities(id)
active_entities[id] = nil
end

11
init.lua Normal file
View File

@ -0,0 +1,11 @@
pick_and_place = {}
local MP = minetest.get_modpath("pick_and_place")
dofile(MP .. "/common.lua")
dofile(MP .. "/serialize.lua")
dofile(MP .. "/entity.lua")
dofile(MP .. "/node.lua")
dofile(MP .. "/tool.lua")
dofile(MP .. "/wield_check.lua")
dofile(MP .. "/preview.lua")

2
mod.conf Normal file
View File

@ -0,0 +1,2 @@
name = pick_and_place
description = Pick and place utility

3
node.lua Normal file
View File

@ -0,0 +1,3 @@
-- TODO: pickup node with WE marker selection formspec
-- TODO: dispense "pick_and_place:placer" tool with schematic, size and id

86
preview.lua Normal file
View File

@ -0,0 +1,86 @@
-- playername => key
local active_preview = {}
local function add_preview_entity(texture, key, visual_size, pos, rotation)
local ent = pick_and_place.add_entity(pos, key)
ent:set_properties({
visual_size = visual_size,
textures = {texture}
})
ent:set_rotation(rotation)
end
function pick_and_place.show_preview(playername, texture, color, pos1, pos2)
texture = texture .. "^[colorize:" .. color
local key =
minetest.pos_to_string(pos1) .. "/" ..
minetest.pos_to_string(pos2) .. "/" ..
texture
if active_preview[playername] == key then
-- already active on the same region
return
end
-- clear previous entities
pick_and_place.clear_preview(playername)
active_preview[playername] = key
local size = vector.subtract(pos2, pos1)
local half_size = vector.divide(size, 2) -- 8 .. n
-- z-
add_preview_entity(texture, key,
{x=size.x, y=size.y},
vector.add(pos1, {x=half_size.x-0.5, y=half_size.y-0.5, z=-0.5}),
{x=0, y=0, z=0}
)
-- z+
add_preview_entity(texture, key,
{x=size.x, y=size.y},
vector.add(pos1, {x=half_size.x-0.5, y=half_size.y-0.5, z=size.z-0.5}),
{x=0, y=0, z=0}
)
-- x-
add_preview_entity(texture, key,
{x=size.z, y=size.y},
vector.add(pos1, {x=-0.5, y=half_size.y-0.5, z=half_size.z-0.5}),
{x=0, y=math.pi/2, z=0}
)
-- x+
add_preview_entity(texture, key,
{x=size.z, y=size.y},
vector.add(pos1, {x=size.x-0.5, y=half_size.y-0.5, z=half_size.z-0.5}),
{x=0, y=math.pi/2, z=0}
)
-- y-
add_preview_entity(texture, key,
{x=size.x, y=size.z},
vector.add(pos1, {x=half_size.x-0.5, y=-0.5, z=half_size.z-0.5}),
{x=math.pi/2, y=0, z=0}
)
-- y+
add_preview_entity(texture, key,
{x=size.x, y=size.z},
vector.add(pos1, {x=half_size.x-0.5, y=size.y-0.5, z=half_size.z-0.5}),
{x=math.pi/2, y=0, z=0}
)
end
function pick_and_place.clear_preview(playername)
if active_preview[playername] then
pick_and_place.remove_entities(active_preview[playername])
active_preview[playername] = nil
end
end
minetest.register_on_leaveplayer(function(player)
pick_and_place.clear_preview(player:get_player_name())
end)

8
serialize.lua Normal file
View File

@ -0,0 +1,8 @@
function pick_and_place.serialize(pos1, pos2)
-- TODO
end
function pick_and_place.deserialize(origin, data, rotation)
-- TODO
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

15
tool.lua Normal file
View File

@ -0,0 +1,15 @@
minetest.register_tool("pick_and_place:placer", {
description = "Placement tool",
inventory_image = "pick_and_place_plus.png^[colorize:#00ff00",
stack_max = 1,
range = 0,
on_use = function(itemstack, player)
end,
})
function pick_and_place.on_step()
end
function pick_and_place.on_blur()
end

37
wield_check.lua Normal file
View File

@ -0,0 +1,37 @@
-- playername -> name
local last_wielded_item = {}
-- check for tools
local function wield_check()
for _, player in ipairs(minetest.get_connected_players()) do
local itemstack = player:get_wielded_item()
local playername = player:get_player_name()
local id, name
if itemstack then
name = itemstack:get_name()
local meta = itemstack:get_meta()
id = meta:get_int("id")
end
local is_placer = name == "pick_and_place:placer"
if last_wielded_item[playername] and name ~= last_wielded_item[playername] then
-- last item got out of focus
local item_def = minetest.registered_items[last_wielded_item[playername]]
if item_def and type(item_def.on_blur) == "function" then
item_def.on_blur(player)
end
end
if is_placer then
pick_and_place.on_step(itemstack, player)
end
last_wielded_item[playername] = name
end
minetest.after(0, wield_check)
end
minetest.after(0, wield_check)
minetest.register_on_leaveplayer(function(player)
last_wielded_item[player:get_player_name()] = nil
end)