Skip to content

Instantly share code, notes, and snippets.

@Nullreff
Created November 3, 2014 21:25
Show Gist options
  • Save Nullreff/337a7b4748892138d09d to your computer and use it in GitHub Desktop.
Save Nullreff/337a7b4748892138d09d to your computer and use it in GitHub Desktop.
Wire Debugging
redpile.behavior('power_wire', {'POWER'}, function(node, messages)
local debug = node.location.x == 0 and node.location.y == 0 and (node.location.z == -2 or node.location.z == -3)
if debug then
print("--- Node " .. node.location.x .. "," .. node.location.y .. "," .. node.location.z .. " ---")
print("Message count: " .. messages.count)
print("Input:")
messages:each(function(message)
print(" " .. message.value .. " from " .. message.source.x .. "," .. message.source.y .. "," .. message.source.z)
end)
end
local covered = node:adjacent(UP).type ~= 'AIR'
local power_msg = messages:max()
node.power = power_msg and power_msg.value or 0
if debug then
print("Power: " .. node.power)
end
if node.power == 0 then
return
end
node:adjacent_each(DOWN, function(found)
if found.type == 'CONDUCTOR' and
has_lower_power(found, messages, node.power)
then
found:send('POWER', 0, node.power)
end
end)
if node.power == 1 then
return
end
local wire_power = node.power - 1
if debug then
print("Output:")
end
node:adjacent_each(NORTH, SOUTH, EAST, WEST, function(found, dir)
if found.type == 'AIR' then
found = found:adjacent(DOWN)
if found.type ~= 'WIRE' then
return
end
elseif found.type == 'CONDUCTOR' then
if node:adjacent(redpile.direction_left(dir)).type ~= 'WIRE' and
node:adjacent(redpile.direction_right(dir)).type ~= 'WIRE' and
has_lower_power(found, messages, wire_power)
then
found:send('POWER', 0, wire_power)
end
if covered then
return
end
found = found:adjacent(UP)
if found.type ~= 'WIRE' then
return
end
end
if has_lower_power(found, messages, wire_power) then
if debug then
print(" " .. wire_power .. " to " .. found.location.x .. "," .. found.location.y .. "," .. found.location.z)
end
found:send('POWER', 0, wire_power)
end
end)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment