private void OnTick(Object myObject, EventArgs myEventArgs) { mapImage.Visible = _client.IsConnected(); if (_client.IsConnecting()) { btConnect.Text = "Cancel"; return; } if (!_client.IsConnected()) { btConnect.Text = "Connect"; if (mapImage.Image != null) { mapImage.Image.Dispose(); mapImage.Image = null; } return; } else { btConnect.Text = "Disconnect"; } Viewer.MapData mapData = _client.GetMapData(); if (mapData != null) { Bitmap bm = GetBitmap(mapData); if (mapImage.Image != null) { mapImage.Image.Dispose(); } mapImage.Image = bm; mapImage.Width = bm.Width; mapImage.Visible = true; mapImage.Height = bm.Height; } else { if (mapImage.Image != null) { mapImage.Image.Dispose(); mapImage.Image = null; } } }
private void ProcessBuffer(ref Client client) { var header = client.header; var buffer = client.buffer; buffer.Seek(0, SeekOrigin.Begin); try { // Read header. header.Deserialize(buffer); if (header.len > buffer.Length - buffer.Position) { return; } switch ((Viewer.DataType)header.type) { case Viewer.DataType.MapData: Viewer.MapData mapData = new Viewer.MapData(); mapData.Deserialize(buffer); OnMapData(mapData); break; default: break; } // Discard current packet. MemoryStream newBuffer = new MemoryStream(); newBuffer.Write(buffer.GetBuffer(), (int)buffer.Position, (int)(buffer.Length - buffer.Position)); // Set buffer. client.buffer = newBuffer; } catch (Exception) { } }
public void BroadcastMapData() { if (!_server.HasClients()) { return; } var now = DateTime.Now; if (now < _nextBroadcast) { return; } // Broadcast only with 20hz. _nextBroadcast = now.AddMilliseconds(1.0f / 20.0f); try { Viewer.MapData data = new Viewer.MapData(); data.w = 512; data.h = 512; data.mapW = Utils.Distance(_worldMins.x, _worldMaxs.x); data.mapH = Utils.Distance(_worldMins.z, _worldMaxs.z); data.density = _config.PopulationDensity; data.zombieSpeed = _worldState.GetZombieSpeed(); data.timescale = _timeScale; lock (_lock) { data.inactive = new List <Viewer.DataZombie>(); var inactive = _inactiveZombies; for (int i = 0; i < inactive.Count; i++) { var zombie = inactive[i]; Vector2i p = WorldToBitmap(zombie.pos); data.inactive.Add(new Viewer.DataZombie { id = zombie.id, x = p.x, y = p.y, }); } data.active = new List <Viewer.DataZombie>(); var active = _activeZombies; for (int i = 0; i < active.Count; i++) { var zombie = active[i]; Vector2i p = WorldToBitmap(zombie.pos); data.active.Add(new Viewer.DataZombie { id = zombie.id, x = p.x, y = p.y, }); } data.playerZones = _playerZones.GetSerializable(this); data.poiZones = _pois.GetSerializable(this); data.worldZones = _worldZones.GetSerializable(this); } _server.Broadcast(Viewer.DataType.MapData, data); } catch (Exception ex) { Log.Exception(ex); } }
Bitmap GetBitmap(Viewer.MapData mapData) { int topBorderSize = 14; Bitmap bm = new Bitmap(Scale(mapData.w), Scale(mapData.h) + topBorderSize); using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bm)) { gr.SmoothingMode = SmoothingMode.None; gr.Clear(System.Drawing.Color.Black); // World Zones if (chkGrid.Checked) { foreach (var zone in mapData.worldZones) { gr.DrawRectangle(Pens.Gray, Scale(zone.x1), Scale(zone.y1), Scale(zone.x2 - zone.x1), Scale(zone.y2 - zone.y1)); } } // POIS if (chkPOIs.Checked) { foreach (var poi in mapData.poiZones) { // Zone gr.DrawRectangle(Pens.DarkGray, Scale(poi.x1), Scale(poi.y1), Scale(poi.x2 - poi.x1), Scale(poi.y2 - poi.y1)); } } // Draw inactive. if (chkInactive.Checked) { foreach (var zombie in mapData.inactive) { gr.FillEllipse(Brushes.Red, Scale(zombie.x), Scale(zombie.y), 2, 2); } } // Active if (chkActive.Checked) { foreach (var zombie in mapData.active) { gr.FillEllipse(Brushes.Blue, Scale(zombie.x), Scale(zombie.y), 2, 2); } } // Visible zones. if (chkPlayers.Checked) { foreach (var zone in mapData.playerZones) { // Zone gr.DrawRectangle(Pens.Green, Scale(zone.x1), Scale(zone.y1), Scale(zone.x2 - zone.x1), Scale(zone.y2 - zone.y1)); // Spawn Block. gr.DrawRectangle(Pens.Yellow, Scale(zone.x3), Scale(zone.y3), Scale(zone.x4 - zone.x3), Scale(zone.y4 - zone.y3)); } } // Stats { gr.FillRectangle(Brushes.Black, 0, 0, mapData.mapW, 20); float x = 4.0f; gr.DrawString(string.Format("Speed: {0}x", mapData.timescale), SystemFonts.DefaultFont, Brushes.Green, x, 4.0f); x += 70.0f; gr.DrawString(string.Format("Map: {0}x{1}", mapData.mapW, mapData.mapH), SystemFonts.DefaultFont, Brushes.Green, x, 4.0f); x += 100.0f; gr.DrawString(string.Format("Density: {0}/km²", mapData.density), SystemFonts.DefaultFont, Brushes.Green, x, 4.0f); x += 100.0f; gr.DrawString(string.Format("Inactive: {0}", mapData.inactive.Count), SystemFonts.DefaultFont, Brushes.Green, x, 4.0f); x += 100.0f; gr.DrawString(string.Format("Active: {0}", mapData.active.Count), SystemFonts.DefaultFont, Brushes.Green, x, 4.0f); x += 70.0f; gr.DrawString(string.Format("Zombie Speed: {0}", mapData.zombieSpeed), SystemFonts.DefaultFont, Brushes.Green, x, 4.0f); x += 100.0f; } } return(bm); }
private void OnMapData(WalkerSim.Viewer.MapData mapData) { _mapData = mapData; }