Пример #1
0
    public void SpawnChunksAroundPoint(Vector3 point)
    {
        SpawnChunkFeild();
        Loom.QueueAsyncTask(WorldThreadName, () => {
            MazeGen module = new MazeGen(VoxelSettings.SuperSizeX / 2, 4, VoxelSettings.SuperSizeZ / 2, VoxelSettings.seed, 2, 1);
            for (int x = -1; x <= VoxelSettings.maxChunksX; x++)
            {
                for (int z = -1; z <= VoxelSettings.maxChunksZ; z++)
                {
                    Vector3Int location3D = new Vector3Int(x, 0, z);
                    Vector2Int location2D = new Vector2Int(location3D.x, location3D.z);
                    try
                    {
                        if (Chunks.ContainsKey(location3D) && !Chunks[location3D].Generated)
                        {
                            float[,] surface = Chunks[location3D].GenerateChunk(module);
                            Chunks[location3D].Render(false);
                        }
                    }
                    catch (Exception e)
                    {
                        SafeDebug.LogException(e);
                    }
                }
            }
            SafeDebug.Log("Finished rendering.");

            Loom.QueueOnMainThread(() =>
            {
                //MapTexture = module.GetTexture();
            });
        });
    }
Пример #2
0
 public void SpawnChunksLinear()
 {
     SpawnChunkFeild();
     Loom.QueueAsyncTask(WorldThreadName, () =>
     {
         TerrainModule module = new TerrainModule(VoxelSettings.seed);
         for (int x = 0; x < VoxelSettings.maxChunksX; x++)
         {
             for (int z = 0; z < VoxelSettings.maxChunksZ; z++)
             {
                 for (int y = 0; y < VoxelSettings.maxChunksY_M; y++)
                 {
                     Vector3Int location3D = new Vector3Int(x, y, z);
                     GenerateChunk(location3D, module);
                 }
             }
         }
         SafeDebug.Log("Finished rendering.");
         Loom.QueueOnMainThread(() =>
         {
             _generating = false;
             OnRenderComplete();
         });
     });
 }
Пример #3
0
        public static void PrintNoFormat(object message)
        {
            SafeDebug.Log("[Server]: " + message);

            /*AddEntry(LogLevel.Print, message.ToString());
             * Action a = () => LogToFile(message.ToString());
             * QueueLog(a);*/
        }
Пример #4
0
        public static void Print(object message, params object[] args)
        {
            SafeDebug.Log("[Server]: " + string.Format(message.ToString(), args));

            /*AddEntry(LogLevel.Print, string.Format(message.ToString(), args));
             * Action a = () => LogToFile(string.Format(message.ToString(), args));
             * QueueLog(a);*/
        }
Пример #5
0
        public static void Log(object message, params object[] args)
        {
            SafeDebug.Log("[Server]: " + string.Format(message.ToString(), args));

            /*string messageStr = string.Format(message.ToString(), args);
             * AddEntry(LogLevel.Log, string.Format("[{0}]: {1}", GetTime(), messageStr));
             * Action a = () => LogToFile(string.Format("[{0}]: {1}", GetTime(), messageStr));
             * QueueLog(a);*/
        }
Пример #6
0
 public void UdpConnect()
 {
     udpClient = new UdpClient();
     udpClient.Connect(Host, UdpPort);
     endPoint     = udpClient.Client.RemoteEndPoint as IPEndPoint;
     UdpLocalPort = ((IPEndPoint)udpClient.Client.LocalEndPoint).Port;
     SafeDebug.Log(string.Format("UDP client starting: {0}, {1}, {2}", UdpLocalPort, endPoint.Port, endPoint.Address.ToString()));
     udpStarted = true;
     BegineReceiveUDP();
     Send(0xff, BufferUtils.AddFirst(0x02, BitConverter.GetBytes(UdpLocalPort)), Protocal.Udp);
     StartUdpPing();
 }
Пример #7
0
 static void Initialize()
 {
     if (!initialized)
     {
         if (!Application.isPlaying)
         {
             return;
         }
         initialized = true;
         var g = new GameObject("Loom");
         _current = g.AddComponent <Loom>();
         SafeDebug.Log("Loom object created.");
     }
 }
