1*9b92b189Sdholland /* $NetBSD: hack.worn.c,v 1.7 2009/08/12 07:28:41 dholland Exp $ */
23ea4a95cSchristos
3210cab45Smycroft /*
41c7f94e5Sjsm * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
51c7f94e5Sjsm * Amsterdam
61c7f94e5Sjsm * All rights reserved.
71c7f94e5Sjsm *
81c7f94e5Sjsm * Redistribution and use in source and binary forms, with or without
91c7f94e5Sjsm * modification, are permitted provided that the following conditions are
101c7f94e5Sjsm * met:
111c7f94e5Sjsm *
121c7f94e5Sjsm * - Redistributions of source code must retain the above copyright notice,
131c7f94e5Sjsm * this list of conditions and the following disclaimer.
141c7f94e5Sjsm *
151c7f94e5Sjsm * - Redistributions in binary form must reproduce the above copyright
161c7f94e5Sjsm * notice, this list of conditions and the following disclaimer in the
171c7f94e5Sjsm * documentation and/or other materials provided with the distribution.
181c7f94e5Sjsm *
191c7f94e5Sjsm * - Neither the name of the Stichting Centrum voor Wiskunde en
201c7f94e5Sjsm * Informatica, nor the names of its contributors may be used to endorse or
211c7f94e5Sjsm * promote products derived from this software without specific prior
221c7f94e5Sjsm * written permission.
231c7f94e5Sjsm *
241c7f94e5Sjsm * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
251c7f94e5Sjsm * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
261c7f94e5Sjsm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
271c7f94e5Sjsm * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
281c7f94e5Sjsm * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
291c7f94e5Sjsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
301c7f94e5Sjsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
311c7f94e5Sjsm * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
321c7f94e5Sjsm * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
331c7f94e5Sjsm * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
341c7f94e5Sjsm * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
351c7f94e5Sjsm */
361c7f94e5Sjsm
371c7f94e5Sjsm /*
381c7f94e5Sjsm * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
391c7f94e5Sjsm * All rights reserved.
401c7f94e5Sjsm *
411c7f94e5Sjsm * Redistribution and use in source and binary forms, with or without
421c7f94e5Sjsm * modification, are permitted provided that the following conditions
431c7f94e5Sjsm * are met:
441c7f94e5Sjsm * 1. Redistributions of source code must retain the above copyright
451c7f94e5Sjsm * notice, this list of conditions and the following disclaimer.
461c7f94e5Sjsm * 2. Redistributions in binary form must reproduce the above copyright
471c7f94e5Sjsm * notice, this list of conditions and the following disclaimer in the
481c7f94e5Sjsm * documentation and/or other materials provided with the distribution.
491c7f94e5Sjsm * 3. The name of the author may not be used to endorse or promote products
501c7f94e5Sjsm * derived from this software without specific prior written permission.
511c7f94e5Sjsm *
521c7f94e5Sjsm * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
531c7f94e5Sjsm * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
541c7f94e5Sjsm * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
551c7f94e5Sjsm * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
561c7f94e5Sjsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
571c7f94e5Sjsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
581c7f94e5Sjsm * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
591c7f94e5Sjsm * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
601c7f94e5Sjsm * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
611c7f94e5Sjsm * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62210cab45Smycroft */
63210cab45Smycroft
643ea4a95cSchristos #include <sys/cdefs.h>
65210cab45Smycroft #ifndef lint
66*9b92b189Sdholland __RCSID("$NetBSD: hack.worn.c,v 1.7 2009/08/12 07:28:41 dholland Exp $");
67210cab45Smycroft #endif /* not lint */
6861f28255Scgd
6961f28255Scgd #include "hack.h"
703ea4a95cSchristos #include "extern.h"
7161f28255Scgd
72*9b92b189Sdholland static struct worn {
7361f28255Scgd long w_mask;
7461f28255Scgd struct obj **w_obj;
7561f28255Scgd } worn[] = {
763ea4a95cSchristos {
773ea4a95cSchristos W_ARM, &uarm
783ea4a95cSchristos },
793ea4a95cSchristos {
803ea4a95cSchristos W_ARM2, &uarm2
813ea4a95cSchristos },
823ea4a95cSchristos {
833ea4a95cSchristos W_ARMH, &uarmh
843ea4a95cSchristos },
853ea4a95cSchristos {
863ea4a95cSchristos W_ARMS, &uarms
873ea4a95cSchristos },
883ea4a95cSchristos {
893ea4a95cSchristos W_ARMG, &uarmg
903ea4a95cSchristos },
913ea4a95cSchristos {
923ea4a95cSchristos W_RINGL, &uleft
933ea4a95cSchristos },
943ea4a95cSchristos {
953ea4a95cSchristos W_RINGR, &uright
963ea4a95cSchristos },
973ea4a95cSchristos {
983ea4a95cSchristos W_WEP, &uwep
993ea4a95cSchristos },
1003ea4a95cSchristos {
1013ea4a95cSchristos W_BALL, &uball
1023ea4a95cSchristos },
1033ea4a95cSchristos {
1043ea4a95cSchristos W_CHAIN, &uchain
1053ea4a95cSchristos },
1063ea4a95cSchristos {
1073ea4a95cSchristos 0, 0
1083ea4a95cSchristos }
10961f28255Scgd };
11061f28255Scgd
1113ea4a95cSchristos void
setworn(struct obj * obj,long mask)1121fa8a9a6Sdholland setworn(struct obj *obj, long mask)
11361f28255Scgd {
1143ea4a95cSchristos struct worn *wp;
1153ea4a95cSchristos struct obj *oobj;
11661f28255Scgd
1173ea4a95cSchristos for (wp = worn; wp->w_mask; wp++)
1183ea4a95cSchristos if (wp->w_mask & mask) {
11961f28255Scgd oobj = *(wp->w_obj);
12061f28255Scgd if (oobj && !(oobj->owornmask & wp->w_mask))
12161f28255Scgd impossible("Setworn: mask = %ld.", wp->w_mask);
1223ea4a95cSchristos if (oobj)
1233ea4a95cSchristos oobj->owornmask &= ~wp->w_mask;
12461f28255Scgd if (obj && oobj && wp->w_mask == W_ARM) {
12561f28255Scgd if (uarm2) {
12661f28255Scgd impossible("Setworn: uarm2 set?");
12761f28255Scgd } else
12861f28255Scgd setworn(uarm, W_ARM2);
12961f28255Scgd }
13061f28255Scgd *(wp->w_obj) = obj;
1313ea4a95cSchristos if (obj)
1323ea4a95cSchristos obj->owornmask |= wp->w_mask;
13361f28255Scgd }
13461f28255Scgd if (uarm2 && !uarm) {
13561f28255Scgd uarm = uarm2;
13661f28255Scgd uarm2 = 0;
13761f28255Scgd uarm->owornmask ^= (W_ARM | W_ARM2);
13861f28255Scgd }
13961f28255Scgd }
14061f28255Scgd
14161f28255Scgd /* called e.g. when obj is destroyed */
1423ea4a95cSchristos void
setnotworn(struct obj * obj)1431fa8a9a6Sdholland setnotworn(struct obj *obj)
1443ea4a95cSchristos {
1453ea4a95cSchristos struct worn *wp;
14661f28255Scgd
14761f28255Scgd for (wp = worn; wp->w_mask; wp++)
14861f28255Scgd if (obj == *(wp->w_obj)) {
14961f28255Scgd *(wp->w_obj) = 0;
15061f28255Scgd obj->owornmask &= ~wp->w_mask;
15161f28255Scgd }
15261f28255Scgd if (uarm2 && !uarm) {
15361f28255Scgd uarm = uarm2;
15461f28255Scgd uarm2 = 0;
15561f28255Scgd uarm->owornmask ^= (W_ARM | W_ARM2);
15661f28255Scgd }
15761f28255Scgd }
158