hack.engrave.c revision 1.6 1 1.6 jsm /* $NetBSD: hack.engrave.c,v 1.6 2003/04/02 18:36:36 jsm 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.6 jsm __RCSID("$NetBSD: hack.engrave.c,v 1.6 2003/04/02 18:36:36 jsm 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.4 christos engr_at(x, y)
89 1.4 christos xchar x, y;
90 1.4 christos {
91 1.4 christos struct engr *ep = head_engr;
92 1.4 christos while (ep) {
93 1.4 christos if (x == ep->engr_x && y == ep->engr_y)
94 1.4 christos return (ep);
95 1.1 cgd ep = ep->nxt_engr;
96 1.1 cgd }
97 1.4 christos return ((struct engr *) 0);
98 1.1 cgd }
99 1.1 cgd
100 1.4 christos int
101 1.4 christos sengr_at(s, x, y)
102 1.5 jsm const char *s;
103 1.4 christos xchar x, y;
104 1.4 christos {
105 1.4 christos struct engr *ep = engr_at(x, y);
106 1.4 christos char *t;
107 1.4 christos int n;
108 1.4 christos if (ep && ep->engr_time <= moves) {
109 1.1 cgd t = ep->engr_txt;
110 1.4 christos /*
111 1.4 christos if(!strcmp(s,t)) return(1);
112 1.4 christos */
113 1.1 cgd n = strlen(s);
114 1.4 christos while (*t) {
115 1.4 christos if (!strncmp(s, t, n))
116 1.4 christos return (1);
117 1.1 cgd t++;
118 1.1 cgd }
119 1.1 cgd }
120 1.4 christos return (0);
121 1.1 cgd }
122 1.1 cgd
123 1.4 christos void
124 1.1 cgd u_wipe_engr(cnt)
125 1.4 christos int cnt;
126 1.1 cgd {
127 1.4 christos if (!u.uswallow && !Levitation)
128 1.1 cgd wipe_engr_at(u.ux, u.uy, cnt);
129 1.1 cgd }
130 1.1 cgd
131 1.4 christos void
132 1.4 christos wipe_engr_at(x, y, cnt)
133 1.4 christos xchar x, y, cnt;
134 1.4 christos {
135 1.4 christos struct engr *ep = engr_at(x, y);
136 1.4 christos int lth, pos;
137 1.4 christos char ch;
138 1.4 christos if (ep) {
139 1.4 christos if ((ep->engr_type != DUST) || Levitation) {
140 1.4 christos cnt = rn2(1 + 50 / (cnt + 1)) ? 0 : 1;
141 1.1 cgd }
142 1.1 cgd lth = strlen(ep->engr_txt);
143 1.4 christos if (lth && cnt > 0) {
144 1.4 christos while (cnt--) {
145 1.1 cgd pos = rn2(lth);
146 1.4 christos if ((ch = ep->engr_txt[pos]) == ' ')
147 1.1 cgd continue;
148 1.1 cgd ep->engr_txt[pos] = (ch != '?') ? '?' : ' ';
149 1.1 cgd }
150 1.1 cgd }
151 1.4 christos while (lth && ep->engr_txt[lth - 1] == ' ')
152 1.1 cgd ep->engr_txt[--lth] = 0;
153 1.4 christos while (ep->engr_txt[0] == ' ')
154 1.1 cgd ep->engr_txt++;
155 1.4 christos if (!ep->engr_txt[0])
156 1.4 christos del_engr(ep);
157 1.1 cgd }
158 1.1 cgd }
159 1.1 cgd
160 1.4 christos void
161 1.4 christos read_engr_at(x, y)
162 1.4 christos int x, y;
163 1.4 christos {
164 1.4 christos struct engr *ep = engr_at(x, y);
165 1.4 christos if (ep && ep->engr_txt[0]) {
166 1.4 christos switch (ep->engr_type) {
167 1.4 christos case DUST:
168 1.4 christos pline("Something is written here in the dust.");
169 1.4 christos break;
170 1.4 christos case ENGRAVE:
171 1.4 christos pline("Something is engraved here on the floor.");
172 1.4 christos break;
173 1.4 christos case BURN:
174 1.4 christos pline("Some text has been burned here in the floor.");
175 1.4 christos break;
176 1.4 christos default:
177 1.4 christos impossible("Something is written in a very strange way.");
178 1.4 christos }
179 1.4 christos pline("You read: \"%s\".", ep->engr_txt);
180 1.1 cgd }
181 1.1 cgd }
182 1.1 cgd
183 1.4 christos void
184 1.4 christos make_engr_at(x, y, s)
185 1.4 christos int x, y;
186 1.5 jsm const char *s;
187 1.1 cgd {
188 1.4 christos struct engr *ep;
189 1.1 cgd
190 1.4 christos if ((ep = engr_at(x, y)) != NULL)
191 1.4 christos del_engr(ep);
192 1.1 cgd ep = (struct engr *)
193 1.4 christos alloc((unsigned) (sizeof(struct engr) + strlen(s) + 1));
194 1.1 cgd ep->nxt_engr = head_engr;
195 1.1 cgd head_engr = ep;
196 1.1 cgd ep->engr_x = x;
197 1.1 cgd ep->engr_y = y;
198 1.4 christos ep->engr_txt = (char *) (ep + 1);
199 1.1 cgd (void) strcpy(ep->engr_txt, s);
200 1.1 cgd ep->engr_time = 0;
201 1.1 cgd ep->engr_type = DUST;
202 1.1 cgd ep->engr_lth = strlen(s) + 1;
203 1.1 cgd }
204 1.1 cgd
205 1.4 christos int
206 1.4 christos doengrave()
207 1.4 christos {
208 1.4 christos int len;
209 1.4 christos char *sp;
210 1.4 christos struct engr *ep, *oep = engr_at(u.ux, u.uy);
211 1.4 christos char buf[BUFSZ];
212 1.4 christos xchar type;
213 1.4 christos int spct; /* number of leading spaces */
214 1.4 christos struct obj *otmp;
215 1.1 cgd multi = 0;
216 1.1 cgd
217 1.4 christos if (u.uswallow) {
218 1.1 cgd pline("You're joking. Hahaha!"); /* riv05!a3 */
219 1.4 christos return (0);
220 1.1 cgd }
221 1.1 cgd /* one may write with finger, weapon or wand */
222 1.1 cgd otmp = getobj("#-)/", "write with");
223 1.4 christos if (!otmp)
224 1.4 christos return (0);
225 1.1 cgd
226 1.4 christos if (otmp == &zeroobj)
227 1.1 cgd otmp = 0;
228 1.4 christos if (otmp && otmp->otyp == WAN_FIRE && otmp->spe) {
229 1.1 cgd type = BURN;
230 1.1 cgd otmp->spe--;
231 1.1 cgd } else {
232 1.1 cgd /* first wield otmp */
233 1.4 christos if (otmp != uwep) {
234 1.4 christos if (uwep && uwep->cursed) {
235 1.4 christos /* Andreas Bormann */
236 1.4 christos pline("Since your weapon is welded to your hand,");
237 1.4 christos pline("you use the %s.", aobjnam(uwep, (char *) 0));
238 1.4 christos otmp = uwep;
239 1.1 cgd } else {
240 1.4 christos if (!otmp)
241 1.4 christos pline("You are now empty-handed.");
242 1.4 christos else if (otmp->cursed)
243 1.4 christos pline("The %s %s to your hand!",
244 1.4 christos aobjnam(otmp, "weld"),
245 1.4 christos (otmp->quan == 1) ? "itself" : "themselves");
246 1.4 christos else
247 1.4 christos pline("You now wield %s.", doname(otmp));
248 1.4 christos setuwep(otmp);
249 1.1 cgd }
250 1.1 cgd }
251 1.4 christos if (!otmp)
252 1.1 cgd type = DUST;
253 1.4 christos else if (otmp->otyp == DAGGER || otmp->otyp == TWO_HANDED_SWORD ||
254 1.4 christos otmp->otyp == CRYSKNIFE ||
255 1.4 christos otmp->otyp == LONG_SWORD || otmp->otyp == AXE) {
256 1.1 cgd type = ENGRAVE;
257 1.4 christos if ((int) otmp->spe <= -3) {
258 1.1 cgd type = DUST;
259 1.1 cgd pline("Your %s too dull for engraving.",
260 1.4 christos aobjnam(otmp, "are"));
261 1.4 christos if (oep && oep->engr_type != DUST)
262 1.4 christos return (1);
263 1.1 cgd }
264 1.4 christos } else
265 1.4 christos type = DUST;
266 1.1 cgd }
267 1.4 christos if (Levitation && type != BURN) { /* riv05!a3 */
268 1.1 cgd pline("You can't reach the floor!");
269 1.4 christos return (1);
270 1.4 christos }
271 1.4 christos if (oep && oep->engr_type == DUST) {
272 1.4 christos pline("You wipe out the message that was written here.");
273 1.4 christos del_engr(oep);
274 1.4 christos oep = 0;
275 1.1 cgd }
276 1.4 christos if (type == DUST && oep) {
277 1.4 christos pline("You cannot wipe out the message that is %s in the rock.",
278 1.4 christos (oep->engr_type == BURN) ? "burned" : "engraved");
279 1.4 christos return (1);
280 1.1 cgd }
281 1.1 cgd pline("What do you want to %s on the floor here? ",
282 1.4 christos (type == ENGRAVE) ? "engrave" : (type == BURN) ? "burn" : "write");
283 1.1 cgd getlin(buf);
284 1.1 cgd clrlin();
285 1.1 cgd spct = 0;
286 1.1 cgd sp = buf;
287 1.4 christos while (*sp == ' ')
288 1.4 christos spct++, sp++;
289 1.1 cgd len = strlen(sp);
290 1.4 christos if (!len || *buf == '\033') {
291 1.4 christos if (type == BURN)
292 1.4 christos otmp->spe++;
293 1.4 christos return (0);
294 1.1 cgd }
295 1.4 christos switch (type) {
296 1.1 cgd case DUST:
297 1.1 cgd case BURN:
298 1.4 christos if (len > 15) {
299 1.4 christos multi = -(len / 10);
300 1.1 cgd nomovemsg = "You finished writing.";
301 1.1 cgd }
302 1.1 cgd break;
303 1.1 cgd case ENGRAVE: /* here otmp != 0 */
304 1.4 christos {
305 1.4 christos int len2 = (otmp->spe + 3) * 2 + 1;
306 1.1 cgd
307 1.1 cgd pline("Your %s dull.", aobjnam(otmp, "get"));
308 1.4 christos if (len2 < len) {
309 1.1 cgd len = len2;
310 1.1 cgd sp[len] = 0;
311 1.1 cgd otmp->spe = -3;
312 1.1 cgd nomovemsg = "You cannot engrave more.";
313 1.1 cgd } else {
314 1.4 christos otmp->spe -= len / 2;
315 1.1 cgd nomovemsg = "You finished engraving.";
316 1.1 cgd }
317 1.1 cgd multi = -len;
318 1.1 cgd }
319 1.1 cgd break;
320 1.1 cgd }
321 1.4 christos if (oep)
322 1.4 christos len += strlen(oep->engr_txt) + spct;
323 1.4 christos ep = (struct engr *) alloc((unsigned) (sizeof(struct engr) + len + 1));
324 1.1 cgd ep->nxt_engr = head_engr;
325 1.1 cgd head_engr = ep;
326 1.1 cgd ep->engr_x = u.ux;
327 1.1 cgd ep->engr_y = u.uy;
328 1.4 christos sp = (char *) (ep + 1); /* (char *)ep + sizeof(struct engr) */
329 1.1 cgd ep->engr_txt = sp;
330 1.4 christos if (oep) {
331 1.1 cgd (void) strcpy(sp, oep->engr_txt);
332 1.1 cgd (void) strcat(sp, buf);
333 1.1 cgd del_engr(oep);
334 1.1 cgd } else
335 1.1 cgd (void) strcpy(sp, buf);
336 1.4 christos ep->engr_lth = len + 1;
337 1.1 cgd ep->engr_type = type;
338 1.4 christos ep->engr_time = moves - multi;
339 1.1 cgd
340 1.1 cgd /* kludge to protect pline against excessively long texts */
341 1.4 christos if (len > BUFSZ - 20)
342 1.4 christos sp[BUFSZ - 20] = 0;
343 1.1 cgd
344 1.4 christos return (1);
345 1.1 cgd }
346 1.1 cgd
347 1.4 christos void
348 1.4 christos save_engravings(fd)
349 1.4 christos int fd;
350 1.4 christos {
351 1.4 christos struct engr *ep = head_engr;
352 1.4 christos while (ep) {
353 1.4 christos if (!ep->engr_lth || !ep->engr_txt[0]) {
354 1.1 cgd ep = ep->nxt_engr;
355 1.1 cgd continue;
356 1.1 cgd }
357 1.4 christos bwrite(fd, (char *) &(ep->engr_lth), sizeof(ep->engr_lth));
358 1.1 cgd bwrite(fd, (char *) ep, sizeof(struct engr) + ep->engr_lth);
359 1.1 cgd ep = ep->nxt_engr;
360 1.1 cgd }
361 1.1 cgd bwrite(fd, (char *) nul, sizeof(unsigned));
362 1.1 cgd head_engr = 0;
363 1.1 cgd }
364 1.1 cgd
365 1.4 christos void
366 1.4 christos rest_engravings(fd)
367 1.4 christos int fd;
368 1.4 christos {
369 1.4 christos struct engr *ep;
370 1.4 christos unsigned lth;
371 1.1 cgd head_engr = 0;
372 1.4 christos while (1) {
373 1.1 cgd mread(fd, (char *) <h, sizeof(unsigned));
374 1.4 christos if (lth == 0)
375 1.4 christos return;
376 1.1 cgd ep = (struct engr *) alloc(sizeof(struct engr) + lth);
377 1.1 cgd mread(fd, (char *) ep, sizeof(struct engr) + lth);
378 1.1 cgd ep->nxt_engr = head_engr;
379 1.1 cgd ep->engr_txt = (char *) (ep + 1); /* Andreas Bormann */
380 1.1 cgd head_engr = ep;
381 1.1 cgd }
382 1.1 cgd }
383 1.1 cgd
384 1.4 christos void
385 1.4 christos del_engr(ep)
386 1.4 christos struct engr *ep;
387 1.4 christos {
388 1.4 christos struct engr *ept;
389 1.4 christos if (ep == head_engr)
390 1.1 cgd head_engr = ep->nxt_engr;
391 1.1 cgd else {
392 1.4 christos for (ept = head_engr; ept; ept = ept->nxt_engr) {
393 1.4 christos if (ept->nxt_engr == ep) {
394 1.1 cgd ept->nxt_engr = ep->nxt_engr;
395 1.1 cgd goto fnd;
396 1.1 cgd }
397 1.1 cgd }
398 1.1 cgd impossible("Error in del_engr?");
399 1.1 cgd return;
400 1.4 christos fnd: ;
401 1.1 cgd }
402 1.1 cgd free((char *) ep);
403 1.1 cgd }
404