Advertisement
Wojbie

Swarminer scanner code

Oct 13th, 2013
3,290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.99 KB | None | 0 0
  1. --Wojbie's Swarm Miner Wireless Scaner
  2. --Displays arr turtles in its range and what they are doing
  3. --Range depends on weather and height of placement
  4. --This is part of set of programs
  5. --To get rest of them make instaler Floppy Disk by placing empty disc in disc drive and using pastebin get LXLBZK25 disk/startup
  6.  
  7. local tArgs = {...}
  8.  
  9. local usegps=true
  10. local pos=nil
  11. local terminate=false
  12.  
  13. local channel=15151
  14. local drawmap=true
  15. local modem=false
  16. local order=nil
  17. local toggle=false
  18. local currents={}
  19. local current={}
  20. local wipedzone={["repeats"]={["x"]=1,["z"]=1,},["manpos"]={["y"]=0,["x"]=0,["z"]=0,["zd"]=0,["xd"]=0,},["start"]={["y"]=0,["x"]=0,["z"]=0,["zd"]=0,["xd"]=0,},["map"]={[1]={[1]="?",},},["zone"]={},["size"]={["z"]=0,["y"]=0,["x"]=0,},}
  21.  
  22. --map handling
  23.  
  24. local function Rev(A)  --reverses table function - keys for values and vice versa
  25.     local Revt={}
  26.     for i,j in pairs(A) do
  27.         Revt[j]=i
  28.     end
  29.     return Revt
  30. end
  31.  
  32. local mapmark={".","+","#"}
  33. local krampam=Rev(mapmark)
  34.  
  35. -- Yes/No Questions
  36.  
  37. local function yn(A)
  38.     if not A then return false end
  39.     local key
  40.     write(A.."[y/n]:")
  41.     while true do
  42.         _,key = os.pullEvent("key")
  43.         if key==21 then write(keys.getName(key).."\n") sleep(0.01) return true
  44.         elseif key==49 then write(keys.getName(key).."\n") sleep(0.01) return false
  45.         end
  46.     end
  47. end
  48.  
  49. --Asking for position
  50.  
  51. local function getpos()
  52.         local temp={}
  53.         while true do
  54.                 print("Enter data from F3 Screen")
  55.                 write("Please enter x coordinate:") temp.x=tonumber(read())
  56.                 write("Please enter y coordinate:") temp.y=tonumber(read())
  57.                 write("Please enter z coordinate:") temp.z=tonumber(read())
  58.                 if temp.x and temp.y and temp.z then
  59.                         print("x:"..temp.x.." y:"..temp.y.." z:"..temp.z)
  60.                         if yn("Are thise correct?") then break end
  61.                 end
  62.         end
  63.         return temp
  64. end
  65.  
  66.  
  67. --File Useage
  68.  
  69. local function save(A,B) local file = fs.open(tostring(A),"w") file.write(B) file.close() end
  70. local function saveT(A,B) save(A,textutils.serialize(B)) end
  71. local function saveTH(A,B) save(A,string.gsub(textutils.serialize(B),"},","},\r\n")) end
  72. local function get(A) local file = fs.open(tostring(A),"r") if not file then return false end local data = file.readAll() file.close() if data then return data end end
  73. local function getT(A) local data = get(A) if data then data = textutils.unserialize(data) end if data then return data end end
  74.  
  75. local status=""
  76.  
  77. current.loc={x=1,z=1}
  78. current.pos=0
  79. local onmap=true
  80.  
  81. --modem connection
  82. local modem=false
  83. for _,i in pairs(redstone.getSides()) do
  84.     if peripheral.isPresent(i) then
  85.         if peripheral.getType(i)=="modem" then
  86.             modem=peripheral.wrap(i)
  87.                 if modem["isWireless"] then
  88.                     if modem.isWireless() then
  89.                         modem.open(channel)
  90.                         break
  91.                     end
  92.                 else
  93.                     modem.open(channel)
  94.                     break
  95.                 end
  96.             modem=false
  97.         end
  98.     end
  99. end
  100.  
  101. --monitor finding
  102. local screen=false
  103. for _,i in pairs(redstone.getSides()) do
  104.     if peripheral.isPresent(i) then
  105.         if peripheral.getType(i)=="monitor" then
  106.             screen=peripheral.wrap(i)
  107.             local i,k=screen.getSize()
  108.             for j=0.5,5,0.5 do
  109.                 screen.setTextScale(j)
  110.                 i,k=screen.getSize()
  111.                 if i<5 or k<3 then screen.setTextScale(j-0.5) break end            
  112.             end
  113.             break
  114.         end
  115.     end
  116. end
  117.  
  118. local function transmit(...)
  119. if modem then modem.transmit(...) end
  120. end
  121.  
  122. local function howmuch(A)
  123. local i=0
  124. for _,_ in pairs(A) do
  125. i=i+1
  126. end
  127. return i
  128. end
  129.  
  130. local function prepMess()
  131. local Out={}
  132. local k,j
  133. for k,j in pairs(currents) do
  134.     if type(j.dist)=="number" and os.clock()-currents[k].atime < 5 then
  135.         Out[k]=j
  136.     end
  137. end
  138. return textutils.serialize(Out)
  139. end
  140.  
  141. local function incMess(A)
  142. if not A then return false end
  143. local k,j
  144.     for k,j in pairs(A) do
  145.         j.dist="EX"
  146.         j.atime=os.clock()
  147.         if currents[k] then
  148.             if os.clock()-currents[k].atime > 5 then currents[k]=j end
  149.         else
  150.             currents[k]=j
  151.         end
  152.     end
  153. end
  154.  
  155. local function xtface(A,B)
  156. if A==0 and B==1 then return 0
  157. elseif A==-1 and B==0 then return 1
  158. elseif A==0 and B==-1 then return 2
  159. elseif A==1 and B==0 then return 3 end
  160. return -1
  161. end
  162.  
  163. local function ordstat()
  164.     if not order then return end
  165.     order.stats={}
  166.     for i=1,#mapmark do
  167.         order.stats[mapmark[i]]=0
  168.     end
  169.     for i=1,order.repeats.x do
  170.         for k=1,order.repeats.z do
  171.             order.stats[order.map[i][k]]=order.stats[order.map[i][k]]+1
  172.         end
  173.     end
  174.     for i=1,#mapmark do
  175.         order.stats[mapmark[i]]=math.floor((order.stats[mapmark[i]]/(order.repeats.x*order.repeats.z)*100)+0.5)
  176.     end
  177.  
  178. end
  179.  
  180. local function monitor()
  181.  
  182.     local function wmap(A,B)
  183.         if current.loc and current.loc.x==A and current.loc.z==B then
  184.             term.setTextColor(colors.black)
  185.             term.setBackgroundColor(colors.white)
  186.             write(order.map[A][B])
  187.             term.setTextColor(colors.white)
  188.             term.setBackgroundColor(colors.black)
  189.         else
  190.             write(order.map[A][B])
  191.         end
  192.     end
  193.    
  194.     local function onscreen()
  195.         if screen then
  196.         for i=1,#mapmark do
  197.             screen.setCursorPos(1,i)
  198.             screen.write(mapmark[i])
  199.             local temp=order.stats[mapmark[i]]
  200.             if temp<10 then screen.write("  "..temp.."%")
  201.             elseif temp<100 then screen.write(" "..temp.."%")
  202.             else screen.write(""..temp.."%")           
  203.             end
  204.         end
  205.     end
  206.    
  207.     end
  208.  
  209.     local i,k,j,l,ioff,idraw
  210.     local m=1
  211.     term.clear()
  212.     term.setCursorPos(1,17)
  213.     write("T-Toggle")
  214.     term.setCursorPos(1,18)
  215.     write("P-Pause")
  216.     term.setCursorPos(1,19)
  217.     write("O-UnPause")
  218.     term.setCursorPos(1,1) 
  219.     while true do
  220.         if order then
  221.             if drawmap then
  222.                 onscreen()
  223.                 drawmap=false
  224.                 if order.repeats.z < 13 or not current.loc then l=0 else l=math.max( math.min(order.repeats.z-13,current.loc.z-7),0) end
  225.                 if order.repeats.x < 13 or not current.loc then j=0 else j=math.max(math.min(order.repeats.x-13,current.loc.x-7),0) end
  226.  
  227.                 for i=1,math.min(order.repeats.x,13) do
  228.                     term.setCursorPos(1,14-i)
  229.                     for k=1,math.min(order.repeats.z,13) do
  230.                             wmap(i+j,k+l)
  231.                     end
  232.                 end
  233.  
  234.                 for i=1,19 do
  235.                     term.setCursorPos(14,i)
  236.                     write("|")
  237.                 end
  238.  
  239.                 for i=1,13 do
  240.                     term.setCursorPos(i,14)
  241.                     write("-")
  242.                 end
  243.  
  244.                 term.setCursorPos(14,14)   
  245.                 write("+")
  246.             end
  247.         else
  248.             term.setCursorPos(1,1) 
  249.             write("Scanning")
  250.             for k=1,m do write(".") end
  251.             for k=4-m,1,-1 do write(" ") end
  252.             m=m+1
  253.             if m>4 then m=1 end
  254.         end
  255.        
  256.             i=1
  257.             ioff=howmuch(currents)
  258.             term.setTextColor(colors.white)
  259.             term.setBackgroundColor(colors.black)
  260.             term.setCursorPos(1,15) for l=1,13 do write(" ") end term.setCursorPos(1,15)
  261.             if current.loc then write("Sector:"..current.loc.x.."x"..current.loc.z) end
  262.             term.setCursorPos(1,16) for l=1,13 do write(" ") end term.setCursorPos(1,16)
  263.             write(current.pos.."/"..ioff)
  264.             if ioff < 19 then ioff=0 else ioff=math.max(math.min(ioff-19,current.pos-9),0) end
  265.             for k,j in pairs(currents) do
  266.  
  267.                 if j.loc and onmap then
  268.                     if current.loc.x==j.loc.x and current.loc.z==j.loc.z then
  269.                     term.setTextColor(colors.black)
  270.                     term.setBackgroundColor(colors.white)
  271.                     current.pos=i
  272.                     end
  273.                 end
  274.                
  275.                 if not onmap and current.pos==i then
  276.                     term.setTextColor(colors.black)
  277.                     term.setBackgroundColor(colors.white)
  278.                     if j.loc then if current.loc~=j.loc then current.loc.x=j.loc.x current.loc.z=j.loc.z drawmap=true end else current.loc={x=0,z=0} end
  279.                 end
  280.                    
  281.                 if i >ioff and i<=19+ioff  then
  282.                     idraw=i-ioff
  283.                     --numbers
  284.                         term.setCursorPos(15,idraw)
  285.                         for l=0,51-15 do write(" ") end
  286.                         term.setCursorPos(15,idraw)
  287.                         if j.id<10 then write("   "..j.id..":")
  288.                         elseif j.id<100 then write("  "..j.id..":")
  289.                         elseif j.id<1000 then write(" "..j.id..":")
  290.                         else write(j.id..":") end
  291.                     --data
  292.                     if not toggle then
  293.                         if j.run then write("M") elseif j.ERROR then write("E") else write("S") end
  294.                         if j.pos and j.pos.x and j.pos.y and j.pos.z then
  295.                             write(" "..j.pos.x..":"..j.pos.y..":"..j.pos.z)
  296.                             if j.pos.xd and j.pos.zd then write(" f:"..xtface(j.pos.xd,j.pos.zd)) end
  297.                         end
  298.                         if j.loc then write(" S:"..j.loc.x.."x"..j.loc.z) end
  299.                         if j.layer then write(" "..j.layer) end
  300.                         if j.ERROR then write(" ERROR") end
  301.                     else
  302.                         if j.fuel then write(" Fuel:"..j.fuel) end
  303.                         write(" Dist:"..j.dist)
  304.                         write(" Time:"..math.floor(os.clock()-j.atime))
  305.                     end
  306.                 end
  307.                
  308.                 term.setTextColor(colors.white)
  309.                 term.setBackgroundColor(colors.black)
  310.                 i=i+1
  311.             end
  312.            
  313.             for j=i,19,1 do
  314.                 term.setCursorPos(15,j)
  315.                 for l=0,51-15 do write(" ") end
  316.             end
  317.  
  318.             sleep(0.2)
  319.     end
  320.     print("I should not get printed")
  321. end
  322.  
  323. --Wireless Part
  324.  
  325.  
  326. local function wireless()
  327.     local event={}
  328.     local clear --=os.startTimer(60)
  329.     local spread=os.startTimer(5)
  330.     local temp
  331.     while true do
  332.         event={os.pullEvent()}
  333.         if event[1]=="modem_message" then
  334.             if event[3]==channel then
  335.                 if event[4]==1 then
  336.                     temp=textutils.unserialize(event[5])
  337.                     if temp then currents[temp.id]=temp currents[temp.id].dist=math.floor(event[6]) currents[temp.id].atime=os.clock() end
  338.                 elseif event[4]==2 then
  339.                     temp=textutils.unserialize(event[5])
  340.                     if temp then order=temp ordstat() drawmap=true end
  341.                 elseif event[4]==10 then
  342.                     temp=textutils.unserialize(event[5])
  343.                     if temp then incMess(temp) end
  344.                 end
  345.             elseif event[3]==gps.CHANNEL_GPS and usegps then transmit(event[4], gps.CHANNEL_GPS, textutils.serialize({pos.x,pos.y,pos.z})) end
  346.         elseif event[1]=="char" then
  347.             --if event[2]=="q" then transmit(channel,channel,"Terminate") status="Terminating"
  348.             if event[2]=="t" or event[2]=="T" then toggle=not toggle status="Toggle"
  349.             elseif event[2]=="p" or event[2]=="P" then transmit(channel,4,"Pause") status="Pausing"
  350.             elseif event[2]=="o" or event[2]=="O" then transmit(channel,5,"UnPause") status="UnPausing" end
  351.         elseif event[1]=="key"  and order then 
  352.             if     event[2]==200 then onmap=true current.pos=0 current.loc.x=math.min(current.loc.x+1,order.repeats.x) drawmap=true
  353.             elseif event[2]==208 then onmap=true current.pos=0 current.loc.x=math.max(current.loc.x-1,1) drawmap=true
  354.             elseif event[2]==205 then onmap=true current.pos=0 current.loc.z=math.min(current.loc.z+1,order.repeats.z) drawmap=true
  355.             elseif event[2]==203 then onmap=true current.pos=0 current.loc.z=math.max(current.loc.z-1,1) drawmap=true
  356.             elseif event[2]==209 or event[2]==31 then onmap=false current.loc={x=0,z=0} current.pos=math.min(current.pos+1,howmuch(currents)) drawmap=true
  357.             elseif event[2]==201 or event[2]==17 then onmap=false current.loc={x=0,z=0} current.pos=math.max(current.pos-1,1) drawmap=true end
  358.         elseif event[1]=="timer" then
  359.             if     event[2]==clear then clear=os.startTimer(60) currents={} --unused old code - left for future posibilites.
  360.             elseif event[2]==spread then  spread=os.startTimer(5) transmit(channel,10,prepMess()) end
  361.         elseif event[1]=="terminate" then
  362.             terminate=true
  363.             print("Terminate Event")
  364.             break
  365.         end
  366.     end
  367. end
  368.  
  369. if #tArgs>=0 then
  370.     if tArgs[1]=="help" or tArgs[1]=="?" then print("Emergency/WipeOrder/Goto") return true
  371. elseif tArgs[1]=="Emergency" then transmit(channel,channel,"Terminate") print("Terminating All in Range") return true
  372. elseif tArgs[1]=="WipeOrder" then transmit(channel,4,"Pause") sleep(1) transmit(channel,7,textutils.serialize(wipedzone))  print("Orders Wiped All in Range") return true
  373. elseif tArgs[1]=="Goto" then transmit(channel,4,"Pause") sleep(1) print("Enter Goto Target:")  transmit(channel,6,textutils.serialize(getpos()))  print("Goto Orders Sent to All in Range") return true
  374.     end
  375. end
  376.  
  377. sleep(0.01)
  378. os.setComputerLabel("Swarminer Scanner id:"..os.getComputerID())
  379. pos=getT("/gps.log")
  380. if pos then
  381.     usegps=true if modem then modem.open(gps.CHANNEL_GPS) end
  382. else
  383.     usegps=false if modem then modem.close(gps.CHANNEL_GPS) end
  384. end
  385. transmit(channel,3,"Order?")
  386.  
  387. --non termination point
  388.  
  389. local oldpullEvent=os.pullEvent
  390. os.pullEvent=os.pullEventRaw
  391.  
  392. repeat
  393.  
  394. transmit(channel,3,"Order?")
  395. parallel.waitForAny(monitor,wireless)
  396.  
  397. until terminate
  398.  
  399. --Restoring normal termination
  400. os.pullEvent=oldpullEvent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement