Source:NetHack 1.4f/do name.c

From NetHackWiki
Revision as of 00:51, 4 March 2008 by Kernigh bot (talk | contribs) (NetHack 1.4f/do name.c moved to Source:NetHack 1.4f/do name.c: Robot: moved page)
(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 1.4f. To link to a particular line, write [[NetHack 1.4f/do_name.c#line123]], for example.

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

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

1.    /*	SCCS Id: @(#)do_name.c	1.4	87/08/08
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* do_name.c - version 1.0.3 */
4.    
5.    #include <stdio.h>
6.    #include "hack.h"
7.    extern char plname[];
8.    extern char *rndmonnam();
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) register struct obj *obj; {
115.  register struct obj *otmp, *otmp2;
116.  register 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.  	/* do freeinv(obj); etc. by hand in order to preserve
137.  	   the position of this object in the inventory */
138.  	if(obj == invent) invent = otmp2;
139.  	else for(otmp = invent; ; otmp = otmp->nobj){
140.  		if(!otmp)
141.  			panic("Do_oname: cannot find obj.");
142.  		if(otmp->nobj == obj){
143.  			otmp->nobj = otmp2;
144.  			break;
145.  		}
146.  	}
147.  	/* obfree(obj, otmp2);	/* now unnecessary: no pointers on bill */
148.  	free((char *) obj);	/* let us hope nobody else saved a pointer */
149.  }
150.  
151.  ddocall()
152.  {
153.  	register struct obj *obj;
154.  	char	ch;
155.  
156.  #ifdef REDO
157.  	if (!in_doagain)
158.  #endif
159.  		pline("Do you want to name an individual object? [ny] ");
160.  	switch(ch = readchar()) {
161.  	case '\033':
162.  		break;
163.  	case 'y':
164.  #ifdef REDO
165.  		savech(ch);
166.  #endif
167.  		obj = getobj("#", "name");
168.  		if(obj) do_oname(obj);
169.  		break;
170.  	default:
171.  #ifdef REDO
172.  		savech(ch);
173.  #endif
174.  #ifdef KAA
175.  		obj = getobj("?!=/*", "call");
176.  #else
177.  		obj = getobj("?!=/", "call");
178.  #endif
179.  		if(obj) docall(obj);
180.  	}
181.  	return(0);
182.  }
183.  
184.  docall(obj)
185.  register struct obj *obj;
186.  {
187.  	char buf[BUFSZ];
188.  	struct obj otemp;
189.  	register char **str1;
190.  	extern char *xname();
191.  	register char *str;
192.  
193.  	otemp = *obj;
194.  	otemp.quan = 1;
195.  	otemp.onamelth = 0;
196.  	str = xname(&otemp);
197.  	pline("Call %s %s: ", index(vowels,*str) ? "an" : "a", str);
198.  	getlin(buf);
199.  	clrlin();
200.  	if(!*buf || *buf == '\033')
201.  		return;
202.  	str = newstring(strlen(buf)+1);
203.  	(void) strcpy(str,buf);
204.  	str1 = &(objects[obj->otyp].oc_uname);
205.  	if(*str1) free(*str1);
206.  	*str1 = str;
207.  }
208.  
209.  char *ghostnames[] = {		/* these names should have length < PL_NSIZ */
210.  	/* Capitalize the names for asthetics -dgk
211.  	 */
212.  	"Adri", "Andries", "Andreas", "Bert", "David", "Dirk", "Emile",
213.  	"Frans", "Fred", "Greg", "Hether", "Jay", "John", "Jon", "Karnov",
214.  	"Kay", "Kenny", "Maud", "Michiel", "Mike", "Peter", "Robert",
215.  	"Ron", "Tom", "Wilmar", "Nick Danger", "Phoenix", "Miracleman",
216.  	"Stephan"
217.  };
218.  
219.  char *
220.  xmonnam(mtmp, vb) register struct monst *mtmp; int vb; {
221.  static char buf[BUFSZ];		/* %% */
222.  extern char *shkname();
223.  	if(mtmp->mnamelth && !vb) {
224.  		(void) strcpy(buf, NAME(mtmp));
225.  		return(buf);
226.  	}
227.  	switch(mtmp->data->mlet) {
228.  	case ' ':
229.  		{ register char *gn = (char *) mtmp->mextra;
230.  		  if(!*gn) {		/* might also look in scorefile */
231.  		    gn = ghostnames[rn2(SIZE(ghostnames))];
232.  		    if(!rn2(2)) (void)
233.  		      strcpy((char *) mtmp->mextra, !rn2(5) ? plname : gn);
234.  		  }
235.  		  (void) sprintf(buf, "%s's ghost", gn);
236.  		}
237.  		break;
238.  	case '@':
239.  		if(mtmp->isshk) {
240.  			(void) strcpy(buf, shkname(mtmp));
241.  			break;
242.  		}
243.  		/* fall into next case */
244.  	default:
245.  		(void) sprintf(buf, "the %s%s",
246.  			mtmp->minvis ? "invisible " : "",
247.  			(Hallucination ? rndmonnam() : mtmp->data->mname));
248.  	}
249.  	if(vb && mtmp->mnamelth) {
250.  		(void) strcat(buf, " called ");
251.  		(void) strcat(buf, NAME(mtmp));
252.  	}
253.  	return(buf);
254.  }
255.  
256.  char *
257.  lmonnam(mtmp) register struct monst *mtmp; {
258.  	return(xmonnam(mtmp, 1));
259.  }
260.  
261.  char *
262.  monnam(mtmp) register struct monst *mtmp; {
263.  	return(xmonnam(mtmp, 0));
264.  }
265.  
266.  char *
267.  Monnam(mtmp) register struct monst *mtmp; {
268.  register char *bp = monnam(mtmp);
269.  	if('a' <= *bp && *bp <= 'z') *bp += ('A' - 'a');
270.  	return(bp);
271.  }
272.  
273.  char *
274.  amonnam(mtmp,adj)
275.  register struct monst *mtmp;
276.  register char *adj;
277.  {
278.  	register char *bp = monnam(mtmp);
279.  	static char buf[BUFSZ];		/* %% */
280.  
281.  	if(!strncmp(bp, "the ", 4)) bp += 4;
282.  	(void) sprintf(buf, "the %s %s", adj, bp);
283.  	return(buf);
284.  }
285.  
286.  char *
287.  Amonnam(mtmp, adj)
288.  register struct monst *mtmp;
289.  register char *adj;
290.  {
291.  	register char *bp = amonnam(mtmp,adj);
292.  
293.  	*bp = 'T';
294.  	return(bp);
295.  }
296.  
297.  char *
298.  Xmonnam(mtmp) register struct monst *mtmp; {
299.  register char *bp = Monnam(mtmp);
300.  	if(!strncmp(bp, "The ", 4)) {
301.  #ifdef KAA
302.  		if(index("AEIOUaeio",*(bp+4))) {
303.  			bp += 1; *(bp+1) = 'n';
304.  		} else
305.  #endif
306.  			bp += 2;
307.  		*bp = 'A';
308.  	}
309.  	return(bp);
310.  }
311.  
312.  char *
313.  defmonnam(mtmp) register struct monst *mtmp; {
314.  register char *bp = Xmonnam(mtmp);
315.  	if (!strncmp(bp,"A ",2) || !strncmp(bp,"An ",3))
316.  		*bp = 'a';
317.  	return(bp);
318.  }
319.  
320.  char *
321.  rndmonnam() {  /* Random name of monster type, if hallucinating */
322.  int x;
323.  	if ((x=rn2(CMNUM+2)) != CMNUM+1) return (&mons[x])->mname;
324.  	return("giant eel");
325.  }
326.  
327.  char *
328.  visctrl(c)
329.  char c;
330.  {
331.  static char ccc[3];
332.  	if(c < 040) {
333.  		ccc[0] = '^';
334.  		ccc[1] = c + 0100;
335.  		ccc[2] = 0;
336.  	} else {
337.  		ccc[0] = c;
338.  		ccc[1] = 0;
339.  	}
340.  	return(ccc);
341.  }