forked from decaprime/LeadAHorseToWater
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Commands.cs
410 lines (344 loc) · 18.9 KB
/
Commands.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
using System.Collections.Generic;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using Il2CppSystem.Collections;
using UnityEngine;
namespace LeadAHorseToWater.VCFCompat
{
using ProjectM;
using Unity.Entities;
using Unity.Transforms;
using VampireCommandFramework;
using Bloodstone.API;
using Unity.Mathematics;
using System;
using Processes;
using ProjectM.Network;
using System.Text;
using static UnityEngine.SpookyHash;
public static partial class Commands
{
private static ManualLogSource _log => Plugin.LogInstance;
static Commands()
{
Enabled = IL2CPPChainloader.Instance.Plugins.TryGetValue("gg.deca.VampireCommandFramework", out var info);
if (Enabled) _log.LogWarning($"VCF Version: {info.Metadata.Version}");
}
public static bool Enabled { get; private set; }
public static void Register() => CommandRegistry.RegisterAll();
private static System.Random _random = new();
public record Horse(Entity Entity);
public class NamedHorseConverter : CommandArgumentConverter<Horse, ChatCommandContext>
{
const float radius = 25f;
public override Horse Parse(ChatCommandContext ctx, string input)
{
var horses = HorseUtil.ClosestHorses(ctx.Event.SenderCharacterEntity, radius);
foreach (var horse in horses)
{
var name = VWorld.Server.EntityManager.GetComponentData<NameableInteractable>(horse);
if (name.Name.ToString().Contains(input, StringComparison.OrdinalIgnoreCase))
{
return new Horse(horse);
}
}
throw ctx.Error($"Could not find a horse within {radius:F1} units named like \"{input}\"");
}
[CommandGroup("horse", "h")]
public class HorseCommands
{
private static PrefabGUID BreedItemType = new PrefabGUID(-570287766);
private Horse GetRequiredClosestHorse(ChatCommandContext ctx)
{
var closest = HorseUtil.GetClosetHorse(ctx.Event.SenderCharacterEntity);
if (closest == null)
{
throw ctx.Error("No closest horse and expected to find one based on usage.");
}
return new Horse(closest.Value);
}
[Command("breedable", shortHand: "brab")]
public void Breedable(ChatCommandContext ctx, Horse horse = null)
{
horse ??= GetRequiredClosestHorse(ctx);
EntityManager em = VWorld.Server.EntityManager;
ComponentDataFromEntity<Team> getTeam =
VWorld.Server.EntityManager.GetComponentDataFromEntity<Team>();
if (em.HasComponent<Team>(horse.Entity))
{
var teamhorse = getTeam[horse.Entity];
var isUnit = Team.IsInUnitTeam(teamhorse);
if (isUnit)
{
ctx.Reply($"This horse cannot be breeded because it is not tamed");
return;
}
String name = null;
horse.Entity.WithComponentDataH((ref NameableInteractable nameable) =>
{
name = nameable.Name.ToString();
});
if (name.Equals("") || name == null)
{
ctx.Reply($"Horse without a name is breedable");
return;
}
ctx.Reply($"Horse with name:<{name}> is breedable");
return;
}
throw ctx.Error($"Horse doesn't have team component!");
}
[Command("breed", shortHand: "br")]
public void Breed(ChatCommandContext ctx)
{
var character = ctx.Event.SenderCharacterEntity;
if (BreedTimerProcess.Instance.IsBreedCooldownActive && Settings.ENABLE_HORSE_BREED_COOLDOWN.Value)
{
throw ctx.Error(
$"You've already bred recently, try again in {Mathf.FloorToInt(BreedTimerProcess.Instance.RemainingTime)} second(s).");
}
if (BreedHorseProcess.NextBabyData != null)
{
throw ctx.Error($"There's already a pair breeding, try again in a few seconds.");
}
if (Settings.ENABLE_HORSE_BREED_COOLDOWN.Value)
{
BreedTimerProcess.Instance.StartCooldown();
}
if (!InventoryUtilities.TryGetInventoryEntity(VWorld.Server.EntityManager, character,
out Entity invEntity))
{
BreedTimerProcess.Instance.StopCooldown();
return;
}
var horses = HorseUtil.ClosestHorses(character);
if (horses.Count < 2)
{
BreedTimerProcess.Instance.StopCooldown();
throw ctx.Error(
$"Too little horses in the area: found {horses.Count}. Please move some closer by.");
}
if (horses.Count != 2)
{
BreedTimerProcess.Instance.StopCooldown();
throw ctx.Error(
$"Too many horses in the area: found {horses.Count}. Please move some further away.");
}
if (!HorseUtil.isTamed(horses[0]) || !HorseUtil.isTamed(horses[1]))
{
BreedTimerProcess.Instance.StopCooldown();
throw ctx.Error("You can only breed tamed horses.");
}
var breedItem = new PrefabGUID(Settings.HORSE_BREED_PREFAB.Value);
var breedAmount = Settings.HORSE_BREED_COST.Value;
var didRemove = InventoryUtilitiesServer.TryRemoveItem(VWorld.Server.EntityManager, invEntity,
breedItem, breedAmount);
if (!didRemove)
{
BreedTimerProcess.Instance.StopCooldown();
throw ctx.Error($"You must have at least one {Settings.HORSE_BREED_ITEM_NAME.Value} in your inventory.");
}
var pos1 = VWorld.Server.EntityManager.GetComponentData<Translation>(horses[0]).Value;
var pos2 = VWorld.Server.EntityManager.GetComponentData<Translation>(horses[1]).Value;
var babyPos = UnityEngine.Vector3.Lerp(pos1, pos2, 0.5f);
VWorld.Server.EntityManager.TryGetComponentData<Mountable>(horses[0], out var mountData1);
//var mountData1 = VWorld.Server.EntityManager.GetComponentData<Mountable>(horses[0]);
VWorld.Server.EntityManager.TryGetComponentData<Mountable>(horses[1], out var mountData2);
//var mountData2 = VWorld.Server.EntityManager.GetComponentData<Mountable>(horses[1]);
_log?.LogInfo(
$"Parent {horses[1].Index}\n Speed: {mountData2.MaxSpeed}\n Acceleration: {mountData2.Acceleration}\n Rotation: {mountData2.RotationSpeed}");
StringBuilder sb = new();
sb.AppendLine("Offspring Details");
var applyMutation = (string label, float parent1, float parent2, float maxValue) =>
{
float parentValue;
string par1 = parent1.ToString("F1"), par2 = parent2.ToString("F2");
string parentValueFormatted;
if (_random.NextDouble() > 0.5)
{
parentValue = parent1;
parentValueFormatted = $"( {par1.Underline()} | {par2.Color("#888")} )";
}
else
{
parentValue = parent2;
parentValueFormatted = $"( {par1.Color("#888")} | {par2.Underline()} )";
}
var adjustmentScale = maxValue * Settings.HORSE_BREED_MUTATION_RANGE.Value;
var mutation = (float)(_random.NextDouble() * 2.0 - 1.0);
var adjustment = mutation * adjustmentScale;
char? sign = mutation > 0 ? '+' : null;
var mutationValues =
$"{sign}{adjustment:F1}[{mutation:P}]".Color(adjustment > 0 ? "#1d1" : "#d11");
var output = MathF.Min(parentValue + adjustment, maxValue);
var atMax = output == maxValue ? " [MAX]".Color("#e0e") : "";
var outPutValueStr = $"{output:F1}{atMax}".Bold();
sb.AppendLine(
$"{label.Bold().Color(Color.White)} {parentValueFormatted} {mutationValues} = {outPutValueStr}");
return output;
};
var babySpeed = applyMutation("Speed", mountData1.MaxSpeed, mountData2.MaxSpeed,
Settings.HORSE_BREED_MAX_SPEED.Value);
var babyAcceleration = applyMutation("Acceleration", mountData1.Acceleration,
mountData2.Acceleration, Settings.HORSE_BREED_MAX_ACCELERATION.Value);
var babyRotation = applyMutation("Rotation", mountData1.RotationSpeed / 10f,
mountData2.RotationSpeed / 10f, Settings.HORSE_BREED_MAX_ROTATION.Value);
// todo: check we can breed and that these are ours
var team = VWorld.Server.EntityManager.GetComponentData<Team>(horses[0]);
HorseUtil.SpawnHorse(1, babyPos);
ctx.Reply(sb.ToString());
var name = $"Baby Horse {StatTag(babySpeed, babyAcceleration, babyRotation)}";
BreedHorseProcess.NextBabyData = new BabyHorseData(name, team, babyPos, babySpeed, babyAcceleration,
babyRotation * 10f, horses[0].Index, horses[1].Index, DateTime.Now.AddSeconds(1.5));
}
private string StatTag(float speed, float acceleration, float rotation) =>
$"<color={TagColor}>Ⓢ {speed:F1} Ⓐ {acceleration:F1} Ⓡ {(rotation):F1}</color>";
private const string TagColor = "#BF5";
[Command("tag-stats")]
public void TagStats(ChatCommandContext ctx, Horse horse = null)
{
horse ??= GetRequiredClosestHorse(ctx);
var nameComponent =
VWorld.Server.EntityManager.GetComponentData<NameableInteractable>(horse.Entity);
var name = nameComponent.Name.ToString();
var statStart = name.LastIndexOf("Ⓢ", StringComparison.Ordinal);
if (statStart > 0)
{
var colorStart = name.LastIndexOf("<color=", StringComparison.Ordinal);
if (colorStart > 0 && colorStart < statStart)
{
name = name[..(colorStart - 1)];
}
}
if (name.Length >= 20)
{
ctx.Reply(
$"Name is too long, must be less than 20 characters, currently [without tag] {name.Length} characters");
return;
}
VWorld.Server.EntityManager.TryGetComponentData<Mountable>(horse.Entity, out var mountData);
var tag = StatTag(mountData.MaxSpeed, mountData.Acceleration, mountData.RotationSpeed / 10f);
horse.Entity.WithComponentData((ref NameableInteractable nameInteract) =>
{
nameInteract.Name = $"{name} {tag}";
});
}
[Command("speed", adminOnly: true)]
public void SetSpeed(ChatCommandContext ctx, float speed) =>
SetSpeed(ctx, GetRequiredClosestHorse(ctx), speed);
[Command("speed", adminOnly: true)]
public void SetSpeed(ChatCommandContext ctx, Horse horse, float speed)
{
horse.Entity.WithComponentDataH((ref Mountable mount) => mount.MaxSpeed = speed);
ctx.Reply($"Horse speed set to {speed}");
}
[Command("acceleration", shortHand: "accel", adminOnly: true)]
public void SetAcceleration(ChatCommandContext ctx, float acceleration) =>
SetAcceleration(ctx, GetRequiredClosestHorse(ctx), acceleration);
[Command("acceleration", shortHand: "accel", adminOnly: true)]
public void SetAcceleration(ChatCommandContext ctx, Horse horse, float acceleration)
{
horse.Entity.WithComponentDataH((ref Mountable mount) => mount.Acceleration = acceleration);
ctx.Reply($"Horse acceleration set to {acceleration}");
}
[Command("rotation", adminOnly: true)]
public void SetRotation(ChatCommandContext ctx, float rotation) =>
SetRotation(ctx, GetRequiredClosestHorse(ctx), rotation);
[Command("rotation", adminOnly: true)]
public void SetRotation(ChatCommandContext ctx, Horse horse, float rotation)
{
horse.Entity.WithComponentDataH((ref Mountable mount) => mount.RotationSpeed = rotation * 10f);
ctx.Reply($"Horse rotation set to {rotation}");
}
[Command("warp", adminOnly: true)]
public void WarpHorse(ChatCommandContext ctx, Horse horse = null)
{
horse ??= GetRequiredClosestHorse(ctx);
var position = VWorld.Server.EntityManager.GetComponentData<LocalToWorld>(horse.Entity).Position;
var entity = VWorld.Server.EntityManager.CreateEntity(
ComponentType.ReadWrite<FromCharacter>(),
ComponentType.ReadWrite<PlayerTeleportDebugEvent>()
);
VWorld.Server.EntityManager.SetComponentData<FromCharacter>(entity, new()
{
User = ctx.Event.SenderUserEntity,
Character = ctx.Event.SenderCharacterEntity
});
VWorld.Server.EntityManager.SetComponentData<PlayerTeleportDebugEvent>(entity, new()
{
Position = position.xyz,
Target = PlayerTeleportDebugEvent.TeleportTarget.Self,
});
ctx.Reply("Warped to horse.");
}
[Command("spawn", adminOnly: true)]
public void HorseMe(ChatCommandContext ctx, int num = 1)
{
float3 localPos = VWorld.Server.EntityManager
.GetComponentData<Translation>(ctx.Event.SenderUserEntity).Value;
HorseUtil.SpawnHorse(num, localPos);
ctx.Reply($"Spawned {num} horse{(num > 1 ? "s" : "")} near you.");
}
[Command("whistle", shortHand: "w", adminOnly: true)]
public void Whistle(ChatCommandContext ctx, Horse horse = null)
{
horse ??= GetRequiredClosestHorse(ctx);
float3 userPos = VWorld.Server.EntityManager
.GetComponentData<Translation>(ctx.Event.SenderUserEntity).Value;
float3 horsePos = VWorld.Server.EntityManager.GetComponentData<Translation>(horse.Entity).Value;
horse.Entity.WithComponentData((ref Translation t) => { t.Value = userPos; });
ctx.Reply("Your horse came to you.");
}
[Command("rename", adminOnly: true)]
public void Rename(ChatCommandContext ctx, string newName) =>
Rename(ctx, GetRequiredClosestHorse(ctx), newName);
[Command("rename", adminOnly: true)]
public void Rename(ChatCommandContext ctx, Horse horse, string newName)
{
string oldName = "";
horse.Entity.WithComponentData((ref NameableInteractable t) =>
{
oldName = t.Name.ToString();
t.Name = newName;
});
ctx.Reply($"Horse '{oldName}' renamed to '{newName}'.");
}
[Command("kill", adminOnly: true)]
public void Kill(ChatCommandContext ctx, Horse horse = null)
{
horse ??= GetRequiredClosestHorse(ctx);
horse.Entity.WithComponentData((ref Health t) =>
{
t.Value = 0;
t.TimeOfDeath = 0;
t.IsDead = true;
});
VWorld.Server.EntityManager.AddComponent(horse.Entity, ComponentType.ReadOnly<DestroyTag>());
ctx.Reply($"Horse removed.");
}
[Command("cull", adminOnly: true)]
public void Kill(ChatCommandContext ctx, float radius = 5f, float percentage = 1f)
{
var horses = HorseUtil.ClosestHorses(ctx.Event.SenderCharacterEntity, radius);
var count = horses.Count;
var toRemove = Math.Clamp((int)(count * percentage), 0, count);
var remaining = toRemove;
foreach (var horse in horses)
{
if (remaining == 0) break;
horse.WithComponentData((ref Health t) =>
{
t.Value = 0;
t.TimeOfDeath = 0;
t.IsDead = true;
});
VWorld.Server.EntityManager.AddComponent(horse, ComponentType.ReadOnly<DestroyTag>());
remaining--;
}
ctx.Reply($"Removed {toRemove} horses.");
}
}
}
}
}