Source:NetHack 3.6.3/src/mapglyph.c
Jump to navigation
Jump to search
Below is the full text to mapglyph.c from the source code of NetHack 3.6.3. To link to a particular line, write [[Source:NetHack 3.6.3/src/mapglyph.c#line12]], for example.
The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.
1. /* NetHack 3.6 mapglyph.c $NHDT-Date: 1573943501 2019/11/16 22:31:41 $ $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.51 $ */ 2. /* Copyright (c) David Cohrs, 1991 */ 3. /* NetHack may be freely redistributed. See license for details. */ 4. 5. #include "hack.h" 6. #if defined(TTY_GRAPHICS) 7. #include "wintty.h" /* for prototype of has_color() only */ 8. #endif 9. #include "color.h" 10. #define HI_DOMESTIC CLR_WHITE /* monst.c */ 11. 12. #if !defined(TTY_GRAPHICS) 13. #define has_color(n) TRUE 14. #endif 15. 16. #ifdef TEXTCOLOR 17. static const int explcolors[] = { 18. CLR_BLACK, /* dark */ 19. CLR_GREEN, /* noxious */ 20. CLR_BROWN, /* muddy */ 21. CLR_BLUE, /* wet */ 22. CLR_MAGENTA, /* magical */ 23. CLR_ORANGE, /* fiery */ 24. CLR_WHITE, /* frosty */ 25. }; 26. 27. #define zap_color(n) color = iflags.use_color ? zapcolors[n] : NO_COLOR 28. #define cmap_color(n) color = iflags.use_color ? defsyms[n].color : NO_COLOR 29. #define obj_color(n) color = iflags.use_color ? objects[n].oc_color : NO_COLOR 30. #define mon_color(n) color = iflags.use_color ? mons[n].mcolor : NO_COLOR 31. #define invis_color(n) color = NO_COLOR 32. #define pet_color(n) color = iflags.use_color ? mons[n].mcolor : NO_COLOR 33. #define warn_color(n) \ 34. color = iflags.use_color ? def_warnsyms[n].color : NO_COLOR 35. #define explode_color(n) color = iflags.use_color ? explcolors[n] : NO_COLOR 36. 37. #else /* no text color */ 38. 39. #define zap_color(n) 40. #define cmap_color(n) 41. #define obj_color(n) 42. #define mon_color(n) 43. #define invis_color(n) 44. #define pet_color(c) 45. #define warn_color(n) 46. #define explode_color(n) 47. #endif 48. 49. #if defined(USE_TILES) && defined(MSDOS) 50. #define HAS_ROGUE_IBM_GRAPHICS \ 51. (currentgraphics == ROGUESET && SYMHANDLING(H_IBM) && !iflags.grmode) 52. #else 53. #define HAS_ROGUE_IBM_GRAPHICS \ 54. (currentgraphics == ROGUESET && SYMHANDLING(H_IBM)) 55. #endif 56. 57. #define is_objpile(x,y) (!Hallucination && level.objects[(x)][(y)] \ 58. && level.objects[(x)][(y)]->nexthere) 59. 60. /*ARGSUSED*/ 61. int 62. mapglyph(glyph, ochar, ocolor, ospecial, x, y, mgflags) 63. int glyph, *ocolor, x, y; 64. int *ochar; 65. unsigned *ospecial; 66. unsigned mgflags; 67. { 68. register int offset, idx; 69. int color = NO_COLOR; 70. nhsym ch; 71. unsigned special = 0; 72. /* condense multiple tests in macro version down to single */ 73. boolean has_rogue_ibm_graphics = HAS_ROGUE_IBM_GRAPHICS, 74. is_you = (x == u.ux && y == u.uy), 75. has_rogue_color = (has_rogue_ibm_graphics 76. && symset[currentgraphics].nocolor == 0); 77. 78. /* 79. * Map the glyph back to a character and color. 80. * 81. * Warning: For speed, this makes an assumption on the order of 82. * offsets. The order is set in display.h. 83. */ 84. if ((offset = (glyph - GLYPH_STATUE_OFF)) >= 0) { /* a statue */ 85. idx = mons[offset].mlet + SYM_OFF_M; 86. if (has_rogue_color) 87. color = CLR_RED; 88. else 89. obj_color(STATUE); 90. special |= MG_STATUE; 91. if (is_objpile(x,y)) 92. special |= MG_OBJPILE; 93. } else if ((offset = (glyph - GLYPH_WARNING_OFF)) >= 0) { /* warn flash */ 94. idx = offset + SYM_OFF_W; 95. if (has_rogue_color) 96. color = NO_COLOR; 97. else 98. warn_color(offset); 99. } else if ((offset = (glyph - GLYPH_SWALLOW_OFF)) >= 0) { /* swallow */ 100. /* see swallow_to_glyph() in display.c */ 101. idx = (S_sw_tl + (offset & 0x7)) + SYM_OFF_P; 102. if (has_rogue_color && iflags.use_color) 103. color = NO_COLOR; 104. else 105. mon_color(offset >> 3); 106. } else if ((offset = (glyph - GLYPH_ZAP_OFF)) >= 0) { /* zap beam */ 107. /* see zapdir_to_glyph() in display.c */ 108. idx = (S_vbeam + (offset & 0x3)) + SYM_OFF_P; 109. if (has_rogue_color && iflags.use_color) 110. color = NO_COLOR; 111. else 112. zap_color((offset >> 2)); 113. } else if ((offset = (glyph - GLYPH_EXPLODE_OFF)) >= 0) { /* explosion */ 114. idx = ((offset % MAXEXPCHARS) + S_explode1) + SYM_OFF_P; 115. explode_color(offset / MAXEXPCHARS); 116. } else if ((offset = (glyph - GLYPH_CMAP_OFF)) >= 0) { /* cmap */ 117. idx = offset + SYM_OFF_P; 118. if (has_rogue_color && iflags.use_color) { 119. if (offset >= S_vwall && offset <= S_hcdoor) 120. color = CLR_BROWN; 121. else if (offset >= S_arrow_trap && offset <= S_polymorph_trap) 122. color = CLR_MAGENTA; 123. else if (offset == S_corr || offset == S_litcorr) 124. color = CLR_GRAY; 125. else if (offset >= S_room && offset <= S_water 126. && offset != S_darkroom) 127. color = CLR_GREEN; 128. else 129. color = NO_COLOR; 130. #ifdef TEXTCOLOR 131. /* provide a visible difference if normal and lit corridor 132. use the same symbol */ 133. } else if (iflags.use_color && offset == S_litcorr 134. && showsyms[idx] == showsyms[S_corr + SYM_OFF_P]) { 135. color = CLR_WHITE; 136. #endif 137. /* try to provide a visible difference between water and lava 138. if they use the same symbol and color is disabled */ 139. } else if (!iflags.use_color && offset == S_lava 140. && (showsyms[idx] == showsyms[S_pool + SYM_OFF_P] 141. || showsyms[idx] == showsyms[S_water + SYM_OFF_P])) { 142. special |= MG_BW_LAVA; 143. } else { 144. cmap_color(offset); 145. } 146. } else if ((offset = (glyph - GLYPH_OBJ_OFF)) >= 0) { /* object */ 147. idx = objects[offset].oc_class + SYM_OFF_O; 148. if (offset == BOULDER) 149. idx = SYM_BOULDER + SYM_OFF_X; 150. if (has_rogue_color && iflags.use_color) { 151. switch (objects[offset].oc_class) { 152. case COIN_CLASS: 153. color = CLR_YELLOW; 154. break; 155. case FOOD_CLASS: 156. color = CLR_RED; 157. break; 158. default: 159. color = CLR_BRIGHT_BLUE; 160. break; 161. } 162. } else 163. obj_color(offset); 164. if (offset != BOULDER && is_objpile(x,y)) 165. special |= MG_OBJPILE; 166. } else if ((offset = (glyph - GLYPH_RIDDEN_OFF)) >= 0) { /* mon ridden */ 167. idx = mons[offset].mlet + SYM_OFF_M; 168. if (has_rogue_color) 169. /* This currently implies that the hero is here -- monsters */ 170. /* don't ride (yet...). Should we set it to yellow like in */ 171. /* the monster case below? There is no equivalent in rogue. */ 172. color = NO_COLOR; /* no need to check iflags.use_color */ 173. else 174. mon_color(offset); 175. special |= MG_RIDDEN; 176. } else if ((offset = (glyph - GLYPH_BODY_OFF)) >= 0) { /* a corpse */ 177. idx = objects[CORPSE].oc_class + SYM_OFF_O; 178. if (has_rogue_color && iflags.use_color) 179. color = CLR_RED; 180. else 181. mon_color(offset); 182. special |= MG_CORPSE; 183. if (is_objpile(x,y)) 184. special |= MG_OBJPILE; 185. } else if ((offset = (glyph - GLYPH_DETECT_OFF)) >= 0) { /* mon detect */ 186. idx = mons[offset].mlet + SYM_OFF_M; 187. if (has_rogue_color) 188. color = NO_COLOR; /* no need to check iflags.use_color */ 189. else 190. mon_color(offset); 191. /* Disabled for now; anyone want to get reverse video to work? */ 192. /* is_reverse = TRUE; */ 193. special |= MG_DETECT; 194. } else if ((offset = (glyph - GLYPH_INVIS_OFF)) >= 0) { /* invisible */ 195. idx = SYM_INVISIBLE + SYM_OFF_X; 196. if (has_rogue_color) 197. color = NO_COLOR; /* no need to check iflags.use_color */ 198. else 199. invis_color(offset); 200. special |= MG_INVIS; 201. } else if ((offset = (glyph - GLYPH_PET_OFF)) >= 0) { /* a pet */ 202. idx = mons[offset].mlet + SYM_OFF_M; 203. if (has_rogue_color) 204. color = NO_COLOR; /* no need to check iflags.use_color */ 205. else 206. pet_color(offset); 207. special |= MG_PET; 208. } else { /* a monster */ 209. idx = mons[glyph].mlet + SYM_OFF_M; 210. if (has_rogue_color && iflags.use_color) { 211. if (is_you) 212. /* actually player should be yellow-on-gray if in corridor */ 213. color = CLR_YELLOW; 214. else 215. color = NO_COLOR; 216. } else { 217. mon_color(glyph); 218. #ifdef TEXTCOLOR 219. /* special case the hero for `showrace' option */ 220. if (iflags.use_color && is_you && flags.showrace && !Upolyd) 221. color = HI_DOMESTIC; 222. #endif 223. } 224. } 225. 226. /* These were requested by a blind player to enhance screen reader use */ 227. if (sysopt.accessibility == 1 && !(mgflags & MG_FLAG_NOOVERRIDE)) { 228. int ovidx; 229. 230. if ((special & MG_PET) != 0) { 231. ovidx = SYM_PET_OVERRIDE + SYM_OFF_X; 232. if (Is_rogue_level(&u.uz) ? ov_rogue_syms[ovidx] 233. : ov_primary_syms[ovidx]) 234. idx = ovidx; 235. } 236. if (is_you) { 237. ovidx = SYM_HERO_OVERRIDE + SYM_OFF_X; 238. if (Is_rogue_level(&u.uz) ? ov_rogue_syms[ovidx] 239. : ov_primary_syms[ovidx]) 240. idx = ovidx; 241. } 242. } 243. 244. ch = showsyms[idx]; 245. #ifdef TEXTCOLOR 246. /* Turn off color if no color defined, or rogue level w/o PC graphics. */ 247. if (!has_color(color) || (Is_rogue_level(&u.uz) && !has_rogue_color)) 248. #endif 249. color = NO_COLOR; 250. *ochar = (int) ch; 251. *ospecial = special; 252. *ocolor = color; 253. return idx; 254. } 255. 256. char * 257. encglyph(glyph) 258. int glyph; 259. { 260. static char encbuf[20]; /* 10+1 would suffice */ 261. 262. Sprintf(encbuf, "\\G%04X%04X", context.rndencode, glyph); 263. return encbuf; 264. } 265. 266. char * 267. decode_mixed(buf, str) 268. char *buf; 269. const char *str; 270. { 271. static const char hex[] = "00112233445566778899aAbBcCdDeEfF"; 272. char *put = buf; 273. 274. if (!str) 275. return strcpy(buf, ""); 276. 277. while (*str) { 278. if (*str == '\\') { 279. int rndchk, dcount, so, gv, ch = 0, oc = 0; 280. unsigned os = 0; 281. const char *dp, *save_str; 282. 283. save_str = str++; 284. switch (*str) { 285. case 'G': /* glyph value \GXXXXNNNN*/ 286. rndchk = dcount = 0; 287. for (++str; *str && ++dcount <= 4; ++str) 288. if ((dp = index(hex, *str)) != 0) 289. rndchk = (rndchk * 16) + ((int) (dp - hex) / 2); 290. else 291. break; 292. if (rndchk == context.rndencode) { 293. gv = dcount = 0; 294. for (; *str && ++dcount <= 4; ++str) 295. if ((dp = index(hex, *str)) != 0) 296. gv = (gv * 16) + ((int) (dp - hex) / 2); 297. else 298. break; 299. so = mapglyph(gv, &ch, &oc, &os, 0, 0, 0); 300. *put++ = showsyms[so]; 301. /* 'str' is ready for the next loop iteration and '*str' 302. should not be copied at the end of this iteration */ 303. continue; 304. } else { 305. /* possible forgery - leave it the way it is */ 306. str = save_str; 307. } 308. break; 309. #if 0 310. case 'S': /* symbol offset */ 311. so = rndchk = dcount = 0; 312. for (++str; *str && ++dcount <= 4; ++str) 313. if ((dp = index(hex, *str)) != 0) 314. rndchk = (rndchk * 16) + ((int) (dp - hex) / 2); 315. else 316. break; 317. if (rndchk == context.rndencode) { 318. dcount = 0; 319. for (; *str && ++dcount <= 2; ++str) 320. if ((dp = index(hex, *str)) != 0) 321. so = (so * 16) + ((int) (dp - hex) / 2); 322. else 323. break; 324. } 325. *put++ = showsyms[so]; 326. break; 327. #endif 328. case '\\': 329. break; 330. case '\0': 331. /* String ended with '\\'. This can happen when someone 332. names an object with a name ending with '\\', drops the 333. named object on the floor nearby and does a look at all 334. nearby objects. */ 335. /* brh - should we perhaps not allow things to have names 336. that contain '\\' */ 337. str = save_str; 338. break; 339. } 340. } 341. *put++ = *str++; 342. } 343. *put = '\0'; 344. return buf; 345. } 346. 347. /* 348. * This differs from putstr() because the str parameter can 349. * contain a sequence of characters representing: 350. * \GXXXXNNNN a glyph value, encoded by encglyph(). 351. * 352. * For window ports that haven't yet written their own 353. * XXX_putmixed() routine, this general one can be used. 354. * It replaces the encoded glyph sequence with a single 355. * showsyms[] char, then just passes that string onto 356. * putstr(). 357. */ 358. 359. void 360. genl_putmixed(window, attr, str) 361. winid window; 362. int attr; 363. const char *str; 364. { 365. char buf[BUFSZ]; 366. 367. /* now send it to the normal putstr */ 368. putstr(window, attr, decode_mixed(buf, str)); 369. } 370. 371. /*mapglyph.c*/