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