Beispiel #1
0
        /// <summary>
        /// Handle result of join lobby operation.
        /// </summary>
        /// <param name="result">The operation result.</param>
        /// <param name="ioFailure">Did IO failure happen?</param>
        void OnLobbyEnter(Steamworks.LobbyEnter_t result, bool ioFailure)
        {
            if (ioFailure || result.m_EChatRoomEnterResponse != (uint)Steamworks.EChatRoomEnterResponse.k_EChatRoomEnterResponseSuccess)
            {
                Logger.Error("Failed to join lobby. (reponse: {result.m_EChatRoomEnterResponse}, ioFailure: {ioFailure})");
                MPGUI.Instance.ShowMessageBox($"Failed to join lobby.\n(reponse: {result.m_EChatRoomEnterResponse}, ioFailure: {ioFailure})");

                players[1] = null;
                return;
            }

            // Setup local player.
            players[0] = new NetLocalPlayer(this, netWorld, Steamworks.SteamUser.GetSteamID());

            Logger.Debug("Entered lobby: " + result.m_ulSteamIDLobby);

            MessagesList.AddMessage("Entered lobby.", MessageSeverity.Info);

            mode           = Mode.Player;
            state          = State.LoadingGameWorld;
            currentLobbyId = new Steamworks.CSteamID(result.m_ulSteamIDLobby);

            ShowLoadingScreen(true);
            SendHandshake(players[1]);
        }
Beispiel #2
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="netManager">The network manager owning this player.</param>
        /// <param name="netWorld">Network world owning this player.</param>
        /// <param name="steamId">The steam id of the player.</param>
        public NetLocalPlayer(
            NetManager netManager, NetWorld netWorld, Steamworks.CSteamID steamId)
            : base(netManager, netWorld, steamId)
        {
            Instance = this;
            steamID  = steamId;

            GameDoorsManager.Instance.onDoorsOpen = (GameObject door) => {
                Messages.OpenDoorsMessage msg = new Messages.OpenDoorsMessage();
                msg.position = Utils.GameVec3ToNet(door.transform.position);
                msg.open     = true;
                netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable);
            };

            GameDoorsManager.Instance.onDoorsClose = (GameObject door) => {
                Messages.OpenDoorsMessage msg = new Messages.OpenDoorsMessage();
                msg.position = Utils.GameVec3ToNet(door.transform.position);
                msg.open     = false;
                netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable);
            };

            LightSwitchManager.Instance.onLightSwitchUsed =
                (GameObject lswitch, bool turnedOn) => {
                Messages.LightSwitchMessage msg = new Messages.LightSwitchMessage();
                msg.pos    = Utils.GameVec3ToNet(lswitch.transform.position);
                msg.toggle = turnedOn;
                netManager.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable);
            };

            if (animManager == null)
            {
                animManager = new PlayerAnimManager();
            }
        }
Beispiel #3
0
        public NetManager()
        {
            this.netManagerCreationTime = DateTime.UtcNow;
            netMessageHandler           = new NetMessageHandler(this);
            netWorld = new NetWorld(this);

            p2pSessionRequestCallback = Steamworks.Callback <Steamworks.P2PSessionRequest_t> .Create((Steamworks.P2PSessionRequest_t result) => {
                if (!Steamworks.SteamNetworking.AcceptP2PSessionWithUser(result.m_steamIDRemote))
                {
                    Logger.Log("Accepted p2p session with " + result.m_steamIDRemote.ToString());
                }
            });

            gameLobbyJoinRequestedCallback = Steamworks.Callback <Steamworks.GameLobbyJoinRequested_t> .Create(OnGameLobbyJoinRequested);

            lobbyCreatedCallResult = new Steamworks.CallResult <Steamworks.LobbyCreated_t>((Steamworks.LobbyCreated_t result, bool ioFailure) => {
                if (result.m_eResult != Steamworks.EResult.k_EResultOK)
                {
                    Logger.Log("Oh my f*****g god i failed to create a lobby for you. Please forgive me. (result: " + result.m_eResult + ")");

                    MPGUI.Instance.ShowMessageBox("Failed to create lobby due to steam error.\n" + result.m_eResult, () => {
                        MPController.Instance.LoadLevel("MainMenu");
                    });
                    return;
                }

                Logger.Log("Hey you! I have lobby id for you! " + result.m_ulSteamIDLobby);

                // Setup local player.
                players[0] = new NetLocalPlayer(this, netWorld, Steamworks.SteamUser.GetSteamID());

                mode           = Mode.Host;
                state          = State.Playing;
                currentLobbyId = new Steamworks.CSteamID(result.m_ulSteamIDLobby);
            });

            lobbyEnterCallResult = new Steamworks.CallResult <Steamworks.LobbyEnter_t>((Steamworks.LobbyEnter_t result, bool ioFailure) => {
                if (result.m_EChatRoomEnterResponse != (uint)Steamworks.EChatRoomEnterResponse.k_EChatRoomEnterResponseSuccess)
                {
                    Logger.Log("Oh my f*****g god i failed to join the lobby for you. Please forgive me. (reponse: " + result.m_EChatRoomEnterResponse + ")");

                    players[1] = null;
                    return;
                }

                // Setup local player.
                players[0] = new NetLocalPlayer(this, netWorld, Steamworks.SteamUser.GetSteamID());

                Logger.Log("Oh hello! " + result.m_ulSteamIDLobby);

                mode           = Mode.Player;
                state          = State.LoadingGameWorld;
                currentLobbyId = new Steamworks.CSteamID(result.m_ulSteamIDLobby);

                ShowLoadingScreen(true);
                SendHandshake();
            });

            RegisterProtocolMessagesHandlers();
        }
