Skip to content

Commit

Permalink
Better abm random
Browse files Browse the repository at this point in the history
  • Loading branch information
proller committed Feb 13, 2024
1 parent a6e479b commit ea70f89
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
13 changes: 9 additions & 4 deletions src/fm_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,13 @@ class AbmWorldThread : public thread_vector
auto time_start = porting::getTimeMs();

if (abm_world_load_all <= 0) {
// Yes, very bad
#if USE_LEVELDB
if (const auto it = m_server->getEnv()
.blocks_with_abm.database.new_iterator();
it) {
for (it->SeekToFirst(); it->Valid(); it->Next()) {
const auto key = it->key().ToString();
const auto &key = it->key().ToString();
if (key.starts_with("a")) {
const v3bpos_t pos = MapDatabase::getStringAsBlock(key);
loadable_blocks.emplace_back(pos);
Expand Down Expand Up @@ -442,7 +443,7 @@ class AbmWorldThread : public thread_vector
m_server->getEnv().getServerMap().getBlockNoCreateNoEx(
pos);
if (block) {
return nullptr;
return block;
}
block = m_server->getEnv().getServerMap().emergeBlock(pos);
if (!block) {
Expand All @@ -453,6 +454,7 @@ class AbmWorldThread : public thread_vector
}
return block;
};

auto *block = load_block(pos);
if (!block) {
continue;
Expand All @@ -467,16 +469,19 @@ class AbmWorldThread : public thread_vector

++processed;

m_server->getEnv().activateBlock(block);
//m_server->getEnv().activateBlock(block);

const auto activate = m_server->getEnv().analyzeBlock(block);

m_server->getEnv().blockStep(block);

//const auto wasats = block->getActualTimestamp();
//const auto wasts = block->getTimestamp();
const auto triggers = block->abmTriggersRun(&m_server->getEnv(),
m_server->getEnv().getGameTime(), activate);
triggers_total += triggers;

//DUMP("ok", pos, cur_n, m_server->getMap().m_blocks.size(), wasts, block->getTimestamp(), wasats, block->getActualTimestamp(), m_server->getEnv().getGameTime(), triggers);
//DUMP("ok", pos, cur_n, m_server->getMap().m_blocks.size(), block->getTimestamp(), block->getActualTimestamp(), m_server->getEnv().getGameTime(), triggers);

if (!(cur_n % 10000)) {
printstat();
Expand Down
72 changes: 52 additions & 20 deletions src/fm_serverenvironment.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
#include "irr_v3d.h"
#include "map.h"
#include "scripting_server.h"
#include "serverenvironment.h"
#include "util/timetaker.h"

int ServerEnvironment::analyzeBlocks(float dtime, unsigned int max_cycle_ms) {
void ServerEnvironment::blockStep(MapBlock *block, float dtime)
{
if (!block)
return;

//u32 dtime_s = 0;
u32 stamp = block->getTimestamp();
if (!dtime && m_game_time > stamp && stamp != BLOCK_TIMESTAMP_UNDEFINED)
dtime = m_game_time - stamp;

// Set current time as timestamp
block->setTimestampNoChangedFlag(m_game_time);

if (!block->m_node_timers
.m_uptime_last) // not very good place, but minimum modifications
block->m_node_timers.m_uptime_last = m_game_time - dtime;
const auto dtime_n = m_game_time - block->m_node_timers.m_uptime_last;
block->m_node_timers.m_uptime_last = m_game_time;
// DUMP("abm random ", block->getPos(), dtime_s, dtime_n,, stamp, block->getTimestamp(), block->m_node_timers.m_uptime_last, m_game_time);

m_lbm_mgr.applyLBMs(this, block, stamp, (float)dtime_n);
if (block->isOrphan())
return;

block->step((float)dtime, [&](v3pos_t p, MapNode n, f32 d) -> bool {
return !block->isOrphan() && m_script->node_on_timer(p, n, d);
});

if (block->abm_triggers) {
//ScopeProfiler sp354(g_profiler, "ABM random trigger blocks", SPT_ADD);
block->abmTriggersRun(this, m_game_time);
}
}

int ServerEnvironment::analyzeBlocks(float dtime, unsigned int max_cycle_ms)
{
u32 n = 0, calls = 0;
const auto end_ms = porting::getTimeMs() + max_cycle_ms;
if (m_active_block_analyzed_last || m_analyze_blocks_interval.step(dtime, 1.0)) {
//if (!m_active_block_analyzed_last) infostream<<"Start ABM analyze cycle s="<<m_active_blocks.m_list.size()<<std::endl;
TimeTaker timer("env: block analyze and abm apply from " + itos(m_active_block_analyzed_last));
TimeTaker timer("env: block analyze and abm apply from " +
itos(m_active_block_analyzed_last));

std::set<v3pos_t> active_blocks_list;
//auto active_blocks_list = m_active_blocks.m_list;
Expand All @@ -17,16 +55,15 @@ int ServerEnvironment::analyzeBlocks(float dtime, unsigned int max_cycle_ms) {
active_blocks_list = m_active_blocks.m_list;
}

for(const auto & p : active_blocks_list)
{
for (const auto &p : active_blocks_list) {
if (n++ < m_active_block_analyzed_last)
continue;
else
m_active_block_analyzed_last = 0;
++calls;

MapBlock *block = m_map->getBlock(p, true);
if(!block)
if (!block)
continue;

analyzeBlock(block);
Expand All @@ -40,11 +77,11 @@ int ServerEnvironment::analyzeBlocks(float dtime, unsigned int max_cycle_ms) {
m_active_block_analyzed_last = 0;
}


if (g_settings->getBool("abm_random") && (!m_abm_random_blocks.empty() || m_abm_random_interval.step(dtime, 10.0))) {
if (g_settings->getBool("abm_random") &&
(!m_abm_random_blocks.empty() || m_abm_random_interval.step(dtime, 10.0))) {
TimeTaker timer("env: random abm " + itos(m_abm_random_blocks.size()));

const auto end_ms = porting::getTimeMs() + max_cycle_ms/10;
const auto end_ms = porting::getTimeMs() + max_cycle_ms / 10;

if (m_abm_random_blocks.empty()) {
#if !ENABLE_THREADS
Expand All @@ -54,27 +91,22 @@ int ServerEnvironment::analyzeBlocks(float dtime, unsigned int max_cycle_ms) {
{
auto lock = m_map->m_blocks.try_lock_shared_rec();
if (lock->owns_lock())
for (auto & ir : m_map->m_blocks) {
if (!ir.second || !ir.second->abm_triggers)
continue;
m_abm_random_blocks.emplace_back(ir.first);
}
for (const auto &ir : m_map->m_blocks) {
if (!ir.second || !ir.second->abm_triggers)
continue;
m_abm_random_blocks.emplace_back(ir.first);
}
}
//infostream<<"Start ABM random cycle s="<<m_abm_random_blocks.size()<<std::endl;
}

for (auto i = m_abm_random_blocks.begin(); i != m_abm_random_blocks.end(); ++i) {
MapBlock* block = m_map->getBlock(*i, true);
MapBlock *block = m_map->getBlock(*i, true);
i = m_abm_random_blocks.erase(i);
//ScopeProfiler sp221(g_profiler, "ABM random look blocks", SPT_ADD);

if (!block)
continue;
blockStep(block, dtime);

if (!block->abm_triggers)
continue;
//ScopeProfiler sp354(g_profiler, "ABM random trigger blocks", SPT_ADD);
block->abmTriggersRun(this, m_game_time);
if (porting::getTimeMs() > end_ms) {
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/serverenvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ class ServerEnvironment final : public Environment
IntervalLimiter m_abm_random_interval;
std::list<v3pos_t> m_abm_random_blocks;
public:
void blockStep(MapBlock *block, float dtime = 0);
int analyzeBlocks(float dtime, unsigned int max_cycle_ms);
u32 m_game_time_start = 0;
public:
Expand Down

0 comments on commit ea70f89

Please sign in to comment.