Update; refactor of initial code

This commit is contained in:
Segey Lapin
2021-10-22 15:43:37 +03:00
parent da73a164a0
commit 2945dd1904
8 changed files with 397 additions and 151 deletions

View File

@@ -32,25 +32,15 @@ class CanvasItem;
* - Implement 3D positions and geometry generation
*/
class RoadGrid: public Reference {
GDCLASS(RoadGrid, Object)
protected:
Ref<RandomNumberGenerator> rnd;
struct cluster {
Vector2i c;
float r;
};
List<struct cluster> clusters;
Dictionary build_diagram(int npatches, int center_count, int center_step,
int spread, int dim);
HashMap<int, int> class_sizes;
struct half_edge;
Rect2 bounds;
void set_class_size(int cl, int sz)
{
class_sizes[cl] = sz;
}
static void _bind_methods();
struct cluster {
Vector2i c;
float r;
};
struct graphedge {
int a, b;
int edge;
};
struct map_site {
enum {
SITE_UNASSIGNED = 0,
SITE_FOREST,
@@ -59,36 +49,33 @@ protected:
SITE_EMPTY,
SITE_MAX
};
struct graphedge {
int a, b;
int edge;
};
struct half_edge {
int a, b;
int site;
float depth;
float length;
};
struct map_site {
int index;
Vector2 pos;
Vector<struct graphedge> graphedges;
Vector<Vector2> vertices;
Vector<Vector2> polygon;
Vector<int> vertices_ind;
Vector<int> polygon_ind;
int site_type;
int cluster;
Vector<struct half_edge> hedges;
Vector2 building_positions;
Rect2i rect;
float avg_height;
};
void index_site(struct map_site *site);
Vector<Vector2> diagram_vertices;
Vector<float> diagram_vertex_heights;
int index;
Vector2 pos;
Vector<struct graphedge> graphedges;
Vector<Vector2> vertices;
Vector<Vector2> polygon;
Vector<int> vertices_ind;
Vector<int> polygon_ind;
int site_type;
int cluster;
Vector<struct half_edge> hedges;
Vector2 building_positions;
Rect2i rect;
float avg_height;
};
struct half_edge {
int a, b;
int site;
float depth;
float length;
};
class RoadDiagram {
protected:
List<struct cluster> clusters;
Vector<struct map_site> map_sites;
Vector<Vector2> diagram_vertices;
Vector<struct half_edge *> map_hedges;
HashMap<int, int> class_sizes;
void classify_sites()
{
int i, j;
@@ -103,7 +90,7 @@ protected:
}
}
int cl_area = (int)(r.get_area() + 1.0f);
for (i = 0; i < SITE_MAX; i++) {
for (i = 0; i < map_site::SITE_MAX; i++) {
if (class_sizes.has(i))
if (cl_area <= class_sizes[i]) {
map_sites.write[j].site_type = i;
@@ -113,9 +100,52 @@ protected:
/* for now the starting town is at 0 */
printf("area: %d class: %d\n", cl_area, map_sites[j].site_type);
}
map_sites.write[0].site_type = SITE_TOWN;
map_sites.write[0].site_type = map_site::SITE_TOWN;
}
void index_site(struct map_site *site);
Dictionary build_diagram(Ref<RandomNumberGenerator> rnd,
int npatches, int center_count, int center_step,
int spread, int dim);
void process_diagram(const Dictionary &diagram);
public:
RoadDiagram();
void set_class_size(int cl, int sz)
{
class_sizes[cl] = sz;
}
const List<struct cluster> &get_clusters() const;
const Vector<struct map_site> &get_map_sites() const;
const Vector<Vector2> &get_diagram_vertices() const;
const Vector<struct half_edge *> &get_map_hedges() const;
void build(Ref<RandomNumberGenerator> rnd,
int npatches, int center_count, int center_step,
int spread, int dim);
};
class RoadGrid: public Reference {
GDCLASS(RoadGrid, Object)
protected:
Ref<RandomNumberGenerator> rnd;
int keep_seed;
List<struct cluster> clusters;
protected:
Rect2 bounds;
static void _bind_methods();
public:
struct vshape {
AABB area;
int instance;
int e1, e2;
int site;
Vector3 p1, p2, p3;
float depth1, depth2;
};
protected:
Vector<Vector2> diagram_vertices;
Vector<struct map_site> map_sites;
Vector<struct half_edge *> map_hedges;
PoolVector<Vector3> vertices;
PoolVector<struct vshape> vshapes;
bool segment_intersects_rect(const Vector2 &a, const Vector2 &b, const Rect2 &rect);
inline bool segment_in_grid_rect(const Vector2 &a, const Vector2 &b, int x, int y)
{
@@ -170,15 +200,15 @@ protected:
items.write[items.size() - 1] = hedge;
hedge_grid[x][y] = items;
}
inline void insert_hedge_to_grid_cell(int x, int y, struct half_edge *hedge)
{
static int count = 0;
count++;
set(x, y, hedge);
printf("count: %d\n", count);
}
};
class hg hedge_grid;
inline void insert_hedge_to_grid_cell(int x, int y, struct half_edge *hedge)
{
static int count = 0;
count++;
hedge_grid.set(x, y, hedge);
printf("count: %d\n", count);
}
inline void add_hedge_to_grid(struct half_edge *hedge)
{
Vector2 a = diagram_vertices[hedge->a];
@@ -201,7 +231,7 @@ protected:
int py = rgrid.position.y + y;
Rect2 xr = get_grid_rect(px, py).grow(16.0f);
if (segment_intersects_rect(a, b, xr))
insert_hedge_to_grid_cell(px, py, hedge);
hedge_grid.insert_hedge_to_grid_cell(px, py, hedge);
}
}
}
@@ -214,17 +244,6 @@ protected:
return (int)(y / grid_height);
}
float grid_width, grid_height;
PoolVector<Vector3> vertices;
public:
struct vshape {
AABB area;
int instance;
int e1, e2;
int site;
Vector3 p1, p2, p3;
};
protected:
PoolVector<struct vshape> vshapes;
void sort_angle(Vector<int> &sort_data);
public:
void build(Ref<Curve> curve, Ref<FastNoiseLite> noise);
@@ -252,17 +271,17 @@ public:
{
return map_hedges.size();
}
void generate_3d_vertices();
void generate_3d_vertices(Ref<Curve> curve, Ref<FastNoiseLite> noise);
void generate_building_positions();
void generate_site_building_positions(const struct map_site *site);
int get_site_from_point(int x, int z);
inline bool site_is_town(int site)
{
return map_sites[site].site_type == SITE_TOWN;
return map_sites[site].site_type == map_site::SITE_TOWN;
}
inline bool site_is_farm(int site)
{
return map_sites[site].site_type == SITE_FARM;
return map_sites[site].site_type == map_site::SITE_FARM;
}
inline float get_site_avg_height(int site)
{
@@ -277,6 +296,8 @@ public:
{
if (polygon_cache_2d.has(site))
return polygon_cache_2d[site];
if (map_sites.size() <= site)
return PoolVector<Vector2>();
int count = 0, i;
int psize = map_sites[site].polygon_ind.size();
PoolVector<Vector2> polygon;
@@ -298,6 +319,8 @@ public:
{
if (polygon_cache_3d.has(site))
return polygon_cache_3d[site];
if (map_sites.size() <= site)
return PoolVector<Vector3>();
int count = 0, i;
int psize = map_sites[site].polygon_ind.size();
PoolVector<Vector3> polygon;
@@ -329,5 +352,6 @@ public:
}
return ret;
}
void save_json(const String &path);
};
#endif