diff --git a/include/qbsp/qbsp.hh b/include/qbsp/qbsp.hh index 00477273..c751f622 100644 --- a/include/qbsp/qbsp.hh +++ b/include/qbsp/qbsp.hh @@ -743,6 +743,7 @@ struct node_t void InitQBSP(int argc, const char **argv); void InitQBSP(const std::vector &args); +void CountLeafs(node_t *headnode); void ProcessFile(); int qbsp_main(int argc, const char **argv); diff --git a/qbsp/brushbsp.cc b/qbsp/brushbsp.cc index 81bfab49..e4a4b7da 100644 --- a/qbsp/brushbsp.cc +++ b/qbsp/brushbsp.cc @@ -1286,8 +1286,7 @@ void BrushBSP(tree_t &tree, mapentity_t &entity, const bspbrush_t::container &br stats.print_stats(); - logging::header("CountLeafs"); - qbsp_options.target_game->print_content_stats(*stats.leafstats, "leafs"); + CountLeafs(tree.headnode); } /* diff --git a/qbsp/qbsp.cc b/qbsp/qbsp.cc index 510662ef..0fd2dd41 100644 --- a/qbsp/qbsp.cc +++ b/qbsp/qbsp.cc @@ -380,13 +380,46 @@ static void CountLeafs_r(node_t *node, content_stats_base_t &stats) CountLeafs_r(node->children[1], stats); } -static void CountLeafs(node_t *headnode) +static int NodeHeight(node_t* node) +{ + if (node->parent) { + return 1 + NodeHeight(node->parent); + } + return 1; +} + +static void CountLeafHeights_r(node_t *node, std::vector &heights) +{ + if (node->is_leaf) { + heights.push_back(NodeHeight(node)); + return; + } + CountLeafHeights_r(node->children[0], heights); + CountLeafHeights_r(node->children[1], heights); +} + +void CountLeafs(node_t *headnode) { logging::funcheader(); auto stats = qbsp_options.target_game->create_content_stats(); CountLeafs_r(headnode, *stats); qbsp_options.target_game->print_content_stats(*stats, "leafs"); + + // count the heights of the tree at each leaf + logging::stat_tracker_t stat_print; + + std::vector leaf_heights; + CountLeafHeights_r(headnode, leaf_heights); + + const int max_height = *std::max_element(leaf_heights.begin(), leaf_heights.end()); + stat_print.register_stat("max tree height").count += max_height; + + double avg_height = 0; + for (int height : leaf_heights) { + avg_height += (height / static_cast(leaf_heights.size())); + } + stat_print.register_stat("avg tree height").count += static_cast(avg_height); } static void GatherBspbrushes_r(node_t *node, bspbrush_t::container &container)