RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

[ALL] Design flaw in BaseAI.cs (TransformMoveDelay)

Lichtblitz

Sorceror
[ALL] Design flaw in BaseAI.cs (TransformMoveDelay)

It took me some time to figure out why a mobile with CurrentSpeed = 3 walks slower than a mobile with CurrentSpeed = 4.01...

This is the section in question (BaseAI.cs, all versions of RunUO)

Code:
		public double TransformMoveDelay( double delay )
		{
			bool isPassive = (delay == m_Mobile.PassiveSpeed);
			bool isControlled = (m_Mobile.Controlled || m_Mobile.Summoned);

[COLOR="Red"]			if( delay == 0.2 )
				delay = 0.3;
			else if( delay == 0.25 )
				delay = 0.45;
			else if( delay == 0.3 )
				delay = 0.6;
			else if( delay == 0.4 )
				delay = 0.9;
			else if( delay == 0.5 )
				delay = 1.05;
			else if( delay == 0.6 )
				delay = 1.2;
			else if( delay == 0.8 )
				delay = 1.5;[/COLOR]

			if( isPassive )
				delay += 0.2;

			if( !isControlled )
			{
				delay += 0.1;
			}
			else if( m_Mobile.Controlled )
			{
				if( m_Mobile.ControlOrder == OrderType.Follow && m_Mobile.ControlTarget == m_Mobile.ControlMaster )
					delay *= 0.5;

				delay -= 0.075;
			}

			double offset = (double)m_Mobile.Hits / m_Mobile.HitsMax;

			if( offset < 0.0 )
				offset = 0.0;
			else if( offset > 1.0 )
				offset = 1.0;

			offset = 1.0 - offset;

			delay += (offset * 0.8);

			if( delay < 0.0 )
				delay = 0.0;

			return delay;
		}

The problematic part is marked in red. The delay (which is actually either ActiveSpeed or PassiveSpeed by the way) is never checked whether it's one of the values that this method checks for. In the distribution files ActveSpeed and PassiveSpeed is always one of these values but as a developer it is impossible to foresee that other values produce erroneous behavior. After all it is a double value and one might suspect that you can assign any (at least positive) value to it. My suggestion would be to either change the speed values to enumerations to make sure that only these specific values can be used or (my preference because it is not intrusive) to change the part in red with the following:

Code:
			if ( delay < 0.4 )
			{
				delay = delay * 3 - 0.3;
			}
			else
			{
				delay = delay * 1.5 + 0.3;
			}

Both functions above meet at (0.4 / 0.9) and the new implementation delivers exactly the same values as the old one but has the advantage of being continuous.
 
Top