Splitting some code files into more manageably-sized chunks.

This commit is contained in:
FaceDeer 2017-01-07 09:52:39 -07:00
parent 152e0f7883
commit e43c5d3974
5 changed files with 523 additions and 523 deletions

@ -1,298 +1,3 @@
local dig_dust = function(pos, facing)
local direction = minetest.facedir_to_dir(facing)
return {
amount = 10,
time = 1.0,
minpos = vector.subtract(pos, vector.new(0.5,0.5,0.5)),
maxpos = vector.add(pos, vector.new(0.5,0.5,0.5)),
minvel = vector.multiply(direction, -10),
maxvel = vector.multiply(direction, -20),
minacc = {x=0, y=-40, z=0},
maxacc = {x=0, y=-40, z=0},
minexptime = 0.25,
maxexptime = 0.5,
minsize = 2,
maxsize = 5,
collisiondetection = false,
vertical = false,
texture = "default_item_smoke.png^[colorize:#9F817080",
}
end
local burn_smoke = function(pos, amount)
return {
amount = math.min(amount, 40),
time = 1.0,
minpos = vector.subtract(pos, vector.new(0.5,0.5,0.5)),
maxpos = vector.add(pos, vector.new(0.5,0.5,0.5)),
minvel = {x=0, y=2, z=0},
maxvel = {x=0, y=5, z=0},
minacc = {x=0, y=0, z=0},
maxacc = {x=0, y=0, z=0},
minexptime = 0.5,
maxexptime = 1.5,
minsize = 8,
maxsize = 12,
collisiondetection = false,
vertical = false,
texture = "default_item_smoke.png^[colorize:#000000DD",
}
end
-- returns newpos, status string
local execute_cycle = function(pos, clicker)
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 status_text = string.format("Heat remaining in controller furnace: %d", fuel_burning)
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.
minetest.sound_play("buzzer", {gain=0.5, pos=pos})
return pos, "Digtron is adjacent to unloaded nodes.\n" .. status_text
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
end
local facing = minetest.get_node(pos).param2
local move_dir = minetest.facedir_to_dir(facing)
local controlling_coordinate = digtron.get_controlling_coordinate(pos, facing)
----------------------------------------------------------------------------------------------------------------------
local nodes_dug = Pointset.create()
local items_dropped = {}
local digging_fuel_cost = 0
local particle_systems = {}
-- execute the execute_dig method on all digtron components that have one
-- This builds a set of nodes that will be dug and returns a list of products that will be generated
-- but doesn't actually dig the nodes yet. That comes later.
-- If we dug them now, sand would fall and some digtron nodes would die.
for k, location in pairs(layout.diggers) do
local target = minetest.get_node(location)
local targetdef = minetest.registered_nodes[target.name]
if targetdef.execute_dig ~= nil then
local fuel_cost, dropped = targetdef.execute_dig(location, layout.protected, nodes_dug, controlling_coordinate)
if dropped ~= nil then
for _, itemname in pairs(dropped) do
table.insert(items_dropped, itemname)
end
if digtron.particle_effects then
table.insert(particle_systems, dig_dust(vector.add(location, move_dir), target.param2))
end
end
digging_fuel_cost = digging_fuel_cost + fuel_cost
else
minetest.log(string.format("%s has digger group but is missing execute_dig method! This is an error in mod programming, file a bug.", targetdef.name))
end
end
----------------------------------------------------------------------------------------------------------------------
-- 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.after(digtron.cycle_time,
function (pos)
minetest.get_meta(pos):set_string("waiting", nil)
end, pos
)
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 --Abort, don't dig and don't build.
end
----------------------------------------------------------------------------------------------------------------------
-- ask each builder node if it can get what it needs from inventory to build this cycle.
-- This is a complicated test because each builder needs to actually *take* the item it'll
-- need from inventory, and then we put it all back afterward.
-- Note that this test may overestimate the amount of work that will actually need to be done so don't treat its fuel cost as authoritative.
local can_build = true
local test_build_return_code = nil
local test_build_return_item = nil
local test_items = {}
local test_fuel_items = {}
local test_build_fuel_cost = 0
for k, location in pairs(layout.builders) do
local target = minetest.get_node(location)
local targetdef = minetest.registered_nodes[target.name]
local test_location = digtron.find_new_pos(location, facing)
if targetdef.test_build ~= nil then
test_build_return_code, test_build_return_item = targetdef.test_build(location, test_location, layout.inventories, layout.protected, nodes_dug, controlling_coordinate, layout.controller)
if test_build_return_code > 1 then
can_build = false
break
end
if test_build_return_code == 1 then
table.insert(test_items, test_build_return_item)
test_build_fuel_cost = test_build_fuel_cost + digtron.build_cost
end
else
minetest.log(string.format("%s has builder group but is missing test_build method! This is an error in mod programming, file a bug.", targetdef.name))
end
end
local test_fuel_needed = test_build_fuel_cost + digging_fuel_cost - fuel_burning
local test_fuel_burned = 0
if test_fuel_needed > 0 then
test_fuel_burned = digtron.burn(layout.fuelstores, test_fuel_needed, true)
end
--Put everything back where it came from
for k, item_return in pairs(test_items) do
digtron.place_in_specific_inventory(item_return.item, item_return.location, layout.inventories, layout.controller)
end
if test_fuel_needed > fuel_burning + test_fuel_burned then
minetest.sound_play("buzzer", {gain=0.5, pos=pos})
return pos, "Digtron needs more fuel." -- abort, don't dig and don't build.
end
if not can_build then
minetest.get_meta(pos):set_string("waiting", "true")
minetest.after(digtron.cycle_time,
function (pos)
minetest.get_meta(pos):set_string("waiting", nil)
end, pos
)
local return_string = nil
if test_build_return_code == 3 then
minetest.sound_play("honk", {gain=0.5, pos=pos}) -- A builder is not configured
return_string = "Digtron connected to at least one builder with no output material assigned.\n"
elseif test_build_return_code == 2 then
minetest.sound_play("dingding", {gain=1.0, pos=pos}) -- Insufficient inventory
return_string = string.format("Digtron has insufficient building materials. Needed: %s\n",
test_build_return_item:get_name())
end
return pos, return_string .. status_text --Abort, don't dig and don't build.
end
----------------------------------------------------------------------------------------------------------------------
-- All tests passed, ready to go for real!
minetest.sound_play("construction", {gain=1.0, pos=pos})
-- store or drop the products of the digger heads
for _, itemname in pairs(items_dropped) do
digtron.place_in_inventory(itemname, layout.inventories, pos)
end
-- 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
-- damage the weak flesh
if digtron.diggers_damage_creatures then
for k, location in pairs(layout.diggers) do
local target = minetest.get_node(location)
local targetdef = minetest.registered_nodes[target.name]
if targetdef.damage_creatures ~= nil then
targetdef.damage_creatures(clicker, location, digtron.find_new_pos(location, target.param2), controlling_coordinate)
end
end
end
--move the array
digtron.move_digtron(facing, layout.all, layout.extents, nodes_dug, clicker:get_player_name())
local oldpos = {x=pos.x, y=pos.y, z=pos.z}
pos = digtron.find_new_pos(pos, facing)
meta = minetest.get_meta(pos)
if move_player then
clicker:moveto(digtron.find_new_pos(player_pos, facing), true)
end
local building_fuel_cost = 0
local strange_failure = false
-- execute_build on all digtron components that have one
for k, location in pairs(layout.builders) do
local target = minetest.get_node(location)
local targetdef = minetest.registered_nodes[target.name]
if targetdef.execute_build ~= nil then
--using the old location of the controller as fallback so that any leftovers land with the rest of the digger output. Not that there should be any.
local build_return = targetdef.execute_build(location, clicker, layout.inventories, layout.protected, nodes_dug, controlling_coordinate, oldpos)
if build_return == false then
-- This happens if there's insufficient inventory, but we should have confirmed there was sufficient inventory during test phase.
-- So this should never happen. However, "should never happens" happen sometimes. So
-- don't interrupt the build cycle as a whole, we've already moved so might as well try to complete as much as possible.
strange_failure = true
elseif build_return == true and not digtron.creative_mode == true then
building_fuel_cost = building_fuel_cost + digtron.build_cost
end
else
minetest.log(string.format("%s has builder group but is missing execute_build method! This is an error in mod programming, file a bug.", targetdef.name))
end
end
local status_text = ""
if strange_failure then
-- We weren't able to detect this build failure ahead of time, so make a big noise now. This is strange, shouldn't happen.
minetest.sound_play("dingding", {gain=1.0, pos=pos})
minetest.sound_play("buzzer", {gain=0.5, pos=pos})
status_text = "Digtron unexpectedly failed to execute one or more build operations, likely due to an inventory error.\n"
end
-- acutally burn the fuel needed
local fuel_cost = digging_fuel_cost + building_fuel_cost
fuel_burning = fuel_burning - fuel_cost
if digtron.particle_effects then
table.insert(particle_systems, burn_smoke(pos, fuel_cost))
end
if fuel_burning < 0 then
fuel_burning = fuel_burning + digtron.burn(layout.fuelstores, -fuel_burning, false)
end
meta:set_float("fuel_burning", fuel_burning)
status_text = status_text .. string.format("Heat remaining in controller furnace: %d", fuel_burning)
-- Eyecandy
for _, particles in pairs(particle_systems) do
minetest.add_particlespawner(particles)
end
-- finally, dig out any nodes remaining to be dug. Some of these will have had their flag revoked because
-- a builder put something there or because they're another digtron node.
local node_to_dig, whether_to_dig = nodes_dug:pop()
while node_to_dig ~= nil do
if whether_to_dig == true then
minetest.log("action", string.format("%s uses Digtron to dig %s at (%d, %d, %d)", clicker:get_player_name(), minetest.get_node(node_to_dig).name, node_to_dig.x, node_to_dig.y, node_to_dig.z))
minetest.remove_node(node_to_dig)
end
-- all of the digtron's nodes wind up in nodes_dug, so this is an ideal place to stick
-- a check to make sand fall after the digtron has passed.
minetest.check_for_falling({x=node_to_dig.x, y=node_to_dig.y+1, z=node_to_dig.z})
node_to_dig, whether_to_dig = nodes_dug:pop()
end
return pos, status_text
end
local controller_nodebox ={
{-0.3125, -0.3125, -0.3125, 0.3125, 0.3125, 0.3125}, -- Core
{-0.1875, 0.3125, -0.1875, 0.1875, 0.5, 0.1875}, -- +y_connector
@ -344,14 +49,14 @@ minetest.register_node("digtron:controller", {
return
end
local newpos, status = execute_cycle(pos, clicker)
local newpos, status = digtron.execute_cycle(pos, clicker)
meta = minetest.get_meta(newpos)
if status ~= nil then
meta:set_string("infotext", status)
end
-- Start the delay before digtron can run again. Do this after moving the array or pos will be wrong.
-- Start the delay before digtron can run again.
minetest.get_meta(newpos):set_string("waiting", "true")
minetest.after(digtron.cycle_time,
function (pos)
@ -361,6 +66,7 @@ minetest.register_node("digtron:controller", {
end,
})
-- Auto-controller
---------------------------------------------------------------------------------------------------------------
local auto_formspec = "size[4.5,1]" ..
@ -384,7 +90,7 @@ digtron.auto_cycle = function(pos)
return
end
local newpos, status = execute_cycle(pos, player)
local newpos, status = digtron.execute_cycle(pos, player)
local cycle = 0
if vector.equals(pos, newpos) then
@ -408,7 +114,6 @@ digtron.auto_cycle = function(pos)
end
end
-- Master controller. Most complicated part of the whole system. Determines which direction a digtron moves and triggers all of its component parts.
minetest.register_node("digtron:auto_controller", {
description = "Digtron Automatic Control Unit",
groups = {cracky = 3, oddly_breakable_by_hand = 3, digtron = 1},

227
util.lua

@ -3,6 +3,9 @@
digtron = {}
dofile( minetest.get_modpath( "digtron" ) .. "/util_item_place_node.lua" ) -- separated out to avoid potential for license complexity
dofile( minetest.get_modpath( "digtron" ) .. "/util_movement.lua" ) -- separated out simply for tidiness, there's some big code in there
dofile( minetest.get_modpath( "digtron" ) .. "/util_layout.lua" ) -- separated out simply for tidiness, there's some big code in there
dofile( minetest.get_modpath( "digtron" ) .. "/util_execute_cycle.lua" ) -- separated out simply for tidiness, there's some big code in there
-- Apparently node_sound_metal_defaults is a newer thing, I ran into games using an older version of the default mod without it.
if default.node_sound_metal_defaults ~= nil then
@ -95,160 +98,6 @@ digtron.can_move_to = function(pos, protected_nodes, dug_nodes)
return false
end
digtron.move_node = function(pos, newpos, player_name)
-- Moves nodes, preserving digtron metadata and inventory
local node = minetest.get_node(pos)
local node_def = minetest.registered_nodes[node.name]
local oldnode = minetest.get_node(newpos)
minetest.log("action", string.format("%s moves %s from (%d, %d, %d) to (%d, %d, %d), displacing %s", player_name, node.name, pos.x, pos.y, pos.z, newpos.x, newpos.y, newpos.z, oldnode.name))
minetest.add_node(newpos, { name=node.name, param1=node.param1, param2=node.param2 })
if node_def.after_place_node then
node_def.after_place_node(newpos)
end
local oldmeta = minetest.get_meta(pos)
local oldinv = oldmeta:get_inventory()
local list = oldinv:get_list("main")
local fuel = oldinv:get_list("fuel")
local oldformspec = oldmeta:get_string("formspec")
local newmeta = minetest.get_meta(newpos)
local newinv = newmeta:get_inventory()
newinv:set_list("main", list)
newinv:set_list("fuel", fuel)
newmeta:set_string("formspec", oldformspec)
newmeta:set_string("triggering_player", oldmeta:get_string("triggering_player")) -- for auto-controllers
newmeta:set_int("offset", oldmeta:get_int("offset"))
newmeta:set_int("period", oldmeta:get_int("period"))
newmeta:set_int("build_facing", oldmeta:get_int("build_facing"))
newmeta:set_float("fuel_burning", oldmeta:get_float("fuel_burning"))
newmeta:set_string("infotext", oldmeta:get_string("infotext"))
-- Move the little floaty entity inside the builders
if minetest.get_item_group(node.name, "digtron") == 4 then
digtron.update_builder_item(newpos)
end
-- remove node from old position
minetest.remove_node(pos)
if node_def.after_dig_node then
node_def.after_dig_node(pos)
end
end
digtron.get_all_digtron_neighbours = function(pos, player)
-- returns table containing a list of all digtron node locations, lists of special digtron node types, a table of the coordinate extents of the digtron array, a Pointset of protected nodes, and a number to determine how many adjacent solid non-digtron nodes there are (for traction)
local layout = {}
--initialize. We're assuming that the start position is a controller digtron, should be a safe assumption since only the controller node should call this
layout.traction = 0
layout.all = {}
layout.inventories = {}
layout.fuelstores = {}
layout.diggers = {}
layout.builders = {}
layout.extents = {}
layout.water_touching = false
layout.lava_touching = false
layout.protected = Pointset.create() -- if any nodes we look at are protected, make note of that. That way we don't need to keep re-testing protection state later.
layout.controller = {x=pos.x, y=pos.y, z=pos.z} --Make a deep copy of the pos parameter just in case the calling code wants to play silly buggers with it
table.insert(layout.all, layout.controller)
layout.extents.max_x = pos.x
layout.extents.min_x = pos.x
layout.extents.max_y = pos.y
layout.extents.min_y = pos.y
layout.extents.max_z = pos.z
layout.extents.min_z = pos.z
-- temporary pointsets used while searching
local to_test = Pointset.create()
local tested = Pointset.create()
tested:set(pos.x, pos.y, pos.z, true)
to_test:set(pos.x + 1, pos.y, pos.z, true)
to_test:set(pos.x - 1, pos.y, pos.z, true)
to_test:set(pos.x, pos.y + 1, pos.z, true)
to_test:set(pos.x, pos.y - 1, pos.z, true)
to_test:set(pos.x, pos.y, pos.z + 1, true)
to_test:set(pos.x, pos.y, pos.z - 1, true)
if minetest.is_protected(pos, player:get_player_name()) and not minetest.check_player_privs(player, "protection_bypass") then
layout.protected:set(pos.x, pos.y, pos.z, true)
end
-- Do a loop on to_test positions, adding new to_test positions as we find digtron nodes. This is a flood fill operation
-- that follows node faces (no diagonals)
local testpos, _ = to_test:pop()
while testpos ~= nil do
tested:set(testpos.x, testpos.y, testpos.z, true) -- track nodes we've looked at to prevent infinite loops
local node = minetest.get_node(testpos)
if node.name == "ignore" then
--buildtron array is next to unloaded nodes, too dangerous to do anything. Abort.
layout.all = nil
return layout
end
if minetest.is_protected(pos, player:get_player_name()) and not minetest.check_player_privs(player, "protection_bypass") then
layout.protected:set(testpos.x, testpos.y, testpos.z, true)
end
if minetest.get_item_group(node.name, "water") ~= 0 then
layout.water_touching = true
elseif minetest.get_item_group(node.name, "lava") ~= 0 then
layout.lava_touching = true
if digtron.lava_impassible == true then
layout.protected:set(testpos.x, testpos.y, testpos.z, true)
end
end
local group_number = minetest.get_item_group(node.name, "digtron")
if group_number > 0 then
--found one. Add it to the digtrons output
table.insert(layout.all, testpos)
-- update extents
layout.extents.max_x = math.max(layout.extents.max_x, testpos.x)
layout.extents.min_x = math.min(layout.extents.min_x, testpos.x)
layout.extents.max_y = math.max(layout.extents.max_y, testpos.y)
layout.extents.min_y = math.min(layout.extents.min_y, testpos.y)
layout.extents.max_z = math.max(layout.extents.max_z, testpos.z)
layout.extents.min_z = math.min(layout.extents.min_z, testpos.z)
-- add a reference to this node's position to special node lists
if group_number == 2 then
table.insert(layout.inventories, testpos)
elseif group_number == 3 then
table.insert(layout.diggers, testpos)
elseif group_number == 4 then
table.insert(layout.builders, testpos)
elseif group_number == 5 then
table.insert(layout.fuelstores, testpos)
elseif group_number == 6 then
table.insert(layout.inventories, testpos)
table.insert(layout.fuelstores, testpos)
end
--queue up potential new test points adjacent to this digtron node
to_test:set_if_not_in(tested, testpos.x + 1, testpos.y, testpos.z, true)
to_test:set_if_not_in(tested, testpos.x - 1, testpos.y, testpos.z, true)
to_test:set_if_not_in(tested, testpos.x, testpos.y + 1, testpos.z, true)
to_test:set_if_not_in(tested, testpos.x, testpos.y - 1, testpos.z, true)
to_test:set_if_not_in(tested, testpos.x, testpos.y, testpos.z + 1, true)
to_test:set_if_not_in(tested, testpos.x, testpos.y, testpos.z - 1, true)
elseif minetest.registered_nodes[node.name].buildable_to ~= true then
-- Tracks whether the digtron is hovering in mid-air. If any part of the digtron array touches something solid it gains traction.
layout.traction = layout.traction + 1
end
testpos, _ = to_test:pop()
end
return layout
end
digtron.place_in_inventory = function(itemname, inventory_positions, fallback_pos)
--tries placing the item in each inventory node in turn. If there's no room, drop it at fallback_pos
@ -291,76 +140,6 @@ digtron.take_from_inventory = function(itemname, inventory_positions)
return nil
end
digtron.move_digtron = function(facing, digtrons, extents, nodes_dug, player_name)
-- move everything. Note! order is important or they'll step on each other, that's why this has complicated loops and filtering.
-- Nodes are moved in a "caterpillar" pattern - front plane first, then next plane back, then next plane back, etc.
-- positions in the digtron list will be updated when this method executes. Note that the inventories list shares
-- references to the node position tables in the digtron list, so it will reflect the updates too.
local dir = digtron.facedir_to_dir_map[facing]
local increment
local filter
local index
local target
if dir == 1 then -- z+
filter = "z"
increment = -1
index = extents.max_z
target = extents.min_z
extents.max_z = extents.max_z + 1
extents.min_z = extents.min_z + 1
elseif dir == 2 then -- x+
filter = "x"
increment = -1
index = extents.max_x
target = extents.min_x
extents.max_x = extents.max_x + 1
extents.min_x = extents.min_x + 1
elseif dir == 3 then -- z-
filter = "z"
increment = 1
index = extents.min_z
target = extents.max_z
extents.max_z = extents.max_z - 1
extents.min_z = extents.min_z - 1
elseif dir == 4 then -- x-
filter = "x"
increment = 1
index = extents.min_x
target = extents.max_x
extents.max_x = extents.max_x - 1
extents.min_x = extents.min_x - 1
elseif dir == 5 then -- y-
filter = "y"
increment = 1
index = extents.min_y
target = extents.max_y
extents.max_y = extents.max_y - 1
extents.min_y = extents.min_y - 1
elseif dir == 6 then -- y+
filter = "y"
increment = -1
index = extents.max_y
target = extents.min_y
extents.max_y = extents.max_y + 1
extents.min_y = extents.min_y + 1
end
while index ~= target + increment do
for k, location in pairs(digtrons) do
if location[filter] == index then
local newpos = digtron.find_new_pos(location, facing)
digtron.move_node(location, newpos, player_name)
--By updating the digtron position table in-place we also update all the special node tables as well
digtrons[k].x= newpos.x
digtrons[k].y= newpos.y
digtrons[k].z= newpos.z
nodes_dug:set(newpos.x, newpos.y, newpos.z, false) -- we've moved a digtron node into this space, mark it so that we don't dig it.
end
end
index = index + increment
end
end
-- Used to determine which coordinate is being checked for periodicity. eg, if the digtron is moving in the z direction, then periodicity is checked for every n nodes in the z axis.
digtron.get_controlling_coordinate = function(pos, facedir)
-- used for determining builder period and offset

293
util_execute_cycle.lua Normal file

@ -0,0 +1,293 @@
local dig_dust = function(pos, facing)
local direction = minetest.facedir_to_dir(facing)
return {
amount = 10,
time = 1.0,
minpos = vector.subtract(pos, vector.new(0.5,0.5,0.5)),
maxpos = vector.add(pos, vector.new(0.5,0.5,0.5)),
minvel = vector.multiply(direction, -10),
maxvel = vector.multiply(direction, -20),
minacc = {x=0, y=-40, z=0},
maxacc = {x=0, y=-40, z=0},
minexptime = 0.25,
maxexptime = 0.5,
minsize = 2,
maxsize = 5,
collisiondetection = false,
vertical = false,
texture = "default_item_smoke.png^[colorize:#9F817080",
}
end
local burn_smoke = function(pos, amount)
return {
amount = math.min(amount, 40),
time = 1.0,
minpos = vector.subtract(pos, vector.new(0.5,0.5,0.5)),
maxpos = vector.add(pos, vector.new(0.5,0.5,0.5)),
minvel = {x=0, y=2, z=0},
maxvel = {x=0, y=5, z=0},
minacc = {x=0, y=0, z=0},
maxacc = {x=0, y=0, z=0},
minexptime = 0.5,
maxexptime = 1.5,
minsize = 8,
maxsize = 12,
collisiondetection = false,
vertical = false,
texture = "default_item_smoke.png^[colorize:#000000DD",
}
end
-- returns newpos, status string
digtron.execute_cycle = function(pos, clicker)
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 status_text = string.format("Heat remaining in controller furnace: %d", fuel_burning)
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.
minetest.sound_play("buzzer", {gain=0.5, pos=pos})
return pos, "Digtron is adjacent to unloaded nodes.\n" .. status_text
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
end
local facing = minetest.get_node(pos).param2
local move_dir = minetest.facedir_to_dir(facing)
local controlling_coordinate = digtron.get_controlling_coordinate(pos, facing)
----------------------------------------------------------------------------------------------------------------------
local nodes_dug = Pointset.create()
local items_dropped = {}
local digging_fuel_cost = 0
local particle_systems = {}
-- execute the execute_dig method on all digtron components that have one
-- This builds a set of nodes that will be dug and returns a list of products that will be generated
-- but doesn't actually dig the nodes yet. That comes later.
-- If we dug them now, sand would fall and some digtron nodes would die.
for k, location in pairs(layout.diggers) do
local target = minetest.get_node(location)
local targetdef = minetest.registered_nodes[target.name]
if targetdef.execute_dig ~= nil then
local fuel_cost, dropped = targetdef.execute_dig(location, layout.protected, nodes_dug, controlling_coordinate)
if dropped ~= nil then
for _, itemname in pairs(dropped) do
table.insert(items_dropped, itemname)
end
if digtron.particle_effects then
table.insert(particle_systems, dig_dust(vector.add(location, move_dir), target.param2))
end
end
digging_fuel_cost = digging_fuel_cost + fuel_cost
else
minetest.log(string.format("%s has digger group but is missing execute_dig method! This is an error in mod programming, file a bug.", targetdef.name))
end
end
----------------------------------------------------------------------------------------------------------------------
-- 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.after(digtron.cycle_time,
function (pos)
minetest.get_meta(pos):set_string("waiting", nil)
end, pos
)
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 --Abort, don't dig and don't build.
end
----------------------------------------------------------------------------------------------------------------------
-- ask each builder node if it can get what it needs from inventory to build this cycle.
-- This is a complicated test because each builder needs to actually *take* the item it'll
-- need from inventory, and then we put it all back afterward.
-- Note that this test may overestimate the amount of work that will actually need to be done so don't treat its fuel cost as authoritative.
local can_build = true
local test_build_return_code = nil
local test_build_return_item = nil
local test_items = {}
local test_fuel_items = {}
local test_build_fuel_cost = 0
for k, location in pairs(layout.builders) do
local target = minetest.get_node(location)
local targetdef = minetest.registered_nodes[target.name]
local test_location = digtron.find_new_pos(location, facing)
if targetdef.test_build ~= nil then
test_build_return_code, test_build_return_item = targetdef.test_build(location, test_location, layout.inventories, layout.protected, nodes_dug, controlling_coordinate, layout.controller)
if test_build_return_code > 1 then
can_build = false
break
end
if test_build_return_code == 1 then
table.insert(test_items, test_build_return_item)
test_build_fuel_cost = test_build_fuel_cost + digtron.build_cost
end
else
minetest.log(string.format("%s has builder group but is missing test_build method! This is an error in mod programming, file a bug.", targetdef.name))
end
end
local test_fuel_needed = test_build_fuel_cost + digging_fuel_cost - fuel_burning
local test_fuel_burned = 0
if test_fuel_needed > 0 then
test_fuel_burned = digtron.burn(layout.fuelstores, test_fuel_needed, true)
end
--Put everything back where it came from
for k, item_return in pairs(test_items) do
digtron.place_in_specific_inventory(item_return.item, item_return.location, layout.inventories, layout.controller)
end
if test_fuel_needed > fuel_burning + test_fuel_burned then
minetest.sound_play("buzzer", {gain=0.5, pos=pos})
return pos, "Digtron needs more fuel." -- abort, don't dig and don't build.
end
if not can_build then
minetest.get_meta(pos):set_string("waiting", "true")
minetest.after(digtron.cycle_time,
function (pos)
minetest.get_meta(pos):set_string("waiting", nil)
end, pos
)
local return_string = nil
if test_build_return_code == 3 then
minetest.sound_play("honk", {gain=0.5, pos=pos}) -- A builder is not configured
return_string = "Digtron connected to at least one builder with no output material assigned.\n"
elseif test_build_return_code == 2 then
minetest.sound_play("dingding", {gain=1.0, pos=pos}) -- Insufficient inventory
return_string = string.format("Digtron has insufficient building materials. Needed: %s\n",
test_build_return_item:get_name())
end
return pos, return_string .. status_text --Abort, don't dig and don't build.
end
----------------------------------------------------------------------------------------------------------------------
-- All tests passed, ready to go for real!
minetest.sound_play("construction", {gain=1.0, pos=pos})
-- store or drop the products of the digger heads
for _, itemname in pairs(items_dropped) do
digtron.place_in_inventory(itemname, layout.inventories, pos)
end
-- 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
-- damage the weak flesh
if digtron.diggers_damage_creatures then
for k, location in pairs(layout.diggers) do
local target = minetest.get_node(location)
local targetdef = minetest.registered_nodes[target.name]
if targetdef.damage_creatures ~= nil then
targetdef.damage_creatures(clicker, location, digtron.find_new_pos(location, target.param2), controlling_coordinate)
end
end
end
--move the array
digtron.move_digtron(facing, layout.all, layout.extents, nodes_dug, clicker:get_player_name())
local oldpos = {x=pos.x, y=pos.y, z=pos.z}
pos = digtron.find_new_pos(pos, facing)
meta = minetest.get_meta(pos)
if move_player then
clicker:moveto(digtron.find_new_pos(player_pos, facing), true)
end
local building_fuel_cost = 0
local strange_failure = false
-- execute_build on all digtron components that have one
for k, location in pairs(layout.builders) do
local target = minetest.get_node(location)
local targetdef = minetest.registered_nodes[target.name]
if targetdef.execute_build ~= nil then
--using the old location of the controller as fallback so that any leftovers land with the rest of the digger output. Not that there should be any.
local build_return = targetdef.execute_build(location, clicker, layout.inventories, layout.protected, nodes_dug, controlling_coordinate, oldpos)
if build_return == false then
-- This happens if there's insufficient inventory, but we should have confirmed there was sufficient inventory during test phase.
-- So this should never happen. However, "should never happens" happen sometimes. So
-- don't interrupt the build cycle as a whole, we've already moved so might as well try to complete as much as possible.
strange_failure = true
elseif build_return == true and not digtron.creative_mode == true then
building_fuel_cost = building_fuel_cost + digtron.build_cost
end
else
minetest.log(string.format("%s has builder group but is missing execute_build method! This is an error in mod programming, file a bug.", targetdef.name))
end
end
local status_text = ""
if strange_failure then
-- We weren't able to detect this build failure ahead of time, so make a big noise now. This is strange, shouldn't happen.
minetest.sound_play("dingding", {gain=1.0, pos=pos})
minetest.sound_play("buzzer", {gain=0.5, pos=pos})
status_text = "Digtron unexpectedly failed to execute one or more build operations, likely due to an inventory error.\n"
end
-- acutally burn the fuel needed
local fuel_cost = digging_fuel_cost + building_fuel_cost
fuel_burning = fuel_burning - fuel_cost
if digtron.particle_effects then
table.insert(particle_systems, burn_smoke(pos, fuel_cost))
end
if fuel_burning < 0 then
fuel_burning = fuel_burning + digtron.burn(layout.fuelstores, -fuel_burning, false)
end
meta:set_float("fuel_burning", fuel_burning)
status_text = status_text .. string.format("Heat remaining in controller furnace: %d", fuel_burning)
-- Eyecandy
for _, particles in pairs(particle_systems) do
minetest.add_particlespawner(particles)
end
-- finally, dig out any nodes remaining to be dug. Some of these will have had their flag revoked because
-- a builder put something there or because they're another digtron node.
local node_to_dig, whether_to_dig = nodes_dug:pop()
while node_to_dig ~= nil do
if whether_to_dig == true then
minetest.log("action", string.format("%s uses Digtron to dig %s at (%d, %d, %d)", clicker:get_player_name(), minetest.get_node(node_to_dig).name, node_to_dig.x, node_to_dig.y, node_to_dig.z))
minetest.remove_node(node_to_dig)
end
-- all of the digtron's nodes wind up in nodes_dug, so this is an ideal place to stick
-- a check to make sand fall after the digtron has passed.
minetest.check_for_falling({x=node_to_dig.x, y=node_to_dig.y+1, z=node_to_dig.z})
node_to_dig, whether_to_dig = nodes_dug:pop()
end
return pos, status_text
end

111
util_layout.lua Normal file

@ -0,0 +1,111 @@
digtron.get_all_digtron_neighbours = function(pos, player)
-- returns table containing a list of all digtron node locations, lists of special digtron node types, a table of the coordinate extents of the digtron array, a Pointset of protected nodes, and a number to determine how many adjacent solid non-digtron nodes there are (for traction)
local layout = {}
--initialize. We're assuming that the start position is a controller digtron, should be a safe assumption since only the controller node should call this
layout.traction = 0
layout.all = {}
layout.inventories = {}
layout.fuelstores = {}
layout.diggers = {}
layout.builders = {}
layout.extents = {}
layout.water_touching = false
layout.lava_touching = false
layout.protected = Pointset.create() -- if any nodes we look at are protected, make note of that. That way we don't need to keep re-testing protection state later.
layout.controller = {x=pos.x, y=pos.y, z=pos.z} --Make a deep copy of the pos parameter just in case the calling code wants to play silly buggers with it
table.insert(layout.all, layout.controller)
layout.extents.max_x = pos.x
layout.extents.min_x = pos.x
layout.extents.max_y = pos.y
layout.extents.min_y = pos.y
layout.extents.max_z = pos.z
layout.extents.min_z = pos.z
-- temporary pointsets used while searching
local to_test = Pointset.create()
local tested = Pointset.create()
tested:set(pos.x, pos.y, pos.z, true)
to_test:set(pos.x + 1, pos.y, pos.z, true)
to_test:set(pos.x - 1, pos.y, pos.z, true)
to_test:set(pos.x, pos.y + 1, pos.z, true)
to_test:set(pos.x, pos.y - 1, pos.z, true)
to_test:set(pos.x, pos.y, pos.z + 1, true)
to_test:set(pos.x, pos.y, pos.z - 1, true)
if minetest.is_protected(pos, player:get_player_name()) and not minetest.check_player_privs(player, "protection_bypass") then
layout.protected:set(pos.x, pos.y, pos.z, true)
end
-- Do a loop on to_test positions, adding new to_test positions as we find digtron nodes. This is a flood fill operation
-- that follows node faces (no diagonals)
local testpos, _ = to_test:pop()
while testpos ~= nil do
tested:set(testpos.x, testpos.y, testpos.z, true) -- track nodes we've looked at to prevent infinite loops
local node = minetest.get_node(testpos)
if node.name == "ignore" then
--buildtron array is next to unloaded nodes, too dangerous to do anything. Abort.
layout.all = nil
return layout
end
if minetest.is_protected(pos, player:get_player_name()) and not minetest.check_player_privs(player, "protection_bypass") then
layout.protected:set(testpos.x, testpos.y, testpos.z, true)
end
if minetest.get_item_group(node.name, "water") ~= 0 then
layout.water_touching = true
elseif minetest.get_item_group(node.name, "lava") ~= 0 then
layout.lava_touching = true
if digtron.lava_impassible == true then
layout.protected:set(testpos.x, testpos.y, testpos.z, true)
end
end
local group_number = minetest.get_item_group(node.name, "digtron")
if group_number > 0 then
--found one. Add it to the digtrons output
table.insert(layout.all, testpos)
-- update extents
layout.extents.max_x = math.max(layout.extents.max_x, testpos.x)
layout.extents.min_x = math.min(layout.extents.min_x, testpos.x)
layout.extents.max_y = math.max(layout.extents.max_y, testpos.y)
layout.extents.min_y = math.min(layout.extents.min_y, testpos.y)
layout.extents.max_z = math.max(layout.extents.max_z, testpos.z)
layout.extents.min_z = math.min(layout.extents.min_z, testpos.z)
-- add a reference to this node's position to special node lists
if group_number == 2 then
table.insert(layout.inventories, testpos)
elseif group_number == 3 then
table.insert(layout.diggers, testpos)
elseif group_number == 4 then
table.insert(layout.builders, testpos)
elseif group_number == 5 then
table.insert(layout.fuelstores, testpos)
elseif group_number == 6 then
table.insert(layout.inventories, testpos)
table.insert(layout.fuelstores, testpos)
end
--queue up potential new test points adjacent to this digtron node
to_test:set_if_not_in(tested, testpos.x + 1, testpos.y, testpos.z, true)
to_test:set_if_not_in(tested, testpos.x - 1, testpos.y, testpos.z, true)
to_test:set_if_not_in(tested, testpos.x, testpos.y + 1, testpos.z, true)
to_test:set_if_not_in(tested, testpos.x, testpos.y - 1, testpos.z, true)
to_test:set_if_not_in(tested, testpos.x, testpos.y, testpos.z + 1, true)
to_test:set_if_not_in(tested, testpos.x, testpos.y, testpos.z - 1, true)
elseif minetest.registered_nodes[node.name].buildable_to ~= true then
-- Tracks whether the digtron is hovering in mid-air. If any part of the digtron array touches something solid it gains traction.
layout.traction = layout.traction + 1
end
testpos, _ = to_test:pop()
end
return layout
end

112
util_movement.lua Normal file

@ -0,0 +1,112 @@
digtron.move_node = function(pos, newpos, player_name)
-- Moves nodes, preserving digtron metadata and inventory
local node = minetest.get_node(pos)
local node_def = minetest.registered_nodes[node.name]
local oldnode = minetest.get_node(newpos)
minetest.log("action", string.format("%s moves %s from (%d, %d, %d) to (%d, %d, %d), displacing %s", player_name, node.name, pos.x, pos.y, pos.z, newpos.x, newpos.y, newpos.z, oldnode.name))
minetest.add_node(newpos, { name=node.name, param1=node.param1, param2=node.param2 })
if node_def.after_place_node then
node_def.after_place_node(newpos)
end
local oldmeta = minetest.get_meta(pos)
local oldinv = oldmeta:get_inventory()
local list = oldinv:get_list("main")
local fuel = oldinv:get_list("fuel")
local oldformspec = oldmeta:get_string("formspec")
local newmeta = minetest.get_meta(newpos)
local newinv = newmeta:get_inventory()
newinv:set_list("main", list)
newinv:set_list("fuel", fuel)
newmeta:set_string("formspec", oldformspec)
newmeta:set_string("triggering_player", oldmeta:get_string("triggering_player")) -- for auto-controllers
newmeta:set_int("offset", oldmeta:get_int("offset"))
newmeta:set_int("period", oldmeta:get_int("period"))
newmeta:set_int("build_facing", oldmeta:get_int("build_facing"))
newmeta:set_float("fuel_burning", oldmeta:get_float("fuel_burning"))
newmeta:set_string("infotext", oldmeta:get_string("infotext"))
-- Move the little floaty entity inside the builders
if minetest.get_item_group(node.name, "digtron") == 4 then
digtron.update_builder_item(newpos)
end
-- remove node from old position
minetest.remove_node(pos)
if node_def.after_dig_node then
node_def.after_dig_node(pos)
end
end
digtron.move_digtron = function(facing, digtrons, extents, nodes_dug, player_name)
-- move everything. Note! order is important or they'll step on each other, that's why this has complicated loops and filtering.
-- Nodes are moved in a "caterpillar" pattern - front plane first, then next plane back, then next plane back, etc.
-- positions in the digtron list will be updated when this method executes. Note that the inventories list shares
-- references to the node position tables in the digtron list, so it will reflect the updates too.
local dir = digtron.facedir_to_dir_map[facing]
local increment
local filter
local index
local target
if dir == 1 then -- z+
filter = "z"
increment = -1
index = extents.max_z
target = extents.min_z
extents.max_z = extents.max_z + 1
extents.min_z = extents.min_z + 1
elseif dir == 2 then -- x+
filter = "x"
increment = -1
index = extents.max_x
target = extents.min_x
extents.max_x = extents.max_x + 1
extents.min_x = extents.min_x + 1
elseif dir == 3 then -- z-
filter = "z"
increment = 1
index = extents.min_z
target = extents.max_z
extents.max_z = extents.max_z - 1
extents.min_z = extents.min_z - 1
elseif dir == 4 then -- x-
filter = "x"
increment = 1
index = extents.min_x
target = extents.max_x
extents.max_x = extents.max_x - 1
extents.min_x = extents.min_x - 1
elseif dir == 5 then -- y-
filter = "y"
increment = 1
index = extents.min_y
target = extents.max_y
extents.max_y = extents.max_y - 1
extents.min_y = extents.min_y - 1
elseif dir == 6 then -- y+
filter = "y"
increment = -1
index = extents.max_y
target = extents.min_y
extents.max_y = extents.max_y + 1
extents.min_y = extents.min_y + 1
end
while index ~= target + increment do
for k, location in pairs(digtrons) do
if location[filter] == index then
local newpos = digtron.find_new_pos(location, facing)
digtron.move_node(location, newpos, player_name)
--By updating the digtron position table in-place we also update all the special node tables as well
digtrons[k].x= newpos.x
digtrons[k].y= newpos.y
digtrons[k].z= newpos.z
nodes_dug:set(newpos.x, newpos.y, newpos.z, false) -- we've moved a digtron node into this space, mark it so that we don't dig it.
end
end
index = index + increment
end
end