-
Notifications
You must be signed in to change notification settings - Fork 38
/
CreatureConfig.cs
230 lines (195 loc) · 8.24 KB
/
CreatureConfig.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
using System;
using System.Linq;
using HarmonyLib;
using Jotunn.Entities;
using UnityEngine;
namespace Jotunn.Configs
{
/// <summary>
/// Configuration class for adding custom creature spawns.<br />
/// Use this in a constructor of <see cref="CustomCreature"/>
/// </summary>
public class CreatureConfig
{
/// <summary>
/// The unique name for your custom creature. May be tokenized.
/// </summary>
public string Name { get; set; } = string.Empty;
/// <summary>
/// Group tag of this creature.<br/>
/// Creatures in the same group don't attack each other, regardless of faction.
/// </summary>
public string Group { get; set; } = string.Empty;
/// <summary>
/// <see cref="Character.Faction"/> of this creature.
/// </summary>
public Character.Faction? Faction { get; set; }
// /// <summary>
// /// Set true to make this creature tameable.<br/>
// /// Jötunn automatically adds the <see cref="MonsterAI"/> and <see cref="Tameable"/> components if needed.<br/>
// /// Tameable creatures must have the <see cref="Humanoid"/> component attached, which won't be added automatically.
// /// </summary>
// public bool MakeTameable { get; set; }
//
// /// <summary>
// /// When <see cref="MakeTameable"/> is true use this to also make the creature commandable.
// /// </summary>
// public bool MakeCommandable { get; set; }
/// <summary>
/// If set to true, <see cref="LevelEffects"/> stack the "EnableObject" action for all levels
/// instead of only activating the GameObject of the highest level matched.
/// </summary>
public bool UseCumulativeLevelEffects { get; set; }
/// <summary>
/// Array of <see cref="DropConfig">DropConfigs</see> to use for this creature's <see cref="CharacterDrop"/>.<br/>
/// A <see cref="CharacterDrop"/> component will automatically be added if not present.<br/>
/// The drop table of an existing component will be replaced.
/// </summary>
public DropConfig[] DropConfigs = Array.Empty<DropConfig>();
/// <summary>
/// Array of <see cref="SpawnConfig">SpawnConfigs</see> used for world spawns of your custom creature.<br/>
/// Leave empty if you don't want your creature to spawn in the world automatically.<br/>
/// </summary>
public SpawnConfig[] SpawnConfigs = Array.Empty<SpawnConfig>();
/// <summary>
/// String array of items this creature can consume to use in the <see cref="MonsterAI"/> component.<br/>
/// Jötunn will try to resolve all strings to <see cref="ItemDrop">ItemDrops</see> at runtime.<br/>
/// An existing consumeItems table will be replaced.
/// </summary>
public string[] Consumables = Array.Empty<string>();
/// <summary>
/// Apply this config's values to a creature GameObject.
/// </summary>
/// <param name="prefab">Prefab to apply this config to</param>
public void Apply(GameObject prefab)
{
if (!prefab.TryGetComponent(out Character character))
{
Logger.LogError($"GameObject {prefab.name} has no Character component attached");
return;
}
// if (MakeTameable && !prefab.GetComponent<Humanoid>())
// {
// Logger.LogError($"GameObject {prefab.name} has no Humanoid component attached, can't make it tameable");
// return;
// }
if (!string.IsNullOrEmpty(Name))
{
character.m_name = Name;
}
if (string.IsNullOrEmpty(character.m_name))
{
character.m_name = prefab.name;
}
if (!string.IsNullOrEmpty(Group))
{
character.m_group = Group;
}
if (Faction.HasValue)
{
character.m_faction = Faction.Value;
}
/*if (MakeTameable)
{
if (prefab.TryGetComponent(out AnimalAI animalAi))
{
if (prefab.TryGetComponent(out ZNetView zNetView))
{
zNetView.Unregister("Alert");
zNetView.Unregister("OnNearProjectileHit");
}
animalAi.CancelInvoke("DoIdleSound");
Object.Destroy(animalAi);
}
if (!prefab.TryGetComponent(out MonsterAI monsterAi))
{
monsterAi = prefab.AddComponent<MonsterAI>();
}
if (!prefab.TryGetComponent(out Tameable tameable))
{
tameable = prefab.AddComponent<Tameable>();
}
tameable.m_monsterAI ??= monsterAi;
monsterAi.m_tamable ??= tameable;
if (MakeCommandable)
{
tameable.m_commandable = true;
}
}*/
var drops = GetDrops().ToList();
if (drops.Any())
{
var comp = prefab.GetOrAddComponent<CharacterDrop>();
comp.m_drops = drops;
}
var consumeItems = GetConsumeItems().ToList();
if (consumeItems.Any() && prefab.TryGetComponent(out MonsterAI ai))
{
ai.m_consumeItems = consumeItems;
}
}
/// <summary>
/// Converts the <see cref="DropConfig">DropConfigs</see> to Valheim style <see cref="CharacterDrop.Drop"/> array.
/// </summary>
/// <returns>The Valheim <see cref="CharacterDrop.Drop"/> array</returns>
public CharacterDrop.Drop[] GetDrops()
{
CharacterDrop.Drop[] drops = new CharacterDrop.Drop[DropConfigs.Length];
for (int i = 0; i < drops.Length; i++)
{
drops[i] = DropConfigs[i].GetDrop();
}
return drops;
}
/// <summary>
/// Converts the <see cref="SpawnConfig">SpawnConfigs</see> to Valheim style <see cref="SpawnSystem.SpawnData"/> array.
/// </summary>
/// <returns>The Valheim <see cref="SpawnSystem.SpawnData"/> array</returns>
public SpawnSystem.SpawnData[] GetSpawns()
{
SpawnSystem.SpawnData[] spawns = new SpawnSystem.SpawnData[SpawnConfigs.Length];
for (int i = 0; i < spawns.Length; i++)
{
spawns[i] = SpawnConfigs[i].GetSpawnData();
}
return spawns;
}
/// <summary>
/// Creates an array of <see cref="ItemDrop"/> mocks for the consumeItem list of the creature.
/// </summary>
/// <returns>An array of <see cref="ItemDrop"/> mocks</returns>
public ItemDrop[] GetConsumeItems()
{
ItemDrop[] itemDrops = new ItemDrop[Consumables.Length];
for (int i = 0; i < itemDrops.Length; i++)
{
itemDrops[i] = Mock<ItemDrop>.Create(Consumables[i]);
}
return itemDrops;
}
/// <summary>
/// Appends a new <see cref="DropConfig"/> to the array of existing ones.
/// </summary>
/// <param name="dropConfig"></param>
public void AddDropConfig(DropConfig dropConfig)
{
DropConfigs = DropConfigs.AddToArray(dropConfig);
}
/// <summary>
/// Appends a new <see cref="SpawnConfig"/> to the array of existing ones.
/// </summary>
/// <param name="spawnConfig"></param>
public void AddSpawnConfig(SpawnConfig spawnConfig)
{
SpawnConfigs = SpawnConfigs.AddToArray(spawnConfig);
}
/// <summary>
/// Appends a new consumable to the array of existing ones.
/// </summary>
/// <param name="consumable"></param>
public void AddConsumable(string consumable)
{
Consumables = Consumables.AddToArray(consumable);
}
}
}