Blueprint bounding box during on_pre_build event with relative snap-to-grid

Place to get help with not working mods / modding interface.
azaghal
Inserter
Inserter
Posts: 40
Joined: Sat Jun 27, 2020 11:13 am
Contact:

Blueprint bounding box during on_pre_build event with relative snap-to-grid

Post by azaghal »

Hello all,

I am trying to figure out how to reliably calculate the bounding box of a blueprint on the surface when it is being placed down by the player during the on_pre_build event. The reason for this is that I need to apply some operations on any number of ghost entities that occupy the affected area to ensure consistency.

So far I have managed to calculate the blueprint width/height and to then use the event's position and direction to calculate the bounding box on top of the surface. This is based around treating the event position as center of the blueprint and doing some iteration over blueprint entities (using their positions within the blueprint) to calculate the blueprint width/height. This works reliably so long as the player is not using a blueprint with snap to grid (either relative or absolute). Current implementation I have for this can be found at:

- https://github.com/azaghal/Factorio-Con ... .lua#L1455 (event handler)
- https://github.com/azaghal/Factorio-Con ... l.lua#L959 (bounding box calculation based on blueprint dimensions, position, and orientation)
- https://github.com/azaghal/Factorio-Con ... l.lua#L918 (blueprint dimensions calculation)
- https://github.com/azaghal/Factorio-Con ... .lua#L1001 (individual entity bounding box calculation within blueprint itself)

The problem when using snap to grid and dragging the blueprint is that the event position will no longer denote the placed blueprint's center (-ish) position on the surface past the first blueprint placed in this manner. The event position will instead usually point to the "edge" where the player has crossed with the cursor outside of the bounds of previously placed blueprint borders.

Does anyone have an idea on how to reliably get the center position of placed blueprint during the on_pre_build event - including the above edge case when using blueprints that snap to grid?
Post Reply

Return to “Modding help”