feat: replace rules will consider all orientations
This commit is contained in:
parent
51fa9c66a1
commit
d675596958
2 changed files with 22 additions and 46 deletions
|
|
@ -1,4 +1,5 @@
|
|||
#include "generative_grammar/grammar.h"
|
||||
#include "core/math/math_funcs.h"
|
||||
#include "core/object/class_db.h"
|
||||
#include "core/variant/typed_array.h"
|
||||
#include "macros.h"
|
||||
|
|
@ -46,8 +47,11 @@ void Sentence::set_at(Vector2i coord, Symbol symbol) {
|
|||
|
||||
bool Sentence::check_match_at(Vector2i at, Ref<Sentence> pattern) {
|
||||
Vector2i const pattern_size{ pattern->get_size() };
|
||||
if (at.x < 0 || at.y < 0) {
|
||||
return false; // can't match out of negative bounds
|
||||
}
|
||||
if (at.x + pattern_size.x >= this->size.x || at.y + pattern_size.y >= this->size.y) {
|
||||
return false; // can't match, out of bounds
|
||||
return false; // can't match, out of positive bounds
|
||||
}
|
||||
for (Vector2i itr{ 0, 0 }; itr.y < pattern_size.y; ++itr.y) {
|
||||
for (; itr.x < pattern_size.x; ++itr.x) {
|
||||
|
|
@ -160,16 +164,7 @@ void ReplaceRule::_bind_methods() {
|
|||
|
||||
bool ReplaceRule::try_match(Ref<Sentence> sentence, Vector2i &out) {
|
||||
ERR_FAIL_COND_V_EDMSG(this->results.size() == 0, false, "ReplaceRule::try_apply failed, no results in list");
|
||||
Vector2i max{ sentence->get_size() - this->pattern->get_size() };
|
||||
for (Vector2i pos{ 0, 0 }; pos.y < max.y; ++pos.y) {
|
||||
for (; pos.x < max.x; ++pos.x) {
|
||||
if (sentence->check_match_at(pos, this->pattern)) {
|
||||
out = pos;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
pos.x = 0;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -190,44 +185,24 @@ bool ReplaceRule::try_apply_all(Ref<Sentence> sentence) {
|
|||
write_result(sentence, at);
|
||||
return true;
|
||||
}
|
||||
this->pattern->read_flip_h = true;
|
||||
if (try_match(sentence, at)) {
|
||||
write_result(sentence, at);
|
||||
return true;
|
||||
}
|
||||
this->pattern->read_flip_v = true;
|
||||
if (try_match(sentence, at)) {
|
||||
write_result(sentence, at);
|
||||
return true;
|
||||
}
|
||||
this->pattern->read_transposed = true;
|
||||
if (try_match(sentence, at)) {
|
||||
write_result(sentence, at);
|
||||
return true;
|
||||
}
|
||||
this->pattern->read_flip_h = false;
|
||||
if (try_match(sentence, at)) {
|
||||
write_result(sentence, at);
|
||||
return true;
|
||||
}
|
||||
this->pattern->read_flip_v = false;
|
||||
if (try_match(sentence, at)) {
|
||||
write_result(sentence, at);
|
||||
return true;
|
||||
}
|
||||
this->pattern->read_flip_h = true;
|
||||
if (try_match(sentence, at)) {
|
||||
write_result(sentence, at);
|
||||
return true;
|
||||
}
|
||||
this->pattern->read_transposed = false;
|
||||
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 };
|
||||
for (unsigned i{ 0 }; i < total; ++i) {
|
||||
this->pattern->read_flip_h = (i * random) & 0b1;
|
||||
this->pattern->read_flip_v = (i * random) & 0b10;
|
||||
this->pattern->read_transposed = (i * random) & 0b100;
|
||||
if (sentence->check_match_at(pos, this->pattern)) {
|
||||
write_result(sentence, pos);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
pos.x = 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ReplaceRule::try_apply(Ref<Sentence> sentence) {
|
||||
bool success{ try_apply_all(sentence) };
|
||||
this->pattern->read_flip_h = false;
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ void initialize_generative_grammar_module(ModuleInitializationLevel p_level) {
|
|||
ClassDB::register_class<RepeatRuleUntilFailure>();
|
||||
ClassDB::register_class<CompositeRule>();
|
||||
ClassDB::register_class<ReplaceRule>();
|
||||
ClassDB::register_class<ResizeRule>();
|
||||
ClassDB::register_class<Generator>();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue