Skip to content

Commit

Permalink
Implemented most of new experimental architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiJbey committed Feb 16, 2024
1 parent bd9e0ea commit 26cbbb4
Show file tree
Hide file tree
Showing 49 changed files with 1,514 additions and 1,267 deletions.
24 changes: 0 additions & 24 deletions Assets/TDRS_Demo/Scripts/MockSaveSystem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.IO;
using UnityEngine;
using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization;
using System.Collections.Generic;
using TDRS.Serialization;

Expand Down Expand Up @@ -40,28 +38,6 @@ void Update()

Debug.Log(json);

YamlMappingNode node = new YamlMappingNode()
{
{"ingredients", new YamlSequenceNode() { "sugar", "water", "purple" } }
};

YamlDocument doc = new YamlDocument(node);

var serializer = new SerializerBuilder()
.JsonCompatible()
.Build();

Debug.Log(serializer.Serialize(node));

Debug.Log(serializer.Serialize(doc));

var ingredients = new IngredientList()
{
ingredients = new List<string>() { "sugar", "water", "purple" }
};

Debug.Log(serializer.Serialize(ingredients));

// string filePath = Path.Combine(Application.persistentDataPath, SAVE_PATH);

// File.WriteAllText(filePath, json);
Expand Down
190 changes: 33 additions & 157 deletions Packages/com.shijbey.tdrs/Runtime/Agent.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Collections.Generic;
using RePraxis;

