Subversion Repositories HelenOS-historic

Rev

Rev 1524 | Rev 1538 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1524 Rev 1528
1
/*  $OpenBSD: tetris.c,v 1.21 2006/04/20 03:24:12 ray Exp $ */
1
/*  $OpenBSD: tetris.c,v 1.21 2006/04/20 03:24:12 ray Exp $ */
2
/*  $NetBSD: tetris.c,v 1.2 1995/04/22 07:42:47 cgd Exp $   */
2
/*  $NetBSD: tetris.c,v 1.2 1995/04/22 07:42:47 cgd Exp $   */
3
 
3
 
4
/*-
4
/*-
5
 * Copyright (c) 1992, 1993
5
 * Copyright (c) 1992, 1993
6
 *  The Regents of the University of California.  All rights reserved.
6
 *  The Regents of the University of California.  All rights reserved.
7
 *
7
 *
8
 * This code is derived from software contributed to Berkeley by
8
 * This code is derived from software contributed to Berkeley by
9
 * Chris Torek and Darren F. Provine.
9
 * Chris Torek and Darren F. Provine.
10
 *
10
 *
11
 * Redistribution and use in source and binary forms, with or without
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
12
 * modification, are permitted provided that the following conditions
13
 * are met:
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
18
 *    documentation and/or other materials provided with the distribution.
19
 * 3. Neither the name of the University nor the names of its contributors
19
 * 3. Neither the name of the University nor the names of its contributors
20
 *    may be used to endorse or promote products derived from this software
20
 *    may be used to endorse or promote products derived from this software
21
 *    without specific prior written permission.
21
 *    without specific prior written permission.
22
 *
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33
 * SUCH DAMAGE.
33
 * SUCH DAMAGE.
34
 *
34
 *
35
 *  @(#)tetris.c    8.1 (Berkeley) 5/31/93
35
 *  @(#)tetris.c    8.1 (Berkeley) 5/31/93
36
 */
36
 */
37
 
37
 
38
#ifndef lint
38
#ifndef lint
39
static const char copyright[] =
39
static const char copyright[] =
40
"@(#) Copyright (c) 1992, 1993\n\
40
"@(#) Copyright (c) 1992, 1993\n\
41
    The Regents of the University of California.  All rights reserved.\n";
41
    The Regents of the University of California.  All rights reserved.\n";
42
#endif /* not lint */
42
#endif /* not lint */
43
 
43
 
44
/*
44
/*
45
 * Tetris (or however it is spelled).
45
 * Tetris (or however it is spelled).
46
 */
46
 */
47
 
47
 
48
//#include <sys/param.h>
-
 
49
#include <sys/time.h>
48
#include <sys/time.h>
50
#include <sys/types.h>
49
#include <sys/types.h>
51
 
50
 
52
#include <err.h>
51
#include <err.h>
53
//#include <signal.h>
-
 
54
#include <stdio.h>
52
#include <stdio.h>
55
#include <stdlib.h>
53
#include <stdlib.h>
56
#include <string.h>
54
#include <string.h>
57
#include <unistd.h>
55
#include <unistd.h>
58
 
56
 
59
#include "input.h"
57
#include "input.h"
60
#include "scores.h"
58
#include "scores.h"
61
#include "screen.h"
59
#include "screen.h"
62
#include "tetris.h"
60
#include "tetris.h"
63
 
61
 
64
cell    board[B_SIZE];
62
cell    board[B_SIZE];
65
int Rows, Cols;
63
int Rows, Cols;
66
const struct shape *curshape;
64
const struct shape *curshape;
67
const struct shape *nextshape;
65
const struct shape *nextshape;
68
long    fallrate;
66
long    fallrate;
69
int score;
67
int score;
70
//gid_t gid, egid;
68
//gid_t gid, egid;
71
char    key_msg[100];
69
char    key_msg[100];
72
int showpreview, classic;
70
int showpreview, classic;
73
 
71
 
74
static void elide(void);
72
static void elide(void);
75
static void setup_board(void);
73
static void setup_board(void);
76
const struct shape *randshape(void);
74
const struct shape *randshape(void);
77
void    onintr(int);
75
void    onintr(int);
78
void    usage(void);
76
void    usage(void);
79
 
77
 
