//methods #region private void CheckForCloseConnections() { distAngle = float.MaxValue; lastDistAngle = float.MaxValue; rot = float.MaxValue; foreach (Connection c in connections) { foundC = ConnectionVoxelContainer.RevealConnections(c); if (foundC.Count == 0) { return; } //if (CheckWithJobs(c, foundC)) return; if (CheckWithoutJobs(c, foundC)) { return; } //if (CheckWithJobsAngle(c, foundC)) return; } }
public static void ResetSimulation(JsonAssembly assembly = null) { rbs.Clear(); ConnectionVoxelContainer.RemoveAllConnections(); CollisionVoxelContainer.RemoveAllGos(); foreach (KeyValuePair <int, Vector3> pos in savePositions) { GameObject go = GlobalReferences.FrozenParts[pos.Key]; go.transform.parent = null; go.transform.position = pos.Value; go.transform.rotation = saveRotations[pos.Key]; Part part = go.GetComponent <Part>(); CollisionVoxelContainer.StoreGameObject(go); foreach (int i in part.ActiveConnections) { ConnectionVoxelContainer.StoreConnection(part.Connections[i]); } } savePositions.Clear(); saveRotations.Clear(); simulationDone = false; }
// Start is called before the first frame update public void Initialize(float _minX, float _maxX, float _minY, float _maxY, float _minZ, float _maxZ, float connectionStep, float colliderStep) { conMaxX = (float)(Math.Ceiling(_maxX / connectionStep) * connectionStep); conMinX = (float)(Math.Floor(_minX / connectionStep) * connectionStep); conMaxY = (float)(Math.Ceiling(_maxY / connectionStep) * connectionStep); conMinY = (float)(Math.Floor(_minY / connectionStep) * connectionStep); conMaxZ = (float)(Math.Ceiling(_maxZ / connectionStep) * connectionStep); conMinZ = (float)(Math.Floor(_minZ / connectionStep) * connectionStep); collMaxX = (float)(Math.Ceiling(_maxX / colliderStep) * colliderStep); collMinX = (float)(Math.Floor(_minX / colliderStep) * colliderStep); collMaxY = (float)(Math.Ceiling(_maxY / colliderStep) * colliderStep); collMinY = (float)(Math.Floor(_minY / colliderStep) * colliderStep); collMaxZ = (float)(Math.Ceiling(_maxZ / colliderStep) * colliderStep); collMinZ = (float)(Math.Floor(_minZ / colliderStep) * colliderStep); gameArea = new Region(_minX, _maxX, _minY, _maxY, _minZ, _maxZ); _conStep = connectionStep; _collStep = colliderStep; ConnectionVoxelContainer.Initialize(ConX, ConY, ConZ, ConOffsetX, ConOffsetY, ConOffsetZ); CollisionVoxelContainer.Initialize(CollX, CollY, CollZ, CollOffsetX, CollOffsetY, CollOffsetZ); }
public void SetInactive(int conID) { if (ActiveConnections.Contains(conID) && connections.Count > conID) { activeConnections.Remove(conID); ConnectionVoxelContainer.RemoveConnection(connections[conID]); } }
public void SetActive(int conID) { if (!ActiveConnections.Contains(conID) && connections.Count > conID && conID >= 0) { activeConnections.Add(conID); ConnectionVoxelContainer.StoreConnection(connections[conID]); } }
public static void UpdateData(byte[] data) { LoadAggregationContainer container = ReadData(data); Dictionary <int, GameObject> checkFrozen = new Dictionary <int, GameObject>(); foreach (KeyValuePair <int, GameObject> pair in GlobalReferences.FrozenParts) { checkFrozen.Add(pair.Key, pair.Value); } foreach (PartSpawnData item in container.data) { GameObject checkGo = GlobalReferences.FrozenParts[item.id]; if (checkGo == null) { Debug.LogError("Part " + item.id + " not found in check"); continue; } Vector3 oldPos = checkGo.transform.position; List <Connection> connections = checkGo.GetComponent <Part>().Connections; if (Vector3.Distance(oldPos, item.Position) > 0.001f) { CollisionVoxelContainer.RemoveGameObject(checkGo); foreach (Connection con in connections) { ConnectionVoxelContainer.RemoveConnection(con); } } checkGo.transform.position = item.Position; checkGo.transform.rotation = item.Rotation; if (Vector3.Distance(oldPos, item.Position) > 0.001f) { CollisionVoxelContainer.StoreGameObject(checkGo); foreach (Connection con in connections) { ConnectionVoxelContainer.StoreConnection(con); } } checkFrozen.Remove(item.id); } GameObject[] leftovers = checkFrozen.Values.ToArray <GameObject>(); for (int i = leftovers.Length - 1; i >= 0; --i) { GameObject go = leftovers[i]; Part leftover = go.GetComponent <Part>(); Debug.LogError("Part " + leftover.ID + " was left over in check and will be deleted"); leftover.LocalDelete(); } }
public static void AfterSimulation(TIExportAR exportItem = null, bool export = false) { ++handlerCallBack; if (handlerCallBack >= handlers.Count) { if (!export) { ConnectionVoxelContainer.RemoveAllConnections(); CollisionVoxelContainer.RemoveAllGos(); foreach (GameObject go in GlobalReferences.FrozenParts.Values) { go.transform.parent = null; Part part = go.GetComponent <Part>(); CollisionVoxelContainer.StoreGameObject(go); foreach (int i in part.ActiveConnections) { ConnectionVoxelContainer.StoreConnection(part.Connections[i]); } } for (int i = rbs.Count - 1; i >= 0; --i) { MonoBehaviour.Destroy(rbs[i]); } simulationInProgress = false; simulationDone = false; } else { string c = expCount.ToString(); while (c.Length < countDigits) { c = "0" + c; } SaveLoad.SaveGame(c + "_", true, folder); } for (int i = handlers.Count - 1; i >= 0; --i) { MonoBehaviour.Destroy(handlers[i]); } handlers.Clear(); handlerCallBack = 0; if (export) { Export(exportItem); } } }
public void Enable() { if (!disabled) { return; } foreach (int i in ActiveConnections) { ConnectionVoxelContainer.StoreConnection(Connections[i]); } GetComponent <MeshRenderer>().material = MaterialHolder.EnabledMat; disabled = false; }
public void Disable() { if (disabled) { return; } foreach (int i in ActiveConnections) { ConnectionVoxelContainer.RemoveConnection(Connections[i]); } GetComponent <MeshRenderer>().material = MaterialHolder.DisabledMat; disabled = true; }
public override void OnEvent(BlockFreeze evnt) { Destroy(gameObject.GetComponent <ConstantForce>()); Part p = gameObject.GetComponent <Part>(); gameObject.transform.position = evnt.BlockPosition; gameObject.transform.rotation = evnt.BlockRotation; p.FreezePart(evnt.ID); p.Parent = evnt.ParentID; p.ParentCon = evnt.ParentCon; Part parentPart = GlobalReferences.FrozenParts[evnt.ParentID].GetComponent <Part>(); ConnectionVoxelContainer.RemoveConnection(parentPart.Connections[evnt.ParentCon]); ConnectionVoxelContainer.RemoveConnection(gameObject.GetComponent <Part>().Connections[evnt.ConnectionID]); parentPart.ChildCons.Add(evnt.ConnectionID); parentPart.SetInactive(parentPart.Connections[evnt.ParentCon]); p.SetInactive(p.Connections[evnt.ConnectionID]); parentPart.Children.Add((int)p.ID); gameObject.transform.position = evnt.BlockPosition; gameObject.transform.rotation = evnt.BlockRotation; if (PlacementReferences.InfiniteParts && entity.IsOwner && p.Respawn) { GameObject go = GlobalReferences.PartSpawner.SpawnPart(p.TemplateID); go.GetComponent <Part>().Respawn = true; if (PlacementReferences.PlacementType == PlacementReferences.PlaceChoreo.Choreo) { GlobalReferences.AffectPart(go); GlobalReferences.FreeParts.Remove(go); } } if (PlacementReferences.PlacementType == PlacementReferences.PlaceChoreo.Choreo && entity.IsOwner) { GlobalReferences.ChangeAffectedNumber(PlacementReferences.AffectedParts); } if (entity.IsOwner) { //entity.Freeze(true); } }
private void RealizeConnection() { if (lastDistAngle < connectionThreshold && ConnectionVoxelContainer.RevealConnections(bestOnPart).Contains(closestConnection)) { Vector3 pos = gameObject.transform.position; Quaternion rot = gameObject.transform.rotation; AlignPlane.Orient(bestOnPart.Pln, closestConnection.Pln, gameObject); if (!CollisionDetection()) { Part p = gameObject.GetComponent <Part>(); p.FreezePart(); p.Parent = closestConnection.ParentPart.ID; p.ParentCon = closestConnection.ParentPart.Connections.IndexOf(closestConnection); ConnectionVoxelContainer.RemoveConnection(closestConnection); ConnectionVoxelContainer.RemoveConnection(bestOnPart); bestOnPart.ParentPart.SetInactive(bestOnPart); closestConnection.ParentPart.SetInactive(closestConnection); closestConnection.ParentPart.ChildCons.Add(bestOnPart.ParentPart.Connections.IndexOf(bestOnPart)); closestConnection.ParentPart.Children.Add((int)p.ID); GameObject _g = PartsHolder.SpawnPart(p.TemplateID); _g.SetActive(true); if (GlobalReferences.PlacementType == PlacementTypeTool.PlaceChoreo.Choreo) { GlobalReferences.AffectPart(_g); GlobalReferences.FreeParts.Remove(_g); } ConnectionScanningHandler handler = gameObject.GetComponent <ConnectionScanningHandler>(); if (handler != null) { handler.TerminateConnection(); } } else { gameObject.transform.position = pos; gameObject.transform.rotation = rot; } } }
public void SetActive(Connection c) { int i = 0; foreach (Connection con in Connections) { if (c == con) { if (!ActiveConnections.Contains(i)) { activeConnections.Add(i); ConnectionVoxelContainer.StoreConnection(con); return; } } ++i; } }
public void SetInactive(Connection c) { int i = 0; foreach (Connection con in Connections) { if (c == con) { if (ActiveConnections.Contains(i)) { activeConnections.Remove(i); ConnectionVoxelContainer.RemoveConnection(con); return; } } ++i; } }
public override void OnEvent(EndSimulation evnt) { if (!evnt.FromSelf) { ConnectionVoxelContainer.RemoveAllConnections(); CollisionVoxelContainer.RemoveAllGos(); foreach (GameObject go in GlobalReferences.FrozenParts.Values) { Part part = go.GetComponent <Part>(); CollisionVoxelContainer.StoreGameObject(go); foreach (int i in part.ActiveConnections) { ConnectionVoxelContainer.StoreConnection(part.Connections[i]); } } } PlacementReferences.Scanning = true; }
public void FreezePart(int?_id = null) { placedMat = Resources.Load <Material>("Materials/PlacedMaterial"); gameObject.GetComponent <MeshRenderer>().material = placedMat; gameObject.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; gameObject.layer = 9; Destroy(gameObject.GetComponent <PartBehaviour>()); Destroy(gameObject.GetComponent <ConnectionScanning>()); foreach (int i in ActiveConnections) { ConnectionVoxelContainer.StoreConnection(connections[i]); } CollisionVoxelContainer.StoreGameObject(gameObject); if (GlobalReferences.AffectedParts.Contains(gameObject)) { GlobalReferences.AffectedParts.Remove(gameObject); } if (GlobalReferences.FreeParts.Contains(gameObject)) { GlobalReferences.FreeParts.Remove(gameObject); } if (_id == null) { id = GlobalReferences.NumOfParts; ++GlobalReferences.NumOfParts; } else { id = _id; } GlobalReferences.FrozenParts.Add((int)ID, gameObject); gameObject.name = name + "_" + id; }
void FreezeSinglePlayer() { Part p = gameObject.GetComponent <Part>(); p.FreezePart(); p.Parent = closestConnection.ParentPart.ID; p.ParentCon = closestConnection.ParentPart.Connections.IndexOf(closestConnection); p.ConToParent = bestOnPart.ParentPart.Connections.IndexOf(bestOnPart); ConnectionVoxelContainer.RemoveConnection(closestConnection); ConnectionVoxelContainer.RemoveConnection(bestOnPart); closestConnection.ParentPart.ChildCons.Add(bestOnPart.ParentPart.Connections.IndexOf(bestOnPart)); closestConnection.ParentPart.Children.Add((int)p.ID); bestOnPart.ParentPart.SetInactive(bestOnPart); closestConnection.ParentPart.SetInactive(closestConnection); if (PlacementReferences.InfiniteParts && p.Respawn) { GameObject go = GlobalReferences.PartSpawner.SpawnPart(p.TemplateID); go.GetComponent <Part>().Respawn = true; if (PlacementReferences.PlacementType == PlacementReferences.PlaceChoreo.Choreo) { GlobalReferences.AffectPart(go); GlobalReferences.FreeParts.Remove(go); } } if (PlacementReferences.PlacementType == PlacementReferences.PlaceChoreo.Choreo) { GlobalReferences.ChangeAffectedNumber(PlacementReferences.AffectedParts); } }
private void EnableDisable(Collider other) { if (Enable && AddRem == AddRemove.Remove) { Part p = other.gameObject.GetComponent <Part>(); if (p != null && !Removed.ContainsKey((int)p.ID)) { List <Connection> tempCons = new List <Connection>(); foreach (int i in p.ActiveConnections) { ConnectionVoxelContainer.RemoveConnection(p.Connections[i]); tempCons.Add(p.Connections[i]); } Removed.Add((int)p.ID, tempCons); } other.GetComponent <MeshRenderer>().material = disableMat; } else if (Enable && AddRem == AddRemove.Add) { Part p = other.gameObject.GetComponent <Part>(); if (p != null && Removed.ContainsKey((int)p.ID)) { foreach (Connection c in Removed[(int)p.ID]) { ConnectionVoxelContainer.StoreConnection(c); } Removed.Remove((int)p.ID); other.GetComponent <MeshRenderer>().material = enableMat; } } else { purgatory.Add(other); } }
//methods #region private void CheckForCloseConnections() { distAngle = 100; lastDistAngle = 1000; rot = 1000; foreach (Connection c in connections) { foundC = ConnectionVoxelContainer.RevealConnections(c); if (foundC.Count > 0) { foreach (Connection _c in foundC) { if (_c.Pln.Parent != null) { dist = Vector3.Distance(c.Pln.Origin, _c.Pln.Origin); angle = AlignPlane.BuildAngle(c.Pln.LocalZVector, _c.Pln.LocalZVector, true); rot = AlignPlane.BuildAngle(c.Pln.XVector, _c.Pln.XVector, false) * 10 * connectionThreshold; distAngle = dist + AngleTightening(angle) + rot / 1000 * connectionThreshold; string grammer = _c.ConType + ">" + c.ConType; if (distAngle < lastDistAngle && _c.CheckForRule(c) != -1 && RuleActive(grammer)) { closestConnection = _c; bestOnPart = c; lastDistAngle = distAngle; if (lastDistAngle < connectionThreshold / 5) { return; } } } } } } }
// Update is called once per frame void Update() { Debug.Log(ConnectionVoxelContainer.CountAllConnections()); }
public static void DeletePart(int id) { GameObject go = frozenParts[id]; int? parentID = go.GetComponent <Part>().Parent; if (parentID != null && frozenParts.ContainsKey((int)parentID)) { List <int> _children = frozenParts[(int)parentID].GetComponent <Part>().Children; for (int i = _children.Count - 1; i >= 0; --i) { if (_children[i] == id) { Part parentP = frozenParts[(int)parentID].GetComponent <Part>(); Part p = go.GetComponent <Part>(); parentP.Children.RemoveAt(i); if (parentP.ChildCons.Count > i) { parentP.ChildCons.RemoveAt(i); } foreach (int j in go.GetComponent <Part>().ChildCons) { Part parentPart = frozenParts[(int)parentID].GetComponent <Part>(); if (parentPart.Connections.Count > j) { parentPart.SetActive(parentPart.Connections[j]); } } if (p.ChildCons.Count > i) { ConnectionVoxelContainer.StoreConnection(frozenParts[p.Children[i]].GetComponent <Part>().Connections[p.ChildCons[i]]); //ConnectionVoxelContainer.StoreConnection(p.ChildCons[i]); } } } } int[] children = go.GetComponent <Part>().Children.ToArray(); for (int i = 0; i < children.Length; ++i) { int child = children[i]; if (frozenParts.ContainsKey(child)) { frozenParts[child].GetComponent <Part>().Parent = null; frozenParts[child].GetComponent <Part>().ParentCon = null; } int?parent = go.GetComponent <Part>().Parent; if (parent != null && frozenParts.ContainsKey((int)parent)) { Part parentPart = frozenParts[(int)parent].GetComponent <Part>(); Connection parentCon = parentPart.Connections[(int)go.GetComponent <Part>().ParentCon]; frozenParts[children[i]].GetComponent <Part>().SetActive(parentCon); ConnectionVoxelContainer.StoreConnection(parentCon); } //frozenParts[children[i]].GetComponent<Part>().SetActive(go.GetComponent<Part>().ParentCon); } frozenParts.Remove(id); parts.Remove(go); Destroy(go); }
public static void DeletePart(int id) { GameObject go = frozenParts[id]; int? parentID = go.GetComponent <Part>().Parent; if (parentID != null && frozenParts.ContainsKey((int)parentID)) { List <int> _children = frozenParts[(int)parentID].GetComponent <Part>().Children; for (int i = _children.Count - 1; i >= 0; --i) { if (_children[i] == id) { Part parentP = frozenParts[(int)parentID].GetComponent <Part>(); Part p = go.GetComponent <Part>(); parentP.Children.RemoveAt(i); if (parentP.ChildCons.Count > i) { parentP.ChildCons.RemoveAt(i); } foreach (int j in go.GetComponent <Part>().ChildCons) { Part parentPart = frozenParts[(int)parentID].GetComponent <Part>(); if (parentPart.Connections.Count > j && j != -1) { parentPart.SetActive(parentPart.Connections[j]); } } if (p.ChildCons.Count > i) { try { ConnectionVoxelContainer.StoreConnection(frozenParts[p.Children[i]].GetComponent <Part>().Connections[p.ChildCons[i]]); } catch { Debug.Log("Connection not found"); } //ConnectionVoxelContainer.StoreConnection(p.ChildCons[i]); } } } } Part part = go.GetComponent <Part>(); int[] children = part.Children.ToArray(); for (int i = 0; i < children.Length; ++i) { int child = children[i]; if (frozenParts.ContainsKey(child)) { Part childPart = frozenParts[child].GetComponent <Part>(); childPart.Parent = -1; childPart.ParentCon = -1; childPart.SetActive(childPart.ConToParent); childPart.ConToParent = -1; } int parent = part.Parent; if (parent != -1 && frozenParts.ContainsKey(parent)) { Part parentPart = frozenParts[parent].GetComponent <Part>(); if (go.GetComponent <Part>().ParentCon != -1) { parentPart.SetActive(part.ParentCon); for (int j = 0; j < parentPart.Children.Count; ++j) { if (parentPart.Children[j] == part.ID) { parentPart.Children.RemoveAt(j); parentPart.ChildCons.RemoveAt(j); break; } } } } } Destroy(go); }
private void RealizeConnection() { if (lastDistAngle < connectionThreshold && ConnectionVoxelContainer.RevealConnections(bestOnPart).Contains(closestConnection)) { Vector3 pos = gameObject.transform.position; Quaternion rot = gameObject.transform.rotation; if (!AlignPlane.Orient(bestOnPart.Pln, closestConnection.Pln, gameObject)) { gameObject.transform.position = pos; gameObject.transform.rotation = rot; return; } //PlacementBehaviour.ReleasePart(true); Destroy(gameObject.GetComponent <ConstantForce>()); Destroy(gameObject.GetComponent <PartBehaviour>()); Destroy(gameObject.GetComponent <ConnectionScanning>()); gameObject.layer = 13; var check = gameObject.AddComponent <CheckCollision>(); check.respawnPosition = pos; check.respawnRotation = rot; check.closestConnection = closestConnection; check.bestOnPart = bestOnPart; if (transform.parent != null) { transform.parent = null; } GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; GetComponent <Rigidbody>().collisionDetectionMode = CollisionDetectionMode.Continuous; Destroy(this); /* * if (BoltNetwork.IsRunning) * { * var checkFreeze = CheckBlockFreeze.Create(gameObject.GetComponent<NetworkBlockBehaviour>().entity); * checkFreeze.BlockPosition = gameObject.transform.position; * checkFreeze.BlockRotation = gameObject.transform.rotation; * * checkFreeze.OldBlockPosition = pos; * checkFreeze.OldBlockRotation = rot; * * if (closestConnection.ParentPart.ID != null) * { * checkFreeze.ParentID = (int)closestConnection.ParentPart.ID; * } * else * { * checkFreeze.ParentID = -1; * } * * checkFreeze.ParentCon = closestConnection.ParentPart.Connections.IndexOf(closestConnection); * checkFreeze.ConnectionID = gameObject.GetComponent<Part>().Connections.IndexOf(bestOnPart); * * checkFreeze.Send(); * } */ } }
public void FreezePart(int _id = -1) { if (!TestDeleteSpawn()) { Destroy(gameObject); return; } placedMat = Resources.Load <Material>("Materials/" + MaterialHolder.MatSet + "/PlacedMaterial"); gameObject.GetComponent <MeshRenderer>().material = placedMat; gameObject.layer = 9; if (GetComponent <ConstantForce>() != null) { Destroy(gameObject.GetComponent <ConstantForce>()); } if (GetComponent <Rigidbody>() != null) { Destroy(gameObject.GetComponent <Rigidbody>()); } if (GetComponent <PartBehaviour>() != null) { Destroy(gameObject.GetComponent <PartBehaviour>()); } if (GetComponent <ConnectionScanning>() != null) { Destroy(gameObject.GetComponent <ConnectionScanning>()); } if (GetComponent <KillTimer>() != null) { Destroy(GetComponent <KillTimer>()); } gameObject.isStatic = true; foreach (int i in ActiveConnections) { ConnectionVoxelContainer.StoreConnection(connections[i]); } CollisionVoxelContainer.StoreGameObject(gameObject); if (GlobalReferences.AffectedParts.Contains(gameObject)) { GlobalReferences.AffectedParts.Remove(gameObject); } if (GlobalReferences.FreeParts.Contains(gameObject)) { GlobalReferences.FreeParts.Remove(gameObject); } if (_id == -1) { id = GlobalReferences.GetNextID(); } else { id = _id; } GlobalReferences.FrozenParts.Add((int)ID, gameObject); gameObject.name = name + "_" + id; if (TICtrlZ.LastID != ID) { TICtrlZ.History.Push(new HistoryItem(ID)); } //Debug.Log("#43 Block Transform Frozen: " + ID + ", Position = " + transform.position.ToString("F2") + ", Rotation = " + transform.rotation.eulerAngles.ToString("F2")); }