Пример #1
0
        public void AddUse(BehaviorUse newValue)
        {
            Debug.Assert(!newValue.id.IsNullOrEmpty());
            List <BehaviorUse> useList = new List <BehaviorUse>(behaviorUses);

            useList.Add(newValue);
            behaviorUses = useList.ToArray();
        }
Пример #2
0
 private void SetPlayerNumberOnPlayerControlsPanel(VoosActor actor, Behaviors.BehaviorUse use, int playerNumber)
 {
     use = use.DeepClone();
     use.SetPropertyValue(PLAYER_NUMBER_PROP_NAME, playerNumber);
     Behaviors.Brain brain = actor.GetBehaviorSystem().GetBrain(actor.GetBrainName());
     if (brain == null)
     {
         Debug.LogErrorFormat("Could not set player# on actor {0} ({1}). No brain.", actor.GetName(), actor.GetDisplayName());
         return;
     }
     brain.SetUse(use);
     actor.GetBehaviorSystem().PutBrain(actor.GetBrainName(), brain);
 }
Пример #3
0
    private void AssignPlayerToActorLocal(int playerNumber, string actorName)
    {
        foreach (VoosActor actor in engine.EnumerateActors())
        {
            // Only act on locally owned actors (all clients are running this method by the miracle of RPCs,
            // so each client will act on the actors they own).
            if (!actor.IsLocallyOwned())
            {
                continue;
            }

            Behaviors.BehaviorUse playerControlsPanel = TryGetPlayerControlsPanel(actor);
            if (playerControlsPanel == null)
            {
                continue;
            }

            if (playerNumber != ASSIGN_ACTOR_AS_NPC)
            {
                // If this actor is set to this player#, set it 0 unless it's the desired actor.
                // If this actor is the desired actor, set its player#.
                int thisPlayerNumber = playerControlsPanel.GetPropertyValue <int>(PLAYER_NUMBER_PROP_NAME, PLAYER_NUMBER_PROP_DEFAULT_VALUE);
                if (thisPlayerNumber == playerNumber && actor.GetName() != actorName)
                {
                    SetPlayerNumberOnPlayerControlsPanel(actor, playerControlsPanel, 0);
                }
                else if (thisPlayerNumber != playerNumber && actor.GetName() == actorName)
                {
                    SetPlayerNumberOnPlayerControlsPanel(actor, playerControlsPanel, playerNumber);
                }
            }
            else
            {
                // Our mission is just to make the desired actor an NPC.
                if (actor.GetName() == actorName)
                {
                    DeletePlayerControlsPanel(actor);
                }
            }
        }
    }
Пример #4
0
 private void DeletePlayerControlsPanel(VoosActor actor)
 {
     Debug.Assert(actor.IsLocallyOwned(), "Actor should be locally owned");
     Behaviors.BehaviorUse playerControlsPanel = TryGetPlayerControlsPanel(actor);
     if (playerControlsPanel == null)
     {
         return;
     }
     Behaviors.Brain brain = actor.GetBehaviorSystem().GetBrain(actor.GetBrainName());
     if (brain == null)
     {
         Debug.LogErrorFormat("Could not set player# on actor {0} ({1}). No brain.", actor.GetName(), actor.GetDisplayName());
         return;
     }
     // PLAYER_CONTROLS_PANEL_HAS_NO_DECKS_ASSUMPTION
     // WARNING: this is a naive delete that doesn't recursively look for behavior uses mentioned
     // in any decks used by the Player Controls panel, so if in the future we do add decks to it,
     // we need to update this logic to remove the panel properly.
     brain.DeleteUse(playerControlsPanel.id);
     actor.GetBehaviorSystem().PutBrain(actor.GetBrainName(), brain);
 }
