X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fenvironment.cpp;h=e805a59a5a453e75e41531fa0b8d27c06f569dec;hb=fd70f4f2f040b64064676706e41d6da90c2b00db;hp=6bbc715d078372fb3fef5f73ba3c7a1f2bfb3643;hpb=f6fc39e69a0a621480bec9ceafd154ec83cf0100;p=minetest.git diff --git a/src/environment.cpp b/src/environment.cpp index 6bbc715d0..e805a59a5 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -36,6 +36,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef SERVER #include "clientmap.h" #include "localplayer.h" +#include "mapblock_mesh.h" #include "event.h" #endif #include "daynightratio.h" @@ -54,6 +55,7 @@ Environment::Environment(): m_enable_day_night_ratio_override(false), m_day_night_ratio_override(0.0f) { + m_cache_enable_shaders = g_settings->getBool("enable_shaders"); } Environment::~Environment() @@ -100,6 +102,18 @@ void Environment::removePlayer(u16 peer_id) } } +void Environment::removePlayer(const char *name) +{ + for (std::list::iterator it = m_players.begin(); + it != m_players.end(); ++it) { + if (strcmp((*it)->getName(), name) == 0) { + delete *it; + m_players.erase(it); + return; + } + } +} + Player * Environment::getPlayer(u16 peer_id) { for(std::list::iterator i = m_players.begin(); @@ -193,8 +207,7 @@ u32 Environment::getDayNightRatio() { if(m_enable_day_night_ratio_override) return m_day_night_ratio_override; - bool smooth = g_settings->getBool("enable_shaders"); - return time_to_daynight_ratio(m_time_of_day_f*24000, smooth); + return time_to_daynight_ratio(m_time_of_day_f*24000, m_cache_enable_shaders); } void Environment::setTimeOfDaySpeed(float speed) @@ -332,10 +345,12 @@ void ActiveBlockList::update(std::list &active_positions, */ ServerEnvironment::ServerEnvironment(ServerMap *map, - GameScripting *scriptIface, IGameDef *gamedef): + GameScripting *scriptIface, IGameDef *gamedef, + const std::string &path_world) : m_map(map), m_script(scriptIface), m_gamedef(gamedef), + m_path_world(path_world), m_send_recommended_timer(0), m_active_block_interval_overload_skip(0), m_game_time(0), @@ -401,196 +416,76 @@ bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16 return true; } -void ServerEnvironment::serializePlayers(const std::string &savedir) +void ServerEnvironment::saveLoadedPlayers() { - std::string players_path = savedir + "/players"; + std::string players_path = m_path_world + DIR_DELIM "players"; fs::CreateDir(players_path); - std::set saved_players; - - std::vector player_files = fs::GetDirListing(players_path); - for(u32 i=0; icheckModified()) - { - // Open file and serialize - std::ostringstream ss(std::ios_base::binary); - player->serialize(ss); - if(!fs::safeWriteToFile(path, ss.str())) - { - infostream<<"Failed to write "<::iterator it = m_players.begin(); + it != m_players.end(); + ++it) { + RemotePlayer *player = static_cast(*it); + if (player->checkModified()) { + player->save(players_path); } } +} - for(std::list::iterator i = m_players.begin(); - i != m_players.end(); ++i) - { - Player *player = *i; - if(saved_players.find(player) != saved_players.end()) - { - /*infostream<<"Player "<getName() - <<" was already saved."<getName(); - // Don't save unnamed player - if(playername == "") - { - //infostream<<"Not saving unnamed player."<getName()<<" to " - <serialize(ss); - if(!fs::safeWriteToFile(path, ss.str())) - { - infostream<<"Failed to write "<(getPlayer(playername.c_str())); + if (player) { + player->save(players_path); } - - //infostream<<"Saved "< player_files = fs::GetDirListing(players_path); - for(u32 i=0; i(getPlayer(playername.c_str())); + bool newplayer = false; + bool found = false; + if (!player) { + player = new RemotePlayer(m_gamedef, playername.c_str()); + newplayer = true; + } - // Load player - { - verbosestream<<"Reading player "<deSerialize(is, player_files[i].name); + RemotePlayer testplayer(m_gamedef, ""); + std::string path = players_path + playername; + for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) { + // Open file and deserialize + std::ifstream is(path.c_str(), std::ios_base::binary); + if (!is.good()) { + return NULL; } - - if(newplayer) - { - addPlayer(player); + testplayer.deSerialize(is, path); + is.close(); + if (testplayer.getName() == playername) { + *player = testplayer; + found = true; + break; } + path = players_path + playername + itos(i); } + if (!found) { + infostream << "Player file for player " << playername + << " not found" << std::endl; + return NULL; + } + if (newplayer) { + addPlayer(player); + } + player->setModified(false); + return player; } -void ServerEnvironment::saveMeta(const std::string &savedir) +void ServerEnvironment::saveMeta() { - std::string path = savedir + "/env_meta.txt"; + std::string path = m_path_world + DIR_DELIM "env_meta.txt"; // Open file and serialize std::ostringstream ss(std::ios_base::binary); @@ -609,44 +504,35 @@ void ServerEnvironment::saveMeta(const std::string &savedir) } } -void ServerEnvironment::loadMeta(const std::string &savedir) +void ServerEnvironment::loadMeta() { - std::string path = savedir + "/env_meta.txt"; + std::string path = m_path_world + DIR_DELIM "env_meta.txt"; // Open file and deserialize std::ifstream is(path.c_str(), std::ios_base::binary); - if(is.good() == false) - { - infostream<<"ServerEnvironment::loadMeta(): Failed to open " - <ndef(); MapNode n_old = m_map->getNodeNoEx(p); + // Call destructor - if(ndef->get(n_old).has_on_destruct) + if (ndef->get(n_old).has_on_destruct) m_script->node_on_destruct(p, n_old); + // Replace node - bool succeeded = m_map->addNodeWithEvent(p, n); - if(!succeeded) + if (!m_map->addNodeWithEvent(p, n)) return false; + + // Update active VoxelManipulator if a mapgen thread + m_map->updateVManip(p); + // Call post-destructor - if(ndef->get(n_old).has_after_destruct) + if (ndef->get(n_old).has_after_destruct) m_script->node_after_destruct(p, n_old); + // Call constructor - if(ndef->get(n).has_on_construct) + if (ndef->get(n).has_on_construct) m_script->node_on_construct(p, n); + return true; } @@ -905,24 +798,36 @@ 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) + if (ndef->get(n_old).has_on_destruct) m_script->node_on_destruct(p, n_old); + // Replace with air // This is slightly optimized compared to addNodeWithEvent(air) - bool succeeded = m_map->removeNodeWithEvent(p); - if(!succeeded) + if (!m_map->removeNodeWithEvent(p)) return false; + + // Update active VoxelManipulator if a mapgen thread + m_map->updateVManip(p); + // Call post-destructor - if(ndef->get(n_old).has_after_destruct) + if (ndef->get(n_old).has_after_destruct) m_script->node_after_destruct(p, n_old); + // Air doesn't require constructor return true; } bool ServerEnvironment::swapNode(v3s16 p, const MapNode &n) { - return m_map->addNodeWithEvent(p, n, false); + if (!m_map->addNodeWithEvent(p, n, false)) + return false; + + // Update active VoxelManipulator if a mapgen thread + m_map->updateVManip(p); + + return true; } std::set ServerEnvironment::getObjectsInsideRadius(v3f pos, float radius) @@ -946,11 +851,10 @@ void ServerEnvironment::clearAllObjects() { infostream<<"ServerEnvironment::clearAllObjects(): " <<"Removing all active objects"< objects_to_remove; + std::vector objects_to_remove; for(std::map::iterator i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; if(obj->getType() == ACTIVEOBJECT_TYPE_PLAYER) continue; @@ -983,15 +887,15 @@ void ServerEnvironment::clearAllObjects() // Id to be removed from m_active_objects objects_to_remove.push_back(id); } + // Remove references from m_active_objects - for(std::list::iterator i = objects_to_remove.begin(); - i != objects_to_remove.end(); ++i) - { + for(std::vector::iterator i = objects_to_remove.begin(); + i != objects_to_remove.end(); ++i) { m_active_objects.erase(*i); } // Get list of loaded blocks - std::list loaded_blocks; + std::vector loaded_blocks; infostream<<"ServerEnvironment::clearAllObjects(): " <<"Listing all loaded blocks"<listAllLoadedBlocks(loaded_blocks); @@ -1000,7 +904,7 @@ void ServerEnvironment::clearAllObjects() < loadable_blocks; + std::vector loadable_blocks; infostream<<"ServerEnvironment::clearAllObjects(): " <<"Listing all loadable blocks"<listAllLoadableBlocks(loadable_blocks); @@ -1010,9 +914,8 @@ void ServerEnvironment::clearAllObjects() <<", now clearing"<::iterator i = loaded_blocks.begin(); - i != loaded_blocks.end(); ++i) - { + for(std::vector::iterator i = loaded_blocks.begin(); + i != loaded_blocks.end(); ++i) { v3s16 p = *i; MapBlock *block = m_map->getBlockNoCreateNoEx(p); assert(block); @@ -1026,9 +929,8 @@ void ServerEnvironment::clearAllObjects() u32 num_blocks_checked = 0; u32 num_blocks_cleared = 0; u32 num_objs_cleared = 0; - for(std::list::iterator i = loadable_blocks.begin(); - i != loadable_blocks.end(); ++i) - { + for(std::vector::iterator i = loadable_blocks.begin(); + i != loadable_blocks.end(); ++i) { v3s16 p = *i; MapBlock *block = m_map->emergeBlock(p, false); if(!block){ @@ -1064,9 +966,8 @@ void ServerEnvironment::clearAllObjects() m_map->unloadUnreferencedBlocks(); // Drop references that were added above - for(std::list::iterator i = loaded_blocks.begin(); - i != loaded_blocks.end(); ++i) - { + for(std::vector::iterator i = loaded_blocks.begin(); + i != loaded_blocks.end(); ++i) { v3s16 p = *i; MapBlock *block = m_map->getBlockNoCreateNoEx(p); assert(block); @@ -1321,11 +1222,6 @@ void ServerEnvironment::step(float dtime) i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; - // Remove non-peaceful mobs on peaceful mode - if(g_settings->getBool("only_peaceful_mobs")){ - if(!obj->isPeaceful()) - obj->m_removed = true; - } // Don't step if is to be removed or stored statically if(obj->m_removed || obj->m_pending_deactivation) continue; @@ -1446,11 +1342,17 @@ bool ServerEnvironment::addActiveObjectAsStatic(ServerActiveObject *obj) inside a radius around a position */ void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius, + s16 player_radius, std::set ¤t_objects, std::set &added_objects) { v3f pos_f = intToFloat(pos, BS); f32 radius_f = radius * BS; + f32 player_radius_f = player_radius * BS; + + if (player_radius_f < 0) + player_radius_f = 0; + /* Go through the object list, - discard m_removed objects, @@ -1470,12 +1372,15 @@ void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius, // Discard if removed or deactivating if(object->m_removed || object->m_pending_deactivation) continue; - if(object->unlimitedTransferDistance() == false){ + + f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f); + if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) { // Discard if too far - f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f); - if(distance_f > radius_f) + if (distance_f > player_radius_f && player_radius_f != 0) continue; - } + } else if (distance_f > radius_f) + continue; + // Discard if already on current_objects std::set::iterator n; n = current_objects.find(id); @@ -1491,11 +1396,17 @@ void ServerEnvironment::getAddedActiveObjects(v3s16 pos, s16 radius, inside a radius around a position */ void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius, + s16 player_radius, std::set ¤t_objects, std::set &removed_objects) { v3f pos_f = intToFloat(pos, BS); f32 radius_f = radius * BS; + f32 player_radius_f = player_radius * BS; + + if (player_radius_f < 0) + player_radius_f = 0; + /* Go through current_objects; object is removed if: - object is not found in m_active_objects (this is actually an @@ -1524,19 +1435,15 @@ void ServerEnvironment::getRemovedActiveObjects(v3s16 pos, s16 radius, continue; } - // If transfer distance is unlimited, don't remove - if(object->unlimitedTransferDistance()) - continue; - f32 distance_f = object->getBasePosition().getDistanceFrom(pos_f); - - if(distance_f >= radius_f) - { - removed_objects.insert(id); + if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) { + if (distance_f <= player_radius_f || player_radius_f == 0) + continue; + } else if (distance_f <= radius_f) continue; - } - - // Not removed + + // Object is no longer visible + removed_objects.insert(id); } } @@ -1631,11 +1538,10 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object, */ void ServerEnvironment::removeRemovedObjects() { - std::list objects_to_remove; + std::vector objects_to_remove; for(std::map::iterator i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + i != m_active_objects.end(); ++i) { u16 id = i->first; ServerActiveObject* obj = i->second; // This shouldn't happen but check it @@ -1705,13 +1611,13 @@ void ServerEnvironment::removeRemovedObjects() // Delete if(obj->environmentDeletes()) delete obj; + // Id to be removed from m_active_objects objects_to_remove.push_back(id); } // Remove references from m_active_objects - for(std::list::iterator i = objects_to_remove.begin(); - i != objects_to_remove.end(); ++i) - { + for(std::vector::iterator i = objects_to_remove.begin(); + i != objects_to_remove.end(); ++i) { m_active_objects.erase(*i); } } @@ -1755,10 +1661,11 @@ static void print_hexdump(std::ostream &o, const std::string &data) */ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s) { - if(block==NULL) + if(block == NULL) return; + // Ignore if no stored objects (to not set changed flag) - if(block->m_static_objects.m_stored.size() == 0) + if(block->m_static_objects.m_stored.empty()) return; verbosestream<<"ServerEnvironment::activateObjects(): " <<"activating objects of block "<getPos()) @@ -1782,17 +1689,14 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s) std::list new_stored; for(std::list::iterator i = block->m_static_objects.m_stored.begin(); - i != block->m_static_objects.m_stored.end(); ++i) - { - /*infostream<<"Server: Creating an active object from " - <<"static data"<m_static_objects.m_stored.end(); ++i) { StaticObject &s_obj = *i; + // Create an active object from the data ServerActiveObject *obj = ServerActiveObject::create - (s_obj.type, this, 0, s_obj.pos, s_obj.data); + ((ActiveObjectType) s_obj.type, this, 0, s_obj.pos, s_obj.data); // If couldn't create object, store static data back. - if(obj==NULL) - { + if(obj == NULL) { errorstream<<"ServerEnvironment::activateObjects(): " <<"failed to create active object from static object " <<"in block "<move(dtime, this, 100*BS); } - - // Update lighting on all players on client - float light = 1.0; - try{ - // Get node at head - v3s16 p = player->getLightPosition(); - MapNode n = m_map->getNode(p); - light = n.getLightBlendF1((float)getDayNightRatio()/1000, m_gamedef->ndef()); - } - catch(InvalidPositionException &e){ - light = blend_light_f1((float)getDayNightRatio()/1000, LIGHT_SUN, 0); - } - player->light = light; } - + + // Update lighting on local player (used for wield item) + u32 day_night_ratio = getDayNightRatio(); + { + // Get node at head + + // On InvalidPositionException, use this as default + // (day: LIGHT_SUN, night: 0) + MapNode node_at_lplayer(CONTENT_AIR, 0x0f, 0); + + v3s16 p = lplayer->getLightPosition(); + node_at_lplayer = m_map->getNodeNoEx(p); + + u16 light = getInteriorLight(node_at_lplayer, 0, m_gamedef->ndef()); + u8 day = light & 0xff; + u8 night = (light >> 8) & 0xff; + finalColorBlend(lplayer->light_color, day, night, day_night_ratio); + } + /* Step active objects and update lighting of them */ @@ -2453,15 +2362,16 @@ void ClientEnvironment::step(float dtime) { // Update lighting u8 light = 0; - try{ - // Get node at head - v3s16 p = obj->getLightPosition(); - MapNode n = m_map->getNode(p); - light = n.getLightBlend(getDayNightRatio(), m_gamedef->ndef()); - } - catch(InvalidPositionException &e){ - light = blend_light(getDayNightRatio(), LIGHT_SUN, 0); - } + bool pos_ok; + + // Get node at head + v3s16 p = obj->getLightPosition(); + MapNode n = m_map->getNodeNoEx(p, &pos_ok); + if (pos_ok) + light = n.getLightBlend(day_night_ratio, m_gamedef->ndef()); + else + light = blend_light(day_night_ratio, LIGHT_SUN, 0); + obj->updateLight(light); } } @@ -2552,15 +2462,16 @@ u16 ClientEnvironment::addActiveObject(ClientActiveObject *object) object->addToScene(m_smgr, m_texturesource, m_irr); { // Update lighting immediately u8 light = 0; - try{ - // Get node at head - v3s16 p = object->getLightPosition(); - MapNode n = m_map->getNode(p); + bool pos_ok; + + // Get node at head + v3s16 p = object->getLightPosition(); + MapNode n = m_map->getNodeNoEx(p, &pos_ok); + if (pos_ok) light = n.getLightBlend(getDayNightRatio(), m_gamedef->ndef()); - } - catch(InvalidPositionException &e){ + else light = blend_light(getDayNightRatio(), LIGHT_SUN, 0); - } + object->updateLight(light); } return object->getId(); @@ -2570,7 +2481,7 @@ void ClientEnvironment::addActiveObject(u16 id, u8 type, const std::string &init_data) { ClientActiveObject* obj = - ClientActiveObject::create(type, m_gamedef, this); + ClientActiveObject::create((ActiveObjectType) type, m_gamedef, this); if(obj == NULL) { infostream<<"ClientEnvironment::addActiveObject(): " @@ -2649,6 +2560,8 @@ void ClientEnvironment::damageLocalPlayer(u8 damage, bool handle_hp) assert(lplayer); if(handle_hp){ + if (lplayer->hp == 0) // Don't damage a dead player + return; if(lplayer->hp > damage) lplayer->hp -= damage; else