Clean up: Bugs, LINQ, constructors, default values, and null checks (#18)

This commit is contained in:
Kamron Batman 2019-03-11 14:29:59 -07:00 • committed by GitHub
parent e748af4430
commit 513e54f70e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
1094 changed files with 15913 additions and 21892 deletions

View file

@ -124,7 +124,6 @@ namespace Server.PathAlgorithms.FastAStar
BaseCreature bc = m as BaseCreature;
int pathCount, parent;
int backtrack = 0, depth = 0;
Direction[] path = m_Path;
@ -182,8 +181,8 @@ namespace Server.PathAlgorithms.FastAStar
if (newNode != destNode)
continue;
pathCount = 0;
parent = m_Nodes[newNode].parent;
int pathCount = 0;
int parent = m_Nodes[newNode].parent;
while (parent != -1)
{

View file

@ -132,10 +132,9 @@ namespace Server.Movement
public bool CheckMovement(Mobile m, Direction d, out int newZ)
{
if (!Enabled && _Successor != null)
return _Successor.CheckMovement(m, d, out newZ);
return CheckMovement(m, m.Map, m.Location, d, out newZ);
return !Enabled && _Successor != null
? _Successor.CheckMovement(m, d, out newZ)
: CheckMovement(m, m.Map, m.Location, d, out newZ);
}
public static void Initialize()
@ -235,7 +234,7 @@ namespace Server.Movement
}
// Stygian Dragon
if (m.Body == 826 && map != null && map.MapID == 5)
if (m.Body == 826 && map?.MapID == 5)
{
if (x >= 307 && x <= 354 && y >= 126 && y <= 192)
{
@ -384,20 +383,13 @@ namespace Server.Movement
private static bool Verify(Item item, int x, int y)
{
return item != null && item.AtWorldPoint(x, y);
return item.AtWorldPoint(x, y);
}
private static bool Verify(Item item, TileFlag reqFlags, bool ignoreMovableImpassables)
{
if (item == null) return false;
if (ignoreMovableImpassables && item.Movable && item.ItemData.Impassable) return false;
if ((item.ItemData.Flags & reqFlags) == 0) return false;
if (item is BaseMulti || item.ItemID > TileData.MaxItemValue) return false;
return true;
return item != null && (!ignoreMovableImpassables || !item.Movable || !item.ItemData.Impassable) &&
(item.ItemData.Flags & reqFlags) != 0 && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue;
}
private static bool Verify(Item item, TileFlag reqFlags, bool ignoreMovableImpassables, int x, int y)
@ -460,11 +452,9 @@ namespace Server.Movement
isSet = true;
}
ItemData itemData;
foreach (Item item in itemList)
{
itemData = item.ItemData;
ItemData itemData = item.ItemData;
int calcTop = item.Z + itemData.CalcHeight;
@ -489,7 +479,8 @@ namespace Server.Movement
if (!isSet)
zLow = zTop = loc.Z;
else if (loc.Z > zTop) zTop = loc.Z;
else if (loc.Z > zTop)
zTop = loc.Z;
}
public void Offset(Direction d, ref int x, ref int y)
@ -560,4 +551,4 @@ namespace Server.Movement
}
}
}
}
}

View file

