Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. _stoneEvent = {}
  2.  
  3. _stoneEvent.config =
  4. {
  5. monsterName = 'Stone Event',
  6. spawnPos = {x = 986, y = 1010, z = 7},
  7. waves = -- waves
  8. {
  9. [95] = -- health percent
  10. {
  11. {name = 'Dragon', qty = 1},
  12. {name = 'Rotworm', qty = 1},
  13. },
  14. [85] =
  15. {
  16. {name = 'Dragon Lord', qty = 1},
  17. },
  18. [50] =
  19. {
  20. {name = 'Demon', qty = 1},
  21. },
  22. [25] =
  23. {
  24. {name = 'Ap Javel', qty = 1},
  25. },
  26. [10] =
  27. {
  28. {name = 'Orshabaal', qty = 1}
  29. }
  30. },
  31. individualReward = -- given only to the player who dealt the most damage
  32. {
  33. {name = '10 Premium Points Emblem', qty = 1, chance = 100} -- you can add more rewards
  34. },
  35. globalReward = -- given to everyone who participated
  36. {
  37. {name = 'Crystal Coin', qty = 10, chance = 100},
  38. {name = 'Magic Plate Armor', qty = 1, chance = 50}
  39. },
  40. globalStatus = 33033,
  41. eventStatus = 33033
  42. }
  43.  
  44. _stoneEvent.temp = {}
  45. _stoneEvent.monsters = {}
  46.  
  47. function _stoneEvent.giveIndivualReward(player)
  48. for _, reward in ipairs(_stoneEvent.config.individualReward) do
  49. if math.random(1, 100) <= reward.chance then
  50. player:addItem(ItemType(reward.name):getId(), reward.qty)
  51. end
  52. end
  53. end
  54.  
  55. function _stoneEvent.giveGlobalReward(player)
  56. for _, reward in ipairs(_stoneEvent.config.globalReward) do
  57. if math.random(1, 100) <= reward.chance then
  58. player:addItem(ItemType(reward.name):getId(), reward.qty)
  59. end
  60. end
  61. end
  62.  
  63. function _stoneEvent.getHealthPercent(monster)
  64. return math.ceil(monster:getHealth() / monster:getMaxHealth() * 100)
  65. end
  66.  
  67. function _stoneEvent.getBounds()
  68. local aux = {}
  69. for hp, _ in pairs(_stoneEvent.config.waves) do
  70. table.insert(aux, hp)
  71. end
  72.  
  73. table.insert(aux, -1)
  74. table.sort(aux, function(a, b) return b < a end)
  75. return aux
  76. end
  77.  
  78. function _stoneEvent.getClosestFreePosition(position)
  79. local usePosition = Position(position)
  80. local tiles = {Tile(usePosition)}
  81. local length = 2
  82. local tile
  83. for y = -length, length do
  84. for x = -length, length do
  85. if x ~= 0 or y ~= 0 then
  86. usePosition.x = position.x - x
  87. usePosition.y = position.y - y
  88. tile = Tile(usePosition)
  89. if tile then
  90. tiles[#tiles + 1] = tile
  91. end
  92. end
  93. end
  94. end
  95. for i = 1, #tiles do
  96. tile = tiles[i]
  97. if tile:getCreatureCount() == 0 and not tile:hasProperty(CONST_PROP_IMMOVABLEBLOCKSOLID) then
  98. return tile:getPosition()
  99. end
  100. end
  101. return Position()
  102. end
  103.  
  104. function _stoneEvent.onEventDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified)
  105. if creature:getName() ~= _stoneEvent.config.monsterName then
  106. Game.setStorageValue(_stoneEvent.config.globalStatus, Game.getStorageValue(_stoneEvent.config.globalStatus) - 1)
  107. table.remove(_stoneEvent.monsters, creature:getId())
  108. if Game.getStorageValue(_stoneEvent.config.globalStatus) == 0 then
  109. Creature(_stoneEvent.config.monsterName):setHiddenHealth(false)
  110. end
  111. else
  112. for _, player in ipairs(Game.getPlayers()) do
  113. if player:getStorageValue(_stoneEvent.config.eventStatus) == 1 then
  114. _stoneEvent.giveGlobalReward(player)
  115. player:setStorageValue(_stoneEvent.config.eventStatus, 0)
  116. end
  117. end
  118. if not mostDamageKiller:isPlayer() then
  119. _stoneEvent.giveIndivualReward(mostDamageKiller:getMaster())
  120. else
  121. _stoneEvent.giveIndivualReward(mostDamageKiller)
  122. end
  123. _stoneEvent.broadcast('[Stone Event]: Event ended!! Congratulations to everyone who participated!!')
  124. end
  125. return true
  126. end
  127.  
  128. function _stoneEvent.onChangeHealth(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
  129. if not creature:isMonster() and creature:getName() ~= _stoneEvent.config.monsterName then
  130. return primaryDamage, primaryType, secondaryDamage, secondaryType
  131. end
  132.  
  133. if not attacker:isPlayer() then
  134. return primaryDamage, primaryType, secondaryDamage, secondaryType
  135. end
  136.  
  137. if attacker:getStorageValue(_stoneEvent.config.eventStatus) <= 0 then
  138. attacker:setStorageValue(_stoneEvent.config.eventStatus, 1)
  139. end
  140.  
  141. local globalStatus = Game.getStorageValue(_stoneEvent.config.globalStatus)
  142. if tonumber(globalStatus) > 0 then
  143. return creature:getPosition():sendMagicEffect(CONST_ME_BLOCKHIT) and true
  144. end
  145.  
  146. local hp = _stoneEvent.temp[1]
  147. if _stoneEvent.getHealthPercent(creature) < hp then
  148. table.remove(_stoneEvent.temp, 1)
  149. for _, monsters in pairs(_stoneEvent.config.waves[hp]) do
  150. for i = 1, monsters.qty do
  151. local t = {x = _stoneEvent.config.spawnPos.x + math.random(-3, 3), y = _stoneEvent.config.spawnPos.y + math.random(-3, 3), z = _stoneEvent.config.spawnPos.z}
  152. local pos = _stoneEvent.getClosestFreePosition(t)
  153. local c = Game.createMonster(monsters.name, pos)
  154. if c then
  155. local globalStatus = Game.getStorageValue(_stoneEvent.config.globalStatus)
  156. Game.setStorageValue(_stoneEvent.config.globalStatus, math.max(0, tonumber(globalStatus)) + 1)
  157. c:registerEvent('stone_event_death')
  158. _stoneEvent.monsters[c:getId()] = c
  159. end
  160. end
  161. end
  162.  
  163. local globalStatus = Game.getStorageValue(_stoneEvent.config.globalStatus)
  164. if tonumber(globalStatus) > 0 then
  165. creature:setHiddenHealth(true)
  166. end
  167. end
  168. return primaryDamage, primaryType, secondaryDamage, secondaryType
  169. end
  170.  
  171. function _stoneEvent.check()
  172. local s = Creature(_stoneEvent.config.monsterName)
  173. if s:isRemoved() then return true end
  174. if #_stoneEvent.monsters > 0 or s:isMonster() then
  175. for _, monster in ipairs(_stoneEvent.monsters) do
  176. monster:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
  177. monster:remove()
  178. end
  179. s:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
  180. s:remove()
  181. Game.setStorageValue(_stoneEvent.config.globalStatus, 0)
  182. end
  183. _stoneEvent.broadcast('[Stone Event]: Exceed maximum play time!! Event ending!!')
  184. end
  185.  
  186. function _stoneEvent.startEvent()
  187. _stoneEvent.broadcast('[Stone Event]: Stone Event just started ! You have 30 minutes to defeat the Stone !')
  188. local s = Game.createMonster(_stoneEvent.config.monsterName, _stoneEvent.config.spawnPos)
  189. if s then
  190. s:registerEvent('stone_event_stats')
  191. s:registerEvent('stone_event_death')
  192. _stoneEvent.temp = _stoneEvent.getBounds()
  193. else
  194. _stoneEvent.broadcast('[Stone Event]: Game couldn\'t start!! Contact the game manager!!')
  195. end
  196. addEvent(_stoneEvent.check, 30 * 60 * 1000)
  197. end
  198.  
  199. function _stoneEvent.broadcast(message)
  200. for _, player in ipairs(Game.getPlayers()) do
  201. player:sendTextMessage(MESSAGE_EVENT_ADVANCE, message)
  202. end
  203. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement