예제 #1
0
		internal GameDataEventArgs(Player FromPlayer, PeerConnection Connection, byte[] Packet)
		{
			this.fromPlayer = FromPlayer;
			this.connection = Connection;
			this.packet = Packet;
			this.data = new BinaryInput(Packet);
		}
예제 #2
0
 internal GameDataEventArgs(Player FromPlayer, PeerConnection Connection, byte[] Packet)
 {
     this.fromPlayer = FromPlayer;
     this.connection = Connection;
     this.packet     = Packet;
     this.data       = new BinaryInput(Packet);
 }
예제 #3
0
		public PlayerPosition(BinaryInput Input)
		{
			this.x = 319 - (int)Input.ReadInt();
			this.y = 207 - (int)Input.ReadInt();
			this.vx = -(int)Input.ReadInt();
			this.vy = -(int)Input.ReadInt();
			this.dead = Input.ReadBool();
		}
        private Player Deserialize(BinaryInput Input)
        {
            IPAddress  PublicAddress  = IPAddress.Parse(Input.ReadString());
            ushort     PublicPort     = Input.ReadUInt16();
            IPEndPoint PublicEndpoint = new IPEndPoint(PublicAddress, PublicPort);

            IPAddress  LocalAddress  = IPAddress.Parse(Input.ReadString());
            ushort     LocalPort     = Input.ReadUInt16();
            IPEndPoint LocalEndpoint = new IPEndPoint(LocalAddress, LocalPort);

            Guid PlayerId = Input.ReadGuid();
            bool LocalPlayer = PlayerId == this.localPlayer.PlayerId;
            int  i, c = (int)Input.ReadUInt();

            KeyValuePair <string, string>[] PlayerMetaInfo = LocalPlayer ? null : new KeyValuePair <string, string> [c];
            string Key, Value;

            for (i = 0; i < c; i++)
            {
                Key   = Input.ReadString();
                Value = Input.ReadString();
                if (!LocalPlayer)
                {
                    PlayerMetaInfo[i] = new KeyValuePair <string, string>(Key, Value);
                }
            }

            if (LocalPlayer)
            {
                return(null);
            }
            else
            {
                return(new Player(PlayerId, PublicEndpoint, LocalEndpoint, PlayerMetaInfo));
            }
        }
예제 #5
0
		public void GetGift(int PlayerIndex, int GiftIndex, BinaryOutput Output, BinaryInput Input)
		{
			switch (GiftIndex)
			{
				case 0:
					this.framesPerPixel = 4;
					this.stepsPerFrame = 1;
					Program.PlayerMsg(this.playerNr, "Slow speed");
					break;

				case 1:
					this.framesPerPixel = 5;
					this.stepsPerFrame = 1;
					Program.PlayerMsg(this.playerNr, "Slower speed");
					break;

				case 2:
					this.framesPerPixel = 6;
					this.stepsPerFrame = 1;
					Program.PlayerMsg(this.playerNr, "Slowest speed");
					break;

				case 3:
					this.framesPerPixel = 2;
					this.stepsPerFrame = 1;
					Program.PlayerMsg(this.playerNr, "Fast speed");
					break;

				case 4:
					this.framesPerPixel = 1;
					this.stepsPerFrame = 1;
					Program.PlayerMsg(this.playerNr, "Faster speed");
					break;

				case 5:
					this.framesPerPixel = 1;
					this.stepsPerFrame = 2;
					if (this.shotSpeed < 2)
						this.shotSpeed = 2;
					Program.PlayerMsg(this.playerNr, "Fastest speed");
					break;

				case 6:
					this.framesPerPixel = 3;
					this.stepsPerFrame = 1;
					Program.PlayerMsg(this.playerNr, "Normal speed");
					break;

				case 7:
					this.shotPower = 5;
					Program.PlayerMsg(this.playerNr, "Tiny bullets");
					break;

				case 8:
					this.shotPower = 10;
					Program.PlayerMsg(this.playerNr, "Small bullets");
					break;

				case 9:
					this.shotPower = 15;
					Program.PlayerMsg(this.playerNr, "Normal bullets");
					break;

				case 10:
					this.shotPower = 20;
					Program.PlayerMsg(this.playerNr, "Large bullets");
					break;

				case 11:
					this.shotPower = 25;
					Program.PlayerMsg(this.playerNr, "Huge bullets");
					break;

				case 12:
					this.shotPower = 30;
					Program.PlayerMsg(this.playerNr, "Humongous bullets");
					break;

				case 13:
					this.shotPower = 50;
					Program.PlayerMsg(this.playerNr, "Peace-maker bullets");
					break;

				case 14:
					this.tail = false;
					Program.PlayerMsg(this.playerNr, "No tail");
					break;

				case 15:
					this.tail = true;
					Program.PlayerMsg(this.playerNr, "Tail");
					break;

				case 16:
					this.shotSpeed = Math.Max(1, this.stepsPerFrame);
					Program.PlayerMsg(this.playerNr, "Slow bullets");
					break;

				case 17:
					this.shotSpeed = Math.Max(2, this.stepsPerFrame);
					Program.PlayerMsg(this.playerNr, "Fast bullets");
					break;

				case 18:
					this.shotSpeed = Math.Max(3, this.stepsPerFrame);
					Program.PlayerMsg(this.playerNr, "Faster bullets");
					break;

				case 19:
					this.shotSpeed = Math.Max(4, this.stepsPerFrame);
					Program.PlayerMsg(this.playerNr, "Fastest bullets");
					break;

				case 20:
					int X, Y;
					int dx, dy;
					bool Ok;
					int TriesLeft = 100;
					int Black = Color.Black.ToArgb();

					if (Input != null)
					{
						this.x = 319 - (int)Input.ReadInt();
						this.y = 207 - (int)Input.ReadInt();
					}
					else
					{
						Ok = false;

						while (TriesLeft-- > 0 && !Ok)
						{
							X = RetroApplication.Random(30, 290);
							Y = RetroApplication.Random(38, 170);
							Ok = true;
							for (dy = -5; dy < 5; dy++)
							{
								for (dx = -5; dx < 5; dx++)
								{
									if (RetroApplication.Raster[X + dx, Y + dy].ToArgb() != Black)
									{
										Ok = false;
										dy = 5;
										break;
									}
								}
							}

							if (Ok)
							{
								Output.WriteInt(X);
								Output.WriteInt(Y);

								this.x = X;
								this.y = Y;
							}
						}

						if (!Ok && Output != null)
						{
							Output.WriteInt(this.x);
							Output.WriteInt(this.y);
						}
					}

					Program.PlayerMsg(this.playerNr, "Teleport");
					break;

				case 21:
					this.gun = Gun.WurstScheibe;
					Program.PlayerMsg(this.playerNr, "Wurst scheibe");
					break;

				case 22:
					this.gun = Gun.Homing;
					Program.PlayerMsg(this.playerNr, "Homing Missile");
					break;

				case 23:
					this.shotPower = 15;
					this.shotSpeed = 1;
					this.tail = true;
					this.gun = Gun.Normal;
					this.invisible = false;
					this.immortal = false;
					this.wrappingShots = false;
					this.wrap = true;
					this.opponent.wrap = true;
					Program.BorderColor = Color.FromKnownColor(KnownColor.DimGray);
					Program.PlayerMsg(this.playerNr, "Reset");
					break;

				case 24:
					int X1, Y1, X2, Y2;
					bool Inside;

					if (Input != null)
					{
						X2 = 319 - (int)Input.ReadInt();
						Y2 = 207 - (int)Input.ReadInt();
						X1 = 319 - (int)Input.ReadInt();
						Y1 = 207 - (int)Input.ReadInt();
					}
					else
					{
						do
						{
							X1 = RetroApplication.Random(30, 290);
							X2 = RetroApplication.Random(30, 290);
							Y1 = RetroApplication.Random(38, 170);
							Y2 = RetroApplication.Random(38, 170);

							if (X2 < X1)
							{
								X = X1;
								X1 = X2;
								X2 = X;
							}

							if (Y2 < Y1)
							{
								Y = Y1;
								Y1 = Y2;
								Y2 = Y;
							}

							Inside =
								(this.x >= X1 - 20 && this.x <= X2 + 20 && this.y >= Y1 - 20 && this.y <= Y2 + 20) ||
								(this.opponent.x >= X1 - 20 && this.opponent.x <= X2 + 20 && this.opponent.y >= Y1 - 20 && this.opponent.y <= Y2 + 20);
						}
						while (Inside || Math.Abs(X1 - X2) < 20 || Math.Abs(Y1 - Y2) < 20);

						if (Output != null)
						{
							Output.WriteInt(X1);
							Output.WriteInt(Y1);
							Output.WriteInt(X2);
							Output.WriteInt(Y2);
						}
					}

					RetroApplication.FillRoundedRectangle(X1, Y1, X2, Y2, 8, 8, (x, y, DestinationColor) =>
					{
						int i = ((x - X1) + (y - Y1)) % 6;
						if (i < 3)
							return Color.Cyan;
						else
							return RetroApplication.Blend(Color.Cyan, DestinationColor, 0.5);
					});
					RetroApplication.DrawRoundedRectangle(X1, Y1, X2, Y2, 8, 8, Color.Cyan);
					Program.PlayerMsg(this.playerNr, "Obstacle");
					break;

				case 25:
					this.invisible = true;
					Program.PlayerMsg(this.playerNr, "Invisibility");
					break;

				case 26:
					this.gun = Gun.BouncingBalls;
					Program.PlayerMsg(this.playerNr, "Bouncing balls");
					break;

				case 27:
					this.shotPower = 200;
					Program.PlayerMsg(this.playerNr, "Atomic Bomb");
					break;

				case 28:
					this.immortalStart = DateTime.Now;
					this.immortal = true;
					this.immortalSecondsLeft = 10;
					Program.PlayerMsg(this.playerNr, "Immortal 10");
					break;

				case 29:
					this.wrappingShots = true;
					Program.PlayerMsg(this.playerNr, "Wrapping shots");
					break;

				case 30:
					this.wrap = false;
					this.opponent.wrap = false;
					Program.BorderColor = Color.FromArgb(40, 40, 40);
					Program.PlayerMsg(this.playerNr, "Lock Border");
					break;

				case 31:
					this.wrap = true;
					this.opponent.wrap = true;
					Program.BorderColor = Color.FromKnownColor(KnownColor.DimGray);
					Program.PlayerMsg(this.playerNr, "Open Border");
					break;
			}
		}
        private void Connection_OnReceived(object Sender, byte[] Packet)
        {
            PeerConnection Connection = (PeerConnection)Sender;
            Guid           PlayerId;
            IPAddress      PlayerRemoteAddress;
            IPEndPoint     PlayerRemoteEndpoint;

            try
            {
                BinaryInput Input = new BinaryInput(Packet);

                PlayerId             = Input.ReadGuid();
                PlayerRemoteAddress  = IPAddress.Parse(Input.ReadString());
                PlayerRemoteEndpoint = new IPEndPoint(PlayerRemoteAddress, Input.ReadUInt16());
            }
            catch (Exception)
            {
                Connection.Dispose();
                return;
            }

            Player Player = (Player)Connection.StateObject;
            Player Player2;

            lock (this.remotePlayersByEndpoint)
            {
                if (!this.playersById.TryGetValue(PlayerId, out Player2) || Player2.PlayerId != Player.PlayerId)
                {
                    Connection.Dispose();
                    return;
                }

                Player.Connection = Connection;
            }

            Connection.RemoteEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork);

            Connection.OnReceived -= new BinaryEventHandler(Connection_OnReceived);
            Connection.OnReceived += new BinaryEventHandler(Peer_OnReceived);

            Connection.OnSent += new BinaryEventHandler(Connection_OnSent);

            BinaryOutput Output = new BinaryOutput();

            Output.WriteGuid(this.localPlayer.PlayerId);
            Output.WriteString(this.ExternalAddress.ToString());
            Output.WriteUInt16((ushort)this.ExternalEndpoint.Port);

            Connection.SendTcp(Output.GetPacket());

            MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerConnected;

            if (!(h is null))
            {
                try
                {
                    h(this, Player);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    Debug.WriteLine(ex.StackTrace.ToString());
                }
            }
        }
        private void Peer_OnReceived(object Sender, byte[] Packet)
        {
            PeerConnection Connection = (PeerConnection)Sender;
            Player         Player;

            if (Connection.StateObject is null)
            {
                BinaryInput Input = new BinaryInput(Packet);
                Guid        PlayerId;
                IPAddress   PlayerRemoteAddress;
                IPEndPoint  PlayerRemoteEndpoint;

                try
                {
                    PlayerId             = Input.ReadGuid();
                    PlayerRemoteAddress  = IPAddress.Parse(Input.ReadString());
                    PlayerRemoteEndpoint = new IPEndPoint(PlayerRemoteAddress, Input.ReadUInt16());
                }
                catch (Exception)
                {
                    Connection.Dispose();
                    return;
                }

                if (Input.BytesLeft == 0)
                {
                    Packet = null;
                }
                else
                {
                    Packet = Input.GetRemainingData();
                }

                bool AllConnected;

                lock (this.remotePlayersByEndpoint)
                {
                    if (!this.playersById.TryGetValue(PlayerId, out Player))
                    {
                        Connection.Dispose();
                        return;
                    }

                    if (Player.Connection is null)
                    {
                        this.connectionCount++;
                    }
                    else
                    {
                        Player.Connection.Dispose();
                    }

                    Player.Connection         = Connection;
                    Connection.StateObject    = Player;
                    Connection.RemoteEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork);

                    AllConnected = this.connectionCount + 1 == this.playerCount;
                }

                MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerConnected;
                if (!(h is null))
                {
                    try
                    {
                        h(this, Player);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                        Debug.WriteLine(ex.StackTrace.ToString());
                    }
                }

                if (AllConnected)
                {
                    this.State = MultiPlayerState.Ready;
                }

                if (Packet is null)
                {
                    return;
                }
            }
            else
            {
                Player = (Player)Connection.StateObject;
            }

            this.GameDataReceived(Player, Connection, Packet);
        }
        private void mqttConnection_OnContentReceived(MqttConnection Sender, MqttContent Content)
        {
            BinaryInput Input   = Content.DataInput;
            byte        Command = Input.ReadByte();

            switch (Command)
            {
            case 0:                     // Hello
                string ApplicationName = Input.ReadString();
                if (ApplicationName != this.applicationName)
                {
                    break;
                }

                Player Player = this.Deserialize(Input);
                if (Player is null)
                {
                    break;
                }

#if LineListener
                Console.Out.WriteLine("Rx: HELLO(" + Player.ToString() + ")");
#endif
                IPEndPoint ExpectedEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork);

                lock (this.remotePlayersByEndpoint)
                {
                    this.remotePlayersByEndpoint[ExpectedEndpoint] = Player;
                    this.remotePlayerIPs[ExpectedEndpoint.Address] = true;
                    this.playersById[Player.PlayerId] = Player;

                    this.UpdateRemotePlayersLocked();
                }

                MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerAvailable;
                if (!(h is null))
                {
                    try
                    {
                        h(this, Player);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                        Debug.WriteLine(ex.StackTrace.ToString());
                    }
                }
                break;

            case 1:                             // Interconnect
                ApplicationName = Input.ReadString();
                if (ApplicationName != this.applicationName)
                {
                    break;
                }

                Player = this.Deserialize(Input);
                if (Player is null)
                {
                    break;
                }

#if LineListener
                Console.Out.Write("Rx: INTERCONNECT(" + Player.ToString());
#endif
                int Index = 0;
                int i, c;
                LinkedList <Player> Players = new LinkedList <Networking.Player>();
                bool LocalPlayerIncluded    = false;

                Player.Index = Index++;
                Players.AddLast(Player);

                c = (int)Input.ReadUInt();
                for (i = 0; i < c; i++)
                {
                    Player = this.Deserialize(Input);
                    if (Player is null)
                    {
#if LineListener
                        Console.Out.Write("," + this.localPlayer.ToString());
#endif
                        this.localPlayer.Index = Index++;
                        LocalPlayerIncluded    = true;
                    }
                    else
                    {
#if LineListener
                        Console.Out.Write("," + Player.ToString());
#endif
                        Player.Index = Index++;
                        Players.AddLast(Player);
                    }
                }

#if LineListener
                Console.Out.WriteLine(")");
#endif
                if (!LocalPlayerIncluded)
                {
                    break;
                }

                this.mqttConnection.Dispose();
                this.mqttConnection = null;

                lock (this.remotePlayersByEndpoint)
                {
                    this.remotePlayersByEndpoint.Clear();
                    this.remotePlayerIPs.Clear();
                    this.remotePlayersByIndex.Clear();
                    this.playersById.Clear();

                    this.remotePlayersByIndex[this.localPlayer.Index] = this.localPlayer;
                    this.playersById[this.localPlayer.PlayerId]       = this.localPlayer;

                    foreach (Player Player2 in Players)
                    {
                        ExpectedEndpoint = Player2.GetExpectedEndpoint(this.p2pNetwork);

                        this.remotePlayersByIndex[Player2.Index]       = Player2;
                        this.remotePlayersByEndpoint[ExpectedEndpoint] = Player2;
                        this.remotePlayerIPs[ExpectedEndpoint.Address] = true;
                        this.playersById[Player2.PlayerId]             = Player2;
                    }

                    this.UpdateRemotePlayersLocked();
                }

                this.State = MultiPlayerState.ConnectingPlayers;
                this.StartConnecting();
                break;

            case 2:                             // Bye
                ApplicationName = Input.ReadString();
                if (ApplicationName != this.applicationName)
                {
                    break;
                }

                Guid PlayerId               = Input.ReadGuid();
                lock (this.remotePlayersByEndpoint)
                {
                    if (!this.playersById.TryGetValue(PlayerId, out Player))
                    {
                        break;
                    }

#if LineListener
                    Console.Out.WriteLine("Rx: BYE(" + Player.ToString() + ")");
#endif
                    ExpectedEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork);

                    this.playersById.Remove(PlayerId);
                    this.remotePlayersByEndpoint.Remove(ExpectedEndpoint);
                    this.remotePlayersByIndex.Remove(Player.Index);

                    IPAddress ExpectedAddress = ExpectedEndpoint.Address;
                    bool      AddressFound    = false;

                    foreach (IPEndPoint EP in this.remotePlayersByEndpoint.Keys)
                    {
                        if (IPAddress.Equals(EP.Address, ExpectedAddress))
                        {
                            AddressFound = true;
                            break;
                        }
                    }

                    if (!AddressFound)
                    {
                        this.remotePlayerIPs.Remove(ExpectedAddress);
                    }

                    this.UpdateRemotePlayersLocked();
                }
                break;
            }
        }
		private void Connection_OnReceived(object Sender, byte[] Packet)
		{
			PeerConnection Connection = (PeerConnection)Sender;
			Guid PlayerId;
			IPAddress PlayerRemoteAddress;
			IPEndPoint PlayerRemoteEndpoint;

			try
			{
				BinaryInput Input = new BinaryInput(Packet);

				PlayerId = Input.ReadGuid();
				PlayerRemoteAddress = IPAddress.Parse(Input.ReadString());
				PlayerRemoteEndpoint = new IPEndPoint(PlayerRemoteAddress, Input.ReadUInt16());
			}
			catch (Exception)
			{
				Connection.Dispose();
				return;
			}

			Player Player = (Player)Connection.StateObject;
			Player Player2;

			lock (this.remotePlayersByEndpoint)
			{
				if (!this.playersById.TryGetValue(PlayerId, out Player2) || Player2.PlayerId != Player.PlayerId)
				{
					Connection.Dispose();
					return;
				}

				Player.Connection = Connection;
			}

			Connection.RemoteEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork);

			Connection.OnReceived -= new BinaryEventHandler(Connection_OnReceived);
			Connection.OnReceived += new BinaryEventHandler(Peer_OnReceived);

			Connection.OnSent += new BinaryEventHandler(Connection_OnSent);

			BinaryOutput Output = new BinaryOutput();

			Output.WriteGuid(this.localPlayer.PlayerId);
			Output.WriteString(this.ExternalAddress.ToString());
			Output.WriteUInt16((ushort)this.ExternalEndpoint.Port);

			Connection.SendTcp(Output.GetPacket());

			MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerConnected;
			if (h != null)
			{
				try
				{
					h(this, Player);
				}
				catch (Exception ex)
				{
					Debug.WriteLine(ex.Message);
					Debug.WriteLine(ex.StackTrace.ToString());
				}
			}
		}
		private void Peer_OnReceived(object Sender, byte[] Packet)
		{
			PeerConnection Connection = (PeerConnection)Sender;
			Player Player;

			if (Connection.StateObject == null)
			{
				BinaryInput Input = new BinaryInput(Packet);
				Guid PlayerId;
				IPAddress PlayerRemoteAddress;
				IPEndPoint PlayerRemoteEndpoint;

				try
				{
					PlayerId = Input.ReadGuid();
					PlayerRemoteAddress = IPAddress.Parse(Input.ReadString());
					PlayerRemoteEndpoint = new IPEndPoint(PlayerRemoteAddress, Input.ReadUInt16());
				}
				catch (Exception)
				{
					Connection.Dispose();
					return;
				}

				if (Input.BytesLeft == 0)
					Packet = null;
				else
					Packet = Input.GetRemainingData();

				bool AllConnected;

				lock (this.remotePlayersByEndpoint)
				{
					if (!this.playersById.TryGetValue(PlayerId, out Player))
					{
						Connection.Dispose();
						return;
					}

					if (Player.Connection == null)
						this.connectionCount++;
					else
						Player.Connection.Dispose();

					Player.Connection = Connection;
					Connection.StateObject = Player;
					Connection.RemoteEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork);

					AllConnected = this.connectionCount + 1 == this.playerCount;
				}

				MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerConnected;
				if (h != null)
				{
					try
					{
						h(this, Player);
					}
					catch (Exception ex)
					{
						Debug.WriteLine(ex.Message);
						Debug.WriteLine(ex.StackTrace.ToString());
					}
				}

				if (AllConnected)
					this.State = MultiPlayerState.Ready;

				if (Packet == null)
					return;
			}
			else
				Player = (Player)Connection.StateObject;

			this.GameDataReceived(Player, Connection, Packet);
		}
		private Player Deserialize(BinaryInput Input)
		{
			IPAddress PublicAddress = IPAddress.Parse(Input.ReadString());
			ushort PublicPort = Input.ReadUInt16();
			IPEndPoint PublicEndpoint = new IPEndPoint(PublicAddress, PublicPort);

			IPAddress LocalAddress = IPAddress.Parse(Input.ReadString());
			ushort LocalPort = Input.ReadUInt16();
			IPEndPoint LocalEndpoint = new IPEndPoint(LocalAddress, LocalPort);

			Guid PlayerId = Input.ReadGuid();
			bool LocalPlayer = PlayerId == this.localPlayer.PlayerId;
			int i, c = (int)Input.ReadUInt();
			KeyValuePair<string, string>[] PlayerMetaInfo = LocalPlayer ? null : new KeyValuePair<string, string>[c];
			string Key, Value;

			for (i = 0; i < c; i++)
			{
				Key = Input.ReadString();
				Value = Input.ReadString();
				if (!LocalPlayer)
					PlayerMetaInfo[i] = new KeyValuePair<string, string>(Key, Value);
			}

			if (LocalPlayer)
				return null;
			else
				return new Player(PlayerId, PublicEndpoint, LocalEndpoint, PlayerMetaInfo);
		}