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); }
public void PutBrain(string brainId, Behaviors.Brain brain) { // Follow predicted-last-writer-wins pattern. var req = new PutRequest <Behaviors.Brain> { id = brainId, value = brain, seqNum = putBrainSeqNum }; putBrainSeqNum++; unackedPutBrains.Enqueue(req); Util.Log($"public PutBrain, numUses: {req.value.behaviorUses.Length}"); PutBrainLocal(req); photonView.RPC("PutBrainRPC", PhotonTargets.AllViaServer, JsonUtility.ToJson(req)); }
private static Behaviors.BehaviorUse TryGetPlayerControlsPanel(VoosActor actor) { Behaviors.Brain brain = actor.GetBehaviorSystem().GetBrain(actor.GetBrainName()); if (brain == null) { return(null); } foreach (Behaviors.BehaviorUse use in actor.GetBehaviorSystem().GetBrain(actor.GetBrainName()).GetUses()) { if (use.behaviorUri == PLAYER_CONTROLS_PANEL_URI) { return(use); } } return(null); }
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); }
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(); }