foxygit / doom Log in
commit 22d473c19cddb35c8ba90fbe52beb7dff8377688
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Tue Jun 12 18:49:29 2007 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Tue Jun 12 18:49:29 2007 +0000

    Emulate overflows in P_FindNextHighestFloor. Thanks to entryway for this
    fix.

    Subversion-branch: /trunk/chocolate-doom
    Subversion-revision: 902
---
 NEWS         |  2 ++
 src/p_spec.c | 85 ++++++++++++++++++++++++++++++++++--------------------------
 2 files changed, 50 insertions(+), 37 deletions(-)

diff --git a/NEWS b/NEWS
index 8603130b..9923fb5f 100644
--- a/NEWS
+++ b/NEWS
@@ -132,6 +132,8 @@
        means that people with non-US keyboards can decide whether to use
        their correct native mapping or behave like Vanilla mapping (which
        assumes all keyboards are US).
+     * Emulate overflow bug in P_FindNextHighestFloor.  Thanks to
+       entryway for the fix for this.

     Bugs fixed:
      * Fix crash when starting new levels due to the intermission screen
diff --git a/src/p_spec.c b/src/p_spec.c
index 8fc99707..01b612a1 100644
--- a/src/p_spec.c
+++ b/src/p_spec.c
@@ -328,59 +328,70 @@ fixed_t	P_FindHighestFloorSurrounding(sector_t *sec)
 // FIND NEXT HIGHEST FLOOR IN SURROUNDING SECTORS
 // Note: this should be doable w/o a fixed array.

+// Thanks to entryway for the Vanilla overflow emulation.
+
 // 20 adjoining sectors max!
-#define MAX_ADJOINING_SECTORS    	20
+#define MAX_ADJOINING_SECTORS     20

 fixed_t
 P_FindNextHighestFloor
-( sector_t*	sec,
-  int		currentheight )
+( sector_t* sec,
+  int       currentheight )
 {
-    int			i;
-    int			h;
-    int			min;
-    line_t*		check;
-    sector_t*		other;
-    fixed_t		height = currentheight;
-
-
-    fixed_t		heightlist[MAX_ADJOINING_SECTORS];
-
-    for (i=0, h=0 ;i < sec->linecount ; i++)
+    int         i;
+    int         h;
+    int         min;
+    line_t*     check;
+    sector_t*   other;
+    fixed_t     height = currentheight;
+    fixed_t     heightlist[MAX_ADJOINING_SECTORS + 2];
+
+    for (i=0, h=0; i < sec->linecount; i++)
     {
-	check = sec->lines[i];
-	other = getNextSector(check,sec);
-
-	if (!other)
-	    continue;
-
-	if (other->floorheight > height)
-	    heightlist[h++] = other->floorheight;
-
-	// Check for overflow. Exit.
-	if ( h >= MAX_ADJOINING_SECTORS )
-	{
-	    fprintf( stderr,
-		     "Sector with more than 20 adjoining sectors\n" );
-	    break;
-	}
+        check = sec->lines[i];
+        other = getNextSector(check,sec);
+
+        if (!other)
+            continue;
+
+        if (other->floorheight > height)
+        {
+            // Emulation of memory (stack) overflow
+            if (h == MAX_ADJOINING_SECTORS + 1)
+            {
+                height = other->floorheight;
+            }
+            else if (h == MAX_ADJOINING_SECTORS + 2)
+            {
+                // Fatal overflow: game crashes at 22 textures
+                I_Error("Sector with more than 22 adjoining sectors. "
+                        "Vanilla will crash here");
+            }
+
+            heightlist[h++] = other->floorheight;
+        }
     }

     // Find lowest height in list
     if (!h)
-	return currentheight;
-
+    {
+        return currentheight;
+    }
+
     min = heightlist[0];

     // Range checking?
-    for (i = 1;i < h;i++)
-	if (heightlist[i] < min)
-	    min = heightlist[i];
-
+    for (i = 1; i < h; i++)
+    {
+        if (heightlist[i] < min)
+        {
+            min = heightlist[i];
+        }
+    }
+
     return min;
 }

-
 //
 // FIND LOWEST CEILING IN THE SURROUNDING SECTORS
 //