Пример #5
0
            public void PerformUpgrades(HashSet <string> brainIdsUsedByActors)
            {
                if (version < FirstVersionWithBrains)
                {
                    Debug.Assert(version == FirstVersionWithBrains - 1);
                    version = FirstVersionWithBrains;

                    Debug.Assert(brainIds.Length == 0);
                    Debug.Assert(brains.Length == 0);

                    HashSet <string> brainIdsToAdd = new HashSet <string>();
                    foreach (LegacyBehaviorUse use in behaviorUses)
                    {
                        brainIdsToAdd.Add(use.brainId);
                    }

                    brainIds = new string[brainIdsToAdd.Count];
                    brains   = new Brain[brainIdsToAdd.Count];
                    for (int i = 0; i < brains.Length; i++)
                    {
                        brains[i] = new Brain();
                    }
                    brainIdsToAdd.CopyTo(brainIds);
                }

                if (version < FirstVersionWithDefaultBehavior)
                {
                    Debug.Assert(version == FirstVersionWithDefaultBehavior - 1);
                    version = FirstVersionWithDefaultBehavior;

                    // Add Default Behavior to every brain.

                    HashSet <string> brainIds = new HashSet <string>();
                    foreach (var use in behaviorUses)
                    {
                        brainIds.Add(use.brainId);
                    }

                    List <string> useIds = new List <string>();
                    useIds.AddRange(behaviorUseIds);
                    List <LegacyBehaviorUse> uses = new List <LegacyBehaviorUse>();
                    uses.AddRange(behaviorUses);

                    foreach (string brainId in brainIds)
                    {
                        useIds.Add(Behaviors.Database.NewUID());
                        uses.Add(new LegacyBehaviorUse {
                            brainId = brainId, behaviorUri = "builtin:Default Behavior", propertyAssignments = new Behaviors.PropertyAssignment[0]
                        });
                    }

                    behaviorUseIds = useIds.ToArray();
                    behaviorUses   = uses.ToArray();
                }

                if (version < FirstVersionWithRequiredBrains)
                {
                    HashSet <string> existingBrains     = new HashSet <string>(brainIds);
                    HashSet <string> referencedBrainIds = new HashSet <string>(behaviorUses.Select(use => use.brainId));
                    List <string>    brainIdsList       = new List <string>(brainIds);
                    List <Brain>     brainsList         = new List <Brain>(brains);
                    // Add empty brain for every non-existant brain.
                    foreach (string brainId in referencedBrainIds.Except(existingBrains))
                    {
                        brainIdsList.Add(brainId);
                        brainsList.Add(new Brain());
                    }
                    brainIds = brainIdsList.ToArray();
                    brains   = brainsList.ToArray();

                    // Upgrade done
                    version = FirstVersionWithRequiredBrains;
                }

                if (version < FirstVersionWithoutUseTable)
                {
                    Debug.Assert(behaviorUses != null, "Before FirstVersionWithoutUseTable, should have non-null behavior uses");

                    // We now also require brains to exists, even if they're not
                    // referenced by a use (ie. only referenced by an actor).
                    if (brainIdsUsedByActors.Count > 0)
                    {
                        HashSet <string> brainIdsSet  = new HashSet <string>(brainIds);
                        List <string>    brainIdsList = new List <string>(brainIds);
                        List <Brain>     brainsList   = new List <Brain>(brains);
                        foreach (string brainId in brainIdsUsedByActors.Except(brainIdsSet))
                        {
                            brainIdsList.Add(brainId);
                            brainsList.Add(new Brain());
                        }
                        brainIds = brainIdsList.ToArray();
                        brains   = brainsList.ToArray();
                    }

                    // Now convert everything to "uses-in-brains" form.
                    Dictionary <string, int> brainIdToIndex = new Dictionary <string, int>();
                    for (int i = 0; i < brainIds.Length; i++)
                    {
                        string brainId = brainIds[i];
                        Brain  brain   = brains[i];

                        Debug.Assert(behaviorUses != null);

                        List <int> useIndexes = new List <int>(
                            from useIndex in Enumerable.Range(0, behaviorUses.Length)
                            where behaviorUses[useIndex].brainId == brainId
                            select useIndex);

                        brain.behaviorUses = useIndexes.Select(index =>
                        {
                            LegacyBehaviorUse use = behaviorUses[index];
                            var newUse            = new BehaviorUse
                            {
                                id                  = behaviorUseIds[index],
                                behaviorUri         = use.behaviorUri,
                                metadataJson        = use.metadataJson,
                                propertyAssignments = use.propertyAssignments.DeepClone()
                            };
                            return(newUse);
                        }).ToArray();
                    }
                    this.behaviorUseIds = new string[0];
                    this.behaviorUses   = new LegacyBehaviorUse[0];
                    // Upgrade done
                    version = FirstVersionWithoutUseTable;
                }

                AssertValid();
            }
Пример #6
0
 public void SetUse(BehaviorUse newValue)
 {
     behaviorUses[IndexOfUse(newValue.id)] = newValue;
 }