Skip to content

Commit

Permalink
implement restarting (for higher speed in some cases)
Browse files Browse the repository at this point in the history
misc. other less notable changes
  • Loading branch information
fl4shk committed Nov 15, 2022
1 parent bc85c3f commit 37a114e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 90 deletions.
14 changes: 7 additions & 7 deletions dungeon.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@



######### #############
####### ##########
#.....# #........#
###.....+,,,,,+........####
#.......# #...........#
#.......+,,,,,+...........#
#.......# #...........#
#.......# #...........#
#.......# #...........#
#.......# #...........#
#####+### ########+####
#.......# #####.......#
#.......# #.......#
####...## #.......#
##+## ####+####
, ,
, ,
,,,,,,,,,,,,,,,,,,
Expand Down
16 changes: 9 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ int main(int argc, char** argv) {
.add_singleton("--width", "-w", HasArg::Req, true)
.add_singleton("--height", "-h", HasArg::Req, true)
.add_singleton("--metatile-dim", "-d", HasArg::Req, false)
.add_singleton("--no-rotate", "-r", HasArg::None, false)
.add_singleton("--no-reflect", "-R", HasArg::None, false)
//.add_singleton("--no-overlap", "-o", HasArg::None, false)
.add_singleton("--backtrack", "-b", HasArg::None, false)
//.add_singleton("--overlap", "-o", HasArg::None, false)
.add_singleton("--rotate", "-r", HasArg::None, false)
.add_singleton("--reflect", "-R", HasArg::None, false)
.add_singleton("--seed", "-s", HasArg::Req, false);
if (
const auto& ap_ret=ap.parse(argc, argv);
Expand Down Expand Up @@ -126,9 +127,10 @@ int main(int argc, char** argv) {
}

const bool
no_rotate = ap.has_opts("--no-rotate"),
no_reflect = ap.has_opts("--no-reflect");
//no_overlap = ap.has_opts("--no-overlap");
backtrack = ap.has_opts("--backtrack"),
//overlap = ap.has_opts("--overlap"),
rotate = ap.has_opts("--rotate"),
reflect = ap.has_opts("--reflect");

u64 rng_seed;

Expand All @@ -141,7 +143,7 @@ int main(int argc, char** argv) {
wfc::Wfc the_wfc
(size_2d, mt_dim,
input_tiles,
no_rotate, no_reflect, //no_overlap,
backtrack, rotate, reflect, //overlap,
rng_seed);
for (size_t j=0; j<the_wfc.result().size(); ++j) {
const auto& row = the_wfc.result().at(j);
Expand Down
156 changes: 87 additions & 69 deletions src/wfc_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ Wfc::Wfc() {}
Wfc::Wfc(
const Vec2<size_t>& s_size_2d, size_t s_mt_dim,
const std::vector<std::vector<size_t>>& input_tiles,
bool s_no_rotate, bool s_no_reflect, //bool s_no_overlap,
bool s_backtrack,
//bool s_overlap,
bool s_rotate, bool s_reflect,
u64 s_rng_seed
)
: _size_2d(s_size_2d),
_mt_dim(s_mt_dim),
//_potential(s_size_2d.y,
// std::vector<TileUset>(s_size_2d.x, TileUset())),
_no_rotate(s_no_rotate),
_no_reflect(s_no_reflect),
//_no_overlap(s_no_overlap),
_backtrack(s_backtrack),
//_overlap(s_overlap),
_rotate(s_rotate),
_reflect(s_reflect),
_rng(s_rng_seed) {
//--------
//#ifdef DEBUG
Expand Down Expand Up @@ -120,16 +123,16 @@ void Wfc::_learn(const std::vector<std::vector<size_t>>& input_tiles) {
// non-reflected `Metatile`'s weights
ext_mt_umap.insert(std::pair(to_insert, item.second));
};
if (!no_reflect()) {
if (reflect()) {
do_mt_insert(Metatile(item.first).reflect_x());
do_mt_insert(Metatile(item.first).reflect_y());
do_mt_insert
(Metatile(item.first).reflect_x().reflect_y());
}
if (!no_rotate()) {
if (rotate()) {
auto rot90 = Metatile(item.first).rotate_p90();
do_mt_insert(rot90);
if (!no_reflect()) {
if (reflect()) {
do_mt_insert(Metatile(rot90).reflect_x());
do_mt_insert(Metatile(rot90).reflect_y());
do_mt_insert
Expand All @@ -138,7 +141,7 @@ void Wfc::_learn(const std::vector<std::vector<size_t>>& input_tiles) {

auto rot180 = Metatile(rot90).rotate_p90();
do_mt_insert(rot180);
if (!no_reflect()) {
if (reflect()) {
do_mt_insert(Metatile(rot180).reflect_x());
do_mt_insert(Metatile(rot180).reflect_y());
do_mt_insert
Expand All @@ -147,7 +150,7 @@ void Wfc::_learn(const std::vector<std::vector<size_t>>& input_tiles) {

auto rot270 = Metatile(rot180).rotate_p90();
do_mt_insert(rot270);
if (!no_reflect()) {
if (reflect()) {
do_mt_insert(Metatile(rot270).reflect_x());
do_mt_insert(Metatile(rot270).reflect_y());
do_mt_insert
Expand Down Expand Up @@ -332,6 +335,8 @@ void Wfc::_gen() {
// _baktk_stk.push(std::move(to_push));
//}
//std::optional<PosDarr> least_entropy_pos_darr = std::nullopt;
const PotElem orig_pe = _result.front().front();

_baktk_stk.push(BaktkStkItem
{.potential=_result});
_baktk_stk.top().least_entropy_pos_darr
Expand Down Expand Up @@ -362,36 +367,13 @@ void Wfc::_gen() {
size_t(0), guess_darr.size() - 1);
stk_top.init_guess_pos();
//--------
//const auto& prev_to_collapse
// = potential.at(guess_pos.y).at(guess_pos.x);

//if (prev_to_collapse.size() == 0) {
// //if (_baktk_stk.size() > 1) {
// _baktk_stk.pop();
// //}
// did_pop = true;
// //_baktk_stk.top().erase_guess();
// //if (_baktk_stk.top().guess_umap.size() == 0) {
// // _baktk_stk.pop();
// //}
// printout("testificate\n");
// continue;
//}
BaktkStkItem to_push;

//printout("guess_umap right before stuff with `to_push`:\n");
//stk_top.print_guess_umap();
//printout("guess_pos: ", guess_pos, "\n");
//printout("guess_umap.at(gp).size(): ",
// guess_umap.at(guess_pos).size(),
// "\n");

//if (guess_umap.at(guess_pos).size() > 0) {
// stk_top.print_guess_umap_at_gp();
//}
//_dbg_print();

BaktkStkItem to_push
= {.potential=potential};
if (backtrack()) {
to_push.potential = potential;
} else {
to_push.potential = std::move(potential);
}
auto& to_collapse
= to_push.potential.at(guess_pos.y).at(guess_pos.x);
const CollapseTemps& ct
Expand All @@ -407,16 +389,6 @@ void Wfc::_gen() {
for (size_t i=0; i<to_collapse.data.size(); ++i) {
to_collapse.erase(i);
}
//to_collapse.clear();
//for (size_t i=0; i<old_to_collapse_size; ++i) {
// to_collapse.push_back(std::nullopt);
//}
//for (size_t ti=0; ti<to_collapse.size(); ++ti) {
// //to_collapse.at(ti) = std::nullopt;
// if (to_collapse.contains(ti)) {
// to_collapse.erase(ti);
// }
//}

const auto& rng_val = ddist(_rng);
guess_ti = ct.tile_darr.at(rng_val);
Expand All @@ -426,6 +398,7 @@ void Wfc::_gen() {
//to_collapse.at(guess_ti) = 1;
//printout("to_collapse: ", to_collapse, "\n");

bool restart = false;
try {
_propagate(to_push.potential, guess_pos);
} catch (const std::exception& e) {
Expand All @@ -435,33 +408,74 @@ void Wfc::_gen() {
// _baktk_stk.size(),
// "\n");
//#endif // DEBUG
//need_pop = true;
_baktk_stk.top().erase_guess();
// hopefully this works?
//if (_baktk_stk.top().guess_umap.size() == 0)
if (_baktk_stk.top().guess_darr.size() == 0) {
if (backtrack()) {
//need_pop = true;
_baktk_stk.top().erase_guess();
// hopefully this works?
//if (_baktk_stk.top().guess_umap.size() == 0)
if (_baktk_stk.top().guess_darr.size() == 0) {
//#ifdef DEBUG
//printout("failed `_propagate()`: ",
// "doing `_baktk_stk.pop()`\n");
//#endif // DEBUG
//did_pop = true;
_baktk_stk.pop();
}

continue;
} else {
//#ifdef DEBUG
//printout("failed `_propagate()`: ",
// "doing `_baktk_stk.pop()`\n");
//printout("restarting\n");
//#endif // DEBUG
//did_pop = true;
_baktk_stk.pop();
//return;
restart = true;
}
}

//if (backtrack()) {
// //_baktk_stk.top().potential = to_push.potential;
//} else { // if (!backtrack())
//}

continue;
if (!restart) {
auto temp_least_entropy_pos_darr
= _calc_least_entropy_pos_darr(to_push.potential);
if (!temp_least_entropy_pos_darr) {
_result = std::move(to_push.potential);
break;
}
to_push.least_entropy_pos_darr
= *temp_least_entropy_pos_darr;
//to_push.init_guess_umap();
to_push.init_guess_darr();
}

auto temp_least_entropy_pos_darr
= _calc_least_entropy_pos_darr(to_push.potential);
if (!temp_least_entropy_pos_darr) {
_result = std::move(to_push.potential);
break;
if (backtrack()) {
_baktk_stk.push(std::move(to_push));
} else {
if (!restart) {
_baktk_stk.top() = std::move(to_push);
} else { // if (restart)
//const auto& temp_pe = to_push.potential.front().front();
//printout(temp_pe.data.size(), " ",
// temp_pe.num_active(), "\n");
//_baktk_stk.push(std::move(to_push));
//printout("_dbg_print(): \n");
//_dbg_print();
//_baktk_stk.pop();

_baktk_stk.top().potential = Potential(size_2d().y,
std::vector<PotElem>(size_2d().x, orig_pe));
_baktk_stk.top().least_entropy_pos_darr
= *_calc_least_entropy_pos_darr
(_baktk_stk.top().potential);
_baktk_stk.top().init_guess_darr();
}
}
to_push.least_entropy_pos_darr
= *temp_least_entropy_pos_darr;
//to_push.init_guess_umap();
to_push.init_guess_darr();
_baktk_stk.push(std::move(to_push));

//if (backtrack()) {
//} else { // if (restarting)
//}
//--------
}

Expand Down Expand Up @@ -971,7 +985,11 @@ void Wfc::_dbg_print() const {
(mt_darr().at(*pot_elem.first_set()).tl_corner()));
} else {
//printout(pot_elem.size());
printout(pot_elem.num_active());
if (pot_elem.num_active() <= 9) {
printout(pot_elem.num_active());
} else {
printout("*");
}
}
}
printout("\n");
Expand Down
18 changes: 11 additions & 7 deletions src/wfc_class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,10 @@ class Wfc final {
size_t
_mt_dim; // metatile dimension
bool
_no_rotate,
_no_reflect;
//_no_overlap;
_backtrack,
//_overlap;
_rotate,
_reflect;
//std::unordered_map<size_t, RuleUset> _rules_umap;
//RuleUset _rule_uset;

Expand All @@ -256,7 +257,9 @@ class Wfc final {
Wfc(
const Vec2<size_t>& s_size_2d, size_t s_mt_dim,
const std::vector<std::vector<size_t>>& input_tiles,
bool s_no_rotate, bool s_no_reflect, //bool s_no_overlap,
bool s_backtrack,
//bool s_overlap,
bool s_rotate, bool s_reflect,
u64 s_rng_seed
);
GEN_CM_BOTH_CONSTRUCTORS_AND_ASSIGN(Wfc);
Expand All @@ -267,9 +270,10 @@ class Wfc final {
GEN_GETTER_BY_CON_REF(mt_dim);
//GEN_GETTER_BY_CON_REF(rules_umap);
//GEN_GETTER_BY_CON_REF(rule_uset);
GEN_GETTER_BY_VAL(no_rotate);
GEN_GETTER_BY_VAL(no_reflect);
//GEN_GETTER_BY_VAL(no_overlap);
GEN_GETTER_BY_VAL(backtrack);
//GEN_GETTER_BY_VAL(overlap);
GEN_GETTER_BY_VAL(rotate);
GEN_GETTER_BY_VAL(reflect);
GEN_GETTER_BY_CON_REF(mt_darr);
GEN_GETTER_BY_CON_REF(r2w_umap);
//GEN_GETTER_BY_CON_REF(weight_umap);
Expand Down

0 comments on commit 37a114e

Please sign in to comment.