Mousetat

Hot Potato by Eshkation (WARNING: quite some bugs)

Jan 7th, 2017
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.48 KB | None | 0 0
  1. tfm.exec.disableAutoNewGame()
  2. tfm.exec.disableAutoShaman()
  3. tfm.exec.disableAfkDeath()
  4.  
  5. _TIMERS = {
  6. }
  7. _PLAYERS = {}
  8. _MODULES = {
  9. configPlayer = {
  10. running = true,
  11. callbacks = {
  12. on_eventNewPlayer = function(player)
  13. for _, key in pairs({0, 1, 2, 3, 32}) do
  14. system.bindKeyboard(player, key, true, true)
  15. end
  16. end
  17. }
  18. },
  19. tagging = {
  20. running = false,
  21. mapWidth = 17,
  22. mapHeight = 17,
  23. diffX = 640,
  24. blockArea = 40,
  25. players = {},
  26. board = {},
  27. colors = {[0] = "FA5956","F55E56","F06356","EB6856","E76D56","E27256","DD7856","D87D56","D48256","CF8756","CA8C56","C59256","C19756","BC9C56","B7A156","B2A656","AEAC56","A9B156","A4B656","9FBB56","9BC056","96C656","91CB56","8CD056","88D556","83DA56","7EE056","79E556","75EA56","70EF56","6BF456"},
  28. userHoldingPotato = "",
  29. sprites = {
  30. [1] = {
  31. south = "1553d3f187c",
  32. east = "1553d3f5870",
  33. west = "1553d570f97",
  34. north = "1553d57aef5",
  35. offsetX = 23,
  36. offsetY = 0
  37. },
  38. [2] = {
  39. south = "15541318d67",
  40. east = "1554131c12c",
  41. west = "155412984a5",
  42. north = "15541289c9c",
  43. offsetX = 23,
  44. offsetY = 0
  45. }
  46. },
  47. tiles = {
  48. [1] = {
  49. url = "1553c658200.png",
  50. offsetX = 0,
  51. offsetY = -2
  52. },
  53. [2] = {
  54. url = "1553c3d754d.png",
  55. offsetX = 1,
  56. offsetY = 1
  57. },
  58. [3] = {
  59. url = "1554c73efa8.png",
  60. offsetX = -78,
  61. offsetY = 31
  62. }
  63. },
  64. removeTileTimer = os.time(),
  65. callbacks = {
  66. on_startModule = function()
  67. local self = _MODULES.tagging
  68. tfm.exec.newGame("@6711195")
  69. self.roomPotatoTimer = os.time()
  70. self.callbacks.initializeBoard()
  71. end,
  72. on_eventNewPlayer = function()
  73. local self = _MODULES.tagging
  74. self.callbacks.displayBoard(nil)
  75. end,
  76. on_eventLoop = function()
  77. local self = _MODULES.tagging
  78. if self.gameRunning and self.players[self.userHoldingPotato].alive then
  79. local potatoTimer = 30-math.ceil((os.time()-self.roomPotatoTimer)/1000)
  80. local color = self.colors[potatoTimer]
  81. if potatoTimer < 5 then
  82.  
  83. end
  84. ui.addTextArea(-1, "<font color='#000000' font='Courier' size='20'>"..potatoTimer, nil, 16, 31, nil, nil, 0, 0, 0, true)
  85. ui.addTextArea(-2, "<font color='#"..color.."' font='Courier' size='20'>"..potatoTimer, nil, 15, 30, nil, nil, 0, 0, 0, true)
  86. if potatoTimer == 0 then
  87. self.callbacks.killPlayerBlock(self.userHoldingPotato)
  88. self.roomPotatoTimer = os.time()
  89. end
  90. --[[if self.removeTileTimer < os.time()-3000 then
  91. for i = 1, self.mapWidth do
  92. for j = 1, self.mapHeight do
  93. if self.board[i][j].hasBlock and self.board[i][j].player == "" then
  94. self.board[i][j].hasBlock = false
  95. self.removeTileTimer = os.time()
  96. self.board[i][j].deleted = true
  97. for k, v in pairs(self.board[i][j].spriteId) do
  98. tfm.exec.removeImage(v)
  99. end
  100. return
  101. end
  102. end
  103. end
  104. end]]--
  105. end
  106. end,
  107. killPlayerBlock = function(player)
  108. local self = _MODULES.tagging
  109. local data = self.players[player]
  110. data.alive = false
  111. tfm.exec.removeImage(data.imageId)
  112. ui.removeTextArea(data.textId)
  113. tfm.exec.killPlayer(player)
  114. local aliveBlocks = self.callbacks.getAliveBlocks()
  115. if player == self.userHoldingPotato then
  116. if #aliveBlocks > 1 then
  117. local newPotato = table.randomKey(aliveBlocks)
  118. local str = string.translate("userPotatoEliminated", {username = self.userHoldingPotato, newUser = newPotato})
  119. tfm.exec.chatMessage(str)
  120. self.callbacks.updatePlayerPotato(newPotato)
  121. else
  122. local winner = aliveBlocks[1]
  123. local str = string.translate("gameWon", {username = self.userHoldingPotato, won = winner})
  124. tfm.exec.chatMessage(str)
  125. end
  126. end
  127. end,
  128. getAliveBlocks = function()
  129. local self = _MODULES.tagging
  130. local t = {}
  131. for player, data in pairs(self.players) do
  132. if data.alive then
  133. table.insert(t, player)
  134. end
  135. end
  136. return t
  137. end,
  138. on_eventKeyboard = function(player, key, down, x, y)
  139. local self = _MODULES.tagging
  140. if self.players[player] and self.players[player].alive and self.players[player].movementTimer < os.time()-150 and self.gameRunning then
  141. self.players[player].movementTimer = os.time()
  142. local diffX = 0
  143. local diffY = 0
  144. local looking = "south"
  145. if key == 0 then
  146. diffX = -1
  147. diffY = 0
  148. looking = "west"
  149. elseif key == 1 then
  150. diffX = 0
  151. diffY = -1
  152. looking = "north"
  153. elseif key == 2 then
  154. diffX = 1
  155. diffY = 0
  156. looking = "east"
  157. elseif key == 3 then
  158. diffX = 0
  159. diffY = 1
  160. looking = "south"
  161. else
  162. return
  163. end
  164. if diffX == 0 and diffY == 0 then
  165. return
  166. end
  167. local posX = self.players[player].x+diffX
  168. local posY = self.players[player].y+diffY
  169. if self.board[posX] and self.board[posX][posY] and self.board[posX][posY].hasBlock then
  170. if self.board[self.players[player].x][self.players[player].y].player == player then
  171. self.board[self.players[player].x][self.players[player].y].player = ""
  172. end
  173. self.players[player].y = posY
  174. self.players[player].x = posX
  175. if not (player == self.userHoldingPotato) then
  176. self.board[self.players[player].x][self.players[player].y].player = player
  177. end
  178. self.players[player].looking = looking
  179. self.callbacks.updatePlayerImage(player)
  180. if player == self.userHoldingPotato then
  181. local tile = self.board[self.players[player].x][self.players[player].y]
  182. if tile.player ~= "" and self.players[tile.player].canCatch < os.time()-5000 then
  183. self.players[player].canCatch = os.time()
  184. self.callbacks.updatePlayerPotato(tile.player)
  185. self.callbacks.updatePlayerImage(player)
  186. self.callbacks.updatePlayerImage(tile.player)
  187. local str = string.translate("potatoSwapped", {username = player, newUser = tile.player})
  188. tfm.exec.chatMessage(str)
  189. end
  190. end
  191. end
  192. end
  193. end,
  194. displayExplosion = function()
  195. local particles = {"15545c58f26", "15545c6aa79", "15545c7327f", "15545c7d1a1", "15545c85e31", "15545c8f98d"}
  196. end,
  197. initializeBoard = function()
  198. local self = _MODULES.tagging
  199. local canVertical = false
  200. local canHorizontal = false
  201. local spawningPlaces = {}
  202. self.board = {}
  203. for i = 1, self.mapWidth do
  204. self.board[i] = {}
  205. canHorizontal = not canHorizontal
  206. for j = 1, self.mapHeight do
  207. self.board[i][j] = {
  208. hasBlock = true,
  209. depth = {},
  210. player = "",
  211. spriteId = {}
  212. }
  213. if i == 1 or i == self.mapWidth or j == 1 or j == self.mapHeight then
  214. self.board[i][j].hasBlock = false
  215. else
  216. canVertical = not canVertical
  217. if canVertical and canHorizontal then
  218. self.board[i][j].hasBlock = false
  219. else
  220. table.insert(spawningPlaces, {i,j})
  221. end
  222. end
  223. end
  224. end
  225. self.callbacks.displayBoard(nil)
  226. local uniqueId = 0
  227. local players = {}
  228. for player, data in pairs(tfm.get.room.playerList) do
  229. local coords = table.randomKey(spawningPlaces)
  230. uniqueId = uniqueId+1
  231. self.players[player] = {
  232. x = coords[1],
  233. y = coords[2],
  234. looking = "south",
  235. imageId = -1,
  236. potatoTimer = false,
  237. movementTimer = os.time(),
  238. textId = uniqueId,
  239. alive = true,
  240. canCatch = os.time()
  241. }
  242. table.insert(players, player)
  243. self.callbacks.updatePlayerImage(player)
  244. end
  245. local p = table.randomKey(players)
  246. self.callbacks.updatePlayerPotato(p)
  247. self.gameRunning = true
  248. local str = string.translate("userPotato", {username = p})
  249. tfm.exec.chatMessage(str)
  250. end,
  251. updatePlayerPotato = function(player)
  252. local self = _MODULES.tagging
  253. self.userHoldingPotato = player
  254. self.players[self.userHoldingPotato].potatoTimer = os.time()
  255. self.callbacks.updatePlayerImage(self.userHoldingPotato)
  256. end,
  257. updatePlayerImage = function(player)
  258. local self = _MODULES.tagging
  259. local data = self.players[player]
  260. local spriteId = 1
  261. if self.userHoldingPotato == player then
  262. spriteId = 2
  263. end
  264. local isometric = math.toIso(data.x, data.y, self.blockArea)
  265. local tile = self.board[data.x][data.y]
  266. local imgData = self.sprites[spriteId]
  267. tfm.exec.removeImage(data.imageId)
  268. data.imageId = tfm.exec.addImage(imgData[data.looking]..".png", "!8000", self.diffX+isometric.x+imgData.offsetX, isometric.y+imgData.offsetY, nil)
  269. --local playerX = 400*(math.floor((self.diffX+isometric.x)/400))+280
  270. --local playerY = 200*(math.floor(isometric.y/200))+120
  271. --playerY = playerY > 680 and 660 or playerY
  272. tfm.exec.movePlayer(player, self.diffX+isometric.x, isometric.y, false, 0, 0, false)
  273. local text = string.format("«%s»", player)
  274. ui.addTextArea(data.textId, "<font color='#FFF3CE' font='Courier'>"..text, nil, (self.diffX+isometric.x+(imgData.offsetX*2)-5)-((string.len(text)*6)/2), (isometric.y+imgData.offsetY)-15, nil, nil, 0x1, 0x0, 0, false)
  275. ui.addTextArea(data.textId, "<font color='#6FFD57' font='Courier'>"..text, player, (self.diffX+isometric.x+(imgData.offsetX*2)-5)-((string.len(text)*6)/2), (isometric.y+imgData.offsetY)-15, nil, nil, 0x1, 0x0, 0, false)
  276.  
  277. end,
  278. displayBoard = function(player)
  279. local self = _MODULES.tagging
  280. local coroutineBuilder = coroutine.create(
  281. function()
  282. for i = self.mapWidth, 1, -1 do
  283. for j = self.mapHeight, 1, -1 do
  284. local index = 1
  285. if self.board[i][j].hasBlock then
  286. index = 2
  287. end
  288. local isometric = math.toIso(i, j, self.blockArea)
  289. local tile = self.tiles[index]
  290. table.insert(self.board[i][j].spriteId, tfm.exec.addImage(tile.url, "!4", self.diffX+isometric.x+tile.offsetX, isometric.y+tile.offsetY, nil))
  291. end
  292. coroutine.yield()
  293. end
  294. end)
  295. _TIMERS["board"..tostring(player)] = loop(function()
  296. local status, result = coroutine.resume(coroutineBuilder)
  297. if result then
  298. if _TIMERS["board"..tostring(player)] then
  299. for _, timerId in pairs(_TIMERS["board"..tostring(player)]) do
  300. system.removeTimer(timerId)
  301. end
  302. end
  303. end
  304. return true
  305. end, 5)
  306. end
  307. }
  308. }
  309. }
  310.  
  311. function removePlayerFromTile(tile, player)
  312. for index, depth in pairs(tile.depth) do
  313. if depth.type == "player" and depth.id == player then
  314. table.remove(tile.depth, index)
  315. break
  316. end
  317. end
  318. end
  319.  
  320. function loop(fnc, ticks)
  321. local s = 1000/ticks
  322. local timers = {}
  323. for t = 0, 1000 - s, s do
  324. system.newTimer(function() table.insert(timers, system.newTimer(fnc, 1000, true)) end, 1000 + t, false)
  325. end
  326. return timers
  327. end
  328.  
  329. function table.getKeys(rawTable)
  330. keys = {}
  331. for index in pairs(rawTable) do
  332. table.insert(keys, index)
  333. end
  334. return keys
  335. end
  336.  
  337. function table.randomKey(rawTable)
  338. return rawTable[math.random(#rawTable)]
  339. end
  340.  
  341. function math.toIso(x, y, area)
  342. local x2 = x-y
  343. local y2 = (x+y)/2
  344. return {x = x2*area, y = y2*area}
  345. end
  346.  
  347. function math.toCart(x, y, area)
  348. local x2 = ((2*y+x)/2)/area
  349. local y2 = ((2*y-x)/2)/area
  350. return {x = math.ceil(x2), y = math.ceil(y2)}
  351. end
  352.  
  353. _MESSAGES = {
  354. br = {
  355. userPotato = "{username} pegou a batata, fujam!",
  356. userPotatoEliminated = "{username} foi eliminado, {newUser} pegou a batata!",
  357. gameWon = "{username} foi eliminado, {won} é o vencedor!",
  358. potatoSwapped = "{username} passou a batata para {newUser}!",
  359. },
  360. en = {
  361. userPotato = "{username} got the potato, run!",
  362. userPotatoEliminated = "{username} has been eliminated, {newUser} has the potato!",
  363. gameWon = "{username} has been eliminated, {won} is the winner!",
  364. potatoSwapped = "{username} gave the potato to {newUser}!",
  365. }
  366. }
  367.  
  368. function string.translate(key, args)
  369. local langue = tfm.get.room.community
  370. local translatedString = _MESSAGES[langue] or _MESSAGES.en
  371. translatedString = translatedString[key] or key
  372. return "<G>"..string.gsub(translatedString, "%{([A-Za-z0-9]+)%}", function(s)
  373. return "<BV>"..(args[s] or s).."</BV>"
  374. end)
  375. end
  376.  
  377.  
  378. function INIT(registeredModules)
  379. local pipes = {"eventNewPlayer", "eventNewGame", "eventKeyboard", "eventMouse", "eventLoop"}
  380. for _, moduleEvent in pairs(pipes) do
  381. _G[moduleEvent] = function(...)
  382. local args = {...}
  383. for _, moduleName in pairs(registeredModules) do
  384. if _MODULES[moduleName].running and _MODULES[moduleName].callbacks["on_"..moduleEvent] then
  385. _MODULES[moduleName].callbacks["on_"..moduleEvent](table.unpack(args))
  386. end
  387. end
  388. end
  389. end
  390. for player, data in pairs(tfm.get.room.playerList) do
  391. eventNewPlayer(player)
  392. print(player)
  393. end
  394. initNewModule()
  395. end
  396.  
  397. function initNewModule()
  398. local registeredModules = {"tagging"}
  399. local randomModule = table.randomKey(registeredModules)
  400. _MODULES[randomModule].running = true
  401. _MODULES[randomModule].callbacks.on_startModule()
  402. end
  403.  
  404. local registeredModules = {"tagging", "configPlayer"}
  405. INIT(registeredModules)
Advertisement
Add Comment
Please, Sign In to add comment