@ -77,7 +77,9 @@ namespace Server.Movement
List<Mobile> mobsLeft = m_MobPools[1];
List<Mobile> mobsRight = m_MobPools[2];
bool checkMobs = m is BaseCreature creature && !creature.Controlled &&
BaseCreature bc = m as BaseCreature;
bool checkMobs = bc?.Controlled == false &&
(xForward != Goal.X || yForward != Goal.Y);
if (checkDiagonals)
@ -114,18 +116,21 @@ namespace Server.Movement
if ((item.ItemData.Flags & reqFlags) == 0)
continue;
if (sector == sectorStart && item.AtWorldPoint(xStart, yStart) && !(item is BaseMulti) &&
item.ItemID <= TileData.MaxItemValue)
itemsStart.Add(item);
else if (sector == sectorForward && item.AtWorldPoint(xForward, yForward) && !(item is BaseMulti) &&
item.ItemID <= TileData.MaxItemValue)
itemsForward.Add(item);
else if (sector == sectorLeft && item.AtWorldPoint(xLeft, yLeft) && !(item is BaseMulti) &&
item.ItemID <= TileData.MaxItemValue)
itemsLeft.Add(item);
else if (sector == sectorRight && item.AtWorldPoint(xRight, yRight) && !(item is BaseMulti) &&
item.ItemID <= TileData.MaxItemValue)
itemsRight.Add(item);
if (!(item is BaseMulti))
{
if (sector == sectorStart && item.AtWorldPoint(xStart, yStart) &&
item.ItemID <= TileData.MaxItemValue)
itemsStart.Add(item);
else if (sector == sectorForward && item.AtWorldPoint(xForward, yForward) &&
item.ItemID <= TileData.MaxItemValue)
itemsForward.Add(item);
else if (sector == sectorLeft && item.AtWorldPoint(xLeft, yLeft) &&
item.ItemID <= TileData.MaxItemValue)
itemsLeft.Add(item);
else if (sector == sectorRight && item.AtWorldPoint(xRight, yRight) &&
item.ItemID <= TileData.MaxItemValue)
itemsRight.Add(item);
}
}
if (checkMobs)
@ -162,12 +167,15 @@ namespace Server.Movement
if ((item.ItemData.Flags & reqFlags) == 0)
continue;
if (item.AtWorldPoint(xStart, yStart) && !(item is BaseMulti) &&
item.ItemID <= TileData.MaxItemValue)
itemsStart.Add(item);
else if (item.AtWorldPoint(xForward, yForward) && !(item is BaseMulti) &&
item.ItemID <= TileData.MaxItemValue)
itemsForward.Add(item);
if (!(item is BaseMulti))
{
if (item.AtWorldPoint(xStart, yStart) &&
item.ItemID <= TileData.MaxItemValue)
itemsStart.Add(item);
else if (item.AtWorldPoint(xForward, yForward) &&
item.ItemID <= TileData.MaxItemValue)
itemsForward.Add(item);
}
}
}
else
@ -215,25 +223,31 @@ namespace Server.Movement
GetStartZ(m, map, loc, itemsStart, out int startZ, out int startTop);
bc?.DebugSay("My Z/Top is {0} {1}", startZ, startTop);
bool moveIsOk = Check(map, m, itemsForward, mobsForward, xForward, yForward, startTop, startZ, m.CanSwim,
m.CantWalk, out newZ);
bc?.DebugSay("Can I move? {0} {1}", moveIsOk, newZ);
if (moveIsOk && checkDiagonals)
{
if (m.Player && m.AccessLevel < AccessLevel.GameMaster)
{
if (!Check(map, m, itemsLeft, mobsLeft, xLeft, yLeft, startTop, startZ, m.CanSwim, m.CantWalk,
out _) || !Check(map, m, itemsRight, mobsRight, xRight, yRight, startTop, startZ, m.CanSwim,
m.CantWalk, out _))
if (!(Check(map, m, itemsLeft, mobsLeft, xLeft, yLeft, startTop, startZ, m.CanSwim, m.CantWalk,
out _) && Check(map, m, itemsRight, mobsRight, xRight, yRight, startTop, startZ, m.CanSwim,
m.CantWalk, out _)))
moveIsOk = false;
}
else
{
if (!Check(map, m, itemsLeft, mobsLeft, xLeft, yLeft, startTop, startZ, m.CanSwim, m.CantWalk,
out _) && !Check(map, m, itemsRight, mobsRight, xRight, yRight, startTop, startZ, m.CanSwim,
m.CantWalk, out _))
if (!(Check(map, m, itemsLeft, mobsLeft, xLeft, yLeft, startTop, startZ, m.CanSwim, m.CantWalk,
out _) || Check(map, m, itemsRight, mobsRight, xRight, yRight, startTop, startZ, m.CanSwim,
m.CantWalk, out _)))
moveIsOk = false;
}
bc?.DebugSay("Can I still move? {0} {1}", moveIsOk, newZ);
}
for (int i = 0; i < (checkDiagonals ? 4 : 2); ++i)
@ -336,56 +350,55 @@ namespace Server.Movement
bool ignoreSpellFields = m is PlayerMobile && map != Map.Felucca;
#region Tiles
for (int i = 0; i < tiles.Length; ++i)
{
StaticTile tile = tiles[i];
ItemData itemData = TileData.ItemTable[tile.ID & TileData.MaxItemValue];
TileFlag flags = itemData.Flags;
if ((flags & ImpassableSurface) == TileFlag.Surface || canSwim && (flags & TileFlag.Wet) != 0
) // Surface && !Impassable
// Surface && !Impassable
if ((flags & ImpassableSurface) != TileFlag.Surface && (!canSwim || (flags & TileFlag.Wet) == 0))
continue;
if (cantWalk && (flags & TileFlag.Wet) == 0)
continue;
int itemZ = tile.Z;
int itemTop = itemZ;
int ourZ = itemZ + itemData.CalcHeight;
int testTop = checkTop;
if (moveIsOk)
{
if (cantWalk && (flags & TileFlag.Wet) == 0)
int cmp = Math.Abs(ourZ - m.Z) - Math.Abs(newZ - m.Z);
if (cmp > 0 || cmp == 0 && ourZ > newZ)
continue;
}
int itemZ = tile.Z;
int itemTop = itemZ;
int ourZ = itemZ + itemData.CalcHeight;
int testTop = checkTop;
if (ourZ + PersonHeight > testTop)
testTop = ourZ + PersonHeight;
if (moveIsOk)
{
int cmp = Math.Abs(ourZ - m.Z) - Math.Abs(newZ - m.Z);
if (!itemData.Bridge)
itemTop += itemData.Height;
if (cmp > 0 || cmp == 0 && ourZ > newZ)
continue;
}
if (stepTop < itemTop)
continue;
if (ourZ + PersonHeight > testTop)
testTop = ourZ + PersonHeight;
int landCheck = itemZ;
if (!itemData.Bridge)
itemTop += itemData.Height;
if (itemData.Height >= StepHeight)
landCheck += StepHeight;
else
landCheck += itemData.Height;
if (stepTop >= itemTop)
{
int landCheck = itemZ;
if (considerLand && landCheck < landCenter && landCenter > ourZ && testTop > landZ)
continue;
if (itemData.Height >= StepHeight)
landCheck += StepHeight;
else
landCheck += itemData.Height;
if (considerLand && landCheck < landCenter && landCenter > ourZ && testTop > landZ)
continue;
if (IsOk(ignoreDoors, ignoreSpellFields, ourZ, testTop, tiles, items))
{
newZ = ourZ;
moveIsOk = true;
}
}
if (IsOk(ignoreDoors, ignoreSpellFields, ourZ, testTop, tiles, items))
{
newZ = ourZ;
moveIsOk = true;
}
}
@ -474,15 +487,13 @@ namespace Server.Movement
#region Mobiles
if (moveIsOk)
for (int i = 0; moveIsOk && i < mobiles.Count; ++i)
{
Mobile mob = mobiles[i];
if (mob != m && mob.Z + 15 > newZ && newZ + 15 > mob.Z && !CanMoveOver(m, mob))
moveIsOk = false;
}
for (int i = 0; moveIsOk && i < mobiles.Count; ++i)
{
Mobile mob = mobiles[i];
if (mob != m && mob.Z + 15 > newZ && newZ + 15 > mob.Z && !CanMoveOver(m, mob))
moveIsOk = false;
}
#endregion
return moveIsOk;
@ -543,11 +554,7 @@ namespace Server.Movement
zLow = tile.Z;
zCenter = calcTop;
int top = tile.Z + id.Height;
if (!isSet || top > zTop)
zTop = top;
zTop = tile.Z + id.Height;
isSet = true;
}
@ -620,4 +627,4 @@ namespace Server.Movement
}
}
}
}
}

