Now layout build works fine

This commit is contained in:
2024-12-01 04:12:25 +03:00
parent 34bded906c
commit 42dc811cf6
28 changed files with 4040 additions and 2640 deletions

View File

@@ -0,0 +1,242 @@
#include "growth_regions.h"
#include "region_tree.h"
#include "editor_event.h"
#include "queries.h"
#include "growth_module.h"
growth_module::growth_module(flecs::world &ecs)
{
ecs.module<growth_module>();
ecs.component<growth_regions>();
ecs.component<region_tree>();
const String &module_name = "::growth_module";
flecs::entity GraphFilter = ecs.entity("GraphFilter")
.add(flecs::Phase)
.depends_on(flecs::OnUpdate);
flecs::entity GraphSolve = ecs.lookup("::graph_module::GraphSolve");
assert(GraphSolve.is_valid());
GraphFilter.disable();
#if 0
flecs::entity GraphGridPrepare = ecs.entity("GraphGridPrepare")
.add(flecs::Phase)
.depends_on(GraphSolve);
#endif
ecs.system("RunGrow")
.immediate()
.write<WorldEditor::components::buildings_layout_grid_floor>()
.write<WorldEditor::components::buildings_layout_grid_size>()
.write<growth_regions>()
.kind(GraphSolve)
.run([module_name, this, GraphFilter](flecs::iter &it) {
flecs::world &&ecs_ = it.world();
it.world().defer_suspend();
flecs::log::dbg("Assembling skeleton done...");
flecs::entity GraphSolveZones = it.world().lookup(
"::graph_module::GraphSolveZones");
assert(GraphSolveZones.is_valid());
GraphSolveZones.disable();
assert(GraphFilter.is_valid());
GraphFilter.enable();
EditorEvent::get_singleton()->event.emit(
"update_layout_view", varray());
// assert(false);
struct subregions subregions(ecs_);
struct queries queries(ecs_);
flecs::log::dbg("Creating regions grow...");
queries.get_qp().each(
[this, &subregions, &queries,
module_name](flecs::iter &it2, size_t count,
const WorldEditor::components::
buildings_layout_grid_size
&size) {
struct grid_misc grid;
// grid.subregions_init(it2.world());
flecs::entity graph_e =
it2.entity(count);
flecs::log::warn(
"creating grid for: %s",
graph_e.path().c_str());
flecs::entity grid_base_e =
queries.get_layout_grid_base();
flecs::entity grid_e =
grid_base_e.lookup(
graph_e.name());
assert(grid_e.is_valid());
flecs::log::warn(
"creating grid for: %s: %s",
graph_e.path().c_str(),
grid_e.path().c_str());
/* starting at grid center */
subregions.build_subregions(grid_e,
grid, size);
struct grow_job_queue job_queue(
grid_e, subregions, size,
module_name);
job_queue.iterate();
});
commit_growth_queue(it.world());
queries.get_mark_cells().each([](flecs::entity e,
const WorldEditor::
components::buildings_layout_grid_cell
&cell) {
int grid_size =
e.parent()
.get<WorldEditor::components::
buildings_layout_grid_floor>()
->grid_size;
int i;
Vector2i position(cell.index % grid_size,
cell.index / grid_size);
Vector2 west(position.x - 1, position.y);
Vector2 north(position.x, position.y - 1);
Vector2 east(position.x + 1, position.y);
Vector2 south(position.x, position.y + 1);
int west_id = west.x + grid_size * west.y;
int north_id = north.x + grid_size * north.y;
int east_id = east.x + grid_size * east.y;
int south_id = south.x + grid_size * south.y;
std::vector<int> neighbors = {
west_id, north_id, east_id, south_id
};
bool outside = false;
#if 0
bool border = false;
#endif
for (i = 0; i < (int)neighbors.size(); i++) {
int id = neighbors[i];
print_line("id=" + itos(id));
String neighbor_cell =
"cell_" + itos(id);
flecs::entity neighbor_e =
e.parent().lookup(
neighbor_cell.ascii()
.ptr());
if (!neighbor_e.is_valid()) {
outside = true;
break;
}
if (!e.parent()
.get<WorldEditor::components::
buildings_layout_grid_floor>()
->cells.has(id)) {
outside = true;
break;
}
}
flecs::log::dbg("outside: %d", outside);
#if 0
for (i = 0; i < (int)neighbors.size(); i++) {
int id = neighbors[i];
print_line("id=" + itos(id));
String neighbor_name = "cell_" + itos(id);
flecs::entity neighbor_e =
e.parent().lookup(neighbor_name.ascii().ptr());
if (!neighbor_e.is_valid())
continue;
const WorldEditor::components::buildings_layout_grid_cell
*neighbor_cell = neighbor_e.get<
WorldEditor::components::
buildings_layout_grid_cell>();
if (cell.type != neighbor_cell->type) {
border = true;
break;
}
}
#endif
if (outside) {
e.add<WorldEditor::components::
outside_wall>();
flecs::log::dbg("outside wall cell %s",
e.path().c_str());
} else
e.add<WorldEditor::components::
final_cell>();
#if 0
if (border)
e.add<WorldEditor::components::border>();
#endif
print_line("outside: " + itos(outside));
print_line("position: " +
(position.operator String()));
print_line("grid size: " + itos(grid_size));
print_line("tile index: " + itos(cell.index));
});
it.world()
.lookup("::growth_module::GraphFilter")
.disable();
it.world().defer_resume();
});
}
void growth_module::grow_cell(flecs::entity seed_e, int id)
{
flecs::entity floor_e = seed_e.parent();
String c_name = "cell_" + itos(id);
flecs::entity c_e = floor_e.lookup(c_name.ascii().ptr());
if (!c_e.is_valid()) {
c_e = seed_e.world()
.entity(c_name.ascii().ptr())
.child_of(floor_e);
assert(c_e.is_valid());
String type = seed_e.get<WorldEditor::components::
buildings_layout_grid_cell>()
->type;
c_e.set<WorldEditor::components::buildings_layout_grid_cell>(
{ type, id });
seed_e.each<WorldEditor::components::belongs>(
[&c_e](flecs::entity second) {
c_e.add<WorldEditor::components::belongs>(
second);
});
int mcount = 0;
seed_e.each<WorldEditor::components::belongs_room>(
[&c_e, &mcount](flecs::entity second) {
assert(mcount == 0);
c_e.add<WorldEditor::components::belongs_room>(
second);
mcount++;
});
floor_e.get_mut<WorldEditor::components::
buildings_layout_grid_floor>()
->size_left--;
floor_e.get_mut<WorldEditor::components::
buildings_layout_grid_floor>()
->cells.insert(id);
floor_e.modified<
WorldEditor::components::buildings_layout_grid_floor>();
}
}
void growth_module::commit_growth_queue(flecs::world &&ecs)
{
flecs::query<WorldEditor::components::buildings_layout_grid_floor,
WorldEditor::components::buildings_layout_grid_queue>
grow_commit_queue =
ecs.query_builder<WorldEditor::components::
buildings_layout_grid_floor,
WorldEditor::components::
buildings_layout_grid_queue>()
.build();
grow_commit_queue.each(
[this](flecs::entity e,
WorldEditor::components::buildings_layout_grid_floor &fl,
WorldEditor::components::buildings_layout_grid_queue
&queue) {
List<Pair<flecs::entity_t, int> >::Element *me =
queue.queue.front();
while (me) {
flecs::entity seed_e =
e.world().entity(me->get().first);
int id = me->get().second;
if (!fl.cells.has(id)) {
grow_cell(seed_e, id);
fl.cells.insert(id);
}
me = me->next();
}
queue.queue.clear();
e.remove<WorldEditor::components::
buildings_layout_grid_queue>();
});
}