xref: /netbsd-src/games/battlestar/room.c (revision 1182a44c59cae4d586117d55eca24b4b8b173211)
1 /*	$NetBSD: room.c,v 1.15 2021/05/02 12:50:43 rillig Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)room.c	8.2 (Berkeley) 4/28/95";
36 #else
37 __RCSID("$NetBSD: room.c,v 1.15 2021/05/02 12:50:43 rillig Exp $");
38 #endif
39 #endif				/* not lint */
40 
41 #include "extern.h"
42 
43 void
writedes(void)44 writedes(void)
45 {
46 	int     compass;
47 	const char   *p;
48 	int     c;
49 
50 	printf("\n\t%s\n", location[position].name);
51 	if (beenthere[position] < ROOMDESC || verbose) {
52 		compass = NORTH;
53 		for (p = location[position].desc; (c = *p++) != 0;)
54 			if (c != '-' && c != '*' && c != '+') {
55 				if (c == '=')
56 					putchar('-');
57 				else
58 					putchar(c);
59 			} else {
60 				if (c != '*')
61 					printf("%s", truedirec(compass, c));
62 				compass++;
63 			}
64 	}
65 }
66 
67 void
printobjs(void)68 printobjs(void)
69 {
70 	unsigned int *p = location[position].objects;
71 	int     n;
72 
73 	printf("\n");
74 	for (n = 0; n < NUMOFOBJECTS; n++)
75 		if (testbit(p, n) && objdes[n])
76 			puts(objdes[n]);
77 }
78 
79 void
whichway(struct room here)80 whichway(struct room here)
81 {
82 	switch (direction) {
83 
84 	case NORTH:
85 		left = here.west;
86 		right = here.east;
87 		ahead = here.north;
88 		back = here.south;
89 		break;
90 
91 	case SOUTH:
92 		left = here.east;
93 		right = here.west;
94 		ahead = here.south;
95 		back = here.north;
96 		break;
97 
98 	case EAST:
99 		left = here.north;
100 		right = here.south;
101 		ahead = here.east;
102 		back = here.west;
103 		break;
104 
105 	case WEST:
106 		left = here.south;
107 		right = here.north;
108 		ahead = here.west;
109 		back = here.east;
110 		break;
111 
112 	}
113 }
114 
115 const char *
truedirec(int way,int option)116 truedirec(int way, int option)
117 {
118 	switch (way) {
119 
120 	case NORTH:
121 		switch (direction) {
122 		case NORTH:
123 			return ("ahead");
124 		case SOUTH:
125 			return (option == '+' ? "behind you" :
126 			    "back");
127 		case EAST:
128 			return ("left");
129 		case WEST:
130 			return ("right");
131 		}
132 		break;
133 
134 	case SOUTH:
135 		switch (direction) {
136 		case NORTH:
137 			return (option == '+' ? "behind you" :
138 			    "back");
139 		case SOUTH:
140 			return ("ahead");
141 		case EAST:
142 			return ("right");
143 		case WEST:
144 			return ("left");
145 		}
146 		break;
147 
148 	case EAST:
149 		switch (direction) {
150 		case NORTH:
151 			return ("right");
152 		case SOUTH:
153 			return ("left");
154 		case EAST:
155 			return ("ahead");
156 		case WEST:
157 			return (option == '+' ? "behind you" :
158 			    "back");
159 		}
160 		break;
161 
162 	case WEST:
163 		switch (direction) {
164 		case NORTH:
165 			return ("left");
166 		case SOUTH:
167 			return ("right");
168 		case EAST:
169 			return (option == '+' ? "behind you" :
170 			    "back");
171 		case WEST:
172 			return ("ahead");
173 		}
174 		break;
175 	}
176 
177 	printf("Error: room %d.  More than four directions wanted.",
178 	    position);
179 	return ("!!");
180 }
181 
182 void
newway(int thisway)183 newway(int thisway)
184 {
185 	switch (direction) {
186 
187 	case NORTH:
188 		switch (thisway) {
189 		case LEFT:
190 			direction = WEST;
191 			break;
192 		case RIGHT:
193 			direction = EAST;
194 			break;
195 		case BACK:
196 			direction = SOUTH;
197 			break;
198 		}
199 		break;
200 	case SOUTH:
201 		switch (thisway) {
202 		case LEFT:
203 			direction = EAST;
204 			break;
205 		case RIGHT:
206 			direction = WEST;
207 			break;
208 		case BACK:
209 			direction = NORTH;
210 			break;
211 		}
212 		break;
213 	case EAST:
214 		switch (thisway) {
215 		case LEFT:
216 			direction = NORTH;
217 			break;
218 		case RIGHT:
219 			direction = SOUTH;
220 			break;
221 		case BACK:
222 			direction = WEST;
223 			break;
224 		}
225 		break;
226 	case WEST:
227 		switch (thisway) {
228 		case LEFT:
229 			direction = SOUTH;
230 			break;
231 		case RIGHT:
232 			direction = NORTH;
233 			break;
234 		case BACK:
235 			direction = EAST;
236 			break;
237 		}
238 		break;
239 	}
240 }
241