-
Notifications
You must be signed in to change notification settings - Fork 0
/
omninew.c
240 lines (182 loc) · 6.92 KB
/
omninew.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "omnitype.h"
#include "omnifunc.h"
static struct Omnimino *GG;
#include "omnimino.def"
void InitGame(struct Omnimino *G) {
memset(G, 0, sizeof(struct Omnimino)); /* GameBufSize = 0; */
}
int CheckParameters(struct Omnimino *G){
GG = G;
if (Aperture > MAX_FIGURE_SIZE){
snprintf(MsgBuf, OM_STRLEN, "[1] Aperture (%d) > MAX_FIGURE_SIZE (%d)", Aperture, MAX_FIGURE_SIZE);
} else if (Metric > 1){
snprintf(MsgBuf, OM_STRLEN, "[2] Metric (%d) can be 0 or 1", Metric);
} else if (WeightMax > MAX_FIGURE_SIZE){
snprintf(MsgBuf, OM_STRLEN, "[3] WeightMax (%d) > MAX_FIGURE_SIZE (%d)", WeightMax, MAX_FIGURE_SIZE);
} else if (WeightMin < 1){
snprintf(MsgBuf, OM_STRLEN, "[4] WeightMin (%d) < 1", WeightMin);
} else if (WeightMin > WeightMax){
snprintf(MsgBuf, OM_STRLEN, "[4] WeightMin (%d) > [3] WeightMax (%d)", WeightMin, WeightMax);
} else if (Gravity > 1){
snprintf(MsgBuf, OM_STRLEN, "[5] Gravity (%d) can be 0 or 1", Gravity);
} else if (SingleLayer > 1){
snprintf(MsgBuf, OM_STRLEN, "[6] SingleLayer (%d) can be 0 or 1", SingleLayer);
} else if (DiscardFullRows > 1){
snprintf(MsgBuf, OM_STRLEN, "[7] DiscardFullRows (%d) can be 0 or 1", DiscardFullRows);
} else if (Goal >= MAX_GOAL){
snprintf(MsgBuf, OM_STRLEN, "[8] Goal (%d) > 2", Goal);
} else {
FigureSize = (Aperture == 0) ? WeightMax : Aperture;
if (GlassWidth < FigureSize){
snprintf(MsgBuf, OM_STRLEN, "[9] GlassWidth (%d) < FigureSize (%d)", GlassWidth, FigureSize);
} else if (GlassWidth > MAX_GLASS_WIDTH){
snprintf(MsgBuf, OM_STRLEN, "[9] GlassWidth (%d) > MAX_GLASS_WIDTH (%d)", GlassWidth, MAX_GLASS_WIDTH);
} else if (GlassHeightBuf < FigureSize){
snprintf(MsgBuf, OM_STRLEN, "[10] GlassHeight (%d) < FigureSize (%d)", GlassHeightBuf, FigureSize);
} else if (GlassHeightBuf > MAX_GLASS_HEIGHT){
snprintf(MsgBuf, OM_STRLEN, "[10] GlassHeight (%d) > MAX_GLASS_HEIGHT (%d)", GlassHeightBuf, MAX_GLASS_HEIGHT);
} else if (FillLevel > GlassHeightBuf){
snprintf(MsgBuf, OM_STRLEN, "[11] FillLevel (%d) > [10] GlassHeight (%d)", FillLevel, GlassHeightBuf);
} else if (FillRatio >= GlassWidth){
snprintf(MsgBuf, OM_STRLEN, "[12] FillRatio (%d) >= [9] GlassWidth (%d)", FillRatio, GlassWidth);
} else if (FixedSequence > 1){
snprintf(MsgBuf, OM_STRLEN, "[13] FixedSequence (%d) can be 0 or 1", FixedSequence);
} else {
unsigned int i, Area;
FullRow = ((1 << (GlassWidth - 1)) << 1) - 1;
TotalArea = GlassWidth * GlassHeightBuf;
for (Area = 0, i = 0; i < Aperture; i++)
Area += Metric ? Aperture : (i | 1);
if (Aperture && (Area < WeightMin)) {
snprintf(MsgBuf, OM_STRLEN, "[4] WeightMin (%d) > Aperture Area (%d)", WeightMin, Area);
} else {
return 0;
}
}
}
return 1;
}
static int ReallyAllocateBuffers(void) {
/* Figure, Block, GlassRow = StoreBuf */
unsigned int MaxFigure = TotalArea / WeightMin + 4;
unsigned int MaxBlock = TotalArea + 2 * MAX_FIGURE_SIZE;
size_t FigureBufSize = MaxFigure * sizeof(struct Coord *);
size_t BlockBufSize = MaxBlock * sizeof(struct Coord);
/*
Sizes of the parameters and data text representations
Parameters: 8*(1+1) + 4*(10+1) + (1+1) = 62
ParentName: OM_STRLEN+1 = 81
FigureNum: 10+1 = 11
CurFigure: 10+1 = 11
FillBuf: FillLevel * (10+1) = FillLevel * 11
BlockN: (FigureNum+2) * (10+1) = MaxFigure * 11
Block: MaxBlock * (2+1,4+1) = MaxBlock * 8
PlayerName: OM_STRLEN+1 = 81
TimeStamp: 10+1 = 11
*/
StoreBufSize = 62 + 81 + 11 + 11 + FillLevel * 11 +
MaxFigure * 11 + MaxBlock * 11 + 81 + 11;
size_t NewGameBufSize = FigureBufSize + BlockBufSize + StoreBufSize;
if (GameBufSize == 0) {
Figure = malloc(NewGameBufSize);
if (Figure == NULL) {
snprintf(MsgBuf, OM_STRLEN, "Failed to allocate %ld byte buffer.", (long)NewGameBufSize);
return 1;
}
GameBufSize = NewGameBufSize;
} else {
if (NewGameBufSize > GameBufSize) {
void *NewBuf = realloc(Figure, NewGameBufSize);
if (NewBuf == NULL) {
snprintf(MsgBuf, OM_STRLEN, "Failed to reallocate %ld byte buffer.", (long)NewGameBufSize);
return 1;
}
Figure = NewBuf;
GameBufSize = NewGameBufSize;
}
}
Block = (struct Coord *) (Figure + MaxFigure);
GlassRow = (unsigned int *) (Block + MaxBlock);
return 0;
}
int AllocateBuffers(struct Omnimino *G) {
GG = G;
return ReallyAllocateBuffers();
}
/**************************************
New game functions
**************************************/
static void FillGlass(void){
unsigned int i, Places, Blocks;
if (FillRatio != 0) {
for (i = 0 ; i < FillLevel ; i++) {
FillBuf[i] = 0;
for (Places = GlassWidth, Blocks = FillRatio ; Places > 0 ; Places--) {
FillBuf[i] <<= 1;
if ((rand() % Places) < Blocks) {
FillBuf[i] |= 1; Blocks--;
}
}
}
TotalArea -= FillRatio * FillLevel;
}
}
static int SelectSlots(struct Coord *Slot, struct Coord *FBlock, int Weight) {
int SlotNum = 0, SeedCnt, x, y;
int UseNeighbours = ((Aperture == 0) && Weight);
int ApertureSize = ((Aperture == 0) ? (Weight ? 3 : 1) : Aperture);
int HalfSize = ApertureSize / 2;
int SeedMax = (UseNeighbours ? Weight : 1);
int MaxX = ApertureSize - HalfSize;
for(SeedCnt = 0; SeedCnt < SeedMax; SeedCnt++){
for(x = -HalfSize; x < MaxX; x++){
int Delta = (Metric ? 0 : abs(x));
int MaxY = MaxX - Delta;
for(y = -HalfSize + Delta; y < MaxY; y++){
Slot[SlotNum].x = x + (UseNeighbours ? FBlock[SeedCnt].x : 0);
Slot[SlotNum].y = y + (UseNeighbours ? FBlock[SeedCnt].y : 0);
if (((Weight >= (int)WeightMin) || (!FindBlock(Slot + SlotNum, FBlock, Weight))) &&
(!FindBlock(Slot + SlotNum, Slot, SlotNum)))
SlotNum++;
}
}
}
return SlotNum;
}
#define MAX_SLOTS (MAX_FIGURE_SIZE*MAX_FIGURE_SIZE)
#define MAX_SLOTS_COMPACT ((MAX_FIGURE_SIZE-1)*9)
#if MAX_SLOTS_COMPACT>MAX_SLOTS
#define MAX_SLOTS MAX_SLOTS_COMPACT
#endif
static int NewFigure(struct Coord *F) {
unsigned int i;
struct Coord Slot[MAX_SLOTS], *B;
for (i = 0, B = F; i < WeightMax; i++){
memcpy(B, Slot + (rand() % SelectSlots(Slot, F, B-F)),sizeof(struct Coord));
if (!FindBlock(B, F, B-F))
B++;
}
return B-F;
}
/**************************************
NewGame
**************************************/
int NewGame(struct Omnimino *G){
GG = G;
if (ReallyAllocateBuffers() != 0)
return 1;
srand((unsigned int)time(NULL));
FillGlass();
for (LastFigure = Figure, Figure[0] = Block; ((*LastFigure) - Figure[0]) < (int)TotalArea; LastFigure++){
LastFigure[1] = (*LastFigure) + NewFigure(*LastFigure);
ForEachIn(LastFigure, ScaleUp, 0);
}
NextFigure = Figure;
LastTouched = Figure - 1;
strcpy(ParentName, "none");
return 0;
}