shithub: duke3d

Download patch

ref: a7d509eca0ad2ece012d0fc46f73bdbf65695ac2
parent: cac93fa12952faa16d4db9d5fadfda13170ee966
author: unknown <fabien@fabien-PC.(none)>
date: Thu Dec 13 20:52:37 EST 2012

Fixed many silly bugs thanks to PVS Studio. Aslo started to heavily comments the different portions of the code

--- a/Engine/src/engine.c
+++ b/Engine/src/engine.c
@@ -295,8 +295,9 @@
 }
 
 /*
-     FCS: Scan through sectors using portals (a portal is wall with a nextwall attribute != -1).
-          Flood is prevented if a portal does not face the POV.
+   FCS: 
+   Scan through sectors using portals (a portal is wall with a nextsector attribute >= 0).
+   Flood is prevented if a portal does not face the POV.
  */
 static void scansector (short sectnum)
 {
@@ -430,27 +431,41 @@
 			if ((yb2[numscans] < 256) || (xb1[numscans] > xb2[numscans])) goto skipitaddwall;
 
 				/* Made it all the way! */
-			thesector[numscans] = sectnum; thewall[numscans] = z;
-			rx1[numscans] = xp1; ry1[numscans] = yp1;
-			rx2[numscans] = xp2; ry2[numscans] = yp2;
+			thesector[numscans] = sectnum; 
+			thewall[numscans] = z;
+
+			//Save the camera space wall endpoints coordinate (camera origin at player location + rotated according to player orientation).
+			rx1[numscans] = xp1; 
+			ry1[numscans] = yp1;
+			rx2[numscans] = xp2; 
+			ry2[numscans] = yp2;
+
 			p2[numscans] = numscans+1;
 			numscans++;
 skipitaddwall:
 
 			if ((wall[z].point2 < z) && (scanfirst < numscans))
-				p2[numscans-1] = scanfirst, scanfirst = numscans;
+			{
+				p2[numscans-1] = scanfirst;
+				scanfirst = numscans;
+			}
 		}
 
         //FCS: TODO rename this p2[]. This name is an abomination
 		for(z=numscansbefore;z<numscans;z++)
+		{
 			if ((wall[thewall[z]].point2 != thewall[p2[z]]) || (xb2[z] >= xb1[p2[z]]))
-				bunchfirst[numbunches++] = p2[z], p2[z] = -1;
+			{
+				bunchfirst[numbunches++] = p2[z];
+				p2[z] = -1;
+			}
+		}
 
         //For each bunch, cache the last wall of the bunch in bunchlast.
 		for(z=bunchfrst;z<numbunches;z++)
 		{
 			for(zz=bunchfirst[z];p2[zz]>=0;zz=p2[zz]);
-			bunchlast[z] = zz;
+				bunchlast[z] = zz;
 		}
     
 	} while (sectorbordercnt > 0);
@@ -3847,7 +3862,12 @@
 	}
 }
 
+/*
+    FCS: Return true if the point (x,Y) is inside the sector sectnum.
+	Note that a sector is closed to the answer is always 0 or 1.
 
+	Algorithm: For each wall one after an other....???
+*/
 int inside(int32_t x, int32_t y, short sectnum)
 {
 	walltype *wal;
@@ -3854,6 +3874,7 @@
 	int32_t i, x1, y1, x2, y2;
 	uint32_t  cnt;
 
+	//Quick check if the sector ID is valide.
 	if ((sectnum < 0) || (sectnum >= numsectors)) return(-1);
 
 	cnt = 0;
@@ -3863,18 +3884,26 @@
 	{
 		y1 = wal->y-y;
         y2 = wall[wal->point2].y-y;
+
+		// Compare the sign of y1 and y2.
+		// If (y1^y2) < 0 then y is above or below both wal->y and wall[wal->point2].y.
 		if ((y1^y2) < 0)
 		{
 			x1 = wal->x-x;
             x2 = wall[wal->point2].x-x;
             
+			//Again, compare the sign of x1 and x2. If (x1^x2) >= 0 then x is on the left or the right of both wal->x and wall[wal->point2].x.
 			if ((x1^x2) >= 0)
                 cnt ^= x1;
             else
                 cnt ^= (x1*y2-x2*y1)^y2;
 		}
-		wal++; i--;
+
+		wal++; 
+		i--;
+
 	} while (i);
+
 	return(cnt>>31);
 }
 
