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