Ejemplo n.º 1
0
 void RegisterMeshNode(IntVector4 node)
 {
     if (!mMeshNodes.Contains(node))
     {
         mMeshNodes.Add(node);
     }
 }
Ejemplo n.º 2
0
 public static void WriteVector4(System.IO.BinaryWriter w, IntVector4 v)
 {
     w.Write(v.x);
     w.Write(v.y);
     w.Write(v.z);
     w.Write(v.w);
 }
Ejemplo n.º 3
0
        public ILayerMasked Do(Vector2 size)
        {
            //int maxDim = (int)JryMath.Max(size.X, size.Y);
//CONSTANT - There is a problem with values that are not 2^n wher n is element of N

            var            maxDim        = 256;
            int            gradSize      = 8;
            IInterpolation interpolation = new LinearClipped();

            Layer2DObject map   = new Layer2DObject(size);
            int           hgrid = (int)size.X;
            int           vgrid = (int)size.Y;


            float gain       = _noiseParameters.Amplitude;
            float lacunarity = _noiseParameters.Lacunarity;

            var gradients = setupGradient(gradSize);

            //set up the random numbers table
            int[] permutations = getPermutaions(maxDim);

            int maxDimMinOne   = maxDim - 1;
            int gradSizeMinOne = gradSize - 1;

            for (int i = 0; i < vgrid; i++)
            {
                for (int j = 0; j < hgrid; j++)
                {
                    float pixel_value = 0.0f;

                    float amplitude = 1.0f;
                    float frequency = 1.0f / maxDim;

                    for (int k = _noiseParameters.FromDepth; k < _noiseParameters.ToDepth; k++)
                    {
                        int x = JryMath.Floor(j * frequency);
                        int y = JryMath.Floor(i * frequency);

                        float fracX = j * frequency - x;
                        float fracY = i * frequency - y;

                        // following two lines solved the bug.
                        x += k;
                        y += k;
                        IntVector4 v              = getIndices(permutations, maxDimMinOne, gradSizeMinOne, x, y);
                        Vector2[]  grads          = getGrads(gradients, v);
                        float      interpolatedxy = biInterpolate(interpolation, grads, fracX, fracY);

                        pixel_value += interpolatedxy * amplitude;
                        amplitude   *= gain;
                        frequency   *= lacunarity;
                    }

                    //put it in the map
                    map[j, i] = pixel_value;
                }
            }
            return(map);
        }
Ejemplo n.º 4
0
    // this function should only be used by the root node.
    public Block45OctNode RerootToContainPos(IntVector4 pos)
    {
        if (!Covers(pos))
        {
            // make a new node that can cover atpos and pos is ALIGNED with LOD Node width
            // B45Node width should be equal to or smaller than LODNode width
            int maskX, maskY, maskZ;
            if (_pos.w < Block45Constants.MAX_LOD)
            {
                int posMask = (1 << (Block45Constants._scaledShift + _pos.w + 1)) - 1;
                maskX = ((_pos.x & posMask) != 0 && pos.x < _pos.x) ? 1 : 0;
                maskY = ((_pos.y & posMask) != 0 && pos.y < _pos.y) ? 1 : 0;
                maskZ = ((_pos.z & posMask) != 0 && pos.z < _pos.z) ? 1 : 0;
            }
            else
            {
                maskX = (pos.x < _pos.x) ? 1 : 0;
                maskY = (pos.y < _pos.y) ? 1 : 0;
                maskZ = (pos.z < _pos.z) ? 1 : 0;
            }
            int        thisOctant = maskX + (maskY << 1) + (maskZ << 2);        // righttop to leftbottom
            IntVector4 newRootPos = new IntVector4(_pos.x - maskX * ScaledSize,
                                                   _pos.y - maskY * ScaledSize,
                                                   _pos.z - maskZ * ScaledSize,
                                                   _pos.w + 1);
            Block45OctNode newRoot = Block45OctNode.CreateNode(newRootPos, OnCreateNode);
            this._parent = newRoot;
            newRoot.Split(thisOctant, this);

            return(newRoot.RerootToContainPos(pos));
        }
        return(this);
    }
