示例#1
0
    void Initialize()
    {
        // Initialize environment
        Universe.root = new Universe();
        BrainRepository repo = new BrainRepository();

        // Characters
        Crewmember[] characters = GetCharacters();

        // Link each character to the central repository
        foreach (Character character in characters)
        {
            character.Repository = repo;
        }

        /* Actions */

        if (!LoadActions("observations.txt", "analyses.txt", repo))
        {
            Debug.LogError("Mafia: Invalid Action data");
            return;
        }

        //List<string> actionIDs = new List<string>(repo.GetActionIDs());

        /* Attributes (Traits, States) */

        Trait.RegisterFactors(repo, Distributions.Normal());

        List <State> states = new List <State>();

        // The higher the anger, the rasher the actions
        State anger =
            new State(
                "anger", () => Random.Range(.1f, .25f),
                Transformations.EaseSquared()
                );

        repo.RegisterState(anger);
        states.Add(anger);

        // The higher the stress, the more likely people will be suspicious of
        // each other
        State stress
            = new State(
                  "stress", () => Random.Range(.1f, .25f),
                  Transformations.EaseSquared()
                  );

        repo.RegisterState(stress);
        states.Add(stress);

        /*
         * // The higher the confusion, the more likely energy will needlessly go down
         * State confusion
         * = new State(
         * "confusion", () => Random.Range(.1f, .25f),
         * Transformations.EaseSquared()
         * );
         * repo.RegisterState(confusion);
         * states.Add(confusion);
         */

        /* Relationships */

        /**
         * Creates the initial alignments for each character for what increases/
         * decreases trust/agreement. The actual relationships are taken care of
         * as characters perform interactions and other characters observe them.
         */
        for (int i = 0; i < characters.Length; ++i)
        {
            var c = characters[i];

            List <State> negTrust = new List <State>();
            List <State> posTrust = new List <State>();
            List <State> posAgree = new List <State>();
            List <State> negAgree = new List <State>();

            //states[indexer];

            if (i < ((characters.Length) / 2))
            {
                negTrust.Add(repo.GetState("anger"));
                negAgree.Add(repo.GetState("anger"));
                posTrust.Add(repo.GetState("stress"));
                posAgree.Add(repo.GetState("stress"));
            }
            else
            {
                if (i < ((characters.Length * 3) / 4))
                {
                    negTrust.Add(repo.GetState("stress"));
                    negAgree.Add(repo.GetState("anger"));
                    posTrust.Add(repo.GetState("anger"));
                    posAgree.Add(repo.GetState("stress"));
                }
                else
                {
                    negTrust.Add(repo.GetState("stress"));
                    negAgree.Add(repo.GetState("stress"));
                    posTrust.Add(repo.GetState("anger"));
                    posAgree.Add(repo.GetState("anger"));
                }
            }

            c.agreementAffinities.RegisterPositive(posAgree);
            c.agreementAffinities.RegisterNegative(negAgree);

            c.trustAffinities.RegisterPositive(posTrust);
            c.trustAffinities.RegisterNegative(negTrust);
        }

        /* Effects */

        foreach (IAttribute prototype in repo.AttributePrototypes)
        {
            if (prototype is State)
            {
                for (float o = -MAX_OFFSET_ABS; o < (MAX_OFFSET_ABS * 5f) / 4f; o += (MAX_OFFSET_ABS / 2))
                {
                    if (o == 0)
                    {
                        continue;
                    }

                    State state = prototype as State;

                    repo.Effects.Add(
                        new InfluencedEffect(
                            (o > 0 ? "raise-" : "lower-")
                            + state.name + "-"
                            + (Mathf.Abs(o) > (MAX_OFFSET_ABS * 3 / 4) ? "strong" : "weak"),
                            TraitLinksFromState(state, repo),
                            LS(state),
                            LS(repo.MOD(state.name, o))
                            )
                        );
                }
            }
        }

        /* Interactions */
        var stress_on_self =
            new InfluencedInteraction(
                0,
                LS(
                    repo.GetTrait(Factor.NEUROTICISM),
                    repo.GetTrait(Factor.AGREEABLENESS)
                    ),
                LS(
                    repo.GetState("stress")
                    ),
                repo.GetAction("stress_on_self")
                );

        stress_on_self.SetDebugLabel("stress_on_self");
        repo.RegisterInteraction(stress_on_self);

        var stress_on_other =
            new InfluencedInteraction(
                1,
                LS(
                    repo.GetTrait(Factor.NEUROTICISM)
                    ),
                LS(
                    repo.GetState("stress")
                    ),
                repo.GetAction("stress_on_other")
                );

        stress_on_other.SetDebugLabel("stress_on_other");
        repo.RegisterInteraction(stress_on_other);

        var anger_on_other =
            new InfluencedInteraction(
                1,
                LS(
                    repo.GetTrait(Factor.AGREEABLENESS)
                    ),
                LS(
                    repo.GetState("anger")
                    ),
                repo.GetAction("anger_on_other")
                );

        anger_on_other.SetDebugLabel("anger_on_other");
        repo.RegisterInteraction(anger_on_other);

        /*
         * var confusion_on_self =
         * new InfluencedInteraction(
         *  0,
         *  LS(
         *    repo.GetTrait(Factor.CONSCIENTIOUSNESS)
         *  ),
         *  LS(
         *    repo.GetState("confusion")
         *  ),
         *  repo.GetAction("confusion_on_self")
         * );
         * stress_on_self.SetDebugLabel("confusion_on_self");
         * repo.RegisterInteraction(confusion_on_self);
         */

        // Attribution
        foreach (Character character in characters)
        {
            character.Subscribe();
            Universe.root.entities.Add(character);
        }


        // Manually force certain attributes
        for (int i = 0; i < characters.Length; ++i)
        {
            var crewmember = characters[i];
            foreach (var attr in crewmember.GetAttributeInstances())
            {
                var instance = attr as State.TransformedInstance;
                if (attr.Prototype.Equals(repo.GetState("anger")))
                {
                    if (i < ((characters.Length) / 2))
                    {
                        instance.State = 0f;
                    }
                    else
                    {
                        if (i < ((characters.Length * 3) / 4))
                        {
                            instance.State = Random.Range(.65f, 7f);
                        }
                        else
                        {
                            instance.State = Random.Range(.85f, .95f);
                        }
                    }
                }
                if (attr.Prototype.Equals(repo.GetState("stress")))
                {
                    if (i > (characters.Length / 2) && i < ((characters.Length * 3) / 4))
                    {
                        instance.State = Random.Range(.4f, .6f);
                    }
                }
            }
        }

        // Unity hooks
        ComponentManager hook = GetComponent <ComponentManager>();

        hook.Hook("Universe.root", Universe.root);
        hook.Hook <BrainRepoComponent>("brain-repo", repo);
    }
    void Awake()
    {
        // Initialize environment
        Universe.root = new Universe();
        BrainRepository repo = new BrainRepository();

        // Characters
        Character[] characters =
        {
            new Character("Jurgen"),
            new Character("Francis"),
            new Character("Eugene")
        };

        // Link each character to the central repository
        foreach (Character character in characters)
        {
            character.Repository = repo;
        }

        /* Actions */

        /*
         * ConsoleActionReader.LoadFile(
         * DATAPATH + "/" + FILENAME, repo
         * );
         */

        List <string> actionIDs = new List <string>(repo.GetActionIDs());

        /* Attributes (Traits, States) */

        Trait.RegisterFactors(repo, Distributions.Normal());

        repo.RegisterState(
            new State(
                "anger",
                Distributions.Uniform(),
                Transformations.EaseSquared()
                )
            );

        repo.RegisterState(
            new State(
                "confusion",
                Distributions.Uniform(),
                Transformations.InvertedSquared()
                )
            );

        repo.RegisterState(
            new State(
                "energy", () => 1,
                Transformations.Linear()
                )
            );

        /* Effects */

        repo.Effects.Add(
            new InfluencedEffect(
                "effect-example",
                LS(repo.GetTrait(Factor.CONSCIENTIOUSNESS)),
                LS(repo.GetState("anger")),
                LS(repo.MOD("confusion", .3f))
                )
            );

        /* Interactions */

        repo.RegisterInteraction(
            new InfluencedInteraction(
                0,
                LS(repo.GetTrait(Factor.AGREEABLENESS)),
                LS(repo.GetState("confusion")),
                repo.GetAction(actionIDs[Random.Range(0, actionIDs.Count)])
                )
            );

        repo.RegisterInteraction(
            new InfluencedInteraction(
                1,
                LS(repo.GetTrait(Factor.NEUROTICISM)),
                LS(repo.GetState("energy")),
                repo.GetAction(actionIDs[Random.Range(0, actionIDs.Count)])
                )
            );

        // Attribution
        foreach (Character character in characters)
        {
            character.Subscribe();
            Universe.root.entities.Add(character);
        }

        // Unity hooks
        ComponentManager hook = GetComponent <ComponentManager>();

        hook.Hook("Universe.root", Universe.root);
        hook.Hook <BrainRepoComponent>("brain-repo", repo);
    }