Subversion Repositories HelenOS-historic

Rev

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

Rev 1472 Rev 1524
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>
48
//#include <sys/param.h>
49
#include <sys/time.h>
49
#include <sys/time.h>
50
#include <sys/types.h>
50
#include <sys/types.h>
51
 
51
 
52
#include <err.h>
52
#include <err.h>
53
//#include <signal.h>
53
//#include <signal.h>
54
#include <stdio.h>
54
#include <stdio.h>
55
#include <stdlib.h>
55
#include <stdlib.h>
56
#include <string.h>
56
#include <string.h>
57
#include <unistd.h>
57
#include <unistd.h>
58
 
58
 
59
#include "input.h"
59
#include "input.h"
60
#include "scores.h"
60
#include "scores.h"
61
#include "screen.h"
61
#include "screen.h"
62
#include "tetris.h"
62
#include "tetris.h"
63
 
63
 
64
cell    board[B_SIZE];
64
cell    board[B_SIZE];
65
int Rows, Cols;
65
int Rows, Cols;
66
const struct shape *curshape;
66
const struct shape *curshape;
67
const struct shape *nextshape;
67
const struct shape *nextshape;
68
long    fallrate;
68
long    fallrate;
69
int score;
69
int score;
70
//gid_t gid, egid;
70
//gid_t gid, egid;
71
char    key_msg[100];
71
char    key_msg[100];
72
int showpreview, classic;
72
int showpreview, classic;
73
 
73
 
74
static void elide(void);
74
static void elide(void);
75
static void setup_board(void);
75
static void setup_board(void);
76
const struct shape *randshape(void);
76
const struct shape *randshape(void);
77
void    onintr(int);
77
void    onintr(int);
78
void    usage(void);
78
void    usage(void);
79
 
79
 
80
/*
80
/*
81
 * Set up the initial board.  The bottom display row is completely set,
81
 * Set up the initial board.  The bottom display row is completely set,
82
 * along with another (hidden) row underneath that.  Also, the left and
82
 * along with another (hidden) row underneath that.  Also, the left and
83
 * right edges are set.
83
 * right edges are set.
84
 */
84
 */
85
static void
85
static void
86
setup_board(void)
86
setup_board(void)
87
{
87
{
88
    int i;
88
    int i;
89
    cell *p;
89
    cell *p;
90
 
90
 
91
    p = board;
91
    p = board;
92
    for (i = B_SIZE; i; i--)
92
    for (i = B_SIZE; i; i--)
93
        *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;
93
        *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;
94
}
94
}
95
 
95
 
96
/*
96
/*
97
 * Elide any full active rows.
97
 * Elide any full active rows.
98
 */
98
 */
99
static void
99
static void
100
elide(void)
100
elide(void)
101
{
101
{
102
    int rows = 0;
102
    int rows = 0;
103
    int i, j, base;
103
    int i, j, base;
104
    cell *p;
104
    cell *p;
105
 
105
 
106
    for (i = A_FIRST; i < A_LAST; i++) {
106
    for (i = A_FIRST; i < A_LAST; i++) {
107
        base = i * B_COLS + 1;
107
        base = i * B_COLS + 1;
108
        p = &board[base];
108
        p = &board[base];
109
        for (j = B_COLS - 2; *p++ != 0;) {
109
        for (j = B_COLS - 2; *p++ != 0;) {
110
            if (--j <= 0) {
110
            if (--j <= 0) {
111
                /* this row is to be elided */
111
                /* this row is to be elided */
112
                rows++;
112
                rows++;
113
                memset(&board[base], 0, B_COLS - 2);
113
                memset(&board[base], 0, B_COLS - 2);
114
                scr_update();
114
                scr_update();
115
                tsleep();
115
                tsleep();
116
                while (--base != 0)
116
                while (--base != 0)
117
                    board[base + B_COLS] = board[base];
117
                    board[base + B_COLS] = board[base];
118
                scr_update();
118
                scr_update();
119
                tsleep();
119
                tsleep();
120
                break;
120
                break;
121
            }
121
            }
122
        }
122
        }
123
    }
123
    }
124
    switch (rows) {
124
    switch (rows) {
125
    case 1:
125
    case 1:
126
        score += 10;
126
        score += 10;
127
        break;
127
        break;
128
    case 2:
128
    case 2:
129
        score += 30;
129
        score += 30;
130
        break;
130
        break;
131
    case 3:
131
    case 3:
132
        score += 70;
132
        score += 70;
133
        break;
133
        break;
134
    case 4:
134
    case 4:
135
        score += 150;
135
        score += 150;
136
        break;
136
        break;
137
    default:
137
    default:
138
        break;
138
        break;
139
    }
139
    }
140
}
140
}
141
 
141
 