80
/*
78
/*
81
 * Set up the initial board.  The bottom display row is completely set,
79
 * Set up the initial board.  The bottom display row is completely set,
82
 * along with another (hidden) row underneath that.  Also, the left and
80
 * along with another (hidden) row underneath that.  Also, the left and
83
 * right edges are set.
81
 * right edges are set.
84
 */
82
 */
85
static void
83
static void
86
setup_board(void)
84
setup_board(void)
87
{
85
{
88
    int i;
86
    int i;
89
    cell *p;
87
    cell *p;
90
 
88
 
91
    p = board;
89
    p = board;
92
    for (i = B_SIZE; i; i--)
90
    for (i = B_SIZE; i; i--)
93
        *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;
91
        *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;
94
}
92
}
95
 
93
 
96
/*
94
/*
97
 * Elide any full active rows.
95
 * Elide any full active rows.
98
 */
96
 */
99
static void
97
static void
100
elide(void)
98
elide(void)
101
{
99
{
102
    int rows = 0;
100
    int rows = 0;
103
    int i, j, base;
101
    int i, j, base;
104
    cell *p;
102
    cell *p;
105
 
103
 
106
    for (i = A_FIRST; i < A_LAST; i++) {
104
    for (i = A_FIRST; i < A_LAST; i++) {
107
        base = i * B_COLS + 1;
105
        base = i * B_COLS + 1;
108
        p = &board[base];
106
        p = &board[base];
109
        for (j = B_COLS - 2; *p++ != 0;) {
107
        for (j = B_COLS - 2; *p++ != 0;) {
110
            if (--j <= 0) {
108
            if (--j <= 0) {
111
                /* this row is to be elided */
109
                /* this row is to be elided */
112
                rows++;
110
                rows++;
113
                memset(&board[base], 0, B_COLS - 2);
111
                memset(&board[base], 0, B_COLS - 2);
114
                scr_update();
112
                scr_update();
115
                tsleep();
113
                tsleep();
116
                while (--base != 0)
114
                while (--base != 0)
117
                    board[base + B_COLS] = board[base];
115
                    board[base + B_COLS] = board[base];
118
                scr_update();
116
                scr_update();
119
                tsleep();
117
                tsleep();
120
                break;
118
                break;
121
            }
119
            }
122
        }
120
        }
123
    }
121
    }
124
    switch (rows) {
122
    switch (rows) {
125
    case 1:
123
    case 1:
126
        score += 10;
124
        score += 10;
127
        break;
125
        break;
128
    case 2:
126
    case 2:
129
        score += 30;
127
        score += 30;
130
        break;
128
        break;
131
    case 3:
129
    case 3:
132
        score += 70;
130
        score += 70;
133
        break;
131
        break;
134
    case 4:
132
    case 4:
135
        score += 150;
133
        score += 150;
136
        break;
134
        break;
137
    default:
135
    default:
138
        break;
136
        break;
139
    }
137
    }
140
}
138
}
141
 
139
 
142
const struct shape *
140
const struct shape *
143
randshape(void)
141
randshape(void)
144
{
142
{
145
    const struct shape *tmp;
143
    const struct shape *tmp;
146
    int i, j;
144
    int i, j;
147
 
145
 
148
    tmp = &shapes[random() % 7];
146
    tmp = &shapes[random() % 7];
149
    j = random() % 4;
147
    j = random() % 4;
150
    for (i = 0; i < j; i++)
148
    for (i = 0; i < j; i++)
151
        tmp = &shapes[classic? tmp->rotc : tmp->rot];
149
        tmp = &shapes[classic? tmp->rotc : tmp->rot];
152
    return (tmp);
150
    return (tmp);
153
}
151
}
154
 
152
 
155
static void srandomdev(void)
153
static void srandomdev(void)
156
{
154
{
157
    struct timeval tv;
155
    struct timeval tv;
158
 
156
 
159
    gettimeofday(&tv, NULL);
157
    gettimeofday(&tv, NULL);
160
    srandom(tv.tv_sec + tv.tv_usec / 100000);
158
    srandom(tv.tv_sec + tv.tv_usec / 100000);
161
}
159
}
162
 
160
 