Ejemplo n.º 5
0
    public void AddRequest(VFVoxelChunkData chunkData)
    {
        IntVector4 chunkPos = chunkData.ChunkPosLod;

        // Req to chunks out of range will return, these chunks will be null and can not be write according to WriteVoxelAtIdx's code
        if (chunkPos.x < 0 || chunkPos.x >= VoxelTerrainConstants._worldMaxCX ||
            chunkPos.y < 0 || chunkPos.y >= VoxelTerrainConstants.WorldMaxCY(chunkPos.w) ||
            chunkPos.z < 0 || chunkPos.z >= VoxelTerrainConstants._worldMaxCZ)
        {
            chunkData.OnDataLoaded(VFVoxelChunkData.S_ChunkDataAir);
            return;
        }

        if (_bImmMode)          // no caching
        {
            int px, py, pz;
            VFFileUtil.WorldChunkPosToPiecePos(chunkPos, out px, out py, out pz);
            ReadPieceDataToBuff(px, py, pz, chunkPos.w);
            _buff.Decompress(px, py, pz, chunkPos.w);
            _buff.SetChunkData(chunkData, _chunkDataProc);
        }
        else
        {
            ReqValue reqValue;
            if (!_chunkReqList.TryGetValue(chunkData, out reqValue) || !chunkData.IsStampIdentical(reqValue.stamp))
            {
                VFFileUtil.WorldChunkPosToPiecePos(chunkPos, out reqValue.px, out reqValue.py, out reqValue.pz);
                reqValue.pw              = chunkPos.w;
                reqValue.stamp           = chunkData.StampOfUpdating;
                _chunkReqList[chunkData] = reqValue;
            }
        }
    }
Ejemplo n.º 6
0
    public VFVoxelChunkData readChunk(int cx, int cy, int cz, int lod = 0)
    {
        int chunkNumX = LODOctreeMan._xChunkCount >> lod;
        int chunkNumY = LODOctreeMan._yChunkCount >> lod;
        int chunkNumZ = LODOctreeMan._zChunkCount >> lod;
        int cxround   = (cx >> lod) % chunkNumX;
        int cyround   = (cy >> lod) % chunkNumY;
        int czround   = (cz >> lod) % chunkNumZ;

        if (cxround < 0)
        {
            cxround += chunkNumX;
        }
        if (cyround < 0)
        {
            cyround += chunkNumY;
        }
        if (czround < 0)
        {
            czround += chunkNumZ;
        }
        VFVoxelChunkData chunkData = (VFVoxelChunkData)_lodNodes[lod][cxround, cyround, czround]._data[_idxData];
        IntVector4       poslod    = chunkData.ChunkPosLod;

        if (poslod.x != cx || poslod.y != cy || poslod.z != cz || poslod.w != lod)
        {
            return(null);
        }
        return(chunkData);
    }
Ejemplo n.º 7
0
    public bool IsPosValid(IntVector4 pos)
    {
        if (pos.w != 0)
        {
            return(false);
        }
        // lod 0
        LODOctreeNode node = GetNodeWithCPos(pos.x >> VoxelTerrainConstants._shift,
                                             pos.y >> VoxelTerrainConstants._shift,
                                             pos.z >> VoxelTerrainConstants._shift,
                                             pos.w);

        if (node == null)
        {
            return(false);
        }

        int n = node._data.Length;

        for (int i = 0; i < n; i++)
        {
            //TODO : code
            //if(!node._data[i]..IsReady)		return false;
        }
        return(true);
    }
Ejemplo n.º 8
0
 void OnTerrainColliderCreated(IntVector4 node)
 {
     if (Match(node))
     {
         OnTerrainEnter(node);
     }
 }
Ejemplo n.º 9
0
 void OnTerrainColliderDestroy(IntVector4 node)
 {
     if (Match(node))
     {
         OnTerrainExit(node);
     }
 }
Ejemplo n.º 10
0
 void OnTerrainPositionDetory(IntVector4 node)
 {
     if (node.w == LODOctreeMan._maxLod)
     {
         RemoveSPTerrainRect(node);
     }
 }
Ejemplo n.º 11
0
    void OnTerrainMeshCreated(IntVector4 node)
    {
        if (CSMain.s_MgCreator.Assembly == null)
        {
            return;
        }

        if (!VFVoxelTerrain.TerrainColliderComplete)
        {
            return;
        }

        if (node.w == 0 && automaticData != null)
        {
            float dx = CSMain.s_MgCreator.Assembly.Position.x - node.x;
            float dy = CSMain.s_MgCreator.Assembly.Position.y - node.y;
            float dz = CSMain.s_MgCreator.Assembly.Position.z - node.z;

            float side = VoxelTerrainConstants._numVoxelsPerAxis << node.w;

            if (dx >= PETools.PEMath.Epsilon && dx <= side &&
                dy >= PETools.PEMath.Epsilon && dy <= side &&
                dz >= PETools.PEMath.Epsilon && dz <= side)
            {
                //To do : destroy block
                foreach (KeyValuePair <int, CSCommon> kp in CSMain.s_MgCreator.GetCommonEntities())
                {
                    if (Random.value < 0.5f)
                    {
                        DigTerrainManager.DestroyTerrainInRange(1, kp.Value.Position, 255.0f, 10.0f);
                    }
                }
            }
        }
    }
