Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ......
- local args = { ... }
- local DEBUG = false
- local ITEM_NOT_FOUND = -1
- local PERIPHERAL_SIDE = "left"
- local PROTOCOL_STR = "gpsl"
- local LOG_FILE = "lastrun.txt"
- local DRAGONS_FILE = "dragons_found.txt"
- local TURTLE_ID = 16
- local RECV_ID = 7
- local STRIDE = 32
- local STRIDES_TO_DO = 32
- local LATERAL_STRIDE = 32 -- default sensor range = 16
- local STRIPS = 32
- local FW_DIRECTION = 1 -- 1 foward, -1 back
- local BK_DIRECTION = -1
- local COAL_FUEL_V = 80
- local REFUEL_THRESHOLD = 1000
- local TURTLE_INV_SZ = 16
- --local WIRELESS_MODEM = "computercraft:peripheral" range limitations
- local WIRELESS_MODEM = "computercraft:advanced_modem"
- local PICKAXE = "minecraft:diamond_pickaxe"
- local SENSOR = "plethora:module"
- local CHUNK_AWAKER = "walkingchunks:chunk_awaker"
- local ENDER_CHEST = "enderstorage:ender_storage"
- local COAL = "minecraft:coal"
- -- peripheral type string returned by getType
- local peripheral_type = {}
- peripheral_type[WIRELESS_MODEM] = "modem"
- peripheral_type[SENSOR] = "plethora:sensor"
- peripheral_type[PICKAXE] = "pickaxe" -- not really it's type name, special case
- local to_skip_list = { WIRELESS_MODEM, PICKAXE, SENSOR, ENDER_CHEST }
- -- foward declare
- local msg_houston
- local fmt_gps_to_str
- local get_gps_pos
- local function log_to_file(msg, log_file)
- local handle = fs.open(log_file, "a")
- if handle then
- handle.write(msg .. '\n')
- handle.close()
- end
- end
- local function log_msg(msg)
- if (DEBUG) then
- print("logging..\n " .. msg)
- end
- log_to_file(msg, LOG_FILE)
- local tmp = log_msg
- log_msg = function(msg_p)
- end --avoid recursion
- msg_houston(msg)
- log_msg = tmp
- end
- local function skip_item(item)
- for i = 1, #to_skip_list, 1 do
- if item.name == to_skip_list[i] then
- return true
- end
- end
- return false
- end
- local function find_item_slot(item_name)
- --log_msg("Finding item slot for " .. item_name)
- for i = 1, TURTLE_INV_SZ, 1 do
- turtle.select(i)
- local item = turtle.getItemDetail()
- if item and item.name == item_name then
- return i
- end
- end
- return ITEM_NOT_FOUND
- end
- local function empty_inv()
- log_msg("Empting inventory...")
- for i = 1, TURTLE_INV_SZ, 1 do
- turtle.select(i)
- local item = turtle.getItemDetail()
- if item and (not skip_item(item)) then
- turtle.drop()
- end
- end
- end
- local function find_free_slot()
- for i = 1, TURTLE_INV_SZ, 1 do
- turtle.select(i)
- local item = turtle.getItemDetail()
- if not item then
- return i
- end
- end
- for i = 1, TURTLE_INV_SZ, 1 do
- turtle.select(i)
- local item = turtle.getItemDetail()
- if not skip_item(item) then
- turtle.drop()
- return i
- end
- end
- return -1
- end
- local function equip_peripheral(p_name)
- --local log = log_func or log_msg -- special case to avoid recursion
- --log_msg("Equiping " .. peripheral_type[p_name])
- if peripheral.getType(PERIPHERAL_SIDE) == peripheral_type[p_name] and p_name ~= PICKAXE then
- --log_msg(peripheral_type[p_name] .. " already equiped, skipping search...")
- return true
- end
- local item_slot = find_item_slot(p_name)
- if item_slot == ITEM_NOT_FOUND then
- log_msg("Could not find item " .. p_name)
- return false
- end
- --log_msg("Found " .. peripheral_type[p_name] .. "in slot " .. tostring(item_slot))
- turtle.select(item_slot)
- turtle.equipLeft()
- return true
- end
- local function refuel()
- log_msg("Refueling at pos " .. fmt_gps_to_str(get_gps_pos()))
- local has_equiped = equip_peripheral(PICKAXE)
- local fuel_chest_slot = find_item_slot(ENDER_CHEST)
- if fuel_chest_slot == ITEM_NOT_FOUND then
- return false
- end
- if turtle.detectDown() then
- turtle.digDown()
- end
- turtle.select(fuel_chest_slot)
- turtle.placeDown()
- local free_slot = find_free_slot()
- turtle.select(free_slot)
- turtle.suckDown()
- turtle.refuel()
- turtle.digDown()
- -- for i = 1, chest.size(), 1 do
- -- local item = chest.getItemMeta(i)
- -- ifitem.name == coal then
- -- free_slot = find_free_slot()
- -- chest.
- -- end
- -- end
- end
- local function check_and_refuel()
- local fuel_level = turtle.getFuelLevel()
- log_msg("Current fuel level " .. tostring(turtle.getFuelLevel()))
- if fuel_level < REFUEL_THRESHOLD then
- refuel()
- end
- end
- msg_houston = function(msg)
- local has_equiped = equip_peripheral(WIRELESS_MODEM)
- rednet.open(PERIPHERAL_SIDE)
- rednet.send(RECV_ID, msg, PROTOCOL_STR)
- rednet.close()
- end
- get_gps_pos = function()
- local has_equiped = equip_peripheral(WIRELESS_MODEM)
- return gps.locate(5)
- end
- fmt_gps_to_str = function(x, y, z)
- return tostring(x) .. " " .. tostring(y) .. " " .. tostring(z)
- end
- local function ez_gps_pos()
- return fmt_gps_to_str(get_gps_pos())
- end
- local function save_dragon(msg)
- log_to_file(msg, DRAGONS_FILE)
- end
- local function notify_houston()
- local x, y, z = get_gps_pos()
- local msg = "*** Dragon found around: " .. fmt_gps_to_str(x, y, z) .. " ***"
- log_msg(msg)
- save_dragon(msg)
- end
- local function scan_for_dragons()
- log_msg("Scanning for Dragons at " .. fmt_gps_to_str(get_gps_pos()))
- local has_equiped = equip_peripheral(SENSOR)
- local ss = peripheral.wrap(PERIPHERAL_SIDE)
- local entities = ss.sense()
- log_msg(tostring(#entities) .. " Entities found")
- for i = 1, #entities, 1 do
- if string.find(entities[i].name, "dragon") then
- return true
- end
- end
- return false
- end
- local function move_foward()
- turtle.dig()
- turtle.forward()
- turtle.digDown()
- end
- local function dig_stride(stride)
- stride = stride or STRIDE
- --log_msg("Starting a dig stride..")
- local has_equiped = equip_peripheral(PICKAXE)
- for i = 1, stride, 1 do
- move_foward()
- end
- end
- local function change_strip(direction, skip_strip)
- local turn = direction == FW_DIRECTION and function() turtle.turnRight() end or function() turtle.turnLeft() end
- local strides = skip_strip and (LATERAL_STRIDE * 2) or LATERAL_STRIDE
- turn()
- dig_stride(strides)
- turn()
- local dir_str = direction == FW_DIRECTION and "forward" or "back"
- log_msg("Changed strip, going " .. dir_str)
- local msg = "T:" .. fmt_gps_to_str(get_gps_pos())
- log_msg(msg)
- end
- local function run()
- local direction = FW_DIRECTION
- local strips_left = STRIPS % 2 == 0 and STRIPS or (STRIPS + 1)
- local skip_strip = false
- for i = 1, strips_left, 1 do
- for j = 1, STRIDES_TO_DO, 1 do
- check_and_refuel()
- dig_stride()
- if scan_for_dragons() then
- notify_houston()
- --log_msg("Stopping search... Beam me up, Scotty!")
- local strides_left = STRIDES_TO_DO - j
- if strides_left > 0 then
- for k = 1, strides_left, 1 do -- lazy workaround to skip the same dragon, probably..
- dig_stride()
- end
- j = j + strides_left
- skip_strip = true
- end
- end
- if turtle.select(TURTLE_INV_SZ) and turtle.getItemDetail() then
- empty_inv()
- end
- end
- change_strip(direction, skip_strip)
- skip_strip = false
- direction = -direction
- end
- local msg = "Finished search for dragons at " .. fmt_gps_to_str(get_gps_pos())
- log_msg(msg)
- end
- local function debug_wireless()
- for i = 1, 30, 1 do
- log_msg("Sending coords " .. ez_gps_pos())
- sleep(3)
- end
- end
- run()
- --debug_wireless()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement