Skip to content

Instantly share code, notes, and snippets.

@dbjorkholm
Last active September 7, 2016 10:50
Show Gist options
  • Save dbjorkholm/6a4dd9d4f56655925f28 to your computer and use it in GitHub Desktop.
Save dbjorkholm/6a4dd9d4f56655925f28 to your computer and use it in GitHub Desktop.
Monster Arena ~
MonsterArena = {
fromPosition = { Position(32075, 32286, 8), Position(32077, 32286, 8) }, --Player Start by lever
toPosition = { Position(32074, 32277, 8), Position(32076, 32277, 8) }, --Player Teleport Arena Positions
spawnPosition = { Position(32074, 32278, 8), Position(32076, 32276, 8) }, --Monster Spawns
area = {
from = Position(32070, 32273, 8),
to = Position(32082, 32282, 8)
},
exitPosition = Position(32075, 32284, 8), --Teleported once its over
reward = {itemId = 10530, count = 1}, --Reward
blockItemId = 3402,
-- Only convincable / summonable monsters
-- You can create custom monsters which are stronger and convincable
monsters = {'Orc Berserker', 'Minotaur Guard', 'Minotaur Archer', 'Dwarf Guard', 'Dwarf Soldier', 'Elf Scout', 'Demon Skeleton'},
event = 'MonsterArenaDeath',
players = {}
}
function MonsterArena.hasPlayer(player)
local position = player:getPosition()
return position.x >= MonsterArena.area.from.x and position.y >= MonsterArena.area.from.y
and position.x <= MonsterArena.area.to.x and position.y <= MonsterArena.area.to.y
and position.z == MonsterArena.area.from.z
end
function MonsterArena.isOccupied()
for _, pid in ipairs(MonsterArena.players) do
local player = Player(pid)
if player and MonsterArena.hasPlayer(player) then
return true
end
end
return false
end
function MonsterArena.clean()
for i = 1, #MonsterArena.players do
MonsterArena.players[i] = nil
end
end
local function revertLever(position)
local tile = Tile(position)
if tile then
local leverItem = tile:getItemById(1946)
if leverItem then
leverItem:transform(1945)
end
end
end
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
if item.itemid ~= 1945 then
return false
end
if MonsterArena.isOccupied() then
player:sendCancelMessage('The monster arena is currently occupied.')
return true
end
local players = { }
for _, fromPosition in ipairs(MonsterArena.fromPosition) do
local tile = Tile(fromPosition)
if tile then
local topCreature = tile:getTopCreature()
if topCreature and topCreature:isPlayer() then
players[#players + 1] = topCreature
end
end
end
if #players ~= #MonsterArena.fromPosition then
player:sendCancelMessage('You need another player for the Monster Arena.')
return true
end
MonsterArena.clean()
local summons = { }
for i, tmpPlayer in ipairs(players) do
tmpPlayer:teleportTo(MonsterArena.toPosition[i])
MonsterArena.fromPosition[i]:sendMagicEffect(CONST_ME_POFF)
MonsterArena.toPosition[i]:sendMagicEffect(CONST_ME_TELEPORT)
local monsterName = MonsterArena.monsters[math.random(#MonsterArena.monsters)]
local monster = Game.createMonster(monsterName, MonsterArena.spawnPosition[i], true)
if monster then
monster:setMaster(tmpPlayer)
monster:registerEvent(MonsterArena.event)
summons[#summons + 1] = monster
end
Game.createItem(MonsterArena.blockItemId, 1, MonsterArena.spawnPosition[i])
tmpPlayer:sendTextMessage(MESSAGE_INFO_DESCR, string.format('A %s is fighting for you this round!', monsterName))
MonsterArena.players[#MonsterArena.players + 1] = tmpPlayer.uid
end
if #summons ~= #players then
return false
end
summons[1]:setTarget(summons[2])
summons[2]:setTarget(summons[1])
item:transform(1946)
addEvent(revertLever, 3000, toPosition)
return true
end
local function getMaster(creature)
if not creature or type(creature) ~= 'userdata' or not creature:isMonster() then
return
end
local master = creature:getMaster()
if not master or not master:isPlayer() then
return
end
return master
end
function onDeath(monster, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
local loserPlayer, winnerPlayer = getMaster(monster), getMaster(killer)
if not loserPlayer or not winnerPlayer then
return
end
local reward = MonsterArena.reward
if reward then
winnerPlayer:sendTextMessage(MESSAGE_INFO_DESCR, 'Your monster won the fight and earned a reward for you!')
winnerPlayer:addItem(reward.itemId, reward.count)
else
winnerPlayer:sendTextMessage(MESSAGE_INFO_DESCR, 'Your monster won the fight!')
end
loserPlayer:sendTextMessage(MESSAGE_INFO_DESCR, 'Your monster lost the fight!')
winnerPlayer:teleportTo(MonsterArena.exitPosition)
loserPlayer:teleportTo(MonsterArena.exitPosition)
MonsterArena.exitPosition:sendMagicEffect(CONST_ME_MAGIC_BLUE)
for _, position in ipairs(MonsterArena.spawnPosition) do
local tile = Tile(position)
if tile then
local item = tile:getItemById(MonsterArena.blockItemId)
if item then
item:remove()
end
end
end
killer:remove()
return true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment