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