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