From 173f350be76237a7dee9c00c389dff4e56b3da4c Mon Sep 17 00:00:00 2001
From: Garux <garux@mail.ru>
Date: Wed, 2 Aug 2017 09:00:11 +0300
Subject: [PATCH] Q3map2: 	* fix crash with nonpthreads xmllib +
 multithreading + output to system console 	* number of code fixes
 Radiant:

misc...
	* fix: clone group ent / map import / saving map as : group ents name labels at 0,0,0;
	* number of code fixes
---
 Makefile                                    |  2 +-
 contrib/bobtoolz/visfind.cpp                |  2 +-
 libs/gtkutil/image.cpp                      |  4 ++--
 libs/os/file.h                              | 10 ++++++++++
 libs/picomodel/picointernal.c               |  6 +++---
 libs/render.h                               |  2 +-
 plugins/entity/doom3group.cpp               |  7 ++++---
 plugins/entity/group.cpp                    |  7 ++++---
 plugins/entity/light.cpp                    | 14 ++++++++------
 radiant/console.cpp                         |  7 ++++++-
 radiant/mainframe.cpp                       |  6 +++---
 radiant/preferences.cpp                     |  4 ++--
 radiant/texwindow.cpp                       |  4 ++--
 radiant/watchbsp.cpp                        |  1 +
 tools/quake2/common/inout.c                 | 18 +++++++++---------
 tools/quake2/q2map/qbsp.c                   |  2 +-
 tools/quake2/qdata/images.c                 |  4 ++--
 tools/quake2/qdata/video.c                  |  4 ++--
 tools/quake2/qdata_heretic2/common/cmdlib.c |  2 +-
 tools/quake2/qdata_heretic2/common/inout.c  | 18 +++++++++---------
 tools/quake3/common/inout.c                 | 16 ++++++++--------
 tools/quake3/common/vfs.h                   |  4 ++++
 tools/quake3/q3map2/bsp.c                   |  2 +-
 tools/quake3/q3map2/fog.c                   |  8 ++++----
 tools/quake3/q3map2/light_bounce.c          |  2 +-
 tools/quake3/q3map2/light_ydnar.c           |  6 ++----
 tools/quake3/q3map2/surface.c               |  4 ++--
 tools/quake3/q3map2/surface_meta.c          |  8 ++++++--
 tools/quake3/q3map2/tjunction.c             |  2 +-
 29 files changed, 101 insertions(+), 75 deletions(-)

diff --git a/Makefile b/Makefile
index 661aec24..048407fb 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ MAKEFILE_CONF      ?= Makefile.conf
 # user customizable stuf
 # you may override this in Makefile.conf or the environment
 BUILD              ?= debug
-# or: release, or: extradebug, or: profile, or: debug
+# or: release, or: debug, or: extradebug, or: profile, or: native
 OS                 ?= $(shell uname)
 # or: Linux, Win32, Darwin
 LDFLAGS            ?=
diff --git a/contrib/bobtoolz/visfind.cpp b/contrib/bobtoolz/visfind.cpp
index 629c1633..33aa5bc8 100644
--- a/contrib/bobtoolz/visfind.cpp
+++ b/contrib/bobtoolz/visfind.cpp
@@ -209,7 +209,7 @@ std::list<DWinding*> *CreateTrace( dleaf_t *leaf, int c, vis_header *header, byt
 		}
 	}
 
-	delete repeatlist;
+	delete [] repeatlist;
 
 	return pointlist;
 }
diff --git a/libs/gtkutil/image.cpp b/libs/gtkutil/image.cpp
index 2791fd47..d8fb0bdd 100644
--- a/libs/gtkutil/image.cpp
+++ b/libs/gtkutil/image.cpp
@@ -47,7 +47,7 @@ GdkPixbuf* pixbuf_new_from_file_with_mask( const char* filename ){
 	{
 		//GdkPixbuf* rgba = gdk_pixbuf_add_alpha( rgb, TRUE, 255, 0, 255 ); //pink to alpha
 		GdkPixbuf* rgba = gdk_pixbuf_add_alpha( rgb, FALSE, 255, 0, 255 ); //alpha
-		gdk_pixbuf_unref( rgb );
+		g_object_unref( rgb );
 		return rgba;
 	}
 }
@@ -60,7 +60,7 @@ GtkImage* image_new_from_file_with_mask( const char* filename ){
 	else
 	{
 		GtkImage* image = GTK_IMAGE( gtk_image_new_from_pixbuf( rgba ) );
-		gdk_pixbuf_unref( rgba );
+		g_object_unref( rgba );
 		return image;
 	}
 }
diff --git a/libs/os/file.h b/libs/os/file.h
index 43381c63..41c20c15 100644
--- a/libs/os/file.h
+++ b/libs/os/file.h
@@ -28,9 +28,19 @@
 #if defined( WIN32 )
 #define S_ISDIR( mode ) ( mode & _S_IFDIR )
 #include <io.h> // _access()
+
+#ifndef F_OK
 #define F_OK 0x00
+#endif
+
+#ifndef W_OK
 #define W_OK 0x02
+#endif
+
+#ifndef R_OK
 #define R_OK 0x04
+#endif
+
 #define access( path, mode ) _access( path, mode )
 #else
 #include <unistd.h> // access()
