home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / doom_i / program / dmreject.exe / SOURCE.ZIP / REJECT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-01  |  4.7 KB  |  151 lines

  1. //**********************************************************************************
  2. //    REJECT.EXE - Reject data table builder for DOOM
  3. //    Copyright (C) 1994 L.M.WITEK 
  4. //
  5. //    This program is free software; you can redistribute it and/or modify
  6. //    it under the terms of the GNU General Public License as published by
  7. //    the Free Software Foundation; either version 1, or (at your option)
  8. //    any later version.
  9. //
  10. //    This program is distributed in the hope that it will be useful,
  11. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //    GNU General Public License for more details.
  14. //
  15. //    You should have received a copy of the GNU General Public License
  16. //    along with this program; if not, write to the Free Software
  17. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. //**********************************************************************************
  19.  
  20. #include <iostream.h>
  21. #include "debug.hpp"
  22. #include "reject.hpp"
  23.  
  24. CReject::CReject (CPWad &w, XString map)
  25.         : pwad (w),
  26.           sect (0),
  27.           rejectdata  (pwad.Read (map, "REJECT")),
  28.           linedefdata (pwad.Read (map, "LINEDEFS")),
  29.           sidedefdata (pwad.Read (map, "SIDEDEFS")),
  30.           vertexdata  (pwad.Read (map, "VERTEXES")),
  31.           sectordata  (pwad.Read (map, "SECTORS")),
  32.           linedef ((LINEDEF *)linedefdata.Buffer()),
  33.           sidedef ((SIDEDEF *)sidedefdata.Buffer()),
  34.           vertex  ((VERTEX  *)vertexdata.Buffer()),
  35.           sector  ((SECTOR  *)sectordata.Buffer())
  36. {
  37.      if (rejectdata.Size () && linedefdata.Size () && sidedefdata.Size () && vertexdata.Size () && sectordata.Size ())
  38.      {
  39.           numsectors = sectordata.Size() / sizeof (SECTOR);
  40.           sect = new CSector [numsectors];
  41.           cdebug.out << "Number of sectors in " << map << ": " << numsectors << endl;
  42.  
  43.           int numlinedefs (linedefdata.Size() / sizeof (LINEDEF));
  44.  
  45.           for (int n = 0; n < numlinedefs; n++)
  46.           {
  47.                if (linedef[n].sidedef1 != 0xffff)
  48.                {
  49.                     sect[sidedef[linedef[n].sidedef1].sector].AddVertex (vertex[linedef[n].from_vertex]);
  50.                     sect[sidedef[linedef[n].sidedef1].sector].AddVertex (vertex[linedef[n].to_vertex]);
  51.                }
  52.  
  53.                if (linedef[n].sidedef2 != 0xffff)
  54.                {
  55.                     sect[sidedef[linedef[n].sidedef2].sector].AddVertex (vertex[linedef[n].from_vertex]);
  56.                     sect[sidedef[linedef[n].sidedef2].sector].AddVertex (vertex[linedef[n].to_vertex]);
  57.                }
  58.           }
  59.  
  60.           for (n = 0; n < numsectors; n++)
  61.           {
  62.                SWORD x, y;
  63.                sect[n].GetCenter (x, y);
  64.                cdebug.out << "Sector " << n << " X: " << x << " Y: " << y << endl;
  65.           }
  66.      }
  67.      else
  68.      {
  69.           Fail ();
  70.      }
  71. }
  72.  
  73. /*********************************************************************************
  74. **
  75. **        A Fast Approximation to the Hypotenuse by Alan Paeth  
  76. **        from "Graphics Gems Volume I", Academic Press, 1990
  77. **        gives approximate distance from (x1,y1) to (x2,y2)
  78. **        with only overestimations, and then never by more
  79. **        than (9/8) + one bit uncertainty.
  80. **
  81. *********************************************************************************/
  82.  
  83. int CReject::idist (int x1, int y1, int x2, int y2)
  84. {
  85.      if ((x2 -= x1) < 0) x2 = -x2;
  86.      if ((y2 -= y1) < 0) y2 = -y2;
  87.      return (x2 + y2 - (((x2>y2) ? y2 : x2) >> 1));
  88. }
  89.  
  90.  
  91. void CReject::SetBit (char* buffer, int bit)
  92. {
  93.      buffer[bit/8] |= (1 << (bit ^ 8)); 
  94. }
  95.  
  96. void CReject::ResetBit (char* buffer, int bit)
  97. {
  98.      buffer[bit/8] &= ~(0x1 << (bit % 8)); 
  99. }
  100.  
  101. CReject::~CReject ()
  102. {
  103.      delete[] sect;
  104. }
  105.  
  106. void CReject::RejectDefault (int threshold)
  107. {
  108.      SWORD x1, y1;
  109.      SWORD x2, y2;
  110.  
  111.      RejectAll ();
  112.  
  113.      for (int n = 0; n < numsectors; n++)
  114.      {
  115.           sect[n].GetCenter (x1, y1);
  116.           for (int m = 0; m < numsectors; m++)
  117.           {
  118.                sect[m].GetCenter (x2, y2);
  119.                if (idist (x1, y1, x2, y2) <= threshold)
  120.                     AcceptSector (n, m);
  121.           }
  122.      }
  123.  
  124. }
  125.  
  126. void CReject::RejectSector (int local, int remote)
  127. {
  128.      SetBit (rejectdata, (remote * numsectors) + local);
  129. }
  130.  
  131. void CReject::AcceptSector (int local, int remote)
  132. {
  133.      ResetBit (rejectdata, (remote * numsectors) + local);
  134. }
  135.  
  136. void CReject::RejectAll ()
  137. {
  138.      rejectdata.MemSet (0xff);
  139. }
  140.  
  141. void CReject::AcceptAll ()
  142. {
  143.      rejectdata.MemSet (0x00);
  144. }
  145.  
  146. MemHandle CReject::GetRejectData ()
  147. {
  148.      return rejectdata;
  149. }
  150.  
  151.