Exemple #1
0
    private void AddBone(GeneData gene)
    {
        var slot = Slots.FirstOrDefault(s => !s.IsOccupied);

        if (slot == null)
        {
            return;
        }

        slot.IsOccupied = true;

        var bonePrefab = PrefabsManager.Instance.LoadPrefab(string.Format("{0}{1}", gene.GeneType, gene.Subtype));
        var newBody    = (GameObject)Instantiate(bonePrefab,
                                                 gameObject.transform.position,
                                                 gameObject.transform.rotation *
                                                 Quaternion.AngleAxis(slot.Angle, Vector3.forward));

        newBody.transform.parent = gameObject.transform;
        newBody.SetActive(true);

        //var slide = newBody.AddComponent<SliderJoint2D>();
        var slide = newBody.AddComponent <HingeJoint2D>();

        slide.connectedBody   = gameObject.GetComponent <Rigidbody2D>();
        slide.connectedAnchor = new Vector2(slot.X, slot.Y);
        //slide.limits = new JointTranslationLimits2D { min = -0.1f, max = 0.1f };
        slide.limits = new JointAngleLimits2D {
            min = -1.0f, max = 1.0f
        };
        slide.useLimits = true;

        gameObject.SendMessage("OnChildAdded", newBody, SendMessageOptions.DontRequireReceiver);
    }
Exemple #2
0
    bool GetIsApplicable(GeneData gene)
    {
        // Part Type check
        if (gene.GeneType != "node" && gene.GeneType != "stats")
        {
            return(false);
        }

        // Subtype check
        if (gene.ApplicantSubtype != 0 && gene.ApplicantSubtype != Subtype)
        {
            return(false);
        }

        // Generation check
        var gen = Generation;

        if (gen != gene.BaseDepth)
        {
            var actDistance = Mathf.Abs(gen - gene.BaseDepth);
            if (UnityEngine.Random.Range(0f, 1f) < actDistance / (gene.DepthTolerance + 1))
            {
                return(false);
            }
        }

        return(true);
    }
    void ParseGeneData(GeneData geneData)
    {
        GameObject container = new GameObject("BeingContainer");

        GameObject baseGO = Instantiate(baseObject, new Vector2(geneData.dropX, geneData.dropY), Quaternion.identity, container.transform) as GameObject;

        baseGO.GetComponent <Rigidbody2D>().mass = geneData.mass;

        foreach (Wheel wheel in geneData.wheels)
        {
            GameObject wheelGO = Instantiate(wheelObject, baseGO.transform.position + wheel.position, wheel.rotation, container.transform) as GameObject;
            wheelGO.GetComponent <WheelJoint2D>().connectedBody = baseGO.GetComponent <Rigidbody2D>();
            wheelGO.transform.localScale = new Vector3(wheel.radius, wheel.radius, 1);
            WheelJoint2D joint = wheelGO.GetComponent <WheelJoint2D>();
            JointMotor2D motor = joint.motor;

            wheelGO.GetComponent <Rigidbody2D>().mass = baseGO.GetComponent <Rigidbody2D>().mass / geneData.wheels.Count;
            float mass = wheelGO.GetComponent <Rigidbody2D>().mass;
            motor.motorSpeed     = Random.Range(bounds_WheelSpeed.x, bounds_WheelSpeed.y) * mass;
            motor.maxMotorTorque = motor.motorSpeed * mass / 10000000;
            joint.motor          = motor;
        }

        foreach (Shooter shooter in geneData.shooters)
        {
            GameObject   shooterGO = Instantiate(shooterObject, baseGO.transform.position + shooter.position, shooter.rotation, baseGO.transform) as GameObject;
            ShooterLogic sl        = shooterGO.GetComponent <ShooterLogic>();
            sl.shootForce = shooter.shootForce;
            sl.shootDelay = shooter.shootDelay;
            sl.ammo       = shooter.ammo;
            sl.ammoType   = shooter.ammoType;
        }

        currentBeing = container;
    }
Exemple #4
0
    private void AddNode(GeneData gene)
    {
        var slot = Slots.FirstOrDefault(s => !s.IsOccupied);

        if (slot == null)
        {
            return;
        }

        slot.IsOccupied = true;

        var nodePrefab = PrefabsManager.Instance.LoadPrefab(string.Format("{0}{1}", gene.GeneType, gene.Subtype));

        var newBody = (GameObject)Instantiate(nodePrefab,
                                              gameObject.transform.position,
                                              Quaternion.FromToRotation(Vector3.right, Vector3.up) *
                                              //Quaternion.AngleAxis(Random.Range(-slot.Angle, slot.Angle), Vector3.forward));
                                              Quaternion.AngleAxis(slot.Angle, Vector3.forward));

        newBody.transform.parent = gameObject.transform;
        newBody.SetActive(true);

        var hinge = newBody.AddComponent <HingeJoint2D>();

        hinge.connectedBody   = gameObject.GetComponent <Rigidbody2D>();
        hinge.connectedAnchor = new Vector2(slot.X, slot.Y);
        hinge.limits          = new JointAngleLimits2D {
            min = -1.0f, max = 1.0f
        };
        hinge.useLimits = true;

        newBody.AddComponent <HingeSmoothPos>();

        gameObject.SendMessage("OnChildAdded", newBody, SendMessageOptions.DontRequireReceiver);
    }
Exemple #5
0
    void OnApplyGene(GeneData gene)
    {
        if (!GetIsApplicable(gene))
        {
            return;
        }

        switch (gene.GeneType.ToLower())
        {
        case "bone":
            AddBone(gene);
            break;

        case "joint":
            AddJoint(gene);
            break;

        case "stats":
            AddStats(gene);
            break;

        default:
            break;
        }
    }
        /// <summary>
        /// Reset the specified teamName and material.
        /// </summary>
        /// <param name="teamName">Team name.</param>
        /// <param name="material">Material.</param>
        public void Reset(string teamName, Color color)
        {
            MeshRenderer[] renderComponents = this.gameObject.GetComponents <MeshRenderer>();
            if (renderComponents.Length != 1)
            {
                LogUtility.LogErrorFormat("Object had {0} render components", renderComponents.Length);
            }

            this.transform.localScale =
                new Vector3(
                    StaticController.GlobalShapeHorizontalScale,
                    this.transform.localScale.y,
                    StaticController.GlobalShapeHorizontalScale);

            renderComponents[0].material.color = color;

            this.gameObject.SetActive(true);

            this.TeamName = teamName;

            this.genome = GenePoolManager.GetData(teamName);

            // Key read only variables
            this.Health           = InitialHealth;
            this.AttackStrength   = InitialAttackStrength;
            this.GrowFoodStrength = InitialGrowFoodStrength;

            // Key read write variables
            this.HelperInt1           = 0;
            this.HelperInt2           = 0;
            this.HelperBool1          = 0;
            this.HelperBool2          = 0;
            this.HelperGridDirection1 = (byte)GridDirectionEnum.North;
            this.HelperGridDirection2 = (byte)GridDirectionEnum.North;
        }
Exemple #7
0
        private void FixAllStagesFromGeneLogic(Beatmap map, GeneData gene)
        {
            int count = map.Stages.Count;

            for (int i = 0; i < count; i++)
            {
                FixStageLogic(map, gene, map.Stages[i], i);
            }
        }
