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