54 lines
1.5 KiB
C++
54 lines
1.5 KiB
C++
#include "generator.h"
|
|
#include "core/config/engine.h"
|
|
#include "core/object/class_db.h"
|
|
#include "macros.h"
|
|
|
|
void Generator::_bind_methods() {
|
|
BIND_HPROPERTY(Variant::OBJECT, state, PROPERTY_HINT_RESOURCE_TYPE, "Sentence");
|
|
}
|
|
|
|
void Generator::initialise_state() {
|
|
for (int i{ 0 }; i < this->state->symbols.size(); ++i) {
|
|
this->state->symbols.set(i, NonTerminals::Undefined);
|
|
}
|
|
for (int i{ 0 }; i < this->state->size.x; ++i) {
|
|
this->state->set_at({ i, 0 }, NonTerminals::Blocked);
|
|
this->state->set_at({ i, this->state->size.y - 1 }, NonTerminals::Blocked);
|
|
}
|
|
for (int i{ 0 }; i < this->state->size.y; ++i) {
|
|
this->state->set_at({ 0, i }, NonTerminals::Blocked);
|
|
this->state->set_at({ this->state->size.x - 1, i }, NonTerminals::Blocked);
|
|
}
|
|
int y{ (int)Math::rand() % this->state->size.y - 4 };
|
|
for (int i{ 1 }; i < this->state->size.x - 1; ++i) {
|
|
this->state->set_at({ i, y }, NonTerminals::Path);
|
|
}
|
|
this->state->set_at({ 0, y }, NonTerminals::Start);
|
|
this->state->set_at({ this->state->size.x - 1, y }, NonTerminals::Goal);
|
|
}
|
|
|
|
void Generator::ready() {
|
|
initialise_state();
|
|
// TODO: repeat until terminal state
|
|
this->rule->try_apply(this->state);
|
|
}
|
|
|
|
void Generator::_notification(int what) {
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
return;
|
|
}
|
|
switch (what) {
|
|
default:
|
|
return;
|
|
case NOTIFICATION_READY:
|
|
ready();
|
|
case NOTIFICATION_CHILD_ORDER_CHANGED:
|
|
for (Variant child : get_children()) {
|
|
if (Rule * rule{ cast_to<Rule>(child) }) {
|
|
this->rule = rule;
|
|
return;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
}
|