PowerContainer syncing, clients can't launch a turret until the server tells them to
This commit is contained in:
@@ -2,10 +2,12 @@
|
||||
using System.Xml.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Barotrauma.Networking;
|
||||
using Lidgren.Network;
|
||||
|
||||
namespace Barotrauma.Items.Components
|
||||
{
|
||||
class PowerContainer : Powered, IDrawableComponent
|
||||
class PowerContainer : Powered, IDrawableComponent, IServerSerializable, IClientSerializable
|
||||
{
|
||||
//[power/min]
|
||||
float capacity;
|
||||
@@ -62,7 +64,7 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
if (!MathUtils.IsValid(value)) return;
|
||||
rechargeSpeed = MathHelper.Clamp(value, 0.0f, maxRechargeSpeed);
|
||||
rechargeSpeed = MathUtils.Round(rechargeSpeed, Math.Max(maxRechargeSpeed * 0.1f, 1.0f));
|
||||
rechargeSpeed = MathUtils.RoundTowardsClosest(rechargeSpeed, Math.Max(maxRechargeSpeed * 0.1f, 1.0f));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,14 +89,26 @@ namespace Barotrauma.Items.Components
|
||||
var button = new GUIButton(new Rectangle(160, 50, 30,30), "-", GUI.Style, GuiFrame);
|
||||
button.OnClicked = (GUIButton btn, object obj) =>
|
||||
{
|
||||
RechargeSpeed = Math.Max(rechargeSpeed - maxRechargeSpeed * 0.1f, 0.0f);
|
||||
RechargeSpeed = rechargeSpeed - maxRechargeSpeed * 0.1f;
|
||||
|
||||
if (GameMain.Server != null)
|
||||
item.CreateServerEvent(this);
|
||||
else if (GameMain.Client != null)
|
||||
item.CreateClientEvent(this);
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
button = new GUIButton(new Rectangle(200, 50, 30, 30), "+", GUI.Style, GuiFrame);
|
||||
button.OnClicked = (GUIButton btn, object obj) =>
|
||||
{
|
||||
RechargeSpeed = Math.Max(rechargeSpeed + maxRechargeSpeed * 0.1f, 0.0f);
|
||||
RechargeSpeed = rechargeSpeed + maxRechargeSpeed * 0.1f;
|
||||
|
||||
if (GameMain.Server != null)
|
||||
item.CreateServerEvent(this);
|
||||
else if (GameMain.Client != null)
|
||||
item.CreateClientEvent(this);
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
@@ -245,31 +259,40 @@ namespace Barotrauma.Items.Components
|
||||
GuiFrame.Update(1.0f / 60.0f);
|
||||
}
|
||||
|
||||
public void ClientWrite(Lidgren.Network.NetBuffer msg, object[] extraData = null)
|
||||
public void ClientWrite(NetBuffer msg, object[] extraData)
|
||||
{
|
||||
float chargeSpeed = MathHelper.Clamp(rechargeSpeed / MaxRechargeSpeed, 0.0f, 1.0f);
|
||||
msg.WriteRangedSingle(chargeSpeed, 0.0f, 1.0f, 8);
|
||||
DebugConsole.NewMessage("writing recharge speed " + (rechargeSpeed / MaxRechargeSpeed * 10), Color.White);
|
||||
msg.WriteRangedInteger(0, 10, (int)(rechargeSpeed / MaxRechargeSpeed * 10));
|
||||
}
|
||||
|
||||
public void ServerRead(Lidgren.Network.NetIncomingMessage msg, Barotrauma.Networking.Client c)
|
||||
public void ServerRead(ClientNetObject type, NetBuffer msg, Client c)
|
||||
{
|
||||
RechargeSpeed = msg.ReadRangedSingle(0.0f, 1.0f, 8) * maxRechargeSpeed;
|
||||
float newRechargeSpeed = msg.ReadRangedInteger(0,10) / 10.0f * maxRechargeSpeed;
|
||||
|
||||
DebugConsole.NewMessage("received recharge speed " + newRechargeSpeed, Color.White);
|
||||
|
||||
if (item.CanClientAccess(c))
|
||||
{
|
||||
RechargeSpeed = newRechargeSpeed;
|
||||
}
|
||||
|
||||
item.CreateServerEvent(this);
|
||||
}
|
||||
|
||||
public void ServerWrite(Lidgren.Network.NetBuffer msg, Barotrauma.Networking.Client c, object[] extraData = null)
|
||||
public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null)
|
||||
{
|
||||
float chargeSpeed = MathHelper.Clamp(rechargeSpeed / MaxRechargeSpeed, 0.0f, 1.0f);
|
||||
msg.WriteRangedSingle(chargeSpeed, 0.0f, 1.0f, 8);
|
||||
|
||||
DebugConsole.NewMessage("writing recharge speed " + (rechargeSpeed / MaxRechargeSpeed * 10), Color.White);
|
||||
msg.WriteRangedInteger(0, 10, (int)(rechargeSpeed / MaxRechargeSpeed * 10));
|
||||
|
||||
float chargeRatio = MathHelper.Clamp(charge / capacity, 0.0f, 1.0f);
|
||||
msg.WriteRangedSingle(chargeRatio, 0.0f, 1.0f, 8);
|
||||
}
|
||||
|
||||
public void ClientRead(Lidgren.Network.NetIncomingMessage msg, float sendingTime)
|
||||
public void ClientRead(ServerNetObject type, NetBuffer msg, float sendingTime)
|
||||
{
|
||||
RechargeSpeed = msg.ReadRangedSingle(0.0f, 1.0f, 8) * maxRechargeSpeed;
|
||||
|
||||
RechargeSpeed = msg.ReadRangedInteger(0, 10) / 10.0f * maxRechargeSpeed;
|
||||
Charge = msg.ReadRangedSingle(0.0f, 1.0f, 8) * capacity;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,12 @@ using System.Xml.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using FarseerPhysics;
|
||||
using Barotrauma.Networking;
|
||||
using Lidgren.Network;
|
||||
|
||||
namespace Barotrauma.Items.Components
|
||||
{
|
||||
class Turret : Powered, IDrawableComponent
|
||||
class Turret : Powered, IDrawableComponent, IServerSerializable
|
||||
{
|
||||
Sprite barrelSprite;
|
||||
|
||||
@@ -159,12 +161,12 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
rotation = maxRotation;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override bool Use(float deltaTime, Character character = null)
|
||||
{
|
||||
if (GameMain.Client != null) return false;
|
||||
|
||||
if (reload > 0.0f) return false;
|
||||
|
||||
var projectiles = GetLoadedProjectiles(true);
|
||||
@@ -181,11 +183,21 @@ namespace Barotrauma.Items.Components
|
||||
float takePower = Math.Min(powerConsumption - availablePower, batteryPower);
|
||||
|
||||
battery.Charge -= takePower/3600.0f;
|
||||
|
||||
if (GameMain.Server != null)
|
||||
{
|
||||
battery.Item.CreateServerEvent(battery);
|
||||
}
|
||||
}
|
||||
|
||||
Launch(projectiles[0].Item);
|
||||
|
||||
reload = reloadTime;
|
||||
return true;
|
||||
}
|
||||
|
||||
Item projectile = projectiles[0].Item;
|
||||
private void Launch(Item projectile)
|
||||
{
|
||||
reload = reloadTime;
|
||||
|
||||
projectile.Drop();
|
||||
projectile.body.Dir = 1.0f;
|
||||
@@ -196,12 +208,14 @@ namespace Barotrauma.Items.Components
|
||||
projectile.FindHull();
|
||||
projectile.Submarine = projectile.body.Submarine;
|
||||
|
||||
projectiles[0].Use(deltaTime);
|
||||
projectiles[0].User = character;
|
||||
projectile.Use((float)Timing.Step);
|
||||
|
||||
if (projectile.Container != null) projectile.Container.RemoveContained(projectile);
|
||||
|
||||
return true;
|
||||
if (GameMain.Server != null)
|
||||
{
|
||||
GameMain.Server.CreateEntityEvent(item, new object[] { NetEntityEvent.Type.ComponentState, item.components.IndexOf(this), projectile });
|
||||
}
|
||||
}
|
||||
|
||||
public override bool AIOperate(float deltaTime, Character character, AIObjectiveOperateItem objective)
|
||||
@@ -249,8 +263,6 @@ namespace Barotrauma.Items.Components
|
||||
objective.AddSubObjective(new AIObjectiveOperateItem(batteryToLoad, character, ""));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//enough shells and power
|
||||
@@ -274,20 +286,10 @@ namespace Barotrauma.Items.Components
|
||||
character.CursorPosition = closestEnemy.WorldPosition;
|
||||
if (item.Submarine!=null) character.CursorPosition -= item.Submarine.Position;
|
||||
character.SetInput(InputType.Aim, false, true);
|
||||
//Vector2 receive
|
||||
|
||||
//Vector2 centerPos = new Vector2(item.WorldRect.X + barrelPos.X, item.WorldRect.Y - barrelPos.Y);
|
||||
|
||||
//Vector2 offset = receivedPos - centerPos;
|
||||
//offset.Y = -offset.Y;
|
||||
|
||||
//targetRotation = MathUtils.WrapAngleTwoPi(MathUtils.VectorToAngle(offset));
|
||||
|
||||
float enemyAngle = MathUtils.VectorToAngle(closestEnemy.WorldPosition-item.WorldPosition);
|
||||
float turretAngle = -rotation;
|
||||
|
||||
|
||||
|
||||
if (Math.Abs(MathUtils.GetShortestAngle(enemyAngle, turretAngle)) > 0.01f) return false;
|
||||
|
||||
var pickedBody = Submarine.PickBody(ConvertUnits.ToSimUnits(item.WorldPosition), closestEnemy.SimPosition, null);
|
||||
@@ -295,9 +297,7 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
if (objective.Option.ToLowerInvariant() == "fire at will") Use(deltaTime, character);
|
||||
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
private float GetAvailablePower()
|
||||
@@ -386,6 +386,26 @@ namespace Barotrauma.Items.Components
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null)
|
||||
{
|
||||
//ID of the launched projectile
|
||||
msg.Write((UInt16)extraData[2]);
|
||||
}
|
||||
|
||||
public void ClientRead(ServerNetObject type, NetBuffer msg, float sendingTime)
|
||||
{
|
||||
UInt16 projectileID = msg.ReadUInt16();
|
||||
Item projectile = Entity.FindEntityByID(projectileID) as Item;
|
||||
|
||||
if (projectile == null)
|
||||
{
|
||||
DebugConsole.ThrowError("Failed to launch a projectile - item with the ID \""+projectileID+" not found");
|
||||
return;
|
||||
}
|
||||
|
||||
Launch(projectile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user