-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAutoBellowsSystem.cs
More file actions
1409 lines (1195 loc) · 48.5 KB
/
AutoBellowsSystem.cs
File metadata and controls
1409 lines (1195 loc) · 48.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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Reflection;
using Vintagestory.API.Config;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.MathTools;
using Vintagestory.GameContent;
namespace AutoBellows
{
public class AutoBellowsSystem : ModSystem
{
private const string HotkeyCode = "autobellows-toggle";
private const string PouringHotkeyCode = "autopouring-toggle";
private const string SettingsHotkeyCode = "autobellows-settings";
private const string ConfigFileName = "AutoBellows.json";
private const int BellowsScanRadius = 30;
private const int BellowsScanRadiusSquared = BellowsScanRadius * BellowsScanRadius;
private const int PourScanRadius = 50;
private const int PourScanRadiusSquared = PourScanRadius * PourScanRadius;
private const int PumpIntervalMs = 215;
private const int ScanIntervalMs = 1000;
private const int MaxInteractionsPerTick = 256;
private const int PourScanIntervalMs = 1000;
private const int PourBurstCooldownMs = 1200;
private const int PourWarmupSteps = 36;
private const int PourSafetySteps = 6;
private const int PourUnitsPerStep = 2;
private const int MaxPourTargetsPerScan = 256;
private static readonly FieldInfo? ToolMoldRequiredUnitsField =
typeof(BlockEntityToolMold).GetField("requiredUnits", BindingFlags.Instance | BindingFlags.NonPublic);
private readonly List<BlockPos> bellowsPositions = new List<BlockPos>();
private readonly List<PourTarget> pourTargets = new List<PourTarget>();
private ICoreClientAPI? capi;
private AutoBellowsSettings settings = new AutoBellowsSettings();
private AutoBellowsSettingsDialog? settingsDialog;
private bool enabled;
private bool pouringEnabled;
private long tickListenerId;
private long nextScanAtMs;
private long nextPourScanAtMs;
private long nextPourActionAtMs;
public override bool ShouldLoad(EnumAppSide forSide)
{
return forSide == EnumAppSide.Client;
}
public override void StartClientSide(ICoreClientAPI api)
{
capi = api;
settings = LoadSettings(api);
api.Input.RegisterHotKey(
HotkeyCode,
"Toggle Auto Bellows",
GlKeys.Z,
HotkeyType.GUIOrOtherControls,
false,
true,
false
);
api.Input.SetHotKeyHandler(HotkeyCode, OnToggleKey);
api.Input.RegisterHotKey(
PouringHotkeyCode,
"Toggle Auto Pouring",
GlKeys.X,
HotkeyType.GUIOrOtherControls,
false,
true,
false
);
api.Input.SetHotKeyHandler(PouringHotkeyCode, OnTogglePouringKey);
api.Input.RegisterHotKey(
SettingsHotkeyCode,
"Auto Bellows Settings",
GlKeys.F1,
HotkeyType.GUIOrOtherControls,
false,
false,
false
);
api.Input.SetHotKeyHandler(SettingsHotkeyCode, OnSettingsKey);
settingsDialog = new AutoBellowsSettingsDialog(api, this);
tickListenerId = api.Event.RegisterGameTickListener(OnClientTick, PumpIntervalMs);
}
private bool OnToggleKey(KeyCombination comb)
{
if (capi?.World?.Player?.Entity == null)
{
return true;
}
SetBellowsEnabled(!enabled);
return true;
}
private bool OnTogglePouringKey(KeyCombination comb)
{
if (capi?.World?.Player?.Entity == null)
{
return true;
}
SetPouringEnabled(!pouringEnabled);
return true;
}
private bool OnSettingsKey(KeyCombination comb)
{
if (capi == null)
{
return true;
}
settingsDialog ??= new AutoBellowsSettingsDialog(capi, this);
if (settingsDialog.IsOpened())
{
settingsDialog.TryClose();
}
else
{
settingsDialog.TryOpen(true);
}
return true;
}
private void OnClientTick(float dt)
{
if (capi == null || capi.IsGamePaused || capi.World?.Player?.Entity == null)
{
return;
}
long now = capi.ElapsedMilliseconds;
if (enabled)
{
if (now >= nextScanAtMs)
{
ScanNearbyBellows();
nextScanAtMs = now + ScanIntervalMs;
}
PumpKnownBellows();
}
if (pouringEnabled)
{
HandleAutoPouring(now);
}
}
private void ScanNearbyBellows()
{
if (capi?.World?.Player?.Entity == null)
{
return;
}
bellowsPositions.Clear();
BlockPos playerPos = capi.World.Player.Entity.Pos.AsBlockPos;
IBlockAccessor blockAccessor = capi.World.BlockAccessor;
for (int dx = -BellowsScanRadius; dx <= BellowsScanRadius; dx++)
{
for (int dy = -BellowsScanRadius; dy <= BellowsScanRadius; dy++)
{
for (int dz = -BellowsScanRadius; dz <= BellowsScanRadius; dz++)
{
if (dx * dx + dy * dy + dz * dz > BellowsScanRadiusSquared)
{
continue;
}
BlockPos pos = new BlockPos(
playerPos.X + dx,
playerPos.Y + dy,
playerPos.Z + dz,
playerPos.dimension
);
if (!blockAccessor.IsValidPos(pos))
{
continue;
}
Block block = blockAccessor.GetBlock(pos);
if (IsBellows(block))
{
bellowsPositions.Add(pos);
}
}
}
}
}
private void PumpKnownBellows()
{
if (capi == null)
{
return;
}
IBlockAccessor blockAccessor = capi.World.BlockAccessor;
int sent = 0;
foreach (BlockPos pos in bellowsPositions)
{
if (sent >= MaxInteractionsPerTick)
{
break;
}
Block block = blockAccessor.GetBlock(pos);
if (!IsBellows(block))
{
continue;
}
SendRightClick(pos, block);
sent++;
}
}
private void SendRightClick(BlockPos pos, Block block)
{
if (capi == null)
{
return;
}
BlockSelection blockSelection = new BlockSelection(pos.Copy(), GetFacingFromCode(block), block)
{
HitPosition = GetDebugHitPosition(pos),
SelectionBoxIndex = 0
};
try
{
block.OnBlockInteractStart(capi.World, capi.World.Player, blockSelection);
}
catch
{
}
capi.Network.SendHandInteraction(
(int)EnumMouseButton.Right,
blockSelection,
null,
EnumHandInteract.BlockInteract,
(int)EnumHandInteractNw.StartBlockUse,
true,
EnumItemUseCancelReason.ReleasedMouse
);
}
private void HandleAutoPouring(long now)
{
if (capi == null)
{
return;
}
if (IsManualHandUseActive())
{
nextPourActionAtMs = now + 250;
return;
}
if (now < nextPourActionAtMs)
{
return;
}
if (now >= nextPourScanAtMs)
{
ScanPouringTargets();
nextPourScanAtMs = now + PourScanIntervalMs;
}
BurstPourTargets(now);
}
private void ScanPouringTargets()
{
if (capi?.World?.Player?.Entity == null)
{
return;
}
pourTargets.Clear();
PourSource? source = GetActivePourSource();
if (source == null)
{
int placedContainers = CountNearbySmeltedContainers();
LogPour("skipped scan: no active hotbar BlockSmeltedContainer with liquid metal; nearby placed smelted containers=" + placedContainers + ". Vanilla pouring uses held-item interaction.");
return;
}
if (!settings.AllowIngotMolds && !settings.AllowToolMolds)
{
LogPour("skipped scan: all pouring target mold types are disabled");
return;
}
List<PourTarget> candidates = new List<PourTarget>();
ScanNearbyMolds(source, candidates, out int moldSlots, out int partialSlots, out int emptySlots, out int fullSlots, out int skippedSlots);
candidates.Sort(ComparePourTargets);
int remainingUnits = source.Units;
PourTarget? leftoverTarget = null;
foreach (PourTarget target in candidates)
{
if (pourTargets.Count >= MaxPourTargetsPerScan)
{
LogPour("scan limit reached: " + MaxPourTargetsPerScan + " planned targets");
break;
}
if (target.NeededUnits > remainingUnits)
{
if (remainingUnits > 0 && leftoverTarget == null)
{
leftoverTarget = target;
}
continue;
}
target.TransferUnits = target.NeededUnits;
pourTargets.Add(target);
remainingUnits -= target.NeededUnits;
}
if (remainingUnits > 0 && leftoverTarget != null && pourTargets.Count < MaxPourTargetsPerScan)
{
leftoverTarget.TransferUnits = remainingUnits;
leftoverTarget.IsLeftoverPour = true;
pourTargets.Add(leftoverTarget);
remainingUnits = 0;
}
int reserved = source.Units - remainingUnits;
LogPour("found active crucible: metal=" + FormatStack(source.MetalStack) + ", units=" + source.Units);
LogPour("found molds: slots=" + moldSlots + ", partial=" + partialSlots + ", empty=" + emptySlots + ", full=" + fullSlots + ", skipped=" + skippedSlots);
LogPour("calculated planned pours=" + pourTargets.Count + ", fullFills=" + CountFullFillTargets() + ", leftoverPours=" + CountLeftoverTargets() + ", reservedUnits=" + reserved + ", unitsAfterPlan=" + remainingUnits);
}
private void ScanNearbyMolds(PourSource source, List<PourTarget> candidates, out int moldSlots, out int partialSlots, out int emptySlots, out int fullSlots, out int skippedSlots)
{
moldSlots = 0;
partialSlots = 0;
emptySlots = 0;
fullSlots = 0;
skippedSlots = 0;
if (capi?.World?.Player?.Entity == null)
{
return;
}
BlockPos playerPos = capi.World.Player.Entity.Pos.AsBlockPos;
IBlockAccessor blockAccessor = capi.World.BlockAccessor;
for (int dx = -PourScanRadius; dx <= PourScanRadius; dx++)
{
for (int dy = -PourScanRadius; dy <= PourScanRadius; dy++)
{
for (int dz = -PourScanRadius; dz <= PourScanRadius; dz++)
{
if (dx * dx + dy * dy + dz * dz > PourScanRadiusSquared)
{
continue;
}
BlockPos pos = new BlockPos(
playerPos.X + dx,
playerPos.Y + dy,
playerPos.Z + dz,
playerPos.dimension
);
if (!blockAccessor.IsValidPos(pos))
{
continue;
}
BlockEntity blockEntity = blockAccessor.GetBlockEntity(pos);
if (blockEntity is BlockEntityToolMold toolMold)
{
if (!settings.AllowToolMolds)
{
continue;
}
moldSlots++;
AddToolMoldTarget(pos, toolMold, source, candidates, ref partialSlots, ref emptySlots, ref fullSlots, ref skippedSlots);
}
else if (blockEntity is BlockEntityIngotMold ingotMold)
{
if (!settings.AllowIngotMolds)
{
continue;
}
AddIngotMoldTargets(pos, ingotMold, source, candidates, ref moldSlots, ref partialSlots, ref emptySlots, ref fullSlots, ref skippedSlots);
}
}
}
}
}
private void AddToolMoldTarget(BlockPos pos, BlockEntityToolMold mold, PourSource source, List<PourTarget> candidates, ref int partialSlots, ref int emptySlots, ref int fullSlots, ref int skippedSlots)
{
if (!mold.CanReceiveAny)
{
skippedSlots++;
LogPour("skip tool mold at " + FormatPos(pos) + ": CanReceiveAny=false");
return;
}
if (mold.IsFull)
{
fullSlots++;
return;
}
if (!mold.CanReceive(source.MetalStack))
{
skippedSlots++;
LogPour("skip tool mold at " + FormatPos(pos) + ": cannot receive " + FormatStack(source.MetalStack));
return;
}
int requiredUnits = GetToolMoldRequiredUnits(mold);
if (requiredUnits <= 0)
{
skippedSlots++;
LogPour("skip tool mold at " + FormatPos(pos) + ": invalid requiredUnits=" + requiredUnits);
return;
}
int fillLevel = Math.Max(0, mold.FillLevel);
int neededUnits = requiredUnits - fillLevel;
if (neededUnits <= 0)
{
fullSlots++;
return;
}
bool isPartial = fillLevel > 0;
if (isPartial)
{
partialSlots++;
}
else
{
emptySlots++;
}
candidates.Add(new PourTarget(
pos.Copy(),
"tool mold",
neededUnits,
isPartial,
GetDistanceSqToPlayer(pos),
null,
new Vec3d(0.5, 0.5, 0.5)
));
}
private void AddIngotMoldTargets(BlockPos pos, BlockEntityIngotMold mold, PourSource source, List<PourTarget> candidates, ref int moldSlots, ref int partialSlots, ref int emptySlots, ref int fullSlots, ref int skippedSlots)
{
AddIngotMoldSideTarget(pos, mold, source, candidates, false, ref moldSlots, ref partialSlots, ref emptySlots, ref fullSlots, ref skippedSlots);
if (mold.QuantityMolds > 1)
{
AddIngotMoldSideTarget(pos, mold, source, candidates, true, ref moldSlots, ref partialSlots, ref emptySlots, ref fullSlots, ref skippedSlots);
}
}
private void AddIngotMoldSideTarget(BlockPos pos, BlockEntityIngotMold mold, PourSource source, List<PourTarget> candidates, bool rightSide, ref int moldSlots, ref int partialSlots, ref int emptySlots, ref int fullSlots, ref int skippedSlots)
{
moldSlots++;
string sideName = rightSide ? "right" : "left";
ItemStack? moldStack = rightSide ? mold.MoldRight : mold.MoldLeft;
if (moldStack == null)
{
skippedSlots++;
LogPour("skip ingot mold " + sideName + " at " + FormatPos(pos) + ": no mold item");
return;
}
if (rightSide ? mold.ShatteredRight : mold.ShatteredLeft)
{
skippedSlots++;
LogPour("skip ingot mold " + sideName + " at " + FormatPos(pos) + ": shattered");
return;
}
if (!IsFiredOrBurnedMold(moldStack))
{
skippedSlots++;
LogPour("skip ingot mold " + sideName + " at " + FormatPos(pos) + ": mold is not fired/burned");
return;
}
bool isFull = rightSide ? mold.IsFullRight : mold.IsFullLeft;
if (isFull)
{
fullSlots++;
return;
}
ItemStack? contents = rightSide ? mold.ContentsRight : mold.ContentsLeft;
if (contents != null && !StacksEqual(contents, source.MetalStack))
{
skippedSlots++;
LogPour("skip ingot mold " + sideName + " at " + FormatPos(pos) + ": contains " + FormatStack(contents) + ", source=" + FormatStack(source.MetalStack));
return;
}
if (contents == null && !mold.CanReceive(source.MetalStack))
{
skippedSlots++;
LogPour("skip ingot mold " + sideName + " at " + FormatPos(pos) + ": CanReceive=false for " + FormatStack(source.MetalStack));
return;
}
int requiredUnits = mold.RequiredUnits;
int fillLevel = Math.Max(0, rightSide ? mold.FillLevelRight : mold.FillLevelLeft);
int neededUnits = requiredUnits - fillLevel;
if (requiredUnits <= 0 || neededUnits <= 0)
{
fullSlots++;
return;
}
if (!TryGetLocalIngotHitForSide(mold, rightSide, out Vec3d localHit))
{
skippedSlots++;
LogPour("skip ingot mold " + sideName + " at " + FormatPos(pos) + ": could not resolve local hit side");
return;
}
if (!TryBuildIngotHitPosition(pos, mold, rightSide, localHit, out Vec3d hitPosition, out string skipReason))
{
skippedSlots++;
LogPour("skip ingot mold " + sideName + " at " + FormatPos(pos) + ": " + skipReason);
return;
}
bool isPartial = fillLevel > 0;
if (isPartial)
{
partialSlots++;
}
else
{
emptySlots++;
}
candidates.Add(new PourTarget(
pos.Copy(),
"ingot mold " + sideName,
neededUnits,
isPartial,
GetDistanceSqToPlayer(pos),
rightSide,
hitPosition
));
}
private void BurstPourTargets(long now)
{
if (capi == null || pourTargets.Count == 0)
{
return;
}
if (IsManualHandUseActive())
{
LogPour("cooldown wait: player is already using held item/block, skipping burst");
return;
}
PourSource? source = GetActivePourSource();
if (source == null)
{
pourTargets.Clear();
LogPour("skipped burst: active hotbar item is no longer a liquid metal crucible");
return;
}
int sent = 0;
int sentUnits = 0;
foreach (PourTarget target in pourTargets)
{
if (!RefreshPourTarget(target, source, out string skipReason))
{
LogPour("skip target " + target.Label + " at " + FormatPos(target.Position) + ": " + skipReason);
continue;
}
if (target.TransferUnits <= 0)
{
LogPour("skip target " + target.Label + " at " + FormatPos(target.Position) + ": no planned transfer units");
continue;
}
SendPourBurst(target);
sent++;
sentUnits += target.TransferUnits;
}
pourTargets.Clear();
nextPourActionAtMs = now + PourBurstCooldownMs;
nextPourScanAtMs = now + PourBurstCooldownMs;
LogPour("burst sent: targets=" + sent + ", plannedUnits=" + sentUnits + ", cooldownMs=" + PourBurstCooldownMs);
}
private bool RefreshPourTarget(PourTarget target, PourSource source, out string skipReason)
{
skipReason = "";
if (capi == null)
{
skipReason = "client API unavailable";
return false;
}
BlockEntity blockEntity = capi.World.BlockAccessor.GetBlockEntity(target.Position);
if (blockEntity is BlockEntityToolMold toolMold)
{
if (!settings.AllowToolMolds)
{
skipReason = "tool mold target type disabled";
return false;
}
if (!toolMold.CanReceiveAny || toolMold.IsFull || !toolMold.CanReceive(source.MetalStack))
{
skipReason = "tool mold no longer receivable";
return false;
}
int requiredUnits = GetToolMoldRequiredUnits(toolMold);
int neededUnits = requiredUnits - Math.Max(0, toolMold.FillLevel);
if (neededUnits <= 0)
{
skipReason = "tool mold already full";
return false;
}
target.NeededUnits = neededUnits;
target.TransferUnits = Math.Min(target.TransferUnits, neededUnits);
target.HitPosition = GetDebugHitPosition(target.Position);
return true;
}
if (blockEntity is BlockEntityIngotMold ingotMold && target.IngotRightSide.HasValue)
{
if (!settings.AllowIngotMolds)
{
skipReason = "ingot mold target type disabled";
return false;
}
bool rightSide = target.IngotRightSide.Value;
bool isFull = rightSide ? ingotMold.IsFullRight : ingotMold.IsFullLeft;
ItemStack? contents = rightSide ? ingotMold.ContentsRight : ingotMold.ContentsLeft;
if (isFull)
{
skipReason = "ingot mold side already full";
return false;
}
if (contents != null && !StacksEqual(contents, source.MetalStack))
{
skipReason = "ingot mold side now contains another metal";
return false;
}
if (contents == null && !ingotMold.CanReceive(source.MetalStack))
{
skipReason = "ingot mold no longer accepts source metal";
return false;
}
int fillLevel = Math.Max(0, rightSide ? ingotMold.FillLevelRight : ingotMold.FillLevelLeft);
int neededUnits = ingotMold.RequiredUnits - fillLevel;
if (neededUnits <= 0)
{
skipReason = "ingot mold side has no remaining capacity";
return false;
}
if (!TryGetLocalIngotHitForSide(ingotMold, rightSide, out Vec3d localHit)
|| !TryBuildIngotHitPosition(target.Position, ingotMold, rightSide, localHit, out Vec3d hitPosition, out skipReason))
{
return false;
}
target.NeededUnits = neededUnits;
target.TransferUnits = Math.Min(target.TransferUnits, neededUnits);
target.HitPosition = hitPosition;
return true;
}
skipReason = "target block entity changed";
return false;
}
private void SendPourBurst(PourTarget target)
{
if (capi?.World?.Player?.Entity == null)
{
return;
}
Block block = capi.World.BlockAccessor.GetBlock(target.Position);
BlockSelection blockSelection = new BlockSelection(target.Position.Copy(), BlockFacing.UP, block)
{
HitPosition = target.HitPosition,
SelectionBoxIndex = 0
};
int useSteps = CalculateUseSteps(target.TransferUnits);
EntityControls controls = capi.World.Player.Entity.Controls;
int previousUsingCount = controls.UsingCount;
controls.UsingCount = 0;
capi.Network.SendHandInteraction(
(int)EnumMouseButton.Right,
blockSelection,
null,
EnumHandInteract.HeldItemInteract,
(int)EnumHandInteractNw.StartHeldItemUse,
true,
EnumItemUseCancelReason.ReleasedMouse
);
controls.UsingCount = useSteps;
capi.Network.SendHandInteraction(
(int)EnumMouseButton.Right,
blockSelection,
null,
EnumHandInteract.HeldItemInteract,
(int)EnumHandInteractNw.StopHeldItemUse,
false,
EnumItemUseCancelReason.ReleasedMouse
);
controls.UsingCount = previousUsingCount;
LogPour("sent pour burst: target=" + target.Label + " at " + FormatPos(target.Position) + ", transferUnits=" + target.TransferUnits + ", capacityUnits=" + target.NeededUnits + ", useSteps=" + useSteps + (target.IsLeftoverPour ? ", leftover=true" : ""));
}
private bool IsManualHandUseActive()
{
EntityControls? controls = capi?.World?.Player?.Entity?.Controls;
return controls != null && controls.HandUse != EnumHandInteract.None;
}
private PourSource? GetActivePourSource()
{
if (capi?.World?.Player?.InventoryManager == null)
{
return null;
}
ItemSlot? slot = capi.World.Player.InventoryManager.ActiveHotbarSlot;
ItemStack? stack = slot?.Itemstack;
if (stack?.Collectible is not BlockSmeltedContainer container)
{
return null;
}
KeyValuePair<ItemStack, int> contents = container.GetContents(capi.World, stack);
if (contents.Key == null || contents.Value <= 0)
{
return null;
}
if (container.HasSolidifed(stack, contents.Key, capi.World))
{
LogPour("skipped source: active crucible contents are solidified");
return null;
}
if (slot == null)
{
return null;
}
return new PourSource(slot, stack, container, contents.Key, contents.Value);
}
private int CountNearbySmeltedContainers()
{
if (capi?.World?.Player?.Entity == null)
{
return 0;
}
int count = 0;
BlockPos playerPos = capi.World.Player.Entity.Pos.AsBlockPos;
IBlockAccessor blockAccessor = capi.World.BlockAccessor;
for (int dx = -PourScanRadius; dx <= PourScanRadius; dx++)
{
for (int dy = -PourScanRadius; dy <= PourScanRadius; dy++)
{
for (int dz = -PourScanRadius; dz <= PourScanRadius; dz++)
{
if (dx * dx + dy * dy + dz * dz > PourScanRadiusSquared)
{
continue;
}
BlockPos pos = new BlockPos(playerPos.X + dx, playerPos.Y + dy, playerPos.Z + dz, playerPos.dimension);
if (blockAccessor.IsValidPos(pos) && blockAccessor.GetBlock(pos) is BlockSmeltedContainer)
{
count++;
}
}
}
}
return count;
}
private bool TryBuildIngotHitPosition(BlockPos pos, BlockEntityIngotMold mold, bool rightSide, Vec3d localHit, out Vec3d hitPosition, out string skipReason)
{
if (IsWithinPickingRange(pos, localHit))
{
hitPosition = localHit;
skipReason = "";
return true;
}
foreach (Vec3d debugHit in GetDebugReachHitCandidates(pos))
{
if (WouldSelectRightIngotSide(mold, debugHit) == rightSide)
{
hitPosition = debugHit;
skipReason = "";
return true;
}
}
hitPosition = localHit;
skipReason = "debug reach hit would select the other ingot side";
return false;
}
private IEnumerable<Vec3d> GetDebugReachHitCandidates(BlockPos targetPos)
{
Vec3d eyePos = GetEyePosition();
double range = Math.Max(1, GetPickingRange() - 0.5);
yield return new Vec3d(eyePos.X - targetPos.X, eyePos.Y - targetPos.Y, eyePos.Z - targetPos.Z);
double[] offsets = new[] { range, -range, range * 0.5, -range * 0.5 };
foreach (double offset in offsets)
{
yield return new Vec3d(eyePos.X + offset - targetPos.X, eyePos.Y - targetPos.Y, eyePos.Z - targetPos.Z);
yield return new Vec3d(eyePos.X - targetPos.X, eyePos.Y - targetPos.Y, eyePos.Z + offset - targetPos.Z);
}
double diagonal = range * 0.6;
yield return new Vec3d(eyePos.X + diagonal - targetPos.X, eyePos.Y - targetPos.Y, eyePos.Z + diagonal - targetPos.Z);
yield return new Vec3d(eyePos.X + diagonal - targetPos.X, eyePos.Y - targetPos.Y, eyePos.Z - diagonal - targetPos.Z);
yield return new Vec3d(eyePos.X - diagonal - targetPos.X, eyePos.Y - targetPos.Y, eyePos.Z + diagonal - targetPos.Z);
yield return new Vec3d(eyePos.X - diagonal - targetPos.X, eyePos.Y - targetPos.Y, eyePos.Z - diagonal - targetPos.Z);
}
private bool TryGetLocalIngotHitForSide(BlockEntityIngotMold mold, bool rightSide, out Vec3d localHit)
{
Vec3d[] candidates =
{
new Vec3d(0.25, 0.5, 0.5),
new Vec3d(0.75, 0.5, 0.5),
new Vec3d(0.5, 0.5, 0.25),
new Vec3d(0.5, 0.5, 0.75)
};
foreach (Vec3d candidate in candidates)
{
if (WouldSelectRightIngotSide(mold, candidate) == rightSide)
{
localHit = candidate;
return true;
}
}
localHit = new Vec3d(0.5, 0.5, 0.5);
return false;
}
private static bool WouldSelectRightIngotSide(BlockEntityIngotMold mold, Vec3d hitPosition)
{
bool previous = mold.IsRightSideSelected;
mold.SetSelectedSide(hitPosition);
bool selected = mold.IsRightSideSelected;
mold.IsRightSideSelected = previous;
return selected;
}
private bool IsWithinPickingRange(BlockPos targetPos, Vec3d hitPosition)
{
Vec3d eyePos = GetEyePosition();
double x = targetPos.X + hitPosition.X - eyePos.X;
double y = targetPos.Y + hitPosition.Y - eyePos.Y;
double z = targetPos.Z + hitPosition.Z - eyePos.Z;
double range = GetPickingRange();
return x * x + y * y + z * z <= range * range;
}
private double GetPickingRange()
{
if (capi?.World?.Player?.WorldData != null)
{
return Math.Max(1, capi.World.Player.WorldData.PickingRange);
}
return 4.5;
}
private Vec3d GetEyePosition()
{
if (capi?.World?.Player?.Entity == null)
{
return new Vec3d();
}
return capi.World.Player.Entity.Pos.XYZ.Add(0, capi.World.Player.Entity.LocalEyePos.Y, 0);
}
private int CalculateUseSteps(int units)
{
return PourWarmupSteps + Math.Max(1, (int)Math.Ceiling(units / (double)PourUnitsPerStep)) + PourSafetySteps;
}
private int CountFullFillTargets()
{
int count = 0;
foreach (PourTarget target in pourTargets)
{
if (!target.IsLeftoverPour)
{
count++;
}
}
return count;
}
private int CountLeftoverTargets()
{
int count = 0;
foreach (PourTarget target in pourTargets)
{
if (target.IsLeftoverPour)
{
count++;
}
}
return count;
}
private int GetToolMoldRequiredUnits(BlockEntityToolMold mold)
{
if (ToolMoldRequiredUnitsField?.GetValue(mold) is int requiredUnits)
{
return requiredUnits;
}
return 0;
}
private bool IsFiredOrBurnedMold(ItemStack moldStack)
{