Advertisement
D4rkSol1tud3

Dragon finder

Mar 26th, 2023 (edited)
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.93 KB | None | 0 0
  1. -- ......
  2. local args = { ... }
  3.  
  4. local DEBUG = false
  5. local ITEM_NOT_FOUND = -1
  6. local PERIPHERAL_SIDE = "left"
  7. local PROTOCOL_STR = "gpsl"
  8. local LOG_FILE = "lastrun.txt"
  9. local DRAGONS_FILE = "dragons_found.txt"
  10.  
  11. local TURTLE_ID = 16
  12. local RECV_ID = 7
  13.  
  14. local STRIDE = 32
  15. local STRIDES_TO_DO = 32
  16. local LATERAL_STRIDE = 32 -- default sensor range = 16
  17. local STRIPS = 32
  18. local FW_DIRECTION = 1    -- 1 foward, -1 back
  19. local BK_DIRECTION = -1
  20.  
  21. local COAL_FUEL_V = 80
  22. local REFUEL_THRESHOLD = 1000
  23.  
  24. local TURTLE_INV_SZ = 16
  25. --local WIRELESS_MODEM = "computercraft:peripheral" range limitations
  26. local WIRELESS_MODEM = "computercraft:advanced_modem"
  27.  
  28. local PICKAXE = "minecraft:diamond_pickaxe"
  29. local SENSOR = "plethora:module"
  30. local CHUNK_AWAKER = "walkingchunks:chunk_awaker"
  31. local ENDER_CHEST = "enderstorage:ender_storage"
  32. local COAL = "minecraft:coal"
  33.  
  34. -- peripheral type string returned by getType
  35. local peripheral_type = {}
  36. peripheral_type[WIRELESS_MODEM] = "modem"
  37. peripheral_type[SENSOR] = "plethora:sensor"
  38. peripheral_type[PICKAXE] = "pickaxe" -- not really it's type name, special case
  39.  
  40. local to_skip_list = { WIRELESS_MODEM, PICKAXE, SENSOR, ENDER_CHEST }
  41.  
  42. -- foward declare
  43. local msg_houston
  44. local fmt_gps_to_str
  45. local get_gps_pos
  46.  
  47. local function log_to_file(msg, log_file)
  48.     local handle = fs.open(log_file, "a")
  49.     if handle then
  50.         handle.write(msg .. '\n')
  51.         handle.close()
  52.     end
  53. end
  54.  
  55. local function log_msg(msg)
  56.     if (DEBUG) then
  57.         print("logging..\n " .. msg)
  58.     end
  59.  
  60.     log_to_file(msg, LOG_FILE)
  61.  
  62.     local tmp = log_msg
  63.     log_msg = function(msg_p)
  64.     end                           --avoid recursion
  65.     msg_houston(msg)
  66.     log_msg = tmp
  67. end
  68.  
  69. local function skip_item(item)
  70.     for i = 1, #to_skip_list, 1 do
  71.         if item.name == to_skip_list[i] then
  72.             return true
  73.         end
  74.     end
  75.     return false
  76. end
  77.  
  78. local function find_item_slot(item_name)
  79.     --log_msg("Finding item slot for " .. item_name)
  80.     for i = 1, TURTLE_INV_SZ, 1 do
  81.         turtle.select(i)
  82.         local item = turtle.getItemDetail()
  83.         if item and item.name == item_name then
  84.             return i
  85.         end
  86.     end
  87.  
  88.     return ITEM_NOT_FOUND
  89. end
  90.  
  91. local function empty_inv()
  92.     log_msg("Empting inventory...")
  93.     for i = 1, TURTLE_INV_SZ, 1 do
  94.         turtle.select(i)
  95.         local item = turtle.getItemDetail()
  96.         if item and (not skip_item(item)) then
  97.             turtle.drop()
  98.         end
  99.     end
  100. end
  101.  
  102. local function find_free_slot()
  103.     for i = 1, TURTLE_INV_SZ, 1 do
  104.         turtle.select(i)
  105.         local item = turtle.getItemDetail()
  106.         if not item then
  107.             return i
  108.         end
  109.     end
  110.  
  111.     for i = 1, TURTLE_INV_SZ, 1 do
  112.         turtle.select(i)
  113.         local item = turtle.getItemDetail()
  114.         if not skip_item(item) then
  115.             turtle.drop()
  116.             return i
  117.         end
  118.     end
  119.  
  120.     return -1
  121. end
  122.  
  123. local function equip_peripheral(p_name)
  124.     --local log = log_func or log_msg -- special case to avoid recursion
  125.     --log_msg("Equiping " .. peripheral_type[p_name])
  126.     if peripheral.getType(PERIPHERAL_SIDE) == peripheral_type[p_name] and p_name ~= PICKAXE then
  127.         --log_msg(peripheral_type[p_name] .. " already equiped, skipping search...")
  128.         return true
  129.     end
  130.  
  131.     local item_slot = find_item_slot(p_name)
  132.     if item_slot == ITEM_NOT_FOUND then
  133.         log_msg("Could not find item " .. p_name)
  134.         return false
  135.     end
  136.  
  137.     --log_msg("Found " .. peripheral_type[p_name] .. "in slot " .. tostring(item_slot))
  138.     turtle.select(item_slot)
  139.     turtle.equipLeft()
  140.     return true
  141. end
  142.  
  143. local function refuel()
  144.     log_msg("Refueling at pos " .. fmt_gps_to_str(get_gps_pos()))
  145.     local has_equiped = equip_peripheral(PICKAXE)
  146.     local fuel_chest_slot = find_item_slot(ENDER_CHEST)
  147.     if fuel_chest_slot == ITEM_NOT_FOUND then
  148.         return false
  149.     end
  150.  
  151.     if turtle.detectDown() then
  152.         turtle.digDown()
  153.     end
  154.     turtle.select(fuel_chest_slot)
  155.     turtle.placeDown()
  156.     local free_slot = find_free_slot()
  157.     turtle.select(free_slot)
  158.     turtle.suckDown()
  159.     turtle.refuel()
  160.     turtle.digDown()
  161.     -- for i = 1, chest.size(), 1 do
  162.     --     local item = chest.getItemMeta(i)
  163.     --     ifitem.name == coal then
  164.     --         free_slot = find_free_slot()
  165.     --         chest.
  166.     --     end
  167.     -- end
  168. end
  169.  
  170. local function check_and_refuel()
  171.     local fuel_level = turtle.getFuelLevel()
  172.     log_msg("Current fuel level " .. tostring(turtle.getFuelLevel()))
  173.     if fuel_level < REFUEL_THRESHOLD then
  174.         refuel()
  175.     end
  176. end
  177.  
  178. msg_houston = function(msg)
  179.     local has_equiped = equip_peripheral(WIRELESS_MODEM)
  180.     rednet.open(PERIPHERAL_SIDE)
  181.     rednet.send(RECV_ID, msg, PROTOCOL_STR)
  182.     rednet.close()
  183. end
  184.  
  185. get_gps_pos = function()
  186.     local has_equiped = equip_peripheral(WIRELESS_MODEM)
  187.     return gps.locate(5)
  188. end
  189.  
  190. fmt_gps_to_str = function(x, y, z)
  191.     return tostring(x) .. " " .. tostring(y) .. " " .. tostring(z)
  192. end
  193.  
  194. local function ez_gps_pos()
  195.     return fmt_gps_to_str(get_gps_pos())
  196. end
  197.  
  198. local function save_dragon(msg)
  199.     log_to_file(msg, DRAGONS_FILE)
  200. end
  201.  
  202. local function notify_houston()
  203.     local x, y, z = get_gps_pos()
  204.     local msg = "*** Dragon found around: " .. fmt_gps_to_str(x, y, z) .. " ***"
  205.     log_msg(msg)
  206.     save_dragon(msg)
  207. end
  208.  
  209. local function scan_for_dragons()
  210.     log_msg("Scanning for Dragons at " .. fmt_gps_to_str(get_gps_pos()))
  211.     local has_equiped = equip_peripheral(SENSOR)
  212.     local ss = peripheral.wrap(PERIPHERAL_SIDE)
  213.     local entities = ss.sense()
  214.     log_msg(tostring(#entities) .. " Entities found")
  215.     for i = 1, #entities, 1 do
  216.         if string.find(entities[i].name, "dragon") then
  217.             return true
  218.         end
  219.     end
  220.     return false
  221. end
  222.  
  223. local function move_foward()
  224.     turtle.dig()
  225.     turtle.forward()
  226.     turtle.digDown()
  227. end
  228.  
  229. local function dig_stride(stride)
  230.     stride = stride or STRIDE
  231.     --log_msg("Starting a dig stride..")
  232.     local has_equiped = equip_peripheral(PICKAXE)
  233.     for i = 1, stride, 1 do
  234.         move_foward()
  235.     end
  236. end
  237.  
  238. local function change_strip(direction, skip_strip)
  239.     local turn = direction == FW_DIRECTION and function() turtle.turnRight() end or function() turtle.turnLeft() end
  240.     local strides = skip_strip and (LATERAL_STRIDE * 2) or LATERAL_STRIDE
  241.     turn()
  242.     dig_stride(strides)
  243.     turn()
  244.     local dir_str = direction == FW_DIRECTION and "forward" or "back"
  245.     log_msg("Changed strip, going " .. dir_str)
  246.     local msg = "T:" .. fmt_gps_to_str(get_gps_pos())
  247.     log_msg(msg)
  248. end
  249.  
  250. local function run()
  251.     local direction = FW_DIRECTION
  252.     local strips_left = STRIPS % 2 == 0 and STRIPS or (STRIPS + 1)
  253.     local skip_strip = false
  254.     for i = 1, strips_left, 1 do
  255.         for j = 1, STRIDES_TO_DO, 1 do
  256.             check_and_refuel()
  257.             dig_stride()
  258.             if scan_for_dragons() then
  259.                 notify_houston()
  260.                 --log_msg("Stopping search... Beam me up, Scotty!")
  261.                 local strides_left = STRIDES_TO_DO - j
  262.                 if strides_left > 0 then
  263.                     for k = 1, strides_left, 1 do -- lazy workaround to skip the same dragon, probably..
  264.                         dig_stride()
  265.                     end
  266.                     j = j + strides_left
  267.                     skip_strip = true
  268.                 end
  269.             end
  270.             if turtle.select(TURTLE_INV_SZ) and turtle.getItemDetail() then
  271.                 empty_inv()
  272.             end
  273.         end
  274.  
  275.         change_strip(direction, skip_strip)
  276.         skip_strip = false
  277.         direction = -direction
  278.     end
  279.     local msg = "Finished search for dragons at " .. fmt_gps_to_str(get_gps_pos())
  280.     log_msg(msg)
  281. end
  282.  
  283. local function debug_wireless()
  284.     for i = 1, 30, 1 do
  285.         log_msg("Sending coords " .. ez_gps_pos())
  286.         sleep(3)
  287.     end
  288. end
  289.  
  290. run()
  291. --debug_wireless()
  292.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement