Skip to content

Commit

Permalink
fix: Removes GetObjectsInRange and fixes boat planks closing (#1579)
Browse files Browse the repository at this point in the history
## BREAKING CHANGE

- Deletes `map.GetObjectsInRange` and `map.GetObejctsInBounds`

### Notes

Developers are expected to enumerate mobiles and items separately now using `map.GetMobilesInRange` and `map.GetItemsInRange`. This helps keep the code streamlined so we don't have to maintain multiple copies of ref struct enumerators that do the same thing.


### Fixes

- [X] Fixes bug with planks closing
- [X] Fixes issue with iterating items/mobiles from a null map
  • Loading branch information
kamronbatman authored Nov 3, 2023
1 parent cb16385 commit 977fdc2
Show file tree
Hide file tree
Showing 21 changed files with 515 additions and 433 deletions.
9 changes: 0 additions & 9 deletions Projects/Server/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2445,15 +2445,6 @@ private static void SetSaveFlag(ref SaveFlag flags, SaveFlag toSet, bool setIf)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool GetSaveFlag(SaveFlag flags, SaveFlag toGet) => (flags & toGet) != 0;

public IPooledEnumerable<IEntity> GetObjectsInRange(int range)
{
var map = m_Map;

return map == null
? PooledEnumeration.NullEnumerable<IEntity>.Instance
: map.GetObjectsInRange(m_Parent == null ? m_Location : GetWorldLocation(), range);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Map.ItemAtEnumerable<Item> GetItemsAt() =>
m_Map == null ? Map.ItemAtEnumerable<Item>.Empty : m_Map.GetItemsAt(m_Parent == null ? m_Location : GetWorldLocation());
Expand Down
13 changes: 12 additions & 1 deletion Projects/Server/Maps/Map.ItemEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace Server;

public partial class Map
{
private static ValueLinkList<Item> _emptyItemLinkList = new();
public static ref readonly ValueLinkList<Item> EmptyItemLinkList => ref _emptyItemLinkList;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ItemAtEnumerable<Item> GetItemsAt(Point3D p) => GetItemsAt<Item>(p);

Expand Down Expand Up @@ -110,7 +113,15 @@ public ItemAtEnumerator(Map map, Point2D loc)
{
_started = false;
_location = loc;
_linkList = ref map.GetRealSector(loc.m_X, loc.m_Y).Items;
if (map == null)
{
_linkList = ref EmptyItemLinkList;
}
else
{
_linkList = ref map.GetRealSector(loc.m_X, loc.m_Y).Items;
}

_version = 0;
_current = null;
}
Expand Down
12 changes: 11 additions & 1 deletion Projects/Server/Maps/Map.MobileEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace Server;

public partial class Map
{
private static ValueLinkList<Mobile> _emptyMobileLinkList = new();
public static ref readonly ValueLinkList<Mobile> EmptyMobileLinkList => ref _emptyMobileLinkList;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MobileAtEnumerable<Mobile> GetMobilesAt(Point3D p) => GetMobilesAt<Mobile>(p);

Expand Down Expand Up @@ -110,7 +113,14 @@ public MobileAtEnumerator(Map map, Point2D loc)
{
_started = false;
_location = loc;
_linkList = ref map.GetRealSector(loc.m_X, loc.m_Y).Mobiles;
if (map == null)
{
_linkList = ref EmptyMobileLinkList;
}
else
{
_linkList = ref map.GetRealSector(loc.m_X, loc.m_Y).Mobiles;
}
_version = 0;
_current = null;
}
Expand Down
14 changes: 2 additions & 12 deletions Projects/Server/Maps/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -865,25 +865,15 @@ public Point3D GetPoint(object o, bool eye)
public IPooledEnumerable<StaticTile[]> GetMultiTilesAt(int x, int y) =>
PooledEnumeration.GetMultiTiles(this, new Rectangle2D(x, y, 1, 1));

public IPooledEnumerable<IEntity> GetObjectsInRange(Point3D p) => GetObjectsInRange(p, Core.GlobalMaxUpdateRange);

public IPooledEnumerable<IEntity> GetObjectsInRange(Point3D p, int range) =>
GetObjectsInBounds(new Rectangle2D(p.m_X - range, p.m_Y - range, range * 2 + 1, range * 2 + 1));

public IPooledEnumerable<IEntity> GetObjectsInBounds(Rectangle2D bounds) =>
PooledEnumeration.GetEntities(this, bounds);

public bool CanFit(
Point3D p, int height, bool checkBlocksFit = false, bool checkMobiles = true,
bool requireSurface = true
) =>
CanFit(p.m_X, p.m_Y, p.m_Z, height, checkBlocksFit, checkMobiles, requireSurface);
) => CanFit(p.m_X, p.m_Y, p.m_Z, height, checkBlocksFit, checkMobiles, requireSurface);

public bool CanFit(
Point2D p, int z, int height, bool checkBlocksFit = false, bool checkMobiles = true,
bool requireSurface = true
) =>
CanFit(p.m_X, p.m_Y, z, height, checkBlocksFit, checkMobiles, requireSurface);
) => CanFit(p.m_X, p.m_Y, z, height, checkBlocksFit, checkMobiles, requireSurface);

public bool CanFit(
int x, int y, int z, int height, bool checkBlocksFit = false, bool checkMobiles = true,
Expand Down
23 changes: 0 additions & 23 deletions Projects/Server/Maps/PooledEnumeration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Server.Collections;
using Server.Items;

namespace Server;
Expand All @@ -32,32 +31,13 @@ public static class PooledEnumeration

static PooledEnumeration()
{
EntitySelector = SelectEntities;
MultiSelector = SelectMultis;
MultiTileSelector = SelectMultiTiles;
}

public static Selector<IEntity> EntitySelector { get; set; }
public static Selector<Mobile> MobileSelector { get; set; }
public static Selector<BaseMulti> MultiSelector { get; set; }
public static Selector<StaticTile[]> MultiTileSelector { get; set; }

public static IEnumerable<IEntity> SelectEntities(Map.Sector s, Rectangle2D bounds)
{
var entities = new List<IEntity>(s.Mobiles.Count + s.Items.Count);
foreach (var mob in s.Mobiles)
{
entities.Add(mob);
}

foreach (var item in s.Items)
{
entities.Add(item);
}

return entities;
}

public static IEnumerable<BaseMulti> SelectMultis(Map.Sector s, Rectangle2D bounds)
{
var entities = new List<BaseMulti>(s.Multis.Count);
Expand Down Expand Up @@ -126,9 +106,6 @@ public static IEnumerable<StaticTile[]> SelectMultiTiles(Map.Sector s, Rectangle
}
}

public static PooledEnumerable<IEntity> GetEntities(Map map, Rectangle2D bounds) =>
PooledEnumerable<IEntity>.Instantiate(map, bounds, EntitySelector ?? SelectEntities);

public static PooledEnumerable<BaseMulti> GetMultis(Map map, Rectangle2D bounds) =>
PooledEnumerable<BaseMulti>.Instantiate(map, bounds, MultiSelector ?? SelectMultis);

Expand Down
Loading

0 comments on commit 977fdc2

Please sign in to comment.