Advertisement
FluttyProger

robo.lua

Jan 15th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.86 KB | None | 0 0
  1. local com = require("component")
  2. local th = require("threads")
  3. local e = require("event")
  4. local nav = require("nav")
  5. local sir = require("serialization")
  6. local robot = require("robot")
  7.  
  8. local colors = com.colors
  9. local modem = com.modem
  10. local args = {...}
  11.  
  12. if #args<1 then
  13.   error("Номер робота напиши")
  14. elseif #args<2 then
  15.   error("ещё цвет")
  16. end
  17.  
  18. th.init()
  19.  
  20. local m, cordss, times, start, ma, chup, tasks, numar, varo, chm, slot
  21.  
  22. modem.open(12345)
  23.  
  24. local color={["red"]={0xff2020,1,2},["blue"]={0x20afff,2,1},["green"]={0xafff20,1,2},["yellow"]={0xffff20,2,1}}
  25.  
  26. local TEAM_COLOR = color[args[2]][1]
  27.  
  28. colors.setColor(TEAM_COLOR)
  29. com.robot.setLightColor(TEAM_COLOR)
  30.  
  31. function ini()
  32.   times, start= 300, false
  33.   tasks={}
  34.   cordss={}
  35.   local ifarr={{7,8,0,0},{7,1,0,9},{19,8,10,0},{18,0,9,10}}
  36.   local raspo={{9,8},{8,9},{9,8},{8,9}}
  37.   local eari={0,2,0,2}
  38.  
  39.   m=nav:new()
  40.   numar=tonumber(args[1])
  41.   slot=math.random(1,13)
  42.   robot.select(slot)
  43.   local kx=ifarr[numar]
  44.   local dx, dy, axz
  45.  
  46.   for x=0, raspo[numar][color[args[2]][2]] do
  47.     for z=0, raspo[numar][color[args[2]][3]] do
  48.       if args[2] == "red" then
  49.         dx=((-948)-x-kx[3])
  50.         dz=((639-z)-kx[4])
  51.         axz={x+kx[1],z-kx[2],0,0}
  52.       elseif args[2] == "yellow" then
  53.         dx=((639-z)-kx[3])
  54.         dz=((-966)+x+kx[4])
  55.         axz={z+kx[1],x-kx[2],0,0}
  56.       elseif args[2] == "blue" then
  57.         dx=((-966)+x+kx[3])
  58.         dz=((621+z)+kx[4])
  59.         axz={x+kx[1],z-kx[2],0,0}
  60.       elseif args[2] == "green" then
  61.         dx=((621+z)+kx[3])
  62.         dz=((-948)-x-kx[4])
  63.         axz={z+kx[1],x-kx[2],0,0}
  64.       else
  65.         error("Правильный введи")
  66.       end
  67.       cordss[tostring(dx)..tostring(dz)]=axz
  68.     end
  69.   end
  70. end
  71.  
  72. function CollectCoin(cordo)
  73.   if cordss[cordo] ~= nil then
  74.     print("Go to: ", cordss[cordo][1], cordss[cordo][2])
  75.     m:go(cordss[cordo])
  76.     robot.suckDown(64)
  77.   end
  78. end
  79.  
  80. function main()
  81.   while true do
  82.     for k,v in pairs(tasks) do
  83.       CollectCoin(v)
  84.       os.sleep(0.3)
  85.     end
  86.     os.sleep(0)
  87.   end
  88. end
  89.  
  90. function rmformtbl(varr)
  91.   for k=#tasks,1,-1 do
  92.     if tasks[k] == varr then
  93.       table.remove(tasks, k)
  94.     end
  95.   end
  96. end
  97.  
  98. function checkup()
  99.   while true do
  100.     local nd=m:getPos()
  101.     if nd.z >= 5 then
  102.       m:stopgo()
  103.     end
  104.     os.sleep(0)
  105.   end
  106. end
  107.  
  108. function checkm()
  109.   while true do
  110.     if sir.serialize(m:gettarg()) == sir.serialize(cordss[varo]) and start==true then
  111.       m:stopgo()
  112.     end
  113.     os.sleep(0)
  114.   end
  115. end
  116.  
  117. function clearmap()
  118.   for k, v in pairs(cordss) do
  119.     m:putMap(v,{})
  120.   end
  121.   print("ok")
  122. end
  123.  
  124. local timero=e.timer(30,clearmap,math.huge)
  125.  
  126. function eve()
  127.   while true do
  128.     local recv={e.pull("modem_message")}
  129.    
  130.     if recv[7] and recv[9] then
  131.       if args[2] == "red" then
  132.         var = tostring(recv[7]+1)..tostring(recv[9])
  133.       elseif args[2] == "yellow" then
  134.         var = tostring(recv[9])..tostring(recv[7]+1)
  135.       elseif args[2] == "blue" then
  136.         var = tostring(recv[7]+1)..tostring(recv[9])
  137.       elseif args[2] == "green" then
  138.         var = tostring(recv[9])..tostring(recv[7]+1)
  139.       else
  140.         error("Правильный введи, идиот")
  141.       end
  142.     end
  143.     local var2 = recv[6]
  144.  
  145.     if var2 == "gamestart" then
  146.       local aone={{11,-1,0},{11,1,0},{22,0,0},{22,0,0}}
  147.       m:go(aone[numar])
  148.       start = true
  149.     end
  150.  
  151.     if start then
  152.       if var2 == "setcoin" then
  153.         table.insert(tasks, var)
  154.       elseif var2 == "unsetcoin" then
  155.         varo = var
  156.         rmformtbl(var)
  157.       elseif var2 == "time" then
  158.         times=recv[7]
  159.       elseif var2 == "gamestop" then
  160.         start = false
  161.         th.kill(ma)
  162.         while true do
  163.           m:go({0,0,0,0})
  164.           os.sleep(0.1)
  165.           local nda=m:getPos()
  166.           print(sir.serialize(nda))
  167.           if nda.x == 0 and nda.y == 0 and nda.z == 0 and nda.f == 0 then
  168.             break
  169.           end
  170.           m:step()
  171.         end
  172.         th.kill(chup)
  173.         th.kill(chm)
  174.         e.cancel(timero)
  175.         for i=0,3 do
  176.           robot.select(slot+i)
  177.           robot.dropDown()
  178.         end
  179.         break
  180.       end
  181.     end
  182.  
  183.     if times <= 30 then
  184.       start = false
  185.       th.kill(ma)
  186.       while true do
  187.         m:go({0,0,0,0})
  188.         os.sleep(0.1)
  189.         local nda=m:getPos()
  190.         print(sir.serialize(nda))
  191.         if nda.x == 0 and nda.y == 0 and nda.z == 0 and nda.f == 0 then
  192.           break
  193.         end
  194.         m:step()
  195.       end
  196.       th.kill(chup)
  197.       th.kill(chm)
  198.       e.cancel(timero)
  199.       for i=0,3 do
  200.         robot.select(slot+i)
  201.         robot.dropDown()
  202.       end
  203.       print(sir.serialize(tasks))
  204.       break
  205.     end
  206.     os.sleep(0)
  207.   end
  208. end
  209.  
  210. th.create(ini)
  211. ma = th.create(main)
  212. chup = th.create(checkup)
  213. th.create(eve)
  214. chm=th.create(checkm)
  215. th.waitForAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement