techage/move_controller/movecontroller.lua

336 lines
9.9 KiB
Lua
Raw Normal View History

2021-11-15 21:26:59 +03:00
--[[
TechAge
=======
2022-06-06 21:39:55 +03:00
Copyright (C) 2020-2022 Joachim Stolberg
2021-11-15 21:26:59 +03:00
AGPL v3
See LICENSE.txt for more information
TA4 Move Controller
2022-01-03 23:40:31 +03:00
2021-11-15 21:26:59 +03:00
]]--
-- for lazy programmers
local M = minetest.get_meta
local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local S2P = minetest.string_to_pos
local S = techage.S
local MP = minetest.get_modpath("techage")
2022-01-03 23:40:31 +03:00
local mark = dofile(MP .. "/basis/mark_lib.lua")
local fly = techage.flylib
2021-11-15 21:26:59 +03:00
local MAX_DIST = 200
2021-11-15 21:26:59 +03:00
local MAX_BLOCKS = 16
local WRENCH_MENU = {
{
type = "dropdown",
choices = "0.5,1,2,4,6,8",
name = "max_speed",
2022-01-03 23:40:31 +03:00
label = S("Maximum Speed"),
2021-11-15 21:26:59 +03:00
tooltip = S("Maximum speed for moving blocks"),
default = "8",
},
{
type = "float",
name = "height",
2022-01-03 23:40:31 +03:00
label = S("Move block height"),
2021-11-15 21:26:59 +03:00
tooltip = S("Value in the range of 0.0 to 1.0"),
default = "1.0",
},
{
type = "float",
name = "offset",
2022-01-03 23:40:31 +03:00
label = S("Object offset"),
tooltip = S("Y-offset for non-player objects like vehicles (-0.5 to 0.5)"),
default = "0.0",
},
2022-06-23 21:36:36 +03:00
{
type = "dropdown",
choices = "A-B / B-A,move xyz",
name = "opmode",
label = S("Operational mode"),
tooltip = S("Switch to the remote controlled 'move xyz' mode"),
default = "A-B / B-A",
},
2021-11-15 21:26:59 +03:00
}
local function formspec(nvm, meta)
local status = meta:get_string("status")
local path = minetest.formspec_escape(meta:contains("path") and meta:get_string("path") or "0,3,0")
2022-06-23 21:36:36 +03:00
local buttons
if meta:get_string("opmode") == "move xyz" then
buttons = "field[0.4,2.5;3.8,1;path;" .. S("Move distance") .. ";" .. path .. "]" ..
"button_exit[4.1,2.2;3.8,1;move2;" .. S("Move") .. "]" ..
"button_exit[0.1,3.3;3.8,1;reset;" .. S("Reset") .. "]"
else
buttons = "field[0.4,2.5;3.8,1;path;" .. S("Move distance (A to B)") .. ";" .. path .. "]" ..
"button_exit[0.1,3.3;3.8,1;moveAB;" .. S("Move A-B") .. "]" ..
"button_exit[4.1,3.3;3.8,1;moveBA;" .. S("Move B-A") .. "]" ..
"button[4.1,2.2;3.8,1;store;" .. S("Store") .. "]"
end
2021-11-15 21:26:59 +03:00
return "size[8,5]" ..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
"box[0,-0.1;7.2,0.5;#c6e8ff]" ..
"label[0.2,-0.1;" .. minetest.colorize( "#000000", S("TA4 Move Controller")) .. "]" ..
techage.wrench_image(7.4, -0.05) ..
"button[0.1,0.8;3.8,1;record;" .. S("Record") .. "]" ..
"button[4.1,0.8;3.8,1;done;" .. S("Done") .. "]" ..
2022-06-23 21:36:36 +03:00
buttons ..
2021-11-15 21:26:59 +03:00
"label[0.3,4.3;" .. status .. "]"
end
minetest.register_node("techage:ta4_movecontroller", {
description = S("TA4 Move Controller"),
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta4.png^techage_frame_ta4_top.png",
"techage_filling_ta4.png^techage_frame_ta4_top.png",
"techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_movecontroller.png",
},
after_place_node = function(pos, placer, itemstack)
local meta = M(pos)
techage.logic.after_place_node(pos, placer, "techage:ta4_movecontroller", S("TA4 Move Controller"))
techage.logic.infotext(meta, S("TA4 Move Controller"))
local nvm = techage.get_nvm(pos)
meta:set_string("formspec", formspec(nvm, meta))
end,
on_receive_fields = function(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
local meta = M(pos)
local nvm = techage.get_nvm(pos)
if fields.record then
nvm.lpos1 = {}
nvm.lpos2 = {}
nvm.moveBA = false
2022-01-10 23:36:54 +03:00
nvm.running = nil
nvm.lastpos = nil
2021-11-15 21:26:59 +03:00
meta:set_string("status", S("Recording..."))
local name = player:get_player_name()
minetest.chat_send_player(name, S("Click on all blocks that shall be moved"))
mark.unmark_all(name)
2021-11-15 21:26:59 +03:00
mark.start(name, MAX_BLOCKS)
meta:set_string("formspec", formspec(nvm, meta))
elseif fields.done then
local name = player:get_player_name()
local pos_list = mark.get_poslist(name)
2021-12-24 18:05:04 +03:00
if fly.to_vector(fields.path or "", MAX_DIST) then
meta:set_string("path", fields.path)
end
2021-11-15 21:26:59 +03:00
local text = #pos_list.." "..S("block positions are stored.")
2022-01-10 23:36:54 +03:00
nvm.running = nil
nvm.lastpos = nil
2021-11-15 21:26:59 +03:00
meta:set_string("status", text)
nvm.lpos1 = pos_list
mark.unmark_all(name)
mark.stop(name)
meta:set_string("formspec", formspec(nvm, meta))
elseif fields.store then
if fly.to_vector(fields.path or "", MAX_DIST) then
meta:set_string("path", fields.path)
2021-11-15 21:26:59 +03:00
meta:set_string("status", S("Stored"))
else
meta:set_string("status", S("Error: Invalid distance !!"))
end
meta:set_string("formspec", formspec(nvm, meta))
local name = player:get_player_name()
mark.stop(name)
nvm.moveBA = false
2022-01-10 23:36:54 +03:00
nvm.running = nil
nvm.lastpos = nil
2021-11-15 21:26:59 +03:00
elseif fields.moveAB then
meta:set_string("status", "")
if fly.move_to_other_pos(pos, false) then
meta:set_string("formspec", formspec(nvm, meta))
local name = player:get_player_name()
mark.stop(name)
end
meta:set_string("formspec", formspec(nvm, meta))
2021-11-15 21:26:59 +03:00
elseif fields.moveBA then
meta:set_string("status", "")
if fly.move_to_other_pos(pos, true) then
meta:set_string("formspec", formspec(nvm, meta))
local name = player:get_player_name()
mark.stop(name)
end
meta:set_string("formspec", formspec(nvm, meta))
2022-06-23 21:36:36 +03:00
elseif fields.move2 then
if fly.to_vector(fields.path or "", MAX_DIST) then
meta:set_string("path", fields.path)
end
local line = fly.to_vector(meta:get_string("path"), MAX_DIST)
2022-06-23 21:36:36 +03:00
if line then
fly.move_to(pos, line)
end
elseif fields.reset then
fly.reset_move(pos)
2021-11-15 21:26:59 +03:00
end
end,
2022-01-03 23:40:31 +03:00
2022-06-23 21:36:36 +03:00
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
if not clicker or minetest.is_protected(pos, clicker:get_player_name()) then
return
end
local meta = M(pos)
local nvm = techage.get_nvm(pos)
meta:set_string("formspec", formspec(nvm, meta))
end,
2021-11-15 21:26:59 +03:00
after_dig_node = function(pos, oldnode, oldmetadata, digger)
local name = digger:get_player_name()
mark.unmark_all(name)
mark.stop(name)
techage.remove_node(pos, oldnode, oldmetadata)
end,
ta4_formspec = WRENCH_MENU,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
2021-11-17 20:55:54 +03:00
local INFO = [[Commands: 'state', 'a2b', 'b2a', 'move']]
2021-11-15 21:26:59 +03:00
techage.register_node({"techage:ta4_movecontroller"}, {
on_recv_message = function(pos, src, topic, payload)
local nvm = techage.get_nvm(pos)
2022-06-23 21:36:36 +03:00
local move_xyz = M(pos):get_string("opmode") == "move xyz"
2021-11-15 21:26:59 +03:00
if topic == "info" then
return INFO
2021-11-17 20:55:54 +03:00
elseif topic == "state" then
return nvm.running and "running" or "stopped"
2022-06-23 21:36:36 +03:00
elseif not move_xyz and topic == "a2b" then
2021-11-15 21:26:59 +03:00
return fly.move_to_other_pos(pos, false)
2022-06-23 21:36:36 +03:00
elseif not move_xyz and topic == "b2a" then
2021-11-15 21:26:59 +03:00
return fly.move_to_other_pos(pos, true)
elseif not move_xyz and topic == "move" then
return fly.move_to_other_pos(pos, nvm.moveBA == false)
2022-06-23 21:36:36 +03:00
elseif move_xyz and topic == "move2" then
local line = fly.to_vector(payload, MAX_DIST)
2022-06-23 21:36:36 +03:00
if line then
return fly.move_to(pos, line)
end
return false
elseif topic == "reset" then
return fly.reset_move(pos)
2021-11-15 21:26:59 +03:00
end
return false
end,
2022-06-06 21:39:55 +03:00
on_beduino_receive_cmnd = function(pos, src, topic, payload)
local nvm = techage.get_nvm(pos)
2022-06-23 21:36:36 +03:00
local move_xyz = M(pos):get_string("opmode") == "move xyz"
--print("on_beduino_receive_cmnd", P2S(pos), move_xyz, topic, payload[1])
if not move_xyz and topic == 11 then
2022-06-06 21:39:55 +03:00
if payload[1] == 1 then
return fly.move_to_other_pos(pos, false) and 0 or 3
elseif payload[1] == 2 then
return fly.move_to_other_pos(pos, true) and 0 or 3
elseif payload[1] == 3 then
return fly.move_to_other_pos(pos, nvm.moveBA == false) and 0 or 3
end
2022-06-23 21:36:36 +03:00
elseif move_xyz and topic == 18 then -- move xyz
local line = {
2022-08-02 14:28:32 +03:00
x = techage.in_range(techage.beduino_signed_var(payload[1]), -100, 100),
y = techage.in_range(techage.beduino_signed_var(payload[2]), -100, 100),
z = techage.in_range(techage.beduino_signed_var(payload[3]), -100, 100),
2022-06-23 21:36:36 +03:00
}
return fly.move_to(pos, line) and 0 or 3
elseif move_xyz and topic == 19 then -- reset
return fly.reset_move(pos) and 0 or 3
2022-06-06 21:39:55 +03:00
else
return 2
end
end,
on_beduino_request_data = function(pos, src, topic, payload)
local nvm = techage.get_nvm(pos)
if topic == 129 then
return 0, {nvm.running and 1 or 6}
end
return 2, ""
end,
on_node_load = function(pos, node)
M(pos):set_string("teleport_mode", "") -- delete not working (legacy) op mode
2024-07-18 07:44:51 +03:00
M(pos):set_string("status", "")
end,
2022-01-03 23:40:31 +03:00
})
2021-11-15 21:26:59 +03:00
2023-02-12 19:46:03 +03:00
minetest.register_node("techage:rack_and_pinion", {
2023-02-12 19:56:38 +03:00
description = S("TA Rack and Pinion"),
2023-02-12 19:46:03 +03:00
tiles = {
-- up, down, right, left, back, front
"default_steel_block.png",
"default_steel_block.png",
"default_steel_block.png",
"default_steel_block.png",
"default_steel_block.png",
"techage_rack_and_pinion.png",
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -6/32, -16/32, 14.1/32, 6/32, 16/32, 16/32},
},
},
paramtype = "light",
use_texture_alpha = techage.CLIP,
sunlight_propagates = true,
paramtype2 = "facedir",
is_ground_content = false,
groups = {cracky = 1, level = 2},
sounds = default.node_sound_metal_defaults(),
})
2023-03-21 22:00:26 +03:00
minetest.register_node("techage:moveblock", {
description = "Techage Invisible Move Block",
drawtype = "glasslike_framed_optional",
inventory_image = 'techage_inv_invisible.png',
tiles = {"blank.png"},
2023-03-21 22:00:26 +03:00
selection_box = {
type = "fixed",
fixed = {
{-16/32, -16/32, -16/32, 16/32, -14/32, 16/32},
},
},
paramtype = "light",
light_source = 0,
sunlight_propagates = true,
walkable = false,
pointable = true,
is_ground_content = false,
groups = {cracky = 3, oddly_breakable_by_hand = 3, not_in_creative_inventory = 1},
2023-03-21 22:00:26 +03:00
sounds = default.node_sound_glass_defaults(),
})
2021-11-15 21:26:59 +03:00
minetest.register_craft({
output = "techage:ta4_movecontroller",
recipe = {
{"default:steel_ingot", "dye:blue", "default:steel_ingot"},
{"default:mese_crystal_fragment", "techage:ta4_wlanchip", "default:mese_crystal_fragment"},
{"group:wood", "basic_materials:gear_steel", "group:wood"},
},
})
2023-02-12 19:46:03 +03:00
minetest.register_craft({
output = "techage:rack_and_pinion 10",
recipe = {
{"", "default:steel_ingot", ""},
{"basic_materials:steel_bar", "default:steel_ingot", "basic_materials:steel_bar"},
{"", "default:steel_ingot", ""},
},
})