@@ -6284,8 +6313,8 @@
 }
 
 /*
-   FCS:  x and y are the new position of the entity.
-         sectnum is an hint (the last known sector number of the entity)
+   FCS:  x and y are the new position of the entity that has just moved:
+         lastKnownSector is an hint (the last known sectorID of the entity).
  
    Thanks to the "hint", the algorithm check:
    1. Is (x,y) inside sectors[sectnum].
@@ -6295,17 +6324,23 @@
    Note: Inside uses cross_product and return as soon as the point switch
          from one side to the other.
  */
-void updatesector(int32_t x, int32_t y, short *sectnum)
+void updatesector(int32_t x, int32_t y, short *lastKnownSector)
 {
 	walltype *wal;
 	int32_t i, j;
 
-	if (inside(x,y,*sectnum) == 1) return;
+	//First check the last sector where (old_x,old_y) was before being updated to (x,y)
+	if (inside(x,y,*lastKnownSector) ) 
+	{
+		//We found it and (x,y) is still in the same sector: nothing to update !
+		return;
+	}
 
-	if ((*sectnum >= 0) && (*sectnum < numsectors))
+	// Seems (x,y) moved into an other sector....hopefully one connected via a portal. Let's flood in each portal.
+	if ((*lastKnownSector >= 0) && (*lastKnownSector < numsectors))
 	{
-		wal = &wall[sector[*sectnum].wallptr];
-		j = sector[*sectnum].wallnum;
+		wal = &wall[sector[*lastKnownSector].wallptr];
+		j = sector[*lastKnownSector].wallnum;
 		do
 		{
 			i = wal->nextsector;
@@ -6312,7 +6347,7 @@
 			if (i >= 0)
 				if (inside(x,y,(short)i) == 1)
 				{
-					*sectnum = i;
+					*lastKnownSector = i;
 					return;
 				}
 			wal++;
@@ -6320,14 +6355,17 @@
 		} while (j != 0);
 	}
 
+	//Damn that is a BIG move, still cannot find which sector (x,y) belongs to. Let's search via linear search.
 	for(i=numsectors-1;i>=0;i--)
+	{
 		if (inside(x,y,(short)i) == 1)
 		{
-			*sectnum = i;
+			*lastKnownSector = i;
 			return;
 		}
-
-	*sectnum = -1;
+	}
+    // (x,y) is contained in NO sector. (x,y) is likely out of the map.
+	*lastKnownSector = -1;
 }
 
 
