VPrint full first code

This commit is contained in:
justuswolff
2026-02-19 19:56:23 +01:00
parent 35bd00feb9
commit 5e299edd9c

View File

@@ -658,7 +658,7 @@ local function VP_splitstack(_stack, x)
if stackmod ~= 0 then if stackmod ~= 0 then
for tempstackindex=0,stackmod,1 do for tempstackindex=0,stackmod,1 do
table.insert(unevenstack, stack[posasstring(math.fmod(tempstackindex, sx),math.floor(tempstackindex/sx))]) unevenstack[posasstring(math.fmod(tempstackindex, sx),math.floor(tempstackindex/sx))] = stack[posasstring(math.fmod(tempstackindex, sx),math.floor(tempstackindex/sx))]
end end
end end
@@ -669,14 +669,16 @@ local function VP_splitstack(_stack, x)
for _=0,x2,1 do for _=0,x2,1 do
local buf = {} local buf = {}
for stackind=eind,stackdiv+eind,1 do for stackind=eind,stackdiv+eind,1 do
table.insert(buf, stack[posasstring(math.fmod(stackind, sx),math.floor(stackind/sx))]) buf[posasstring(math.fmod(stackind, sx),math.floor(stackind/sx))] = stack[posasstring(math.fmod(stackind, sx),math.floor(stackind/sx))]
end end
table.insert(stacks, buf) table.insert(stacks, buf)
eind = eind + stackdiv eind = eind + stackdiv
end end
end end
local out = {} local out = {
height=height
}
if #unevenstack > 0 then table.insert(out,unevenstack) end if #unevenstack > 0 then table.insert(out,unevenstack) end
for _,v in pairs(stacks) do for _,v in pairs(stacks) do
table.insert(out,v) table.insert(out,v)
@@ -700,14 +702,26 @@ end
end end
end end
end]] end]]
local function VP_printstack(buf, dimensions, cx,cz,direction) local function VP_printstack(buf, dimensions, cx,cy,cz,direction)
reset() reset()
move("right")
move("forward")
move("left")
move("up") move("up")
for _=1,buf["height"],1 do if buf["height"] < cy then
for _=1,math.abs(buf["height"]-cy),1 do
move("down")
end
else
for _=1,buf["height"]-cy,1 do
move("up") move("up")
end end
end
local ox,oz = cx,cz local ox,oz = cx,cz
cx,cz,direction = VP_moveto(cx+1,cz, cx,cz,direction) cx,cz,direction = VP_moveto(cx+1,cz, cx,cz,direction)
move("left")
move("forward")
move("right")
for x=1,dimensions["x"],1 do for x=1,dimensions["x"],1 do
for z=1,dimensions["z"],1 do for z=1,dimensions["z"],1 do
@@ -734,6 +748,73 @@ local function VP_printstack(buf, dimensions, cx,cz,direction)
end end
return cx,cz,direction return cx,cz,direction
end end
local function VP_calccost(stacks, height,sx,sz) -- calculate needed blocks
local cost = 0
for cx=1,sx,1 do
for cz=1,sz,1 do
local tempbuf = stacks[posasstring(cx,cz)]
if tempbuf then
for ind=1,height,1 do
if tempbuf[ind] then cost = cost + 1 end
end
end
end
end
return cost
end
local function VP_selectpos(design,dimensions,msg, cx,cz)
cx,cz = cx or 0, cz or 0
local camx,camz = cx,cz
while true do
render(design,{}, camx,1,camz, camx,camz, dimensions["x"],dimensions["z"], {1,4})
term.setCursorPos(1,1)
term.write(msg)
local _,y = term.getSize()
term.setCursorPos(1,y)
term.write("x: ")
term.write(tostring(camx))
term.write(" z: ")
term.write(tostring(camz))
local _,key = os.pullEvent("key")
key = keys.getName(key)
if key == "w" then
camz = camz - 1
end
if key == "s" then
camz = camz + 1
end
if key == "a" then
camx = camx - 1
end
if key == "d" then
camx = camx + 1
end
if key == "enter" then
break
end
end
return camx,camz
end
local function expwarn()
reset()
term.write("This mode is experimental! Be cautious!")
incline()
term.write("Enter to continue.")
read("")
end
local function randomstr(length)
local possible = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
local out = ""
for _=1,length,1 do
local ind = math.random(#possible)
out = out .. string.sub(possible, ind, ind)
end
return out
end
while true do while true do
integritycheck() integritycheck()
@@ -741,6 +822,7 @@ while true do
"New Design", "New Design",
"Print Design", "Print Design",
"VPrint Design", "VPrint Design",
"Join VPrint",
}, "Select Action") }, "Select Action")
if action == 1 then if action == 1 then
@@ -776,11 +858,7 @@ while true do
end end
end end
if action == 3 then if action == 3 then
reset() expwarn()
term.write("This mode is experimental! Be cautious!")
incline()
term.write("Enter to continue.")
read("")
reset() reset()
for _,v in pairs(fs.list("designs")) do for _,v in pairs(fs.list("designs")) do
@@ -794,10 +872,102 @@ while true do
else else
local buf,dimensions = load(name) local buf,dimensions = load(name)
local stacks = VP_createstack(buf, dimensions) local stacks = VP_createstack(buf, dimensions)
print("Stacks created.")
local code = randomstr(5)
print("Pair code: '"..code.."'")
local cx,cz,direction = 1,1,0 write("Enter amount of partaking turtles: ")
VP_printstack(stacks, dimensions, cx,cz,direction) local expam = tonumber(read())
end
write("Press enter to send pair request.")
read("")
rednet.broadcast({
code=code,
}, "HB_vprint_pair")
print("Sent. awaiting pair accepts.")
local accepted = {}
while #accepted < expam do
local id,msg = rednet.receive("HB_vprint_pairespond")
if msg["code"] == code then
table.insert(accepted, id)
reset()
term.write(tostring(#accepted).."/"..tostring(expam))
end
end
reset()
print("Paired. Splitting stacks and sending out...")
stacks = VP_splitstack(stacks, expam+1) -- plus 1 because we also build as the master turtle.
for i,v in pairs(accepted) do
reset()
print("ID "..tostring(v).." needs "..VP_calccost(stacks[i], stacks["height"],dimensions["x"],dimensions["z"]).." blocks.")
print("Please ensure that turtle has that many blocks and press enter to continue.")
read("")
rednet.send(v, {
stack=stacks[i],
dimensions=dimensions
}, "HB_vprint_pairacknowledge")
end
local x,y = 0,0
for _,v in pairs(accepted) do
x,y = VP_selectpos(buf,dimensions,"Select position of turtle "..tostring(v), x,y)
rednet.send(v, {
cx=x,
cy=y,
}, "HB_vprint_pairpossend")
end
reset()
write("Ready to begin, press enter to continue")
read("")
print("Sending begin signal.")
for _,v in pairs(accepted) do
rednet.send(v, {}, "HB_vprint_begin")
end
VP_printstack(stacks[expam+1],dimensions, 1,1,1,0)
end
end
if action == 4 then
expwarn()
reset()
term.write("Enter the code: ")
local code = read()
local masterid = 0
local buf = {}
local dimensions = {}
local cx,cz = 0,0
while true do -- await pairing request and respond
local id,request = rednet.receive("HB_vprint_pair")
if request["code"] == code then
masterid = id
print("Detected master: "..tostring(masterid))
print("Responding...")
rednet.send(masterid, {
code=code,
}, "HB_vprint_pairespond")
print("Waiting for response...")
local id,msg = -1,{}
repeat
id,msg = rednet.receive("HB_vprint_pairacknowledge")
until id == masterid
buf = msg["stack"]
dimensions = msg["dimensions"]
break
end
end
print("Received stacks and dimensions. ID: "..tostring(os.getComputerID()))
local id,msg = -1,"" -- wait until we get our current position
repeat
id,msg = rednet.receive("HB_vprint_pairpossend")
until id == masterid
cx,cz = msg["cx"],msg["cz"]
repeat -- wait until we can begin
id = rednet.receive("HB_vprint_begin")
until id == masterid
VP_printstack(buf,dimensions, cx,1,cz,0)
end end
end end