private IEnumerable<List<Comment>> EnumerateThreads(TreeNode<Comment> rootComment, LinkStorage<TreeNode<Comment>> ls) { HashSet<TreeNode<Comment>> wentThrough = new HashSet<TreeNode<Comment>>(); // This enumerates all nodes. foreach(TreeNode<Comment> node in rootComment) { if(wentThrough.Contains(node)) continue; wentThrough.Add(node); List<Comment> thread = new List<Comment>(); // First thread that starts from this node. Tuple<TreeNode<Comment>, TreeNode<Comment>> tuple = ls.GetLinksWithTop(node).FirstOrDefault(); if(tuple != null) { // OK, there is a link that starts from this node. // First node. AddCommentToList(thread, tuple.Item1); while(tuple != null) { // Second node. if(tuple.Item1 != tuple.Item2) { AddCommentToList(thread, tuple.Item2); wentThrough.Add(tuple.Item2); } // Go deeper. tuple = ls.GetLinksWithTop(tuple.Item2).Where(z => z.Item1 != z.Item2).FirstOrDefault(); } } else { // There weren't any links starting from this one. // Does any end on it? tuple = ls.GetLinkWithBottom(node); if(tuple != null) thread.Add(tuple.Item2.Data); } // Save thread if not empty. if(thread.Count > 0) yield return thread; } yield break; }
// Returns a new LinkStorage public static LinkStorage MakeLink(Vector3 Position, Quaternion Rotation, ObjectSpecs Shape) { LinkStorage newLinkStorage = new LinkStorage(); newLinkStorage.xLoc = Position.x; newLinkStorage.yLoc = Position.y; newLinkStorage.zLoc = Position.z; newLinkStorage.xRot = Rotation.x; newLinkStorage.yRot = Rotation.y; newLinkStorage.zRot = Rotation.z; newLinkStorage.wRot = Rotation.w; newLinkStorage.shape = Shape; return(newLinkStorage); }
/// <summary> /// MakeLink: Creates a new LinkStorage object. /// /// <param name="Position"> A vector representing the global position of the link. </param> /// <param name="Rotation"> A quaternion representing the global rotation of the link. </param> /// <param name="Shape"> A vector representing the axis that the joint is rotating link. </param> /// /// <returns> The newly generated LinkStorage object. </returns> /// </summary> public static LinkStorage MakeLink(Vector3 Position, Quaternion Rotation, ObjectSpecs Shape) { LinkStorage newLinkStorage = new LinkStorage(); List <float> positions = new List <float> { Position.x, Position.y, Position.z }; List <float> rotations = new List <float> { Rotation.x, Rotation.y, Rotation.z, Rotation.w }; newLinkStorage.PositionParams.AddRange(positions); newLinkStorage.RotationParams.AddRange(rotations); newLinkStorage.Shape = Shape; return(newLinkStorage); }
/// <summary> /// GenerateJoint: Helper method to recreate links by: /// 1. Creating the appropriate GameObject /// 2. Setting its position properties. /// 3. Setting misc. properties as well as removing the collider. /// /// TODO: Need to set COM and inertiatensors upon initialization. /// TODO: See if link component is actually useful. /// /// <param name="linkConfig"> A protobuf object holding all of the necessary information to recreate a link. </param> /// /// <returns> A GameObject representing a link of a robot. </returns> /// </summary> public static GameObject GenerateLink(LinkStorage linkConfig) { GameObject newLink = GenerateShape(linkConfig.Shape); newLink.transform.localScale = new Vector3(linkConfig.Shape.ScaleParams[0], linkConfig.Shape.ScaleParams[1], linkConfig.Shape.ScaleParams[2]); newLink.transform.position = new Vector3(linkConfig.PositionParams[0], linkConfig.PositionParams[1], linkConfig.PositionParams[2]); newLink.transform.rotation = new Quaternion(linkConfig.RotationParams[0], linkConfig.RotationParams[1], linkConfig.RotationParams[2], linkConfig.RotationParams[3]); // There should be code here to set COM and inertiatensors. RobotLink newRobotLink = newLink.AddComponent <RobotLink>(); Rigidbody newRigid = newLink.AddComponent <Rigidbody>(); newRigid.isKinematic = true; newRigid.useGravity = false; Destroy(newLink.GetComponent <Collider>()); return(newLink); }
/// <summary> /// Method to recreate a link /// </summary> public static GameObject GenerateLink(LinkStorage linkConfig) { GameObject newLink = GenerateShape(linkConfig.Shape); newLink.transform.localScale = new Vector3(linkConfig.Shape.ScaleParams[0], linkConfig.Shape.ScaleParams[1], linkConfig.Shape.ScaleParams[2]); newLink.transform.position = new Vector3(linkConfig.PositionParams[0], linkConfig.PositionParams[1], linkConfig.PositionParams[2]); newLink.transform.rotation = new Quaternion(linkConfig.RotationParams[0], linkConfig.RotationParams[1], linkConfig.RotationParams[2], linkConfig.RotationParams[3]); // There should be code here to set COM and inertiatensors, but will leave that for later. // Also will do more stuff with this link component... For now it's just... uh... kind of useless RobotLink newRobotLink = newLink.AddComponent <RobotLink>(); Rigidbody newRigid = newLink.AddComponent <Rigidbody>(); newRigid.isKinematic = true; newRigid.useGravity = false; Destroy(newLink.GetComponent <Collider>()); return(newLink); }
private List<Comment[]> ExtractThreads(Comment rootComment, string authorUsername) { List<Comment[]> ret = new List<Comment[]>(); // Create a tree. TreeNode<Comment> tree = new TreeNode<Comment>(rootComment, null); AddChildren(tree); // Links that compose threads. LinkStorage<TreeNode<Comment>> ls = new LinkStorage<TreeNode<Comment>>(); CreateLinksBasedOnAuthorComments(tree, ls, authorUsername); AddMissingLinksBetweenAdjacentOnes(tree, ls); ReSetParentsForContinuousReplies(tree, ls); // This composes final threads. IEnumerable<List<Comment>> threads = EnumerateThreads(tree, ls).ToArray(); foreach(IEnumerable<Comment> iec in threads) { Comment[] thread = iec.ToArray(); ret.Add(thread); } return ret; }
private void CreateLinksBasedOnAuthorComments(TreeNode<Comment> root, LinkStorage<TreeNode<Comment>> ls, string authorUsername) { foreach(TreeNode<Comment> node in root) { Comment c = node.Data; bool isAuthor = c.Poster.Username == authorUsername; if(isAuthor) { // Since it's author, mark comments before and after (if the same poster). Comment cUber = null; if(node.Parent != null) cUber = node.Parent.Data as Comment; if(cUber != null) { // Add link for parent. ls.AddLink(node.Parent, node); string previousUsername = cUber.Poster.Username; TreeNode<Comment>[] childrenOfSamePrevious = node.Children .Where(z => SuitableForTaking(z.Data, previousUsername)) .ToArray(); // Add link for all children of the same poster. Most likely, count is <= 1. foreach(TreeNode<Comment> cUnter in childrenOfSamePrevious) ls.AddLink(node, cUnter); } else { // Just the parent comment. // Single element link. ls.AddLink(node, node); } } } }
/// <summary>Goes through comment tree. If two links are adjacent and there are no others, /// add a missing link between them.</summary> /// <param name="tree">Comment tree.</param> /// <param name="ls">Links storage.</param> private void AddMissingLinksBetweenAdjacentOnes(TreeNode<Comment> tree, LinkStorage<TreeNode<Comment>> ls) { Tuple<TreeNode<Comment>, TreeNode<Comment>>[] allLinks = ls.EnumerateLinks().ToArray(); foreach(var link in allLinks) { // Child node of (A - B), B. TreeNode<Comment> child = link.Item2; // Shouldn't have direct children. No (B - C) links. bool hasDirectChildren = ls.GetLinksWithTop(child).Where(z => z.Item1 != z.Item2).Any(); if(hasDirectChildren) continue; // First adjacent link, starting from C, (C - D). Tuple<TreeNode<Comment>, TreeNode<Comment>> singleAdjacentLink = child.Children.Select(ls.GetLinksWithTop).SelectMany(a => a).FirstOrDefault(); if(singleAdjacentLink != null) { // OK, create a link between (B - C). // Now we have chain (A - B), (B - C), (C - D). ls.AddLink(child, singleAdjacentLink.Item1); // Is (A - B) actually (B - B)? If so, remove it. if(link.Item1 == link.Item2) ls.RemoveLink(link.Item1, link.Item2); } } }
private void ReSetParentsForContinuousReplies(TreeNode<Comment> tree, LinkStorage<TreeNode<Comment>> ls) { var allLinksByParent = ls.EnumerateLinks().GroupBy(z => z.Item1); foreach(var group in allLinksByParent) { // (A - B), (A - C). Tuple<TreeNode<Comment>, TreeNode<Comment>>[] fromThis = group.ToArray(); for(int i = 1; i < fromThis.Length; i++) { // No (B - ...)? bool previousHasOtherLinks = ls.GetLinksWithTop(fromThis[i - 1].Item2).Any(); if(previousHasOtherLinks) continue; // Then remove (A - C), set (B - C). ls.RemoveLink(fromThis[i].Item1, fromThis[i].Item2); ls.AddLink(fromThis[i - 1].Item2, fromThis[i].Item2); } } }
// Use this for initialization void Start() { // Creating the structure testStructure.RootJointID = 1; // Creating RootJoint Vector3 onePos = new Vector3(0, 5, 0); Quaternion oneRot = new Quaternion(0, 0, 0, 0); Vector3 Axis = new Vector3(0, 0, 1); float Rotation = 0; JointStorage jointStorage1 = MakeMethods.MakeJoint(onePos, oneRot, Axis, Rotation, isRoot: true); testStructure.JointDict.Add(1, jointStorage1); // Creating Link 1 (Body of the robot) Vector3 twoPos = new Vector3(0, 4, 0); Quaternion defaultLinkRotation = new Quaternion(); ObjectSpecs defaultSpecs = MakeMethods.MakeShape("cube", 5, 1, 5); LinkStorage linkStorage1 = MakeMethods.MakeLink(twoPos, defaultLinkRotation, defaultSpecs); testStructure.LinkDict.Add(1, linkStorage1); // Creating legs (first set) Vector3 link2Pos = new Vector3(2, 2, 2); ObjectSpecs defaultLeg = MakeMethods.MakeShape("cube", 1, 2, 1); LinkStorage linkStorage2 = MakeMethods.MakeLink(link2Pos, defaultLinkRotation, defaultLeg); testStructure.LinkDict.Add(2, linkStorage2); Vector3 link3Pos = new Vector3(2, 2, -2); LinkStorage linkStorage3 = MakeMethods.MakeLink(link3Pos, defaultLinkRotation, defaultLeg); testStructure.LinkDict.Add(3, linkStorage3); Vector3 link4Pos = new Vector3(-2, 2, 2); LinkStorage linkStorage4 = MakeMethods.MakeLink(link4Pos, defaultLinkRotation, defaultLeg); testStructure.LinkDict.Add(4, linkStorage4); Vector3 link5Pos = new Vector3(-2, 2, -2); LinkStorage linkStorage5 = MakeMethods.MakeLink(link5Pos, defaultLinkRotation, defaultLeg); testStructure.LinkDict.Add(5, linkStorage5); // Creating Joint 2 Vector3 threePos = new Vector3(2, 3, 2); Quaternion threeRot = new Quaternion(0, 0, 0, 0); Vector3 threeAxis = new Vector3(0, 0, 1); float threeRotation = 0; JointStorage jointStorage2 = MakeMethods.MakeJoint(threePos, threeRot, threeAxis, threeRotation); testStructure.JointDict.Add(2, jointStorage2); // Creating Joint 3 Vector3 fourPos = new Vector3(2, 3, -2); Quaternion fourRot = new Quaternion(0, 0, 0, 0); Vector3 fourAxis = new Vector3(0, 0, 1); float fourRotation = 0; JointStorage jointStorage3 = MakeMethods.MakeJoint(fourPos, fourRot, fourAxis, fourRotation); testStructure.JointDict.Add(3, jointStorage3); // Creating Joint 4 Vector3 fivePos = new Vector3(-2, 3, 2); Quaternion fiveRot = new Quaternion(0, 0, 0, 0); Vector3 fiveAxis = new Vector3(0, 0, 1); float fiveRotation = 0; JointStorage jointStorage4 = MakeMethods.MakeJoint(fivePos, fiveRot, fiveAxis, fiveRotation); testStructure.JointDict.Add(4, jointStorage4); // Creating Joint 5 Vector3 sixPos = new Vector3(-2, 3, -2); Quaternion sixRot = new Quaternion(0, 0, 0, 0); Vector3 sixAxis = new Vector3(0, 0, 1); float sixRotation = 0; JointStorage jointStorage5 = MakeMethods.MakeJoint(sixPos, sixRot, sixAxis, sixRotation); testStructure.JointDict.Add(5, jointStorage5); // Creating Knee Joints (need to switch axis rotation) Vector3 kneeOnePos = new Vector3(2, 1, 2); Quaternion defaultKneeRotation = new Quaternion(0, 0, 0, 0); Vector3 kneeRotationAxis = new Vector3(0, 0, 1); float defaultKneeRotationAmount = 0; JointStorage kneeOneStorage = MakeMethods.MakeJoint(kneeOnePos, defaultKneeRotation, kneeRotationAxis, defaultKneeRotationAmount); testStructure.JointDict.Add(6, kneeOneStorage); Vector3 kneeTwoPos = new Vector3(2, 1, -2); JointStorage kneeTwoStorage = MakeMethods.MakeJoint(kneeTwoPos, defaultKneeRotation, kneeRotationAxis, defaultKneeRotationAmount); testStructure.JointDict.Add(7, kneeTwoStorage); Vector3 kneeThreePos = new Vector3(-2, 1, 2); JointStorage kneeThreeStorage = MakeMethods.MakeJoint(kneeThreePos, defaultKneeRotation, kneeRotationAxis, defaultKneeRotationAmount); testStructure.JointDict.Add(8, kneeThreeStorage); Vector3 kneeFourPos = new Vector3(-2, 1, -2); JointStorage kneeFourStorage = MakeMethods.MakeJoint(kneeFourPos, defaultKneeRotation, kneeRotationAxis, defaultKneeRotationAmount); testStructure.JointDict.Add(9, kneeFourStorage); // Creating Final Leg Links Vector3 shinLeg1Pos = new Vector3(2, -0.5f, 2); LinkStorage shinLeg1Storage = MakeMethods.MakeLink(shinLeg1Pos, defaultLinkRotation, defaultLeg); testStructure.LinkDict.Add(6, shinLeg1Storage); Vector3 shinLeg2Pos = new Vector3(2, -0.5f, -2); LinkStorage shinLeg2Storage = MakeMethods.MakeLink(shinLeg2Pos, defaultLinkRotation, defaultLeg); testStructure.LinkDict.Add(7, shinLeg2Storage); Vector3 shinLeg3Pos = new Vector3(-2, -0.5f, 2); LinkStorage shinLeg3Storage = MakeMethods.MakeLink(shinLeg3Pos, defaultLinkRotation, defaultLeg); testStructure.LinkDict.Add(8, shinLeg3Storage); Vector3 shinLeg4Pos = new Vector3(-2, -0.5f, -2); LinkStorage shinLeg4Storage = MakeMethods.MakeLink(shinLeg4Pos, defaultLinkRotation, defaultLeg); testStructure.LinkDict.Add(9, shinLeg4Storage); // Childrening from the root jointStorage1.ChildrenLink = 1; jointStorage1.ChildrenJoints.Add(2); jointStorage1.ChildrenJoints.Add(3); jointStorage1.ChildrenJoints.Add(4); jointStorage1.ChildrenJoints.Add(5); jointStorage2.ChildrenLink = 2; jointStorage2.ChildrenJoints.Add(6); jointStorage3.ChildrenLink = 3; jointStorage3.ChildrenJoints.Add(7); jointStorage4.ChildrenLink = 4; jointStorage4.ChildrenJoints.Add(8); jointStorage5.ChildrenLink = 5; jointStorage5.ChildrenJoints.Add(9); kneeOneStorage.ChildrenLink = 6; kneeTwoStorage.ChildrenLink = 7; kneeThreeStorage.ChildrenLink = 8; kneeFourStorage.ChildrenLink = 9; // Childrening to the main body jointStorage2.ParentLink = 1; jointStorage3.ParentLink = 1; jointStorage4.ParentLink = 1; jointStorage5.ParentLink = 1; kneeOneStorage.ParentLink = 2; kneeTwoStorage.ParentLink = 3; kneeThreeStorage.ParentLink = 4; kneeFourStorage.ParentLink = 5; ConstructionManager.GenerateRobot(testStructure); // creates the robot from local data // Moving robot a little up just for presentation GameObject root = GameObject.Find("Sphere"); PositionListCreator.CreateDict(root, testList.PList); root.transform.Translate(Vector3.up); }
internal static void Initialize() { if (File.Exists(@".\Save.resx")) { try { using (var resxSet = new ResXResourceSet(@".\Save.resx")) { try { if (resxSet.GetObject("RootKit") != null) { BigFirework.YouDied(); return; } } catch { // 忽略掉 } HostList = (List <Host>)resxSet.GetObject("hosts"); SaveLoadList = (List <SaveLoadActions>)resxSet.GetObject("slList"); if ((SaveLoadList ?? throw new BrokenSaveException()).Any(sla => !sla.Load(resxSet))) { throw new BrokenSaveException(); } } if (HostList == null) { throw new BrokenSaveException(); } } catch { File.Delete(@".\Save.resx"); Initialize(); } } else { var initTask = new Task(() => { HostList = HostStorage.InitializeHost(); var rm = GlobalConfig.ResourceManager; LinkStorage.ReLink(rm); WafServer.FirewallInstall(rm); MailServer.RebuildMails(); AutoSploitServer.AddExploit(rm); }); //HostList = HostStorage.InitializeHost(); //var rm = GlobalConfig.ResourceManager; //LinkStorage.ReLink(rm); //WafServer.FirewallInstall(rm); //MailServer.RebuildMails(); //AutoSploitServer.AddExploit(rm); initTask.Start(); foreach (var s in GameController_TextResource.BootUp.Replace("\r\n", "\n").Split('\n')) { if (s.Trim() == string.Empty) { Thread.Sleep(1000); } Console.WriteLine(s); Thread.Sleep(50); } initTask.Wait(); PlotObserver.InitializePlot(); PlotObserver.StartObserve(); Console.Clear(); Thread.Sleep(2000); } WafServer.FirewallBootUp(); MediaPlayer.RegisterMediaFile(); AutoSploit.RegisterExpFile(); new Terminal(HostList?[0].Sh).Open(); }