Home | History | Annotate | Line # | Download | only in hack
hack.engrave.c revision 1.4
      1  1.4  christos /*	$NetBSD: hack.engrave.c,v 1.4 1997/10/19 16:57:58 christos Exp $	*/
      2  1.4  christos 
      3  1.2   mycroft /*
      4  1.2   mycroft  * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
      5  1.2   mycroft  */
      6  1.2   mycroft 
      7  1.4  christos #include <sys/cdefs.h>
      8  1.2   mycroft #ifndef lint
      9  1.4  christos __RCSID("$NetBSD: hack.engrave.c,v 1.4 1997/10/19 16:57:58 christos Exp $");
     10  1.4  christos #endif				/* not lint */
     11  1.1       cgd 
     12  1.4  christos #include <stdlib.h>
     13  1.4  christos #include "hack.h"
     14  1.4  christos #include "extern.h"
     15  1.1       cgd 
     16  1.1       cgd struct engr {
     17  1.4  christos 	struct engr    *nxt_engr;
     18  1.4  christos 	char           *engr_txt;
     19  1.4  christos 	xchar           engr_x, engr_y;
     20  1.4  christos 	unsigned        engr_lth;	/* for save & restore; not length of
     21  1.4  christos 					 * text */
     22  1.4  christos 	long            engr_time;	/* moment engraving was (will be)
     23  1.4  christos 					 * finished */
     24  1.4  christos 	xchar           engr_type;
     25  1.1       cgd #define	DUST	1
     26  1.1       cgd #define	ENGRAVE	2
     27  1.1       cgd #define	BURN	3
     28  1.4  christos }              *head_engr;
     29  1.1       cgd 
     30  1.4  christos struct engr    *
     31  1.4  christos engr_at(x, y)
     32  1.4  christos 	xchar           x, y;
     33  1.4  christos {
     34  1.4  christos 	struct engr    *ep = head_engr;
     35  1.4  christos 	while (ep) {
     36  1.4  christos 		if (x == ep->engr_x && y == ep->engr_y)
     37  1.4  christos 			return (ep);
     38  1.1       cgd 		ep = ep->nxt_engr;
     39  1.1       cgd 	}
     40  1.4  christos 	return ((struct engr *) 0);
     41  1.1       cgd }
     42  1.1       cgd 
     43  1.4  christos int
     44  1.4  christos sengr_at(s, x, y)
     45  1.4  christos 	char           *s;
     46  1.4  christos 	xchar           x, y;
     47  1.4  christos {
     48  1.4  christos 	struct engr    *ep = engr_at(x, y);
     49  1.4  christos 	char           *t;
     50  1.4  christos 	int             n;
     51  1.4  christos 	if (ep && ep->engr_time <= moves) {
     52  1.1       cgd 		t = ep->engr_txt;
     53  1.4  christos 		/*
     54  1.4  christos 				if(!strcmp(s,t)) return(1);
     55  1.4  christos 		*/
     56  1.1       cgd 		n = strlen(s);
     57  1.4  christos 		while (*t) {
     58  1.4  christos 			if (!strncmp(s, t, n))
     59  1.4  christos 				return (1);
     60  1.1       cgd 			t++;
     61  1.1       cgd 		}
     62  1.1       cgd 	}
     63  1.4  christos 	return (0);
     64  1.1       cgd }
     65  1.1       cgd 
     66  1.4  christos void
     67  1.1       cgd u_wipe_engr(cnt)
     68  1.4  christos 	int             cnt;
     69  1.1       cgd {
     70  1.4  christos 	if (!u.uswallow && !Levitation)
     71  1.1       cgd 		wipe_engr_at(u.ux, u.uy, cnt);
     72  1.1       cgd }
     73  1.1       cgd 
     74  1.4  christos void
     75  1.4  christos wipe_engr_at(x, y, cnt)
     76  1.4  christos 	xchar           x, y, cnt;
     77  1.4  christos {
     78  1.4  christos 	struct engr    *ep = engr_at(x, y);
     79  1.4  christos 	int             lth, pos;
     80  1.4  christos 	char            ch;
     81  1.4  christos 	if (ep) {
     82  1.4  christos 		if ((ep->engr_type != DUST) || Levitation) {
     83  1.4  christos 			cnt = rn2(1 + 50 / (cnt + 1)) ? 0 : 1;
     84  1.1       cgd 		}
     85  1.1       cgd 		lth = strlen(ep->engr_txt);
     86  1.4  christos 		if (lth && cnt > 0) {
     87  1.4  christos 			while (cnt--) {
     88  1.1       cgd 				pos = rn2(lth);
     89  1.4  christos 				if ((ch = ep->engr_txt[pos]) == ' ')
     90  1.1       cgd 					continue;
     91  1.1       cgd 				ep->engr_txt[pos] = (ch != '?') ? '?' : ' ';
     92  1.1       cgd 			}
     93  1.1       cgd 		}
     94  1.4  christos 		while (lth && ep->engr_txt[lth - 1] == ' ')
     95  1.1       cgd 			ep->engr_txt[--lth] = 0;
     96  1.4  christos 		while (ep->engr_txt[0] == ' ')
     97  1.1       cgd 			ep->engr_txt++;
     98  1.4  christos 		if (!ep->engr_txt[0])
     99  1.4  christos 			del_engr(ep);
    100  1.1       cgd 	}
    101  1.1       cgd }
    102  1.1       cgd 
    103  1.4  christos void
    104  1.4  christos read_engr_at(x, y)
    105  1.4  christos 	int             x, y;
    106  1.4  christos {
    107  1.4  christos 	struct engr    *ep = engr_at(x, y);
    108  1.4  christos 	if (ep && ep->engr_txt[0]) {
    109  1.4  christos 		switch (ep->engr_type) {
    110  1.4  christos 		case DUST:
    111  1.4  christos 			pline("Something is written here in the dust.");
    112  1.4  christos 			break;
    113  1.4  christos 		case ENGRAVE:
    114  1.4  christos 			pline("Something is engraved here on the floor.");
    115  1.4  christos 			break;
    116  1.4  christos 		case BURN:
    117  1.4  christos 			pline("Some text has been burned here in the floor.");
    118  1.4  christos 			break;
    119  1.4  christos 		default:
    120  1.4  christos 			impossible("Something is written in a very strange way.");
    121  1.4  christos 		}
    122  1.4  christos 		pline("You read: \"%s\".", ep->engr_txt);
    123  1.1       cgd 	}
    124  1.1       cgd }
    125  1.1       cgd 
    126  1.4  christos void
    127  1.4  christos make_engr_at(x, y, s)
    128  1.4  christos 	int             x, y;
    129  1.4  christos 	char           *s;
    130  1.1       cgd {
    131  1.4  christos 	struct engr    *ep;
    132  1.1       cgd 
    133  1.4  christos 	if ((ep = engr_at(x, y)) != NULL)
    134  1.4  christos 		del_engr(ep);
    135  1.1       cgd 	ep = (struct engr *)
    136  1.4  christos 		alloc((unsigned) (sizeof(struct engr) + strlen(s) + 1));
    137  1.1       cgd 	ep->nxt_engr = head_engr;
    138  1.1       cgd 	head_engr = ep;
    139  1.1       cgd 	ep->engr_x = x;
    140  1.1       cgd 	ep->engr_y = y;
    141  1.4  christos 	ep->engr_txt = (char *) (ep + 1);
    142  1.1       cgd 	(void) strcpy(ep->engr_txt, s);
    143  1.1       cgd 	ep->engr_time = 0;
    144  1.1       cgd 	ep->engr_type = DUST;
    145  1.1       cgd 	ep->engr_lth = strlen(s) + 1;
    146  1.1       cgd }
    147  1.1       cgd 
    148  1.4  christos int
    149  1.4  christos doengrave()
    150  1.4  christos {
    151  1.4  christos 	int             len;
    152  1.4  christos 	char           *sp;
    153  1.4  christos 	struct engr    *ep, *oep = engr_at(u.ux, u.uy);
    154  1.4  christos 	char            buf[BUFSZ];
    155  1.4  christos 	xchar           type;
    156  1.4  christos 	int             spct;	/* number of leading spaces */
    157  1.4  christos 	struct obj     *otmp;
    158  1.1       cgd 	multi = 0;
    159  1.1       cgd 
    160  1.4  christos 	if (u.uswallow) {
    161  1.1       cgd 		pline("You're joking. Hahaha!");	/* riv05!a3 */
    162  1.4  christos 		return (0);
    163  1.1       cgd 	}
    164  1.1       cgd 	/* one may write with finger, weapon or wand */
    165  1.1       cgd 	otmp = getobj("#-)/", "write with");
    166  1.4  christos 	if (!otmp)
    167  1.4  christos 		return (0);
    168  1.1       cgd 
    169  1.4  christos 	if (otmp == &zeroobj)
    170  1.1       cgd 		otmp = 0;
    171  1.4  christos 	if (otmp && otmp->otyp == WAN_FIRE && otmp->spe) {
    172  1.1       cgd 		type = BURN;
    173  1.1       cgd 		otmp->spe--;
    174  1.1       cgd 	} else {
    175  1.1       cgd 		/* first wield otmp */
    176  1.4  christos 		if (otmp != uwep) {
    177  1.4  christos 			if (uwep && uwep->cursed) {
    178  1.4  christos 				/* Andreas Bormann */
    179  1.4  christos 				pline("Since your weapon is welded to your hand,");
    180  1.4  christos 				pline("you use the %s.", aobjnam(uwep, (char *) 0));
    181  1.4  christos 				otmp = uwep;
    182  1.1       cgd 			} else {
    183  1.4  christos 				if (!otmp)
    184  1.4  christos 					pline("You are now empty-handed.");
    185  1.4  christos 				else if (otmp->cursed)
    186  1.4  christos 					pline("The %s %s to your hand!",
    187  1.4  christos 					      aobjnam(otmp, "weld"),
    188  1.4  christos 					      (otmp->quan == 1) ? "itself" : "themselves");
    189  1.4  christos 				else
    190  1.4  christos 					pline("You now wield %s.", doname(otmp));
    191  1.4  christos 				setuwep(otmp);
    192  1.1       cgd 			}
    193  1.1       cgd 		}
    194  1.4  christos 		if (!otmp)
    195  1.1       cgd 			type = DUST;
    196  1.4  christos 		else if (otmp->otyp == DAGGER || otmp->otyp == TWO_HANDED_SWORD ||
    197  1.4  christos 			 otmp->otyp == CRYSKNIFE ||
    198  1.4  christos 			 otmp->otyp == LONG_SWORD || otmp->otyp == AXE) {
    199  1.1       cgd 			type = ENGRAVE;
    200  1.4  christos 			if ((int) otmp->spe <= -3) {
    201  1.1       cgd 				type = DUST;
    202  1.1       cgd 				pline("Your %s too dull for engraving.",
    203  1.4  christos 				      aobjnam(otmp, "are"));
    204  1.4  christos 				if (oep && oep->engr_type != DUST)
    205  1.4  christos 					return (1);
    206  1.1       cgd 			}
    207  1.4  christos 		} else
    208  1.4  christos 			type = DUST;
    209  1.1       cgd 	}
    210  1.4  christos 	if (Levitation && type != BURN) {	/* riv05!a3 */
    211  1.1       cgd 		pline("You can't reach the floor!");
    212  1.4  christos 		return (1);
    213  1.4  christos 	}
    214  1.4  christos 	if (oep && oep->engr_type == DUST) {
    215  1.4  christos 		pline("You wipe out the message that was written here.");
    216  1.4  christos 		del_engr(oep);
    217  1.4  christos 		oep = 0;
    218  1.1       cgd 	}
    219  1.4  christos 	if (type == DUST && oep) {
    220  1.4  christos 		pline("You cannot wipe out the message that is %s in the rock.",
    221  1.4  christos 		      (oep->engr_type == BURN) ? "burned" : "engraved");
    222  1.4  christos 		return (1);
    223  1.1       cgd 	}
    224  1.1       cgd 	pline("What do you want to %s on the floor here? ",
    225  1.4  christos 	 (type == ENGRAVE) ? "engrave" : (type == BURN) ? "burn" : "write");
    226  1.1       cgd 	getlin(buf);
    227  1.1       cgd 	clrlin();
    228  1.1       cgd 	spct = 0;
    229  1.1       cgd 	sp = buf;
    230  1.4  christos 	while (*sp == ' ')
    231  1.4  christos 		spct++, sp++;
    232  1.1       cgd 	len = strlen(sp);
    233  1.4  christos 	if (!len || *buf == '\033') {
    234  1.4  christos 		if (type == BURN)
    235  1.4  christos 			otmp->spe++;
    236  1.4  christos 		return (0);
    237  1.1       cgd 	}
    238  1.4  christos 	switch (type) {
    239  1.1       cgd 	case DUST:
    240  1.1       cgd 	case BURN:
    241  1.4  christos 		if (len > 15) {
    242  1.4  christos 			multi = -(len / 10);
    243  1.1       cgd 			nomovemsg = "You finished writing.";
    244  1.1       cgd 		}
    245  1.1       cgd 		break;
    246  1.1       cgd 	case ENGRAVE:		/* here otmp != 0 */
    247  1.4  christos 		{
    248  1.4  christos 			int             len2 = (otmp->spe + 3) * 2 + 1;
    249  1.1       cgd 
    250  1.1       cgd 			pline("Your %s dull.", aobjnam(otmp, "get"));
    251  1.4  christos 			if (len2 < len) {
    252  1.1       cgd 				len = len2;
    253  1.1       cgd 				sp[len] = 0;
    254  1.1       cgd 				otmp->spe = -3;
    255  1.1       cgd 				nomovemsg = "You cannot engrave more.";
    256  1.1       cgd 			} else {
    257  1.4  christos 				otmp->spe -= len / 2;
    258  1.1       cgd 				nomovemsg = "You finished engraving.";
    259  1.1       cgd 			}
    260  1.1       cgd 			multi = -len;
    261  1.1       cgd 		}
    262  1.1       cgd 		break;
    263  1.1       cgd 	}
    264  1.4  christos 	if (oep)
    265  1.4  christos 		len += strlen(oep->engr_txt) + spct;
    266  1.4  christos 	ep = (struct engr *) alloc((unsigned) (sizeof(struct engr) + len + 1));
    267  1.1       cgd 	ep->nxt_engr = head_engr;
    268  1.1       cgd 	head_engr = ep;
    269  1.1       cgd 	ep->engr_x = u.ux;
    270  1.1       cgd 	ep->engr_y = u.uy;
    271  1.4  christos 	sp = (char *) (ep + 1);	/* (char *)ep + sizeof(struct engr) */
    272  1.1       cgd 	ep->engr_txt = sp;
    273  1.4  christos 	if (oep) {
    274  1.1       cgd 		(void) strcpy(sp, oep->engr_txt);
    275  1.1       cgd 		(void) strcat(sp, buf);
    276  1.1       cgd 		del_engr(oep);
    277  1.1       cgd 	} else
    278  1.1       cgd 		(void) strcpy(sp, buf);
    279  1.4  christos 	ep->engr_lth = len + 1;
    280  1.1       cgd 	ep->engr_type = type;
    281  1.4  christos 	ep->engr_time = moves - multi;
    282  1.1       cgd 
    283  1.1       cgd 	/* kludge to protect pline against excessively long texts */
    284  1.4  christos 	if (len > BUFSZ - 20)
    285  1.4  christos 		sp[BUFSZ - 20] = 0;
    286  1.1       cgd 
    287  1.4  christos 	return (1);
    288  1.1       cgd }
    289  1.1       cgd 
    290  1.4  christos void
    291  1.4  christos save_engravings(fd)
    292  1.4  christos 	int             fd;
    293  1.4  christos {
    294  1.4  christos 	struct engr    *ep = head_engr;
    295  1.4  christos 	while (ep) {
    296  1.4  christos 		if (!ep->engr_lth || !ep->engr_txt[0]) {
    297  1.1       cgd 			ep = ep->nxt_engr;
    298  1.1       cgd 			continue;
    299  1.1       cgd 		}
    300  1.4  christos 		bwrite(fd, (char *) &(ep->engr_lth), sizeof(ep->engr_lth));
    301  1.1       cgd 		bwrite(fd, (char *) ep, sizeof(struct engr) + ep->engr_lth);
    302  1.1       cgd 		ep = ep->nxt_engr;
    303  1.1       cgd 	}
    304  1.1       cgd 	bwrite(fd, (char *) nul, sizeof(unsigned));
    305  1.1       cgd 	head_engr = 0;
    306  1.1       cgd }
    307  1.1       cgd 
    308  1.4  christos void
    309  1.4  christos rest_engravings(fd)
    310  1.4  christos 	int             fd;
    311  1.4  christos {
    312  1.4  christos 	struct engr    *ep;
    313  1.4  christos 	unsigned        lth;
    314  1.1       cgd 	head_engr = 0;
    315  1.4  christos 	while (1) {
    316  1.1       cgd 		mread(fd, (char *) &lth, sizeof(unsigned));
    317  1.4  christos 		if (lth == 0)
    318  1.4  christos 			return;
    319  1.1       cgd 		ep = (struct engr *) alloc(sizeof(struct engr) + lth);
    320  1.1       cgd 		mread(fd, (char *) ep, sizeof(struct engr) + lth);
    321  1.1       cgd 		ep->nxt_engr = head_engr;
    322  1.1       cgd 		ep->engr_txt = (char *) (ep + 1);	/* Andreas Bormann */
    323  1.1       cgd 		head_engr = ep;
    324  1.1       cgd 	}
    325  1.1       cgd }
    326  1.1       cgd 
    327  1.4  christos void
    328  1.4  christos del_engr(ep)
    329  1.4  christos 	struct engr    *ep;
    330  1.4  christos {
    331  1.4  christos 	struct engr    *ept;
    332  1.4  christos 	if (ep == head_engr)
    333  1.1       cgd 		head_engr = ep->nxt_engr;
    334  1.1       cgd 	else {
    335  1.4  christos 		for (ept = head_engr; ept; ept = ept->nxt_engr) {
    336  1.4  christos 			if (ept->nxt_engr == ep) {
    337  1.1       cgd 				ept->nxt_engr = ep->nxt_engr;
    338  1.1       cgd 				goto fnd;
    339  1.1       cgd 			}
    340  1.1       cgd 		}
    341  1.1       cgd 		impossible("Error in del_engr?");
    342  1.1       cgd 		return;
    343  1.4  christos fnd:		;
    344  1.1       cgd 	}
    345  1.1       cgd 	free((char *) ep);
    346  1.1       cgd }
    347