163
int
161
int
164
main(int argc, char *argv[])
162
main(int argc, char *argv[])
165
{
163
{
166
    int pos, c;
164
    int pos, c;
167
    char *keys;
165
    char *keys;
168
    int level = 2;
166
    int level = 2;
169
    char key_write[6][10];
167
    char key_write[6][10];
170
    const char *errstr;
168
    const char *errstr;
171
    int ch, i, j;
169
    int ch, i, j;
172
 
170
 
173
    keys = "jkl pq";
171
    keys = "jkl pq";
174
 
172
 
175
//  gid = getgid();
173
//  gid = getgid();
176
//  egid = getegid();
174
//  egid = getegid();
177
//  setegid(gid);
175
//  setegid(gid);
178
 
176
 
179
    classic = showpreview = 0;
177
    classic = showpreview = 0;
180
 
178
 
181
/*  while ((ch = getopt(argc, argv, "ck:l:ps")) != -1) */
179
/*  while ((ch = getopt(argc, argv, "ck:l:ps")) != -1) */
182
/*      switch(ch) { */
180
/*      switch(ch) { */
183
/*      case 'c': */
181
/*      case 'c': */
184
/*          /\* */
182
/*          /\* */
185
/*           * this means: */
183
/*           * this means: */
186
/*           *  - rotate the other way; */
184
/*           *  - rotate the other way; */
187
/*           *  - no reverse video. */
185
/*           *  - no reverse video. */
188
/*           *\/ */
186
/*           *\/ */
189
/*          classic = 1; */
187
/*          classic = 1; */
190
/*          break; */
188
/*          break; */
191
/*      case 'k': */
189
/*      case 'k': */
192
/*          if (strlen(keys = optarg) != 6) */
190
/*          if (strlen(keys = optarg) != 6) */
193
/*              usage(); */
191
/*              usage(); */
194
/*          break; */
192
/*          break; */
195
/*      case 'l': */
193
/*      case 'l': */
196
/*          level = (int)strtonum(optarg, MINLEVEL, MAXLEVEL, */
194
/*          level = (int)strtonum(optarg, MINLEVEL, MAXLEVEL, */
197
/*              &errstr); */
195
/*              &errstr); */
198
/*          if (errstr) */
196
/*          if (errstr) */
199
/*              errx(1, "level must be from %d to %d", */
197
/*              errx(1, "level must be from %d to %d", */
200
/*                  MINLEVEL, MAXLEVEL); */
198
/*                  MINLEVEL, MAXLEVEL); */
201
/*          break; */
199
/*          break; */
202
/*      case 'p': */
200
/*      case 'p': */
203
/*          showpreview = 1; */
201
/*          showpreview = 1; */
204
/*          break; */
202
/*          break; */
205
/*      case 's': */
203
/*      case 's': */
206
/*          showscores(0); */
204
/*          showscores(0); */
207
/*          exit(0); */
205
/*          exit(0); */
208
/*      default: */
206
/*      default: */
209
/*          usage(); */
207
/*          usage(); */
210
/*      } */
208
/*      } */
211
 
209
 
212
/*  argc -= optind; */
210
/*  argc -= optind; */
213
/*  argv += optind; */
211
/*  argv += optind; */
214
 
212
 
215
/*  if (argc) */
213
/*  if (argc) */
216
/*      usage(); */
214
/*      usage(); */
217
 
215
 
218
    fallrate = 1000000 / level;
216
    fallrate = 1000000 / level;
219
 
217
 
220
    for (i = 0; i <= 5; i++) {
218
    for (i = 0; i <= 5; i++) {
221
        for (j = i+1; j <= 5; j++) {
219
        for (j = i+1; j <= 5; j++) {
222
            if (keys[i] == keys[j])
220
            if (keys[i] == keys[j])
223
                errx(1, "duplicate command keys specified.");
221
                errx(1, "duplicate command keys specified.");
224
        }
222
        }
225
        if (keys[i] == ' ')
223
        if (keys[i] == ' ')
226
            strncpy(key_write[i], "<space>", sizeof key_write[i]);
224
            strncpy(key_write[i], "<space>", sizeof key_write[i]);
227
        else {
225
        else {
228
            key_write[i][0] = keys[i];
226
            key_write[i][0] = keys[i];
229
            key_write[i][1] = '\0';
227
            key_write[i][1] = '\0';
230
        }
228
        }
231
    }
229
    }
232
 
230
 
233
    snprintf(key_msg, sizeof key_msg,
231
    snprintf(key_msg, sizeof key_msg,
234
"%s - left   %s - rotate   %s - right   %s - drop   %s - pause   %s - quit",
232
"%s - left   %s - rotate   %s - right   %s - drop   %s - pause   %s - quit",
235
        key_write[0], key_write[1], key_write[2], key_write[3],
233
        key_write[0], key_write[1], key_write[2], key_write[3],
236
        key_write[4], key_write[5]);
234
        key_write[4], key_write[5]);
237
 
235
 
238
    scr_init();
236
    scr_init();
239
    setup_board();
237
    setup_board();
240
 
238
 
241
    srandomdev();
239
    srandomdev();
242
    scr_set();
240
    scr_set();
243
 
241
 
244
    pos = A_FIRST*B_COLS + (B_COLS/2)-1;
242
    pos = A_FIRST*B_COLS + (B_COLS/2)-1;
245
    nextshape = randshape();
243
    nextshape = randshape();
246
    curshape = randshape();
244
    curshape = randshape();
247
 
245
 
248
    scr_msg(key_msg, 1);
246
    scr_msg(key_msg, 1);
249
 
247
 
250
    for (;;) {
248
    for (;;) {
251
        place(curshape, pos, 1);
249
        place(curshape, pos, 1);
252
        scr_update();
250
        scr_update();
253
        place(curshape, pos, 0);
251
        place(curshape, pos, 0);
254
        c = tgetchar();
252
        c = tgetchar();
255
        if (c < 0) {
253
        if (c < 0) {
256
            /*
254
            /*
257
             * Timeout.  Move down if possible.
255
             * Timeout.  Move down if possible.
258
             */
256
             */
259
            if (fits_in(curshape, pos + B_COLS)) {
257
            if (fits_in(curshape, pos + B_COLS)) {
260
                pos += B_COLS;
258
                pos += B_COLS;
261
                continue;
259
                continue;
262
            }
260
            }
263
 
261
 
264
            /*
262
            /*
265
             * Put up the current shape `permanently',
263
             * Put up the current shape `permanently',
266
             * bump score, and elide any full rows.
264
             * bump score, and elide any full rows.
267
             */
265
             */
268
            place(curshape, pos, 1);
266
            place(curshape, pos, 1);
269
            score++;
267
            score++;
270
            elide();
268
            elide();
271
 
269
 
272
            /*
270
            /*
273
             * Choose a new shape.  If it does not fit,
271
             * Choose a new shape.  If it does not fit,
274
             * the game is over.
272
             * the game is over.
275
             */
273
             */
276
            curshape = nextshape;
274
            curshape = nextshape;
277
            nextshape = randshape();
275
            nextshape = randshape();
278
            pos = A_FIRST*B_COLS + (B_COLS/2)-1;
276
            pos = A_FIRST*B_COLS + (B_COLS/2)-1;
279
            if (!fits_in(curshape, pos))
277
            if (!fits_in(curshape, pos))
280
                break;
278
                break;
281
            continue;
279
            continue;
282
        }
280
        }
283
 
281
 
284
        /*
282
        /*
285
         * Handle command keys.
283
         * Handle command keys.
286
         */
284
         */
287
        if (c == keys[5]) {
285
        if (c == keys[5]) {
288
            /* quit */
286
            /* quit */
289
            break;
287
            break;
290
        }
288
        }
291
        if (c == keys[4]) {
289
        if (c == keys[4]) {
292
            static char msg[] =
290
            static char msg[] =
293
                "paused - press RETURN to continue";
291
                "paused - press RETURN to continue";
294
 
292
 
295
            place(curshape, pos, 1);
293
            place(curshape, pos, 1);
296
            do {
294
            do {
297
                scr_update();
295
                scr_update();
298
                scr_msg(key_msg, 0);
296
                scr_msg(key_msg, 0);
299
                scr_msg(msg, 1);
297
                scr_msg(msg, 1);
300
//              (void) fflush(stdout);
298
//              (void) fflush(stdout);
301
            } while (rwait((struct timeval *)NULL) == -1);
299
            } while (rwait((struct timeval *)NULL) == -1);
302
            scr_msg(msg, 0);
300
            scr_msg(msg, 0);
303
            scr_msg(key_msg, 1);
301
            scr_msg(key_msg, 1);
304
            place(curshape, pos, 0);
302
            place(curshape, pos, 0);
305
            continue;
303
            continue;
306
        }
304
        }
307
        if (c == keys[0]) {
305
        if (c == keys[0]) {
308
            /* move left */
306
            /* move left */
309
            if (fits_in(curshape, pos - 1))
307
            if (fits_in(curshape, pos - 1))
310
                pos--;
308
                pos--;
311
            continue;
309
            continue;
312
        }
310
        }
313
        if (c == keys[1]) {
311
        if (c == keys[1]) {
314
            /* turn */
312
            /* turn */
315
            const struct shape *new = &shapes[
313
            const struct shape *new = &shapes[
316
                classic? curshape->rotc : curshape->rot];
314
                classic? curshape->rotc : curshape->rot];
317
 
315
 
318
            if (fits_in(new, pos))
316
            if (fits_in(new, pos))
319
                curshape = new;
317
                curshape = new;
320
            continue;
318
            continue;
321
        }
319
        }
322
        if (c == keys[2]) {
320
        if (c == keys[2]) {
323
            /* move right */
321
            /* move right */
324
            if (fits_in(curshape, pos + 1))
322
            if (fits_in(curshape, pos + 1))
325
                pos++;
323
                pos++;
326
            continue;
324
            continue;
327
        }
325
        }
328
        if (c == keys[3]) {
326
        if (c == keys[3]) {
329
            /* move to bottom */
327
            /* move to bottom */
330
            while (fits_in(curshape, pos + B_COLS)) {
328
            while (fits_in(curshape, pos + B_COLS)) {
331
                pos += B_COLS;
329
                pos += B_COLS;
332
                score++;
330
                score++;
333
            }
331
            }
334
            continue;
332
            continue;
335
        }
333
        }
336
        if (c == '\f') {
334
        if (c == '\f') {
337
            scr_clear();
335
            scr_clear();
338
            scr_msg(key_msg, 1);
336
            scr_msg(key_msg, 1);
339
        }
337
        }
340
    }
338
    }
341
 
339
 
342
    scr_clear();
340
    scr_clear();
343
    scr_end();
341
    scr_end();
344
 
342
 
345
    if (showpreview == 0)
343
    if (showpreview == 0)
346
        (void)printf("Your score:  %d point%s  x  level %d  =  %d\n",
344
        (void)printf("Your score:  %d point%s  x  level %d  =  %d\n",
347
            score, score == 1 ? "" : "s", level, score * level);
345
            score, score == 1 ? "" : "s", level, score * level);
348
    else {
346
    else {
349
        (void)printf("Your score:  %d point%s x level %d x preview penalty %0.3f = %d\n",
347
        (void)printf("Your score:  %d point%s x level %d x preview penalty %0.3f = %d\n",
350
            score, score == 1 ? "" : "s", level, (double)PRE_PENALTY,
348
            score, score == 1 ? "" : "s", level, (double)PRE_PENALTY,
351
            (int)(score * level * PRE_PENALTY));
349
            (int)(score * level * PRE_PENALTY));
352
        score = score * PRE_PENALTY;
350
        score = score * PRE_PENALTY;
353
    }
351
    }
354
    savescore(level);
352
    savescore(level);
355
 
353
 
356
    printf("\nHit RETURN to see high scores, ^C to skip.\n");
354
    printf("\nHit RETURN to see high scores, ^C to skip.\n");
357
 
355
 
358
    while ((i = getchar()) != '\n')
356
    while ((i = getchar()) != '\n')
359
        if (i == EOF)
357
        if (i == EOF)
360
            break;
358
            break;
361
 
359
 
362
    showscores(level);
360
    showscores(level);
363
 
361
 
364
    exit(0);
362
    exit(0);
365
}
363
}
366
 
364
 
367
/* void */
365
/* void */
368
/* onintr(int signo) */
366
/* onintr(int signo) */
369
/* { */
367
/* { */
370
/*  scr_clear();        /\* XXX signal race *\/ */
368
/*  scr_clear();        /\* XXX signal race *\/ */
371
/*  scr_end();      /\* XXX signal race *\/ */
369
/*  scr_end();      /\* XXX signal race *\/ */
372
/*  _exit(0); */
370
/*  _exit(0); */
373
/* } */
371
/* } */
374
 
372
 
375
void
373
void
376
usage(void)
374
usage(void)
377
{
375
{
378
    (void)fprintf(stderr, "usage: tetris [-ps] [-k keys] [-l level]\n");
376
    (void)fprintf(stderr, "usage: tetris [-ps] [-k keys] [-l level]\n");
379
    exit(1);
377
    exit(1);
380
}
378
}
381
 
379