home *** CD-ROM | disk | FTP | other *** search
/ Deathday Collection / dday.bin / edit / doombsp / saveblck.m < prev    next >
Text File  |  1994-04-06  |  4KB  |  212 lines

  1. // saveblocks.m
  2.  
  3. #import "doombsp.h"
  4.  
  5. short    datalist[0x10000], *data_p;
  6. float    orgx, orgy;
  7.  
  8. #define    BLOCKSIZE    128
  9.  
  10.  
  11. float        xl, xh, yl, yh;
  12.  
  13.  
  14. boolean    LineContact (worldline_t *wl)
  15. {
  16.     NXPoint        *p1, *p2, pt1, pt2;
  17.     float        lxl, lxh, lyl, lyh;
  18.     divline_t    ld;
  19.     int            s1,s2;
  20.     
  21.     p1 = &wl->p1;
  22.     p2 = &wl->p2;
  23.     ld.pt.x = p1->x;
  24.     ld.pt.y = p1->y;
  25.     ld.dx = p2->x - p1->x;
  26.     ld.dy = p2->y - p1->y;
  27.     
  28.     if (p1->x < p2->x)
  29.     {
  30.         lxl = p1->x;
  31.         lxh = p2->x;
  32.     }
  33.     else
  34.     {
  35.         lxl = p2->x;
  36.         lxh = p1->x;
  37.     }
  38.     if (p1->y < p2->y)
  39.     {
  40.         lyl = p1->y;
  41.         lyh = p2->y;
  42.     }
  43.     else
  44.     {
  45.         lyl = p2->y;
  46.         lyh = p1->y;
  47.     }
  48.  
  49.     if (lxl >= xh || lxh < xl || lyl >= yh || lyh < yl)        
  50.         return false;    // no bbox intersections
  51.  
  52.     if ( ld.dy / ld.dx > 0)
  53.     {    // positive slope
  54.         pt1.x = xl;
  55.         pt1.y = yh;
  56.         pt2.x = xh;
  57.         pt2.y = yl;
  58.     }
  59.     else
  60.     {    // negetive slope
  61.         pt1.x = xh;
  62.         pt1.y = yh;
  63.         pt2.x = xl;
  64.         pt2.y = yl;
  65.     }
  66.     
  67.     s1 = PointOnSide (&pt1, &ld);
  68.     s2 = PointOnSide (&pt2, &ld);
  69.     
  70.     return s1 != s2;
  71. }
  72.  
  73.  
  74. /*
  75. ================
  76. =
  77. = GenerateBlockList
  78. =
  79. ================
  80. */
  81.  
  82. void GenerateBlockList (int x, int y)
  83. {
  84.     NXRect        r;
  85.     worldline_t    *wl;
  86.     int            count, i;
  87.     
  88.     *data_p++ = 0;        // leave space for thing links
  89.     
  90.     xl = orgx + x*BLOCKSIZE;
  91.     xh = xl+BLOCKSIZE;
  92.     yl = orgy + y*BLOCKSIZE;
  93.     yh = yl+BLOCKSIZE;
  94.     
  95.     r.origin.x = xl;
  96.     r.origin.y = yl;
  97.     r.size.width = r.size.height = BLOCKSIZE;
  98.     if (draw)
  99.         NXEraseRect (&r);
  100.     
  101.     count = [linestore_i count];
  102.     wl = [linestore_i elementAt: 0];
  103.     
  104.     for (i=0 ; i<count ; i++,wl++)
  105.     {
  106.         if (wl->p1.x == wl->p2.x)
  107.         {    // vertical
  108.             if (wl->p1.x < xl || wl->p1.x >= xh)
  109.                 continue;
  110.             if (wl->p1.y < wl->p2.y)
  111.             {
  112.                 if (wl->p1.y >= yh || wl->p2.y < yl)
  113.                     continue;
  114.             }
  115.             else
  116.             {
  117.                 if (wl->p2.y >= yh || wl->p1.y < yl)
  118.                     continue;
  119.             }
  120.             *data_p++ = SHORT(i);
  121.             continue;
  122.         }
  123.         if (wl->p1.y == wl->p2.y)
  124.         {    // horizontal
  125.             if (wl->p1.y < yl || wl->p1.y >= yh)
  126.                 continue;
  127.             if (wl->p1.x < wl->p2.x)
  128.             {
  129.                 if (wl->p1.x >= xh || wl->p2.x < xl)
  130.                     continue;
  131.             }
  132.             else
  133.             {
  134.                 if (wl->p2.x >= xh || wl->p1.x < xl)
  135.                     continue;
  136.             }
  137.             *data_p++ = SHORT(i);
  138.             continue;
  139.         }
  140.         // diagonal
  141.         if (LineContact (wl) ) 
  142.             *data_p++ = SHORT(i);
  143.     }
  144.     
  145.     *data_p++ = -1;        // end of list marker
  146. }
  147.  
  148.  
  149. /*
  150. ================
  151. =
  152. = SaveBlocks
  153. =
  154. block lump holds:
  155. orgx
  156. orgy
  157. blockwidth
  158. blockheight
  159. listoffset[blockwidth*blockheight]
  160. lists
  161.     one short left blank for thing list
  162.     linedef numbers
  163.     -1 terminator
  164.     
  165. ================
  166. */
  167.  
  168. void SaveBlocks (void)
  169. {
  170.     int        blockwidth, blockheight;
  171.     int        x,y, len;
  172.     short    *pointer_p;
  173.     
  174.     blockwidth = (worldbounds.size.width+BLOCKSIZE-1)/BLOCKSIZE;
  175.     blockheight = (worldbounds.size.height+BLOCKSIZE-1)/BLOCKSIZE;
  176.     orgx = worldbounds.origin.x;
  177.     orgy = worldbounds.origin.y;
  178.     
  179.     pointer_p = datalist;
  180.     *pointer_p++ = SHORT(orgx);
  181.     *pointer_p++ = SHORT(orgy);
  182.     *pointer_p++ = SHORT(blockwidth);
  183.     *pointer_p++ = SHORT(blockheight);
  184.     
  185.     data_p = pointer_p + blockwidth*blockheight;
  186.     
  187.     for (y=0 ; y<blockheight ; y++)
  188.         for (x=0 ; x<blockwidth ; x++)
  189.         {
  190.             len = data_p - datalist;
  191.             *pointer_p++ = SHORT(len);
  192.             GenerateBlockList (x,y);
  193.         }
  194.     
  195.     len = 2*(data_p-datalist);
  196.     
  197.     printf ("blockmap: (%i, %i) = %i\n",blockwidth, blockheight, len);
  198.      
  199. #if 0
  200. {
  201.     int    outhandle;
  202.     
  203.     outhandle = SafeOpenWrite ("blockmap.lmp");
  204.     SafeWrite (outhandle, datalist, len);
  205.     close (outhandle);
  206. }
  207. #endif
  208.     [wad_i addName: "blockmap" data:datalist size:len];
  209.  
  210. }
  211.  
  212.