Advertisement
Forecaster

Token Program (OpenComputers)

Mar 26th, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.45 KB | None | 0 0
  1. function string:split(delimiter)
  2.   local result = { }
  3.   local from  = 1
  4.   local delim_from, delim_to = string.find( self, delimiter, from  )
  5.   while delim_from do
  6.     table.insert( result, string.sub( self, from , delim_from-1 ) )
  7.     from  = delim_to + 1
  8.     delim_from, delim_to = string.find( self, delimiter, from  )
  9.   end
  10.   table.insert( result, string.sub( self, from  ) )
  11.   return result
  12. end
  13.  
  14. function increment(variable, value)
  15.   return variable + value
  16. end
  17.  
  18. function decrement(variable, value)
  19.   return variable - value
  20. end
  21.  
  22. --Now uses "aspects.color" instead, defined at line 35 which translate to the corresponding number according to the table below, this is included by computronics
  23. --Aspects:
  24. --1 Green
  25. --2 Blink Yellow
  26. --3 Yellow
  27. --4 Blink Red
  28. --5 Red
  29.  
  30. local c = require("component")
  31. local event = require("event")
  32.  
  33. local rec = c.digital_receiver_box
  34. local con = c.digital_controller_box
  35. local aspects = c.digital_controller_box.aspects
  36.  
  37. local tokens = {}
  38.  
  39. tokens[1] = {2, 2}
  40. tokens[2] = {1, 1}
  41.  
  42. for k,v in pairs(tokens) do
  43.   con.setAspect(k .. "-ctrl", aspects.green)
  44. end
  45.  
  46. local running = true
  47. while running do
  48.   local myEvent, address, origin, aspect, player = event.pull()
  49. --  print(myEvent, address, origin, aspect, player)
  50.   if (myEvent == "interrupted") then
  51.     running = false
  52.     for k,v in pairs(tokens) do
  53.       con.setAspect(k .. "-ctrl", 5)
  54.     end
  55.   elseif (myEvent == "aspect_changed") then
  56.     local data = string.split(origin, "-")
  57.     local group = tonumber(data[1])
  58.     local mode = data[2]
  59.    
  60.     if (aspect == aspects.green) then
  61. --      print("Group: " .. group, "Mode: " .. mode)
  62. --      print("Group currently has " .. tokens[group][1] .. "/" .. tokens[group][2] .. " tokens.")
  63.       if (mode == "in") then
  64.         if (tokens[group][1] > 0) then
  65.           tokens[group][1] = decrement(tokens[group][1], 1)
  66.           print("Group " .. group .. " now has " .. tokens[group][1] .. "/" .. tokens[group][2] .. " tokens")
  67.         end
  68.       elseif (mode == "out") then
  69.         if (tokens[group][1] < tokens[group][2]) then
  70.           tokens[group][1] = increment(tokens[group][1], 1)
  71.           print("Group " .. group .. " now has " .. tokens[group][1] .. "/" .. tokens[group][2] .. " tokens")
  72.         end
  73.       end
  74.       if (tokens[group][1] == 0) then
  75.         con.setAspect(group .. "-ctrl", aspects.red)
  76.       else
  77.         con.setAspect(group .. "-ctrl", aspects.green)
  78.       end
  79.     end
  80.   end
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement