Repository navigation
Expand file tree
/
Copy pathExecutionSystem.cs
More file actions
911 lines (802 loc) · 37.5 KB
/
Copy pathExecutionSystem.cs
File metadata and controls
911 lines (802 loc) · 37.5 KB
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
using System.Numerics;
using Meitou.Data.Gameplay;
using Meitou.Data.Gameplay.Ai;
using Meitou.Data.Gameplay.Combat;
using Meitou.Modding;
using Meitou.Simulation;
using Meitou.Simulation.Ai;
using Meitou.Simulation.Bodies;
using Meitou.Simulation.Combat;
using Meitou.Simulation.Crime;
using Meitou.Simulation.Dialogue;
using Meitou.Simulation.Kidnap;
using Meitou.Simulation.Movement;
using Meitou.Simulation.Player;
using Meitou.Simulation.Production;
namespace Execution;
/// <summary>
/// An order to execute a downed character. With <see cref="AsJob"/> (Shift), each of <paramref name="Attackers"/> gets the Executing job.
/// </summary>
public sealed record ExecuteOrder(IReadOnlyList<CharacterId> Attackers, CharacterId Target) : OrderCommand
{
/// <summary>
/// Give the Executing job instead of a one-off order.
/// </summary>
public bool AsJob { get; init; }
/// <summary>
/// Send every eligible attacker on an execution order, selected in the mod settings when the order was given.
/// </summary>
public bool Together { get; init; }
/// <summary>
/// A maim order: the limbs to cut off instead of killing (bit k for limb slot k: 0 left arm, 1 right arm, 2 left leg, 3 right leg); 0 executes.
/// </summary>
public int Limbs { get; init; }
}
/// <summary>
/// The AI tab's Execute Neutrals option set on or off. An order command, so a paused game applies it at once.
/// </summary>
public sealed record SetExecuteStrangers(bool On) : OrderCommand;
/// <summary>
/// Copies the Group executions setting into the simulation, which reads only this copy (the setting itself is kept per machine).
/// </summary>
public sealed record SetGroupExecutions(bool On) : OrderCommand;
/// <summary>
/// Executions: a player character walks to an unconscious (or playing dead) character and chops at it until it dies. Each blow is the game's
/// captive execution blow (<see cref="CombatSystem.Execute"/>: no block or dodge, through armour, with the usual blood, sound, death and lost
/// limbs), one per <c>chop down static</c> clip as the prison executioners swing. Every blow sets a pending Murder against the victim's faction, so
/// the game's witnesses judge it as any crime, and counts with the victim's faction as a blow on a downed body does in a fight
/// (<see cref="FactionDeeds.Blow"/>). Runs on the mod's own task type <see cref="Row"/> (claimed with the game's mod jobs hook) as a player order
/// (the Violence... menu row) or as the Executing job (Shift + Violence...), which finishes off downed bodies near the character whenever the job
/// list reaches it. Walking is <see cref="CharacterTask.GoTo"/>, chopping <see cref="CharacterTask.Idle"/> with the clip; another order or Stop ends
/// it. An execution under way is not saved; the job is (the game saves a mod job in its place in the list), and the AI tab's Execute Neutrals option
/// is, in the mod's own save data.
/// </summary>
public abstract class ExecutionSystem : ITickSystem, IOrderOwner, ITaskOwner, ITaskHandler, IJobSource, IModSaveData
{
#region Setup
/// <summary>
/// The order's and the job's task type, claimed through the mod jobs hook (<see cref="ModContext.AddJobType"/>; mod types start at
/// <see cref="SimulationExtensions.FirstModCode"/>).
/// </summary>
public const TaskType Row = (TaskType)(SimulationExtensions.FirstModCode + 229);
/// <summary>
/// The job's name in the jobs panel.
/// </summary>
public const string JobName = "Executing";
/// <summary>
/// The swing clip, the prison executioners' (<see cref="CaptiveSystem.ChopClip"/>).
/// </summary>
public const string Clip = CaptiveSystem.ChopClip;
/// <summary>
/// How near the victim the executioner must stand to swing, and how far beside it the walk aims.
/// </summary>
public const float Reach = 16, StandOff = 8;
/// <summary>
/// How far from the character the Executing job looks for bodies.
/// </summary>
public const float JobRange = 750;
/// <summary>
/// How long the job leaves a body alone after failing to reach it (seconds).
/// </summary>
public const float GiveUpSeconds = 60;
/// <summary>
/// The fraction of a swing at which its blow lands (the captives' <c>BlowAt</c>).
/// </summary>
public const float BlowAt = 0.55f;
/// <summary>
/// Walks tried before the executioner gives up on reaching the body, and blows struck before it gives up on killing it.
/// </summary>
public const int MaxWalks = 3, MaxBlows = 80;
/// <summary>
/// How many maimed bodies are watched for a death by blood loss at once (the oldest is dropped).
/// </summary>
public const int MaxMaimed = 64;
/// <summary>
/// The standard systems it works with: the blow and the relation deeds, the murder, the swing's length, the squads' memory of who fought them.
/// </summary>
readonly CombatSystem? _combat;
readonly CrimeSystem? _crimes;
readonly CaptiveSystem? _captives;
readonly DialogueSystem? _dialogue;
/// <summary>
/// The animation library and clip lengths (null headless): which weapon swings can be played and how long each lasts.
/// </summary>
readonly AnimationSystem? _animation;
/// <summary>
/// The world's body options, shared with the blow (null in a world without them): a maim blow aims at its limb through
/// <see cref="BodyOptions.ForcedHitPart"/> and cuts it off through <see cref="BodyOptions.Dismemberment"/>, both put back after the blow.
/// </summary>
readonly BodyOptions? _options;
/// <summary>
/// The maimed bodies still alive, with who maimed them and the body's faction: a death by blood loss is then a murder.
/// </summary>
readonly List<(CharacterId Victim, CharacterId Maimer, int Faction)> _maimed = [];
/// <summary>
/// The limbs of the order being started (<see cref="IOrderOwner.Start"/> hands them to the task's start, which makes the job).
/// </summary>
int _startLimbs;
/// <summary>
/// The executions under way, in the order they began.
/// </summary>
readonly List<Job> _jobs = [];
/// <summary>
/// The weapon swing each chopping executioner plays, for the animation layer (<see cref="SwingOf"/>, read on the interface's thread).
/// </summary>
readonly System.Collections.Concurrent.ConcurrentDictionary<CharacterId, (CombatTechnique Technique, long Start, long Ticks)> _swings = new();
/// <summary>
/// Bodies a character gave up on reaching, and until when (tick).
/// </summary>
readonly List<(CharacterId Attacker, CharacterId Victim, long Until)> _gaveUp = [];
/// <summary>
/// The AI tab's Execute Neutrals option: the job also goes after strangers. Set in the serial input step; the interface reads it.
/// </summary>
volatile bool _strangers;
/// <summary>
/// Group executions as the simulation sees it: set only by <see cref="SetGroupExecutions"/> and saved with the game.
/// </summary>
volatile bool _group;
/// <summary>
/// Set while the walk hands over to the swing, so the walk's end does not drop the job.
/// </summary>
bool _switching;
/// <summary>
/// One execution.
/// </summary>
sealed class Job
{
public CharacterId Attacker;
public CharacterId Victim;
public bool Chopping;
public float Time;
public int Blows;
public int Walks;
public Vector2 Goal;
public Vector2 ApproachDirection;
public bool WideApproach;
public float SwingSeconds;
public float StrikeAt;
/// <summary>
/// The limbs still to cut off (bits as in <see cref="ExecuteOrder.Limbs"/>); 0 for an execution.
/// </summary>
public int Limbs;
/// <summary>
/// Where the executioner stands for the swing; held there, as a swing's root motion would otherwise step it onto the body each loop.
/// </summary>
public Vector3 Stand;
}
/// <summary>
/// Finds the standard systems the mod works with. The game's own registration of the Executing job differs between game versions, so each
/// version has its subclass (<see cref="ExecutionSystemLegacy"/> for games before the code-mod API 3; <c>ExecutionSystemChooser</c> in
/// Execution.Chooser.dll for the others).
/// </summary>
protected ExecutionSystem(SystemBuildContext context)
{
_options = context.Data.BodyOptions;
_combat = context.Find<CombatSystem>();
_crimes = context.Find<CrimeSystem>();
_captives = context.Find<CaptiveSystem>();
_dialogue = context.Find<DialogueSystem>();
_animation = context.Find<AnimationSystem>();
}
/// <summary>
/// The system of the world made last (the running game's), for the AI tab.
/// </summary>
public static ExecutionSystem? Current { get; private set; }
/// <summary>
/// The AI tab's Execute Neutrals option.
/// </summary>
public bool Strangers => _strangers;
/// <summary>
/// The simulation's copy of the Group executions setting, for the interface to compare against the setting.
/// </summary>
public bool Group => _group;
/// <summary>
/// Keeps <paramref name="made"/> as <see cref="Current"/> and returns it (each subclass's factory ends here).
/// </summary>
public static ITickSystem Remember(ExecutionSystem made)
{
Current = made;
return made;
}
/// <summary>
/// Saves the Execute Neutrals option with the game (one byte: 1 on, 0 off).
/// </summary>
public void Write(Stream stream)
{
stream.WriteByte(_strangers ? (byte)1 : (byte)0);
stream.WriteByte(_group ? (byte)1 : (byte)0);
}
/// <summary>
/// Reads the Execute Neutrals option back (a game saved without it keeps the default, off).
/// </summary>
public void Read(Stream stream)
{
_strangers = stream.ReadByte() == 1;
_group = stream.ReadByte() == 1;
}
/// <summary>
/// Registers the row's act-out with this system.
/// </summary>
public void RegisterTasks(TaskHandlers tasks) => tasks.Register(Row, this);
#endregion
#region Who may execute whom
/// <summary>
/// A body that can be executed: alive, out cold or playing dead, not carried, not in a cage or bed.
/// </summary>
static bool Executable(CharacterCold? v) =>
v is { Medical: { Dead: false } m, Race: not null, Stats: not null } && (m.Unconscious || m.PlayingDead)
&& v.CarriedBy.IsNone && !v.IsImprisoned;
/// <summary>
/// A character that can carry out an execution: the player's, awake, able to fight, hands free, not carried or caged.
/// </summary>
static bool CanExecute(CharacterCold? a) => a is { IsPlayer: true } && Able(a);
/// <summary>
/// Any character (the player's or an NPC) fit to carry an execution on: awake, able to fight, hands free, not carried or caged.
/// </summary>
static bool Able(CharacterCold? a) =>
a is { Medical.Incapacitated: false, Fighter.CanAttack: true, Stats: not null, Race: not null }
&& a.Carrying.IsNone && a.CarriedBy.IsNone && !a.IsImprisoned;
/// <summary>
/// Whether the Executing job may take this body: never the player's faction or an ally of it; a hostile always; a stranger only when the
/// setting allows strangers. Hostile means evidence the character is against the player: its squad fought a member of the executioner's
/// squad (it tagged one TEMP_ENEMY), or, not an animal, its faction is at war with the player's (<see cref="CharacterFacts.RegardsEnemy"/>).
/// Neither a lawless faction nor the BANDIT class is enough (seen in play in The Hub: slave hunters knocked out a crowd of Holy Nation Outlaws,
/// a lawless faction of mostly civilians; taking their one bandit-class member turned the faction hostile, after which every downed member
/// counted).
/// </summary>
bool JobMayTake(World world, int slot, CharacterCold v)
{
int player = world.Player.Faction;
var relations = _crimes?.Relations;
bool known = relations is not null && player >= 0 && relations.Contains(v.Faction);
if (v.IsPlayer || v.Faction == player) return false;
if (known && relations!.IsAlly(player, v.Faction)) return false;
if (_strangers || FoughtSquad(world, slot, v)) return true;
return !CharacterFacts.Animal(v) && known && CharacterFacts.RegardsEnemy(relations, player, v.Faction);
}
/// <summary>
/// The body's squad took the executioner or one of its squad mates for an enemy (the short-term TEMP_ENEMY tag a fight leaves).
/// </summary>
bool FoughtSquad(World world, int slot, CharacterCold v)
{
if (_dialogue is null || v.SquadId < 0) return false;
var table = world.Characters;
var self = table.IdOf(slot);
if (_dialogue.Memory.HasShort(world, v.SquadId, self, MemoryTag.TempEnemy)) return true;
if (world.Squads.Find(table.Cold(slot)!.SquadId) is not { } squad) return false;
foreach (var mate in squad.Members)
if (mate != self && _dialogue.Memory.HasShort(world, v.SquadId, mate, MemoryTag.TempEnemy)) return true;
return false;
}
#endregion
#region Orders
/// <summary>
/// Takes the tick's execute orders and Execute Neutrals changes.
/// </summary>
public void Inputs(World world, IReadOnlyList<SimCommand> commands)
{
foreach (var command in commands)
{
if (command is SetExecuteStrangers set) _strangers = set.On;
if (command is SetGroupExecutions group) _group = group.On;
if (command is not ExecuteOrder order) continue;
if (order.AsJob) AddJobs(world, order);
else
Begin(world, order);
}
}
/// <summary>
/// Shift + Execute: each player character named gets the Executing job at the end of its list (once), and its jobs switch turns on.
/// </summary>
static void AddJobs(World world, ExecuteOrder order)
{
var table = world.Characters;
foreach (var id in order.Attackers)
{
if (!table.TryResolveNext(id, out int a) || table.Cold(a) is not { IsPlayer: true } c) continue;
if (c.Jobs.Exists(j => j.Type == Row)) continue;
c.Jobs.Add(PermaJob.Untargeted(Row));
c.Orders |= CharacterOrders.Jobs;
DecisionSystem.Request(c);
}
}
/// <summary>
/// Gives an execution to the whole selection if requested, or gives the order to the nearest eligible attacker.
/// </summary>
void Begin(World world, ExecuteOrder order)
{
var table = world.Characters;
if (!table.TryResolveNext(order.Target, out int v) || !Executable(table.Cold(v))) return;
var entry = new OrderEntry(Row, this) { Target = order.Target, Detail = order.Limbs == 0 ? null : order.Limbs };
if (order.Together && order.Limbs == 0)
{
foreach (var id in order.Attackers.Distinct())
{
if (!table.TryResolveNext(id, out int a) || !PlayerOrders.CanRun(world, a, entry).CanRun) continue;
PlayerOrders.Give(world, a, new OrderEntry(Row, this) { Target = order.Target }, order.Queued);
}
return;
}
var at = table.Next[v].Position;
int best = -1;
float bestDistance = float.MaxValue;
foreach (var id in order.Attackers)
{
if (!table.TryResolveNext(id, out int a) || !PlayerOrders.CanRun(world, a, entry).CanRun) continue;
float d = Vector3.DistanceSquared(table.Next[a].Position, at);
if (d >= bestDistance) continue;
(best, bestDistance) = (a, d);
}
if (best >= 0) PlayerOrders.Give(world, best, entry, order.Queued);
}
/// <summary>
/// The order runs while the victim can still be executed (else it leaves the queue) and the executioner is fit (else it waits).
/// </summary>
OrderCheck IOrderOwner.CanRun(World world, int slot, OrderEntry order)
{
var table = world.Characters;
if (!table.TryResolveNext(order.Target, out int v) || !Executable(table.Cold(v))) return OrderCheck.Gone;
return CanExecute(table.Cold(slot)) ? OrderCheck.Run : OrderCheck.Blocked();
}
/// <summary>
/// Starts the walk to the body (a maim order's limbs go with it).
/// </summary>
void IOrderOwner.Start(World world, int slot, OrderEntry order)
{
_startLimbs = order.Detail is int limbs ? limbs : 0;
TaskStart.Start(world, slot, new TaskRequest(CharacterTask.GoTo, Row, TaskSource.Order) { Target = order.Target });
_startLimbs = 0;
}
/// <summary>
/// A running execution stops when its body can no longer be executed.
/// </summary>
bool IOrderOwner.StillHolds(World world, int slot, OrderEntry order) =>
world.Characters.TryResolveNext(order.Target, out int v) && Executable(world.Characters.Cold(v));
/// <summary>
/// Never saved: vanilla saves stay as they are, and a loaded game has no execution under way.
/// </summary>
int IOrderOwner.SaveKind(OrderEntry order) => -1;
#endregion
#region The Executing job
/// <summary>
/// The job runs while its execution is under way; otherwise it can start when the character is fit and a body it may take lies within
/// <see cref="JobRange"/>.
/// </summary>
DecisionCheck IJobSource.CanRun(World world, int slot, int k)
{
var table = world.Characters;
var cold = table.Cold(slot);
var self = table.IdOf(slot);
if (cold?.Running is { Row: Row, Source: TaskSource.Job } && _jobs.Exists(j => j.Attacker == self)) return DecisionCheck.Runs;
if (!CanExecute(cold)) return DecisionCheck.No(null);
return FindBody(world, slot) >= 0 ? DecisionCheck.Run : DecisionCheck.No(null);
}
/// <summary>
/// Starts the walk to the nearest body the job may take.
/// </summary>
void IJobSource.Start(World world, int slot, int k)
{
int v = FindBody(world, slot);
if (v < 0) return;
var request = new TaskRequest(CharacterTask.GoTo, Row, TaskSource.Job) { Target = world.Characters.IdOf(v), Job = k };
TaskStart.Start(world, slot, request);
}
/// <summary>
/// The job can no longer run and nothing else can: its execution ends.
/// </summary>
void IJobSource.Stop(World world, int slot, int k) => Finish(world, slot);
/// <summary>
/// Whether a maim order is under way on <paramref name="victim"/> (a maim is never joined). A plain loop: no closure on the search's path.
/// </summary>
bool Maimed(CharacterId victim)
{
foreach (var j in _jobs)
if (j.Victim == victim && j.Limbs != 0) return true;
return false;
}
/// <summary>
/// The nearest eligible unclaimed body within range. With group executions on, the job can help with the nearest existing execution
/// when no eligible unclaimed body is available.
/// </summary>
int FindBody(World world, int slot)
{
var table = world.Characters;
var self = table.IdOf(slot);
var at = table.Next[slot].Position;
bool canHelp = _group;
int best = -1, help = -1;
float bestDistance = JobRange * JobRange;
float helpDistance = bestDistance;
for (int v = 0; v < table.HighWater; v++)
{
if (v == slot || !table.Next[v].Alive) continue;
float d = Vector3.DistanceSquared(table.Next[v].Position, at);
if (d >= bestDistance || table.Cold(v) is not { } vc || !Executable(vc)) continue;
var id = table.IdOf(v);
bool claimed = Claimed(self, id);
if (claimed && (!canHelp || d >= helpDistance || Maimed(id))) continue;
if (GaveUp(world, self, id) || !JobMayTake(world, slot, vc)) continue;
if (claimed) (help, helpDistance) = (v, d);
else (best, bestDistance) = (v, d);
}
return best >= 0 ? best : help;
}
/// <summary>
/// Another character's execution is already on this body.
/// </summary>
bool Claimed(CharacterId self, CharacterId victim) => _jobs.Exists(j => j.Victim == victim && j.Attacker != self);
/// <summary>
/// The character gave up on reaching this body within the last <see cref="GiveUpSeconds"/>.
/// </summary>
bool GaveUp(World world, CharacterId self, CharacterId victim)
{
_gaveUp.RemoveAll(g => g.Until < world.Tick);
return _gaveUp.Exists(g => g.Attacker == self && g.Victim == victim);
}
#endregion
#region Task
/// <summary>
/// The walk's start makes the job and sets off; the swing's start stands the executioner still with the clip.
/// </summary>
void ITaskHandler.Start(World world, int slot, in TaskRequest request)
{
var table = world.Characters;
var attacker = table.IdOf(slot);
if (request.Task == CharacterTask.Idle)
{
MovementSystem.Stop(table, slot);
table.Cold(slot)!.ActionClip = Clip;
return;
}
_jobs.RemoveAll(j => j.Attacker == attacker);
var job = new Job { Attacker = attacker, Victim = request.Target, Limbs = _startLimbs };
_jobs.Add(job);
if (table.TryResolveNext(request.Target, out int v)) Walk(world, slot, v, job);
}
/// <summary>
/// Another order, Stop, or the end of the execution: the job goes and the clip with it (not while the walk hands over to the swing).
/// </summary>
void ITaskHandler.End(World world, int slot, CharacterTask task, TaskEnd reason)
{
if (_switching) return;
Drop(world.Characters, slot);
}
/// <summary>
/// The task is done once its job is gone.
/// </summary>
bool ITaskHandler.Done(World world, int slot, CharacterTask task)
{
var attacker = world.Characters.IdOf(slot);
return world.Characters.Next[slot].Task != (byte)task || !_jobs.Exists(j => j.Attacker == attacker);
}
/// <summary>
/// Forgets the slot's job and clears its swing clip.
/// </summary>
void Drop(CharacterTable table, int slot)
{
var attacker = table.IdOf(slot);
_jobs.RemoveAll(j => j.Attacker == attacker);
_swings.TryRemove(attacker, out _);
if (table.Cold(slot) is { } c && c.ActionClip == Clip) c.ActionClip = null;
}
#endregion
#region Each tick
/// <summary>
/// Advances every execution: the walk until in reach, then the swings until the victim dies.
/// </summary>
public void SlowWorld(World world)
{
for (int k = _jobs.Count - 1; k >= 0; k--)
{
if (k >= _jobs.Count) continue;
Step(world, _jobs[k]);
}
WatchMaimed(world);
}
/// <summary>
/// One tick of one execution.
/// </summary>
void Step(World world, Job job)
{
var table = world.Characters;
if (!table.TryResolveNext(job.Attacker, out int a))
{
_jobs.Remove(job);
return;
}
if (!Able(table.Cold(a)) || !table.TryResolveNext(job.Victim, out int v) || !Executable(table.Cold(v)))
{
Finish(world, a);
return;
}
if (job.Chopping) Chop(world, job, a, v);
else
Approach(world, job, a, v);
}
/// <summary>
/// The walk: on reaching the body the swing starts; a walk that ended short is tried again, then given up.
/// </summary>
void Approach(World world, Job job, int a, int v)
{
var table = world.Characters;
ref var n = ref table.Next[a];
var victim = table.Next[v].Position;
bool walking = n.Task == (byte)CharacterTask.GoTo;
// The swing starts as the walk brakes into its stand-off beside the body (the swing then holds it there), still on the walk so its end
// does not finish the task; a walk that stopped short but within reach swings from where it stands.
// A spread-out approach (Group executions) swings once within the same distance but only on its own side of the body (within 60 degrees
// of its approach), so a walk stopped short by a crowd or moved by the floor still swings, and one still walking round does not.
var offset = Flat(n.Position) - Flat(victim);
bool atSwingSpot = offset.Length() <= StandOff + 3
&& (!job.WideApproach || offset.LengthSquared() < 1e-4f || Vector2.Dot(Vector2.Normalize(offset), job.ApproachDirection) >= 0.5f);
if (InReach(n.Position, victim) && (!walking || atSwingSpot))
{
// One only playing dead does not lie still for the blade: it gets up as the executioner stands over it, and there is no execution.
if (table.Cold(v)!.Medical is { PlayingDead: true } faking)
{
faking.StandUp();
Finish(world, a);
return;
}
StartSwing(world, job, a);
return;
}
bool moved = Vector2.Distance(job.Goal, Flat(victim)) > StandOff;
if (walking && !moved) return;
if (++job.Walks > MaxWalks)
{
world.Bark(job.Attacker, "Can't reach them.");
_gaveUp.Add((job.Attacker, job.Victim, world.Tick + (long)(GiveUpSeconds / world.TickSeconds)));
Finish(world, a);
return;
}
Walk(world, a, v, job);
}
/// <summary>
/// The walk hands over to the swing: the same order or job, now standing with the clip.
/// </summary>
void StartSwing(World world, Job job, int a)
{
var running = world.Characters.Cold(a)!.Running;
var request = new TaskRequest(CharacterTask.Idle, Row, running?.Source ?? TaskSource.Order)
{
Target = job.Victim,
Job = running?.Job ?? -1,
};
_switching = true;
try
{
TaskStart.Start(world, a, request);
}
finally
{
_switching = false;
}
job.Chopping = true;
job.Time = 0;
job.Stand = world.Characters.Next[a].Position;
var cold = world.Characters.Cold(a)!;
var (technique, seconds, strikeAt) = SwingFor(cold);
(job.SwingSeconds, job.StrikeAt) = (seconds, strikeAt);
if (technique is null) return;
// A weapon swing replaces the executioners' chop: the animation asks the mod's combat move what the character plays (ExecutionMod.Swing).
cold.ActionClip = null;
_swings[job.Attacker] = (technique, world.Tick, Math.Max(1, (long)MathF.Round(seconds / world.TickSeconds)));
}
/// <summary>
/// The swing for the executioner's weapon (<see cref="ExecutionMod.Swings"/> by its technique category): the first candidate the animation
/// library has and the character's own rig can play, with its length (the clip's over the technique's speed) and the fraction at which its
/// first blow strikes. Otherwise none, keeping the executioners' chop (<see cref="Clip"/>) and <see cref="BlowAt"/>.
/// </summary>
(CombatTechnique? Technique, float Seconds, float StrikeAt) SwingFor(CharacterCold c)
{
float chop = MathF.Max(_captives?.ChopSeconds ?? 1.5f, 0.1f);
if (c.Fighter is not { } fighter || _animation is null || !ExecutionMod.Swings.TryGetValue(fighter.TechniqueCategory, out var candidates))
{
return (null, chop, BlowAt);
}
foreach (var t in candidates)
{
int index = _animation.Library.IndexOfTechnique(t.StringId);
if (index < 0 || !CanPlay(c, index)) continue;
float seconds = _animation.Lengths.For(c.Race?.StringId).Of(t.AnimName) / MathF.Max(t.AnimSpeedMult, 0.1f);
return (t, MathF.Max(seconds, 0.3f), Math.Clamp(t.StrikeProgress(0), 0.1f, 0.95f));
}
return (null, chop, BlowAt);
}
/// <summary>
/// Whether <paramref name="c"/>'s skeleton rig has the clip of animation definition <paramref name="index"/> (the same test
/// <see cref="AnimationSystem"/> applies before playing a technique).
/// </summary>
bool CanPlay(CharacterCold c, int index)
{
if (_animation?.Rigs is not { } rigs || c.Appearance is not { } look) return true;
return rigs.Plays(look, _animation.Library.Definitions[index].Name);
}
/// <summary>
/// The weapon swing <paramref name="id"/> plays at <paramref name="tick"/> and how far through it (looping), for the animation layer; null when
/// it is not chopping with a weapon swing. Any thread.
/// </summary>
public (CombatTechnique Technique, float Progress, bool IsAttack)? SwingOf(CharacterId id, long tick)
{
if (!_swings.TryGetValue(id, out var s)) return null;
long into = Math.Max(0, tick - s.Start) % s.Ticks;
return (s.Technique, into / (float)s.Ticks, true);
}
/// <summary>
/// The swing: facing the body, one blow per swing at its strike moment, each booking the murder and counting with the victim's faction;
/// the victim's death ends it. A body dragged out of reach ends it too.
/// </summary>
void Chop(World world, Job job, int a, int v)
{
var table = world.Characters;
ref var n = ref table.Next[a];
var victim = table.Next[v].Position;
if (n.Task != (byte)CharacterTask.Idle || table.Cold(a)!.Running is not { Row: Row } || !InReach(n.Position, victim) || _combat is null)
{
Finish(world, a);
return;
}
// An attack plays with root motion (the animation layer moves the character by it, and a downed body is not in the way), so each looped
// swing would carry the executioner further onto the body: it stays where it started swinging.
n.Position = job.Stand;
Face(ref n, victim);
// A weapon swing is made with the weapon in hand: drawn every tick, as the combat step puts a player character's weapon away while it idles.
if (_swings.ContainsKey(job.Attacker) && table.Cold(a)!.Fighter is { Unarmed: false, Weapon: { } weapon })
table.Cold(a)!.DrawnWeapon = HandHold.OfCategory(weapon.Data.AnimationCategory).Weapon;
job.Time += world.TickSeconds;
if (job.Time < (job.Blows + job.StrikeAt) * job.SwingSeconds) return;
var vc = table.Cold(v)!;
bool maim = job.Limbs != 0;
HealthPart? limb = null;
if (maim && (limb = NextLimb(vc.Medical!, ref job.Limbs)) is null)
{
Finish(world, a);
return;
}
job.Blows++;
// A maim books Assault, an execution Murder; a maimed body that later bleeds to death is a murder too (WatchMaimed).
_crimes?.SetPending(world, a, maim ? CrimeCode.Assault : CrimeCode.Murder, vc.Faction, job.Victim, CrimeSystem.WitnessTimer);
if (maim) Maimed(job.Victim, job.Attacker, vc.Faction);
bool died = limb is null ? _combat.Execute(world, a, v, job.Blows) : Maim(world, a, v, job.Blows, limb);
_combat.Deeds?.Blow(world, a, v, down: true, dead: died);
// A maim stops the moment its last chosen limb is off.
if (died || job.Blows >= MaxBlows || maim && NextLimb(vc.Medical!, ref job.Limbs) is null) Finish(world, a);
}
/// <summary>
/// The next chosen limb that a blow can still cut off, in slot order (left arm, right arm, left leg, right leg); the others leave
/// <paramref name="limbs"/>: one already off (a stump) or a prosthetic, one the body has no part for, and one wrecked past cutting while still
/// on (its flesh at minus its maximum or below, which a blow can no longer land on). Null when none is left.
/// </summary>
static HealthPart? NextLimb(MedicalState medical, ref int limbs)
{
for (int k = 0; k < 4; k++)
{
if ((limbs & (1 << k)) == 0) continue;
if (medical.LimbPart(k) is { Limb: LimbState.Original or LimbState.Crushed } p && p.Flesh > -p.MaxHp) return p;
limbs &= ~(1 << k);
}
return null;
}
/// <summary>
/// A maim blow: the execution blow aimed at <paramref name="limb"/>, and able to cut it off even with the game's Dismemberment option at Never
/// (at Rare, the default, and Frequent the setting is left as it is). The options are put back after the blow, so every other blow keeps the
/// player's setting, and the debug menu's forced part with it. True when the body died of it.
/// </summary>
bool Maim(World world, int a, int v, int blow, HealthPart limb)
{
if (_options is not { } options) return _combat!.Execute(world, a, v, blow);
var (forced, setting) = (options.ForcedHitPart, options.Dismemberment);
options.ForcedHitPart = (limb.Template.StringId, limb.Template.Name);
if (setting < Dismemberment.Rare) options.Dismemberment = Dismemberment.Rare;
try
{
return _combat!.Execute(world, a, v, blow);
}
finally
{
options.ForcedHitPart = forced;
options.Dismemberment = setting;
}
}
/// <summary>
/// Remembers that <paramref name="maimer"/> maimed <paramref name="victim"/> of <paramref name="faction"/> (the latest maimer counts).
/// </summary>
void Maimed(CharacterId victim, CharacterId maimer, int faction)
{
_maimed.RemoveAll(m => m.Victim == victim);
if (_maimed.Count >= MaxMaimed) _maimed.RemoveAt(0);
_maimed.Add((victim, maimer, faction));
}
/// <summary>
/// The maimed bodies: one that has died of blood loss is a murder by its maimer, set as a pending Murder for witnesses to judge; when the
/// body's faction already wants the maimer (it saw the maiming), the Murder is booked with it at once. One that died otherwise, or is gone, is
/// forgotten. Not saved: a loaded game watches nobody.
/// </summary>
void WatchMaimed(World world)
{
var table = world.Characters;
for (int k = _maimed.Count - 1; k >= 0; k--)
{
var (victim, maimer, faction) = _maimed[k];
bool found = table.TryResolveNext(victim, out int v);
if (found && table.Cold(v)?.Medical is { Dead: false }) continue;
_maimed.RemoveAt(k);
if (_crimes is null || !found || table.Cold(v)?.Medical is not { Cause: DeathCause.BloodLoss }) continue;
if (!table.TryResolveNext(maimer, out int a)) continue;
bool known = table.Cold(a) is { } culprit && _crimes.IsWantedBy(culprit, faction);
if (_crimes.SetPending(world, a, CrimeCode.Murder, faction, victim, CrimeSystem.WitnessTimer) && known)
_crimes.Book(world, a, faction, CharacterId.None);
}
}
/// <summary>
/// Ends the execution: the job goes and, while its task still runs, the task ends as done (an order leaves the queue; a job is decided again).
/// </summary>
void Finish(World world, int a)
{
var table = world.Characters;
var cold = table.Cold(a)!;
bool running = cold.Running is { Row: Row };
Drop(table, a);
if (running) TaskStart.End(world, a, TaskEnd.Done);
}
#endregion
#region Geometry
/// <summary>
/// Sets off for a point beside the body, on its floor. Executioners approaching the same body spread out around it.
/// </summary>
void Walk(World world, int a, int v, Job job)
{
var table = world.Characters;
var from = table.Next[a].Position;
var body = table.Next[v].Position;
var away = Flat(from) - Flat(body);
var dir = away.LengthSquared() > 1e-4f ? Vector2.Normalize(away) : Vector2.UnitX;
job.ApproachDirection = _group ? OpenApproach(job, dir) : dir;
job.WideApproach = job.ApproachDirection != dir;
dir = job.ApproachDirection;
var goal = new Vector3(body.X + dir.X * StandOff, body.Y, body.Z + dir.Y * StandOff);
MovementSystem.GoToFloor(world, a, goal, table.Cold(a)!.RunSpeed);
job.Goal = Flat(body);
}
/// <summary>
/// Prefer the executioner's own side. If someone is already approaching from there, try the neighboring sides first.
/// </summary>
Vector2 OpenApproach(Job job, Vector2 preferred)
{
var best = preferred;
float bestCrowding = float.PositiveInfinity;
foreach (int offset in (ReadOnlySpan<int>)[0, 1, -1, 2, -2, 3])
{
var direction = offset == 0 ? preferred : Vector2.Transform(preferred, Matrix3x2.CreateRotation(offset * (MathF.PI / 3)));
float crowding = -1;
foreach (var other in _jobs)
{
if (other == job || other.Victim != job.Victim) continue;
crowding = MathF.Max(crowding, Vector2.Dot(direction, other.ApproachDirection));
}
if (crowding <= 0.51f) return direction;
if (crowding >= bestCrowding) continue;
(best, bestCrowding) = (direction, crowding);
}
return best;
}
/// <summary>
/// Within <see cref="Reach"/> across the ground and on the same floor.
/// </summary>
static bool InReach(Vector3 at, Vector3 body) =>
Vector2.Distance(Flat(at), Flat(body)) <= Reach && MathF.Abs(at.Y - body.Y) <= MovementSystem.FloorSlack;
/// <summary>
/// The ground position (X/Z).
/// </summary>
static Vector2 Flat(Vector3 p) => new(p.X, p.Z);
/// <summary>
/// Turns the executioner to face the body.
/// </summary>
static void Face(ref CharacterHot n, Vector3 body)
{
float dx = body.X - n.Position.X, dz = body.Z - n.Position.Z;
if (dx * dx + dz * dz > 1e-4f) n.Yaw = MathF.Atan2(dx, dz);
}
#endregion
}