Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.86 KB | None | 0 0
  1. --Creator: Bolodefchoco
  2. --Made in: 06/02/2017
  3. --Last update: 26/07/2017
  4.  
  5. --[[ Module ]]--
  6. local module = {
  7.     _VERSION = "3.8",
  8.     _NAME = "grounds",
  9.     _STATUS = "semi-official",
  10.     _AUTHOR = "Bolodefchoco",
  11.     _LICENSE = [[
  12.         MIT LICENSE
  13.        
  14.         Copyright (c) 2017 @Transformice + @Bolodefchoco
  15.        
  16.         Permission is hereby granted, free of charge, to any person obtaining
  17.         a copy of this software and associated documentation files (the
  18.         "Software"), to deal in the Software without restriction, including
  19.         without limitation the rights to use, copy, modify, merge, and to
  20.         permit persons to whom the Software is furnished to do so, subject to
  21.         the following conditions:
  22.  
  23.         The above copyright notice and this permission notice shall be included
  24.         in all copies or substantial portions of the Software.
  25.  
  26.         THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  27.         OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR
  28.         A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  29.         IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  30.         CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  31.         TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  32.         SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33.     ]],
  34.     _FREEACCESS = { -- Verified players
  35.         -- 3 : Commands + Room admin + Debug
  36.         Bolodefchoco = 3,
  37.         -- 2 : Commands
  38.         Bodykudo = 2,
  39.         Error_404 = 2,
  40.         Jordynl = 2,
  41.         Laagaadoo = 2,
  42.         Sebafrancuz = 2,
  43.         Tocutoeltuco = 2,
  44.         -- 1 : Some commands
  45.         Artinoe = 1,
  46.         Bapereira = 1,
  47.         Barberserk = 1,
  48.         Byontr = 1,
  49.         Claumiau = 1,
  50.         Drescen = 1,
  51.         Ekull = 1,
  52.         Elvismouse = 1,
  53.         Grastfetry = 1,
  54.         Kimsterjay = 1,
  55.         Mcqv = 1,
  56.         Mescouleur = 1,
  57.         Mquk = 1,
  58.         Reshman = 1,
  59.         Rikkeshang = 1,
  60.         Ruamorangos = 1,
  61.         Sammynya = 1,
  62.     },
  63. }
  64.  
  65. --[[ Optimization ]]--
  66.   --[[ String ]]--
  67. local stringlen,stringfind,stringgmatch,stringbyte,stringdump,stringreverse,stringupper,stringformat,stringrep,stringlower,stringsub,stringgsub,stringmatch,stringchar = string.len,string.find,string.gmatch,string.byte,string.dump,string.reverse,string.upper,string.format,string.rep,string.lower,string.sub,string.gsub,string.match,string.char
  68.   --[[ Math ]]--
  69. local mathdeg,mathfmod,mathrandom,mathasin,mathmax,mathmodf,mathfloor,mathcosh,mathldexp,mathatan2,mathpow,mathrandomseed,mathfrexp,mathabs,mathtanh,mathacos,mathlog,mathtan,mathmin,mathceil,mathsinh,mathsqrt,mathhuge,mathrad,mathsin,mathexp,mathcos,mathatan,mathpi = math.deg,math.fmod,math.random,math.asin,math.max,math.modf,math.floor,math.cosh,math.ldexp,math.atan2,math.pow,math.randomseed,math.frexp,math.abs,math.tanh,math.acos,math.log,math.tan,math.min,math.ceil,math.sinh,math.sqrt,math.huge,math.rad,math.sin,math.exp,math.cos,math.atan,math.pi
  70.   --[[ Table ]]--
  71. local tablepack,tableforeachi,tableforeach,tableremove,tableinsert,tableunpack,tableconcat,tablesort = table.pack,table.foreachi,table.foreach,table.remove,table.insert,table.unpack,table.concat,table.sort
  72.  
  73. --[[ API ]]--
  74.     -- Timers
  75. system.newGameTimer = 0
  76.  
  77.     -- Control
  78. system.officialMode = {"",""}
  79. system.setAdmins = function()
  80.     local out = {}
  81.     for k,v in next,module._FREEACCESS do
  82.         if v > 2 then
  83.             out[k] = true
  84.         end
  85.     end
  86.     return out
  87. end
  88.  
  89.     -- Improvements
  90. tableconcat = function(list,sep,f,i,j)
  91.     local txt = ""
  92.     sep = sep or ""
  93.     i,j = i or 1,j or #list
  94.     for k,v in next,list do
  95.         if type(k) ~= "number" and true or (k >= i and k <= j) then
  96.             txt = txt .. (f and f(k,v) or v) .. sep
  97.         end
  98.     end
  99.     return stringsub(txt,1,-1-#sep)
  100. end
  101. do
  102.     local newGame = tfm.exec.newGame
  103.     tfm.exec.newGame = function(code)
  104.         if os.time() > system.newGameTimer then
  105.             system.newGameTimer = os.time() + 6000
  106.             newGame(code)
  107.             return true
  108.         end
  109.         return false
  110.     end
  111.    
  112.     local addImage = tfm.exec.addImage
  113.     local removeImage = tfm.exec.removeImage
  114.     tfm.exec.addImage = function(...)
  115.         local id = addImage(...)
  116.         if id then
  117.             system.objects.image[id] = true
  118.         end
  119.         return id
  120.     end
  121.     tfm.exec.removeImage = function(id)
  122.         removeImage(id)
  123.         if system.objects.image[id] then
  124.             system.objects.image[id] = nil
  125.         end
  126.     end
  127.    
  128.     local addTextArea = ui.addTextArea
  129.     ui.addTextArea = function(id,...)
  130.         addTextArea(id,...)
  131.         if not system.objects.textarea[id] then
  132.             system.objects.textarea[id] = true
  133.         end
  134.     end
  135.    
  136.     local chatMessage = tfm.exec.chatMessage
  137.     tfm.exec.chatMessage = function(txt,n)
  138.         if #txt > 1000 then
  139.             local total = 0
  140.             while #txt > total do
  141.                 chatMessage(stringsub(txt,total,total + 1000),n)
  142.                 total = total + 1001
  143.             end
  144.         else
  145.             chatMessage(txt,n)
  146.         end
  147.     end
  148.    
  149.     local loadPlayerData = system.loadPlayerData
  150.     system.loadPlayerData = function(n)
  151.         if module._STATUS == "official" then
  152.             return loadPlayerData(n)
  153.         else
  154.             if _G["eventPlayerDataLoaded"] then
  155.                 eventPlayerDataLoaded(n,"")
  156.             end
  157.             return true
  158.         end
  159.     end
  160.    
  161.     local savePlayerData = system.savePlayerData
  162.     system.savePlayerData = function(n,data)
  163.         if module._STATUS == "official" then
  164.             savePlayerData(n,data)
  165.             return true
  166.         else
  167.             return false
  168.         end
  169.     end
  170. end
  171.  
  172.     -- Room
  173. system.isRoom = stringbyte(tfm.get.room.name,2) ~= 3
  174. system.roomAdmins = system.setAdmins()
  175. system.miscAttrib = 0
  176. system.roomNumber,system.roomAttributes = (function()
  177.     if system.isRoom then
  178.         local number,attribute = stringmatch(tfm.get.room.name,"%*?#"..module._NAME.."(%d+)(.*)")
  179.         return tonumber(number) or "",attribute or ""
  180.     else
  181.         return "",""
  182.     end
  183. end)()
  184.  
  185.     -- Player
  186. system.isPlayer = function(n)
  187.     --[[
  188.         The player must not be a souris;
  189.         The player must have played Transformice for at least 5 days
  190.     ]]
  191.     return tfm.get.room.playerList[n] and stringsub(n,1,1) ~= "*" and tfm.get.room.playerList[n].registrationDate and (os.time() - (tfm.get.room.playerList[n].registrationDate or 0) >= 432e6) or false
  192. end
  193. system.players = function(alivePlayers)
  194.     local alive,total = 0,0
  195.     if alivePlayers then
  196.         alive = {}
  197.     end
  198.     for k,v in next,tfm.get.room.playerList do
  199.         if system.isPlayer(k) then
  200.             if not v.isDead and not v.isVampire then
  201.                 if alivePlayers then
  202.                     alive[#alive + 1] = k
  203.                 else
  204.                     alive = alive + 1
  205.                 end
  206.             end
  207.             total = total + 1
  208.         end
  209.     end
  210.     if alivePlayers then
  211.         return alive
  212.     else
  213.         return alive,total
  214.     end
  215. end
  216.  
  217.     -- System
  218. currentTime,leftTime = 0,0
  219. system.loadTable = function(s)
  220.     -- "a.b.c.1" returns a[b][c][1]
  221.     local list
  222.     for tbl in stringgmatch(s,"[^%.]+") do
  223.         tbl = tonumber(tbl) and tonumber(tbl) or tbl
  224.         list = (list and list[tbl] or _G[tbl])
  225.     end
  226.     return list
  227. end
  228. system.getTranslation = function(index,n)
  229.     local t = stringformat("mode.%s.translations.%s.%s",system.gameMode,(n and mode[system.gameMode].info[n].langue or mode[system.gameMode].langue),index)
  230.     return system.loadTable(t) or system.loadTable(stringgsub(t,"translations%..-%.",function() return "translations.en." end))
  231. end
  232. system.looping = function(f,tick)
  233.     local s = 1000 / tick
  234.     local t = {}
  235.    
  236.     local fooTimer = function()
  237.         t[#t+1] = system.newTimer(f,1000,true)
  238.     end
  239.     for timer = 0,1000 - s,s do
  240.         system.newTimer(fooTimer,1000 + timer,false)
  241.     end
  242.     return t
  243. end
  244.  
  245.     -- Interface
  246. ui.banner = function(image,aX,aY,n,time)
  247.     time = time or 5
  248.     axis = axis or {100,100}
  249.    
  250.     local img = tfm.exec.addImage(image .. ".png","&0",aX,aY,n)
  251.     system.newTimer(function()
  252.         tfm.exec.removeImage(img)
  253.     end,time * 1000,false)
  254. end
  255.  
  256.     -- Math
  257. mathisNegative = function(x,iN,iP)
  258.     return (x<0 and (iN or x) or (iP or x))
  259. end
  260. mathpercent = function(x,y,v)
  261.     v = (v or 100)
  262.     local m = x/y * v
  263.     return mathmin(m,v)
  264. end
  265. mathpythag = function(x1,y1,x2,y2,range)
  266.     return (x1-x2)^2 + (y1-y2)^2 <= (range^2)
  267. end
  268. mathsetLim = function(value,min,max)
  269.     return mathmax(min,mathmin(max,value))
  270. end
  271.  
  272.     -- String
  273. stringsplit = function(value,pattern,f)
  274.     local out = {}
  275.     for v in stringgmatch(value,pattern) do
  276.         out[#out + 1] = (f and f(v) or v)
  277.     end
  278.     return out
  279. end
  280. stringnick = function(player)
  281.     return stringgsub(stringlower(player),"%a",stringupper,1)
  282. end
  283.  
  284.     -- Table
  285. tablefind = function(list,value,index,f)
  286.     for k,v in next,list do
  287.         local i = (type(v) == "table" and index and v[index] or v)
  288.         if (f and f(i) or i) == value then
  289.             return true,k
  290.         end
  291.     end
  292.     return false,0
  293. end
  294. tableturnTable = function(x)
  295.     return (type(x)=="table" and x or {x})
  296. end
  297. tablerandom = function(t)
  298.     return (type(t) == "table" and t[mathrandom(#t)] or mathrandom())
  299. end
  300. tableshuffle = function(t)
  301.     local randomized = {}
  302.     for v = 1,#t do
  303.         tableinsert(randomized,mathrandom(#randomized),t[v])
  304.     end
  305.     return randomized
  306. end
  307.  
  308.     -- Others
  309. deactivateAccents=function(str)
  310.     local letters = {a = {"á","â","à","å","ã","ä"},e = {"é","ê","è","ë"},i = {"í","î","ì","ï"},o = {"ó","ô","ò","õ","ö"},u = {"ú","û","ù","ü"}}
  311.     for k,v in next,letters do
  312.         for i = 1,#v do
  313.             str = stringgsub(str,v[i],tostring(k))
  314.         end
  315.     end
  316.     return str
  317. end
  318. normalizeTime = function(time)
  319.     return mathfloor(time) + ((time - mathfloor(time)) >= .5 and .5 or 0)
  320. end
  321. disableChatCommand = function(command)
  322.     system.disableChatCommandDisplay(command,true)
  323.     system.disableChatCommandDisplay(stringlower(command),true)
  324.     system.disableChatCommandDisplay(stringupper(command),true)
  325. end
  326. normalizeTranslation = function()
  327.     local t = mode[system.gameMode].translations
  328.  
  329.     for k,v in next,t.en do
  330.         for i,j in next,t do
  331.             if i ~= "en" then
  332.                 if not j[k] then
  333.                     j[k] = v
  334.                 end
  335.             end
  336.         end
  337.     end
  338. end
  339.  
  340.     -- XML Dealer
  341. xml = {}
  342. xml.parse = function(currentXml)
  343.     currentXml = stringmatch(currentXml,"<P (.-)/>") or ""
  344.     local out = {}
  345.     for tag,_,value in stringgmatch(currentXml,"([%-%w]+)=([\"'])(.-)%2") do
  346.         out[tag] = value
  347.     end
  348.     return out
  349. end
  350. xml.attribFunc = function(currentXml,funcs)
  351.     local attributes = xml.parse(currentXml)
  352.     for k,v in next,funcs do
  353.         if attributes[v.attribute] then
  354.             v.func(attributes[v.attribute])
  355.         end
  356.     end
  357. end
  358. xml.addAttrib = function(currentXml,out,launch)
  359.     local parameters = stringmatch(currentXml,"<P (.-)/>") or ""
  360.     for k,v in next,out do
  361.         if not stringfind(parameters,v.tag) then
  362.             currentXml = stringgsub(currentXml,"<P (.-)/>",function(attribs)
  363.                 return stringformat("<P %s=\"%s\" %s/>",v.tag,v.value,attribs)
  364.             end)
  365.         end
  366.     end
  367.     if launch then
  368.         tfm.exec.newGame(currentXml)
  369.     else
  370.         return currentXml
  371.     end
  372. end
  373. xml.getCoordinates = function(s)
  374.     if stringfind(s,";") then
  375.         local x,y
  376.         local axis,value = stringmatch(s,"(%a);(%d+)")
  377.         value = tonumber(value)
  378.         if value then
  379.             if axis == "x" then x = value else y = value end
  380.         end
  381.         return x or 0,y or 0
  382.     else
  383.         local pos = {}
  384.         for x,y in stringgmatch(s,"(%d+) ?, ?(%d+)") do
  385.             pos[#pos+1] = {x = x,y = y}
  386.         end
  387.         return pos
  388.     end
  389. end
  390.  
  391. --[[ GameMode ]]--
  392. system.submodes = {}
  393.  
  394. system.gameMode = module._NAME
  395. system.modeChanged = os.time() + 10e3
  396.  
  397. system.getGameMode = function(value,notFirst)
  398.     local found,submode = tablefind(system.submodes,stringlower(value),nil,stringlower)
  399.     if found then
  400.         system.gameMode = system.submodes[submode]
  401.        
  402.         if notFirst then
  403.             eventModeChanged()
  404.         end
  405.        
  406.         system.modeChanged = os.time() + 10e3
  407.     end
  408.     return found
  409. end
  410.  
  411. --[[ Modes ]]--
  412. mode = setmetatable({},{
  413.     __newindex = function(list,key,value)
  414.         rawset(list,key,value)
  415.         system.submodes[#system.submodes+1] = key
  416.     end
  417. })
  418.  
  419. --[[ File Modules Aqui doesn't exist. ]]--
  420.  
  421.  
  422. --[[ ModeChanged ]]--
  423. system.objects = {
  424.     image = {},
  425.     textarea = {}
  426. }
  427. eventModeChanged = function()
  428.     -- Remove content
  429.     for k in next,system.objects.image do
  430.         tfm.exec.removeImage(k)
  431.     end
  432.    
  433.     for k in next,system.objects.textarea do
  434.         ui.removeTextArea(k,nil)
  435.     end
  436.    
  437.     system.objects = {
  438.         image = {},
  439.         textarea = {}
  440.     }
  441.    
  442.     ui.addPopup(0,0,"",nil,-1500,-1500)
  443.    
  444.     -- Unbind keyboard and mouse, also normalize color name and scores
  445.     for k in next,tfm.get.room.playerList do
  446.         for i = 0,255 do
  447.             for v = 0,1 do
  448.                 system.bindKeyboard(k,i,v == 0,false)
  449.             end
  450.         end
  451.        
  452.         system.bindMouse(k,false)
  453.        
  454.         tfm.exec.setNameColor(k,-1)
  455.         tfm.exec.setPlayerScore(k,0)
  456.     end
  457.    
  458.     -- Set admin back
  459.     system.roomAdmins = system.setAdmins()
  460.    
  461.     -- Reset settings
  462.     for k,v in next,{"AutoScore","WatchCommand","AutoNewGame","AutoShaman","AllShamanSkills","MortCommand","DebugCommand","MinimalistMode","AfkDeath","PhysicalConsumables","AutoTimeLeft"} do
  463.         tfm.exec["disable" .. v](false)
  464.     end
  465.     tfm.exec.setAutoMapFlipMode()
  466.    
  467.     tfm.exec.setRoomMaxPlayers(25)
  468.     tfm.exec.setRoomPassword("")   
  469. end
  470.  
  471. --[[ Events ]]--
  472. events = {}
  473.  
  474. events.eventLoop = function(currentTime,leftTime)
  475.     _G.currentTime = normalizeTime(currentTime / 1e3)
  476.     _G.leftTime = normalizeTime(leftTime / 1e3)
  477. end
  478.  
  479. events.eventChatCommand = function(n,c)
  480.     disableChatCommand(c)
  481.     if module._FREEACCESS[n] then
  482.         if c == "refresh" and (not system.isRoom or module._FREEACCESS[n] > 1) then
  483.             eventModeChanged()
  484.             system.init(true)
  485.         elseif stringsub(c,1,4) == "room" and (not system.isRoom or module._FREEACCESS[n] > 1) then
  486.             system.roomNumber = tonumber(stringsub(c,6)) or 0
  487.             if _G["eventChatCommand"] then
  488.                 eventChatCommand(n,"refresh")
  489.             end
  490.         elseif stringsub(c,1,4) == "load" and (not system.isRoom or module._FREEACCESS[n] > 2) then
  491.             if os.time() > system.modeChanged and os.time() > system.newGameTimer then
  492.                 local newMode = system.getGameMode(stringsub(c,6),true)
  493.                 if newMode then
  494.                     system.init(system.isRoom)
  495.                 end
  496.             end
  497.         end
  498.     end
  499.     if stringsub(c,1,6) == "module" then
  500.         c = stringupper(stringsub(c,8))
  501.         if module["_" .. c] then
  502.             tfm.exec.chatMessage(c .. " : " .. tableconcat(tableturnTable(module["_" .. c]),"\n",function(k,v)
  503.                 return (c == "FREEACCESS" and stringformat("%s(%s)",k,v) or v)
  504.             end),n)
  505.         else
  506.             tfm.exec.chatMessage(stringformat("VERSION : %s\nNAME : %s\nSTATUS : %s\nAUTHOR : %s\n\nMODE : %s",module._VERSION,module._NAME,module._STATUS,module._AUTHOR,system.gameMode),n)
  507.         end
  508.     elseif c == "modes" then
  509.         tfm.exec.chatMessage(tableconcat(system.submodes,"\n",function(k,v)
  510.             return "#" .. v
  511.         end),n)
  512.     elseif c == "admin" then
  513.         tfm.exec.chatMessage(tableconcat(system.roomAdmins,", ",tostring),n)
  514.     elseif c == "stop" and system.roomAdmins[n] then
  515.         system.exit()
  516.     elseif stringsub(c,1,3) == "adm" and (system.roomAdmins[n] or module._FREEACCESS[n] > 2) then
  517.         system.roomAdmins[stringnick(stringsub(c,5))] = true
  518.     end
  519. end
  520.  
  521. events.eventNewPlayer = function(n)
  522.     tfm.exec.lowerSyncDelay(n)
  523.    
  524.     if system.officialMode[2] ~= "" then
  525.         tfm.exec.chatMessage(system.officialMode[2],n)
  526.     end
  527. end
  528.  
  529. --[[ RoomSettings ]]--
  530. system.roomSettings = {
  531.     ["@"] = function(n)
  532.         system.roomAdmins[stringnick(n)] = true
  533.     end,
  534.     ["#"] = function(id)
  535.         system.miscAttrib = tonumber(id) or 1
  536.         system.miscAttrib = mathsetLim(system.miscAttrib,1,99) -- mathmax(1,mathmin(system.miscAttrib,99))
  537.     end,
  538.     ["*"] = function(name)
  539.         local game = system.getGameMode(name)
  540.         if not game then
  541.             system.gameMode = module._NAME
  542.         end
  543.     end
  544. }
  545. system.setRoom = function()
  546.     if system.isRoom and system.roomAttributes then
  547.         local chars = ""
  548.         for k in next,system.roomSettings do
  549.             chars = chars .. k
  550.         end
  551.  
  552.         for char,value in stringgmatch(system.roomAttributes,"(["..chars.."])([^"..chars.."]+)") do
  553.             value = stringmatch(value,"[^%s]+")
  554.             for k,v in next,system.roomSettings do
  555.                 if k == char then
  556.                     v(value)
  557.                     break
  558.                 end
  559.             end
  560.         end
  561.        
  562.         local officialModes = {
  563.             {"vanilla","<VP>Enjoy your vanilla (: .. okno"},
  564.             {"survivor","<R>Aw, you cannot play survivor on #grounds"},
  565.             {"racing","<CH>Uh, racing? Good luck!"},
  566.             {"music","<BV>Music? Nice choice! Why don't you try a rock'n'roll?"},
  567.             {"bootcamp","<PT>Bootcamp? Ok. This is unfair and your data won't be saved out of the room."},
  568.             {"defilante","<R>Aw, you cannot play defilante on #grounds"},
  569.             {"village","<R>You cannot play village on #grounds. Please, change your room."},
  570.         }
  571.         for k,v in next,officialModes do
  572.             if stringfind(system.roomAttributes,v[1] .. "$") then
  573.                 system.officialMode = {v[1],v[2]}
  574.                 break
  575.             end
  576.         end
  577.     end
  578. end
  579.  
  580. --[[ Initialize ]]--
  581. execute = {}
  582. system.setRoom()
  583.  
  584. system.init = function(refresh)
  585.     for i,event in next,{"Loop","NewGame","PlayerDied","PlayerGetCheese","PlayerVampire","PlayerWon","PlayerLeft","EmotePlayed","Keyboard","Mouse","PopupAnswer","TextAreaCallback","ChatCommand","ChatMessage","SummoningStart","SummoningEnd","SummoningCancel","NewPlayer","PlayerRespawn","ColorPicked"} do
  586.         local e = "event" .. event
  587.        
  588.         local found = false
  589.         for k,v in next,mode[system.gameMode] do
  590.             if k == e then
  591.                 execute[e] = v
  592.                 found = true
  593.                 break
  594.             end
  595.         end
  596.         if not found then
  597.             execute[e] = function() end
  598.         end
  599.  
  600.         _G[e] = function(...)
  601.             if events[e] then
  602.                 events[e](...)
  603.             end
  604.             execute[e](...)
  605.         end
  606.     end
  607.  
  608.     if refresh then
  609.         if mode[system.gameMode].reset then
  610.             mode[system.gameMode].reset()
  611.         end
  612.     end
  613.    
  614.     if _G["eventNewPlayer"] then
  615.         tableforeach(tfm.get.room.playerList,eventNewPlayer)
  616.     end
  617.    
  618.     normalizeTranslation()
  619.     mode[system.gameMode].init()
  620. end
  621. system.init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement