Source:NetHack 2.3e/do name.c

From NetHackWiki
Revision as of 00:13, 4 July 2024 by Furey (talk | contribs) (Delete link-to-particular-source instructions. Source code covers that now. "the latest release" -> "newer releases".)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Below is the full text to do_name.c from the source code of NetHack 2.3e.

Warning! This is the source code from an old release. For newer releases, see Source code

Screenshots and source code from Hack are used under the CWI license.

1.    /*	SCCS Id: @(#)do_name.c	2.3	88/02/11
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    
4.    #include <stdio.h>
5.    #include "hack.h"
6.    extern char plname[];
7.    extern char *rndmonnam();
8.    extern void savech();
9.    
10.   getpos(cc,force,goal)
11.   coord	*cc;
12.   int force; char *goal;
13.   {
14.   register cx,cy,i,c;
15.   extern char sdir[];		/* defined in hack.c */
16.   extern schar xdir[], ydir[];	/* idem */
17.   extern char *visctrl();		/* see below */
18.   	pline("(For instructions type a ?)");
19.   	cx = u.ux;
20.   	cy = u.uy;
21.   	curs(cx,cy+2);
22.   	while((c = readchar()) != '.'){
23.   		for(i=0; i<8; i++) if(sdir[i] == c){
24.   			if(1 <= cx + xdir[i] && cx + xdir[i] <= COLNO)
25.   				cx += xdir[i];
26.   			if(0 <= cy + ydir[i] && cy + ydir[i] <= ROWNO-1)
27.   				cy += ydir[i];
28.   			goto nxtc;
29.   		}
30.   		if(c == '?'){
31.   			pline("Use [hjkl] to move the cursor to %s.", goal);
32.   			pline("Type a . when you are at the right place.");
33.   		} else {
34.   			pline("Unknown direction: '%s' (%s).",
35.   				visctrl(c),
36.   				force ? "use hjkl or ." : "aborted");
37.   			if(force) goto nxtc;
38.   			cc->x = -1;
39.   			cc->y = 0;
40.   			return(0);
41.   		}
42.   	nxtc:	;
43.   		curs(cx,cy+2);
44.   	}
45.   	cc->x = cx;
46.   	cc->y = cy;
47.   	return(0);
48.   }
49.   
50.   do_mname(){
51.   char buf[BUFSZ];
52.   coord cc;
53.   register int cx,cy,lth,i;
54.   register struct monst *mtmp, *mtmp2;
55.   extern char *lmonnam();
56.   	getpos(&cc, 0, "the monster you want to name");
57.   	cx = cc.x;
58.   	cy = cc.y;
59.   	if(cx < 0) return(0);
60.   #ifdef DGKMOD
61.   	if (cx == u.ux && cy == u.uy) {
62.   		pline("This ugly monster is called %s and cannot be renamed.",
63.   		plname);
64.   		return(1);
65.   	}
66.   	if (!cansee(cx, cy) || !(mtmp = m_at(cx, cy)) || mtmp->mimic) {
67.   		pline("I see no monster there.");
68.   		return(1);
69.   	}
70.   #else
71.   	mtmp = m_at(cx,cy);
72.   	if(!mtmp){
73.   	    if(cx == u.ux && cy == u.uy)
74.   		pline("This ugly monster is called %s and cannot be renamed.",
75.   		    plname);
76.   	    else
77.   		pline("There is no monster there.");
78.   	    return(1);
79.   	}
80.   	if(mtmp->mimic){
81.   	    pline("I see no monster there.");
82.   	    return(1);
83.   	}
84.   	if(!cansee(cx,cy)) {
85.   	    pline("I cannot see a monster there.");
86.   	    return(1);
87.   	}
88.   #endif
89.   	pline("What do you want to call %s? ", lmonnam(mtmp));
90.   	getlin(buf);
91.   	clrlin();
92.   	if(!*buf || *buf == '\033')
93.   		return(1);
94.   	lth = strlen(buf)+1;
95.   	if(lth > 63){
96.   		buf[62] = 0;
97.   		lth = 63;
98.   	}
99.   	mtmp2 = newmonst(mtmp->mxlth + lth);
100.  	*mtmp2 = *mtmp;
101.  	for(i=0; i<mtmp->mxlth; i++)
102.  		((char *) mtmp2->mextra)[i] = ((char *) mtmp->mextra)[i];
103.  	mtmp2->mnamelth = lth;
104.  	(void) strcpy(NAME(mtmp2), buf);
105.  	replmon(mtmp,mtmp2);
106.  	return(1);
107.  }
108.  
109.  /*
110.   * This routine changes the address of  obj . Be careful not to call it
111.   * when there might be pointers around in unknown places. For now: only
112.   * when  obj  is in the inventory.
113.   */
114.  do_oname(obj)
115.  	register struct obj *obj;
116.  {
117.  	char buf[BUFSZ];
118.  
119.  	pline("What do you want to name %s? ", doname(obj));
120.  	getlin(buf);
121.  	clrlin();
122.  	if(!*buf || *buf == '\033')	return;
123.  #ifdef RPH
124.  	if(!strcmp(buf, "Excalibur")) {
125.  		pline("Somehow you can't seem to engrave that word.");
126.  		return;
127.  	}
128.  #endif
129.  	oname(obj, buf);
130.  }
131.  
132.  oname(obj, buf)
133.  	register struct obj *obj;
134.  	char	*buf;
135.  {
136.  register struct obj *otmp, *otmp2;
137.  register int	lth;
138.  
139.  	lth = strlen(buf)+1;
140.  	if(lth > 63){
141.  		buf[62] = 0;
142.  		lth = 63;
143.  	}
144.  	otmp2 = newobj(lth);
145.  	*otmp2 = *obj;
146.  	otmp2->onamelth = lth;
147.  	(void) strcpy(ONAME(otmp2), buf);
148.  
149.  	setworn((struct obj *) 0, obj->owornmask);
150.  	setworn(otmp2, otmp2->owornmask);
151.  
152.  	/* do freeinv(obj); etc. by hand in order to preserve
153.  	   the position of this object in the inventory */
154.  	if(obj == invent) invent = otmp2;
155.  	else for(otmp = invent; ; otmp = otmp->nobj){
156.  		if(!otmp)
157.  			panic("oname: cannot find obj.");
158.  		if(otmp->nobj == obj){
159.  			otmp->nobj = otmp2;
160.  			break;
161.  		}
162.  	}
163.  	/* obfree(obj, otmp2);	/* now unnecessary: no pointers on bill */
164.  	free((char *) obj);	/* let us hope nobody else saved a pointer */
165.  }
166.  
167.  ddocall()
168.  {
169.  	register struct obj *obj;
170.  	char	ch;
171.  
172.  #ifdef REDO
173.  	if (!in_doagain)
174.  #endif
175.  		pline("Do you want to name an individual object? [ny] ");
176.  	switch(ch = readchar()) {
177.  	case '\033':
178.  		break;
179.  	case 'y':
180.  #ifdef REDO
181.  		savech(ch);
182.  #endif
183.  		obj = getobj("#", "name");
184.  		if(obj) do_oname(obj);
185.  		break;
186.  	default:
187.  #ifdef REDO
188.  		savech(ch);
189.  #endif
190.  #ifdef KAA
191.  		obj = getobj("?!=/*", "call");
192.  #else
193.  		obj = getobj("?!=/", "call");
194.  #endif
195.  		if(obj) docall(obj);
196.  	}
197.  	return(0);
198.  }
199.  
200.  docall(obj)
201.  register struct obj *obj;
202.  {
203.  	char buf[BUFSZ];
204.  	struct obj otemp;
205.  	register char **str1;
206.  	extern char *xname();
207.  	register char *str;
208.  
209.  	otemp = *obj;
210.  	otemp.quan = 1;
211.  	otemp.onamelth = 0;
212.  	str = xname(&otemp);
213.  	pline("Call %s %s: ", index(vowels,*str) ? "an" : "a", str);
214.  	getlin(buf);
215.  	clrlin();
216.  	if(!*buf || *buf == '\033')
217.  		return;
218.  	str = newstring(strlen(buf)+1);
219.  	(void) strcpy(str,buf);
220.  	str1 = &(objects[obj->otyp].oc_uname);
221.  	if(*str1) free(*str1);
222.  	*str1 = str;
223.  }
224.  
225.  char *ghostnames[] = {		/* these names should have length < PL_NSIZ */
226.  	/* Capitalize the names for asthetics -dgk
227.  	 */
228.  	"Adri", "Andries", "Andreas", "Bert", "David", "Dirk", "Emile",
229.  	"Frans", "Fred", "Greg", "Hether", "Jay", "John", "Jon", "Karnov",
230.  	"Kay", "Kenny", "Kevin", "Maud", "Michiel", "Mike", "Peter", "Robert",
231.  	"Ron", "Tom", "Wilmar", "Nick Danger", "Phoenix", "Miracleman",
232.  	"Stephan"
233.  };
234.  
235.  char *
236.  xmonnam(mtmp, vb) register struct monst *mtmp; int vb; {
237.  static char buf[BUFSZ];		/* %% */
238.  extern char *shkname();
239.  	if(mtmp->mnamelth && !vb) {
240.  		(void) strcpy(buf, NAME(mtmp));
241.  		return(buf);
242.  	}
243.  	switch(mtmp->data->mlet) {
244.  	case ' ':
245.  		{ register char *gn = (char *) mtmp->mextra;
246.  		  if(!*gn) {		/* might also look in scorefile */
247.  		    gn = ghostnames[rn2(SIZE(ghostnames))];
248.  		    if(!rn2(2)) (void)
249.  		      strcpy((char *) mtmp->mextra, !rn2(5) ? plname : gn);
250.  		  }
251.  		  (void) sprintf(buf, "%s's ghost", gn);
252.  		}
253.  		break;
254.  	case '@':
255.  		if(mtmp->isshk) {
256.  			(void) strcpy(buf, shkname(mtmp));
257.  			break;
258.  		}
259.  #ifdef STOOGES
260.  		if(mtmp->isstooge) {
261.  			(void) strcpy(buf, mtmp->data->mname);
262.  			break;
263.  		}
264.  #endif
265.  		/* fall into next case */
266.  	default:
267.  		(void) sprintf(buf, "the %s%s",
268.  			mtmp->minvis ? "invisible " : "",
269.  			(Hallucination ? rndmonnam() : mtmp->data->mname));
270.  	}
271.  	if(vb && mtmp->mnamelth) {
272.  		(void) strcat(buf, " called ");
273.  		(void) strcat(buf, NAME(mtmp));
274.  	}
275.  	return(buf);
276.  }
277.  
278.  char *
279.  lmonnam(mtmp) register struct monst *mtmp; {
280.  	return(xmonnam(mtmp, 1));
281.  }
282.  
283.  char *
284.  monnam(mtmp) register struct monst *mtmp; {
285.  	return(xmonnam(mtmp, 0));
286.  }
287.  
288.  char *
289.  Monnam(mtmp) register struct monst *mtmp; {
290.  register char *bp = monnam(mtmp);
291.  	if('a' <= *bp && *bp <= 'z') *bp += ('A' - 'a');
292.  	return(bp);
293.  }
294.  
295.  char *
296.  amonnam(mtmp,adj)
297.  register struct monst *mtmp;
298.  register char *adj;
299.  {
300.  	register char *bp = monnam(mtmp);
301.  	static char buf[BUFSZ];		/* %% */
302.  
303.  	if(!strncmp(bp, "the ", 4)) bp += 4;
304.  	(void) sprintf(buf, "the %s %s", adj, bp);
305.  	return(buf);
306.  }
307.  
308.  char *
309.  Amonnam(mtmp, adj)
310.  register struct monst *mtmp;
311.  register char *adj;
312.  {
313.  	register char *bp = amonnam(mtmp,adj);
314.  
315.  	*bp = 'T';
316.  	return(bp);
317.  }
318.  
319.  char *
320.  Xmonnam(mtmp) register struct monst *mtmp; {
321.  register char *bp = Monnam(mtmp);
322.  	if(!strncmp(bp, "The ", 4)) {
323.  #ifdef KAA
324.  		if(index("AEIOUaeio",*(bp+4))) {
325.  			bp += 1; *(bp+1) = 'n';
326.  		} else
327.  #endif
328.  			bp += 2;
329.  		*bp = 'A';
330.  	}
331.  	return(bp);
332.  }
333.  
334.  char *
335.  defmonnam(mtmp) register struct monst *mtmp; {
336.  register char *bp = Xmonnam(mtmp);
337.  	if (!strncmp(bp,"A ",2) || !strncmp(bp,"An ",3))
338.  		*bp = 'a';
339.  	return(bp);
340.  }
341.  
342.  char *
343.  rndmonnam() {  /* Random name of monster type, if hallucinating */
344.  int x;
345.  	if ((x=rn2(CMNUM+2)) != CMNUM+1) return (&mons[x])->mname;
346.  	return("giant eel");
347.  }
348.  
349.  char *
350.  visctrl(c)
351.  char c;
352.  {
353.  static char ccc[3];
354.  	if(c < 040) {
355.  		ccc[0] = '^';
356.  		ccc[1] = c + 0100;
357.  		ccc[2] = 0;
358.  	} else {
359.  		ccc[0] = c;
360.  		ccc[1] = 0;
361.  	}
362.  	return(ccc);
363.  }