Exemple #8
0
 private void FixTimingLogic(GeneData gene, Beatmap.Timing timing)
 {
     if (!gene.TimingAccessable)
     {
         timing.Speed    = 1f;
         timing.TimingID = 0;
     }
     if (!gene.SfxAccessable)
     {
         timing.soundFxIndex = 0;
     }
 }
    GeneData Breed(GeneData genes1, GeneData genes2)
    {
        GeneData child = new GeneData();

        //TODO: Optimize with reflection
        child.dropX    = ROOZ ? genes1.dropX : genes2.dropX;
        child.dropY    = ROOZ ? genes1.dropY : genes2.dropY;
        child.mass     = ROOZ ? genes1.mass : genes2.mass;
        child.wheels   = ROOZ ? genes1.wheels : genes2.wheels;
        child.shooters = ROOZ ? genes1.shooters : genes2.shooters;

        return(child);
    }
    // Update is called once per frame
    void Update()
    {
        if (manager.playing)
        {
            perGeneration = manager.quizAnswers.population;

            //Update Timer
            counter += Time.deltaTime;

            if (counter > timeLimit)
            {
                counter = 0;
                manager.Reset();

                //End of generation - reset stuff and sort the list to cull the shit ones
                if (currentMember == perGeneration)
                {
                    currentGeneration++;
                    currentMember = 0;
                    manager.SortPerformance();
                    manager.CullBottom();
                }

                currentMember++;
                if (currentBeing)
                {
                    Destroy(currentBeing);
                }

                if (currentGeneration == 1)
                {
                    //Generate a random population
                    GeneData newGenes = GenerateRandomGeneData();
                    ParseGeneData(newGenes);
                    manager.currentGenes = newGenes;
                }
                else
                {
                    //Breed them randomly
                    GeneData newGenes = Breed(manager.performance[Random.Range(0, manager.performance.Count)].genes, manager.performance[Random.Range(0, manager.performance.Count)].genes);
                    //25% chance of mutation
                    ParseGeneData(ROTF ? Mutate(newGenes) : newGenes);
                    manager.currentGenes = newGenes;
                }
            }

            //Update UI
            infoText.text = "Generation: " + currentGeneration + "\n" + "Member: " + currentMember;
        }
    }
Exemple #11
0
        private void FixNoteLogic(GeneData gene, Beatmap.Note note)
        {
            var config = GetNoteConfig(gene, note.trackIndex);

            if (config.UseConfig)
            {
                if (config.ItemType.Active)
                {
                    note.itemType = config.ItemType.Value;
                }
                if (config.X.Active)
                {
                    note.x = config.X.Value;
                }
                if (config.Z.Active)
                {
                    note.z = config.Z.Value;
                }
                if (config.Duration.Active)
                {
                    note.duration = config.Duration.Value;
                }
                if (config.Width.Active)
                {
                    note.width = config.Width.Value;
                }
                if (config.LinkedNoteIndex.Active)
                {
                    note.linkedNoteIndex = config.LinkedNoteIndex.Value;
                }
                if (!gene.SfxAccessable)
                {
                    note.soundFxIndex = 0;
                }
                if (config.ClickSound.Active)
                {
                    note.clickSoundIndex = (short)config.ClickSound.Value;
                }
                if (!gene.TimingAccessable)
                {
                    note.TimingID = 0;
                }
            }
        }
