mail[Wesnoth-commits] r20656 - /trunk/src/mapgen.cpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by alinktomine on September 22, 2007 - 02:42:
Author: alink
Date: Sat Sep 22 02:41:43 2007
New Revision: 20656

URL: http://svn.gna.org/viewcvs/wesnoth?rev=20656&view=rev
Log:
Optimize the random map generator: slow for big map, old FR from bug #3508
The main bottleneck was the village placement. The worst case scenario,
100x100 with max village density, took ~25 seconds here. Now only ~2.
(it was some repetetive terrains-list creation, I just added a little cache)
PS: The roads for numerous players is the next bottleneck. Planning to remove
some redundant operations (drawing the same road several times).

Modified:
    trunk/src/mapgen.cpp

Modified: trunk/src/mapgen.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/mapgen.cpp?rev=20656&r1=20655&r2=20656&view=diff
==============================================================================
--- trunk/src/mapgen.cpp (original)
+++ trunk/src/mapgen.cpp Sat Sep 22 02:41:43 2007
@@ -512,9 +512,12 @@
        return surrounding_ranking + current_ranking;
 }
 
+typedef std::map<t_translation::t_letter, t_translation::t_list> 
letter_list_cache;
+
 //!
 static gamemap::location place_village(const t_translation::t_map& map,
-       const size_t x, const size_t y, const size_t radius, const config& 
cfg)
+       const size_t x, const size_t y, const size_t radius, const config& 
cfg,
+       letter_list_cache &adj_liked_cache)
 {
        const gamemap::location loc(x,y);
        std::set<gamemap::location> locs;
@@ -530,9 +533,19 @@
                        continue;
                }
 
-               const std::string str = 
t_translation::write_letter(map[i->x][i->y]);
+               const t_translation::t_letter t = map[i->x][i->y];
+               const std::string str = t_translation::write_letter(t);
                const config* const child = 
cfg.find_child("village","terrain",str);
                if(child != NULL) {
+                       letter_list_cache::iterator l = 
adj_liked_cache.find(t);
+                       t_translation::t_list *adjacent_liked;
+                       if (l != adj_liked_cache.end()) {
+                               adjacent_liked = &(l->second);
+                       } else {
+                               adj_liked_cache[t] = 
t_translation::read_list((*child)["adjacent_liked"]);
+                               adjacent_liked = &(adj_liked_cache[t]);
+                       }
+
                        size_t rating = atoi((*child)["rating"].c_str());
                        gamemap::location adj[6];
                        get_adjacent_tiles(gamemap::location(i->x,i->y),adj);
@@ -544,11 +557,8 @@
                                        continue;
                                }
 
-                               const t_translation::t_letter t = 
map[adj[n].x][adj[n].y];
-                               const t_translation::t_list& adjacent_liked =
-                                       
t_translation::read_list((*child)["adjacent_liked"]);
-
-                               rating += 
std::count(adjacent_liked.begin(),adjacent_liked.end(),t);
+                               const t_translation::t_letter t2 = 
map[adj[n].x][adj[n].y];
+                               rating += 
std::count(adjacent_liked->begin(),adjacent_liked->end(),t2);
                        }
 
                        if(rating > best_rating) {
@@ -1162,6 +1172,7 @@
                }
 
                std::set<std::string> used_names;
+               letter_list_cache adj_liked_cache;
 
                for(size_t vx = 0; vx < width; vx += village_x) {
                        LOG_NG << "village at " << vx << "\n";
@@ -1172,7 +1183,7 @@
                                const size_t x = (vx + add_x) - 1;
                                const size_t y = (vy + add_y) - 1;
 
-                               const gamemap::location res = 
place_village(terrain,x,y,2,cfg);
+                               const gamemap::location res = 
place_village(terrain,x,y,2,cfg,adj_liked_cache);
 
                                if(res.x >= static_cast<long>(width) / 3 &&
                                                res.x  < 
static_cast<long>(width * 2) / 3 &&




Related Messages


Powered by MHonArc, Updated Sat Sep 22 03:40:39 2007