-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.c
More file actions
249 lines (217 loc) · 5.46 KB
/
object.c
File metadata and controls
249 lines (217 loc) · 5.46 KB
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
242
243
244
245
246
247
248
249
#include<stdlib.h>
#include<cairo/cairo.h>
#include "script.h"
#include "warning.h"
#include "X.h"
#include "window.h"
#include "rules.h"
#include "draw.h"
#include "moves.h"
#include "gmoves.h"
#include "gsaves.h"
#include "object.h"
#include "keyboard.h"
#include "imgsave.h"
#include "menudraw.h"
#include "main.h"
static void analyzeobject(ff_object *obj);
ff_object *first_object = NULL; // prvni objekt ve spojovem seznamu
// vyrobi novy objekt a zaradi do spojoveho seznamu
ff_object *new_object(const char *color, char type, int x, int y,
int width, int height, const char *data)
{
ff_object *result;
int i;
result = (ff_object *)mymalloc(sizeof(ff_object));
result->type = type;
result->c.x = x;
result->c.y = y;
result->width = width;
result->height = height;
result->gflip = result->flip = 0;
result->data = (char *)mymalloc(sizeof(char)*width*height);
for(i=0; i<width*height; i++) result->data[i] = data[i];
for(i=0; i<4; i++) result->color[i] = color[i];
result->next = first_object;
first_object = result;
analyzeobject(result);
return result;
}
// verze new_object pro lua
int script_new_object(lua_State *L)
{
const char *color = luaL_checklstring(L, 1, NULL);
char type = luaL_checkinteger(L, 2);
int x = luaL_checkinteger(L, 3);
int y = luaL_checkinteger(L, 4);
int width = luaL_checkinteger(L, 5);
int height = luaL_checkinteger(L, 6);
const char *data = luaL_checklstring(L, 7, NULL);
lua_pushlightuserdata(L, new_object(color, type, x, y, width, height, data));
return 1;
}
// nastavi rozmery (lua funkci)
int script_setroomsize(lua_State *L)
{
room_width = luaL_checkinteger(L, 1);
room_height = luaL_checkinteger(L, 2);
return 0;
}
/*
Objekt obsahuje polozku char *data, ktera urcuje, jeho tvar.
Jedna se o pole jednicek a nul o delce width*height. Jednicka
znamena policko objektu, nula znamena prazdno, toto pole je
zapsano po radcich.
Funkce analyzeobject si k objektu vyrobi seznam policek, co
jsou od nej vpravo / vlevo / nahore / dole. Tato policka
(jejich souradnice) ulozi do prislusnych poli dirsquares. Delky
techto poli pak do dirnum. Takto vygenerovane informace se budou
hodit pri strkani / padani objektu.
*/
static void analyzeobject(ff_object *obj)
{
int i, x, y;
char state;
for(i=0; i<4; i++) obj->dirnum[i] = 0;
for(y=0; y<obj->height; y++){
state = 0;
for(x=0; x<obj->width; x++){
if(obj->data[x+y*obj->width]){
if(!state){
obj->dirnum[LEFT]++;
obj->dirnum[RIGHT]++;
state = 1;
}
}
else state = 0;
}
}
for(x=0; x<obj->width; x++){
state = 0;
for(y=0; y<obj->height; y++){
if(obj->data[x+y*obj->width]){
if(!state){
obj->dirnum[UP]++;
obj->dirnum[DOWN]++;
state = 1;
}
}
else state = 0;
}
}
obj->dirsquares[UP] = mymalloc(sizeof(coor)*obj->dirnum[UP]);
obj->dirsquares[DOWN] = mymalloc(sizeof(coor)*obj->dirnum[DOWN]);
obj->dirsquares[LEFT] = mymalloc(sizeof(coor)*obj->dirnum[LEFT]);
obj->dirsquares[RIGHT] = mymalloc(sizeof(coor)*obj->dirnum[RIGHT]);
i = 0;
for(y=0; y<obj->height; y++){
state = 0;
for(x=0; x<obj->width; x++){
if(obj->data[x+y*obj->width]){
if(!state){
obj->dirsquares[LEFT][i] = coord(x-1, y);
state = 1;
i++;
}
}
else state = 0;
}
}
i = 0;
for(y=0; y<obj->height; y++){
state = 0;
for(x=obj->width-1; x>=0; x--){
if(obj->data[x+y*obj->width]){
if(!state){
obj->dirsquares[RIGHT][i] = coord(x+1, y);
state = 1;
i++;
}
}
else state = 0;
}
}
i = 0;
for(x=0; x<obj->width; x++){
state = 0;
for(y=0; y<obj->height; y++){
if(obj->data[x+y*obj->width]){
if(!state){
obj->dirsquares[UP][i] = coord(x, y-1);
state = 1;
i++;
}
}
else state = 0;
}
}
i = 0;
for(x=0; x<obj->width; x++){
state = 0;
for(y=obj->height-1; y>=0; y--){
if(obj->data[x+y*obj->width]){
if(!state){
obj->dirsquares[DOWN][i] = coord(x, y+1);
state = 1;
i++;
}
}
else state = 0;
}
}
}
/* spocita objekty, vytvori objlisty, tato funkce se spusti
po vyrobeni vsech objektu */
void finish_objects()
{
int i, j;
ff_object *obj;
objnum = 0;
for(obj = first_object; obj; obj = obj->next) objnum++;
for(i=0; i<NUMOBJLISTS; i++)
objlist[i] = (ff_object **)mymalloc(objnum*sizeof(ff_object *));
for(i=0, obj=first_object; obj; i++, obj=obj->next)
for(j=0; j<NUMOBJLISTS; j++){
objlist[j][i] = obj;
obj->index[j] = i;
}
}
// prohodi objekt obj1 s objektem na pozici index2 v objlist[list]
void swapobject(ff_object *obj1, int index2, int list)
{
objlist[list][obj1->index[list]] = objlist[list][index2];
objlist[list][index2]->index[list] = obj1->index[list];
obj1->index[list] = index2;
objlist[list][index2] = obj1;
}
int backdir(int dir) // vrati opacny smer
{
return (dir+2)%4;
}
coor coord(int x, int y) // vrati zadane souradnice ve strukture coor
{
coor result;
result.x = x;
result.y = y;
return result;
}
coor coorsum(coor c1, coor c2) // secte souradnice (po slozkach)
{
coor result;
result.x = c1.x+c2.x;
result.y = c1.y+c2.y;
return result;
}
void delete_objects() // uvolni z pameti vsechny objekty
{
ff_object *obj;
int i;
while(first_object){
obj = first_object;
first_object = first_object->next;
for(i=0; i<4; i++) if(obj->dirsquares[i]) free(obj->dirsquares[i]);
if(obj->data) free(obj->data);
free(obj);
}
for(i=0; i<NUMOBJLISTS; i++) free(objlist[i]);
}