Пример #8
0
    private void AllocateBlockArray(int sizeX, int sizeY, int sizeZ)
    {
        //blocks = new Block[sizeX * sizeY * sizeZ];
        blocks_is0  = new float[sizeX * sizeY * sizeZ];
        blocks_type = new uint[sizeX * sizeY * sizeZ];
        blocks_set  = new bool[sizeX * sizeY * sizeZ];
        SurfaceData = new double[(sizeX + 2) * (sizeZ + 2)];

        GridNormals = new Vector3[sizeX, sizeY, sizeZ];

        SafeDebug.Log("test index: " + Get_Flat_Index(0, 16, 15));
        SafeDebug.Log("test index: " + (0 + 32 * (16 + 16 * 15)));
        SafeDebug.Log(string.Format("{0}, {1}, {2},", ChunkSizeX, ChunkSizeY, ChunkSizeZ));
        SafeDebug.Log(blocks_set.Length);
    }
Пример #9
0
    private void ReceiveChunk_cmd(Data data)
    {
        DebugTimer.Stop();
        SafeDebug.Log(string.Format("Received chunk: {0}, Time: {1}.", data.Buffer.Length, DebugTimer.Elapsed()));
        BinaryReader reader = GetReaderStream(data.Buffer);

        int      locX = reader.ReadInt32();
        int      locY = reader.ReadInt32();
        int      locZ = reader.ReadInt32();
        LOD_Mode mode = (LOD_Mode)reader.ReadByte();



        //ColumnPacket packet = new ColumnPacket();
    }
Пример #10
0
    private Vector3Int[] Trace(Dictionary <Vector3Int, Vector3Int> cameFrom, Vector3Int goal, out bool pathBrocken)
    {
        Vector3Int current = goal;

        pathBrocken = false;
        List <Vector3Int> path = new List <Vector3Int>();

        try {
            for (int i = 0; i < 10000; i++)
            {
                if (cameFrom.ContainsKey(current))
                {
                    current = cameFrom[current];
                    if (!path.Contains(current))
                    {
                        path.Add(current);
                    }
                    if (current == start)
                    {
                        break;
                    }
                }
                else
                {
                    SafeDebug.Log("Path brocken");
                    pathBrocken = true;
                    break;
                }
            }
            path.Reverse();
        }
        catch (Exception e) {
            SafeDebug.LogException(e);
            SafeDebug.LogError("length: " + path.Count);
            throw new Exception(":(");
        }
        return(path.ToArray());
    }
Пример #11
0
    public MeshData Render(bool renderOnly)
    {
        List <Vector3> vertices  = new List <Vector3>();
        List <int>     triangles = new List <int>();
        List <Vector3> Normals   = new List <Vector3>();

        if (empty)
        {
            SafeDebug.Log("Chunk is empty... skipping.");
            return(new MeshData(vertices.ToArray(), triangles.ToArray(), null, Normals.ToArray()));
        }

        if (Initialized)
        {
            SetSurroundingChunks();
            int cx = location.x;
            int cy = location.y;
            int cz = location.z;

            int xStart = cx * ChunkSizeX;
            int xEnd   = cx * ChunkSizeX + ChunkSizeX;

            int yStart = cy * ChunkSizeY;
            int yEnd   = cy * ChunkSizeY + ChunkSizeY;

            int zStart = cz * ChunkSizeZ;
            int zEnd   = cz * ChunkSizeZ + ChunkSizeZ;

            int x = 0;
            int y = 0;
            int z = 0;

            int globalLocX = 0;
            int globalLocY = 0;
            int globalLocZ = 0;
            //try
            //{
            watch.Reset();
            watch.Start();
            //
            for (globalLocY = yStart, y = 0; y < ChunkSizeY; globalLocY++, y++)
            {
                if (globalLocY < Sampler.GetMin() - 20 || globalLocY > Sampler.GetMax())
                {
                    continue;
                }

                for (globalLocZ = zStart, z = 0; z < ChunkSizeZ; globalLocZ++, z++)
                {
                    for (globalLocX = xStart, x = 0; x < ChunkSizeX; globalLocX++, x++)
                    {
                        if (deactivated)
                        {
                            break;
                        }

                        Vector3     worldPos  = new Vector3(x * xSideLength, y * ySideLength, z * zSideLength);
                        Vector3Int  globalPos = new Vector3Int(globalLocX * skipDist, globalLocY * skipDist, globalLocZ * skipDist);
                        Vector3Int  localPos  = new Vector3Int(x, y, z);
                        GridPoint[] grid      = new GridPoint[8];
                        //miscWatch.Start();
                        for (int i = 0; i < grid.Length; i++)
                        {
                            grid[i] = GetVector4(worldPos + locOffset[i], localPos + directionOffsets[i], globalPos + globalOffsets[i], !renderOnly);
                        }
                        //miscWatch.Stop();

                        RenderBlock(grid, 0, vertices, triangles, Normals, null);
                    }
                }
            }
            ResetPageNeighbors();
            watch.Stop();
            renderTime = watch.Elapsed.ToString();

            //Debug.Log("Chunk generated in: " + renderTime);

            miscTime = miscWatch.Elapsed.ToString();
            miscTime = globalLocZ.ToString();

            /*}
             * catch (Exception e)
             * {
             *  SafeDebug.LogError(e.GetType().ToString() + ": " + e.Message + "\n " + e.StackTrace);
             * }*/
        }
        return(new MeshData(vertices.ToArray(), triangles.ToArray(), null, Normals.ToArray()));
    }
