Advertisement
Guest User

tfm

a guest
Nov 20th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.59 KB | None | 0 0
  1. -- Version Survoltaje
  2.  
  3. --[[
  4. TODO:
  5.  - check if t[x] = nil does not cause memory leak or something
  6. DOING:
  7.  - implement the n-points-away condition to win
  8. --]]
  9.  
  10. tfm.exec.disableAutoShaman()
  11. tfm.exec.disableAutoNewGame()
  12. tfm.exec.disableAutoTimeLeft()
  13.  
  14. local ADMINISTRADOR = "Hamiskley#0000" -- CHEF DU MODULE
  15. local WINS = 10 -- POINTS POUR GAGNER
  16. system.disableChatCommandDisplay("np", true)
  17. system.disableChatCommandDisplay("pause", true)
  18. system.disableChatCommandDisplay("skip", true)
  19. local names = {
  20. "Equipe rouge", -- rouge
  21. "Equipe bleue"  -- bleue
  22. }
  23.  
  24. local teams = {
  25. [1] = {},
  26. [2] = {},
  27. }
  28. local points = {
  29. [1] = 0,
  30. [2] = 0,
  31. }
  32.  
  33. local points_away = 1
  34. local bonus_podium = 0
  35.  
  36. local podium = {}
  37. local podium_index = {0, 0}
  38.  
  39. local gameRunning = false
  40. local winTime = false
  41. local map_ended = false
  42.  
  43. local playersInGame = {}
  44. local maps = {
  45. 2, 11, 12, 19, 22, 24, 40,  44, 45, 49, 53, 55, 57,62, 67, 69, 71, 73, 74, 75, 79, 80, 85, 86, 96, 119, 123, 127, 138, 142, 145, 150
  46. }
  47. local colors = {
  48. [1] = "ff6347",
  49. [2] = "19b5fe"
  50. }
  51. local queue = {}
  52. local playerScores = {}
  53.  
  54. function newMap()
  55. local mapcode = maps[math.random(#maps)]
  56. if #queue > 0 then
  57. mapcode = queue[1]
  58. table.remove(queue, 1)
  59. end
  60. tfm.exec.newGame(mapcode) -- don't flip the maps, it's bugged! (19/05/2017)
  61. for player, data in pairs(tfm.get.room.playerList) do
  62. if not playersInGame[player] then
  63. tfm.exec.killPlayer(player)
  64. end
  65. end
  66. tfm.exec.setGameTime(60)
  67. tfm.exec.disablePhysicalConsumables(true)
  68. end
  69.  
  70. function eventPlayerWon(player)
  71.  
  72. local function isWinner()
  73. winner = false
  74. if (points[1] >= WINS and math.abs(points[1]-points[2])>= points_away) then
  75. winner = 1
  76. elseif (points[2] >= WINS and math.abs(points[1]-points[2])>= points_away) then -- TODO: if should replace elseif to include the tie case
  77. winner = 2
  78. end
  79. if winner then
  80. gameRunning = false
  81. winTime = os.time()
  82.  
  83. ui.addTextArea(20, string.format("<p align='center'><font size='37' color='#000000'>L'équipe %s est vainqueur!\nBravo!", names[winner]), nil, 0, 171, 800, 500, 0, 0, 0, true)
  84. ui.addTextArea(21, string.format("<p align='center'><font size='37' color='#000000'>L'équipe %s est vainqueur!\nBravo!", names[winner]), nil, 1, 170, 800, 500, 0, 0, 0, true)
  85. ui.addTextArea(22, string.format("<p align='center'><font size='37'><font color='#FFFFFF'>L'équipe <font color='#%s'>%s</font> est vainqueur!\nBravo!", colors[winner], names[winner]), nil, 1, 170, 800, 500, 0, 0, 0, true)
  86. end
  87. displayScore()
  88. displayPlayerScores()
  89. return winner
  90. end
  91.  
  92. if not podium[1] then
  93. podium[1] = player
  94. if table.contains(teams[1], player) or table.contains(teams[2], player) then
  95. playerScores[player] = playerScores[player]+1 -- prevent exiting the game if a mouse finishes first while it is neither in team1 or team2
  96. end
  97. if table.contains(teams[1], player) then
  98. points[1] = points[1]+1
  99. for i = 0, 3 do
  100. defaultEffect(9, {13}, math.random(800), math.random(400), 20)
  101. end
  102. if (#teams[1] < 3 or bonus_podium == 0) and not map_ended then
  103. tfm.exec.setGameTime(5) -- only if podium is disabled
  104. map_ended = true
  105. end
  106. end
  107. if table.contains(teams[2], player) then
  108. points[2] = points[2]+1
  109. for i = 0, 3 do
  110. defaultEffect(9, {9}, math.random(800), math.random(400), 20)
  111. end
  112. if (#teams[2] < 3 or bonus_podium == 0) and not map_ended then
  113. tfm.exec.setGameTime(5) -- only if podium is disabled
  114. map_ended = true
  115. end
  116. end
  117. if (not (table.contains(teams[1], player) or table.contains(teams[2], player))) then
  118. podium[1] = nil -- only if the first mouse is not in a team (preventing mouse-killer bugs)
  119. print("Intrusive mouse spotted!")
  120. end
  121.  
  122. elseif not podium[2] then
  123. podium[2] = player
  124. if not(table.contains(teams[1], player) or table.contains(teams[2], player)) then
  125. podium[2] = nil -- only if the second mouse is not in a team (preventing mouse-killer bugs)
  126. end
  127. if ( (table.contains(teams[1], podium[1]) and table.contains(teams[2], podium[2]))
  128. or (table.contains(teams[2], podium[1]) and table.contains(teams[1], podium[2])) )
  129. and not map_ended then
  130. tfm.exec.setGameTime(5)
  131. map_ended = true
  132. end
  133.  
  134. elseif not podium[3] then
  135. podium[3] = player
  136. if not(table.contains(teams[1], player) or table.contains(teams[2], player)) then
  137. podium[3] = nil -- only if the third mouse is not in a team (preventing mouse-killer bugs)
  138. end
  139. if table.contains(teams[1], podium[1]) and table.contains(teams[1], podium[2]) and table.contains(teams[1], podium[3]) then
  140. points[1] = points[1]+bonus_podium
  141. podium_index[1] = podium_index[1]+1
  142. -- TODO: much more than just four defaultEffect ??
  143. elseif table.contains(teams[2], podium[1]) and table.contains(teams[2], podium[2]) and table.contains(teams[2], podium[3]) then
  144. points[2] = points[2]+bonus_podium
  145. podium_index[2] = podium_index[2]+1
  146. -- TODO: much more than just four defaultEffect ??
  147. end
  148. if not map_ended then
  149. tfm.exec.setGameTime(5)
  150. end
  151. end
  152.  
  153. isWinner()
  154. end
  155.  
  156. function eventLoop(elapsed, remain)
  157. if gameRunning then
  158. remain = remain/1000
  159. if remain < 0 then
  160. remain = 100
  161. newMap()
  162. end
  163. else
  164. if winTime then
  165. if winTime > os.time()-30000 then
  166. for i = 0, 2 do
  167. defaultEffect(9, {11, 9, 0, 13}, math.random(800), math.random(400), 80)
  168. end
  169. else
  170. winTime = false
  171. ui.removeTextArea(20)
  172. ui.removeTextArea(21)
  173. ui.removeTextArea(22)
  174. displayTeams()
  175. teams = {
  176. [1] = {},
  177. [2] = {},
  178. }
  179. points = {
  180. [1] = 0,
  181. [2] = 0,
  182. }
  183. playersInGame = {}
  184. podium_index = {0, 0}
  185. end
  186. end
  187. end
  188. end
  189.  
  190. function string.title(s)
  191. return string.gsub(s, "%a", function(c)
  192. return string.upper(c)
  193. end, 1)
  194. end
  195.  
  196. function eventChatCommand(player, command)
  197. -- COMMAND LIST: team1, team2, score, np, pause, wins, skip, podium
  198. if player:lower() == ADMINISTRADOR:lower() then
  199. args = string.split(command, " ")
  200. if args[1] == "team1" then
  201. table.remove(args, 1)
  202. teams[1] = {}
  203. points[1] = 0
  204. for index, player in pairs(args) do
  205. player = string.title(player)
  206. table.insert(teams[1], player)
  207. playersInGame[player] = true
  208. end
  209. displayTeams()
  210.  
  211. elseif args[1] == "team2" then
  212. table.remove(args, 1)
  213. teams[2] = {}
  214. points[2] = 0
  215. for index, player in pairs(args) do
  216. player = string.title(player)
  217. table.insert(teams[2], player)
  218. playersInGame[player] = true
  219. end
  220. displayTeams()
  221.  
  222. elseif args[1] == "score" then
  223. if args[2] and args[3] then
  224. local team = tonumber(args[2]:match("team(%d+)") or 0)
  225. if team > 0 and team < 3 then
  226. local newScore = tonumber(args[3]) or points[team]
  227. points[team] = newScore
  228. displayScore()
  229. end
  230. end
  231.  
  232. elseif args[1] == "np" then
  233. if args[2] then
  234. table.insert(queue, args[2])
  235. end
  236.  
  237. elseif args[1] == "pause" then
  238. gameRunning = not gameRunning
  239. if not gameRunning then
  240. ui.addTextArea(90, "<p align='center'><font size='25'><R>P A U S E D", nil, 0, 200, 800, nil, 0, 0, 0, true)
  241. tfm.exec.disableAutoTimeLeft(true)
  242. else
  243. ui.removeTextArea(90)
  244. tfm.exec.disableAutoTimeLeft(false)
  245. end
  246.  
  247. elseif args[1] == "wins" then
  248. -- TODO: secure this in case of non-integer string input or negative value (cases undefined)
  249. if args[2] then
  250. WINS = tonumber(args[2])
  251. end
  252.  
  253. elseif args[1] == "skip" then
  254. newMap()
  255.  
  256. elseif args[1] == "podium" then
  257. -- TODO: secure this in case of non-integer string input or negative value (cases undefined)
  258. if args[2] then
  259. bonus_podium = tonumber(args[2])
  260. else
  261. print("First argument should be an int: how many points would you add to a team in case of podium?")
  262. end
  263.  
  264. elseif args[1] == "points_away" then
  265. -- TODO: secure this in case of non-integer string input or negative value (cases undefined)
  266. if args[2] then
  267. points_away = tonumber(args[2])
  268. end
  269.  
  270. else
  271. print("Command undefined.")
  272. end
  273.  
  274. else
  275. print("You need to be set as an administrator to perform commands!")
  276. end
  277. end
  278.  
  279. function eventNewGame()
  280. if gameRunning then
  281. map_ended = false
  282. if podium[1] then
  283. podium[1] = nil
  284. end
  285. if podium[2] then
  286. podium[2] = nil
  287. end
  288. if podium[3] then
  289. podium[3] = nil
  290. end
  291. for i, p in pairs(teams[1]) do
  292. tfm.exec.setNameColor(p, "0x"..colors[1])
  293. end
  294. for i, p in pairs(teams[2]) do
  295. tfm.exec.setNameColor(p, "0x"..colors[2])
  296. end
  297. displayScore()
  298. displayPlayerScores()
  299. end
  300. end
  301.  
  302. function displayScore()
  303. ui.addTextArea(17, string.format("<p align='center'><font size='23' color='#000000'>%s x %s", points[1], points[2]), nil, 0, 21, 800, 30, 0, 0, 0, true)
  304. ui.addTextArea(18, string.format("<p align='center'><font size='23' color='#000000'>%s x %s", points[1], points[2]), nil, 1, 20, 800, 30, 0, 0, 0, true)
  305. ui.addTextArea(19, string.format("<p align='center'><font size='23'><font color='#%s'>%s<N> x <font color='#%s'>%s", colors[1], points[1], colors[2], points[2]), nil, 0, 20, 800, 30, 0, 0, 0, true)
  306. end
  307.  
  308. function displayTeams()
  309. ui.addTextArea(1, "", nil, 199, 69, 400, 260, 0x5A7A8B, 0x5A7A8B, 1, true)
  310. ui.addTextArea(2, "", nil, 201, 71, 400, 260, 0x0E1417, 0x0E1417, 1, true)
  311. ui.addTextArea(3, "", nil, 200, 70, 400, 260, 0x324650, 0x324650, 1, true)
  312. ui.addTextArea(4, "", nil, 209, 79, 142, 22, 0x324650, 0x5A7A8B, 1, true)
  313. ui.addTextArea(5, "<p align='center'><V>Team 1", nil, 210, 80, 140, 20, 0x324650, 0x324650, 1, true)
  314. ui.addTextArea(6, "", nil, 449, 79, 142, 22, 0x324650, 0x5A7A8B, 1, true)
  315. ui.addTextArea(7, "<p align='center'><V>Team 2", nil, 450, 80, 140, 20, 0x324650, 0x324650, 1, true)
  316. ui.addTextArea(8, "<p align='center'><font color='#5A7A8B'>|</font>", nil, 210, 102, 140, 200, 0, 0, 0, true)
  317. ui.addTextArea(9, "<p align='center'><font color='#5A7A8B'>|</font>", nil, 450, 102, 140, 200, 0, 0, 0, true)
  318. ui.addTextArea(10, "", nil, 209, 120, 140, 200, 0x5A7A8B, 0x5A7A8B, 1, true)
  319. ui.addTextArea(11, "<p align='center'><font color='#"..colors[1].."'>"..table.concat(teams[1], "\n"), nil, 210, 121, 138, 198, 0x324650, 0x324650, 1, true)
  320. ui.addTextArea(12, "", nil, 451, 120, 140, 200, 0x5A7A8B, 0x5A7A8B, 1, true)
  321. ui.addTextArea(13, "<p align='center'><font color='#"..colors[2].."'>"..table.concat(teams[2], "\n"), nil, 452, 121, 138, 198, 0x324650, 0x324650, 1, true)
  322. ui.addTextArea(14, "<p align='center'><font color='#5A7A8B'>____ ____</font>", nil, 330, 200, 140, 200, 0, 0, 0, true)
  323. ui.addTextArea(15, "", nil, 380, 202, 40, 20, 0x5A7A8B, 0x5A7A8B, 1, true)
  324. ui.addTextArea(16, "<p align='center'><V>VS", nil, 381, 203, 38, 18, 0x324650, 0x324650, 1, true)
  325. ui.addTextArea(16, "<p align='center'><V><a href='event:iniciarJogo'>GO", ADMINISTRADOR, 381, 203, 38, 18, 0x324650, 0x324650, 1, true)
  326. end
  327.  
  328. function displayPlayerScores()
  329. -- TODO: TextAreas ids should be change to fit the rest of the code
  330. local s1 = ""
  331. local s2 = ""
  332. for _,v in pairs(teams[1]) do
  333. s1 = s1 .. v .. " : " .. playerScores[v] .. "\n"
  334. end
  335. for _,v in pairs(teams[2]) do
  336. s2 = s2 .. v .. " : " .. playerScores[v] .. "\n"
  337. end
  338. if #teams[1] >= 3 then
  339. s1 = s1 .. "Podiums" .. " : " .. podium_index[1]
  340. end
  341. if #teams[2] >= 3 then
  342. s2 = s2 .. "Podiums" .. " : " .. podium_index[2]
  343. end
  344. s1 = "<font size='12' color='#"..colors[1].."'>"..s1
  345. s2 = "<font size='12' color='#"..colors[2].."'>"..s2
  346. ui.addTextArea(123, s1, nil, -150, 30, 0x324650, colors[1], 0, true)
  347. ui.addTextArea(124, s2, nil, 850, 30, 0x324650, colors[2], 0, true)
  348. end
  349.  
  350. function eventTextAreaCallback(id, player, callback)
  351. if callback == 'iniciarJogo' then
  352. if #teams[2] > 0 and #teams[1] > 0 then
  353. for name,bool in pairs(playersInGame) do
  354. if bool then
  355. playerScores[name] = 0
  356. end
  357. end
  358. gameRunning = true
  359. for i = 1, 16 do
  360. ui.removeTextArea(i)
  361. end
  362. defaultEffect(9, {9}, 400, 212, 80)
  363. newMap()
  364. end
  365. end
  366. end
  367.  
  368. function string.split(s, pattern, n)
  369. local st = {}
  370. for sb in string.gmatch(s, "[^"..pattern.."]+") do
  371. if not n or n > -1 then
  372. table.insert(st,sb)
  373. else
  374. st[#st] = st[#st]..pattern..sb
  375. end
  376. n = n and n-1 or false
  377. end
  378. return st
  379. end
  380.  
  381. function table.contains(tableT, element)
  382. for _, value in pairs(tableT) do
  383. if value == element then
  384. return true
  385. end
  386. end
  387. return false
  388. end
  389.  
  390. defaultEffect=function(id,p,x,y,rand) -- thanks for the function santah
  391. local minDist = 1
  392. local outerBorder = 20
  393. local maxDist = 30
  394. local totalParticles = rand and 40 or (id == -1 and 35 or 75)
  395. for i = 1, totalParticles do
  396. if rand then
  397. id = p[math.random(#p)]
  398. end
  399. local dist = math.min(math.random(minDist, maxDist), outerBorder)
  400. local angle = math.random(0, 360)
  401. local r = math.rad(angle)
  402. local dx = math.cos(r)
  403. local dy = math.sin(r)
  404. local vx = dist * dx / 10
  405. local vy = dist * dy / 10
  406. local ax = -vx / dist / 15
  407. local ay = (-vy / dist / 15) + 0.05
  408. if id == -1 then
  409. tfm.exec.displayParticle(9, x + dx, y + dy, vx, vy, ax, ay, nil)
  410. tfm.exec.displayParticle(1, x + dx, y + dy, vx, vy, ax, ay, nil)
  411. else
  412. tfm.exec.displayParticle(id, x + dx, y + dy, vx, vy, ax, ay, nil)
  413. end
  414. end
  415. end
  416.  
  417. displayTeams()
  418.  
  419. function eventPlayerDied(playerName)
  420. local alive = 0
  421. for k,v in pairs(tfm.get.room.playerList) do
  422. if not v.isDead then
  423. alive = alive+1
  424. end
  425. end
  426. if alive == 0 and not map_ended then
  427. tfm.exec.setGameTime(5)
  428. map_ended = true
  429. end
  430. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement