Refactored region growth
This commit is contained in:
@@ -59,15 +59,14 @@ public:
|
|||||||
const String &module_name);
|
const String &module_name);
|
||||||
void room_growth_module(flecs::world &ecs,
|
void room_growth_module(flecs::world &ecs,
|
||||||
const String &module_name);
|
const String &module_name);
|
||||||
|
void zones_graph_module(flecs::world &ecs,
|
||||||
|
const String &module_name);
|
||||||
void create_floor_components(
|
void create_floor_components(
|
||||||
flecs::entity floor_e, flecs::entity base_floor_e,
|
flecs::entity floor_e, flecs::entity base_floor_e,
|
||||||
const WorldEditor::components::buildings_layout_grid_size
|
const WorldEditor::components::buildings_layout_grid_size
|
||||||
&size);
|
&size);
|
||||||
void create_region(flecs::entity floor_e, flecs::entity seed_e,
|
|
||||||
flecs::entity region_e,
|
|
||||||
const Vector2i &position, float area);
|
|
||||||
bool check_region(flecs::entity floor_e, int index,
|
bool check_region(flecs::entity floor_e, int index,
|
||||||
const Rect2i &rect);
|
const Rect2i &rect) const;
|
||||||
graph_module(flecs::world &ecs);
|
graph_module(flecs::world &ecs);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -273,15 +273,13 @@ void BuildingLayoutGraph::graph_module::create_floor_components(
|
|||||||
floor_e.set<WorldEditor::components::buildings_layout_grid_floor>(
|
floor_e.set<WorldEditor::components::buildings_layout_grid_floor>(
|
||||||
{ Set<int>(), size.grid_size, size.growth_size });
|
{ Set<int>(), size.grid_size, size.growth_size });
|
||||||
floor_e.set<growth_regions>(
|
floor_e.set<growth_regions>(
|
||||||
{ Vector<struct growth_regions::region>() });
|
{ Vector<struct growth_regions::region>(), false });
|
||||||
floor_e.add<WorldEditor::components::belongs>(base_floor_e);
|
floor_e.add<WorldEditor::components::belongs>(base_floor_e);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildingLayoutGraph::graph_module::create_region(flecs::entity floor_e,
|
void growth_regions::create_region(flecs::entity floor_e, flecs::entity seed_e,
|
||||||
flecs::entity seed_e,
|
flecs::entity region_e,
|
||||||
flecs::entity region_e,
|
const Vector2i &position, float area)
|
||||||
const Vector2i &position,
|
|
||||||
float area)
|
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
struct growth_regions::region r;
|
struct growth_regions::region r;
|
||||||
@@ -291,8 +289,9 @@ void BuildingLayoutGraph::graph_module::create_region(flecs::entity floor_e,
|
|||||||
r.rect.size = Vector2i(1, 1);
|
r.rect.size = Vector2i(1, 1);
|
||||||
r.remains_area = MAX((int)(area * 2.0f) / 16 + 1, 16);
|
r.remains_area = MAX((int)(area * 2.0f) / 16 + 1, 16);
|
||||||
r.can_grow_square = true;
|
r.can_grow_square = true;
|
||||||
|
r.can_grow = true;
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
assert(check_region(floor_e, -1, r.rect));
|
assert(check_region(-1, r.rect));
|
||||||
const struct growth_regions *reg = floor_e.get<growth_regions>();
|
const struct growth_regions *reg = floor_e.get<growth_regions>();
|
||||||
for (i = 0; i < reg->regions.size(); i++) {
|
for (i = 0; i < reg->regions.size(); i++) {
|
||||||
if (reg->regions[i].region_et == r.region_et) {
|
if (reg->regions[i].region_et == r.region_et) {
|
||||||
@@ -307,91 +306,27 @@ void BuildingLayoutGraph::graph_module::create_region(flecs::entity floor_e,
|
|||||||
r.remains_area -= 1;
|
r.remains_area -= 1;
|
||||||
assert(ok);
|
assert(ok);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
|
floor_e.get_mut<growth_regions>()->complete = false;
|
||||||
floor_e.get_mut<growth_regions>()->regions.push_back(r);
|
floor_e.get_mut<growth_regions>()->regions.push_back(r);
|
||||||
floor_e.modified<growth_regions>();
|
floor_e.modified<growth_regions>();
|
||||||
flecs::log::warn("region created for %s",
|
flecs::log::warn("region created for %s",
|
||||||
region_e.path().c_str());
|
region_e.path().c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BuildingLayoutGraph::graph_module::check_region(flecs::entity floor_e,
|
bool BuildingLayoutGraph::graph_module::check_region(flecs::entity floor_e,
|
||||||
int index,
|
int index,
|
||||||
const Rect2i &rect)
|
const Rect2i &rect) const
|
||||||
{
|
{
|
||||||
const Vector<growth_regions::region> ®ions =
|
const growth_regions *g = floor_e.get<growth_regions>();
|
||||||
floor_e.get<growth_regions>()->regions;
|
return g->check_region(index, rect);
|
||||||
int i;
|
|
||||||
bool ret = true;
|
|
||||||
for (i = 0; i < regions.size(); i++) {
|
|
||||||
if (i == index)
|
|
||||||
continue;
|
|
||||||
if (rect.intersects(regions[i].rect)) {
|
|
||||||
ret = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
void BuildingLayoutGraph::graph_module::zones_graph_module(
|
||||||
BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
flecs::world &ecs, const String &module_name)
|
||||||
{
|
{
|
||||||
ecs.module<BuildingLayoutGraph::graph_module>();
|
|
||||||
ecs.import <flecs::stats>();
|
|
||||||
ecs.set<flecs::Rest>({});
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_graph>();
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_graph_node>();
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_base>();
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_room>();
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_zone>();
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_unit>();
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_floor>();
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_floor_index>()
|
|
||||||
.member<int>("index");
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_grid>();
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_grid_cell>();
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_grid_floor>();
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_grid_size>()
|
|
||||||
.member<int>("grid_size");
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_area>()
|
|
||||||
.member<float>("area");
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_order>()
|
|
||||||
.member<int>("index");
|
|
||||||
ecs.component<WorldEditor::components::belongs>();
|
|
||||||
#if 0
|
|
||||||
ecs.component<
|
|
||||||
WorldEditor::components::buildings_layout_commands::command>()
|
|
||||||
.member<int>("command")
|
|
||||||
.member<Vector<Variant> >("args");
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_commands>()
|
|
||||||
.member<WorldEditor::components::buildings_layout_commands::
|
|
||||||
command>("commands");
|
|
||||||
#endif
|
|
||||||
ecs.component<WorldEditor::components::buildings_layout_commands>();
|
|
||||||
// get_layout_grid_base();
|
|
||||||
// BuildingLayoutGraph::get_singleton()->get_layout_grid_base();
|
|
||||||
const String &module_name = "::BuildingLayoutGraph::graph_module";
|
|
||||||
flecs::entity GraphSolveZones = ecs.entity("GraphSolveZones")
|
flecs::entity GraphSolveZones = ecs.entity("GraphSolveZones")
|
||||||
.add(flecs::Phase)
|
.add(flecs::Phase)
|
||||||
.depends_on(flecs::OnUpdate);
|
.depends_on(flecs::OnUpdate);
|
||||||
GraphSolveZones.disable();
|
GraphSolveZones.disable();
|
||||||
flecs::entity GraphSolveUnits = ecs.entity("GraphSolveUnits")
|
|
||||||
.add(flecs::Phase)
|
|
||||||
.depends_on(GraphSolveZones);
|
|
||||||
flecs::entity GraphSolveFloors = ecs.entity("GraphSolveFloors")
|
|
||||||
.add(flecs::Phase)
|
|
||||||
.depends_on(GraphSolveUnits);
|
|
||||||
GraphSolve = ecs.entity("GraphSolve")
|
|
||||||
.add(flecs::Phase)
|
|
||||||
.depends_on(GraphSolveFloors);
|
|
||||||
flecs::entity GraphAssembleSkeleton =
|
|
||||||
ecs.entity("GraphAssembleSkeleton")
|
|
||||||
.add(flecs::Phase)
|
|
||||||
.depends_on(GraphSolve);
|
|
||||||
GraphAssembleSkeleton.disable();
|
|
||||||
flecs::entity GraphPostSolve = ecs.entity("GraphPostSolve")
|
|
||||||
.add(flecs::Phase)
|
|
||||||
.depends_on(flecs::PostUpdate);
|
|
||||||
GraphPostSolve.disable();
|
|
||||||
|
|
||||||
ecs.system("ZonesStart")
|
ecs.system("ZonesStart")
|
||||||
.kind(GraphSolveZones)
|
.kind(GraphSolveZones)
|
||||||
@@ -592,7 +527,68 @@ BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
|||||||
.run([module_name](flecs::iter &it) {
|
.run([module_name](flecs::iter &it) {
|
||||||
print_line("Processing units...");
|
print_line("Processing units...");
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
||||||
|
{
|
||||||
|
const String &module_name = "::BuildingLayoutGraph::graph_module";
|
||||||
|
ecs.module<BuildingLayoutGraph::graph_module>();
|
||||||
|
ecs.import <flecs::stats>();
|
||||||
|
ecs.set<flecs::Rest>({});
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_graph>();
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_graph_node>();
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_base>();
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_room>();
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_zone>();
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_unit>();
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_floor>();
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_floor_index>()
|
||||||
|
.member<int>("index");
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_grid>();
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_grid_cell>();
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_grid_floor>();
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_grid_size>()
|
||||||
|
.member<int>("grid_size");
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_area>()
|
||||||
|
.member<float>("area");
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_order>()
|
||||||
|
.member<int>("index");
|
||||||
|
ecs.component<WorldEditor::components::belongs>();
|
||||||
|
#if 0
|
||||||
|
ecs.component<
|
||||||
|
WorldEditor::components::buildings_layout_commands::command>()
|
||||||
|
.member<int>("command")
|
||||||
|
.member<Vector<Variant> >("args");
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_commands>()
|
||||||
|
.member<WorldEditor::components::buildings_layout_commands::
|
||||||
|
command>("commands");
|
||||||
|
#endif
|
||||||
|
ecs.component<WorldEditor::components::buildings_layout_commands>();
|
||||||
|
// get_layout_grid_base();
|
||||||
|
// BuildingLayoutGraph::get_singleton()->get_layout_grid_base();
|
||||||
|
flecs::entity GraphPostSolve = ecs.entity("GraphPostSolve")
|
||||||
|
.add(flecs::Phase)
|
||||||
|
.depends_on(flecs::PostUpdate);
|
||||||
|
GraphPostSolve.disable();
|
||||||
|
|
||||||
|
zones_graph_module(ecs, module_name);
|
||||||
|
flecs::entity GraphSolveZones =
|
||||||
|
ecs.lookup((module_name + "::GraphSolveZones").ascii().ptr());
|
||||||
|
assert(GraphSolveZones.is_valid());
|
||||||
|
flecs::entity GraphSolveUnits = ecs.entity("GraphSolveUnits")
|
||||||
|
.add(flecs::Phase)
|
||||||
|
.depends_on(GraphSolveZones);
|
||||||
|
flecs::entity GraphSolveFloors = ecs.entity("GraphSolveFloors")
|
||||||
|
.add(flecs::Phase)
|
||||||
|
.depends_on(GraphSolveUnits);
|
||||||
|
GraphSolve = ecs.entity("GraphSolve")
|
||||||
|
.add(flecs::Phase)
|
||||||
|
.depends_on(GraphSolveFloors);
|
||||||
|
flecs::entity GraphAssembleSkeleton =
|
||||||
|
ecs.entity("GraphAssembleSkeleton")
|
||||||
|
.add(flecs::Phase)
|
||||||
|
.depends_on(GraphSolve);
|
||||||
|
GraphAssembleSkeleton.disable();
|
||||||
ecs.system<WorldEditor::components::buildings_layout_unit>("UnitArea")
|
ecs.system<WorldEditor::components::buildings_layout_unit>("UnitArea")
|
||||||
.kind(GraphSolveUnits)
|
.kind(GraphSolveUnits)
|
||||||
.without<WorldEditor::components::buildings_layout_area>()
|
.without<WorldEditor::components::buildings_layout_area>()
|
||||||
|
|||||||
@@ -7,8 +7,63 @@ struct growth_regions {
|
|||||||
Rect2i rect;
|
Rect2i rect;
|
||||||
int remains_area;
|
int remains_area;
|
||||||
bool can_grow_square;
|
bool can_grow_square;
|
||||||
|
bool can_grow;
|
||||||
|
bool can_grow_region() const
|
||||||
|
{
|
||||||
|
bool ret = can_grow;
|
||||||
|
if (remains_area <= 0)
|
||||||
|
ret = false;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
bool update_region_size(Rect2i &mrect)
|
||||||
|
{
|
||||||
|
bool ret = false;
|
||||||
|
int old_area = rect.get_area();
|
||||||
|
int new_area = mrect.get_area();
|
||||||
|
int area_diff = new_area - old_area;
|
||||||
|
if (area_diff > 0) {
|
||||||
|
rect = mrect;
|
||||||
|
remains_area -= area_diff;
|
||||||
|
ret = true;
|
||||||
|
}
|
||||||
|
if (remains_area <= 0) {
|
||||||
|
can_grow_square = false;
|
||||||
|
can_grow = false;
|
||||||
|
}
|
||||||
|
flecs::log::dbg("update_region_size %d -> %d",
|
||||||
|
area_diff, ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
Vector<struct region> regions;
|
Vector<struct region> regions;
|
||||||
|
bool complete;
|
||||||
|
bool check_region(int index, const Rect2i &rect) const
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
bool ret = true;
|
||||||
|
for (i = 0; i < regions.size(); i++) {
|
||||||
|
if (i == index)
|
||||||
|
continue;
|
||||||
|
if (rect.intersects(regions[i].rect)) {
|
||||||
|
ret = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
flecs::log::dbg("check_region: %d -> %d", index, ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
bool update_region_size(int index, Rect2i &mrect)
|
||||||
|
{
|
||||||
|
bool ret = false;
|
||||||
|
bool ok = check_region(index, mrect);
|
||||||
|
if (ok)
|
||||||
|
ret = regions.write[index].update_region_size(mrect);
|
||||||
|
flecs::log::dbg("update_region_size %d -> %d", index, ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
void create_region(flecs::entity floor_e, flecs::entity seed_e,
|
||||||
|
flecs::entity region_e, const Vector2i &position,
|
||||||
|
float area);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct make_random {
|
struct make_random {
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -6,8 +6,13 @@
|
|||||||
void BuildingLayoutGraph::graph_module::room_growth_module(
|
void BuildingLayoutGraph::graph_module::room_growth_module(
|
||||||
flecs::world &ecs, const String &module_name)
|
flecs::world &ecs, const String &module_name)
|
||||||
{
|
{
|
||||||
flecs::entity GraphFilter = ecs.entity("GraphFilter");
|
flecs::entity GraphFilter =
|
||||||
|
ecs.lookup((module_name + "::GraphFilter").ascii().ptr());
|
||||||
assert(GraphFilter.is_valid());
|
assert(GraphFilter.is_valid());
|
||||||
|
flecs::entity GraphGrowUnitAreas = ecs.entity("GraphGrowUnitAreas")
|
||||||
|
.add(flecs::Phase)
|
||||||
|
.depends_on(flecs::OnUpdate);
|
||||||
|
GraphGrowUnitAreas.disable();
|
||||||
flecs::entity GraphMarkData = ecs.entity("GraphMarkData")
|
flecs::entity GraphMarkData = ecs.entity("GraphMarkData")
|
||||||
.add(flecs::Phase)
|
.add(flecs::Phase)
|
||||||
.depends_on(flecs::OnUpdate);
|
.depends_on(flecs::OnUpdate);
|
||||||
@@ -16,8 +21,55 @@ void BuildingLayoutGraph::graph_module::room_growth_module(
|
|||||||
.add(flecs::Phase)
|
.add(flecs::Phase)
|
||||||
.depends_on(flecs::OnUpdate);
|
.depends_on(flecs::OnUpdate);
|
||||||
GraphProcessRooms.disable();
|
GraphProcessRooms.disable();
|
||||||
|
ecs.system<const WorldEditor::components::buildings_layout_unit>(
|
||||||
|
"AllocateUnitGrow")
|
||||||
|
.kind(GraphGrowUnitAreas)
|
||||||
|
.each([](flecs::entity ec,
|
||||||
|
const WorldEditor::components::buildings_layout_unit
|
||||||
|
&unit) {
|
||||||
|
ec.world()
|
||||||
|
.query_builder<const WorldEditor::components::
|
||||||
|
buildings_layout_zone>()
|
||||||
|
.build()
|
||||||
|
.each([](flecs::entity e,
|
||||||
|
const WorldEditor::components::
|
||||||
|
buildings_layout_zone &zone) {
|
||||||
|
// assert(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
ecs.system("CheckGrow").kind(GraphFilter).run([module_name](flecs::iter &it) {
|
ecs.system("CheckGrow").kind(GraphFilter).run([module_name](flecs::iter &it) {
|
||||||
|
int failed_zones = 0, total_zones = 0;
|
||||||
|
it.world()
|
||||||
|
.query_builder<const WorldEditor::components::
|
||||||
|
buildings_layout_zone>()
|
||||||
|
.build()
|
||||||
|
.each([&failed_zones, &total_zones](
|
||||||
|
flecs::entity ec,
|
||||||
|
const WorldEditor::components::
|
||||||
|
buildings_layout_zone &zone) {
|
||||||
|
int count = 0;
|
||||||
|
ec.world()
|
||||||
|
.query_builder<
|
||||||
|
const WorldEditor::components::
|
||||||
|
buildings_layout_grid_cell>()
|
||||||
|
.with<WorldEditor::components::belongs>(
|
||||||
|
ec)
|
||||||
|
.build()
|
||||||
|
.each([&count](
|
||||||
|
flecs::entity e,
|
||||||
|
const WorldEditor::components::
|
||||||
|
buildings_layout_grid_cell
|
||||||
|
&cell) {
|
||||||
|
count++;
|
||||||
|
});
|
||||||
|
if (count == 0)
|
||||||
|
failed_zones++;
|
||||||
|
total_zones++;
|
||||||
|
});
|
||||||
|
if (failed_zones > 0)
|
||||||
|
flecs::log::err("failed to allocate zones: %d/%d",
|
||||||
|
failed_zones, total_zones);
|
||||||
flecs::query<const WorldEditor::components::
|
flecs::query<const WorldEditor::components::
|
||||||
buildings_layout_grid_floor>
|
buildings_layout_grid_floor>
|
||||||
q = it.world()
|
q = it.world()
|
||||||
@@ -35,7 +87,7 @@ void BuildingLayoutGraph::graph_module::room_growth_module(
|
|||||||
count_left += MAX(0, fl.size_left);
|
count_left += MAX(0, fl.size_left);
|
||||||
});
|
});
|
||||||
// assert(false);
|
// assert(false);
|
||||||
if (count_run > 0 && count_left <= 0) {
|
if (count_run > 0 && count_left <= 0 && failed_zones == 0) {
|
||||||
it.world()
|
it.world()
|
||||||
.lookup((module_name + "::GraphFilter")
|
.lookup((module_name + "::GraphFilter")
|
||||||
.ascii()
|
.ascii()
|
||||||
@@ -48,6 +100,20 @@ void BuildingLayoutGraph::graph_module::room_growth_module(
|
|||||||
.ptr())
|
.ptr())
|
||||||
.enable();
|
.enable();
|
||||||
print_line("Mark started...");
|
print_line("Mark started...");
|
||||||
|
} else if (count_run > 0 && count_left <= 0 &&
|
||||||
|
failed_zones > 0) {
|
||||||
|
it.world()
|
||||||
|
.lookup((module_name + "::GraphFilter")
|
||||||
|
.ascii()
|
||||||
|
.ptr())
|
||||||
|
.disable();
|
||||||
|
print_line("Grow done");
|
||||||
|
it.world()
|
||||||
|
.lookup((module_name + "::GraphGrowUnitAreas")
|
||||||
|
.ascii()
|
||||||
|
.ptr())
|
||||||
|
.enable();
|
||||||
|
print_line("Grow unit started...");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ecs.system<const WorldEditor::components::buildings_layout_grid_cell>(
|
ecs.system<const WorldEditor::components::buildings_layout_grid_cell>(
|
||||||
@@ -377,4 +443,4 @@ void BuildingLayoutGraph::graph_module::room_growth_module(
|
|||||||
"not all rooms were assigned: %d < %d",
|
"not all rooms were assigned: %d < %d",
|
||||||
assigned_count, count);
|
assigned_count, count);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user