142
const struct shape *
142
const struct shape *
143
randshape(void)
143
randshape(void)
144
{
144
{
145
    const struct shape *tmp;
145
    const struct shape *tmp;
146
    int i, j;
146
    int i, j;
147
 
147
 
148
    tmp = &shapes[random() % 7];
148
    tmp = &shapes[random() % 7];
149
    j = random() % 4;
149
    j = random() % 4;
150
    for (i = 0; i < j; i++)
150
    for (i = 0; i < j; i++)
151
        tmp = &shapes[classic? tmp->rotc : tmp->rot];
151
        tmp = &shapes[classic? tmp->rotc : tmp->rot];
152
    return (tmp);
152
    return (tmp);
153
}
153
}
154
 
154
 
155
static void srandomdev(void)
155
static void srandomdev(void)
156
{
156
{
157
    struct timeval tv;
157
    struct timeval tv;
158
 
158
 
159
    gettimeofday(&tv, NULL);
159
    gettimeofday(&tv, NULL);
160
    srandom(tv.tv_sec + tv.tv_usec / 100000);
160
    srandom(tv.tv_sec + tv.tv_usec / 100000);
161
}
161
}
162
 
162
 
163
int
163
int
164
main(int argc, char *argv[])
164
main(int argc, char *argv[])
165
{
165
{
166
    int pos, c;
166
    int pos, c;
167
    char *keys;
167
    char *keys;
168
    int level = 2;
168
    int level = 2;
169
    char key_write[6][10];
169
    char key_write[6][10];
170
    const char *errstr;
170
    const char *errstr;
171
    int ch, i, j;
171
    int ch, i, j;
172
 
172
 
173
    keys = "jkl pq";
173
    keys = "jkl pq";
174
 
174
 
175
//  gid = getgid();
175
//  gid = getgid();
176
//  egid = getegid();
176
//  egid = getegid();
177
//  setegid(gid);
177
//  setegid(gid);
178
 
178
 
179
    classic = showpreview = 0;
179
    classic = showpreview = 0;
180
 
180
 
181
/*  while ((ch = getopt(argc, argv, "ck:l:ps")) != -1) */
181
/*  while ((ch = getopt(argc, argv, "ck:l:ps")) != -1) */
182
/*      switch(ch) { */
182
/*      switch(ch) { */
183
/*      case 'c': */
183
/*      case 'c': */
184
/*          /\* */
184
/*          /\* */
185
/*           * this means: */
185
/*           * this means: */
186
/*           *  - rotate the other way; */
186
/*           *  - rotate the other way; */
187
/*           *  - no reverse video. */
187
/*           *  - no reverse video. */
188
/*           *\/ */
188
/*           *\/ */
189
/*          classic = 1; */
189
/*          classic = 1; */
190
/*          break; */
190
/*          break; */
191
/*      case 'k': */
191
/*      case 'k': */
192
/*          if (strlen(keys = optarg) != 6) */
192
/*          if (strlen(keys = optarg) != 6) */
193
/*              usage(); */
193
/*              usage(); */
194
/*          break; */
194
/*          break; */
195
/*      case 'l': */
195
/*      case 'l': */
196
/*          level = (int)strtonum(optarg, MINLEVEL, MAXLEVEL, */
196
/*          level = (int)strtonum(optarg, MINLEVEL, MAXLEVEL, */
197
/*              &errstr); */
197
/*              &errstr); */
198
/*          if (errstr) */
198
/*          if (errstr) */
199
/*              errx(1, "level must be from %d to %d", */
199
/*              errx(1, "level must be from %d to %d", */
200
/*                  MINLEVEL, MAXLEVEL); */
200
/*                  MINLEVEL, MAXLEVEL); */
201
/*          break; */
201
/*          break; */
202
/*      case 'p': */
202
/*      case 'p': */
203
/*          showpreview = 1; */
203
/*          showpreview = 1; */
204
/*          break; */
204
/*          break; */
205
/*      case 's': */
205
/*      case 's': */
206
/*          showscores(0); */
206
/*          showscores(0); */
207
/*          exit(0); */
207
/*          exit(0); */
208
/*      default: */
208
/*      default: */
209
/*          usage(); */
209
/*          usage(); */
210
/*      } */
210
/*      } */
211
 
211
 
212
/*  argc -= optind; */
212
/*  argc -= optind; */
213
/*  argv += optind; */
213
/*  argv += optind; */
214
 
214
 
215
/*  if (argc) */
215
/*  if (argc) */
216
/*      usage(); */
216
/*      usage(); */
217
 
217
 
218
    fallrate = 1000000 / level;
218
    fallrate = 1000000 / level;
219
 
219
 
220
    for (i = 0; i <= 5; i++) {
220
    for (i = 0; i <= 5; i++) {
221
        for (j = i+1; j <= 5; j++) {
221
        for (j = i+1; j <= 5; j++) {
222
            if (keys[i] == keys[j])
222
            if (keys[i] == keys[j])
223
                errx(1, "duplicate command keys specified.");
223
                errx(1, "duplicate command keys specified.");
224
        }
224
        }
225
        if (keys[i] == ' ')
225
        if (keys[i] == ' ')
226
            strncpy(key_write[i], "<space>", sizeof key_write[i]);
226
            strncpy(key_write[i], "<space>", sizeof key_write[i]);
227
        else {
227
        else {
228
            key_write[i][0] = keys[i];
228
            key_write[i][0] = keys[i];
229
            key_write[i][1] = '\0';
229
            key_write[i][1] = '\0';
230
        }
230
        }
231
    }
231
    }
232
 
232
 
233
    snprintf(key_msg, sizeof key_msg,
233
    snprintf(key_msg, sizeof key_msg,
234
"%s - left   %s - rotate   %s - right   %s - drop   %s - pause   %s - quit",
234
"%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],
235
        key_write[0], key_write[1], key_write[2], key_write[3],
236
        key_write[4], key_write[5]);
236
        key_write[4], key_write[5]);
237
 
237
 
238
//  (void)signal(SIGINT, onintr);
-
 
239
    scr_init();
238
    scr_init();
240
    setup_board();
239
    setup_board();
241
 
240
 
242
    srandomdev();
241
    srandomdev();
243
    scr_set();
242
    scr_set();
244
 
243
 
245
    pos = A_FIRST*B_COLS + (B_COLS/2)-1;
244
    pos = A_FIRST*B_COLS + (B_COLS/2)-1;
246
    nextshape = randshape();
245
    nextshape = randshape();
247
    curshape = randshape();
246
    curshape = randshape();
248
 
247
 
249
    scr_msg(key_msg, 1);
248
    scr_msg(key_msg, 1);
250
 
249
 
251
    for (;;) {
250
    for (;;) {
252
        place(curshape, pos, 1);
251
        place(curshape, pos, 1);
253
        scr_update();
252
        scr_update();
254
        place(curshape, pos, 0);
253
        place(curshape, pos, 0);
255
        c = tgetchar();
254
        c = tgetchar();
256
        if (c < 0) {
255
        if (c < 0) {
257
            /*
256
            /*
258
             * Timeout.  Move down if possible.
257
             * Timeout.  Move down if possible.
259
             */
258
             */
260
            if (fits_in(curshape, pos + B_COLS)) {
259
            if (fits_in(curshape, pos + B_COLS)) {
261
                pos += B_COLS;
260
                pos += B_COLS;
262
                continue;
261
                continue;
263
            }
262
            }
264
 
263
 
265
            /*
264
            /*
266
             * Put up the current shape `permanently',
265
             * Put up the current shape `permanently',
267
             * bump score, and elide any full rows.
266
             * bump score, and elide any full rows.
268
             */
267
             */
269
            place(curshape, pos, 1);
268
            place(curshape, pos, 1);
270
            score++;
269
            score++;
271
            elide();
270
            elide();
272
 
271
 
273
            /*
272
            /*
274
             * Choose a new shape.  If it does not fit,
273
             * Choose a new shape.  If it does not fit,
275
             * the game is over.
274
             * the game is over.
276
             */
275
             */
277
            curshape = nextshape;
276
            curshape = nextshape;
278
            nextshape = randshape();
277
            nextshape = randshape();
279
            pos = A_FIRST*B_COLS + (B_COLS/2)-1;
278
            pos = A_FIRST*B_COLS + (B_COLS/2)-1;
280
            if (!fits_in(curshape, pos))
279
            if (!fits_in(curshape, pos))
281
                break;
280
                break;
282
            continue;
281
            continue;
283
        }
282
        }
284
 
283
 
285
        /*
284
        /*
286
         * Handle command keys.
285
         * Handle command keys.
287
         */
286
         */
288
        if (c == keys[5]) {
287
        if (c == keys[5]) {
289
            /* quit */
288
            /* quit */
290
            break;
289
            break;
291
        }
290
        }
292
        if (c == keys[4]) {
291
        if (c == keys[4]) {
293
            static char msg[] =
292
            static char msg[] =
294
                "paused - press RETURN to continue";
293
                "paused - press RETURN to continue";
295
 
294
 
296
            place(curshape, pos, 1);
295
            place(curshape, pos, 1);
297
            do {
296
            do {
298
                scr_update();
297
                scr_update();
299
                scr_msg(key_msg, 0);
298
                scr_msg(key_msg, 0);
300
                scr_msg(msg, 1);
299
                scr_msg(msg, 1);
301
//              (void) fflush(stdout);
300
//              (void) fflush(stdout);
302
            } while (rwait((struct timeval *)NULL) == -1);
301
            } while (rwait((struct timeval *)NULL) == -1);
303
            scr_msg(msg, 0);
302
            scr_msg(msg, 0);
304
            scr_msg(key_msg, 1);
303
            scr_msg(key_msg, 1);
305
            place(curshape, pos, 0);
304
            place(curshape, pos, 0);
306
            continue;
305
            continue;
307
        }
306
        }
308
        if (c == keys[0]) {
307
        if (c == keys[0]) {
309
            /* move left */
308
            /* move left */
310
            if (fits_in(curshape, pos - 1))
309
            if (fits_in(curshape, pos - 1))
311
                pos--;
310
                pos--;
312
            continue;
311
            continue;
313
        }
312
        }
314
        if (c == keys[1]) {
313
        if (c == keys[1]) {
315
            /* turn */
314
            /* turn */
316
            const struct shape *new = &shapes[
315
            const struct shape *new = &shapes[
317
                classic? curshape->rotc : curshape->rot];
316
                classic? curshape->rotc : curshape->rot];
318
 
317
 
319
            if (fits_in(new, pos))
318
            if (fits_in(new, pos))
320
                curshape = new;
319
                curshape = new;
321
            continue;
320
            continue;
322
        }
321
        }
323
        if (c == keys[2]) {
322
        if (c == keys[2]) {
324
            /* move right */
323
            /* move right */
325
            if (fits_in(curshape, pos + 1))
324
            if (fits_in(curshape, pos + 1))
326
                pos++;
325
                pos++;
327
            continue;
326
            continue;
328
        }
327
        }
329
        if (c == keys[3]) {
328
        if (c == keys[3]) {
330
            /* move to bottom */
329
            /* move to bottom */
331
            while (fits_in(curshape, pos + B_COLS)) {
330
            while (fits_in(curshape, pos + B_COLS)) {
332
                pos += B_COLS;
331
                pos += B_COLS;
333
                score++;
332
                score++;
334
            }
333
            }
335
            continue;
334
            continue;
336
        }
335
        }
337
        if (c == '\f') {
336
        if (c == '\f') {
338
            scr_clear();
337
            scr_clear();
339
            scr_msg(key_msg, 1);
338
            scr_msg(key_msg, 1);
340
        }
339
        }
341
    }
340
    }
342
 
341
 
343
    scr_clear();
342
    scr_clear();
344
    scr_end();
343
    scr_end();
345
 
344
 
346
    if (showpreview == 0)
345
    if (showpreview == 0)
347
        (void)printf("Your score:  %d point%s  x  level %d  =  %d\n",
346
        (void)printf("Your score:  %d point%s  x  level %d  =  %d\n",
348
            score, score == 1 ? "" : "s", level, score * level);
347
            score, score == 1 ? "" : "s", level, score * level);
349
    else {
348
    else {
350
        (void)printf("Your score:  %d point%s x level %d x preview penalty %0.3f = %d\n",
349
        (void)printf("Your score:  %d point%s x level %d x preview penalty %0.3f = %d\n",
351
            score, score == 1 ? "" : "s", level, (double)PRE_PENALTY,
350
            score, score == 1 ? "" : "s", level, (double)PRE_PENALTY,
352
            (int)(score * level * PRE_PENALTY));
351
            (int)(score * level * PRE_PENALTY));
353
        score = score * PRE_PENALTY;
352
        score = score * PRE_PENALTY;
354
    }
353
    }
355
    savescore(level);
354
    savescore(level);
356
 
355
 
357
    printf("\nHit RETURN to see high scores, ^C to skip.\n");
356
    printf("\nHit RETURN to see high scores, ^C to skip.\n");
358
 
357
 
359
    while ((i = getchar()) != '\n')
358
    while ((i = getchar()) != '\n')
360
        if (i == EOF)
359
        if (i == EOF)
361
            break;
360
            break;
362
 
361
 
363
    showscores(level);
362
    showscores(level);
364
 
363
 
365
    exit(0);
364
    exit(0);
366
}
365
}
367
 
366
 
368
/* void */
367
/* void */
369
/* onintr(int signo) */
368
/* onintr(int signo) */
370
/* { */
369
/* { */
371
/*  scr_clear();        /\* XXX signal race *\/ */
370
/*  scr_clear();        /\* XXX signal race *\/ */
372
/*  scr_end();      /\* XXX signal race *\/ */
371
/*  scr_end();      /\* XXX signal race *\/ */
373
/*  _exit(0); */
372
/*  _exit(0); */
374
/* } */
373
/* } */
375
 
374
 
376
void
375
void
377
usage(void)
376
usage(void)
378
{
377
{
379
    (void)fprintf(stderr, "usage: tetris [-ps] [-k keys] [-l level]\n");
378
    (void)fprintf(stderr, "usage: tetris [-ps] [-k keys] [-l level]\n");
380
    exit(1);
379
    exit(1);
381
}
380
}
382
 
381