View difference between Paste ID: YrgNXhhk and SBduXkcw
SHOW: | | - or go back to the newest paste.
1
local name = "<@>" -- Discord ID
2
local uri = "https://disco" -- Discord Uri
3-
local lever = "front" -- format: front,back,left,right,top,bottom ONLY
3+
local lever = "front" -- format: front, back, left, right, top, bottom ONLY
4
local failping = 0 -- format: Will ping on Failed node execution 0 = Dont Ping, 1 = Ping
5-
local ship=peripheral.find("warpdriveShipCore")
5+
local ship = peripheral.find("warpdriveShipCore")
6-
local initialx,initialy,initialz = ship.getLocalPosition()
6+
local initialx, initialy, initialz = ship.getLocalPosition()
7
local navdatafile = ".navdata"
8
local navbackup = ".navbackup"
9-
local navdata={} -- format: {{dx,dy,dz},{dx,dy,dz},...} -- these are ship coords (forward, up, right)
9+
local navdata = {} -- format: {{dx,dy,dz},{dx,dy,dz},...} -- these are ship coords (forward, up, right)
10
11
if not (ship.isInHyperspace() or ship.isInSpace()) then
12
    error("Ship needs to be in space or hyperspace!")
13
end
14-
  on = redstone.getAnalogInput(lever)
14+
15
while true do
16-
  if on > 6 then
16+
    on = redstone.getAnalogInput(lever)
17-
 print("AutoPilot: Disabled") 
17+
18
    if on > 6 then
19-
elseif on < 5 then
19+
        print("AutoPilot: Disabled") 
20
        sleep(3)
21-
print("We are currently in "..(ship.isInHyperspace() and "HYPERSPACE"or"SPACE").." at "..initialx.." "..initialy.." "..initialz)
21+
    elseif on < 5 then
22
        print("We are currently in " .. (ship.isInHyperspace() and "HYPERSPACE" or "SPACE") .. " at " .. initialx .. " " .. initialy .. " " .. initialz)
23-
if fs.exists(navdatafile) then
23+
24-
    local h = fs.open(navdatafile, "r")
24+
        if fs.exists(navdatafile) then
25-
    local text = h.readAll()
25+
            local h = fs.open(navdatafile, "r")
26-
    navdata = textutils.unserialize(text)
26+
            local text = h.readAll()
27-
    h.close()
27+
            navdata = textutils.unserialize(text)
28-
    print("Waiting 25 seconds before starting node execution...")
28+
            h.close()
29-
    sleep(25)
29+
            print("Waiting for ship core cooldown to be done before starting node execution...")
30
			os.pullEvent("shipCoreCooldownDone")
31
        end
32-
if #navdata <= 0 then -- if navdata wasnt loaded
32+
33-
    print("Enter target pos: x y z")
33+
        if #navdata <= 0 then -- if navdata wasn't loaded
34-
    local input = read()
34+
            print("Enter target pos: x y z")
35-
    local txs,tys,tzs=input:gmatch("(%-?%d+) (%-?%d+) (%-?%d+)")()
35+
            local input = read()
36-
    print("'"..txs.."'")
36+
local txs, tys, tzs = input:gmatch("(%-?%d+) (%-?%d+) (%-?%d+)")()
37-
    print("'"..tys.."'")
37+
local tx = tonumber(txs)
38-
    print("'"..tzs.."'")
38+
local ty = tonumber(tys)
39-
    local tx = tonumber(txs)
39+
local tz = tonumber(tzs)
40-
    local ty = tonumber(tys)
40+
if tx == nil or ty == nil or tz == "" then
41-
    local tz = tonumber(tzs)
41+
    error("Please use the proper number format.")
42-
    if tx == nil or ty == nil or tz == "" then
42+
43-
        error("Please use the proper number format.")
43+
local ship_len_back, ship_len_left, ship_len_down = ship.dim_negative()
44
local ship_len_front, ship_len_right, ship_len_up = ship.dim_positive()
45-
    local ship_len_back,ship_len_left,ship_len_down=ship.dim_negative()
45+
46-
    local ship_len_front,ship_len_right,ship_len_up=ship.dim_positive()
46+
function dst(x1, y1, z1, x2, y2, z2)
47
    local dx = x2 - x1
48-
    function dst(x1,y1,z1,x2,y2,z2)
48+
    local dy = y2 - y1
49-
        local dx = x2-x1
49+
    local dz = z2 - z1
50-
        local dy = y2-y1
50+
    return math.sqrt(dx * dx + dy * dy + dz * dz)
51-
        local dz = z2-z1
51+
end --dst
52-
        return math.sqrt(dx*dx+dy*dy+dz*dz)
52+
53-
    end --dst
53+
if ty - ship_len_down <= 0 or ty + ship_len_up >= 256 then
54
    error("Target y position needs to be inside " .. ship_len_down .. " and " .. 256 - ship_len_up .. ".")
55-
    if ty-ship_len_down <= 0 or ty+ship_len_up >= 256 then
55+
56-
        error("Target y position needs to be inside "..ship_len_down.." and "..256-ship_len_up..".")
56+
57
local stayInHyper = true -- Assuming you always want to stay in hyperspace
58
print("----------------------------------")
59-
    print("Do you want to stay in hyperspace after jumping? (Y/n)")
59+
print("Is this information correct?")
60-
    local input2 = read()
60+
print("Target coords: ", tx, ty, tz)
61-
    local stayInHyper = input2~="n" and input2~="N"
61+
print("Target dimension: " .. (stayInHyper and "HYPERSPACE" or "SPACE"))
62-
    print("----------------------------------")
62+
print("Total distance: " .. math.floor(dst(initialx, initialy, initialz, tx, ty, tz)) .. " blocks")
63-
    print("Is this information correct?")
63+
print("----------------------------------")
64-
    print("Target coords: ", tx,ty,tz)
64+
65-
    print("Target dimension: "..(stayInHyper and "HYPERSPACE" or "SPACE"))
65+
print("Computing navigation steps...")
66-
    print("Total distance: "..math.floor(dst(initialx,initialy,initialz,tx,ty,tz)).." blocks")
66+
67-
    print("----------------------------------")
67+
68-
    for i=1,0,-1 do    
68+
69
    local orix,_,oriz = ship.getOrientation()
70-
        term.write("Please double check the info..."..i.."   ")
70+
71
    if orix == 1 then
72
        shipdeltafront = tx-initialx
73-
    term.clearLine()
73+
74-
    print()
74+
75-
    print("Enter 'Y' or press ENTER to engage navigation.")
75+
76-
    local input3 = read()
76+
77-
    if input3 ~= "Y" and input3 ~="y" and input3 ~="" then
77+
78-
        print("Aborting nagivation.")
78+
79-
        return
79+
80
    elseif oriz == -1 then
81-
    print("Computing navigation steps...")
81+
82
        shipdeltaright = tx-initialx
83
    else 
84
        error("Unexpected ship orientation!")
85
    end
86
 
87
    print("We need to move "..shipdeltafront.." block(s) forward, "..shipdeltaup.." block(s) upward and "..shipdeltaright.." block(s) to the right.")
88
    local minforward = ship_len_front+ship_len_back
89
    local minup = ship_len_up+ship_len_down
90
    local minright = ship_len_right+ship_len_left
91
    ship.command("MANUAL",false)
92
    local success, maxdist = ship.getMaxJumpDistance()
93
    if not success then error("unable to get the ships max jump distance: "..maxdist) end
94
    if maxdist <= 0 then error("max jump distance is zero") end
95
    print("Max jump length = "..maxdist)
96
    function computeMove(mindist,remaining,ignoreconstraint) -- returns a move to make along that axis
97
        if remaining == 0 then return 0 end
98
        
99
        local remsign = (remaining < 0) and -1 or 1
100
            
101
        if math.abs(remaining) < mindist and not ignoreconstraint then -- if the move would be impossible because it is too short, move further away
102
            return -remsign * mindist
103
        end
104
        return remsign * math.min(math.abs(remaining),maxdist)  
105
    end
106
 
107
    repeat
108
        local move = {} 
109
        move[2] = computeMove(minup+1,shipdeltaup,true) --y     
110
        shipdeltaup = shipdeltaup - move[2]
111
        
112
        move[1] = computeMove(minforward+1,shipdeltafront,math.abs(move[2]) > minup) --x
113
        
114
        if not (math.abs(move[2]) > minup) and  shipdeltafront == 0 and shipdeltaright == 0 and shipdeltaup ~= 0 and move[1] == 0 then
115
            move[1] = minforward+1
116
        end
117
        
118
        shipdeltafront = shipdeltafront - move[1]
119
        move[3] = computeMove(minright+1,shipdeltaright, math.abs(move[2]) > minup or math.abs(move[1]) > minforward) --z
120
        shipdeltaright = shipdeltaright - move[3]
121
        navdata[#navdata+1] = move
122
        print("Computed move: #"..#navdata..": "..move[1].." block(s) forward, "..move[2].." block(s) upward and "..move[3].." block(s) to the right.")
123
        print("Remaining: "..shipdeltafront..":"..shipdeltaup..":"..shipdeltaright)
124
        
125
    until shipdeltafront == 0 and shipdeltaup == 0 and shipdeltaright == 0
126
    print("Navigational path plotted using "..#navdata.." jump(s).")
127
end
128
for i=1,#navdata do
129
    local move = navdata[i]
130
        
131
    print("Executing next node... There are "..#navdata.." node(s) left to execute.")
132
 http.post(uri,"{\"content\":\"```yaml\\nAutopilot: Ship currently at: "..initialx..", "..initialy..", "..initialz..". Executing next node There are "..#navdata.." node(s) left to execute.```\"}",{['content-type']="application/json"})
133
    table.remove(navdata,1)
134
    if fs.exists(navbackup) then
135
    fs.delete(navbackup)
136
end
137
 if #navdata > 0 then
138
  local text = textutils.serialize(navdata) 
139
        local h = fs.open(navdatafile, "w")
140
        h.write(text)
141
        h.close()
142
        fs.copy(navdatafile, navbackup)
143
    else
144
        fs.delete(navdatafile)
145
    end
146
    ship.command("MANUAL", false)
147
    ship.movement(move[1],move[2],move[3])
148-
 http.post(uri,"{\"content\":\"```yaml\\nAutopilot: Executing next node There are "..#navdata.." node(s) left to execute.```\"}",{['content-type']="application/json"})
148+
149
    ship.command("MANUAL", true)
150
    
151
    for i=60,0,-1 do    
152
        term.setCursorPos(1,select(2,term.getCursorPos()))
153
        term.write("Waiting for the ship to jump..."..i.."   ")
154
        sleep(1)
155
    end
156
number = #navdata + 1
157
http.post(uri,"{\"content\":\"```yaml\\nAutoPilot: Ship currently at: "..initialx..", "..initialy..", "..initialz..". Failed Executing node "..number..".```\"}",{['content-type']="application/json"})
158
fs.delete(navdatafile)
159
fs.copy(navbackup, navdatafile)
160
print("The ship did not jump.")
161
if failping >= 0 then
162
http.post(uri,"{\"content\":\""..name.."\"}",{['content-type']="application/json"})
163
end
164
       
165
sleep(3)
166
os.reboot()
167
end
168
end
169
end