SHOW:
|
|
- or go back to the newest paste.
1 | local ship = peripheral.find("warpdriveShipCore") | |
2 | local lasers = peripheral.getNames() | |
3 | ||
4 | local offsetDistance = 18 | |
5 | ||
6 | for i = #lasers, 1, -1 do | |
7 | if peripheral.getType(lasers[i]) ~= "warpdriveLaserCamera" then | |
8 | table.remove(lasers, i) | |
9 | else | |
10 | peripheral.wrap(lasers[i]).beamFrequency(1420) | |
11 | end | |
12 | end | |
13 | ||
14 | ship_front, ship_right, ship_up = ship.dim_positive() | |
15 | ship_back, ship_left, ship_down = ship.dim_negative() | |
16 | ship_isInHyper = ship.isInHyperspace() | |
17 | ship_movement = { ship.movement() } | |
18 | ship_rotationSteps = ship.rotationSteps() | |
19 | ||
20 | print("Emit Scanning Laser to Jump Ship and Aligning the Mining Laser") | |
21 | ||
22 | while true do | |
23 | local event, laserName, lx, ly, lz, block, _, _, _, type, metadata, resistance = os.pullEvent() | |
24 | ||
25 | if event == "laserScanning" then | |
26 | -- Convert the laser scanning coordinates to numbers and store them | |
27 | local lastLx = tonumber(lx) | |
28 | local lastLy = tonumber(ly) | |
29 | local lastLz = tonumber(lz) | |
30 | ||
31 | -- Get current ship position from the ship core. | |
32 | local mx, my, mz = ship.getLocalPosition() | |
33 | ||
34 | print("Laser target: X:" .. lastLx .. " Y:" .. lastLy .. " Z:" .. lastLz) | |
35 | print("Ship current position: X:" .. mx .. " Y:" .. my .. " Z:" .. mz) | |
36 | ||
37 | -- Calculate the vector from laser target (L) to ship (M) | |
38 | local vx = mx - lastLx | |
39 | local vy = my - lastLy | |
40 | local vz = mz - lastLz | |
41 | ||
42 | local dist = math.sqrt(vx * vx + vy * vy + vz * vz) | |
43 | ||
44 | if dist > 0 then | |
45 | -- Normalize the vector. | |
46 | local ux = vx / dist | |
47 | local uy = vy / dist | |
48 | local uz = vz / dist | |
49 | ||
50 | -- Calculate dx, dy, dz as the point offsetDistance away from the laser target along the vector toward the ship. | |
51 | local dx = lastLx + ux * offsetDistance | |
52 | local dy = lastLy + uy * offsetDistance | |
53 | local dz = lastLz + uz * offsetDistance | |
54 | ||
55 | -- Round the coordinates to whole numbers. | |
56 | dx = math.floor(dx + 0.5) | |
57 | dy = math.floor(dy + 0.5) | |
58 | dz = math.floor(dz + 0.5) | |
59 | ||
60 | print("Calculated jump coordinates: (" .. dx .. ", " .. dy .. ", " .. dz .. ")") | |
61 | local rx, ry, rz = ship.getOrientation() | |
62 | - | print("Error: The ship and the laser target are at the same location!") |
62 | + | minForwardBack = math.abs(ship_front+ship_back+1) |
63 | minLeftRight = math.abs(ship_left+ship_right+1) | |
64 | minUpDown = math.abs(ship_up+ship_down+1) | |
65 | jx = dx-mx | |
66 | jy = dy-my | |
67 | jz = dz-mz | |
68 | ||
69 | forwardBackMov = 0 | |
70 | leftRightMov = 0 | |
71 | upDownMov = 0 | |
72 | ||
73 | if rx == 1 then | |
74 | forwardBackMov = jx | |
75 | leftRightMov = jz | |
76 | elseif rx == -1 then | |
77 | forwardBackMov = -jx | |
78 | leftRightMov = -jz | |
79 | elseif rz == 1 then | |
80 | forwardBackMov = jz | |
81 | leftRightMov = -jx | |
82 | elseif rz == -1 then | |
83 | forwardBackMov = -jz | |
84 | leftRightMov = jx | |
85 | end | |
86 | ||
87 | if math.abs(forwardBackMov) < minForwardBack or math.abs(upDownMov) < minUpDown or math.abs(leftRightMov) < minLeftRight then | |
88 | print("The movement is too small!") | |
89 | else | |
90 | leftRightMov = leftRightMov*1 | |
91 | ship.movement(forwardBackMov, upDownMov, leftRightMov) | |
92 | ship.rotationSteps(0) | |
93 | ship.command("MANUAL", true) | |
94 | end | |
95 | end | |
96 | end | |
97 |