Esempio n. 1
0
        public byte GetLight(int x, int y, int z)
        {
            Vector3i chunkPos = OCChunk.ToChunkPosition(x, y, z);
            Vector3i localPos = OCChunk.ToLocalPosition(x, y, z);

            return(GetLight(chunkPos, localPos, y));
        }
Esempio n. 2
0
        public static void SetLightDirty(OCMap map, Vector3i pos)
        {
            Vector3i chunkPos = OCChunk.ToChunkPosition(pos);
            Vector3i localPos = OCChunk.ToLocalPosition(pos);

            SetChunkLightDirty(map, chunkPos);

            if (localPos.x == 0)
            {
                SetChunkLightDirty(map, chunkPos - Vector3i.right);
            }
            if (localPos.y == 0)
            {
                SetChunkLightDirty(map, chunkPos - Vector3i.up);
            }
            if (localPos.z == 0)
            {
                SetChunkLightDirty(map, chunkPos - Vector3i.forward);
            }

            if (localPos.x == OCChunk.SIZE_X - 1)
            {
                SetChunkLightDirty(map, chunkPos + Vector3i.right);
            }
            if (localPos.y == OCChunk.SIZE_Y - 1)
            {
                SetChunkLightDirty(map, chunkPos + Vector3i.up);
            }
            if (localPos.z == OCChunk.SIZE_Z - 1)
            {
                SetChunkLightDirty(map, chunkPos + Vector3i.forward);
            }
        }
Esempio n. 3
0
    public void Set(I val, int x, int y, int z)
    {
        Vector3i    chunkPos = OCChunk.ToChunkPosition(x, y, z);
        Vector3i    localPos = OCChunk.ToLocalPosition(x, y, z);
        Chunk3D <I> chunk    = GetChunkInstance(chunkPos);

        chunk.Set(val, localPos);
    }
Esempio n. 4
0
    public I Get(int x, int y, int z)
    {
        Vector3i    chunkPos = OCChunk.ToChunkPosition(x, y, z);
        Vector3i    localPos = OCChunk.ToLocalPosition(x, y, z);
        Chunk3D <I> chunk    = GetChunk(chunkPos);

        if (chunk != null)
        {
            return(chunk.Get(localPos));
        }
        return(defaultValue);
    }
Esempio n. 5
0
        public bool SetMaxLight(byte light, int x, int y, int z)
        {
            Vector3i       chunkPos = OCChunk.ToChunkPosition(x, y, z);
            Vector3i       localPos = OCChunk.ToLocalPosition(x, y, z);
            Chunk3D <byte> chunk    = lights.GetChunkInstance(chunkPos);
            byte           oldLight = chunk.Get(localPos);

            if (oldLight < light)
            {
                chunk.Set(light, localPos);
                return(true);
            }
            return(false);
        }
