Commit [d0e2e7] Maximize Restore History

Merge commit '9db5e03efe069945656cb089375235f07a03327b'

henon henon 2011-12-21

added GameModeFortress/Shadows.cs
added .gitignore
changed GameModeFortress/GuiStates/GuiStateEscapeMenu.cs
changed GameModeFortress/GameModeFortress.csproj
changed GameModeFortress/ManicDiggerGameWindow.cs
changed GameModeFortress/Program.cs
changed GameModeFortress/Server.cs
changed GameModeFortress/ServerWorldManager.cs
changed ManicDigger/MapTools/Shadows.cs
changed ManicDigger/Renderers/TerrainRenderer.cs
changed ManicDigger/Renderers/WeaponRenderer.cs
changed ManicDigger/DirtyChunks.cs
changed ManicDigger/ManicDigger.cs
copied GameModeFortress/InfiniteMap.cs -> GameModeFortress/TerrainRenderer.cs
GameModeFortress/Shadows.cs Diff Switch to side-by-side view
Loading...
.gitignore Diff Switch to side-by-side view
Loading...
GameModeFortress/GuiStates/GuiStateEscapeMenu.cs Diff Switch to side-by-side view
Loading...
GameModeFortress/GameModeFortress.csproj Diff Switch to side-by-side view
Loading...
GameModeFortress/ManicDiggerGameWindow.cs Diff Switch to side-by-side view
Loading...
GameModeFortress/Program.cs Diff Switch to side-by-side view
Loading...
GameModeFortress/Server.cs Diff Switch to side-by-side view
Loading...
GameModeFortress/ServerWorldManager.cs Diff Switch to side-by-side view
Loading...
ManicDigger/MapTools/Shadows.cs Diff Switch to side-by-side view
Loading...
ManicDigger/Renderers/TerrainRenderer.cs Diff Switch to side-by-side view
Loading...
ManicDigger/Renderers/WeaponRenderer.cs Diff Switch to side-by-side view
Loading...
ManicDigger/DirtyChunks.cs Diff Switch to side-by-side view
Loading...
ManicDigger/ManicDigger.cs Diff Switch to side-by-side view
Loading...
GameModeFortress/InfiniteMap.cs to GameModeFortress/TerrainRenderer.cs
--- a/GameModeFortress/InfiniteMap.cs
+++ b/GameModeFortress/TerrainRenderer.cs
@@ -1,224 +1,235 @@
 ďťżusing System;
 using System.Collections.Generic;
 using System.Text;
