• 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!

TFS 1.X+ re spawn monster if it's out of it's spawn range / spawn size

Felipe93

Ghost Member
Joined
Mar 21, 2015
Messages
1,990
Solutions
9
Reaction score
334
Location
Chile
Hello

I'm trying to make monsters re-spawn if they are outside of their spawn range / spawn size
this is the main code made for tfs 0.4
Lua:
void Monster::onThink(uint32_t interval)
{
    Creature::onThink(interval);
    if(despawn())
    {
        if(spawn)
        {
            spawn->removeMonster(this);
            spawn->startEvent();
            spawn = NULL;
            masterRadius = -1;
        }
    }

    updateIdleStatus();
    if(isIdle)
        return;
i tried to adapt it few times but the code never worked at all and i did things like these
Code:
    if (!isInSpawnRange(position)) {
    spawn->removeMonster(this);
    spawn->startSpawnCheck();
    spawn->startup();
    spawn = NULL;
    despawnRadius = -1;



        else if (!isInSpawnRange(position)) {
        spawn->removeMonster(this);
        spawn->startSpawnCheck();
        spawn->startup();
        spawn = NULL;
        despawnRadius = -1;
        g_game.placeCreature(this);
    }
       
        {



        else if (!isInSpawnRange(position)) {
        spawn->removeMonster(this);
        spawn->startSpawnCheck();
        spawn->startup();
        spawn = NULL;
        despawnRadius = -1;
        g_game.placeCreature(this);
    }

after of being thinking i saw the default code that comes inside the tfs 1.x that removes monster if they're outside of if spawn range and teleport them back
to it's spawn:
Code:
    if (!isInSpawnRange(position)) {
        g_game.addMagicEffect(this->getPosition(), CONST_ME_POFF);
        if (g_config.getBoolean(ConfigManager::REMOVE_ON_DESPAWN)) {
            g_game.removeCreature(this, false);
        }
        else {
            g_game.internalTeleport(this, masterPos);
            setIdle(true);
        }
    }

so i changed it to this
Code:
    if (!isInSpawnRange(position)) {
        g_game.addMagicEffect(this->getPosition(), CONST_ME_POFF);
        if (g_config.getBoolean(ConfigManager::REMOVE_ON_DESPAWN)) {
            //g_game.removeCreature(this, false);
        }
        else {
           
            g_game.placeCreature(this, masterPos);
            setIdle(true);
        }
    }

when the monster is out of it's spawn range, it's start to constantly pooffing the problem is that the new monster never appears
(note that i did not remove the poofing part of the cause in order to view if the code was partially working
please lend me a hand
 
function checkMonsterSpawn()
for _, monster in pairs(Game.getMonsterList()) do
if not monster:isInSpawnRange() or not monster:isInSpawnSize() then
monster:remove()
Game.createMonster(monster:getName(), monster:getPosition())
end
end
end

--Call the function in a loop, for example every minute
setTimer(checkMonsterSpawn, 60000, 0)

This code defines a function checkMonsterSpawn() that loops through all the monsters on the map, and checks if each monster is inside of both its spawn range and spawn size. If it is not, the monster is removed and a new instance of that monster is created at the same position.

The function is being called every minute using the setTimer function, which will repeatedly call the function every 60000 milliseconds (60 seconds).

Please note that this is only an example and may require further modifications for your specific use case. Also, this code is for TFS 0.4, an old version of Tibia, and it may not work as expected with new version of the game.
 
Yes, you are correct. This code is specifically for TFS 0.4 and it may not work as expected in newer versions of the game. Additionally, it is also important to note that the Game.getMonsterList() function, monster:isInSpawnRange() and monster:isInSpawnSize() functions may have different names or not exist at all in newer versions.

The checkMonsterSpawn() function is used to check the spawn range and size of all monsters on the map and respawn them if they are outside of their spawn area. This is typically used to prevent monsters from wandering too far away from their designated spawn location, or to respawn them if they were killed or removed from the game.

It is worth noting that the best way to manage the monsters in new versions of Tibia is to use the monster's script that comes with the game, or to use the spawn manager that comes with the server software. Also, it's worth to mention that this script may cause a lot of server load if the amount of players and monsters are high, and it may cause lag.
 
Lua:
void Spawn::checkSpawn()
{
    checkSpawnEvent = 0;

    cleanup();

    uint32_t spawnCount = 0;

    for (auto& it : spawnMap) {
        uint32_t spawnId = it.first;
        if (spawnedMap.find(spawnId) != spawnedMap.end()) {
            continue;
        }

       
spawnBlock_t& sb = it.second;

        if (OTSYS_TIME() >= sb.lastSpawn + sb.interval) {

            if (!spawnMonster(spawnId, sb)) {
                sb.lastSpawn = OTSYS_TIME();
                continue;
            }

            if (++spawnCount >= static_cast<uint32_t>(g_config.getNumber(ConfigManager::RATE_SPAWN))) {
                break;
            }
        }
    }

    if (spawnedMap.size() < spawnMap.size()) {
        checkSpawnEvent = g_scheduler.addEvent(createSchedulerTask(getInterval(), std::bind(&Spawn::checkSpawn, this)));
    }
}

found the code, but really don't know what to do in here, ideas?
 
Back
Top