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