Home | History | Annotate | Line # | Download | only in hack
hack.engrave.c revision 1.7
      1  1.7  dholland /*	$NetBSD: hack.engrave.c,v 1.7 2009/06/07 18:30:39 dholland Exp $	*/
      2  1.4  christos 
      3  1.2   mycroft /*
      4  1.6       jsm  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
      5  1.6       jsm  * Amsterdam
      6  1.6       jsm  * All rights reserved.
      7  1.6       jsm  *
      8  1.6       jsm  * Redistribution and use in source and binary forms, with or without
      9  1.6       jsm  * modification, are permitted provided that the following conditions are
     10  1.6       jsm  * met:
     11  1.6       jsm  *
     12  1.6       jsm  * - Redistributions of source code must retain the above copyright notice,
     13  1.6       jsm  * this list of conditions and the following disclaimer.
     14  1.6       jsm  *
     15  1.6       jsm  * - Redistributions in binary form must reproduce the above copyright
     16  1.6       jsm  * notice, this list of conditions and the following disclaimer in the
     17  1.6       jsm  * documentation and/or other materials provided with the distribution.
     18  1.6       jsm  *
     19  1.6       jsm  * - Neither the name of the Stichting Centrum voor Wiskunde en
     20  1.6       jsm  * Informatica, nor the names of its contributors may be used to endorse or
     21  1.6       jsm  * promote products derived from this software without specific prior
     22  1.6       jsm  * written permission.
     23  1.6       jsm  *
     24  1.6       jsm  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     25  1.6       jsm  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  1.6       jsm  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     27  1.6       jsm  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     28  1.6       jsm  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     29  1.6       jsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     30  1.6       jsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     31  1.6       jsm  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     32  1.6       jsm  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     33  1.6       jsm  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     34  1.6       jsm  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35  1.6       jsm  */
     36  1.6       jsm 
     37  1.6       jsm /*
     38  1.6       jsm  * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
     39  1.6       jsm  * All rights reserved.
     40  1.6       jsm  *
     41  1.6       jsm  * Redistribution and use in source and binary forms, with or without
     42  1.6       jsm  * modification, are permitted provided that the following conditions
     43  1.6       jsm  * are met:
     44  1.6       jsm  * 1. Redistributions of source code must retain the above copyright
     45  1.6       jsm  *    notice, this list of conditions and the following disclaimer.
     46  1.6       jsm  * 2. Redistributions in binary form must reproduce the above copyright
     47  1.6       jsm  *    notice, this list of conditions and the following disclaimer in the
     48  1.6       jsm  *    documentation and/or other materials provided with the distribution.
     49  1.6       jsm  * 3. The name of the author may not be used to endorse or promote products
     50  1.6       jsm  *    derived from this software without specific prior written permission.
     51  1.6       jsm  *
     52  1.6       jsm  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     53  1.6       jsm  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     54  1.6       jsm  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
     55  1.6       jsm  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     56  1.6       jsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     57  1.6       jsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     58  1.6       jsm  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     59  1.6       jsm  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     60  1.6       jsm  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     61  1.6       jsm  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62  1.2   mycroft  */
     63  1.2   mycroft 
     64  1.4  christos #include <sys/cdefs.h>
     65  1.2   mycroft #ifndef lint
     66  1.7  dholland __RCSID("$NetBSD: hack.engrave.c,v 1.7 2009/06/07 18:30:39 dholland Exp $");
     67  1.4  christos #endif				/* not lint */
     68  1.1       cgd 
     69  1.4  christos #include <stdlib.h>
     70  1.4  christos #include "hack.h"
     71  1.4  christos #include "extern.h"
     72  1.1       cgd 
     73  1.1       cgd struct engr {
     74  1.4  christos 	struct engr    *nxt_engr;
     75  1.4  christos 	char           *engr_txt;
     76  1.4  christos 	xchar           engr_x, engr_y;
     77  1.4  christos 	unsigned        engr_lth;	/* for save & restore; not length of
     78  1.4  christos 					 * text */
     79  1.4  christos 	long            engr_time;	/* moment engraving was (will be)
     80  1.4  christos 					 * finished */
     81  1.4  christos 	xchar           engr_type;
     82  1.1       cgd #define	DUST	1
     83  1.1       cgd #define	ENGRAVE	2
     84  1.1       cgd #define	BURN	3
     85  1.4  christos }              *head_engr;
     86  1.1       cgd 
     87  1.4  christos struct engr    *
     88  1.7  dholland engr_at(xchar x, xchar y)
     89  1.4  christos {
     90  1.4  christos 	struct engr    *ep = head_engr;
     91  1.4  christos 	while (ep) {
     92  1.4  christos 		if (x == ep->engr_x && y == ep->engr_y)
     93  1.4  christos 			return (ep);
     94  1.1       cgd 		ep = ep->nxt_engr;
     95  1.1       cgd 	}
     96  1.4  christos 	return ((struct engr *) 0);
     97  1.1       cgd }
     98  1.1       cgd 
     99  1.4  christos int
    100  1.7  dholland sengr_at(const char *s, xchar x, xchar y)
    101  1.4  christos {
    102  1.4  christos 	struct engr    *ep = engr_at(x, y);
    103  1.4  christos 	char           *t;
    104  1.4  christos 	int             n;
    105  1.4  christos 	if (ep && ep->engr_time <= moves) {
    106  1.1       cgd 		t = ep->engr_txt;
    107  1.4  christos 		/*
    108  1.4  christos 				if(!strcmp(s,t)) return(1);
    109  1.4  christos 		*/
    110  1.1       cgd 		n = strlen(s);
    111  1.4  christos 		while (*t) {
    112  1.4  christos 			if (!strncmp(s, t, n))
    113  1.4  christos 				return (1);
    114  1.1       cgd 			t++;
    115  1.1       cgd 		}
    116  1.1       cgd 	}
    117  1.4  christos 	return (0);
    118  1.1       cgd }
    119  1.1       cgd 
    120  1.4  christos void
    121  1.7  dholland u_wipe_engr(int cnt)
    122  1.1       cgd {
    123  1.4  christos 	if (!u.uswallow && !Levitation)
    124  1.1       cgd 		wipe_engr_at(u.ux, u.uy, cnt);
    125  1.1       cgd }
    126  1.1       cgd 
    127  1.4  christos void
    128  1.7  dholland wipe_engr_at(xchar x, xchar y, xchar cnt)
    129  1.4  christos {
    130  1.4  christos 	struct engr    *ep = engr_at(x, y);
    131  1.4  christos 	int             lth, pos;
    132  1.4  christos 	char            ch;
    133  1.4  christos 	if (ep) {
    134  1.4  christos 		if ((ep->engr_type != DUST) || Levitation) {
    135  1.4  christos 			cnt = rn2(1 + 50 / (cnt + 1)) ? 0 : 1;
    136  1.1       cgd 		}
    137  1.1       cgd 		lth = strlen(ep->engr_txt);
    138  1.4  christos 		if (lth && cnt > 0) {
    139  1.4  christos 			while (cnt--) {
    140  1.1       cgd 				pos = rn2(lth);
    141  1.4  christos 				if ((ch = ep->engr_txt[pos]) == ' ')
    142  1.1       cgd 					continue;
    143  1.1       cgd 				ep->engr_txt[pos] = (ch != '?') ? '?' : ' ';
    144  1.1       cgd 			}
    145  1.1       cgd 		}
    146  1.4  christos 		while (lth && ep->engr_txt[lth - 1] == ' ')
    147  1.1       cgd 			ep->engr_txt[--lth] = 0;
    148  1.4  christos 		while (ep->engr_txt[0] == ' ')
    149  1.1       cgd 			ep->engr_txt++;
    150  1.4  christos 		if (!ep->engr_txt[0])
    151  1.4  christos 			del_engr(ep);
    152  1.1       cgd 	}
    153  1.1       cgd }
    154  1.1       cgd 
    155  1.4  christos void
    156  1.7  dholland read_engr_at(int x, int y)
    157  1.4  christos {
    158  1.4  christos 	struct engr    *ep = engr_at(x, y);
    159  1.4  christos 	if (ep && ep->engr_txt[0]) {
    160  1.4  christos 		switch (ep->engr_type) {
    161  1.4  christos 		case DUST:
    162  1.4  christos 			pline("Something is written here in the dust.");
    163  1.4  christos 			break;
    164  1.4  christos 		case ENGRAVE:
    165  1.4  christos 			pline("Something is engraved here on the floor.");
    166  1.4  christos 			break;
    167  1.4  christos 		case BURN:
    168  1.4  christos 			pline("Some text has been burned here in the floor.");
    169  1.4  christos 			break;
    170  1.4  christos 		default:
    171  1.4  christos 			impossible("Something is written in a very strange way.");
    172  1.4  christos 		}
    173  1.4  christos 		pline("You read: \"%s\".", ep->engr_txt);
    174  1.1       cgd 	}
    175  1.1       cgd }
    176  1.1       cgd 
    177  1.4  christos void
    178  1.7  dholland make_engr_at(int x, int y, const char *s)
    179  1.1       cgd {
    180  1.4  christos 	struct engr    *ep;
    181  1.1       cgd 
    182  1.4  christos 	if ((ep = engr_at(x, y)) != NULL)
    183  1.4  christos 		del_engr(ep);
    184  1.1       cgd 	ep = (struct engr *)
    185  1.4  christos 		alloc((unsigned) (sizeof(struct engr) + strlen(s) + 1));
    186  1.1       cgd 	ep->nxt_engr = head_engr;
    187  1.1       cgd 	head_engr = ep;
    188  1.1       cgd 	ep->engr_x = x;
    189  1.1       cgd 	ep->engr_y = y;
    190  1.4  christos 	ep->engr_txt = (char *) (ep + 1);
    191  1.1       cgd 	(void) strcpy(ep->engr_txt, s);
    192  1.1       cgd 	ep->engr_time = 0;
    193  1.1       cgd 	ep->engr_type = DUST;
    194  1.1       cgd 	ep->engr_lth = strlen(s) + 1;
    195  1.1       cgd }
    196  1.1       cgd 
    197  1.4  christos int
    198  1.7  dholland doengrave(void)
    199  1.4  christos {
    200  1.4  christos 	int             len;
    201  1.4  christos 	char           *sp;
    202  1.4  christos 	struct engr    *ep, *oep = engr_at(u.ux, u.uy);
    203  1.4  christos 	char            buf[BUFSZ];
    204  1.4  christos 	xchar           type;
    205  1.4  christos 	int             spct;	/* number of leading spaces */
    206  1.4  christos 	struct obj     *otmp;
    207  1.1       cgd 	multi = 0;
    208  1.1       cgd 
    209  1.4  christos 	if (u.uswallow) {
    210  1.1       cgd 		pline("You're joking. Hahaha!");	/* riv05!a3 */
    211  1.4  christos 		return (0);
    212  1.1       cgd 	}
    213  1.1       cgd 	/* one may write with finger, weapon or wand */
    214  1.1       cgd 	otmp = getobj("#-)/", "write with");
    215  1.4  christos 	if (!otmp)
    216  1.4  christos 		return (0);
    217  1.1       cgd 
    218  1.4  christos 	if (otmp == &zeroobj)
    219  1.1       cgd 		otmp = 0;
    220  1.4  christos 	if (otmp && otmp->otyp == WAN_FIRE && otmp->spe) {
    221  1.1       cgd 		type = BURN;
    222  1.1       cgd 		otmp->spe--;
    223  1.1       cgd 	} else {
    224  1.1       cgd 		/* first wield otmp */
    225  1.4  christos 		if (otmp != uwep) {
    226  1.4  christos 			if (uwep && uwep->cursed) {
    227  1.4  christos 				/* Andreas Bormann */
    228  1.4  christos 				pline("Since your weapon is welded to your hand,");
    229  1.4  christos 				pline("you use the %s.", aobjnam(uwep, (char *) 0));
    230  1.4  christos 				otmp = uwep;
    231  1.1       cgd 			} else {
    232  1.4  christos 				if (!otmp)
    233  1.4  christos 					pline("You are now empty-handed.");
    234  1.4  christos 				else if (otmp->cursed)
    235  1.4  christos 					pline("The %s %s to your hand!",
    236  1.4  christos 					      aobjnam(otmp, "weld"),
    237  1.4  christos 					      (otmp->quan == 1) ? "itself" : "themselves");
    238  1.4  christos 				else
    239  1.4  christos 					pline("You now wield %s.", doname(otmp));
    240  1.4  christos 				setuwep(otmp);
    241  1.1       cgd 			}
    242  1.1       cgd 		}
    243  1.4  christos 		if (!otmp)
    244  1.1       cgd 			type = DUST;
    245  1.4  christos 		else if (otmp->otyp == DAGGER || otmp->otyp == TWO_HANDED_SWORD ||
    246  1.4  christos 			 otmp->otyp == CRYSKNIFE ||
    247  1.4  christos 			 otmp->otyp == LONG_SWORD || otmp->otyp == AXE) {
    248  1.1       cgd 			type = ENGRAVE;
    249  1.4  christos 			if ((int) otmp->spe <= -3) {
    250  1.1       cgd 				type = DUST;
    251  1.1       cgd 				pline("Your %s too dull for engraving.",
    252  1.4  christos 				      aobjnam(otmp, "are"));
    253  1.4  christos 				if (oep && oep->engr_type != DUST)
    254  1.4  christos 					return (1);
    255  1.1       cgd 			}
    256  1.4  christos 		} else
    257  1.4  christos 			type = DUST;
    258  1.1       cgd 	}
    259  1.4  christos 	if (Levitation && type != BURN) {	/* riv05!a3 */
    260  1.1       cgd 		pline("You can't reach the floor!");
    261  1.4  christos 		return (1);
    262  1.4  christos 	}
    263  1.4  christos 	if (oep && oep->engr_type == DUST) {
    264  1.4  christos 		pline("You wipe out the message that was written here.");
    265  1.4  christos 		del_engr(oep);
    266  1.4  christos 		oep = 0;
    267  1.1       cgd 	}
    268  1.4  christos 	if (type == DUST && oep) {
    269  1.4  christos 		pline("You cannot wipe out the message that is %s in the rock.",
    270  1.4  christos 		      (oep->engr_type == BURN) ? "burned" : "engraved");
    271  1.4  christos 		return (1);
    272  1.1       cgd 	}
    273  1.1       cgd 	pline("What do you want to %s on the floor here? ",
    274  1.4  christos 	 (type == ENGRAVE) ? "engrave" : (type == BURN) ? "burn" : "write");
    275  1.1       cgd 	getlin(buf);
    276  1.1       cgd 	clrlin();
    277  1.1       cgd 	spct = 0;
    278  1.1       cgd 	sp = buf;
    279  1.4  christos 	while (*sp == ' ')
    280  1.4  christos 		spct++, sp++;
    281  1.1       cgd 	len = strlen(sp);
    282  1.4  christos 	if (!len || *buf == '\033') {
    283  1.4  christos 		if (type == BURN)
    284  1.4  christos 			otmp->spe++;
    285  1.4  christos 		return (0);
    286  1.1       cgd 	}
    287  1.4  christos 	switch (type) {
    288  1.1       cgd 	case DUST:
    289  1.1       cgd 	case BURN:
    290  1.4  christos 		if (len > 15) {
    291  1.4  christos 			multi = -(len / 10);
    292  1.1       cgd 			nomovemsg = "You finished writing.";
    293  1.1       cgd 		}
    294  1.1       cgd 		break;
    295  1.1       cgd 	case ENGRAVE:		/* here otmp != 0 */
    296  1.4  christos 		{
    297  1.4  christos 			int             len2 = (otmp->spe + 3) * 2 + 1;
    298  1.1       cgd 
    299  1.1       cgd 			pline("Your %s dull.", aobjnam(otmp, "get"));
    300  1.4  christos 			if (len2 < len) {
    301  1.1       cgd 				len = len2;
    302  1.1       cgd 				sp[len] = 0;
    303  1.1       cgd 				otmp->spe = -3;
    304  1.1       cgd 				nomovemsg = "You cannot engrave more.";
    305  1.1       cgd 			} else {
    306  1.4  christos 				otmp->spe -= len / 2;
    307  1.1       cgd 				nomovemsg = "You finished engraving.";
    308  1.1       cgd 			}
    309  1.1       cgd 			multi = -len;
    310  1.1       cgd 		}
    311  1.1       cgd 		break;
    312  1.1       cgd 	}
    313  1.4  christos 	if (oep)
    314  1.4  christos 		len += strlen(oep->engr_txt) + spct;
    315  1.4  christos 	ep = (struct engr *) alloc((unsigned) (sizeof(struct engr) + len + 1));
    316  1.1       cgd 	ep->nxt_engr = head_engr;
    317  1.1       cgd 	head_engr = ep;
    318  1.1       cgd 	ep->engr_x = u.ux;
    319  1.1       cgd 	ep->engr_y = u.uy;
    320  1.4  christos 	sp = (char *) (ep + 1);	/* (char *)ep + sizeof(struct engr) */
    321  1.1       cgd 	ep->engr_txt = sp;
    322  1.4  christos 	if (oep) {
    323  1.1       cgd 		(void) strcpy(sp, oep->engr_txt);
    324  1.1       cgd 		(void) strcat(sp, buf);
    325  1.1       cgd 		del_engr(oep);
    326  1.1       cgd 	} else
    327  1.1       cgd 		(void) strcpy(sp, buf);
    328  1.4  christos 	ep->engr_lth = len + 1;
    329  1.1       cgd 	ep->engr_type = type;
    330  1.4  christos 	ep->engr_time = moves - multi;
    331  1.1       cgd 
    332  1.1       cgd 	/* kludge to protect pline against excessively long texts */
    333  1.4  christos 	if (len > BUFSZ - 20)
    334  1.4  christos 		sp[BUFSZ - 20] = 0;
    335  1.1       cgd 
    336  1.4  christos 	return (1);
    337  1.1       cgd }
    338  1.1       cgd 
    339  1.4  christos void
    340  1.7  dholland save_engravings(int fd)
    341  1.4  christos {
    342  1.4  christos 	struct engr    *ep = head_engr;
    343  1.4  christos 	while (ep) {
    344  1.4  christos 		if (!ep->engr_lth || !ep->engr_txt[0]) {
    345  1.1       cgd 			ep = ep->nxt_engr;
    346  1.1       cgd 			continue;
    347  1.1       cgd 		}
    348  1.4  christos 		bwrite(fd, (char *) &(ep->engr_lth), sizeof(ep->engr_lth));
    349  1.1       cgd 		bwrite(fd, (char *) ep, sizeof(struct engr) + ep->engr_lth);
    350  1.1       cgd 		ep = ep->nxt_engr;
    351  1.1       cgd 	}
    352  1.1       cgd 	bwrite(fd, (char *) nul, sizeof(unsigned));
    353  1.1       cgd 	head_engr = 0;
    354  1.1       cgd }
    355  1.1       cgd 
    356  1.4  christos void
    357  1.7  dholland rest_engravings(int fd)
    358  1.4  christos {
    359  1.4  christos 	struct engr    *ep;
    360  1.4  christos 	unsigned        lth;
    361  1.1       cgd 	head_engr = 0;
    362  1.4  christos 	while (1) {
    363  1.1       cgd 		mread(fd, (char *) &lth, sizeof(unsigned));
    364  1.4  christos 		if (lth == 0)
    365  1.4  christos 			return;
    366  1.1       cgd 		ep = (struct engr *) alloc(sizeof(struct engr) + lth);
    367  1.1       cgd 		mread(fd, (char *) ep, sizeof(struct engr) + lth);
    368  1.1       cgd 		ep->nxt_engr = head_engr;
    369  1.1       cgd 		ep->engr_txt = (char *) (ep + 1);	/* Andreas Bormann */
    370  1.1       cgd 		head_engr = ep;
    371  1.1       cgd 	}
    372  1.1       cgd }
    373  1.1       cgd 
    374  1.4  christos void
    375  1.7  dholland del_engr(struct engr *ep)
    376  1.4  christos {
    377  1.4  christos 	struct engr    *ept;
    378  1.4  christos 	if (ep == head_engr)
    379  1.1       cgd 		head_engr = ep->nxt_engr;
    380  1.1       cgd 	else {
    381  1.4  christos 		for (ept = head_engr; ept; ept = ept->nxt_engr) {
    382  1.4  christos 			if (ept->nxt_engr == ep) {
    383  1.1       cgd 				ept->nxt_engr = ep->nxt_engr;
    384  1.1       cgd 				goto fnd;
    385  1.1       cgd 			}
    386  1.1       cgd 		}
    387  1.1       cgd 		impossible("Error in del_engr?");
    388  1.1       cgd 		return;
    389  1.4  christos fnd:		;
    390  1.1       cgd 	}
    391  1.1       cgd 	free((char *) ep);
    392  1.1       cgd }
    393