Advertisement
Kodos

VOP Code

Apr 6th, 2015
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local colors = require("colors")
  3. local sides = require("sides")
  4. local event = require("event")
  5.  
  6. local rs = component.redstone
  7. local cb = component.chat_box
  8. local cl = component.colorful_lamp
  9. local de = component.draconic_storage
  10. local ni = component.notification_interface -- TODO: Correct component name
  11. local db = component.debug
  12. local ws = component.world_sensor -- TODO: Correct this one too
  13.  
  14.  
  15.  
  16. -- ChatBox init
  17. cb.setName("Dave")
  18.  
  19. if cb.getDistance() < 32768 then
  20. cb.setDistance(32768)
  21. end
  22.  
  23. local function rgbto15(r,g,b)
  24.   return (b%32)+((g%32)*32)+((r%32)*1024)
  25. end
  26.  
  27. local function round(num, idp)
  28.     local mult = 10^(idp or 0)
  29.     return math.floor(num * mult + 0.5) / mult
  30. end
  31.  
  32. local messages = {
  33.  
  34.   ["lights"] = {
  35.     --Syntax: ["keyword"] = {function, all, other, arguments}
  36.     ["on"] = {rs.setBundledOutput, sides.back, colors.cyan, 15},
  37.     -- ["on"] = {rs.setBundledOutput, sides.back, colors.orange, 15},
  38.     ["off"] = {rs.setBundledOutput, sides.back, colors.cyan, 0},
  39.     -- ["off"] = {rs.setBundledOutput, sides.back, colors.orange, 0}
  40.   },
  41.  
  42.   ["door"] = {
  43.     ["open"] = {rs.setBundledOutput, sides.back, colors.magenta, 15},
  44.     ["close"] = {rs.setBundledOutput, sides.back, colors.magenta, 0}
  45.   },
  46.  
  47.   ["all machines"] = {
  48.     ["on"] = {cl.setLampColor, rgbto15(0,255,0)},
  49.     ["off"] = {cl.setLampColor, rgbto15(255,0,0)}
  50.   },
  51.  
  52.   ["core"] = {
  53.     ["current"] = {cb.say, tostring(de.getEnergyStored())},
  54.     ["max"] = {cb.say, tostring(de.getMaxEnergyStored())}
  55.   }
  56.  
  57.   ["reactor"] = {
  58.   ["fuel"] = {}
  59.   }
  60.  
  61.   }
  62.  
  63.  
  64. local function getFunction(tbl, msg)
  65.   if tbl[1] then
  66.     return tbl
  67.   end
  68.   for i,j in pairs(tbl) do
  69.     if type(i) == "string" then
  70.       print(i)
  71.       if msg:find(i) and type(j) == "table" then
  72.         return getFunction(j, msg)
  73.       end
  74.     end
  75.   end
  76. end
  77.  
  78. local function parseMessage(msg)
  79.   local fnc = getFunction(messages, msg)
  80.   if not fnc then return false, "no command found" end
  81.   return pcall(table.unpack(fnc))
  82. end
  83.  
  84. while true do
  85. local _, _, user, message = event.pull("chat_message")
  86. local result, response = parseMessage(message:lower())
  87. print(result)
  88. print(response)
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement