• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Daily Boss

Crystals

Member
Joined
Sep 28, 2024
Messages
58
Reaction score
10
Hi ! :) How to add a boss, e.g. Orshabaal, so that he respawns on specific dates/times in a specific position? :D

What I mean here is an event that, for example, on Tuesday at 3:30 p.m. the boss respawns in a given place

TFs 1.4.2 otcv8
 
LUA:
local pos = Position(x, y ,z) -- set pos here
local bossName = "Orshabaal" -- bossName
function onTime(interval)
    local tile = Tile(pos)
    if not tile then --unecessary check, can be ignored
       print("Error, tile not found.")
       return true
    end
    local boss = Game.createMonster(bossName, pos)
    if not boss then
       print("Error, boss with name " .. bossName .. " doesnt exsists.")
       return true
    end
    
    --access to boss object
    --boss:registerEvent("test") -- some random lines
    
    return true
end

XML:
<globalevent name="Simple Boss" time="09:55:00" script="boss.lua" />
 
I created the script directly in data/scripts, which is Revscriptsys. I added a table with specific times and positions where the Boss will spawn, along with a message. It also supports multiple spawn times, etc. In a single script, you can add more bosses.


It hasn't been tested yet xD.

data/scripts/DailyBossSpawn.lua
LUA:
-- Boss Configuration
local config = {
    bosses = {
        ["Orshabaal"] = {
            name = "Orshabaal",
            position = Position(601, 1043, 7),
            spawnDays = {"Friday"},
            spawnTimes = {"15:30"}, -- Simplified format HH:MM 3:30 p.m.
            deathMessage = "The mighty Wolf has been defeated!",
            spawnMessage = "Wolf has spawned! Beware adventurers!"
        },
        ["Morgaroth"] = {
            name = "Morgaroth",
            position = Position(2000, 2000, 7),
            spawnDays = {"Monday", "Thursday"},
            spawnTimes = {"15:30", "20:00"},
            deathMessage = "Morgaroth has been defeated!",
            spawnMessage = "Morgaroth has appeared!"
        }
    }
}

-- Days of the week
local daysOfWeek = {
    [1] = "Sunday",
    [2] = "Monday",
    [3] = "Tuesday",
    [4] = "Wednesday",
    [5] = "Thursday",
    [6] = "Friday",
    [7] = "Saturday"
}

local activeBosses = {}
local defeatedBosses = {}

local BossManager = {
    debug = true -- Set to true to enable debug messages
}

function BossManager:log(message)
    if self.debug then
        print(os.date("[%Y-%m-%d %H:%M:%S]") .. " " .. message)
    end
end

function BossManager:add(bossId, bossName)
    activeBosses[bossId] = bossName
    self:log("Boss added: " .. bossName .. " (ID: " .. bossId .. ")")
end

function BossManager:remove(bossId)
    local bossName = activeBosses[bossId]
    if bossName then
        activeBosses[bossId] = nil
        self:log("Boss removed: " .. bossName .. " (ID: " .. bossId .. ")")
    end
end

function BossManager:isActive(bossName)
    for _, name in pairs(activeBosses) do
        if name == bossName then
            return true
        end
    end
    return false
end

function BossManager:isDefeated(bossName)
    return defeatedBosses[bossName] == true
end

function BossManager:markDefeated(bossName)
    defeatedBosses[bossName] = true
    self:log("Boss marked as defeated: " .. bossName)
end

local function isSpawnTime(bossConfig)
    local currentTime = os.date("*t")
    local currentTimeStr = string.format("%02d:%02d", currentTime.hour, currentTime.min)
   
    for _, spawnTime in ipairs(bossConfig.spawnTimes) do
        if currentTimeStr == spawnTime then
            return true
        end
    end
    return false
end

local function spawnBoss(bossConfig)
    if BossManager:isActive(bossConfig.name) then
        BossManager:log("Spawn canceled - " .. bossConfig.name .. " is already active.")
        return false
    end

    if BossManager:isDefeated(bossConfig.name) then
        BossManager:log("Spawn canceled - " .. bossConfig.name .. " has already been defeated today.")
        return false
    end

    local monster = Game.createMonster(bossConfig.name, bossConfig.position)
    if monster then
        BossManager:add(monster:getId(), bossConfig.name)
        monster:registerEvent("DailyBossDeath")
        Game.broadcastMessage(bossConfig.spawnMessage, MESSAGE_STATUS_WARNING)
        BossManager:log("Boss successfully spawned: " .. bossConfig.name)
        return true
    end

    BossManager:log("Failed to spawn boss: " .. bossConfig.name)
    return false
end

local DailyBossEvent = GlobalEvent("DailyBossSpawn")

function DailyBossEvent.onThink(interval)
    local currentTime = os.date("*t")
    local currentDay = daysOfWeek[currentTime.wday]
   
    BossManager:log("Checking boss spawns - Current day: " .. currentDay)

    for bossName, bossConfig in pairs(config.bosses) do
        if table.contains(bossConfig.spawnDays, currentDay) then
            if isSpawnTime(bossConfig) then
                BossManager:log("Attempting to spawn " .. bossName)
                spawnBoss(bossConfig)
            end
        end
    end
    return true
end

DailyBossEvent:interval(6000) -- Check every 1 minute
DailyBossEvent:register()

local creatureEvent = CreatureEvent("DailyBossDeath")

function creatureEvent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    local creatureId = creature:getId()
    local bossName = activeBosses[creatureId]
   
    if bossName then
        local bossConfig = config.bosses[bossName]
        if bossConfig then
            BossManager:remove(creatureId)
            BossManager:markDefeated(bossName)
            Game.broadcastMessage(bossConfig.deathMessage, MESSAGE_STATUS_WARNING)
            BossManager:log(bossName .. " was defeated by " .. (killer:getName() or "an unknown player"))
        end
    end
    return true
end

creatureEvent:register()
If the script works and debug messages appear in the console or in the game, simply disable this line:
LUA:
local BossManager = {
    debug = true -- Set to true to enable debug messages
}
Set it to false to stop them from appearing.
 
Last edited:
Back
Top