godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.16k stars 97 forks source link

Implement a flood fill algorithm #10392

Open Kakiroi opened 2 months ago

Kakiroi commented 2 months ago

Describe the project you are working on

Grid based game.

Describe the problem or limitation you are having in your project

Flood fill is very common in grid based game. Would be lovely (and performant!) to have this in the core alongside with current astar grid 2d.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

This could be in either separate class, or included in astar_grid_2d class. Since this algorithm is usually used in 2d grid based game, I think this being inside astar_grid_2d class makes sense. User will pass callable that will compare current point with neighboring point and when the callable returns false, next point will be picked out from point list queue. Might need additional property for astar_gird_2d to automatically pick out any solid points from the queue(fill_blocked_by_solid?).

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

It may look like this. Array[Vector2i] fill(begin_point: Vector2i) Array[Vector2i] fill_custom(begin_point: Vector2i, func: Callable)

And you will have something like this.:

func fill_same_weight(a , b) ->bool:
  var a_weight = astar_grid.get_point_weight_scale(a)
  var b_weight = astar_grid.get_point_weight_scale(b)
  return a == b

If the callable returns false during processing fill_custom, b point will be picked out from point list queue. This will give out an array of filled points which has same weight as begin_point.

If this enhancement will not be used often, can it be worked around with a few lines of script?

It can be, but I think this is common in grid based game enough to be in the astar_grid_2d class. (Or be in separate class.)

Is there a reason why this should be core and not an add-on in the asset library?

This Could be in the separate gdextension class. But I think this function being inside astar_grid_2d makes sense as it will be very easy to setup grid, disable points, and immediately get filled point from it. Could be in the separate class and take an array of points as an arguments or property too if made in gdextension class.

Ivorforce commented 1 day ago

I'm contributing a link to OpenCV's implementation, which could be integrated under the Apache 2 license, if desired.