void RemoveSimilarPackets() //we merge same inputs into one { SavedMove toSendMove = new SavedMove(); int counter = 0; foreach (SavedMove sm in PendingMoveList) { if (counter == 0) { toSendMove = sm; counter++; //first just scouts for first msg } else { if (Vector3.Distance(toSendMove.getPostion(), sm.getPostion()) < 0.05 && toSendMove.getDirection() == sm.getDirection() && !sm.shooting) { Debug.Log("SAME PACKET TO REMOVE"); counter++; //now counts amount of mssg to remove toSendMove = sm; } else { break; //next is diffrent so we will send first one anywat } } } Debug.Log(" PACKET TO REMOVE : " + (counter - 1)); if (counter > 1) { PendingMoveList.RemoveRange(0, counter - 1); } }
private void RecreateShoot(SavedMove savedMove) { if (savedMove.shooting) { RecreatePostionListner.Invoke(savedMove.timestamp); RaycastHit hit; Debug.Log("shooting"); if (Physics.Raycast(rayOrgin.position, rayOrgin.TransformDirection(Vector3.forward), out hit, 700)) { Debug.DrawRay(rayOrgin.position, rayOrgin.TransformDirection(Vector3.forward) * hit.distance * 700, Color.red); Debug.Log("hit"); if (hit.transform.tag == "Target") { Debug.Log("target hit at " + hit.transform.position); } } else { Debug.DrawRay(rayOrgin.position, rayOrgin.TransformDirection(Vector3.forward) * hit.distance * 700, Color.white); Debug.Log("no hit"); } } ReturnToOrginalPostionListner.Invoke(); }
void ServerMove(SavedMove savedMove) { if (botColor == BotColor.Red && photonView.IsMine) { Vector3 serverStartPos = transform.position; float startRotation = transform.rotation.eulerAngles.y; if (serverStartPos != savedMove.getStartPostion()) { if (serverStartPos == Vector3.zero) { serverStartPos = savedMove.getStartPostion(); transform.position = savedMove.getStartPostion(); } } PerformeMovment(savedMove, Time.deltaTime); float distance = Mathf.Abs(Vector3.Distance(transform.position, savedMove.getPostion())); SaveCurrentStateToFile(); ServerLastMove = new SavedMove(savedMove.timestamp, savedMove.forwardmove, savedMove.sidemove, transform.rotation.eulerAngles.y, startRotation, transform.position, serverStartPos, CurrentSpeed, savedMove.shooting, savedMove.stand); if (distance > maxPosDiff) { if ((CorrectionTime + CorrectionBound) < PhotonNetwork.Time) { ClientAdjustment adjustment = new ClientAdjustment(false, serverStartPos, transform.position, transform.rotation.y, savedMove.timestamp); photonView.RPC("ClientAdjustPosition", RpcTarget.All, adjustment); byte evCode = 2; RaiseEventOptions raiseEventOptions = new RaiseEventOptions { Receivers = ReceiverGroup.Others }; SendOptions sendOptions = new SendOptions { Reliability = true }; PhotonNetwork.RaiseEvent(evCode, adjustment, raiseEventOptions, sendOptions); CorrectionTime = PhotonNetwork.Time; } } else { Debug.Log(" MOVE CORRECT "); RecreateShoot(savedMove); ClientAdjustment adjustment = new ClientAdjustment(); adjustment.AckGoodMove = true; adjustment.TimeStamp = savedMove.timestamp; byte evCode = 2; RaiseEventOptions raiseEventOptions = new RaiseEventOptions { Receivers = ReceiverGroup.Others }; SendOptions sendOptions = new SendOptions { Reliability = true }; PhotonNetwork.RaiseEvent(evCode, adjustment, raiseEventOptions, sendOptions); } } }
private void ReplicateMoveToServer(Vector3 startPostion, float startRotation) //just adding move to list { SavedMove toSaveMove = new SavedMove(PhotonNetwork.Time, moveDir.z, moveDir.x, transform.rotation.eulerAngles.y, startRotation, transform.position, startPostion, CurrentSpeed, shooting, standing); if (PendingMoveList != null) { PendingMoveList.Add(toSaveMove); } }
public void OnEvent(EventData photonEvent) { if (photonEvent.Code == 1) { if (botColor == BotColor.Red && photonView.IsMine) { SavedMove recivedMove = (SavedMove)photonEvent.CustomData; ServerMove(recivedMove); } } if (photonEvent.Code == 2) { if (botColor == BotColor.Green && photonView.IsMine) { ClientAdjustment clientAdjustment = (ClientAdjustment)photonEvent.CustomData; ClientAdjustPosition(clientAdjustment); } } }
// Start is called before the first frame update void Start() { PhotonPeer.RegisterType(typeof(SavedMove), 2, SavedMove.Serialize, SavedMove.Deserialize); PhotonPeer.RegisterType(typeof(ClientAdjustment), 3, ClientAdjustment.Serialize, ClientAdjustment.Deserialize); controller = GetComponent <CharacterController>(); animat = GetComponent <Animator>(); photonView = GetComponent <PhotonView>(); Destroy(playerCamera); Destroy(playerAudio); LaserObject = Instantiate(LaserEffect, rayOrgin); LaserObject.SetActive(false); controller.detectCollisions = false; StartTime = PhotonNetwork.Time; CurrentStartPhotonTime = 0; // PhotonNetwork.Time-0.5f; MyLocalTime = Time.time; //savedMoves = new Queue<SavedMove>(); PendingMoveList = new List <SavedMove>(); InterpolationBuffer = new Queue <SavedMove>(); ServerLastMove = new SavedMove(); ReplicatedMovement = new SavedMove(); latestAdjustment = new ClientAdjustment(); logfile = new StringBuilder(); var newLine = string.Format("{0}:{1}:{2}:{3}:{4}", "Index", "PostionX", "PostionZ", "RotationAngle", "PhotonTime"); logfile.AppendLine(newLine); PhotonNetwork.SendRate = 80; PhotonNetwork.SerializationRate = 80; if (botColor == BotColor.Target) { RecreatePostionListner += recreatePostion; ReturnToOrginalPostionListner += returnToOrginalPosition; } }
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { if (stream.IsWriting) { if (botColor == BotColor.Red || botColor == BotColor.Target) { if (testcounter % 2 == 0) { Debug.Log("sending shoots" + ServerLastMove.shooting); stream.SendNext(ServerLastMove); testcounter = 0; } testcounter++; } } else { SavedMove server_lastMove = (SavedMove)stream.ReceiveNext(); ServerLastMove = server_lastMove; } }
private void PerformeMovment(SavedMove savedMove, float deltaTime) { if (savedMove.stand) { BotState = BotState.Standing; animat.SetInteger("condition", 0); } else { BotState = BotState.Moving; animat.SetInteger("condition", 1); } Vector3 MoveDirection = savedMove.getDirection(); Move(MoveDirection, deltaTime); transform.rotation = Quaternion.Euler(savedMove.getRotationAngle()); if (savedMove.shooting) { Shooting(); } }
void FixedUpdate() { SaveCounter++; //Debug.Log("me is " + botColor + " mine " + photonView.IsMine); if (photonView.IsMine && botColor == BotColor.Green) { if (bUpdatePosition) { ClientUpdatePosition(); } Vector3 OldPos = transform.position; float OldRot = transform.rotation.eulerAngles.y; MovementTestSequnce();//performe movement SaveCurrentStateToFile(); ReplicateMoveToServer(OldPos, OldRot); //serverMove byte evCode = 1; RaiseEventOptions raiseEventOptions = new RaiseEventOptions { Receivers = ReceiverGroup.Others }; SendOptions sendOptions = new SendOptions { Reliability = true }; PhotonNetwork.RaiseEvent(evCode, PendingMoveList[PendingMoveList.Count - 1], raiseEventOptions, sendOptions); } else if (photonView.IsMine && botColor == BotColor.Red) { } else if (photonView.IsMine && botColor == BotColor.Target) { Vector3 OldPos = transform.position; float OldRot = transform.rotation.eulerAngles.y; TargetMovmentScript(); ReplicateMoveToServer(OldPos, OldRot); ServerLastMove = PendingMoveList[0]; InterpolationBuffer.Enqueue(ServerLastMove); //works as history here PendingMoveList.RemoveAt(0); if (InterpolationBuffer.Count > 100) //cleaning { InterpolationBuffer.Dequeue(); } } else if (!photonView.IsMine && botColor == BotColor.Red) { if (InterpolationActive) { SimulateMovment(); } else { PerformeMovment(ServerLastMove, Time.deltaTime); } SaveCurrentStateToFile(); } else if (!photonView.IsMine && botColor == BotColor.Target) { if (InterpolationActive) { SimulateMovment(); } else { PerformeMovment(ServerLastMove, Time.deltaTime); } } if (SaveCounter == 750) { string filename = ""; if (photonView.IsMine && botColor == BotColor.Green) { filename = "UnrealGreen"; } else if (photonView.IsMine && botColor == BotColor.Red) { filename = "UnrealRedServer"; } else if (!photonView.IsMine && botColor == BotColor.Red) { filename = "UnrealRedProxy"; } File.WriteAllText(@"/path" + filename + ".csv", logfile.ToString()); Debug.Log(" Log Saved in file "); } }
private void SmoothClientPosition_Interpolate(SavedMove ClientData, SavedMove ClientDataNext, ENetworkSmoothingMode smoothingMode) { //if (ClientDataNext.shooting) //{ // Shooting(); //} //if (ClientDataNext.stand) //{ // BotState = BotState.Standing; // animat.SetInteger("condition", 0); // double RemainingTime = ClientDataNext.timestamp - ClientData.timestamp; // double LastCorrectionDelta = ClientDataNext.timestamp - InterpolationFrameStartTime; // double LerpPercent = RemainingTime / LastCorrectionDelta; // transform.position = Vector3.Lerp(ClientData.getPostion(), ClientDataNext.getPostion(), (float)LerpPercent); // //is this good ? // // Debug.Log(botColor + " interpolating to" + ClientDataNext.timestamp + " desired postion " + ClientDataNext.getPostion() + " shooting " + ClientDataNext.shooting + " standing " + ClientDataNext.stand); // interpolating = false; //} //else if (smoothingMode == ENetworkSmoothingMode.Linear) { // Debug.Log(botColor + " interpolating to" + ClientDataNext.timestamp + " desired postion " + ClientDataNext.getPostion() + " shooting " + ClientDataNext.shooting + " standing " + ClientDataNext.stand); float LerpPercent = 0f; const float LerpLimit = 1.15f; CurrentStartPhotonTime += Time.deltaTime; // if time is right ReplicatedMovement double RemainingTime = ClientDataNext.timestamp - ClientData.timestamp; double LastCorrectionDelta = ClientDataNext.timestamp - InterpolationFrameStartTime; double CurrentSmoothTime = LastCorrectionDelta - RemainingTime; // Debug.Log("TIMES server " + ServerData.timestamp + " client data " + ClientData.timestamp + " CurrentStartPhotonTime " + CurrentStartPhotonTime + " started this shiet on " + InterpolationFrameStartTime); double toClamp = CurrentSmoothTime / LastCorrectionDelta; LerpPercent = Mathf.Clamp((float)toClamp, 0.0f, LerpLimit); // Debug.Log("TIMES serverpack " + ServerData.timestamp + " client data " + ClientData.timestamp + " LastCorrectionDelta "+ LastCorrectionDelta+ " RemainingTime " + RemainingTime + " Percent " + LerpPercent); if (LerpPercent >= 1.0f - 0.001f) { interpolating = false; CurrentStartPhotonTime = ClientDataNext.timestamp; transform.rotation = Quaternion.Euler(ClientDataNext.getRotationAngle()); } else { BotState = BotState.Moving; if (!ClientDataNext.shooting) { animat.SetInteger("condition", 1); } Vector3 offset = Vector3.Lerp(Vector3.zero, orginaloffsetPostion, LerpPercent); Vector3 CurrentDelta = ClientData.getPostion() - ClientDataNext.getPostion(); Vector3 finalPostion = transform.position + offset; //Debug.Log(" orginaloffset " + orginaloffsetPostion + "percent " + LerpPercent + " offset " + offset + " postion from " + ClientData.getPostion() + " to " + ClientDataNext.getPostion() + " delta beetween " + CurrentDelta + " postion after adding offset" + finalPostion); Quaternion rotationOffset = Quaternion.Lerp(orginaloffsetRotation, Quaternion.Euler(ClientDataNext.getRotationAngle()), LerpPercent); transform.rotation = rotationOffset; //transform.position = finalPostion; Move(offset, Time.deltaTime); } } else if (smoothingMode == ENetworkSmoothingMode.Exponential) { } else if (smoothingMode == ENetworkSmoothingMode.Replay) { float StartTime = Time.time; if (CurrentReplayTime >= ClientData.timestamp && CurrentReplayTime <= ClientDataNext.timestamp) { const float EPSILON = 0.01f; double Delta = ClientDataNext.timestamp - ClientData.timestamp; float LerpPercent; if (Delta > EPSILON) { double toClamp = (CurrentReplayTime - ClientData.timestamp) / Delta; LerpPercent = Mathf.Clamp((float)toClamp, 0.0f, 1.0f); } else { LerpPercent = 1.0f; } Vector3 Location = Vector3.Lerp(ClientData.getPostion(), ClientDataNext.getPostion(), LerpPercent); Quaternion rotation = Quaternion.Lerp(Quaternion.Euler(ClientData.getRotationAngle()), Quaternion.Euler(ClientDataNext.getRotationAngle()), LerpPercent); //normailzed ? //Debug.Log("REPLAY Time on " + botColor + CurrentReplayTime + " location from " + ClientData.getPostion() + "loc to" + ClientDataNext.getPostion() + " lerp " + Location + " my postion " + transform.position + " rotation " + rotation); transform.position = Location; transform.rotation = rotation; //PerformeMovment(ClientData, Time.deltaTime); } else { Debug.Log("REPLAY TIME NOT CAUGHT Q!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); } float DeltaTime = Time.time - StartTime; CurrentReplayTime += DeltaTime; } else { } }