1*d5b021c7Sdholland /* $NetBSD: hack.engrave.c,v 1.14 2011/08/07 06:03:45 dholland Exp $ */
23ea4a95cSchristos
302ded532Smycroft /*
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.
6202ded532Smycroft */
6302ded532Smycroft
643ea4a95cSchristos #include <sys/cdefs.h>
6502ded532Smycroft #ifndef lint
66*d5b021c7Sdholland __RCSID("$NetBSD: hack.engrave.c,v 1.14 2011/08/07 06:03:45 dholland Exp $");
6702ded532Smycroft #endif /* not lint */
6861f28255Scgd
693ea4a95cSchristos #include <stdlib.h>
7061f28255Scgd #include "hack.h"
713ea4a95cSchristos #include "extern.h"
7261f28255Scgd
7361f28255Scgd struct engr {
7461f28255Scgd struct engr *nxt_engr;
7561f28255Scgd char *engr_txt;
7661f28255Scgd xchar engr_x, engr_y;
773ea4a95cSchristos unsigned engr_lth; /* for save & restore; not length of
783ea4a95cSchristos * text */
793ea4a95cSchristos long engr_time; /* moment engraving was (will be)
803ea4a95cSchristos * finished */
8161f28255Scgd xchar engr_type;
8261f28255Scgd #define DUST 1
8361f28255Scgd #define ENGRAVE 2
8461f28255Scgd #define BURN 3
859b92b189Sdholland };
8661f28255Scgd
879b92b189Sdholland static struct engr *head_engr;
889b92b189Sdholland
899b92b189Sdholland static void del_engr(struct engr *);
909b92b189Sdholland
919b92b189Sdholland static struct engr *
engr_at(xchar x,xchar y)921fa8a9a6Sdholland engr_at(xchar x, xchar y)
933ea4a95cSchristos {
943ea4a95cSchristos struct engr *ep = head_engr;
9561f28255Scgd while (ep) {
9661f28255Scgd if (x == ep->engr_x && y == ep->engr_y)
9761f28255Scgd return (ep);
9861f28255Scgd ep = ep->nxt_engr;
9961f28255Scgd }
10061f28255Scgd return ((struct engr *) 0);
10161f28255Scgd }
10261f28255Scgd
1033ea4a95cSchristos int
sengr_at(const char * s,xchar x,xchar y)1041fa8a9a6Sdholland sengr_at(const char *s, xchar x, xchar y)
1053ea4a95cSchristos {
1063ea4a95cSchristos struct engr *ep = engr_at(x, y);
1073ea4a95cSchristos char *t;
108*d5b021c7Sdholland size_t n;
109*d5b021c7Sdholland
11061f28255Scgd if (ep && ep->engr_time <= moves) {
11161f28255Scgd t = ep->engr_txt;
11261f28255Scgd /*
11361f28255Scgd if(!strcmp(s,t)) return(1);
11461f28255Scgd */
11561f28255Scgd n = strlen(s);
11661f28255Scgd while (*t) {
1173ea4a95cSchristos if (!strncmp(s, t, n))
1183ea4a95cSchristos return (1);
11961f28255Scgd t++;
12061f28255Scgd }
12161f28255Scgd }
12261f28255Scgd return (0);
12361f28255Scgd }
12461f28255Scgd
1253ea4a95cSchristos void
u_wipe_engr(int cnt)1261fa8a9a6Sdholland u_wipe_engr(int cnt)
12761f28255Scgd {
12861f28255Scgd if (!u.uswallow && !Levitation)
12961f28255Scgd wipe_engr_at(u.ux, u.uy, cnt);
13061f28255Scgd }
13161f28255Scgd
1323ea4a95cSchristos void
wipe_engr_at(xchar x,xchar y,xchar cnt)1331fa8a9a6Sdholland wipe_engr_at(xchar x, xchar y, xchar cnt)
1343ea4a95cSchristos {
1353ea4a95cSchristos struct engr *ep = engr_at(x, y);
136*d5b021c7Sdholland int pos;
13761f28255Scgd char ch;
138*d5b021c7Sdholland size_t lth;
139*d5b021c7Sdholland
14061f28255Scgd if (ep) {
14161f28255Scgd if ((ep->engr_type != DUST) || Levitation) {
14261f28255Scgd cnt = rn2(1 + 50 / (cnt + 1)) ? 0 : 1;
14361f28255Scgd }
14461f28255Scgd lth = strlen(ep->engr_txt);
14561f28255Scgd if (lth && cnt > 0) {
14661f28255Scgd while (cnt--) {
14761f28255Scgd pos = rn2(lth);
14861f28255Scgd if ((ch = ep->engr_txt[pos]) == ' ')
14961f28255Scgd continue;
15061f28255Scgd ep->engr_txt[pos] = (ch != '?') ? '?' : ' ';
15161f28255Scgd }
15261f28255Scgd }
15361f28255Scgd while (lth && ep->engr_txt[lth - 1] == ' ')
15461f28255Scgd ep->engr_txt[--lth] = 0;
15561f28255Scgd while (ep->engr_txt[0] == ' ')
15661f28255Scgd ep->engr_txt++;
1573ea4a95cSchristos if (!ep->engr_txt[0])
1583ea4a95cSchristos del_engr(ep);
15961f28255Scgd }
16061f28255Scgd }
16161f28255Scgd
1623ea4a95cSchristos void
read_engr_at(int x,int y)1631fa8a9a6Sdholland read_engr_at(int x, int y)
1643ea4a95cSchristos {
1653ea4a95cSchristos struct engr *ep = engr_at(x, y);
16661f28255Scgd if (ep && ep->engr_txt[0]) {
16761f28255Scgd switch (ep->engr_type) {
16861f28255Scgd case DUST:
16961f28255Scgd pline("Something is written here in the dust.");
17061f28255Scgd break;
17161f28255Scgd case ENGRAVE:
17261f28255Scgd pline("Something is engraved here on the floor.");
17361f28255Scgd break;
17461f28255Scgd case BURN:
17561f28255Scgd pline("Some text has been burned here in the floor.");
17661f28255Scgd break;
17761f28255Scgd default:
17861f28255Scgd impossible("Something is written in a very strange way.");
17961f28255Scgd }
18061f28255Scgd pline("You read: \"%s\".", ep->engr_txt);
18161f28255Scgd }
18261f28255Scgd }
18361f28255Scgd
1843ea4a95cSchristos void
make_engr_at(int x,int y,const char * s)1851fa8a9a6Sdholland make_engr_at(int x, int y, const char *s)
18661f28255Scgd {
1873ea4a95cSchristos struct engr *ep;
18861f28255Scgd
1893ea4a95cSchristos if ((ep = engr_at(x, y)) != NULL)
19061f28255Scgd del_engr(ep);
191434d266eSdholland ep = alloc(sizeof(*ep) + strlen(s) + 1);
192434d266eSdholland
19361f28255Scgd ep->nxt_engr = head_engr;
19461f28255Scgd head_engr = ep;
19561f28255Scgd ep->engr_x = x;
19661f28255Scgd ep->engr_y = y;
19761f28255Scgd ep->engr_txt = (char *) (ep + 1);
19861f28255Scgd (void) strcpy(ep->engr_txt, s);
19961f28255Scgd ep->engr_time = 0;
20061f28255Scgd ep->engr_type = DUST;
20161f28255Scgd ep->engr_lth = strlen(s) + 1;
20261f28255Scgd }
20361f28255Scgd
2043ea4a95cSchristos int
doengrave(void)2051fa8a9a6Sdholland doengrave(void)
2063ea4a95cSchristos {
2073ea4a95cSchristos int len;
2083ea4a95cSchristos char *sp;
2093ea4a95cSchristos struct engr *ep, *oep = engr_at(u.ux, u.uy);
21061f28255Scgd char buf[BUFSZ];
21161f28255Scgd xchar type;
21261f28255Scgd int spct; /* number of leading spaces */
2133ea4a95cSchristos struct obj *otmp;
21461f28255Scgd multi = 0;
21561f28255Scgd
21661f28255Scgd if (u.uswallow) {
21761f28255Scgd pline("You're joking. Hahaha!"); /* riv05!a3 */
21861f28255Scgd return (0);
21961f28255Scgd }
22061f28255Scgd /* one may write with finger, weapon or wand */
22161f28255Scgd otmp = getobj("#-)/", "write with");
2223ea4a95cSchristos if (!otmp)
2233ea4a95cSchristos return (0);
22461f28255Scgd
22561f28255Scgd if (otmp == &zeroobj)
22661f28255Scgd otmp = 0;
22761f28255Scgd if (otmp && otmp->otyp == WAN_FIRE && otmp->spe) {
22861f28255Scgd type = BURN;
22961f28255Scgd otmp->spe--;
23061f28255Scgd } else {
23161f28255Scgd /* first wield otmp */
23261f28255Scgd if (otmp != uwep) {
23361f28255Scgd if (uwep && uwep->cursed) {
23461f28255Scgd /* Andreas Bormann */
23561f28255Scgd pline("Since your weapon is welded to your hand,");
2362c0ecb1aSdholland pline("you use the %s.", aobjnam(uwep, NULL));
23761f28255Scgd otmp = uwep;
23861f28255Scgd } else {
23961f28255Scgd if (!otmp)
24061f28255Scgd pline("You are now empty-handed.");
24161f28255Scgd else if (otmp->cursed)
24261f28255Scgd pline("The %s %s to your hand!",
24361f28255Scgd aobjnam(otmp, "weld"),
24461f28255Scgd (otmp->quan == 1) ? "itself" : "themselves");
24561f28255Scgd else
24661f28255Scgd pline("You now wield %s.", doname(otmp));
24761f28255Scgd setuwep(otmp);
24861f28255Scgd }
24961f28255Scgd }
25061f28255Scgd if (!otmp)
25161f28255Scgd type = DUST;
2523ea4a95cSchristos else if (otmp->otyp == DAGGER || otmp->otyp == TWO_HANDED_SWORD ||
25361f28255Scgd otmp->otyp == CRYSKNIFE ||
25461f28255Scgd otmp->otyp == LONG_SWORD || otmp->otyp == AXE) {
25561f28255Scgd type = ENGRAVE;
25661f28255Scgd if ((int) otmp->spe <= -3) {
25761f28255Scgd type = DUST;
25861f28255Scgd pline("Your %s too dull for engraving.",
25961f28255Scgd aobjnam(otmp, "are"));
2603ea4a95cSchristos if (oep && oep->engr_type != DUST)
2613ea4a95cSchristos return (1);
26261f28255Scgd }
2633ea4a95cSchristos } else
2643ea4a95cSchristos type = DUST;
26561f28255Scgd }
26661f28255Scgd if (Levitation && type != BURN) { /* riv05!a3 */
26761f28255Scgd pline("You can't reach the floor!");
26861f28255Scgd return (1);
26961f28255Scgd }
27061f28255Scgd if (oep && oep->engr_type == DUST) {
27161f28255Scgd pline("You wipe out the message that was written here.");
27261f28255Scgd del_engr(oep);
27361f28255Scgd oep = 0;
27461f28255Scgd }
27561f28255Scgd if (type == DUST && oep) {
27661f28255Scgd pline("You cannot wipe out the message that is %s in the rock.",
27761f28255Scgd (oep->engr_type == BURN) ? "burned" : "engraved");
27861f28255Scgd return (1);
27961f28255Scgd }
28061f28255Scgd pline("What do you want to %s on the floor here? ",
28161f28255Scgd (type == ENGRAVE) ? "engrave" : (type == BURN) ? "burn" : "write");
28261f28255Scgd getlin(buf);
28361f28255Scgd clrlin();
28461f28255Scgd spct = 0;
28561f28255Scgd sp = buf;
2863ea4a95cSchristos while (*sp == ' ')
2873ea4a95cSchristos spct++, sp++;
28861f28255Scgd len = strlen(sp);
28961f28255Scgd if (!len || *buf == '\033') {
2903ea4a95cSchristos if (type == BURN)
2913ea4a95cSchristos otmp->spe++;
29261f28255Scgd return (0);
29361f28255Scgd }
29461f28255Scgd switch (type) {
29561f28255Scgd case DUST:
29661f28255Scgd case BURN:
29761f28255Scgd if (len > 15) {
29861f28255Scgd multi = -(len / 10);
29961f28255Scgd nomovemsg = "You finished writing.";
30061f28255Scgd }
30161f28255Scgd break;
30261f28255Scgd case ENGRAVE: /* here otmp != 0 */
3033ea4a95cSchristos {
3043ea4a95cSchristos int len2 = (otmp->spe + 3) * 2 + 1;
30561f28255Scgd
30661f28255Scgd pline("Your %s dull.", aobjnam(otmp, "get"));
30761f28255Scgd if (len2 < len) {
30861f28255Scgd len = len2;
30961f28255Scgd sp[len] = 0;
31061f28255Scgd otmp->spe = -3;
31161f28255Scgd nomovemsg = "You cannot engrave more.";
31261f28255Scgd } else {
31361f28255Scgd otmp->spe -= len / 2;
31461f28255Scgd nomovemsg = "You finished engraving.";
31561f28255Scgd }
31661f28255Scgd multi = -len;
31761f28255Scgd }
31861f28255Scgd break;
31961f28255Scgd }
3203ea4a95cSchristos if (oep)
3213ea4a95cSchristos len += strlen(oep->engr_txt) + spct;
322434d266eSdholland ep = alloc(sizeof(*ep) + len + 1);
32361f28255Scgd ep->nxt_engr = head_engr;
32461f28255Scgd head_engr = ep;
32561f28255Scgd ep->engr_x = u.ux;
32661f28255Scgd ep->engr_y = u.uy;
32761f28255Scgd sp = (char *) (ep + 1); /* (char *)ep + sizeof(struct engr) */
32861f28255Scgd ep->engr_txt = sp;
32961f28255Scgd if (oep) {
33061f28255Scgd (void) strcpy(sp, oep->engr_txt);
33161f28255Scgd (void) strcat(sp, buf);
33261f28255Scgd del_engr(oep);
33361f28255Scgd } else
33461f28255Scgd (void) strcpy(sp, buf);
33561f28255Scgd ep->engr_lth = len + 1;
33661f28255Scgd ep->engr_type = type;
33761f28255Scgd ep->engr_time = moves - multi;
33861f28255Scgd
33961f28255Scgd /* kludge to protect pline against excessively long texts */
3403ea4a95cSchristos if (len > BUFSZ - 20)
3413ea4a95cSchristos sp[BUFSZ - 20] = 0;
34261f28255Scgd
34361f28255Scgd return (1);
34461f28255Scgd }
34561f28255Scgd
3463ea4a95cSchristos void
save_engravings(int fd)3471fa8a9a6Sdholland save_engravings(int fd)
3483ea4a95cSchristos {
3493ea4a95cSchristos struct engr *ep = head_engr;
35061f28255Scgd while (ep) {
35161f28255Scgd if (!ep->engr_lth || !ep->engr_txt[0]) {
35261f28255Scgd ep = ep->nxt_engr;
35361f28255Scgd continue;
35461f28255Scgd }
3558d5216e8Sdholland bwrite(fd, &(ep->engr_lth), sizeof(ep->engr_lth));
3568d5216e8Sdholland bwrite(fd, ep, sizeof(struct engr) + ep->engr_lth);
35761f28255Scgd ep = ep->nxt_engr;
35861f28255Scgd }
3598d5216e8Sdholland bwrite(fd, nul, sizeof(unsigned));
36061f28255Scgd head_engr = 0;
36161f28255Scgd }
36261f28255Scgd
3633ea4a95cSchristos void
rest_engravings(int fd)3641fa8a9a6Sdholland rest_engravings(int fd)
3653ea4a95cSchristos {
3663ea4a95cSchristos struct engr *ep;
36761f28255Scgd unsigned lth;
36861f28255Scgd head_engr = 0;
36961f28255Scgd while (1) {
37025ec2965Sdholland mread(fd, <h, sizeof(unsigned));
3713ea4a95cSchristos if (lth == 0)
3723ea4a95cSchristos return;
373434d266eSdholland ep = alloc(sizeof(*ep) + lth);
374434d266eSdholland mread(fd, ep, sizeof(*ep) + lth);
37561f28255Scgd ep->nxt_engr = head_engr;
37661f28255Scgd ep->engr_txt = (char *) (ep + 1); /* Andreas Bormann */
37761f28255Scgd head_engr = ep;
37861f28255Scgd }
37961f28255Scgd }
38061f28255Scgd
3819b92b189Sdholland static void
del_engr(struct engr * ep)3821fa8a9a6Sdholland del_engr(struct engr *ep)
3833ea4a95cSchristos {
3843ea4a95cSchristos struct engr *ept;
38561f28255Scgd if (ep == head_engr)
38661f28255Scgd head_engr = ep->nxt_engr;
38761f28255Scgd else {
38861f28255Scgd for (ept = head_engr; ept; ept = ept->nxt_engr) {
38961f28255Scgd if (ept->nxt_engr == ep) {
39061f28255Scgd ept->nxt_engr = ep->nxt_engr;
39161f28255Scgd goto fnd;
39261f28255Scgd }
39361f28255Scgd }
39461f28255Scgd impossible("Error in del_engr?");
39561f28255Scgd return;
39661f28255Scgd fnd: ;
39761f28255Scgd }
3988e73b3adSdholland free(ep);
39961f28255Scgd }
400