--- a/Game/src/audiolib/fx_man.c
+++ b/Game/src/audiolib/fx_man.c
@@ -328,7 +328,7 @@
 
 int FX_SetCallBack
    (
-   void ( *function )( uint32_t )
+   void ( *function )( int32_t )
    )
 
    {
--- a/Game/src/audiolib/fx_man.h
+++ b/Game/src/audiolib/fx_man.h
@@ -88,7 +88,7 @@
 int   FX_SetupSoundBlaster( fx_blaster_config blaster, int *MaxVoices, int *MaxSampleBits, int *MaxChannels );
 int   FX_Init( int SoundCard, int numvoices, int numchannels, int samplebits, unsigned mixrate );
 int   FX_Shutdown( void );
-int   FX_SetCallBack( void ( *function )( uint32_t ) );
+int   FX_SetCallBack( void ( *function )( int32_t ) );
 void  FX_SetVolume( int volume );
 int   FX_GetVolume( void );
 
--- a/Game/src/duke3d.h
+++ b/Game/src/duke3d.h
@@ -114,7 +114,8 @@
 #define CRC_BASE_GRP_PLUTONIUM_14	0xF514A6AC
 #define CRC_BASE_GRP_ATOMIC_15		0xFD3DCFF1 
 
-#define PLUTOPAK  (!VOLUMEONE && !VOLUMEALL) // implies  conVersion == 14 or conVersion == 15
+// implies  conVersion == 14 or conVersion == 15
+#define PLUTOPAK  (!VOLUMEONE && !VOLUMEALL) 
 #define VOLUMEONE (groupefil_crc32[0]==CRC_BASE_GRP_SHAREWARE_13)
 // VOLUMEALL = 1.3d full
 #define VOLUMEALL (groupefil_crc32[0]==CRC_BASE_GRP_FULL_13 || conVersion == 13 && groupefil_crc32[0]!=CRC_BASE_GRP_SHAREWARE_13 && groupefil_crc32[0]!=CRC_BASE_GRP_PLUTONIUM_14 && groupefil_crc32[0]!=CRC_BASE_GRP_ATOMIC_15)
--- a/Game/src/game.c
+++ b/Game/src/game.c
@@ -5670,10 +5670,9 @@
             case RECON:
 
                 k = getangle(s->x-x,s->y-y);
-                if( T1 < 4 )
-                    k = (((s->ang+3072+128-k)&2047)/170);
-                else k = (((s->ang+3072+128-k)&2047)/170);
 
+				k = (((s->ang+3072+128-k)&2047)/170);
+
                 if(k>6)
                 {
                     k = 12-k;
@@ -8086,7 +8085,7 @@
 	else
 		sprintf(groupfilefullpath, "%s", baseDir);
     
-	printf("Searching '%d':\n\n",groupfilefullpath);
+	printf("Searching '%s':\n\n",groupfilefullpath);
 	hFind = FindFirstFile(groupfilefullpath,&FindFileData);
     
 	if ( hFind==INVALID_HANDLE_VALUE )
@@ -8108,7 +8107,7 @@
 			kbdKey = getch();
 		while(kbdKey < '1' || kbdKey > ('0' + i));
 		printf("%c\n", kbdKey);
-		grpID =  groupfile[kbdKey-'1'];
+		grpID =  kbdKey-'1';
 		
 	}
 	
@@ -10709,7 +10708,7 @@
 {
 	char  szFilename[256];
 	int i;
-	uint8_t  score[20];
+	char  score[20];
 	time_t time4file;
 	struct tm *tmHMS;
     
@@ -10741,14 +10740,12 @@
 			if(ud.m_coop==0 || ud.m_coop==2)  // if DM or DM No spawn. Add Score as well
 			{
 				strcat(tempbuf, "(");
-                snprintf(ps[i].frag-ps[i].fraggedself, sizeof(ps[i].frag-ps[i].fraggedself), "%d", score);
-				strcat(tempbuf, ps[i].frag-ps[i].fraggedself);
-				strcat(tempbuf, ") vs ");
+                snprintf(score, sizeof(score), "%d", ps[i].frag-ps[i].fraggedself-ps[i].fraggedself);
+				strcat(tempbuf, score);
+				strcat(tempbuf, ")");
 			}
-			else
-				strcat(tempbuf, " vs ");
 		}	
-		tempbuf[strlen(tempbuf)-4]=0; // remove last vs
+		
 		strcat(tempbuf, "]");
 	}
 	strcat(tempbuf, ".bmp");
--- a/Game/src/premap.c
+++ b/Game/src/premap.c
@@ -1288,7 +1288,10 @@
     if(fp != -1)
         kread(fp,(uint8_t  *)&numl,1);
     else
+	{
         gameexit("\nERROR: File 'LOOKUP.DAT' not found.");
+		return;
+	}
 
     for(j=0;j < numl;j++)
     {
--- a/Game/src/sector.c
+++ b/Game/src/sector.c
@@ -849,13 +849,13 @@
             if ( (sptr->lotag&0x8000) )
             {
                 q = (sptr->ceilingz+sptr->floorz)>>1;
-                j = setanimation(sn,&sptr->floorz,q,sptr->extra);
+               // j = setanimation(sn,&sptr->floorz,q,sptr->extra);
                 j = setanimation(sn,&sptr->ceilingz,q,sptr->extra);
             }
             else
             {
                 q = sector[nextsectorneighborz(sn,sptr->floorz,1,1)].floorz;
-                j = setanimation(sn,&sptr->floorz,q,sptr->extra);
+               // j = setanimation(sn,&sptr->floorz,q,sptr->extra);
                 q = sector[nextsectorneighborz(sn,sptr->ceilingz,-1,-1)].ceilingz;
                 j = setanimation(sn,&sptr->ceilingz,q,sptr->extra);
             }
--- a/Game/src/types.h
+++ b/Game/src/types.h
@@ -63,15 +63,7 @@
 typedef int64_t                 float128;
 typedef float64                 appfloat;
 
-#define MAXINT32                0x7fffffff
-#define MININT32                -0x80000000
-#define MAXUINT32               0xffffffff
-#define MINUINT32               0
 
-#define MAXINT16                0x7fff
-#define MININT16                -0x8000
-#define MAXUINT16               0xffff
-#define MINUINT16               0
 
 //***************************************************************************
 //