home *** CD-ROM | disk | FTP | other *** search
/ The Complete Doom Accessory Pack 2 / TheCompleteDoomAccessoryPackVolumeII.iso / editors / vnb / blockmap.c next >
C/C++ Source or Header  |  1994-05-24  |  8KB  |  191 lines

  1. /******************************************************************************
  2.     MODULE:        BLOCKMAP.C
  3.     WRITTEN BY:    Robert Fenske, Jr. (rfenske@swri.edu)
  4.                 Southwest Research Institute
  5.                 Electromagnetics Division
  6.                 6220 Culebra
  7.                 San Antonio, Texas 78238-5166
  8.     CREATED:    Feb. 1994
  9.     DESCRIPTION:    This module contains routines to generate the BLOCKMAP
  10.             section.  See the generation routine for an explanation
  11.             of the method used to generate the BLOCKMAP.  The
  12.             optimizations for the horizontal LINEDEF, vertical
  13.             LINEDEF, and single block cases came from ideas
  14.             presented in the Unofficial DOOM Specs written by
  15.             Matt Fell.
  16.  
  17.             DOOM is a trademark of id Software, Inc.
  18. ******************************************************************************/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include "dmglobal.i"
  23.  
  24. local short *Blockmap;                /* built BLOCKMAP */
  25.  
  26.  
  27. /******************************************************************************
  28.     ROUTINE:    blockmap_add_line(block,lndx,nlines)
  29.     WRITTEN BY:    Robert Fenske, Jr.
  30.     CREATED:    Feb. 1994
  31.     DESCRIPTION:    This routine adds the LINEDEF lndx to the block's
  32.             block LINEDEF list.  If no list exists yet for the
  33.             block, one is created.  Memory is allocated for no more
  34.             than fifty LINEDEFS (to save memory); thus this routine
  35.             assumes that no more than fifty LINEDEFS will be in any
  36.             one block.
  37. ******************************************************************************/
  38. void blockmap_add_line(block,lndx,nlines)
  39. register short **block;
  40. int lndx, nlines;
  41. {
  42.   register int k;
  43.  
  44.   if (*block == NULL)                /* allocate if no list yet */
  45.     *block = blockmem(short,min(nlines,50));
  46.   for (k = 0; (*block)[k]; k++);        /* seek to end of list */
  47.   (*block)[k] = lndx+1;                /* add this LINEDEF */
  48. }
  49.  
  50.  
  51. /******************************************************************************
  52.     ROUTINE:    blockmap_make(blockmap,lines,nlines,vert)
  53.     WRITTEN BY:    Robert Fenske, Jr.
  54.     CREATED:    Feb. 1994
  55.     DESCRIPTION:    This routine builds the BLOCKMAP section given the
  56.             LINEDEFS and VERTEXES.  The BLOCKMAP section has the
  57.             following information (all are 16-bit words):
  58.  
  59.             block grid X origin
  60.             block grid Y origin
  61.             # blocks along X axis (total # blocks --> )
  62.             # blocks along Y axis (N = Xn * Yn        )
  63.             block 0 offset (# words)
  64.             block 1 offset (# words)
  65.                 .
  66.                 .
  67.             block N-1 offset (# words)
  68.             block 0 data (M words: 0,LINEDEFs in block 0,FFFF)
  69.             block 1 data (M words: 0,LINEDEFs in block 1,FFFF)
  70.                 .
  71.                 .
  72.             block N-1 data (M words: 0,LINEDEFs in block N-1,FFFF)
  73.  
  74.             Block 0 is at the lower left, block 1 is to the right
  75.             of block 0 along the x-axis, ..., block N-1 is at the
  76.             upper right.  An N-element pointer array is allocated
  77.             to hold pointers to the list of LINEDEFS in each block.
  78.             If no LINEDEFS occur within a block, it's pointer will
  79.             be NULL.  Then the LINEDEFS are scanned to find the
  80.             blocks each line occupies, building the lists of
  81.             LINEDEFS along the way.  Four cases are considered
  82.             for each LINEDEF.  The line is either diagonal,
  83.             horizontal, vertical, or resides in a single block
  84.             (regardless of orientation).  The non-diagonal cases
  85.             can be optimized since the blocks occupied can be
  86.             directly calculated.  The diagonal case basically
  87.             computes the blocks occupied on each row for all the
  88.             rows between the LINEDEF endpoints.  Once this is
  89.             complete the actual blockmap is allocated and filled.
  90.             It returns the number of words in the blockmap.
  91.     MODIFIED:        Robert Fenske, Jr.    Mar. 1994
  92.             Added in the optimizations for the orthogonal line
  93.             cases using the ideas presented in the Unofficial DOOM
  94.             Specs written by Matt Fell.
  95. ******************************************************************************/
  96. int blockmap_make(blockmap,lines,nlines,vert)
  97. register short **blockmap;            /* built BLOCKMAP */
  98. register DOOM_LINE *lines;            /* map LINEDEFS */
  99. int nlines;                    /* map # lines */
  100. DOOM_VERT *vert;                /* map VERTEXES */
  101. {
  102.   short xmin,ymin, xmax,ymax;            /* map coords min/max */
  103.   long scl = 1000;                /* line following scaling */
  104.   short size = 0x80;                /* block size (map coords) */
  105.   short xorig, yorig;                /* blockmap x,y origin */
  106.   short xn, yn;                    /* # blocks in x,y dirs */
  107.   short xcc, xcl;
  108.   long xf,yf, xt,yt;
  109.   long xd, yd;                    /* x direction, y direction */
  110.   int o;
  111.   int l, k, i, p, c, m;
  112.   register short **boxlist;            /* array of blocks' lists */
  113.   register int b;
  114.   register int t;
  115.  
  116.   xmin = ymin = (short)0x7FFF, xmax = ymax = (short)0x8000;
  117.   for (l = 0; l < nlines; l++) {        /* find min/max map coords */
  118.     xf = vert[lines[l].fndx].x, yf = vert[lines[l].fndx].y;
  119.     if (xf < xmin) xmin = (short)xf;        /* check from vertex */
  120.     if (yf < ymin) ymin = (short)yf;
  121.     if (xmax < xf) xmax = (short)xf;
  122.     if (ymax < yf) ymax = (short)yf;
  123.     xt = vert[lines[l].tndx].x, yt = vert[lines[l].tndx].y;
  124.     if (xt < xmin) xmin = (short)xt;        /* check to vertex */
  125.     if (yt < ymin) ymin = (short)yt;
  126.     if (xmax < xt) xmax = (short)xt;
  127.     if (ymax < yt) ymax = (short)yt;
  128.   }
  129.   xorig = xmin-8;                /* get x origin */
  130.   yorig = ymin-8;                /* get y origin */
  131.   xn = (xmax-xmin+size)/size;            /* get # in x direction */
  132.   yn = (ymax-ymin+size)/size;            /* get # in y direction */
  133.   boxlist = blockmem(short *,xn*yn);
  134.   t = 0;                    /* total len of all lists */
  135.   for (l = 0; l < nlines; l++) {        /* scan LINEDEFS here */
  136.     xf = vert[lines[l].fndx].x-xorig;
  137.     yf = vert[lines[l].fndx].y-yorig;
  138.     xt = vert[lines[l].tndx].x-xorig;
  139.     yt = vert[lines[l].tndx].y-yorig;
  140.     xd = sgn(xt-xf), yd = sgn(yt-yf);
  141.     switch (2*(xf/size == xt/size) + (yf/size == yt/size)) {
  142.      bcase 0: c=0,                      p=yd*xn;/* diagonal line */
  143.      bcase 1: c=abs(xt/size-xf/size)+1, p=xd;    /* horizontal line */
  144.      bcase 2: c=abs(yt/size-yf/size)+1, p=yd*xn;/* vertical line */
  145.      bcase 3: c=0+1,                    p=1;    /* start,end in same block */
  146.     }
  147.     b = xf/size + xn*(yf/size);            /* start @ this block */
  148.     for (i = 0; i < c; i++, b+=p) {        /* add to lists for special */
  149.       blockmap_add_line(&boxlist[b],l,nlines);    /* cases: horizontal,       */
  150.       t++;                    /* vertical, & single block */
  151.     }
  152.     if (c == 0) {                /* handle diagonal lines */
  153.       m = scl*(yt-yf)/(xt-xf);            /* spanning > 1 block    */
  154.       xcl = (short)xf;
  155.       if (yd == -1) xcc = xf + scl*((yf/size)*size -   1 - yf)/m;
  156.       else          xcc = xf + scl*((yf/size)*size + 128 - yf)/m;
  157.       do {
  158.         for (c = 0; c < abs(xcc/size - xcl/size) + 1; c++, b+=xd) {
  159.           blockmap_add_line(&boxlist[b],l,nlines);
  160.           t++;
  161.         }
  162.         b += p-xd;
  163.         xcl = xcc, xcc += yd*scl*size/m;
  164.         if (xd*xcc > xd*xt) xcc = (short)xt;    /* don't overrun endpoint */
  165.       } while (xd*xcl < xd*xt);
  166.     }
  167.   }
  168.   blockfree(Blockmap);
  169.   Blockmap = blockmem(short,4+xn*yn+t+2*xn*yn);
  170.   t = 0;
  171.   Blockmap[t++] = xorig;            /* fill in X,Y origin */
  172.   Blockmap[t++] = yorig;
  173.   Blockmap[t++] = xn;                /* fill in # in X and */
  174.   Blockmap[t++] = yn;                /* Y directions       */
  175.   o = t;
  176.   t += xn*yn;
  177.   for (i = 0; i < xn*yn; i++) {            /* now fill in BLOCKMAP */
  178.     Blockmap[o++] = t;                /* offset in BLOCKMAP */
  179.     Blockmap[t++] = 0;                /* always zero */
  180.     if (boxlist[i] != NULL) {
  181.       for (k = 0; boxlist[i][k]; k++)        /* list of lines in this */
  182.         Blockmap[t++] = boxlist[i][k]-1;    /* block                 */
  183.       blockfree(boxlist[i]);
  184.     }
  185.     Blockmap[t++] = -1;                /* always -1 */
  186.   }
  187.   blockfree(boxlist);
  188.   *blockmap = Blockmap;
  189.   return t;                    /* # words in BLOCKMAP */
  190. }
  191.