Exemple #12
0
 private void AddJoint(GeneData gene)
 {
     //        float x, y;
     //        gene.FloatModifiers.TryGetValue("X", out x);
     //        gene.FloatModifiers.TryGetValue("Y", out y);
     //
     //        var slot = new ChildSlot { X = x, Y = y };
     //
     //        var newBody = (GameObject)Instantiate(BonePrefab,
     //                  gameObject.transform.position + new Vector3(slot.X, slot.Y),
     //                  gameObject.transform.rotation);
     //        newBody.transform.parent = gameObject.transform;
     //        newBody.SetActive(true);
     //
     //        var spring = newBody.AddComponent<SpringJoint2D>();
     //        spring.connectedBody = gameObject.GetComponent<Rigidbody2D>();
     //        spring.distance = 0.1f;
     //        spring.dampingRatio = 0.01f;
     //        spring.frequency = 10.0f;
     //
     //		gameObject.SendMessage("OnChildAdded", newBody, SendMessageOptions.DontRequireReceiver);
 }
    GeneData GenerateRandomGeneData()
    {
        GeneData genes = new GeneData();

        genes.dropX = Random.Range(bounds_DropX.x, bounds_DropX.y);
        genes.dropY = Random.Range(bounds_DropY.x, bounds_DropY.y);

        genes.mass = Random.Range(bounds_Mass.x, bounds_Mass.y);

        int numberOfWheels = Mathf.Clamp(Random.Range(-wheelLimit, wheelLimit), 0, wheelLimit);

        for (int i = 0; i < numberOfWheels; i++)
        {
            Wheel wheel = new Wheel();
            wheel.radius    = Random.Range(bounds_WheelRadius.x, bounds_WheelRadius.y);
            wheel.position  = new Vector3(Random.Range(bounds_WheelPosition.x, bounds_WheelPosition.y), Random.Range(bounds_WheelPosition.x, bounds_WheelPosition.y), transform.position.z);
            wheel.spinSpeed = Random.Range(bounds_WheelSpeed.x, bounds_WheelSpeed.y);
            genes.wheels.Add(wheel);
        }


        int numberOfShooters = Mathf.Clamp(Random.Range(-shooterLimit, shooterLimit), 0, shooterLimit);

        for (int i = 0; i < numberOfShooters; i++)
        {
            Shooter shooter = new Shooter();
            //We could make this point away using the dot product (do this if you want) but I want it randomererererer(TM)
            shooter.rotation   = Quaternion.Euler(new Vector3(0, 0, Random.Range(0, 359)));
            shooter.position   = new Vector3(Random.Range(bounds_ShooterPosition.x, bounds_ShooterPosition.y), Random.Range(bounds_ShooterPosition.x, bounds_ShooterPosition.y), transform.position.z);
            shooter.shootForce = Random.Range(bounds_ShooterForce.x, bounds_ShooterForce.y);
            shooter.shootDelay = Random.Range(bounds_ShooterDelay.x, bounds_ShooterDelay.y);
            shooter.ammo       = (int)Random.Range(bounds_ShooterAmmo.x, bounds_ShooterAmmo.y);
            shooter.ammoType   = Random.Range(0, 100);
            genes.shooters.Add(shooter);
        }

        return(genes);
    }
