condecero

ItemCounter mit Savefunktion V.1.3 by redled72 5/2012

Jan 31st, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.95 KB | None | 0 0
  1. -- ItemCounter mit Savefunktion V.1.3 by redled72 5/2012
  2.  
  3. cdel  = ("right")         -- Reset Counter (Button, Switch...)
  4. cin   = ("left")          -- Input Counter (RP-Signal, Redstonepipe...)
  5. sfile = "counter.txt"     -- Filename Counterzaehlerstand
  6.  
  7. local inp    = false
  8. local s      = 0
  9. local z      = 0
  10. local stack  = 0
  11. local stueck = 0
  12. local count  = 0
  13. local wrt    = 0
  14.  
  15. function _dat()            -- File anlegen/Standardwert    
  16.          h = fs.open(sfile, "w")
  17.          h.write(0)
  18.          h.close()
  19. end
  20.  
  21. function _readdat()        -- File oeffnen und erste Zeile lesen
  22.          dat = fs.open(sfile, "r")
  23.          zeile = dat.readLine()
  24.          dat.close()
  25.   return zeile
  26. end
  27.  
  28. function _writedat(z)      -- Fileinhalt ueberschreiben
  29.          h = fs.open(sfile, "w")
  30.          h.write(z)
  31.          h.close()
  32. end
  33.  
  34. function _wrtimer(z)       -- periodisch speichern
  35.          wrt = wrt + 1
  36.          if wrt == 8 then
  37.             _writedat(z)
  38.             wrt = 0
  39.          end
  40. end
  41.  
  42. function _latch(inp)      -- Counter
  43.          if inp == true then
  44.             count = count + 1
  45.             while rs.getInput(cin) == inp do                 
  46.                   sleep(0.04)
  47.             end
  48.          else
  49.             sleep(0.03)      
  50.          end
  51.          if rs.getInput(cdel) == true then
  52.             _dat()
  53.             count = 0
  54.          end
  55.    return count
  56. end
  57.  
  58. function _str(z)          -- Stack/Stueckrechnung
  59.          sta = math.floor(z / 64)
  60.          stu = math.fmod(z, 64)
  61.    return sta, stu
  62. end
  63.  
  64. function _ascii()
  65.           out = string.char(98,121,32,82,101,100,108,101,100,55,50)
  66.     return out
  67.  end
  68.  
  69. if not fs.exists(sfile) then _dat() end
  70. count = _readdat()        
  71.  
  72. while s == 0 do           -- Loop Hauptschleife  
  73.          inp = rs.getInput(cin)
  74.          z = _latch(inp)
  75.          _wrtimer(z)         
  76.          stack, stueck = _str(z)
  77.          shell.run("clear")
  78.          print("Counter Stueckzahl: " ..z.. " ")
  79.          print("Anzahl in Stack   : " ..stack.." & " ..stueck.. " Stueck")
  80. end
  81.  
  82. print(newline)
  83. print("\"CC-ItemCounter\" " .._ascii().. "")
Advertisement
Add Comment
Please, Sign In to add comment