Ejemplo n.º 12
0
 void OnTerrainMeshDestroy(IntVector4 node)
 {
     if (node.w == 0)
     {
         RemoveMeshNode(node);
     }
 }
Ejemplo n.º 13
0
    void OnTerrainMeshCreated(IntVector4 node)
    {
        if (node.w == 0)
        {
            IntVector2 mark = AiUtil.ConvertToIntVector2FormLodLevel(node, VoxelTerrainMaxLod);
            if (pointTable.ContainsKey(mark))
            {
                SPTerrainRect tRect = pointTable[mark];
                if (tRect != null)
                {
                    List <SPPoint> points = tRect.points;
                    points = points.FindAll(ret => Match(ret, node.x, node.z));

                    foreach (SPPoint point in points)
                    {
                        point.AttachEvent(node);
                        //point.position = new Vector3(point.position.x, node.y, point.position.z);
                    }
                }
            }

            RegisterMeshNode(node);

            LODOctreeMan.self.AttachNodeEvents(null, null, null, null, OnTerrainColliderCreated, node.ToVector3(), 0);
        }
    }
Ejemplo n.º 14
0
 void RemoveMeshNode(IntVector4 node)
 {
     if (mMeshNodes.Contains(node))
     {
         mMeshNodes.Remove(node);
     }
 }
Ejemplo n.º 15
0
 void SetupEnvironmentAudio(IntVector4 node)
 {
     if (Random.value < audioRate)
     {
         SetupAudioController(node);
     }
 }
Ejemplo n.º 16
0
 void OnColliderCreate(IntVector4 node)
 {
     if (m_Obj != null)
     {
         m_Obj.SetActive(true);
     }
 }
Ejemplo n.º 17
0
    void SetupAudioController(IntVector4 node)
    {
        Vector3 position = node.ToVector3();

        position += new Vector3(Random.Range(0.0f, VoxelTerrainConstants._numVoxelsPerAxis << node.w),
                                0.0f,
                                Random.Range(0.0f, VoxelTerrainConstants._numVoxelsPerAxis << node.w));

        int        height = VoxelTerrainConstants._numVoxelsPerAxis << node.w;
        RaycastHit hitInfo;

        if (Physics.Raycast(position + Vector3.up * height, Vector3.down, out hitInfo, height, GameConfig.GroundLayer))
        {
            float waterHeight;
            if (PETools.PEUtil.GetWaterSurfaceHeight(hitInfo.point, out waterHeight))
            {
                position = hitInfo.point + Vector3.up * Random.Range(0.0f, waterHeight - hitInfo.point.y);
            }
            else
            {
                position = hitInfo.point + Vector3.up * Random.Range(0.5f, 5.0f);
            }

            int             sid    = AISpawnDataStory.GetEnvMusicID(GetMapID(position));
            Transform       parent = mAudioPoint != null ? mAudioPoint.transform : null;
            AudioController ctrl   = AudioManager.instance.Create(position, sid, parent, false, false);
            envSounds.Add(ctrl);
        }
    }
Ejemplo n.º 18
0
 void OnColliderDestroy(IntVector4 node)
 {
     if (m_Obj != null)
     {
         m_Obj.SetActive(false);
     }
 }
Ejemplo n.º 19
0
 public GridLayout(string CellsArrangement, string Spaceoffsets, int spaceX, int spaceY)
 {
     Space            = new IntVector2(spaceX, spaceY);
     CellsArrangement = Arrangement;
     Offsets          = Get4(Spaceoffsets);
     RegenerateLayout();
 }
Ejemplo n.º 20
0
 void OnMeshDestroy(IntVector4 node)
 {
     if (m_Obj != null)
     {
         GameObject.Destroy(m_Obj);
     }
 }
Ejemplo n.º 21
0
 // Read & Write voxel
 public byte this[IntVector3 idx, IntVector4 cposlod]
 {
     get
     {
         return(m_Chunks[cposlod.x, cposlod.y, cposlod.z].ReadVoxelAtIdx(idx.x, idx.y, idx.z).Volume);
     }
 }
Ejemplo n.º 22
0
    public void GetOfs(IntVector4 piecePos, out int pieceDataOfs, out int pieceDataLen)
    {
#if USE_ZIPPED_DATA
        // TODO: optimization
        int ofsx = (piecePos.x % VoxelTerrainConstants._mapPieceCountXorZ) >> _lod;
        int ofsz = (piecePos.z % VoxelTerrainConstants._mapPieceCountXorZ) >> _lod;
        int ofsy = (piecePos.y) >> _lod;

        // X,Z,then Y ----> correspongding to data making
        int idx = (ofsx * (FSetDataCountXZ >> _lod) + ofsz) * (FSetDataCountY >> _lod) + ofsy;
        idx         *= 4;
        pieceDataOfs = ofsData[idx] + (ofsData[idx + 1] << 8) + (ofsData[idx + 2] << 16) + (ofsData[idx + 3] << 24);
        int fSetOfsDataLenLod = (VFFileDataClone.FSetDataCountXZ >> _lod) *
                                (VFFileDataClone.FSetDataCountXZ >> _lod) *
                                (VFFileDataClone.FSetDataCountY >> _lod) * 4;
        if (idx >= fSetOfsDataLenLod - 4)
        {
            pieceDataLen = len - pieceDataOfs;
        }
        else
        {
            pieceDataLen = ofsData[idx + 4] + (ofsData[idx + 5] << 8) + (ofsData[idx + 6] << 16) + (ofsData[idx + 7] << 24) - pieceDataOfs;
        }
#else
        pieceDataOfs = 0;
        pieceDataLen = len;
#endif
    }
Ejemplo n.º 23
0
    public static Block45OctNode GetNodeRW(IntVector4 poslod, ref Block45OctNode root)          // Write new if null
    {
        // Note: if req's w > root.w, root would be returned
        root = root.RerootToContainPos(poslod);
        int            ind         = 0;
        int            nodeCenterX = 0;
        int            nodeCenterY = 0;
        int            nodeCenterZ = 0;
        Block45OctNode cur         = root;

        while (cur._pos.w > poslod.w)
        {
            if (cur.IsLeaf)
            {
                cur.Split();
            }

            int centerOfs = Block45Constants.CenterOfs(cur._pos.w);
            nodeCenterX = cur._pos.x + centerOfs;
            nodeCenterY = cur._pos.y + centerOfs;
            nodeCenterZ = cur._pos.z + centerOfs;
            ind         = ((poslod.x >= nodeCenterX) ? 1 : 0) |
                          ((poslod.y >= nodeCenterY) ? 2 : 0) |
                          ((poslod.z >= nodeCenterZ) ? 4 : 0);
            cur = cur._children[ind];
        }
        return(cur);
    }
Ejemplo n.º 24
0
    public static void AddReadRequest(VFVoxelChunkData chunkData)
    {
        IntVector4 chunkPos = chunkData.ChunkPosLod;

        // Req to chunks out of range will return, these chunks will be null and can not be write according to WriteVoxelAtIdx's code
        if (chunkPos.x < 0 || chunkPos.x >= VoxelTerrainConstants._worldMaxCX)
        {
            return;
        }
        if (chunkPos.y < 0 || chunkPos.y >= VoxelTerrainConstants.WorldMaxCY(chunkPos.w))
        {
            return;
        }
        if (chunkPos.z < 0 || chunkPos.z >= VoxelTerrainConstants._worldMaxCZ)
        {
            return;
        }

        // no caching
        IntVector4 piecePos;

        VFFileDataClone.WorldChunkPosToPiecePos(chunkPos, out piecePos);

        IntVector4 fileIndex;

        VFFileDataClone.PiecePos2FileIndex(piecePos, out fileIndex);

        VFPieceDataClone pieceData = GetPieceDataSub(piecePos, GetFileSetSub(fileIndex));

        pieceData.Decompress();
        pieceData.SetChunkData(chunkData);
        pieceData._data = null;
        pieceData       = null;
    }
