Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- height,width,length = 16,15,8
- startx,starty,startz = 0,0,0
- finalx,finaly,finalz = 15,14,7
- local tArgs = {...}
- if #tArgs == 0 then
- print("normal: run vanilla version")
- print("distance: run with distance formula")
- error("pick an arg")
- end
- function get(name,address)
- local response = nil
- local tmp = fs.open(name,"w")
- local function setColor(color)
- if term.isColor() then
- term.setTextColor(color)
- end
- end
- setColor(colors.yellow)
- if address:sub(1,4) == "http" then
- write("Connecting to Github.com... ")
- response = http.get(address)
- else
- write("Connecting to Pastebin.com... ")
- response = http.get("http://pastebin.com/raw/"..tostring(address))
- end
- if response then
- setColor(colors.green)
- print("Success")
- tmp.write( response.readAll() )
- tmp.close()
- setColor(colors.white)
- return true
- else
- setColor(colors.red)
- print("Failed")
- setColor(colors.white)
- return false
- end
- end
- get("3opt","Lh9TZzTw")
- get("textutilsFIX","3wguFBXn")
- get("instructions","txAUaKmw")
- shell.run("3opt")
- os.loadAPI("textutilsFIX")
- instructions = textutilsFIX.unserialize( fs.open("instructions","r").readAll() )
- function calculateDistance(tNode1,tNode2)
- local turnCost = 0
- local deltaY,deltaZ
- local y1 = tNode1[2]
- local z1 = tNode1[3]
- local y2 = tNode2[2]
- local z2 = tNode2[3]
- deltaZ = z2-z1
- deltaY = y2-y1
- if deltaZ == 0 or deltaY == 0 then
- turnCost = 0
- else
- turnCost = 1
- end
- if tArgs[1] == "distance" then
- return math.sqrt( (z2-z1)^2 + (y2-y1)^2 )
- else
- return math.abs(deltaZ) + math.abs(deltaY) + turnCost
- end
- end
- getBlockId = function(x,y,z)
- for n = 1,#instructions do
- local xyz = instructions[n]
- if xyz[1] == x and xyz[2] == y and xyz[3] == z then
- return xyz[4]
- end
- end
- end
- getData = function(x,y,z)
- for n = 1,#instructions do
- local xyz = instructions[n]
- if xyz[1] == x and xyz[2] == y and xyz[3] == z then
- return xyz[5]
- end
- end
- end
- --deconstruct
- local function instructions2layers(instructions)
- local layers = {}
- for x = startx,finalx do
- layers[x] = {}
- for n=1,#instructions do
- if instructions[n][1] == x then
- table.insert(layers[x],{unpack(instructions[n],1,3)})
- end
- end
- end
- return layers
- end
- function printNodes(nodes)
- for i,v in pairs(nodes) do
- textutils.pagedPrint("["..i.."] = {"..v[1]..","..v[2]..","..v[3].."}")
- end
- end
- function slowNodes(nodes)
- for i,v in pairs(nodes) do
- term.setCursorPos(v[2],v[3])
- term.setBackgroundColor(2^getData(unpack(v)))
- term.write(" ")
- sleep(0)
- end
- end
- function slowSimulate(layers)
- shell.run("clr")
- for i = startx,finalx do
- slowNodes(layers[i])
- term.setBackgroundColor(colors.black)
- shell.run("clr")
- end
- end
- function simulateIns(instructions)
- local lastx = 0
- shell.run("clr")
- for n = 1,#instructions do
- if lastx~=instructions[n][1] then
- term.setBackgroundColor(colors.black)
- shell.run("clr")
- end
- term.setCursorPos(instructions[n][2]+1,instructions[n][3]+1)
- term.setBackgroundColor(2^instructions[n][5])
- term.write(" ")
- lastx = instructions[n][1]
- sleep(0)
- end
- end
- --textutils.pagedPrint(textutils.serialize(layers))
- --printNodes(layers[0])
- local function organize(layers)
- local startingPosition = {startx,starty,startz}
- for x = startx,finalx do
- table.insert(layers[x],1,startingPosition)
- layers[x] = tsp_algorithm(layers[x])
- startingPosition = layers[x][#layers[x]]
- --startingPosition[1] = startingPosition[1] + 1
- end
- return layers
- end
- local function layers2instructions(layers)
- local instructions = {}
- --organizedlayers only
- for x = startx,finalx do
- for i = 2,#layers[x] do
- table.insert(instructions,layers[x][i])
- end
- end
- return instructions
- end
- local function add_id_and_data(instructions)
- for n = 1,#instructions do
- instructions[n][4] = getBlockId(unpack(instructions[n],1,3))
- instructions[n][5] = getData(unpack(instructions[n],1,3))
- end
- return instructions
- end
- layers = instructions2layers(instructions)
- layers = organize(layers)
- new_instructions = layers2instructions(layers)
- new_instructions = add_id_and_data(new_instructions)
- --slowSimulate(layers)
- --printNodes(layers[1])
- --textutils.pagedPrint(textutils.serialize(new_instructions))
- --for i,v in pairs(new_instructions) do
- --textutils.pagedPrint(i..": "..v[1]..","..v[2]..","..v[3]..","..v[4]..","..v[5])
- --end
- simulateIns(new_instructions)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement