Fandom

Wikihack

Source:Hack 1.0/hack.do name.c

2,034pages on
this wiki
Add New Page
Talk0

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Below is the full text to hack.do_name.c from the source code of Hack 1.0. To link to a particular line, write [[Hack 1.0/hack.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.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */
2.    
3.    #include "hack.h"
4.    #include <stdio.h>
5.    
6.    coord
7.    getpos(force,goal) int force; char *goal; {
8.    register cx,cy,i,c;
9.    extern char sdir[];		/* defined in hack.c */
10.   extern schar xdir[], ydir[];	/* idem */
11.   extern char *visctrl();		/* see below */
12.   coord cc;
13.   	pline("(For instructions type a ?)");
14.   	cx = u.ux;
15.   	cy = u.uy;
16.   	curs(cx,cy+2);
17.   	while((c = readchar()) != '.'){
18.   		for(i=0; i<8; i++) if(sdir[i] == c){
19.   			if(1 <= cx + xdir[i] && cx + xdir[i] <= COLNO)
20.   				cx += xdir[i];
21.   			if(0 <= cy + ydir[i] && cy + ydir[i] <= ROWNO-1)
22.   				cy += ydir[i];
23.   			goto nxtc;
24.   		}
25.   		if(c == '?'){
26.   			pline("Use [hjkl] to move the cursor to %s.", goal);
27.   			pline("Type a . when you are at the right place.");
28.   		} else {
29.   			pline("unknown direction: '%s' (%s)",
30.   				visctrl(c),
31.   				force ? "use hjkl or ." : "aborted");
32.   			if(force) goto nxtc;
33.   			cc.x = -1;
34.   			cc.y = 0;
35.   			return(cc);
36.   		}
37.   	nxtc:	;
38.   		curs(cx,cy+2);
39.   	}
40.   	cc.x = cx;
41.   	cc.y = cy;
42.   	return(cc);
43.   }
44.   
45.   do_mname(){
46.   char buf[BUFSZ];
47.   coord cc;
48.   register int cx,cy,lth,i;
49.   register struct monst *mtmp, *mtmp2;
50.   extern char *lmonnam();
51.   	cc = getpos(0, "the monster you want to name");
52.   	cx = cc.x;
53.   	cy = cc.y;
54.   	if(cx < 0) return(0);
55.   	mtmp = m_at(cx,cy);
56.   	if(!mtmp){
57.   	    if(cx == u.ux && cy == u.uy){
58.   		extern char plname[];
59.   		pline("This ugly monster is called %s and cannot be renamed.",
60.   		    plname);
61.   	    } else	pline("There is no monster there.");
62.   	    return(1);
63.   	}
64.   	if(mtmp->mimic){
65.   	    pline("I see no monster there.");
66.   	    return(1);
67.   	}
68.   	if(!cansee(cx,cy)) {
69.   	    pline("I cannot see a monster there.");
70.   	    return(1);
71.   	}
72.   	pline("What do you want to call %s? ", lmonnam(mtmp));
73.   	getlin(buf);
74.   	clrlin();
75.   	if(!*buf) return(1);
76.   	lth = strlen(buf)+1;
77.   	if(lth > 63){
78.   		buf[62] = 0;
79.   		lth = 63;
80.   	}
81.   	mtmp2 = newmonst(mtmp->mxlth + lth);
82.   	*mtmp2 = *mtmp;
83.   	for(i=0; i<mtmp->mxlth; i++)
84.   		((char *) mtmp2->mextra)[i] = ((char *) mtmp->mextra)[i];
85.   	mtmp2->mnamelth = lth;
86.   	(void) strcpy(NAME(mtmp2), buf);
87.   	replmon(mtmp,mtmp2);
88.   	if(mtmp2->isshk) setshk();	/* redefine shopkeeper and bill */
89.   	if(mtmp2->isgd) setgd( /* mtmp2 */ );
90.   	return(1);
91.   }
92.   
93.   /*
94.    * This routine changes the address of  obj . Be careful not to call it
95.    * when there might be pointers around in unknown places. For now: only
96.    * when  obj  is in the inventory.
97.    */
98.   do_oname(obj) register struct obj *obj; {
99.   register struct obj *otmp, *otmp2;
100.  register lth;
101.  char buf[BUFSZ];
102.  	pline("What do you want to name %s? ", doname(obj));
103.  	getlin(buf);
104.  	clrlin();
105.  	if(!*buf) return;
106.  	lth = strlen(buf)+1;
107.  	if(lth > 63){
108.  		buf[62] = 0;
109.  		lth = 63;
110.  	}
111.  	otmp2 = newobj(lth);
112.  	*otmp2 = *obj;
113.  	otmp2->onamelth = lth;
114.  	(void) strcpy(ONAME(otmp2), buf);
115.  
116.  	setworn((struct obj *) 0, obj->owornmask);
117.  	setworn(otmp2, otmp2->owornmask);
118.  
119.  	/* do freeinv(obj); etc. by hand in order to preserve
120.  	   the position of this object in the inventory */
121.  	if(obj == invent) invent = otmp2;
122.  	else for(otmp = invent; ; otmp = otmp->nobj){
123.  		if(!otmp)
124.  			panic("Do_oname: cannot find obj.");
125.  		if(otmp->nobj == obj){
126.  			otmp->nobj = otmp2;
127.  			break;
128.  		}
129.  	}
130.  	/* obfree(obj, otmp2);	/* now unnecessary: no pointers on bill */
131.  	free((char *) obj);	/* let us hope nobody else saved a pointer */
132.  }
133.  
134.  ddocall()
135.  {
136.  	register struct obj *obj;
137.  
138.  	pline("Do you want to name an individual object? [yn] ");
139.  	if(readchar() == 'y'){
140.  		obj = getobj("#", "name");
141.  		if(obj) do_oname(obj);
142.  	} else {
143.  		obj = getobj("?!=/", "call");
144.  		if(obj) docall(obj);
145.  	}
146.  	return(0);
147.  }
148.  
149.  docall(obj)
150.  register struct obj *obj;
151.  {
152.  	char buf[BUFSZ];
153.  	register char **str1;
154.  	extern char *xname();
155.  	register char *str = xname(obj);
156.  
157.  	pline("Call %s %s: ", index(vowels,*str) ? "an" : "a", str);
158.  	getlin(buf);
159.  	clrlin();
160.  	if(!*buf) return;
161.  	str = newstring(strlen(buf)+1);
162.  	(void) strcpy(str,buf);
163.  	str1 = &(objects[obj->otyp].oc_uname);
164.  	if(*str1) free(*str1);
165.  	*str1 = str;
166.  }
167.  
168.  char *
169.  xmonnam(mtmp, vb) register struct monst *mtmp; int vb; {
170.  static char buf[BUFSZ];		/* %% */
171.  extern char *shkname();
172.  	if(mtmp->mnamelth && !vb) return(NAME(mtmp));
173.  	switch(mtmp->data->mlet) {
174.  	case ' ':
175.  		(void) sprintf(buf, "%s's ghost", (char *) mtmp->mextra);
176.  		break;
177.  	case '@':
178.  		if(mtmp->isshk) {
179.  			(void) strcpy(buf, shkname());
180.  			break;
181.  		}
182.  		/* fall into next case */
183.  	default:
184.  		(void) sprintf(buf, "the %s%s",
185.  			mtmp->minvis ? "invisible " : "",
186.  			mtmp->data->mname);
187.  	}
188.  	if(vb && mtmp->mnamelth) {
189.  		(void) strcat(buf, " called ");
190.  		(void) strcat(buf, NAME(mtmp));
191.  	}
192.  	return(buf);
193.  }
194.  
195.  char *
196.  lmonnam(mtmp) register struct monst *mtmp; {
197.  	return(xmonnam(mtmp, 1));
198.  }
199.  
200.  char *
201.  monnam(mtmp) register struct monst *mtmp; {
202.  	return(xmonnam(mtmp, 0));
203.  }
204.  
205.  char *
206.  Monnam(mtmp) register struct monst *mtmp; {
207.  register char *bp = monnam(mtmp);
208.  	if('a' <= *bp && *bp <= 'z') *bp += ('A' - 'a');
209.  	return(bp);
210.  }
211.  
212.  char *
213.  amonnam(mtmp,adj)
214.  register struct monst *mtmp;
215.  register char *adj;
216.  {
217.  	register char *bp = monnam(mtmp);
218.  	static char buf[BUFSZ];		/* %% */
219.  
220.  	if(!strncmp(bp, "the ", 4)) bp += 4;
221.  	(void) sprintf(buf, "the %s %s", adj, bp);
222.  	return(buf);
223.  }
224.  
225.  char *
226.  Amonnam(mtmp, adj)
227.  register struct monst *mtmp;
228.  register char *adj;
229.  {
230.  	register char *bp = amonnam(mtmp,adj);
231.  
232.  	*bp = 'T';
233.  	return(bp);
234.  }
235.  
236.  char *
237.  Xmonnam(mtmp) register struct monst *mtmp; {
238.  register char *bp = Monnam(mtmp);
239.  	if(!strncmp(bp, "The ", 4)) {
240.  		bp += 2;
241.  		*bp = 'A';
242.  	}
243.  	return(bp);
244.  }
245.  
246.  char *
247.  visctrl(c)
248.  char c;
249.  {
250.  static char ccc[3];
251.  	if(c < 040) {
252.  		ccc[0] = '^';
253.  		ccc[1] = c + 0100;
254.  		ccc[2] = 0;
255.  	} else {
256.  		ccc[0] = c;
257.  		ccc[1] = 0;
258.  	}
259.  	return(ccc);
260.  }

Also on Fandom

Random Wiki