Skip to content

Commit

Permalink
model: PoE get region by region and global coord methods. core: fixed…
Browse files Browse the repository at this point in the history
… Region.GetNearbyRegions returning invalid regions
  • Loading branch information
justas-d committed Nov 2, 2017
1 parent 555a8ba commit fbe8b4e
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 33 deletions.
1 change: 1 addition & 0 deletions CScape.Core/Commands/TestCommandClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ public void GetPos(CommandContext ctx)
var ent = ctx.Callee.Parent;

ent.SystemMessage($"X: {t.X} Y: {t.Y} Z: {t.Z}", CoreSystemMessageFlags.Debug | CoreSystemMessageFlags.Entity);
ent.SystemMessage($"REGX: {t.Region.X} REGY: {t.Region.Y}", CoreSystemMessageFlags.Debug | CoreSystemMessageFlags.Entity);
ent.SystemMessage($"LX: {client.Local.X} LY: {client.Local.Y}", CoreSystemMessageFlags.Debug | CoreSystemMessageFlags.Entity);
ent.SystemMessage($"CRX: {client.ClientRegion.Y} + 6 CRY: {client.ClientRegion.Y} + 6", CoreSystemMessageFlags.Debug | CoreSystemMessageFlags.Entity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ private void CatchNearbyEntityDying(EntityMessage msg)
TryDeleteEntityFromSeeables(msg.Entity);
}

// TODO : lightweight version of the VisionComponent whose only purpose is to send out NearbyEntityQueuedForDeath upon receiving QueuedForDeath WITHOUT relying on VisionComponent
public override void ReceiveMessage(IGameMessage msg)
{
switch (msg.EventId)
Expand Down
2 changes: 1 addition & 1 deletion CScape.Core/Game/Entity/Component/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void SetInteractingEntity([NotNull] IInteractingEntity ent)

private void UpdateRegion()
{
var region = PoE.GetRegion(X, Y) ?? throw new ArgumentNullException("PoE.GetRegion(X, Y)");
var region = PoE.GetRegionByWorldCoordinate(X, Y) ?? throw new ArgumentNullException(nameof(PoE.GetRegionByWorldCoordinate));

if (Region == region) return;

Expand Down
10 changes: 5 additions & 5 deletions CScape.Core/Game/Entity/Message/SystemMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public enum CoreSystemMessageFlags : ulong
None = SystemMessageFlags.None,
Normal = SystemMessageFlags.Normal,
Debug = SystemMessageFlags.Debug,
Skill,
Item,
Network,
Interface,
Entity
Skill = 2,
Item = 4,
Network = 8,
Interface = 16,
Entity = 32
}

public sealed class SystemMessage : IGameMessage
Expand Down
4 changes: 3 additions & 1 deletion CScape.Core/Game/World/PlaneOfExistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ public PlaneOfExistence([NotNull] IGameServer server, [NotNull] string name)
/// Gets the region coords by global position.
/// </summary>
[NotNull]
public IRegion GetRegion(int x, int y)
public IRegion GetRegionByWorldCoordinate(int x, int y)
{
var key = (x >> Region.Shift, y >> Region.Shift);
return GetRegion(key);
}

public IRegion GetRegionByRegionCoordinate(int x, int y) => GetRegion((x, y));

public void GC()
{
_entities.RemoveWhere(e => e.IsDead());
Expand Down
16 changes: 8 additions & 8 deletions CScape.Core/Game/World/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ public IEnumerable<IRegion> GetNearbyInclusive()
{
return _nearbyRegions ?? (_nearbyRegions = new[]
{
Poe.GetRegion(X + 1, Y),
Poe.GetRegion(X + 1, Y + 1),
Poe.GetRegion(X + 1, Y - 1),
Poe.GetRegionByRegionCoordinate(X + 1, Y),
Poe.GetRegionByRegionCoordinate(X + 1, Y + 1),
Poe.GetRegionByRegionCoordinate(X + 1, Y - 1),

Poe.GetRegion(X - 1, Y),
Poe.GetRegion(X - 1, Y + 1),
Poe.GetRegion(X - 1, Y - 1),
Poe.GetRegionByRegionCoordinate(X - 1, Y),
Poe.GetRegionByRegionCoordinate(X - 1, Y + 1),
Poe.GetRegionByRegionCoordinate(X - 1, Y - 1),

this,
Poe.GetRegion(X, Y + 1),
Poe.GetRegion(X, Y - 1)
Poe.GetRegionByRegionCoordinate(X, Y + 1),
Poe.GetRegionByRegionCoordinate(X, Y - 1)
});
}
}
Expand Down
39 changes: 28 additions & 11 deletions CScape.Core/Network/Entity/Component/MessageNetworkSyncComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,41 @@ public MessageNetworkSyncComponent(IEntity parent)
#endif
}

public override void ReceiveMessage(IGameMessage msg)
private static bool IsBitSet(ulong flags, CoreSystemMessageFlags bit)
{
return (flags & ((ulong)bit)) != 0;
}

private bool CanSync()
{
// don't sync messages if the user has got a chat interface open.
var interf = Parent.GetInterfaces();
return interf?.Chat == null;
}

private bool IsMessageIgnored(ulong msgFlags)
{

if (IsBitSet(msgFlags, CoreSystemMessageFlags.Network))
return true;

if (IsBitSet(msgFlags, CoreSystemMessageFlags.Debug))
return !SyncDebugMessages;

return false;
}

public override void ReceiveMessage(IGameMessage msg)
{
if (msg.EventId == (int)MessageId.NewSystemMessage)
{
// don't sync messages if the user has got a chat interface open.
var interf = Parent.GetInterfaces();
if (interf?.Chat != null)
if (!CanSync())
return;

var msgStr = msg.AsSystemMessage();
var isDebugBitSet = (msgStr.Flags & (ulong)CoreSystemMessageFlags.Debug) != 0;

if (!isDebugBitSet || SyncDebugMessages)
Network.SendPacket(new SystemChatMessagePacket(msgStr.Msg));
var sysMsgData = msg.AsSystemMessage();

if(!IsMessageIgnored(sysMsgData.Flags))
Network.SendPacket(new SystemChatMessagePacket(sysMsgData.Msg));
}
}

}
}
4 changes: 1 addition & 3 deletions CScape.Core/Network/Handler/PickupGroundItemPacketHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public void Handle(IEntity entity, PacketMessage packet)
var id = packet.Data.ReadInt16() + 1;
var x = packet.Data.ReadInt16();



var region = entity.GetTransform().PoE.GetRegion(x, y);
var region = entity.GetTransform().PoE.GetRegionByWorldCoordinate(x, y);

// select first item in the region's item list
// where the pos of the item matches the packet data
Expand Down
11 changes: 8 additions & 3 deletions CScape.Models/Game/World/IPlaneOfExistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ public interface IPlaneOfExistence : IEnumerable<IEntityHandle>, IEquatable<IPla
void RegisterNewEntity([NotNull] ITransform transform);

/// <summary>
/// Returns a region at the given coordinates. If it doesn't exist, a new one is created.
/// If x or y are negative, this method is undefined.
/// Returns the region to which the tile at the given position belongs to.
/// </summary>
[CanBeNull]
IRegion GetRegion(int x, int y);
IRegion GetRegionByWorldCoordinate(int x, int y);

/// <summary>
/// Returns the region at the given region coordinates.
/// </summary>
[CanBeNull]
IRegion GetRegionByRegionCoordinate(int x, int y);
}
}

0 comments on commit fbe8b4e

Please sign in to comment.