Source:NetHack 3.6.0/src/mapglyph.c

From NetHackWiki
(Redirected from Source:Mapglyph.c)
Jump to: navigation, search

Below is the full text to mapglyph.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/mapglyph.c#line123]], 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.

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. */
  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.  static int explcolors[] = {
  13.      CLR_BLACK,   /* dark    */
  14.      CLR_GREEN,   /* noxious */
  15.      CLR_BROWN,   /* muddy   */
  16.      CLR_BLUE,    /* wet     */
  17.      CLR_MAGENTA, /* magical */
  18.      CLR_ORANGE,  /* fiery   */
  19.      CLR_WHITE,   /* frosty  */
  20.  };
  21.  
  22.  #if !defined(TTY_GRAPHICS)
  23.  #define has_color(n) TRUE
  24.  #endif
  25.  
  26.  #ifdef TEXTCOLOR
  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.  /*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 (level.objects[x][y] && level.objects[x][y]->nexthere)
  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.          } else {
  76.              cmap_color(offset);
  77.          }
  78.      } else if ((offset = (glyph - GLYPH_OBJ_OFF)) >= 0) { /* object */
  79.          idx = objects[offset].oc_class + SYM_OFF_O;
  80.          if (offset == BOULDER)
  81.              idx = SYM_BOULDER + SYM_OFF_X;
  82.          if (has_rogue_color && iflags.use_color) {
  83.              switch (objects[offset].oc_class) {
  84.              case COIN_CLASS:
  85.                  color = CLR_YELLOW;
  86.                  break;
  87.              case FOOD_CLASS:
  88.                  color = CLR_RED;
  89.                  break;
  90.              default:
  91.                  color = CLR_BRIGHT_BLUE;
  92.                  break;
  93.              }
  94.          } else
  95.              obj_color(offset);
  96.          if (offset != BOULDER && level.objects[x][y]
  97.              && level.objects[x][y]->nexthere)
  98.              special |= MG_OBJPILE;
  99.      } else if ((offset = (glyph - GLYPH_RIDDEN_OFF)) >= 0) { /* mon ridden */
  100.          idx = mons[offset].mlet + SYM_OFF_M;
  101.          if (has_rogue_color)
  102.              /* This currently implies that the hero is here -- monsters */
  103.              /* don't ride (yet...).  Should we set it to yellow like in */
  104.              /* the monster case below?  There is no equivalent in rogue. */
  105.              color = NO_COLOR; /* no need to check iflags.use_color */
  106.          else
  107.              mon_color(offset);
  108.          special |= MG_RIDDEN;
  109.      } else if ((offset = (glyph - GLYPH_BODY_OFF)) >= 0) { /* a corpse */
  110.          idx = objects[CORPSE].oc_class + SYM_OFF_O;
  111.          if (has_rogue_color && iflags.use_color)
  112.              color = CLR_RED;
  113.          else
  114.              mon_color(offset);
  115.          special |= MG_CORPSE;
  116.          if (level.objects[x][y] && level.objects[x][y]->nexthere)
  117.              special |= MG_OBJPILE;
  118.      } else if ((offset = (glyph - GLYPH_DETECT_OFF)) >= 0) { /* mon detect */
  119.          idx = mons[offset].mlet + SYM_OFF_M;
  120.          if (has_rogue_color)
  121.              color = NO_COLOR; /* no need to check iflags.use_color */
  122.          else
  123.              mon_color(offset);
  124.          /* Disabled for now; anyone want to get reverse video to work? */
  125.          /* is_reverse = TRUE; */
  126.          special |= MG_DETECT;
  127.      } else if ((offset = (glyph - GLYPH_INVIS_OFF)) >= 0) { /* invisible */
  128.          idx = SYM_INVISIBLE + SYM_OFF_X;
  129.          if (has_rogue_color)
  130.              color = NO_COLOR; /* no need to check iflags.use_color */
  131.          else
  132.              invis_color(offset);
  133.          special |= MG_INVIS;
  134.      } else if ((offset = (glyph - GLYPH_PET_OFF)) >= 0) { /* a pet */
  135.          idx = mons[offset].mlet + SYM_OFF_M;
  136.          if (has_rogue_color)
  137.              color = NO_COLOR; /* no need to check iflags.use_color */
  138.          else
  139.              pet_color(offset);
  140.          special |= MG_PET;
  141.      } else { /* a monster */
  142.          idx = mons[glyph].mlet + SYM_OFF_M;
  143.          if (has_rogue_color && iflags.use_color) {
  144.              if (x == u.ux && y == u.uy)
  145.                  /* actually player should be yellow-on-gray if in corridor */
  146.                  color = CLR_YELLOW;
  147.              else
  148.                  color = NO_COLOR;
  149.          } else {
  150.              mon_color(glyph);
  151.  #ifdef TEXTCOLOR
  152.              /* special case the hero for `showrace' option */
  153.              if (iflags.use_color && x == u.ux && y == u.uy
  154.                  && flags.showrace && !Upolyd)
  155.                  color = HI_DOMESTIC;
  156.  #endif
  157.          }
  158.      }
  159.  
  160.      ch = showsyms[idx];
  161.  #ifdef TEXTCOLOR
  162.      /* Turn off color if no color defined, or rogue level w/o PC graphics. */
  163.      if (!has_color(color) || (Is_rogue_level(&u.uz) && !has_rogue_color))
  164.          color = NO_COLOR;
  165.  #endif
  166.  
  167.      *ochar = (int) ch;
  168.      *ospecial = special;
  169.  #ifdef TEXTCOLOR
  170.      *ocolor = color;
  171.  #endif
  172.      return idx;
  173.  }
  174.  

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*/