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
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <draw.h>
#include "mahjongg.h"
void
consumeline(Biobuf *b)
{
while(Bgetc(b) != '\n')
;
}
/* parse a level file */
int
parse(char *layout)
{
int x = 0, y = 0, depth = 0;
char c;
Biobuf *b;
b = Bopen(layout, OREAD);
if(b == nil) {
fprint(2, "could not open file %s: %r\n", layout);
return 0;
}
level.remaining = 0;
while((c = Bgetc(b)) > 0) {
switch(c) {
case '\n':
x = 0;
y = (y+1) % Ly;
if(!y)
depth++;
break;
case '.':
orig.board[depth][x][y].which = 0;
x++;
break;
case '1':
orig.remaining++;
case '2':
case '3':
case '4':
orig.board[depth][x++][y].which = c-48;
break;
default:
consumeline(b);
break;
}
}
Bterm(b);
return 1;
}
int
indextype(int type)
{
int t;
if(type < 108)
t = (type/36)*Facey * 9 + ((type%36)/4)*Facex;
else if(type < 112)
t = Seasons;
else if(type < 128)
t = 3*Facey + (((type+12)%36)/4)*Facex;
else if(type < 132)
t = Flowers;
else
t = 4*Facey + (((type+28)%36)/4)*Facex;
return t;
}
Point
indexpt(int type)
{
Point p;
/*
* the first 108 bricks are 4 of each, 36 per line:
* x = (index%36)/4
* y = (index)/36
* then multiply by the size of a single tile.
* the next 4 are the seasons, so x = index%4...
* and so on...
*/
if(type < 108)
p = Pt(((type%36)/4)*Facex, (type/36)*Facey);
else if(type < 112)
p = Pt((type%4)*Facex, 3*Facey);
else if(type < 128)
p = Pt((((type+12)%36)/4)*Facex, 3*Facey);
else if(type < 132)
p = Pt(((type+4)%4)*Facex, 4*Facey);
else
p = Pt((((type+28)%36)/4)*Facex, 4*Facey);
return p;
}
/* use the seed to generate a replayable game */
void
generate(uint seed)
{
int x, y, d, n;
int order[144];
Point p;
srand(seed);
for (x = 0; x < Tiles; x++)
order[x] = x;
for(x = 0; x < Tiles; x++) {
n = order[x];
y = nrand(Tiles);
order[x] = order[y];
order[y] = n;
}
n = 0;
for(d = 0; d < Depth; d++)
for(y = 0; y < Ly; y++)
for(x = 0; x < Lx; x++)
if(orig.board[d][x][y].which == 1) {
orig.board[d][x][y].type = indextype(order[n]);
p = indexpt(order[n++]);
orig.board[d][x][y].start = p;
orig.board[d][x+1][y].start = p;
orig.board[d][x][y+1].start = p;
orig.board[d][x+1][y+1].start = p;
}
if(n != orig.remaining)
fprint(2, "level improperly generated: %d elements, "
"should have %d\n", n, orig.remaining);
orig.c = NC;
orig.l = NC;
orig.done = 0;
level = orig;
}
|