Esempio n. 6
0
        private static void SetChunkLightDirty(OCMap map, Vector3i chunkPos)
        {
            OCChunk chunkData = map.GetChunk(chunkPos);

            if (chunkData == null)
            {
                return;
            }
            OCChunkRenderer chunk = chunkData.GetChunkRenderer();

            if (chunk == null)
            {
                return;
            }
            chunk.SetLightDirty();
        }
        private static void Scatter(OCMap map, OCColumnMap columnMap, List <Vector3i> list)          // рассеивание
        {
            OCSunLightMap lightmap = map.GetSunLightmap();

            for (int i = 0; i < list.Count; i++)
            {
                Vector3i pos = list[i];
                if (pos.y < 0)
                {
                    continue;
                }

                OCBlockData block = map.GetBlock(pos);
                int         light = lightmap.GetLight(pos) - OCLightComputerUtils.GetLightStep(block);
                if (light <= MIN_LIGHT)
                {
                    continue;
                }

                Vector3i chunkPos = OCChunk.ToChunkPosition(pos);
                if (columnMap != null && !columnMap.IsBuilt(chunkPos.x, chunkPos.z))
                {
                    continue;
                }

                foreach (Vector3i dir in Vector3i.directions)
                {
                    Vector3i nextPos = pos + dir;
                    block = map.GetBlock(nextPos);
                    if (block != null && block.IsAlpha() && lightmap.SetMaxLight((byte)light, nextPos))
                    {
                        list.Add(nextPos);
                    }
                    if (block != null && !block.IsEmpty())
                    {
                        OCLightComputerUtils.SetLightDirty(map, nextPos);
                    }
                }
            }
        }
    public void LoadLevel()
    {
        int verticalOffset = 85;

        Debug.Log("About to load level folder: " + _fullMapPath + ".");

        Substrate.AnvilWorld mcWorld = Substrate.AnvilWorld.Create(_fullMapPath);

        Substrate.AnvilRegionManager mcAnvilRegionManager = mcWorld.GetRegionManager();

        OpenCog.BlockSet.OCBlockSet blockSet = _map.GetBlockSet();

        //_map.GetSunLightmap().SetSunHeight(20, 4, 4);

        int createCount = 0;

        System.Collections.Generic.Dictionary <int, int> unmappedBlockTypes = new System.Collections.Generic.Dictionary <int, int>();

        //Debug.Log("In LoadLevel, there are " + blockSet.BlockCount + " blocks available.");

        foreach (Substrate.AnvilRegion mcAnvilRegion in mcAnvilRegionManager)
        {
            // Loop through x-axis of chunks in this region
            for (int iMCChunkX = 0; iMCChunkX < mcAnvilRegion.XDim; iMCChunkX++)
            {
                // Loop through z-axis of chunks in this region.
                for (int iMCChunkZ = 0; iMCChunkZ < mcAnvilRegion.ZDim; iMCChunkZ++)
                {
                    // Retrieve the chunk at the current position in our 2D loop...
                    Substrate.ChunkRef mcChunkRef = mcAnvilRegion.GetChunkRef(iMCChunkX, iMCChunkZ);

                    if (mcChunkRef != null)
                    {
                        if (mcChunkRef.IsTerrainPopulated)
                        {
                            // Ok...now to stick the blocks in...

                            int iMCChunkY = 0;

                            OCChunk chunk     = null;                //new OCChunk(_map, new Vector3i(iMCChunkX, iMCChunkY, iMCChunkZ));
                            OCChunk lastChunk = null;


                            Vector3i chunkPos     = new Vector3i(mcAnvilRegion.ChunkGlobalX(iMCChunkX), iMCChunkY + verticalOffset, mcAnvilRegion.ChunkGlobalZ(iMCChunkZ));
                            Vector3i lastChunkPos = Vector3i.zero;
                            chunk = _map.GetChunkInstance(chunkPos);

                            for (int iMCChunkInternalY = 0; iMCChunkInternalY < mcChunkRef.Blocks.YDim; iMCChunkInternalY++)
                            {
                                if (iMCChunkInternalY / OCChunk.SIZE_Y > iMCChunkY)
                                {
                                    lastChunk    = chunk;
                                    lastChunkPos = chunkPos;
                                    chunkPos     = new Vector3i(mcAnvilRegion.ChunkGlobalX(iMCChunkX), (iMCChunkInternalY + verticalOffset) / OCChunk.SIZE_Y, mcAnvilRegion.ChunkGlobalZ(iMCChunkZ));
                                    chunk        = _map.GetChunkInstance(chunkPos);
                                }

                                for (int iMCChunkInternalX = 0; iMCChunkInternalX < mcChunkRef.Blocks.XDim; iMCChunkInternalX++)
                                {
                                    for (int iMCChunkInternalZ = 0; iMCChunkInternalZ < mcChunkRef.Blocks.ZDim; iMCChunkInternalZ++)
                                    {
                                        int iBlockID = mcChunkRef.Blocks.GetID(iMCChunkInternalX, iMCChunkInternalY, iMCChunkInternalZ);

                                        if (iBlockID != 0)
                                        {
                                            Vector3i blockPos = new Vector3i(iMCChunkInternalX, iMCChunkInternalY % OCChunk.SIZE_Y, iMCChunkInternalZ);

                                            int ourBlockID = -1;

//											switch (iBlockID)
//											{
//											case 3: // Dirt to first grass
//												ourBlockID = 1;
//												break;
//											case 12: // Grass to grass
//												ourBlockID = 1;
//												break;
//											case 13: // Gravel to stone
//												ourBlockID = 4;
//												break;
//											case 1: // Stone to second stone
//												ourBlockID = 5;
//												break;
//											case 16: // Coal ore to fungus
//												ourBlockID = 17;
//												break;
//											case 15: // Iron ore to pumpkin
//												ourBlockID = 20;
//												break;
//											case 9: // Water to water
//												ourBlockID = 8;
//												//Debug.Log ("Creating some water at [" + blockPos.x + ", " + blockPos.y + ", " + blockPos.z + "]");
//												break;
////											case 2:
////												iBlockID = 16;
////												break;
////											case 4:
////												iBlockID = 16;
////												break;
////											case 18:
////												iBlockID = 16;
////												break;
//											default:
//											{
//												//Debug.Log ("Unmapped BlockID: " + iBlockID);
//
//												if (!unmappedBlockTypes.ContainsKey (iBlockID))
//												{
//													unmappedBlockTypes.Add (iBlockID, 1);
//												}
//												else
//												{
//													unmappedBlockTypes[iBlockID] += 1;
//												}
//
//												break;
//												}
//											}

                                            if (mcToOCBlockDictionary.ContainsKey(iBlockID))
                                            {
                                                ourBlockID = mcToOCBlockDictionary[iBlockID];
                                            }
                                            else
                                            {
                                                if (!unmappedBlockTypes.ContainsKey(iBlockID))
                                                {
                                                    unmappedBlockTypes.Add(iBlockID, 1);
                                                }
                                                else
                                                {
                                                    unmappedBlockTypes[iBlockID] += 1;
                                                }
                                            }

                                            if (ourBlockID != -1)
                                            {
                                                OCBlock newBlock = blockSet.GetBlock(ourBlockID);

                                                //OCBlockData block = new OpenCog.Map.OCBlockData(newBlock, blockPos);
                                                OCBlockData block = (OCBlockData)OCScriptableObject.CreateInstance <OCBlockData>();
                                                block.Init(newBlock, blockPos);

                                                chunk.SetBlock(block, blockPos);
                                                OpenCog.Map.Lighting.OCLightComputer.RecomputeLightAtPosition(_map, blockPos);

                                                if (block.block.GetName() == "Battery")
                                                {
                                                    GameObject batteryPrefab = OCMap.Instance.BatteryPrefab;
                                                    if (batteryPrefab == null)
                                                    {
                                                        UnityEngine.Debug.Log("OCBuilder::Update, batteryPrefab == null");
                                                    }
                                                    else
                                                    {
                                                        GameObject battery = (GameObject)GameObject.Instantiate(batteryPrefab);
                                                        battery.transform.position = blockPos;
                                                        battery.name             = "Battery";
                                                        battery.transform.parent = OCMap.Instance.BatteriesSceneObject.transform;
                                                    }
                                                }

                                                if (block.block.GetName() == "Hearth")
                                                {
                                                    GameObject hearthPrefab = OCMap.Instance.HearthPrefab;
                                                    if (hearthPrefab == null)
                                                    {
                                                        UnityEngine.Debug.Log("OCBuilder::Update, hearthPrefab == null");
                                                    }
                                                    else
                                                    {
                                                        GameObject hearth = (GameObject)GameObject.Instantiate(hearthPrefab);
                                                        hearth.transform.position = blockPos;
                                                        hearth.name             = "Hearth";
                                                        hearth.transform.parent = OCMap.Instance.HearthsSceneObject.transform;
                                                    }
                                                }

                                                createCount += 1;
                                            }
                                        }
                                    }                     // End for (int iMCChunkInternalZ = 0; iMCChunkInternalZ < mcChunkRef.Blocks.ZDim; iMCChunkInternalZ++)
                                }                         // End for (int iMCChunkInternalY = 0; iMCChunkInternalY < mcChunkRef.Blocks.YDim; iMCChunkInternalY++)

                                string chunkCoord = chunkPos.x + ", " + chunkPos.z;

                                if (!chunkList.ContainsKey(chunkCoord))
                                {
                                    chunkList.Add(chunkCoord, chunkPos);
                                }

                                if (iMCChunkY < iMCChunkInternalY / OCChunk.SIZE_Y)
                                {
                                    _map.Chunks.AddOrReplace(lastChunk, lastChunkPos);
                                    _map.UpdateChunkLimits(lastChunkPos);
                                    _map.SetDirty(lastChunkPos);
                                    iMCChunkY = iMCChunkInternalY / OCChunk.SIZE_Y;
                                }
                            } // End for (int iMCChunkInternalX = 0; iMCChunkInternalX < mcChunkRef.Blocks.XDim; iMCChunkInternalX++)
                        }     // End if (mcChunkRef.IsTerrainPopulated)
                    }         // End if (mcChunkRef != null)
                }             // End for (int iMCChunkZ = 0; iMCChunkZ < mcAnvilRegion.ZDim; iMCChunkZ++)
            }                 // End for (int iMCChunkX  = 0; iMCChunkX < mcAnvilRegion.XDim; iMCChunkX++)
        } // End foreach( Substrate.AnvilRegion mcAnvilRegion in mcAnvilRegionManager )

        foreach (Vector3i chunkToLight in chunkList.Values)
        {
            OpenCog.Map.Lighting.OCChunkSunLightComputer.ComputeRays(_map, chunkToLight.x, chunkToLight.z);
            OpenCog.Map.Lighting.OCChunkSunLightComputer.Scatter(_map, null, chunkToLight.x, chunkToLight.z);
        }

        foreach (System.Collections.Generic.KeyValuePair <int, int> unmappedBlockData in unmappedBlockTypes)
        {
            UnityEngine.Debug.Log("Unmapped BlockID '" + unmappedBlockData.Key + "' found " + unmappedBlockData.Value + " times.");
        }

        Debug.Log("Loaded level: " + _fullMapPath + ", created " + createCount + " blocks.");

        _map.AddColliders();
    } // End public void LoadLevel()
        //---------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------

        #region Private Member Functions

        //---------------------------------------------------------------------------

        private void FindGoalBlockPositionInChunks(List3D <OCChunk> chunks)
        {
            Vector3 sourcePos   = gameObject.transform.position;
            Vector3 distanceVec = ((Vector3)GoalBlockPos) - sourcePos;

//		//		if(distanceVec.y < -1.0f + 0.5f && distanceVec.y > -1.0f - 0.5f)
//		if(distanceVec.sqrMagnitude < 2.25f)
//		{
//			Debug.Log("We've arrived at our goal TNT block...");
//			map.SetBlockAndRecompute(new OCBlockData(), TargetBlockPos);
//			TargetBlockPos = Vector3i.zero;
//
//			OCAction[] actions = gameObject.GetComponentsInChildren<OCAction>();
//
//			foreach(OCAction action in actions)
//			{
//				action.EndTarget.transform.position = Vector3.zero;
//				action.StartTarget.transform.position = Vector3.zero;
//			}
//		}

            bool doesGoalExist = false;

            //distanceVec = new Vector3(1000,1000,1000);
            for (int cx = chunks.GetMinX(); cx < chunks.GetMaxX(); ++cx)
            {
                for (int cy = chunks.GetMinY(); cy < chunks.GetMaxY(); ++cy)
                {
                    for (int cz = chunks.GetMinZ(); cz < chunks.GetMaxZ(); ++cz)
                    {
                        Vector3i chunkPos = new Vector3i(cx, cy, cz);
                        OCChunk  chunk    = chunks.SafeGet(chunkPos);
                        if (chunk != null)
                        {
                            for (int z = 0; z < OCChunk.SIZE_Z; z++)
                            {
                                for (int x = 0; x < OCChunk.SIZE_X; x++)
                                {
                                    for (int y = 0; y < OCChunk.SIZE_Y; y++)
                                    {
                                        Vector3i    localPos     = new Vector3i(x, y, z);
                                        OCBlockData blockData    = chunk.GetBlock(localPos);
                                        Vector3i    candidatePos = OCChunk.ToWorldPosition(chunk.GetPosition(), localPos);
                                        Vector3     candidateVec = ((Vector3)candidatePos) - sourcePos;
                                        if (!blockData.IsEmpty() && blockData.block.GetName() == _goalBlockType.GetName())
                                        {
                                            doesGoalExist = true;
                                            if (candidateVec.sqrMagnitude < distanceVec.sqrMagnitude)
                                            {
                                                GoalBlockPos = candidatePos;
                                                distanceVec  = candidateVec;

                                                if (_ShouldMoveTargets)
                                                {
                                                    OCAction[] actions = gameObject.GetComponentsInChildren <OCAction>();

                                                    foreach (OCAction action in actions)
                                                    {
                                                        action.EndTarget.transform.position   = new Vector3(GoalBlockPos.x, GoalBlockPos.y, GoalBlockPos.z);
                                                        action.StartTarget.transform.position = gameObject.transform.position;
                                                    }
                                                }

                                                Debug.Log("We found some " + _goalBlockType.GetName() + " nearby: " + GoalBlockPos + "!");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (GoalBlockPos != Vector3i.zero && (!doesGoalExist || _map.GetBlock(GoalBlockPos).IsEmpty()))
            {
                Debug.Log("No more " + _goalBlockType.GetName() + "... :(");

                GoalBlockPos = Vector3i.zero;

                OCAction[] actions = gameObject.GetComponentsInChildren <OCAction>();

                foreach (OCAction action in actions)
                {
                    action.EndTarget.transform.position   = Vector3.zero;
                    action.StartTarget.transform.position = Vector3.zero;
                }
            }
        }