- Radiant is now Vista compatible (Aero must be disabled)
[xonotic/netradiant.git] / contrib / bobtoolz / misc.cpp
index f793bcc426e52b22f12d05d0a88e07e4601fb569..5d926986b2d32de900b55066dc27d19be46068b4 100644 (file)
@@ -17,13 +17,11 @@ License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
-#include "StdAfx.h"
+#include "misc.h"
 
-#include "gtkr_list.h"
+#include <list>
 #include "str.h"
 
-#include "misc.h"
-
 #include "DPoint.h"
 #include "DPlane.h"
 #include "DBrush.h"
@@ -39,8 +37,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #endif
 
 #include "iundo.h"
-
-#include "refcounted_ptr.h"
+#include "ientity.h"
+#include "iscenegraph.h"
+#include "qerplugin.h"
 
 #include <vector>
 #include <list>
@@ -61,7 +60,7 @@ char  g_CurrentTexture[256] = "";
 
 void ReadCurrentTexture()
 {
-       const char* textureName = g_FuncTable.m_pfnGetCurrentTexture();
+       const char* textureName = GlobalRadiant().TextureBrowser_getSelectedShader();
        strcpy(g_CurrentTexture, textureName);
 }
 
@@ -132,12 +131,10 @@ void SetInitialStairPos(int dir, vec3_t min, vec3_t max, float width)
 char* TranslateString (char *buf)
 {
        static  char    buf2[32768];
-       int             i, l;
-       char    *out;
 
-       l = strlen(buf);
-       out = buf2;
-       for (i=0 ; i<l ; i++)
+  std::size_t l = strlen(buf);
+       char* out = buf2;
+       for (int i=0 ; i<l ; i++)
        {
                if (buf[i] == '\n')
                {
@@ -152,29 +149,6 @@ char* TranslateString (char *buf)
        return buf2;
 }
 
-void Sys_ERROR (char* text, ...)
-{
-       va_list argptr;
-       char    buf[32768];
-
-       va_start (argptr,text);
-       vsprintf (buf, text,argptr);
-       va_end (argptr);
-
-       Sys_Printf("BobToolz::ERROR->%s", buf);
-}
-
-/*void Sys_Printf (char *text, ...)
-{
-       va_list argptr;
-       char    buf[32768];
-
-       va_start (argptr,text);
-       vsprintf (buf, text,argptr);
-       va_end (argptr);
-
-       g_FuncTable.m_pfnSysMsg ( buf );
-}*/
 
 char* UnixToDosPath(char* path)
 {
@@ -192,7 +166,7 @@ char* UnixToDosPath(char* path)
 
 const char* ExtractFilename(const char* path)
 {
-       char* p = strrchr(path, '/');
+       const char* p = strrchr(path, '/');
        if(!p)
        {
                p = strrchr(path, '\\');
@@ -214,7 +188,7 @@ extern char* PLUGIN_NAME;
        return buffer;
 }*/
 
-#if defined (__linux__) || defined (__APPLE__)
+#if defined (POSIX)
 // the bCreateConsole parameter is ignored on linux ..
 bool Q_Exec( const char *pCmd, bool bCreateConsole )
 {
@@ -241,8 +215,6 @@ bool Q_Exec( const char *pCmd, bool bCreateConsole )
 }
 #endif
 
-#include <windows.h>
-
 #ifdef WIN32
 
 #include <windows.h>
@@ -263,7 +235,7 @@ bool Q_Exec( const char *pCmd, bool bCreateConsole )
 
        for(; *pCmd == ' '; pCmd++);
 
-       if(!CreateProcess(NULL, (char *)pCmd, NULL, NULL, FALSE, dwCreationFlags, NULL, NULL, &si, &pi))
+       if(!CreateProcess(NULL, (char *)pCmd, NULL, NULL, false, dwCreationFlags, NULL, NULL, &si, &pi))
                return false;
 
   return true;
@@ -277,7 +249,7 @@ void StartBSP()
        UnixToDosPath(exename); // do we want this done in linux version?
 
        char mapname[256];  
-  const char *pn = g_FuncTable.m_pfnReadProjectKey("mapspath");
+  const char *pn = GlobalRadiant().getMapsPath();
   
        strcpy( mapname, pn );
        strcat( mapname, "/ac_prt.map" );
@@ -286,19 +258,49 @@ void StartBSP()
        char command[1024];
        sprintf(command, "%s -nowater -fulldetail %s", exename, mapname);
 
-       Q_Exec( command, TRUE );
+       Q_Exec( command, true );
 }
 
-void BuildMiniPrt(list<Str>* exclusionList)
+class EntityWriteMiniPrt
+{
+       mutable DEntity world;
+  FILE* pFile;
+  std::list<Str>* exclusionList;
+public:
+  EntityWriteMiniPrt(FILE* pFile, std::list<Str>* exclusionList)
+    : pFile(pFile), exclusionList(exclusionList)
+  {
+  }
+  void operator()(scene::Instance& instance) const
+  {
+               const char* classname = Node_getEntity(instance.path().top())->getKeyValue("classname");
+
+               if(!strcmp(classname, "worldspawn"))
+               {
+                       world.LoadFromEntity(instance.path().top(), false);
+                       world.RemoveNonCheckBrushes(exclusionList, true);
+                       world.SaveToFile(pFile);
+               }
+               else if(strstr(classname, "info_"))
+               {
+                       world.ClearBrushes();
+                       world.ClearEPairs();
+                       world.LoadEPairList(Node_getEntity(instance.path().top()));
+                       world.SaveToFile(pFile);
+               }
+  }
+};
+void BuildMiniPrt(std::list<Str>* exclusionList)
 {
        // yes, we could just use -fulldetail option, but, as SPOG said
        // it'd be faster without all the hint, donotenter etc textures and
        // doors, etc
 
-       DEntity world;
+       
        
        char buffer[128];
-  const char *pn = g_FuncTable.m_pfnReadProjectKey("mapspath");
+  const char *pn = GlobalRadiant().getMapsPath();
 
        strcpy( buffer, pn );
        strcat( buffer, "/ac_prt.map" );
@@ -308,73 +310,39 @@ void BuildMiniPrt(list<Str>* exclusionList)
        if(!pFile)
                return;
 
-#if 0
-  int count = g_FuncTable.m_pfnGetEntityCount();
-       for(int i = 0; i < count; i++)
-       {
-               entity_t* ent = (entity_t*)g_FuncTable.m_pfnGetEntityHandle(i);
-
-               epair_t* epl = *g_EntityTable.m_pfnGetEntityKeyValList(ent);
-
-               epair_t* ep = epl;
-               while(ep)
-               {
-                       if(!strcmp(ep->key, "classname"))
-                       {
-                               if(!strcmp(ep->value, "worldspawn"))
-                               {
-                                       world.LoadFromEntity(i, FALSE);
-                                       world.RemoveNonCheckBrushes(exclusionList, TRUE);
-                                       world.SaveToFile(pFile);
-                               }
-                               else if(strstr(ep->value, "info_"))
-                               {
-                                       world.ClearBrushes();
-                                       world.ClearEPairs();
-                                       world.LoadEPairList(epl);
-                                       world.SaveToFile(pFile);
-                               }
-                               break;
-                       }
-
-                       ep = ep->next;
-               }
-       }
-#endif
+  Scene_forEachEntity(EntityWriteMiniPrt(pFile, exclusionList));
 
        fclose(pFile);
 
        StartBSP();
 }
 
-scene::Path* FindEntityFromTargetname(const char* targetname, int* entNum)
+class EntityFindByTargetName
 {
-#if 0
-       DEntity world;
-
-       int count = g_FuncTable.m_pfnGetEntityCount();
-       for(int i = 0; i < count; i++)
-       {
-               world.ClearEPairs();
-
-               entity_s* ent = (entity_s*)g_FuncTable.m_pfnGetEntityHandle(i);
-
-               world.LoadEPairList(*g_EntityTable.m_pfnGetEntityKeyValList(ent));
-
-               DEPair* tn = world.FindEPairByKey("targetname");
-               if(tn)
-               {
-                       if(!stricmp(tn->value, targetname)) {
-                               if(entNum) {
-                                       *entNum = i;
-                               }
-                               return ent;
-                       }
-               }
-       }
+  const char* targetname;
+public:
+       mutable const scene::Path* result;
+  EntityFindByTargetName(const char* targetname)
+    : targetname(targetname), result(0)
+  {
+  }
+  void operator()(scene::Instance& instance) const
+  {
+    if(result == 0)
+    {
+                 const char* value = Node_getEntity(instance.path().top())->getKeyValue("targetname");
 
-#endif
-       return NULL;
+                 if(!strcmp(value, targetname))
+                 {
+                         result = &instance.path();
+                 }
+    }
+  }
+};
+const scene::Path* FindEntityFromTargetname(const char* targetname)
+{
+  return Scene_forEachEntity(EntityFindByTargetName(targetname)).result;
 }
 
 void FillDefaultTexture(_QERFaceData* faceData, vec3_t va, vec3_t vb, vec3_t vc, const char* texture)
@@ -384,13 +352,13 @@ void FillDefaultTexture(_QERFaceData* faceData, vec3_t va, vec3_t vb, vec3_t vc,
        faceData->m_texdef.scale[1] = 0.5;
        faceData->m_texdef.shift[0] = 0;
        faceData->m_texdef.shift[1] = 0;
-       faceData->m_texdef.contents = 0;
-       faceData->m_texdef.flags = 0;
-       faceData->m_texdef.value = 0;
+       faceData->contents = 0;
+       faceData->flags = 0;
+       faceData->value = 0;
        if(*texture)
-               faceData->m_texdef.SetName(texture);
+               faceData->m_shader = texture;
        else
-               faceData->m_texdef.SetName("textures/common/caulk");
+               faceData->m_shader = "textures/common/caulk";
        VectorCopy(va, faceData->m_p0);
        VectorCopy(vb, faceData->m_p1);
        VectorCopy(vc, faceData->m_p2);
@@ -405,14 +373,14 @@ float Determinant3x3(float a1, float a2, float a3,
 
 bool GetEntityCentre(const char* entity, vec3_t centre)
 {
-  const scene::Path* ent = FindEntityFromTargetname(entity, NULL);
+  const scene::Path* ent = FindEntityFromTargetname(entity);
        if(!ent)
-               return FALSE;
+               return false;
 
   scene::Instance& instance = *GlobalSceneGraph().find(*ent);
-  VectorCopy(instance.aabb_world().origin, centre);
+  VectorCopy(instance.worldAABB().origin, centre);
 
-       return TRUE;
+       return true;
 }
 
 vec_t Min(vec_t a, vec_t b)
@@ -422,9 +390,17 @@ vec_t Min(vec_t a, vec_t b)
        return b;
 }
 
-void MakeNormal( vec_t* va, vec_t* vb, vec_t* vc, vec_t* out ) {
+void MakeNormal( const vec_t* va, const vec_t* vb, const vec_t* vc, vec_t* out ) {
        vec3_t v1, v2;
        VectorSubtract(va, vb, v1);
        VectorSubtract(vc, vb, v2);
        CrossProduct(v1, v2, out);
 }
+
+char* GetFilename(char* buffer, const char* filename) {
+       strcpy(buffer, GlobalRadiant().getAppPath());
+       strcat(buffer, "plugins/");
+       strcat(buffer, filename);
+       return buffer;
+}
+