CPR syncing + some refactoring

Clients send an entityevent to the server when they start/stop doing CPR, and server includes the animation state in messages when character is dragging some other character
This commit is contained in:
Regalis
2017-03-22 18:32:10 +02:00
parent 823ad12058
commit d4db37f8dd
8 changed files with 96 additions and 33 deletions
@@ -1273,6 +1273,15 @@ namespace Barotrauma
} }
} }
if (character.MemState[0].Animation == AnimController.Animation.CPR)
{
character.AnimController.Anim = AnimController.Animation.CPR;
}
else if (character.AnimController.Anim == AnimController.Animation.CPR)
{
character.AnimController.Anim = AnimController.Animation.None;
}
Collider.LinearVelocity = Vector2.Zero; Collider.LinearVelocity = Vector2.Zero;
Collider.CorrectPosition(character.MemState, deltaTime, out overrideTargetMovement); Collider.CorrectPosition(character.MemState, deltaTime, out overrideTargetMovement);
@@ -1341,22 +1350,30 @@ namespace Barotrauma
else if (serverPos.Interact is Item) else if (serverPos.Interact is Item)
{ {
var newSelectedConstruction = (Item)serverPos.Interact; var newSelectedConstruction = (Item)serverPos.Interact;
if (newSelectedConstruction != null && character.SelectedConstruction != newSelectedConstruction) if (newSelectedConstruction != null &&
character.SelectedConstruction != newSelectedConstruction)
{ {
newSelectedConstruction.Pick(character, true, true); newSelectedConstruction.Pick(character, true, true);
} }
} }
} }
if (localPos.Animation != serverPos.Animation)
{
if (serverPos.Animation == AnimController.Animation.CPR)
{
character.AnimController.Anim = AnimController.Animation.CPR;
}
else if (character.AnimController.Anim == AnimController.Animation.CPR)
{
character.AnimController.Anim = AnimController.Animation.None;
}
}
Vector2 positionError = serverPos.Position - localPos.Position; Vector2 positionError = serverPos.Position - localPos.Position;
for (int i = localPosIndex; i < character.MemLocalState.Count; i++) for (int i = localPosIndex; i < character.MemLocalState.Count; i++)
{ {
character.MemLocalState[i] = character.MemLocalState[i].Translate(positionError);
new CharacterStateInfo(
character.MemLocalState[i].Position + positionError,
character.MemLocalState[i].ID,
character.MemLocalState[i].Direction,
character.MemLocalState[i].Interact);
} }
Collider.SetTransform(Collider.SimPosition + positionError, Collider.Rotation); Collider.SetTransform(Collider.SimPosition + positionError, Collider.Rotation);
+1 -1
View File
@@ -1147,7 +1147,7 @@ namespace Barotrauma
} }
cam.OffsetAmount = MathHelper.Lerp(cam.OffsetAmount, 250.0f, 0.05f); cam.OffsetAmount = MathHelper.Lerp(cam.OffsetAmount, 250.0f, 0.05f);
} }
cursorPosition = cam.ScreenToWorld(PlayerInput.MousePosition); cursorPosition = cam.ScreenToWorld(PlayerInput.MousePosition);
if (AnimController.CurrentHull != null && AnimController.CurrentHull.Submarine != null) if (AnimController.CurrentHull != null && AnimController.CurrentHull.Submarine != null)
{ {
@@ -168,6 +168,11 @@ namespace Barotrauma
limb.pullJoint.Enabled = false; limb.pullJoint.Enabled = false;
} }
if (GameMain.Client != null)
{
GameMain.Client.CreateEntityEvent(Character.Controlled, new object[] { NetEntityEvent.Type.Repair });
}
return true; return true;
}; };
} }
@@ -13,21 +13,25 @@ namespace Barotrauma
public readonly Entity Interact; //the entity being interacted with public readonly Entity Interact; //the entity being interacted with
public CharacterStateInfo(Vector2 pos, float time, Direction dir, Entity interact) public readonly AnimController.Animation Animation;
: this(pos, 0, time, dir, interact)
public CharacterStateInfo(Vector2 pos, float time, Direction dir, Entity interact, AnimController.Animation animation = AnimController.Animation.None)
: this(pos, 0, time, dir, interact, animation)
{ {
} }
public CharacterStateInfo(Vector2 pos, UInt16 ID, Direction dir, Entity interact) public CharacterStateInfo(Vector2 pos, UInt16 ID, Direction dir, Entity interact, AnimController.Animation animation = AnimController.Animation.None)
: this(pos, ID, 0.0f, dir, interact) : this(pos, ID, 0.0f, dir, interact, animation)
{ {
} }
protected CharacterStateInfo(Vector2 pos, UInt16 ID, float time, Direction dir, Entity interact) protected CharacterStateInfo(Vector2 pos, UInt16 ID, float time, Direction dir, Entity interact, AnimController.Animation animation = AnimController.Animation.None)
: base(pos, ID, time) : base(pos, ID, time)
{ {
Direction = dir; Direction = dir;
Interact = interact; Interact = interact;
Animation = animation;
} }
} }
@@ -66,6 +70,7 @@ namespace Barotrauma
public InputNetFlags states; //keys pressed/other boolean states at this step public InputNetFlags states; //keys pressed/other boolean states at this step
public UInt16 intAim; //aim angle, represented as an unsigned short where 0=0º, 65535=just a bit under 360º public UInt16 intAim; //aim angle, represented as an unsigned short where 0=0º, 65535=just a bit under 360º
public UInt16 interact; //id of the entity being interacted with public UInt16 interact; //id of the entity being interacted with
public AnimController.Animation? animation;
public UInt16 networkUpdateID; public UInt16 networkUpdateID;
} }
@@ -133,13 +138,15 @@ namespace Barotrauma
var closestEntity = Entity.FindEntityByID(memInput[memInput.Count - 1].interact); var closestEntity = Entity.FindEntityByID(memInput[memInput.Count - 1].interact);
if (closestEntity is Item) if (closestEntity is Item)
{ {
closestItem = closestEntity as Item; closestItem = (Item)closestEntity;
closestCharacter = null;
} }
else if (closestEntity is Character) else if (closestEntity is Character)
{ {
closestCharacter = closestEntity as Character; closestCharacter = (Character)closestEntity;
closestItem = null;
} }
memInput.RemoveAt(memInput.Count - 1); memInput.RemoveAt(memInput.Count - 1);
TransformCursorPos(); TransformCursorPos();
@@ -163,7 +170,8 @@ namespace Barotrauma
SimPosition, SimPosition,
LastNetworkUpdateID, LastNetworkUpdateID,
AnimController.TargetDir, AnimController.TargetDir,
selectedCharacter == null ? (Entity)selectedConstruction : (Entity)selectedCharacter); selectedCharacter == null ? (Entity)selectedConstruction : (Entity)selectedCharacter,
AnimController.Anim);
memLocalState.Add(posInfo); memLocalState.Add(posInfo);
@@ -221,9 +229,19 @@ namespace Barotrauma
{ {
if (GameMain.Server != null) return; if (GameMain.Server != null) return;
if (extraData != null && (NetEntityEvent.Type)extraData[0] == NetEntityEvent.Type.InventoryState) if (extraData != null)
{ {
inventory.ClientWrite(msg, extraData); if ((NetEntityEvent.Type)extraData[0] == NetEntityEvent.Type.InventoryState)
{
msg.Write(true);
inventory.ClientWrite(msg, extraData);
}
else if ((NetEntityEvent.Type)extraData[0] == NetEntityEvent.Type.Repair)
{
msg.Write(false);
msg.Write(AnimController.Anim == AnimController.Animation.CPR);
}
msg.WritePadBits();
} }
else else
{ {
@@ -248,11 +266,6 @@ namespace Barotrauma
{ {
msg.Write(memInput[i].interact); msg.Write(memInput[i].interact);
} }
/*if (memInput[i].HasFlag(InputNetFlags.Select) || memInput[i].HasFlag(InputNetFlags.Aim))
{
msg.Write(memMousePos[i].X);
msg.Write(memMousePos[i].Y);
}*/
} }
} }
} }
@@ -278,7 +291,6 @@ namespace Barotrauma
for (int i = 0; i < inputCount; i++) for (int i = 0; i < inputCount; i++)
{ {
InputNetFlags newInput = (InputNetFlags)msg.ReadRangedInteger(0, (int)InputNetFlags.MaxVal); InputNetFlags newInput = (InputNetFlags)msg.ReadRangedInteger(0, (int)InputNetFlags.MaxVal);
//Vector2 newMousePos = Position;
UInt16 newAim = 0; UInt16 newAim = 0;
UInt16 newInteract = 0; UInt16 newInteract = 0;
@@ -288,7 +300,7 @@ namespace Barotrauma
} }
if (newInput.HasFlag(InputNetFlags.Select) || newInput.HasFlag(InputNetFlags.Use)) if (newInput.HasFlag(InputNetFlags.Select) || newInput.HasFlag(InputNetFlags.Use))
{ {
newInteract = msg.ReadUInt16(); newInteract = msg.ReadUInt16();
} }
if (AllowInput) if (AllowInput)
@@ -320,7 +332,17 @@ namespace Barotrauma
break; break;
case ClientNetObject.ENTITY_STATE: case ClientNetObject.ENTITY_STATE:
inventory.ServerRead(type, msg, c); bool isInventoryUpdate = msg.ReadBoolean();
if (isInventoryUpdate)
{
inventory.ServerRead(type, msg, c);
}
else
{
bool doingCPR = msg.ReadBoolean();
AnimController.Anim = doingCPR ? AnimController.Animation.CPR : AnimController.Animation.None;
}
msg.ReadPadBits();
break; break;
} }
} }
@@ -408,6 +430,10 @@ namespace Barotrauma
{ {
tempBuffer.Write(true); tempBuffer.Write(true);
tempBuffer.Write(selectedCharacter != null ? selectedCharacter.ID : selectedConstruction.ID); tempBuffer.Write(selectedCharacter != null ? selectedCharacter.ID : selectedConstruction.ID);
if (selectedCharacter != null)
{
tempBuffer.Write(AnimController.Anim == AnimController.Animation.CPR);
}
} }
else else
{ {
@@ -471,10 +497,20 @@ namespace Barotrauma
bool entitySelected = msg.ReadBoolean(); bool entitySelected = msg.ReadBoolean();
Entity selectedEntity = null; Entity selectedEntity = null;
AnimController.Animation animation = AnimController.Animation.None;
if (entitySelected) if (entitySelected)
{ {
ushort entityID = msg.ReadUInt16(); ushort entityID = msg.ReadUInt16();
selectedEntity = FindEntityByID(entityID); selectedEntity = FindEntityByID(entityID);
if (selectedEntity is Character)
{
bool doingCpr = msg.ReadBoolean();
if (doingCpr && selectedCharacter != null)
{
animation = AnimController.Animation.CPR;
}
}
} }
Vector2 pos = new Vector2( Vector2 pos = new Vector2(
@@ -485,7 +521,7 @@ namespace Barotrauma
int index = 0; int index = 0;
if (GameMain.NetworkMember.Character == this && AllowInput) if (GameMain.NetworkMember.Character == this && AllowInput)
{ {
var posInfo = new CharacterStateInfo(pos, networkUpdateID, facingRight ? Direction.Right : Direction.Left, selectedEntity); var posInfo = new CharacterStateInfo(pos, networkUpdateID, facingRight ? Direction.Right : Direction.Left, selectedEntity, animation);
while (index < memState.Count && NetIdUtils.IdMoreRecent(posInfo.ID, memState[index].ID)) while (index < memState.Count && NetIdUtils.IdMoreRecent(posInfo.ID, memState[index].ID))
index++; index++;
@@ -493,7 +529,7 @@ namespace Barotrauma
} }
else else
{ {
var posInfo = new CharacterStateInfo(pos, sendingTime, facingRight ? Direction.Right : Direction.Left, selectedEntity); var posInfo = new CharacterStateInfo(pos, sendingTime, facingRight ? Direction.Right : Direction.Left, selectedEntity, animation);
while (index < memState.Count && posInfo.Timestamp > memState[index].Timestamp) while (index < memState.Count && posInfo.Timestamp > memState[index].Timestamp)
index++; index++;
+1 -1
View File
@@ -148,7 +148,7 @@ namespace Barotrauma
if (GameMain.Client != null) if (GameMain.Client != null)
{ {
GameMain.Client.CreateEntityEvent(item, new object[] { NetEntityEvent.Type.RepairItem, item.FixRequirements.IndexOf(requirement)}); GameMain.Client.CreateEntityEvent(item, new object[] { NetEntityEvent.Type.Repair, item.FixRequirements.IndexOf(requirement)});
} }
else if (GameMain.Server != null) else if (GameMain.Server != null)
{ {
+2 -2
View File
@@ -1815,7 +1815,7 @@ namespace Barotrauma
case NetEntityEvent.Type.InventoryState: case NetEntityEvent.Type.InventoryState:
ownInventory.ClientWrite(msg, extraData); ownInventory.ClientWrite(msg, extraData);
break; break;
case NetEntityEvent.Type.RepairItem: case NetEntityEvent.Type.Repair:
if (FixRequirements.Count > 0) if (FixRequirements.Count > 0)
{ {
int requirementIndex = (int)extraData[1]; int requirementIndex = (int)extraData[1];
@@ -1847,7 +1847,7 @@ namespace Barotrauma
case NetEntityEvent.Type.InventoryState: case NetEntityEvent.Type.InventoryState:
ownInventory.ServerRead(type, msg, c); ownInventory.ServerRead(type, msg, c);
break; break;
case NetEntityEvent.Type.RepairItem: case NetEntityEvent.Type.Repair:
if (FixRequirements.Count == 0) return; if (FixRequirements.Count == 0) return;
int requirementIndex = FixRequirements.Count == 1 ? int requirementIndex = FixRequirements.Count == 1 ?
@@ -14,7 +14,7 @@ namespace Barotrauma.Networking
ComponentState, ComponentState,
InventoryState, InventoryState,
Status, Status,
RepairItem, Repair,
ApplyStatusEffect, ApplyStatusEffect,
ChangeProperty ChangeProperty
} }
+5
View File
@@ -52,6 +52,11 @@ namespace Barotrauma
Position += ConvertUnits.ToSimUnits(sub.Position); Position += ConvertUnits.ToSimUnits(sub.Position);
} }
} }
public void Translate(Vector2 amount)
{
Position += amount;
}
} }
class PhysicsBody class PhysicsBody