protected override IList <Effect> Observation( Interaction interaction, IEntity host, ICollection <IEntity> targets ) { Character character = host as Character; if (character == null) { return(null); } InfluencedInteraction influencedInteraction = interaction as InfluencedInteraction; // Black box var effects = oracle.ObservationEffects( influencedInteraction, character, targets, BrainRepo ); /* Relationship (with host) */ UpdateRelationship(character, influencedInteraction, effects); return(effects); }
public IList <Effect> ObservationEffects( InfluencedInteraction interaction, Character host, ICollection <IEntity> targets, BrainRepository repo ) { var effects = ChooseStronglyInfluencedEffects(repo, interaction, host); //TODO: Change selection via host/other methods; with the addtion of the //relationship module, this seems more reasonable return(effects); }
public IList <Effect> ReactionEffects( InfluencedInteraction i, Character host, // Unused BrainRepository repo ) { var effects = ChooseStronglyInfluencedEffects(repo, i, host); //TODO: Change selection via host/other methods; with the addtion of the //relationship module, this seems more reasonable return(effects); }
protected void UpdateRelationship( Character character, InfluencedInteraction influencedInteraction, IList <Effect> effects ) { Relationship withHost = GetRelationship(character); if (withHost == null) { withHost = CreateRelationship( character, influencedInteraction ); } float trustOffset = 0, agreementOffset = 0; foreach (Effect effect in effects) { var e = effect as InfluencedEffect; if (e == null) { continue; } // Check for matches foreach (string name in e.strongStateInfluences.Keys) { var matchTrust = trustAffinities.Match(name); var matchAgreement = agreementAffinities.Match(name); float trust = matchTrust > 0 ? RELATIONSHIP_OFFSET : matchTrust < 0 ? -RELATIONSHIP_OFFSET : 0; float agreement = matchAgreement > 0 ? RELATIONSHIP_OFFSET : matchAgreement < 0 ? -RELATIONSHIP_OFFSET : 0; withHost.trust.Offset(trust); withHost.agreement.Offset(agreement); trustOffset += trust; agreementOffset += agreement; } } // Event OnUpdateRelationshipEventHandler handler = OnUpdateRelationship; if (handler != null) { handler(this, character, trustOffset, agreementOffset, withHost); } }
protected override IList <Effect> Reaction( Interaction interaction, IEntity host ) { Character hostCharacter = host as Character; InfluencedInteraction influencedInteraction = interaction as InfluencedInteraction; // Black box var effects = oracle.ReactionEffects( influencedInteraction, hostCharacter, BrainRepo ); if (this != hostCharacter) { UpdateRelationship(hostCharacter, influencedInteraction, effects); } return(effects); }
public float ComboScore( IEnumerable <State> state, Character host, InfluencedInteraction interaction, ICollection <IEntity> targets, BrainRepository repo ) { float score = 0f; int count = 0; foreach (State s in interaction.strongStateInfluences.Values) { State.TransformedInstance currState = host[s] as State.TransformedInstance; if (currState == null) { continue; } score = currState.TransformedState; foreach (State t in state) { if (t.Equals(s)) { score += .25f; } } count++; } if (count != 0) { score /= count; } //BehaviorEngine.Debug.Logger.Log("Calculated score"); score *= StabilityScore(state, host, interaction, targets, repo); return(score); }
private IList <Effect> ChooseStronglyInfluencedEffects( BrainRepository repo, InfluencedInteraction i, Character host ) { // Return value List <Effect> effects = new List <Effect>(); List <InfluencedEffect> unused = new List <InfluencedEffect>(); List <int> differentials = new List <int>(); var allEffects = new List <InfluencedEffect>(repo.Effects); // Guarantee that default Effect will be different every time Shuffle(allEffects); int position = 0, total = allEffects.Count; foreach (InfluencedEffect e in allEffects) { // Difference in influence (zero = the same) int differential = 0; List <float> traitValues = new List <float>(); List <float> stateValues = new List <float>(); foreach (var pair in i.strongTraitInfluences) { if (!e.strongTraitInfluences.ContainsKey(pair.Key)) { differential++; } else { foreach (var instance in host.GetAttributeInstances()) { if (instance.Prototype.Equals(pair.Value)) { var val = instance as Trait.TransformedInstance; traitValues.Add(val.TransformedState); } } } } foreach (var pair in i.strongStateInfluences) { if (!e.strongStateInfluences.ContainsKey(pair.Key)) { differential++; } else { foreach (var instance in host.GetAttributeInstances()) { if (instance.Prototype.Equals(pair.Value)) { var val = instance as State.TransformedInstance; stateValues.Add(val.TransformedState); } } } } if (differential == 0) { bool chosen = false; float accumulatorVal = 0f; float modTotal = 0f; int numTraits = 0; foreach (var m in e.Modifiers) { var f = m as FloatModifier; float valCalc = 0f; foreach (var traitVal in traitValues) { foreach (var stateVal in stateValues) { valCalc += stateVal; } numTraits++; } valCalc /= numTraits; accumulatorVal += valCalc; modTotal += f.offset; } accumulatorVal /= e.Modifiers.Count; modTotal /= e.Modifiers.Count; chosen = (accumulatorVal < LOW_STABILITY && modTotal > 0 && modTotal < MOD_OFFSET_LOW) || (accumulatorVal > LOW_STABILITY && accumulatorVal < MIDPOINT && modTotal >= MOD_OFFSET_LOW) || (accumulatorVal > MIDPOINT && accumulatorVal < HI_STABILITY && modTotal <0f && modTotal> -MOD_OFFSET_LOW) || (accumulatorVal > HI_STABILITY && modTotal <= -MOD_OFFSET_LOW); if (chosen) { effects.Add(e); } else { //TODO: Effect is not chosen; perhaps could be a useful point. } } else { unused.Add(e); differentials.Add(differential); } position++; } /* * while(effects.Count == 0) { * Shuffle(unused); * int index = 0; * double chanceTotal = (differentials[index] * total); * double toTheEnd = ((double)(total - position)) / chanceTotal; * double selectChance = random.NextDouble(); * * // the values are always guaranteed to be non-negative * if (selectChance > Math.Min(toTheEnd, effects.Count)) { * effects.Add(unused[index]); * } * index++; * } */ if (effects.Count != 1) { //TODO: Debug statement in case no effects or more than one //effect is chosen /*BehaviorEngine.Debug.Logger.Log( * "Number of effects chosen: " + effects.Count * ); */ } /* * if(effects.Count == 0) { * effects.Add(unused[0]); * } */ return(effects); }
private float StabilityScore( IEnumerable <State> state, Character host, InfluencedInteraction interaction, ICollection <IEntity> targets, BrainRepository repo ) { float ownScore = 0f; int count = 0; foreach (State s in state) { State.TransformedInstance currState = host[s] as State.TransformedInstance; ownScore += currState.TransformedState; count++; } ownScore /= count; float ease = .3f; if (ownScore < LOW_STABILITY) { ease = .5f; } else { if (ownScore > HI_STABILITY) { ease = .1f; } } var func = Transformations.EaseSquaredAtValue(ease); var effects = ChooseStronglyInfluencedEffects(repo, interaction, host); //List<FloatModifier> modifiers = new List<FloatModifier>(); float modifyVal = 0f; foreach (Effect e in effects) { float currMod = 0f; foreach (IModifier m in e.Modifiers) { FloatModifier floatMod = m as FloatModifier; currMod = Math.Abs(floatMod.offset); if (floatMod.Attribute.Equals(repo.GetState("anger"))) { } } if (e.Modifiers.Count != 0) { currMod /= e.Modifiers.Count; } modifyVal += currMod; } if (effects.Count != 0) { modifyVal /= effects.Count; } ownScore = func(modifyVal); if (targets == null) { //BehaviorEngine.Debug.Logger.Log("Score: " + ownScore); return(ownScore); } float otherScore = 0f; foreach (IEntity target in targets) { var attrs = target.GetAttributeInstances(); float stabilityValue = 0f; float weight = 1f; foreach (IAttributeInstance a in attrs) { NormalizedAttribute.Instance attr = a as NormalizedAttribute.Instance; stabilityValue += attr.State; } stabilityValue /= attrs.Count; weight = .3f; if (stabilityValue <= LOW_STABILITY) { weight = .5f; } else { weight = .1f; } var f = Transformations.EaseSquaredAtValue(weight); otherScore += f(modifyVal); // Attempt to bias interaction towards people that crew will vote for var crewmember = target as Character; var hostCrew = host as Crewmember; if (crewmember != null && hostCrew != null) { var chooseVote = hostCrew.ChooseVote(); //Will be null if relationship does not exist if (chooseVote != null) { if (hostCrew.ChooseVote().Equals(crewmember)) { otherScore *= 2; //Debug.Logger.Log("Doubled, found vote"); } } } } if (targets.Count != 0) { otherScore /= targets.Count; } float score = (ownScore + otherScore) / 2; //BehaviorEngine.Debug.Logger.Log("Score: " + score); return(score); }
// Create first-time relationship Relationship CreateRelationship( Character target, InfluencedInteraction interaction ) { Relationship r = GetRelationship(target); if (r != null) { return(null); } var states = interaction.strongStateInfluences; float trust = 0, agreement = 0; foreach (string name in states.Keys) { agreement = agreementAffinities.Match(name) > 0 ? agreement + 1 : agreement - 1; trust = trustAffinities.Match(name) > 0 ? trust + 1 : trust - 1; } // Calculate initial axes if (trust == 0) { trust = .5f; } else if (trust < 0) { trust = 1.0f / (-(trust - 2)); } else { trust = .5f + (.05f * trust); } if (agreement == 0) { agreement = .5f; } else if (agreement < 0) { agreement = 1.0f / (-(agreement - 2)); } else { agreement = .5f + (.05f * agreement); } // Create relationship r = new Relationship(target); r.trust.Offset(trust * 2 - 1); // Scale r.agreement.Offset(agreement * 2 - 1); // Scale relationships[target.name] = r; return(r); }
public void RegisterInteraction(InfluencedInteraction i) { interactions.Add(i); }