Advertisement
bombillo

GAMELEVEL_IO

Jan 5th, 2016
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.13 KB | None | 0 0
  1. #include "r3dPCH.h"
  2. #include "r3d.h"
  3.  
  4. #include "GameObjects/ObjManag.h"
  5. #include "ObjectsCode/WORLD/obj_Group.h"
  6.  
  7. #include "TrueNature2/Terrain3.h"
  8. #include "TrueNature2/Terrain2.h"
  9.  
  10. #include "GameLevel.h"
  11.  
  12. #ifdef WO_SERVER
  13. #include "ServerGameLogic.h"
  14.  
  15. // temporary server object
  16. class obj_ServerDummyObject : public GameObject
  17. {
  18.   public:
  19.     DECLARE_CLASS(obj_ServerDummyObject, GameObject)
  20.     // keep alive, do not destroy
  21.     // virtual BOOL Update() { return FALSE; }
  22. };
  23. IMPLEMENT_CLASS(obj_ServerDummyObject, "obj_ServerDummyObject", "Object");
  24. AUTOREGISTER_CLASS(obj_ServerDummyObject);
  25.  
  26. #endif
  27.  
  28. GameObject * LoadLevelObject ( pugi::xml_node & curNode, bool JustForBuildings=false )
  29. {                                                                    
  30.     pugi::xml_node posNode = curNode.child("position");
  31.     const char* class_name = curNode.attribute("className").value();
  32.     const char* load_name = curNode.attribute("fileName").value();
  33.     r3dPoint3D pos(0, 0, 0);
  34.     pos.x = posNode.attribute("x").as_float();
  35.     pos.y = posNode.attribute("y").as_float();
  36.     pos.z = posNode.attribute("z").as_float();
  37.  
  38.     GameObject* obj = NULL;
  39.  
  40. #ifdef WO_SERVER
  41.     int class_id = AObjectTable_GetClassID(class_name, "Object");
  42.     if(class_id == -1)
  43.     {
  44.         r3dOutToLog("skipped not defined server object %s\n", class_name);
  45.         class_name = "obj_ServerDummyObject";
  46.     }
  47. #endif
  48.  
  49.     //TheHexa:: For new fast load method..
  50.     if(JustForBuildings)
  51.     {
  52.         if(!strcmp(class_name, "obj_Building"))
  53.         {
  54.             obj = srv_CreateGameObject(class_name, load_name, pos);
  55.             if(!obj)
  56.             {
  57.                 r3dOutToLog("!!!Failed to create object! class: %s, name: %s\n", class_name, load_name);
  58.                 return NULL;
  59.             }
  60.             r3d_assert(obj);
  61.             obj->ReadSerializedData(curNode);
  62.             obj->LastShadowExtrusion = r3dGetTime();
  63.             obj->ObjFlags |= OBJFLAG_ForceSceneBoxBBox | OBJFLAG_AlwaysDraw;
  64.             return obj;
  65.         }
  66.         return NULL;
  67.     }
  68.  
  69.     //TheHexa: BUG FIX: STUDIO IN EDIT MODE SHOULD NOT RUN WITH FAST LOAD EVEN IF OPTION IS ENABLED!
  70. #ifdef FINAL_BUILD
  71.     if(strcmp(r3dGameLevel::GetHomeDir(), "Levels\\WZ_FrontEndLighting") && g_FastLoading->GetBool())
  72.     {
  73.         if(!strcmp( class_name, "obj_StoreNPC" ) ||
  74.             !strcmp( class_name, "obj_Gravestone" ) ||
  75.             !strcmp( class_name, "obj_RepairBench" ) ||
  76.             !strcmp( class_name, "obj_VaultNPC" ) ||
  77.             !strcmp( class_name, "obj_GuardNPC" ) ||
  78.             !strcmp( class_name, "obj_Terrain" ) ||
  79.             !strcmp( class_name, "obj_PermanentNote" ) ||
  80.             !strcmp( class_name, "obj_WaterPlane" ) ||
  81.             !strcmp( class_name, "obj_Lake" ) ||
  82.             !strcmp( class_name, "obj_Road" ) ||
  83.             !strcmp( class_name, "obj_ParticleSystem" ) ||
  84.             !strcmp( class_name, "obj_LightMesh" ) ||
  85.             !strcmp( class_name, "obj_LightHelper" ) ||
  86.             !strcmp( class_name, "obj_AmbientSound" ))
  87.         {
  88.             obj = srv_CreateGameObject(class_name, load_name, pos);
  89.             if(!obj)
  90.             {
  91.                 r3dOutToLog("!!!Failed to create object! class: %s, name: %s\n", class_name, load_name);
  92.                 return NULL;
  93.             }
  94.             r3d_assert(obj);
  95.             obj->ReadSerializedData(curNode);
  96.             return obj;
  97.         }
  98.     }
  99.     else
  100.     {
  101.         obj = srv_CreateGameObject(class_name, load_name, pos);
  102.         if(!obj)
  103.         {
  104.             r3dOutToLog("!!!Failed to create object! class: %s, name: %s\n", class_name, load_name);
  105.             return NULL;
  106.         }
  107.         r3d_assert(obj);
  108.         obj->ReadSerializedData(curNode);
  109.         return obj;
  110.     }
  111. #else
  112.     extern bool g_bEditMode;
  113.     if(strcmp(r3dGameLevel::GetHomeDir(), "Levels\\WZ_FrontEndLighting") && g_FastLoading->GetBool() && !g_bEditMode)
  114.     {
  115.         if(!strcmp( class_name, "obj_StoreNPC" ) ||
  116.             !strcmp( class_name, "obj_Gravestone" ) ||
  117.             !strcmp( class_name, "obj_RepairBench" ) ||
  118.             !strcmp( class_name, "obj_VaultNPC" ) ||
  119.             !strcmp( class_name, "obj_GuardNPC" ) ||
  120.             !strcmp( class_name, "obj_Terrain" ) ||
  121.             !strcmp( class_name, "obj_PermanentNote" ) ||
  122.             !strcmp( class_name, "obj_WaterPlane" ) ||
  123.             !strcmp( class_name, "obj_Lake" ) ||
  124.             !strcmp( class_name, "obj_Road" ) ||
  125.             !strcmp( class_name, "obj_ParticleSystem" ) ||
  126.             !strcmp( class_name, "obj_LightMesh" ) ||
  127.             !strcmp( class_name, "obj_LightHelper" ) ||
  128.             !strcmp( class_name, "obj_AmbientSound" ))
  129.         {
  130.             obj = srv_CreateGameObject(class_name, load_name, pos);
  131.             if(!obj)
  132.             {
  133.                 r3dOutToLog("!!!Failed to create object! class: %s, name: %s\n", class_name, load_name);
  134.                 return NULL;
  135.             }
  136.             r3d_assert(obj);
  137.             obj->ReadSerializedData(curNode);
  138.             return obj;
  139.         }
  140.     }
  141.     else
  142.     {
  143.         obj = srv_CreateGameObject(class_name, load_name, pos);
  144.         if(!obj)
  145.         {
  146.             r3dOutToLog("!!!Failed to create object! class: %s, name: %s\n", class_name, load_name);
  147.             return NULL;
  148.         }
  149.         r3d_assert(obj);
  150.         obj->ReadSerializedData(curNode);
  151.         return obj;
  152.     }
  153. #endif
  154.  
  155. #ifdef WO_SERVER
  156.     obj = srv_CreateGameObject(class_name, load_name, pos);
  157.     if(!obj)
  158.     {
  159.         r3dOutToLog("!!!Failed to create object! class: %s, name: %s\n", class_name, load_name);
  160.         return NULL;
  161.     }
  162.     r3d_assert ( obj );
  163.     obj->ReadSerializedData(curNode);
  164.     return obj;
  165. #endif
  166.  
  167.     return NULL;
  168. }
  169.  
  170. void LoadLevelObjectsGroups ( pugi::xml_node & curNode, r3dTL::TArray < GameObject * > & dObjects )
  171. {
  172.     dObjects.Clear ();
  173.     pugi::xml_node xmlObject = curNode.child("object");
  174.     while(!xmlObject.empty())
  175.     {
  176.         GameObject* obj = LoadLevelObject ( xmlObject );
  177.  
  178.         dObjects.PushBack(obj);
  179.  
  180.         xmlObject = xmlObject.next_sibling();
  181.     }
  182. }
  183.  
  184.  
  185. void LoadLevelObjects ( pugi::xml_node & curNode, float range, ESerializeFile sfType, bool isFastLoadBuilding = false )
  186. {
  187.     int count = 0;
  188.  
  189.     pugi::xml_node xmlObject = curNode.child("object");
  190.  
  191.     int hasTerrain = 0 ;
  192.  
  193.     while(!xmlObject.empty())
  194.     {
  195.         count ++;
  196.  
  197.         if( !hasTerrain )
  198.         {
  199.             const char *className = xmlObject.attribute( "className" ).value() ;
  200.             if( !stricmp( className, "obj_Terrain" ) )
  201.             {
  202.                 hasTerrain = 1 ;
  203.             }
  204.         }
  205.  
  206.         xmlObject = xmlObject.next_sibling();
  207.     }
  208.  
  209.     if( hasTerrain )
  210.     {
  211.         range -= 0.1f ;
  212.     }
  213.  
  214.     float delta = 0.f;
  215.  
  216.     if( hasTerrain )
  217.     {
  218.         r3dITerrain::LoadingProgress = 0.25f * range;
  219.         delta = count ? ( range * 0.75f ) / ( count - 1 ) : 0.f;
  220.     }
  221.     else
  222.     {
  223.         r3dITerrain::LoadingProgress = 0.0f;
  224.         delta = count ? range / count : 0.f;
  225.     }
  226.  
  227.     xmlObject = curNode.child("object");
  228.     while(!xmlObject.empty())
  229.     {      
  230.         GameObject* obj = LoadLevelObject(xmlObject, isFastLoadBuilding);
  231.  
  232.         void AdvanceLoadingProgress( float );
  233.  
  234.         if( obj && obj->ObjTypeFlags & OBJTYPE_Terrain )
  235.         {
  236.             AdvanceLoadingProgress( r3dITerrain::LoadingProgress );
  237.             r3dITerrain::LoadingProgress = 0.f;
  238.         }
  239.         else
  240.         {
  241.             AdvanceLoadingProgress( delta );
  242.         }
  243.  
  244.         xmlObject = xmlObject.next_sibling();
  245.     }
  246. }
  247.  
  248. bool ParseXMLFile(const char *xmlFileName, pugi::xml_document &outXml, char *& outBuf)
  249. {
  250.     r3dFile* f = r3d_open(xmlFileName, "rb");
  251.     if ( ! f )
  252.         return false;
  253.  
  254.     outBuf = game_new char[f->size + 1];
  255.     fread(outBuf, f->size, 1, f);
  256.     outBuf[f->size] = 0;
  257.     pugi::xml_parse_result parseResult = outXml.load_buffer_inplace(outBuf, f->size);
  258.     fclose(f);
  259.     if(!parseResult)
  260.         r3dError("Failed to parse XML, error: %s", parseResult.description());
  261.     return true;
  262. }
  263.  
  264. int LoadLevel_Objects( float BarRange, bool JustForBuilding_FastLoading )
  265. {
  266.     char fname[MAX_PATH];
  267.     sprintf(fname, "%s\\LevelData.xml", r3dGameLevel::GetHomeDir());
  268.  
  269.     pugi::xml_document xmlLevelFile;
  270.     char *levelData = 0;
  271.     ParseXMLFile(fname, xmlLevelFile, levelData);
  272.  
  273.     pugi::xml_node xmlLevel = xmlLevelFile.child("level");
  274.  
  275.     g_leveldata_xml_ver->SetInt( 0 );
  276.     if( !xmlLevel.attribute("version").empty() )
  277.     {
  278.         g_leveldata_xml_ver->SetInt( xmlLevel.attribute("version").as_int() );
  279.     }
  280.  
  281.  
  282.     if( g_level_settings_ver->GetInt() < 2 || g_level_settings_ver->GetInt() >=3 )
  283.     {
  284.         GameWorld().m_MinimapOrigin.x = xmlLevel.attribute("minimapOrigin.x").as_float();
  285.         GameWorld().m_MinimapOrigin.z = xmlLevel.attribute("minimapOrigin.z").as_float();
  286.         GameWorld().m_MinimapSize.x = xmlLevel.attribute("minimapSize.x").as_float();  
  287.         GameWorld().m_MinimapSize.z = xmlLevel.attribute("minimapSize.z").as_float();
  288.        
  289.         if(g_level_settings_ver->GetInt() < 2 )
  290.         {
  291.             r_shadow_extrusion_limit->SetFloat(xmlLevel.attribute("shadowLimitHeight").as_float());
  292.             if(!xmlLevel.attribute("near_plane").empty())
  293.             {
  294.                 r_near_plane->SetFloat(xmlLevel.attribute("near_plane").as_float());
  295.                 r_far_plane->SetFloat(xmlLevel.attribute("far_plane").as_float());
  296.             }
  297.         }
  298.     }
  299.  
  300.     if(GameWorld().m_MinimapSize.x == 0 || GameWorld().m_MinimapSize.z == 0)
  301.     {
  302.         GameWorld().m_MinimapSize.x = 100;
  303.         GameWorld().m_MinimapSize.z = 100;
  304.     }
  305.  
  306.     const float SOUND_BAR_RATIO = 0.25f;
  307.  
  308.     LoadLevelObjects ( xmlLevel, BarRange * ( 1.f - SOUND_BAR_RATIO ), SF_LevelData, JustForBuilding_FastLoading);
  309.  
  310.     //  Sound data (do not load on server)
  311. #ifndef WO_SERVER
  312.     sprintf(fname, "%s\\SoundData.xml", r3dGameLevel::GetHomeDir());
  313.  
  314.     pugi::xml_document xmlSoundFile;
  315.     char *soundData = 0;
  316.     ParseXMLFile(fname, xmlSoundFile, soundData);
  317.  
  318.     xmlLevel = xmlSoundFile.child("sounds");
  319.     LoadLevelObjects ( xmlLevel, BarRange * SOUND_BAR_RATIO, SF_SoundData );
  320. #endif
  321.  
  322.     //  Don't load server data in final build (will load on server automatically)
  323.     //TheHexa:: Why?? This will desync with our studio.. we can't see/edit/save - items spawns/player spawns/vehicles spawns etc..
  324. #ifndef FINAL_BUILD
  325.     {
  326.         //TheHexa:: SHOULD LOAD ONLY FOR EDIT MODE.
  327.         extern bool g_bEditMode;
  328.         if(g_bEditMode)
  329.         {
  330.             //  Server data
  331.             sprintf(fname, "%s\\ServerData.xml", r3dGameLevel::GetHomeDir());
  332.  
  333.             pugi::xml_document xmlServerFile;
  334.             char *serverData = 0;
  335.             ParseXMLFile(fname, xmlServerFile, serverData);
  336.  
  337.             xmlLevel = xmlServerFile.child("server_objects");
  338.             LoadLevelObjects ( xmlLevel, 0.f, SF_ServerData);
  339.  
  340.             delete [] serverData;
  341.         }
  342.     }
  343. #endif
  344.  
  345.     {
  346. #ifdef WO_SERVER
  347.         //  Server data
  348.         if(gServerLogic.ginfo_.channel != 3 && gServerLogic.ginfo_.channel != 4)
  349.             sprintf(fname, "%s\\ServerData.xml", r3dGameLevel::GetHomeDir());
  350.         else
  351.             sprintf(fname, "%s\\ServerDataPremium.xml", r3dGameLevel::GetHomeDir());
  352.  
  353.         pugi::xml_document xmlServerFile;
  354.         char *serverData = 0;
  355.         ParseXMLFile(fname, xmlServerFile, serverData);
  356.  
  357.         xmlLevel = xmlServerFile.child("server_objects");
  358.         LoadLevelObjects ( xmlLevel, 0.f, SF_ServerData);
  359.  
  360.         delete [] serverData;
  361. #endif
  362.     }
  363.  
  364.     // delete only after we are done parsing xml!
  365.     delete [] levelData;
  366. #ifndef WO_SERVER
  367.     delete [] soundData;
  368. #endif
  369.  
  370.     return 1;
  371. }
  372.  
  373. #ifndef FINAL_BUILD
  374. #ifndef WO_SERVER
  375. int LoadLevel_Groups ()
  376. {
  377.     char fname[MAX_PATH];
  378.     sprintf(fname, "Data\\ObjectsDepot\\LevelGroups.xml", r3dGameLevel::GetHomeDir());
  379.     obj_Group::LoadFromFile(fname);
  380.     return 1;
  381. }
  382. #endif 
  383. #endif
  384.  
  385. int LoadLevel_MatLibs()
  386. {
  387.   if(r3dMaterialLibrary::IsDynamic) {
  388.     // skip loading level materials if we're in editing mode
  389.     return 1;
  390.   }
  391.  
  392.   char fname[MAX_PATH];
  393.   sprintf(fname, "%s\\room.mat", r3dGameLevel::GetHomeDir());
  394.  
  395.   r3dFile* f = r3d_open(fname, "rt");
  396.   if(!f) {
  397.     r3dArtBug("LoadLevel: can't find %s - switching to dynamic matlib\n", fname);
  398.     r3dMaterialLibrary::IsDynamic = true;
  399.     return 1;
  400.   }
  401.  
  402.   char Str2[256], Str3[256];
  403.   sprintf(Str2,"%s\\room.mat", r3dGameLevel::GetHomeDir());
  404.   sprintf(Str3,"%s\\Textures\\", r3dGameLevel::GetHomeDir());
  405.    
  406.   r3dMaterialLibrary::LoadLibrary(Str2, Str3);
  407.   return 1;
  408. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement