Advertisement
Fastmapler

Get RoomData

Sep 4th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. int worldXSize = 200;
  2. int worldYSize = 400;
  3.  
  4. public uint[,,] roomData = new uint[2, worldXSize, worldYSize];
  5.  
  6. static void OnMessage(object sender, PlayerIOClient.Message m)
  7. {
  8. if (m.Type.Equals("init"))
  9. {
  10. conn.Send("init2");
  11.  
  12. var chunks = InitParse.Parse(m);
  13.  
  14. foreach (var chunk in chunks)
  15. foreach (var pos in chunk.Locations)
  16. roomData[chunk.Layer, pos.X, pos.Y] = chunk.Type;
  17. }
  18. else if (m.Type.Equals("b"))
  19. {
  20. roomData[m.GetInteger(0), m.GetUInt(1), m.GetUInt(2)] = m.GetUInt(3);
  21. }
  22. }
  23.  
  24.  
  25.  
  26.  
  27. public static class InitParse
  28. {
  29. public static DataChunk[] Parse(Message m)
  30. {
  31. if (m == null) throw new ArgumentNullException("m");
  32. if (m.Type != "init" && m.Type != "reset") throw new ArgumentException("Invalid message type.", "m");
  33.  
  34. // Get world data
  35. var p = 0u;
  36. var data = new Stack<object>();
  37. while (m[p++] as string != "ws") { }
  38. while (m[p] as string != "we") { data.Push(m[p++]); }
  39.  
  40. // Parse world data
  41. var chunks = new List<DataChunk>();
  42. while (data.Count > 0)
  43. {
  44. var args = new Stack<object>();
  45. while (!(data.Peek() is byte[]))
  46. args.Push(data.Pop());
  47.  
  48. var ys = (byte[])data.Pop();
  49. var xs = (byte[])data.Pop();
  50. var layer = (int)data.Pop();
  51. var type = (uint)data.Pop();
  52.  
  53. chunks.Add(new DataChunk(layer, type, xs, ys, args.ToArray()));
  54. }
  55.  
  56. return chunks.ToArray();
  57. }
  58. }
  59.  
  60. public class DataChunk
  61. {
  62. public int Layer { get; set; }
  63. public uint Type { get; set; }
  64. public Point[] Locations { get; set; }
  65. public object[] Args { get; set; }
  66.  
  67. public DataChunk(int layer, uint type, byte[] xs, byte[] ys, object[] args)
  68. {
  69. this.Layer = layer;
  70. this.Type = type;
  71. this.Args = args;
  72. this.Locations = GetLocations(xs, ys);
  73. }
  74.  
  75. private static Point[] GetLocations(byte[] xs, byte[] ys)
  76. {
  77. var points = new List<Point>();
  78. for (var i = 0; i < xs.Length; i += 2)
  79. points.Add(new Point(
  80. (xs[i] << 8) | xs[i + 1],
  81. (ys[i] << 8) | ys[i + 1]));
  82. return points.ToArray();
  83. }
  84. }
  85.  
  86. public struct Point
  87. {
  88. public int X { get; set; }
  89. public int Y { get; set; }
  90.  
  91. public Point(int x, int y)
  92. : this()
  93. {
  94. this.X = x;
  95. this.Y = y;
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement