#include "remoteplayer.h"
#include "scripting_server.h"
#include "server.h"
-#include "voxelalgorithms.h"
#include "util/serialize.h"
#include "util/basic_macros.h"
#include "util/pointedthing.h"
*/
ABMWithState::ABMWithState(ActiveBlockModifier *abm_):
- abm(abm_),
- timer(0)
+ abm(abm_)
{
// Initialize timer to random value to spread processing
float itv = abm->getTriggerInterval();
void LBMContentMapping::deleteContents()
{
- for (std::vector<LoadingBlockModifierDef *>::iterator it = lbm_list.begin();
- it != lbm_list.end(); ++it) {
- delete *it;
+ for (auto &it : lbm_list) {
+ delete it;
}
}
lbm_list.push_back(lbm_def);
- for (std::set<std::string>::const_iterator it = lbm_def->trigger_contents.begin();
- it != lbm_def->trigger_contents.end(); ++it) {
+ for (const std::string &nodeTrigger: lbm_def->trigger_contents) {
std::set<content_t> c_ids;
- bool found = nodedef->getIds(*it, c_ids);
+ bool found = nodedef->getIds(nodeTrigger, c_ids);
if (!found) {
- content_t c_id = gamedef->allocateUnknownNodeId(*it);
+ content_t c_id = gamedef->allocateUnknownNodeId(nodeTrigger);
if (c_id == CONTENT_IGNORE) {
// Seems it can't be allocated.
- warningstream << "Could not internalize node name \"" << *it
+ warningstream << "Could not internalize node name \"" << nodeTrigger
<< "\" while loading LBM \"" << lbm_def->name << "\"." << std::endl;
continue;
}
c_ids.insert(c_id);
}
- for (std::set<content_t>::const_iterator iit =
- c_ids.begin(); iit != c_ids.end(); ++iit) {
- content_t c_id = *iit;
+ for (content_t c_id : c_ids) {
map[c_id].push_back(lbm_def);
}
}
const std::vector<LoadingBlockModifierDef *> *
LBMContentMapping::lookup(content_t c) const
{
- container_map::const_iterator it = map.find(c);
+ lbm_map::const_iterator it = map.find(c);
if (it == map.end())
return NULL;
// This first dereferences the iterator, returning
LBMManager::~LBMManager()
{
- for (std::map<std::string, LoadingBlockModifierDef *>::iterator it =
- m_lbm_defs.begin(); it != m_lbm_defs.end(); ++it) {
- delete it->second;
+ for (auto &m_lbm_def : m_lbm_defs) {
+ delete m_lbm_def.second;
}
- for (lbm_lookup_map::iterator it = m_lbm_lookup.begin();
- it != m_lbm_lookup.end(); ++it) {
- (it->second).deleteContents();
+
+ for (auto &it : m_lbm_lookup) {
+ (it.second).deleteContents();
}
}
LBMContentMapping &lbms_we_introduce_now = m_lbm_lookup[now];
LBMContentMapping &lbms_running_always = m_lbm_lookup[U32_MAX];
- for (std::map<std::string, LoadingBlockModifierDef *>::iterator it =
- m_lbm_defs.begin(); it != m_lbm_defs.end(); ++it) {
- if (it->second->run_at_every_load) {
- lbms_running_always.addLBM(it->second, gamedef);
+ for (auto &m_lbm_def : m_lbm_defs) {
+ if (m_lbm_def.second->run_at_every_load) {
+ lbms_running_always.addLBM(m_lbm_def.second, gamedef);
} else {
- lbms_we_introduce_now.addLBM(it->second, gamedef);
+ lbms_we_introduce_now.addLBM(m_lbm_def.second, gamedef);
}
}
"attempted to query on non fully set up LBMManager");
std::ostringstream oss;
- for (lbm_lookup_map::iterator it = m_lbm_lookup.begin();
- it != m_lbm_lookup.end(); ++it) {
- u32 time = it->first;
- std::vector<LoadingBlockModifierDef *> &lbm_list = it->second.lbm_list;
- for (std::vector<LoadingBlockModifierDef *>::iterator iit = lbm_list.begin();
- iit != lbm_list.end(); ++iit) {
+ for (const auto &it : m_lbm_lookup) {
+ u32 time = it.first;
+ const std::vector<LoadingBlockModifierDef *> &lbm_list = it.second.lbm_list;
+ for (const auto &lbm_def : lbm_list) {
// Don't add if the LBM runs at every load,
// then introducement time is hardcoded
// and doesn't need to be stored
- if ((*iit)->run_at_every_load)
+ if (lbm_def->run_at_every_load)
continue;
- oss << (*iit)->name << "~" << time << ";";
+ oss << lbm_def->name << "~" << time << ";";
}
}
return oss.str();
iit->second.lookup(c);
if (!lbm_list)
continue;
- for (std::vector<LoadingBlockModifierDef *>::const_iterator iit =
- lbm_list->begin(); iit != lbm_list->end(); ++iit) {
- (*iit)->trigger(env, pos + pos_of_block, n);
+ for (auto lbmdef : *lbm_list) {
+ lbmdef->trigger(env, pos + pos_of_block, n);
}
}
}
m_map(map),
m_script(scriptIface),
m_server(server),
- m_path_world(path_world),
- m_send_recommended_timer(0),
- m_active_block_interval_overload_skip(0),
- m_game_time(0),
- m_game_time_fraction_counter(0),
- m_last_clear_objects_time(0),
- m_recommended_send_interval(0.1),
- m_max_lag_estimate(0.1),
- m_player_database(NULL)
+ m_path_world(path_world)
{
// Determine which database backend to use
std::string conf_path = path_world + DIR_DELIM + "world.mt";
}
setTimeOfDay(args.exists("time_of_day") ?
- // set day to morning by default
- args.getU64("time_of_day") : 9000);
+ // set day to early morning by default
+ args.getU64("time_of_day") : 5250);
m_last_clear_objects_time = args.exists("last_clear_objects_time") ?
// If missing, do as if clearObjects was never called
// Update this one
// NOTE: This is kind of funny on a singleplayer game, but doesn't
// really matter that much.
- static const float server_step = g_settings->getFloat("dedicated_server_step");
+ static thread_local const float server_step =
+ g_settings->getFloat("dedicated_server_step");
m_recommended_send_interval = server_step;
/*
*/
{
ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG);
- for (std::vector<RemotePlayer *>::iterator i = m_players.begin();
- i != m_players.end(); ++i) {
- RemotePlayer *player = dynamic_cast<RemotePlayer *>(*i);
- assert(player);
-
+ for (RemotePlayer *player : m_players) {
// Ignore disconnected players
- if(player->peer_id == 0)
+ if (player->peer_id == 0)
continue;
// Move
- player->move(dtime, this, 100*BS);
+ player->move(dtime, this, 100 * BS);
}
}
Get player block positions
*/
std::vector<v3s16> players_blockpos;
- for (std::vector<RemotePlayer *>::iterator i = m_players.begin();
- i != m_players.end(); ++i) {
- RemotePlayer *player = dynamic_cast<RemotePlayer *>(*i);
- assert(player);
-
+ for (RemotePlayer *player: m_players) {
// Ignore disconnected players
if (player->peer_id == 0)
continue;
PlayerSAO *playersao = player->getPlayerSAO();
assert(playersao);
- v3s16 blockpos = getNodeBlockPos(
- floatToInt(playersao->getBasePosition(), BS));
- players_blockpos.push_back(blockpos);
+ players_blockpos.push_back(
+ getNodeBlockPos(floatToInt(playersao->getBasePosition(), BS)));
}
/*
Update list of active blocks, collecting changes
*/
- static const s16 active_block_range = g_settings->getS16("active_block_range");
+ static thread_local const s16 active_block_range =
+ g_settings->getS16("active_block_range");
std::set<v3s16> blocks_removed;
std::set<v3s16> blocks_added;
m_active_blocks.update(players_blockpos, active_block_range,
// Convert active objects that are no more in active blocks to static
deactivateFarObjects(false);
- for(std::set<v3s16>::iterator
- i = blocks_removed.begin();
- i != blocks_removed.end(); ++i) {
- v3s16 p = *i;
-
- /* infostream<<"Server: Block " << PP(p)
- << " became inactive"<<std::endl; */
-
+ for (const v3s16 &p: blocks_removed) {
MapBlock *block = m_map->getBlockNoCreateNoEx(p);
- if(block==NULL)
+ if (!block)
continue;
// Set current time as timestamp (and let it set ChangedFlag)
Handle added blocks
*/
- for(std::set<v3s16>::iterator
- i = blocks_added.begin();
- i != blocks_added.end(); ++i)
- {
- v3s16 p = *i;
-
+ for (const v3s16 &p: blocks_added) {
MapBlock *block = m_map->getBlockOrEmerge(p);
- if(block==NULL){
+ if (!block) {
m_active_blocks.m_list.erase(p);
continue;
}
activateBlock(block);
- /* infostream<<"Server: Block " << PP(p)
- << " became active"<<std::endl; */
}
}
float dtime = m_cache_nodetimer_interval;
- for(std::set<v3s16>::iterator
- i = m_active_blocks.m_list.begin();
- i != m_active_blocks.m_list.end(); ++i)
- {
- v3s16 p = *i;
-
- /*infostream<<"Server: Block ("<<p.X<<","<<p.Y<<","<<p.Z
- <<") being handled"<<std::endl;*/
-
+ for (const v3s16 &p: m_active_blocks.m_list) {
MapBlock *block = m_map->getBlockNoCreateNoEx(p);
- if(block==NULL)
+ if (!block)
continue;
// Reset block usage timer
MOD_REASON_BLOCK_EXPIRED);
// Run node timers
- std::vector<NodeTimer> elapsed_timers =
- block->m_node_timers.step((float)dtime);
+ std::vector<NodeTimer> elapsed_timers = block->m_node_timers.step(dtime);
if (!elapsed_timers.empty()) {
MapNode n;
- for (std::vector<NodeTimer>::iterator i = elapsed_timers.begin();
- i != elapsed_timers.end(); ++i) {
- n = block->getNodeNoEx(i->position);
- p = i->position + block->getPosRelative();
- if (m_script->node_on_timer(p, n, i->elapsed)) {
+ v3s16 p2;
+ for (const NodeTimer &elapsed_timer: elapsed_timers) {
+ n = block->getNodeNoEx(elapsed_timer.position);
+ p2 = elapsed_timer.position + block->getPosRelative();
+ if (m_script->node_on_timer(p2, n, elapsed_timer.elapsed)) {
block->setNodeTimer(NodeTimer(
- i->timeout, 0, i->position));
+ elapsed_timer.timeout, 0, elapsed_timer.position));
}
}
}
}
if (m_active_block_modifier_interval.step(dtime, m_cache_abm_interval))
- do{ // breakable
- if(m_active_block_interval_overload_skip > 0){
+ do { // breakable
+ if (m_active_block_interval_overload_skip > 0) {
ScopeProfiler sp(g_profiler, "SEnv: ABM overload skips");
m_active_block_interval_overload_skip--;
break;
// Initialize handling of ActiveBlockModifiers
ABMHandler abmhandler(m_abms, m_cache_abm_interval, this, true);
- for(std::set<v3s16>::iterator
- i = m_active_blocks.m_list.begin();
- i != m_active_blocks.m_list.end(); ++i)
- {
- v3s16 p = *i;
-
- /*infostream<<"Server: Block ("<<p.X<<","<<p.Y<<","<<p.Z
- <<") being handled"<<std::endl;*/
-
+ for (const v3s16 &p : m_active_blocks.m_list) {
MapBlock *block = m_map->getBlockNoCreateNoEx(p);
- if(block == NULL)
+ if (!block)
continue;
// Set current time as timestamp
u32 time_ms = timer.stop(true);
u32 max_time_ms = 200;
- if(time_ms > max_time_ms){
+ if (time_ms > max_time_ms) {
warningstream<<"active block modifiers took "
<<time_ms<<"ms (longer than "
<<max_time_ms<<"ms)"<<std::endl;
/*
Manage active objects
*/
- if(m_object_management_interval.step(dtime, 0.5))
- {
+ if (m_object_management_interval.step(dtime, 0.5)) {
ScopeProfiler sp(g_profiler, "SEnv: remove removed objs avg /.5s", SPT_AVG);
/*
Remove objects that satisfy (m_removed && m_known_by_count==0)
void ServerEnvironment::deleteParticleSpawner(u32 id, bool remove_from_object)
{
m_particle_spawners.erase(id);
- std::unordered_map<u32, u16>::iterator it = m_particle_spawner_attachments.find(id);
+ const auto &it = m_particle_spawner_attachments.find(id);
if (it != m_particle_spawner_attachments.end()) {
- u16 obj_id = (*it).second;
+ u16 obj_id = it->second;
ServerActiveObject *sao = getActiveObject(obj_id);
if (sao != NULL && remove_from_object) {
sao->detachParticleSpawner(id);
return message;
}
+void ServerEnvironment::getSelectedActiveObjects(
+ const core::line3d<f32> &shootline_on_map,
+ std::vector<PointedThing> &objects)
+{
+ std::vector<u16> objectIds;
+ getObjectsInsideRadius(objectIds, shootline_on_map.start,
+ shootline_on_map.getLength() + 10.0f);
+ const v3f line_vector = shootline_on_map.getVector();
+
+ for (u32 i = 0; i < objectIds.size(); i++) {
+ ServerActiveObject* obj = getActiveObject(objectIds[i]);
+
+ aabb3f selection_box;
+ if (!obj->getSelectionBox(&selection_box))
+ continue;
+
+ v3f pos = obj->getBasePosition();
+
+ aabb3f offsetted_box(selection_box.MinEdge + pos,
+ selection_box.MaxEdge + pos);
+
+ v3f current_intersection;
+ v3s16 current_normal;
+ if (boxLineCollision(offsetted_box, shootline_on_map.start, line_vector,
+ ¤t_intersection, ¤t_normal)) {
+ objects.push_back(PointedThing(
+ (s16) objectIds[i], current_intersection, current_normal,
+ (current_intersection - shootline_on_map.start).getLengthSQ()));
+ }
+ }
+}
+
/*
************ Private methods *************
*/