forked from phillbush/9ball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
9ball.c
620 lines (567 loc) · 14 KB
/
9ball.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <cursor.h>
#include <mouse.h>
#include <keyboard.h>
#define STACKSIZE (8 * 1024)
#define TIME 30
#define TILESX 32
#define TILESY 20
#define FACT 1024
#define WIDTH (TILESX * FACT)
#define HEIGHT (TILESY * FACT)
#define MINCAPTURED 0.70
#define MAXBALLS 50
#define COLOR_BALL DGreen
#define COLOR_BOARD DYellow
#define COLOR_WALL DBlack
#define COLOR_EXT0 DBlue
#define COLOR_EXT1 DRed
#define COLOR_BARBG DWhite
#define COLOR_BARFG DBlack
enum Orientation {
HORZ,
VERT,
};
enum WallStatus {
WALL_INACTIVE = -1,
WALL_BUILDING = 0,
WALL_BUILT = 1,
};
struct Ball {
Point p; /* position of top-left corner of square around ball */
int dx, dy; /* ball speed */
};
struct Wall {
Point p; /* position of wall */
int s0, s1; /* size of wall; s0 goes negative, while s1 goes positive */
enum WallStatus e0, e1; /* status of each wall extension */
enum Orientation o; /* orientation of wall */
};
struct Click {
Point p; /* position of click */
enum Orientation o; /* orientation of cursor */
};
Channel *drawc, *clickc;
Image *ballimg, *boardimg, *wallimg, *ext0img, *ext1img, *barbgimg, *barfgimg;
Mousectl *mctl;
Keyboardctl *kctl;
Cursor horz = {
{-8, -8},
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x10, 0x08, 0x30, 0x0c, 0x7f, 0xfe,
0x7f, 0xfe, 0x30, 0x0c, 0x10, 0x08, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x18, 0x18, 0x28, 0x14, 0x4f, 0xf2, 0x80, 0x01,
0x80, 0x01, 0x4f, 0xf2, 0x28, 0x14, 0x18, 0x18,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
};
Cursor vert = {
{-8, -8},
{ 0x00, 0x00, 0x01, 0x80, 0x03, 0xc0, 0x07, 0xe0,
0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,
0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,
0x07, 0xe0, 0x03, 0xc0, 0x01, 0x80, 0x00, 0x00,
},
{ 0x01, 0x80, 0x02, 0x40, 0x04, 0x20, 0x08, 0x10,
0x0e, 0x70, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40,
0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x0e, 0x70,
0x08, 0x10, 0x04, 0x20, 0x02, 0x40, 0x01, 0x80,
},
};
static Image *
eallocimage(Rectangle r, ulong chan, int repl, ulong col)
{
Image *img;
if((img = allocimage(display, r, chan, repl, col)) == nil)
sysfatal("allocimage: %r");
return img;
}
static void
initimgs(void)
{
ballimg = eallocimage(Rect(0,0,1,1), screen->chan, 1, COLOR_BALL);
boardimg = eallocimage(Rect(0,0,1,1), screen->chan, 1, COLOR_BOARD);
wallimg = eallocimage(Rect(0,0,1,1), screen->chan, 1, COLOR_WALL);
ext0img = eallocimage(Rect(0,0,1,1), screen->chan, 1, COLOR_EXT0);
ext1img = eallocimage(Rect(0,0,1,1), screen->chan, 1, COLOR_EXT1);
barbgimg = eallocimage(Rect(0,0,1,1), screen->chan, 1, COLOR_BARBG);
barfgimg = eallocimage(Rect(0,0,1,1), screen->chan, 1, COLOR_BARFG);
}
static void
terminate(void)
{
chanclose(drawc);
chanclose(clickc);
closekeyboard(kctl);
closemouse(mctl);
closedisplay(display);
threadexitsall(nil);
}
static void
tilesinit(int tiles[TILESX][TILESY])
{
int i;
for(i = 0; i < TILESX; i++)
tiles[i][0] = tiles[i][TILESY - 1] = 1;
for(i = 0; i < TILESY; i++)
tiles[0][i] = tiles[TILESX - 1][i] = 1;
}
static void
tilesclear(int tiles[TILESX][TILESY])
{
int i, j;
for(i = 1; i < TILESX - 1; i++){
for(j = 1; j < TILESY - 1; j++){
tiles[i][j] = 0;
}
}
}
static void
boardcalc(Point *p, int *s)
{
double d;
int w, h;
w = Dx(screen->r);
h = Dy(screen->r) - font->height;
if((double)w/h < (double)WIDTH/HEIGHT) {
d = (double)w * (double)HEIGHT / (double)WIDTH;
*s = w / TILESX;
p->x = screen->r.min.x + (w - *s * TILESX) / 2;
p->y = screen->r.min.y + (h - d) / 2;
}else{
d = (double)h * (double)WIDTH / (double)HEIGHT;
*s = h / TILESY;
p->x = screen->r.min.x + (w - d) / 2;
p->y = screen->r.min.y + (h - *s * TILESY) / 2;
}
}
static int
hasball(struct Ball *balls, int nballs, int x, int y)
{
int i;
for(i = 0; i < nballs; i++)
if(balls[i].p.x == x && balls[i].p.y == y)
return 1;
return 0;
}
static void
boarddraw(int tiles[TILESX][TILESY], struct Ball *balls, struct Wall *wall, Point *orig, int fact, int nballs)
{
int i, j;
Rectangle r;
Point p;
draw(screen, screen->r, boardimg, nil, ZP);
/* draw wall extensions */
if(wall->e0 == WALL_BUILDING){
if(wall->o == HORZ){
r.max.x = orig->x + wall->p.x * fact;
r.max.y = orig->y + wall->p.y * fact + fact;
r.min.x = r.max.x + (wall->s0 * fact);
r.min.y = orig->y + wall->p.y * fact;
draw(screen, r, ext0img, nil, ZP);
}else{
r.max.y = orig->y + wall->p.y * fact;
r.max.x = orig->x + wall->p.x * fact + fact;
r.min.y = r.max.y + (wall->s0 * fact);
r.min.x = orig->x + wall->p.x * fact;
draw(screen, r, ext0img, nil, ZP);
}
}
if(wall->e1 == WALL_BUILDING){
if(wall->o == HORZ){
r.min.x = orig->x + wall->p.x * fact;
r.min.y = orig->y + wall->p.y * fact;
r.max.x = r.min.x + (wall->s1 * fact);
r.max.y = orig->y + wall->p.y * fact + fact;
draw(screen, r, ext1img, nil, ZP);
}else{
r.min.y = orig->y + wall->p.y * fact;
r.min.x = orig->x + wall->p.x * fact;
r.max.y = r.min.y + (wall->s1 * fact);
r.max.x = orig->x + wall->p.x * fact + fact;
draw(screen, r, ext1img, nil, ZP);
}
}
/* draw captured tiles */
for(i = 0; i < TILESX; i++){
for(j = 0; j < TILESY; j++){
if(tiles[i][j]){
r.min.x = orig->x + i * fact;
r.min.y = orig->y + j * fact;
r.max.x = r.min.x + fact;
r.max.y = r.min.y + fact;
draw(screen, r, wallimg, nil, ZP);
}
}
}
/* draw balls */
for(i = 0; i < nballs; i++){
p.x = orig->x + balls[i].p.x * fact + fact / 2;
p.y = orig->y + balls[i].p.y * fact + fact / 2;
fillellipse(screen, p, fact / 2, fact / 2, ballimg, ZP);
}
}
static void
statusdraw(int lvl, int nlives, double captured)
{
Rectangle r;
Point p;
int w;
char buf[128];
r = screen->r;
r.min.y = r.max.y - font->height;
draw(screen, r, barbgimg, nil, ZP);
snprint(buf, sizeof(buf), "lvl: %d; nlives: %d; captured: %.1f%%", lvl, nlives, captured * 100.0);
w = stringwidth(font, buf);
p.x = screen->r.min.x + (Dx(screen->r) - w) / 2;
p.y = screen->r.max.y - font->height;
string(screen, p, barfgimg, ZP, font, buf);
}
static int
newlvl(struct Ball *balls, int lvl)
{
int nballs;
int i;
nballs = lvl + 1;
if (nballs > MAXBALLS)
nballs = MAXBALLS;
for(i = 0; i < nballs; i++){
balls[i].p.x = 4 + ntruerand(TILESX - 8);
balls[i].p.y = 4 + ntruerand(TILESY - 8);
balls[i].dx = ntruerand(2) % 2 ? 1 : -1;
balls[i].dy = ntruerand(2) % 2 ? 1 : -1;
}
return nballs;
}
static Point
gettile(Point click, Point orig, int fact)
{
Point p;
p = Pt(0,0);
if(click.x >= orig.x && click.x < orig.x + TILESX * fact &&
click.y >= orig.y && click.y < orig.y + TILESY * fact){
p.x = (click.x - orig.x) / fact;
p.y = (click.y - orig.y) / fact;
}
return p;
}
static double
getcaptured(int tiles[TILESX][TILESY])
{
int i, j, n;
n = 0;
for(i = 1; i < TILESX - 1; i++)
for(j = 1; j < TILESY - 1; j++)
if(tiles[i][j])
n++;
return (double)n/(TILESX * TILESY);
}
/* build walls, bounce balls, do collision detection; return life difference */
static int
gameupdate(int tiles[TILESX][TILESY], struct Ball *balls, struct Wall *wall, int nballs)
{
int i, j, x, y;
int life;
int cons0, cons1;
int bx, by;
life = 0;
/* build wall */
if(wall->e0 == WALL_BUILDING){
if(wall->o == VERT){
i = wall->p.y + wall->s0;
if(tiles[wall->p.x][i]){
for(j = i; j <= wall->p.y; j++)
tiles[wall->p.x][j] = 1;
wall->e0 = WALL_BUILT;
}
}else{
i = wall->p.x + wall->s0;
if(tiles[i][wall->p.y]){
for(j = i; j <= wall->p.x; j++)
tiles[j][wall->p.y] = 1;
wall->e0 = WALL_BUILT;
}
}
if (wall->e0 == WALL_BUILDING){
wall->s0--;
}
}
if(wall->e1 == WALL_BUILDING){
if(wall->o == VERT){
i = wall->p.y + wall->s1;
if(tiles[wall->p.x][i]){
for(j = wall->p.y; j <= i; j++)
tiles[wall->p.x][j] = 1;
wall->e1 = WALL_BUILT;
}
}else{
i = wall->p.x + wall->s1;
if(tiles[i][wall->p.y]){
for(j = wall->p.x; j <= i; j++)
tiles[j][wall->p.y] = 1;
wall->e1 = WALL_BUILT;
}
}
if (wall->e1 == WALL_BUILDING){
wall->s1++;
}
}
/* check if ball crosses a building wall */
if (wall->e0 == WALL_BUILDING){
if(wall->o == VERT){
for(i = wall->p.y + wall->s0; i <= wall->p.y; i++){
if(hasball(balls, nballs, wall->p.x, i)){
life--;
wall->e0 = WALL_INACTIVE;
}
}
}else{
for(i = wall->p.x + wall->s0; i <= wall->p.x; i++){
if(hasball(balls, nballs, i, wall->p.y)){
life--;
wall->e0 = WALL_INACTIVE;
}
}
}
}
if (wall->e1 == WALL_BUILDING){
if(wall->o == VERT){
for(i = wall->p.y; i <= wall->p.y + wall->s1; i++){
if(hasball(balls, nballs, wall->p.x, i)){
life--;
wall->e1 = WALL_INACTIVE;
}
}
}else{
for(i = wall->p.x; i <= wall->p.x + wall->s1; i++){
if(hasball(balls, nballs, i, wall->p.y)){
life--;
wall->e1 = WALL_INACTIVE;
}
}
}
}
/* capture areas */
cons0 = cons1 = 1;
if((wall->e0 == WALL_BUILT && wall->e1 == WALL_INACTIVE) ||
(wall->e1 == WALL_BUILT && wall->e0 == WALL_INACTIVE)){
wall->e0 = wall->e1 = WALL_INACTIVE;
}else if(wall->e0 == WALL_BUILT && wall->e1 == WALL_BUILT){
if(wall->o == VERT){
for(i = wall->s0; (cons0 || cons1) && i <= wall->s1; i++){
for(j = wall->p.x - 1; cons0 && !tiles[j][wall->p.y + i]; j--){
if(hasball(balls, nballs, j, wall->p.y + i)){
cons0 = 0;
break;
}
}
for(j = wall->p.x + 1; cons1 && !tiles[j][wall->p.y + i]; j++){
if(hasball(balls, nballs, j, wall->p.y + i)){
cons1 = 0;
break;
}
}
}
for(i = wall->s0; (cons0 || cons1) && i <= wall->s1; i++){
for(j = wall->p.x - 1; cons0 && !tiles[j][wall->p.y + i]; j--){
tiles[j][wall->p.y + i] = 1;
}
for(j = wall->p.x + 1; cons1 && !tiles[j][wall->p.y + i]; j++){
tiles[j][wall->p.y + i] = 1;
}
}
}else{
for(i = wall->s0; (cons0 || cons1) && i <= wall->s1; i++){
for(j = wall->p.y - 1; cons0 && !tiles[wall->p.x + i][j]; j--){
if(hasball(balls, nballs, wall->p.x + i, j)){
cons0 = 0;
break;
}
}
for(j = wall->p.y + 1; cons1 && !tiles[wall->p.x + i][j]; j++){
if(hasball(balls, nballs, wall->p.x + i, j)){
cons1 = 0;
break;
}
}
}
for(i = wall->s0; (cons0 || cons1) && i <= wall->s1; i++){
for(j = wall->p.y - 1; cons0 && !tiles[wall->p.x + i][j]; j--){
tiles[wall->p.x + i][j] = 1;
}
for(j = wall->p.y + 1; cons1 && !tiles[wall->p.x + i][j]; j++){
tiles[wall->p.x + i][j] = 1;
}
}
}
wall->e0 = wall->e1 = WALL_INACTIVE;
}
for(i = 0; i < nballs; i++){
/* collision detection */
x = balls[i].p.x + balls[i].dx;
y = balls[i].p.y + balls[i].dy;
if(tiles[x][y]){
bx = by = 1;
if((balls[i].dy > 0 && !tiles[x][y - 1]) ||
(balls[i].dy < 0 && !tiles[x][y + 1]))
bx = 0;
if((balls[i].dx > 0 && !tiles[x - 1][y]) ||
(balls[i].dx < 0 && !tiles[x + 1][y]))
by = 0;
if(bx)
balls[i].dx *= -1;
if(by)
balls[i].dy *= -1;
}
/* update ball position */
balls[i].p.x += balls[i].dx;
balls[i].p.y += balls[i].dy;
}
return life;
}
static void
gamethread(void *)
{
struct Ball balls[MAXBALLS];
struct Click click;
struct Wall wall;
Point orig;
double captured;
int fact;
int tiles[TILESX][TILESY];
int lvl, nballs, nlives;
int haswon;
int resized;
Alt alts[] = {
{drawc, &resized, CHANRCV},
{clickc, &click, CHANRCV},
{nil, nil, CHANEND},
};
lvl = 1;
nlives = nballs = newlvl(balls, lvl);
haswon = 0;
captured = 0.0;
for(;;){
wall.e0 = wall.e1 = WALL_INACTIVE;
tilesinit(tiles);
tilesclear(tiles);
boardcalc(&orig, &fact);
boarddraw(tiles, balls, &wall, &orig, fact, nballs);
statusdraw(lvl, nlives, 0.0);
flushimage(display, 1);
while(nlives > 0 && !haswon){
switch(alt(alts)){
case 0:
if(resized){
boardcalc(&orig, &fact);
}else{
nlives += gameupdate(tiles, balls, &wall, nballs);
captured = getcaptured(tiles);
}
boarddraw(tiles, balls, &wall, &orig, fact, nballs);
statusdraw(lvl, nlives, captured);
flushimage(display, 1);
haswon = captured >= MINCAPTURED;
break;
case 1:
if(wall.e0 == WALL_INACTIVE && wall.e1 == WALL_INACTIVE){
wall.p = gettile(click.p, orig, fact);
if(wall.p.x == 0 || wall.p.y == 0)
break;
wall.e0 = wall.e1 = WALL_BUILDING;
wall.s0 = wall.s1 = 0;
wall.o = click.o;
}
break;
default:
sysfatal("can't happen");
}
}
if(haswon)
lvl++;
else
lvl = 1;
nlives = nballs = newlvl(balls, lvl);
haswon = 0;
}
}
static void
clockproc(void *)
{
for(;;){
sleep(TIME);
sendul(drawc, 0);
}
}
static void
mousethread(void *)
{
struct Click click;
Mouse m;
int o;
o = VERT;
for(;;){
recv(mctl->c, &m);
if(m.buttons == (1 << 2)){
if(o == HORZ){
setcursor(mctl, &vert);
o = VERT;
}else{
setcursor(mctl, &horz);
o = HORZ;
}
}else if(m.buttons == (1 << 0)){
click.p = m.xy;
click.o = o;
send(clickc, &click);
}
}
}
static void
resizethread(void *)
{
for(;;){
recvul(mctl->resizec);
if(getwindow(display, Refnone) < 0)
sysfatal("getwindow: %r");
sendul(drawc, 1);
}
}
static void
keyboardthread(void *)
{
Rune r;
for(;;){
recv(kctl->c, &r);
if(r == Kdel || r == 'q'){
terminate();
}
}
}
void
threadmain(int, char *[])
{
if(initdraw(nil, nil, argv0) < 0)
sysfatal("initdraw: %r");
if((mctl = initmouse(nil, nil)) == nil)
sysfatal("initmouse: %r");
if((kctl = initkeyboard(nil)) == nil)
sysfatal("initkeyboard: %r");
srand(time(0));
initimgs();
setcursor(mctl, &vert);
drawc = chancreate(sizeof(ulong), 0);
clickc = chancreate(sizeof(struct Click), 0);
proccreate(clockproc, nil, STACKSIZE);
threadcreate(gamethread, nil, STACKSIZE);
threadcreate(resizethread, nil, STACKSIZE);
threadcreate(mousethread, nil, STACKSIZE);
threadcreate(keyboardthread, nil, STACKSIZE);
threadexits(nil);
}