MCCTeam / Minecraft-Console-Client

Lightweight console for Minecraft chat and automated scripts
https://mccteam.github.io
Other
1.67k stars 402 forks source link

Improve the FindBlock() algorithm #1912

Closed ReinforceZwei closed 2 years ago

ReinforceZwei commented 2 years ago

Discussed in https://github.com/MCCTeam/Minecraft-Console-Client/discussions/1889

Originally posted by **Daenges** January 2, 2022 I had an idea to use Linq to improve the FindBlock() function in `world.cs`: ``` public List FindBlock(Location from, Material block, int radiusx, int radiusy, int radiusz) { Location minPoint = new Location(from.X - radiusx, from.Y - radiusy, from.Z - radiusz); Location maxPoint = new Location(from.X + radiusx, from.Y + radiusy, from.Z + radiusz); List xRange = Enumerable.Range(Convert.ToInt32(Math.Floor(minPoint.X)), Convert.ToInt32(Math.Floor(maxPoint.X - minPoint.X)) + 1).ToList(); List yRange = Enumerable.Range(Convert.ToInt32(Math.Floor(minPoint.Y)), Convert.ToInt32(Math.Floor(maxPoint.Y - minPoint.Y)) + 1).ToList(); List zRange = Enumerable.Range(Convert.ToInt32(Math.Floor(minPoint.Z)), Convert.ToInt32(Math.Floor(maxPoint.Z - minPoint.Z)) + 1).ToList(); List listOfBlocks = xRange.SelectMany(x => yRange.SelectMany(y => zRange.Select(z => new Location(x, y, z)))).ToList(); return listOfBlocks.Where(loc => GetWorld().GetBlock(loc).Type == block).ToList(); } ``` I benchmarked it against the default function and found out that the larger the radius is, the better it performes against the current implementation. Unfortunately cases, where you would actually notice a difference are never reached, due to the limited render distance. Although this implementation provides a small improvement (< 1 sec.), the user will not actually notice a real difference. I did not want this idea to die unheared. Therefore I posted it here, maybe you have some thoughts about it?
ORelio commented 2 years ago

If it's never slower than the original implementation and more readable, why not?

milutinke commented 2 years ago

I'll test it out with the farm bot I made when I am back, and I'll report back on the results, since it utilizes FindBlock heavily.

milutinke commented 2 years ago

Tested using the Farm bot, on the same configuration of farmland, both ran for the same period of time from the same start position. image

I guess this concludes it, it's worth switching.

milutinke commented 2 years ago

Added in #2287