Skip to content

Commit

Permalink
model: NpcId and PlayerId renamed to InstanceId. core: fixed Npc sycn…
Browse files Browse the repository at this point in the history
…ing, added InitNpcSegment, refactored player and npc syncers
  • Loading branch information
justas-d committed Nov 2, 2017
1 parent 5ee1b53 commit 4b9cb27
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 50 deletions.
4 changes: 3 additions & 1 deletion CScape.Core/Commands/TestCommandClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ public void SpawnNpc(CommandContext ctx)

var factory = ctx.Callee.Parent.Server.Services.ThrowOrGet<INpcFactory>();
var npc = factory.Create("Spanwed NPC", defId);

npc.Get().GetTransform().Teleport(ctx.Callee.Parent.GetTransform());

ctx.Callee.Parent.SystemMessage($"Spawned NPC. Intance ID: {npc.Get().AssertGetNpc().NpcId}", CoreSystemMessageFlags.Debug | CoreSystemMessageFlags.Entity);
ctx.Callee.Parent.SystemMessage($"Spawned NPC. Intance ID: {npc.Get().AssertGetNpc().InstanceId}", CoreSystemMessageFlags.Debug | CoreSystemMessageFlags.Entity);
}

[CommandMethod("clearinv")]
Expand Down
6 changes: 3 additions & 3 deletions CScape.Core/Game/Entity/Component/NpcComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class NpcComponent : EntityComponent, INpcComponent
public override int Priority => (int)ComponentPriority.Npc;

public short DefinitionId { get; private set; }
public short NpcId { get; }
public short InstanceId { get; }

public NpcComponent(
IEntity parent,
Expand All @@ -24,7 +24,7 @@ public NpcComponent(
{
_destroyCallback = destroyCallback;
DefinitionId = defId;
NpcId = npcId;
InstanceId = npcId;
}

public void ChangeDefinitionId(short newId)
Expand All @@ -46,6 +46,6 @@ public override void ReceiveMessage(IGameMessage msg)
}

public override string ToString()
=> $"Npc {NpcId} def-id {DefinitionId}";
=> $"Npc {InstanceId} def-id {DefinitionId}";
}
}
10 changes: 5 additions & 5 deletions CScape.Core/Game/Entity/Component/PlayerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum Title : byte
public const int MaxUsernameChars = 12;
public const int MaxPasswordChars = 64;

public int PlayerId { get; }
public int InstanceId { get; }

public PlayerAppearance Apperance { get; private set; }

