2021-12-22 22:46:37 +00:00
|
|
|
|
using QSB.Messaging;
|
|
|
|
|
using QSB.Player;
|
|
|
|
|
using QSB.QuantumSync.WorldObjects;
|
2021-01-26 14:07:17 +00:00
|
|
|
|
using QuantumUNET.Transport;
|
|
|
|
|
|
2021-12-24 00:26:31 +00:00
|
|
|
|
namespace QSB.QuantumSync.Messages
|
2021-01-26 14:07:17 +00:00
|
|
|
|
{
|
2021-12-22 22:46:37 +00:00
|
|
|
|
public class QuantumAuthorityMessage : QSBWorldObjectMessage<IQSBQuantumObject>
|
2021-01-26 14:07:17 +00:00
|
|
|
|
{
|
2022-01-03 05:26:52 +00:00
|
|
|
|
private uint AuthorityOwner;
|
2021-01-26 14:07:17 +00:00
|
|
|
|
|
2022-01-03 05:26:52 +00:00
|
|
|
|
public QuantumAuthorityMessage(uint authorityOwner) => AuthorityOwner = authorityOwner;
|
2021-12-23 02:31:58 +00:00
|
|
|
|
|
2021-01-26 14:07:17 +00:00
|
|
|
|
public override void Serialize(QNetworkWriter writer)
|
|
|
|
|
{
|
|
|
|
|
base.Serialize(writer);
|
2022-01-03 05:26:52 +00:00
|
|
|
|
writer.Write(AuthorityOwner);
|
2021-01-26 14:07:17 +00:00
|
|
|
|
}
|
2021-12-22 22:46:37 +00:00
|
|
|
|
|
2021-12-23 02:31:58 +00:00
|
|
|
|
public override void Deserialize(QNetworkReader reader)
|
|
|
|
|
{
|
|
|
|
|
base.Deserialize(reader);
|
2022-01-03 05:26:52 +00:00
|
|
|
|
AuthorityOwner = reader.ReadUInt32();
|
2021-12-23 02:31:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-22 22:46:37 +00:00
|
|
|
|
public override bool ShouldReceive
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (!base.ShouldReceive)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:26:52 +00:00
|
|
|
|
// Deciding if to change the object's owner
|
|
|
|
|
// Message
|
|
|
|
|
// | = 0 | > 0 |
|
|
|
|
|
// = 0 | No | Yes |
|
|
|
|
|
// > 0 | Yes | No |
|
|
|
|
|
// if Obj==Message then No
|
|
|
|
|
// Obj
|
2021-12-22 22:46:37 +00:00
|
|
|
|
|
2022-01-03 05:26:52 +00:00
|
|
|
|
return (WorldObject.ControllingPlayer == 0 || AuthorityOwner == 0)
|
|
|
|
|
&& WorldObject.ControllingPlayer != AuthorityOwner;
|
2021-12-22 22:46:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:26:52 +00:00
|
|
|
|
public override void OnReceiveLocal() => WorldObject.ControllingPlayer = AuthorityOwner;
|
2021-12-22 22:46:37 +00:00
|
|
|
|
|
|
|
|
|
public override void OnReceiveRemote()
|
|
|
|
|
{
|
2022-01-03 05:26:52 +00:00
|
|
|
|
WorldObject.ControllingPlayer = AuthorityOwner;
|
|
|
|
|
if (WorldObject.ControllingPlayer == 00 && WorldObject.IsEnabled)
|
2021-12-22 22:46:37 +00:00
|
|
|
|
{
|
|
|
|
|
// object has no owner, but is still active for this player. request ownership
|
2022-01-03 05:26:52 +00:00
|
|
|
|
WorldObject.SendMessage(new QuantumAuthorityMessage(QSBPlayerManager.LocalPlayerId));
|
2021-12-22 22:46:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-26 14:07:17 +00:00
|
|
|
|
}
|
2021-12-22 22:46:37 +00:00
|
|
|
|
}
|