Advertisement
AI_UBI

Uncomplete turrtle for ComputerCraft

Feb 15th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.57 KB | None | 0 0
  1. os.loadAPI("db")
  2.  
  3. local arguments = {...}
  4. local pos_x = 0 pos_y = 0 pos_z = 0
  5. local dig_x = 0 dig_y = 0 dig_z = 0
  6. local gps_x = nil gps_y = nil gps_z = nil
  7. local gps_used = 0
  8. local ang = 0
  9. local fuel_need = 0 fuel_level = 0
  10. local t_db = db.get()
  11. local log = nil
  12.  
  13. local modem = nil
  14. local server_port = 0 client_port = 0
  15.  
  16. function dialog()
  17.    
  18.     local input = read()
  19.  
  20.     if string.lower(input) == "y" then return {1,"y"} end
  21.  
  22.     return {0,"n"}
  23.  
  24. end
  25.  
  26. function setup_wirelles()
  27.  
  28.     info("Start wirelles", 3)
  29.  
  30.     local return_t = 0
  31.  
  32.     modem = peripheral.wrap("right")
  33.  
  34.     if not modem then
  35.  
  36.         info("Modem not found", 1)
  37.        
  38.         local result = {}
  39.  
  40.         info("Continue ? [y|n]", 1)
  41.  
  42.         result = dialog()
  43.  
  44.         if result[1] == 0 then
  45.  
  46.             return_t = 1
  47.  
  48.             info("Aborting", 1)
  49.  
  50.         else
  51.  
  52.             info("Continue", 1)
  53.  
  54.         end
  55.  
  56.     else
  57.  
  58.         info("Modem initialized - ["..tostring(client_port).."]", 2)
  59.  
  60.         modem.open(client_port)
  61.  
  62.     end
  63.  
  64.     info("Abort wirelles", 3)
  65.  
  66.     return return_t
  67.  
  68. end
  69.  
  70. function info(message, lvl)
  71.  
  72.     lvl = lvl or 0
  73.  
  74.     if lvl == 0 then term.blit("[ERR]:","0eee00","ffffff") end
  75.     if lvl == 1 then term.blit("[WRN]:","044400","ffffff") end
  76.     if lvl == 2 then term.blit("[LOG]:","055500","ffffff") end
  77.     if lvl == 3 then term.blit("[PRC]:","0bbb00","ffffff") end
  78.  
  79.     print(message)
  80.  
  81.     log.writeLine((lvl == 0 and "[ERR]:" or lvl == 1 and "[WRN]:" or lvl == 2 and "[LOG]:" or "[PRC]:")..tostring(message))
  82.  
  83.     if modem then
  84.  
  85.         modem.transmit(server_port, client_port, (lvl == 0 and "[ERR]:" or lvl == 1 and "[WRN]:" or lvl == 2 and "[LOG]:" or "[PRC]:")..tostring(message))
  86.  
  87.     end
  88.  
  89.     os.sleep(0.1)
  90.  
  91. end
  92.  
  93. function find_item_by_name(name)
  94.    
  95.     for SLOT_ID = 1, 16 do
  96.  
  97.         local SLOT_DATA = turtle.getItemDetail(SLOT_ID)
  98.         if SLOT_DATA and SLOT_DATA.name == name then return SLOT_ID end
  99.  
  100.     end
  101.  
  102. end
  103.  
  104. function initialization()
  105.    
  106.     log = fs.open("log", "w")
  107.  
  108.     info("Start initializing", 3)
  109.  
  110.     if setup() == 0 then
  111.  
  112.         if refuel() == 0 then
  113.            
  114.             if setup_gps() == 0 then
  115.  
  116.                 if setup_angle() == 0 then
  117.  
  118.                 end
  119.  
  120.             end
  121.  
  122.         end
  123.    
  124.     end
  125.    
  126.     setAng(0)
  127.  
  128.     info("Abort initializing", 3)
  129.  
  130.     log.close()
  131.  
  132. end
  133.  
  134. function refuel()
  135.  
  136.     local return_t = 0
  137.  
  138.     info("Start refueling", 3)
  139.  
  140.     info("Fuel level - "..tostring(100 / fuel_need * fuel_level).."%", 2)
  141.  
  142.     if fuel_level < fuel_need then
  143.        
  144.         info("Fuel level is low", 1)
  145.  
  146.         return_t = scanfuel()
  147.  
  148.     end
  149.  
  150.     info("Abort refueling", 3)
  151.  
  152.     return return_t
  153.  
  154. end
  155.  
  156. function scanfuel()
  157.    
  158.     info("Start scaning fuel", 3)
  159.  
  160.     local return_t = 0
  161.     local finish = 0
  162.  
  163.     for SLOT_ID = 1, 16 do
  164.  
  165.         if finish == 1 then break end
  166.  
  167.         local SLOT_DATA = turtle.getItemDetail(SLOT_ID)
  168.  
  169.         if SLOT_DATA then
  170.            
  171.             for k, v in pairs(t_db.turtle.fuel) do
  172.                
  173.                 if v.name == SLOT_DATA.name then
  174.  
  175.                     info("Fuel item ".."["..tostring(SLOT_DATA.count).."] - "..SLOT_DATA.name, 2)
  176.  
  177.                     local count_need = (math.ceil((fuel_need-fuel_level)/v.fuel))
  178.  
  179.                     count_need = (count_need > SLOT_DATA.count and SLOT_DATA.count or (count_need < SLOT_DATA.count and 1 or count_need))
  180.  
  181.                     turtle.select(SLOT_ID)
  182.                     turtle.refuel(count_need)
  183.  
  184.                     fuel_level = turtle.getFuelLevel()
  185.  
  186.                     if fuel_level >= fuel_need then
  187.  
  188.                         finish = 1
  189.  
  190.                         break
  191.  
  192.                     end
  193.  
  194.                 end
  195.  
  196.             end
  197.  
  198.         end
  199.  
  200.     end
  201.  
  202.     if fuel_level == 0 then
  203.  
  204.         info("Fuel level 0% aborting", 0)
  205.  
  206.         return_t = 1
  207.  
  208.     elseif fuel_level < fuel_need then
  209.  
  210.         info("Can't full refueling", 1)
  211.        
  212.         local result = {}
  213.  
  214.         info("Warning you can lose turtle!", 1)
  215.         info("Continue ? [y|n]", 1)
  216.  
  217.         result = dialog()
  218.  
  219.         if result[1] == 0 then
  220.  
  221.             return_t = 1
  222.  
  223.             info("Aborting", 1)
  224.  
  225.         else
  226.  
  227.             info("Continue", 1)
  228.  
  229.         end
  230.    
  231.     end
  232.  
  233.     info("Abort scaning fuel", 3)
  234.  
  235.     return return_t
  236.  
  237. end
  238.  
  239. function setup()
  240.  
  241.     info("Start setup", 3)
  242.  
  243.     local return_t = 0
  244.  
  245.     if #arguments == 3 then
  246.  
  247.         dig_x = arguments[1]
  248.         dig_y = arguments[2]
  249.         dig_z = arguments[3]
  250.         fuel_need = dig_x*dig_y*dig_z
  251.         fuel_level = turtle.getFuelLevel()
  252.  
  253.         info("Size - "..tostring(dig_x)..","..tostring(dig_y)..","..tostring(dig_z), 2)
  254.         info("Bloks to be mined - "..tostring(fuel_need), 2)
  255.  
  256.     else
  257.  
  258.         info("Arguments input need MAX_X MAX_Y MAX_Z", 0)
  259.  
  260.         return_t = 1
  261.  
  262.     end
  263.  
  264.     info("Abort setup", 3)
  265.  
  266.     return return_t
  267.  
  268. end
  269.  
  270. function inspect_pour(lvl)
  271.  
  272.     lvl = lvl or 0
  273.  
  274.     info("Inspect pour deep:"..tostring(lvl), 2)
  275.  
  276.     if turtle.detect() then
  277.  
  278.         local i_f = turtle.inspect()
  279.  
  280.         if i_f then
  281.  
  282.             if t_db.turtle.pourable[1] == i_f.name or t_db.turtle.pourable[2] == i_f.name then
  283.  
  284.                 turtle.dig()
  285.  
  286.                 info("Pour delete on ["..tostring(pos_x).."]["..tostring(pos_y).."]["..tostring(pos_z).."]", 2)
  287.  
  288.                 os.sleep(0.5)
  289.  
  290.                 inspect_pour(lvl+1)
  291.  
  292.             end
  293.  
  294.         end
  295.  
  296.     end
  297.  
  298. end
  299.  
  300. function setup_gps()
  301.  
  302.     info("Start setup GPS", 3)
  303.  
  304.     local return_t = 0
  305.     gps_x, gps_y, gps_z = gps.locate(5)
  306.  
  307.     if not gps_x then
  308.  
  309.         local result = {}
  310.  
  311.         info("GPS Server not found", 1)
  312.         info("Continue ? [y|n]", 1)
  313.  
  314.         result = dialog()
  315.  
  316.         if result[1] == 0 then
  317.  
  318.             return_t = 1
  319.  
  320.             info("Aborting", 1)
  321.  
  322.         else
  323.  
  324.             gps_x, gps_y, gps_z = 0
  325.             gps_used = 1
  326.  
  327.             info("Continue with local coordinate system", 1)
  328.  
  329.         end
  330.  
  331.     else
  332.  
  333.         info("GPS Server found", 2)
  334.         info("Position ["..tostring(gps_x).."]["..tostring(gps_y).."]["..tostring(gps_z).."]", 2)
  335.  
  336.     end
  337.  
  338.     info("Abort setup GPS", 3)
  339.  
  340.     return return_t
  341.  
  342. end
  343.  
  344. function gps_get_pos()
  345.  
  346.     info("Geting position from GPS", 2)
  347.  
  348.     local return_t = 0
  349.  
  350.     local x, y, z = gps.locate(5)
  351.  
  352.     if not x then
  353.    
  354.         return_t = 1
  355.         x, y, z = 0, 0, 0
  356.  
  357.         info("Lose GPS signal", 0)
  358.  
  359.     end
  360.  
  361.     return return_t, x, y, z
  362.  
  363. end
  364.  
  365. function setup_angle()
  366.    
  367.     info("Start setup angle", 3)
  368.  
  369.     inspect_pour(0)
  370.  
  371.     turtle.forward()
  372.  
  373.     local return_t, x, y, z = gps_get_pos()
  374.  
  375.     if x > gps_x then ang = 0
  376.     elseif x < gps_x then ang = 180
  377.     elseif y > gps_y then ang = 90
  378.     elseif y < gps_y then ang = -90 end
  379.  
  380.     info("Angle - "..tostring(ang), 2)
  381.     info("Position ["..tostring(x).."]["..tostring(y).."]["..tostring(z).."]", 2)
  382.  
  383.     turtle.back()
  384.  
  385.     info("End setup angle", 3)
  386.  
  387.     return return_t
  388.  
  389. end
  390.  
  391. function setAng(s_ang)
  392.  
  393.     if ang == s_ang then return end
  394.  
  395.     if ang < s_ang then
  396.  
  397.         ang = (ang+90)
  398.  
  399.         turtle.turnRight()
  400.  
  401.     elseif ang > s_ang then
  402.  
  403.         ang = (ang-90)
  404.  
  405.         turtle.turnLeft()
  406.  
  407.     end
  408.  
  409.     setAng(s_ang)
  410.  
  411.     info("Set ang - ["..tostring(ang).."]", 2)
  412.  
  413. end
  414.  
  415. function rotate_to(r_x, r_y)
  416.  
  417.     if gps_used == 1 then
  418.  
  419.        
  420.  
  421.     else
  422.  
  423.        
  424.  
  425.     end
  426.  
  427. end
  428.  
  429. function dig_process()
  430.    
  431.     info("Start dig", 3)
  432.    
  433.     local finish = 0
  434.  
  435.     while not finish do
  436.  
  437.        
  438.  
  439.     end
  440.  
  441.     info("Abort dig", 3)
  442.  
  443. end
  444.  
  445. initialization()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement