From 35559c69d6f2bf29b4f41b65419e474596a383b6 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 1 Apr 2026 16:03:11 +0200 Subject: [PATCH] feat: added deterministic flag for replacerules --- modules/generative_grammar/grammar.cpp | 14 +- modules/generative_grammar/grammar.h | 3 +- project/scenes/levels/dungeon.tscn | 306 ++++++++++++++----------- 3 files changed, 181 insertions(+), 142 deletions(-) diff --git a/modules/generative_grammar/grammar.cpp b/modules/generative_grammar/grammar.cpp index 82b385fa65..fae5edcc04 100644 --- a/modules/generative_grammar/grammar.cpp +++ b/modules/generative_grammar/grammar.cpp @@ -164,12 +164,7 @@ bool RepeatRuleUntilFailure::try_apply(Ref sentence) { void ReplaceRule::_bind_methods() { BIND_HPROPERTY(Variant::OBJECT, pattern, PROPERTY_HINT_RESOURCE_TYPE, Sentence::get_class_static()); BIND_HPROPERTY(Variant::DICTIONARY, results_dict, PROPERTY_HINT_DICTIONARY_TYPE, vformat("%s/%s:%s;float", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, Sentence::get_class_static())); -} - -bool ReplaceRule::try_match(Ref sentence, Vector2i &out) { - ERR_FAIL_COND_V_EDMSG(this->results.size() == 0, false, "ReplaceRule::try_apply failed, no results in list"); - - return false; + BIND_PROPERTY(Variant::BOOL, deterministic); } void ReplaceRule::write_result(Ref to, Vector2i at) { @@ -184,15 +179,10 @@ void ReplaceRule::write_result(Ref to, Vector2i at) { } bool ReplaceRule::try_apply_all(Ref sentence) { - Vector2i at{}; - if (try_match(sentence, at)) { - write_result(sentence, at); - return true; - } unsigned total{ 2 * 2 * 2 }; for (Vector2i pos{ 0, 0 }; pos.y < sentence->get_size().y; ++pos.y) { for (; pos.x < sentence->get_size().x; ++pos.x) { - unsigned random{ (Math::rand() * 3) % total }; + unsigned random{ this->deterministic ? 1 : (Math::rand() * 3) % total }; for (unsigned i{ 0 }; i < total; ++i) { this->pattern->read_flip_h = (i * random) & 0b1; this->pattern->read_flip_v = (i * random) & 0b10; diff --git a/modules/generative_grammar/grammar.h b/modules/generative_grammar/grammar.h index 439c159023..5a33f7d47f 100644 --- a/modules/generative_grammar/grammar.h +++ b/modules/generative_grammar/grammar.h @@ -88,7 +88,6 @@ public: class ReplaceRule : public Rule { GDCLASS(ReplaceRule, Rule); static void _bind_methods(); - bool try_match(Ref graph, Vector2i &out); void write_result(Ref to, Vector2i at); bool try_apply_all(Ref sentence); @@ -98,9 +97,11 @@ public: private: Ref pattern{}; Vector, float>> results{}; + bool deterministic{ false }; public: GET_SET_FNS(Ref, pattern); + GET_SET_FNS(bool, deterministic); void set_results(Vector, float>> value) { this->results = value; } Vector, float>> get_results() { return this->results; } void set_results_dict(Dictionary value); diff --git a/project/scenes/levels/dungeon.tscn b/project/scenes/levels/dungeon.tscn index b9332b5ea9..352be3c25b 100644 --- a/project/scenes/levels/dungeon.tscn +++ b/project/scenes/levels/dungeon.tscn @@ -17,6 +17,100 @@ background_mode = 2 sky = SubResource("Sky_srduu") glow_enabled = true +[sub_resource type="Sentence" id="Sentence_c7dwr"] +size = Vector2i(5, 5) +symbols_string = "sssss +sssss +sssss +sssss +sssss" + +[sub_resource type="Sentence" id="Sentence_ohg4i"] +size = Vector2i(5, 5) +symbols_string = "ppppp +ppppp +ppspp +ppppp +ppppp" + +[sub_resource type="Sentence" id="Sentence_yxqus"] +size = Vector2i(7, 7) +symbols_string = "*#####* +upppppu +upppppu +upppppu +upppppu +upppppu +*#####*" + +[sub_resource type="Sentence" id="Sentence_08grt"] +size = Vector2i(7, 7) +symbols_string = "*#####* +uupppuu +uupppuu +uupppuu +uupppuu +uupppuu +*#####*" + +[sub_resource type="Sentence" id="Sentence_b4io3"] +size = Vector2i(4, 4) +symbols_string = "#uuu +#uuu +#uuu +####" + +[sub_resource type="Sentence" id="Sentence_cygrb"] +size = Vector2i(4, 4) +symbols_string = "#puu +#ppu +#ppp +####" + +[sub_resource type="Sentence" id="Sentence_cxvmb"] +size = Vector2i(4, 4) +symbols_string = "#uuu +#puu +#ppu +####" + +[sub_resource type="Sentence" id="Sentence_45kae"] +size = Vector2i(4, 4) +symbols_string = "pppu +pppu +pppu +uuuu" + +[sub_resource type="Sentence" id="Sentence_eijdf"] +size = Vector2i(4, 4) +symbols_string = "pppu +ppuu +puuu +uuuu" + +[sub_resource type="Sentence" id="Sentence_5lcvo"] +size = Vector2i(4, 4) +symbols_string = "pppu +pppu +ppuu +uuuu" + +[sub_resource type="Sentence" id="Sentence_5ax7p"] +size = Vector2i(2, 1) +symbols_string = "pu" + +[sub_resource type="Sentence" id="Sentence_ntpuo"] +size = Vector2i(2, 1) +symbols_string = "pw" + +[sub_resource type="Sentence" id="Sentence_25vwi"] +size = Vector2i(2, 1) +symbols_string = "xu" + +[sub_resource type="Sentence" id="Sentence_wvupy"] +size = Vector2i(2, 1) +symbols_string = "xw" + [sub_resource type="Sentence" id="Sentence_bb2w7"] size = Vector2i(15, 15) symbols_string = "uuuuuuuuuuuuuuu @@ -50,8 +144,8 @@ uxppu [sub_resource type="Sentence" id="Sentence_qw2yk"] size = Vector2i(5, 3) symbols_string = "uuuuu -upppu -*pux*" +uppxu +*pup*" [sub_resource type="Sentence" id="Sentence_pbld3"] size = Vector2i(5, 4) @@ -103,95 +197,28 @@ symbols_string = "px" size = Vector2i(2, 1) symbols_string = "xp" -[sub_resource type="Sentence" id="Sentence_c7dwr"] -size = Vector2i(5, 5) -symbols_string = "sssss -sssss -sssss -sssss -sssss" +[sub_resource type="Sentence" id="Sentence_dkjoc"] +size = Vector2i(3, 3) +symbols_string = "*pu +*pp +u**" -[sub_resource type="Sentence" id="Sentence_ohg4i"] -size = Vector2i(5, 5) -symbols_string = "ppppp -ppppp -ppspp -ppppp -ppppp" +[sub_resource type="Sentence" id="Sentence_g57ti"] +size = Vector2i(3, 3) +symbols_string = "*pu +*xp +u**" -[sub_resource type="Sentence" id="Sentence_b4io3"] -size = Vector2i(4, 4) -symbols_string = "#uuu -#uuu -#uuu -####" +[sub_resource type="Sentence" id="Sentence_7rlrl"] +size = Vector2i(3, 3) +symbols_string = "uuu +upu +*p*" -[sub_resource type="Sentence" id="Sentence_cygrb"] -size = Vector2i(4, 4) -symbols_string = "#puu -#ppu -#ppp -####" - -[sub_resource type="Sentence" id="Sentence_cxvmb"] -size = Vector2i(4, 4) -symbols_string = "#uuu -#puu -#ppu -####" - -[sub_resource type="Sentence" id="Sentence_45kae"] -size = Vector2i(4, 4) -symbols_string = "pppu -pppu -pppu -uuuu" - -[sub_resource type="Sentence" id="Sentence_eijdf"] -size = Vector2i(4, 4) -symbols_string = "pppu -ppuu -puuu -uuuu" - -[sub_resource type="Sentence" id="Sentence_5lcvo"] -size = Vector2i(4, 4) -symbols_string = "pppu -pppu -ppuu -uuuu" - -[sub_resource type="Sentence" id="Sentence_yxqus"] -size = Vector2i(7, 5) -symbols_string = "uuuuuuu -*ppppp* -#ppppp# -#ppppp# -#ppppp#" - -[sub_resource type="Sentence" id="Sentence_08grt"] -size = Vector2i(7, 5) -symbols_string = "uuuuuuu -*uuuuu* -#ppppp# -#ppppp# -#ppppp#" - -[sub_resource type="Sentence" id="Sentence_5ax7p"] -size = Vector2i(2, 1) -symbols_string = "pu" - -[sub_resource type="Sentence" id="Sentence_ntpuo"] -size = Vector2i(2, 1) -symbols_string = "pw" - -[sub_resource type="Sentence" id="Sentence_25vwi"] -size = Vector2i(2, 1) -symbols_string = "xu" - -[sub_resource type="Sentence" id="Sentence_wvupy"] -size = Vector2i(2, 1) -symbols_string = "xw" +[sub_resource type="Sentence" id="Sentence_13d85"] +size = Vector2i(3, 2) +symbols_string = "uuu +uxu" [node name="Dungeon" type="Node3D" unique_id=719313039] @@ -202,6 +229,58 @@ environment = SubResource("Environment_s5a2w") transform = Transform3D(-0.8660254, -0.43301278, 0.25, 0, 0.49999997, 0.86602545, -0.50000006, 0.75, -0.43301266, 0, 0, 0) shadow_enabled = true +[node name="ResizeRule" type="ResizeRule" parent="." unique_id=630306379] +factor = 5 + +[node name="SingleStart" type="ReplaceRule" parent="." unique_id=80625713] +pattern = SubResource("Sentence_c7dwr") +results_dict = { +SubResource("Sentence_ohg4i"): 1.0 +} + +[node name="TightenPaths" type="RepeatRuleUntilFailure" parent="." unique_id=1419444164] + +[node name="Path" type="ReplaceRule" parent="TightenPaths" unique_id=582926889] +pattern = SubResource("Sentence_yxqus") +results_dict = { +SubResource("Sentence_08grt"): 1.0 +} +deterministic = true + +[node name="Naturalise" type="RepeatRuleUntilFailure" parent="." unique_id=945333249] + +[node name="InnerCorner" type="ReplaceRule" parent="Naturalise" unique_id=1294874606] +pattern = SubResource("Sentence_b4io3") +results_dict = { +SubResource("Sentence_cygrb"): 1.0, +SubResource("Sentence_cxvmb"): 1.0 +} +deterministic = true + +[node name="OuterCorner" type="ReplaceRule" parent="Naturalise" unique_id=1858990914] +pattern = SubResource("Sentence_45kae") +results_dict = { +SubResource("Sentence_eijdf"): 1.0, +SubResource("Sentence_5lcvo"): 1.0 +} +deterministic = true + +[node name="AddWalls" type="RepeatRuleUntilFailure" parent="." unique_id=351448478] + +[node name="Path" type="ReplaceRule" parent="AddWalls" unique_id=1194164836] +pattern = SubResource("Sentence_5ax7p") +results_dict = { +SubResource("Sentence_ntpuo"): 1.0 +} +deterministic = true + +[node name="Room" type="ReplaceRule" parent="AddWalls" unique_id=1878579537] +pattern = SubResource("Sentence_25vwi") +results_dict = { +SubResource("Sentence_wvupy"): 1.0 +} +deterministic = true + [node name="Generator" type="Generator" parent="." unique_id=1532743122] state = SubResource("Sentence_bb2w7") tiles_dict = { @@ -242,56 +321,25 @@ results_dict = { SubResource("Sentence_q0ye3"): 1.0 } -[node name="ClearAdjacentRooms" type="RepeatRuleUntilFailure" parent="Generator/Generate" unique_id=1503159395] +[node name="RefineRooms" type="RepeatRuleUntilFailure" parent="Generator/Generate" unique_id=1503159395] -[node name="ReplaceDouble" type="ReplaceRule" parent="Generator/Generate/ClearAdjacentRooms" unique_id=1043806381] +[node name="ReplaceDouble" type="ReplaceRule" parent="Generator/Generate/RefineRooms" unique_id=1043806381] pattern = SubResource("Sentence_7ibwp") results_dict = { SubResource("Sentence_gm6wd"): 1.0, SubResource("Sentence_lpnri"): 1.0 } +deterministic = true -[node name="ResizeRule" type="ResizeRule" parent="Generator/Generate" unique_id=630306379] -factor = 5 - -[node name="SingleStart" type="ReplaceRule" parent="Generator/Generate" unique_id=80625713] -pattern = SubResource("Sentence_c7dwr") +[node name="RoomOnCorners" type="ReplaceRule" parent="Generator/Generate/RefineRooms" unique_id=792182208] +pattern = SubResource("Sentence_dkjoc") results_dict = { -SubResource("Sentence_ohg4i"): 1.0 +SubResource("Sentence_g57ti"): 1.0 } +deterministic = true -[node name="Naturalise" type="RepeatRuleUntilFailure" parent="Generator/Generate" unique_id=945333249] - -[node name="InnerCorner" type="ReplaceRule" parent="Generator/Generate/Naturalise" unique_id=1294874606] -pattern = SubResource("Sentence_b4io3") +[node name="DeadEnd" type="ReplaceRule" parent="Generator/Generate/RefineRooms" unique_id=1706813607] +pattern = SubResource("Sentence_7rlrl") results_dict = { -SubResource("Sentence_cygrb"): 1.0, -SubResource("Sentence_cxvmb"): 1.0 -} - -[node name="OuterCorner" type="ReplaceRule" parent="Generator/Generate/Naturalise" unique_id=1858990914] -pattern = SubResource("Sentence_45kae") -results_dict = { -SubResource("Sentence_eijdf"): 1.0, -SubResource("Sentence_5lcvo"): 1.0 -} - -[node name="Path" type="ReplaceRule" parent="Generator/Generate/Naturalise" unique_id=582926889] -pattern = SubResource("Sentence_yxqus") -results_dict = { -SubResource("Sentence_08grt"): 1.0 -} - -[node name="AddWalls" type="RepeatRuleUntilFailure" parent="Generator/Generate" unique_id=351448478] - -[node name="Path" type="ReplaceRule" parent="Generator/Generate/AddWalls" unique_id=1194164836] -pattern = SubResource("Sentence_5ax7p") -results_dict = { -SubResource("Sentence_ntpuo"): 1.0 -} - -[node name="Room" type="ReplaceRule" parent="Generator/Generate/AddWalls" unique_id=1878579537] -pattern = SubResource("Sentence_25vwi") -results_dict = { -SubResource("Sentence_wvupy"): 1.0 +SubResource("Sentence_13d85"): 1.0 }