Trotnic

Untitled

Dec 8th, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.54 KB | None | 0 0
  1. inf = basics.inf
  2. str_xyz = basics.str_xyz
  3.  
  4. --lua_print = print
  5. --log_file = fs.open('log.txt', 'w')
  6. --function print(thing)
  7. -- lua_print(thing)
  8. -- log_file.writeLine(thing)
  9. --end
  10.  
  11.  
  12. bumps = {
  13. north = { 0, 0, -1},
  14. south = { 0, 0, 1},
  15. east = { 1, 0, 0},
  16. west = {-1, 0, 0},
  17. }
  18.  
  19.  
  20. left_shift = {
  21. north = 'west',
  22. south = 'east',
  23. east = 'north',
  24. west = 'south',
  25. }
  26.  
  27.  
  28. right_shift = {
  29. north = 'east',
  30. south = 'west',
  31. east = 'south',
  32. west = 'north',
  33. }
  34.  
  35.  
  36. reverse_shift = {
  37. north = 'south',
  38. south = 'north',
  39. east = 'west',
  40. west = 'east',
  41. }
  42.  
  43.  
  44. move = {
  45. forward = turtle.forward,
  46. up = turtle.up,
  47. down = turtle.down,
  48. back = turtle.back,
  49. left = turtle.turnLeft,
  50. right = turtle.turnRight
  51. }
  52.  
  53.  
  54. detect = {
  55. forward = turtle.detect,
  56. up = turtle.detectUp,
  57. down = turtle.detectDown
  58. }
  59.  
  60.  
  61. inspect = {
  62. forward = turtle.inspect,
  63. up = turtle.inspectUp,
  64. down = turtle.inspectDown
  65. }
  66.  
  67.  
  68. dig = {
  69. forward = turtle.dig,
  70. up = turtle.digUp,
  71. down = turtle.digDown
  72. }
  73.  
  74. attack = {
  75. forward = turtle.attack,
  76. up = turtle.attackUp,
  77. down = turtle.attackDown
  78. }
  79.  
  80.  
  81. getblock = {
  82.  
  83. up = function(pos, fac)
  84. if not pos then pos = state.location end
  85. if not fac then fac = state.orientation end
  86. return {x = pos.x, y = pos.y + 1, z = pos.z}
  87. end,
  88.  
  89. down = function(pos, fac)
  90. if not pos then pos = state.location end
  91. if not fac then fac = state.orientation end
  92. return {x = pos.x, y = pos.y - 1, z = pos.z}
  93. end,
  94.  
  95. forward = function(pos, fac)
  96. if not pos then pos = state.location end
  97. if not fac then fac = state.orientation end
  98. local bump = bumps[fac]
  99. return {x = pos.x + bump[1], y = pos.y + bump[2], z = pos.z + bump[3]}
  100. end,
  101.  
  102. back = function(pos, fac)
  103. if not pos then pos = state.location end
  104. if not fac then fac = state.orientation end
  105. local bump = bumps[fac]
  106. return {x = pos.x - bump[1], y = pos.y - bump[2], z = pos.z - bump[3]}
  107. end,
  108.  
  109. left = function(pos, fac)
  110. if not pos then pos = state.location end
  111. if not fac then fac = state.orientation end
  112. local bump = bumps[left_shift[fac]]
  113. return {x = pos.x + bump[1], y = pos.y + bump[2], z = pos.z + bump[3]}
  114. end,
  115.  
  116. right = function(pos, fac)
  117. if not pos then pos = state.location end
  118. if not fac then fac = state.orientation end
  119. local bump = bumps[right_shift[fac]]
  120. return {x = pos.x + bump[1], y = pos.y + bump[2], z = pos.z + bump[3]}
  121. end,
  122. }
  123.  
  124.  
  125. function up()
  126. return go('up')
  127. end
  128.  
  129.  
  130. function forward()
  131. return go('forward')
  132. end
  133.  
  134.  
  135. function down()
  136. return go('down')
  137. end
  138.  
  139.  
  140. function back()
  141. return go('back')
  142. end
  143.  
  144.  
  145. function left()
  146. return go('left')
  147. end
  148.  
  149.  
  150. function right()
  151. return go('right')
  152. end
  153.  
  154.  
  155. function follow_route(route)
  156. for step in route:gmatch'.' do
  157. if step == 'u' then
  158. if not go('up') then return false end
  159. elseif step == 'f' then
  160. if not go('forward') then return false end
  161. elseif step == 'd' then
  162. if not go('down') then return false end
  163. elseif step == 'b' then
  164. if not go('back') then return false end
  165. elseif step == 'l' then
  166. if not go('left') then return false end
  167. elseif step == 'r' then
  168. if not go('right') then return false end
  169. end
  170. end
  171. return true
  172. end
  173.  
  174.  
  175. function face(orientation)
  176. if state.orientation == orientation then
  177. return true
  178. elseif right_shift[state.orientation] == orientation then
  179. if not go('right') then return false end
  180. elseif left_shift[state.orientation] == orientation then
  181. if not go('left') then return false end
  182. elseif right_shift[right_shift[state.orientation]] == orientation then
  183. if not go('right') then return false end
  184. if not go('right') then return false end
  185. else
  186. return false
  187. end
  188. return true
  189. end
  190.  
  191.  
  192. function log_movement(direction)
  193. if direction == 'up' then
  194. state.location.y = state.location.y +1
  195. elseif direction == 'down' then
  196. state.location.y = state.location.y -1
  197. elseif direction == 'forward' then
  198. bump = bumps[state.orientation]
  199. state.location = {x = state.location.x + bump[1], y = state.location.y + bump[2], z = state.location.z + bump[3]}
  200. elseif direction == 'back' then
  201. bump = bumps[state.orientation]
  202. state.location = {x = state.location.x - bump[1], y = state.location.y - bump[2], z = state.location.z - bump[3]}
  203. elseif direction == 'left' then
  204. state.orientation = left_shift[state.orientation]
  205. elseif direction == 'right' then
  206. state.orientation = right_shift[state.orientation]
  207. end
  208. return true
  209. end
  210.  
  211.  
  212. function go(direction, nodig)
  213. if not nodig then
  214. if detect[direction] then
  215. if detect[direction]() then
  216. safedig(direction)
  217. end
  218. end
  219. end
  220. if not move[direction] then
  221. return false
  222. end
  223. if not move[direction]() then
  224. if attack[direction] then
  225. attack[direction]()
  226. end
  227. return false
  228. end
  229. log_movement(direction)
  230. return true
  231. end
  232.  
  233.  
  234. function go_to_axis(axis, coordinate, nodig)
  235. local delta = coordinate - state.location[axis]
  236. if delta == 0 then
  237. return true
  238. end
  239.  
  240. if axis == 'x' then
  241. if delta > 0 then
  242. if not face('east') then return false end
  243. else
  244. if not face('west') then return false end
  245. end
  246. elseif axis == 'z' then
  247. if delta > 0 then
  248. if not face('south') then return false end
  249. else
  250. if not face('north') then return false end
  251. end
  252. end
  253.  
  254. for i = 1, math.abs(delta) do
  255. if axis == 'y' then
  256. if delta > 0 then
  257. if not go('up', nodig) then return false end
  258. else
  259. if not go('down', nodig) then return false end
  260. end
  261. else
  262. if not go('forward', nodig) then return false end
  263. end
  264. end
  265. return true
  266. end
  267.  
  268.  
  269. function go_to(end_location, end_orientation, path, nodig)
  270. if path then
  271. for axis in path:gmatch'.' do
  272. if not go_to_axis(axis, end_location[axis], nodig) then return false end
  273. end
  274. elseif end_location.path then
  275. for axis in end_location.path:gmatch'.' do
  276. if not go_to_axis(axis, end_location[axis], nodig) then return false end
  277. end
  278. else
  279. return false
  280. end
  281. if end_orientation then
  282. if not face(end_orientation) then return false end
  283. elseif end_location.orientation then
  284. if not face(end_location.orientation) then return false end
  285. end
  286. return true
  287. end
  288.  
  289.  
  290. function go_route(route, xyzo)
  291. local xyz_string
  292. if xyzo then
  293. xyz_string = str_xyz(xyzo)
  294. end
  295. local location_str = basics.str_xyz(state.location)
  296. while route[location_str] and location_str ~= xyz_string do
  297. if not go_to(route[location_str], nil, 'xyz') then return false end
  298. location_str = basics.str_xyz(state.location)
  299. end
  300. if xyzo then
  301. if location_str ~= xyz_string then
  302. return false
  303. end
  304. if xyzo.orientation then
  305. if not face(xyzo.orientation) then return false end
  306. end
  307. end
  308. return true
  309. end
  310.  
  311.  
  312. function go_to_home()
  313. state.updated_not_home = nil
  314. if basics.in_area(state.location, config.locations.home_area) then
  315. return true
  316. elseif basics.in_area(state.location, config.locations.greater_home_area) then
  317. if not go_to_home_exit() then return false end
  318. elseif basics.in_area(state.location, config.locations.waiting_room_area) then
  319. if not go_to(config.locations.mine_exit, nil, config.paths.waiting_room_to_mine_exit, true) then return false end
  320. elseif state.location.y < config.locations.mine_enter.y then
  321. return false
  322. end
  323. if config.locations.main_loop_route[basics.str_xyz(state.location)] then
  324. if not go_route(config.locations.main_loop_route, config.locations.home_enter) then return false end
  325. elseif basics.in_area(state.location, config.locations.control_room_area) then
  326. if not go_to(config.locations.home_enter, nil, config.paths.control_room_to_home_enter, true) then return false end
  327. else
  328. return false
  329. end
  330. if not forward() then return false end
  331. while detect.down() do
  332. if not forward() then return false end
  333. end
  334. if not down() then return false end
  335. if not right() then return false end
  336. if not right() then return false end
  337. return true
  338. end
  339.  
  340.  
  341. function go_to_home_exit()
  342. if basics.in_area(state.location, config.locations.greater_home_area) then
  343. if not go_to(config.locations.home_exit, nil, config.paths.home_to_home_exit) then return false end
  344. elseif config.locations.main_loop_route[basics.str_xyz(state.location)] then
  345. if not go_route(config.locations.main_loop_route, config.locations.home_exit) then return false end
  346. else
  347. return false
  348. end
  349. return true
  350. end
  351.  
  352.  
  353. function go_to_item_drop()
  354. if not config.locations.main_loop_route[basics.str_xyz(state.location)] then
  355. if not go_to_home() then return false end
  356. if not go_to_home_exit() then return false end
  357. end
  358. if not go_route(config.locations.main_loop_route, config.locations.item_drop) then return false end
  359. return true
  360. end
  361.  
  362.  
  363. function go_to_refuel()
  364. if not config.locations.main_loop_route[basics.str_xyz(state.location)] then
  365. if not go_to_home() then return false end
  366. if not go_to_home_exit() then return false end
  367. end
  368. if not go_route(config.locations.main_loop_route, config.locations.refuel) then return false end
  369. return true
  370. end
  371.  
  372.  
  373. function go_to_waiting_room()
  374. if not basics.in_area(state.location, config.locations.waiting_room_line_area) then
  375. if not go_to_home() then return false end
  376. end
  377. if not go_to(config.locations.waiting_room, nil, config.paths.home_to_waiting_room) then return false end
  378. return true
  379. end
  380.  
  381.  
  382. function go_to_mine_enter()
  383. if not go_route(config.locations.waiting_room_to_mine_enter_route) then return false end
  384. return true
  385. end
  386.  
  387.  
  388. function go_to_strip(strip)
  389. if state.location.y < config.locations.mine_enter.y or basics.in_location(state.location, config.locations.mine_enter) then
  390. if state.type == 'mining' then
  391. local bump = bumps[strip.orientation]
  392. strip = {
  393. x = strip.x + bump[1],
  394. y = strip.y + bump[2],
  395. z = strip.z + bump[3],
  396. orientation = strip.orientation
  397. }
  398. end
  399. if not go_to(strip, nil, config.paths.mine_enter_to_strip) then return false end
  400. return true
  401. end
  402. end
  403.  
  404.  
  405. function go_to_mine_exit(strip)
  406. if state.location.y < config.locations.mine_enter.y or (state.location.x == config.locations.mine_exit.x and state.location.z == config.locations.mine_exit.z) then
  407. if state.location.x == config.locations.mine_enter.x and state.location.z == config.locations.mine_enter.z then
  408. -- If directly under mine_enter, shift over to exit
  409. if not go_to_axis('z', config.locations.mine_exit.z) then return false end
  410. elseif state.location.x ~= config.locations.mine_exit.x or state.location.z ~= config.locations.mine_exit.z then
  411. -- If NOT directly under mine_exit go to proper y
  412. if not go_to_axis('y', strip.y + 1) then return false end
  413. if state.location.z ~= config.locations.mine_enter.z and strip.z ~= config.locations.mine_enter.z then
  414. -- If not in main_shaft, find your strip
  415. if not go_to_axis('x', strip.x) then return false end
  416. end
  417. if state.location.x ~= config.locations.mine_exit.x then
  418. -- If not in strip x = origin, go to main_shaft
  419. if not go_to_axis('z', config.locations.mine_enter.z) then return false end
  420. end
  421. end
  422. if not go_to(config.locations.mine_exit, nil, 'xzy') then return false end
  423. return true
  424. end
  425. end
  426.  
  427.  
  428. function safedig(direction)
  429. -- DIG IF BLOCK NOT ON BLACKLIST
  430. if not direction then
  431. direction = 'forward'
  432. end
  433.  
  434. local block_name = ({inspect[direction]()})[2].name
  435. if block_name then
  436. for _, word in pairs(config.dig_blacklist) do
  437. if string.find(string.lower(block_name), word) then
  438. return false
  439. end
  440. end
  441.  
  442. return dig[direction]()
  443. end
  444. return true
  445. end
  446.  
  447.  
  448. function dump_items(omit)
  449. for slot = 1, 16 do
  450. if turtle.getItemCount(slot) > 0 and ((not omit) or (not omit[turtle.getItemDetail(slot).name])) then
  451. turtle.select(slot)
  452. if not turtle.drop() then return false end
  453. end
  454. end
  455. return true
  456. end
  457.  
  458.  
  459.  
  460. function prepare(min_fuel_amount)
  461. if state.item_count > 0 then
  462. if not go_to_item_drop() then return false end
  463. if not dump_items(config.fuelnames) then return false end
  464. end
  465. local min_fuel_amount = min_fuel_amount + config.fuel_padding
  466. if not go_to_refuel() then return false end
  467. if not dump_items() then return false end
  468. turtle.select(1)
  469. while turtle.getFuelLevel() < min_fuel_amount do
  470. if not turtle.suck(math.min(64, math.ceil(min_fuel_amount / config.fuel_per_unit))) then return false end
  471. turtle.refuel()
  472. end
  473. return true
  474. end
  475.  
  476.  
  477. function calibrate()
  478. -- GEOPOSITION BY MOVING TO ADJACENT BLOCK AND BACK
  479. local sx, sy, sz = gps.locate()
  480. -- if sx == config.interface.x and sy == config.interface.y and sz == config.interface.z then
  481. -- refuel()
  482. -- end
  483. if not sx or not sy or not sz then
  484. return false
  485. end
  486. for i = 1, 4 do
  487. -- TRY TO FIND EMPTY ADJACENT BLOCK
  488. if not turtle.detect() then
  489. break
  490. end
  491. if not turtle.turnRight() then return false end
  492. end
  493. if turtle.detect() then
  494. -- TRY TO DIG ADJACENT BLOCK
  495. for i = 1, 4 do
  496. safedig('forward')
  497. if not turtle.detect() then
  498. break
  499. end
  500. if not turtle.turnRight() then return false end
  501. end
  502. if turtle.detect() then
  503. return false
  504. end
  505. end
  506. if not turtle.forward() then return false end
  507. local nx, ny, nz = gps.locate()
  508. if nx == sx + 1 then
  509. state.orientation = 'east'
  510. elseif nx == sx - 1 then
  511. state.orientation = 'west'
  512. elseif nz == sz + 1 then
  513. state.orientation = 'south'
  514. elseif nz == sz - 1 then
  515. state.orientation = 'north'
  516. else
  517. return false
  518. end
  519. state.location = {x = nx, y = ny, z = nz}
  520. print('Calibrated to ' .. str_xyz(state.location, state.orientation))
  521.  
  522. back()
  523.  
  524. if basics.in_area(state.location, config.locations.home_area) then
  525. face(left_shift[left_shift[config.locations.homes.increment]])
  526. end
  527.  
  528. return true
  529. end
  530.  
  531.  
  532. function initialize(session_id, config_values)
  533. -- INITIALIZE TURTLE
  534.  
  535. state.session_id = session_id
  536.  
  537. -- COPY CONFIG DATA INTO MEMORY
  538. for k, v in pairs(config_values) do
  539. config[k] = v
  540. end
  541.  
  542. -- DETERMINE TURTLE TYPE
  543. state.peripheral_left = peripheral.getType('left')
  544. state.peripheral_right = peripheral.getType('right')
  545. if state.peripheral_right == 'modem' then
  546. state.type = 'mining'
  547. for k, v in pairs(config.mining_turtle_locations) do
  548. config.locations[k] = v
  549. end
  550. state.peripheral_left = 'pick'
  551. state.peripheral_right = 'modem'
  552. -- if state.peripheral_left == 'modem' then
  553. -- state.peripheral_right = 'pick'
  554. -- else
  555. -- state.peripheral_left = 'pick'
  556. -- end
  557. else
  558. state.type = 'chunky'
  559. for k, v in pairs(config.chunky_turtle_locations) do
  560. config.locations[k] = v
  561. end
  562. end
  563.  
  564. state.request_id = 1
  565. state.initialized = true
  566. print(state.type)
  567. print(state.peripheral_left)
  568. return true
  569. end
  570.  
  571.  
  572. function getcwd()
  573. local running_program = shell.getRunningProgram()
  574. local program_name = fs.getName(running_program)
  575. return "/" .. running_program:sub(1, #running_program - #program_name)
  576. end
  577.  
  578.  
  579. function pass()
  580. return true
  581. end
  582.  
  583.  
  584. function dump(direction)
  585. if not face(direction) then return false end
  586. if ({inspect.forward()})[2].name ~= 'computercraft:turtle_advanced' then
  587. return false
  588. end
  589. for slot = 1, 16 do
  590. if turtle.getItemCount(slot) > 0 then
  591. turtle.select(slot)
  592. turtle.drop()
  593. end
  594. end
  595. return true
  596. end
  597.  
  598.  
  599. function detect_ore(direction)
  600. if config.orenames[({inspect[direction]()})[2].name] then
  601. return true
  602. end
  603. return false
  604. end
  605.  
  606.  
  607. function scan(valid, ores)
  608. local checked_left = false
  609. local checked_right = false
  610.  
  611. local f = str_xyz(getblock.forward())
  612. local u = str_xyz(getblock.up())
  613. local d = str_xyz(getblock.down())
  614. local l = str_xyz(getblock.left())
  615. local r = str_xyz(getblock.right())
  616. local b = str_xyz(getblock.back())
  617.  
  618. if not valid[f] and valid[f] ~= false then
  619. valid[f] = detect_ore('forward')
  620. ores[f] = valid[f]
  621. end
  622. if not valid[u] and valid[u] ~= false then
  623. valid[u] = detect_ore('up')
  624. ores[u] = valid[u]
  625. end
  626. if not valid[d] and valid[d] ~= false then
  627. valid[d] = detect_ore('down')
  628. ores[d] = valid[d]
  629. end
  630. if not valid[l] and valid[l] ~= false then
  631. left()
  632. checked_left = true
  633. valid[l] = detect_ore('forward')
  634. ores[l] = valid[l]
  635. end
  636. if not valid[r] and valid[r] ~= false then
  637. right()
  638. if checked_left then
  639. right()
  640. end
  641. checked_right = true
  642. valid[r] = detect_ore('forward')
  643. ores[r] = valid[r]
  644. end
  645. if not valid[b] and valid[b] ~= false then
  646. if checked_right then
  647. right()
  648. elseif checked_left then
  649. left()
  650. else
  651. right(2)
  652. end
  653. valid[b] = detect_ore('forward')
  654. ores[b] = valid[b]
  655. end
  656. end
  657.  
  658.  
  659. function fastest_route(area, pos, fac, end_locations)
  660. local queue = {}
  661. local explored = {}
  662. table.insert(queue,
  663. {
  664. coords = {x = pos.x, y = pos.y, z = pos.z},
  665. facing = fac,
  666. path = '',
  667. }
  668. )
  669. explored[str_xyz(pos, fac)] = true
  670.  
  671. while #queue > 0 do
  672. local node = table.remove(queue, 1)
  673. if end_locations[str_xyz(node.coords)] or end_locations[str_xyz(node.coords, node.facing)] then
  674. return node.path
  675. end
  676. for _, step in pairs({
  677. {coords = node.coords, facing = left_shift[node.facing], path = node.path .. 'l'},
  678. {coords = node.coords, facing = right_shift[node.facing], path = node.path .. 'r'},
  679. {coords = getblock.forward(node.coords, node.facing), facing = node.facing, path = node.path .. 'f'},
  680. {coords = getblock.up(node.coords, node.facing), facing = node.facing, path = node.path .. 'u'},
  681. {coords = getblock.down(node.coords, node.facing), facing = node.facing, path = node.path .. 'd'},
  682. }) do
  683. explore_string = str_xyz(step.coords, step.facing)
  684. if not explored[explore_string] and (not area or area[str_xyz(step.coords)]) then
  685. explored[explore_string] = true
  686. table.insert(queue, step)
  687. end
  688. end
  689. end
  690. end
  691.  
  692.  
  693. function mine_vein(direction)
  694. if not face(direction) then return false end
  695.  
  696. -- Log starting location
  697. local start = str_xyz({x = state.location.x, y = state.location.y, z = state.location.z}, state.orientation)
  698.  
  699. -- Begin block map
  700. local valid = {}
  701. local ores = {}
  702. valid[str_xyz(state.location)] = true
  703. valid[str_xyz(getblock.back(state.location, state.orientation))] = false
  704. for i = 1, config.vein_max do
  705.  
  706. -- Scan adjacent
  707. scan(valid, ores)
  708.  
  709. -- Search for nearest ore
  710. local route = fastest_route(valid, state.location, state.orientation, ores)
  711.  
  712. -- Check if there is one
  713. if not route then
  714. break
  715. end
  716.  
  717. -- Retrieve ore
  718. turtle.select(1)
  719. if not follow_route(route) then return false end
  720. ores[str_xyz(state.location)] = nil
  721.  
  722. end
  723.  
  724. if not follow_route(fastest_route(valid, state.location, state.orientation, {[start] = true})) then return false end
  725.  
  726. if detect.up() then
  727. safedig('up')
  728. end
  729.  
  730. return true
  731. end
  732.  
  733.  
  734. function clear_gravity_blocks()
  735. for _, direction in pairs({'forward', 'up'}) do
  736. while config.gravitynames[ ({inspect[direction]()})[2].name ] do
  737. safedig(direction)
  738. sleep(1)
  739. end
  740. end
  741. return true
  742. end
Advertisement
Add Comment
Please, Sign In to add comment