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