Beispiel #4
0
        /// <summary>
        /// Handle result of create lobby operation.
        /// </summary>
        /// <param name="result">The operation result.</param>
        /// <param name="ioFailure">Did IO failure happen?</param>
        void OnLobbyCreated(Steamworks.LobbyCreated_t result, bool ioFailure)
        {
            if (result.m_eResult != Steamworks.EResult.k_EResultOK || ioFailure)
            {
                Logger.Log($"Failed to create lobby. (result: {result.m_eResult}, io failure: {ioFailure})");

                MPGUI.Instance.ShowMessageBox($"Failed to create lobby due to steam error.\n{result.m_eResult}/{ioFailure}", () => {
                    MPController.Instance.LoadLevel("MainMenu");
                });
                return;
            }

            Logger.Debug($"Lobby has been created, lobby id: {result.m_ulSteamIDLobby}");
            MessagesList.AddMessage("Session started.", MessageSeverity.Info);

            // Setup local player.
            players[0] = new NetLocalPlayer(this, netWorld, Steamworks.SteamUser.GetSteamID());

            mode           = Mode.Host;
            state          = State.Playing;
            currentLobbyId = new Steamworks.CSteamID(result.m_ulSteamIDLobby);
        }
Beispiel #5
0
        public NetManager()
        {
            this.netManagerCreationTime = DateTime.UtcNow;
            netWorld = new NetWorld(this);

            // Setup local player.
            players[0] = new NetLocalPlayer(this, netWorld, Steamworks.SteamUser.GetSteamID());

            p2pSessionRequestCallback = Steamworks.Callback <Steamworks.P2PSessionRequest_t> .Create((Steamworks.P2PSessionRequest_t result) => {
                if (!Steamworks.SteamNetworking.AcceptP2PSessionWithUser(result.m_steamIDRemote))
                {
                    Logger.Log("Accepted p2p session with " + result.m_steamIDRemote.ToString());
                }
            });

            gameLobbyJoinRequestedCallback = Steamworks.Callback <Steamworks.GameLobbyJoinRequested_t> .Create(OnGameLobbyJoinRequested);

            lobbyCreatedCallResult = new Steamworks.CallResult <Steamworks.LobbyCreated_t>((Steamworks.LobbyCreated_t result, bool ioFailure) => {
                if (result.m_eResult != Steamworks.EResult.k_EResultOK)
                {
                    Logger.Log("Oh my f*****g god i failed to create a lobby for you. Please forgive me. (result: " + result.m_eResult + ")");

                    MPGUI.Instance.ShowMessageBox("Failed to create lobby due to steam error.\n" + result.m_eResult, () => {
                        MPController.Instance.LoadLevel("MainMenu");
                    });
                    return;
                }

                Logger.Log("Hey you! I have lobby id for you! " + result.m_ulSteamIDLobby);

                mode           = Mode.Host;
                state          = State.Playing;
                currentLobbyId = new Steamworks.CSteamID(result.m_ulSteamIDLobby);

                netWorld.RegisterPickupables();
            });

            lobbyEnterCallResult = new Steamworks.CallResult <Steamworks.LobbyEnter_t>((Steamworks.LobbyEnter_t result, bool ioFailure) => {
                if (result.m_EChatRoomEnterResponse != (uint)Steamworks.EChatRoomEnterResponse.k_EChatRoomEnterResponseSuccess)
                {
                    Logger.Log("Oh my f*****g god i failed to join the lobby for you. Please forgive me. (reponse: " + result.m_EChatRoomEnterResponse + ")");

                    players[1] = null;
                    return;
                }

                Logger.Log("Oh hello! " + result.m_ulSteamIDLobby);

                mode           = Mode.Player;
                state          = State.LoadingGameWorld;
                currentLobbyId = new Steamworks.CSteamID(result.m_ulSteamIDLobby);

                SendHandshake();
            });

            // TODO: Move message handlers to some class.

            BindMessageHandler((Steamworks.CSteamID sender, Messages.HandshakeMessage msg) => {
                remoteClock = msg.clock;
                HandleHandshake(sender, msg);
            });

            BindMessageHandler((Steamworks.CSteamID sender, Messages.PlayerSyncMessage msg) => {
                if (players[1] == null)
                {
                    Logger.Log("Received synchronization packet but no remote player is currently connected.");
                    return;
                }

                players[1].HandleSynchronize(msg);
            });

            BindMessageHandler((Steamworks.CSteamID sender, Messages.HeartbeatMessage msg) => {
                var message         = new Messages.HeartbeatResponseMessage();
                message.clientClock = msg.clientClock;
                message.clock       = GetNetworkClock();
                BroadcastMessage(message, Steamworks.EP2PSend.k_EP2PSendReliable);
            });

            BindMessageHandler((Steamworks.CSteamID sender, Messages.HeartbeatResponseMessage msg) => {
                ping = (uint)(GetNetworkClock() - msg.clientClock);

                // TODO: Some smart lag compensation.
                remoteClock = msg.clock;

                timeSinceLastHeartbeat = 0.0f;
            });

            BindMessageHandler((Steamworks.CSteamID sender, Messages.DisconnectMessage msg) => {
                HandleDisconnect(false);
            });

            BindMessageHandler((Steamworks.CSteamID sender, Messages.OpenDoorsMessage msg) => {
                NetPlayer player = players[1];
                // 1.5 is a length of the ray used to check interaction with doors in game scripts.
                Vector3 interactionPosition = player.GetPosition() + player.GetRotation() * Vector3.forward * 1.5f;
                Game.Objects.GameDoor doors = Game.GameDoorsManager.Instance.FindGameDoors(interactionPosition);
                if (doors == null)
                {
                    Logger.Log($"Player tried to open doors however he is not close to any: {interactionPosition}.");
                    return;
                }
                doors.Open(msg.open);
            });

            BindMessageHandler((Steamworks.CSteamID sender, Messages.FullWorldSyncMessage msg) => {
                netWorld.HandleFullWorldSync(msg);

                // Now spawn player.

                if (players[1] != null)
                {
                    players[1].Spawn();
                }

                // World loaded we are playing!

                state = State.Playing;
            });

            BindMessageHandler((Steamworks.CSteamID sender, Messages.AskForWorldStateMessage msg) => {
                var msgF = new Messages.FullWorldSyncMessage();
                netWorld.WriteFullWorldSync(msgF);
                BroadcastMessage(msgF, Steamworks.EP2PSend.k_EP2PSendReliable);
            });

            BindMessageHandler((Steamworks.CSteamID sender, Messages.VehicleEnterMessage msg) => {
                NetPlayer player = players[1];
                if (player == null)
                {
                    return;
                }

                NetVehicle vehicle = netWorld.GetVehicle(msg.vehicleId);
                if (vehicle == null)
                {
                    Logger.Log("Player " + player.SteamId + " tried to enter vehicle " + msg.vehicleId + " but there is no vehicle with such id.");
                    return;
                }

                player.EnterVehicle(vehicle, msg.passenger);
            });

            BindMessageHandler((Steamworks.CSteamID sender, Messages.VehicleLeaveMessage msg) => {
                NetPlayer player = players[1];
                if (player == null)
                {
                    return;
                }
                player.LeaveVehicle();
            });


            BindMessageHandler((Steamworks.CSteamID sender, Messages.VehicleSyncMessage msg) => {
                NetPlayer player = players[1];
                if (player == null)
                {
                    return;
                }

                player.HandleVehicleSync(msg);
            });


            BindMessageHandler((Steamworks.CSteamID sender, Messages.PickupObjectMessage msg) => {
                NetPlayer player = players[1];
                if (player == null)
                {
                    return;
                }
                player.PickupObject(msg.netId);
            });

            BindMessageHandler((Steamworks.CSteamID sender, Messages.ReleaseObjectMessage msg) => {
                NetPlayer player = players[1];
                if (player == null)
                {
                    return;
                }
                player.ReleaseObject(msg.drop);
            });
        }