local config = {
bossName = "Demon",
bossPosition = Position(1079, 1001, 7),
bossLoot = {
{9020, 1, 3, 1000}, -- ItemId, ItemAmount Min, ItemAmount Max , Chance
},
topLeft = Position(1072, 994, 7), -- TOP Left Area Position
bottomRight = Position(1085, 1006, 7), -- BOTTOM Right Area Position
storage = 50000, -- For Exhaust.
actionId = 4723, -- The action id of the lever.
expReward = 5000000, -- Experience reward for players that did at least 2% damage to the boss
minHealthDamagePercent = 0.02, -- Minimum damage percent to be eligible for experience reward
time = 30, -- seconds
}
----------------------------------------------------
-- local playersInArea = getPlayersInArea(topLeft, bottomRight)
local function getPlayersInArea(topLeft, bottomRight)
local count = 0
for _, player in ipairs(Game.getPlayers()) do
local pos = player:getPosition()
if pos.z == topLeft.z
and pos.x >= topLeft.x and pos.x <= bottomRight.x
and pos.y >= topLeft.y and pos.y <= bottomRight.y then
count = count + 1
end
end
return count
end
----------------------------------------------------
local action = Action()
function action.onUse(player, item, fromPos, target, toPos, isHotkey)
if not item:getActionId() == config.actionId then
return true
end
if Game.getStorageValue(config.storage) == 1 then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'The boss is already spawned.')
return true
end
if Game.getStorageValue(config.storage) > os.time() then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'Boss can be spawned again in ' ..Game.getStorageValue(config.storage) - os.time()..' seconds.')
return true
end
local playersInArea = getPlayersInArea(config.topLeft, config.bottomRight)
local monster = Game.createMonster(config.bossName, config.bossPosition, nil, true)
if monster then
monster:setMaxHealth(monster:getMaxHealth() * playersInArea) -- If you got 4 players in the area the boss will have 4x health.
monster:addHealth(monster:getMaxHealth()) -- Heal the monster to full health after changing max health.
monster:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
monster:registerEvent("bossGuildDeath") -- Death Event Register
end
Game.setStorageValue(config.storage, 1) -- This will avoid spawning multipe bosses.
return true
end
action:aid(config.actionId)
action:register()
----------------------------------------------------
-- Boss death event (LOOT HANDLER)
local creatureEvent = CreatureEvent("bossGuildDeath")
function creatureEvent.onDeath(creature, corpse, lastHitKiller, mostDamageKiller)
if creature:isPlayer() or creature:getMaster() then
return true
end
if not corpse or not corpse:isContainer() then
return true
end
local playersInArea = getPlayersInArea(config.topLeft, config.bottomRight)
for _, loot in ipairs(config.bossLoot) do
if math.random(1000) <= loot[4] then
local amount = math.random(loot[2] * playersInArea, loot[3] * playersInArea)
corpse:addItem(loot[1], amount)
end
end
for pid, info in pairs(creature:getDamageMap()) do
local percent = creature:getMaxHealth() * config.minHealthDamagePercent
local player = Player(pid)
if player and info.total >= percent then
player:addExperience(config.expReward, true)
end
end
Game.setStorageValue(config.storage, 0) -- Keep this to check if the boss is spawned or not.
Game.setStorageValue(config.storage, os.time() + config.time)
return true
end
creatureEvent:register()