Expand All @@ -49,7 +49,7 @@ public PlayerComponent(
PlayerAppearance appearance,
bool isMember,
int titleId,
int playerId,
int instanceId,
[CanBeNull] Action<PlayerComponent> destroyCallback)
:base(parent)
{
Expand All @@ -61,7 +61,7 @@ public PlayerComponent(
}

_destroyCallback = destroyCallback ?? throw new ArgumentNullException(nameof(destroyCallback));
PlayerId = playerId;
InstanceId = instanceId;
TitleId = titleId;
IsMember = isMember;
Username = username;
Expand All @@ -80,7 +80,7 @@ private void InitPlayer()
var net = Parent.GetNetwork();
if (net != null)
{
net.SendPacket(new InitializePlayerPacket(PlayerId, IsMember));
net.SendPacket(new InitializePlayerPacket(InstanceId, IsMember));
// TODO : delegate the retrieval of SetPlayerOptionPacket to the current Region
net.SendPacket(SetPlayerOptionPacket.Follow);
net.SendPacket(SetPlayerOptionPacket.TradeWith);
Expand Down Expand Up @@ -199,7 +199,7 @@ public override bool Equals(object obj)

public override string ToString()
{
return $"Player \"{Username}\" PID: {PlayerId})";
return $"Player \"{Username}\" PID: {InstanceId})";
}

public override int GetHashCode()
Expand Down
5 changes: 3 additions & 2 deletions CScape.Core/Game/Entity/Factory/NpcFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public IEntityHandle Create(string name, int definitionId)

ent.Components.Add(new MovementActionComponent(ent));
ent.Components.Add(new TileMovementComponent(ent));
ent.Components.Add(new FlagAccumulatorComponent(ent));

// TODO : set npc health according to it's definition when creating the npc.
var health = new HealthComponent(ent);
Expand All @@ -67,8 +68,8 @@ public IEntityHandle Create(string name, int definitionId)

private void DestroyCallback(NpcComponent npc)
{
_log.Normal(this, $"Freeing npc slot {npc.NpcId} named {npc.Parent.Name}");
InstanceLookup[npc.NpcId] = null;
_log.Normal(this, $"Freeing npc slot {npc.InstanceId} named {npc.Parent.Name}");
InstanceLookup[npc.InstanceId] = null;
}

public IEntityHandle Get(int id) => GetById(id);
Expand Down
4 changes: 2 additions & 2 deletions CScape.Core/Game/Entity/Factory/PlayerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ private void DestroyCallback([NotNull] PlayerComponent component)
{
if (component == null) throw new ArgumentNullException(nameof(component));

Log.Normal(this, $"Freeing player slot {component.PlayerId} {component.Username}");
Log.Normal(this, $"Freeing player slot {component.InstanceId} {component.Username}");

InstanceLookup[component.PlayerId] = null;
InstanceLookup[component.InstanceId] = null;
_usernameLookup.Remove(component.Username);

NumAlivePlayers--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class NpcInteractingEntity : IInteractingEntity
public NpcInteractingEntity([NotNull] INpcComponent npc)
{
Entity = npc.Parent.Handle ?? throw new ArgumentNullException(nameof(npc));
Id = npc.NpcId;
Id = npc.InstanceId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class PlayerInteractingEntity : IInteractingEntity
public PlayerInteractingEntity([NotNull] IPlayerComponent player)
{
Entity = player.Parent.Handle;
Id = (short) (player.PlayerId + 32769);
Id = (short) (player.InstanceId + 32769);
}
}
}
22 changes: 16 additions & 6 deletions CScape.Core/Network/Entity/Component/EntityNetworkSyncComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ private void RemoveEntity([NotNull] IEntityHandle ent)
protected abstract bool IsHandleableEntity(IEntityHandle h);

protected IUpdateSegment CommonSegmentResolve(
FlagAccumulatorComponent flags, bool needsUpdate)
[NotNull] FlagAccumulatorComponent flags,
bool needsUpdate)
{
if (flags == null) throw new ArgumentNullException(nameof(flags));

if (flags.Movement != null)
{
Expand All @@ -82,9 +84,12 @@ protected IUpdateSegment CommonSegmentResolve(


protected IEnumerable<IUpdateSegment> GetSyncSegments(
IList<IUpdateWriter> updateSegments,
Func<FlagAccumulatorComponent, IUpdateWriter> updateWriterFactory)
[NotNull] IList<IUpdateWriter> updateSegments,
[NotNull] Func<FlagAccumulatorComponent, IUpdateWriter> updateWriterFactory)
{
if (updateSegments == null) throw new ArgumentNullException(nameof(updateSegments));
if (updateWriterFactory == null) throw new ArgumentNullException(nameof(updateWriterFactory));

var removeList = new List<IEntityHandle>();
var syncSegments = new List<IUpdateSegment>();

Expand Down Expand Up @@ -138,9 +143,14 @@ protected IEnumerable<IUpdateSegment> GetSyncSegments(
protected abstract void SetInitialFlags(IUpdateWriter writer, IEntity ent);

protected IEnumerable<IUpdateSegment> GetInitSegments(
IList<IUpdateWriter> updateSegments,
Func<FlagAccumulatorComponent, IUpdateWriter> updateWriterFactory)
[NotNull] IList<IUpdateWriter> updateSegments,
[NotNull] Func<FlagAccumulatorComponent, IUpdateWriter> updateWriterFactory,
[NotNull] Func<(bool needsUpdate, IEntity entityToBeInitialized), IUpdateSegment> initSegmentFactory)
{
if (updateSegments == null) throw new ArgumentNullException(nameof(updateSegments));
if (updateWriterFactory == null) throw new ArgumentNullException(nameof(updateWriterFactory));
if (initSegmentFactory == null) throw new ArgumentNullException(nameof(initSegmentFactory));

var init = new List<IUpdateSegment>();

/* Initialize */
Expand All @@ -156,7 +166,7 @@ protected IEnumerable<IUpdateSegment> GetInitSegments(

var needsUpd = updater.NeedsUpdate();

init.Add(new InitPlayerSegment(entity.AssertGetPlayer(), Parent.AssertGetPlayer(), needsUpd));
init.Add(initSegmentFactory((needsUpd, entity)));

if (needsUpd)
{
Expand Down
20 changes: 18 additions & 2 deletions CScape.Core/Network/Entity/Component/NpcNetworkSyncComponent.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections.Generic;
using CScape.Core.Extensions;
using CScape.Core.Game.Entities;
using CScape.Core.Game.Entity.Component;
using CScape.Core.Network.Entity.Flag;
using CScape.Core.Network.Entity.Segment;
using CScape.Core.Network.Packet;
using CScape.Models.Extensions;
using CScape.Models.Game.Entity;
Expand Down Expand Up @@ -37,13 +37,29 @@ protected override void Sync()
var updates = new List<IUpdateWriter>();

var sync = GetSyncSegments(updates, f => new NpcUpdateWriter(f));
var init = GetInitSegments(updates, f => new NpcUpdateWriter(f));
var init = CreateNpcInitSegments(updates);

Parent.AssertGetNetwork().SendPacket(
new NpcUpdatePacket(
sync,
init,
updates));
}

private IEnumerable<IUpdateSegment> CreateNpcInitSegments(List<IUpdateWriter> updates)
{
IUpdateWriter UpdateWriterFactory(FlagAccumulatorComponent f)
{
return new NpcUpdateWriter(f);
}

IUpdateSegment InitSegmentFactory((bool needsUpdate, IEntity entityToBeInitialized) data)
{
return new InitNpcSegment(data.entityToBeInitialized.AssertGetNpc(), Parent, data.needsUpdate);
}

var init = GetInitSegments(updates, UpdateWriterFactory, InitSegmentFactory);
return init;
}
}
}
58 changes: 37 additions & 21 deletions CScape.Core/Network/Entity/Component/PlayerNetworkSyncComponent.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System.Collections.Generic;
using CScape.Core.Game.Entity.Component;
using CScape.Core.Game.Entity.Message;
using CScape.Core.Network.Entity.Flag;
using CScape.Core.Network.Entity.Segment;
using CScape.Core.Network.Packet;
using CScape.Models.Extensions;
using CScape.Models.Game.Entity;
using CScape.Models.Game.Message;

namespace CScape.Core.Network.Entity.Component
{
Expand Down Expand Up @@ -46,27 +44,10 @@ protected override void Sync()
{
var updates = new List<IUpdateWriter>();

/* Local */
IUpdateSegment local;
{
var flags = Parent.Components.AssertGet<FlagAccumulatorComponent>();
var updater = new LocalPlayerUpdateWriter(flags);
var needsUpdate = updater.NeedsUpdate();

if (flags.Reinitialize)
{
local = new LocalPlayerInitSegment(Parent.AssertGetPlayer(), needsUpdate);
}
else
local = CommonSegmentResolve(flags, needsUpdate);

if (needsUpdate)
updates.Add(updater);
}

var local = CreateUpdateSegmentForParent(updates);

var sync = GetSyncSegments(updates, f => new PlayerUpdateWriter(f));
var init = GetInitSegments(updates, f => new PlayerUpdateWriter(f));
var init = CreatePlayerInitSegments(updates);

/* Send packet */
Parent.Components.AssertGet<NetworkingComponent>().SendPacket(
Expand All @@ -77,5 +58,40 @@ protected override void Sync()
updates));
}

private IEnumerable<IUpdateSegment> CreatePlayerInitSegments(List<IUpdateWriter> updates)
{
IUpdateWriter UpdateWriterFactory(FlagAccumulatorComponent f) => new PlayerUpdateWriter(f);

IUpdateSegment InitSegmentFactory((bool needsUpdate, IEntity entityToBeInitialized) data)
=> new InitPlayerSegment(data.entityToBeInitialized.AssertGetPlayer(), Parent.AssertGetPlayer(),
data.needsUpdate);

var init = GetInitSegments(updates,
UpdateWriterFactory,
InitSegmentFactory);

return init;
}

private IUpdateSegment CreateUpdateSegmentForParent(List<IUpdateWriter> updates)
{
IUpdateSegment local;

var flags = Parent.Components.AssertGet<FlagAccumulatorComponent>();
var updater = new LocalPlayerUpdateWriter(flags);
var needsUpdate = updater.NeedsUpdate();

if (flags.Reinitialize)
{
local = new LocalPlayerInitSegment(Parent.AssertGetPlayer(), needsUpdate);
}
else
local = CommonSegmentResolve(flags, needsUpdate);

if (needsUpdate)
updates.Add(updater);

return local;
}
}
}
49 changes: 49 additions & 0 deletions CScape.Core/Network/Entity/Segment/InitNpcSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using CScape.Core.Game.Entities;
using CScape.Models.Extensions;
using CScape.Models.Game.Entity;
using JetBrains.Annotations;

namespace CScape.Core.Network.Entity.Segment
{
public sealed class InitNpcSegment : IUpdateSegment
{
private readonly bool _needsUpdate;
private readonly int _npcInstanceId;
private readonly int _yDelta;
private readonly int _xDelta;
private readonly int _definitionId;

public InitNpcSegment(
[NotNull] INpcComponent npc,
[NotNull] IEntity observerEntity,
bool needsUpdate)
{
if (npc == null) throw new ArgumentNullException(nameof(npc));
if (observerEntity == null) throw new ArgumentNullException(nameof(observerEntity));

var observerTransform = observerEntity.GetTransform();
var npcTransform = npc.Parent.GetTransform();

_yDelta = npcTransform.Y - observerTransform.Y;
_xDelta = npcTransform.X - observerTransform.X;

_npcInstanceId = npc.InstanceId;
_needsUpdate = needsUpdate;
_definitionId = npc.DefinitionId;
}

public void Write(OutBlob stream)
{
stream.WriteBits(14, _npcInstanceId);

stream.WriteBits(5, _yDelta);
stream.WriteBits(5, _xDelta);

stream.WriteBits(1, 1); // setpos?? flag // todo : setpos flag
stream.WriteBits(12, _definitionId);
stream.WriteBits(1, _needsUpdate ? 1 : 0);

}
}
}
2 changes: 1 addition & 1 deletion CScape.Core/Network/Entity/Segment/InitPlayerSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public InitPlayerSegment(
[NotNull] IPlayerComponent newPlayer, [NotNull] IPlayerComponent localPlayer,
bool needsUpdate)
{
_pid = newPlayer.PlayerId;
_pid = newPlayer.InstanceId;
_needsUpdate = needsUpdate;
_xdelta = newPlayer.Parent.GetTransform().X - localPlayer.Parent.GetTransform().X;
_ydelta = newPlayer.Parent.GetTransform().Y - localPlayer.Parent.GetTransform().Y;
Expand Down
2 changes: 1 addition & 1 deletion CScape.Models/Game/Entity/Component/INpcComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface INpcComponent : IEntityComponent
/// <summary>
/// The instance id of the npc.
/// </summary>
short NpcId { get; }
short InstanceId { get; }

/// <summary>
/// Changes the definition id of the npc.
Expand Down
2 changes: 1 addition & 1 deletion CScape.Models/Game/Entity/Component/IPlayerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface IPlayerComponent : IEquatable<IPlayerComponent>, IEquatable<str
/// <summary>
/// The instance id of the player.
/// </summary>
int PlayerId { get; }
int InstanceId { get; }

/// <summary>
/// The implementation specific id of the player's title.
Expand Down
Loading

0 comments on commit 4b9cb27

Please sign in to comment.