namespace TDRS
{
/// <summary>
/// An entity within a social graph that is connected to other agents via relationships.
/// </summary>
public class Agent : IEffectable
public class Agent : ISocialEntity, IEffectable
{
#region Properties

Expand Down Expand Up @@ -36,11 +35,6 @@ public class Agent : IEffectable
/// </summary>
public StatManager Stats { get; }

/// <summary>
/// All social rules affecting this agent.
/// </summary>
public SocialRuleManager SocialRules { get; }

/// <summary>
/// Manages all effects applied to this agent.
/// </summary>
Expand Down Expand Up @@ -74,9 +68,8 @@ public Agent(SocialEngine engine, string uid, string agentType)
UID = uid;
AgentType = agentType;
Engine = engine;
Traits = new TraitManager();
Traits = new TraitManager(this);
Stats = new StatManager();
SocialRules = new SocialRuleManager();
Effects = new EffectManager();
OutgoingRelationships = new Dictionary<Agent, Relationship>();
IncomingRelationships = new Dictionary<Agent, Relationship>();
Expand All @@ -94,7 +87,7 @@ public Agent(SocialEngine engine, string uid, string agentType)
/// <param name="traitID"></param>
/// <param name="duration"></param>
/// <returns></returns>
public bool AddTrait(string traitID)
public bool AddTrait(string traitID, int duration = -1)
{
Trait trait = Engine.TraitLibrary.Traits[traitID];

Expand All @@ -109,26 +102,10 @@ public bool AddTrait(string traitID)
);
}

EffectContext ctx = new EffectContext(
Engine,
trait.Description,
new Dictionary<string, object>()
{
{ "?owner", UID }
},
trait
);

TraitInstance traitInstance = TraitInstance.CreateInstance(Engine, trait, ctx, this);
Traits.AddTrait(trait, duration);

Engine.DB.Insert($"{UID}.traits.{traitID}");

Traits.AddTrait(traitInstance);

SocialRules.AddSource(traitInstance);

traitInstance.Apply();

ReevaluateRelationships();

return true;
Expand All @@ -143,16 +120,10 @@ public bool RemoveTrait(string traitID)
{
if (!Traits.HasTrait(traitID)) return false;

var traitInstance = Traits.GetTrait(traitID);

Traits.RemoveTrait(traitID);

Engine.DB.Delete($"{UID}.traits.{traitID}");

SocialRules.RemoveSource(traitInstance);

traitInstance.Remove();

ReevaluateRelationships();

return true;
Expand All @@ -163,144 +134,49 @@ public bool RemoveTrait(string traitID)
/// </summary>
public void Tick()
{
Effects.TickEffects();
TickTraits();

List<TraitInstance> traitInstanceList = new List<TraitInstance>(Traits.Traits);
OnTick?.Invoke(this, EventArgs.Empty);
}

foreach (var traitInstance in traitInstanceList)
/// <summary>
/// Update the traits associated with this agent.
/// </summary>
public void TickTraits()
{
List<TraitInstance> traitInstances = new List<TraitInstance>(Traits.Traits);
foreach (var instance in traitInstances)
{
if (!Traits.HasTrait(traitInstance.TraitID)) continue;
traitInstance.TickSocialRuleInstances();
}
instance.Tick();

OnTick?.Invoke(this, EventArgs.Empty);
if (instance.HasDuration && instance.Duration <= 0)
{
RemoveTrait(instance.TraitID);
}
}
}

/// <summary>
/// Check and recalculate social rule effects.
/// Reevaluate relationships against the social rules.
/// </summary>
public void ReevaluateRelationships()
{
foreach (var socialRuleSource in SocialRules.Sources)
foreach (var (_, relationship) in OutgoingRelationships)
{
foreach (var socialRule in socialRuleSource.SocialRules)
{
// Try to apply the social rule to existing outgoing relationships
foreach (var (other, relationship) in OutgoingRelationships)
{
if (
socialRuleSource.HasSocialRuleInstance(
socialRule,
UID,
other.UID
)
)
{
var instance = socialRuleSource.GetSocialRuleInstance(
socialRule, UID, other.UID
);

var isValid = new DBQuery(socialRule.Preconditions).Run(
Engine.DB,
instance.Context.Bindings
).Success;

if (!isValid)
{
instance.Remove();
socialRuleSource.RemoveSocialRuleInstance(instance);
}

continue;
}

var results = new DBQuery(socialRule.Preconditions).Run(
Engine.DB,
new Dictionary<string, object>()
{
{"?owner", UID},
{"?other", other.UID}
}
);

if (!results.Success) continue;

EffectContext ctx = new EffectContext(
Engine,
socialRule.DescriptionTemplate,
new Dictionary<string, object>(){
{"?owner", UID},
{"?other", other.UID}
},
socialRule.Source
);

var ruleInstance = SocialRuleInstance.Instantiate(socialRule, ctx);

socialRuleSource.AddSocialRuleInstance(ruleInstance);

ruleInstance.Apply();
}

foreach (var (other, relationship) in IncomingRelationships)
{
if (
socialRuleSource.HasSocialRuleInstance(
socialRule,
UID,
other.UID
)
)
{
var instance = socialRuleSource.GetSocialRuleInstance(
socialRule, UID, other.UID
);

var isValid = new DBQuery(socialRule.Preconditions).Run(
Engine.DB,
instance.Context.Bindings
).Success;

if (!isValid)
{
instance.Remove();
socialRuleSource.RemoveSocialRuleInstance(instance);
}

continue;
}

var results = new DBQuery(socialRule.Preconditions).Run(
Engine.DB,
new Dictionary<string, object>()
{
{"?owner", UID},
{"?other", other.UID}
}
);

if (!results.Success) continue;

EffectContext ctx = new EffectContext(
Engine,
socialRule.DescriptionTemplate,
new Dictionary<string, object>(){
{"?owner", UID},
{"?other", other.UID}
},
socialRule.Source
);

var ruleInstance = SocialRuleInstance.Instantiate(socialRule, ctx);

socialRuleSource.AddSocialRuleInstance(ruleInstance);

ruleInstance.Apply();
}
}
relationship.ReevaluateSocialRules();
}

foreach (var (_, relationship) in IncomingRelationships)
{
relationship.ReevaluateSocialRules();
}
}

public void ReevaluateRelationship(Agent target)
{
OutgoingRelationships[target].ReevaluateSocialRules();
}

public override string ToString()
{
return $"Agent({UID})";
Expand Down
14 changes: 14 additions & 0 deletions Packages/com.shijbey.tdrs/Runtime/AgentConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,19 @@ public class AgentConfig
/// Default traits added to this agent type when created.
/// </summary>
public string[] traits;

public AgentConfig()
{
agentType = "";
stats = new StatSchema[0];
traits = new string[0];
}

public AgentConfig(string agentType, StatSchema[] stats, string[] traits)
{
this.agentType = agentType;
this.stats = stats;
this.traits = traits;
}
}
}
28 changes: 28 additions & 0 deletions Packages/com.shijbey.tdrs/Runtime/ISocialEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace TDRS
{
/// <summary>
/// An agent or relationship in the social engine.
/// </summary>
public interface ISocialEntity
{
/// <summary>
/// A reference to the manager that owns this entity.
/// </summary>
public SocialEngine Engine { get; }

/// <summary>
/// The collection of traits associated with this entity.
/// </summary>
public TraitManager Traits { get; }

/// <summary>
/// A collection of stats associated with this entity.
/// </summary>
public StatManager Stats { get; }

/// <summary>
/// Manages all effects applied to this entity.
/// </summary>
public EffectManager Effects { get; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 26cbbb4

Please sign in to comment.