MatchEngine: Difference between revisions
Tayfunwiki (talk | contribs) No edit summary |
Tayfunwiki (talk | contribs) No edit summary Β |
||
(One intermediate revision by the same user not shown) | |||
Line 2: | Line 2: | ||
== Overview == | == Overview == | ||
The <code>MatchEngine</code> is part of the Intrigues core system and provides a lightweight, | The <code>MatchEngine</code> is part of the Intrigues core system and provides a lightweight, callback-based matchmaking mechanism between actors. | ||
Β | |||
It helps characters find suitable partners using rule-based compatibility checks and user-defined filters. | |||
---- | ---- | ||
== π§ | == π§ CandidateFilter (Optional) == | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
IM.Instance.MatchEngine.CandidateFilter = (self, other) => ...; | IM.Instance.MatchEngine.CandidateFilter = (self, other) => ...; | ||
Line 11: | Line 13: | ||
=== Description === | === Description === | ||
<code>CandidateFilter</code> is a user-defined | <code>CandidateFilter</code> is a user-defined function that determines who can be evaluated as a match. | ||
Β | |||
It runs '''before''' expensive rule-based logic (<code>IsCompatibleAsync</code>) to filter out invalid candidates. | |||
---- | |||
=== Signature === | === Signature === | ||
Line 17: | Line 22: | ||
public Func<Actor, Actor, bool> CandidateFilter; | public Func<Actor, Actor, bool> CandidateFilter; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
---- | |||
=== Purpose === | === Purpose === | ||
To | To filter out obviously incompatible actors early β improving performance and allowing logic customization | ||
Β | |||
(e.g., same-gender marriage, family bans, rank restrictions). | |||
---- | |||
=== Example === | === Example === | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
// | // Only opposite-gender, unmarried actors | ||
IM.Instance.MatchEngine.CandidateFilter = (self, other) => | IM.Instance.MatchEngine.CandidateFilter = (self, other) => | ||
other.HasSpouse | Β Β !other.HasSpouse && other.Gender != self.Gender; | ||
// | // Same-gender allowed | ||
IM.Instance.MatchEngine.CandidateFilter = (self, other) => | IM.Instance.MatchEngine.CandidateFilter = (self, other) => | ||
other.HasSpouse | Β Β !other.HasSpouse; // No gender check | ||
</syntaxhighlight>π '''Note:''' | </syntaxhighlight><blockquote>π '''Note:''' CandidateFilter is typically set in <code>DemoManager.cs > Start()</code> and affects all matchmaking.</blockquote> | ||
---- | |||
Β | == π FindMatch (Callback-Based) == | ||
π | <syntaxhighlight lang="c#"> | ||
IM.Instance.MatchEngine.FindMatch(actor, "Love Rule", match => { | |||
Β Β if (match != null) | |||
Β Β Β Β actor.StartScheme("Love", match); | |||
}); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Description === | === Description === | ||
<code>FindMatch</code> scans all eligible candidates for the given actor, using a specified rule (like <code>"Love Rule"</code>). | |||
Β | |||
It executes asynchronously in the background and returns the first valid match through a callback. | |||
---- | |||
=== Signature === | === Signature === | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public | public void FindMatch(Actor actor, string rule, Action<Actor> onMatchFound) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
---- | |||
=== Parameters === | === Parameters === | ||
Line 53: | Line 70: | ||
|<code>actor</code> | |<code>actor</code> | ||
|<code>Actor</code> | |<code>Actor</code> | ||
|The actor | |The actor seeking a match | ||
|- | |- | ||
|<code>rule</code> | |<code>rule</code> | ||
|<code>string</code> | |<code>string</code> | ||
|The | |The rule used to determine compatibility | ||
|- | |||
|<code>onMatchFound</code> | |||
|<code>Action<Actor></code> | |||
|Called with the matched actor or <code>null</code> if none found | |||
|} | |} | ||
Β | ---- | ||
Β | |||
=== Example === | === Example === | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
IM.Instance.MatchEngine.FindMatch(actor, "Love Rule", match => { | |||
Β | |||
Β Β Β if (match != null) | Β Β Β if (match != null) | ||
Β Β Β Β Β actor.StartScheme("Love", match); | Β Β Β Β Β actor.StartScheme("Love", match); | ||
Β Β Β else | Β Β Β else | ||
Β Β Β Β Β Debug.Log("No match found."); | |||
Β Β Β Β Β Debug.Log("No | }); | ||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
---- | |||
=== Notes === | |||
* The callback '''always fires''', even if no match is found (with <code>null</code>). | |||
* Ideal for use in <code>Update()</code>, timers, <code>MarriageTimer()</code>, or any non-async context. | |||
* Evaluation is spread over frames to avoid freezing the main thread. | |||
---- | |||
== β οΈ Performance Tip == | == β οΈ Performance Tip == | ||
Always define a <code>CandidateFilter</code> β especially in simulations with 100+ actors β to avoid evaluating thousands of unnecessary compatibility rules. | |||
---- | ---- | ||
== π§ͺ Debugging Tip == | == π§ͺ Debugging Tip == | ||
<syntaxhighlight lang="c#"> | |||
var count = IM.Actors.Count(p => MatchEngine.CandidateFilter(actor, p)); | var count = IM.Actors.Count(p => MatchEngine.CandidateFilter(actor, p)); | ||
Debug.Log($"Evaluating {count} candidates for {actor.Name}"); | Debug.Log($"Evaluating {count} candidates for {actor.Name}"); | ||
</syntaxhighlight> | </syntaxhighlight>You can log how many candidates passed the filter: | ||
---- | |||
== β
Summary == | == β
Summary == | ||
Line 103: | Line 117: | ||
!Purpose | !Purpose | ||
|- | |- | ||
| | |CandidateFilter | ||
| | |Pre-filters actors before heavy compatibility check | ||
|- | |- | ||
| | |FindMatch | ||
| | |Finds the first compatible partner via callback | ||
|- | |- | ||
|Fully async | |Fully async | ||
| | |Internally asynchronous without requiring async/await | ||
|- | |||
|Lightweight | |||
|Designed to scale to hundreds of actors | |||
|- | |- | ||
|Customizable | |Customizable | ||
|Users define | |Users define their own filtering logic | ||
|} | |} | ||
---- | ---- |
Latest revision as of 22:23, 16 May 2025
π§ Intrigues MatchEngine API Reference
Overview
The MatchEngine
is part of the Intrigues core system and provides a lightweight, callback-based matchmaking mechanism between actors.
It helps characters find suitable partners using rule-based compatibility checks and user-defined filters.
π§ CandidateFilter (Optional)
IM.Instance.MatchEngine.CandidateFilter = (self, other) => ...;
Description
CandidateFilter
is a user-defined function that determines who can be evaluated as a match.
It runs before expensive rule-based logic (IsCompatibleAsync
) to filter out invalid candidates.
Signature
public Func<Actor, Actor, bool> CandidateFilter;
Purpose
To filter out obviously incompatible actors early β improving performance and allowing logic customization
(e.g., same-gender marriage, family bans, rank restrictions).
Example
// Only opposite-gender, unmarried actors
IM.Instance.MatchEngine.CandidateFilter = (self, other) =>
!other.HasSpouse && other.Gender != self.Gender;
// Same-gender allowed
IM.Instance.MatchEngine.CandidateFilter = (self, other) =>
!other.HasSpouse; // No gender check
π Note: CandidateFilter is typically set in
DemoManager.cs > Start()
and affects all matchmaking.
π FindMatch (Callback-Based)
IM.Instance.MatchEngine.FindMatch(actor, "Love Rule", match => {
if (match != null)
actor.StartScheme("Love", match);
});
Description
FindMatch
scans all eligible candidates for the given actor, using a specified rule (like "Love Rule"
).
It executes asynchronously in the background and returns the first valid match through a callback.
Signature
public void FindMatch(Actor actor, string rule, Action<Actor> onMatchFound)
Parameters
Name | Type | Description |
---|---|---|
actor
|
Actor
|
The actor seeking a match |
rule
|
string
|
The rule used to determine compatibility |
onMatchFound
|
Action<Actor>
|
Called with the matched actor or null if none found
|
Example
IM.Instance.MatchEngine.FindMatch(actor, "Love Rule", match => {
if (match != null)
actor.StartScheme("Love", match);
else
Debug.Log("No match found.");
});
Notes
- The callback always fires, even if no match is found (with
null
). - Ideal for use in
Update()
, timers,MarriageTimer()
, or any non-async context. - Evaluation is spread over frames to avoid freezing the main thread.
β οΈ Performance Tip
Always define a CandidateFilter
β especially in simulations with 100+ actors β to avoid evaluating thousands of unnecessary compatibility rules.
π§ͺ Debugging Tip
var count = IM.Actors.Count(p => MatchEngine.CandidateFilter(actor, p));
Debug.Log($"Evaluating {count} candidates for {actor.Name}");
You can log how many candidates passed the filter:
β Summary
Feature | Purpose |
---|---|
CandidateFilter | Pre-filters actors before heavy compatibility check |
FindMatch | Finds the first compatible partner via callback |
Fully async | Internally asynchronous without requiring async/await |
Lightweight | Designed to scale to hundreds of actors |
Customizable | Users define their own filtering logic |