X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fenvironment.cpp;h=51255b91847113885e552db5febe0a9a07a167f5;hb=8e5b33d3590719ff2cf1138f563e928d05754d7a;hp=10ebd412729a394d6e651c238a2327fb4ae89f2b;hpb=1cc1b93e65d6d88e20786383cb09caf22a8c6494;p=oweals%2Fminetest.git diff --git a/src/environment.cpp b/src/environment.cpp index 10ebd4127..51255b918 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -43,6 +43,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #endif #include "daynightratio.h" #include "map.h" +#include "util/serialize.h" #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")" @@ -202,7 +203,8 @@ void Environment::printPlayers(std::ostream &o) u32 Environment::getDayNightRatio() { - return time_to_daynight_ratio(m_time_of_day); + bool smooth = (g_settings->getS32("enable_shaders") != 0); + return time_to_daynight_ratio(m_time_of_day_f*24000, smooth); } void Environment::stepTimeOfDay(float dtime) @@ -328,7 +330,8 @@ ServerEnvironment::ServerEnvironment(ServerMap *map, lua_State *L, m_send_recommended_timer(0), m_active_block_interval_overload_skip(0), m_game_time(0), - m_game_time_fraction_counter(0) + m_game_time_fraction_counter(0), + m_recommended_send_interval(0.1) { } @@ -790,7 +793,7 @@ void ServerEnvironment::activateBlock(MapBlock *block, u32 additional_dtime) < elapsed_timers = @@ -801,7 +804,8 @@ void ServerEnvironment::activateBlock(MapBlock *block, u32 additional_dtime) i = elapsed_timers.begin(); i != elapsed_timers.end(); i++){ n = block->getNodeNoEx(i->first); - if(scriptapi_node_on_timer(m_lua,i->first,n,i->second.elapsed)) + v3s16 p = i->first + block->getPosRelative(); + if(scriptapi_node_on_timer(m_lua,p,n,i->second.elapsed)) block->setNodeTimer(i->first,NodeTimer(i->second.timeout,0)); } } @@ -816,6 +820,45 @@ void ServerEnvironment::addActiveBlockModifier(ActiveBlockModifier *abm) m_abms.push_back(ABMWithState(abm)); } +bool ServerEnvironment::setNode(v3s16 p, const MapNode &n) +{ + INodeDefManager *ndef = m_gamedef->ndef(); + MapNode n_old = m_map->getNodeNoEx(p); + // Call destructor + if(ndef->get(n_old).has_on_destruct) + scriptapi_node_on_destruct(m_lua, p, n_old); + // Replace node + bool succeeded = m_map->addNodeWithEvent(p, n); + if(!succeeded) + return false; + // Call post-destructor + if(ndef->get(n_old).has_after_destruct) + scriptapi_node_after_destruct(m_lua, p, n_old); + // Call constructor + if(ndef->get(n).has_on_construct) + scriptapi_node_on_construct(m_lua, p, n); + return true; +} + +bool ServerEnvironment::removeNode(v3s16 p) +{ + INodeDefManager *ndef = m_gamedef->ndef(); + MapNode n_old = m_map->getNodeNoEx(p); + // Call destructor + if(ndef->get(n_old).has_on_destruct) + scriptapi_node_on_destruct(m_lua, p, n_old); + // Replace with air + // This is slightly optimized compared to addNodeWithEvent(air) + bool succeeded = m_map->removeNodeWithEvent(p); + if(!succeeded) + return false; + // Call post-destructor + if(ndef->get(n_old).has_after_destruct) + scriptapi_node_after_destruct(m_lua, p, n_old); + // Air doesn't require constructor + return true; +} + std::set ServerEnvironment::getObjectsInsideRadius(v3f pos, float radius) { std::set objects; @@ -939,6 +982,11 @@ void ServerEnvironment::step(float dtime) /* Step time of day */ stepTimeOfDay(dtime); + // Update this one + // NOTE: This is kind of funny on a singleplayer game, but doesn't + // really matter that much. + m_recommended_send_interval = g_settings->getFloat("dedicated_server_step"); + /* Increment game time */ @@ -1249,10 +1297,11 @@ u16 getFreeServerActiveObjectId( u16 ServerEnvironment::addActiveObject(ServerActiveObject *object) { assert(object); - u16 id = addActiveObjectRaw(object, true); + u16 id = addActiveObjectRaw(object, true, 0); return id; } +#if 0 bool ServerEnvironment::addActiveObjectAsStatic(ServerActiveObject *obj) { assert(obj); @@ -1295,6 +1344,7 @@ bool ServerEnvironment::addActiveObjectAsStatic(ServerActiveObject *obj) return succeeded; } +#endif /* Finds out what new objects have been added to @@ -1408,7 +1458,7 @@ ActiveObjectMessage ServerEnvironment::getActiveObjectMessage() */ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object, - bool set_changed) + bool set_changed, u32 dtime_s) { assert(object); if(object->getId() == 0){ @@ -1448,7 +1498,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object, // Register reference in scripting api (must be done before post-init) scriptapi_add_object_reference(m_lua, object); // Post-initialize object - object->addedToEnvironment(); + object->addedToEnvironment(dtime_s); // Add static data to block if(object->isStaticAllowed()) @@ -1515,13 +1565,15 @@ void ServerEnvironment::removeRemovedObjects() */ if(obj->m_static_exists && obj->m_removed) { - MapBlock *block = m_map->emergeBlock(obj->m_static_block); - if(block) - { + MapBlock *block = m_map->emergeBlock(obj->m_static_block, false); + if (block) { block->m_static_objects.remove(id); block->raiseModified(MOD_STATE_WRITE_NEEDED, "removeRemovedObjects"); obj->m_static_exists = false; + } else { + infostream << "failed to emerge block from which " + "an object to be removed was loaded from. id="< new_stored; // Loop through stored static objects @@ -1639,7 +1691,7 @@ void ServerEnvironment::activateObjects(MapBlock *block) <<"activated static object pos="<removeFromScene(); + obj->removeFromScene(true); delete obj; m_active_objects.remove(id); }