Skip to content

Conversation

@BlvckBytes
Copy link
Contributor

As I've already stated in the CraftBook Discord channel, the Gate mechanic often selects blocks which completely go against the intuition of the user, simply due to its search-algorithm, which doesn't consider distance at all. Expanding radially outwards from the point of the sign to me seems like the only right way of doing it.

Copy link
Member

@me4502 me4502 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

Have you done any testing to assess how this performs to the old method? A simple loop was chosen here for speed, as some servers have tens of thousands of gates controlled by redstone/etc. My assumption would be that this will have substantially worse cache locality / predictions, but will likely find the gate block earlier than the previous one for most cases.

import org.enginehub.craftbook.util.EventUtil;
import org.enginehub.craftbook.util.ProtectionUtil;
import org.enginehub.craftbook.util.SignUtil;
import org.enginehub.craftbook.util.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to please make sure that import style is retained & follows our style guide?

enumerationLoop: while (!enumerationQueue.isEmpty()) {
Block currentOrigin = enumerationQueue.poll();

if (Math.abs(currentOrigin.getX() - sign.getX()) >= searchRadius)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please ensure that the if statements/etc here keep the braces and follow the style guide?

@BlvckBytes
Copy link
Contributor Author

Have you done any testing to assess how this performs to the old method? A simple loop was chosen here for speed, as some servers have tens of thousands of gates controlled by redstone/etc. My assumption would be that this will have substantially worse cache locality / predictions, but will likely find the gate block earlier than the previous one for most cases.

While I didn't conduct any benchmarks, I have a hard time imagining that this implementation will affect performance in a way that is even noteworthy in real-world scenarios. We're not talking about pipes here, where thousands of blocks are enumerated (possibly) every tick, but rather concise gates with a modest search-radius that may be toggled every so often. Even if there are thousands of them, the overall CPU-time is so minuscule that I wouldn't worry about, personally speaking. This is definitely one of the cases where algorithmic correctness (finding the closest block, as the player expects) is by far outweighing the tiny performance-penalty we take for doing so. Also, are we even sure that cache-hits are increased by walking in an axis-aligned way? I, for one, am not sure if that makes all the difference in the first place.

The only concern I had was the needless allocations of hashing blocks (#getLocation()) in the visited-set, which could use something analogous to my compact-ids, but then again, does it really matter? This version is certainly easier to read and comprehend.

for (var face : LocationUtil.getDirectFaces()) {
Block neighbor = currentOrigin.getRelative(face);

if (!visited.add(neighbor))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's just this if statement missing braces if you'd be able to please fix it

@me4502
Copy link
Member

me4502 commented Dec 26, 2025

"Does it matter?" mostly just depends on whether something is going to result in another issue from someone stating that Gates have gotten significantly slower. Overall I feel this is probably fine at least for now, I was mostly just asking if you'd done much performance testing around it.

@me4502 me4502 merged commit fe92cb5 into EngineHub:five Dec 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants