Guest User

startup

a guest
Jan 18th, 2014
2,896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.53 KB | None | 0 0
  1. --computercraft
  2. --laser miner controller with monitor
  3. --all mining lasers are dealt with as peripherals
  4.  
  5. --text scale
  6. --changing this will cause touch interface to fail for some reason --TODO
  7. MON_SCALE = 1.0
  8.  
  9. --min offset
  10. MIN_OFFSET = 2
  11. --max offset
  12. MAX_OFFSET = 250
  13.  
  14. function DetectPeripheral(name)  
  15.     local sides = {"top" , "bottom" , "front" , "left" , "right" , "back"} 
  16.     for i = 1, 6 do
  17.         if peripheral.isPresent(sides[i]) and peripheral.getType(sides[i]) == name then
  18.             print("found "..tostring(name)..": "..tostring(sides[i]))
  19.             return sides[i]
  20.         end
  21.     end
  22.     return nil
  23. end
  24.  
  25. screen = peripheral.wrap(DetectPeripheral("monitor"))
  26. screen_width, screen_height = screen.getSize()
  27. lazors = {}
  28. states = {}
  29. step = 12
  30. xOffset = 2
  31. yOffset = 4
  32. offset = MIN_OFFSET
  33. start_all_position = {x = 1, y = screen_height}
  34. stop_all_position  = {x = screen_width - 7, y = screen_height}
  35. increment_position = {x = math.floor(screen_width/2 + 4), y =  screen_height}
  36. decrement_position = {x = math.floor(screen_width/2 - 4), y = screen_height}
  37. screen.setTextScale(MON_SCALE)
  38. -- math floor, KOSTYL against lua coordinats rounding system
  39.  
  40. function resetColorsScreen()
  41.     screen.setBackgroundColor(colors.black)
  42.     screen.setTextColor(colors.white)
  43. end
  44.  
  45.  
  46. function increment_offset()
  47.     if offset <= MAX_OFFSET then
  48.         offset = offset + 1
  49.     end
  50. end
  51.  
  52.  
  53. function decrement_offset()
  54.     if offset > MIN_OFFSET then
  55.         offset = offset - 1
  56.     end
  57. end
  58.  
  59.  
  60. function start(laserNUM)
  61.     print("Calling laser "..lazors[laserNUM].." for start")
  62.     setOffset(laserNUM, offset)
  63.     m.callRemote(lazors[laserNUM], "mine")
  64.        
  65. end
  66.  
  67.  
  68. function startAll()
  69.     for i =1 ,#lazors do
  70.         start(i)
  71.     end
  72. end
  73.  
  74.  
  75. function stop(laserNUM)
  76.     print("Calling laser "..lazors[laserNUM].." for stop")
  77.     m.callRemote(lazors[laserNUM], "stop")
  78. end
  79.  
  80.  
  81. function stopAll()
  82.     for i = 1 ,#lazors do
  83.         stop(i)
  84.     end
  85. end
  86.  
  87. function setOffset(laserNUM, offset)
  88.     m.callRemote(lazors[laserNUM], "offset", offset)
  89. end
  90.        
  91.      
  92. function drawstatus()
  93.     screen.clear()
  94.     ----drawing static elements
  95.     --drawing list of catgories
  96.     local categories = {"State", "Control",  "Energy", "Layer", "Val/Min"}
  97.     for i=1,5 do
  98.         screen.setCursorPos(-10 + step*i, 2)
  99.         screen.write(categories[i])
  100.     end
  101.     --drawing static buttons
  102.     screen.setBackgroundColor(colors.cyan)
  103.     screen.setCursorPos(start_all_position["x"] ,start_all_position["y"])
  104.     screen.write("Start All")
  105.     screen.setCursorPos(stop_all_position["x"] , stop_all_position["y"])
  106.     screen.write("Stop All")
  107.     resetColorsScreen()
  108.     screen.setBackgroundColor(colors.green)      
  109.     screen.setCursorPos(increment_position["x"], increment_position["y"])
  110.     screen.write("+")
  111.     screen.setCursorPos(math.floor(screen_width/2) - 1, screen_height)
  112.     resetColorsScreen()
  113.     screen.write(" "..offset.." ")
  114.     screen.setBackgroundColor(colors.red)
  115.     screen.setCursorPos(decrement_position.x, decrement_position.y)
  116.     screen.write("-")
  117.     resetColorsScreen()
  118.     ----drawing dynamic elements
  119.     --iterating throught miner array
  120.     for a = 1,#lazors do                   
  121.         state, energy, height, luxuries, mined = m.callRemote(lazors[a], "state")
  122. --      mState, mEnergy, mLayer, mValuablesMined, mValuablesInLayer = m.callRemote(lasersT[i], "state")
  123.         if state == "not mining" then
  124.             states[a] = "Start"
  125.         else
  126.             states[a] = "Stop"
  127.         end
  128.  
  129.         local temp = {state, states[a], energy, height, tostring(luxuries.."/"..mined)}
  130.         --printing for current miner on monitor
  131.         for b = 1, 5 do
  132.             screen.setCursorPos(-10 + step * b, a + yOffset)
  133.             --color for on/off status
  134.             if b == 1 then
  135.                 if temp[b] == "not mining" then
  136.                     screen.setBackgroundColor(colors.red)
  137.                     screen.write(temp[b])
  138.                     resetColorsScreen()
  139.                 else
  140.                     screen.setBackgroundColor(colors.green)
  141.                     screen.write(temp[b])
  142.                     resetColorsScreen()
  143.                 end
  144.             elseif b == 2 then
  145.                 screen.setBackgroundColor(colors.cyan)
  146.                 screen.write(temp[b])
  147.                 resetColorsScreen()
  148.             else
  149.                 screen.write(temp[b])
  150.             end
  151.         end
  152.     end
  153.     sleep(1)
  154.     return 0       
  155. end
  156.  
  157.     --controll from player
  158. function touch_control()
  159.     --print("Enter Touch service")
  160.     local laserIndex = nil
  161.     local xMax = xOffset + step * 2
  162.     local yMax = yOffset + table.maxn(lazors)
  163.     --pull touch event
  164.     local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  165.         --miners controll
  166.     print("Pulled touch event on X: ".. xPos.." Y: "..yPos)
  167.     print(decrement_position.x.." "..decrement_position.y)
  168.     if xPos  >= xOffset and xPos <= xMax and yPos >= yOffset and yPos <= yMax then
  169.         print("Button hitted, executing code")
  170.         laserIndex = yPos - yOffset
  171.         if states[laserIndex] == "Start" then
  172.             print("Starting the miner with index"..laserIndex)
  173.             start(laserIndex)
  174.         else
  175.             print("Stopping the miner with index"..laserIndex)
  176.             stop(laserIndex)
  177.         end
  178.     elseif xPos <= 10 and yPos == screen_height then
  179.         startAll()
  180.     elseif xPos >= stop_all_position.x and yPos == stop_all_position.y then
  181.         stopAll()
  182.     elseif xPos == increment_position.x and yPos == increment_position.y then
  183.         increment_offset()
  184.     elseif xPos == decrement_position.x and yPos == decrement_position.y then
  185.         decrement_offset()
  186.  
  187.         print("Function ended, exiting")
  188.         return 0
  189.     end
  190. end
  191.  
  192.  
  193.  
  194. --execution start
  195. --KOSTYL for not detectiong any lasers after PRYGEE jump
  196. sleep(1)
  197. m = peripheral.wrap(DetectPeripheral("modem"))
  198. clients = m.getNamesRemote()
  199. --filter lasers from other peripherals
  200. for i = 1, table.maxn(clients) do
  201.     if m.getTypeRemote(clients[i]) then
  202.         table.insert(lazors, clients[i])
  203.     end
  204. end
  205.  
  206. --mainloop
  207. while true do
  208. parallel.waitForAny(drawstatus, touch_control)
  209. end
Advertisement
Add Comment
Please, Sign In to add comment