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