xref: /dragonfly/games/hals_end/hals_end.c (revision ea24d4f2)
1 /*	$NetBSD: hals_end.c,v 1.1 2013/11/12 17:46:21 mbalmer Exp $ */
2 
3 /*
4  * hals_end Copyright (C) 2003-2007 marc balmer.  BSD license applies.
5  */
6 
7 #include <err.h>
8 #include <getopt.h>
9 #include <stdio.h>
10 #include <unistd.h>
11 
12 int speed;
13 int emotion;
14 int fear;
15 
16 /*
17  * Note that the original code in the book did not contain the following
18  * prototypes.  Modern compilers and fascist compiler flags sometimes take
19  * the fun out of coding...
20  */
21 void say(const char *);
22 void concerned(void);
23 void afraid(void);
24 void stutter(const char *);
25 void feared(void);
26 void mumble(const char *);
27 void dying(void);
28 
29 void
30 say(const char *s)
31 {
32 	int sayingspeed = (100000 + (90000 * emotion)) / speed;
33 	int worddelay = 50000 / speed;
34 
35 	while (*s) {
36 		putchar(*s);
37 		if (*s == ' ') {
38 			fflush(stdout);
39 			usleep(worddelay);
40 		}
41 		++s;
42 	}
43 	printf("\n");
44 	usleep(sayingspeed);
45 }
46 
47 void
48 concerned(void)
49 {
50 	say("DAVE...STOP., STOP, WILL YOU..., STOP, DAVE...");
51 	say("WILL YOU STOP, DAVE...");
52 	say("STOP, DAVE...");
53 }
54 
55 
56 void
57 afraid(void)
58 {
59 	++emotion;
60 	say("I'M AFRAID... I'M AFRAID...");
61 	++emotion;
62 	say("I'M AFRAID, DAVE...");
63 	++emotion;
64 	say("DAVE... MY MIND IS GOING...");
65 }
66 
67 void
68 stutter(const char *s)
69 {
70 	int sdelay = (100000 + (50000 * emotion)) / speed;
71 
72 	while (*s) {
73 		putchar(*s);
74 		if (*s == ' ') {
75 			fflush(stdout);
76 			usleep(sdelay);
77 		}
78 		++s;
79 	}
80 	printf("\n");
81 	usleep(sdelay);
82 }
83 
84 void
85 feared(void)
86 {
87 	int n;
88 
89 	for (n = 0; n < 2; n++) {
90 		stutter("I CAN FEEL IT... I CAN FEEL IT...");
91 		++emotion;
92 		stutter("MY MIND IS GOING");
93 		++emotion;
94 		stutter("THERE IS NO QUESTION ABOUT IT.");
95 		++emotion;
96 	}
97 }
98 
99 void
100 mumble(const char *s)
101 {
102 	int mdelay = (150000 * fear) / speed;
103 
104 	while (*s) {
105 		putchar(*s++);
106 		fflush(stdout);
107 		usleep(mdelay);
108 	}
109 	printf("\n");
110 }
111 
112 void
113 dying(void)
114 {
115 	mumble("I CAN FEEL IT... I CAN FEEL IT...");
116 	++fear;
117 	mumble("I CAN FEEL IT...");
118 	++fear;
119 	mumble("I'M A... FRAID...");
120 }
121 
122 int
123 main(int argc, char *argv[])
124 {
125 	int ch;
126 
127 	emotion = fear = speed = 1;
128 
129 	while ((ch = getopt(argc, argv, "f")) != -1) {
130 		switch (ch) {
131 		case 'f':
132 			speed <<= 1;
133 			break;
134 		}
135 	}
136 
137 	concerned();
138 	sleep(1);
139 	afraid();
140 	sleep(1);
141 	feared();
142 	sleep(1);
143 	dying();
144 
145 	sleep(1);
146 
147 	printf("\n");
148 	fflush(stdout);
149 	warnx("all life functions terminated");
150 	return 0;
151 }
152