Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("db")
- local arguments = {...}
- local pos_x = 0 pos_y = 0 pos_z = 0
- local dig_x = 0 dig_y = 0 dig_z = 0
- local gps_x = nil gps_y = nil gps_z = nil
- local gps_used = 0
- local ang = 0
- local fuel_need = 0 fuel_level = 0
- local t_db = db.get()
- local log = nil
- local modem = nil
- local server_port = 0 client_port = 0
- function dialog()
- local input = read()
- if string.lower(input) == "y" then return {1,"y"} end
- return {0,"n"}
- end
- function setup_wirelles()
- info("Start wirelles", 3)
- local return_t = 0
- modem = peripheral.wrap("right")
- if not modem then
- info("Modem not found", 1)
- local result = {}
- info("Continue ? [y|n]", 1)
- result = dialog()
- if result[1] == 0 then
- return_t = 1
- info("Aborting", 1)
- else
- info("Continue", 1)
- end
- else
- info("Modem initialized - ["..tostring(client_port).."]", 2)
- modem.open(client_port)
- end
- info("Abort wirelles", 3)
- return return_t
- end
- function info(message, lvl)
- lvl = lvl or 0
- if lvl == 0 then term.blit("[ERR]:","0eee00","ffffff") end
- if lvl == 1 then term.blit("[WRN]:","044400","ffffff") end
- if lvl == 2 then term.blit("[LOG]:","055500","ffffff") end
- if lvl == 3 then term.blit("[PRC]:","0bbb00","ffffff") end
- print(message)
- log.writeLine((lvl == 0 and "[ERR]:" or lvl == 1 and "[WRN]:" or lvl == 2 and "[LOG]:" or "[PRC]:")..tostring(message))
- if modem then
- modem.transmit(server_port, client_port, (lvl == 0 and "[ERR]:" or lvl == 1 and "[WRN]:" or lvl == 2 and "[LOG]:" or "[PRC]:")..tostring(message))
- end
- os.sleep(0.1)
- end
- function find_item_by_name(name)
- for SLOT_ID = 1, 16 do
- local SLOT_DATA = turtle.getItemDetail(SLOT_ID)
- if SLOT_DATA and SLOT_DATA.name == name then return SLOT_ID end
- end
- end
- function initialization()
- log = fs.open("log", "w")
- info("Start initializing", 3)
- if setup() == 0 then
- if refuel() == 0 then
- if setup_gps() == 0 then
- if setup_angle() == 0 then
- end
- end
- end
- end
- setAng(0)
- info("Abort initializing", 3)
- log.close()
- end
- function refuel()
- local return_t = 0
- info("Start refueling", 3)
- info("Fuel level - "..tostring(100 / fuel_need * fuel_level).."%", 2)
- if fuel_level < fuel_need then
- info("Fuel level is low", 1)
- return_t = scanfuel()
- end
- info("Abort refueling", 3)
- return return_t
- end
- function scanfuel()
- info("Start scaning fuel", 3)
- local return_t = 0
- local finish = 0
- for SLOT_ID = 1, 16 do
- if finish == 1 then break end
- local SLOT_DATA = turtle.getItemDetail(SLOT_ID)
- if SLOT_DATA then
- for k, v in pairs(t_db.turtle.fuel) do
- if v.name == SLOT_DATA.name then
- info("Fuel item ".."["..tostring(SLOT_DATA.count).."] - "..SLOT_DATA.name, 2)
- local count_need = (math.ceil((fuel_need-fuel_level)/v.fuel))
- count_need = (count_need > SLOT_DATA.count and SLOT_DATA.count or (count_need < SLOT_DATA.count and 1 or count_need))
- turtle.select(SLOT_ID)
- turtle.refuel(count_need)
- fuel_level = turtle.getFuelLevel()
- if fuel_level >= fuel_need then
- finish = 1
- break
- end
- end
- end
- end
- end
- if fuel_level == 0 then
- info("Fuel level 0% aborting", 0)
- return_t = 1
- elseif fuel_level < fuel_need then
- info("Can't full refueling", 1)
- local result = {}
- info("Warning you can lose turtle!", 1)
- info("Continue ? [y|n]", 1)
- result = dialog()
- if result[1] == 0 then
- return_t = 1
- info("Aborting", 1)
- else
- info("Continue", 1)
- end
- end
- info("Abort scaning fuel", 3)
- return return_t
- end
- function setup()
- info("Start setup", 3)
- local return_t = 0
- if #arguments == 3 then
- dig_x = arguments[1]
- dig_y = arguments[2]
- dig_z = arguments[3]
- fuel_need = dig_x*dig_y*dig_z
- fuel_level = turtle.getFuelLevel()
- info("Size - "..tostring(dig_x)..","..tostring(dig_y)..","..tostring(dig_z), 2)
- info("Bloks to be mined - "..tostring(fuel_need), 2)
- else
- info("Arguments input need MAX_X MAX_Y MAX_Z", 0)
- return_t = 1
- end
- info("Abort setup", 3)
- return return_t
- end
- function inspect_pour(lvl)
- lvl = lvl or 0
- info("Inspect pour deep:"..tostring(lvl), 2)
- if turtle.detect() then
- local i_f = turtle.inspect()
- if i_f then
- if t_db.turtle.pourable[1] == i_f.name or t_db.turtle.pourable[2] == i_f.name then
- turtle.dig()
- info("Pour delete on ["..tostring(pos_x).."]["..tostring(pos_y).."]["..tostring(pos_z).."]", 2)
- os.sleep(0.5)
- inspect_pour(lvl+1)
- end
- end
- end
- end
- function setup_gps()
- info("Start setup GPS", 3)
- local return_t = 0
- gps_x, gps_y, gps_z = gps.locate(5)
- if not gps_x then
- local result = {}
- info("GPS Server not found", 1)
- info("Continue ? [y|n]", 1)
- result = dialog()
- if result[1] == 0 then
- return_t = 1
- info("Aborting", 1)
- else
- gps_x, gps_y, gps_z = 0
- gps_used = 1
- info("Continue with local coordinate system", 1)
- end
- else
- info("GPS Server found", 2)
- info("Position ["..tostring(gps_x).."]["..tostring(gps_y).."]["..tostring(gps_z).."]", 2)
- end
- info("Abort setup GPS", 3)
- return return_t
- end
- function gps_get_pos()
- info("Geting position from GPS", 2)
- local return_t = 0
- local x, y, z = gps.locate(5)
- if not x then
- return_t = 1
- x, y, z = 0, 0, 0
- info("Lose GPS signal", 0)
- end
- return return_t, x, y, z
- end
- function setup_angle()
- info("Start setup angle", 3)
- inspect_pour(0)
- turtle.forward()
- local return_t, x, y, z = gps_get_pos()
- if x > gps_x then ang = 0
- elseif x < gps_x then ang = 180
- elseif y > gps_y then ang = 90
- elseif y < gps_y then ang = -90 end
- info("Angle - "..tostring(ang), 2)
- info("Position ["..tostring(x).."]["..tostring(y).."]["..tostring(z).."]", 2)
- turtle.back()
- info("End setup angle", 3)
- return return_t
- end
- function setAng(s_ang)
- if ang == s_ang then return end
- if ang < s_ang then
- ang = (ang+90)
- turtle.turnRight()
- elseif ang > s_ang then
- ang = (ang-90)
- turtle.turnLeft()
- end
- setAng(s_ang)
- info("Set ang - ["..tostring(ang).."]", 2)
- end
- function rotate_to(r_x, r_y)
- if gps_used == 1 then
- else
- end
- end
- function dig_process()
- info("Start dig", 3)
- local finish = 0
- while not finish do
- end
- info("Abort dig", 3)
- end
- initialization()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement