-
Notifications
You must be signed in to change notification settings - Fork 482
/
overlay-structure.c
242 lines (213 loc) · 8.24 KB
/
overlay-structure.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* This file is part of the Minecraft Overviewer.
*
* Minecraft Overviewer is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Minecraft Overviewer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with the Overviewer. If not, see <http://www.gnu.org/licenses/>.
*/
#include "overlay.h"
typedef enum { false, true } bool;
typedef struct {
/* inherits from overlay */
RenderPrimitiveOverlay parent;
void *structures;
int numcolors;
} RenderPrimitiveStructure;
struct Condition{
int relx, rely, relz;
unsigned char block;
};
struct Color {
int numconds;
struct Condition *conditions;
unsigned char r, g, b, a;
};
static void get_color(void *data,
RenderState *state,
unsigned char *r,
unsigned char *g,
unsigned char *b,
unsigned char *a) {
/**
* Calculate the color at the current position and store the values to r,g,b,a.
**/
RenderPrimitiveStructure *self = (RenderPrimitiveStructure *)data;
int x = state->x, z = state->z, y_max, y, col, cond;
struct Color *structures = (struct Color *)(self->structures);
struct Condition * c = NULL;
bool all = true;
y_max = state->y + 1;
/**
* Check for every color in every y level if all its Conditions are met.
* If all conditions are met for one y level set r,b,g,a accordingly.
**/
// iterate over all the colors
for ( col = 0; col < self->numcolors; col++) {
// iterate over all y levels
for (y = state->chunky * -16; y <= y_max; y++) {
// iterate over all the conditions
for (cond = 0; cond < structures[col].numconds; cond++) {
all = true;
c = (struct Condition *)&structures[col].conditions[cond];
// check if the condition does apply and break from the conditions loop if not.
if(!(c->block == get_data(state, BLOCKS, x+c->relx, y+c->rely, z+c->relz))) {
all = false;
break;
}
}
if (all){
// set the color
*r = structures[col].r;
*g = structures[col].g;
*b = structures[col].b;
*a = structures[col].a;
return;
}
}
}
return;
}
static int overlay_structure_start(void *data, RenderState *state, PyObject *support) {
/**
* Initializing the search for structures by parsing the arguments and storing them into
* appropriate structures. If no arguments are passed create and use default values.
**/
PyObject *opt;
RenderPrimitiveStructure* self;
/* first, chain up */
int ret = primitive_overlay.start(data, state, support);
if (ret != 0)
return ret;
/* now do custom initializations */
self = (RenderPrimitiveStructure *)data;
// opt is a borrowed reference. do not deref
// store the structures python object into opt.
if (!render_mode_parse_option(support, "structures", "O", &(opt)))
return 1;
/**
* Check if a sane option was passed.
**/
if (opt && opt != Py_None) {
struct Color *structures = NULL;
struct Condition *cond = NULL;
Py_ssize_t structures_size = 0, i, cond_size = 0, n = 0;
bool cont = true;
opt = PySequence_Fast(opt, "expected a sequence");
if (!opt) {
PyErr_SetString(PyExc_TypeError, "'structures' must be a a sequence");
return 1;
}
structures_size = PySequence_Fast_GET_SIZE(opt);
// Getting space on the heap and do not forget to set self->numcolors.
structures = self->structures = calloc(structures_size, sizeof(struct Color));
self->numcolors = structures_size;
if (structures == NULL) {
PyErr_SetString(PyExc_MemoryError, "failed to allocate memory");
return 1;
}
/**
* Try to parse the definitions of conditions and colors.
**/
if (cont) {
for (i = 0; i < structures_size; i++) {
PyObject *structure = PyList_GET_ITEM(opt, i);
// condspy holding the conditions tuple of variable length (python object)
PyObject *condspy;
// colorpy holding the 4 tuple with r g b a values of the color
PyObject *colorpy;
// getting the condspy and colorpy out of the structures.
if (!PyArg_ParseTuple(structure, "OO", &condspy, &colorpy)) {
// Exception set automatically
free(structures);
self->structures = NULL;
return 1;
}
// Parse colorpy into a c-struct.
if (!PyArg_ParseTuple( colorpy, "bbbb",
&structures[i].r,
&structures[i].g,
&structures[i].b,
&structures[i].a)) {
free(structures);
self->structures = NULL;
return 1;
}
// Convert condspy to a fast sequence
condspy = PySequence_Fast(condspy, "Failed to parse conditions");
if(condspy == NULL) {
free(structures);
self->structures = NULL;
return 1;
}
// get the number of conditions.
structures[i].numconds = PySequence_Fast_GET_SIZE(condspy);
// reserve enough memory for the conditions.
cond = calloc(structures[i].numconds, sizeof(struct Condition));
structures[i].conditions = cond;
if (structures[i].conditions == NULL) {
PyErr_SetString(PyExc_MemoryError, "failed to allocate memory");
free(structures);
self->structures = NULL;
return 1;
}
// iterate over all the conditions and read them.
for (n = 0; n < structures[i].numconds; n++) {
PyObject *ccond = PySequence_Fast_GET_ITEM(condspy, n);
if(!PyArg_ParseTuple( ccond, "iiib",
&cond[n].relx,
&cond[n].rely,
&cond[n].relz,
&cond[n].block)){
int x = 0;
for(x = 0; x < structures_size; x++){
free(structures[x].conditions);
}
free(structures);
self->structures = NULL;
return 1;
}
}
}
}
}
/* setup custom color */
self->parent.get_color = get_color;
return 0;
}
static void overlay_structure_finish(void *data, RenderState *state) {
/* first free all *our* stuff */
RenderPrimitiveStructure* self = (RenderPrimitiveStructure *)data;
int i = 0;
if(self->structures) {
// freeing the nested structure
struct Color * m = self->structures;
for(i = 0; i < self->numcolors; i++){
if(m[i].conditions)
free(m[i].conditions);
}
}
if (self->structures) {
free(self->structures);
self->structures = NULL;
}
/* now, chain up */
primitive_overlay.finish(data, state);
}
RenderPrimitiveInterface primitive_overlay_structure = {
"overlay-structure",
sizeof(RenderPrimitiveStructure),
overlay_structure_start,
overlay_structure_finish,
NULL,
NULL,
overlay_draw,
};