home *** CD-ROM | disk | FTP | other *** search
/ Doom 2 Explosion / Doom2Explosion.bin / doom2exp / programs / ibsp101s / saveblck.c < prev    next >
C/C++ Source or Header  |  1994-07-10  |  5KB  |  247 lines

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