From 670449e4e1f6b477b7b82b5b75fbdcb1b17ad6aa Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 19 Apr 2017 17:22:54 +1000 Subject: [PATCH] reduce code duplication --- MCGalaxy/Entity/Entities.cs | 113 +++++++++++++------------- MCGalaxy/Network/Player.Networking.cs | 9 +- 2 files changed, 61 insertions(+), 61 deletions(-) diff --git a/MCGalaxy/Entity/Entities.cs b/MCGalaxy/Entity/Entities.cs index 4b148191f..7240e157f 100644 --- a/MCGalaxy/Entity/Entities.cs +++ b/MCGalaxy/Entity/Entities.cs @@ -34,7 +34,7 @@ namespace MCGalaxy { /// Spawns this player to all other players that can see the player in the current world. public static void GlobalSpawn(Player p, Position pos, Orientation rot, bool self, string possession = "") { Player[] players = PlayerInfo.Online.Items; - p.Game.lastSpawnColor = p.Game.Infected ? ZombieGame.InfectCol : p.color; + p.Game.lastSpawnColor = p.Game.Infected ? ZombieGame.InfectCol : p.color; TabList.Update(p, self); foreach (Player other in players) { @@ -50,7 +50,7 @@ namespace MCGalaxy { } } - /// Despawns this player to all other players that cannot + /// Despawns this player to all other players that cannot /// (or can if 'toVisible' is true) see the player in the current world. public static void GlobalDespawn(Player p, bool self, bool toVisible = false) { Player[] players = PlayerInfo.Online.Items; @@ -72,7 +72,7 @@ namespace MCGalaxy { internal static void Spawn(Player dst, Player p) { Spawn(dst, p, p.Pos, p.Rot); } - internal static void Spawn(Player dst, Player p, Position pos, + internal static void Spawn(Player dst, Player p, Position pos, Orientation rot, string possession = "") { byte id = p == dst ? Entities.SelfID : p.id; @@ -80,11 +80,7 @@ namespace MCGalaxy { TabList.Add(dst, p, id); if (!Server.zombie.Running || !p.Game.Infected) { string col = GetSupportedCol(dst, p.color); - if (dst.hasExtList) { - dst.SendExtAddEntity2(id, p.SkinName, col + p.truename + possession, p.Model, pos, rot); - } else { - dst.SendSpawn(id, col + p.truename + possession, p.Model, pos, rot); - } + Spawn(dst, id, p.SkinName, col + p.truename + possession, p.Model, p.Pos, p.Rot); return; } @@ -93,12 +89,8 @@ namespace MCGalaxy { name = ZombieGameProps.ZombieName; skinName = name; } - string model = id == Entities.SelfID ? p.Model : ZombieGameProps.ZombieModel; - if (dst.hasExtList) { - dst.SendExtAddEntity2(id, skinName, Colors.red + name + possession, model, pos, rot); - } else { - dst.SendSpawn(id, Colors.red + name + possession, model, pos, rot); - } + string model = p == dst ? p.Model : ZombieGameProps.ZombieModel; + Spawn(dst, id, skinName, Colors.red + name + possession, model, p.Pos, p.Rot); } /// Spawns this player to all other players, and spawns all others players to this player. @@ -110,10 +102,10 @@ namespace MCGalaxy { foreach (Player other in players) { if (other.level != p.level || !p.CanSeeEntity(other) || p == other) continue; Spawn(p, other); - } + } GlobalSpawn(p, pos, rot, true); - if (!bots) return; + if (!bots) return; PlayerBot[] botsList = PlayerBot.Bots.Items; foreach (PlayerBot b in botsList) if (b.level == p.level) Spawn(p, b); @@ -131,23 +123,34 @@ namespace MCGalaxy { PlayerBot[] botsList = PlayerBot.Bots.Items; foreach (PlayerBot b in botsList) { if (p.level == b.level) Despawn(p, b); - } + } } internal static void Spawn(Player dst, PlayerBot b) { string name = Chat.Format(b.color + b.DisplayName, dst, true, true, false); if (b.DisplayName.CaselessEq("empty")) name = ""; string skin = Chat.Format(b.SkinName, dst, true, true, false); - - if (dst.hasExtList) { - dst.SendExtAddEntity2(b.id, skin, name, b.Model, b.Pos, b.Rot); - } else { - dst.SendSpawn(b.id, name, b.Model, b.Pos, b.Rot); - } + + Spawn(dst, b.id, skin, name, b.Model, b.Pos, b.Rot); if (Server.TablistBots) TabList.Add(dst, b); } + static void Spawn(Player dst, byte id, string skin, string name, + string model, Position pos, Orientation rot) { + if (dst.hasExtList) { + dst.SendExtAddEntity2(id, skin, name, pos, rot); + } else { + dst.SendSpawn(id, name, pos, rot); + } + + if (dst.hasChangeModel) dst.Send(Packet.ChangeModel(id, model, dst.hasCP437)); + if (dst.HasCpeExt(CpeExt.EntityProperty)) { + dst.Send(Packet.EntityProperty(id, EntityProp.RotX, rot.RotX)); + dst.Send(Packet.EntityProperty(id, EntityProp.RotZ, rot.RotZ)); + } + } + internal static void Despawn(Player dst, byte id) { dst.SendRaw(Opcode.RemoveEntity, id); if (!Server.TablistGlobal) @@ -160,7 +163,7 @@ namespace MCGalaxy { TabList.Remove(dst, b.id); } - #endregion + #endregion internal static string GetSupportedCol(Player dst, string col) { if (col == null) return null; @@ -182,7 +185,7 @@ namespace MCGalaxy { Player[] players = PlayerInfo.Online.Items; entity.Model = model; Level lvl = entity.Level; - entity.ModelBB = AABB.ModelAABB(model, lvl); + entity.ModelBB = AABB.ModelAABB(model, lvl); foreach (Player pl in players) { if (pl.level != lvl || !pl.HasCpeExt(CpeExt.ChangeModel)) continue; @@ -193,17 +196,17 @@ namespace MCGalaxy { } } - /// Updates a property of an entity to all other players in same level. + /// Updates a property of an entity to all other players in same level. public static void UpdateEntityProp(Entity entity, EntityProp prop, int value) { Player[] players = PlayerInfo.Online.Items; Level lvl = entity.Level; Orientation rot = entity.Rot; - if (prop == EntityProp.RotX) + if (prop == EntityProp.RotX) rot.RotX = Orientation.DegreesToPacked(value); - if (prop == EntityProp.RotY) + if (prop == EntityProp.RotY) rot.RotY = Orientation.DegreesToPacked(value); - if (prop == EntityProp.RotZ) + if (prop == EntityProp.RotZ) rot.RotZ = Orientation.DegreesToPacked(value); entity.Rot = rot; @@ -233,9 +236,9 @@ namespace MCGalaxy { return buffer; } - public unsafe static void GetPositionPacket(ref byte* ptr, byte id, bool extPositions, Position pos, + public unsafe static void GetPositionPacket(ref byte* ptr, byte id, bool extPositions, Position pos, Position oldPos, Orientation rot, Orientation oldRot) { - Position delta = GetDelta(pos, oldPos, extPositions); + Position delta = GetDelta(pos, oldPos, extPositions); bool posChanged = delta.X != 0 || delta.Y != 0 || delta.Z != 0; bool oriChanged = rot.RotY != oldRot.RotY || rot.HeadX != oldRot.HeadX; bool absPosUpdate = Math.Abs(delta.X) > 32 || Math.Abs(delta.Y) > 32 || Math.Abs(delta.Z) > 32; @@ -244,7 +247,7 @@ namespace MCGalaxy { *ptr = Opcode.EntityTeleport; ptr++; *ptr = id; ptr++; *ptr = (byte)(pos.X >> 8); ptr++; *ptr = (byte)pos.X; ptr++; - *ptr = (byte)(pos.Y >> 8); ptr++; *ptr = (byte)pos.Y; ptr++; + *ptr = (byte)(pos.Y >> 8); ptr++; *ptr = (byte)pos.Y; ptr++; *ptr = (byte)(pos.Z >> 8); ptr++; *ptr = (byte)pos.Z; ptr++; } else if (posChanged && oriChanged) { *ptr = Opcode.RelPosAndOrientationUpdate; ptr++; @@ -253,13 +256,13 @@ namespace MCGalaxy { *ptr = (byte)(delta.Y); ptr++; *ptr = (byte)(delta.Z); ptr++; } else if (posChanged) { - *ptr = Opcode.RelPosUpdate; ptr++; + *ptr = Opcode.RelPosUpdate; ptr++; *ptr = id; ptr++; *ptr = (byte)(delta.X); ptr++; *ptr = (byte)(delta.Y); ptr++; *ptr = (byte)(delta.Z); ptr++; } else if (oriChanged) { - *ptr = Opcode.OrientationUpdate; ptr++; + *ptr = Opcode.OrientationUpdate; ptr++; *ptr = id; ptr++; } @@ -293,38 +296,38 @@ namespace MCGalaxy { } unsafe static void UpdatePosition(Player p) { - Player[] players = PlayerInfo.Online.Items; - byte* src = stackalloc byte[10 * 256]; // 10 = size of absolute update - byte* ptr = src; - - foreach (Player pl in players) { - if (p == pl || p.level != pl.level || !p.CanSeeEntity(pl)) continue; - - Orientation rot = pl.Rot; - rot.HeadX = p.hasChangeModel ? MakePitch(pl, rot.HeadX) : MakeClassicPitch(pl, rot.HeadX); - Entities.GetPositionPacket(ref ptr, pl.id, pl.supportsExtPositions, - pl.tempPos, pl.lastPos, rot, pl.lastRot); - } - - int count = (int)(ptr - src); - if (count == 0) return; - byte[] packet = new byte[count]; - for (int i = 0; i < packet.Length; i++) - packet[i] = src[i]; - p.Send(packet); + Player[] players = PlayerInfo.Online.Items; + byte* src = stackalloc byte[10 * 256]; // 10 = size of absolute update + byte* ptr = src; + + foreach (Player pl in players) { + if (p == pl || p.level != pl.level || !p.CanSeeEntity(pl)) continue; + + Orientation rot = pl.Rot; + rot.HeadX = p.hasChangeModel ? MakePitch(pl, rot.HeadX) : MakeClassicPitch(pl, rot.HeadX); + Entities.GetPositionPacket(ref ptr, pl.id, pl.supportsExtPositions, + pl.tempPos, pl.lastPos, rot, pl.lastRot); + } + + int count = (int)(ptr - src); + if (count == 0) return; + byte[] packet = new byte[count]; + for (int i = 0; i < packet.Length; i++) + packet[i] = src[i]; + p.Send(packet); } static byte MakePitch(Player p, byte pitch) { if (Server.flipHead || p.flipHead) if (pitch > 64 && pitch < 192) return pitch; - else return 128; + else return 128; return pitch; } static byte MakeClassicPitch(Player p, byte pitch) { if (Server.flipHead || p.flipHead || p.Game.Infected) if (pitch > 64 && pitch < 192) return pitch; - else return 128; + else return 128; return pitch; } #endregion diff --git a/MCGalaxy/Network/Player.Networking.cs b/MCGalaxy/Network/Player.Networking.cs index c696af9eb..f8ad08b4a 100644 --- a/MCGalaxy/Network/Player.Networking.cs +++ b/MCGalaxy/Network/Player.Networking.cs @@ -333,12 +333,11 @@ namespace MCGalaxy { /// Sends a packet indicating an entity was spawned in the current map /// at the given absolute position + coordinates - public void SendSpawn(byte id, string name, string model, Position pos, Orientation rot) { + public void SendSpawn(byte id, string name, Position pos, Orientation rot) { // NOTE: Fix for standard clients if (id == Entities.SelfID) pos.Y -= 22; Send(Packet.AddEntity(id, name, pos, rot, hasCP437)); - if (hasChangeModel) Send(Packet.ChangeModel(id, model, hasCP437)); } /// Sends a packet indicating an absolute position + orientation change for an enity. @@ -394,18 +393,16 @@ namespace MCGalaxy { Send(buffer); } - public void SendExtAddEntity(byte id, string name, string displayName, string model) { + public void SendExtAddEntity(byte id, string name, string displayName) { Send(Packet.ExtAddEntity(id, name, displayName, hasCP437)); - if (hasChangeModel) Send(Packet.ChangeModel(id, model, hasCP437)); } - public void SendExtAddEntity2(byte id, string skinName, string displayName, string model, + public void SendExtAddEntity2(byte id, string skinName, string displayName, Position pos, Orientation rot) { // NOTE: Fix for standard clients if (id == Entities.SelfID) pos.Y -= 22; Send(Packet.ExtAddEntity2(id, skinName, displayName, pos, rot, hasCP437)); - if (hasChangeModel) Send(Packet.ChangeModel(id, model, hasCP437)); } public void SendExtAddPlayerName(byte id, string listName, string displayName, string grp, byte grpRank) {