From 5e299edd9c319f91fde9bfddb9851135fb7351f5 Mon Sep 17 00:00:00 2001 From: justuswolff Date: Thu, 19 Feb 2026 19:56:23 +0100 Subject: [PATCH] VPrint full first code --- src/main.lua | 196 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 183 insertions(+), 13 deletions(-) diff --git a/src/main.lua b/src/main.lua index 4d785d2..e285924 100644 --- a/src/main.lua +++ b/src/main.lua @@ -658,7 +658,7 @@ local function VP_splitstack(_stack, x) if stackmod ~= 0 then 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 @@ -669,14 +669,16 @@ local function VP_splitstack(_stack, x) for _=0,x2,1 do local buf = {} 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 table.insert(stacks, buf) eind = eind + stackdiv end end - local out = {} + local out = { + height=height + } if #unevenstack > 0 then table.insert(out,unevenstack) end for _,v in pairs(stacks) do table.insert(out,v) @@ -700,14 +702,26 @@ end end end end]] -local function VP_printstack(buf, dimensions, cx,cz,direction) +local function VP_printstack(buf, dimensions, cx,cy,cz,direction) reset() + move("right") + move("forward") + move("left") move("up") - for _=1,buf["height"],1 do - move("up") + 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") + end end local ox,oz = cx,cz 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 z=1,dimensions["z"],1 do @@ -734,6 +748,73 @@ local function VP_printstack(buf, dimensions, cx,cz,direction) end return cx,cz,direction 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 integritycheck() @@ -741,6 +822,7 @@ while true do "New Design", "Print Design", "VPrint Design", + "Join VPrint", }, "Select Action") if action == 1 then @@ -776,11 +858,7 @@ while true do end end if action == 3 then - reset() - term.write("This mode is experimental! Be cautious!") - incline() - term.write("Enter to continue.") - read("") + expwarn() reset() for _,v in pairs(fs.list("designs")) do @@ -794,10 +872,102 @@ while true do else local buf,dimensions = load(name) local stacks = VP_createstack(buf, dimensions) + print("Stacks created.") + local code = randomstr(5) + print("Pair code: '"..code.."'") - local cx,cz,direction = 1,1,0 - VP_printstack(stacks, dimensions, cx,cz,direction) + write("Enter amount of partaking turtles: ") + local expam = tonumber(read()) + + 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