home *** CD-ROM | disk | FTP | other *** search
/ 1,001 Nights of Doom / 1001NightsOfDoom1995wickedSensations.iso / nodebild / idbsp10.zip / SAVEBLCK.C < prev    next >
Text File  |  1994-05-30  |  6KB  |  230 lines

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