-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoodManager.cs
393 lines (326 loc) · 10.3 KB
/
MoodManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using ComplexLifeforms.Enums;
using static ComplexLifeforms.Utils;
namespace ComplexLifeforms {
[SuppressMessage ("ReSharper", "MemberCanBePrivate.Global")]
[SuppressMessage("ReSharper", "ConvertToAutoPropertyWhenPossible")]
public class MoodManager {
public const int URGE_CAP = 50;
public const int EMOTION_CAP = 99;
/// <summary>Column separator for ToString and ToStringHeader methods. Default=' '</summary>
public static char Separator = ' ';
/// <summary>Column width limit for ToString and ToStringHeader methods. Default=4</summary>
public static int TruncateTo = 4;
/// <summary>Adds _moodValue to ToString and ToStringHeader methods. Default=false</summary>
public static bool Extended = false;
/// <summary>Represents the lifeform which this object belongs to.</summary>
[SuppressMessage ("ReSharper", "NotAccessedField.Global")]
public Lifeform Lifeform;
protected internal bool Asleep;
private static readonly int[] EMOTION_MOOD_EFFECT = { 10, 3, -1, 0, -4, -3, -5, 0 };
private static readonly int[,] TYPE_VALUES = { { 1, 1, 1 }, { 2, 1, 1 }, { 3, 2, 1 } };
private static readonly string[,] EMOTION_NAMES = {
{ "Serenity", "Acceptance", "Apprehension", "Distraction",
"Pensiveness", "Boredom", "Annoyance", "Interest" },
{ "Joy", "Trust", "Fear", "Surprise",
"Sadness", "Disgust", "Anger", "Anticipation" },
{ "Ecstasy", "Admiration", "Terror", "Amazement",
"Grief", "Loathing", "Rage", "Vigilance" },
{ "Love", "Submission", "Awe", "Disapproval",
"Remorse", "Contempt", "Aggression", "Optimism" }
};
private readonly Tier[] _emotionBias;
private readonly Tier[] _urgeBias;
private readonly int[] _emotionValues;
private readonly int[] _urgeValues;
private Mood _mood;
private Urge _urge;
private Emotion _emotion;
private int _moodValue;
public MoodManager (Lifeform lifeform=null) : this(lifeform, true) {
Lifeform = lifeform;
Asleep = false;
_urgeValues = new int[URGE_COUNT];
_emotionValues = new int[EMOTION_COUNT];
Update();
}
public MoodManager (Tier[] urgeBias, Tier[] emotionBias, Lifeform lifeform=null) : this(lifeform) {
_urgeBias = urgeBias;
_emotionBias = emotionBias;
}
private MoodManager (Lifeform lifeform, bool generateBias) {
if (generateBias) {
_urgeBias = GenerateUrgeBias(lifeform?.Species);
_emotionBias = GenerateEmotionBias(lifeform?.Species);
}
}
/// <summary>Represents the current general mood.</summary>
public Mood Mood => _mood;
/// <summary>Represents the current strongest urge.</summary>
public Urge Urge => _urge;
/// <summary>Represents the current strongest emotion.</summary>
public Emotion Emotion => _emotion;
public Tier[] UrgeBias => _urgeBias;
public Tier[] EmotionBias => _emotionBias;
public static string EmotionName (MoodManager mood) {
int[] result = EmotionIntensity(mood._emotionValues);
return EMOTION_NAMES[result[0], result[1]];
}
public static string ToStringHeader () {
char s = Separator;
string data = "";
bool first = true;
foreach (string urge in Enum.GetNames(typeof(Urge))) {
if (first) {
data += Truncate(urge, TruncateTo, 1);
first = false;
continue;
}
data += s + Truncate(urge, TruncateTo, 1);
}
data += s;
foreach (string emotion in Enum.GetNames(typeof(Emotion))) {
data += s + Truncate(emotion, TruncateTo, 1);
}
if (Extended) {
data += $"{s}{s}" + Truncate("mood", TruncateTo, 1);
}
return data;
}
public void Update () {
ProcessChanges();
ClampValues();
ProcessMood();
_urge = (Urge) Utils.MaxIndex(_urgeValues);
_emotion = (Emotion) Utils.MaxIndex(_emotionValues);
}
public void Action (Urge action) {
int iaction = (int) action;
int[] indexes = EdgeIndexes(_urgeValues);
int maxA = indexes[0];
int maxB = indexes[1];
int minA = indexes[2];
int minB = indexes[3];
Emotion[] emotions;
int type = 0;
switch (action) {
case Urge.Eat:
case Urge.Drink:
if (iaction == maxA) {
// highest
emotions = new[] { Emotion.Joy, Emotion.Surprise, Emotion.Trust };
type = 2;
} else if (iaction == maxB) {
// second highest
emotions = new[] { Emotion.Joy, Emotion.Anticipation };
type = 1;
} else if (iaction == minA) {
// lowest
emotions = new[] { Emotion.Disgust, Emotion.Sadness, Emotion.Anger };
type = 2;
} else if (iaction == minB) {
// second lowest
emotions = new[] { Emotion.Disgust, Emotion.Sadness };
type = 1;
} else {
emotions = new[] { Emotion.Anticipation, Emotion.Sadness, Emotion.Trust };
}
break;
case Urge.Excrete:
case Urge.Reproduce:
if (iaction == maxA) {
// highest
emotions = new[] { Emotion.Joy, Emotion.Trust, Emotion.Surprise };
type = 2;
} else if (iaction == maxB) {
// second highest
emotions = new[] { Emotion.Joy, Emotion.Anticipation };
type = 1;
} else if (iaction == minA) {
// lowest
emotions = new[] { Emotion.Disgust, Emotion.Anger, Emotion.Fear };
type = 2;
} else if (iaction == minB) {
// second lowest
emotions = new[] { Emotion.Disgust, Emotion.Anger };
type = 1;
} else {
emotions = new[] { Emotion.Anticipation, Emotion.Sadness, Emotion.Trust };
}
break;
case Urge.Sleep:
if (iaction == maxA) {
// highest
emotions = new[] { Emotion.Joy, Emotion.Fear };
type = 2;
} else if (iaction == maxB) {
// second highest
emotions = new[] { Emotion.Joy, Emotion.Fear };
type = 1;
} else if (iaction == minA) {
// lowest
emotions = new[] { Emotion.Anger, Emotion.Joy };
type = 2;
} else if (iaction == minB) {
// second lowest
emotions = new[] { Emotion.Anger, Emotion.Joy };
type = 1;
} else {
emotions = new[] { Emotion.Joy, Emotion.Fear, Emotion.Trust };
}
break;
case Urge.Heal:
if (iaction == maxA) {
// highest
emotions = new[] { Emotion.Joy, Emotion.Fear, Emotion.Anticipation };
type = 2;
} else if (iaction == maxB) {
// second highest
emotions = new[] { Emotion.Fear, Emotion.Joy };
type = 1;
} else if (iaction == minA) {
// lowest
emotions = new[] { Emotion.Fear, Emotion.Anticipation, Emotion.Anger };
type = 2;
} else if (iaction == minB) {
// second lowest
emotions = new[] { Emotion.Fear, Emotion.Anticipation };
type = 1;
} else {
emotions = new[] { Emotion.Fear, Emotion.Anticipation, Emotion.Trust };
}
break;
default:
Console.WriteLine($"Unimplemented action. a:{action}");
return;
}
for (int i = 0; i < emotions.Length; ++i) {
_emotionValues[(int) emotions[i]] += TYPE_VALUES[type, i];
}
_urgeValues[iaction] -= 5;
}
public void AffectUrge (Urge urge, int delta) {
// todo expand
_urgeValues[(int) urge] += delta;
}
public override string ToString () {
char s = Separator;
string data = $"{_urgeValues[0],2} {(int) _urgeBias[0]}";
for (int i = 1; i < URGE_COUNT; ++i) {
data += $"{s}{_urgeValues[i],2} {(int) _urgeBias[i]}";
}
data += s;
for (int i = 0; i < EMOTION_COUNT; ++i) {
data += $"{s}{_emotionValues[i],2} {(int) _emotionBias[i]}";
}
if (Extended) {
data += $"{s}{s}{_moodValue,4}";
}
return data;
}
private static int EmotionIntensity (int value) {
const int high = (int) (EMOTION_CAP * 0.75);
const int low = (int) (EMOTION_CAP * 0.25);
int intensity = 1;
if (value >= high) {
intensity = 2;
} else if (value <= low) {
intensity = 0;
}
return intensity;
}
private static int[] EmotionIntensity (IReadOnlyList<int> values) {
const double threshold = 0.25;
int emotionIndex = Utils.MaxIndex(values);
int emotionValue = values[emotionIndex];
int indexLeft;
int indexRight;
if (emotionIndex == 0) {
indexLeft = values.Count - 1;
} else {
indexLeft = emotionIndex - 1;
}
if (emotionIndex == values.Count - 1) {
indexRight = 0;
} else {
indexRight = emotionIndex + 1;
}
double valueLeft = values[indexLeft];
double valueRight = values[indexRight];
int[] result = { EmotionIntensity(emotionValue), emotionIndex };
if (valueLeft > valueRight && valueLeft / emotionValue >= threshold) {
result = new[] { 3, indexLeft };
} else if (valueRight > valueLeft && valueRight / emotionValue >= threshold) {
result = new[] { 3, indexRight };
}
return result;
}
private void ProcessChanges () {
for (int i = 0; i < URGE_COUNT; ++i) {
if (Utils.Random.Next((int) _urgeBias[i], TIER_COUNT + 1) == TIER_COUNT) {
if (!Asleep) {
++_urgeValues[i];
}
}
}
for (int i = 0; i < EMOTION_COUNT; ++i) {
if (Asleep && Utils.Random.Next(TIER_COUNT - (int) _emotionBias[i] + 1, TIER_COUNT + 1) == TIER_COUNT) {
_emotionValues[i] -= TIER_COUNT - (int) _emotionBias[i] + 1;
}
}
}
private void ProcessMood () {
const int good = 15;
const int neutral = 5;
const int bad = -5;
const int terrible = -15;
int mood = _moodValue / 2;
for (int i = 0; i < EMOTION_COUNT; ++i) {
int intensity = EmotionIntensity(_emotionValues[i]);
if (_emotionValues[i] != 0) {
mood += (intensity + 1) * EMOTION_MOOD_EFFECT[i];
}
}
if (mood > good) {
_mood = Mood.Great;
} else if (mood > neutral) {
_mood = Mood.Good;
} else if (mood > bad) {
_mood = Mood.Neutral;
} else if (mood > terrible) {
_mood = Mood.Bad;
} else {
_mood = Mood.Terrible;
}
_moodValue = mood;
}
private void ClampValues () {
for (int i = 0; i < URGE_COUNT; ++i) {
if (_urgeBias[i] == Tier.None) {
_urgeValues[i] = 0;
continue;
}
int u = _urgeValues[i];
if (u < 0) {
_urgeValues[i] = 0;
} else if (u > URGE_CAP) {
_urgeValues[i] = URGE_CAP;
}
}
for (int i = 0; i < EMOTION_COUNT; ++i) {
if (_emotionBias[i] == Tier.None) {
_emotionValues[i] = 0;
continue;
}
int e = _emotionValues[i];
if (e < 0) {
_emotionValues[i] = 0;
} else if (e > EMOTION_CAP) {
_emotionValues[i] = EMOTION_CAP;
}
}
}
}
}