From 52098ce5f14231b9e35275f55aecd220134abba5 Mon Sep 17 00:00:00 2001
From: divverent <divverent@61c419a2-8eb2-4b30-bcec-8cead039b335>
Date: Sat, 23 May 2009 18:23:14 +0000
Subject: [PATCH] fix "invalid tracenode: 0" bug

git-svn-id: svn://svn.icculus.org/netradiant/trunk@374 61c419a2-8eb2-4b30-bcec-8cead039b335
---
 tools/quake3/q3map2/light_trace.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/tools/quake3/q3map2/light_trace.c b/tools/quake3/q3map2/light_trace.c
index 42613bea..ea3769a1 100644
--- a/tools/quake3/q3map2/light_trace.c
+++ b/tools/quake3/q3map2/light_trace.c
@@ -201,6 +201,9 @@ static int AllocTraceNode( void )
 	memset( &traceNodes[ numTraceNodes ], 0, sizeof( traceNode_t ) );
 	traceNodes[ numTraceNodes ].type = TRACE_LEAF;
 	ClearBounds( traceNodes[ numTraceNodes ].mins, traceNodes[ numTraceNodes ].maxs );
+
+	/* Sys_Printf("alloc node %d\n", numTraceNodes); */
+
 	numTraceNodes++;
 	
 	/* return the count */
@@ -362,7 +365,7 @@ recursively create the initial trace node structure from the bsp tree
 
 static int SetupTraceNodes_r( int bspNodeNum )
 {
-	int				i, nodeNum, bspLeafNum;
+	int				i, nodeNum, bspLeafNum, newNode;
 	bspPlane_t		*plane;
 	bspNode_t 		*bspNode;
 	
@@ -388,7 +391,10 @@ static int SetupTraceNodes_r( int bspNodeNum )
 			bspLeafNum = -bspNode->children[ i ] - 1;
 			
 			/* new code */
-			traceNodes[ nodeNum ].children[ i ] = AllocTraceNode();
+			newNode = AllocTraceNode();
+			traceNodes[ nodeNum ].children[ i ] = newNode;
+			/* have to do this separately, as gcc first executes LHS, then RHS, and if a realloc took place, this fails */
+
 			if( bspLeafs[ bspLeafNum ].cluster == -1 )
 				traceNodes[ traceNodes[ nodeNum ].children[ i ] ].type = TRACE_LEAF_SOLID;
 		}
@@ -397,6 +403,8 @@ static int SetupTraceNodes_r( int bspNodeNum )
 		else
 			traceNodes[ nodeNum ].children[ i ] = SetupTraceNodes_r( bspNode->children[ i ] );
 	}
+
+	/* Sys_Printf("node %d children: %d %d\n", nodeNum, traceNodes[ nodeNum ].children[0], traceNodes[ nodeNum ].children[1]); */
 	
 	/* return node number */
 	return nodeNum;
-- 
2.39.5