From c0248e1b11a6caad5db1f87210e0e97db26f917d Mon Sep 17 00:00:00 2001 From: Sergey Lapin Date: Mon, 25 Nov 2024 16:20:48 +0300 Subject: [PATCH] Updated test --- .vscode/settings.json | 3 +- godot/astream/building_layout_data.conf | 94 +- src/flecs | 2 +- src/godot | 2 +- src/meshoptimizer | 2 +- src/modules/stream/ui/graph_module_growth.cpp | 1148 +---------------- src/modules/stream/ui/graph_module_rooms.cpp | 10 + src/modules/stream/ui/region_tree.cpp | 16 - tests/godot_mockery/core/ustring.cpp | 145 +++ tests/godot_mockery/core/vector.h | 2 +- tests/regions.cpp | 28 + 11 files changed, 249 insertions(+), 1203 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index e1d9be1..43e0534 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -70,7 +70,8 @@ "format": "cpp", "future": "cpp", "cassert": "cpp", - "bitset": "cpp" + "bitset": "cpp", + "unordered_set": "cpp" }, "cmake.ignoreCMakeListsMissing": true } \ No newline at end of file diff --git a/godot/astream/building_layout_data.conf b/godot/astream/building_layout_data.conf index d2eec58..ae1773c 100644 --- a/godot/astream/building_layout_data.conf +++ b/godot/astream/building_layout_data.conf @@ -132,7 +132,7 @@ entries=[ { "type": "zone", "zone_type": 0 }, { -"children": [ 49, 50, 51, 52 ], +"children": [ 49, 50, 51, 52, 53 ], "commands": [ ], "index": 17, "name": "zone_3", @@ -260,7 +260,7 @@ entries=[ { "type": "room", "window": true }, { -"children": [ 53, 54, 55 ], +"children": [ 54, 55, 56 ], "commands": [ [ 3, [ ] ], [ 4, [ ] ] ], "index": 30, "name": "zone_0", @@ -268,7 +268,7 @@ entries=[ { "type": "zone", "zone_type": 0 }, { -"children": [ 56, 57 ], +"children": [ 57, 58 ], "commands": [ [ 3, [ ] ], [ 4, [ ] ] ], "index": 31, "name": "zone_1", @@ -286,7 +286,7 @@ entries=[ { "type": "room", "window": true }, { -"children": [ 58 ], +"children": [ 59 ], "commands": [ [ 5, [ ] ] ], "index": 33, "name": "stair_0", @@ -296,7 +296,7 @@ entries=[ { "type": "room", "window": true }, { -"children": [ 59, 60, 61 ], +"children": [ 60, 61, 62 ], "commands": [ [ 3, [ ] ], [ 4, [ ] ] ], "index": 34, "name": "zone_0", @@ -304,7 +304,7 @@ entries=[ { "type": "zone", "zone_type": 0 }, { -"children": [ 62, 63 ], +"children": [ 63, 64 ], "commands": [ [ 3, [ ] ], [ 4, [ ] ] ], "index": 35, "name": "zone_1", @@ -312,7 +312,7 @@ entries=[ { "type": "zone", "zone_type": 1 }, { -"children": [ 64 ], +"children": [ 65 ], "commands": [ [ 3, [ ] ], [ 4, [ ] ] ], "index": 36, "name": "zone_0", @@ -320,7 +320,7 @@ entries=[ { "type": "zone", "zone_type": 0 }, { -"children": [ 65, 66 ], +"children": [ 66, 67 ], "commands": [ [ 3, [ ] ], [ 4, [ ] ] ], "index": 37, "name": "zone_1", @@ -328,7 +328,7 @@ entries=[ { "type": "zone", "zone_type": 1 }, { -"children": [ 67 ], +"children": [ 68 ], "commands": [ [ 3, [ ] ], [ 4, [ ] ] ], "index": 38, "name": "zone_0", @@ -336,7 +336,7 @@ entries=[ { "type": "zone", "zone_type": 0 }, { -"children": [ 68 ], +"children": [ 69 ], "commands": [ [ 3, [ ] ], [ 4, [ ] ] ], "index": 39, "name": "zone_1", @@ -344,7 +344,7 @@ entries=[ { "type": "zone", "zone_type": 1 }, { -"children": [ 69 ], +"children": [ 70 ], "commands": [ [ 3, [ ] ], [ 4, [ ] ] ], "index": 40, "name": "zone_0", @@ -352,7 +352,7 @@ entries=[ { "type": "zone", "zone_type": 0 }, { -"children": [ 70 ], +"children": [ 71 ], "commands": [ [ 3, [ ] ], [ 4, [ ] ] ], "index": 41, "name": "zone_1", @@ -471,8 +471,18 @@ entries=[ { "window": true }, { "children": [ ], -"commands": [ [ 5, [ ] ] ], +"commands": [ ], "index": 53, +"name": "enterance_1", +"order": 5, +"room_area": 16.0, +"room_type": 304, +"type": "room", +"window": true +}, { +"children": [ ], +"commands": [ [ 5, [ ] ] ], +"index": 54, "name": "bathroom_0", "order": 0, "room_area": 16.0, @@ -482,7 +492,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 54, +"index": 55, "name": "wc_0", "order": 1, "room_area": 16.0, @@ -492,7 +502,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 55, +"index": 56, "name": "bedroom_0", "order": 2, "room_area": 36.0, @@ -502,7 +512,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 56, +"index": 57, "name": "living_room_0", "order": 0, "room_area": 36.0, @@ -512,7 +522,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 57, +"index": 58, "name": "kitchen_0", "order": 1, "room_area": 16.0, @@ -520,17 +530,17 @@ entries=[ { "type": "room", "window": true }, { -"children": [ 71, 72 ], +"children": [ 72, 73 ], "commands": [ ], "floor_index": 1, -"index": 58, +"index": 59, "name": "floor_0", "order": 0, "type": "floor" }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 59, +"index": 60, "name": "wc_0", "order": 0, "room_area": 16.0, @@ -540,7 +550,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 60, +"index": 61, "name": "bathroom_0", "order": 1, "room_area": 16.0, @@ -550,7 +560,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 61, +"index": 62, "name": "bedroom_0", "order": 2, "room_area": 64.0, @@ -560,7 +570,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 62, +"index": 63, "name": "kitchen_0", "order": 0, "room_area": 16.0, @@ -570,7 +580,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 63, +"index": 64, "name": "living_room_0", "order": 1, "room_area": 144.0, @@ -580,7 +590,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 64, +"index": 65, "name": "bathroom_0", "order": 0, "room_area": 16.0, @@ -590,7 +600,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 65, +"index": 66, "name": "kitchen_0", "order": 0, "room_area": 64.0, @@ -600,7 +610,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 66, +"index": 67, "name": "living_room_0", "order": 1, "room_area": 64.0, @@ -610,7 +620,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 67, +"index": 68, "name": "wc_0", "order": 0, "room_area": 16.0, @@ -620,7 +630,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 68, +"index": 69, "name": "living_room_0", "order": 0, "room_area": 64.0, @@ -630,7 +640,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 69, +"index": 70, "name": "bathroom_0", "order": 0, "room_area": 16.0, @@ -640,7 +650,7 @@ entries=[ { }, { "children": [ ], "commands": [ [ 5, [ ] ] ], -"index": 70, +"index": 71, "name": "living_room_0", "order": 0, "room_area": 64.0, @@ -648,24 +658,24 @@ entries=[ { "type": "room", "window": true }, { -"children": [ 73 ], +"children": [ 74 ], "commands": [ ], -"index": 71, +"index": 72, "name": "zone_0", "order": 0, "type": "zone", "zone_type": 1 }, { -"children": [ 74, 75 ], +"children": [ 75, 76 ], "commands": [ ], -"index": 72, +"index": 73, "name": "unit_0", "order": 0, "type": "unit" }, { "children": [ ], "commands": [ ], -"index": 73, +"index": 74, "name": "storage_room_0", "order": 0, "room_area": 64.0, @@ -673,17 +683,17 @@ entries=[ { "type": "room", "window": true }, { -"children": [ 76 ], +"children": [ 77 ], "commands": [ ], -"index": 74, +"index": 75, "name": "zone_0", "order": 0, "type": "zone", "zone_type": 0 }, { -"children": [ 77 ], +"children": [ 78 ], "commands": [ ], -"index": 75, +"index": 76, "name": "zone_1", "order": 0, "type": "zone", @@ -691,7 +701,7 @@ entries=[ { }, { "children": [ ], "commands": [ ], -"index": 76, +"index": 77, "name": "wc_0", "order": 0, "room_area": 16.0, @@ -701,7 +711,7 @@ entries=[ { }, { "children": [ ], "commands": [ ], -"index": 77, +"index": 78, "name": "living_room_0", "order": 0, "room_area": 16.0, diff --git a/src/flecs b/src/flecs index 3710bec..e3219d7 160000 --- a/src/flecs +++ b/src/flecs @@ -1 +1 @@ -Subproject commit 3710bec50554ce1549f0ccd9c87f6ba3ea0a9b5d +Subproject commit e3219d774d4b52b3cf4e97faaba9b88806498bdc diff --git a/src/godot b/src/godot index 12e9b22..f75abbb 160000 --- a/src/godot +++ b/src/godot @@ -1 +1 @@ -Subproject commit 12e9b22777c5e8de47b6731e7b5367cf1276af21 +Subproject commit f75abbb3ec4a7a9046159f78431f234542400d7a diff --git a/src/meshoptimizer b/src/meshoptimizer index ebd0378..bab6997 160000 --- a/src/meshoptimizer +++ b/src/meshoptimizer @@ -1 +1 @@ -Subproject commit ebd0378fd30785cf2a44cdc3fa4f81ed3868511b +Subproject commit bab69978d8f035ff32d6b5e7efb64196e284508c diff --git a/src/modules/stream/ui/graph_module_growth.cpp b/src/modules/stream/ui/graph_module_growth.cpp index 252ac26..097fdef 100644 --- a/src/modules/stream/ui/graph_module_growth.cpp +++ b/src/modules/stream/ui/graph_module_growth.cpp @@ -2,6 +2,10 @@ #include "editor_event.h" #include "world_editor.h" #include "building_layout_graph.h" +#include "grid_misc.h" +#include "queries.h" +#include "grow_job.h" +#include "region_tree.h" #include "graph_module.h" struct grid_calc { @@ -45,1147 +49,11 @@ struct grid_calc { assert(grid_size > 0); } }; -struct grid_misc { - LocalVector > positions; - LocalVector accepted; - struct make_random r; - grid_misc() - : r(100) - { - } - void get_dim_candidates(const Vector2i &base, int dim, - Vector2i *candidates) - { - int i; - std::vector candidates_data = { - /* clang-format off */ - { base.x + dim, base.y }, - { base.x, base.y + dim }, - { base.x - dim, base.y }, - { base.x, base.y - dim }, - { base.x + dim, base.y }, - { base.x, base.y + dim }, - { base.x - dim, base.y }, - { base.x, base.y - dim }, - /* clang-format on */ - }; - for (i = 0; i < (int)candidates_data.size(); i++) - candidates[i] = candidates_data[i]; - } - int get_floor_index(flecs::entity e) const - { - return e.get() - ->index; - } - flecs::entity get_grid_floor(flecs::entity grid_e, - flecs::entity graph_floor_e) - { - assert(grid_e.is_valid()); - assert(graph_floor_e.is_valid()); - int floor_index = get_floor_index(graph_floor_e); - String floor_name = "floor_" + itos(floor_index); - flecs::log::dbg("floor: %s", floor_name.ascii().ptr()); - flecs::entity floor_e = grid_e.lookup(floor_name.ascii().ptr()); - assert(floor_e.is_valid()); - return floor_e; - } - bool check_duplicates() - { - int i, j; - bool result = true; - for (i = 0; i < (int)positions.size(); i++) - for (j = 0; j < (int)positions.size(); j++) { - if (i == j) - continue; - if (positions[i].second == - positions[j].second) { - flecs::log::err("duplicate positions"); - result = false; - goto out; - } - } -out: - return result; - } - void place_regions(flecs::entity grid_floor_e) - { - int i; - int grid_size = - grid_floor_e - .get() - ->grid_size; - flecs::log::dbg("###=== %s", grid_floor_e.path().c_str()); - flecs::entity grid_e = grid_floor_e.parent(); - assert(grid_e.is_valid()); - growth_regions *g = grid_floor_e.get_mut(); - for (i = 0; i < (int)positions.size(); i++) { - int cell_id = positions[i].second.x + - grid_size * positions[i].second.y; - flecs::entity region_e = - grid_e.world().entity(positions[i].first); - assert(region_e.is_valid()); - Rect2i check; - check.position = positions[i].second; - check.size = Vector2i(1, 1); - flecs::log::dbg("%s ->region: %s cell_id %d", - grid_floor_e.path().c_str(), - region_e.path().c_str(), cell_id); - assert(g->check_region(-1, check)); - flecs::entity cell_e = - g->create_cell(grid_floor_e, region_e, cell_id); - float area = get_entity_area(region_e); - flecs::log::dbg("region: %s: parent: %s", - region_e.path().c_str(), - region_e.parent().path().c_str()); - g->create_region(grid_floor_e, cell_e, - region_e.parent(), region_e, - positions[i].second, area); - flecs::log::warn("grid cell: %s", - cell_e.path().c_str()); - } - flecs::log::dbg("###=== %s done", grid_floor_e.path().c_str()); - } - float get_entity_area(flecs::entity e) const - { - return e.get() - ->area; - } - float get_entity_area(flecs::world &&ecs, flecs::entity_t et) const - { - flecs::entity e = ecs.entity(et); - return e.get() - ->area; - } - float get_entity_area(flecs::world &ecs, flecs::entity_t et) const - { - flecs::entity e = ecs.entity(et); - return e.get() - ->area; - } - int get_base_radius(flecs::world &&ecs, flecs::entity_t et) const - { - float base_area = get_entity_area(ecs, et); - int base_radius = (int)((Math::sqrt(base_area) * 1.6f) / 2.0f) / - 4; /* grid conversion */ - return base_radius; - } - int get_base_radius(flecs::world &ecs, flecs::entity_t et) const - { - float base_area = get_entity_area(ecs, et); - int base_radius = (int)((Math::sqrt(base_area) * 1.6f) / 2.0f) / - 4; /* grid conversion */ - return base_radius; - } - int distance_squared(const Vector2i &p1, const Vector2i &p2) const - { - int lx = p2.x - p1.x; - int ly = p2.y - p1.y; - return lx * lx + ly * ly; - } - bool check_candidates_tolerance(const Vector2i check) - { - int i; - bool ret = true; - for (i = 0; i < (int)positions.size(); i++) - if (distance_squared(check, positions[i].second) < 1) { - ret = false; - break; - } - return ret; - } - bool check_candidate(const Vector2i &candidate) - { - int m; - bool bad = false; - for (m = 0; m < (int)positions.size(); m++) { - Rect2i check1, check2; - check1.position = positions[m].second; - check1.size = Vector2i(1, 1); - check2.position = candidate; - check2.size = Vector2i(1, 1); - if (check1.intersects(check2)) { - bad = true; - break; - } - } - return !bad; - } - bool accept_candidate(flecs::entity ce, const Vector2i &candidate, - float area) - { - int k; - int local_radius = (int)((Math::sqrt(area) * 1.6f) / 2.0f) / - 4; /* grid conversion */ - local_radius = MAX(1, local_radius); - bool ok = false; - for (k = 0; k < (int)positions.size(); k++) { - assert(k < (int)positions.size()); - flecs::entity_t pbase_et = positions[k].first; - float parea = get_entity_area(ce.world(), pbase_et); - int pdim = (int)((Math::sqrt(parea) * 1.5f) / 2.0f) / - 4; /* radius converted to grid 4x4*/ - int radius = pdim + local_radius; - int radius_sq = radius * radius; - if (distance_squared(positions[k].second, candidate) < - radius_sq) { - flecs::log::dbg( - "too close to positions %d < %d", - distance_squared(positions[k].second, - candidate), - radius_sq); - continue; - } - assert(check_candidates_tolerance(candidate)); - accepted.push_back(candidate); - ok = true; - } - return ok; - } - bool accept_candidate2(flecs::entity ce, const Vector2i &candidate) - { - int k; - bool ok = false; - for (k = 0; k < (int)positions.size(); k++) { - assert(k < (int)positions.size()); - flecs::entity_t pbase_et = positions[k].first; - int radius_sq = 4; - if (distance_squared(positions[k].second, candidate) < - radius_sq) { - flecs::log::dbg( - "too close to positions %d < %d", - distance_squared(positions[k].second, - candidate), - radius_sq); - continue; - } - assert(check_candidates_tolerance(candidate)); - accepted.push_back(candidate); - ok = true; - } - return ok; - } - bool process_candidates(flecs::entity ce, const Vector2i &base, - float area, flecs::entity_t base_et, int dim, - const Rect2i &clip, bool tight) - { - int j; - assert(dim > 1); - int base_radius = get_base_radius(ce.world(), base_et); - base_radius = MAX(1, base_radius); - int local_radius = (int)((Math::sqrt(area) * 1.6f) / 2.0f) / - 4; /* grid conversion */ - local_radius = MAX(1, local_radius); - // int dim = base_radius + local_radius; - // dim = MAX(4, dim); - Vector2i candidates[8]; - assert(clip.has_point(base)); - get_dim_candidates(base, dim, &candidates[0]); - for (j = 0; - j < (int)(sizeof(candidates) / sizeof(candidates[0])); - j++) { - print_line("base: " + itos(base.x) + ", " + - itos(base.y)); - print_line( - "possible candidate: " + itos(candidates[j].x) + - ", " + itos(candidates[j].y)); - if (!clip.has_point(candidates[j])) { - print_line("clipped by grid field"); - print_line(clip.operator String()); - continue; - } - if (!check_candidates_tolerance(candidates[j])) { - print_line("too close to existing positions"); - continue; - } - if (!check_candidate(candidates[j])) { - print_line( - "too close to existing positions (rect)"); - continue; - } - print_line("valid candidate: " + itos(candidates[j].x) + - ", " + itos(candidates[j].y)); - flecs::log::dbg( - "accepting candidate: %s", - (candidates[j].operator String()).ascii().ptr()); - if (!tight) - accept_candidate(ce, candidates[j], area); - else - accept_candidate2(ce, candidates[j]); - } - if (accepted.size() > 0) - return true; - else - return false; - } - void filter_candidates(flecs::entity ce, float area, - const Rect2i &clip_rect) - { - if (positions.empty()) { - /* starting at grid center */ - Vector2i start_pos( - clip_rect.position.x + clip_rect.size.x / 2, - clip_rect.position.y + clip_rect.size.y / 2); - positions.push_back(Pair( - ce.id(), start_pos)); - return; - } - while (1) { - int which = which_position(); - const Vector2i &base = positions[which].second; - flecs::entity_t base_et = positions[which].first; - int base_radius = get_base_radius(ce.world(), base_et); - base_radius = MAX(1, base_radius); - int local_radius = - (int)((Math::sqrt(area) * 1.6f) / 2.0f) / - 4; /* grid conversion */ - local_radius = MAX(1, local_radius); - /* minimum distance between region positions */ - int dim = base_radius + local_radius; - dim = MAX(2, dim); - int md = 1; - int clip_size = MIN(clip_rect.size.x, clip_rect.size.y); - assert(dim > 1); - - if (clip_size > 30) - md = 8; - else if (clip_size > 20) - md = 6; - else if (clip_size > 15) - md = 4; - else if (clip_size > 10) - md = 2; - if (clip_size < 6) - md = 0; - Rect2i clip(clip_rect.position.x + md, - clip_rect.position.y + md, - clip_rect.size.x - md * 2, - clip_rect.size.y - md * 2); -#if 0 - Rect2i dim_rect_min(base.x - dim, base.y - dim, dim, - dim), - dim_rect_max(base.x, base.y, dim, dim); - dim_rect_min = clip.clip(dim_rect_min); - dim_rect_max = clip.clip(dim_rect_max); - int dim_min = - MAX(dim_rect_min.size.x, dim_rect_min.size.y); - int dim_max = - MAX(dim_rect_max.size.x, dim_rect_max.size.y); - int avg_dim = (dim_min + dim_max) / 2; - dim = MIN(dim, avg_dim - 1); -#endif - flecs::log::dbg("dim=%d", dim); - assert(dim > 1); - assert(clip.has_point(base)); - flecs::log::warn( - "filter_candidates: %d %d / %s / %s", - clip_size, dim, - (base.operator String()).ascii().ptr(), - (clip.operator String()).ascii().ptr()); - assert(clip.has_point(base)); - assert(clip.has_point(base + Vector2i(dim, dim)) || - clip.has_point(base + Vector2i(dim, 0)) || - clip.has_point(base + Vector2i(0, dim)) || - clip.has_point(base + Vector2i(-dim, -dim)) || - clip.has_point(base + Vector2i(-dim, 0)) || - clip.has_point(base + Vector2i(0, -dim))); - if (dim < 4) - process_candidates(ce, base, area, base_et, dim, - clip, true); - else - process_candidates(ce, base, area, base_et, dim, - clip, false); - if (accepted.size() > 0) { - assert(accepted.size() > 0); - const Vector2i &selected = - accepted[which_selected()]; - assert(check_candidates_tolerance(selected)); - Pair m(ce.id(), - selected); - check_candidate(selected); - positions.push_back(m); - flecs::log::warn("add position: %d, %d", - selected.x, selected.y); - accepted.clear(); - break; - } else { - assert(positions.size() > 0); - continue; - } - } - } - int which_position() - { - int which; - if (positions.size() == 0) - return -1; - which = r.get() % positions.size(); - assert(which < (int)positions.size()); - return which; - } - int which_selected() - { - int which; - if (accepted.size() == 0) - return -1; - which = r.get() % accepted.size(); - assert(which < (int)accepted.size()); - return which; - } - void place_region_cells( - flecs::entity grid_floor_e, - const WorldEditor::components::buildings_layout_grid_floor &fl, - growth_regions &g) - { - int i; - for (i = 0; i < g.regions.size(); i++) { - int x, y; - Rect2i rect = g.regions[i].rect; - for (x = rect.position.x; - x <= rect.position.x + rect.size.x; x++) - for (y = rect.position.y; - y <= rect.position.y + rect.size.y; y++) { - int id = x + fl.grid_size * y; - if (!fl.cells.has(id)) { - flecs::entity seed_e = - grid_floor_e.world().entity( - g.regions[i] - .seed_et); - assert(seed_e.is_valid()); - assert(seed_e.parent() - .is_valid()); - assert(seed_e.parent().id() == - grid_floor_e.id()); - queue_grow_cell(seed_e, id); - } - } - } - } - void queue_grow_cell(flecs::entity seed_e, int id) - { - assert(seed_e.is_valid()); - flecs::entity floor_e = seed_e.parent(); - if (!floor_e.is_valid()) - flecs::log::err("the parent of %s is not grid floor", - seed_e.path().c_str()); - assert(floor_e.is_valid()); - if (!floor_e.has()) - flecs::log::err("the parent of %s is not grid floor %s", - seed_e.path().c_str(), - floor_e.path().c_str()); - assert(floor_e.has< - WorldEditor::components::buildings_layout_grid_floor>()); - Pair m(seed_e.id(), id); - if (!floor_e.has()) { - List > queue; - queue.push_back(m); - floor_e.set( - { queue }); - } else { - floor_e.get_mut() - ->queue.push_back(m); - floor_e.modified(); - } - } - void print_can_grow(int state, growth_regions &g, int index, - const char *what) - { - const growth_regions::region ®ion = g.regions[index]; - bool can = false; - if (!strcmp(what, "square")) - can = region.can_grow_square; - else if (!strcmp(what, "rect")) - can = region.can_grow; - - if (can) - flecs::log::dbg( - "state: %d: index %d: region can still grow %s %d", - state, index, what, region.remains_area); - else - flecs::log::dbg( - "state: %d: index %d: region can't grow %s %d", - state, index, what, region.remains_area); - if (region.can_grow_region()) - flecs::log::dbg( - "state %d: index %d: region can still continue", - state, index); - } - bool grow_state0(growth_regions &g, int index, const Rect2i &clip) - { - bool ok = true, ret = false; - Rect2i mrect; - growth_regions::region ®ion = g.regions.write[index]; - mrect = region.rect; - assert(g.check_region(index, mrect)); - - mrect = mrect.grow(1); - ok = clip.encloses(mrect); - if (!ok) - flecs::log::dbg("state: %d: index %d: out of clip area", - 0, index); - if (ok) { - ok = g.check_region(index, mrect); - if (!ok) - flecs::log::dbg( - "state: %d: index %d: check_region failed", - 0, index); - } - if (ok) { - ret = region.update_region_size(mrect); - if (!ret) - flecs::log::dbg( - "state: %d: index %d: update_region_size failed", - 0, index); - } - print_can_grow(0, g, index, "square"); - if (!ret) - flecs::log::dbg("state %d could not grow region %d: %d", - 0, index, region.remains_area); - else - flecs::log::dbg("state %d could grow region %d: %d", 0, - index, region.remains_area); - return ret; - } - bool grow_state1(growth_regions &g, int index, const Rect2i &clip) - { - int d; - bool ret = false; - Rect2i mrect; - growth_regions::region ®ion = g.regions.write[index]; - int count = 0; - for (d = 0; d < 4; d++) { - bool ok; - mrect = region.rect; - assert(g.check_region(index, mrect)); - switch (d) { - case 0: - mrect.position.y -= 1; - mrect.size.y += 1; - break; - case 1: - mrect.size.y += 1; - break; - case 2: - mrect.position.x -= 1; - mrect.size.x += 1; - break; - case 3: - mrect.size.x += 1; - break; - } - ok = clip.encloses(mrect); - if (ok) { - ok = false; - if (mrect.size.y > 0 && mrect.size.x > 0) { - float aspect = (float)mrect.size.x / - (float)mrect.size.y; - ok = (aspect > 0.2f && aspect < 5.0f); - } - } - if (ok) - ok = g.check_region(index, mrect); - if (ok) { - bool result = region.update_region_size(mrect); - if (result) - count++; - } - } - if (count > 0) { - ret = true; - flecs::log::dbg( - "state %d could grow region %d: %d - %d out of %d times", - 0, index, region.remains_area, count, 4); - } - print_can_grow(1, g, index, "rect"); - if (!ret) - flecs::log::dbg( - "state %d could not grow region rect %d: %d", 0, - index, region.remains_area); - return ret; - } - template - void grow_region_rects( - T *obj, flecs::entity floor_e, - WorldEditor::components::buildings_layout_grid_floor &fl, - growth_regions &g) - { - int i; - bool grown = true; - int state = 0; - if (g.complete) - return; - Rect2i clip(0, 0, fl.grid_size, fl.grid_size); - state = 0; - flecs::log::dbg("growing square"); - while (grown) { - grown = false; - int count = 0; - for (i = 0; i < g.regions.size(); i++) { - growth_regions::region ®ion = - g.regions.write[i]; - Rect2i mrect = region.rect; - flecs::log::dbg("grow_region_rects: region %d", - i); - if (!region.can_grow_region()) { - flecs::log::dbg( - "grow_region_rects: skip %d", - i); - continue; - } - mrect = region.rect; - bool result = false; - result = grow_state0(g, i, clip); - if (result) - count++; - } - if (count > 0) { - grown = true; - flecs::log::dbg("grown squares %d times of %d", - count, g.regions.size()); - } - if (!grown) - flecs::log::dbg( - "grow_region_rects: could not grow more squares"); - } - state = 1; - grown = true; - flecs::log::dbg("growing rect"); - while (grown) { - grown = false; - int count = 0; - for (i = 0; i < g.regions.size(); i++) { - growth_regions::region ®ion = - g.regions.write[i]; - Rect2i mrect = region.rect; - flecs::log::dbg("grow_region_rects: region %d", - i); - if (!region.can_grow_region()) { - flecs::log::dbg( - "grow_region_rects: skip %d", - i); - continue; - } - mrect = region.rect; - bool result = false; - result = grow_state1(g, i, clip); - if (result) - count++; - if (!result) - flecs::log::dbg( - "state %d could not grow region %d", - state, i); - else - flecs::log::dbg( - "state %d could grow region %d: %d", - state, i, region.remains_area); - } - if (count > 0) { - grown = true; - flecs::log::dbg("grown rects %d times of %d", - count, g.regions.size()); - } - if (!grown) - flecs::log::dbg( - "grow_region_rects: could not grow any more rects"); - } - flecs::log::dbg("grow_region_rects: complete"); - g.complete = true; - flecs::log::dbg("grow_region_rects: %s: done", - floor_e.path().c_str()); - } - Vector > all_items; - void subregions_init(flecs::world w) - { - flecs::query - qprep = w.query_builder() - .scope_open() - .with() - .or_() - .with() - .scope_close() - .build(); - qprep.each([this](flecs::entity ce, - const WorldEditor::components:: - buildings_layout_area &area) { - all_items.push_back({ ce, ce.parent().id() }); - }); - } - void get_subregions(flecs::entity parent, - List > *subregions) - { - flecs::query - qprep = parent.world() - .query_builder< - const WorldEditor::components:: - buildings_layout_area>() - .with(flecs::ChildOf, parent) - .scope_open() - .with() - .or_() - .with() - .or_() - .with() - .scope_close() - .build(); - qprep.each([&subregions](flecs::entity ce, - const WorldEditor::components:: - buildings_layout_area &area) { - subregions->push_back({ ce, area.area }); - }); - } -}; - -struct queries { - flecs::query_builder - qp; - flecs::query_builder - qr; - flecs::query_builder< - const WorldEditor::components::buildings_layout_grid_cell> - mark_cells; - HashMap > > - sub_subregions; - flecs::query - get_qp() - { - return qp.build(); - } - flecs::query - get_qr() - { - return qr.build(); - } - flecs::query - get_mark_cells() - { - return mark_cells.build(); - } - queries(flecs::world &w) - : qp(w.query_builder() - .write()) - , qr(w.query_builder< - WorldEditor::components::buildings_layout_grid_floor, - growth_regions>()) - , mark_cells( - w.query_builder() - .without() - .read() - .write() - .write()) - { - } - int get_floor_index(flecs::entity e) const - { - return e.get() - ->index; - } - flecs::entity get_grid_floor(flecs::entity grid_e, - flecs::entity graph_floor_e) - { - assert(grid_e.is_valid()); - assert(graph_floor_e.is_valid()); - int floor_index = get_floor_index(graph_floor_e); - String floor_name = "floor_" + itos(floor_index); - flecs::log::dbg("floor: %s", floor_name.ascii().ptr()); - flecs::entity floor_e = grid_e.lookup(floor_name.ascii().ptr()); - assert(floor_e.is_valid()); - return floor_e; - } - void get_subregions(flecs::entity parent, - List > *subregions) - { - flecs::query - qprep = parent.world() - .query_builder< - const WorldEditor::components:: - buildings_layout_area>() - .with(flecs::ChildOf, parent) - .scope_open() - .with() - .or_() - .with() - .or_() - .with() - .scope_close() - .build(); - qprep.each([&subregions](flecs::entity ce, - const WorldEditor::components:: - buildings_layout_area &area) { - subregions->push_back({ ce, area.area }); - }); - } - void build_subregions( - flecs::entity grid_e, struct grid_misc &grid, - const WorldEditor::components::buildings_layout_grid_size &size) - { - const List >::Element *me = - size.floors.front(); - List subregions_queue; - while (me) { - subregions_queue.push_back(me->get().second); - flecs::entity_t base_floor_et = me->get().second; - flecs::entity base_floor_e = - grid_e.world().entity(base_floor_et); - flecs::log::dbg("job_queue: base_floor: %s", - base_floor_e.path().c_str()); - flecs::entity grid_floor_e = - get_grid_floor(grid_e, base_floor_e); - flecs::log::dbg("job_queue: grid_floor: %s", - grid_floor_e.path().c_str()); - if (!grid_floor_e - .has()) - create_floor_components(grid_floor_e, - base_floor_e, size); - me = me->next(); - } - while (!subregions_queue.empty()) { - // eid is graph entity - flecs::entity_t eid = subregions_queue.front()->get(); - subregions_queue.pop_front(); - List > subregions; - flecs::entity item_e = grid_e.world().entity(eid); - get_subregions(grid_e.world().entity(eid), &subregions); - if (subregions.empty()) - flecs::log::dbg("!no subregions for: %s", - item_e.path().c_str()); - sub_subregions[eid] = subregions; - List >::Element *fe = - subregions.front(); - while (fe) { - subregions_queue.push_back( - fe->get().first.id()); - fe = fe->next(); - } - } - } - void create_floor_components( - flecs::entity floor_e, flecs::entity base_floor_e, - const WorldEditor::components::buildings_layout_grid_size &size) - const - { - assert(!floor_e.has< - WorldEditor::components::buildings_layout_grid_floor>()); - assert(!floor_e.has()); - floor_e.set( - { Set(), size.grid_size, size.growth_size }); - floor_e.set( - { Vector(), false }); - floor_e.add(base_floor_e); - } -}; -struct grow_job_queue { - struct grow_job { - enum { INITIAL, COMMON }; - int job_type; - flecs::entity_t parent_id; - flecs::entity_t base_floor_id; - flecs::entity_t grid_floor_id; - List > subregions; - struct grid_misc grid; - }; - flecs::entity grid_e; - int grid_size; - struct queries &queries; - const String &module_name; - List job_list; - grow_job_queue( - flecs::entity grid_e, struct grid_misc &grid, - struct queries &queries, - const WorldEditor::components::buildings_layout_grid_size &size, - const String &module_name); - void iterate(); -}; -grow_job_queue::grow_job_queue( - flecs::entity grid_e, struct grid_misc &grid, struct queries &queries, - const WorldEditor::components::buildings_layout_grid_size &size, - const String &module_name) - : grid_e(grid_e) - , grid_size(size.grid_size) - , queries(queries) - , module_name(module_name) -{ - const List >::Element *me; - List > job_create_queue; - me = size.floors.front(); - while (me) { - // graph entity - job_create_queue.push_back( - { me->get().second, me->get().second }); - me = me->next(); - } - while (!job_create_queue.empty()) { - // graph entity - flecs::entity_t et = job_create_queue.front()->get().first; - // parent graph entity - flecs::entity_t base_floor_et = - job_create_queue.front()->get().second; - job_create_queue.pop_front(); - flecs::entity base_floor_e = - grid_e.world().entity(base_floor_et); - flecs::log::dbg("job_queue: base_floor: %s", - base_floor_e.path().c_str()); - flecs::entity grid_floor_e = - grid.get_grid_floor(grid_e, base_floor_e); - flecs::log::dbg("job_queue: grid_floor: %s", - grid_floor_e.path().c_str()); - if (queries.sub_subregions.has(et) && - !queries.sub_subregions[et].empty()) { - flecs::log::dbg( - "subregions for: %s", - grid_e.world().entity(et).path().c_str()); - struct grow_job job; - if (job_list.size() == 0) - job.job_type = grow_job::INITIAL; - else - job.job_type = grow_job::COMMON; - job.parent_id = et; - job.base_floor_id = base_floor_et; - job.grid_floor_id = grid_floor_e.id(); - job.subregions = queries.sub_subregions[job.parent_id]; - job_list.push_back(job); - List >::Element *se = - job.subregions.front(); - while (se) { - job_create_queue.push_back( - { se->get().first.id(), - base_floor_et }); - se = se->next(); - } - } else - flecs::log::dbg( - "no subregions for: %s", - grid_e.world().entity(et).path().c_str()); - } - flecs::log::dbg("created jobs: %d", job_list.size()); -} -void grow_job_queue::iterate() -{ - while (!job_list.empty()) { - List::Element *job_e = job_list.front(); - const List > &subregions = - job_e->get().subregions; - const List >::Element *fe = - subregions.front(); - flecs::entity base_floor_e = - grid_e.world().entity(job_e->get().base_floor_id); - flecs::entity grid_floor_e = - grid_e.world().entity(job_e->get().grid_floor_id); - flecs::log::dbg("create: base_floor: %s", - base_floor_e.path().c_str()); - flecs::log::dbg("create: grid_floor: %s", - grid_floor_e.path().c_str()); - growth_regions *g = grid_floor_e.get_mut(); - switch (job_e->get().job_type) { - case grow_job::INITIAL: { - int i; - Rect2i clip_rect(0, 0, grid_size, grid_size); - bool restart = false, found = false; - for (i = 0; i < (int)g->regions.size(); i++) { - flecs::log::dbg( - "region %d: %s", i, - grid_e.world() - .entity(g->regions[i].region_et) - .path() - .c_str()); - if (g->regions[i].region_et == - job_e->get().parent_id) { - flecs::log::err( - "parent is in regions %d", i); - if (!g->regions[i].complete) { - job_list.push_back( - job_e->get()); - job_list.pop_front(); - restart = true; - flecs::log::err( - "parent is in regions %d incomplete", - i); - flecs::log::warn( - "delay job fo %s (%d jobs)", - grid_e.world() - .entity(job_e->get() - .parent_id) - .path() - .c_str(), - job_list.size()); - break; - } else { - clip_rect = g->regions[i].rect; - g->regions.remove(i); - break; - } - found = true; - } - } - if (!found && g->regions.size() > 0) { - flecs::entity parent_e = grid_e.world().entity( - job_e->get().parent_id); - if (base_floor_e != parent_e.parent()) { - // no parent region yet - flecs::log::warn( - "%s != %s", - base_floor_e.path().c_str(), - parent_e.parent() - .path() - .c_str()); - job_list.push_back(job_e->get()); - job_list.pop_front(); - flecs::log::warn( - "delay job fo %s (%d jobs)", - parent_e.path().c_str(), - job_list.size()); - restart = true; - } - } - if (restart) - continue; - - while (fe) { - flecs::entity ce = fe->get().first; - float area = fe->get().second; - flecs::log::warn("generating positions for: %s", - ce.path().c_str()); - job_e->get().grid.filter_candidates(ce, area, - clip_rect); - fe = fe->next(); - } - job_e->get().grid.place_regions(grid_floor_e); - - flecs::log::dbg("Running grow..."); - queries.get_qr().each( - [this](flecs::entity grid_floor_e, - WorldEditor::components:: - buildings_layout_grid_floor &fl, - growth_regions &g) { - struct grid_misc grd; - if (g.complete) - return; - grd.grow_region_rects( - this, grid_floor_e, fl, g); - grd.place_region_cells(grid_floor_e, fl, - g); - }); - queries.get_qr().each( - [](flecs::entity grid_floor_e, - WorldEditor::components:: - buildings_layout_grid_floor &fl, - growth_regions &g) { - int i; - for (i = 0; i < (int)g.regions.size(); - i++) - g.regions.write[i].complete = - true; - g.complete = true; - }); - grid_e.world() - .system(grid_e.world().lookup( - (module_name + "::GrowCommitQueue") - .ascii() - .ptr())) - .run(); - queries.get_mark_cells().each([](flecs::entity e, - const WorldEditor:: - components::buildings_layout_grid_cell - &cell) { - int grid_size = - e.parent() - .get() - ->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 neighbors = { - west_id, north_id, east_id, south_id - }; - bool outside = false; - bool border = false; - for (i = 0; i < (int)neighbors.size(); i++) { - int id = neighbors[i]; - print_line("id=" + itos(id)); - if (!e.parent() - .get() - ->cells.has(id)) { - outside = true; - break; - } - } - 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; - } - } - if (outside) - e.add(); - else - e.add(); - if (border) - e.add(); - 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)); - }); - } break; - } - job_list.pop_front(); - } - flecs::log::dbg("processed jobs (created region initial positions): %d", - job_list.size()); -} void BuildingLayoutGraph::graph_module::growth_module(flecs::world &ecs, const String &module_name) { ecs.component(); + ecs.component(); flecs::entity GraphFilter = ecs.entity("GraphFilter") .add(flecs::Phase) .depends_on(flecs::OnUpdate); @@ -1415,8 +283,8 @@ void BuildingLayoutGraph::graph_module::growth_module(flecs::world &ecs, .kind(GraphFilter) .run([module_name, this](flecs::iter &it) { flecs::world &&ecs = it.world(); - struct queries queries(ecs); it.world().defer_suspend(); + struct queries queries(ecs); flecs::log::dbg("Creating regions grow..."); queries.get_qp().each( [this, &queries, @@ -1425,7 +293,7 @@ void BuildingLayoutGraph::graph_module::growth_module(flecs::world &ecs, buildings_layout_grid_size &size) { struct grid_misc grid; - grid.subregions_init(it2.world()); + // grid.subregions_init(it2.world()); flecs::entity graph_e = it2.entity(count); flecs::log::warn( @@ -1445,7 +313,7 @@ void BuildingLayoutGraph::graph_module::growth_module(flecs::world &ecs, queries.build_subregions(grid_e, grid, size); struct grow_job_queue job_queue( - grid_e, grid, queries, size, + grid_e, queries, size, module_name); job_queue.iterate(); // assert(false); diff --git a/src/modules/stream/ui/graph_module_rooms.cpp b/src/modules/stream/ui/graph_module_rooms.cpp index 76ca242..837061c 100644 --- a/src/modules/stream/ui/graph_module_rooms.cpp +++ b/src/modules/stream/ui/graph_module_rooms.cpp @@ -1,6 +1,7 @@ #include "base_data.h" #include "world_editor.h" #include "building_layout_graph.h" +#include "grid_misc.h" #include "graph_module.h" void BuildingLayoutGraph::graph_module::room_growth_module( @@ -430,6 +431,15 @@ void BuildingLayoutGraph::graph_module::room_growth_module( if (id >= 0) { flecs::entity selected_e = prio.get_entity(id); + int mcount = 0; + selected_e.each< + WorldEditor::components:: + belongs_room>( + [&mcount](flecs::entity + e) { + mcount++; + }); + assert(mcount == 0); selected_e.add< WorldEditor::components:: belongs_room>( diff --git a/src/modules/stream/ui/region_tree.cpp b/src/modules/stream/ui/region_tree.cpp index 4768c24..674cd40 100644 --- a/src/modules/stream/ui/region_tree.cpp +++ b/src/modules/stream/ui/region_tree.cpp @@ -78,7 +78,6 @@ void region_tree::dump(flecs::entity grid_floor_e) const void region_tree::grow() { -#ifndef TESTS List grow_list; List queue; List::Element *e, *e1; @@ -296,12 +295,10 @@ void region_tree::grow() break; } #endif -#endif } void region_tree::place(flecs::entity grid_floor_e) const { -#ifndef TESTS int i, j; List queue; int grid_size = grid_floor_e @@ -340,12 +337,10 @@ void region_tree::place(flecs::entity grid_floor_e) const queue.push_back(item->children[i]); } } -#endif } void region_tree::get_rects(List *rect_list) const { -#ifndef TESTS List queue; queue.push_back(this); while (!queue.empty()) { @@ -362,14 +357,12 @@ void region_tree::get_rects(List *rect_list) const for (i = 0; i < (int)item->children.size(); i++) queue.push_back(item->children[i]); } -#endif } bool region_tree::check_candidate(int i, const RegionRect2i &candidate) const { int j; bool ok = true; -#ifndef TESTS if (!region.rect.encloses(candidate)) return false; for (j = 0; j < children.size(); j++) { @@ -392,14 +385,12 @@ bool region_tree::check_candidate(int i, const RegionRect2i &candidate) const break; } } -#endif return ok; } bool region_tree::check_candidate(const RegionRect2i &candidate) const { bool ok = true; -#ifndef TESTS if (parent && !parent->region.rect.encloses(candidate)) return false; if (candidate.intersects(region.rect)) @@ -410,7 +401,6 @@ bool region_tree::check_candidate(const RegionRect2i &candidate) const ok = false; if (region.rect.intersects(candidate)) ok = false; -#endif return ok; } @@ -421,7 +411,6 @@ flecs::entity region_tree::update_cell(flecs::entity grid_floor_e, flecs::entity region_e = grid_floor_e.world().entity(region_et); flecs::entity parent_e = grid_floor_e.world().entity(parent_et); flecs::log::dbg("create_cell: %s %d", region_e.path().c_str(), id); -#ifndef TESTS String pname("cell_" + itos(id)); flecs::entity cell_e = grid_floor_e.lookup(pname.ascii().ptr()); if (!cell_e.is_valid()) { @@ -459,19 +448,14 @@ flecs::entity region_tree::update_cell(flecs::entity grid_floor_e, cell_e.add( region_e); } -#else - flecs::entity cell_e; -#endif return cell_e; } void region_tree::update_candidate(const RegionRect2i &candidate) { -#if 0 int orig_area = region.rect.get_area(); region.rect = candidate; int new_area = candidate.get_area(); int avalue = MAX(0, new_area - orig_area); region.remains_area -= avalue; -#endif } diff --git a/tests/godot_mockery/core/ustring.cpp b/tests/godot_mockery/core/ustring.cpp index 488d9d7..c66771a 100644 --- a/tests/godot_mockery/core/ustring.cpp +++ b/tests/godot_mockery/core/ustring.cpp @@ -68,6 +68,100 @@ String &String::operator+=(const char *p_str) { return *this; } +bool String::operator==(const String &p_str) const { + if (length() != p_str.length()) { + return false; + } + if (empty()) { + return true; + } + + int l = length(); + + const CharType *src = c_str(); + const CharType *dst = p_str.c_str(); + + /* Compare char by char */ + for (int i = 0; i < l; i++) { + if (src[i] != dst[i]) { + return false; + } + } + + return true; +} + +bool String::operator!=(const String &p_str) const { + return !(*this == p_str); +} + +bool String::operator==(const char *p_str) const { + int len = 0; + const char *aux = p_str; + + while (*(aux++) != 0) { + len++; + } + + if (length() != len) { + return false; + } + if (empty()) { + return true; + } + + int l = length(); + + const CharType *dst = c_str(); + + /* Compare char by char */ + for (int i = 0; i < l; i++) { + if (p_str[i] != dst[i]) { + return false; + } + } + + return true; +} + +bool String::operator==(const CharType *p_str) const { + int len = 0; + const CharType *aux = p_str; + + while (*(aux++) != 0) { + len++; + } + + if (length() != len) { + return false; + } + if (empty()) { + return true; + } + + int l = length(); + + const CharType *dst = c_str(); + + /* Compare char by char */ + for (int i = 0; i < l; i++) { + if (p_str[i] != dst[i]) { + return false; + } + } + + return true; +} + +bool String::operator!=(const char *p_str) const { + return (!(*this == p_str)); +} + +bool String::operator!=(const CharType *p_str) const { + return (!(*this == p_str)); +} + + CharString String::ascii(bool p_allow_extended) const { if (!length()) { return CharString(); @@ -314,5 +408,56 @@ const CharType *String::c_str() const { } +bool operator==(const char *p_chr, const String &p_str) { + return p_str == p_chr; +} + +String operator+(const char *p_chr, const String &p_str) { + String tmp = p_chr; + tmp += p_str; + return tmp; +} + +String itos(int64_t p_val) { + return String::num_int64(p_val); +} + +String String::num_int64(int64_t p_num, int base, bool capitalize_hex) { + bool sign = p_num < 0; + + int64_t n = p_num; + + int chars = 0; + do { + n /= base; + chars++; + } while (n); + + if (sign) { + chars++; + } + String s; + s.resize(chars + 1); + CharType *c = s.ptrw(); + c[chars] = 0; + n = p_num; + do { + int mod = ABS(n % base); + if (mod >= 10) { + char a = (capitalize_hex ? 'A' : 'a'); + c[--chars] = a + (mod - 10); + } else { + c[--chars] = '0' + mod; + } + + n /= base; + } while (n); + + if (sign) { + c[0] = '-'; + } + + return s; +} diff --git a/tests/godot_mockery/core/vector.h b/tests/godot_mockery/core/vector.h index aa273ec..88c10c6 100644 --- a/tests/godot_mockery/core/vector.h +++ b/tests/godot_mockery/core/vector.h @@ -188,7 +188,7 @@ void Vector::append_array(Vector p_other) { template bool Vector::push_back(T p_elem) { Error err = resize(size() + 1); - assert(err != OK); + assert(err == OK); set(size() - 1, p_elem); return false; diff --git a/tests/regions.cpp b/tests/regions.cpp index 0366a68..1689a64 100644 --- a/tests/regions.cpp +++ b/tests/regions.cpp @@ -2,6 +2,34 @@ int main() { + int i; + flecs::world ecs; + flecs::entity graph_base_e = ecs.entity("graph"); + flecs::entity graph_e = ecs.entity("v1").child_of(graph_base_e); + flecs::entity grid_base_e = ecs.entity("grid"); + flecs::entity grid_e = ecs.entity("v1").child_of(grid_base_e); + flecs::entity grid_floor_e = ecs.entity("floor_0").child_of(grid_e); + List region_list; + struct region region; + region.region_et = graph_e.id(); + region.rect = RegionRect2i(0, 0, 25, 25); + struct region_tree regions; + regions.region = region; + regions.dump(grid_floor_e); + flecs::entity zone1_e = ecs.entity("zone1").child_of(graph_e), + zone2_e = ecs.entity("zone2").child_of(graph_e), + zone3_e = ecs.entity("zone3").child_of(graph_e); + struct region region_data[] = { + {.region_et = zone1_e.id(), .rect = {0, 0, 1, 1}}, + {.region_et = zone2_e.id(), .rect = {2, 2, 1, 1}}, + {.region_et = zone3_e.id(), .rect = {4, 4, 1 ,1}} + }; + for (i = 0; i < sizeof(region_data) / sizeof(region_data[0]); i++) + region_list.push_back(region_data[i]); + regions.split(region_list); + regions.dump(grid_floor_e); + regions.grow(); + regions.dump(grid_floor_e); return 0; }