Пример #12
0
    private void ListenLoop(Action finished)
    {
        Loom.QueueAsyncTask("Net_loop", () => {
            watch.Start();
            tcpClient = new TcpClient();
            tcpClient.Connect(Host, TcpPort);
            tcpSocket = tcpClient.Client;
            Run       = true;
            ManualResetEvent reset = new ManualResetEvent(false);
            timeOutWatch.Start();
            reset.WaitOne(10);
            finished();
            while (Run)
            {
                if (watch.Elapsed.Seconds >= 1)
                {
                    //BytesReceived_PS = _receivedBytes;
                    //PacketsReceived_PS = _received;
                    //BytesSent_PS = _sentBytes;
                    //PacketsSent_PS = _sent;

                    //_receivedBytes = 0;
                    //_received = 0;
                    //_sentBytes = 0;
                    //_sent = 0;

                    watch.Reset();
                    watch.Start();
                }

                if (timeOutWatch.ElapsedMilliseconds >= 10000)
                {
                    SafeDebug.Log("Connection timed out!");
                    Loom.QueueOnMainThread(() => Close("TCP Time Out."));
                    break;
                }

                if (tcpSocket.Available >= 1)
                {
                    byte[] lengthBuff = new byte[2];
                    tcpSocket.Receive(lengthBuff, 0, 2, SocketFlags.None);
                    int bufferLength     = BitConverter.ToUInt16(lengthBuff, 0) - 2;
                    byte[] dataBuff      = new byte[bufferLength];
                    List <byte> listBuff = new List <byte>();
                    int bytesNeeded      = bufferLength;
                    while (bytesNeeded > 0)
                    {
                        byte[] partialReceiveBuff = new byte[65536];
                        int rx             = tcpSocket.Receive(partialReceiveBuff, 0, bytesNeeded, SocketFlags.None);
                        byte[] partialBuff = new byte[rx];
                        Array.Copy(partialReceiveBuff, 0, partialBuff, 0, rx);
                        listBuff.AddRange(partialBuff);
                        bytesNeeded -= rx;
                    }
                    dataBuff = listBuff.ToArray();
                    timeOutWatch.Reset();
                    timeOutWatch.Start();
                    Connected = true;
                    Process(dataBuff, Protocal.Tcp);
                }
                reset.WaitOne(1);
            }
        });
    }
