89 lines
2.6 KiB
Lua
89 lines
2.6 KiB
Lua
pp = require("cc.pretty")
|
|
|
|
local filename = "tobuild"
|
|
rednet.open("left")
|
|
function stringtovec(str)
|
|
parts = {}
|
|
for part in string.gmatch(str, "([^,]+)") do
|
|
table.insert(parts,tonumber(part))
|
|
end
|
|
return vector.new(unpack(parts))
|
|
end
|
|
|
|
while true do
|
|
id, message, protocol = rednet.receive()
|
|
if protocol == "getnexttobuild" then
|
|
if message then
|
|
file = fs.open(
|
|
filename,
|
|
"r"
|
|
)
|
|
lines = {}
|
|
vec = stringtovec(message)
|
|
print(vector.new().tostring(vec))
|
|
record = nil
|
|
repeat
|
|
line = file.readLine()
|
|
wec = (line and stringtovec(line)) or nil
|
|
adding = wec
|
|
if
|
|
(wec)
|
|
and (
|
|
(not record)
|
|
or
|
|
(wec.y <= record.y)
|
|
)
|
|
then
|
|
if
|
|
(not record)
|
|
or
|
|
(
|
|
(vec-wec):length()
|
|
<
|
|
(vec-record):length()
|
|
)
|
|
then
|
|
--pp.pretty_print(wec)
|
|
--write("is better than")
|
|
--pp.pretty_print(record)
|
|
adding = record
|
|
record = wec
|
|
end
|
|
end
|
|
if adding then
|
|
table.insert(lines,adding:tostring())
|
|
end
|
|
until not line
|
|
rednet.send(id,record:tostring(),"nexttobuild")
|
|
print(record)
|
|
file.close()
|
|
file = fs.open(filename,"w")
|
|
for i,v in pairs(lines) do
|
|
file.writeLine(v)
|
|
end
|
|
file.close()
|
|
|
|
else
|
|
file = fs.open(filename,"r")
|
|
line = file.readLine()
|
|
restof = file.readAll()
|
|
file.close()
|
|
file = fs.open(filename,"w")
|
|
file.write(restof)
|
|
file.close()
|
|
print(line)
|
|
rednet.send(id,line,"nexttobuild")
|
|
end
|
|
elseif protocol == "failedtobuild" then
|
|
print(message.."failed")
|
|
file = fs.open(filename,"r")
|
|
restof = file.readAll()
|
|
file.close()
|
|
file = fs.open(filename,"w")
|
|
file.writeLine(message)
|
|
file.write(restof)
|
|
file.close()
|
|
end
|
|
--file = fs.open(filename,"r")
|
|
--print(file.readAll())
|
|
end
|