Source:NetHack 3.6.1/src/mapglyph.c

From NetHackWiki
(Redirected from Source:Ref/mapglyph)
Jump to: navigation, search

Below is the full text to mapglyph.c from the source code of NetHack 3.6.1. To link to a particular line, write [[Source:NetHack 3.6.1/src/mapglyph.c#line123]], for example.

Top of file

  1.  /* NetHack 3.6	mapglyph.c	$NHDT-Date: 1448175698 2015/11/22 07:01:38 $  $NHDT-Branch: master $:$NHDT-Revision: 1.40 $ */
  2.  /* Copyright (c) David Cohrs, 1991                                */
  3.  /* NetHack may be freely redistributed.  See license for details. */

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.  
  2.  #include "hack.h"
  3.  #if defined(TTY_GRAPHICS)
  4.  #include "wintty.h" /* for prototype of has_color() only */
  5.  #endif
  6.  #include "color.h"
  7.  #define HI_DOMESTIC CLR_WHITE /* monst.c */
  8.  
  9.  static int explcolors[] = {
  10.      CLR_BLACK,   /* dark    */
  11.      CLR_GREEN,   /* noxious */
  12.      CLR_BROWN,   /* muddy   */
  13.      CLR_BLUE,    /* wet     */
  14.      CLR_MAGENTA, /* magical */
  15.      CLR_ORANGE,  /* fiery   */
  16.      CLR_WHITE,   /* frosty  */
  17.  };
  18.  
  19.  #if !defined(TTY_GRAPHICS)
  20.  #define has_color(n) TRUE
  21.  #endif
  22.  
  23.  #ifdef TEXTCOLOR
  24.  #define zap_color(n) color = iflags.use_color ? zapcolors[n] : NO_COLOR
  25.  #define cmap_color(n) color = iflags.use_color ? defsyms[n].color : NO_COLOR
  26.  #define obj_color(n) color = iflags.use_color ? objects[n].oc_color : NO_COLOR
  27.  #define mon_color(n) color = iflags.use_color ? mons[n].mcolor : NO_COLOR
  28.  #define invis_color(n) color = NO_COLOR
  29.  #define pet_color(n) color = iflags.use_color ? mons[n].mcolor : NO_COLOR
  30.  #define warn_color(n) \
  31.      color = iflags.use_color ? def_warnsyms[n].color : NO_COLOR
  32.  #define explode_color(n) color = iflags.use_color ? explcolors[n] : NO_COLOR
  33.  
  34.  #else /* no text color */
  35.  
  36.  #define zap_color(n)
  37.  #define cmap_color(n)
  38.  #define obj_color(n)
  39.  #define mon_color(n)
  40.  #define invis_color(n)
  41.  #define pet_color(c)
  42.  #define warn_color(n)
  43.  #define explode_color(n)
  44.  #endif
  45.  
  46.  #if defined(USE_TILES) && defined(MSDOS)
  47.  #define HAS_ROGUE_IBM_GRAPHICS \
  48.      (currentgraphics == ROGUESET && SYMHANDLING(H_IBM) && !iflags.grmode)
  49.  #else
  50.  #define HAS_ROGUE_IBM_GRAPHICS \
  51.      (currentgraphics == ROGUESET && SYMHANDLING(H_IBM))
  52.  #endif
  53.  
  54.  #define is_objpile(x,y) (!Hallucination && level.objects[(x)][(y)] \
  55.                           && level.objects[(x)][(y)]->nexthere)
  56.  
  57.  /*ARGSUSED*/

mapglyph

  1.  int
  2.  mapglyph(glyph, ochar, ocolor, ospecial, x, y)
  3.  int glyph, *ocolor, x, y;
  4.  int *ochar;
  5.  unsigned *ospecial;
  6.  {
  7.      register int offset, idx;
  8.      int color = NO_COLOR;
  9.      nhsym ch;
  10.      unsigned special = 0;
  11.      /* condense multiple tests in macro version down to single */
  12.      boolean has_rogue_ibm_graphics = HAS_ROGUE_IBM_GRAPHICS;
  13.      boolean has_rogue_color = (has_rogue_ibm_graphics
  14.                                 && symset[currentgraphics].nocolor == 0);
  15.  
  16.      /*
  17.       *  Map the glyph back to a character and color.
  18.       *
  19.       *  Warning:  For speed, this makes an assumption on the order of
  20.       *            offsets.  The order is set in display.h.
  21.       */
  22.      if ((offset = (glyph - GLYPH_STATUE_OFF)) >= 0) { /* a statue */
  23.          idx = mons[offset].mlet + SYM_OFF_M;
  24.          if (has_rogue_color)
  25.              color = CLR_RED;
  26.          else
  27.              obj_color(STATUE);
  28.          special |= MG_STATUE;
  29.          if (is_objpile(x,y))
  30.              special |= MG_OBJPILE;
  31.      } else if ((offset = (glyph - GLYPH_WARNING_OFF)) >= 0) { /* warn flash */
  32.          idx = offset + SYM_OFF_W;
  33.          if (has_rogue_color)
  34.              color = NO_COLOR;
  35.          else
  36.              warn_color(offset);
  37.      } else if ((offset = (glyph - GLYPH_SWALLOW_OFF)) >= 0) { /* swallow */
  38.          /* see swallow_to_glyph() in display.c */
  39.          idx = (S_sw_tl + (offset & 0x7)) + SYM_OFF_P;
  40.          if (has_rogue_color && iflags.use_color)
  41.              color = NO_COLOR;
  42.          else
  43.              mon_color(offset >> 3);
  44.      } else if ((offset = (glyph - GLYPH_ZAP_OFF)) >= 0) { /* zap beam */
  45.          /* see zapdir_to_glyph() in display.c */
  46.          idx = (S_vbeam + (offset & 0x3)) + SYM_OFF_P;
  47.          if (has_rogue_color && iflags.use_color)
  48.              color = NO_COLOR;
  49.          else
  50.              zap_color((offset >> 2));
  51.      } else if ((offset = (glyph - GLYPH_EXPLODE_OFF)) >= 0) { /* explosion */
  52.          idx = ((offset % MAXEXPCHARS) + S_explode1) + SYM_OFF_P;
  53.          explode_color(offset / MAXEXPCHARS);
  54.      } else if ((offset = (glyph - GLYPH_CMAP_OFF)) >= 0) { /* cmap */
  55.          idx = offset + SYM_OFF_P;
  56.          if (has_rogue_color && iflags.use_color) {
  57.              if (offset >= S_vwall && offset <= S_hcdoor)
  58.                  color = CLR_BROWN;
  59.              else if (offset >= S_arrow_trap && offset <= S_polymorph_trap)
  60.                  color = CLR_MAGENTA;
  61.              else if (offset == S_corr || offset == S_litcorr)
  62.                  color = CLR_GRAY;
  63.              else if (offset >= S_room && offset <= S_water
  64.                       && offset != S_darkroom)
  65.                  color = CLR_GREEN;
  66.              else
  67.                  color = NO_COLOR;
  68.  #ifdef TEXTCOLOR
  69.          /* provide a visible difference if normal and lit corridor
  70.             use the same symbol */
  71.          } else if (iflags.use_color && offset == S_litcorr
  72.                     && showsyms[idx] == showsyms[S_corr + SYM_OFF_P]) {
  73.              color = CLR_WHITE;
  74.  #endif
  75.          /* try to provide a visible difference between water and lava
  76.             if they use the same symbol and color is disabled */
  77.          } else if (!iflags.use_color && offset == S_lava
  78.                     && (showsyms[idx] == showsyms[S_pool + SYM_OFF_P]
  79.                         || showsyms[idx] == showsyms[S_water + SYM_OFF_P])) {
  80.              special |= MG_BW_LAVA;
  81.          } else {
  82.              cmap_color(offset);
  83.          }
  84.      } else if ((offset = (glyph - GLYPH_OBJ_OFF)) >= 0) { /* object */
  85.          idx = objects[offset].oc_class + SYM_OFF_O;
  86.          if (offset == BOULDER)
  87.              idx = SYM_BOULDER + SYM_OFF_X;
  88.          if (has_rogue_color && iflags.use_color) {
  89.              switch (objects[offset].oc_class) {
  90.              case COIN_CLASS:
  91.                  color = CLR_YELLOW;
  92.                  break;
  93.              case FOOD_CLASS:
  94.                  color = CLR_RED;
  95.                  break;
  96.              default:
  97.                  color = CLR_BRIGHT_BLUE;
  98.                  break;
  99.              }
  100.          } else
  101.              obj_color(offset);
  102.          if (offset != BOULDER && is_objpile(x,y))
  103.              special |= MG_OBJPILE;
  104.      } else if ((offset = (glyph - GLYPH_RIDDEN_OFF)) >= 0) { /* mon ridden */
  105.          idx = mons[offset].mlet + SYM_OFF_M;
  106.          if (has_rogue_color)
  107.              /* This currently implies that the hero is here -- monsters */
  108.              /* don't ride (yet...).  Should we set it to yellow like in */
  109.              /* the monster case below?  There is no equivalent in rogue. */
  110.              color = NO_COLOR; /* no need to check iflags.use_color */
  111.          else
  112.              mon_color(offset);
  113.          special |= MG_RIDDEN;
  114.      } else if ((offset = (glyph - GLYPH_BODY_OFF)) >= 0) { /* a corpse */
  115.          idx = objects[CORPSE].oc_class + SYM_OFF_O;
  116.          if (has_rogue_color && iflags.use_color)
  117.              color = CLR_RED;
  118.          else
  119.              mon_color(offset);
  120.          special |= MG_CORPSE;
  121.          if (is_objpile(x,y))
  122.              special |= MG_OBJPILE;
  123.      } else if ((offset = (glyph - GLYPH_DETECT_OFF)) >= 0) { /* mon detect */
  124.          idx = mons[offset].mlet + SYM_OFF_M;
  125.          if (has_rogue_color)
  126.              color = NO_COLOR; /* no need to check iflags.use_color */
  127.          else
  128.              mon_color(offset);
  129.          /* Disabled for now; anyone want to get reverse video to work? */
  130.          /* is_reverse = TRUE; */
  131.          special |= MG_DETECT;
  132.      } else if ((offset = (glyph - GLYPH_INVIS_OFF)) >= 0) { /* invisible */
  133.          idx = SYM_INVISIBLE + SYM_OFF_X;
  134.          if (has_rogue_color)
  135.              color = NO_COLOR; /* no need to check iflags.use_color */
  136.          else
  137.              invis_color(offset);
  138.          special |= MG_INVIS;
  139.      } else if ((offset = (glyph - GLYPH_PET_OFF)) >= 0) { /* a pet */
  140.          idx = mons[offset].mlet + SYM_OFF_M;
  141.          if (has_rogue_color)
  142.              color = NO_COLOR; /* no need to check iflags.use_color */
  143.          else
  144.              pet_color(offset);
  145.          special |= MG_PET;
  146.      } else { /* a monster */
  147.          idx = mons[glyph].mlet + SYM_OFF_M;
  148.          if (has_rogue_color && iflags.use_color) {
  149.              if (x == u.ux && y == u.uy)
  150.                  /* actually player should be yellow-on-gray if in corridor */
  151.                  color = CLR_YELLOW;
  152.              else
  153.                  color = NO_COLOR;
  154.          } else {
  155.              mon_color(glyph);
  156.  #ifdef TEXTCOLOR
  157.              /* special case the hero for `showrace' option */
  158.              if (iflags.use_color && x == u.ux && y == u.uy
  159.                  && flags.showrace && !Upolyd)
  160.                  color = HI_DOMESTIC;
  161.  #endif
  162.          }
  163.      }
  164.  
  165.      ch = showsyms[idx];
  166.  #ifdef TEXTCOLOR
  167.      /* Turn off color if no color defined, or rogue level w/o PC graphics. */
  168.      if (!has_color(color) || (Is_rogue_level(&u.uz) && !has_rogue_color))
  169.          color = NO_COLOR;
  170.  #endif
  171.  
  172.      *ochar = (int) ch;
  173.      *ospecial = special;
  174.  #ifdef TEXTCOLOR
  175.      *ocolor = color;
  176.  #endif
  177.      return idx;
  178.  }
  179.  

encglyph

  1.  char *
  2.  encglyph(glyph)
  3.  int glyph;
  4.  {
  5.      static char encbuf[20];
  6.  
  7.      Sprintf(encbuf, "\\G%04X%04X", context.rndencode, glyph);
  8.      return encbuf;
  9.  }
  10.  

genl_putmixed

  1.  /*
  2.   * This differs from putstr() because the str parameter can
  3.   * contain a sequence of characters representing:
  4.   *        \GXXXXNNNN    a glyph value, encoded by encglyph().
  5.   *
  6.   * For window ports that haven't yet written their own
  7.   * XXX_putmixed() routine, this general one can be used.
  8.   * It replaces the encoded glyph sequence with a single
  9.   * showsyms[] char, then just passes that string onto
  10.   * putstr().
  11.   */
  12.  
  13.  void
  14.  genl_putmixed(window, attr, str)
  15.  winid window;
  16.  int attr;
  17.  const char *str;
  18.  {
  19.      static const char hex[] = "00112233445566778899aAbBcCdDeEfF";
  20.      char buf[BUFSZ];
  21.      const char *cp = str;
  22.      char *put = buf;
  23.  
  24.      while (*cp) {
  25.          if (*cp == '\\') {
  26.              int rndchk, dcount, so, gv, ch = 0, oc = 0;
  27.              unsigned os = 0;
  28.              const char *dp, *save_cp;
  29.  
  30.              save_cp = cp++;
  31.              switch (*cp) {
  32.              case 'G': /* glyph value \GXXXXNNNN*/
  33.                  rndchk = dcount = 0;
  34.                  for (++cp; *cp && ++dcount <= 4; ++cp)
  35.                      if ((dp = index(hex, *cp)) != 0)
  36.                          rndchk = (rndchk * 16) + ((int) (dp - hex) / 2);
  37.                      else
  38.                          break;
  39.                  if (rndchk == context.rndencode) {
  40.                      gv = dcount = 0;
  41.                      for (; *cp && ++dcount <= 4; ++cp)
  42.                          if ((dp = index(hex, *cp)) != 0)
  43.                              gv = (gv * 16) + ((int) (dp - hex) / 2);
  44.                          else
  45.                              break;
  46.                      so = mapglyph(gv, &ch, &oc, &os, 0, 0);
  47.                      *put++ = showsyms[so];
  48.                      /* 'cp' is ready for the next loop iteration and '*cp'
  49.                         should not be copied at the end of this iteration */
  50.                      continue;
  51.                  } else {
  52.                      /* possible forgery - leave it the way it is */
  53.                      cp = save_cp;
  54.                  }
  55.                  break;
  56.  #if 0
  57.              case 'S': /* symbol offset */
  58.                  so = rndchk = dcount = 0;
  59.                  for (++cp; *cp && ++dcount <= 4; ++cp)
  60.                      if ((dp = index(hex, *cp)) != 0)
  61.                          rndchk = (rndchk * 16) + ((int) (dp - hex) / 2);
  62.                      else
  63.                          break;
  64.                  if (rndchk == context.rndencode) {
  65.                      dcount = 0;
  66.                      for (; *cp && ++dcount <= 2; ++cp)
  67.                          if ((dp = index(hex, *cp)) != 0)
  68.                              so = (so * 16) + ((int) (dp - hex) / 2);
  69.                          else
  70.                              break;
  71.                  }
  72.                  *put++ = showsyms[so];
  73.                  break;
  74.  #endif
  75.              case '\\':
  76.                  break;
  77.              }
  78.          }
  79.          *put++ = *cp++;
  80.      }
  81.      *put = '\0';
  82.      /* now send it to the normal putstr */
  83.      putstr(window, attr, buf);
  84.  }
  85.  
  86.  /*mapglyph.c*/