View file

@ -28,11 +28,9 @@ namespace Server
try
{
PathAlgorithm alg = OverrideAlgorithm;
PathAlgorithm alg = OverrideAlgorithm ?? FastAStarAlgorithm.Instance;
if (alg == null) alg = FastAStarAlgorithm.Instance;
if (alg != null && alg.CheckCondition(m, map, start, goal))
if (alg?.CheckCondition(m, map, start, goal) == true)
Directions = alg.Find(m, map, start, goal);
}
catch (Exception e)
@ -49,7 +47,7 @@ namespace Server
public Direction[] Directions{ get; }
public bool Success => Directions != null && Directions.Length > 0;
public bool Success => Directions?.Length > 0;
public static PathAlgorithm OverrideAlgorithm{ get; set; }
@ -106,4 +104,4 @@ namespace Server
OverrideAlgorithm = null;
}
}
}
}

View file

@ -44,7 +44,7 @@ namespace Server
public void Advance(ref Point3D p, int index)
{
if (m_Path != null && m_Path.Success)
if (m_Path?.Success == true)
{
Direction[] dirs = m_Path.Directions;
@ -70,18 +70,10 @@ namespace Server
if (!Enabled)
return false;
bool repath = false;
Point3D goal = GetGoalLocation();
if (m_Path == null)
repath = true;
else if ((!m_Path.Success || goal != m_LastGoalLoc) && m_LastPathTime + RepathDelay <= DateTime.UtcNow)
repath = true;
else if (m_Path.Success && Check(m_From.Location, m_LastGoalLoc, 0))
repath = true;
if (!repath)
if (m_Path != null && (m_Path.Success && goal == m_LastGoalLoc || m_LastPathTime + RepathDelay > DateTime.Now) &&
!(m_Path.Success && Check(m_From.Location, m_LastGoalLoc, 0)))
return false;
m_LastPathTime = DateTime.UtcNow;
@ -95,17 +87,12 @@ namespace Server
Advance(ref m_Next, m_Index);
return true;
}
public bool Check(Point3D loc, Point3D goal, int range)
{
if (!Utility.InRange(loc, goal, range))
return false;
if (range <= 1 && Math.Abs(loc.Z - goal.Z) >= 16)
return false;
return true;
return Utility.InRange(loc, goal, range) && (range > 1 || Math.Abs(loc.Z - goal.Z) < 16);
}
public bool Follow(bool run, int range)
@ -118,7 +105,7 @@ namespace Server
bool repathed = CheckPath();
if (!Enabled || !m_Path.Success)
if (!(Enabled && m_Path.Success))
{
d = m_From.GetDirectionTo(goal);
@ -190,4 +177,4 @@ namespace Server
return Check(m_From.Location, goal, range);
}
}
}
}