Home | History | Annotate | Line # | Download | only in hack
hack.do_name.c revision 1.5
      1 /*	$NetBSD: hack.do_name.c,v 1.5 2001/03/25 20:43:59 jsm Exp $	*/
      2 
      3 /*
      4  * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
      5  */
      6 
      7 #include <sys/cdefs.h>
      8 #ifndef lint
      9 __RCSID("$NetBSD: hack.do_name.c,v 1.5 2001/03/25 20:43:59 jsm Exp $");
     10 #endif				/* not lint */
     11 
     12 #include <stdlib.h>
     13 #include "hack.h"
     14 #include "extern.h"
     15 
     16 coord
     17 getpos(force, goal)
     18 	int             force;
     19 	const char           *goal;
     20 {
     21 	int             cx, cy, i, c;
     22 	coord           cc;
     23 	pline("(For instructions type a ?)");
     24 	cx = u.ux;
     25 	cy = u.uy;
     26 	curs(cx, cy + 2);
     27 	while ((c = readchar()) != '.') {
     28 		for (i = 0; i < 8; i++)
     29 			if (sdir[i] == c) {
     30 				if (1 <= cx + xdir[i] && cx + xdir[i] <= COLNO)
     31 					cx += xdir[i];
     32 				if (0 <= cy + ydir[i] && cy + ydir[i] <= ROWNO - 1)
     33 					cy += ydir[i];
     34 				goto nxtc;
     35 			}
     36 		if (c == '?') {
     37 			pline("Use [hjkl] to move the cursor to %s.", goal);
     38 			pline("Type a . when you are at the right place.");
     39 		} else {
     40 			pline("Unknown direction: '%s' (%s).",
     41 			      visctrl(c),
     42 			      force ? "use hjkl or ." : "aborted");
     43 			if (force)
     44 				goto nxtc;
     45 			cc.x = -1;
     46 			cc.y = 0;
     47 			return (cc);
     48 		}
     49 nxtc:		;
     50 		curs(cx, cy + 2);
     51 	}
     52 	cc.x = cx;
     53 	cc.y = cy;
     54 	return (cc);
     55 }
     56 
     57 int
     58 do_mname()
     59 {
     60 	char            buf[BUFSZ];
     61 	coord           cc;
     62 	int             cx, cy, lth, i;
     63 	struct monst   *mtmp, *mtmp2;
     64 	cc = getpos(0, "the monster you want to name");
     65 	cx = cc.x;
     66 	cy = cc.y;
     67 	if (cx < 0)
     68 		return (0);
     69 	mtmp = m_at(cx, cy);
     70 	if (!mtmp) {
     71 		if (cx == u.ux && cy == u.uy)
     72 			pline("This ugly monster is called %s and cannot be renamed.",
     73 			      plname);
     74 		else
     75 			pline("There is no monster there.");
     76 		return (1);
     77 	}
     78 	if (mtmp->mimic) {
     79 		pline("I see no monster there.");
     80 		return (1);
     81 	}
     82 	if (!cansee(cx, cy)) {
     83 		pline("I cannot see a monster there.");
     84 		return (1);
     85 	}
     86 	pline("What do you want to call %s? ", lmonnam(mtmp));
     87 	getlin(buf);
     88 	clrlin();
     89 	if (!*buf || *buf == '\033')
     90 		return (1);
     91 	lth = strlen(buf) + 1;
     92 	if (lth > 63) {
     93 		buf[62] = 0;
     94 		lth = 63;
     95 	}
     96 	mtmp2 = newmonst(mtmp->mxlth + lth);
     97 	*mtmp2 = *mtmp;
     98 	for (i = 0; i < mtmp->mxlth; i++)
     99 		((char *) mtmp2->mextra)[i] = ((char *) mtmp->mextra)[i];
    100 	mtmp2->mnamelth = lth;
    101 	(void) strcpy(NAME(mtmp2), buf);
    102 	replmon(mtmp, mtmp2);
    103 	return (1);
    104 }
    105 
    106 /*
    107  * This routine changes the address of  obj . Be careful not to call it
    108  * when there might be pointers around in unknown places. For now: only
    109  * when  obj  is in the inventory.
    110  */
    111 void
    112 do_oname(obj)
    113 	struct obj     *obj;
    114 {
    115 	struct obj     *otmp, *otmp2;
    116 	int lth;
    117 	char            buf[BUFSZ];
    118 	pline("What do you want to name %s? ", doname(obj));
    119 	getlin(buf);
    120 	clrlin();
    121 	if (!*buf || *buf == '\033')
    122 		return;
    123 	lth = strlen(buf) + 1;
    124 	if (lth > 63) {
    125 		buf[62] = 0;
    126 		lth = 63;
    127 	}
    128 	otmp2 = newobj(lth);
    129 	*otmp2 = *obj;
    130 	otmp2->onamelth = lth;
    131 	(void) strcpy(ONAME(otmp2), buf);
    132 
    133 	setworn((struct obj *) 0, obj->owornmask);
    134 	setworn(otmp2, otmp2->owornmask);
    135 
    136 	/*
    137 	 * do freeinv(obj); etc. by hand in order to preserve the position of
    138 	 * this object in the inventory
    139 	 */
    140 	if (obj == invent)
    141 		invent = otmp2;
    142 	else
    143 		for (otmp = invent;; otmp = otmp->nobj) {
    144 			if (!otmp)
    145 				panic("Do_oname: cannot find obj.");
    146 			if (otmp->nobj == obj) {
    147 				otmp->nobj = otmp2;
    148 				break;
    149 			}
    150 		}
    151 #if 0
    152 	obfree(obj, otmp2);	/* now unnecessary: no pointers on bill */
    153 #endif
    154 	free((char *) obj);	/* let us hope nobody else saved a pointer */
    155 }
    156 
    157 int
    158 ddocall()
    159 {
    160 	struct obj     *obj;
    161 
    162 	pline("Do you want to name an individual object? [ny] ");
    163 	switch (readchar()) {
    164 	case '\033':
    165 		break;
    166 	case 'y':
    167 		obj = getobj("#", "name");
    168 		if (obj)
    169 			do_oname(obj);
    170 		break;
    171 	default:
    172 		obj = getobj("?!=/", "call");
    173 		if (obj)
    174 			docall(obj);
    175 	}
    176 	return (0);
    177 }
    178 
    179 void
    180 docall(obj)
    181 	struct obj     *obj;
    182 {
    183 	char            buf[BUFSZ];
    184 	struct obj      otemp;
    185 	char          **str1;
    186 	char           *str;
    187 
    188 	otemp = *obj;
    189 	otemp.quan = 1;
    190 	otemp.onamelth = 0;
    191 	str = xname(&otemp);
    192 	pline("Call %s %s: ", strchr(vowels, *str) ? "an" : "a", str);
    193 	getlin(buf);
    194 	clrlin();
    195 	if (!*buf || *buf == '\033')
    196 		return;
    197 	str = newstring(strlen(buf) + 1);
    198 	(void) strcpy(str, buf);
    199 	str1 = &(objects[obj->otyp].oc_uname);
    200 	if (*str1)
    201 		free(*str1);
    202 	*str1 = str;
    203 }
    204 
    205 const char *const ghostnames[] = {/* these names should have length < PL_NSIZ */
    206 	"adri", "andries", "andreas", "bert", "david", "dirk", "emile",
    207 	"frans", "fred", "greg", "hether", "jay", "john", "jon", "kay",
    208 	"kenny", "maud", "michiel", "mike", "peter", "robert", "ron",
    209 	"tom", "wilmar"
    210 };
    211 
    212 char           *
    213 xmonnam(mtmp, vb)
    214 	struct monst   *mtmp;
    215 	int             vb;
    216 {
    217 	static char     buf[BUFSZ];	/* %% */
    218 	if (mtmp->mnamelth && !vb) {
    219 		(void) strcpy(buf, NAME(mtmp));
    220 		return (buf);
    221 	}
    222 	switch (mtmp->data->mlet) {
    223 	case ' ':
    224 		{
    225 			const char           *gn = (char *) mtmp->mextra;
    226 			if (!*gn) {	/* might also look in scorefile */
    227 				gn = ghostnames[rn2(SIZE(ghostnames))];
    228 				if (!rn2(2))
    229 					(void)
    230 						strcpy((char *) mtmp->mextra, !rn2(5) ? plname : gn);
    231 			}
    232 			(void) sprintf(buf, "%s's ghost", gn);
    233 		}
    234 		break;
    235 	case '@':
    236 		if (mtmp->isshk) {
    237 			(void) strcpy(buf, shkname(mtmp));
    238 			break;
    239 		}
    240 		/* fall into next case */
    241 	default:
    242 		(void) sprintf(buf, "the %s%s",
    243 			       mtmp->minvis ? "invisible " : "",
    244 			       mtmp->data->mname);
    245 	}
    246 	if (vb && mtmp->mnamelth) {
    247 		(void) strcat(buf, " called ");
    248 		(void) strcat(buf, NAME(mtmp));
    249 	}
    250 	return (buf);
    251 }
    252 
    253 char           *
    254 lmonnam(mtmp)
    255 	struct monst   *mtmp;
    256 {
    257 	return (xmonnam(mtmp, 1));
    258 }
    259 
    260 char           *
    261 monnam(mtmp)
    262 	struct monst   *mtmp;
    263 {
    264 	return (xmonnam(mtmp, 0));
    265 }
    266 
    267 char           *
    268 Monnam(mtmp)
    269 	struct monst   *mtmp;
    270 {
    271 	char           *bp = monnam(mtmp);
    272 	if ('a' <= *bp && *bp <= 'z')
    273 		*bp += ('A' - 'a');
    274 	return (bp);
    275 }
    276 
    277 char           *
    278 amonnam(mtmp, adj)
    279 	struct monst   *mtmp;
    280 	const char           *adj;
    281 {
    282 	char           *bp = monnam(mtmp);
    283 	static char     buf[BUFSZ];	/* %% */
    284 
    285 	if (!strncmp(bp, "the ", 4))
    286 		bp += 4;
    287 	(void) sprintf(buf, "the %s %s", adj, bp);
    288 	return (buf);
    289 }
    290 
    291 char           *
    292 Amonnam(mtmp, adj)
    293 	struct monst   *mtmp;
    294 	const char           *adj;
    295 {
    296 	char           *bp = amonnam(mtmp, adj);
    297 
    298 	*bp = 'T';
    299 	return (bp);
    300 }
    301 
    302 char           *
    303 Xmonnam(mtmp)
    304 	struct monst   *mtmp;
    305 {
    306 	char           *bp = Monnam(mtmp);
    307 	if (!strncmp(bp, "The ", 4)) {
    308 		bp += 2;
    309 		*bp = 'A';
    310 	}
    311 	return (bp);
    312 }
    313 
    314 char           *
    315 visctrl(c)
    316 	char            c;
    317 {
    318 	static char     ccc[3];
    319 	if (c < 040) {
    320 		ccc[0] = '^';
    321 		ccc[1] = c + 0100;
    322 		ccc[2] = 0;
    323 	} else {
    324 		ccc[0] = c;
    325 		ccc[1] = 0;
    326 	}
    327 	return (ccc);
    328 }
    329