-using ManicDigger;
-
-namespace GameModeFortress
+using OpenTK;
+using System.Diagnostics;
+
+namespace ManicDigger
 {
-    public class InfiniteMapChunked : IMapStorage, IMapStoragePortion
+    public partial class ManicDiggerGameWindow
     {
-        public class Chunk
-        {
-            public byte[] data;
-            public int LastUpdate;
-            public bool IsPopulated;
-            public int LastChange;
-        }
-        //[Inject]
-        //public IWorldGenerator generator;
-        [Inject]
-        public IIsChunkDirty d_IsChunkReady;
-        public Chunk[] chunks;
-        #region IMapStorage Members
-        public int MapSizeX { get; set; }
-        public int MapSizeY { get; set; }
-        public int MapSizeZ { get; set; }
-        public int GetBlock(int x, int y, int z)
-        {
-            int cx = x / chunksize;
-            int cy = y / chunksize;
-            int cz = z / chunksize;
-            Chunk chunk = chunks[MapUtil.Index3d(cx, cy, cz, MapSizeX / chunksize, MapSizeY / chunksize)];
-            if (chunk == null)
-            {
-                return 0;
-            }
-            return chunk.data[MapUtil.Index3d(x % chunksize, y % chunksize, z % chunksize, chunksize, chunksize)];
-        }
-        public void SetBlock(int x, int y, int z, int tileType)
-        {
-            byte[] chunk = GetChunk(x, y, z);
-            chunk[MapUtil.Index3d(x % chunksize, y % chunksize, z % chunksize, chunksize, chunksize)] = (byte)tileType;
-            SetChunkDirty(x / chunksize, y / chunksize, z / chunksize, true);
-        }
-        public void UseMap(byte[, ,] map)
-        {
-        }
-        #endregion
-        public byte[] GetChunk(int x, int y, int z)
-        {
-            x = x / chunksize;
-            y = y / chunksize;
-            z = z / chunksize;
-            int mapsizexchunks = MapSizeX / chunksize;
-            int mapsizeychunks = MapSizeY / chunksize;
-            Chunk chunk = chunks[MapUtil.Index3d(x, y, z, mapsizexchunks, mapsizeychunks)];
-            if (chunk == null)
-            {
-                //byte[, ,] newchunk = new byte[chunksize, chunksize, chunksize];
-                //byte[, ,] newchunk = generator.GetChunk(x, y, z, chunksize);
-                //if (newchunk != null)
-                //{
-                //    chunks[x, y, z] = new Chunk() { data = MapUtil.ToFlatMap(newchunk) };
-                //}
-                //else
-                {
-                    chunks[MapUtil.Index3d(x, y, z, mapsizexchunks, mapsizeychunks)] = new Chunk() { data = new byte[chunksize * chunksize * chunksize] };
-                }
-                return chunks[MapUtil.Index3d(x, y, z, mapsizexchunks, mapsizeychunks)].data;
-            }
-            return chunk.data;
-        }
-        public int chunksize = 16;
-        public void Reset(int sizex, int sizey, int sizez)
-        {
-            MapSizeX = sizex;
-            MapSizeY = sizey;
-            MapSizeZ = sizez;
-            chunks = new Chunk[(sizex / chunksize) * (sizey / chunksize) * (sizez / chunksize)];
-            SetAllChunksNotDirty();
-        }
-        #region IMapStorage Members
-        public void SetMapPortion(int x, int y, int z, byte[, ,] chunk)
-        {
-            int chunksizex = chunk.GetUpperBound(0) + 1;
-            int chunksizey = chunk.GetUpperBound(1) + 1;
-            int chunksizez = chunk.GetUpperBound(2) + 1;
-            if (chunksizex % chunksize != 0) { throw new ArgumentException(); }
-            if (chunksizey % chunksize != 0) { throw new ArgumentException(); }
-            if (chunksizez % chunksize != 0) { throw new ArgumentException(); }
-            byte[, ,][] localchunks = new byte[chunksizex / chunksize, chunksizey / chunksize, chunksizez / chunksize][];
-            for (int cx = 0; cx < chunksizex / chunksize; cx++)
-            {
-                for (int cy = 0; cy < chunksizey / chunksize; cy++)
-                {
-                    for (int cz = 0; cz < chunksizex / chunksize; cz++)
-                    {
-                        localchunks[cx, cy, cz] = GetChunk(x + cx * chunksize, y + cy * chunksize, z + cz * chunksize);
-                        FillChunk(localchunks[cx, cy, cz], chunksize, cx * chunksize, cy * chunksize, cz * chunksize, chunk);
-                    }
-                }
-            }
-            for (int xxx = 0; xxx < chunksizex; xxx += chunksize)
-            {
-                for (int yyy = 0; yyy < chunksizex; yyy += chunksize)
-                {
-                    for (int zzz = 0; zzz < chunksizex; zzz += chunksize)
-                    {
-                        SetChunkDirty((x + xxx) / chunksize, (y + yyy) / chunksize, (z + zzz) / chunksize, true);
-                        SetChunksAroundDirty((x + xxx) / chunksize, (y + yyy) / chunksize, (z + zzz) / chunksize);
-                    }
-                }
-            }
-        }
-        private void SetChunksAroundDirty(int x, int y, int z)
-        {
-            if (IsValidChunkPosition(x, y, z)) { SetChunkDirty(x - 1, y, z, true); }
-            if (IsValidChunkPosition(x - 1, y, z)) { SetChunkDirty(x - 1, y, z, true); }
-            if (IsValidChunkPosition(x + 1, y, z)) { SetChunkDirty(x + 1, y, z, true); }
-            if (IsValidChunkPosition(x, y - 1, z)) { SetChunkDirty(x, y - 1, z, true); }
-            if (IsValidChunkPosition(x, y + 1, z)) { SetChunkDirty(x, y + 1, z, true); }
-            if (IsValidChunkPosition(x, y, z - 1)) { SetChunkDirty(x, y, z - 1, true); }
-            if (IsValidChunkPosition(x, y, z + 1)) { SetChunkDirty(x, y, z + 1, true); }
-        }
-        private bool IsValidChunkPosition(int xx, int yy, int zz)
-        {
-            return xx >= 0 && yy >= 0 && zz >= 0
-                && xx < MapSizeX / chunksize
-                && yy < MapSizeY / chunksize
-                && zz < MapSizeZ / chunksize;
-        }
-        private void FillChunk(byte[] destination, int destinationchunksize,
-            int sourcex, int sourcey, int sourcez, byte[, ,] source)
-        {
-            for (int x = 0; x < destinationchunksize; x++)
-            {
-                for (int y = 0; y < destinationchunksize; y++)
-                {
-                    for (int z = 0; z < destinationchunksize; z++)
-                    {
-                        //if (x + sourcex < source.GetUpperBound(0) + 1
-                        //    && y + sourcey < source.GetUpperBound(1) + 1
-                        //    && z + sourcez < source.GetUpperBound(2) + 1)
+        int chunkupdates;
+        public int ChunkUpdates { get { return chunkupdates; } }
+
+        int mapAreaSize { get { return (int)d_Config3d.viewdistance; } }
+        int centerAreaSize { get { return (int)d_Config3d.viewdistance / 2; } }
+
+        public void StartTerrain()
+        {
+            //Toggle fog.
+            var p = MapUtil.PlayerArea(mapAreaSize, centerAreaSize, PlayerBlockPosition());
+            CurrentRendererMapPositionG = new Vector3i(p.X, p.Y, 0);
+            if (RendererMap != null)
+            {
+                throw new NotImplementedException();
+            }
+            RendererMap = new RenderedChunk[(mapAreaSize / chunksize) * (mapAreaSize / chunksize) * (MapSizeZ / chunksize)];
+            for (int i = 0; i < RendererMap.Length; i++)
+            {
+                RendererMap[i] = new RenderedChunk();
+            }
+        }
+
+        public void DrawTerrain()
+        {
+            d_Batcher.Draw(LocalPlayerPosition);
+        }
+
+        public void TryRemap()
+        {
+            var p = MapUtil.PlayerArea(mapAreaSize, centerAreaSize, PlayerBlockPosition());
+            var newMapPosition = new Vector3i(p.X, p.Y, 0);
+            if (CurrentRendererMapPositionG != newMapPosition)
+            {
+                //todo: check if complete terrain in new area is already downloaded.
+                Remap(newMapPosition);
+                CurrentRendererMapPositionG = newMapPosition;
+            }
+        }
+
+        //todo: use this for chunk byte[], not just for terrain renderer meshes.
+        public void Remap(Vector3i newMapPosition)
+        {
+            //make a list of old chunks
+            var newRendererMap = new RenderedChunk[RendererMap.Length];
+            Dictionary<Vector3i, RenderedChunk> oldChunks = new Dictionary<Vector3i, RenderedChunk>();
+            for (int x = 0; x < mapAreaSize / chunksize; x++)
+            {
+                for (int y = 0; y < mapAreaSize / chunksize; y++)
+                {
+                    for (int z = 0; z < MapSizeZ / chunksize; z++)
+                    {
+                        int pos = MapUtil.Index3d(x, y, z, mapAreaSize / chunksize, mapAreaSize / chunksize);
+                        int chunkx = x + CurrentRendererMapPositionG.x / chunksize;
+                        int chunky = y + CurrentRendererMapPositionG.y / chunksize;
+                        int chunkz = z + CurrentRendererMapPositionG.z / chunksize;
+                        Vector3i pos2 = new Vector3i(chunkx, chunky, chunkz);
+                        oldChunks[pos2] = RendererMap[pos];
+                    }
+                }
+            }
+            for (int x = 0; x < mapAreaSize / chunksize; x++)
+            {
+                for (int y = 0; y < mapAreaSize / chunksize; y++)
+                {
+                    for (int z = 0; z < MapSizeZ / chunksize; z++)
+                    {
+                        int pos = MapUtil.Index3d(x, y, z, mapAreaSize / chunksize, mapAreaSize / chunksize);
+                        int newchunkx = x + newMapPosition.x / chunksize;
+                        int newchunky = y + newMapPosition.y / chunksize;
+                        int newchunkz = z + newMapPosition.z / chunksize;
+                        Vector3i pos2 = new Vector3i(newchunkx, newchunky, newchunkz);
+                        if (oldChunks.ContainsKey(pos2))
                         {
-                            destination[MapUtil.Index3d(x, y, z, destinationchunksize, destinationchunksize)]
-                                = source[x + sourcex, y + sourcey, z + sourcez];
+                            //if already loaded
+                            newRendererMap[pos] = oldChunks[pos2];
+                            oldChunks[pos2] = null;
                         }
-                    }
-                }
-            }
-        }
-        #endregion
-        #region IIsChunkReady Members
-        public bool IsChunkReady(int x, int y, int z)
-        {
-            return IsChunkDirty(x, y, z);
-        }
-        #endregion
-        #region IIsChunkReady Members
-        public bool IsChunkDirty(int x, int y, int z)
-        {
-            return d_IsChunkReady.IsChunkDirty(x, y, z);
-        }
-        public void SetChunkDirty(int x, int y, int z, bool dirty)
-        {
-            d_IsChunkReady.SetChunkDirty(x, y, z, dirty);
-        }
-        #endregion
-        public void SetAllChunksNotDirty()
-        {
-            d_IsChunkReady.SetAllChunksNotDirty();
-        }
-        public void GetMapPortion(byte[] outPortion, int x, int y, int z, int portionsizex, int portionsizey, int portionsizez)
-        {
-            Array.Clear(outPortion, 0, outPortion.Length);
-
-            int chunksizebits = (int)Math.Log(chunksize, 2);
-            int mapchunksx = MapSizeX / chunksize;
-            int mapchunksy = MapSizeY / chunksize;
-            int mapchunksz = MapSizeZ / chunksize;
-            int mapsizechunks = mapchunksx * mapchunksy * mapchunksz;
-
-            for (int xx = 0; xx < portionsizex; xx++)
-            {
-                for (int yy = 0; yy < portionsizey; yy++)
-                {
-                    for (int zz = 0; zz < portionsizez; zz++)
-                    {
-                        //Find chunk.
-                        int cx = (x + xx) >> chunksizebits;
-                        int cy = (y + yy) >> chunksizebits;
-                        int cz = (z + zz) >> chunksizebits;
-                        //int cpos = MapUtil.Index3d(cx, cy, cz, MapSizeX / chunksize, MapSizeY / chunksize);
-                        int cpos = (cz * mapchunksy + cy) * mapchunksx + cx;
-                        //if (cpos < 0 || cpos >= ((MapSizeX / chunksize) * (MapSizeY / chunksize) * (MapSizeZ / chunksize)))
-                        if (cpos < 0 || cpos >= mapsizechunks)
+                        else
                         {
-                            continue;
+                            //if needs loading
+                            newRendererMap[pos] = new RenderedChunk();
                         }
-                        Chunk chunk = chunks[cpos];
-                        if (chunk == null || chunk.data == null)
+                    }
+                }
+            }
+            foreach (var k in oldChunks)
+            {
+                //wasn't used in new area.
+                if (k.Value != null && k.Value.ids != null)
+                {
+                    foreach (var subMeshId in k.Value.ids)
+                    {
+                        d_Batcher.Remove(subMeshId);
+                    }
+                }
+                //todo: save to disk
+            }
+            for (int i = 0; i < newRendererMap.Length; i++)
+            {
+                RendererMap[i] = newRendererMap[i];
+            }
+        }
+
+        public void UpdateTerrain()
+        {
+            if (RendererMap == null)
+            {
+                //Start() not called yet.
+                return;
+            }
+            Stopwatch stopwatch = new Stopwatch();
+            stopwatch.Start();
+
+            TryRemap();
+
+            for (int x = 0; x < mapAreaSize / chunksize; x++)
+            {
+                for (int y = 0; y < mapAreaSize / chunksize; y++)
+                {
+                    for (int z = 0; z < MapSizeZ / chunksize; z++)
+                    {
+                        int pos = MapUtil.Index3d(x, y, z, mapAreaSize / chunksize, mapAreaSize / chunksize);
+                        if (RendererMap[pos].dirty)
                         {
-                            continue;
+                            if (RendererMap[pos].ids != null)
+                            {
+                                foreach (int loadedSubmesh in RendererMap[pos].ids)
+                                {
+                                    d_Batcher.Remove(loadedSubmesh);
+                                }
+                            }
+                            RendererMap[pos].dirty = false;
+                            List<int> ids = new List<int>();
+                            var a = d_TerrainChunkTesselator.MakeChunk(CurrentRendererMapPositionG.x / chunksize + x,
+                                CurrentRendererMapPositionG.y / chunksize + y, z);
+                            foreach (var submesh in a)
+                            {
+                                if (submesh.indices.Length != 0)
+                                {
+                                    Vector3 center = new Vector3(submesh.position.X + chunksize / 2, submesh.position.Z + chunksize / 2, submesh.position.Y + chunksize / 2);
+                                    float radius = chunksize;
+                                    ids.Add(d_Batcher.Add(submesh.indices, submesh.indicesCount, submesh.vertices, submesh.verticesCount, submesh.transparent, submesh.texture, center, radius));
+                                }
+                            }
+                            RendererMap[pos].ids = ids.ToArray();
+                            if (stopwatch.ElapsedMilliseconds > 2)
+                            {
+                                goto exit;
+                            }
                         }
-                        //int pos = MapUtil.Index3d((x + xx) % chunksize, (y + yy) % chunksize, (z + zz) % chunksize, chunksize, chunksize);
-                        int chunkGlobalX = cx << chunksizebits;
-                        int chunkGlobalY = cy << chunksizebits;
-                        int chunkGlobalZ = cz << chunksizebits;
-
-                        int inChunkX = (x + xx) - chunkGlobalX;
-                        int inChunkY = (y + yy) - chunkGlobalY;
-                        int inChunkZ = (z + zz) - chunkGlobalZ;
-
-                        //int pos = MapUtil.Index3d(inChunkX, inChunkY, inChunkZ, chunksize, chunksize);
-                        int pos = (((inChunkZ << chunksizebits) + inChunkY) << chunksizebits) + inChunkX;
-
-                        int block = chunk.data[pos];
-                        //outPortion[MapUtil.Index3d(xx, yy, zz, portionsizex, portionsizey)] = (byte)block;
-                        outPortion[(zz * portionsizey + yy) * portionsizex + xx] = (byte)block;
-                    }
-                }
-            }
-        }
+                    }
+                }
+            }
+        exit:
+            ;
+        }
+
+        public void RedrawBlock(int x, int y, int z)
+        {
+            foreach (var a in MapUtil.BlocksAround(new Vector3(x, y, z)))
+            {
+                int xx = (int)a.X - CurrentRendererMapPositionG.x;
+                int yy = (int)a.Y - CurrentRendererMapPositionG.y;
+                int zz = (int)a.Z - CurrentRendererMapPositionG.z;
+                if (xx < 0 || yy < 0 || zz < 0 || xx >= mapAreaSize || yy >= mapAreaSize || zz >= MapSizeZ)
+                {
+                    return;
+                }
+                RendererMap[MapUtil.Index3d(xx / chunksize, yy / chunksize, zz / chunksize, mapAreaSize / chunksize, mapAreaSize / chunksize)].dirty = true;
+            }
+        }
+
+        void SetChunkDirty(int cx, int cy, int cz, bool dirty)
+        {
+            int x = cx * chunksize;
+            int y = cy * chunksize;
+            int z = cz * chunksize;
+            if (x >= CurrentRendererMapPositionG.x
+                && y >= CurrentRendererMapPositionG.y
+                && z >= CurrentRendererMapPositionG.z
+                && x < CurrentRendererMapPositionG.x + mapAreaSize
+                && y < CurrentRendererMapPositionG.y + mapAreaSize
+                && z < MapSizeZ)
+            {
+                int xx = x - CurrentRendererMapPositionG.x;
+                int yy = y - CurrentRendererMapPositionG.y;
+                int zz = z - CurrentRendererMapPositionG.z;
+                RendererMap[MapUtil.Index3d(xx / chunksize, yy / chunksize, zz / chunksize, mapAreaSize / chunksize, mapAreaSize / chunksize)].dirty = dirty;
+            }
+        }
+
+        public class RenderedChunk
+        {
+            public int[] ids;
+            public bool dirty = true;
+        }
+
+        RenderedChunk[] RendererMap;
+        Vector3i CurrentRendererMapPositionG;
+
+        public void RedrawAllBlocks()
+        {
+            if (RendererMap == null)
+            {
+                return;
+            }
+            for (int i = 0; i < RendererMap.Length; i++)
+            {
+                RendererMap[i].dirty = true;
+            }
+        }
+
+        Vector3i PlayerBlockPosition()
+        {
+            return new Vector3i((int)LocalPlayerPosition.X, (int)LocalPlayerPosition.Z, (int)LocalPlayerPosition.Y);
+        }
+
+        public int TrianglesCount()
+        {
+            return d_Batcher.TotalTriangleCount;
+        }
+
+        bool shadowssimple;
     }
 }