Refactored region growth
This commit is contained in:
@@ -59,15 +59,14 @@ public:
|
||||
const String &module_name);
|
||||
void room_growth_module(flecs::world &ecs,
|
||||
const String &module_name);
|
||||
void zones_graph_module(flecs::world &ecs,
|
||||
const String &module_name);
|
||||
void create_floor_components(
|
||||
flecs::entity floor_e, flecs::entity base_floor_e,
|
||||
const WorldEditor::components::buildings_layout_grid_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,
|
||||
const Rect2i &rect);
|
||||
const Rect2i &rect) const;
|
||||
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>(
|
||||
{ Set<int>(), size.grid_size, size.growth_size });
|
||||
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);
|
||||
}
|
||||
|
||||
void BuildingLayoutGraph::graph_module::create_region(flecs::entity floor_e,
|
||||
flecs::entity seed_e,
|
||||
flecs::entity region_e,
|
||||
const Vector2i &position,
|
||||
float area)
|
||||
void growth_regions::create_region(flecs::entity floor_e, flecs::entity seed_e,
|
||||
flecs::entity region_e,
|
||||
const Vector2i &position, float area)
|
||||
{
|
||||
int i;
|
||||
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.remains_area = MAX((int)(area * 2.0f) / 16 + 1, 16);
|
||||
r.can_grow_square = true;
|
||||
r.can_grow = 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>();
|
||||
for (i = 0; i < reg->regions.size(); i++) {
|
||||
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;
|
||||
assert(ok);
|
||||
if (ok) {
|
||||
floor_e.get_mut<growth_regions>()->complete = false;
|
||||
floor_e.get_mut<growth_regions>()->regions.push_back(r);
|
||||
floor_e.modified<growth_regions>();
|
||||
flecs::log::warn("region created for %s",
|
||||
region_e.path().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
bool BuildingLayoutGraph::graph_module::check_region(flecs::entity floor_e,
|
||||
int index,
|
||||
const Rect2i &rect)
|
||||
const Rect2i &rect) const
|
||||
{
|
||||
const Vector<growth_regions::region> ®ions =
|
||||
floor_e.get<growth_regions>()->regions;
|
||||
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;
|
||||
const growth_regions *g = floor_e.get<growth_regions>();
|
||||
return g->check_region(index, rect);
|
||||
}
|
||||
|
||||
BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
||||
void BuildingLayoutGraph::graph_module::zones_graph_module(
|
||||
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")
|
||||
.add(flecs::Phase)
|
||||
.depends_on(flecs::OnUpdate);
|
||||
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")
|
||||
.kind(GraphSolveZones)
|
||||
@@ -592,7 +527,68 @@ BuildingLayoutGraph::graph_module::graph_module(flecs::world &ecs)
|
||||
.run([module_name](flecs::iter &it) {
|
||||
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")
|
||||
.kind(GraphSolveUnits)
|
||||
.without<WorldEditor::components::buildings_layout_area>()
|
||||
|
||||
@@ -7,8 +7,63 @@ struct growth_regions {
|
||||
Rect2i rect;
|
||||
int remains_area;
|
||||
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;
|
||||
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 {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -6,8 +6,13 @@
|
||||
void BuildingLayoutGraph::graph_module::room_growth_module(
|
||||
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());
|
||||
flecs::entity GraphGrowUnitAreas = ecs.entity("GraphGrowUnitAreas")
|
||||
.add(flecs::Phase)
|
||||
.depends_on(flecs::OnUpdate);
|
||||
GraphGrowUnitAreas.disable();
|
||||
flecs::entity GraphMarkData = ecs.entity("GraphMarkData")
|
||||
.add(flecs::Phase)
|
||||
.depends_on(flecs::OnUpdate);
|
||||
@@ -16,8 +21,55 @@ void BuildingLayoutGraph::graph_module::room_growth_module(
|
||||
.add(flecs::Phase)
|
||||
.depends_on(flecs::OnUpdate);
|
||||
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) {
|
||||
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::
|
||||
buildings_layout_grid_floor>
|
||||
q = it.world()
|
||||
@@ -35,7 +87,7 @@ void BuildingLayoutGraph::graph_module::room_growth_module(
|
||||
count_left += MAX(0, fl.size_left);
|
||||
});
|
||||
// assert(false);
|
||||
if (count_run > 0 && count_left <= 0) {
|
||||
if (count_run > 0 && count_left <= 0 && failed_zones == 0) {
|
||||
it.world()
|
||||
.lookup((module_name + "::GraphFilter")
|
||||
.ascii()
|
||||
@@ -48,6 +100,20 @@ void BuildingLayoutGraph::graph_module::room_growth_module(
|
||||
.ptr())
|
||||
.enable();
|
||||
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>(
|
||||
@@ -377,4 +443,4 @@ void BuildingLayoutGraph::graph_module::room_growth_module(
|
||||
"not all rooms were assigned: %d < %d",
|
||||
assigned_count, count);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user