1
0
mirror of https://github.com/openbsd/src.git synced 2025-01-10 06:47:55 -08:00
openbsd-src/games/worm/worm.c

414 lines
7.7 KiB
C
Raw Normal View History

/* $OpenBSD: worm.c,v 1.39 2018/08/24 11:14:49 mestre Exp $ */
1995-10-18 01:37:01 -07:00
/*
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
*
* 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. Neither the name of the University nor the names of its contributors
1995-10-18 01:37:01 -07:00
* 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.
*/
/*
* Worm. Written by Michael Toy
* UCSC
*/
#include <ctype.h>
#include <curses.h>
1998-09-15 17:44:36 -07:00
#include <err.h>
#include <poll.h>
1995-10-18 01:37:01 -07:00
#include <signal.h>
#include <stdlib.h>
1998-02-08 10:59:56 -08:00
#include <unistd.h>
1995-10-18 01:37:01 -07:00
#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 growthscale = 1;
1995-10-18 01:37:01 -07:00
int running = 0;
int slow = 0;
int score = 0;
int start_len = LENGTH;
int visible_len;
int lastch;
1995-10-18 01:37:01 -07:00
char outbuf[BUFSIZ];
2003-05-19 00:53:54 -07:00
volatile sig_atomic_t wantleave = 0;
volatile sig_atomic_t wantsuspend = 0;
__dead void crash(void);
void display(struct body *, char);
void leave(int);
void life(void);
void newpos(struct body *);
struct body *newlink(void);
int process(int);
void prize(void);
int rnd(int);
void setup(void);
void suspend(int);
1995-10-18 01:37:01 -07:00
1998-02-08 10:59:56 -08:00
int
2003-05-19 00:58:00 -07:00
main(int argc, char **argv)
1995-10-18 01:37:01 -07:00
{
int retval;
struct pollfd pfd[1];
const char *errstr;
struct timespec t, tn, tdiff;
timespecclear(&t);
1995-10-18 01:37:01 -07:00
setvbuf(stdout, outbuf, _IOFBF, sizeof outbuf);
1995-10-18 01:37:01 -07:00
signal(SIGINT, leave);
signal(SIGQUIT, leave);
signal(SIGTSTP, suspend); /* process control signal */
initscr();
if (pledge("stdio tty", NULL) == -1)
err(1, "pledge");
cbreak();
1995-10-18 01:37:01 -07:00
noecho();
keypad(stdscr, TRUE);
1995-10-18 01:37:01 -07:00
slow = (baudrate() <= 1200);
clear();
if (COLS < 18 || LINES < 5) {
endwin();
errx(1, "screen too small");
}
growthscale = COLS * LINES / 2000;
if (growthscale == 0)
growthscale = 1;
if (argc >= 2) {
start_len = strtonum(argv[1], 1, ((LINES-3) * (COLS-2)) / 3,
&errstr);
if (errstr) {
endwin();
errx(1, "length argument is %s.", errstr);
}
}
1995-10-18 01:37:01 -07: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 */
wmove(tv, head->y, head->x); /* Leave cursor on worm */
wrefresh(tv);
2015-08-20 19:42:26 -07:00
while (1) {
if (wantleave) {
2015-08-20 19:42:26 -07:00
endwin();
return 0;
}
if (wantsuspend) {
move(LINES-1, 0);
refresh();
endwin();
fflush(stdout);
kill(getpid(), SIGSTOP);
signal(SIGTSTP, suspend);
cbreak();
noecho();
setup();
wantsuspend = 0;
}
2015-08-20 19:42:26 -07:00
if (running) {
1995-10-18 01:37:01 -07:00
running--;
process(lastch);
2015-08-20 19:42:26 -07:00
} else {
/* Check for timeout. */
clock_gettime(CLOCK_MONOTONIC, &tn);
if (timespeccmp(&t, &tn, <=)) {
t = tn;
t.tv_sec += 1;
process(lastch);
continue;
}
/* Prepare next read */
pfd[0].fd = STDIN_FILENO;
pfd[0].events = POLLIN;
timespecsub(&t, &tn, &tdiff);
retval = ppoll(pfd, 1, &tdiff, NULL);
/* Nothing to do if timed out or signal. */
if (retval <= 0)
continue;
/* Only update timer if valid key was pressed. */
if (process(getch()) == 0)
continue;
/* Update using clock_gettime(), tn is too old now. */
clock_gettime(CLOCK_MONOTONIC, &t);
t.tv_sec += 1;
1995-10-18 01:37:01 -07:00
}
}
}
1998-02-08 10:59:56 -08:00
void
2004-11-29 00:52:28 -08:00
life(void)
1995-10-18 01:37:01 -07:00
{
2002-05-30 21:21:20 -07:00
struct body *bp, *np;
int i,j = 1;
1995-10-18 01:37:01 -07:00
head = newlink();
1998-02-08 10:59:56 -08:00
head->x = start_len % (COLS-5) + 2;
head->y = LINES / 2;
1995-10-18 01:37:01 -07:00
head->next = NULL;
display(head, HEAD);
for (i = 0, bp = head; i < start_len; i++, bp = np) {
np = newlink();
np->next = bp;
bp->prev = np;
1998-09-15 17:44:36 -07:00
if (((bp->x <= 2) && (j == 1)) || ((bp->x >= COLS-4) && (j == -1))) {
1998-02-08 10:59:56 -08:00
j *= -1;
np->x = bp->x;
np->y = bp->y + 1;
} else {
np->x = bp->x - j;
np->y = bp->y;
}
1995-10-18 01:37:01 -07:00
display(np, BODY);
}
tail = np;
tail->prev = NULL;
visible_len = start_len + 1;
1995-10-18 01:37:01 -07:00
}
1998-02-08 10:59:56 -08:00
void
2003-05-19 00:58:00 -07:00
display(struct body *pos, char chr)
1995-10-18 01:37:01 -07:00
{
wmove(tv, pos->y, pos->x);
waddch(tv, chr);
}
void
2003-05-19 00:58:00 -07:00
leave(int dummy)
1995-10-18 01:37:01 -07:00
{
wantleave = 1;
1995-10-18 01:37:01 -07:00
}
1998-02-08 10:59:56 -08:00
int
2003-05-19 00:58:00 -07:00
rnd(int range)
1995-10-18 01:37:01 -07:00
{
return arc4random_uniform(range);
1995-10-18 01:37:01 -07:00
}
1998-02-08 10:59:56 -08:00
void
2003-05-19 00:58:00 -07:00
newpos(struct body *bp)
1995-10-18 01:37:01 -07:00
{
if (visible_len == (LINES-3) * (COLS-3) - 1) {
endwin();
printf("\nYou won!\nYour final score was %d\n\n", score);
exit(0);
}
1995-10-18 01:37:01 -07:00
do {
bp->y = rnd(LINES-3)+ 1;
1995-10-18 01:37:01 -07:00
bp->x = rnd(COLS-3) + 1;
wmove(tv, bp->y, bp->x);
} while(winch(tv) != ' ');
}
1998-02-08 10:59:56 -08:00
void
2004-11-29 00:52:28 -08:00
prize(void)
1995-10-18 01:37:01 -07:00
{
int value;
value = rnd(9) + 1;
newpos(&goody);
waddch(tv, value+'0');
wrefresh(tv);
}
int
2003-05-19 00:58:00 -07:00
process(int ch)
1995-10-18 01:37:01 -07:00
{
2002-05-30 21:21:20 -07:00
int x,y;
1995-10-18 01:37:01 -07:00
struct body *nh;
x = head->x;
y = head->y;
2015-08-20 19:42:26 -07:00
switch(ch) {
#ifdef KEY_LEFT
2015-08-20 19:42:26 -07:00
case KEY_LEFT:
#endif
2015-08-20 19:42:26 -07:00
case 'h':
x--;
break;
#ifdef KEY_DOWN
2015-08-20 19:42:26 -07:00
case KEY_DOWN:
#endif
2015-08-20 19:42:26 -07:00
case 'j':
y++;
break;
#ifdef KEY_UP
2015-08-20 19:42:26 -07:00
case KEY_UP:
#endif
2015-08-20 19:42:26 -07:00
case 'k':
y--;
break;
#ifdef KEY_RIGHT
2015-08-20 19:42:26 -07:00
case KEY_RIGHT:
#endif
2015-08-20 19:42:26 -07:00
case 'l':
x++;
break;
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 (0);
2015-08-20 19:42:26 -07:00
case CNTRL('Z'):
suspend(0);
return (0);
2015-08-20 19:42:26 -07:00
case CNTRL('C'):
crash();
return (0);
2015-08-20 19:42:26 -07:00
case CNTRL('D'):
crash();
return (0);
2015-08-20 19:42:26 -07:00
case ERR:
leave(0);
return (0);
2015-08-20 19:42:26 -07:00
default:
return (0);
1995-10-18 01:37:01 -07:00
}
lastch = ch;
2015-08-20 19:42:26 -07:00
if (growing == 0) {
1995-10-18 01:37:01 -07:00
display(tail, ' ');
tail->next->prev = NULL;
nh = tail->next;
free(tail);
tail = nh;
visible_len--;
2015-08-20 19:42:26 -07:00
} else
growing--;
1995-10-18 01:37:01 -07:00
display(head, BODY);
wmove(tv, y, x);
2015-08-20 19:42:26 -07:00
if (isdigit(ch = winch(tv))) {
int amt = ch - '0';
growing += amt * growthscale;
1995-10-18 01:37:01 -07:00
prize();
score += amt;
1995-10-18 01:37:01 -07:00
running = 0;
1998-02-08 10:59:56 -08:00
wmove(stw, 0, COLS - 12);
1995-10-18 01:37:01 -07:00
wprintw(stw, "Score: %3d", score);
wrefresh(stw);
2015-08-20 19:42:26 -07:00
} else if(ch != ' ')
crash();
1995-10-18 01:37:01 -07:00
nh = newlink();
nh->next = NULL;
nh->prev = head;
head->next = nh;
nh->y = y;
nh->x = x;
display(nh, HEAD);
head = nh;
visible_len++;
if (!(slow && running)) {
wmove(tv, head->y, head->x);
1995-10-18 01:37:01 -07:00
wrefresh(tv);
}
return (1);
1995-10-18 01:37:01 -07:00
}
1998-09-15 17:44:36 -07:00
struct body *
2004-11-29 00:52:28 -08:00
newlink(void)
1998-09-15 17:44:36 -07:00
{
struct body *tmp;
if ((tmp = malloc(sizeof (struct body))) == NULL) {
1998-09-15 17:44:36 -07:00
endwin();
errx(1, "out of memory");
}
return (tmp);
}
1998-02-08 10:59:56 -08:00
void
2004-11-29 00:52:28 -08:00
crash(void)
1995-10-18 01:37:01 -07:00
{
sleep(2);
clear();
1998-02-08 10:59:56 -08:00
endwin();
1995-10-18 01:37:01 -07:00
printf("Well, you ran into something and the game is over.\n");
printf("Your final score was %d\n", score);
1998-02-08 10:59:56 -08:00
exit(0); /* leave() calls endwin(), which would hose the printf()'s */
1995-10-18 01:37:01 -07:00
}
void
2003-05-19 00:58:00 -07:00
suspend(int dummy)
1995-10-18 01:37:01 -07:00
{
wantsuspend = 1;
1995-10-18 01:37:01 -07:00
}
1998-02-08 10:59:56 -08:00
void
2004-11-29 00:52:28 -08:00
setup(void)
1995-10-18 01:37:01 -07:00
{
clear();
refresh();
touchwin(stw);
wrefresh(stw);
touchwin(tv);
wrefresh(tv);
}