|
| 1 | +# NPC Personality Vector Example |
| 2 | + |
| 3 | +This repository contains a simplified example implementation of a personality component for non-player characters in simulation games. I made it to track how likely characters are to engage in different behaviors. Also, I wanted to be able to calculate how compatible two personalities are. |
| 4 | + |
| 5 | +The `Personality` class represents character personalities as a vector of real-valued numbers. You can access each personality trait using a corresponding enum value. Also, you can calculate the similarity of two personalities on a scale from -1 to 1. -1 means they are diametrically opposed, and 1 means they are completely compatible. |
| 6 | + |
| 7 | +By default, all personality attributes are clamped on the interval [-100, 100]. Setting the value of `ATTRS_MAX_ABS` will adjust this interval. For instance, `Personality<PersonalityAttrs>.ATTR_MAX_ABS = 50` will change the interval to [-50, 50]. `ATTRS_MAX_ABS` may only be a positive number. |
| 8 | + |
| 9 | +This package can be used with Unity. You will need to write your own `PersonalityController` class to wrap the personality instance and make it available within the Inspector. See the example below. |
| 10 | + |
| 11 | +If you have any questions, create a new GitHub issue. Don't forget to star the repository if you find this useful. |
| 12 | + |
| 13 | +## Package Dependencies |
| 14 | + |
| 15 | +- [MathNet.Numerics](https://www.nuget.org/packages/MathNet.Numerics/) v5.0.0 |
| 16 | + |
| 17 | +## General C\# Example |
| 18 | + |
| 19 | +```csharp |
| 20 | +using PersonalitySample; |
| 21 | + |
| 22 | +// Start by defining an enum stating what the attributes of a relationship are. |
| 23 | +public enum PersonalityAttrs |
| 24 | +{ |
| 25 | + VALUES_MONEY, |
| 26 | + VALUES_FAMILY, |
| 27 | + VALUES_LOYALTY, |
| 28 | + VALUES_POWER, |
| 29 | + VALUES_LOVE |
| 30 | +} |
| 31 | + |
| 32 | +Personality<PersonalityAttrs> personalityA = new Personality<PersonalityAttrs>(); |
| 33 | +personalityA.Set( PersonalityAttrs.VALUES_MONEY, 45 ); |
| 34 | +// Output: Personality(VALUES_MONEY=45, VALUES_FAMILY=0, VALUES_LOYALTY=0, VALUES_POWER=0, VALUES_LOVE=0) |
| 35 | +
|
| 36 | +Personality<PersonalityAttrs> personalityB = new Personality<PersonalityAttrs>(); |
| 37 | +personalityB.Set( PersonalityAttrs.VALUES_MONEY, -10 ); |
| 38 | +// Output: Personality(VALUES_MONEY=-10, VALUES_FAMILY=0, VALUES_LOYALTY=0, VALUES_POWER=0, VALUES_LOVE=0) |
| 39 | +
|
| 40 | +Console.WriteLine(personalityA.Similarity(personalityB)); |
| 41 | +// Output: -0.3 |
| 42 | +// These characters are not very compatible. |
| 43 | +
|
| 44 | +personalityB.Set( PersonalityAttrs.VALUES_MONEY, 45 ); |
| 45 | +Console.WriteLine(personalityA.Similarity(personalityB)); |
| 46 | +// Output: 1 |
| 47 | +// These characters are now 100% compatible |
| 48 | +
|
| 49 | +``` |
| 50 | + |
| 51 | +## Unity Example |
| 52 | + |
| 53 | +```csharp |
| 54 | +using UnityEngine; |
| 55 | +using PersonalitySample; |
| 56 | + |
| 57 | +public class PersonalityController : MonoBehaviour |
| 58 | +{ |
| 59 | + public enum PersonalityAttrs |
| 60 | + { |
| 61 | + VALUES_MONEY, |
| 62 | + VALUES_FAMILY, |
| 63 | + VALUES_LOYALTY, |
| 64 | + VALUES_POWER, |
| 65 | + VALUES_LOVE |
| 66 | + } |
| 67 | + |
| 68 | + public Personality<PersonalityAttrs> personality; |
| 69 | + |
| 70 | + // Initial values for personality traits |
| 71 | + public float values_money; |
| 72 | + public float values_family; |
| 73 | + public float values_loyalty; |
| 74 | + public float values_power; |
| 75 | + public float values_love; |
| 76 | + |
| 77 | + void Awake() |
| 78 | + { |
| 79 | + personality = new Personality<PersonalityAttrs>(); |
| 80 | + personality[VALUES_MONEY] = values_money; |
| 81 | + personality[VALUES_FAMILY] = values_family; |
| 82 | + personality[VALUES_LOYALTY] = values_loyalty; |
| 83 | + personality[VALUES_POWER] = values_power; |
| 84 | + personality[VALUES_LOVE] = values_love; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +``` |
0 commit comments