Ejemplo n.º 25
0
    public void ReplaceChunkDatas(List <IntVector4> newChunkPosList, ProcToMergeChunkAtPos mergeChunkDataAtPos)
    {
        Dictionary <IntVector4, Dictionary <IntVector4, List <IntVector4> > > file2piece2chunkList = new Dictionary <IntVector4, Dictionary <IntVector4, List <IntVector4> > >();
        int n = newChunkPosList.Count;
        int px, py, pz;
        int fx, fz;

        for (int i = 0; i < n; i++)
        {
            IntVector4 chunkPos = newChunkPosList[i];
            VFFileUtil.WorldChunkPosToPiecePos(chunkPos, out px, out py, out pz);
            VFFileUtil.PiecePos2FilePos(px, py, pz, chunkPos.w, out fx, out fz);

            IntVector4 filePos  = new IntVector4(fx, 0, fz, chunkPos.w);
            IntVector4 piecePos = new IntVector4(px, py, pz, chunkPos.w);
            if (!file2piece2chunkList.ContainsKey(filePos))
            {
                file2piece2chunkList.Add(filePos, new Dictionary <IntVector4, List <IntVector4> >());
            }
            if (!file2piece2chunkList[filePos].ContainsKey(piecePos))
            {
                file2piece2chunkList[filePos].Add(piecePos, new List <IntVector4>());
            }
            file2piece2chunkList[filePos][piecePos].Add(chunkPos);
        }
        List <IntVector4> fileIndexList = file2piece2chunkList.Keys.Cast <IntVector4>().ToList();
        int nFileIndexList = fileIndexList.Count;

        for (int i = 0; i < nFileIndexList; i++)
        {
            IntVector4 fileIndex = fileIndexList[i];
            ReplacePiecesInFile(file2piece2chunkList[fileIndex], mergeChunkDataAtPos, fileIndex);
        }
    }
Ejemplo n.º 26
0
    void OnTerrainColliderDestroy(IntVector4 node)
    {
        if (node.w == 0)
        {
            IntVector2 mark = new IntVector2(node.x, node.z);
            if (exists.Contains(mark))
            {
                List <AudioController> acList = envSounds.FindAll(ret => Match(ret, node));
                foreach (AudioController acCtrl in acList)
                {
                    envSounds.Remove(acCtrl);
                    acCtrl.Delete();
                }

                List <GameObject> envEffList = envEffects.FindAll(ret => MatchEffect(ret, node));

                foreach (GameObject ite in envEffList)
                {
                    envEffects.Remove(ite);
                    GameObject.Destroy(ite);
                }

                exists.Remove(mark);
            }
        }
    }
        private static int GetComponent(IntVector4 v, char component)
        {
            switch (component)
            {
            case '0':
                return(0);

            case '1':
                return(1);

            case 'x':
            case 'r':
                return(v.x);

            case 'y':
            case 'g':
                return(v.y);

            case 'z':
            case 'b':
                return(v.z);

            case 'w':
            case 'a':
                return(v.w);
            }
            throw new InvalidOperationException("Invalid component: " + component);
        }
Ejemplo n.º 28
0
 void SetupEnvironmentEffect(IntVector4 node)
 {
     if (Random.value < effectRate)
     {
         SetupEffect(node);
     }
 }
Ejemplo n.º 29
0
    public override void Init(IntVector4 idx, Transform parent = null,
                              int spid    = 0, int pathid       = 0, bool isActive    = true, bool revisePos = true,
                              bool isBoss = false, bool isErode = true, bool isDelete = true, SimplexNoise noise = null,
                              AssetReq.ReqFinishDelegate onSpawned = null, CommonInterface common = null)
    {
        base.Init(idx, parent, spid, pathid, isActive, revisePos, isBoss, isErode, isDelete, noise, onSpawned, common);

        if (pathid > 0)
        {
            AiAsset.AiDataBlock aiData = AiAsset.AiDataBlock.GetAIDataBase(pathid);
            if (aiData != null)
            {
                //mDamage = aiData.damageSimulate;
                mMaxHp = aiData.maxHpSimulate;
                mHp    = mMaxHp;
            }
        }

        if (spid > 0)
        {
            AISpawnPath path = AISpawnPath.GetSpawnPath(spid);
            {
                if (path != null)
                {
                    //mDamage = path.damage;
                    mMaxHp = path.maxHp;
                    mHp    = mMaxHp;
                }
            }
        }
    }
Ejemplo n.º 30
0
    public virtual void Init(IntVector4 idx,
                             Transform parent   = null,
                             int spid           = 0,
                             int pathid         = 0,
                             bool isActive      = true,
                             bool revisePos     = true,
                             bool isBoss        = false,
                             bool isErode       = true,
                             bool isDelete      = true,
                             SimplexNoise noise = null,
                             AssetReq.ReqFinishDelegate onSpawned = null,
                             CommonInterface common = null)
    {
        mIndex          = idx;
        mType           = PointType.PT_NULL;
        mSpID           = spid;
        mPathID         = pathid;
        mActive         = isActive;
        mIsBoss         = isBoss;
        mErode          = isErode;
        mDelete         = isDelete;
        mNoise          = noise;
        mNodes          = new List <IntVector4>();
        mRevisePosition = revisePos;
        mWaitForSpawned = false;
        mReqFinish      = onSpawned;
        //mCommon = common;

        AttachEventFromMesh();

        AttachCollider();

        RegisterPoint(this);
    }