From d72ccee22f51ac6fd3d1e618770ae0b48aabdb21 Mon Sep 17 00:00:00 2001 From: Tim Young Date: Wed, 7 Mar 2018 13:51:29 -0600 Subject: [PATCH] Increase chance of damage when traversing long links. --- EduNetworkBuilder/Network.cs | 4 +++ EduNetworkBuilder/NetworkLink.cs | 57 +++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/EduNetworkBuilder/Network.cs b/EduNetworkBuilder/Network.cs index c5798e6..3dda6da 100644 --- a/EduNetworkBuilder/Network.cs +++ b/EduNetworkBuilder/Network.cs @@ -1830,6 +1830,10 @@ namespace EduNetworkBuilder { return Math.Sqrt(Math.Pow((start.X - dest.X),2) + Math.Pow((start.Y - dest.Y),2)) / 5; //use grid size... } + public double pixeldistance(Point start, Point dest) + { + return Math.Sqrt(Math.Pow((start.X - dest.X), 2) + Math.Pow((start.Y - dest.Y), 2)); //pixel size, not grid size + } public double distance(NetworkDevice start, NetworkDevice dest) { diff --git a/EduNetworkBuilder/NetworkLink.cs b/EduNetworkBuilder/NetworkLink.cs index 131fda8..f0b1bbf 100644 --- a/EduNetworkBuilder/NetworkLink.cs +++ b/EduNetworkBuilder/NetworkLink.cs @@ -366,6 +366,24 @@ namespace EduNetworkBuilder } + public void TakeDamageIfNeeded(Point location, Packet tPacket) + { + Network myNet = NB.GetNetwork(); + + if (myNet.DeviceIsOverDamaging(theLinkType, location)) + { + tPacket.health -= 10; + //Console.WriteLine(" health=" + tPacket.health.ToString()); + } + if (theLinkType == LinkType.wireless) + { + if (myNet.DeviceInTree(theLinkType, location)) + { + tPacket.health -= 60; + } + } + } + /// /// Have the packet traverse the network /// @@ -375,22 +393,45 @@ namespace EduNetworkBuilder NetworkDevice movingTo = null; Network myNet = NB.GetNetwork(); HostNicID target; + int oldpercent = tPacket.myLinkPercent; + Point oldLocation = PositionOnLine(tPacket.myDirection, tPacket.myLinkPercent); tPacket.IncrementDistance(); Point newLocation = PositionOnLine(tPacket.myDirection, tPacket.myLinkPercent); - if (myNet.DeviceIsOverDamaging(theLinkType, newLocation)) + List PointsToCheck = new List(); + PointsToCheck.Add(newLocation); + + double distance = myNet.pixeldistance(oldLocation, newLocation); + if(distance > (double)myNet.itemsize / 2) { - tPacket.health -= 10; - //Console.WriteLine(" health=" + tPacket.health.ToString()); + //We are traveling too fast to accurately take damage. take some snapshot point along the way + int count = (int)(distance / ((double)myNet.itemsize / 2)); + int newpercent = tPacket.myLinkPercent; + int delta = Math.Abs(newpercent - oldpercent) / (count +1); + if (delta < 1) count = 1; + if (delta > 5) count = 5; + int start = newpercent + delta; + int end = oldpercent; + + if(oldpercent - newpercent < 0) + { + start = oldpercent + delta; + end = newpercent; + } + for(int percent = start; percent < end; percent+= delta ) + { + Point next = PositionOnLine(tPacket.myDirection, percent); + PointsToCheck.Add(next); + } } + + foreach(Point one in PointsToCheck) + TakeDamageIfNeeded(one, tPacket); + if(theLinkType == LinkType.wireless) { - if(myNet.DeviceInTree(theLinkType, newLocation)) - { - tPacket.health -= 60; - } NetworkDevice sDev = myNet.GetDeviceFromID(SrcNic); NetworkDevice dDev = myNet.GetDeviceFromID(DstNic); - double distance = myNet.distance(sDev, dDev) * (tPacket.myLinkPercent * 0.01); + distance = myNet.distance(sDev, dDev) * (tPacket.myLinkPercent * 0.01); if(distance > NB.WirelessMaxSuccessfulLink) { //The wireless link is too far. Drop the packet