• 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 0.X Game::checkCreatureWalk and Game::checkCreatures optimization

potinho

Advanced OT User
Joined
Oct 11, 2009
Messages
1,458
Solutions
17
Reaction score
175
Location
Brazil
Hi guys,

I have moved my server from a Ryzen VPS to a regular VPS and I noticed that the use of the functions mentioned in the title has increased a lot. For me, it is not an option to migrate now because I am on a machine that has a stable network, which did not happen on the other one. Is it possible to optimize these functions without affecting the behavior on the server too much?

C++:
void Game::checkCreatures(uint64_t index)
{
    checkCreatureEvent = g_scheduler.addEvent(createSchedulerTask(
        EVENT_CHECK_CREATURE_INTERVAL, std::bind(&Game::checkCreatures, this, (index + 1) % EVENT_CREATURECOUNT)));

    std::vector<Creature*>::iterator it;
    std::vector<Creature*>& checkCreatureVector = checkCreatureVectors[index];

    for(it = checkCreatureVector.begin(); it != checkCreatureVector.end(); )
    {
        if((*it)->creatureCheck)
        {
            if ((*it)->getHealth() > 0 || !(*it)->onDeath())
            {
                (*it)->onThink(EVENT_CREATURE_THINK_INTERVAL);
                (*it)->onAttacking(EVENT_CREATURE_THINK_INTERVAL);
                (*it)->executeConditions(EVENT_CREATURE_THINK_INTERVAL);
            }
            ++it;
        }
        else
        {
            (*it)->inCheckCreaturesVector = false;
            freeThing(*it);
            it = checkCreatureVector.erase(it);
        }
    }

    cleanup();
    g_stats.playersOnline = getPlayersOnline();
}
void Game::checkCreatureWalk(uint32_t creatureId)
{
    Creature* creature = getCreatureByID(creatureId);
    if(!creature || creature->getHealth() < 1)
        return;

    creature->onWalk();
    cleanup();
}
void Game::updateCreatureWalk(uint32_t creatureId)
{
    Creature* creature = getCreatureByID(creatureId);
        if(creature && creature->getHealth() > 0)
        creature->goToFollowCreature();
}

void Game::checkCreatureAttack(uint32_t creatureId)
{
    Creature* creature = getCreatureByID(creatureId);
    if(creature && creature->getHealth() > 0)
        creature->onAttacking(0);
}
void Game::addCreatureCheck(Creature* creature)
{
    if(creature->isRemoved())
        return;

    creature->creatureCheck = true;
    if(creature->inCheckCreaturesVector) //already in a vector, or about to be added
        return;

    creature->inCheckCreaturesVector = true;
    checkCreatureVectors[uniform_random(0, EVENT_CREATURECOUNT - 1)].push_back(creature);
    creature->addRef();
}

void Game::removeCreatureCheck(Creature* creature)
{
    if(!creature->inCheckCreaturesVector) //not in any vector
        return;

    creature->creatureCheck = false;
}



1745967056990.webp1745967043509.webp
 

Similar threads

Back
Top