Advertisement
Guest User

RGBnator

a guest
Oct 2nd, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.38 KB | None | 0 0
  1. tfm.exec.disableAfkDeath(true)
  2.  
  3. system.modes = {
  4.     [1] = {math.random(0, 255), math.random(0, 255), math.random(0, 255)}
  5. }
  6. system.type = 1
  7. system.grounds = {
  8.     [0] = "C7A889";
  9.     [1] = "C5D9E3";
  10.     [2] = "654385";
  11.     [3] = "B62B01";
  12.     [4] = "662F11";
  13.     [5] = "3E2312";
  14.     [6] = "B0C51D";
  15.     [7] = "F5E68B";
  16.     [10] = "605A4C";
  17.     [11] = "D5D6D6";
  18. };
  19.  
  20. function eventNewGame()
  21. mapAuthor = tfm.get.room.xmlMapInfo.author
  22.     if mapAuthor ~= '#Module' then
  23.         tfm.exec.disableAutoShaman(false)
  24.         system.loadMap(tfm.get.room.xmlMapInfo.xml)
  25.     else
  26.         tfm.exec.disableAutoShaman(true)
  27.         system.mapToLoad = true
  28.         system.loadTime = os.time()*8000
  29.     end
  30. end
  31.  
  32. function string.split(stringt, sep)
  33.     st = {}
  34.     for str in stringt:gmatch("[^"..sep.."]+") do
  35.         table.insert(st, str)
  36.     end
  37.     return st
  38. end
  39.  
  40. function system.loadMap(xml)
  41.     allgrounds = ""
  42.     alljoints = ""
  43.     alldecorations = ""
  44.     grounds = xml:match("<S>(.+)</S>")
  45.     xml = xml:gsub("<S>(.+)</S>", "<S>{allgrounds}</S>")
  46.     joints = xml:match("<L>(.+)</L>")
  47.  
  48.  
  49.     for object in grounds:gmatch("<S([^/>]+)/>") do
  50.         objectType = object:match("T=\"([0-9]+)\"")+0
  51.         color = object:match("o=\"([0-9A-Za-z]+)\"")
  52.         if (color == nil) then
  53.             color = system.grounds[objectType]
  54.         end
  55.        
  56.         if (color and not object:find("m=") and string.len(color) <= 6 and color:upper() ~= "6A7495") then
  57.             colort = system.applyColor(color, system.modes[system.type])
  58.             if (objectType == 13) then
  59.                 allgrounds = allgrounds..string.format("<S %s o=\"%s\" />", object, colort)
  60.             else
  61.                 if not (objectType == 9) then
  62.                     allgrounds = allgrounds..string.format("<S %s o=\"%s\" T=\"12\" />", object, colort)
  63.                 else
  64.                     allgrounds = allgrounds..string.format("<S %s />", object)
  65.                 end
  66.             end
  67.         else
  68.             allgrounds = allgrounds..string.format("<S %s />", object)
  69.         end
  70.     end
  71.    
  72.     joints = xml:match("<L>(.+)</L>")
  73.     if (joints ~= nil) then
  74.         xml = xml:gsub("<L>(.+)</L>", "<L>{alljoints}</L>")
  75.         for index, joint in ipairs(string.split(joints, "/>")) do
  76.             if (joint:match("c=\"([0-9A-Za-z,]+)\"") ~= nil) then
  77.                 c = joint:match("c=\"([0-9A-Za-z,]+)\"")
  78.                 pr = ""
  79.                 prop = string.split(c, ",")
  80.                 newC = system.applyColor(prop[1], system.modes[system.type])
  81.                 prop[1] = newC
  82.                 for ind in pairs(prop) do
  83.                     pr = pr..","..prop[ind]
  84.                 end
  85.                 pr = pr:sub(2, string.len(pr))
  86.                 jointA = joint
  87.                 joint = joint:gsub("c=\"([0-9A-Za-z,]+)\"","c=\""..pr.."\"")
  88.             end
  89.             alljoints = alljoints..joint.."/>"
  90.         end
  91.     end
  92.    
  93.     decorations = xml:match("<D>(.+)</D>")
  94.     if (decorations ~= nil) then
  95.         xml = xml:gsub("<D>(.+)</D>", "<D>{alldecorations}</D>")
  96.         for index, decoration in ipairs(string.split(decorations, "/>")) do
  97.             if (decoration:match("C=\"([0-9A-Za-z,]+)\"") ~= nil) then
  98.                 c = decoration:match("C=\"([0-9A-Za-z,]+)\"")
  99.                 pr = ""
  100.                 prop = string.split(c, ",")
  101.                 for ind, prt in pairs(prop) do
  102.                     prop[ind] = system.applyColor(prt, system.modes[system.type])
  103.                 end
  104.                
  105.                 for ind, prt in pairs(prop) do
  106.                     pr = pr..","..prt
  107.                 end
  108.                 pr = pr:sub(2)
  109.                 decoration = decoration:gsub("C=\"([0-9A-Za-z,]+)\"","C=\""..pr.."\"")
  110.             end
  111.             alldecorations = alldecorations..decoration.."/>"
  112.         end
  113.     end
  114.    
  115.     xml = xml:gsub("{allgrounds}", allgrounds)
  116.     xml = xml:gsub("{alljoints}", alljoints)
  117.     xml = xml:gsub("{alldecorations}", alldecorations)
  118.     system.mapLoaded = true
  119.     system.loadTime = os.time()
  120.     system.mapToLoad = xml
  121. end
  122.  
  123. function eventLoop(ct, tr)
  124.     if (system.mapToLoad) then
  125.         if (system.loadTime < os.time()-500) then
  126.             tfm.exec.newGame(system.mapToLoad)
  127.             system.mapToLoad = false
  128.         end
  129.     end
  130.     dark = system.applyColor(system.gethex(system.modes[system.type]), {150, 150, 150})
  131.     bright = system.applyColor(system.gethex(system.modes[system.type]), {200, 200, 200})
  132.     ui.addTextArea(0, "", nil, 770, 370, 25, 25, "0x"..bright, "0x"..dark, 1, true)
  133. end
  134.  
  135. function system.getrgb(hex,d)
  136.     hex = hex:gsub("#","")
  137.     while (string.len(hex) < 6) do
  138.         hex = '0'..hex
  139.     end
  140.     if (d) then
  141.         return {tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6))}
  142.     else
  143.         return {r = tonumber("0x"..hex:sub(1,2)), g = tonumber("0x"..hex:sub(3,4)), b = tonumber("0x"..hex:sub(5,6))}
  144.     end
  145. end
  146.  
  147. function system.gethex(rgb)
  148.     hex = ""
  149.     for ind,st in ipairs(rgb) do
  150.         st = string.format("%x", st)
  151.         if (string.len(st) == 1) then
  152.             st = "0"..st
  153.         end
  154.         hex = hex..st
  155.     end
  156.     return hex
  157. end
  158.  
  159. function eventChatCommand(player, command)
  160.     if (command:sub(0,3) == "rgb") then
  161.         command = command:sub(5)
  162.         if (command:find("%(") ~= nil and command:find("%)") ~= nil) then
  163.             rgb = command:sub(command:find("%(")+1, command:find("%)")-1)
  164.             rgb = string.split(rgb, ",")
  165.             if (#rgb > 3) then
  166.                 print("Invalid RGB")
  167.             else
  168.                 newHex = system.gethex(rgb)
  169.                 system.modes[system.type] = rgb
  170.                 print("HEX set: "..newHex)
  171.             end
  172.         else
  173.             print("Invalid RGB")
  174.         end
  175.     elseif (command:sub(0,3) == "hex") then
  176.         command = command:sub(5):gsub("#","")
  177.         if (#command > 6) then
  178.             print("Invalid HEX")
  179.         else
  180.             rgb = system.getrgb(command, true)
  181.             system.modes[system.type] = rgb
  182.             print("HEX set: "..command)
  183.         end
  184.     end
  185. end
  186.  
  187. function system.applyColor(rawColor, mode)
  188.     color = system.getrgb(rawColor)
  189.     color.r = math.floor((color.r*mode[1])/255)
  190.     color.g = math.floor((color.g*mode[2])/255)
  191.     color.b = math.floor((color.b*mode[3])/255)
  192.     hex = system.gethex({color.r, color.g, color.b})
  193.     return hex
  194. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement