Commit a90687b6 authored by Jacob Piela's avatar Jacob Piela
Browse files

initial commit

parent 5fad3947
if not setup then
--Settings
mainRobot = true
useChat = true
speed = 25
safetyOn = true
--Helper functions
posToString = function(pos)
return string.concat({pos.x, ",", pos.y, ",", pos.z})
end
sayit = function(out)
if mainRobot then
if useChat then
say(out)
else
write_text.down(out)
end
end
end
copyCords = function(cord)
return {x = cord.x,y = cord.y,z = cord.z}
end
runit = function(obj)
if running == false then
action = obj
running = true
else
sayit(string.concat({owner, ": Please wait"}))
end
end
setblock = function(pos, fill)
if safetyOn then
if keyboard.read(pos) == "air" then
keyboard.set(pos,fill)
end
else
keyboard.set(pos,fill)
end
end
lerp = function(start, e, t)
return start + t * (e-start)
end
dist = function(pos1, pos2)
return math.sqrt(math.pow(pos2.x - pos1.x,2) + math.pow(pos2.y - pos1.y,2) + math.pow(pos2.z - pos1.z,2))
end
dir = function(num1,num2)
if num1 > num2 then
return -1
else
return 1
end
end
bookpos = 1
running = false
paused = false
action = nil
owner = read_text.down("owner") --Find name of robot owner
if not rom.pos1 or not rom.pos2 then
rom.pos1 = {x=0,y=0,z=0}
rom.pos2 = {x=0,y=0,z=0}
end
self.listen(1)
setup = true
end
speaker, msg = self.listen_msg()
if msg and speaker == owner then
local tokens = {}
for t in string.gmatch(string.concat({msg, " "}),"([^ ]*) ") do
table.insert(tokens, t)
end
if tokens[1] == "RE" then
if tokens[2] == "exit" then
self.remove()
elseif tokens[2] == "setpos1" then
local pos1 = {x=tonumber(tokens[3]),y=tonumber(tokens[4]),z=tonumber(tokens[5])}
if tokens[3] and pos1.x and pos1.y and pos1.z then
rom.pos1 = pos1
else
pos1 = player.getpos(owner)
rom.pos1 = {x=math.floor(pos1.x), y=math.floor(pos1.y), z=math.floor(pos1.z)}
end
sayit(string.concat({owner, ": pos1 set to ", posToString(rom.pos1)}))
elseif tokens[2] == "setpos2" then
local pos2 = {x=tonumber(tokens[3]),y=tonumber(tokens[4]),z=tonumber(tokens[5])}
if tokens[3] and pos2.x and pos2.y and pos2.z then
rom.pos2 = pos2
else
pos2 = player.getpos(owner)
rom.pos2 = {x=math.floor(pos2.x), y=math.floor(pos2.y), z=math.floor(pos2.z)}
end
sayit(string.concat({owner, ": pos2 set to ", posToString(rom.pos2)}))
elseif tokens[2] == "pos1" then
sayit(string.concat({owner, ": pos1 is ", posToString(rom.pos1)}))
elseif tokens[2] == "pos2" then
sayit(string.concat({owner, ": pos1 is ", posToString(rom.pos2)}))
elseif tokens[2] == "continue" then
if running and paused then
paused = false
sayit(string.concat({owner, ": resuming"}))
end
elseif tokens[2] == "safety" then
if tokens[3] == "on" then
safetyOn = true
elseif tokens[3] == "off" then
safetyOn = false
end
if safetyOn then
sayit(string.concat({owner, ": safety set to on"}))
else
sayit(string.concat({owner, ": safety set to off"}))
end
elseif tokens[2] == "save" then
local _block = tokens[3] or "default:stone"
runit({
data = {
block = _block,
pos1 = copyCords(rom.pos1),
pos2 = copyCords(rom.pos2)
},
action = save
})
running = true
sayit(string.concat({owner, ": Saveing"}))
elseif tokens[2] == "setnodes" then
local _fill = tonumber(tokens[3]) or 0
runit({
data = {
fill = _fill,
pos1 = copyCords(rom.pos1),
pos2 = copyCords(rom.pos2)
},
action = setNodes
})
running = true
sayit(string.concat({owner, ": Setting Nodes"}))
elseif tokens[2] == "load" then
local _fill = tonumber(tokens[3]) or 1
local _s = tonumber(tokens[4]) or 1
local _e = tonumber(tokens[5]) or 16
runit({
data = {
fill = _fill,
pos1 = copyCords(rom.pos1),
pos2 = copyCords(rom.pos2),
s = _s,
e = _e
},
action = setNodes
})
running = true
sayit(string.concat({owner, ": Loading books ", _s, " to ", _e}))
elseif tokens[2] == "line" then
local _fill = tonumber(tokens[3]) or 0
runit({
data = {
fill = _fill,
pos1 = copyCords(rom.pos1),
pos2 = copyCords(rom.pos2)
},
action = line
})
running = true
sayit(string.concat({owner, ": Setting Nodes"}))
elseif tokens[2] == "help" then
sayit(string.concat({owner, ": RoboEdit commands start with \"RE\".The available commands are {exit,help,setpos1,setpos2,pos1,pos2,setnodes,line,save,load}"}))
else
sayit(string.concat({owner, ": Error \"", tokens[2], "\" is not a command. Try \"RE help\""}))
end
end
end
if not paused and running then
action.data = action.action(action.data)
end
--actions
setNodes = function(data)
if not data.pos then
data.pos = copyCords(data.pos1)
data.dir = {x = dir(data.pos.x,data.pos2.x),y = dir(data.pos.y,data.pos2.y),z = dir(data.pos.z,data.pos2.z)}
end
for i = 1,speed,1 do
setblock(data.pos,data.fill)
if data.pos.x ~= data.pos2.x then
data.pos.x = data.pos.x + data.dir.x
else
data.pos.x = data.pos1.x
if data.pos.z ~= data.pos2.z then
data.pos.z = data.pos.z + data.dir.z
else
data.pos.z = data.pos1.z
if data.pos.y ~= data.pos2.y then
data.pos.y = data.pos.y + data.dir.y
else
running = false
sayit(string.concat({owner, ": Done"}))
return
end
end
end
end
return data
end
line = function(data)
if not data.pos then
data.pos = copyCords(data.pos1)
data.ta = 1/dist(data.pos1,data.pos2)
data.t = 0
end
for i = 1,speed,1 do
setblock(data.pos,data.fill)
if data.pos ~= data.pos2 and data.t < 1 then
data.t = data.t + data.ta
data.pos = {x=math.floor(lerp(data.pos1.x, data.pos2.x, data.t)+0.5), y=math.floor(lerp(data.pos1.y, data.pos2.y, data.t)+0.5), z=math.floor(lerp(data.pos1.z, data.pos2.z, data.t)+0.5)}
else
running = false
sayit(string.concat({owner, ": Done"}))
return
end
end
return data
end
save = function(data)
if not data.pos then
data.pos = copyCords(data.pos1)
data.dir = {x = dir(data.pos.x,data.pos2.x),y = dir(data.pos.y,data.pos2.y),z = dir(data.pos.z,data.pos2.z)}
data.output = ""
data.count = 1
end
for i = 1,speed,1 do
name = keyboard.read(data.pos)
if data.block == "all" then
--TODO
sayit(string.concat({owner, ": save all is unimplemented"}))
elseif name ~= "air" and name ~= "ignore" then
data.output = string.concat({data.output , "," , data.pos.x - data.pos1.x})
end
if data.pos.x ~= data.pos2.x then
data.pos.x = data.pos.x + data.dir.x
else
data.pos.x = data.pos1.x
if data.pos.z ~= data.pos2.z then
data.pos.z = data.pos.z + data.dir.z
data.output = string.concat({data.output , ",z"})
else
data.pos.z = data.pos1.z
if data.pos.y ~= data.pos2.y then
data.pos.y = data.pos.y + data.dir.y
data.output = string.concat({data.output , ",y"})
else
running = false
book.write(bookpos,string.concat({data.count, " ", data.block}),data.output)
sayit(string.concat({owner, ": Saving Done"}))
return data
end
end
end
if string.len(data.output) > 1000 then
book.write(bookpos,string.concat({data.count, " ", data.block}),data.output)
data.output = ""
bookpos = bookpos + 1
data.count = data.count + 1
if bookpos > 15 then
sayit(string.concat({owner, ": Please empty libray and then run \"RE continue\""}))
bookpos = 1
paused = true
end
end
end
return data
end
load = function(data)
if not data.pos then
data.pos = copyCords(data.pos1)
data.dir = {x = dir(data.pos.x,data.pos2.x),y = dir(data.pos.y,data.pos2.y),z = dir(data.pos.z,data.pos2.z)}
data.bookIndex = data.s
data.bookChar = 2
data.bookTitle, data.bookData = book.read(data.bookIndex)
end
for i = 1,speed,1 do
local nextComa = string.find(data.bookData, ",") or string.len(data.bookData)
local char = string.sub(data.bookData, 1, nextComa)
data.bookData = string.sub(data.bookData, nextComa+1, nextComa)
if char == "z" then
data.pos.z = data.pos.z + 1
elseif char == "y" then
data.pos.y = data.pos.y + 1
data.pos.z = data.pos1.z
else
sayit(string.concat({owner, ": ", string.len(data.bookData)}))
setblock({x=data.pos1.x+tonumber(char),y=data.pos.y,z=data.pos.z},data.fill)
end
if not string.find(data.bookData, ",") then
data.bookIndex = data.bookIndex + 1
data.bookChar = 2
data.bookTitle, data.bookData = book.read(data.bookIndex)
if data.bookIndex == data.e or not book.read(data.bookIndex) then
running = false
sayit(string.concat({owner, ": Done"}))
return
end
end
end
return data
end
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment