Just moving some code around in preparation to adding a "lateral shunt" option to the auto-controller

This commit is contained in:
FaceDeer 2017-01-08 21:34:27 -07:00
parent 32ed41ce6a
commit 7af220009b
4 changed files with 103 additions and 85 deletions

4
description.txt Normal file

@ -0,0 +1,4 @@
digtron - a modular tunnel boring/building machine for minetest, v0.7 - 2017-01-08
Forum : https://forum.minetest.net/viewtopic.php?f=9&t=16295
github: https://github.com/FaceDeer/digtron/

@ -49,7 +49,7 @@ minetest.register_node("digtron:controller", {
return return
end end
local newpos, status, return_code = digtron.execute_cycle(pos, clicker) local newpos, status, return_code = digtron.execute_dig_cycle(pos, clicker)
meta = minetest.get_meta(newpos) meta = minetest.get_meta(newpos)
if status ~= nil then if status ~= nil then
@ -90,7 +90,7 @@ digtron.auto_cycle = function(pos)
return return
end end
local newpos, status, return_code = digtron.execute_cycle(pos, player) local newpos, status, return_code = digtron.execute_dig_cycle(pos, player)
local cycle = 0 local cycle = 0
if vector.equals(pos, newpos) then if vector.equals(pos, newpos) then
@ -222,69 +222,12 @@ minetest.register_node("digtron:pusher", {
return return
end end
local layout = digtron.get_all_digtron_neighbours(pos, clicker) local newpos, status_text, return_code = digtron.execute_move_cycle(pos, clicker)
if layout.all == nil then meta:set_string("infotext", status_text)
-- get_all_digtron_neighbours returns nil if the digtron array touches unloaded nodes, too dangerous to do anything in that situation. Abort.
minetest.sound_play("buzzer", {gain=0.5, pos=pos})
meta:set_string("infotext", "Digtron is adjacent to unloaded nodes.")
return
end
if layout.traction * digtron.traction_factor < table.getn(layout.all) then -- Start the delay before digtron can run again.
-- digtrons can't fly minetest.get_meta(newpos):set_string("waiting", "true")
minetest.sound_play("squeal", {gain=1.0, pos=pos}) minetest.get_node_timer(newpos):start(digtron.cycle_time)
meta:set_string("infotext", string.format("Digtron has %d nodes but only enough traction to move %d nodes.", table.getn(layout.all), layout.traction * digtron.traction_factor))
return
end
local facing = minetest.get_node(pos).param2
local controlling_coordinate = digtron.get_controlling_coordinate(pos, facing)
local nodes_dug = Pointset.create() -- empty set, we're not digging anything
-- test if any digtrons are obstructed by non-digtron nodes that haven't been marked
-- as having been dug.
local can_move = true
for _, location in pairs(layout.all) do
local newpos = digtron.find_new_pos(location, facing)
if not digtron.can_move_to(newpos, layout.protected, nodes_dug) then
can_move = false
end
end
if not can_move then
-- mark this node as waiting, will clear this flag in digtron.cycle_time seconds
meta:set_string("waiting", "true")
minetest.get_node_timer(pos):start(digtron.cycle_time)
minetest.sound_play("squeal", {gain=1.0, pos=pos})
minetest.sound_play("buzzer", {gain=0.5, pos=pos})
meta:set_string("infotext", "Digtron is obstructed.")
return --Abort
end
meta:set_string("infotext", nil)
minetest.sound_play("truck", {gain=1.0, pos=pos})
-- if the player is standing within the array or next to it, move him too.
local player_pos = clicker:getpos()
local move_player = false
if player_pos.x >= layout.extents.min_x - 1 and player_pos.x <= layout.extents.max_x + 1 and
player_pos.y >= layout.extents.min_y - 1 and player_pos.y <= layout.extents.max_y + 1 and
player_pos.z >= layout.extents.min_z - 1 and player_pos.z <= layout.extents.max_z + 1 then
move_player = true
end
--move the array
digtron.move_digtron(facing, layout.all, layout.extents, nodes_dug)
local oldpos = {x=pos.x, y=pos.y, z=pos.z}
pos = digtron.find_new_pos(pos, facing)
if move_player then
clicker:moveto(digtron.find_new_pos(player_pos, facing), true)
end
-- Start the delay before digtron can run again. Do this after moving the array or pos will be wrong.
minetest.get_meta(pos):set_string("waiting", "true")
minetest.get_node_timer(pos):start(digtron.cycle_time)
end, end,
on_timer = function(pos, elapsed) on_timer = function(pos, elapsed)

@ -3,12 +3,12 @@ The sounds in this folder were sampled from source .wavs from Freesound.org. Spe
buzzer.ogg - https://freesound.org/people/hypocore/sounds/164090/ - public domain via CC 1.0 by hypocore buzzer.ogg - https://freesound.org/people/hypocore/sounds/164090/ - public domain via CC 1.0 by hypocore
construction.ogg - https://www.freesound.org/people/mediapetros/sounds/109117/ - under the CC BY 3.0 license by mediapetros construction.ogg - https://www.freesound.org/people/mediapetros/sounds/109117/ - under the CC BY 3.0 license by mediapetros
dingding.ogg - https://www.freesound.org/people/JohnsonBrandEditing/sounds/173932/ public domain via CC 1.0 by JohnsonBrandEditing dingding.ogg - https://www.freesound.org/people/JohnsonBrandEditing/sounds/173932/ public domain via CC 1.0 by JohnsonBrandEditing
squeal.ogg - https://www.freesound.org/people/RutgerMuller/sounds/104026/ public domain via CC 1.0 by RutgerMuller
honk.ogg - https://freesound.org/people/bigmanjoe/sounds/349922/ public domain via CC 1.0 by bigmanjoe honk.ogg - https://freesound.org/people/bigmanjoe/sounds/349922/ public domain via CC 1.0 by bigmanjoe
truck.ogg - https://www.freesound.org/people/jberkuta14/sounds/134898/ public domain via CC 1.0 by jberkuta14
sploosh.ogg - https://www.freesound.org/people/mr_marcello/sounds/257609/ public domain via CC 1.0 by mr_marcello sploosh.ogg - https://www.freesound.org/people/mr_marcello/sounds/257609/ public domain via CC 1.0 by mr_marcello
woopwoopwoop.ogg - https://www.freesound.org/people/gregconquest/sounds/188012/ public domain via CC 1.0 by gregconquest squeal.ogg - https://www.freesound.org/people/RutgerMuller/sounds/104026/ public domain via CC 1.0 by RutgerMuller
truck.ogg - https://www.freesound.org/people/jberkuta14/sounds/134898/ public domain via CC 1.0 by jberkuta14
whirr.ogg - https://www.freesound.org/people/daveincamas/sounds/25034/ - under the CC BY 3.0 license by daveincamas whirr.ogg - https://www.freesound.org/people/daveincamas/sounds/25034/ - under the CC BY 3.0 license by daveincamas
woopwoopwoop.ogg - https://www.freesound.org/people/gregconquest/sounds/188012/ public domain via CC 1.0 by gregconquest
Creative Commons Attribution 3.0 license: Creative Commons Attribution 3.0 license:

@ -39,6 +39,32 @@ local burn_smoke = function(pos, amount)
} }
end end
--Performs various tests on a layout to play warning noises and see if Digtron can move at all.
local function neighbour_test(layout, status_text)
if layout.all == nil then
-- get_all_digtron_neighbours returns nil if the digtron array touches unloaded nodes, too dangerous to do anything in that situation. Abort.
minetest.sound_play("buzzer", {gain=0.25, pos=layout.controller})
return "Digtron is adjacent to unloaded nodes.\n" .. status_text, 1
end
if layout.water_touching == true then
minetest.sound_play("sploosh", {gain=1.0, pos=layout.controller})
end
if layout.lava_touching == true then
minetest.sound_play("woopwoopwoop", {gain=1.0, pos=layout.controller})
end
if layout.traction * digtron.traction_factor < table.getn(layout.all) then
-- digtrons can't fly
minetest.sound_play("squeal", {gain=1.0, pos=layout.controller})
return string.format("Digtron has %d nodes but only enough traction to move %d nodes.\n", table.getn(layout.all), layout.traction * digtron.traction_factor)
.. status_text, 2
end
return status_text, 0
end
-- returns newpos, status string, and a return code indicating why the method returned (so the auto-controller can keep trying if it's due to unloaded nodes) -- returns newpos, status string, and a return code indicating why the method returned (so the auto-controller can keep trying if it's due to unloaded nodes)
-- 0 - success -- 0 - success
-- 1 - failed due to unloaded nodes -- 1 - failed due to unloaded nodes
@ -48,31 +74,18 @@ end
-- 5 - unknown builder error during testing -- 5 - unknown builder error during testing
-- 6 - builder with unset output -- 6 - builder with unset output
-- 7 - insufficient builder materials in inventory -- 7 - insufficient builder materials in inventory
digtron.execute_cycle = function(pos, clicker) digtron.execute_dig_cycle = function(pos, clicker)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local fuel_burning = meta:get_float("fuel_burning") -- get amount of burned fuel left over from last cycle local fuel_burning = meta:get_float("fuel_burning") -- get amount of burned fuel left over from last cycle
local status_text = string.format("Heat remaining in controller furnace: %d", fuel_burning) local status_text = string.format("Heat remaining in controller furnace: %d", fuel_burning)
local layout = digtron.get_all_digtron_neighbours(pos, clicker) local layout = digtron.get_all_digtron_neighbours(pos, clicker)
if layout.all == nil then
-- get_all_digtron_neighbours returns nil if the digtron array touches unloaded nodes, too dangerous to do anything in that situation. Abort. local status_text, return_code = neighbour_test(layout, status_text)
minetest.sound_play("buzzer", {gain=0.25, pos=pos}) if return_code ~= 0 then
return pos, "Digtron is adjacent to unloaded nodes.\n" .. status_text, 1 return pos, status_text, return_code
end end
if layout.water_touching == true then
minetest.sound_play("sploosh", {gain=1.0, pos=pos})
end
if layout.lava_touching == true then
minetest.sound_play("woopwoopwoop", {gain=1.0, pos=pos})
end
if layout.traction * digtron.traction_factor < table.getn(layout.all) then
-- digtrons can't fly
minetest.sound_play("squeal", {gain=1.0, pos=pos})
return pos, string.format("Digtron has %d nodes but only enough traction to move %d nodes.\n", table.getn(layout.all), layout.traction * digtron.traction_factor)
.. status_text, 2
end
local facing = minetest.get_node(pos).param2 local facing = minetest.get_node(pos).param2
local move_dir = minetest.facedir_to_dir(facing) local move_dir = minetest.facedir_to_dir(facing)
local controlling_coordinate = digtron.get_controlling_coordinate(pos, facing) local controlling_coordinate = digtron.get_controlling_coordinate(pos, facing)
@ -294,3 +307,61 @@ digtron.execute_cycle = function(pos, clicker)
end end
return pos, status_text, 0 return pos, status_text, 0
end end
-- Simplified version of the above method that only moves, and doesn't execute diggers or builders.
digtron.execute_move_cycle = function(pos, clicker)
local meta = minetest.get_meta(pos)
local layout = digtron.get_all_digtron_neighbours(pos, clicker)
local status_text = ""
local status_text, return_code = neighbour_test(layout, status_text)
if return_code ~= 0 then
return pos, status_text, return_code
end
local facing = minetest.get_node(pos).param2
local controlling_coordinate = digtron.get_controlling_coordinate(pos, facing)
local nodes_dug = Pointset.create() -- empty set, we're not digging anything
-- test if any digtrons are obstructed by non-digtron nodes that haven't been marked
-- as having been dug.
local can_move = true
for _, location in pairs(layout.all) do
local newpos = digtron.find_new_pos(location, facing)
if not digtron.can_move_to(newpos, layout.protected, nodes_dug) then
can_move = false
end
end
if not can_move then
-- mark this node as waiting, will clear this flag in digtron.cycle_time seconds
minetest.get_meta(pos):set_string("waiting", "true")
minetest.get_node_timer(pos):start(digtron.cycle_time)
minetest.sound_play("squeal", {gain=1.0, pos=pos})
minetest.sound_play("buzzer", {gain=0.5, pos=pos})
return pos, "Digtron is obstructed.\n" .. status_text, 3 --Abort, don't dig and don't build.
end
minetest.sound_play("truck", {gain=1.0, pos=pos})
-- if the player is standing within the array or next to it, move him too.
local player_pos = clicker:getpos()
local move_player = false
if player_pos.x >= layout.extents.min_x - 1 and player_pos.x <= layout.extents.max_x + 1 and
player_pos.y >= layout.extents.min_y - 1 and player_pos.y <= layout.extents.max_y + 1 and
player_pos.z >= layout.extents.min_z - 1 and player_pos.z <= layout.extents.max_z + 1 then
move_player = true
end
--move the array
digtron.move_digtron(facing, layout.all, layout.extents, nodes_dug)
local oldpos = {x=pos.x, y=pos.y, z=pos.z}
pos = digtron.find_new_pos(pos, facing)
if move_player then
clicker:moveto(digtron.find_new_pos(player_pos, facing), true)
end
return pos, "", 0
end