public int GetMaxY(int x, int z) { Vector3i chunkPos = OCChunk.ToChunkPosition(x, 0, z); chunkPos.y = _chunks.GetMax().y; Vector3i localPos = OCChunk.ToLocalPosition(x, 0, z); for (; chunkPos.y >= 0; chunkPos.y--) { localPos.y = OCChunk.SIZE_Y - 1; for (; localPos.y >= 0; localPos.y--) { OCChunk chunk = _chunks.SafeGet(chunkPos); if (chunk == null) { break; } OCBlockData block = chunk.GetBlock(localPos); if (block != null && !block.IsEmpty()) { return(OCChunk.ToWorldPosition(chunkPos, localPos).y); } } } return(0); }
public OCBlockData GetBlock(int x, int y, int z) { OCChunk chunk = null; if (_lastRequestedChunk != null) { Vector3i requestedChunkPosition = OCChunk.ToChunkPosition(x, y, z); if (requestedChunkPosition.x == _lastRequestedChunk.GetPosition().x) { if (requestedChunkPosition.y == _lastRequestedChunk.GetPosition().y) { if (requestedChunkPosition.z == _lastRequestedChunk.GetPosition().z) { chunk = _lastRequestedChunk; } } } } if (chunk == null) { chunk = GetChunk(OCChunk.ToChunkPosition(x, y, z)); _lastRequestedChunk = chunk; } if (chunk == null) { return(OCBlockData.CreateInstance <OCBlockData>()); } return(chunk.GetBlock(OCChunk.ToLocalPosition(x, y, z))); }
public static Vector3?Intersection(OpenCog.Map.OCMap map, Ray ray, float distance) { Vector3 start = ray.origin; Vector3 end = ray.origin + ray.direction * distance; int startX = Mathf.RoundToInt(start.x); int startY = Mathf.RoundToInt(start.y); int startZ = Mathf.RoundToInt(start.z); int endX = Mathf.RoundToInt(end.x); int endY = Mathf.RoundToInt(end.y); int endZ = Mathf.RoundToInt(end.z); if (startX > endX) { int tmp = startX; startX = endX; endX = tmp; } if (startY > endY) { int tmp = startY; startY = endY; endY = tmp; } if (startZ > endZ) { int tmp = startZ; startZ = endZ; endZ = tmp; } float minDistance = distance; for (int z = startZ; z <= endZ; z++) { for (int y = startY; y <= endY; y++) { for (int x = startX; x <= endX; x++) { OpenCog.Map.OCBlockData block = map.GetBlock(x, y, z); if (block == null || block.IsEmpty() || block.IsFluid()) { continue; } float dis = BlockRayIntersection(new Vector3(x, y, z), ray); minDistance = Mathf.Min(minDistance, dis); } } } if (minDistance != distance) { return(ray.origin + ray.direction * minDistance); } return(null); }
public void SetBlock(OCBlockData block, int x, int y, int z) { OCChunk chunk = GetChunkInstance(OCChunk.ToChunkPosition(x, y, z)); if (chunk != null) { chunk.SetBlock(block, OCChunk.ToLocalPosition(x, y, z)); } }
public void Generate(int x, int y, int z) { OpenCog.Map.OCBlockData block = map.GetBlock(x, y - 1, z); if (block.IsEmpty() || !block.block.GetName().Equals("Dirt")) { return; } if (Random.Range(0f, 1f) > 0.2f) { return; } GenerateTree(x, y, z); }
public OCChunk(OCMap map, Vector3i position) { this.map = map; this.position = position; for (int x = blocks.GetMinX(); x < blocks.GetMaxX(); ++x) { for (int y = blocks.GetMinY(); y < blocks.GetMaxY(); ++y) { for (int z = blocks.GetMinZ(); z < blocks.GetMaxZ(); ++z) { Vector3i pos = new Vector3i(x, y, z); blocks.Set(OCBlockData.CreateInstance <OCBlockData>().Init(null, pos), pos); } } } }
private static Contact?GetClosestContact(OpenCog.Map.OCMap map, CharacterController controller) { Vector3 pos = controller.transform.position; int x1 = Mathf.FloorToInt(pos.x - controller.radius); int y1 = Mathf.FloorToInt(pos.y); int z1 = Mathf.FloorToInt(pos.z - controller.radius); int x2 = Mathf.CeilToInt(pos.x + controller.radius); int y2 = Mathf.CeilToInt(pos.y + controller.height); int z2 = Mathf.CeilToInt(pos.z + controller.radius); Contact?contact = null; for (int x = x1; x <= x2; x++) { for (int y = y1; y <= y2; y++) { for (int z = z1; z <= z2; z++) { OpenCog.Map.OCBlockData block = map.GetBlock(x, y, z); if (block.IsSolid()) { Contact?_newContact = BlockCharacterCollision.GetContactBlockCharacter(new Vector3i(x, y, z), pos, controller); if (_newContact.HasValue && _newContact.Value.delta.magnitude > float.Epsilon) { Contact newContact = _newContact.Value; if (!contact.HasValue || newContact.sqrDistance > contact.Value.sqrDistance) { contact = newContact; } } } } } } return(contact); // return null; }
public static void Build(Vector3i localPos, Vector3i worldPos, OpenCog.Map.OCMap map, OpenCog.Builder.OCMeshBuilder mesh, bool onlyLight) { OpenCog.Map.OCBlockData block = map.GetBlock(worldPos); OCCubeBlock cube = (OCCubeBlock)block.block; OpenCog.Map.OCBlockDirection direction = block.GetDirection(); for (int i = 0; i < 6; i++) { OCCubeBlock.CubeFace face = faces[i]; Vector3i dir = directions[i]; Vector3i nearPos = worldPos + dir; if (IsFaceVisible(map, nearPos)) { if (!onlyLight) { BuildFace(face, cube, direction, localPos, mesh); } BuildFaceLight(face, map, worldPos, mesh); } } }
public void SetBlock(OCBlockData block, Vector3i pos) { SetBlock(block, pos.x, pos.y, pos.z); }
public void SetBlock(OpenCog.BlockSet.BaseBlockSet.OCBlock block, int x, int y, int z) { SetBlock(OCBlockData.CreateInstance <OCBlockData>().Init(block, new Vector3i(x, y, z)), x, y, z); }
public void SetBlock(OpenCog.BlockSet.BaseBlockSet.OCBlock block, Vector3i pos) { SetBlock(OCBlockData.CreateInstance <OCBlockData>().Init(block, pos), pos); }
//--------------------------------------------------------------------------- #endregion //--------------------------------------------------------------------------- #region Public Member Functions //--------------------------------------------------------------------------- public void SetBlockAndRecompute(OCBlockData block, Vector3i pos) { //MethodInfo info = this.GetType().GetMember("SetBlockAndRecompute")[0] as MethodInfo; // TODO [BLOCKED]: uncomment when aspect stuff is in place? // object[] attributes = info.GetCustomAttributes(typeof(OCLogAspect), true); // OCLogAspect asp = null; // // if(attributes != null) // asp = attributes[0] as OCLogAspect; // // if(asp == null) // Debug.Log("No OCLog Aspect..."); // // asp.OnEntry(null); OCBlockData oldBlock = GetBlock(pos); SetBlock(block, pos); // Convert the global coordinate of the block to the chunk coordinates. Vector3i chunkPos = OCChunk.ToChunkPosition(pos); UpdateChunkLimits(chunkPos); // Convert the global coordinate of the block to the coordinate within the chunk. Vector3i localPos = OCChunk.ToLocalPosition(pos); SetDirty(chunkPos); // If on the lower boundary of a chunk...set the neighbouring chunk to dirty too. if (localPos.x == 0) { SetDirty(chunkPos - Vector3i.right); } if (localPos.y == 0) { SetDirty(chunkPos - Vector3i.up); } if (localPos.z == 0) { SetDirty(chunkPos - Vector3i.forward); } // If on the upper boundary of a chunk...set the neighbouring chunk to dirty too. if (localPos.x == OCChunk.SIZE_X - 1) { SetDirty(chunkPos + Vector3i.right); } if (localPos.y == OCChunk.SIZE_Y - 1) { SetDirty(chunkPos + Vector3i.up); } if (localPos.z == OCChunk.SIZE_Z - 1) { SetDirty(chunkPos + Vector3i.forward); } OpenCog.Map.Lighting.OCSunLightComputer.RecomputeLightAtPosition(this, pos); OpenCog.Map.Lighting.OCLightComputer.RecomputeLightAtPosition(this, pos); UpdateMeshColliderAfterBlockChange(); // TODO [BLOCKED]: uncomment when aspect stuff is in place? // asp.OnExit(null); OpenCog.Embodiment.OCPerceptionCollector perceptionCollector = OpenCog.Embodiment.OCPerceptionCollector.Instance; List <GameObject> batteries = GameObject.FindGameObjectsWithTag("OCBattery").ToList(); GameObject battery = batteries.Where(b => VectorUtil.AreVectorsEqual(b.transform.position, new Vector3((float)pos.x, (float)pos.y, (float)pos.z))).FirstOrDefault(); List <GameObject> hearths = GameObject.FindGameObjectsWithTag("OCHearth").ToList(); GameObject hearth = hearths.Where(h => VectorUtil.AreVectorsEqual(h.transform.position, new Vector3((float)pos.x, (float)pos.y, (float)pos.z))).FirstOrDefault(); if (block.IsEmpty() && !oldBlock.IsEmpty()) { UnityEngine.Debug.Log(OCLogSymbol.RUNNING + "OCMap.SetBlockAndRecompute(): Null block type sent; destroying block."); if (perceptionCollector != null && oldBlock.block.GetName() != "Battery") { perceptionCollector.NotifyBlockRemoved(pos); } // I'm going to take a gamble here...since NotifyBatteryRemoved only does its work when it finds a battery at this location...it should be ok... if (perceptionCollector != null && oldBlock.block.GetName() == "Battery") { perceptionCollector.NotifyBatteryRemoved(pos); } if (battery != default(GameObject) && battery != null) { GameObject.DestroyImmediate(battery); } if (hearth != default(GameObject) && hearth != null) { GameObject.DestroyImmediate(hearth); } } else { UnityEngine.Debug.Log(OCLogSymbol.RUNNING + "OCMap.SetBlockAndRecompute(): Block type sent; creating block."); // Moved notify down...to AFTER the point where it is actually created... } if (block.block != null && block.block.GetName() == "Battery" && (battery == default(GameObject) || battery == null)) { GameObject batteryPrefab = OCMap.Instance.BatteryPrefab; if (batteryPrefab == null) { UnityEngine.Debug.LogWarning(OCLogSymbol.WARN + "OCBuilder.Update(), batteryPrefab == null"); } else { GameObject newBattery = (GameObject)GameObject.Instantiate(batteryPrefab); newBattery.transform.position = pos; newBattery.name = "Battery"; newBattery.transform.parent = OCMap.Instance.BatteriesSceneObject.transform; if (perceptionCollector != null) { perceptionCollector.NotifyBatteryAdded(pos); } } } if (block.block != null && block.block.GetName() == "Hearth" && (hearth == default(GameObject) || hearth == null)) { GameObject hearthPrefab = OCMap.Instance.HearthPrefab; if (hearthPrefab == null) { UnityEngine.Debug.LogWarning(OCLogSymbol.WARN + "OCBuilder::Update, hearthPrefab == null"); } else { GameObject newHearth = (GameObject)GameObject.Instantiate(hearthPrefab); newHearth.transform.position = pos; newHearth.name = "Hearth"; newHearth.transform.parent = OCMap.Instance.HearthsSceneObject.transform; if (perceptionCollector != null) { perceptionCollector.NotifyBlockAdded(pos); } } } }
public void SetBlock(OCBlockData block, int x, int y, int z) { blocks.Set(block, new Vector3i(z, y, x)); _isEmpty = false; }
/// <summary> /// Update is called once per frame. /// </summary> public void Update() { if (UnityEngine.Screen.showCursor) { return; } if (UnityEngine.Input.GetKeyDown(UnityEngine.KeyCode.LeftControl)) { Vector3i?point = GetCursor(false); if (point.HasValue) { byte sun = _map.GetSunLightmap().GetLight(point.Value); byte light = _map.GetLightmap().GetLight(point.Value); OCLogger.Info("Sun " + " " + sun + " Light " + light); } } if (UnityEngine.Input.GetKeyDown(UnityEngine.KeyCode.RightControl)) { Vector3i?point = GetCursor(true); if (point.HasValue) { byte sun = _map.GetSunLightmap().GetLight(point.Value); byte light = _map.GetLightmap().GetLight(point.Value); OCLogger.Info("Sun " + sun + " Light " + light); } } if (UnityEngine.Input.GetMouseButtonDown(0)) { Vector3i?point = GetCursor(true); if (point.HasValue) { _map.SetBlockAndRecompute(new OpenCog.Map.OCBlockData(), point.Value); } } if (UnityEngine.Input.GetMouseButtonDown(1)) { Vector3i?point = GetCursor(false); if (point.HasValue) { bool empty = !BlockCharacterCollision.GetContactBlockCharacter(point.Value, transform.position, gameObject.GetComponent <CharacterController>()).HasValue; if (empty) { OpenCog.Map.OCBlockData block = new OpenCog.Map.OCBlockData(_selectedBlock, OpenCog.Utility.VectorUtil.Vector3ToVector3i(point.Value)); block.SetDirection(GetDirection(-transform.forward)); _map.SetBlockAndRecompute(block, point.Value); } } } Vector3i?cursor = GetCursor(true); _cursor.SetActive(cursor.HasValue); if (cursor.HasValue) { _cursor.transform.position = cursor.Value; } OCLogger.Fine(gameObject.name + " is updated."); }
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) { OpenCog.BlockSet.BaseBlockSet.OCBlock newBlock = blockSet.GetBlock(ourBlockID); OCBlockData block = new OpenCog.Map.OCBlockData(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()
public void SetBlock(OCBlockData block, int x, int y, int z) { blocks[z, y, x] = block; _isEmpty = false; }
/// <summary> /// Update is called once per frame. /// </summary> public void Update() { if (UnityEngine.Screen.showCursor) { return; } if (UnityEngine.Input.GetKeyDown(UnityEngine.KeyCode.LeftControl)) { Vector3i?point = GetCursor(false); if (point.HasValue) { byte sun = _map.GetSunLightmap().GetLight(point.Value); byte light = _map.GetLightmap().GetLight(point.Value); OCLogger.Info("Sun " + " " + sun + " Light " + light); } } if (UnityEngine.Input.GetKeyDown(UnityEngine.KeyCode.RightControl)) { Vector3i?point = GetCursor(true); if (point.HasValue) { byte sun = _map.GetSunLightmap().GetLight(point.Value); byte light = _map.GetLightmap().GetLight(point.Value); OCLogger.Info("Sun " + sun + " Light " + light); } } if (UnityEngine.Input.GetMouseButtonDown(0)) { Vector3i?point = GetCursor(true); if (point.HasValue) { // Vector3i above = point.Value; // above.y += 1; // OCBlockData blockAbove = _map.GetBlock(above); // OCBlockData blockData = _map.GetBlock (point.Value); // if(blockAbove.IsEmpty() && !blockData.IsEmpty()) // { // _map.SetBlockAndRecompute(blockData, above); // _map.SetBlockAndRecompute(OCBlockData.CreateInstance<OCBlockData>().Init(null, point.Value), point.Value); // } // else _map.SetBlockAndRecompute(OCBlockData.CreateInstance <OCBlockData>().Init(null, point.Value), point.Value); } } if (UnityEngine.Input.GetMouseButtonDown(1)) { Vector3i?point = GetCursor(false); if (point.HasValue) { bool empty = !BlockCharacterCollision.GetContactBlockCharacter(point.Value, transform.position, gameObject.GetComponent <CharacterController>()).HasValue; if (empty) { OpenCog.Map.OCBlockData block = OCBlockData.CreateInstance <OCBlockData>().Init(_selectedBlock, OpenCog.Utility.VectorUtil.Vector3ToVector3i(point.Value)); block.SetDirection(GetDirection(-transform.forward)); _map.SetBlockAndRecompute(block, point.Value); OCGoalController[] goalControllers = (OCGoalController[])GameObject.FindObjectsOfType(typeof(OCGoalController)); foreach (OCGoalController goalController in goalControllers) { if (goalController.GoalBlockType == _selectedBlock) { Vector3 sourcePos = goalController.gameObject.transform.position; Vector3 oldDistanceVec = ((Vector3)goalController.GoalBlockPos) - sourcePos; Vector3 newDistanceVec = point.Value - sourcePos; if (newDistanceVec.sqrMagnitude < oldDistanceVec.sqrMagnitude) { goalController.GoalBlockPos = point.Value; goalController.MoveTargetsIfNecessary(); } } } } } } Vector3i?cursor = GetCursor(true); _cursor.SetActive(cursor.HasValue); if (cursor.HasValue) { _cursor.transform.position = cursor.Value; } OCLogger.Fine(gameObject.name + " is updated."); }