Looking at the calculation itself (I only glanced at it before) it is obvious the course works on the angle in radians, but even so, any fuller answer on (2) would be appreciated.

From: netrek-dev-bounces at us.netrek.org [mailto:netrek-dev-bounces at us.netrek.org] On Behalf Of Thomas E. Lackey
Sent: Tuesday, April 04, 2017 7:57 PM
To: netrek-dev at us.netrek.org
Subject: [netrek-dev] Robots, get_course, and casting floats to unsigned types.

Hello!

After setting up my new server, I noticed several of my robots camped off in a corner of the galaxy instead of fighting each other and I went on crawl through the robot code to figure out why.

According to their 'dstate' debug output, they usually--not always, but that is a different story--thought they were doing something--engaging an enemy, protecting a planet, etc--while in fact they were turning in circles in the upper-right most corner of KLI space.

While not a complete answer to robot strangeness, I think one of the culprits is something wrong with the get_course()/get_acourse() functions in robotd/util.c

get_course does this to calculate the course:
(unsigned char)(atan2((double) (x - me->p_x), (double) (me->p_y - y)) / 3.141592 * 128.);

I am fairly certain per-C99 that the result of casting a negative floating point to any unsigned type is undefined, so exactly what happens may vary by architecture and compiler.  On my ARM RaspberryPI compiling with gcc, the result is always a value of 0.

I separated the value of the calculation before and after the cast and added some logging to confirm:

Engaging: Galla get_intercept_course : x: 6735, y: 59554
get_course (UNCORRECTED): ret = -102.354382 , ret_crs = 0

I am not certain how the course system works, but guessing from the fact that it needs to fit in an unsigned char, I am thinking that 127 + the absolute value would be a good guess like this updated log output:

get_course (CORRECTED): ret = -102.354382 , ret_crs = 229

After making that change, my robots still do plenty of other strange things now and again, but at least they don't seem to be stuck in the corner of the galaxy spinning in circles.

I have two questions:

(1) Does all that sound correct?

(2) How is the unsigned char-based course system expected to work?  360 degrees carved into 255 slices?  Or Something else?

My changes are not in a state to check in, but if this sounds reasonable, I will polish them up.

Cheers,
Thomas Lackey
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.us.netrek.org/pipermail/netrek-dev/attachments/20170405/1bce656e/attachment.html>