PsyOps

LVZTogglePacket.cs

Jun 15th, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 KB | None | 0 0
  1. //-----------------------------------------------------------------------
  2. //
  3. // NAME:        LVZTogglePacket.cs
  4. //
  5. // PROJECT:     Battle Core Protocol Library
  6. //
  7. // COMPILER:    Microsoft Visual Studio .NET 2005
  8. //
  9. // DESCRIPTION: Player Death Packet implementation.
  10. //
  11. // NOTES:       None.
  12. //
  13. // $History: LVZTogglePacket.cs $
  14. //
  15. //-----------------------------------------------------------------------
  16.  
  17. // Namespace usage
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Text;
  21. using System.IO;
  22. using BattleCore.Events;
  23.  
  24. // Namespace declaration
  25. namespace BattleCore.Protocol
  26. {
  27.     /// <summary>
  28.     /// LVZTogglePacket object.  This object is used to send/receive LVZ
  29.     /// Toggle packets from the server.
  30.     /// </summary>
  31.     internal class LVZTogglePacket : IPacket
  32.     {
  33.         /// <summary>LVZ Toggle Packet Event Data</summary>
  34.         private LVZToggleEvent m_event = new LVZToggleEvent();
  35.  
  36.         ///<summary>Reliable Packet Property</summary>
  37.         public Boolean Reliable { get { return false; } }
  38.  
  39.         /// <summary>
  40.         /// LVZ Toggle Event Property
  41.         /// </summary>
  42.         public LVZToggleEvent Event
  43.         {
  44.             set { m_event = value; }
  45.             get { return m_event; }
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Packet Data Property
  50.         /// </summary>
  51.         public Byte[] Packet
  52.         {
  53.             set
  54.             {
  55.                 m_event = new LVZToggleEvent();
  56.                 m_event.TargetPlayerId = BitConverter.ToUInt16(value, 0);
  57.                 m_event.LVZObjects = new Dictionary<ushort, bool>();
  58.  
  59.                 for (Int32 i = 1; i < value.Length; i += 2)
  60.                 {
  61.                     bool state = false;
  62.  
  63.                     byte[] nexttwo = new byte[2] { value[i], value[i + 1] };
  64.                     ushort result = (ushort)BitConverter.ToInt16(nexttwo, 0);
  65.                    
  66.                     if (result >= 32768)
  67.                     {
  68.                         state = true;
  69.                         result = (ushort)(result - 32768);
  70.                     }
  71.  
  72.                     m_event.LVZObjects[result] = state;
  73.                     //m_event.LVZObjects.Add((ushort)result, state);
  74.                 }
  75.  
  76.                 /*
  77.                 // Set the event data from reading the packet
  78.                 m_event.DeathGreen = value[1];
  79.                 m_event.KillerId = BitConverter.ToUInt16(value, 2);
  80.                 m_event.KilledId = BitConverter.ToUInt16(value, 4);
  81.                 m_event.Bounty = BitConverter.ToUInt16(value, 6);
  82.                 m_event.FlagsCarried = BitConverter.ToUInt16(value, 8);
  83.                 */
  84.             }
  85.             get
  86.             {
  87.                 // Offset  Length  Description
  88.                 // 0       1       Encapsulating Type Byte (0x0A)
  89.                 // 1       2       Player ID (-1 for all players)
  90.                 // 3       1       Real Type Byte (0x35)
  91.                 // 4       2*      Object Toggle Data
  92.                 //
  93.                 // WARNING: http://www.twcore.org/wiki/SubspaceProtocol#a0x35-LVZObjectToggle appears to describe wrong structure for Object Toggle Data
  94.                 // Real structure is as follows
  95.                 //
  96.                 // Bit Field   Bit Length   Description
  97.                 // 0           1            New state of LVZ object...0 denotes visible, 1 denotes hidden
  98.                 // 1           15           LVZ object ID
  99.                 //
  100.                 // Therefore, 0000 0000 0000 1111 shows LVZ object 15, and 1000 0000 0000 1111 hides LVZ object 15
  101.  
  102.                 // Create the packet memory stream
  103.                 int size = 4 + 2 * m_event.LVZObjects.Count;
  104.                 MemoryStream packet = new MemoryStream(size);
  105.  
  106.                 // Write the packet data to the memory stream
  107.                 packet.WriteByte(0x0A);
  108.                 packet.Write(BitConverter.GetBytes(m_event.TargetPlayerId), 0, 2);
  109.                 packet.WriteByte(0x35);
  110.  
  111.                 foreach (var item in m_event.LVZObjects)
  112.                 {
  113.                     UInt16 data = item.Key; // lvz ID occupies the right-most bits
  114.                     if (!item.Value)
  115.                         data += 32768;  // 1000 0000 0000 0000  hide object
  116.  
  117.                     packet.Write(BitConverter.GetBytes(data), 0, 2);
  118.                 }
  119.  
  120.                 // return the packet data
  121.                 return packet.GetBuffer();
  122.             }
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment