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