public MapTile(TilePosition TilePosition) { this.TilePosition = TilePosition; // We create the tile Landscape = GlobalVars.GameEngine.Scene.CreateLandscape("Land" + TilePosition.TileX + ";" + TilePosition.TileZ); Landscape.GenerateTerrain(null, CONST_TV_LANDSCAPE_PRECISION.TV_PRECISION_HIGH, TileSize, TileSize, TilePosition.TileX * 256 * TileSize, 0, TilePosition.TileZ * 256 * TileSize); //Landscape.CreateEmptyTerrain(CONST_TV_LANDSCAPE_PRECISION.TV_PRECISION_LOW, TileSize, TileSize, TilePosition.TileX * 256 * TileSize, 0, TilePosition.TileZ * 256 * TileSize); //Landscape.CreateEmptyTerrain(CONST_TV_LANDSCAPE_PRECISION.TV_PRECISION_LOW, TileSize, TileSize, TilePosition.TileX * 256 + (TileSize - 1) * 256, 0, TilePosition.TileZ * 256 + (TileSize - 1) * 256); //Latest : //Landscape.EnableLOD(true, 100, CONST_TV_LANDSCAPE_PRECISION.TV_PRECISION_ULTRA_LOW, 0, true); Landscape.SetCullMode(CONST_TV_CULLING.TV_BACK_CULL); Landscape.SetCollisionEnable(false); System.Diagnostics.Debug.WriteLine(" Create landscape X:" + TilePosition.TileX * TileSize * 256 + "; Y:0; Z:" + TilePosition.TileZ * TileSize * 256 + "!"); }
public int TileDistanceTo(TilePosition Position) { return (int)Math.Sqrt(Math.Pow(Position.TileX - this.TileX, 2) + Math.Pow(Position.TileZ - this.TileZ, 2)); }
public WorldPosition(TilePosition Position) : base(Position) { this.TileX = Position.TileX; this.TileZ = Position.TileZ; }
public TilePosition(TilePosition Position) { this.TileX = Position.TileX; this.TileZ = Position.TileZ; }
//Second way of loading height private void LoadTileHeightmap(TilePosition Tilepos) { int tilei = Tilepos.TileX - WorldPos.TileX + RenderedTilesDistance; int tilej = Tilepos.TileX - WorldPos.TileX + RenderedTilesDistance; if ((tilei >= 0) && (tilei <= 2 * RenderedTilesDistance) && (tilej >= 0) && (tilej <= 2 * RenderedTilesDistance)) { //If the tile is still used MapTiles[tilei][tilej].Landscape.SetHeight(1, 1, 50); } }
//Force : force the reload of all the tiles //Check if there are tiles to load and load them public bool CheckLoadTiles(bool force = false) { System.Diagnostics.Debug.WriteLine("Start loadtiles (" + WorldPos.TileX + "," + WorldPos.TileZ + ")"); if (force || ((PlayerPos.TileX != WorldPos.TileX) || (PlayerPos.TileZ != WorldPos.TileZ))) { //The player moved too much LoadingMapTiles = true; System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest; int TilesMoveX = PlayerPos.TileX - WorldPos.TileX; int TilesMoveZ = PlayerPos.TileZ - WorldPos.TileZ; TimeSpan CheckLandscapeBegin = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; //We create the new MapTile which will contain the new terrains MapTile[][] MapTiles_New = new MapTile[2 * RenderedTilesDistance + 1][];// = MapTiles = new MapTile[2 * RenderedTilesDistance + 1, 2 * RenderedTilesDistance + 1]; LinkedList<MapTile> TilesList_New = new LinkedList<MapTile>(); //We check all the new tiles to see if we have to create a new one or copy an old one for (short i = 0; i < (2 * RenderedTilesDistance + 1); i++) { MapTiles_New[i] = new MapTile[2 * RenderedTilesDistance + 1]; for (short j = 0; j < (2 * RenderedTilesDistance + 1); j++) { int oldIndexX = i + TilesMoveX; //Correspondance to the old MapTiles int oldIndexZ = j + TilesMoveZ; //Correspondance to the old MapTiles if (!force && (((oldIndexX >= 0) && (oldIndexX <= 2 * RenderedTilesDistance)) && ((oldIndexZ >= 0) && (oldIndexZ <= 2 * RenderedTilesDistance)))) { //The old index is valid, we just move the tile MapTiles_New[i][j] = MapTiles[oldIndexX][oldIndexZ]; } else { //New landscape, we create it TimeSpan NewLandscapeBegin = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; MapTiles_New[i][j] = new MapTile(new TilePosition(PlayerPos.TileX + i - RenderedTilesDistance, PlayerPos.TileZ + j - RenderedTilesDistance)); TimeSpan NewLandscapeEnd = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; System.Diagnostics.Debug.WriteLine("New tile i:" + i + "(" + (PlayerPos.TileX + i - RenderedTilesDistance) + ");j:" + j + "(" + (PlayerPos.TileZ + j - RenderedTilesDistance) + ") - " + (NewLandscapeEnd - NewLandscapeBegin).TotalMilliseconds + " ms."); } if ((i - RenderedTilesDistance == 0) && (j - RenderedTilesDistance == 0)) { //Player's tile MapTiles_New[i][j].Landscape.SetTexture(GlobalVars.GameEngine.Globals.GetTex("LandTexture"), -1); MapTiles_New[i][j].Landscape.SetTextureScale(3, 3, -1); } else { //Another tile MapTiles_New[i][j].Landscape.SetTexture(GlobalVars.GameEngine.Globals.GetTex("LandTexture2"), -1); MapTiles_New[i][j].Landscape.SetTextureScale(3, 3, -1); } TilePosition Position = new TilePosition(i - RenderedTilesDistance + PlayerPos.TileX, j - RenderedTilesDistance + PlayerPos.TileZ); TilesHeightmapToLoad.Enqueue(Position, Position.TileDistanceTo(PlayerPos)); //LoadTileHeightmap(new TilePosition(i - RenderedTilesDistance, j - RenderedTilesDistance)); //LoadTileHeightmap2(MapTiles_New[i][j]); WorldPosition SplatPosition = new WorldPosition(Position); /* MapTiles_New[i][j].Landscape.AddSplattingTexture(GlobalVars.GameEngine.Globals.GetTex("SplattingTexture"), 1, 1, 1, 0, 0); MapTiles_New[i][j].Landscape.ExpandSplattingTexture(GlobalVars.GameEngine.Globals.GetTex("SplattingAlphaTexture"), GlobalVars.GameEngine.Globals.GetTex("SplattingTexture"), 0, 0, 4, 4); MapTiles_New[i][j].Landscape.SetSplattingEnable(true);*/ //AddSplattingToTile(SplatPosition, GlobalVars.GameEngine.Globals.GetTex("SplattingTexture")); TilesList_New.AddLast(MapTiles_New[i][j]); } } TimeSpan CheckLandscapeEnd = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; //We replace the old tiles with the new ones MapTiles = MapTiles_New; TilesList = TilesList_New; TimeSpan CopyMapTilesEnd = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; WorldPos.TileX = PlayerPos.TileX; WorldPos.TileZ = PlayerPos.TileZ; //DEBUG : add a mesh to each tile /*for (short i = 0; i < (2 * RenderedTilesDistance + 1); i++) { for (short j = 0; j < (2 * RenderedTilesDistance + 1); j++) { if (!MapTiles[i][j].MeshesLoaded) //DEBUG { MapTiles[i][j].MeshesLoaded = true; //DEBUG //Debug : we add a mesh TVMesh newmesh = new TVMesh(); newmesh = GlobalVars.GameEngine.Scene.CreateMeshBuilder(); newmesh.CreateTeapot(); newmesh.SetScale(50, 50, 50); newmesh.SetCullMode(CONST_TV_CULLING.TV_BACK_CULL); AddMeshToTile(new WorldPosition(i - RenderedTilesDistance + PlayerPos.TileX, i - RenderedTilesDistance + PlayerPos.TileZ, 0, 0, 0), newmesh); } } }*/ LoadingMapTiles = false; Thread.CurrentThread.Priority = ThreadPriority.Normal; System.Diagnostics.Debug.WriteLine("Finished load new MapTiles (" + WorldPos.TileX + "," + WorldPos.TileZ + ") - " + (CheckLandscapeEnd - CheckLandscapeBegin).TotalMilliseconds + " ms|" + (CopyMapTilesEnd - CheckLandscapeEnd).TotalMilliseconds + " ms"); return true; } System.Diagnostics.Debug.WriteLine("Nothing loaded"); return false; }
public int TileDistanceTo(TilePosition Position) { return((int)Math.Sqrt(Math.Pow(Position.TileX - this.TileX, 2) + Math.Pow(Position.TileZ - this.TileZ, 2))); }
//Check if there are tiles to load and load them public bool CheckLoadTiles(bool force = false) //Force : force the reload of all the tiles { System.Diagnostics.Debug.WriteLine("Start loadtiles (" + WorldPos.TileX + "," + WorldPos.TileZ + ")"); if (force || ((PlayerPos.TileX != WorldPos.TileX) || (PlayerPos.TileZ != WorldPos.TileZ))) { //The player moved too much LoadingMapTiles = true; System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest; int TilesMoveX = PlayerPos.TileX - WorldPos.TileX; int TilesMoveZ = PlayerPos.TileZ - WorldPos.TileZ; TimeSpan CheckLandscapeBegin = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; //We create the new MapTile which will contain the new terrains MapTile[][] MapTiles_New = new MapTile[2 * RenderedTilesDistance + 1][];// = MapTiles = new MapTile[2 * RenderedTilesDistance + 1, 2 * RenderedTilesDistance + 1]; LinkedList <MapTile> TilesList_New = new LinkedList <MapTile>(); //We check all the new tiles to see if we have to create a new one or copy an old one for (short i = 0; i < (2 * RenderedTilesDistance + 1); i++) { MapTiles_New[i] = new MapTile[2 * RenderedTilesDistance + 1]; for (short j = 0; j < (2 * RenderedTilesDistance + 1); j++) { int oldIndexX = i + TilesMoveX; //Correspondance to the old MapTiles int oldIndexZ = j + TilesMoveZ; //Correspondance to the old MapTiles if (!force && (((oldIndexX >= 0) && (oldIndexX <= 2 * RenderedTilesDistance)) && ((oldIndexZ >= 0) && (oldIndexZ <= 2 * RenderedTilesDistance)))) { //The old index is valid, we just move the tile MapTiles_New[i][j] = MapTiles[oldIndexX][oldIndexZ]; } else { //New landscape, we create it TimeSpan NewLandscapeBegin = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; MapTiles_New[i][j] = new MapTile(new TilePosition(PlayerPos.TileX + i - RenderedTilesDistance, PlayerPos.TileZ + j - RenderedTilesDistance)); TimeSpan NewLandscapeEnd = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; System.Diagnostics.Debug.WriteLine("New tile i:" + i + "(" + (PlayerPos.TileX + i - RenderedTilesDistance) + ");j:" + j + "(" + (PlayerPos.TileZ + j - RenderedTilesDistance) + ") - " + (NewLandscapeEnd - NewLandscapeBegin).TotalMilliseconds + " ms."); } if ((i - RenderedTilesDistance == 0) && (j - RenderedTilesDistance == 0)) { //Player's tile MapTiles_New[i][j].Landscape.SetTexture(GlobalVars.GameEngine.Globals.GetTex("LandTexture"), -1); MapTiles_New[i][j].Landscape.SetTextureScale(3, 3, -1); } else { //Another tile MapTiles_New[i][j].Landscape.SetTexture(GlobalVars.GameEngine.Globals.GetTex("LandTexture2"), -1); MapTiles_New[i][j].Landscape.SetTextureScale(3, 3, -1); } TilePosition Position = new TilePosition(i - RenderedTilesDistance + PlayerPos.TileX, j - RenderedTilesDistance + PlayerPos.TileZ); TilesHeightmapToLoad.Enqueue(Position, Position.TileDistanceTo(PlayerPos)); //LoadTileHeightmap(new TilePosition(i - RenderedTilesDistance, j - RenderedTilesDistance)); //LoadTileHeightmap2(MapTiles_New[i][j]); WorldPosition SplatPosition = new WorldPosition(Position); /*MapTiles_New[i][j].Landscape.AddSplattingTexture(GlobalVars.GameEngine.Globals.GetTex("SplattingTexture"), 1, 1, 1, 0, 0); * MapTiles_New[i][j].Landscape.ExpandSplattingTexture(GlobalVars.GameEngine.Globals.GetTex("SplattingAlphaTexture"), GlobalVars.GameEngine.Globals.GetTex("SplattingTexture"), 0, 0, 4, 4); * MapTiles_New[i][j].Landscape.SetSplattingEnable(true);*/ //AddSplattingToTile(SplatPosition, GlobalVars.GameEngine.Globals.GetTex("SplattingTexture")); TilesList_New.AddLast(MapTiles_New[i][j]); } } TimeSpan CheckLandscapeEnd = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; //We replace the old tiles with the new ones MapTiles = MapTiles_New; TilesList = TilesList_New; TimeSpan CopyMapTilesEnd = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; WorldPos.TileX = PlayerPos.TileX; WorldPos.TileZ = PlayerPos.TileZ; //DEBUG : add a mesh to each tile /*for (short i = 0; i < (2 * RenderedTilesDistance + 1); i++) * { * for (short j = 0; j < (2 * RenderedTilesDistance + 1); j++) * { * if (!MapTiles[i][j].MeshesLoaded) //DEBUG * { * MapTiles[i][j].MeshesLoaded = true; //DEBUG * * //Debug : we add a mesh * TVMesh newmesh = new TVMesh(); * newmesh = GlobalVars.GameEngine.Scene.CreateMeshBuilder(); * newmesh.CreateTeapot(); * newmesh.SetScale(50, 50, 50); * newmesh.SetCullMode(CONST_TV_CULLING.TV_BACK_CULL); * AddMeshToTile(new WorldPosition(i - RenderedTilesDistance + PlayerPos.TileX, i - RenderedTilesDistance + PlayerPos.TileZ, 0, 0, 0), newmesh); * } * } * }*/ LoadingMapTiles = false; Thread.CurrentThread.Priority = ThreadPriority.Normal; System.Diagnostics.Debug.WriteLine("Finished load new MapTiles (" + WorldPos.TileX + "," + WorldPos.TileZ + ") - " + (CheckLandscapeEnd - CheckLandscapeBegin).TotalMilliseconds + " ms|" + (CopyMapTilesEnd - CheckLandscapeEnd).TotalMilliseconds + " ms"); return(true); } System.Diagnostics.Debug.WriteLine("Nothing loaded"); return(false); }