Advertisement
Guest User

Untitled

a guest
Dec 24th, 2014
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.47 KB | None | 0 0
  1. dofile('data/libs/TWD/towerDefenseSpellsArea.lua')
  2. dofile('data/libs/TWD/towerDefenseConfig.lua')
  3.  
  4. targetTurret = nil
  5. turretPosition = nil
  6.  
  7. local twdEvents = {
  8.     "TWDOnLose",
  9.     "TWDBuildWindow",
  10.     "TWDOtherWindow",
  11.     "TWDHealthChange"
  12. }
  13.  
  14. function Player.resetValues(self)
  15.     self:removeItem(2557, 1)
  16.     self:setStorageValue(coinStorage, 0)
  17.     self:addHealth(self:getMaxHealth())
  18.     self:setStorageValue(playingGameStorage, 0)
  19.     self:teleportTo(self:getTown():getTemplePosition())
  20.     for i = 1, #twdEvents do
  21.         self:unregisterEvent(twdEvents[i])
  22.     end
  23. end
  24.  
  25. function sendReward(cid)
  26.     local player = Player(cid)
  27.     player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have won the Tower Of Defense Event.")
  28.     player:addItem(2160, 10)
  29.     player:resetValues()
  30. end
  31.  
  32. function resetEvent()
  33.     turretPosition = nil
  34.     targetTurret = nil
  35.     setWaveLevel(0)
  36.     Game.setStorageValue(totalMonsterKillCountGlobalStorage, 0)
  37.     Game.setStorageValue(totalMonsterCountGlobalStorage, 0)
  38.  
  39.     local specs, turrets = Game.getSpectators(eventCenterPosition, false, false, 40, 40, 40, 40)
  40.     for i = 1, #specs do
  41.         turrets = specs[i]
  42.         if turrets:isNpc() and turrets:getName() == "Turret" then
  43.             turrets:remove()
  44.         end
  45.     end
  46. end
  47.  
  48. -- Everytime the monster need to turn, you have to write the position of the turning point and where it should walk after it reached the point.
  49. local walkPaths = {
  50.     Position(1003, 1076, 7),
  51.     Position(1007, 1076, 7),
  52.     Position(1007, 1085, 7),
  53.     Position(998, 1085, 7),
  54.     Position(998, 1091, 7),
  55.     Position(1009, 1091, 7),
  56.     Position(1009, 1096, 7),
  57.     Position(1014, 1096, 7),
  58.     Position(1014, 1091, 7),
  59.     Position(1021, 1091, 7),
  60.     Position(1021, 1079, 7),
  61.     Position(1018, 1079, 7),
  62.     Position(1018, 1075, 7)
  63. }
  64.  
  65. local function monsterWalkTo(cid, fromPos, toPos, state) -- Limos
  66.     local toPosState = toPos[state]
  67.     if not toPosState then
  68.         return false
  69.     end
  70.  
  71.     if fromPos.y == toPosState.y then
  72.         fromPos.x = fromPos.x > toPosState.x and fromPos.x - 1 or (fromPos.x < toPosState.x and fromPos.x + 1 or fromPos.x)
  73.          else
  74.         fromPos.y = fromPos.y > toPosState.y and fromPos.y - 1 or (fromPos.y < toPosState.y and fromPos.y + 1 or fromPos.y)
  75.          end
  76.  
  77.     local monster = Monster(cid)
  78.     if not monster then
  79.         return false
  80.     end
  81.  
  82.          monster:teleportTo(fromPos, true)
  83.          if fromPos.x == toPosState.x and fromPos.y == toPosState.y then
  84.              state = state + 1
  85.          end
  86.  
  87.     local speed = monsters[monster:getName()].speed
  88.     if not speed then
  89.         speed = 0
  90.     end
  91.  
  92.          addEvent(monsterWalkTo, 1000 - speed, cid, fromPos, toPos, state)
  93. end
  94.  
  95. function Npc.searchTarget(self, xRange, yRange)
  96.     local target = self:getTarget()
  97.     local specs, creatures = Game.getSpectators(self:getPosition(), false, false, xRange, xRange, yRange, yRange)
  98.     for i = 1, #specs do
  99.         if target then -- We already have a target, which is in range. Let's break the loop then
  100.             break
  101.         end
  102.  
  103.         creatures = specs[i]
  104.         if creatures:isMonster() then -- Let's pick a target, which is a monster
  105.             return self:setTarget(creatures)
  106.         end
  107.     end
  108. end
  109.  
  110. function Npc.shootSpell(self, attackType, target, combat, area, min, max, magicEffect, distEffect)
  111.     if attackType == "aoe" then
  112.         doAreaCombatHealth(self, combat, self:getPosition(), area, -min, -max, magicEffect)
  113.     elseif attackType == "targetAoe" then
  114.         doAreaCombatHealth(self, combat, target:getPosition(), area, -min, -max, magicEffect)
  115.         self:getPosition():sendDistanceEffect(target:getPosition(), distEffect)
  116.     else
  117.         doTargetCombatHealth(self, target, combat, -min, -max, magicEffect)
  118.         self:getPosition():sendDistanceEffect(target:getPosition(), distEffect)
  119.     end
  120. end
  121.  
  122. function getPlayerInEvent(xRange, yRange)
  123.     local player
  124.     if player then
  125.         return player
  126.     end
  127.  
  128.     local specs = Game.getSpectators(eventCenterPosition, false, true, xRange, xRange, yRange, yRange)
  129.     for i = 1, #specs do
  130.         if specs[i]:getStorageValue(playingGameStorage) == 1 then
  131.             player = specs[i]
  132.             return player
  133.         end
  134.     end
  135. end
  136.  
  137. local function summonMonster(name)
  138.     local monster = Game.createMonster(name .."_TWD", summonMonsterPosition, false, true)
  139.     if monster then
  140.         monster:setDirection(EAST)
  141.         monsterWalkTo(monster:getId(), monster:getPosition(), walkPaths, 1)
  142.         summonMonsterPosition:sendMagicEffect(CONST_ME_TELEPORT)
  143.         monster:changeSpeed(-monster:getSpeed() + 130)
  144.  
  145.         local extraHealth = monsters[name].extraHealth
  146.         if extraHealth then
  147.             monster:setMaxHealth(monster:getMaxHealth() + extraHealth)
  148.             monster:addHealth(monster:getMaxHealth())
  149.         end
  150.     end
  151. end
  152.  
  153. function startWaveLevel(level) -- Ninja
  154.     local table, total = waves, 0
  155.     for a = 1, #waves do
  156.         table = waves[level]
  157.         for b = 1, #table.monsters do
  158.             for c = 1, table.monsters[b].count do
  159.                 addEvent(function()
  160.                     addEvent(summonMonster, b * table.monsters[b].interval, table.monsters[b].name)
  161.                 end, c * table.interval)
  162.             end
  163.  
  164.             total = total + table.monsters[b].count
  165.         end
  166.         break
  167.     end
  168.  
  169.     Game.setStorageValue(totalMonsterCountGlobalStorage, total)
  170. end      
  171.  
  172. function startNextWave(level, interval)
  173.     addEvent(startWaveLevel, interval * 1000, level)
  174. end
  175.  
  176. function Npc.setTurretLevel(self, level)
  177.     if level > 3 then
  178.         level = 3
  179.     end
  180.  
  181.     local lookId = self:getOutfit().lookType
  182.     local setColor = turrets[lookId].cfg[level].colorId
  183.     self:setOutfit({lookType = lookId, lookHead = setColor , lookBody = setColor, lookLegs = setColor, lookFeet = setColor, lookAddons = level})
  184. end
  185.  
  186. function Npc.getTurretLevel(self)
  187.     local addon = self:getOutfit().lookAddons
  188.     if addon == 0 then
  189.         return 1
  190.     end
  191.  
  192.     return addon
  193. end
  194.  
  195. function getWaveLevel()
  196.     return Game.getStorageValue(waveLevelGlobalStorage) or 0
  197. end
  198.  
  199. function setWaveLevel(lvl)
  200.     Game.setStorageValue(waveLevelGlobalStorage, lvl)
  201. end
  202.  
  203. function Player.getCoins(self)
  204.     return self:getStorageValue(coinStorage)
  205. end
  206.  
  207. function Player.addCoins(self, amount)
  208.     self:setStorageValue(coinStorage, math.max(0, self:getStorageValue(coinStorage)) + amount)
  209. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement