xref: /dragonfly/games/rogue/ring.c (revision f746689a)
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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * @(#)ring.c	8.1 (Berkeley) 5/31/93
37  * $FreeBSD: src/games/rogue/ring.c,v 1.3 1999/11/30 03:49:26 billf Exp $
38  * $DragonFly: src/games/rogue/ring.c,v 1.3 2006/09/02 19:31:07 pavalos Exp $
39  */
40 
41 /*
42  * ring.c
43  *
44  * This source herein may be modified and/or distributed by anybody who
45  * so desires, with the following restrictions:
46  *    1.)  No portion of this notice shall be removed.
47  *    2.)  Credit shall not be taken for the creation of this source.
48  *    3.)  This code is not to be traded, sold, or used for personal
49  *         gain or profit.
50  *
51  */
52 
53 #include "rogue.h"
54 
55 const char *left_or_right = "left or right hand?";
56 const char *no_ring = "there's no ring on that hand";
57 short stealthy;
58 short r_rings;
59 short add_strength;
60 short e_rings;
61 short regeneration;
62 short ring_exp;
63 short auto_search;
64 boolean r_teleport;
65 boolean r_see_invisible;
66 boolean sustain_strength;
67 boolean maintain_armor;
68 
69 extern char *curse_message;
70 extern boolean wizard;
71 
72 void
73 put_on_ring(void)
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 	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(object *ring, boolean on_left)
135 {
136 	if (on_left) {
137 		ring->in_use_flags |= ON_LEFT_HAND;
138 		rogue.left_ring = ring;
139 	} else {
140 		ring->in_use_flags |= ON_RIGHT_HAND;
141 		rogue.right_ring = ring;
142 	}
143 }
144 
145 void
146 remove_ring(void)
147 {
148 	boolean left = 0, right = 0;
149 	short ch;
150 	char buf[DCOLS];
151 	object *ring = NULL;
152 
153 	if (r_rings == 0) {
154 		inv_rings();
155 	} else if (rogue.left_ring && !rogue.right_ring) {
156 		left = 1;
157 	} else if (!rogue.left_ring && rogue.right_ring) {
158 		right = 1;
159 	} else {
160 		message(left_or_right, 0);
161 		do {
162 			ch = rgetchar();
163 		} while ((ch != CANCEL) && (ch != 'l') && (ch != 'r') &&
164 			(ch != '\n') && (ch != '\r'));
165 		left = (ch == 'l');
166 		right = (ch == 'r');
167 		check_message();
168 	}
169 	if (left || right) {
170 		if (left) {
171 			if (rogue.left_ring) {
172 				ring = rogue.left_ring;
173 			} else {
174 				message(no_ring, 0);
175 			}
176 		} else {
177 			if (rogue.right_ring) {
178 				ring = rogue.right_ring;
179 			} else {
180 				message(no_ring, 0);
181 			}
182 		}
183 		if (ring->is_cursed) {
184 			message(curse_message, 0);
185 		} else {
186 			un_put_on(ring);
187 			strcpy(buf, "removed ");
188 			get_desc(ring, buf + 8);
189 			message(buf, 0);
190 			reg_move();
191 		}
192 	}
193 }
194 
195 void
196 un_put_on(object *ring)
197 {
198 	if (ring && (ring->in_use_flags & ON_LEFT_HAND)) {
199 		ring->in_use_flags &= (~ON_LEFT_HAND);
200 		rogue.left_ring = 0;
201 	} else if (ring && (ring->in_use_flags & ON_RIGHT_HAND)) {
202 		ring->in_use_flags &= (~ON_RIGHT_HAND);
203 		rogue.right_ring = 0;
204 	}
205 	ring_stats(1);
206 }
207 
208 void
209 gr_ring(object *ring, boolean assign_wk)
210 {
211 	ring->what_is = RING;
212 	if (assign_wk) {
213 		ring->which_kind = get_rand(0, (RINGS - 1));
214 	}
215 	ring->class = 0;
216 
217 	switch(ring->which_kind) {
218 	case R_TELEPORT:
219 		ring->is_cursed = 1;
220 		break;
221 	case ADD_STRENGTH:
222 	case DEXTERITY:
223 		while ((ring->class = (get_rand(0, 4) - 2)) == 0) ;
224 		ring->is_cursed = (ring->class < 0);
225 		break;
226 	case ADORNMENT:
227 		ring->is_cursed = coin_toss();
228 		break;
229 	}
230 }
231 
232 void
233 inv_rings(void)
234 {
235 	char buf[DCOLS];
236 
237 	if (r_rings == 0) {
238 		message("not wearing any rings", 0);
239 	} else {
240 		if (rogue.left_ring) {
241 			get_desc(rogue.left_ring, buf);
242 			message(buf, 0);
243 		}
244 		if (rogue.right_ring) {
245 			get_desc(rogue.right_ring, buf);
246 			message(buf, 0);
247 		}
248 	}
249 	if (wizard) {
250 		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",
251 			stealthy, r_rings, e_rings, r_teleport, sustain_strength,
252 			add_strength, regeneration, ring_exp, r_see_invisible,
253 			maintain_armor, auto_search);
254 		message(buf, 0);
255 	}
256 }
257 
258 void
259 ring_stats(boolean pr)
260 {
261 	short i;
262 	object *ring;
263 
264 	stealthy = 0;
265 	r_rings = 0;
266 	e_rings = 0;
267 	r_teleport = 0;
268 	sustain_strength = 0;
269 	add_strength = 0;
270 	regeneration = 0;
271 	ring_exp = 0;
272 	r_see_invisible = 0;
273 	maintain_armor = 0;
274 	auto_search = 0;
275 
276 	for (i = 0; i < 2; i++) {
277 		if (!(ring = ((i == 0) ? rogue.left_ring : rogue.right_ring))) {
278 			continue;
279 		}
280 		r_rings++;
281 		e_rings++;
282 		switch(ring->which_kind) {
283 		case STEALTH:
284 			stealthy++;
285 			break;
286 		case R_TELEPORT:
287 			r_teleport = 1;
288 			break;
289 		case REGENERATION:
290 			regeneration++;
291 			break;
292 		case SLOW_DIGEST:
293 			e_rings -= 2;
294 			break;
295 		case ADD_STRENGTH:
296 			add_strength += ring->class;
297 			break;
298 		case SUSTAIN_STRENGTH:
299 			sustain_strength = 1;
300 			break;
301 		case DEXTERITY:
302 			ring_exp += ring->class;
303 			break;
304 		case ADORNMENT:
305 			break;
306 		case R_SEE_INVISIBLE:
307 			r_see_invisible = 1;
308 			break;
309 		case MAINTAIN_ARMOR:
310 			maintain_armor = 1;
311 			break;
312 		case SEARCHING:
313 			auto_search += 2;
314 			break;
315 		}
316 	}
317 	if (pr) {
318 		print_stats(STAT_STRENGTH);
319 		relight();
320 	}
321 }
322