Disturbing bug in directional functions

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
hertogjan
Director
Director
Posts: 560
Joined: 03 Jan 2006 20:45
Location: Netherlands

Disturbing bug in directional functions

Post by hertogjan »

It seems to me that quite a large bug has been introduced in the directional functions. This totally breaks the whole game, since road vehicles will all be stuck on corners. It seems that the following piece of code in direction.h is the cause of it.

Code: Select all

static inline DirDiff DirDifference(Direction d0, Direction d1)
{
	return (DirDiff)((d0 + 8 - d1) % 8);
}
I think this wouldn't work. The signed difference between a pair of numbers a, b is b-a, not a-b. This is to make sure that
a + difference(a, b) = b,
which should be what you expect. Defining it the other way around results in unnatural minus signs. This should be avoided since it causes bugs.

This should be fixed as soon as possible.
Moreover, I have a little message for the dev team: Please be a little more careful when adding or changing code!
hertogjan
Director
Director
Posts: 560
Joined: 03 Jan 2006 20:45
Location: Netherlands

Post by hertogjan »

It seems that it is fixed now. At least, the roadvehicles behave correctly now. However, the directional function is still in the wrong order. Or, at least, the parameter names (d0, d1) should be swapped, or be changed to something more instructive, like d_new and d_old for d0 and d1. At least it should be clear that the function does this

Code: Select all

static inline DirDiff DirDifference(Direction d_old, Direction d_new)
{
   return (DirDiff)((d_new + 8 - d_old) % 8);
}
Again, this is to prevent confusion and mistakes in the future.
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 19 guests