diff --git a/libs/picomodel/picointernal.c b/libs/picomodel/picointernal.c
index e1bf9d1b..712e2543 100644
--- a/libs/picomodel/picointernal.c
+++ b/libs/picomodel/picointernal.c
@@ -606,12 +606,12 @@ int _pico_nofname( const char *path, char *dest, int destSize ){
  *  string otherwise. given 'path' is not altered. -sea
  */
 const char *_pico_nopath( const char *path ){
-	const char *src;
-	src = path + ( strlen( path ) - 1 );
-
 	if ( path == NULL ) {
 		return "";
 	}
+	const char *src;
+	src = path + ( strlen( path ) - 1 );
+
 	if ( !strchr( path,'/' ) && !strchr( path,'\\' ) ) {
 		return ( path );
 	}
diff --git a/libs/render.h b/libs/render.h
index b53612bf..6bc9410c 100644
--- a/libs/render.h
+++ b/libs/render.h
@@ -84,7 +84,7 @@ void insert( RenderIndex index ){
 	m_data.push_back( index );
 }
 void swap( IndexBuffer& other ){
-	std::swap( m_data, m_data );
+	std::swap( m_data, other.m_data );
 }
 };
 
diff --git a/plugins/entity/doom3group.cpp b/plugins/entity/doom3group.cpp
index b53017c5..15f7f3fa 100644
--- a/plugins/entity/doom3group.cpp
+++ b/plugins/entity/doom3group.cpp
@@ -91,8 +91,8 @@ NameKeys m_nameKeys;
 TraversableObserverPairRelay m_traverseObservers;
 Doom3GroupOrigin m_funcStaticOrigin;
 RenderablePivot m_renderOrigin;
-RenderableNamedEntity m_renderName;
 mutable Vector3 m_name_origin;
+RenderableNamedEntity m_renderName;
 ModelSkinKey m_skin;
 
 public:
@@ -259,8 +259,8 @@ Doom3Group( EntityClass* eclass, scene::Node& node, const Callback& transformCha
 	m_named( m_entity ),
 	m_nameKeys( m_entity ),
 	m_funcStaticOrigin( m_traverse, m_origin ),
-	m_renderName( m_named, m_name_origin ),
 	m_name_origin( g_vector3_identity ),
+	m_renderName( m_named, m_name_origin ),
 	m_skin( SkinChangedCaller( *this ) ),
 	m_curveNURBS( boundsChanged ),
 	m_curveCatmullRom( boundsChanged ),
@@ -278,7 +278,8 @@ Doom3Group( const Doom3Group& other, scene::Node& node, const Callback& transfor
 	m_named( m_entity ),
 	m_nameKeys( m_entity ),
 	m_funcStaticOrigin( m_traverse, m_origin ),
-	m_renderName( m_named, g_vector3_identity ),
+	m_name_origin( g_vector3_identity ),
+	m_renderName( m_named, m_name_origin ),
 	m_skin( SkinChangedCaller( *this ) ),
 	m_curveNURBS( boundsChanged ),
 	m_curveCatmullRom( boundsChanged ),
diff --git a/plugins/entity/group.cpp b/plugins/entity/group.cpp
index 8e308c4c..65d8cca6 100644
--- a/plugins/entity/group.cpp
+++ b/plugins/entity/group.cpp
@@ -63,8 +63,8 @@ NameKeys m_nameKeys;
 OriginKey m_originKey;
 Vector3 m_origin;
 
-RenderableNamedEntity m_renderName;
 mutable Vector3 m_name_origin;
+RenderableNamedEntity m_renderName;
 
 Callback m_transformChanged;
 Callback m_evaluateTransform;
@@ -83,8 +83,8 @@ Group( EntityClass* eclass, scene::Node& node, const Callback& transformChanged,
 	m_nameKeys( m_entity ),
 	m_originKey( OriginChangedCaller( *this ) ),
 	m_origin( ORIGINKEY_IDENTITY ),
-	m_renderName( m_named, m_name_origin ),
 	m_name_origin( g_vector3_identity ),
+	m_renderName( m_named, m_name_origin ),
 	m_transformChanged( transformChanged ),
 	m_evaluateTransform( evaluateTransform ){
 	construct();
@@ -96,7 +96,8 @@ Group( const Group& other, scene::Node& node, const Callback& transformChanged,
 	m_nameKeys( m_entity ),
 	m_originKey( OriginChangedCaller( *this ) ),
 	m_origin( ORIGINKEY_IDENTITY ),
-	m_renderName( m_named, g_vector3_identity ),
+	m_name_origin( g_vector3_identity ),
+	m_renderName( m_named, m_name_origin ),
 	m_transformChanged( transformChanged ),
 	m_evaluateTransform( evaluateTransform ){
 	construct();
diff --git a/plugins/entity/light.cpp b/plugins/entity/light.cpp
index 3793824a..f0261cc5 100644
--- a/plugins/entity/light.cpp
+++ b/plugins/entity/light.cpp
@@ -655,11 +655,11 @@ void render( RenderStateFlags state ) const {
 	aabb_draw_wire( points );
 }
 };
-
+/*
 inline void default_extents( Vector3& extents ){
 	extents = Vector3( 12, 12, 12 );
 }
-
+*/
 class ShaderRef
 {
 CopiedString m_name;
@@ -773,6 +773,8 @@ Doom3GroupOrigin m_funcStaticOrigin;
 LightRadii m_radii;
 Doom3LightRadius m_doom3Radius;
 
+AABB m_aabb_light;
+
 RenderLightRadiiWire m_radii_wire;
 RenderLightRadiiFill m_radii_fill;
 RenderLightRadiiBox m_radii_box;
@@ -805,16 +807,14 @@ RenderLightProjection m_renderProjection;
 
 LightShader m_shader;
 
-AABB m_aabb_light;
-
 Callback m_transformChanged;
 Callback m_boundsChanged;
 Callback m_evaluateTransform;
 
 void construct(){
 	default_rotation( m_rotation );
-	m_aabb_light.origin = Vector3( 0, 0, 0 );
-	default_extents( m_aabb_light.extents );
+	//m_aabb_light.origin = Vector3( 0, 0, 0 );
+	//default_extents( m_aabb_light.extents );
 
 	m_keyObservers.insert( "classname", ClassnameFilter::ClassnameChangedCaller( m_filter ) );
 	m_keyObservers.insert( Static<KeyIsName>::instance().m_nameKey, NamedEntity::IdentifierChangedCaller( m_named ) );
@@ -982,6 +982,7 @@ Light( EntityClass* eclass, scene::Node& node, const Callback& transformChanged,
 	m_nameKeys( m_entity ),
 	m_funcStaticOrigin( m_traverse, m_originKey.m_origin ),
 	m_doom3Radius( EntityClass_valueForKey( m_entity.getEntityClass(), "light_radius" ) ),
+	m_aabb_light( Vector3( 0, 0, 0 ), Vector3( 12, 12, 12 ) ),
 	m_radii_wire( m_radii, m_aabb_light.origin ),
 	m_radii_fill( m_radii, m_aabb_light.origin ),
 	m_radii_box( m_aabb_light.origin ),
@@ -1005,6 +1006,7 @@ Light( const Light& other, scene::Node& node, const Callback& transformChanged,
 	m_nameKeys( m_entity ),
 	m_funcStaticOrigin( m_traverse, m_originKey.m_origin ),
 	m_doom3Radius( EntityClass_valueForKey( m_entity.getEntityClass(), "light_radius" ) ),
+	m_aabb_light( Vector3( 0, 0, 0 ), Vector3( 12, 12, 12 ) ),
 	m_radii_wire( m_radii, m_aabb_light.origin ),
 	m_radii_fill( m_radii, m_aabb_light.origin ),
 	m_radii_box( m_aabb_light.origin ),
diff --git a/radiant/console.cpp b/radiant/console.cpp
index 72a64140..94060a45 100644
--- a/radiant/console.cpp
+++ b/radiant/console.cpp
@@ -136,6 +136,9 @@ GtkWidget* Console_constructWindow( GtkWindow* toplevel ){
 	return scr;
 }
 
+//#pragma GCC push_options
+//#pragma GCC optimize ("O0")
+
 class GtkTextBufferOutputStream : public TextOutputStream
 {
 GtkTextBuffer* textBuffer;
@@ -144,12 +147,14 @@ GtkTextTag* tag;
 public:
 GtkTextBufferOutputStream( GtkTextBuffer* textBuffer, GtkTextIter* iter, GtkTextTag* tag ) : textBuffer( textBuffer ), iter( iter ), tag( tag ){
 }
-std::size_t write( const char* buffer, std::size_t length ){
+std::size_t __attribute__((optimize("O0"))) write( const char* buffer, std::size_t length ){
 	gtk_text_buffer_insert_with_tags( textBuffer, iter, buffer, gint( length ), tag, 0 );
 	return length;
 }
 };
 
+//#pragma GCC pop_options
+
 std::size_t Sys_Print( int level, const char* buf, std::size_t length ){
 	bool contains_newline = std::find( buf, buf + length, '\n' ) != buf + length;
 
diff --git a/radiant/mainframe.cpp b/radiant/mainframe.cpp
index 4c8ddd7c..3d33df07 100644
--- a/radiant/mainframe.cpp
+++ b/radiant/mainframe.cpp
@@ -1,4 +1,4 @@
-/*
+/*
    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
    For a list of contributors, see the accompanying CONTRIBUTORS file.
 
@@ -232,7 +232,7 @@ void HomePaths_Realise(){
 			if ( shfolder ) {
 				FreeLibrary( shfolder );
 			}
-			if ( SHGetFolderPath( NULL, CSIDL_PERSONAL, NULL, 0, mydocsdir ) ) {
+			if ( SUCCEEDED( SHGetFolderPath( NULL, CSIDL_PERSONAL, NULL, 0, mydocsdir ) ) ) {
 				path.clear();
 				path << DirectoryCleaned( mydocsdir ) << "My Games/" << ( prefix + 1 ) << "/";
 				// win32: only add it if it already exists
@@ -2756,7 +2756,7 @@ void MainFrame::Create(){
 		GdkPixbuf* pixbuf = pixbuf_new_from_file_with_mask( "bitmaps/icon.png" );
 		if ( pixbuf != 0 ) {
 			gtk_window_set_icon( window, pixbuf );
-			gdk_pixbuf_unref( pixbuf );
+			g_object_unref( pixbuf );
 		}
 	}
 #endif
diff --git a/radiant/preferences.cpp b/radiant/preferences.cpp
index d93457c7..9f0c09dd 100644
--- a/radiant/preferences.cpp
+++ b/radiant/preferences.cpp
@@ -1,4 +1,4 @@
-/*
+/*
    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
    For a list of contributors, see the accompanying CONTRIBUTORS file.
 
@@ -118,7 +118,7 @@ CGameDescription::CGameDescription( xmlDocPtr pDoc, const CopiedString& gameFile
 	// read the user-friendly game name
 	xmlNodePtr pNode = pDoc->children;
 
-	while ( strcmp( (const char*)pNode->name, "game" ) && pNode != 0 )
+	while ( pNode != 0 && strcmp( (const char*)pNode->name, "game" ) )
 	{
 		pNode = pNode->next;
 	}
diff --git a/radiant/texwindow.cpp b/radiant/texwindow.cpp
index 60e9c101..3ea764bb 100644
--- a/radiant/texwindow.cpp
+++ b/radiant/texwindow.cpp
@@ -1577,7 +1577,7 @@ void TextureBrowser_constructTreeStore(){
 	TextureGroups groups = TextureGroups_constructTreeView();
 	GtkTreeStore* store = gtk_tree_store_new( 1, G_TYPE_STRING );
 	TextureGroups_constructTreeModel( groups, store );
-	std::set<CopiedString>::iterator iter;
+	//std::set<CopiedString>::iterator iter;
 
 	GtkTreeModel* model = GTK_TREE_MODEL( store );
 
@@ -1587,7 +1587,7 @@ void TextureBrowser_constructTreeStore(){
 }
 
 void TextureBrowser_constructTreeStoreTags(){
-	TextureGroups groups;
+	//TextureGroups groups;
 	GtkTreeStore* store = gtk_tree_store_new( 1, G_TYPE_STRING );
 	GtkTreeModel* model = GTK_TREE_MODEL( g_TextureBrowser.m_all_tags_list );
 
diff --git a/radiant/watchbsp.cpp b/radiant/watchbsp.cpp
index 79348257..4edc7f34 100644
--- a/radiant/watchbsp.cpp
+++ b/radiant/watchbsp.cpp
@@ -210,6 +210,7 @@ void BuildMonitor_Construct(){
 
 void BuildMonitor_Destroy(){
 	delete g_pWatchBSP;
+	g_pWatchBSP = NULL;
 }
 
 CWatchBSP *GetWatchBSP(){
diff --git a/tools/quake2/common/inout.c b/tools/quake2/common/inout.c
index 30ae39df..ed018d31 100644
--- a/tools/quake2/common/inout.c
+++ b/tools/quake2/common/inout.c
@@ -67,7 +67,7 @@ xmlNodePtr xml_NodeForVec( vec3_t v ){
 
 	sprintf( buf, "%f %f %f", v[0], v[1], v[2] );
 	ret = xmlNewNode( NULL, "point" );
-	xmlNodeSetContent( ret, buf );
+	xmlNodeAddContent( ret, buf );
 	return ret;
 }
 
@@ -145,14 +145,14 @@ void xml_Select( char *msg, int entitynum, int brushnum, qboolean bError ){
 	// now build a proper "select" XML node
 	sprintf( buf, "Entity %i, Brush %i: %s", entitynum, brushnum, msg );
 	node = xmlNewNode( NULL, "select" );
-	xmlNodeSetContent( node, buf );
+	xmlNodeAddContent( node, buf );
 	level[0] = (int)'0' + ( bError ? SYS_ERR : SYS_WRN )  ;
 	level[1] = 0;
 	xmlSetProp( node, "level", (char *)&level );
 	// a 'select' information
 	sprintf( buf, "%i %i", entitynum, brushnum );
 	select = xmlNewNode( NULL, "brush" );
-	xmlNodeSetContent( select, buf );
+	xmlNodeAddContent( select, buf );
 	xmlAddChild( node, select );
 	xml_SendNode( node );
 
@@ -172,14 +172,14 @@ void xml_Point( char *msg, vec3_t pt ){
 	char level[2];
 
 	node = xmlNewNode( NULL, "pointmsg" );
-	xmlNodeSetContent( node, msg );
+	xmlNodeAddContent( node, msg );
 	level[0] = (int)'0' + SYS_ERR;
 	level[1] = 0;
 	xmlSetProp( node, "level", (char *)&level );
 	// a 'point' node
 	sprintf( buf, "%g %g %g", pt[0], pt[1], pt[2] );
 	point = xmlNewNode( NULL, "point" );
-	xmlNodeSetContent( point, buf );
+	xmlNodeAddContent( point, buf );
 	xmlAddChild( node, point );
 	xml_SendNode( node );
 
@@ -196,7 +196,7 @@ void xml_Winding( char *msg, vec3_t p[], int numpoints, qboolean die ){
 	int i;
 
 	node = xmlNewNode( NULL, "windingmsg" );
-	xmlNodeSetContent( node, msg );
+	xmlNodeAddContent( node, msg );
 	level[0] = (int)'0' + SYS_ERR;
 	level[1] = 0;
 	xmlSetProp( node, "level", (char *)&level );
@@ -213,7 +213,7 @@ void xml_Winding( char *msg, vec3_t p[], int numpoints, qboolean die ){
 	}
 
 	winding = xmlNewNode( NULL, "winding" );
-	xmlNodeSetContent( winding, buf );
+	xmlNodeAddContent( winding, buf );
 	xmlAddChild( node, winding );
 	xml_SendNode( node );
 
@@ -260,7 +260,7 @@ void FPrintf( int flag, char *buf ){
 	static qboolean bGotXML = false;
 	char level[2];
 
-	printf( buf );
+	printf( "%s", buf );
 
 	// the following part is XML stuff only.. but maybe we don't want that message to go down the XML pipe?
 	if ( flag == SYS_NOXML ) {
@@ -282,7 +282,7 @@ void FPrintf( int flag, char *buf ){
 		bGotXML = true;
 	}
 	node = xmlNewNode( NULL, "message" );
-	xmlNodeSetContent( node, buf );
+	xmlNodeAddContent( node, buf );
 	level[0] = (int)'0' + flag;
 	level[1] = 0;
 	xmlSetProp( node, "level", (char *)&level );
diff --git a/tools/quake2/q2map/qbsp.c b/tools/quake2/q2map/qbsp.c
index 128fb7ab..e5486bce 100644
--- a/tools/quake2/q2map/qbsp.c
+++ b/tools/quake2/q2map/qbsp.c
@@ -242,7 +242,7 @@ void ProcessWorldModel( void ){
 			Sys_FPrintf( SYS_NOXML, "**********************\n" );
 			polyline = LeakFile( tree );
 			leaknode = xmlNewNode( NULL, "message" );
-			xmlNodeSetContent( leaknode, "MAP LEAKED\n" );
+			xmlNodeAddContent( leaknode, "MAP LEAKED\n" );
 			xmlAddChild( leaknode, polyline );
 			level[0] = (int) '0' + SYS_ERR;
 			level[1] = 0;
diff --git a/tools/quake2/qdata/images.c b/tools/quake2/qdata/images.c
index 4c877600..ce3ff136 100644
--- a/tools/quake2/qdata/images.c
+++ b/tools/quake2/qdata/images.c
@@ -1,4 +1,4 @@
-/*
+/*
    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
    For a list of contributors, see the accompanying CONTRIBUTORS file.
 
@@ -692,7 +692,7 @@ void Cmd_Mipdir( void ){
 	GetToken( false );
 	strcpy( mip_prefix, token );
 	// create the directory if needed
-	sprintf( filename, "%stextures", gamedir, mip_prefix );
+	sprintf( filename, "%stextures", gamedir );
 	Q_mkdir( filename );
 	sprintf( filename, "%stextures/%s", gamedir, mip_prefix );
 	Q_mkdir( filename );
diff --git a/tools/quake2/qdata/video.c b/tools/quake2/qdata/video.c
index 0fc06757..5e506cb4 100644
--- a/tools/quake2/qdata/video.c
+++ b/tools/quake2/qdata/video.c
@@ -1194,7 +1194,7 @@ void Cmd_Video( void ){
 	// build the dictionary
 	for ( frame = startframe ;  ; frame++ )
 	{
-		printf( "counting ", frame );
+		printf( "counting %i", frame );
 		in = LoadFrame( base, frame, digits, &palette );
 		if ( !in.data ) {
 			break;
@@ -1213,7 +1213,7 @@ void Cmd_Video( void ){
 	// compress it with the dictionary
 	for ( frame = startframe ;  ; frame++ )
 	{
-		printf( "packing ", frame );
+		printf( "packing %i", frame );
 		in = LoadFrame( base, frame, digits, &palette );
 		if ( !in.data ) {
 			break;
diff --git a/tools/quake2/qdata_heretic2/common/cmdlib.c b/tools/quake2/qdata_heretic2/common/cmdlib.c
index 514a1c7b..3511ea99 100644
--- a/tools/quake2/qdata_heretic2/common/cmdlib.c
+++ b/tools/quake2/qdata_heretic2/common/cmdlib.c
@@ -900,7 +900,7 @@ int ParseNum( const char *str ){
    // all output ends up through here
    void FPrintf (int flag, char *buf)
    {
-   printf(buf);
+   printf( "%s", buf );
 
    }
 
diff --git a/tools/quake2/qdata_heretic2/common/inout.c b/tools/quake2/qdata_heretic2/common/inout.c
index 30ae39df..ed018d31 100644
--- a/tools/quake2/qdata_heretic2/common/inout.c
+++ b/tools/quake2/qdata_heretic2/common/inout.c
@@ -67,7 +67,7 @@ xmlNodePtr xml_NodeForVec( vec3_t v ){
 
 	sprintf( buf, "%f %f %f", v[0], v[1], v[2] );
 	ret = xmlNewNode( NULL, "point" );
-	xmlNodeSetContent( ret, buf );
+	xmlNodeAddContent( ret, buf );
 	return ret;
 }
 
@@ -145,14 +145,14 @@ void xml_Select( char *msg, int entitynum, int brushnum, qboolean bError ){
 	// now build a proper "select" XML node
 	sprintf( buf, "Entity %i, Brush %i: %s", entitynum, brushnum, msg );
 	node = xmlNewNode( NULL, "select" );
-	xmlNodeSetContent( node, buf );
+	xmlNodeAddContent( node, buf );
 	level[0] = (int)'0' + ( bError ? SYS_ERR : SYS_WRN )  ;
 	level[1] = 0;
 	xmlSetProp( node, "level", (char *)&level );
 	// a 'select' information
 	sprintf( buf, "%i %i", entitynum, brushnum );
 	select = xmlNewNode( NULL, "brush" );
-	xmlNodeSetContent( select, buf );
+	xmlNodeAddContent( select, buf );
 	xmlAddChild( node, select );
 	xml_SendNode( node );
 
@@ -172,14 +172,14 @@ void xml_Point( char *msg, vec3_t pt ){
 	char level[2];
 
 	node = xmlNewNode( NULL, "pointmsg" );
-	xmlNodeSetContent( node, msg );
+	xmlNodeAddContent( node, msg );
 	level[0] = (int)'0' + SYS_ERR;
 	level[1] = 0;
 	xmlSetProp( node, "level", (char *)&level );
 	// a 'point' node
 	sprintf( buf, "%g %g %g", pt[0], pt[1], pt[2] );
 	point = xmlNewNode( NULL, "point" );
-	xmlNodeSetContent( point, buf );
+	xmlNodeAddContent( point, buf );
 	xmlAddChild( node, point );
 	xml_SendNode( node );
 
@@ -196,7 +196,7 @@ void xml_Winding( char *msg, vec3_t p[], int numpoints, qboolean die ){
 	int i;
 
 	node = xmlNewNode( NULL, "windingmsg" );
-	xmlNodeSetContent( node, msg );
+	xmlNodeAddContent( node, msg );
 	level[0] = (int)'0' + SYS_ERR;
 	level[1] = 0;
 	xmlSetProp( node, "level", (char *)&level );
@@ -213,7 +213,7 @@ void xml_Winding( char *msg, vec3_t p[], int numpoints, qboolean die ){
 	}
 
 	winding = xmlNewNode( NULL, "winding" );
-	xmlNodeSetContent( winding, buf );
+	xmlNodeAddContent( winding, buf );
 	xmlAddChild( node, winding );
 	xml_SendNode( node );
 
@@ -260,7 +260,7 @@ void FPrintf( int flag, char *buf ){
 	static qboolean bGotXML = false;
 	char level[2];
 
-	printf( buf );
+	printf( "%s", buf );
 
 	// the following part is XML stuff only.. but maybe we don't want that message to go down the XML pipe?
 	if ( flag == SYS_NOXML ) {
@@ -282,7 +282,7 @@ void FPrintf( int flag, char *buf ){
 		bGotXML = true;
 	}
 	node = xmlNewNode( NULL, "message" );
-	xmlNodeSetContent( node, buf );
+	xmlNodeAddContent( node, buf );
 	level[0] = (int)'0' + flag;
 	level[1] = 0;
 	xmlSetProp( node, "level", (char *)&level );
diff --git a/tools/quake3/common/inout.c b/tools/quake3/common/inout.c
index 14e9eaaf..827bdceb 100644
--- a/tools/quake3/common/inout.c
+++ b/tools/quake3/common/inout.c
@@ -70,7 +70,7 @@ xmlNodePtr xml_NodeForVec( vec3_t v ){
 
 	sprintf( buf, "%f %f %f", v[0], v[1], v[2] );
 	ret = xmlNewNode( NULL, (xmlChar*)"point" );
-	xmlNodeSetContent( ret, (xmlChar*)buf );
+	xmlNodeAddContent( ret, (xmlChar*)buf );
 	return ret;
 }
 
@@ -148,14 +148,14 @@ void xml_Select( char *msg, int entitynum, int brushnum, qboolean bError ){
 	// now build a proper "select" XML node
 	sprintf( buf, "Entity %i, Brush %i: %s", entitynum, brushnum, msg );
 	node = xmlNewNode( NULL, (xmlChar*)"select" );
-	xmlNodeSetContent( node, (xmlChar*)buf );
+	xmlNodeAddContent( node, (xmlChar*)buf );
 	level[0] = (int)'0' + ( bError ? SYS_ERR : SYS_WRN )  ;
 	level[1] = 0;
 	xmlSetProp( node, (xmlChar*)"level", (xmlChar *)&level );
 	// a 'select' information
 	sprintf( buf, "%i %i", entitynum, brushnum );
 	select = xmlNewNode( NULL, (xmlChar*)"brush" );
-	xmlNodeSetContent( select, (xmlChar*)buf );
+	xmlNodeAddContent( select, (xmlChar*)buf );
 	xmlAddChild( node, select );
 	xml_SendNode( node );
 
@@ -175,14 +175,14 @@ void xml_Point( char *msg, vec3_t pt ){
 	char level[2];
 
 	node = xmlNewNode( NULL, (xmlChar*)"pointmsg" );
-	xmlNodeSetContent( node, (xmlChar*)msg );
+	xmlNodeAddContent( node, (xmlChar*)msg );
 	level[0] = (int)'0' + SYS_ERR;
 	level[1] = 0;
 	xmlSetProp( node, (xmlChar*)"level", (xmlChar *)&level );
 	// a 'point' node
 	sprintf( buf, "%g %g %g", pt[0], pt[1], pt[2] );
 	point = xmlNewNode( NULL, (xmlChar*)"point" );
-	xmlNodeSetContent( point, (xmlChar*)buf );
+	xmlNodeAddContent( point, (xmlChar*)buf );
 	xmlAddChild( node, point );
 	xml_SendNode( node );
 
@@ -199,7 +199,7 @@ void xml_Winding( char *msg, vec3_t p[], int numpoints, qboolean die ){
 	int i;
 
 	node = xmlNewNode( NULL, (xmlChar*)"windingmsg" );
-	xmlNodeSetContent( node, (xmlChar*)msg );
+	xmlNodeAddContent( node, (xmlChar*)msg );
 	level[0] = (int)'0' + SYS_ERR;
 	level[1] = 0;
 	xmlSetProp( node, (xmlChar*)"level", (xmlChar *)&level );
@@ -216,7 +216,7 @@ void xml_Winding( char *msg, vec3_t p[], int numpoints, qboolean die ){
 	}
 
 	winding = xmlNewNode( NULL, (xmlChar*)"winding" );
-	xmlNodeSetContent( winding, (xmlChar*)buf );
+	xmlNodeAddContent( winding, (xmlChar*)buf );
 	xmlAddChild( node, winding );
 	xml_SendNode( node );
 
@@ -287,7 +287,7 @@ void FPrintf( int flag, char *buf ){
 	node = xmlNewNode( NULL, (xmlChar*)"message" );
 	{
 		gchar* utf8 = g_locale_to_utf8( buf, -1, NULL, NULL, NULL );
-		xmlNodeSetContent( node, (xmlChar*)utf8 );
+		xmlNodeAddContent( node, (xmlChar*)utf8 );
 		g_free( utf8 );
 	}
 	level[0] = (int)'0' + flag;
diff --git a/tools/quake3/common/vfs.h b/tools/quake3/common/vfs.h
index 95a21e15..89da7b14 100644
--- a/tools/quake3/common/vfs.h
+++ b/tools/quake3/common/vfs.h
@@ -39,7 +39,11 @@
 #else
 #include <wtypes.h>
 #include <io.h>
+
+#ifndef R_OK
 #define R_OK 04
+#endif
+
 #define S_ISDIR( mode ) ( mode & _S_IFDIR )
 #define PATH_MAX 260
 #endif
diff --git a/tools/quake3/q3map2/bsp.c b/tools/quake3/q3map2/bsp.c
index 6d61c58a..b9344a3a 100644
--- a/tools/quake3/q3map2/bsp.c
+++ b/tools/quake3/q3map2/bsp.c
@@ -349,7 +349,7 @@ void ProcessWorldModel( void ){
 		Sys_FPrintf( SYS_NOXML, "**********************\n" );
 		polyline = LeakFile( tree );
 		leaknode = xmlNewNode( NULL, (xmlChar*)"message" );
-		xmlNodeSetContent( leaknode, (xmlChar*)"MAP LEAKED\n" );
+		xmlNodeAddContent( leaknode, (xmlChar*)"MAP LEAKED\n" );
 		xmlAddChild( leaknode, polyline );
 		level[0] = (int) '0' + SYS_ERR;
 		level[1] = 0;
diff --git a/tools/quake3/q3map2/fog.c b/tools/quake3/q3map2/fog.c
index 900e0cb8..5b2ca6f7 100644
--- a/tools/quake3/q3map2/fog.c
+++ b/tools/quake3/q3map2/fog.c
@@ -712,7 +712,7 @@ int FogForBounds( vec3_t mins, vec3_t maxs, float epsilon ){
  */
 
 void CreateMapFogs( void ){
-	int i;
+	int i, j;
 	entity_t    *entity;
 	brush_t     *brush;
 	fog_t       *fog;
@@ -759,10 +759,10 @@ void CreateMapFogs( void ){
 				VectorScale( fog->si->fogDir, -1.0f, invFogDir );
 
 				/* find the brush side */
-				for ( i = 0; i < brush->numsides; i++ )
+				for ( j = 0; j < brush->numsides; j++ )
 				{
-					if ( VectorCompare( invFogDir, mapplanes[ brush->sides[ i ].planenum ].normal ) ) {
-						fog->visibleSide = i;
+					if ( VectorCompare( invFogDir, mapplanes[ brush->sides[ j ].planenum ].normal ) ) {
+						fog->visibleSide = j;
 						//%	Sys_Printf( "Brush num: %d Side num: %d\n", fog->brushNum, fog->visibleSide );
 						break;
 					}
diff --git a/tools/quake3/q3map2/light_bounce.c b/tools/quake3/q3map2/light_bounce.c
index b9cb8242..cca3e601 100644
--- a/tools/quake3/q3map2/light_bounce.c
+++ b/tools/quake3/q3map2/light_bounce.c
@@ -580,7 +580,7 @@ static void RadSubdivideDiffuseLight( int lightmapNum, bspDrawSurface_t *ds, raw
 		VectorMA( light->origin, 1.0f, light->normal, light->origin );
 		light->dist = DotProduct( light->origin, normal );
 
-		/* optionally create a point splashsplash light for first pass */
+		/* optionally create a point backsplash light for first pass */
 		if ( original && si->backsplashFraction > 0 ) {
 			/* allocate a new point light */
 			splash = safe_malloc( sizeof( *splash ) );
diff --git a/tools/quake3/q3map2/light_ydnar.c b/tools/quake3/q3map2/light_ydnar.c
index 4aa7eac7..83595337 100644
--- a/tools/quake3/q3map2/light_ydnar.c
+++ b/tools/quake3/q3map2/light_ydnar.c
@@ -1825,7 +1825,7 @@ static void SubsampleRawLuxel_r( rawLightmap_t *lm, trace_t *trace, vec3_t sampl
 	int b, samples, mapped, lighted;
 	int cluster[ 4 ];
 	vec4_t luxel[ 4 ];
-	vec3_t deluxel[ 3 ];
+	vec3_t deluxel[ 4 ];
 	vec3_t origin[ 4 ], normal[ 4 ];
 	float biasDirs[ 4 ][ 2 ] = { { -1.0f, -1.0f }, { 1.0f, -1.0f }, { -1.0f, 1.0f }, { 1.0f, 1.0f } };
 	vec3_t color, direction = { 0, 0, 0 }, total;
@@ -3222,7 +3222,6 @@ void IlluminateVertexes( int num ){
 void SetupBrushesFlags( int mask_any, int test_any, int mask_all, int test_all ){
 	int i, j, b;
 	unsigned int compileFlags, allCompileFlags;
-	qboolean inside;
 	bspBrush_t      *brush;
 	bspBrushSide_t  *side;
 	bspShader_t     *shader;
@@ -3249,10 +3248,9 @@ void SetupBrushesFlags( int mask_any, int test_any, int mask_all, int test_all )
 		brush = &bspBrushes[ b ];
 
 		/* check all sides */
-		inside = qtrue;
 		compileFlags = 0;
 		allCompileFlags = ~( 0u );
-		for ( j = 0; j < brush->numSides && inside; j++ )
+		for ( j = 0; j < brush->numSides; j++ )
 		{
 			/* do bsp shader calculations */
 			side = &bspBrushSides[ brush->firstSide + j ];
diff --git a/tools/quake3/q3map2/surface.c b/tools/quake3/q3map2/surface.c
index 65a651c4..5aa290f9 100644
--- a/tools/quake3/q3map2/surface.c
+++ b/tools/quake3/q3map2/surface.c
@@ -82,7 +82,7 @@ void FinishSurface( mapDrawSurface_t *ds ){
 
 
 	/* dummy check */
-	if ( ds->type <= SURFACE_BAD || ds->type >= NUM_SURFACE_TYPES || ds == NULL || ds->shaderInfo == NULL ) {
+	if ( ds == NULL || ds->shaderInfo == NULL || ds->type <= SURFACE_BAD || ds->type >= NUM_SURFACE_TYPES ) {
 		return;
 	}
 
@@ -3368,7 +3368,7 @@ int AddSurfaceModels( mapDrawSurface_t *ds ){
 			alpha /= ds->numVerts;
 			centroid.color[ 0 ][ 0 ] = 0xFF;
 			centroid.color[ 0 ][ 1 ] = 0xFF;
-			centroid.color[ 0 ][ 2 ] = 0xFF;
+			//centroid.color[ 0 ][ 2 ] = 0xFF;
 			centroid.color[ 0 ][ 2 ] = ( alpha > 255.0f ? 0xFF : alpha );
 
 			/* head vert is centroid */
diff --git a/tools/quake3/q3map2/surface_meta.c b/tools/quake3/q3map2/surface_meta.c
index 453b7531..e0cc6b12 100644
--- a/tools/quake3/q3map2/surface_meta.c
+++ b/tools/quake3/q3map2/surface_meta.c
@@ -1705,8 +1705,12 @@ static void MetaTrianglesToSurface( int numPossibles, metaTriangle_t *possibles,
 		ClearBounds( ds->mins, ds->maxs );
 
 		/* clear verts/indexes */
-		memset( verts, 0, sizeof( verts ) );
-		memset( indexes, 0, sizeof( indexes ) );
+//		memset( verts, 0, sizeof( verts ) );
+//		memset( indexes, 0, sizeof( indexes ) );
+		//is more correct, but upper works ok too
+		memset( verts, 0, sizeof( *verts ) * maxSurfaceVerts );
+		memset( indexes, 0, sizeof( *indexes ) * maxSurfaceIndexes );
+
 
 		/* add the first triangle */
 		if ( AddMetaTriangleToSurface( ds, seed, qfalse ) ) {
diff --git a/tools/quake3/q3map2/tjunction.c b/tools/quake3/q3map2/tjunction.c
index c405d317..2a0f6d05 100644
--- a/tools/quake3/q3map2/tjunction.c
+++ b/tools/quake3/q3map2/tjunction.c
@@ -603,7 +603,7 @@ int EdgeCompare( const void *elem1, const void *elem2 ) {
 	if ( d1 < d2 ) {
 		return -1;
 	}
-	if ( d2 > d1 ) {
+	if ( d1 > d2 ) {
 		return 1;
 	}
 	return 0;
-- 
2.39.5