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

working save and load

parent a90687b6
<html lang="en">
<head>
<meta charset="utf-8">
<title>RoboEdit</title>
</head>
<body>
Block Name:<input type="text" id="name" value="default:stone"><br>
Booktext<textarea id="input"></textarea><br>
<button onclick="roboEdit()">Load</button><br>
<div id="output"></div>
<script>
var file = null;
function roboEdit(){
let s = document.getElementById("input").value;
let name = document.getElementById("name").value;
let b = s.split(",");
let out = "5:return {";
let x = 1;
let z = 1;
let y = 0;
for(let i = 0; i<b.length; i++){
if (b[i] == "z"){
z++;
} else if (b[i] == "y"){
y++;
z=0;
} else if(b[i] != "") {
out += "{[\"x\"] =" + b[i] + ", [\"meta\"] = {[\"fields\"] = {}, [\"inventory\"] = {}}, [\"y\"] = " + y + ", [\"z\"] =" + z + ", [\"name\"] = \""+ name +"\"},";
}
}
out += "}";
document.getElementById("output").innerHTML = out;
}
</script>
</body>
</html>
\ No newline at end of file
......@@ -2,15 +2,36 @@ if not setup then
--Settings
mainRobot = true
useChat = true
speed = 25
speed = 15
safetyOn = true
LookColor = "#9999FF"
--Set Look
texture = "[combine:64x64:0,0=robot_button.png\\^[resize\\:64x64:15,10=082.png\\^[resize\\:18x24:37,32=069.png\\^[resize\\:18x24"
local nameTag = colorize(LookColor,"RoboEdit V1.0")
self.set_properties({
is_visible = true,
physical = false,
textures={
"face3.png", --top
"face3.png", --bottom
texture, --east
texture, --west
texture, --north
texture, },--south
visual="cube",
visual_size = {x=1, y=1},
nametag=nameTag,
glow=16
})
--Helper functions
posToString = function(pos)
return string.concat({pos.x, ",", pos.y, ",", pos.z})
end
sayit = function(out)
out = colorize(LookColor, out)
if mainRobot then
if useChat then
say(out)
......@@ -32,7 +53,8 @@ if not setup then
end
setblock = function(pos, fill)
if safetyOn then
if keyboard.read(pos) == "air" then
local name = keyboard.read(pos)
if name == "air" or string.find(name, "^basic_robot:button") then
keyboard.set(pos,fill)
end
else
......@@ -42,6 +64,27 @@ if not setup then
lerp = function(start, e, t)
return start + t * (e-start)
end
getCorners = function(pos1, pos2)
local xsmall = pos2.x
local xbig = pos1.x
if pos1.x > pos2.x then
xsmall = pos1.x
xbig = pos2.x
end
local ysmall = pos2.y
local ybig = pos1.y
if pos1.y > pos2.y then
ysmall = pos1.y
ybig = pos2.y
end
local zsmall = pos2.z
local zbig = pos1.z
if pos1.z > pos2.z then
zsmall = pos1.z
zbig = pos2.z
end
return {x=xsmall, y=ysmall, z=zsmall},{x=xbig, y=ybig, z=zbig}
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
......@@ -68,8 +111,10 @@ end
speaker, msg = self.listen_msg()
if msg and speaker == owner then
local tokens = {}
local c = 1--work around no table insert without robot priv
for t in string.gmatch(string.concat({msg, " "}),"([^ ]*) ") do
table.insert(tokens, t)
tokens[c] = t
c=c+1
end
if tokens[1] == "RE" then
if tokens[2] == "exit" then
......@@ -113,42 +158,55 @@ if msg and speaker == owner then
sayit(string.concat({owner, ": safety set to off"}))
end
elseif tokens[2] == "save" then
local _block = tokens[3] or "default:stone"
if mainRobot 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"}))
end
elseif tokens[2] == "setnodes" then
local _fill = tonumber(tokens[3]) or 0
runit({
data = {
block = _block,
fill = _fill,
pos1 = copyCords(rom.pos1),
pos2 = copyCords(rom.pos2)
},
action = save
action = setNodes
})
running = true
sayit(string.concat({owner, ": Saveing"}))
elseif tokens[2] == "setnodes" then
local _fill = tonumber(tokens[3]) or 0
sayit(string.concat({owner, ": Setting Nodes"}))
elseif tokens[2] == "circle" then
local _r = tonumber(tokens[3]) or 1
local _fill = tonumber(tokens[4]) or 0
runit({
data = {
fill = _fill,
pos1 = copyCords(rom.pos1),
pos2 = copyCords(rom.pos2)
r = _r,
pos1 = copyCords(rom.pos1)
},
action = setNodes
action = circle
})
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
local _bookIndex = tonumber(tokens[3]) or 1
local _fill = tonumber(tokens[4]) or 1
runit({
data = {
fill = _fill,
pos1 = copyCords(rom.pos1),
pos2 = copyCords(rom.pos2),
s = _s,
e = _e
bookIndex = _bookIndex
},
action = setNodes
action = load
})
running = true
sayit(string.concat({owner, ": Loading books ", _s, " to ", _e}))
......@@ -212,7 +270,7 @@ line = function(data)
data.ta = 1/dist(data.pos1,data.pos2)
data.t = 0
end
for i = 1,speed,1 do
for i = 1,math.floor(speed/4),1 do
setblock(data.pos,data.fill)
if data.pos ~= data.pos2 and data.t < 1 then
data.t = data.t + data.ta
......@@ -226,20 +284,40 @@ line = function(data)
return data
end
save = function(data)
load = function(data)
if not data.count then
data.bookTitle, data.bookData = book.read(data.bookIndex)
data.we = deserialize(data.bookData)
data.count = 1
data.bookCount = 1
end
for i = 1,speed,1 do
if data.we[data.count] ~= nil then
setblock({x=data.we[data.count].x+data.pos1.x, y=data.we[data.count].y+data.pos1.y, z=data.we[data.count].z+data.pos1.z},data.fill)
data.count = data.count + 1
else
running = false
sayit(string.concat({owner, ": Done"}))
return
end
end
return data
end
save = function(data)--TODO
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.we = {}
data.count = 1
data.bookCount = 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})
if name ~= "air" and name ~= "ignore" then
data.we[data.count] = {x=data.pos.x-data.pos1.x,y=data.pos.y-data.pos1.y,z=data.pos.z-data.pos1.z,name=data.block}
data.count = data.count + 1
end
if data.pos.x ~= data.pos2.x then
data.pos.x = data.pos.x + data.dir.x
......@@ -247,68 +325,61 @@ save = function(data)
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
book.write(1,string.concat({data.bookCount, " ", data.block}),serialize(data.we))
data.bookCount = data.bookCount + 1
running = false
book.write(bookpos,string.concat({data.count, " ", data.block}),data.output)
sayit(string.concat({owner, ": Saving Done"}))
return data
sayit(string.concat({owner, ": Done"}))
return
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)
circle = function(data)
if not data.p then
data.pos1 = copyCords(data.pos1)
data.p = 1 - data.r
data.z = 0
data.x = data.r
data.y = 0
setblock({x=data.r+data.pos1.x,y=data.pos1.y,z=data.pos1.z},data.fill)
setblock({x=(-data.r)+data.pos1.x,y=data.pos1.y,z=data.pos1.z},data.fill)
setblock({x=data.pos1.x,y=data.pos1.y,z=data.r+data.pos1.z},data.fill)
setblock({x=data.pos1.x,y=data.pos1.y,z=(-data.r)+data.pos1.z},data.fill)
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)
for i = 1,math.floor(speed/4),1 do
data.z = data.z + 1
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
if data.p <= 0 then
data.p = data.p + 2*data.z + 1;
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)
data.x = data.x - 1
data.p = data.p + 2*data.z - 2*data.x + 1;
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
setblock({x=data.x+data.pos1.x,y=data.y+data.pos1.y,z=data.z+data.pos1.z},data.fill)
setblock({x=(-data.x)+data.pos1.x,y=data.y+data.pos1.y,z=data.z+data.pos1.z},data.fill)
setblock({x=data.x+data.pos1.x,y=data.y+data.pos1.y,z=(-data.z)+data.pos1.z},data.fill)
setblock({x=(-data.x)+data.pos1.x,y=data.y+data.pos1.y,z=(-data.z)+data.pos1.z},data.fill)
if data.x < data.z then
running = false
sayit(string.concat({owner, ": Done"}))
return
end
end
if data.x ~= data.y then
setblock({x=data.z+data.pos1.x,y=data.y+data.pos1.y,z=data.x+data.pos1.z},data.fill)
setblock({x=(-data.z)+data.pos1.x,y=data.y+data.pos1.y,z=data.x+data.pos1.z},data.fill)
setblock({x=data.z+data.pos1.x,y=data.y+data.pos1.y,z=(-data.x)+data.pos1.z},data.fill)
setblock({x=(-data.z)+data.pos1.x,y=data.y+data.pos1.y,z=(-data.x)+data.pos1.z},data.fill)
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