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