suxonov

Untitled

May 21st, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.90 KB | None | 0 0
  1. --[[To-do list:
  2.     • Map rotation; --almost done (no)
  3.     • remove guide message to shaman on divine mode
  4.     • finish round if shaman has left
  5.     • disable skills
  6.     • if mice are won shaman is dead
  7.     • use towers[key].speed
  8.     • voting system to skip map (?) ]]
  9.  
  10. --[[    keys
  11.     C   67
  12.     V   86
  13.     B   66
  14.     N   78
  15.    
  16.     Q   81
  17.     W   87
  18.     E   69
  19.     Ct  17
  20.     H   72 ]]--
  21.  
  22. tfm.exec.disableAutoNewGame(true)
  23. tfm.exec.disableAutoTimeLeft(true)
  24. tfm.exec.disableAfkDeath(true)
  25.  
  26. players={}
  27. toDesp = {}
  28. timerList = {}
  29. towerdefense = {
  30.     version = '<J>Towerdefense <T>BETA_1';
  31.  
  32.     towers = {
  33.         [67] = {call = 'Cannon Tower'; price = 6; obj = 17};
  34.         [86] = {call = 'Ball Tower'; price = 3; obj = 6};
  35.         [66] = {call = 'Spirit Tower'; price = 3, obj = 24}
  36.     };
  37.    
  38.     maps = {
  39.         1387919;
  40.         1675985;
  41.         1617965;
  42.         1565707;
  43.         1395396;
  44.         1415547;
  45.         4932803;
  46.         2365986;
  47.     };
  48.    
  49.     coords = {
  50.     -- [''] = {x = ; y = }
  51.         ['@1387919'] = {x = 25; y = 329};
  52.         ['@1675985'] = {x = 27; y = 340};
  53.         ['@1617965'] = {x = 23; y = 340};
  54.         ['@1565707'] = {x = 22; y = 345};
  55.         ['@1395396'] = {x = 23; y = 279};
  56.         ['@1415547'] = {x = 35; y = 328};
  57.         ['@4932803'] = {x = 180; y = 340};
  58.         ['@2365986'] = {x = 40; y = 345};
  59.     };
  60.  
  61.     Shamankeys = {
  62.         67, 86, 66, 78, 17, 72
  63.     };
  64.     keys = {
  65.         17, 72
  66.     };
  67.    
  68.     messages = {
  69.         chat_welcome = 'Welcome to <J>%s<BL>! Press <J>[H]<BL> for more information!';
  70.         chat_help = 'If you\'re the <CH>shaman<bl> defense the cheese! Press <V>C, V, B<BL> to buy the tower, summon the arrow to place it on the map! Press <V>[Ctrl]<BL> to get more information about towers!\nEvery <V>6 seconds<BL> you\'ll get <V>1 point<BL>!\n\nIf you\'re the <V>mouse<BL> try to get the cheese!';
  71.         chat_towerTab = '<J>Tower               Key     Price\n<T>Cannon tower       <V>C          <V>6\n<T>Ball tower           <V>B          <V>3\n<T>Spirit tower         <V>N          <V>3';
  72.         chat_lessP = '<R>You haven\'t enough points to buy this tower!';
  73.         chat_Swarning = '<R>You must turn off the divine mode to play this module as the shaman!';
  74.         chat_buy = 'You have bought <T>%s';
  75.         chat_angle = 'Angle: <V>%x°';
  76.         chat_shaman = 'You\'re the <CH>Shaman<BL>! Defense the <J>cheese<BL>! You have <V>10 seconds<BL> to place the towers before 1st wave starts!';
  77.         chat_sWon = '<CH>Shaman<BL> has won!';
  78.         chat_mWon = '<V>Mice<BL> have won!';
  79.     };
  80. }
  81.  
  82. module = {
  83.     INIT = function()
  84.         mice_won = false
  85.         module.newGame()
  86.     end;
  87.     newGame = function()
  88.         tfm.exec.newGame(towerdefense.maps[math.random(#towerdefense.maps)])
  89.     end;
  90.     sendMessage = function(m, name)
  91.         tfm.exec.chatMessage(m, name)
  92.     end;
  93.     buyTower = function(key, name)
  94.         if towerdefense.towers[key] ~= nil then
  95.             local points = players[name].points
  96.             local price = towerdefense.towers[key].price
  97.                 if points >= price then
  98.                     points = points - price
  99.                     players[name] = {points = points, key = towerdefense.towers[key].obj, angle = 0}
  100.                     module.sendMessage(string.format(towerdefense.messages.chat_buy, towerdefense.towers[key].call), name)
  101.                     ui.updateTextArea(0, "<b><p align = 'center'>Points: "..players[name].points.."</p>", name)
  102.                 else
  103.                     module.sendMessage(towerdefense.messages.chat_lessP, name)
  104.                 end
  105.         end
  106.     end;
  107.     setTower = function(name, x, y, angle)
  108.         local id = players[name].key
  109.         local xS = 50
  110.         local xY = 50
  111.         players[name].timest = os.time()
  112.         if id == 17 and angle == 0 then
  113.             angle = 180;
  114.         elseif id == 17 and angle ~= 0 then
  115.             angle = -angle
  116.         elseif id == 6 then
  117.             xS = 20
  118.             yS = 20
  119.         end
  120.         module.addTimer(function() local object = tfm.exec.addShamanObject(id, x, y, angle, xS, yS) table.insert(toDesp,{os.time(),object}) end, 2500, 1000)
  121.         players[name].key = 0
  122.     end;
  123.     spawnPlayers = function()
  124.         local i = tfm.get.room.currentMap
  125.         if towerdefense.coords[i] ~= nil then
  126.             for name,players in pairs(tfm.get.room.playerList) do
  127.                 if not players.isShaman then
  128.                     tfm.exec.movePlayer(name, towerdefense.coords[i].x, towerdefense.coords[i].y)
  129.                 end
  130.             end
  131.         end
  132.     end;
  133.     respawnPlayers = function()
  134.         local i = tfm.get.room.currentMap
  135.         for name,player in pairs(tfm.get.room.playerList) do
  136.             if not player.isShaman and player.isDead then
  137.                 tfm.exec.respawnPlayer(name)
  138.                 tfm.exec.movePlayer(name, towerdefense.coords[i].x, towerdefense.coords[i].y)
  139.             end
  140.         end
  141.     end;
  142.     addPoints = function()
  143.         for name,player in pairs(tfm.get.room.playerList) do
  144.             if player.isShaman and started == true then
  145.                 players[name].points = players[name].points + 1
  146.                 ui.updateTextArea(0, "<b><p align = 'center'>Points: "..players[name].points.."</p>", name)
  147.             end
  148.         end
  149.     end;
  150.     addTimer = function(callback, time, loops, label, ...) -- thanks to Eshkation for help and source
  151.         local id = #timerList + 1
  152.         timerList[id] = {
  153.             callback = callback;
  154.             time = time;
  155.             loops = loops or 1;
  156.             label = label;
  157.             currentTime = 0;
  158.             currentLoop = 0;
  159.             isComplete = false;
  160.             arguments = {...};
  161.         }
  162.         return id
  163.     end;
  164.     removeTimer = function(id)
  165.         if timerList[id] ~= nil then
  166.             timerList[id] = 0
  167.             return true
  168.         end
  169.         return false
  170.     end;
  171.     removeAllTimers = function ()
  172.         timerList = {}
  173.     end;       
  174. }
  175.     eventNewGame = function()
  176.         mice = 0
  177.         n = 0
  178.         seconds = 10
  179.         mice_won = false
  180.         module.removeAllTimers()
  181.         module.addTimer(module.addPoints, 8000, 1000) -- +1 point
  182.         module.addTimer(module.respawnPlayers, 5000, 1000) -- respawn
  183.         tfm.exec.setGameTime(180)
  184.         for k,v in pairs(tfm.get.room.playerList) do
  185.             mice = mice + 1
  186.         end
  187.         print('bind == true')
  188.         for name,player in pairs(tfm.get.room.playerList) do
  189.             if player.isShaman and tfm.get.room.playerList[name].shamanMode ~= 2 then
  190.                 module.sendMessage(towerdefense.messages.chat_shaman, name)
  191.                 for i,key in ipairs(towerdefense.Shamankeys) do
  192.                     system.bindKeyboard(name, key, true, true)
  193.                 end
  194.                 players[name] = {points = 10, key = 0, timest = os.time()}
  195.                 ui.addTextArea(0, "<b><p align = 'center'>Points: "..players[name].points.."</p>", name, -18, 380, 100, 100, 0x324650, 0x0, 0, true)
  196.             elseif player.isShaman and tfm.get.room.playerList[name].shamanMode == 2 then
  197.                 module.sendMessage(towerdefense.messages.chat_Swarning)
  198.                 tfm.exec.setGameTime(0)
  199.             end
  200.             if not player.isShaman then
  201.                 ui.removeTextArea(0)
  202.                 players[name] = {}
  203.                 for key,s in ipairs(towerdefense.keys) do
  204.                     system.bindKeyboard(name, s, true, true)
  205.                 end
  206.             end
  207.         end
  208.     end;
  209.     eventNewPlayer = function(name)
  210.         players[name] = {}
  211.         for i,key in ipairs(towerdefense.keys) do
  212.             system.bindKeyboard(name, key, true, true)
  213.         end
  214.         module.sendMessage(string.format(towerdefense.messages.chat_welcome, towerdefense.version), name)
  215.     end;
  216.     eventKeyboard = function(name, keyc, down, x, y)
  217.             if keyc == 17 then
  218.                 module.sendMessage(towerdefense.messages.chat_towerTab, name)
  219.             elseif keyc == 72 then
  220.                 module.sendMessage(towerdefense.messages.chat_help, name)
  221.             elseif players[name].key == 0 then
  222.                 module.buyTower(keyc, name)
  223.             end
  224.     end;
  225.     eventSummoningEnd = function (name, id, x, y, angle)
  226.         if players[name].key ~= 0 then
  227.             module.setTower(name, x, y, angle)
  228.         end
  229.     end;
  230.     function eventPlayerGetCheese (name)
  231.         tfm.exec.playerVictory(name)
  232.     end;
  233.     function eventPlayerWon(name)
  234.             n = n + 1
  235.             i = math.ceil(mice/2)
  236.             module.sendMessage('<V>'..name..' <N>got the cheese! (<V>'..n..'/'..i..'<N>)', nil)
  237.             if n >= i then
  238.                 module.sendMessage(towerdefense.messages.chat_mWon, nil)
  239.                 tfm.exec.setGameTime(10)
  240.                 started = false
  241.                 for name,player in pairs(tfm.get.room.playerList) do
  242.                     if player.isShaman then
  243.                         players[name] = {points = 0, key = 0}
  244.                     else
  245.                         players[name] = {}
  246.                     end
  247.                 end
  248.             end
  249.     end;       
  250.     function eventLoop(time, rem)
  251.         seconds = seconds - 0.5
  252.         if seconds == 0 then
  253.             module.spawnPlayers()
  254.         end
  255.         if time >= 10000 and started == false then
  256.             started = true
  257.         end
  258.         if rem <= 0 and mice_won == true then
  259.             module.newGame()
  260.         elseif rem <= 0 and mice_won == false then
  261.             module.sendMessage(towerdefense.messages.chat_sWon, nil)
  262.             module.newGame()
  263.         end
  264.         for k,obj in ipairs(toDesp) do
  265.             if obj[1] <= os.time() - 2500 then
  266.                 tfm.exec.removeObject(obj[2])
  267.                 table.remove(toDesp,k)
  268.             end
  269.         end
  270.         -- timer
  271.         local timersToRemove = {}
  272.         for id = 1, #timerList do
  273.             local timer = timerList[id]
  274.             if type(timer) == 'table' then
  275.                 if not timer.isComplete then
  276.                     timer.currentTime = timer.currentTime + 500
  277.                     if timer.currentTime >= timer.time then
  278.                         timer.currentTime = 0
  279.                         timer.currentLoop = timer.currentLoop + 1
  280.                         if timer.loops > 0 then
  281.                             if timer.currentLoop >= timer.loops then
  282.                                 timer.isComplete = true
  283.                             end
  284.                         end
  285.                         if type(timer.callback) == 'function' then
  286.                             timer.callback(timer.currentLoop, table.unpack(timer.arguments))
  287.                         end
  288.                     end
  289.                 end
  290.                 if timer.isComplete then
  291.                     if type(eventTimerComplete) == 'function' then
  292.                         eventTimerComplete(id, timer.label)
  293.                     end
  294.                     timersToRemove[#timersToRemove+1] = id
  295.                 end
  296.             end
  297.         end
  298.     end;
  299.  
  300. module.INIT()
Advertisement
Add Comment
Please, Sign In to add comment