From 80ef052e158449ce357e9644d87f9af2cd23b783 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 10 Jan 2025 21:36:14 -0500 Subject: [PATCH] Fixed a problem in the world builder after a refactor then made it do less random paths. --- worldbuilder.cpp | 58 +++++++++++++++++++++--------------------------- worldbuilder.hpp | 1 - 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/worldbuilder.cpp b/worldbuilder.cpp index e732fb6..157c6ee 100644 --- a/worldbuilder.cpp +++ b/worldbuilder.cpp @@ -95,17 +95,6 @@ void WorldBuilder::update_door(Point &at, int wall_or_space) { $map.$walls[at.y][at.x] = wall_or_space; } -void WorldBuilder::tunnel_doors(PointList &holes, Room &src, Room &target) { - $map.set_target(target.entry); - $map.make_paths(); - - bool found = dig_tunnel(holes, src.exit, target.entry); - dbc::check(found, "room should always be found"); - - $map.INVARIANT(); - $map.clear_target(target.entry); -} - void WorldBuilder::stylize_room(int room, string tile_name, float size) { Point center = $map.place_entity(room); @@ -271,22 +260,25 @@ void WorldBuilder::place_rooms() { } } -inline bool random_path(Map &map, PointList &holes, Point src) { - bool found = false; - Matrix &paths = map.paths(); - Point out{src.x, src.y}; +inline bool random_path(Map &map, PointList &holes, Point src, Point target) { + bool keep_going = false; + bool target_found = false; int count = 0; + map.set_target(target); + map.make_paths(); + Matrix &paths = map.paths(); + Point out{src.x, src.y}; do { - found = map.neighbors(out, true); + keep_going = map.neighbors(out, true); holes.push_back(out); + target_found = paths[out.y][out.x] == 0; + } while(!target_found && keep_going && ++count < WORLDBUILD_MAX_PATH); - if(paths[out.y][out.x] == 0) { - return true; - } - } while(found && ++count < WORLDBUILD_MAX_PATH); + map.INVARIANT(); + map.clear_target(target); - return found; + return target_found; } inline void straight_path(PointList &holes, Point src, Point target) { @@ -296,17 +288,17 @@ inline void straight_path(PointList &holes, Point src, Point target) { } } -bool WorldBuilder::dig_tunnel(PointList &holes, Point &src, Point &target) { - Matrix &paths = $map.paths(); - - dbc::check(paths[src.y][src.x] != WALL_PATH_LIMIT, - "source room has path as a wall"); - dbc::check(paths[target.y][target.x] != WALL_PATH_LIMIT, - "target room has path as a wall"); - - if(!random_path($map, holes, src)) { - straight_path(holes, src, target); +void WorldBuilder::tunnel_doors(PointList &holes, Room &src, Room &target) { + int path_type = Random::uniform(0, 3); + switch(path_type) { + case 0: + // for now do 25% as simple straight paths + straight_path(holes, src.exit, target.entry); + break; + default: + // then do the rest as random with fallback + if(!random_path($map, holes, src.exit, target.entry)) { + straight_path(holes, src.exit, target.entry); + } } - - return true; } diff --git a/worldbuilder.hpp b/worldbuilder.hpp index e4d39c6..a20b240 100644 --- a/worldbuilder.hpp +++ b/worldbuilder.hpp @@ -14,7 +14,6 @@ class WorldBuilder { void add_door(Room &room); void set_door(Room &room, int value); void place_rooms(); - bool dig_tunnel(PointList &holes, Point &src, Point &target); void tunnel_doors(PointList &holes, Room &src, Room &target); void update_door(Point &at, int wall_or_space); void stylize_room(int room, string tile_name, float size);