Exemple #14
0
 private void AddStats(GeneData gene)
 {
     throw new System.NotImplementedException();
 }
    GeneData Mutate(GeneData genes)
    {
        GeneData mutant = genes;

        //TODO: Optimize with reflection

        float dropXMutation = (ROOZ ? -1 : 1) * Random.Range(0, (bounds_DropX.y - bounds_DropX.x)) / 10;

        mutant.dropX += (ROTF ? dropXMutation : 0);

        float dropYMutation = (ROOZ ? -1 : 1) * Random.Range(0, (bounds_DropY.y - bounds_DropY.x)) / 10;

        mutant.dropY += (ROTF ? dropYMutation : 0);

        float massMutation = (ROOZ ? -1 : 1) * Random.Range(0, (bounds_Mass.y - bounds_Mass.x)) / 100;

        mutant.mass += (ROTF ? massMutation : 0);

        //Wheels  =====================
        //We add or remove wheels randomly 25%

        if (ROTF)
        {
            int wheelNumber = mutant.wheels.Count + (ROOZ ? -1 : 1);

            if (wheelNumber > mutant.wheels.Count)
            {
                //If there are now more wheels add a wheel
                Wheel wheel = new Wheel();
                wheel.radius    = Random.Range(bounds_WheelRadius.x, bounds_WheelRadius.y);
                wheel.position  = new Vector3(Random.Range(bounds_WheelPosition.x, bounds_WheelPosition.y), Random.Range(bounds_WheelPosition.x, bounds_WheelPosition.y), transform.position.z);
                wheel.spinSpeed = Random.Range(bounds_WheelSpeed.x, bounds_WheelSpeed.y);
                mutant.wheels.Add(wheel);
            }
            else
            {
                //Otherwise remove a random wheel
                mutant.wheels.RemoveAt(Random.Range(0, mutant.wheels.Count));
            }
        }

        //Mutate wheel settings randomly
        foreach (Wheel wheel in mutant.wheels)
        {
            float wheelRadiusMutation = (ROOZ ? -1 : 1) * Random.Range(0, (bounds_WheelRadius.y - bounds_WheelRadius.x)) / 10;
            if (wheel.radius + wheelRadiusMutation > 0)
            {
                wheel.radius += (ROTN ? wheelRadiusMutation : 0);
            }

            Vector3 wheelPositionMutation = new Vector3((ROOZ ? -1 : 1) * Random.Range(0, (bounds_WheelPosition.y - bounds_WheelPosition.x)) / 10, (ROTF ? -1 : 1) * Random.Range(0, (bounds_WheelPosition.y - bounds_WheelPosition.x)) / 10, 0);
            wheel.position += (ROTN ? wheelPositionMutation : Vector3.zero);

            float wheelSpeedMutation = (ROOZ ? -1 : 1) * Random.Range(0, (bounds_WheelSpeed.y - bounds_WheelSpeed.x)) / 10;
            wheel.spinSpeed += (ROTN ? wheelSpeedMutation : 0);
        }

        foreach (Shooter shooter in mutant.shooters)
        {
            float shooterShootForceMutation = (ROOZ ? -1 : 1) * Random.Range(0, (bounds_ShooterForce.y - bounds_ShooterForce.x)) / 10;
            shooter.shootForce += (ROTN ? shooterShootForceMutation : 0);

            float shooterShootDelayMutation = (ROOZ ? -1 : 1) * Random.Range(0, (bounds_ShooterDelay.y - bounds_ShooterDelay.x)) / 10;
            shooter.shootDelay += (ROTN ? shooterShootDelayMutation : 0);

            Vector3 shooterPositionMutation = new Vector3((ROOZ ? -1 : 1) * Random.Range(0, (bounds_ShooterPosition.y - bounds_ShooterPosition.x)) / 10, (ROTF ? -1 : 1) * Random.Range(0, (bounds_ShooterPosition.y - bounds_ShooterPosition.x)) / 10, 0);
            shooter.position += (ROTN ? shooterPositionMutation : Vector3.zero);

            int shooterAmmoMutation = (ROOZ ? -1 : 1) * (int)Random.Range(0, (bounds_ShooterAmmo.y - bounds_ShooterAmmo.x)) / 10;
            shooter.ammo += (ROTN ? shooterAmmoMutation : 0);

            int shooterAmmoTypeMutation = Random.Range(1, 100);
            shooter.ammoType += (ROTN ? shooterAmmoTypeMutation : 0);

            //This is special bc literally no one thinks in quaternions (except for hamilton). Note how we are simply assigning directly instead of adding anything
            Quaternion shooterRotationMutation = Quaternion.Euler(shooter.rotation.eulerAngles + new Vector3(0, 0, Random.Range(0, 359)));
            shooter.rotation = (ROTN ? shooterRotationMutation :  shooter.rotation);
        }


        return(mutant);
    }
