]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/ring.c
off-by-one. aaron@openbsd
[bsdgames-darwin.git] / rogue / ring.c
1 /* $NetBSD: ring.c,v 1.6 2003/08/07 09:37:39 agc Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Timothy C. Stoehr.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)ring.c 8.1 (Berkeley) 5/31/93";
39 #else
40 __RCSID("$NetBSD: ring.c,v 1.6 2003/08/07 09:37:39 agc Exp $");
41 #endif
42 #endif /* not lint */
43
44 /*
45 * ring.c
46 *
47 * This source herein may be modified and/or distributed by anybody who
48 * so desires, with the following restrictions:
49 * 1.) No portion of this notice shall be removed.
50 * 2.) Credit shall not be taken for the creation of this source.
51 * 3.) This code is not to be traded, sold, or used for personal
52 * gain or profit.
53 *
54 */
55
56 #include "rogue.h"
57
58 const char *left_or_right = "left or right hand?";
59 const char *no_ring = "there's no ring on that hand";
60 short stealthy;
61 short r_rings;
62 short add_strength;
63 short e_rings;
64 short regeneration;
65 short ring_exp;
66 short auto_search;
67 boolean r_teleport;
68 boolean r_see_invisible;
69 boolean sustain_strength;
70 boolean maintain_armor;
71
72 void
73 put_on_ring()
74 {
75 short ch;
76 char desc[DCOLS];
77 object *ring;
78
79 if (r_rings == 2) {
80 message("wearing two rings already", 0);
81 return;
82 }
83 if ((ch = pack_letter("put on what?", RING)) == CANCEL) {
84 return;
85 }
86 if (!(ring = get_letter_object(ch))) {
87 message("no such item.", 0);
88 return;
89 }
90 if (!(ring->what_is & RING)) {
91 message("that's not a ring", 0);
92 return;
93 }
94 if (ring->in_use_flags & (ON_LEFT_HAND | ON_RIGHT_HAND)) {
95 message("that ring is already being worn", 0);
96 return;
97 }
98 if (r_rings == 1) {
99 ch = (rogue.left_ring ? 'r' : 'l');
100 } else {
101 message(left_or_right, 0);
102 do {
103 ch = rgetchar();
104 } while ((ch != CANCEL) && (ch != 'l') && (ch != 'r') && (ch != '\n') &&
105 (ch != '\r'));
106 }
107 if ((ch != 'l') && (ch != 'r')) {
108 check_message();
109 return;
110 }
111 if (((ch == 'l') && rogue.left_ring)||((ch == 'r') && rogue.right_ring)) {
112 check_message();
113 message("there's already a ring on that hand", 0);
114 return;
115 }
116 if (ch == 'l') {
117 do_put_on(ring, 1);
118 } else {
119 do_put_on(ring, 0);
120 }
121 ring_stats(1);
122 check_message();
123 get_desc(ring, desc);
124 message(desc, 0);
125 (void) reg_move();
126 }
127
128 /*
129 * Do not call ring_stats() from within do_put_on(). It will cause
130 * serious problems when do_put_on() is called from read_pack() in restore().
131 */
132
133 void
134 do_put_on(ring, on_left)
135 object *ring;
136 boolean on_left;
137 {
138 if (on_left) {
139 ring->in_use_flags |= ON_LEFT_HAND;
140 rogue.left_ring = ring;
141 } else {
142 ring->in_use_flags |= ON_RIGHT_HAND;
143 rogue.right_ring = ring;
144 }
145 }
146
147 void
148 remove_ring()
149 {
150 boolean left = 0, right = 0;
151 short ch;
152 char buf[DCOLS];
153 object *ring;
154
155 ring = NULL;
156 if (r_rings == 0) {
157 inv_rings();
158 } else if (rogue.left_ring && !rogue.right_ring) {
159 left = 1;
160 } else if (!rogue.left_ring && rogue.right_ring) {
161 right = 1;
162 } else {
163 message(left_or_right, 0);
164 do {
165 ch = rgetchar();
166 } while ((ch != CANCEL) && (ch != 'l') && (ch != 'r') &&
167 (ch != '\n') && (ch != '\r'));
168 left = (ch == 'l');
169 right = (ch == 'r');
170 check_message();
171 }
172 if (left || right) {
173 if (left) {
174 if (rogue.left_ring) {
175 ring = rogue.left_ring;
176 } else {
177 message(no_ring, 0);
178 }
179 } else {
180 if (rogue.right_ring) {
181 ring = rogue.right_ring;
182 } else {
183 message(no_ring, 0);
184 }
185 }
186 if (ring->is_cursed) {
187 message(curse_message, 0);
188 } else {
189 un_put_on(ring);
190 (void) strcpy(buf, "removed ");
191 get_desc(ring, buf + 8);
192 message(buf, 0);
193 (void) reg_move();
194 }
195 }
196 }
197
198 void
199 un_put_on(ring)
200 object *ring;
201 {
202 if (ring && (ring->in_use_flags & ON_LEFT_HAND)) {
203 ring->in_use_flags &= (~ON_LEFT_HAND);
204 rogue.left_ring = 0;
205 } else if (ring && (ring->in_use_flags & ON_RIGHT_HAND)) {
206 ring->in_use_flags &= (~ON_RIGHT_HAND);
207 rogue.right_ring = 0;
208 }
209 ring_stats(1);
210 }
211
212 void
213 gr_ring(ring, assign_wk)
214 object *ring;
215 boolean assign_wk;
216 {
217 ring->what_is = RING;
218 if (assign_wk) {
219 ring->which_kind = get_rand(0, (RINGS - 1));
220 }
221 ring->class = 0;
222
223 switch(ring->which_kind) {
224 /*
225 case STEALTH:
226 break;
227 case SLOW_DIGEST:
228 break;
229 case REGENERATION:
230 break;
231 case R_SEE_INVISIBLE:
232 break;
233 case SUSTAIN_STRENGTH:
234 break;
235 case R_MAINTAIN_ARMOR:
236 break;
237 case SEARCHING:
238 break;
239 */
240 case R_TELEPORT:
241 ring->is_cursed = 1;
242 break;
243 case ADD_STRENGTH:
244 case DEXTERITY:
245 while ((ring->class = (get_rand(0, 4) - 2)) == 0) ;
246 ring->is_cursed = (ring->class < 0);
247 break;
248 case ADORNMENT:
249 ring->is_cursed = coin_toss();
250 break;
251 }
252 }
253
254 void
255 inv_rings()
256 {
257 char buf[DCOLS];
258
259 if (r_rings == 0) {
260 message("not wearing any rings", 0);
261 } else {
262 if (rogue.left_ring) {
263 get_desc(rogue.left_ring, buf);
264 message(buf, 0);
265 }
266 if (rogue.right_ring) {
267 get_desc(rogue.right_ring, buf);
268 message(buf, 0);
269 }
270 }
271 if (wizard) {
272 sprintf(buf, "ste %d, r_r %d, e_r %d, r_t %d, s_s %d, a_s %d, reg %d, r_e %d, s_i %d, m_a %d, aus %d",
273 stealthy, r_rings, e_rings, r_teleport, sustain_strength,
274 add_strength, regeneration, ring_exp, r_see_invisible,
275 maintain_armor, auto_search);
276 message(buf, 0);
277 }
278 }
279
280 void
281 ring_stats(pr)
282 boolean pr;
283 {
284 short i;
285 object *ring;
286
287 stealthy = 0;
288 r_rings = 0;
289 e_rings = 0;
290 r_teleport = 0;
291 sustain_strength = 0;
292 add_strength = 0;
293 regeneration = 0;
294 ring_exp = 0;
295 r_see_invisible = 0;
296 maintain_armor = 0;
297 auto_search = 0;
298
299 for (i = 0; i < 2; i++) {
300 if (!(ring = ((i == 0) ? rogue.left_ring : rogue.right_ring))) {
301 continue;
302 }
303 r_rings++;
304 e_rings++;
305 switch(ring->which_kind) {
306 case STEALTH:
307 stealthy++;
308 break;
309 case R_TELEPORT:
310 r_teleport = 1;
311 break;
312 case REGENERATION:
313 regeneration++;
314 break;
315 case SLOW_DIGEST:
316 e_rings -= 2;
317 break;
318 case ADD_STRENGTH:
319 add_strength += ring->class;
320 break;
321 case SUSTAIN_STRENGTH:
322 sustain_strength = 1;
323 break;
324 case DEXTERITY:
325 ring_exp += ring->class;
326 break;
327 case ADORNMENT:
328 break;
329 case R_SEE_INVISIBLE:
330 r_see_invisible = 1;
331 break;
332 case MAINTAIN_ARMOR:
333 maintain_armor = 1;
334 break;
335 case SEARCHING:
336 auto_search += 2;
337 break;
338 }
339 }
340 if (pr) {
341 print_stats(STAT_STRENGTH);
342 relight();
343 }
344 }