Skip to content

Commit

Permalink
add npc.globalUpdareRate config
Browse files Browse the repository at this point in the history
  • Loading branch information
AmyrAhmady committed Feb 4, 2025
1 parent b79f747 commit b000574
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Server/Components/NPCs/npcs_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ void NPCComponent::onTick(Microseconds elapsed, TimePoint now)
// Clean this pool because it is now processed
markedForKick.clear();

// Stop further processings if update rate is smaller than elapsed time
if (duration_cast<Milliseconds>(now - lastUpdate).count() <= getGeneralNPCUpdateRate())
{
return;
}

for (auto& npc : storage)
{
static_cast<NPC*>(npc)->tick(elapsed, now);
Expand Down
30 changes: 29 additions & 1 deletion Server/Components/NPCs/npcs_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,41 @@ class NPCComponent final : public INPCComponent, public CoreEventHandler, public
return *footSyncRate;
}

int getGeneralNPCUpdateRate() const
{
return *generalNPCUpdateRateMS;
}

void provideConfiguration(ILogger& logger, IEarlyConfig& config, bool defaults) override
{
int defaultGeneralNPCUpdateRateMS = 50;
if (defaults)
{
config.setInt("npc.globalUpdareRate", defaultGeneralNPCUpdateRateMS);
}
else
{
// Set default values if options are not set.
if (config.getType("npc.globalUpdareRate") == ConfigOptionType_None)
{
config.setInt("npc.globalUpdareRate", defaultGeneralNPCUpdateRateMS);
}
}

generalNPCUpdateRateMS = config.getInt("npc.globalUpdareRate");
}

private:
ICore* core = nullptr;
NPCNetwork npcNetwork;
DefaultEventDispatcher<NPCEventHandler> eventDispatcher;
MarkedDynamicPoolStorage<NPC, INPC, 0, NPC_POOL_SIZE> storage;
int* footSyncRate = nullptr;
bool shouldCallCustomEvents = true;
TimePoint lastUpdate;

// Update rates
int* generalNPCUpdateRateMS = nullptr;
int* footSyncRate = nullptr;

// Components
IVehiclesComponent* vehicles = nullptr;
Expand Down

0 comments on commit b000574

Please sign in to comment.