forked from raduprv/Eternal-Lands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.c
156 lines (132 loc) · 2.82 KB
/
cluster.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifdef CLUSTER_INSIDES
#include "cluster.h"
#include "actors.h"
static short* clusters = NULL;
short current_cluster = 0;
void set_clusters (const char* data)
{
const short* cdata = (const short*) data;
int nx = tile_map_size_x * 6;
int ny = tile_map_size_y * 6;
int idx;
clusters = calloc (nx * ny, sizeof (short));
for (idx = 0; idx < nx*ny; idx++)
clusters[idx] = SDL_SwapLE16 (cdata[idx]);
}
#ifdef MAP_EDITOR
void get_clusters (char** data, int *len)
{
if (!clusters)
{
*data = NULL;
*len = 0;
}
else
{
int nx = tile_map_size_x * 6;
int ny = tile_map_size_y * 6;
int idx;
short* cdata = calloc (nx*ny, sizeof (short));
for (idx = 0; idx < nx*ny; idx++)
cdata[idx] = SDL_SwapLE16 (clusters[idx]);
*len = nx * ny * sizeof (short);
*data = (char *) cdata;
}
}
#endif
void compute_clusters (const char* occupied)
{
int nr_clusters;
short cluster_idx[1024];
short nb_idx[4];
int ic, cnr;
int nx = tile_map_size_x * 6;
int ny = tile_map_size_y * 6;
int x, y, idx;
clusters = calloc (nx * ny, sizeof (short));
nr_clusters = 0;
cluster_idx[0] = 0;
idx = 0;
if (occupied[idx])
{
nr_clusters++;
clusters[idx] = cluster_idx[nr_clusters] = nr_clusters;
}
for (++idx; idx < nx; idx++)
{
if (occupied[idx])
{
if (occupied[idx-1])
{
clusters[idx] = clusters[idx-1];
}
else
{
nr_clusters++;
clusters[idx] = cluster_idx[nr_clusters] = nr_clusters;
}
}
}
for (y = 1; y < ny; y++)
{
for (x = 0; x < nx; x++, idx++)
{
if (occupied[idx])
{
int cidx, i;
nb_idx[0] = x > 0 ? cluster_idx[clusters[idx-nx-1]] : 0;
nb_idx[1] = cluster_idx[clusters[idx-nx]];
nb_idx[2] = x < nx-1 ? cluster_idx[clusters[idx-nx+1]] : 0;
nb_idx[3] = x > 0 ? cluster_idx[clusters[idx-1]] : 0;
cidx = 0;
for (i = 0; i < 4; i++)
if (nb_idx[i] && (!cidx || nb_idx[i] < cidx))
cidx = nb_idx[i];
if (!cidx)
{
nr_clusters++;
clusters[idx] = cluster_idx[nr_clusters] = nr_clusters;
}
else
{
clusters[idx] = cidx;
for (i = 0; i < 4; i++)
if (nb_idx[i] && cluster_idx[nb_idx[i]] > cidx)
cluster_idx[nb_idx[i]] = cidx;
}
}
}
}
cnr = 0;
for (ic = 1; ic <= nr_clusters; ic++)
{
if (cluster_idx[ic] == ic)
cluster_idx[ic] = ++cnr;
else
cluster_idx[ic] = cluster_idx[cluster_idx[ic]];
}
for (idx = 0; idx < nx*ny; idx++)
clusters[idx] = cluster_idx[clusters[idx]];
}
short get_cluster (int x, int y)
{
if (clusters == NULL)
return 0;
if (x < 0 || x >= tile_map_size_x*6 || y < 0 || y >= tile_map_size_y*6)
return 0;
return clusters[y*tile_map_size_x*6+x];
}
void destroy_clusters_array ()
{
if (clusters)
free (clusters);
clusters = NULL;
}
#ifndef MAP_EDITOR
short get_actor_cluster ()
{
actor *me = get_our_actor ();
return me ? me->cluster : 0;
}
#endif
#endif // CLUSTER_INSIDES