1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
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
|
/* $OpenBSD: worm.c,v 1.21 2003/06/03 03:01:42 millert Exp $ */
/*
* 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
* 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.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1980, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)worm.c 8.1 (Berkeley) 5/31/93";
#else
static char rcsid[] = "$OpenBSD: worm.c,v 1.21 2003/06/03 03:01:42 millert Exp $";
#endif
#endif /* not lint */
/*
* Worm. Written by Michael Toy
* UCSC
*/
#include <sys/types.h>
#include <ctype.h>
#include <curses.h>
#include <err.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#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;
int visible_len;
int lastch;
char outbuf[BUFSIZ];
volatile sig_atomic_t wantleave = 0;
volatile sig_atomic_t wantsuspend = 0;
void crash(void);
void display(struct body *, char);
void leave(int);
void life(void);
void newpos(struct body *);
struct body *newlink(void);
void process(int);
void prize(void);
int rnd(int);
void setup(void);
void suspend(int);
int
main(int argc, char **argv)
{
int retval;
struct timeval t, tod;
struct timezone tz;
fd_set rset;
FD_ZERO(&rset);
setbuf(stdout, outbuf);
srandomdev();
signal(SIGINT, leave);
signal(SIGQUIT, leave);
signal(SIGTSTP, suspend); /* process control signal */
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
slow = (baudrate() <= 1200);
clear();
if (COLS < 18 || LINES < 5) {
endwin();
errx(1, "screen too small");
}
if (argc == 2)
start_len = atoi(argv[1]);
if ((start_len <= 0) || (start_len > ((LINES-3) * (COLS-2)) / 3))
start_len = LENGTH;
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 (wantleave) {
endwin(); /* XXX signal race */
exit(0);
}
if (wantsuspend) {
move(LINES-1, 0);
refresh();
endwin();
fflush(stdout);
kill(getpid(), SIGSTOP);
signal(SIGTSTP, suspend);
cbreak();
noecho();
setup();
wantsuspend = 0;
}
if (running)
{
running--;
process(lastch);
}
else
{
/* fflush(stdout); */
/* Delay could be a command line option */
t.tv_sec = 1;
t.tv_usec = 0;
(void)gettimeofday(&tod, &tz);
FD_SET(STDIN_FILENO, &rset);
retval = select(STDIN_FILENO + 1, &rset, NULL, NULL, &t);
if (retval > 0)
process(getch());
else
process(lastch);
}
}
}
void
life()
{
struct body *bp, *np;
int i,j = 1;
head = newlink();
head->x = start_len % (COLS-5) + 2;
head->y = LINES / 2;
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;
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;
}
display(np, BODY);
}
tail = np;
tail->prev = NULL;
visible_len = start_len + 1;
}
void
display(struct body *pos, char chr)
{
wmove(tv, pos->y, pos->x);
waddch(tv, chr);
}
void
leave(int dummy)
{
wantleave = 1;
}
int
rnd(int range)
{
return random() % range;
}
void
newpos(struct body *bp)
{
if (visible_len == (LINES-3) * (COLS-3) - 1) {
endwin();
printf("\nYou won!\nYour final score was %d\n\n", score);
exit(0);
}
do {
bp->y = rnd(LINES-3)+ 1;
bp->x = rnd(COLS-3) + 1;
wmove(tv, bp->y, bp->x);
} while(winch(tv) != ' ');
}
void
prize()
{
int value;
value = rnd(9) + 1;
newpos(&goody);
waddch(tv, value+'0');
wrefresh(tv);
}
void
process(int ch)
{
int x,y;
struct body *nh;
x = head->x;
y = head->y;
switch(ch)
{
#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;
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;
case CNTRL('Z'): suspend(0); return;
case CNTRL('C'): crash(); return;
case CNTRL('D'): crash(); return;
case ERR: leave(0); return;
default: return;
}
lastch = ch;
if (growing == 0)
{
display(tail, ' ');
tail->next->prev = NULL;
nh = tail->next;
free(tail);
tail = nh;
visible_len--;
}
else growing--;
display(head, BODY);
wmove(tv, y, x);
if (isdigit(ch = winch(tv)))
{
growing += ch-'0';
prize();
score += growing;
running = 0;
wmove(stw, 0, COLS - 12);
wprintw(stw, "Score: %3d", score);
wrefresh(stw);
}
else if(ch != ' ') crash();
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);
wrefresh(tv);
}
}
struct body *
newlink()
{
struct body *tmp;
if ((tmp = (struct body *) malloc(sizeof (struct body))) == NULL) {
endwin();
errx(1, "out of memory");
}
return (tmp);
}
void
crash()
{
sleep(2);
clear();
endwin();
printf("Well, you ran into something and the game is over.\n");
printf("Your final score was %d\n", score);
exit(0); /* leave() calls endwin(), which would hose the printf()'s */
}
void
suspend(int dummy)
{
wantsuspend = 1;
}
void
setup()
{
clear();
refresh();
touchwin(stw);
wrefresh(stw);
touchwin(tv);
wrefresh(tv);
}
|