Пример #13
0
    private void RenderSides(BlockType _type, byte[] _sides, Vector3Int _GlobalPosition, Vector3Int _localPosition, ref List <Vector3> _vertices, ref List <int> _triangles, ref List <Vector2> _uv, float _sideLength, int[] _textureIndex)
    {
        if (_type.baseType == BaseType.solid)
        {
            int x = _localPosition.x;
            int y = _localPosition.y;
            int z = _localPosition.z;
            try {
                // y+
                if (BlockTypes[_sides[0]].baseType == BaseType.air)
                {
                    int vertexIndex = _vertices.Count;
                    _vertices.Add(new Vector3(x * _sideLength, y * _sideLength + _sideLength, z * _sideLength));
                    _vertices.Add(new Vector3(x * _sideLength, y * _sideLength + _sideLength, z * _sideLength + _sideLength));

                    _vertices.Add(new Vector3(x * _sideLength + _sideLength, y * _sideLength + _sideLength, z * _sideLength + _sideLength));
                    _vertices.Add(new Vector3(x * _sideLength + _sideLength, y * _sideLength + _sideLength, z * _sideLength));

                    _triangles.Add(vertexIndex);
                    _triangles.Add(vertexIndex + 1);
                    _triangles.Add(vertexIndex + 2);

                    _triangles.Add(vertexIndex + 2);
                    _triangles.Add(vertexIndex + 3);
                    _triangles.Add(vertexIndex);

                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[0]].x, AtlasUvs[_textureIndex[0]].y));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[0]].x + AtlasUvs[_textureIndex[0]].width, AtlasUvs[_textureIndex[0]].y));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[0]].x + AtlasUvs[_textureIndex[0]].width, AtlasUvs[_textureIndex[0]].y + AtlasUvs[_textureIndex[0]].height));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[0]].x, AtlasUvs[_textureIndex[0]].y + AtlasUvs[_textureIndex[0]].height));
                }

                // Y-
                if (BlockTypes[_sides[1]].baseType == BaseType.air)
                {
                    int vertexIndex = _vertices.Count;
                    _vertices.Add(new Vector3(x * _sideLength, y * _sideLength, z * _sideLength));
                    _vertices.Add(new Vector3(x * _sideLength, y * _sideLength, z * _sideLength + _sideLength));

                    _vertices.Add(new Vector3(x * _sideLength + _sideLength, y * _sideLength, z * _sideLength + _sideLength));
                    _vertices.Add(new Vector3(x * _sideLength + _sideLength, y * _sideLength, z * _sideLength));

                    _triangles.Add(vertexIndex);
                    _triangles.Add(vertexIndex + 2);
                    _triangles.Add(vertexIndex + 1);

                    _triangles.Add(vertexIndex + 3);
                    _triangles.Add(vertexIndex + 2);
                    _triangles.Add(vertexIndex);

                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[1]].x, AtlasUvs[_textureIndex[1]].y));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[1]].x + AtlasUvs[_textureIndex[1]].width, AtlasUvs[_textureIndex[1]].y));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[1]].x + AtlasUvs[_textureIndex[1]].width, AtlasUvs[_textureIndex[1]].y + AtlasUvs[_textureIndex[1]].height));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[1]].x, AtlasUvs[_textureIndex[1]].y + AtlasUvs[_textureIndex[1]].height));
                }

                // Z+
                if (BlockTypes[_sides[2]].baseType == BaseType.air)
                {
                    int vertexIndex = _vertices.Count;
                    _vertices.Add(new Vector3(x * _sideLength, y * _sideLength, z * _sideLength + _sideLength));
                    _vertices.Add(new Vector3(x * _sideLength + _sideLength, y * _sideLength, z * _sideLength + _sideLength));

                    _vertices.Add(new Vector3(x * _sideLength + _sideLength, y * _sideLength + _sideLength, z * _sideLength + _sideLength));
                    _vertices.Add(new Vector3(x * _sideLength, y * _sideLength + _sideLength, z * _sideLength + _sideLength));

                    _triangles.Add(vertexIndex);
                    _triangles.Add(vertexIndex + 1);
                    _triangles.Add(vertexIndex + 2);

                    _triangles.Add(vertexIndex + 2);
                    _triangles.Add(vertexIndex + 3);
                    _triangles.Add(vertexIndex);

                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[2]].x, AtlasUvs[_textureIndex[2]].y));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[2]].x + AtlasUvs[_textureIndex[2]].width, AtlasUvs[_textureIndex[2]].y));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[2]].x + AtlasUvs[_textureIndex[2]].width, AtlasUvs[_textureIndex[2]].y + AtlasUvs[_textureIndex[2]].height));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[2]].x, AtlasUvs[_textureIndex[2]].y + AtlasUvs[_textureIndex[2]].height));
                }

                // Z-
                if (BlockTypes[_sides[3]].baseType == BaseType.air)
                {
                    int vertexIndex = _vertices.Count;
                    _vertices.Add(new Vector3(x * _sideLength, y * _sideLength, z * _sideLength));
                    _vertices.Add(new Vector3(x * _sideLength + _sideLength, y * _sideLength, z * _sideLength));

                    _vertices.Add(new Vector3(x * _sideLength + _sideLength, y * _sideLength + _sideLength, z * _sideLength));
                    _vertices.Add(new Vector3(x * _sideLength, y * _sideLength + _sideLength, z * _sideLength));

                    _triangles.Add(vertexIndex);
                    _triangles.Add(vertexIndex + 2);
                    _triangles.Add(vertexIndex + 1);

                    _triangles.Add(vertexIndex + 3);
                    _triangles.Add(vertexIndex + 2);
                    _triangles.Add(vertexIndex);

                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[3]].x, AtlasUvs[_textureIndex[3]].y));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[3]].x + AtlasUvs[_textureIndex[3]].width, AtlasUvs[_textureIndex[3]].y));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[3]].x + AtlasUvs[_textureIndex[3]].width, AtlasUvs[_textureIndex[3]].y + AtlasUvs[_textureIndex[3]].height));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[3]].x, AtlasUvs[_textureIndex[3]].y + AtlasUvs[_textureIndex[3]].height));
                }

                // X+
                if (BlockTypes[_sides[4]].baseType == BaseType.air)
                {
                    int vertexIndex = _vertices.Count;
                    _vertices.Add(new Vector3(x * _sideLength + _sideLength, y * _sideLength, z * _sideLength));
                    _vertices.Add(new Vector3(x * _sideLength + _sideLength, y * _sideLength + _sideLength, z * _sideLength));

                    _vertices.Add(new Vector3(x * _sideLength + _sideLength, y * _sideLength + _sideLength, z * _sideLength + _sideLength));
                    _vertices.Add(new Vector3(x * _sideLength + _sideLength, y * _sideLength, z * _sideLength + _sideLength));

                    _triangles.Add(vertexIndex);
                    _triangles.Add(vertexIndex + 1);
                    _triangles.Add(vertexIndex + 2);

                    _triangles.Add(vertexIndex + 2);
                    _triangles.Add(vertexIndex + 3);
                    _triangles.Add(vertexIndex);

                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[4]].x, AtlasUvs[_textureIndex[4]].y));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[4]].x + AtlasUvs[_textureIndex[4]].width, AtlasUvs[_textureIndex[4]].y));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[4]].x + AtlasUvs[_textureIndex[4]].width, AtlasUvs[_textureIndex[4]].y + AtlasUvs[_textureIndex[4]].height));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[4]].x, AtlasUvs[_textureIndex[4]].y + AtlasUvs[_textureIndex[4]].height));
                }

                // X-
                if (BlockTypes[_sides[5]].baseType == BaseType.air)
                {
                    int vertexIndex = _vertices.Count;
                    _vertices.Add(new Vector3(x * _sideLength, y * _sideLength, z * _sideLength));
                    _vertices.Add(new Vector3(x * _sideLength, y * _sideLength + _sideLength, z * _sideLength));

                    _vertices.Add(new Vector3(x * _sideLength, y * _sideLength + _sideLength, z * _sideLength + _sideLength));
                    _vertices.Add(new Vector3(x * _sideLength, y * _sideLength, z * _sideLength + _sideLength));

                    _triangles.Add(vertexIndex);
                    _triangles.Add(vertexIndex + 2);
                    _triangles.Add(vertexIndex + 1);

                    _triangles.Add(vertexIndex + 3);
                    _triangles.Add(vertexIndex + 2);
                    _triangles.Add(vertexIndex);

                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[5]].x, AtlasUvs[_textureIndex[5]].y));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[5]].x + AtlasUvs[_textureIndex[5]].width, AtlasUvs[_textureIndex[5]].y));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[5]].x + AtlasUvs[_textureIndex[5]].width, AtlasUvs[_textureIndex[5]].y + AtlasUvs[_textureIndex[5]].height));
                    _uv.Add(new Vector2(AtlasUvs[_textureIndex[5]].x, AtlasUvs[_textureIndex[5]].y + AtlasUvs[_textureIndex[5]].height));
                }
            }
            catch (Exception e) {
                SafeDebug.LogError(string.Format("Message: {0}, Info:", e.Message), e);
                string _UVs = string.Empty;
                for (int i = 0; i < _textureIndex.Length; i++)
                {
                    _UVs += _textureIndex[i] + " ";
                }
                SafeDebug.Log("texture indexes: " + _UVs);
                SafeDebug.Log("Blocktype: " + _type.ToString());
                if (AtlasUvs == null)
                {
                    SafeDebug.Log("AtlasUvs is null!");
                }
            }
        }
    }