hack.do_name.c revision 1.7 1 1.7 dholland /* $NetBSD: hack.do_name.c,v 1.7 2008/01/28 06:55:41 dholland 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.7 dholland __RCSID("$NetBSD: hack.do_name.c,v 1.7 2008/01/28 06:55:41 dholland Exp $");
67 1.4 christos #endif /* not lint */
68 1.1 cgd
69 1.4 christos #include <stdlib.h>
70 1.1 cgd #include "hack.h"
71 1.4 christos #include "extern.h"
72 1.1 cgd
73 1.1 cgd coord
74 1.4 christos getpos(force, goal)
75 1.4 christos int force;
76 1.5 jsm const char *goal;
77 1.4 christos {
78 1.4 christos int cx, cy, i, c;
79 1.4 christos coord cc;
80 1.1 cgd pline("(For instructions type a ?)");
81 1.1 cgd cx = u.ux;
82 1.1 cgd cy = u.uy;
83 1.4 christos curs(cx, cy + 2);
84 1.4 christos while ((c = readchar()) != '.') {
85 1.4 christos for (i = 0; i < 8; i++)
86 1.4 christos if (sdir[i] == c) {
87 1.4 christos if (1 <= cx + xdir[i] && cx + xdir[i] <= COLNO)
88 1.4 christos cx += xdir[i];
89 1.4 christos if (0 <= cy + ydir[i] && cy + ydir[i] <= ROWNO - 1)
90 1.4 christos cy += ydir[i];
91 1.4 christos goto nxtc;
92 1.4 christos }
93 1.4 christos if (c == '?') {
94 1.1 cgd pline("Use [hjkl] to move the cursor to %s.", goal);
95 1.1 cgd pline("Type a . when you are at the right place.");
96 1.1 cgd } else {
97 1.1 cgd pline("Unknown direction: '%s' (%s).",
98 1.4 christos visctrl(c),
99 1.4 christos force ? "use hjkl or ." : "aborted");
100 1.4 christos if (force)
101 1.4 christos goto nxtc;
102 1.1 cgd cc.x = -1;
103 1.1 cgd cc.y = 0;
104 1.4 christos return (cc);
105 1.1 cgd }
106 1.4 christos nxtc: ;
107 1.4 christos curs(cx, cy + 2);
108 1.1 cgd }
109 1.1 cgd cc.x = cx;
110 1.1 cgd cc.y = cy;
111 1.4 christos return (cc);
112 1.1 cgd }
113 1.1 cgd
114 1.4 christos int
115 1.4 christos do_mname()
116 1.4 christos {
117 1.4 christos char buf[BUFSZ];
118 1.4 christos coord cc;
119 1.7 dholland int cx, cy, lth;
120 1.7 dholland unsigned i;
121 1.4 christos struct monst *mtmp, *mtmp2;
122 1.1 cgd cc = getpos(0, "the monster you want to name");
123 1.1 cgd cx = cc.x;
124 1.1 cgd cy = cc.y;
125 1.4 christos if (cx < 0)
126 1.4 christos return (0);
127 1.4 christos mtmp = m_at(cx, cy);
128 1.4 christos if (!mtmp) {
129 1.4 christos if (cx == u.ux && cy == u.uy)
130 1.4 christos pline("This ugly monster is called %s and cannot be renamed.",
131 1.4 christos plname);
132 1.4 christos else
133 1.4 christos pline("There is no monster there.");
134 1.4 christos return (1);
135 1.4 christos }
136 1.4 christos if (mtmp->mimic) {
137 1.4 christos pline("I see no monster there.");
138 1.4 christos return (1);
139 1.4 christos }
140 1.4 christos if (!cansee(cx, cy)) {
141 1.4 christos pline("I cannot see a monster there.");
142 1.4 christos return (1);
143 1.1 cgd }
144 1.1 cgd pline("What do you want to call %s? ", lmonnam(mtmp));
145 1.1 cgd getlin(buf);
146 1.1 cgd clrlin();
147 1.4 christos if (!*buf || *buf == '\033')
148 1.4 christos return (1);
149 1.4 christos lth = strlen(buf) + 1;
150 1.4 christos if (lth > 63) {
151 1.1 cgd buf[62] = 0;
152 1.1 cgd lth = 63;
153 1.1 cgd }
154 1.1 cgd mtmp2 = newmonst(mtmp->mxlth + lth);
155 1.1 cgd *mtmp2 = *mtmp;
156 1.4 christos for (i = 0; i < mtmp->mxlth; i++)
157 1.1 cgd ((char *) mtmp2->mextra)[i] = ((char *) mtmp->mextra)[i];
158 1.1 cgd mtmp2->mnamelth = lth;
159 1.1 cgd (void) strcpy(NAME(mtmp2), buf);
160 1.4 christos replmon(mtmp, mtmp2);
161 1.4 christos return (1);
162 1.1 cgd }
163 1.1 cgd
164 1.1 cgd /*
165 1.1 cgd * This routine changes the address of obj . Be careful not to call it
166 1.1 cgd * when there might be pointers around in unknown places. For now: only
167 1.1 cgd * when obj is in the inventory.
168 1.1 cgd */
169 1.4 christos void
170 1.4 christos do_oname(obj)
171 1.4 christos struct obj *obj;
172 1.4 christos {
173 1.4 christos struct obj *otmp, *otmp2;
174 1.4 christos int lth;
175 1.4 christos char buf[BUFSZ];
176 1.1 cgd pline("What do you want to name %s? ", doname(obj));
177 1.1 cgd getlin(buf);
178 1.1 cgd clrlin();
179 1.4 christos if (!*buf || *buf == '\033')
180 1.1 cgd return;
181 1.4 christos lth = strlen(buf) + 1;
182 1.4 christos if (lth > 63) {
183 1.1 cgd buf[62] = 0;
184 1.1 cgd lth = 63;
185 1.1 cgd }
186 1.1 cgd otmp2 = newobj(lth);
187 1.1 cgd *otmp2 = *obj;
188 1.1 cgd otmp2->onamelth = lth;
189 1.1 cgd (void) strcpy(ONAME(otmp2), buf);
190 1.1 cgd
191 1.1 cgd setworn((struct obj *) 0, obj->owornmask);
192 1.1 cgd setworn(otmp2, otmp2->owornmask);
193 1.1 cgd
194 1.4 christos /*
195 1.4 christos * do freeinv(obj); etc. by hand in order to preserve the position of
196 1.4 christos * this object in the inventory
197 1.4 christos */
198 1.4 christos if (obj == invent)
199 1.4 christos invent = otmp2;
200 1.4 christos else
201 1.4 christos for (otmp = invent;; otmp = otmp->nobj) {
202 1.4 christos if (!otmp)
203 1.4 christos panic("Do_oname: cannot find obj.");
204 1.4 christos if (otmp->nobj == obj) {
205 1.4 christos otmp->nobj = otmp2;
206 1.4 christos break;
207 1.4 christos }
208 1.1 cgd }
209 1.4 christos #if 0
210 1.4 christos obfree(obj, otmp2); /* now unnecessary: no pointers on bill */
211 1.4 christos #endif
212 1.1 cgd free((char *) obj); /* let us hope nobody else saved a pointer */
213 1.1 cgd }
214 1.1 cgd
215 1.4 christos int
216 1.1 cgd ddocall()
217 1.1 cgd {
218 1.4 christos struct obj *obj;
219 1.1 cgd
220 1.1 cgd pline("Do you want to name an individual object? [ny] ");
221 1.4 christos switch (readchar()) {
222 1.1 cgd case '\033':
223 1.1 cgd break;
224 1.1 cgd case 'y':
225 1.1 cgd obj = getobj("#", "name");
226 1.4 christos if (obj)
227 1.4 christos do_oname(obj);
228 1.1 cgd break;
229 1.1 cgd default:
230 1.1 cgd obj = getobj("?!=/", "call");
231 1.4 christos if (obj)
232 1.4 christos docall(obj);
233 1.1 cgd }
234 1.4 christos return (0);
235 1.1 cgd }
236 1.1 cgd
237 1.4 christos void
238 1.1 cgd docall(obj)
239 1.4 christos struct obj *obj;
240 1.1 cgd {
241 1.4 christos char buf[BUFSZ];
242 1.4 christos struct obj otemp;
243 1.4 christos char **str1;
244 1.4 christos char *str;
245 1.1 cgd
246 1.1 cgd otemp = *obj;
247 1.1 cgd otemp.quan = 1;
248 1.1 cgd otemp.onamelth = 0;
249 1.1 cgd str = xname(&otemp);
250 1.4 christos pline("Call %s %s: ", strchr(vowels, *str) ? "an" : "a", str);
251 1.1 cgd getlin(buf);
252 1.1 cgd clrlin();
253 1.4 christos if (!*buf || *buf == '\033')
254 1.1 cgd return;
255 1.4 christos str = newstring(strlen(buf) + 1);
256 1.4 christos (void) strcpy(str, buf);
257 1.1 cgd str1 = &(objects[obj->otyp].oc_uname);
258 1.4 christos if (*str1)
259 1.4 christos free(*str1);
260 1.1 cgd *str1 = str;
261 1.1 cgd }
262 1.1 cgd
263 1.5 jsm const char *const ghostnames[] = {/* these names should have length < PL_NSIZ */
264 1.1 cgd "adri", "andries", "andreas", "bert", "david", "dirk", "emile",
265 1.1 cgd "frans", "fred", "greg", "hether", "jay", "john", "jon", "kay",
266 1.1 cgd "kenny", "maud", "michiel", "mike", "peter", "robert", "ron",
267 1.1 cgd "tom", "wilmar"
268 1.1 cgd };
269 1.1 cgd
270 1.4 christos char *
271 1.4 christos xmonnam(mtmp, vb)
272 1.4 christos struct monst *mtmp;
273 1.4 christos int vb;
274 1.4 christos {
275 1.4 christos static char buf[BUFSZ]; /* %% */
276 1.4 christos if (mtmp->mnamelth && !vb) {
277 1.1 cgd (void) strcpy(buf, NAME(mtmp));
278 1.4 christos return (buf);
279 1.1 cgd }
280 1.4 christos switch (mtmp->data->mlet) {
281 1.1 cgd case ' ':
282 1.4 christos {
283 1.5 jsm const char *gn = (char *) mtmp->mextra;
284 1.4 christos if (!*gn) { /* might also look in scorefile */
285 1.4 christos gn = ghostnames[rn2(SIZE(ghostnames))];
286 1.4 christos if (!rn2(2))
287 1.4 christos (void)
288 1.4 christos strcpy((char *) mtmp->mextra, !rn2(5) ? plname : gn);
289 1.4 christos }
290 1.4 christos (void) sprintf(buf, "%s's ghost", gn);
291 1.1 cgd }
292 1.1 cgd break;
293 1.1 cgd case '@':
294 1.4 christos if (mtmp->isshk) {
295 1.1 cgd (void) strcpy(buf, shkname(mtmp));
296 1.1 cgd break;
297 1.1 cgd }
298 1.1 cgd /* fall into next case */
299 1.1 cgd default:
300 1.1 cgd (void) sprintf(buf, "the %s%s",
301 1.4 christos mtmp->minvis ? "invisible " : "",
302 1.4 christos mtmp->data->mname);
303 1.1 cgd }
304 1.4 christos if (vb && mtmp->mnamelth) {
305 1.1 cgd (void) strcat(buf, " called ");
306 1.1 cgd (void) strcat(buf, NAME(mtmp));
307 1.1 cgd }
308 1.4 christos return (buf);
309 1.1 cgd }
310 1.1 cgd
311 1.4 christos char *
312 1.4 christos lmonnam(mtmp)
313 1.4 christos struct monst *mtmp;
314 1.4 christos {
315 1.4 christos return (xmonnam(mtmp, 1));
316 1.1 cgd }
317 1.1 cgd
318 1.4 christos char *
319 1.4 christos monnam(mtmp)
320 1.4 christos struct monst *mtmp;
321 1.4 christos {
322 1.4 christos return (xmonnam(mtmp, 0));
323 1.1 cgd }
324 1.1 cgd
325 1.4 christos char *
326 1.4 christos Monnam(mtmp)
327 1.4 christos struct monst *mtmp;
328 1.4 christos {
329 1.4 christos char *bp = monnam(mtmp);
330 1.4 christos if ('a' <= *bp && *bp <= 'z')
331 1.4 christos *bp += ('A' - 'a');
332 1.4 christos return (bp);
333 1.1 cgd }
334 1.1 cgd
335 1.4 christos char *
336 1.4 christos amonnam(mtmp, adj)
337 1.4 christos struct monst *mtmp;
338 1.5 jsm const char *adj;
339 1.1 cgd {
340 1.4 christos char *bp = monnam(mtmp);
341 1.4 christos static char buf[BUFSZ]; /* %% */
342 1.1 cgd
343 1.4 christos if (!strncmp(bp, "the ", 4))
344 1.4 christos bp += 4;
345 1.1 cgd (void) sprintf(buf, "the %s %s", adj, bp);
346 1.4 christos return (buf);
347 1.1 cgd }
348 1.1 cgd
349 1.4 christos char *
350 1.1 cgd Amonnam(mtmp, adj)
351 1.4 christos struct monst *mtmp;
352 1.5 jsm const char *adj;
353 1.1 cgd {
354 1.4 christos char *bp = amonnam(mtmp, adj);
355 1.1 cgd
356 1.1 cgd *bp = 'T';
357 1.4 christos return (bp);
358 1.1 cgd }
359 1.1 cgd
360 1.4 christos char *
361 1.4 christos Xmonnam(mtmp)
362 1.4 christos struct monst *mtmp;
363 1.4 christos {
364 1.4 christos char *bp = Monnam(mtmp);
365 1.4 christos if (!strncmp(bp, "The ", 4)) {
366 1.1 cgd bp += 2;
367 1.1 cgd *bp = 'A';
368 1.1 cgd }
369 1.4 christos return (bp);
370 1.1 cgd }
371 1.1 cgd
372 1.4 christos char *
373 1.1 cgd visctrl(c)
374 1.4 christos char c;
375 1.1 cgd {
376 1.4 christos static char ccc[3];
377 1.4 christos if (c < 040) {
378 1.1 cgd ccc[0] = '^';
379 1.1 cgd ccc[1] = c + 0100;
380 1.1 cgd ccc[2] = 0;
381 1.1 cgd } else {
382 1.1 cgd ccc[0] = c;
383 1.1 cgd ccc[1] = 0;
384 1.1 cgd }
385 1.4 christos return (ccc);
386 1.1 cgd }
387