Fix valve protection and tank cart bugs, add command payload to buttons

This commit is contained in:
Joachim Stolberg 2021-10-22 19:03:02 +02:00
parent 58b1be93ca
commit 43bb2705cf
14 changed files with 151 additions and 122 deletions

View File

@ -136,4 +136,4 @@ end
function techage.recipes.get_recipe(name)
return NormalizedRecipes[name]
end

View File

@ -118,6 +118,9 @@ minetest.register_node("techage:tank_cart", {
local nvm = techage.get_nvm(pos)
nvm.liquid = nvm.liquid or {}
M(pos):set_string("formspec", techage.liquid.formspec(pos, nvm))
-- Delete the network between pump and cart
Pipe:after_dig_node(pos)
Pipe:after_place_node(pos)
end,
set_cargo = function(pos, data)

View File

@ -264,7 +264,7 @@ dofile(MP.."/logic/repeater.lua")
dofile(MP.."/logic/programmer.lua")
dofile(MP.."/logic/signallamp.lua")
dofile(MP.."/logic/sequencer.lua")
dofile(MP.."/logic/sequencer2.lua")
--dofile(MP.."/logic/sequencer2.lua") still under dev.
dofile(MP.."/logic/timer.lua")
dofile(MP.."/logic/lua_logic.lua") -- old
dofile(MP.."/logic/logic_block.lua") -- new
@ -277,7 +277,7 @@ dofile(MP.."/logic/doorcontroller.lua") -- old
dofile(MP.."/logic/doorcontroller2.lua") -- new
dofile(MP.."/logic/collector.lua")
dofile(MP.."/logic/button_4x.lua")
dofile(MP.."/logic/movecontroller.lua")
--dofile(MP.."/logic/movecontroller.lua") still under dev.
if minetest.global_exists("mesecon") then
dofile(MP.."/logic/mesecons_converter.lua")
end

View File

@ -45,7 +45,6 @@ local function pumping(pos, nvm, state, capa)
local mem = techage.get_mem(pos)
mem.dbg_cycles = (mem.dbg_cycles or 0) - 1
local outdir = M(pos):get_int("outdir")
--print("pumping", outdir, Flip[outdir])
local taken, name = liquid.take(pos, Pipe, Flip[outdir], nil, capa, mem.dbg_cycles > 0)
if taken > 0 then
local leftover = liquid.put(pos, Pipe, outdir, name, taken, mem.dbg_cycles > 0)

View File

@ -43,6 +43,9 @@ minetest.register_node("techage:ta3_valve_open", {
return false
end,
on_rightclick = function(pos, node, clicker)
if minetest.is_protected(pos, clicker:get_player_name()) then
return
end
if liquid.turn_valve_off(pos, Pipe, "techage:ta3_valve_closed", "techage:ta3_valve_open") then
minetest.sound_play("techage_valve", {
pos = pos,
@ -84,6 +87,9 @@ minetest.register_node("techage:ta3_valve_closed", {
},
on_rightclick = function(pos, node, clicker)
if minetest.is_protected(pos, clicker:get_player_name()) then
return
end
if liquid.turn_valve_on(pos, Pipe, "techage:ta3_valve_closed", "techage:ta3_valve_open") then
minetest.sound_play("techage_valve", {
pos = pos,

View File

@ -38,8 +38,9 @@ local function switch_on(pos)
logic.swap_node(pos, "techage:ta4_button_on")
end
local meta = M(pos)
local cmnd = meta:contains("command") and meta:get_string("command") or "on"
logic.send_cmnd(pos, M(pos), cmnd, cycle_time)
local s = meta:contains("command") and meta:get_string("command") or "on"
local command, payload = unpack(string.split(s, " ", false, 1))
logic.send_cmnd(pos, M(pos), command, payload, cycle_time)
minetest.sound_play("techage_button", {
pos = pos,
gain = 0.5,

View File

@ -18,20 +18,22 @@ local S = techage.S
local function get_button_num(pos, clicker, pointed_thing)
-- use the node behind the button to get better results
local offs = vector.subtract(pointed_thing.under, pointed_thing.above)
pointed_thing.under = vector.add(pointed_thing.under, offs)
pointed_thing.above = vector.add(pointed_thing.above, offs)
local pos1 = minetest.pointed_thing_to_face_pos(clicker, pointed_thing)
local y = pos1.y - pos.y
if y < -0.3 then
return 4
elseif y < -0.03 and y > -0.22 then
return 3
elseif y > 0.03 and y < 0.22 then
return 2
elseif y > 0.3 then
return 1
if clicker and pointed_thing then
local offs = vector.subtract(pointed_thing.under, pointed_thing.above)
pointed_thing.under = vector.add(pointed_thing.under, offs)
pointed_thing.above = vector.add(pointed_thing.above, offs)
local pos1 = minetest.pointed_thing_to_face_pos(clicker, pointed_thing)
local y = pos1.y - pos.y
if y < -0.3 then
return 4
elseif y < -0.03 and y > -0.22 then
return 3
elseif y > 0.03 and y < 0.22 then
return 2
elseif y > 0.3 then
return 1
end
end
end
@ -134,10 +136,11 @@ local function send_cmnd(pos, num)
local meta = M(pos)
local own_num = meta:get_string("node_number")
local dest = meta:get_string("dest_number" .. num)
local cmnd = meta:get_string("command" .. num)
local s = meta:get_string("command" .. num)
local command, payload = unpack(string.split(s, " ", false, 1))
local owner = meta:get_string("owner")
if techage.check_numbers(dest, owner) then
techage.send_multi(own_num, dest, cmnd)
techage.send_multi(own_num, dest, command, payload)
end
end

View File

@ -425,3 +425,5 @@ for _, name in ipairs(ProtectorDoors) do
logic.register_doorcontroller_nodes({name .. "_" .. postfix})
end
end
logic.SimpleNodes = RegisteredNodes

View File

@ -67,13 +67,13 @@ function techage.logic.send_on(pos, meta, time)
return own_num == numbers
end
function techage.logic.send_cmnd(pos, meta, cmnd, time)
function techage.logic.send_cmnd(pos, meta, cmnd, payload, time)
local own_num = meta:get_string("node_number") or ""
local numbers = meta:get_string("numbers") or ""
if time and time > 0 then
minetest.get_node_timer(pos):start(time)
end
techage.send_multi(own_num, numbers, cmnd)
techage.send_multi(own_num, numbers, cmnd, payload)
end
function techage.logic.send_off(pos, meta)

View File

@ -88,12 +88,12 @@ end
-- Attach player/mob to given parent object (block)
local function attach_single_object(parent, obj, dir, height)
local self = parent:get_luaentity()
local rot = obj:get_rotation() or {x=0, y=0, z=0}
local rot = {x=0, y=0, z=0}
local prop = obj:get_properties()
local res = obj:get_attach()
if not res and prop and prop.collisionbox then
dir = table.copy(dir)
dir.y = dir.y - 1.5 + height - prop.collisionbox[2]
dir.y = dir.y - 1.5 + (height or 1) - prop.collisionbox[2]
dir = vector.multiply(dir, 29)
obj:set_attach(parent, "", dir, rot, true)
obj:set_properties({visual_size = {x = 2.9, y = 2.9}})
@ -108,23 +108,25 @@ local function attach_single_object(parent, obj, dir, height)
end
-- Attach all entities around to the parent object (block).
-- height is the parrwent block height (-0.5 to 0.5)
-- height is the parent block height (0.1 to 1.0)
local function attach_objects(pos, parent, dir, height)
local pos1 = vector.add(pos, dir)
local self = parent:get_luaentity()
self.players = self.players or {}
self.entities = self.entities or {}
for _, obj in pairs(minetest.get_objects_inside_radius(pos1, 0.8)) do
print("pos1", P2S(pos1))
for _, obj in pairs(minetest.get_objects_inside_radius(pos1, 0.9)) do
local entity = obj:get_luaentity()
if entity then
if entity.name == "__builtin:item" then -- dropped items
--obj:set_attach(objref, "", {x=0, y=0, z=0}, {x=0, y=0, z=0}, true) -- hier kracht es
elseif entity.name ~= "techage:move_item" then
attach_single_object(parent, obj, dir, height)
attach_single_object(parent, obj, dir, 1)
end
elseif obj:is_player() then
attach_single_object(parent, obj, dir)
print("is_player")
attach_single_object(parent, obj, dir, 1)
end
end
end
@ -158,7 +160,7 @@ local function detach_objects(pos, parent, dir)
end
local function entity_to_node(pos, obj)
local name = obj:get_luaentity().item or "air"
local name = (obj:get_luaentity() or {}).item or "air"
local rot = obj:get_rotation()
detach_objects(pos, obj)
obj:remove()
@ -183,13 +185,16 @@ local function node_to_entity(pos, handover)
local dir = minetest.facedir_to_dir(node.param2)
local yaw = minetest.dir_to_yaw(dir)
local obj = minetest.add_entity(pos, "techage:move_item")
local self = obj:get_luaentity()
obj:set_rotation({x=0, y=yaw, z=0})
obj:set_properties({wield_item = node.name})
obj:set_armor_groups({immortal=1})
self.item = node.name
self.handover = handover
return obj
if obj then
local self = obj:get_luaentity()
obj:set_rotation({x=0, y=yaw, z=0})
obj:set_properties({wield_item = node.name})
obj:set_armor_groups({immortal=1})
self.item = node.name
self.handover = handover
self.start_pos = table.copy(pos)
return obj
end
end
local function capture_entity(pos)
@ -219,12 +224,15 @@ local function move_entity(obj, pos2, dir, max_speed)
end
-- Handover the entity to the next movecontroller
local function handover_to(self)
local function handover_to(pos, self)
local info = techage.get_node_info(self.handover)
if info and info.name == "techage:ta4_movecontroller" then
local mem = techage.get_mem(info.pos)
if not mem.entities_are_there then
mem.entities_are_there = true
-- copy move direction
--print("techage.get_nvm(pos).pos_2to1", techage.get_nvm(pos).pos_2to1)
techage.get_nvm(info.pos).pos_2to1 = techage.get_nvm(pos).pos_2to1
minetest.after(0.2, techage.send_single, "0", self.handover, "handover")
end
return true
@ -249,7 +257,9 @@ minetest.register_entity("techage:move_item", {
item = self.item,
max_speed = self.max_speed,
dest_pos = self.dest_pos,
start_pos = self.start_pos,
dir = self.dir,
respawn = true,
})
end,
@ -259,8 +269,12 @@ minetest.register_entity("techage:move_item", {
self.item = tbl.item or "air"
self.max_speed = tbl.max_speed or MAX_SPEED
self.dest_pos = tbl.dest_pos or self.object:get_pos()
self.start_pos = tbl.start_pos or self.object:get_pos()
self.dir = tbl.dir or {x=0, y=0, z=0}
self.object:set_properties({wield_item = self.item})
if tbl.respawn then
entity_to_node(self.start_pos, self.object)
end
end
end,
@ -277,7 +291,7 @@ minetest.register_entity("techage:move_item", {
obj:set_acceleration({x=0, y=0, z=0})
obj:set_velocity({x=0, y=0, z=0})
self.dest_pos = nil
if not self.handover or not handover_to(self) then
if not self.handover or not handover_to(pos, self) then
minetest.after(0.5, entity_to_node, pos, obj)
end
self.ttl = 2
@ -308,7 +322,7 @@ minetest.register_entity("techage:move_item", {
-------------------------------------------------------------------------------
local MarkedNodes = {} -- t[player] = {{entity, pos},...}
local CurrentPos -- to mark punched entities
local RegisteredNodes = {} -- to be checked before removed/placed
local SimpleNodes = techage.logic.SimpleNodes
local function is_air_like(pos)
local node = minetest.get_node(pos)
@ -322,8 +336,8 @@ end
local function is_simple_node(pos)
-- special handling
local name = minetest.get_node(pos).name
if RegisteredNodes[name] ~= nil then
return RegisteredNodes[name]
if SimpleNodes[name] ~= nil then
return SimpleNodes[name]
end
local ndef = minetest.registered_nodes[name]
@ -496,13 +510,15 @@ local function move_node(pos, pos1, pos2, max_speed, handover, height)
local dir = determine_dir(pos1, pos2)
local obj = node_to_entity(pos1, handover)
attach_objects(pos1, obj, {x=0, y=1, z=0}, height)
if dir.y == 0 then
if (dir.x ~= 0 and dir.z == 0) or (dir.x == 0 and dir.z ~= 0) then
attach_objects(pos1, obj, dir, height)
if obj then
attach_objects(pos1, obj, {x=0, y=height, z=0}, height)
if dir.y == 0 then
if (dir.x ~= 0 and dir.z == 0) or (dir.x == 0 and dir.z ~= 0) then
attach_objects(pos1, obj, dir, 1)
end
end
move_entity(obj, pos2, dir, max_speed)
end
move_entity(obj, pos2, dir, max_speed)
end
local function move_nodes(pos, lpos1, lpos2, handover)
@ -596,16 +612,16 @@ local function move_to_other_pos(pos)
local meta = M(pos)
local nvm = techage.get_nvm(pos)
if nvm.pos2 then
if nvm.pos_2to1 then
local lpos1 = nvm.lpos1 or {}
local lpos2 = nvm.lpos2 or {}
nvm.pos2 = false
nvm.pos_2to1 = false
local handover = meta:contains("handoverA") and meta:get_string("handoverA")
return move_nodes(pos, lpos2, lpos1, handover)
else
local lpos1 = nvm.lpos1 or {}
local lpos2 = nvm.lpos2 or {}
nvm.pos2 = true
nvm.pos_2to1 = true
local handover = meta:contains("handoverB") and meta:get_string("handoverB")
return move_nodes(pos, lpos1, lpos2, handover)
end
@ -617,16 +633,16 @@ local function takeover(pos)
local mem = techage.get_mem(pos)
mem.entities_are_there = nil
if nvm.pos2 then
if nvm.pos_2to1 then
local lpos1 = nvm.lpos1 or {}
local lpos2 = nvm.lpos2 or {}
nvm.pos2 = false
nvm.pos_2to1 = false
local handover = meta:contains("handoverA") and meta:get_string("handoverA")
return moveon_nodes(pos, lpos2, lpos1, handover)
else
local lpos1 = nvm.lpos1 or {}
local lpos2 = nvm.lpos2 or {}
nvm.pos2 = true
nvm.pos_2to1 = true
local handover = meta:contains("handoverB") and meta:get_string("handoverB")
return moveon_nodes(pos, lpos1, lpos2, handover)
end
@ -660,7 +676,7 @@ minetest.register_node("techage:ta4_movecontroller", {
if fields.record then
nvm.lpos1 = nil
nvm.lpos2 = nil
nvm.pos2 = false
nvm.pos_2to1 = false
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"))
@ -674,17 +690,17 @@ minetest.register_node("techage:ta4_movecontroller", {
meta:set_string("distance", fields.distance)
nvm.lpos1 = pos_list
nvm.lpos2 = table_add(pos_list, to_vector(fields.distance))
nvm.pos2 = false
nvm.pos_2to1 = false
unmark_all(name)
meta:set_string("formspec", formspec(nvm, meta))
elseif fields.store then
meta:set_string("distance", fields.distance)
nvm.lpos2 = table_add(nvm.lpos1, to_vector(fields.distance))
nvm.pos2 = false
nvm.pos_2to1 = false
meta:set_string("formspec", formspec(nvm, meta))
elseif fields.moveAB then
meta:set_string("status", "")
nvm.pos2 = false
nvm.pos_2to1 = false
if move_to_other_pos(pos) then
meta:set_string("formspec", formspec(nvm, meta))
local name = player:get_player_name()
@ -692,7 +708,7 @@ minetest.register_node("techage:ta4_movecontroller", {
end
elseif fields.moveBA then
meta:set_string("status", "")
nvm.pos2 = true
nvm.pos_2to1 = true
if move_to_other_pos(pos) then
meta:set_string("formspec", formspec(nvm, meta))
local name = player:get_player_name()
@ -723,11 +739,11 @@ techage.register_node({"techage:ta4_movecontroller"}, {
return INFO
elseif topic == "a2b" then
local nvm = techage.get_nvm(pos)
nvm.pos2 = false
nvm.pos_2to1 = false
return move_to_other_pos(pos)
elseif topic == "b2a" then
local nvm = techage.get_nvm(pos)
nvm.pos2 = true
nvm.pos_2to1 = true
return move_to_other_pos(pos)
elseif topic == "move" then
return move_to_other_pos(pos)
@ -738,14 +754,14 @@ techage.register_node({"techage:ta4_movecontroller"}, {
end,
})
--minetest.register_craft({
-- output = "techage:ta4_movecontroller",
-- recipe = {
-- {"techage:aluminum", "group:wood","techage:aluminum"},
-- {"default:mese_crystal_fragment", "techage:ta4_wlanchip", "default:mese_crystal_fragment"},
-- {"group:wood", "basic_materials:gear_steel", "group:wood"},
-- },
--})
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"},
},
})
minetest.register_on_joinplayer(function(player)
unlock_player(player)

View File

@ -60,7 +60,7 @@ local function formspec_help()
"label[0,1;Define a sequence of commands\nto control other machines.]"..
"label[0,2.2;Numbers(s) are the node numbers,\nthe command shall sent to.]"..
"label[0,3.4;The commands 'on'/'off' are used\n for machines and other nodes.]"..
"label[0,4.6;Offset is the time to the\nnext line in seconds (1..999).]"..
"label[0,4.6;Offset is the time to the\nnext line in seconds (0.2 to 999).]"..
"label[0,5.8;If endless is set, the Sequencer\nrestarts again and again.]"..
"label[0,7;The command ' ' does nothing,\nonly consuming the offset time.]"..
"button[3,8;2,1;exit;close]"

View File

@ -31,10 +31,8 @@ end
local function command(s)
local num, cmd, pld = unpack(string.split(s, " ", false, 2))
if not num or not cmd then
print("error: Invalid command")
return
return "Invalid command!"
end
print(num, cmd, pld)
return {number = num, cmnd = cmd, payload = pld}
end
@ -62,14 +60,12 @@ local function compile(s, tRes)
local old_idx = 0
local start_idx
print("Compile")
for i, line in ipairs(strsplit(s)) do
line = trim(line)
line = string.split(line, "--", true, 1)[1] or ""
if line ~= "" then
local idx, cmnd1, cmnd2 = unpack(string.split(line, " ", false, 2))
idx = tonumber(string.match(idx, "^%[(%d+)%]$"))
print(idx, old_idx)
if not idx then
return exception(tRes, i, "Syntax error!")
end
@ -81,23 +77,29 @@ local function compile(s, tRes)
tCode[old_idx].next_idx = idx
end
if cmnd1 == "send" then
tCode[idx] = command(cmnd2)
local res = command(cmnd2)
if type(res) == "string" then
return exception(tRes, i, res)
end
tCode[idx] = res
elseif cmnd1 == "goto" then
tCode[idx] = {next_idx = tonumber(cmnd2) or 1}
elseif cmnd1 == "stop" then
tCode[idx] = false
elseif cmnd1 == nil then
tCode[idx] = {}
end
old_idx = idx
end
end
-- return {
-- Returns:
-- {
-- start_idx = 1,
-- tCode = {
-- <idx> = {number = <number>, cmnd = <string>, payload = <data>, next_idx = <idx>},
-- ...
-- },
-- }
print(dump(tCode))
return {start_idx=start_idx, tCode=tCode}
end
@ -160,7 +162,6 @@ local function restart_timer(pos, time)
end
local function node_timer(pos, elapsed)
print("node_timer")
local nvm = techage.get_nvm(pos)
if nvm.running then
local mem = techage.get_mem(pos)
@ -169,7 +170,8 @@ local function node_timer(pos, elapsed)
mem.idx = mem.idx or mem.code.start_idx
local code = mem.code.tCode[mem.idx]
if code and code.cmnd then
print(code.number, code.cmnd, code.payload, code.next_idx)
local src = M(pos):get_string("node_number")
techage.send_single(src, code.number, code.cmnd, code.payload)
end
if code and code.next_idx then
local offs = code.next_idx - mem.idx
@ -186,36 +188,37 @@ local function on_receive_fields(pos, formname, fields, player)
return
end
--print(dump(fields))
local meta = M(pos)
local nvm = techage.get_nvm(pos)
local mem = techage.get_mem(pos)
nvm.running = nvm.running or false
if fields.help then
meta:set_string("formspec", formspec_help(nvm, meta))
return
elseif fields.edit then
meta:set_string("formspec", formspec(nvm, meta))
return
end
if fields.save then
if fields.stop then
nvm.running = false
meta:set_string("text", fields.text or "")
mem.code = nil
mem.idx = nil
elseif fields.start then
if check_syntax(meta) then
nvm.running = true
minetest.get_node_timer(pos):stop()
elseif not nvm.running then
if fields.help then
meta:set_string("formspec", formspec_help(nvm, meta))
return
elseif fields.edit then
meta:set_string("formspec", formspec(nvm, meta))
return
end
if fields.save then
nvm.running = false
meta:set_string("text", fields.text or "")
mem.code = nil
mem.idx = nil
minetest.get_node_timer(pos):start(0.5)
elseif fields.start then
if check_syntax(meta) then
nvm.running = true
meta:set_string("text", fields.text or "")
mem.code = nil
mem.idx = nil
minetest.get_node_timer(pos):start(0.5)
end
end
elseif fields.stop then
nvm.running = false
minetest.get_node_timer(pos):stop()
end
meta:set_string("formspec", formspec(nvm, meta))
end
@ -257,35 +260,31 @@ minetest.register_node("techage:ta4_sequencer", {
minetest.register_craft({
output = "techage:ta4_sequencer",
recipe = {
{"group:wood", "group:wood", ""},
{"default:mese_crystal", "techage:wlanchip", ""},
{"group:wood", "group:wood", ""},
{"default:steel_ingot", "dye:blue", "default:steel_ingot"},
{"techage:ta4_ramchip", "default:mese_crystal", "techage:wlanchip"},
{"techage:aluminum", "group:wood","techage:aluminum"},
},
})
local INFO = [[Commands: 'goto <num>', 'stop']]
techage.register_node({"techage:ta4_sequencer"}, {
on_recv_message = function(pos, src, topic, payload)
if topic == "on" then
start_the_sequencer(pos)
elseif topic == "off" then
-- do not stop immediately
if topic == "goto" then
local mem = techage.get_mem(pos)
local nvm = techage.get_nvm(pos)
if not nvm.running then
nvm.endless = not (nvm.endless or false)
else
nvm.endless = false
end
elseif topic == "pause" then
stop_the_sequencer(pos)
nvm.running = true
mem.idx = tonumber(payload or 1) or 1
restart_timer(pos, 0.1)
elseif topic == "stop" then
local nvm = techage.get_nvm(pos)
nvm.running = false
minetest.get_node_timer(pos):stop()
elseif topic == "info" then
return INFO
else
return "unsupported"
end
end,
on_node_load = function(pos)
local nvm = techage.get_nvm(pos)
if nvm.running then
minetest.get_node_timer(pos):start(1)
end
end,
})

View File

@ -76,7 +76,7 @@ local function read_state(itemstack, user, pointed_thing)
if ndef and ndef.description then
local info = techage.send_single("0", number, "info", nil)
if info and info ~= "" and info ~= "unsupported" then
info = dump(info)
info = tostring(info)
minetest.chat_send_player(user:get_player_name(), ndef.description.." "..number..":\n"..info.." ")
end
local state = techage.send_single("0", number, "state", nil)

View File

@ -130,7 +130,7 @@ local function value_check(elem, value)
if elem.check then
return elem.check(value)
end
return true
return value ~= nil
end
local function evaluate_data(pos, meta, form_def, fields, player_name)