Concerning dwarves searching for items, and checking distance to every item, if this is done that way, it should be optimized. Distance calculations should avoid sqrt() which is expensive if they don't already.
You can easily compare distances with acceptable accuracy just by using only addition, bitshifting, and Abs.
Example:
(Abs(Origin_X-Target_X) Shl 4) + (Abs(Origin_Y-Target_Y) Shl 4) + (Abs(Origin_Z-Target_Z) Shl 12)
Such comparison would be lightning fast, and would rarely assign a dwarf something that is a greater distance away. It would also help to prevent dwarves from thinking something directly above them that may or may not have nearby access is actually closer, though I do think Toady's got that covered with the current system.
How does it work?
Abs(X1-X2) makes the number positive. This operation is extremely fast, requiring only 3 opcodes on the x86. Abs(X1-X2) will henceforth be referred to as "A".
A Shl Y Is equivalent to A*2^Y. While this is far from a perfect linear distance, it would work
beautifully for mere comparisons. This operation is a single opcode on the x86.
And, lastly, adding the three A values for X, Y, and Z (shifting the bits farther left for the Z axis) is, very obviously, trivial.
Sorry for getting so technical, but if Toady sees this post, he must realize the awesomeness of this method.
ADD IT NOW, TARN! PLEASE!!
[/size]
Yeah what you described is Manhattan distance, it is an option too. Well I don't get why you shift all distances instead of Z only which you want to multiply, hmm... Also you can simply multiply or divide them by 2, 4 etc, compilers optimize these operations to bitshifts anyway as long as it is a power of 2.
What I meant in my earlier post is that with Euclidean distance instead of using:
dist = sqrt(xdist*xdist + ydist*ydist + zdist*zdist);
if (dist < minimumDist) { //this item is closest so far, do stuff
You should use:
squareDist = xdist*xdist + ydist*ydist + zdist*zdist);
if (squareDist < minimumSquareDist) { //this item is closest so far, do stuff
You avoid sqrt which is expensive (needs many iterations to calculate) and if you would use it in a loop iterating though 100000 items it may matter a lot.
Or use Manhattan distance instead, yeah.