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

  1. /* savebsp.m */
  2.  
  3. #include "idbsp.h"
  4.  
  5. /* id           secdefstore_i; */
  6. STORAGE            *secdefstore_i;
  7.  
  8. /*
  9.    #define         MAXVERTEX               8192
  10.    #define         MAXTOUCHSECS    16
  11.    #define         MAXSECTORS              2048
  12.    #define         MAXSUBSECTORS   2048
  13.  */
  14.  
  15. int                 vertexsubcount[MAXVERTEX];
  16. short               vertexsublist[MAXVERTEX][MAXTOUCHSECS];
  17.  
  18. /* short **vertexsublist; */
  19.  
  20. int                 subsectordef[MAXSUBSECTORS];
  21. int                 subsectornum[MAXSUBSECTORS];
  22.  
  23. int                 buildsector;
  24.  
  25.  
  26. /*
  27.    ==========================
  28.    =
  29.    = RecursiveGroupSubsector
  30.    =
  31.    ==========================
  32.  */
  33.  
  34. void                RecursiveGroupSubsector(int ssnum)
  35. {
  36.     int                 i,
  37.                         l,
  38.                         count;
  39.     int                 vertex;
  40.     int                 checkss;
  41.     short              *vertsub;
  42.  
  43. /*      short vertsub[MAXTOUCHSECS]; */
  44.  
  45.     int                 vt;
  46.     mapseg_t           *seg;
  47.     mapsubsector_t     *ss;
  48.     maplinedef_t       *ld;
  49.     mapsidedef_t       *sd;
  50.  
  51. /*      ss = [subsecstore_i elementAt:ssnum]; */
  52.     ss = (mapsubsector_t *) subsecstore_i -> data + ssnum;
  53.     subsectornum[ssnum] = buildsector;
  54.  
  55.     for (l = 0; l < ss -> numsegs; l++)
  56.         {
  57. /*
  58.    seg = [maplinestore_i elementAt: ss->firstseg+l];
  59.    ld = [ldefstore_i elementAt: seg->linedef];
  60.  */
  61.         seg = (mapseg_t *) maplinestore_i -> data + ss -> firstseg + l;
  62.         ld = (maplinedef_t *) ldefstore_i -> data + seg -> linedef;
  63.         DrawLineDef(ld);
  64. /*              sd = [sdefstore_i elementAt: ld->sidenum[seg->side]]; */
  65.         sd = (mapsidedef_t *) sdefstore_i -> data + ld -> sidenum[seg -> side];
  66.         sd -> sector = buildsector;
  67.  
  68.         for (vt = 0; vt < 2; vt++)
  69.             {
  70.             if (vt)
  71.                 vertex = seg -> v1;
  72.             else
  73.                 vertex = seg -> v2;
  74.  
  75.             vertsub = vertexsublist[vertex];
  76. /*
  77.    fseek(vertexsublist, vertex * MAXTOUCHSECS, SEEK_SET);
  78.    fread(vertsub,sizeof(short),MAXTOUCHSECS,vertexsublist);
  79.  */
  80.             count = vertexsubcount[vertex];
  81.             for (i = 0; i < count; i++)
  82.                 {
  83.                 checkss = vertsub[i];
  84.                 if (subsectordef[checkss] == subsectordef[ssnum])
  85.                     {
  86.                     if (subsectornum[checkss] == -1)
  87.                         {
  88.                         RecursiveGroupSubsector(checkss);
  89.                         continue;
  90.                         }
  91.                     if (subsectornum[checkss] != buildsector)
  92.                         /*      Error ("RecusiveGroup: regrouped a sector"); */
  93.                         Error("RecursiveGroupSubsector: regrouped (%d (subsectornum[%d]) != %d",
  94.                             subsectornum[checkss], checkss, buildsector);
  95.  
  96.                     }
  97.                 }
  98.             }
  99.         }
  100. }
  101.  
  102. /*
  103.    =================
  104.    =
  105.    = UniqueSector
  106.    =
  107.    = Returns the sector number, adding a new sector if needed
  108.    =================
  109.  */
  110.  
  111. int                 UniqueSector(sectordef_t * def)
  112. {
  113.     int                 i,
  114.                         count;
  115.     mapsector_t         ms,
  116.                        *msp;
  117.  
  118.     ms.floorheight = def -> floorheight;
  119.     ms.ceilingheight = def -> ceilingheight;
  120.     memcpy(ms.floorpic, def -> floorflat, 8);
  121.     memcpy(ms.ceilingpic, def -> ceilingflat, 8);
  122.     ms.lightlevel = def -> lightlevel;
  123.     ms.special = def -> special;
  124.     ms.tag = def -> tag;
  125.  
  126. /*
  127.    see if an identical sector already exists
  128.    count = [secdefstore_i count];
  129.    msp = [secdefstore_i elementAt:0];
  130.  */
  131.     count = secdefstore_i -> count;
  132.     msp = (mapsector_t *) secdefstore_i -> data;
  133.     for (i = 0; i < count; i++, msp++)
  134. /*
  135.    if (!bcmp(msp, &ms, sizeof(ms)))
  136.    return i;
  137.  */
  138.         if (!memcmp(msp, &ms, sizeof(ms)))
  139.             return i;
  140.  
  141. /*      [secdefstore_i addElement: &ms]; */
  142.     memcpy((mapsector_t *) secdefstore_i -> data + secdefstore_i -> count, &ms, sizeof(mapsector_t));
  143.     secdefstore_i -> count += 1;
  144.     secdefstore_i -> data = (mapsector_t *) realloc(secdefstore_i -> data,
  145.         sizeof(mapsector_t) * (secdefstore_i -> count + 1));
  146.  
  147.     return count;
  148. }
  149.  
  150.  
  151.  
  152.  
  153. void                AddSubsectorToVertex(int subnum, int vertex)
  154. {
  155.     int                 j;
  156.  
  157. /*      short vsl[MAXTOUCHSECS]; */
  158.  
  159.     for (j = 0; j < vertexsubcount[vertex]; j++)
  160.         if (vertexsublist[vertex][j] == subnum)
  161.             return;
  162.     vertexsublist[vertex][j] = subnum;
  163.     vertexsubcount[vertex]++;
  164. /*
  165.    fseek(vertexsublist, vertex * MAXTOUCHSECS, SEEK_SET);
  166.    fread(vsl,sizeof(short),MAXTOUCHSECS, vertexsublist);
  167.  
  168.    for (j=0;j<vertexsubcount[vertex];j++)
  169.    if (vsl[j] == subnum)
  170.    return
  171.  
  172.    vsl[j] = subnum;
  173.    vertexsubcount[vertex]++;
  174.  
  175.    fseek(vertexsublist, -MAXTOUCHSECS, SEEK_CUR);
  176.    fwrite(vsl,sizeof(short),MAXTOUCHSECS, vertexsublist);
  177.  */
  178. }
  179.  
  180.  
  181. /*
  182.    ================
  183.    =
  184.    = BuildSectordefs
  185.    =
  186.    = Call before ProcessNodes
  187.    ================
  188.  */
  189.  
  190. void                BuildSectordefs(void)
  191. {
  192.     int                 i;
  193.     worldline_t        *wl;
  194.     int                 count;
  195.     mapseg_t           *seg;
  196.  
  197. /*
  198.    build sectordef list
  199.  */
  200. /*
  201.    secdefstore_i = [[Storage alloc]
  202.    initCount:              0
  203.    elementSize:    sizeof(mapsector_t)
  204.    description:    NULL];
  205.  */
  206.  
  207.     secdefstore_i = (STORAGE *) SafeMalloc(sizeof(STORAGE));
  208.     secdefstore_i -> data = (mapsector_t *) SafeMalloc(sizeof(mapsector_t));
  209.     secdefstore_i -> count = 0;
  210.     secdefstore_i -> size = sizeof(mapsector_t);
  211.  
  212. /*
  213.    count = [linestore_i count];
  214.    wl= [linestore_i elementAt:0];
  215.  */
  216.  
  217.     count = linestore_i -> count;
  218.     wl = linestore_i -> data;
  219.     for (i = 0; i < count; i++, wl++)
  220.         {
  221. /*
  222.    seg = [maplinestore_i elementAt: i];
  223.  */
  224. /*                seg = (mapseg_t *)maplinestore_i->data + i; */
  225.         wl -> side[0].sector = UniqueSector(&wl -> side[0].sectordef);
  226.         if (wl -> flags & ML_TWOSIDED)
  227.             {
  228.             wl -> side[1].sector = UniqueSector(&wl -> side[1].sectordef);
  229.             }
  230.         }
  231. }
  232.  
  233.  
  234. /*
  235.    ================
  236.    =
  237.    = ProcessSectors
  238.    =
  239.    = Must be called after ProcessNodes, because it references the subsector list
  240.    ================
  241.  */
  242.  
  243. void                ProcessSectors(void)
  244. {
  245.     int                 i,
  246.                         l;
  247.     int                 numss;
  248.     mapsubsector_t     *ss;
  249.     mapsector_t         sec;
  250.     mapseg_t           *seg;
  251.     maplinedef_t       *ml;
  252.     mapsidedef_t       *ms;
  253.  
  254. /*
  255.    build a connection matrix that lists all the subsectors that touch
  256.    each vertex
  257.  */
  258.     memset(vertexsubcount, 0, sizeof(vertexsubcount));
  259.     memset(vertexsublist, 0, sizeof(vertexsublist));
  260. /*      memset(vertexsublist, 0, MAXVERTEX * MAXTOUCHSECS); */
  261. /*      numss = [subsecstore_i count]; */
  262.     numss = subsecstore_i -> count;
  263.  
  264.     printf("\n");
  265.     for (i = 0; i < numss; i++)
  266.         {
  267. /*              ss = [subsecstore_i elementAt: i]; */
  268.         printf("Processing subsector #%d of %d\r", i, numss);
  269.         ss = (mapsubsector_t *) subsecstore_i -> data + i;
  270.         for (l = 0; l < ss -> numsegs; l++)
  271.             {
  272. /*                      seg = [maplinestore_i elementAt: ss->firstseg+l]; */
  273.             seg = (mapseg_t *) maplinestore_i -> data + ss -> firstseg + l;
  274.             AddSubsectorToVertex(i, seg -> v1);
  275.             AddSubsectorToVertex(i, seg -> v2);
  276.             }
  277.         subsectornum[i] = -1;           /* ungrouped */
  278. /*
  279.    ml = [ldefstore_i elementAt: seg->linedef];
  280.    ms = [sdefstore_i elementAt: ml->sidenum[seg->side]];
  281.  */
  282.         ml = (maplinedef_t *) ldefstore_i -> data + seg -> linedef;
  283.         ms = (mapsidedef_t *) sdefstore_i -> data + ml -> sidenum[seg -> side];
  284.         subsectordef[i] = ms -> sector;
  285.         }
  286.  
  287. /*
  288.    recursively build final sectors
  289.  */
  290. /*
  291.    secstore_i = [[Storage alloc]
  292.    initCount:              0
  293.    elementSize:    sizeof(mapsector_t)
  294.    description:    NULL];
  295.  */
  296.     secstore_i = (STORAGE *) SafeMalloc(sizeof(STORAGE));
  297.     secstore_i -> data = (mapsector_t *) SafeMalloc(sizeof(mapsector_t));
  298.     secstore_i -> count = 0;
  299.     secstore_i -> size = sizeof(mapsector_t);
  300.  
  301.     buildsector = 0;
  302. /*
  303.    if (draw)
  304.    PSsetgray (0);
  305.  */
  306.     for (i = 0; i < numss; i++)
  307.         {
  308.         if (subsectornum[i] == -1)
  309.             {
  310.             EraseWindow();
  311.             RecursiveGroupSubsector(i);
  312.             sec = *((mapsector_t *) secdefstore_i -> data + subsectordef[i]);
  313. /*                      sec = *(mapsector_t *)[secdefstore_i elementAt: subsectordef[i]]; */
  314. /*                      [secstore_i addElement: &sec]; */
  315.             memcpy((mapsector_t *) secstore_i -> data + secstore_i -> count, &sec, sizeof(mapsector_t));
  316.             secstore_i -> count += 1;
  317.             secstore_i -> data = (mapsector_t *) realloc(secstore_i -> data,
  318.                 sizeof(mapsector_t) * (secstore_i -> count + 1));
  319.  
  320.             buildsector++;
  321.             }
  322.         }
  323.  
  324. }
  325.