From 414f0275cfa014423394cc9c37ecb0151c3bd46c Mon Sep 17 00:00:00 2001 From: kwolekr Date: Sat, 30 Mar 2013 19:12:23 -0400 Subject: Optimize CNodeDefManager::getIds --- src/nodedef.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/nodedef.h | 3 +++ 2 files changed, 39 insertions(+) (limited to 'src') diff --git a/src/nodedef.cpp b/src/nodedef.cpp index d41df5c3b..59db68a36 100644 --- a/src/nodedef.cpp +++ b/src/nodedef.cpp @@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "settings.h" #include "nameidmapping.h" #include "util/serialize.h" +//#include "profiler.h" // For TimeTaker /* NodeBox @@ -452,6 +453,7 @@ public: virtual void getIds(const std::string &name, std::set &result) const { + //TimeTaker t("getIds", NULL, PRECISION_MICRO); if(name.substr(0,6) != "group:"){ content_t id = CONTENT_IGNORE; if(getId(name, id)) @@ -459,6 +461,20 @@ public: return; } std::string group = name.substr(6); + +#if 1 // Optimized version, takes less than 1 microsecond at -O1 + std::map::const_iterator + i = m_group_to_items.find(group); + if (i == m_group_to_items.end()) + return; + + const GroupItems &items = i->second; + for (GroupItems::const_iterator j = items.begin(); + j != items.end(); ++j) { + if ((*j).second != 0) + result.insert((*j).first); + } +#else // Old version, takes about ~150-200us at -O1 for(u16 id=0; id<=MAX_CONTENT; id++) { const ContentFeatures &f = m_content_features[id]; @@ -467,6 +483,8 @@ public: if(itemgroup_get(f.groups, group) != 0) result.insert(id); } +#endif + //printf("getIds: %dus\n", t.stop()); } virtual const ContentFeatures& get(const std::string &name) const { @@ -498,6 +516,21 @@ public: m_content_features[c] = def; if(def.name != "") addNameIdMapping(c, def.name); + + // Add this content to the list of all groups it belongs to + for (ItemGroupList::const_iterator i = def.groups.begin(); + i != def.groups.end(); ++i) { + std::string group_name = i->first; + + std::map::iterator + j = m_group_to_items.find(group_name); + if (j == m_group_to_items.end()) { + m_group_to_items[group_name].push_back(std::make_pair(c, i->second)); + } else { + GroupItems &items = j->second; + items.push_back(std::make_pair(c, i->second)); + } + } } virtual content_t set(const std::string &name, const ContentFeatures &def) @@ -787,6 +820,9 @@ private: // item aliases too. Updated by updateAliases() // Note: Not serialized. std::map m_name_id_mapping_with_aliases; + // A mapping from groups to a list of content_ts (and their levels) + // that belong to it. Necessary for a direct lookup in getIds(). + std::map m_group_to_items; }; IWritableNodeDefManager* createNodeDefManager() diff --git a/src/nodedef.h b/src/nodedef.h index 4f07565d1..d846489ae 100644 --- a/src/nodedef.h +++ b/src/nodedef.h @@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include +#include #include "mapnode.h" #ifndef SERVER #include "tile.h" @@ -36,6 +37,8 @@ class IItemDefManager; class ITextureSource; class IGameDef; +typedef std::list > GroupItems; + enum ContentParamType { CPT_NONE, -- cgit v1.2.3 From c517215bcf3c92ccef1690ad871a234e547c8435 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Sat, 30 Mar 2013 21:24:37 -0400 Subject: Fix MapgenV6::generateCaves possible division by 0 and misc. cosmetic fixes --- src/mapgen_v6.cpp | 36 +++++++++++++++++++++--------------- src/nodedef.cpp | 1 + src/scriptapi.cpp | 2 +- 3 files changed, 23 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/mapgen_v6.cpp b/src/mapgen_v6.cpp index 0b419617d..275d4b78f 100644 --- a/src/mapgen_v6.cpp +++ b/src/mapgen_v6.cpp @@ -928,20 +928,20 @@ void MapgenV6::growGrass() { void MapgenV6::defineCave(Cave &cave, PseudoRandom ps, v3s16 node_min, bool large_cave) { - cave.min_tunnel_diameter = 2; - cave.max_tunnel_diameter = ps.range(2,6); - cave.dswitchint = ps.range(1,14); - cave.flooded = true; //large_cave && ps.range(0,4); - if(large_cave){ - cave.part_max_length_rs = ps.range(2,4); - cave.tunnel_routepoints = ps.range(5, ps.range(15,30)); - cave.min_tunnel_diameter = 5; - cave.max_tunnel_diameter = ps.range(7, ps.range(8,24)); - } else { - cave.part_max_length_rs = ps.range(2,9); - cave.tunnel_routepoints = ps.range(10, ps.range(15,30)); - } - cave.large_cave_is_flat = (ps.range(0,1) == 0); + cave.min_tunnel_diameter = 2; + cave.max_tunnel_diameter = ps.range(2,6); + cave.dswitchint = ps.range(1,14); + cave.flooded = true; //large_cave && ps.range(0,4); + if (large_cave){ + cave.part_max_length_rs = ps.range(2,4); + cave.tunnel_routepoints = ps.range(5, ps.range(15,30)); + cave.min_tunnel_diameter = 5; + cave.max_tunnel_diameter = ps.range(7, ps.range(8,24)); + } else { + cave.part_max_length_rs = ps.range(2,9); + cave.tunnel_routepoints = ps.range(10, ps.range(15,30)); + } + cave.large_cave_is_flat = (ps.range(0,1) == 0); } @@ -1120,7 +1120,13 @@ void MapgenV6::generateCaves(int max_stone_y) { rp.Z = ar.Z-1; vec = rp - orp; - for(float f=0; f<1.0; f+=1.0/vec.getLength()) + float veclen = vec.getLength(); + // As odd as it sounds, veclen is *exactly* + // 0.0 sometimes, causing a FPE + if (veclen == 0.0) + veclen = 1.0; + + for(float f=0; f<1.0; f+=1.0/veclen) { v3f fp = orp + vec * f; fp.X += 0.1*ps.range(-10,10); diff --git a/src/nodedef.cpp b/src/nodedef.cpp index 59db68a36..ca8898907 100644 --- a/src/nodedef.cpp +++ b/src/nodedef.cpp @@ -822,6 +822,7 @@ private: std::map m_name_id_mapping_with_aliases; // A mapping from groups to a list of content_ts (and their levels) // that belong to it. Necessary for a direct lookup in getIds(). + // Note: Not serialized. std::map m_group_to_items; }; diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 80abffa2b..81fcc08d3 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -727,7 +727,7 @@ static int l_register_ore(lua_State *L) if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) { errorstream << "register_ore: clust_scarcity and clust_num_ores" - "must be greater than 0"; + " must be greater than 0" << std::endl; delete ore; return 0; } -- cgit v1.2.3 From 96387ee88a89d172fa23ef325a15da510afac3e5 Mon Sep 17 00:00:00 2001 From: khonkhortisan Date: Sat, 30 Mar 2013 14:49:46 -0700 Subject: Don't load menu textures every frame --- src/main.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 2e57a8c20..439b59d31 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -618,7 +618,7 @@ void drawMenuBackground(video::IVideoDriver* driver) { std::string path = getTexturePath("menubg.png"); if (path[0]) { - video::ITexture *bgtexture = + static const video::ITexture *bgtexture = driver->getTexture(path.c_str()); if (bgtexture) { @@ -646,7 +646,7 @@ void drawMenuFooter(video::IVideoDriver* driver, bool clouds) { std::string path = getTexturePath(clouds ? "menufooter_clouds.png" : "menufooter.png"); if (path[0]) { - video::ITexture *footertexture = + static const video::ITexture *footertexture = driver->getTexture(path.c_str()); if (footertexture) { @@ -678,7 +678,7 @@ void drawMenuHeader(video::IVideoDriver* driver) { std::string path = getTexturePath("menuheader.png"); if (path[0]) { - video::ITexture *splashtexture = + static const video::ITexture *splashtexture = driver->getTexture(path.c_str()); if(splashtexture) { @@ -713,7 +713,7 @@ void drawMenuHeader(video::IVideoDriver* driver) { void drawMenuSplash(video::IVideoDriver* driver) { core::dimension2d screensize = driver->getScreenSize(); if (getTexturePath("menusplash.png") != "") { - video::ITexture *splashtexture = + static const video::ITexture *splashtexture = driver->getTexture(getTexturePath("menusplash.png").c_str()); if(splashtexture) { -- cgit v1.2.3 From 7d3b3890b5fde204051662f5ad73dd5d75e0a6f8 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Sat, 30 Mar 2013 18:40:33 -0400 Subject: Add a small optimization to the menu splash and remove a old comment --- src/main.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 439b59d31..08af6a37d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -682,9 +682,6 @@ void drawMenuHeader(video::IVideoDriver* driver) { driver->getTexture(path.c_str()); if(splashtexture) { - //v2s32 splashsize((splashtexture->getOriginalSize().Width*100)/ - // splashtexture->getOriginalSize().Height, 80); - f32 mult = (((f32)screensize.Width / 2)) / ((f32)splashtexture->getOriginalSize().Width); @@ -712,9 +709,10 @@ void drawMenuHeader(video::IVideoDriver* driver) { // Draw the Splash over the clouds and under the main menu void drawMenuSplash(video::IVideoDriver* driver) { core::dimension2d screensize = driver->getScreenSize(); - if (getTexturePath("menusplash.png") != "") { + std::string path = getTexturePath("menusplash.png"); + if (path[0]) { static const video::ITexture *splashtexture = - driver->getTexture(getTexturePath("menusplash.png").c_str()); + driver->getTexture(path.c_str()); if(splashtexture) { core::rect splashrect(0, 0, screensize.Width, screensize.Height); -- cgit v1.2.3