XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

DeathMod

【Terraria】DeathMod (now aka Soul Harvest)

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/DeathMod

优化死域吸附、状态同步与连击判定机制

本次更新实现了死域掉落物全图吸附与归属权同步,新增角色状态同步确认机制,优化死神连击挥砍判定与动画状态同步,提升玩法流畅性和同步健壮性。新增相关方法与消息类型,完善状态重置与网络包处理逻辑。

3c713a3
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

5 个文件 +156 -22
Modified Common/MyPlayer.cs +68 -2
@@ -56,6 +56,7 @@ public class MyPlayer : ModPlayer
56 56 private bool serverReaperProgressionInitialized;
57 57 private int clientCharacterStateUploadDelay;
58 58 private int clientCharacterStateUploadAttempts;
59 private bool clientCharacterStateAcknowledged;
59 60 private bool reaperDeathStateCleared;
60 61 private bool reaperLegacyMigrationChecked;
61 62 private int reaperLegacyMigrationVersion;
@@ -126,6 +127,7 @@ public class MyPlayer : ModPlayer
126 127 serverReaperProgressionInitialized = false;
127 128 clientCharacterStateUploadDelay = 0;
128 129 clientCharacterStateUploadAttempts = 0;
130 clientCharacterStateAcknowledged = false;
129 131 DeathDomainProgression.Reset();
130 132 ReaperProgression.Reset();
131 133 VisibleReaperForm = ReaperFormId.Base;
@@ -207,6 +209,7 @@ public class MyPlayer : ModPlayer
207 209
208 210 public override void OnEnterWorld()
209 211 {
212 ReaperCombatService.ResetPlayerState(Player.whoAmI);
210 213 ClearReaperCombatRuntime(sync: false);
211 214 if (Main.netMode == NetmodeID.MultiplayerClient
212 215 && Player.whoAmI == Main.myPlayer)
@@ -217,6 +220,7 @@ public class MyPlayer : ModPlayer
217 220 // its uninitialized state and reject combat, forms and the domain.
218 221 clientCharacterStateUploadDelay = 1;
219 222 clientCharacterStateUploadAttempts = 0;
223 clientCharacterStateAcknowledged = false;
220 224 }
221 225 if (Main.netMode == NetmodeID.Server || Player.whoAmI != Main.myPlayer || reaperLegacyMigrationChecked)
222 226 return;
@@ -301,7 +305,7 @@ public class MyPlayer : ModPlayer
301 305 {
302 306 if (Main.netMode != NetmodeID.MultiplayerClient
303 307 || Player.whoAmI != Main.myPlayer
304 || clientCharacterStateUploadAttempts >= 3)
308 || clientCharacterStateAcknowledged)
305 309 {
306 310 return;
307 311 }
@@ -319,10 +323,21 @@ public class MyPlayer : ModPlayer
319 323 {
320 324 1 => 30,
321 325 2 => 90,
322 _ => 0
326 3 => 180,
327 _ => 300
323 328 };
324 329 }
325 330
331 internal void AcknowledgeServerCharacterState()
332 {
333 if (Main.netMode == NetmodeID.MultiplayerClient
334 && Player.whoAmI == Main.myPlayer)
335 {
336 clientCharacterStateAcknowledged = true;
337 clientCharacterStateUploadDelay = 0;
338 }
339 }
340
326 341 private void TryDeliverPendingLegacyMigrationRefund()
327 342 {
328 343 if (pendingLegacyMigrationEssence <= 0)
@@ -1473,6 +1488,7 @@ public class MyPlayer : ModPlayer
1473 1488
1474 1489 bool descended = IsDeathDomainDescended(necklace);
1475 1490 ApplyDeathDomainForces(necklace, descended);
1491 ApplyDeathDomainItemAttraction();
1476 1492 List<NPC> targets = FindDeathDomainTargets(necklace, descended);
1477 1493 float visualMastery = DeathDomainProgression.VisualMastery;
1478 1494 UpdateDeathDomainTelegraphs(targets, visualMastery);
@@ -1829,6 +1845,56 @@ public class MyPlayer : ModPlayer
1829 1845 }
1830 1846 }
1831 1847
1848 private void ApplyDeathDomainItemAttraction()
1849 {
1850 // The domain owns the whole world while active. Move eligible drops in
1851 // world space on the authority instead of relying on vanilla pickup range;
1852 // this also lets coins and loot travel through terrain rather than becoming
1853 // trapped against a distant wall. Eligible drops are reserved to the
1854 // domain owner while the attraction is active.
1855 Vector2 destination = Player.MountedCenter;
1856 for (int itemIndex = 0; itemIndex < Main.maxItems; itemIndex++)
1857 {
1858 Item item = Main.item[itemIndex];
1859 if (!item.active || item.IsAir || item.stack <= 0
1860 || !ItemLoader.CanPickup(item, Player)
1861 || !Player.CanPullItem(item, Player.ItemSpace(item)))
1862 {
1863 continue;
1864 }
1865
1866 // Item.Update continuously chooses a reservation owner. Without
1867 // claiming the drop here, that vanilla pass can immediately hand a
1868 // full-world attraction target to another nearby player and undo the
1869 // domain movement before its owner can collect it.
1870 item.playerIndexTheItemIsReservedFor = Player.whoAmI;
1871
1872 Vector2 toPlayer = destination - item.Center;
1873 float distance = toPlayer.Length();
1874 if (distance <= 1f)
1875 continue;
1876
1877 Vector2 direction = toPlayer / distance;
1878 float step = Math.Min(distance,
1879 MathHelper.Clamp(distance * 0.085f, 18f, 180f));
1880 item.position += direction * step;
1881 item.velocity = direction * Math.Min(24f, 8f + distance * 0.004f);
1882 item.noGrabDelay = 0;
1883
1884 if (distance < 54f)
1885 {
1886 item.Center = destination;
1887 item.velocity = Vector2.Zero;
1888 }
1889
1890 if (Main.netMode == NetmodeID.Server
1891 && (Main.GameUpdateCount + (ulong)itemIndex) % 6UL == 0UL)
1892 {
1893 NetMessage.SendData(MessageID.SyncItem, -1, -1, null, itemIndex);
1894 }
1895 }
1896 }
1897
1832 1898 private static int CalculateDeathDomainSlashDamage(NPC target, DeathDomainProgression progression)
1833 1899 {
1834 1900 bool bossOrBossPart = target.boss
Modified Common/ReaperCombatService.cs +24 -11
@@ -33,6 +33,23 @@ public sealed class ReaperCombatSystem : ModSystem
33 33
34 34 public static class ReaperCombatService
35 35 {
36 internal static void ResetPlayerState(int owner)
37 {
38 if (owner < 0 || owner >= Main.maxPlayers)
39 return;
40
41 for (int form = 0; form < ReaperCombatSystem.ComboPhase.GetLength(1); form++)
42 {
43 ReaperCombatSystem.ComboPhase[owner, form] = 0;
44 ReaperCombatSystem.LastComboTick[owner, form] = 0;
45 }
46 ReaperCombatSystem.ActivePrimaryProjectile[owner] = -1;
47 ReaperCombatSystem.HasActivePrimaryProjectile[owner] = false;
48 ReaperCombatSystem.NextPrimaryAuthorizationTick[owner] = 0;
49 ReaperCombatSystem.DeathPrimaryChain[owner] = 0;
50 ReaperCombatSystem.LastDeathPrimaryTick[owner] = 0;
51 }
52
36 53 internal static bool TryAuthorizePrimarySwing(Player player, Projectile projectile,
37 54 in SickleCombatSnapshot snapshot, out int phase, out float deathTempo)
38 55 {
@@ -67,20 +84,16 @@ public static class ReaperCombatService
67 84 ReaperCombatSystem.HasActivePrimaryProjectile[owner] = false;
68 85
69 86 phase = TakeNextPrimaryPhase(player, snapshot, out deathTempo);
70 int baseDuration = ReaperCombatRegistry.GetUseTime(snapshot.Form, snapshot.Stage, phase);
71 float effectiveAttackSpeed = snapshot.AttackSpeedMultiplier * deathTempo;
72 if (snapshot.Form == ReaperFormId.Death)
73 effectiveAttackSpeed = Math.Min(effectiveAttackSpeed,
74 snapshot.DeathPrimaryMaximumTempo);
75 int minimumDuration = snapshot.Form == ReaperFormId.Death ? 3 : 12;
76 int authoritativeDuration = Math.Max(minimumDuration,
77 (int)Math.Ceiling(baseDuration / Math.Max(0.1f,
78 effectiveAttackSpeed)));
79 87 ReaperCombatSystem.ActivePrimaryProjectile[owner] = projectile.whoAmI;
80 88 ReaperCombatSystem.HasActivePrimaryProjectile[owner] = true;
81 int minimumAuthorizationGap = snapshot.Form == ReaperFormId.Death ? 2 : 8;
89 // Item reuse and the authoritative held projectile already enforce the
90 // real animation duration. This clock is only duplicate-packet protection.
91 // Basing it on the whole previous swing made normal network jitter reject
92 // the next legitimate owner projectile; the client then held its predicted
93 // terminal pose for the 180-frame authority timeout.
94 int minimumAuthorizationGap = snapshot.Form == ReaperFormId.Death ? 2 : 6;
82 95 ReaperCombatSystem.NextPrimaryAuthorizationTick[owner] = now
83 + (ulong)Math.Max(minimumAuthorizationGap, authoritativeDuration - 1);
96 + (ulong)minimumAuthorizationGap;
84 97 return true;
85 98 }
86 99
Modified Items/NormalSickle.cs +1 -1
@@ -313,7 +313,7 @@ public class NormalSickle : ModItem
313 313 if (!UsesModernSwing)
314 314 return true;
315 315
316 if (player.ownedProjectileCounts[ModContent.ProjectileType<SickleSwingProjectile>()] > 0)
316 if (SickleSwingProjectile.HasBlockingPrimarySwing(player))
317 317 return false;
318 318 if (player.ownedProjectileCounts[ModContent.ProjectileType<ReaperActionControllerProjectile>()] > 0)
319 319 return false;
Modified Projectiles/SickleSwingProjectile.cs +33 -5
@@ -52,6 +52,7 @@ public sealed class SickleSwingProjectile : ModProjectile
52 52 private Vector2 infernalMoveVelocity;
53 53
54 54 internal bool ServerValidated => serverValidated;
55 internal bool BlocksNewPrimarySwing => !configured || !initialized || timer < duration;
55 56 internal float DeathTempo => deathTempo;
56 57 internal bool DeathSpaceBreak => snapshot.Form == ReaperFormId.Death
57 58 && snapshot.DeathSpaceBreakUnlocked
@@ -67,6 +68,22 @@ public sealed class SickleSwingProjectile : ModProjectile
67 68 private bool IsComboFinisher => phase >= ReaperCombatRegistry.GetPrimaryPhaseCount(snapshot.Form, snapshot.Stage) - 1;
68 69 public override string Texture => "Terraria/Images/Projectile_0";
69 70
71 internal static bool HasBlockingPrimarySwing(Player player)
72 {
73 int swingType = ModContent.ProjectileType<SickleSwingProjectile>();
74 foreach (Projectile projectile in Main.ActiveProjectiles)
75 {
76 if (projectile.owner == player.whoAmI
77 && projectile.type == swingType
78 && projectile.ModProjectile is SickleSwingProjectile swing
79 && swing.BlocksNewPrimarySwing)
80 {
81 return true;
82 }
83 }
84 return false;
85 }
86
70 87 internal void Configure(in SickleCombatSnapshot value, int attackPhase,
71 88 float primaryDeathTempo = 1f, bool authorizeServer = false)
72 89 {
@@ -172,12 +189,23 @@ public sealed class SickleSwingProjectile : ModProjectile
172 189 timer++;
173 190 Projectile.timeLeft = 2;
174 191 Projectile.Center = player.MountedCenter;
175 player.heldProj = Projectile.whoAmI;
176 player.itemTime = 2;
177 player.itemAnimation = 2;
178 player.ChangeDir(Projectile.velocity.X >= 0f ? 1 : -1);
179 grip = player.MountedCenter;
180 192 float progress = MathHelper.Clamp(timer / (float)duration, 0f, 1f);
193 if (progress < 1f)
194 {
195 player.heldProj = Projectile.whoAmI;
196 player.itemTime = 2;
197 player.itemAnimation = 2;
198 player.ChangeDir(Projectile.velocity.X >= 0f ? 1 : -1);
199 }
200 else if (player.heldProj == Projectile.whoAmI)
201 {
202 // A predicted multiplayer swing may remain alive briefly while it
203 // waits for its server authorization snapshot. That hidden,
204 // non-damaging wait must never keep the player's item locked or make
205 // the next click stall for the full authority timeout.
206 player.heldProj = -1;
207 }
208 grip = player.MountedCenter;
181 209 animationProgress = progress;
182 210 float previousPoseAngle = weaponAngle;
183 211 UpdatePose(progress);
Modified SoulHarvest.cs +30 -3
@@ -39,7 +39,8 @@ public class SoulHarvest : Mod
39 39 SyncReaperTransaction,
40 40 SyncBloodShieldConsumption,
41 41 SyncReaperMovement,
42 AcknowledgeReaperMovement
42 AcknowledgeReaperMovement,
43 AcknowledgeReaperCharacterState
43 44 }
44 45
45 46 internal enum DeathDomainVisualAction : byte
@@ -171,12 +172,27 @@ public class SoulHarvest : Mod
171 172 bool deathDomainEnabled = reader.ReadBoolean();
172 173 bool deathDomainDescended = reader.ReadBoolean();
173 174 MyPlayer modPlayer = Main.player[playerIndex].GetModPlayer<MyPlayer>();
174 if (modPlayer.TryInitializeServerCharacterState(
175 bool initializedNow = modPlayer.TryInitializeServerCharacterState(
175 176 souls,
176 177 deathDomainEnabled,
177 178 deathDomainDescended,
178 reader))
179 reader);
180 if (initializedNow)
179 181 modPlayer.SyncPlayer(-1, -1, false);
182 if (modPlayer.ServerCharacterStateReady)
183 SendReaperCharacterStateAcknowledgement(Main.player[playerIndex]);
184 }
185 break;
186 }
187 case MessageType.AcknowledgeReaperCharacterState:
188 {
189 byte playerIndex = reader.ReadByte();
190 if (Main.netMode == NetmodeID.MultiplayerClient
191 && playerIndex == Main.myPlayer
192 && playerIndex < Main.maxPlayers)
193 {
194 Main.player[playerIndex].GetModPlayer<MyPlayer>()
195 .AcknowledgeServerCharacterState();
180 196 }
181 197 break;
182 198 }
@@ -626,6 +642,17 @@ public class SoulHarvest : Mod
626 642 packet.Send();
627 643 }
628 644
645 private static void SendReaperCharacterStateAcknowledgement(Player player)
646 {
647 if (Main.netMode != NetmodeID.Server || !player.active)
648 return;
649
650 ModPacket packet = ModContent.GetInstance<SoulHarvest>().GetPacket();
651 packet.Write((byte)MessageType.AcknowledgeReaperCharacterState);
652 packet.Write((byte)player.whoAmI);
653 packet.Send(player.whoAmI);
654 }
655
629 656 internal static void SendReaperProgression(Player player, int toWho = -1, int fromWho = -1)
630 657 {
631 658 if (Main.netMode == NetmodeID.SinglePlayer)