From 386b136fb7d31d6424bd988d1d7c095626ceb393 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Wed, 22 Feb 2023 12:18:21 -0500 Subject: Added painting room randomisation --- generator.cpp | 24 ++++++++++++++++++++++++ generator.h | 43 +++++++++++++++++++++++++++++++++++++++++++ main.cpp | 44 +++++++++++++++++++++++++++++++++++++++++++- paintings.txt | 29 +++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 paintings.txt diff --git a/generator.cpp b/generator.cpp index 6b6c1be..8334904 100644 --- a/generator.cpp +++ b/generator.cpp @@ -654,6 +654,30 @@ void Generator::GenerateCrossTower( SavePanel(west_other_name3, sets[3][3], sets[3][3]); } +void Generator::GeneratePaintingPuzzle(std::string panel_name, std::string painting_name) { + std::string node_name; + std::string answer; + int resource_id = 0; + + for (;;) { + std::tie(node_name, answer, resource_id) = paintings_->GetPainting(rng_); + if (!used_paintings_.count(node_name)) { + break; + } + } + + used_paintings_.insert(node_name); + + SavePanel(panel_name, "painting", answer, {}); + + std::string resource_path = std::string("res://nodes/paintings/") + node_name + ".tscn"; + if (resource_id == 0) { + resources_.emplace_back(resource_path, "PackedScene"); + } + + replace_nodes_[painting_name] = {resource_path, resource_id}; +} + void Generator::SavePanel(std::string name, std::string question, std::string answer, GenerateOptions options) { if (options.save_for_later) { reusable_.push_back(answer); diff --git a/generator.h b/generator.h index 7803004..d0333a6 100644 --- a/generator.h +++ b/generator.h @@ -118,6 +118,25 @@ private: std::vector> sets_; }; +class Paintings { +public: + explicit Paintings(std::string filename) { + std::ifstream file(filename); + std::string line; + while (std::getline(file, line)) { + auto parts = hatkirby::split>(line, ","); + paintings_.emplace_back(parts[0], parts[1], std::atoi(parts[2].c_str())); + } + } + + const std::tuple& GetPainting(std::mt19937& rng) const { + return paintings_.at(std::uniform_int_distribution(0, paintings_.size()-1)(rng)); + } + +private: + std::vector> paintings_; +}; + class Generator { public: @@ -125,6 +144,7 @@ public: database_ = std::make_unique("/Users/hatkirby/Dropbox/Programming/verbly-datafiles/d1.3_lingo7"); wanderlust_ = std::make_unique("../wanderlust_words.txt", "../wanderlust_puzzles.txt"); cross_tower_ = std::make_unique("../cross_tower.txt"); + paintings_ = std::make_unique("../paintings.txt"); } // Querying @@ -136,6 +156,18 @@ public: return panels_.at(name); } + bool IsNodeRandomized(const std::string& name) const { + return replace_nodes_.count(name); + } + + const std::tuple& GetNode(const std::string& name) const { + return replace_nodes_.at(name); + } + + const std::vector>& GetResources() const { + return resources_; + } + // Sets the panel's question and answer to static values. void GenerateStaticPanel(std::string name, std::string question, std::string answer = ""); @@ -196,6 +228,9 @@ public: std::string west_other_name2, std::string west_other_name3); + // Generate a painting/panel pair. + void GeneratePaintingPuzzle(std::string panel_name, std::string painting_name); + private: verbly::filter MakeHintFilter(verbly::filter subfilter, Height height, Colour colour, FilterDirection filter_direction) const; @@ -223,12 +258,20 @@ private: std::unique_ptr database_; std::unique_ptr wanderlust_; std::unique_ptr cross_tower_; + std::unique_ptr paintings_; // name, question, answer std::map> panels_; + // name, resource_path, resource_id (0 if needs import) + std::map> replace_nodes_; + + // path, type + std::vector> resources_; + std::vector reusable_; std::map> pools_; + std::set used_paintings_; }; #endif /* end of include guard: GENERATOR_H_811386CE */ diff --git a/main.cpp b/main.cpp index 505004d..ba40def 100644 --- a/main.cpp +++ b/main.cpp @@ -405,7 +405,12 @@ public: - + // Room Room + gen_.GeneratePaintingPuzzle("Panel_painting_flower", "flower_painting_gray"); + gen_.GeneratePaintingPuzzle("Panel_painting_eye", "eye_painting_gray"); + gen_.GeneratePaintingPuzzle("Panel_painting_snowman", "snowman_painting_gray"); + gen_.GeneratePaintingPuzzle("Panel_painting_owl", "owl_painting_gray"); + gen_.GeneratePaintingPuzzle("Panel_painting_panda", "panda_painting_gray"); @@ -578,6 +583,9 @@ public: std::string name; std::string question; std::string answer; + std::map resource_id_by_path; + int last_id = 0; + bool need_to_output_resources = false; while (std::getline(level1, line)) { if (line.substr(0, 18) == "[node name=\"Panel_") { std::string stripstart = line.substr(12); @@ -604,6 +612,40 @@ public: //std::cout << name << ": " << question << "? " << answer << "!" << std::endl; output << "answer = \"" << answer << "\"" << std::endl; + } else if (line.substr(0, 9) == "[gd_scene") { + std::string stripstart = line.substr(21); + std::string numstr = stripstart.substr(0, stripstart.find(" ")); + int load_steps = std::atoi(numstr.c_str()); + output << "[gd_scene load_steps=" << (load_steps + gen_.GetResources().size()) << " format=2]\n"; + } else if (line.substr(0, 13) == "[ext_resource") { + std::string stripstart = line.substr(line.find("id=") + 3); + std::string numstr = stripstart.substr(0, stripstart.find("]")); + last_id = std::atoi(numstr.c_str()); + need_to_output_resources = true; + output << line << "\n"; + } else if (line.substr(0, 12) == "[node name=\"") { + std::string stripstart = line.substr(12); + std::string tempname = stripstart.substr(0, stripstart.find("\"")); + + if (gen_.IsNodeRandomized(tempname)) { + int id_pos = line.find("(") + 2; + auto [path, res_id] = gen_.GetNode(tempname); + if (res_id != 0) { + output << line.substr(0, id_pos) + std::to_string(res_id) + " )]\n"; + } else { + output << line.substr(0, id_pos) + std::to_string(resource_id_by_path[path]) + " )]\n"; + } + } else { + output << line << "\n"; + } + } else if (line.empty() && need_to_output_resources) { + for (const auto& [path, type] : gen_.GetResources()) { + last_id++; + resource_id_by_path[path] = last_id; + output << "[ext_resource path=\"" << path << "\" type=\"" << type << "\" id=" << last_id << "]\n"; + } + output << "\n"; + need_to_output_resources = false; } else { output << line << "\n"; } diff --git a/paintings.txt b/paintings.txt new file mode 100644 index 0000000..f91a808 --- /dev/null +++ b/paintings.txt @@ -0,0 +1,29 @@ +beach,beach,0 +bg_bg4,clouds,0 +bg_bookshelf,bookshelf,0 +bg_candle,candle,0 +bg_egg,egg,0 +bg_fish,fish,0 +bg_halloween_2,pumpkin,0 +bg_knight,knight,0 +bg_scorpion,scorpion,0 +bg_thanksgiving,turkey,0 +catlike,cat,0 +crown,crown,0 +emmy,nest,0 +ether,rocket ship,0 +hatkirby,shooting star,0 +icy,parrot,0 +ig_note,note,0 +ig_rose,rose,0 +kiwi,kiwi,0 +ninjonicx_square,spiral,0 +rainbow,rainbow,0 +rever,fishbowl,0 +tree,tree,0 +yinyang,yinyang,0 +eight,eight,59 +north,north,44 +east,east,48 +south,south,47 +west,west,46 \ No newline at end of file -- cgit 1.4.1