Exemple #16
0
        private void FixStageLogic(Beatmap map, GeneData gene, Beatmap.Stage stage, int index)
        {
            var config = GetStageConfig(gene, index);

            if (config.UseConfig)
            {
                if (config.Time_Real.Active)
                {
                    stage.time = (int)(config.Time_Real.Value * GetMusicDuration());
                }
                if (config.Duration_Real.Active)
                {
                    stage.duration = (int)(config.Duration_Real.Value * GetMusicDuration());
                }
                if (config.ItemType.Active)
                {
                    stage.itemType = config.ItemType.Value;
                }
                if (config.X.Active)
                {
                    stage.x = config.X.Value;
                }
                if (config.Y.Active)
                {
                    stage.y = config.Y.Value;
                }
                if (config.Rotation.Active)
                {
                    stage.rotation = config.Rotation.Value;
                }
                if (config.Width.Active)
                {
                    stage.width = config.Width.Value;
                }
                if (config.Height.Active)
                {
                    stage.height = config.Height.Value;
                }
                if (config.PivotY.Active)
                {
                    stage.pivotY = config.PivotY.Value;
                }
                if (config.Speed.Active)
                {
                    stage.speed = config.Speed.Value;
                }
                if (!config.Motion_Pos)
                {
                    stage.positions.Clear();
                }
                if (!config.Motion_Rot)
                {
                    stage.rotations.Clear();
                }
                if (!config.Motion_Width)
                {
                    stage.widths.Clear();
                }
                if (!config.Motion_Height)
                {
                    stage.heights.Clear();
                }
                if (!config.Motion_Color)
                {
                    stage.colors.Clear();
                }
                // Tile Tracks
                if (config.TileTrack)
                {
                    int currentTrackIndex = 0;
                    int trackCount        = map.Tracks.Count;
                    int trackCountInStage = 0;
                    for (int i = 0; i < trackCount; i++)
                    {
                        if (map.Tracks[i].StageIndex == index)
                        {
                            trackCountInStage++;
                        }
                    }
                    for (int i = 0; i < trackCount && trackCountInStage > 0; i++)
                    {
                        var track = map.Tracks[i];
                        if (track.StageIndex != index)
                        {
                            continue;
                        }
                        int trackWidth = 1000 / trackCountInStage;
                        track.width = trackWidth;
                        track.x     = (int)(((float)currentTrackIndex / trackCountInStage) * 1000f + (trackWidth / 2f));
                        currentTrackIndex++;
                    }
                }
                // Add Track For Trackless Stage
                if (!gene.TrackAccessable)
                {
                }
            }
        }
Exemple #17
0
 private GeneData.TrackConfig GetTrackConfig(GeneData gene, int index) => index >= 0 && index < gene.StaticConfigs_Track.Length ? gene.StaticConfigs_Track[index] : gene.Config_Track;
Exemple #18
0
        private void FixTrackLogic(Beatmap map, GeneData gene, Beatmap.Track track, int index)
        {
            var config = GetTrackConfig(gene, index);

            if (config.UseConfig)
            {
                bool tiling = GetStageConfig(gene, track.stageIndex).TileTrack;

                if (config.Time_Real.Active)
                {
                    track.time = (int)(config.Time_Real.Value * GetMusicDuration());
                }
                if (config.Duration_Real.Active)
                {
                    track.duration = (int)(config.Duration_Real.Value * map.GetDuration(0, track.stageIndex));
                }
                if (config.ItemType.Active)
                {
                    track.itemType = config.ItemType.Value;
                }
                if (config.X.Active && !tiling)
                {
                    track.x = config.X.Value;
                }
                if (config.Angle.Active)
                {
                    track.angle = config.Angle.Value;
                }
                if (config.Width.Active && !tiling)
                {
                    track.width = config.Width.Value;
                }
                if (config.Speed.Active)
                {
                    track.speed = config.Speed.Value;
                }
                if (config.StageIndex.Active)
                {
                    track.stageIndex = config.StageIndex.Value;
                }
                if (config.HasTray.Active)
                {
                    track.hasTray = config.HasTray.Value == 1;
                }
                if (!config.Motion_X)
                {
                    track.xs.Clear();
                }
                if (!config.Motion_Angle)
                {
                    track.angles.Clear();
                }
                if (!config.Motion_Width)
                {
                    track.widths.Clear();
                }
                if (!config.Motion_Color)
                {
                    track.colors.Clear();
                }
            }
        }
Exemple #19
0
 private GeneData.StageConfig GetStageConfig(GeneData gene, int index) => index >= 0 && index < gene.StaticConfigs_Stage.Length ? gene.StaticConfigs_Stage[index] : gene.Config_Stage;
Exemple #20
0
 private GeneData.NoteConfig GetNoteConfig(GeneData gene, int configIndex) => configIndex >= 0 && configIndex < gene.StaticConfigs_Note.Length ? gene.StaticConfigs_Note[configIndex] : gene.Config_Note;