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