public bool ConstructNewRandomBody() // returns false if body fails to be constructed { myDNA.ClearDNA(); // making a new random body, gotta clear out old data in DNA nodes.Clear(); joints.Clear(); // This loop attempts to create a body by creating a node in a valid location, then connecting it to an existing node for (int i = 0; i < gd.numberOfNodes; i++) { Vector3 position = GetValidSpaceForNode(); if (position == ILLEGALVECTOR) { return(false); } GameObject newlyCreatedNode = gd.lb.GetNode(position); newlyCreatedNode.GetComponent <Rigidbody>().useGravity = true; // The new node must use gravity nodes.Add(newlyCreatedNode); if (i != 0) // If this is the first node, we just let it exist, otherwise we need to connect it to something { if (!GetValidConnectionToNode(nodes.Count - 1, Random.Range(0, i))) { gd.lb.ReturnNode(newlyCreatedNode); nodes.Remove(newlyCreatedNode); i--; } else { myDNA.AddToPositions(newlyCreatedNode.transform.position - origin); // This position is added in } } else { myDNA.AddToPositions(newlyCreatedNode.transform.position - origin); } } // At this point we've created a bot that is an acyclic graph. in order to introduce the possibility of // cyclic structures we throw on an additional attempt to build a few connections // This next part splits the nodes into groups, then attempts to pair up two of those nodes in either group // once we successfully do this numberOfExtraConnections times (or 50 attempts) we jump out of the loop int count = 0; int loop = 0; while (count < gd.additionalConnections && loop < 10) { loop++; int divider = Random.Range(1, nodes.Count - 1); if (GetValidConnectionToNode(Random.Range(0, divider), Random.Range(divider, nodes.Count))) { count++; } } return(true); }