FactionController - jimdroberts/FishMMO GitHub Wiki

Description

Manages all faction relationships, standings, and alliance logic for a character. Tracks reputation with all known factions, provides methods for updating and querying alliances, and integrates with network serialization and gameplay systems.


API Access

Fields

  • private Dictionary<int, Faction> factions

    Dictionary of all factions for this character, keyed by template ID. Holds reputation/standing values for each faction.

  • private Dictionary<int, Faction> allied

    Dictionary of allied factions (positive standing), keyed by template ID.

  • private Dictionary<int, Faction> neutral

    Dictionary of neutral factions (zero standing), keyed by template ID.

  • private Dictionary<int, Faction> hostile

    Dictionary of hostile factions (negative standing), keyed by template ID.

  • [SerializeField] private bool isAggressive

    If true, this character is aggressive and will treat others as enemies regardless of faction standing.

  • [SerializeField] private RaceTemplate raceTemplate

    The race template associated with this character, used for initial faction setup.

Properties

  • public bool IsAggressive { get; set; }

    Gets or sets whether the character is aggressive (treats others as enemies).

  • public Dictionary<int, Faction> Factions { get; }

    Public accessor for all factions and their standing values.

  • public Dictionary<int, Faction> Allied { get; }

    Public accessor for allied factions.

  • public Dictionary<int, Faction> Neutral { get; }

    Public accessor for neutral factions.

  • public Dictionary<int, Faction> Hostile { get; }

    Public accessor for hostile factions.

  • public RaceTemplate RaceTemplate { get; }

    Gets the race template for this character.

Methods

  • public override void OnStartCharacter()

    Registers client-side event handlers for faction updates when the character starts (client only).

  • public override void OnStopCharacter()

    Unregisters client-side event handlers for faction updates when the character stops (client only).

  • public override void ResetState(bool asServer)

    Clears all faction data and resets state for the character.

  • public override void ReadPayload(NetworkConnection conn, Reader reader)

    Reads faction data from the network payload and updates local state.

  • public override void WritePayload(NetworkConnection conn, Writer writer)

    Writes faction data to the network payload for clients.

  • public void CopyFrom(IFactionController factionController)

    Copies all faction data from another IFactionController instance.

  • public void SetFaction(int templateID, int value, bool skipEvent = false)

    Sets the reputation value for a faction, updating alliance groups and firing events as needed. templateID (int): The template ID for the faction. value (int): The new reputation or standing value. skipEvent (bool): If true, does not fire update events.

  • public void Add(FactionTemplate template, int amount = 1)

    Adds an amount to the reputation value for a faction, clamping to valid bounds. template (FactionTemplate): The faction template. amount (int): The amount to add (default 1).

  • public void AdjustFaction(IFactionController defenderFactionController, float alliedPercentToSubtract, float hostilePercentToAdd)

    Adjusts reputation with allied and hostile factions based on another controller's state and given percentages. defenderFactionController (IFactionController): The other controller. alliedPercentToSubtract (float): Percentage to subtract from allied factions. hostilePercentToAdd (float): Percentage to add to hostile factions.

  • public FactionAllianceLevel GetAllianceLevel(IFactionController otherFactionController)

    Returns the alliance level (Ally, Neutral, Enemy) with another controller, considering party, guild, and aggression flags. otherFactionController (IFactionController): The other controller to check against. Returns: FactionAllianceLevel (Ally, Neutral, Enemy).

  • public Color GetAllianceLevelColor(IFactionController otherFactionController)

    Returns a color representing the alliance level with another controller (green for Ally, blue for Neutral, red for Enemy). otherFactionController (IFactionController): The other controller to check against. Returns: Color.


Basic Usage

Setup

  1. Attach FactionController to a character or NPC GameObject.
  2. Assign a RaceTemplate to the character for initial faction setup.
  3. Use SetFaction, Add, or AdjustFaction to update reputation values during gameplay.
  4. Use GetAllianceLevel to determine relationships with other characters.

Example

// Example 1: Setting a faction value
factionController.SetFaction(templateID, 1000);

// Example 2: Adjusting reputation after combat
factionController.AdjustFaction(defenderFactionController, 0.5f, 0.25f);

// Example 3: Querying alliance level
FactionAllianceLevel level = factionController.GetAllianceLevel(otherFactionController);
Color color = factionController.GetAllianceLevelColor(otherFactionController);

Best Practices

  • Always clear and reset faction data when reusing or respawning characters.
  • Use the provided methods to update and query reputation to ensure alliance groups stay in sync.
  • Consider party, guild, and aggression flags when determining relationships for gameplay logic.
  • Clamp reputation values to the allowed range to prevent logic errors.
⚠️ **GitHub.com Fallback** ⚠️