Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.80 KB | None | 0 0
  1. --first team
  2. TEAM_BATTLE_A = {
  3. name = 'Blue',
  4. color = 88
  5. }
  6. --second team
  7. TEAM_BATTLE_B = {
  8. name = 'Red',
  9. color = 94
  10. }
  11.  
  12. CONDITION_TEAM_A_MALE = createConditionObject(CONDITION_OUTFIT)
  13. setConditionParam(CONDITION_TEAM_A_MALE, CONDITION_PARAM_TICKS, 8 * 60 * 1000)
  14. addOutfitCondition(CONDITION_TEAM_A_MALE, 0, 131, TEAM_BATTLE_A.color, TEAM_BATTLE_A.color, TEAM_BATTLE_A.color, TEAM_BATTLE_A.color)
  15.  
  16. CONDITION_TEAM_A_FEMALE = createConditionObject(CONDITION_OUTFIT)
  17. setConditionParam(CONDITION_TEAM_A_FEMALE, CONDITION_PARAM_TICKS, 8 * 60 * 1000)
  18. addOutfitCondition(CONDITION_TEAM_A_FEMALE, 0, 139, TEAM_BATTLE_A.color, TEAM_BATTLE_A.color, TEAM_BATTLE_A.color, TEAM_BATTLE_A.color)
  19.  
  20. CONDITION_TEAM_B_MALE = createConditionObject(CONDITION_OUTFIT)
  21. setConditionParam(CONDITION_TEAM_B_MALE, CONDITION_PARAM_TICKS, 8 * 60 * 1000)
  22. addOutfitCondition(CONDITION_TEAM_B_MALE, 0, 131, TEAM_BATTLE_B.color, TEAM_BATTLE_B.color, TEAM_BATTLE_B.color, TEAM_BATTLE_B.color)
  23.  
  24. CONDITION_TEAM_B_FEMALE = createConditionObject(CONDITION_OUTFIT)
  25. setConditionParam(CONDITION_TEAM_B_FEMALE, CONDITION_PARAM_TICKS, 8 * 60 * 1000)
  26. addOutfitCondition(CONDITION_TEAM_B_FEMALE, 0, 139, TEAM_BATTLE_B.color, TEAM_BATTLE_B.color, TEAM_BATTLE_B.color, TEAM_BATTLE_B.color)
  27.  
  28. function getIndex(array, v)
  29. for index, value in ipairs(array) do
  30. if v == value then
  31. return index
  32. end
  33. end
  34. end
  35.  
  36. function table.clone(t)
  37. local t_type = type(t)
  38. local copy
  39. if t_type == 'table' then
  40. copy = {}
  41. for t_key, t_value in pairs(t) do
  42. copy[t_key] = t_value
  43. end
  44. else -- number, string, boolean, etc
  45. copy = t
  46. end
  47. return copy
  48. end
  49.  
  50. function getTeamLevel(t)
  51. local v = 0
  52. for _, player in ipairs(t) do
  53. v = v + player:getLevel()
  54. end
  55. return v
  56. end
  57.  
  58. -->[[
  59. --> CONSTANTS
  60. --<]]
  61. team_battle = {
  62. --minutes to begin event
  63. start_time = 2,
  64. --minutes to finish event
  65. finish_time = 8,
  66. --min players required for event
  67. min_players = 4,
  68. --max players allowed in event
  69. max_players = 32,
  70. --custom channel to broadcast event messages
  71. channel = 10,
  72. --delay in minutes to broadcast event stats
  73. event_info = 3,
  74. --mininum level of player
  75. player_level = 100,
  76. --reward for winner team
  77. reward = {2160, 100},
  78. --allow players with same IP in event? [false = allow, true = don't allow]
  79. ip_check = false,
  80. -->[[
  81. --> STORAGES & DATA
  82. --<]]
  83. --event's stat globalstorage
  84. status = 12750,
  85. --team a's score globalstorage
  86. team_a_frags = 12751,
  87. --team b's score globalstorage
  88. team_b_frags = 12752,
  89. --team a players
  90. team_a = {},
  91. --team b players
  92. team_b = {},
  93. --player's storages
  94. stor_team = 23750,
  95. -->[[
  96. --> STRINGS (do not alter %d or %s)
  97. --<]]
  98. msg_call = 'Team Battle will begin in %s! Write "!teambattle" to join the event.',
  99. msg_join = '[Team Battle] %s has joined the event.',
  100. msg_cancel = 'Team Battle was canceled for insufficient players.',
  101. msg_begin = 'Team Battle has started!',
  102. msg_finish = 'Team Battle will finish in %d minutes...',
  103. msg_win = 'Team Battle has finished and team %s is the winner with %d frags! [' .. TEAM_BATTLE_A.name .. ': %d - ' .. TEAM_BATTLE_B.name .. ': %d] A rewards has been added to the players of winner team.',
  104. msg_draw = 'Team Battle has ended with a draw! Both teams scored %s frags...',
  105. msg_kill = '[Team Battle] %s killed %s. [' .. TEAM_BATTLE_A.name .. ': %d - ' .. TEAM_BATTLE_B.name .. ': %d]',
  106. msg_stat = '[Team Battle] [' .. TEAM_BATTLE_A.name .. ': %d - ' .. TEAM_BATTLE_B.name .. ': %d].',
  107. msg_defeat = '[Team Battle] %s.',
  108. msg_bonus = '[Team Battle] You have been awarded with x%d %s.',
  109. -->[[
  110. --> POSITIONS (map wait_room with PZ+NO-LOGOUT, arena with PVP+NO-LOGOUT)
  111. --<]]
  112. wait_room = {from = Position(858, 864, 7), to = Position(874, 880, 7)},
  113. arena = {
  114. [1] = {from = Position(696, 805, 7), to = Position(729, 827, 7)},
  115. [2] = {from = Position(696, 805, 7), to = Position(729, 827, 7)}
  116. },
  117. -->[[
  118. --> FUNCTIONS
  119. --<]]
  120. isEnemy = function (player, attacker)
  121. local enemy = attacker
  122. if attacker:isCreature() == nil then
  123. return true
  124. end
  125.  
  126. if (enemy:getMaster() ~= nil) and (enemy:getMaster():isPlayer() ~= nil) then
  127. enemy = enemy:getMaster()
  128. end
  129.  
  130. return player:getOutfit().lookBody ~= enemy:getOutfit().lookBody
  131. end,
  132.  
  133. broadcast = function (msg, class)
  134. for _, player in ipairs(Game.getPlayers()) do
  135. player:sendChannelMessage('', msg, class, team_battle.channel)
  136. end
  137. end,
  138.  
  139. getData = function (player, data)
  140. local key = {
  141. ['team'] = team_battle.stor_team
  142. }
  143. return player:getStorageValue(key[data])
  144. end,
  145.  
  146. hasDuplicateIP = function (player1)
  147. for _, player2 in ipairs(Game.getPlayers()) do
  148. if player2:getPosition():isInRange(team_battle.wait_room.from, team_battle.wait_room.to) then
  149. if player1:getIp() == player2:getIp() then
  150. return true
  151. elseif player2:getIp() == 0 then
  152. player2:remove()
  153. end
  154. end
  155. end
  156. return false
  157. end,
  158.  
  159. getTeamInArena = function (t)
  160. local count = 0
  161. for _, player in ipairs(Game.getPlayers()) do
  162. if player:getPosition():isInRange(team_battle.arena[t].from, team_battle.arena[t].to) and (team_battle.getData(player, 'team') == t) then
  163. count = count + 1
  164. end
  165. end
  166. return count
  167. end,
  168.  
  169. setOutfit = function (player, team)
  170. local condition = {
  171. {[0] = CONDITION_TEAM_A_FEMALE, [1] = CONDITION_TEAM_A_MALE},
  172. {[0] = CONDITION_TEAM_B_FEMALE, [1] = CONDITION_TEAM_B_MALE}
  173. }
  174. player:addCondition(condition[team][player:getSex()], true)
  175. end,
  176.  
  177. setTeams = function ()
  178. local players = {}
  179. for _, player in ipairs(Game.getPlayers()) do
  180. if player:getPosition():isInRange(team_battle.wait_room.from, team_battle.wait_room.to) then
  181. table.insert(players, player)
  182. end
  183. end
  184.  
  185. local size = math.floor(#players/2)
  186. local team_battle_a, team_battle_b = {}, {}
  187. for i = 1, 100 do
  188. math.randomseed(i)
  189. team_battle_a, team_battle_b = {}, {}
  190. local _players = table.clone(players)
  191. repeat
  192. local v = _players[math.random(#_players)]
  193. table.insert(#team_battle_a < size and team_battle_a or team_battle_b, v)
  194. table.remove(_players, getIndex(_players, v))
  195. until #_players == 0
  196. if math.abs(getTeamLevel(team_battle_a) - getTeamLevel(team_battle_b)) < 100 then
  197. break
  198. end
  199. end
  200.  
  201. for _, player in ipairs(team_battle_a) do
  202. team_battle.setOutfit(player, 1)
  203. player:setStorageValue(team_battle.stor_team, 1)
  204. team_battle.register(player)
  205. team_battle.team_a[player:getName()] = {}
  206. end
  207. for _, player in ipairs(team_battle_b) do
  208. team_battle.setOutfit(player, 2)
  209. player:setStorageValue(team_battle.stor_team, 2)
  210. team_battle.register(player)
  211. team_battle.team_b[player:getName()] = {}
  212. end
  213. end,
  214.  
  215. teleport = function (player, from, to)
  216. player:teleportTo(Position(math.random(from.x, to.x), math.random(from.y, to.y), from.z))
  217. end,
  218.  
  219. heal = function (player)
  220. player:addHealth(player:getMaxHealth())
  221. player:addMana(player:getMaxMana())
  222. local condition = 1
  223. player:removeCondition(condition)
  224. for n = 1, 27 do
  225. condition = condition * 2
  226. if condition ~= 64 then
  227. player:removeCondition(condition)
  228. end
  229. end
  230. end,
  231.  
  232. onEnd = function (player)
  233. team_battle.heal(player)
  234. player:removeCondition(CONDITION_OUTFIT)
  235. team_battle.unregister(player)
  236. player:teleportTo(player:getTown():getTemplePosition())
  237. end,
  238.  
  239. sendLongMessage = function (array, class, init)
  240. local strings, i, position, added = {""}, 1, 1, false
  241. for index = 1, #array do
  242. if(added) then
  243. if(i > (position * 10)) then
  244. strings[position] = strings[position] .. ","
  245. position = position + 1
  246. strings[position] = ""
  247. else
  248. strings[position] = i == 1 and "" or strings[position] .. ", "
  249. end
  250. end
  251. strings[position] = strings[position] .. array[index]
  252. i = i + 1
  253. added = true
  254. end
  255. for i, str in ipairs(strings) do
  256. if(str:sub(str:len()) ~= ",") then
  257. str = str .. "."
  258. end
  259. sendChannelMessage(team_battle.channel, class, (init and i == 1) and (init .. str) or str)
  260. end
  261. end,
  262.  
  263. info_event = function ()
  264. if getGlobalStorageValue(team_battle.status) == 1 then
  265. sendChannelMessage(team_battle.channel, 12, team_battle.msg_stat:format(getGlobalStorageValue(team_battle.team_a_frags), getGlobalStorageValue(team_battle.team_b_frags)))
  266. addEvent(team_battle.info_event, team_battle.event_info*60*1000)
  267. end
  268. end,
  269.  
  270. getTeamPlayers = function (t)
  271. local list = {}
  272. for _, player in ipairs(Game.getPlayers()) do
  273. if team_battle.getData(player, 'team') == t then
  274. table.insert(list, player:getName())
  275. end
  276. end
  277. return list
  278. end,
  279.  
  280. register = function (player)
  281. player:registerEvent('teambattle_stats')
  282. player:registerEvent('teambattle_death')
  283. end,
  284.  
  285. unregister = function (player)
  286. player:unregisterEvent('teambattle_stats')
  287. player:unregisterEvent('teambattle_death')
  288. player:setStorageValue(team_battle.stor_team, -1)
  289. end,
  290.  
  291. cancel = function ()
  292. setGlobalStorageValue(team_battle.status, -1)
  293. for _, player in ipairs(Game.getPlayers()) do
  294. if player:getPosition():isInRange(team_battle.wait_room.from, team_battle.wait_room.to) then
  295. player:teleportTo(player:getTown():getTemplePosition())
  296. end
  297. end
  298. end,
  299.  
  300. finish = function ()
  301. if getGlobalStorageValue(team_battle.status) == 1 then
  302. if getGlobalStorageValue(team_battle.team_a_frags) > getGlobalStorageValue(team_battle.team_b_frags) then
  303. sendChannelMessage(team_battle.channel, 14, team_battle.msg_win:format(TEAM_BATTLE_A.name, getGlobalStorageValue(team_battle.team_a_frags), getGlobalStorageValue(team_battle.team_a_frags), getGlobalStorageValue(team_battle.team_b_frags)))
  304. for k, v in pairs(team_battle.team_a) do
  305. local player = Player(k)
  306. if player ~= nil then
  307. player:addItem(team_battle.reward[1], team_battle.reward[2])
  308. player:sendChannelMessage('', team_battle.msg_bonus:format(team_battle.reward[2],ItemType(team_battle.reward[1]):getName()), 14, team_battle.channel)
  309. team_battle.team_a[k] = nil
  310. else
  311. team_battle.team_a[k] = {team_battle.reward[1], team_battle.reward[2]}
  312. end
  313. end
  314. elseif getGlobalStorageValue(team_battle.team_b_frags) > getGlobalStorageValue(team_battle.team_a_frags) then
  315. sendChannelMessage(team_battle.channel, 14, team_battle.msg_win:format(TEAM_BATTLE_B.name, getGlobalStorageValue(team_battle.team_b_frags), getGlobalStorageValue(team_battle.team_a_frags), getGlobalStorageValue(team_battle.team_b_frags)))
  316. for k, v in pairs(team_battle.team_b) do
  317. local player = Player(k)
  318. if player ~= nil then
  319. player:addItem(team_battle.reward[1], team_battle.reward[2])
  320. player:sendChannelMessage('', team_battle.msg_bonus:format(team_battle.reward[2],ItemType(team_battle.reward[1]):getName()), 14, team_battle.channel)
  321. team_battle.team_b[k] = nil
  322. else
  323. team_battle.team_b[k] = {team_battle.reward[1], team_battle.reward[2]}
  324. end
  325. end
  326. elseif getGlobalStorageValue(team_battle.team_a_frags) == getGlobalStorageValue(team_battle.team_b_frags) then
  327. sendChannelMessage(team_battle.channel, 14, team_battle.msg_draw:format(getGlobalStorageValue(team_battle.team_a_frags)))
  328. end
  329. setGlobalStorageValue(team_battle.status, -1)
  330. setGlobalStorageValue(team_battle.team_a_frags, -1)
  331. setGlobalStorageValue(team_battle.team_b_frags, -1)
  332. for _, player in ipairs(Game.getPlayers()) do
  333. if team_battle.getData(player, 'team') > 0 then
  334. team_battle.onEnd(player)
  335. end
  336. end
  337. end
  338. end,
  339.  
  340. start = function ()
  341. if getGlobalStorageValue(team_battle.status) == 0 then
  342. local count = 0
  343. for i, player in ipairs(Game.getPlayers()) do
  344. if player:getPosition():isInRange(team_battle.wait_room.from, team_battle.wait_room.to) then
  345. count = count + 1
  346. end
  347. end
  348. if count < team_battle.min_players then
  349. team_battle.cancel()
  350. return broadcastMessage(team_battle.msg_cancel, MESSAGE_EVENT_ADVANCE)
  351. end
  352. team_battle.team_a = {}
  353. team_battle.team_b = {}
  354. team_battle.setTeams()
  355. sendChannelMessage(team_battle.channel, 14, team_battle.msg_begin)
  356. team_battle.sendLongMessage(team_battle.getTeamPlayers(1), 12, '[Team ' .. TEAM_BATTLE_A.name .. ' Players] ')
  357. team_battle.sendLongMessage(team_battle.getTeamPlayers(2), 12, '[Team ' .. TEAM_BATTLE_B.name .. ' Players] ')
  358. setGlobalStorageValue(team_battle.status, 1)
  359. setGlobalStorageValue(team_battle.team_a_frags, 0)
  360. setGlobalStorageValue(team_battle.team_b_frags, 0)
  361. broadcastMessage(team_battle.msg_begin, MESSAGE_EVENT_ADVANCE)
  362. addEvent(team_battle.info_event, team_battle.event_info*60*1000)
  363. for _, player in ipairs(Game.getPlayers()) do
  364. local id = team_battle.getData(player, 'team')
  365. if id > 0 then
  366. player:openChannel(team_battle.channel)
  367. team_battle.teleport(player, team_battle.arena[id].from, team_battle.arena[id].to)
  368. player:say('Go!', TALKTYPE_MONSTER_YELL)
  369. end
  370. end
  371. addEvent(team_battle.finish, team_battle.finish_time*60*1000)
  372. addEvent(sendChannelMessage, (team_battle.finish_time-2)*60*1000, team_battle.channel, 14, team_battle.msg_finish:format(2))
  373. end
  374. end,
  375. }
  376.  
  377. function Position:isInRange(fromPosition, toPosition)
  378. return (self.x >= fromPosition.x and self.y >= fromPosition.y and self.z >= fromPosition.z
  379. and self.x <= toPosition.x and self.y <= toPosition.y and self.z <= toPosition.z)
  380. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement