2001-12-06 15:24:00 +03:00
|
|
|
/* $NetBSD: worm.c,v 1.23 2001/12/06 12:24:00 blymn Exp $ */
|
1995-04-22 11:53:41 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1995-04-22 11:53:41 +04:00
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-10-12 06:12:45 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-04-22 11:53:41 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)worm.c 8.1 (Berkeley) 5/31/93";
|
|
|
|
#else
|
2001-12-06 15:24:00 +03:00
|
|
|
__RCSID("$NetBSD: worm.c,v 1.23 2001/12/06 12:24:00 blymn Exp $");
|
1995-04-22 11:53:41 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Worm. Written by Michael Toy
|
|
|
|
* UCSC
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <curses.h>
|
1999-09-09 21:27:58 +04:00
|
|
|
#include <err.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <signal.h>
|
1995-04-24 16:21:37 +04:00
|
|
|
#include <stdlib.h>
|
1995-04-22 11:53:41 +04:00
|
|
|
#include <termios.h>
|
1997-10-12 06:12:45 +04:00
|
|
|
#include <unistd.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#define newlink() (struct body *) malloc(sizeof (struct body));
|
|
|
|
#define HEAD '@'
|
|
|
|
#define BODY 'o'
|
|
|
|
#define LENGTH 7
|
|
|
|
#define RUNLEN 8
|
|
|
|
#define CNTRL(p) (p-'A'+1)
|
|
|
|
|
|
|
|
WINDOW *tv;
|
|
|
|
WINDOW *stw;
|
|
|
|
struct body {
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
struct body *prev;
|
|
|
|
struct body *next;
|
|
|
|
} *head, *tail, goody;
|
|
|
|
int growing = 0;
|
|
|
|
int running = 0;
|
|
|
|
int slow = 0;
|
|
|
|
int score = 0;
|
|
|
|
int start_len = LENGTH;
|
2001-08-31 11:15:44 +04:00
|
|
|
int visible_len;
|
1999-10-26 10:35:49 +04:00
|
|
|
int lastch;
|
1993-03-21 12:45:37 +03:00
|
|
|
char outbuf[BUFSIZ];
|
|
|
|
|
1998-09-13 19:27:25 +04:00
|
|
|
void crash __P((void)) __attribute__((__noreturn__));
|
Add use of `const' where appropriate to the games.
This merges in all such remaining changes from the Linux port of the
NetBSD games, except in hunt (where substantial changes from OpenBSD
need to be looked at).
Some such changes were previously covered in PRs bin/6041, bin/6146,
bin/6148, bin/6150, bin/6151, bin/6580, bin/6660, bin/7993, bin/7994,
bin/8039, bin/8057 and bin/8093.
1999-09-09 01:17:44 +04:00
|
|
|
void display __P((const struct body *, char));
|
1997-10-12 06:12:45 +04:00
|
|
|
int main __P((int, char **));
|
1998-09-13 19:27:25 +04:00
|
|
|
void leave __P((int)) __attribute__((__noreturn__));
|
1997-10-12 06:12:45 +04:00
|
|
|
void life __P((void));
|
|
|
|
void newpos __P((struct body *));
|
1999-10-26 10:35:49 +04:00
|
|
|
void process __P((int));
|
1997-10-12 06:12:45 +04:00
|
|
|
void prize __P((void));
|
|
|
|
int rnd __P((int));
|
|
|
|
void setup __P((void));
|
|
|
|
void wake __P((int));
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
|
|
|
|
1999-09-12 13:02:20 +04:00
|
|
|
/* Revoke setgid privileges */
|
2000-05-08 11:55:59 +04:00
|
|
|
setgid(getgid());
|
1999-09-12 13:02:20 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
setbuf(stdout, outbuf);
|
|
|
|
srand(getpid());
|
|
|
|
signal(SIGALRM, wake);
|
|
|
|
signal(SIGINT, leave);
|
|
|
|
signal(SIGQUIT, leave);
|
|
|
|
initscr();
|
2001-12-06 15:24:00 +03:00
|
|
|
cbreak();
|
1993-03-21 12:45:37 +03:00
|
|
|
noecho();
|
1999-10-26 10:35:49 +04:00
|
|
|
#ifdef KEY_LEFT
|
|
|
|
keypad(stdscr, TRUE);
|
|
|
|
#endif
|
1993-08-10 06:05:36 +04:00
|
|
|
slow = (baudrate() <= 1200);
|
1993-03-21 12:45:37 +03:00
|
|
|
clear();
|
2001-08-30 14:49:50 +04:00
|
|
|
if (COLS < 18 || LINES < 5) {
|
|
|
|
/*
|
|
|
|
* Insufficient room for the line with " Worm" and the
|
|
|
|
* score if fewer than 18 columns; insufficient room for
|
|
|
|
* anything much if fewer than 5 lines.
|
|
|
|
*/
|
|
|
|
endwin();
|
|
|
|
errx(1, "screen too small");
|
|
|
|
}
|
2001-08-30 03:25:58 +04:00
|
|
|
if (argc == 2)
|
|
|
|
start_len = atoi(argv[1]);
|
|
|
|
if ((start_len <= 0) || (start_len > ((LINES-3) * (COLS-2)) / 3))
|
|
|
|
start_len = LENGTH;
|
1993-03-21 12:45:37 +03:00
|
|
|
stw = newwin(1, COLS-1, 0, 0);
|
|
|
|
tv = newwin(LINES-1, COLS-1, 1, 0);
|
|
|
|
box(tv, '*', '*');
|
|
|
|
scrollok(tv, FALSE);
|
|
|
|
scrollok(stw, FALSE);
|
|
|
|
wmove(stw, 0, 0);
|
|
|
|
wprintw(stw, " Worm");
|
|
|
|
refresh();
|
|
|
|
wrefresh(stw);
|
|
|
|
wrefresh(tv);
|
|
|
|
life(); /* Create the worm */
|
|
|
|
prize(); /* Put up a goal */
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
if (running)
|
|
|
|
{
|
|
|
|
running--;
|
|
|
|
process(lastch);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fflush(stdout);
|
1999-10-26 10:35:49 +04:00
|
|
|
process(getch());
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
life()
|
|
|
|
{
|
1997-10-12 06:12:45 +04:00
|
|
|
struct body *bp, *np;
|
2001-08-30 03:25:58 +04:00
|
|
|
int i, j = 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
np = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
head = newlink();
|
1999-09-09 21:27:58 +04:00
|
|
|
if (head == NULL)
|
2000-01-09 20:17:19 +03:00
|
|
|
err(1, NULL);
|
2001-08-30 03:25:58 +04:00
|
|
|
head->x = start_len % (COLS-5) + 2;
|
|
|
|
head->y = LINES / 2;
|
1993-03-21 12:45:37 +03:00
|
|
|
head->next = NULL;
|
|
|
|
display(head, HEAD);
|
|
|
|
for (i = 0, bp = head; i < start_len; i++, bp = np) {
|
|
|
|
np = newlink();
|
1999-09-09 21:27:58 +04:00
|
|
|
if (np == NULL)
|
2000-01-09 20:17:19 +03:00
|
|
|
err(1, NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
np->next = bp;
|
|
|
|
bp->prev = np;
|
2001-08-30 03:25:58 +04:00
|
|
|
if (((bp->x <= 2) && (j == 1)) || ((bp->x >= COLS-4) && (j == -1))) {
|
|
|
|
j *= -1;
|
|
|
|
np->x = bp->x;
|
|
|
|
np->y = bp->y + 1;
|
|
|
|
} else {
|
|
|
|
np->x = bp->x - j;
|
|
|
|
np->y = bp->y;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
display(np, BODY);
|
|
|
|
}
|
|
|
|
tail = np;
|
|
|
|
tail->prev = NULL;
|
2001-08-31 11:15:44 +04:00
|
|
|
visible_len = start_len + 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
display(pos, chr)
|
Add use of `const' where appropriate to the games.
This merges in all such remaining changes from the Linux port of the
NetBSD games, except in hunt (where substantial changes from OpenBSD
need to be looked at).
Some such changes were previously covered in PRs bin/6041, bin/6146,
bin/6148, bin/6150, bin/6151, bin/6580, bin/6660, bin/7993, bin/7994,
bin/8039, bin/8057 and bin/8093.
1999-09-09 01:17:44 +04:00
|
|
|
const struct body *pos;
|
1997-10-12 06:12:45 +04:00
|
|
|
char chr;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
wmove(tv, pos->y, pos->x);
|
|
|
|
waddch(tv, chr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-10-12 06:12:45 +04:00
|
|
|
leave(dummy)
|
|
|
|
int dummy;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
endwin();
|
1999-08-08 07:08:08 +04:00
|
|
|
|
|
|
|
if (dummy == 0){ /* called via crash() */
|
|
|
|
printf("\nWell, you ran into something and the game is over.\n");
|
|
|
|
printf("Your final score was %d\n\n", score);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-10-12 06:12:45 +04:00
|
|
|
wake(dummy)
|
1999-09-09 01:45:25 +04:00
|
|
|
int dummy __attribute__((__unused__));
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
signal(SIGALRM, wake);
|
|
|
|
fflush(stdout);
|
|
|
|
process(lastch);
|
|
|
|
}
|
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
rnd(range)
|
1997-10-12 06:12:45 +04:00
|
|
|
int range;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
return abs((rand()>>5)+(rand()>>5)) % range;
|
|
|
|
}
|
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
newpos(bp)
|
1997-10-12 06:12:45 +04:00
|
|
|
struct body * bp;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-08-31 11:15:44 +04:00
|
|
|
if (visible_len == (LINES-3) * (COLS-3) - 1) {
|
|
|
|
endwin();
|
|
|
|
|
|
|
|
printf("\nYou won!\n");
|
|
|
|
printf("Your final score was %d\n\n", score);
|
|
|
|
exit(0);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
do {
|
2001-08-30 14:49:50 +04:00
|
|
|
bp->y = rnd(LINES-3)+ 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
bp->x = rnd(COLS-3) + 1;
|
|
|
|
wmove(tv, bp->y, bp->x);
|
|
|
|
} while(winch(tv) != ' ');
|
|
|
|
}
|
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
prize()
|
|
|
|
{
|
|
|
|
int value;
|
|
|
|
|
|
|
|
value = rnd(9) + 1;
|
|
|
|
newpos(&goody);
|
|
|
|
waddch(tv, value+'0');
|
|
|
|
wrefresh(tv);
|
|
|
|
}
|
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
process(ch)
|
1999-10-26 10:35:49 +04:00
|
|
|
int ch;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-12 06:12:45 +04:00
|
|
|
int x,y;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct body *nh;
|
|
|
|
|
|
|
|
alarm(0);
|
|
|
|
x = head->x;
|
|
|
|
y = head->y;
|
|
|
|
switch(ch)
|
|
|
|
{
|
1999-10-26 10:35:49 +04:00
|
|
|
#ifdef KEY_LEFT
|
|
|
|
case KEY_LEFT:
|
|
|
|
#endif
|
|
|
|
case 'h':
|
|
|
|
x--; break;
|
|
|
|
|
|
|
|
#ifdef KEY_DOWN
|
|
|
|
case KEY_DOWN:
|
|
|
|
#endif
|
|
|
|
case 'j':
|
|
|
|
y++; break;
|
|
|
|
|
|
|
|
#ifdef KEY_UP
|
|
|
|
case KEY_UP:
|
|
|
|
#endif
|
|
|
|
case 'k':
|
|
|
|
y--; break;
|
|
|
|
|
|
|
|
#ifdef KEY_RIGHT
|
|
|
|
case KEY_RIGHT:
|
|
|
|
#endif
|
|
|
|
case 'l':
|
|
|
|
x++; break;
|
|
|
|
|
1993-12-03 13:12:57 +03:00
|
|
|
case 'H': x--; running = RUNLEN; ch = tolower(ch); break;
|
|
|
|
case 'J': y++; running = RUNLEN/2; ch = tolower(ch); break;
|
|
|
|
case 'K': y--; running = RUNLEN/2; ch = tolower(ch); break;
|
|
|
|
case 'L': x++; running = RUNLEN; ch = tolower(ch); break;
|
|
|
|
case '\f': setup(); return;
|
1999-10-26 10:35:49 +04:00
|
|
|
|
|
|
|
case ERR:
|
|
|
|
case CNTRL('C'):
|
|
|
|
case CNTRL('D'):
|
|
|
|
crash();
|
|
|
|
return;
|
|
|
|
|
1993-12-03 13:12:57 +03:00
|
|
|
default: if (! running) alarm(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
lastch = ch;
|
|
|
|
if (growing == 0)
|
|
|
|
{
|
|
|
|
display(tail, ' ');
|
|
|
|
tail->next->prev = NULL;
|
|
|
|
nh = tail->next;
|
|
|
|
free(tail);
|
|
|
|
tail = nh;
|
2001-08-31 11:15:44 +04:00
|
|
|
visible_len--;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
else growing--;
|
|
|
|
display(head, BODY);
|
|
|
|
wmove(tv, y, x);
|
|
|
|
if (isdigit(ch = winch(tv)))
|
|
|
|
{
|
|
|
|
growing += ch-'0';
|
|
|
|
prize();
|
|
|
|
score += growing;
|
|
|
|
running = 0;
|
2001-08-30 14:49:50 +04:00
|
|
|
wmove(stw, 0, COLS - 12);
|
1993-03-21 12:45:37 +03:00
|
|
|
wprintw(stw, "Score: %3d", score);
|
|
|
|
wrefresh(stw);
|
|
|
|
}
|
|
|
|
else if(ch != ' ') crash();
|
|
|
|
nh = newlink();
|
1999-09-09 21:27:58 +04:00
|
|
|
if (nh == NULL)
|
2000-01-09 20:17:19 +03:00
|
|
|
err(1, NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
nh->next = NULL;
|
|
|
|
nh->prev = head;
|
|
|
|
head->next = nh;
|
|
|
|
nh->y = y;
|
|
|
|
nh->x = x;
|
|
|
|
display(nh, HEAD);
|
|
|
|
head = nh;
|
2001-08-31 11:15:44 +04:00
|
|
|
visible_len++;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!(slow && running))
|
1999-07-29 03:09:45 +04:00
|
|
|
{
|
|
|
|
wmove(tv, head->y, head->x);
|
1993-03-21 12:45:37 +03:00
|
|
|
wrefresh(tv);
|
1999-07-29 03:09:45 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!running)
|
|
|
|
alarm(1);
|
|
|
|
}
|
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
crash()
|
|
|
|
{
|
1997-10-12 06:12:45 +04:00
|
|
|
leave(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-10-12 06:12:45 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
setup()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
refresh();
|
|
|
|
touchwin(stw);
|
|
|
|
wrefresh(stw);
|
|
|
|
touchwin(tv);
|
|
|
|
wrefresh(tv);
|
|
|
|
alarm(1);
|
|
|
|
}
|