xref: /openbsd-src/games/hack/hack.worn.c (revision 043fbe51c197dbbcd422e917b65f765d8b5f8874)
1*043fbe51Sderaadt /*	$OpenBSD: hack.worn.c,v 1.5 2009/10/27 23:59:25 deraadt Exp $	*/
2d0b779f3Sniklas 
3df930be7Sderaadt /*
4d25013f2Scamield  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5d25013f2Scamield  * Amsterdam
6d25013f2Scamield  * All rights reserved.
7d25013f2Scamield  *
8d25013f2Scamield  * Redistribution and use in source and binary forms, with or without
9d25013f2Scamield  * modification, are permitted provided that the following conditions are
10d25013f2Scamield  * met:
11d25013f2Scamield  *
12d25013f2Scamield  * - Redistributions of source code must retain the above copyright notice,
13d25013f2Scamield  * this list of conditions and the following disclaimer.
14d25013f2Scamield  *
15d25013f2Scamield  * - Redistributions in binary form must reproduce the above copyright
16d25013f2Scamield  * notice, this list of conditions and the following disclaimer in the
17d25013f2Scamield  * documentation and/or other materials provided with the distribution.
18d25013f2Scamield  *
19d25013f2Scamield  * - Neither the name of the Stichting Centrum voor Wiskunde en
20d25013f2Scamield  * Informatica, nor the names of its contributors may be used to endorse or
21d25013f2Scamield  * promote products derived from this software without specific prior
22d25013f2Scamield  * written permission.
23d25013f2Scamield  *
24d25013f2Scamield  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25d25013f2Scamield  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26d25013f2Scamield  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27d25013f2Scamield  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28d25013f2Scamield  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29d25013f2Scamield  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30d25013f2Scamield  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31d25013f2Scamield  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32d25013f2Scamield  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33d25013f2Scamield  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34d25013f2Scamield  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35d25013f2Scamield  */
36d25013f2Scamield 
37d25013f2Scamield /*
38d25013f2Scamield  * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39d25013f2Scamield  * All rights reserved.
40d25013f2Scamield  *
41d25013f2Scamield  * Redistribution and use in source and binary forms, with or without
42d25013f2Scamield  * modification, are permitted provided that the following conditions
43d25013f2Scamield  * are met:
44d25013f2Scamield  * 1. Redistributions of source code must retain the above copyright
45d25013f2Scamield  *    notice, this list of conditions and the following disclaimer.
46d25013f2Scamield  * 2. Redistributions in binary form must reproduce the above copyright
47d25013f2Scamield  *    notice, this list of conditions and the following disclaimer in the
48d25013f2Scamield  *    documentation and/or other materials provided with the distribution.
49d25013f2Scamield  * 3. The name of the author may not be used to endorse or promote products
50d25013f2Scamield  *    derived from this software without specific prior written permission.
51d25013f2Scamield  *
52d25013f2Scamield  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53d25013f2Scamield  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54d25013f2Scamield  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
55d25013f2Scamield  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56d25013f2Scamield  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57d25013f2Scamield  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58d25013f2Scamield  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59d25013f2Scamield  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60d25013f2Scamield  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61d25013f2Scamield  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62df930be7Sderaadt  */
63df930be7Sderaadt 
64df930be7Sderaadt #include "hack.h"
65df930be7Sderaadt 
66df930be7Sderaadt struct worn {
67df930be7Sderaadt 	long w_mask;
68df930be7Sderaadt 	struct obj **w_obj;
69df930be7Sderaadt } worn[] = {
70df930be7Sderaadt 	{ W_ARM, &uarm },
71df930be7Sderaadt 	{ W_ARM2, &uarm2 },
72df930be7Sderaadt 	{ W_ARMH, &uarmh },
73df930be7Sderaadt 	{ W_ARMS, &uarms },
74df930be7Sderaadt 	{ W_ARMG, &uarmg },
75df930be7Sderaadt 	{ W_RINGL, &uleft },
76df930be7Sderaadt 	{ W_RINGR, &uright },
77df930be7Sderaadt 	{ W_WEP, &uwep },
78df930be7Sderaadt 	{ W_BALL, &uball },
79df930be7Sderaadt 	{ W_CHAIN, &uchain },
80df930be7Sderaadt 	{ 0, 0 }
81df930be7Sderaadt };
82df930be7Sderaadt 
834a5fbbc4Spjanzen void
setworn(struct obj * obj,long mask)844a5fbbc4Spjanzen setworn(struct obj *obj, long mask)
85df930be7Sderaadt {
864a5fbbc4Spjanzen 	struct worn *wp;
874a5fbbc4Spjanzen 	struct obj *oobj;
88df930be7Sderaadt 
89df930be7Sderaadt 	for(wp = worn; wp->w_mask; wp++) if(wp->w_mask & mask) {
90df930be7Sderaadt 		oobj = *(wp->w_obj);
91df930be7Sderaadt 		if(oobj && !(oobj->owornmask & wp->w_mask))
92df930be7Sderaadt 			impossible("Setworn: mask = %ld.", wp->w_mask);
93df930be7Sderaadt 		if(oobj) oobj->owornmask &= ~wp->w_mask;
94df930be7Sderaadt 		if(obj && oobj && wp->w_mask == W_ARM){
95df930be7Sderaadt 			if(uarm2) {
96df930be7Sderaadt 				impossible("Setworn: uarm2 set?");
97df930be7Sderaadt 			} else
98df930be7Sderaadt 				setworn(uarm, W_ARM2);
99df930be7Sderaadt 		}
100df930be7Sderaadt 		*(wp->w_obj) = obj;
101df930be7Sderaadt 		if(obj) obj->owornmask |= wp->w_mask;
102df930be7Sderaadt 	}
103df930be7Sderaadt 	if(uarm2 && !uarm) {
104df930be7Sderaadt 		uarm = uarm2;
105df930be7Sderaadt 		uarm2 = 0;
106df930be7Sderaadt 		uarm->owornmask ^= (W_ARM | W_ARM2);
107df930be7Sderaadt 	}
108df930be7Sderaadt }
109df930be7Sderaadt 
110df930be7Sderaadt /* called e.g. when obj is destroyed */
1114a5fbbc4Spjanzen void
setnotworn(struct obj * obj)1124a5fbbc4Spjanzen setnotworn(struct obj *obj)
1134a5fbbc4Spjanzen {
1144a5fbbc4Spjanzen 	struct worn *wp;
115df930be7Sderaadt 
116df930be7Sderaadt 	for(wp = worn; wp->w_mask; wp++)
117df930be7Sderaadt 		if(obj == *(wp->w_obj)) {
118df930be7Sderaadt 			*(wp->w_obj) = 0;
119df930be7Sderaadt 			obj->owornmask &= ~wp->w_mask;
120df930be7Sderaadt 		}
121df930be7Sderaadt 	if(uarm2 && !uarm) {
122df930be7Sderaadt 		uarm = uarm2;
123df930be7Sderaadt 		uarm2 = 0;
124df930be7Sderaadt 		uarm->owornmask ^= (W_ARM | W_ARM2);
125df930be7Sderaadt 	}
126df930be7Sderaadt }
127