Tetris

Check-in [25baf927f6]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add "shadow" option -h to show where piece would land if dropped
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:25baf927f6f15395b23fba9eef99b8c99d2700c7d0d8c4eec7a2eda4985a4ce7
User & Date: ajv-899-334-8894@vsta.org 2008-10-28 02:37:44
Context
2008-10-29
00:31
Add "n" to move downward one step. check-in: b915664999 user: ajv-899-334-8894@vsta.org tags: trunk
2008-10-28
02:37
Add "shadow" option -h to show where piece would land if dropped check-in: 25baf927f6 user: ajv-899-334-8894@vsta.org tags: trunk
2008-10-26
19:50
Handle a couple of paths where SOstr may not be set check-in: 08519c34f6 user: ajv-899-334-8894@vsta.org tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to screen.c.

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

/*
 * Set up from termcap.
 */
void
scr_init()
{
	static int bsflag, xsflag, sgnum;
#ifdef unneeded
	static int ncflag;
#endif
	char *term, *fill;
	static struct tcninfo {	/* termcap numeric and flag info */
		char tcname[3];
		int *tcaddr;
	} tcflags[] = {
		{"bs", &bsflag},
		{"ms", &MSflag},
#ifdef unneeded
		{"nc", &ncflag},
#endif
		{"xs", &xsflag},
		{ {0}, NULL}
	}, tcnums[] = {
		{"co", &COnum},
		{"li", &LInum},
		{"sg", &sgnum},
		{ {0}, NULL}
	};
	
	if ((term = getenv("TERM")) == NULL)
		stop("you must set the TERM environment variable");

	if (tgetent(tbuf, term) <= 0)
		stop("cannot find your termcap");

	fill = combuf;
	{
		struct tcsinfo *p;

		for (p = tcstrings; p->tcaddr; p++)
			*p->tcaddr = tgetstr(p->tcname, &fill);
	}

	{
		struct tcninfo *p;

		for (p = tcflags; p->tcaddr; p++)
			*p->tcaddr = tgetflag(p->tcname);

		for (p = tcnums; p->tcaddr; p++)
			*p->tcaddr = tgetnum(p->tcname);
	}

	if (bsflag)
		BC = BCdefault;
	else if (BC == NULL && bcstr != NULL)
		BC = bcstr;

	if (CLstr == NULL)
		stop("cannot clear screen");

	if (CMstr == NULL || UP == NULL || BC == NULL)
		stop("cannot do random cursor positioning via tgoto()");

	PC = pcstr ? *pcstr : 0;
	if (sgnum >= 0 || xsflag)
		SOstr = SEstr = NULL;

#ifdef unneeded
	if (ncflag)
		CRstr = NULL;
	else if (CRstr == NULL)
		CRstr = "\r";

#endif
}

/* this foolery is needed to modify tty state `atomically' */
static jmp_buf scr_onstop;

static void
stopset(sig)
	int sig;
{
	sigset_t sigset;

	(void) signal(sig, SIG_DFL);
	(void) kill(getpid(), sig);
	sigemptyset(&sigset);
	sigaddset(&sigset, sig);
	(void) sigprocmask(SIG_UNBLOCK, &sigset, (sigset_t *)0);
	longjmp(scr_onstop, 1);
}

static void
scr_stop(sig)
	int sig;
{
	sigset_t sigset;

	scr_end();
	(void) kill(getpid(), sig);
	sigemptyset(&sigset);
	sigaddset(&sigset, sig);
	(void) sigprocmask(SIG_UNBLOCK, &sigset, (sigset_t *)0);
	scr_set();
	scr_msg(key_msg, 1);
}

/*
 * Set up screen mode.
 */
void
scr_set()
{
	struct winsize ws;
	struct termios newtt;
	sigset_t sigset, osigset;
	void (*ttou)(int);

	sigemptyset(&sigset);
	sigaddset(&sigset, SIGTSTP);
	sigaddset(&sigset, SIGTTOU);
	(void) sigprocmask(SIG_BLOCK, &sigset, &osigset);
	if ((tstp = signal(SIGTSTP, stopset)) == SIG_IGN)
		(void) signal(SIGTSTP, SIG_IGN);

	if ((ttou = signal(SIGTTOU, stopset)) == SIG_IGN)
		(void) signal(SIGTTOU, SIG_IGN);

	/*
	 * At last, we are ready to modify the tty state.  If
	 * we stop while at it, stopset() above will longjmp back
	 * to the setjmp here and we will start over.
	 */
	(void) setjmp(scr_onstop);
	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
	Rows = 0, Cols = 0;
	if (ioctl(0, TIOCGWINSZ, &ws) == 0) {
		Rows = ws.ws_row;
		Cols = ws.ws_col;
	}
	if (Rows == 0)
		Rows = LInum;

	if (Cols == 0)
	Cols = COnum;

	if (Rows < MINROWS || Cols < MINCOLS) {
		(void) fprintf(stderr,
		    "the screen is too small: must be at least %dx%d, ",
		    MINCOLS, MINROWS);
		stop("");	/* stop() supplies \n */
	}
	if (tcgetattr(0, &oldtt) < 0)
		stop("tcgetattr() fails");

	newtt = oldtt;
	newtt.c_lflag &= ~(ICANON|ECHO);
	newtt.c_oflag &= ~OXTABS;
	newtt.c_cc[VMIN] = 1;
	newtt.c_cc[VTIME] = 0;
	if (tcsetattr(0, TCSADRAIN, &newtt) < 0)
		stop("tcsetattr() fails");

	ospeed = cfgetospeed(&newtt);
	(void) sigprocmask(SIG_BLOCK, &sigset, &osigset);

	/*
	 * We made it.  We are now in screen mode, modulo TIstr
	 * (which we will fix immediately).
	 */
	if (TIstr)
		putstr(TIstr);	/* termcap(5) says this is not padded */

	if (tstp != SIG_IGN)
		(void) signal(SIGTSTP, scr_stop);

	if (ttou != SIG_IGN)
		(void) signal(SIGTTOU, ttou);


	isset = 1;
	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
	scr_clear();
}

/*
 * End screen mode.
 */
void
scr_end()
{
	sigset_t sigset, osigset;

	sigemptyset(&sigset);
	sigaddset(&sigset, SIGTSTP);
	sigaddset(&sigset, SIGTTOU);
	(void) sigprocmask(SIG_BLOCK, &sigset, &osigset);
	/* move cursor to last line */
	if (LLstr)
		putstr(LLstr);	/* termcap(5) says this is not padded */
	else
		moveto(Rows - 1, 0);

	/* exit screen mode */
	if (TEstr)
		putstr(TEstr);	/* termcap(5) says this is not padded */

	(void) fflush(stdout);
	(void) tcsetattr(0, TCSADRAIN, &oldtt);
	isset = 0;
	/* restore signals */
	(void) signal(SIGTSTP, tstp);
	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
}

void
stop(why)
	const char *why;
{

	if (isset)
		scr_end();

	(void) fprintf(stderr, "aborting: %s\n", why);
	exit(1);
}

/*
 * Clear the screen, forgetting the current contents in the process.
 */
void
scr_clear()
{

	putpad(CLstr);
	curscore = -1;
	memset((char *)curscreen, 0, sizeof(curscreen));
}

/*
 * Update the screen.
 */
void
scr_update()
{
	cell *bp, *sp, so, cur_so = 0;
	int i, ccol, j;
	sigset_t sigset, osigset;
	static const struct shape *lastshape;

	sigemptyset(&sigset);
	sigaddset(&sigset, SIGTSTP);
	(void) sigprocmask(SIG_BLOCK, &sigset, &osigset);

	/* always leave cursor after last displayed point */
	curscreen[D_LAST * B_COLS - 1] = -1;

	if (score != curscore) {
		if (HOstr)
			putpad(HOstr);
		else
			moveto(0, 0);

		(void) printf("Score: %d", score);
		curscore = score;
	}

	/* draw preview of nextpattern */
	if (showpreview && (nextshape != lastshape)) {
		int i;
		static int r=5, c=2;
		int tr, tc, t; 

		lastshape = nextshape;
		
		/* clean */
		putpad(SEstr);
		for (i = r-1; i <= r+2; ++i) {
		    moveto(i, c-1); putstr("          ");
		}

		moveto(r-3, c-2);
		putstr("Next shape:");
						
		/* draw */
		if (SOstr) {
		    putpad(SOstr);
		}
		moveto(r, 2*c);
		putstr("  ");
		for(i=0; i<3; i++) {
			t = c + r*B_COLS;
			t += nextshape->off[i];

			tr = t / B_COLS;
			tc = t % B_COLS;

			moveto(tr, 2*tc);
			putstr("  ");
		}
		if (SOstr) {
		    putpad(SEstr);
		}
	}
	
	/*
	 * Repaint the actual board
	 *
	 * "bp" Points at the updated board contents
	 * "sp" Points at what was last painted on the screen
	 * "so" Flags whether or not "standout" is active,
	 *  and "cur_so" keeps track of whether or not standout
	 *  is current selected on the terminal.

	 * "ccol" indicates the current column, or -1 if the
	 *  hardware cursor should be moved before next output.
	 */
	bp = &board[D_FIRST * B_COLS];
	sp = &curscreen[D_FIRST * B_COLS];
	for (j = D_FIRST; j < D_LAST; j++) {


		ccol = -1;
		for (i = 0; i < B_COLS; bp++, sp++, i++) {




			if (*sp == (so = *bp)) {
				continue;
			}


			*sp = so;

			/* Need to move cursor for next output */
			if (i != ccol) {
				/*
				 * If can't move with standout enabled,
				 *  turn it off.
				 */
				if (cur_so && MSflag) {
					putpad(SEstr);
					cur_so = 0;
				}
				moveto(RTOD(j), CTOD(i));
			}

			/*
			 * Paint a position, using either standout applied
			 *  to spaces, or explicit characters if the
			 *  terminal doesn't support standout.
			 */















			if (SOstr) {

				if (so != cur_so) {
					putpad(so ? SOstr : SEstr);

					cur_so = so;
				}
				putstr("  ");


			} else {
				putstr(so ? "XX" : "  ");
			}






			/* Update notion of current column */
			ccol = i + 1;

			/*
			 * Look ahead a bit, to avoid extra motion if
			 * we will be redrawing the cell after the next.
			 * Motion probably takes four or more characters,
			 * so we save even if we rewrite two cells
			 * `unnecessarily'.  Skip it all, though, if
			 * the next cell is a different color.
			 *
			 * We inhibit extra motion by putting -1 into the
			 *  "current screen"'s idea of its contents, which
			 *  forces our loop to write those character
			 *  positions rather than try to skip them.
			 */
#define	STOP (B_COLS - 3)
			if (i > STOP || sp[1] != bp[1] || so != bp[1]) {

				continue;
			}
			if (sp[2] != bp[2]) {
				sp[1] = -1;
			} else if (i < STOP && so == bp[2] && sp[3] != bp[3]) {

				sp[2] = -1;
				sp[1] = -1;
			}
		}
	}


	if (cur_so) {
		putpad(SEstr);
	}

	(void) fflush(stdout);
	(void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
}

/*
 * Write a message (set!=0), or clear the same message (set==0).
 * (We need its length in case we have to overwrite with blanks.)
 */
void
scr_msg(s, set)
	char *s;
	int set;
{
	
	if (set || CEstr == NULL) {
		int l = strlen(s);

		moveto(Rows - 2, ((Cols - l) >> 1) - 1);
		if (set)
			putstr(s);
		else
			while (--l >= 0)
				(void) putchar(' ');


	} else {
		moveto(Rows - 2, 0);
		putpad(CEstr);
	}
}







|

|

|
|
|
|
|
|
|

|

|
|
|
|
|
|
|
|
|
|
|
>
|
|
>
|
|
|

|
|

>
|
|

|
|
>
|
|

>
|
|
|
|
>
|
|
>
|
|
>
|
|
|
>

|
|
|
|
>







<
|

|

|
|
|
|
|
|



|
<

|

|
|
|
|
|
|
|






|

|
|
|
|

|
|
|
|
|
|
>
|
|
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
|

>
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
>
|
|

|
|
|
|
|
|
>
|
|
>
|
|
|
>
|
|
|








|

|
|
|
|
|
|
|
|
|
>
|
|
|
>
|
|
|
|
|
|



<
|


|
|
>
|
|









|
|
|








|
|
|
|

|
|
|

|
|

|
|
|
|
|
>
|
|
|

|
|
|
|
|

|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|

|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
>
>
|
|
>
>
>
>
|
|
|
>
>
|

|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
<
>
|
|
|
>
>
|
|
|
>

>
>
>
>
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|

<
>
|
|
|
|
<
>
|
|
|
|
|
>
>
|
|
|
>
|
|











|
|
|

|
|
|
|
|
|
>
>
|
|
|
|

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

/*
 * Set up from termcap.
 */
void
scr_init()
{
    static int bsflag, xsflag, sgnum;
#ifdef unneeded
    static int ncflag;
#endif
    char *term, *fill;
    static struct tcninfo {	/* termcap numeric and flag info */
	char tcname[3];
	int *tcaddr;
    } tcflags[] = {
	{"bs", &bsflag},
	{"ms", &MSflag},
#ifdef unneeded
	{"nc", &ncflag},
#endif
	{"xs", &xsflag},
	{ {0}, NULL}
    }, tcnums[] = {
	{"co", &COnum},
	{"li", &LInum},
	{"sg", &sgnum},
	{ {0}, NULL}
    };
    
    if ((term = getenv("TERM")) == NULL) {
	stop("you must set the TERM environment variable");
    }
    if (tgetent(tbuf, term) <= 0) {
	stop("cannot find your termcap");
    }
    fill = combuf;
    {
	struct tcsinfo *p;

	for (p = tcstrings; p->tcaddr; p++) {
	    *p->tcaddr = tgetstr(p->tcname, &fill);
	}
    }
    {
	struct tcninfo *p;

	for (p = tcflags; p->tcaddr; p++) {
	    *p->tcaddr = tgetflag(p->tcname);
	}
	for (p = tcnums; p->tcaddr; p++) {
	    *p->tcaddr = tgetnum(p->tcname);
	}
    }
    if (bsflag) {
	BC = BCdefault;
    } else if (BC == NULL && bcstr != NULL) {
	BC = bcstr;
    }
    if (CLstr == NULL) {
	stop("cannot clear screen");
    }
    if (CMstr == NULL || UP == NULL || BC == NULL) {
	stop("cannot do random cursor positioning via tgoto()");
    }
    PC = pcstr ? *pcstr : 0;
    if (sgnum >= 0 || xsflag) {
	SOstr = SEstr = NULL;
    }
#ifdef unneeded
    if (ncflag) {
	CRstr = NULL;
    } else if (CRstr == NULL) {
	CRstr = "\r";
    }
#endif
}

/* this foolery is needed to modify tty state `atomically' */
static jmp_buf scr_onstop;

static void

stopset(int sig)
{
    sigset_t sigset;

    (void) signal(sig, SIG_DFL);
    (void) kill(getpid(), sig);
    sigemptyset(&sigset);
    sigaddset(&sigset, sig);
    (void) sigprocmask(SIG_UNBLOCK, &sigset, (sigset_t *)0);
    longjmp(scr_onstop, 1);
}

static void
scr_stop(int sig)

{
    sigset_t sigset;

    scr_end();
    (void) kill(getpid(), sig);
    sigemptyset(&sigset);
    sigaddset(&sigset, sig);
    (void) sigprocmask(SIG_UNBLOCK, &sigset, (sigset_t *)0);
    scr_set();
    scr_msg(key_msg, 1);
}

/*
 * Set up screen mode.
 */
void
scr_set(void)
{
    struct winsize ws;
    struct termios newtt;
    sigset_t sigset, osigset;
    void (*ttou)(int);

    sigemptyset(&sigset);
    sigaddset(&sigset, SIGTSTP);
    sigaddset(&sigset, SIGTTOU);
    (void) sigprocmask(SIG_BLOCK, &sigset, &osigset);
    if ((tstp = signal(SIGTSTP, stopset)) == SIG_IGN) {
	(void) signal(SIGTSTP, SIG_IGN);
    }
    if ((ttou = signal(SIGTTOU, stopset)) == SIG_IGN) {
	(void) signal(SIGTTOU, SIG_IGN);
    }
    /*
     * At last, we are ready to modify the tty state.  If
     * we stop while at it, stopset() above will longjmp back
     * to the setjmp here and we will start over.
     */
    (void) setjmp(scr_onstop);
    (void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
    Rows = 0, Cols = 0;
    if (ioctl(0, TIOCGWINSZ, &ws) == 0) {
	Rows = ws.ws_row;
	Cols = ws.ws_col;
    }
    if (Rows == 0) {
	Rows = LInum;
    }
    if (Cols == 0) {
	Cols = COnum;
    }
    if (Rows < MINROWS || Cols < MINCOLS) {
	(void) fprintf(stderr,
	    "the screen is too small: must be at least %dx%d, ",
	    MINCOLS, MINROWS);
	stop("");	/* stop() supplies \n */
    }
    if (tcgetattr(0, &oldtt) < 0) {
	stop("tcgetattr() fails");
    }
    newtt = oldtt;
    newtt.c_lflag &= ~(ICANON|ECHO);
    newtt.c_oflag &= ~OXTABS;
    newtt.c_cc[VMIN] = 1;
    newtt.c_cc[VTIME] = 0;
    if (tcsetattr(0, TCSADRAIN, &newtt) < 0) {
	stop("tcsetattr() fails");
    }
    ospeed = cfgetospeed(&newtt);
    (void) sigprocmask(SIG_BLOCK, &sigset, &osigset);

    /*
     * We made it.  We are now in screen mode, modulo TIstr
     * (which we will fix immediately).
     */
    if (TIstr) {
	    putstr(TIstr);	/* termcap(5) says this is not padded */
    }
    if (tstp != SIG_IGN) {
	(void) signal(SIGTSTP, scr_stop);
    }
    if (ttou != SIG_IGN) {
	(void) signal(SIGTTOU, ttou);
    }

    isset = 1;
    (void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
    scr_clear();
}

/*
 * End screen mode.
 */
void
scr_end()
{
    sigset_t sigset, osigset;

    sigemptyset(&sigset);
    sigaddset(&sigset, SIGTSTP);
    sigaddset(&sigset, SIGTTOU);
    (void) sigprocmask(SIG_BLOCK, &sigset, &osigset);
    /* move cursor to last line */
    if (LLstr) {
	putstr(LLstr);	/* termcap(5) says this is not padded */
    } else {
	moveto(Rows - 1, 0);
    }
    /* exit screen mode */
    if (TEstr) {
	putstr(TEstr);	/* termcap(5) says this is not padded */
    }
    (void) fflush(stdout);
    (void) tcsetattr(0, TCSADRAIN, &oldtt);
    isset = 0;
    /* restore signals */
    (void) signal(SIGTSTP, tstp);
    (void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
}

void

stop(const char *why)
{

    if (isset) {
	scr_end();
    }
    (void) fprintf(stderr, "aborting: %s\n", why);
    exit(1);
}

/*
 * Clear the screen, forgetting the current contents in the process.
 */
void
scr_clear()
{

    putpad(CLstr);
    curscore = -1;
    memset((char *)curscreen, 0, sizeof(curscreen));
}

/*
 * Update the screen.
 */
void
scr_update()
{
    cell *bp, *sp, cur_so = 0;
    int i, ccol, j;
    sigset_t sigset, osigset;
    static const struct shape *lastshape;

    sigemptyset(&sigset);
    sigaddset(&sigset, SIGTSTP);
    (void) sigprocmask(SIG_BLOCK, &sigset, &osigset);

    /* always leave cursor after last displayed point */
    curscreen[D_LAST * B_COLS - 1] = -1;

    if (score != curscore) {
	if (HOstr) {
	    putpad(HOstr);
	} else {
	    moveto(0, 0);
	}
	(void) printf("Score: %d", score);
	curscore = score;
    }

    /* draw preview of nextpattern */
    if (showpreview && (nextshape != lastshape)) {
	int i;
	static int r=5, c=2;
	int tr, tc, t; 

	lastshape = nextshape;
	
	/* clean */
	putpad(SEstr);
	for (i = r-1; i <= r+2; ++i) {
	    moveto(i, c-1); putstr("          ");
	}

	moveto(r-3, c-2);
	putstr("Next shape:");
					
	/* draw */
	if (SOstr) {
	    putpad(SOstr);
	}
	moveto(r, 2*c);
	putstr("  ");
	for(i=0; i<3; i++) {
	    t = c + r*B_COLS;
	    t += nextshape->off[i];

	    tr = t / B_COLS;
	    tc = t % B_COLS;

	    moveto(tr, 2*tc);
	    putstr("  ");
	}
	if (SOstr) {
	    putpad(SEstr);
	}
    }
    
    /*
     * Repaint the actual board
     *
     * "bp" Points at the updated board contents
     * "sp" Points at what was last painted on the screen
     * "so" Flags whether or not "standout" is active,
     *  and "cur_so" keeps track of whether or not standout
     *  is current selected on the terminal.
     * "cpos" is the character value at the current position.
     * "ccol" indicates the current column, or -1 if the
     *  hardware cursor should be moved before next output.
     */
    bp = &board[D_FIRST * B_COLS];
    sp = &curscreen[D_FIRST * B_COLS];
    for (j = D_FIRST; j < D_LAST; j++) {
	int cpos;

	ccol = -1;
	for (i = 0; i < B_COLS; bp++, sp++, i++) {
	    /*
	     * Fetch current position, and continue if
	     *  screen's contents already matches.
	     */
	    if (*sp == (cpos = *bp)) {
		continue;
	    }

	    /* Update screen image */
	    *sp = cpos;

	    /* Need to move cursor for next output */
	    if (i != ccol) {
		/*
		 * If can't move with standout enabled,
		 *  turn it off.
		 */
		if (cur_so && MSflag) {
		    putpad(SEstr);
		    cur_so = 0;
		}
		moveto(RTOD(j), CTOD(i));
	    }

	    /*
	     * Paint a position, using either standout applied
	     *  to spaces, or explicit characters if the
	     *  terminal doesn't support standout.
	     */
	    switch (cpos) {
	    case 0:	/* We want a blank here */
	    case 1:	/* We want a non-standout "##" shadow */

		/* Turn off standout if needed */
		if (cur_so) {
		    putpad(SEstr);
		    cur_so = 0;
		}
		putstr(cpos ? "##" : "  ");
		break;

	    case 2:	/* We want to paint a block here */

		/* Paint using standout on spaces */
		if (SOstr) {
		    /* Turn on standout if needed */
		    if (!cur_so) {

			putpad(SOstr);
			cur_so = 1;
		    }
		    putstr("  ");

		/* Otherwise paint "XX"'s */
		} else {
		    putstr("XX");
		}
		break;

	    default:
		abort();
	    }

	    /* Update notion of current column */
	    ccol = i + 1;

	    /*
	     * Look ahead a bit, to avoid extra motion if
	     * we will be redrawing the cell after the next.
	     * Motion probably takes four or more characters,
	     * so we save even if we rewrite two cells
	     * `unnecessarily'.  Skip it all, though, if
	     * the next cell is a different color.
	     *
	     * We inhibit extra motion by putting -1 into the
	     *  "current screen"'s idea of its contents, which
	     *  forces our loop to write those character
	     *  positions rather than try to skip them.
	     */
#define	STOP (B_COLS - 3)

	    if ((i > STOP) || (sp[1] != bp[1]) || (cpos != bp[1])) {
		continue;
	    }
	    if (sp[2] != bp[2]) {
		sp[1] = -1;

	    } else if ((i < STOP) && (cpos == bp[2]) && (sp[3] != bp[3])) {
		sp[2] = -1;
		sp[1] = -1;
	    }
	}
    }

    /* Leave standout shut off at end */
    if (cur_so) {
	putpad(SEstr);
    }

    (void) fflush(stdout);
    (void) sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
}

/*
 * Write a message (set!=0), or clear the same message (set==0).
 * (We need its length in case we have to overwrite with blanks.)
 */
void
scr_msg(s, set)
	char *s;
	int set;
{
    
    if (set || CEstr == NULL) {
	int l = strlen(s);

	moveto(Rows - 2, ((Cols - l) >> 1) - 1);
	if (set) {
	    putstr(s);
	} else {
	    while (--l >= 0) {
		(void) putchar(' ');
	    }
	}
    } else {
	moveto(Rows - 2, 0);
	putpad(CEstr);
    }
}

Changes to tetris.c.

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
..
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
...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
...
259
260
261
262
263
264
265



266
267
268
269
270
271
272

long	fallrate;		/* less than 1 million; smaller => faster */

int	score;			/* the obvious thing */
gid_t	gid, egid;

char	key_msg[100];
int	showpreview;

static	void	elide(void);
static	void	setup_board(void);
	int	main(int, char **);
	void	onintr(int) __attribute__((__noreturn__));
	void	usage(void) __attribute__((__noreturn__));

................................................................................
 * Set up the initial board.  The bottom display row is completely set,
 * along with another (hidden) row underneath that.  Also, the left and
 * right edges are set.
 */
static void
setup_board()
{
	int i;
	cell *p;

	p = board;
	for (i = B_SIZE; i; i--)
		*p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;





}

/*
 * Elide any full active rows.
 */
static void
elide()
{
	int i, j, base;
	cell *p;

	for (i = A_FIRST; i < A_LAST; i++) {
		base = i * B_COLS + 1;
		p = &board[base];
		for (j = B_COLS - 2; *p++ != 0;) {
			if (--j <= 0) {
				/* this row is to be elided */
				memset(&board[base], 0, B_COLS - 2);
				scr_update();
				tsleep();
				while (--base != 0)
					board[base + B_COLS] = board[base];

				scr_update();
				tsleep();
				break;
			}
		}
	}
}

/*
 * play_game()
 *	Core game processing
 */
static void
................................................................................
play_game(void)
{
    int pos, c;

    pos = A_FIRST*B_COLS + (B_COLS/2)-1;

    for (;;) {












	place(curshape, pos, 1);
	scr_update();





	place(curshape, pos, 0);

	c = tgetchar();
	if (c < 0) {
	    /*
	     * Timeout.  Move down if possible.
	     */
	    if (fits_in(curshape, pos + B_COLS)) {
		pos += B_COLS;
................................................................................
		continue;
	    }

	    /*
	     * Put up the current shape `permanently',
	     * bump score, and elide any full rows.
	     */
	    place(curshape, pos, 1);
	    score++;
	    elide();

	    /*
	     * Choose a new shape.  If it does not fit,
	     * the game is over.
	     */
................................................................................
	    /* quit */
	    break;
	}
	if (c == keys[4]) {
	    static char msg[] =
		"paused - press RETURN to continue";

	    place(curshape, pos, 1);
	    do {
		scr_update();
		scr_msg(key_msg, 0);
		scr_msg(msg, 1);
		(void) fflush(stdout);
	    } while (rwait((struct timeval *)NULL) == -1);
	    scr_msg(msg, 0);
................................................................................
	fd = open("/dev/null", O_RDONLY);
	if (fd < 3)
		exit(1);
	close(fd);

	keys = "jkl pq";

	while ((ch = getopt(argc, argv, "k:l:ps")) != -1)
		switch(ch) {
		case 'k':
			if (strlen(keys = optarg) != 6)
				usage();
			break;
		case 'l':
			level = atoi(optarg);
................................................................................
				errx(1, "level must be from %d to %d",
				     MINLEVEL, MAXLEVEL);
			}
			break;
		case 'p':
			showpreview = 1;
			break;



		case 's':
			showscores(0);
			exit(0);
		case '?':
		default:
			usage();
		}







|







 







|
|

|
|
|
>
>
>
>
>








|
|

|
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|







 







>
>
>
>
>
>
>
>
>
>
>
>
|

>
>
>
>
>

>







 







|







 







|







 







|







 







>
>
>







64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
..
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
...
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
...
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
...
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

long	fallrate;		/* less than 1 million; smaller => faster */

int	score;			/* the obvious thing */
gid_t	gid, egid;

char	key_msg[100];
int	showpreview, showshadow;

static	void	elide(void);
static	void	setup_board(void);
	int	main(int, char **);
	void	onintr(int) __attribute__((__noreturn__));
	void	usage(void) __attribute__((__noreturn__));

................................................................................
 * Set up the initial board.  The bottom display row is completely set,
 * along with another (hidden) row underneath that.  Also, the left and
 * right edges are set.
 */
static void
setup_board()
{
    int i;
    cell *p;

    p = board;
    for (i = B_SIZE; i; i--) {
	if ((i <= (2 * B_COLS)) || ((i % B_COLS) < 2)) {
	    *p++ = 2;
	} else {
	    *p++ = 0;
	}
    }
}

/*
 * Elide any full active rows.
 */
static void
elide()
{
    int i, j, base;
    cell *p;

    for (i = A_FIRST; i < A_LAST; i++) {
	base = i * B_COLS + 1;
	p = &board[base];
	for (j = B_COLS - 2; *p++ != 0; ) {
	    if (--j <= 0) {
		/* this row is to be elided */
		memset(&board[base], 0, B_COLS - 2);
		scr_update();
		tsleep();
		while (--base != 0) {
		    board[base + B_COLS] = board[base];
		}
		scr_update();
		tsleep();
		break;
	    }
	}
    }
}

/*
 * play_game()
 *	Core game processing
 */
static void
................................................................................
play_game(void)
{
    int pos, c;

    pos = A_FIRST*B_COLS + (B_COLS/2)-1;

    for (;;) {
	/* Display "shadow" of where piece would drop? */
	if (showshadow) {
	    for (c = 1; ; c += 1) {
		if (!fits_in(curshape, pos + B_COLS*c)) {
		    c -= 1;
		    break;
		}
	    }
	    place(curshape, pos + B_COLS*c, 1);
	}

	/* Paint piece onto board image, then update screen */
	place(curshape, pos, 2);
	scr_update();

	/* Remove shadow (if any), and piece image */
	if (showshadow) {
	    place(curshape, pos + B_COLS*c, 0);
	}
	place(curshape, pos, 0);

	c = tgetchar();
	if (c < 0) {
	    /*
	     * Timeout.  Move down if possible.
	     */
	    if (fits_in(curshape, pos + B_COLS)) {
		pos += B_COLS;
................................................................................
		continue;
	    }

	    /*
	     * Put up the current shape `permanently',
	     * bump score, and elide any full rows.
	     */
	    place(curshape, pos, 2);
	    score++;
	    elide();

	    /*
	     * Choose a new shape.  If it does not fit,
	     * the game is over.
	     */
................................................................................
	    /* quit */
	    break;
	}
	if (c == keys[4]) {
	    static char msg[] =
		"paused - press RETURN to continue";

	    place(curshape, pos, 2);
	    do {
		scr_update();
		scr_msg(key_msg, 0);
		scr_msg(msg, 1);
		(void) fflush(stdout);
	    } while (rwait((struct timeval *)NULL) == -1);
	    scr_msg(msg, 0);
................................................................................
	fd = open("/dev/null", O_RDONLY);
	if (fd < 3)
		exit(1);
	close(fd);

	keys = "jkl pq";

	while ((ch = getopt(argc, argv, "k:l:psh")) != -1)
		switch(ch) {
		case 'k':
			if (strlen(keys = optarg) != 6)
				usage();
			break;
		case 'l':
			level = atoi(optarg);
................................................................................
				errx(1, "level must be from %d to %d",
				     MINLEVEL, MAXLEVEL);
			}
			break;
		case 'p':
			showpreview = 1;
			break;
		case 'h':
			showshadow = 1;
			break;
		case 's':
			showscores(0);
			exit(0);
		case '?':
		default:
			usage();
		}

Changes to tetris.h.

167
168
169
170
171
172
173
174
175
176
177
178
 * we find that it is at rest and integrate it---until then, it can
 * still be moved or rotated).
 */
extern int	score;		/* the obvious thing */
extern gid_t	gid, egid;

extern char	key_msg[100];
extern int	showpreview;

int	fits_in(const struct shape *, int);
void	place(const struct shape *, int, int);
void	stop(const char *) __attribute__((__noreturn__));







|




167
168
169
170
171
172
173
174
175
176
177
178
 * we find that it is at rest and integrate it---until then, it can
 * still be moved or rotated).
 */
extern int	score;		/* the obvious thing */
extern gid_t	gid, egid;

extern char	key_msg[100];
extern int	showpreview, showshadow;

int	fits_in(const struct shape *, int);
void	place(const struct shape *, int, int);
void	stop(const char *) __attribute__((__noreturn__));