So my idea is pretty simple for the event. The event is only fired/called after all checks game normally runs determines an entity can be placed, but right before it passes that boolean result to the rest of the game. Then the handler for event checks whatever criteria it wants and just returns false to treat it as a bad placement. Any other value returned from the handler has no effect. (Also might be good to also return a reason message ).
There are advantages to this compared to the current state of affairs. Currently to achieve similar behavior one checks their constraints in the on_built_entity event. Then if they do not pass instantly mine the entity. Some advantages to this is first the entity the player wants to place will look red just like any other entity that normally could not be placed, and would make placing blue prints for entities with additional constraints behave much nicer and consistent ect...
Also just a side note maybe a better name would be something along the lines pre_build_additional_criteria_check ect...
Add something along the lines pre_final_build_collision_check
Add something along the lines pre_final_build_collision_check
Last edited by Anfilt on Sat Oct 29, 2022 7:37 am, edited 3 times in total.
Re: Add something along the lines pre_final_build_collision_check
One problem for the entity to be red: it would have to apply *every time* the entity could be built by the cursor. Filtering for an item or entity specifically might not be bad for singular entities, but to check every entity in a blueprint every time the blueprint is moved would be extremely disruptive for performance unless (maybe) filters could be checked only at the start of pulling out a blueprint, and optimized away to very strict options (area, nearby entities). For an entity or blueprint with off-grid placement for example, every time the player moved their mouse conditional placement would have to be completely checked by Lua.
Checking only when someone places, and printing a flying text would be a lot more efficient than preemptively refusing.
Checking only when someone places, and printing a flying text would be a lot more efficient than preemptively refusing.
I have mods! I guess!
Link
Link
- Stringweasel
- Filter Inserter
- Posts: 367
- Joined: Thu Apr 27, 2017 8:22 pm
- Contact:
Re: Add something along the lines pre_final_build_collision_check
Not to mention, the cursor isn't part of the game state. Meaning it's not possible for a mod to do calculations on the location of an item in the cursor, because it would desync.
In Factorio all clients do all calculations for all players. Meaning if you have Factory Planner installed, and your friend opens the GUI and does something, the code on your PC will also do the calculations for your friend. All clients do the exact same calculations. The problem comes that the cursor isn't part of the game state, meaning your PC won't know where your friend's cursor is, meaning it can't do the calculations. This will result the two saves getting into different states, and thus, desynchronise.
In Factorio all clients do all calculations for all players. Meaning if you have Factory Planner installed, and your friend opens the GUI and does something, the code on your PC will also do the calculations for your friend. All clients do the exact same calculations. The problem comes that the cursor isn't part of the game state, meaning your PC won't know where your friend's cursor is, meaning it can't do the calculations. This will result the two saves getting into different states, and thus, desynchronise.
Alt-F4 Author | Factorio Modder
My Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock |Weasel's Demolition Derby
Official Contributor to Space Exploration
My Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock |
Official Contributor to Space Exploration
- Stringweasel
- Filter Inserter
- Posts: 367
- Joined: Thu Apr 27, 2017 8:22 pm
- Contact:
Re: Add something along the lines pre_final_build_collision_check
Best workaround is to use collisions, like how Space Exploration very closely dictates which entities can be placed on which surfaces.
Alt-F4 Author | Factorio Modder
My Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock |Weasel's Demolition Derby
Official Contributor to Space Exploration
My Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock |
Official Contributor to Space Exploration
Re: Add something along the lines pre_final_build_collision_check
I don't see this being that performance intensive. The script would be returning a boolean. If the script in the mod is that slow, although making the event only fire for certain/chosen would be fine I assume for most use cases. Also the game runs checks already on the ghost/filler entities as moved around by the cursor.Honktown wrote: ↑Sat Oct 08, 2022 6:24 pmOne problem for the entity to be red: it would have to apply *every time* the entity could be built by the cursor. Filtering for an item or entity specifically might not be bad for singular entities, but to check every entity in a blueprint every time the blueprint is moved would be extremely disruptive for performance unless (maybe) filters could be checked only at the start of pulling out a blueprint, and optimized away to very strict options (area, nearby entities). For an entity or blueprint with off-grid placement for example, every time the player moved their mouse conditional placement would have to be completely checked by Lua.
Checking only when someone places, and printing a flying text would be a lot more efficient than preemptively refusing.
Re: Add something along the lines pre_final_build_collision_check
I don't see a reason why a mod would need to change the game state in a event like this. All a mod would be doing here is returning a boolean to say if building is allowed or not. It should only be needing to read the current state of things to decide yes or no. So in the use case I am not sure how your thinking a de-sync would happen.Stringweasel wrote: ↑Sat Oct 08, 2022 6:30 pmNot to mention, the cursor isn't part of the game state. Meaning it's not possible for a mod to do calculations on the location of an item in the cursor, because it would desync.
In Factorio all clients do all calculations for all players. Meaning if you have Factory Planner installed, and your friend opens the GUI and does something, the code on your PC will also do the calculations for your friend. All clients do the exact same calculations. The problem comes that the cursor isn't part of the game state, meaning your PC won't know where your friend's cursor is, meaning it can't do the calculations. This will result the two saves getting into different states, and thus, desynchronise.
Also there are events that I am pretty sure don't fire across all clients like your thinking. For example `on_load` I am pretty sure only fires for the connecting client.
- Stringweasel
- Filter Inserter
- Posts: 367
- Joined: Thu Apr 27, 2017 8:22 pm
- Contact:
Re: Add something along the lines pre_final_build_collision_check
Thinking about it again, they could probably add a new event for your request without having the cursor part of the game state. It's like a build event, just not successful.
If you're curious when those `on_load` events fire then you can read more here. And those are events when opening Factorio and joining a game, not how Factorio actually runs in multiplayer. For that you can read more in this Alt-F4 article which explains why all things happen for all players on all clients. Although if you don't trust my opinion then it might not help because I helped write it
Alt-F4 Author | Factorio Modder
My Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock |Weasel's Demolition Derby
Official Contributor to Space Exploration
My Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock |
Official Contributor to Space Exploration
Re: Add something along the lines pre_final_build_collision_check
Yea that was my thought.Stringweasel wrote: ↑Sat Oct 29, 2022 7:20 amThinking about it again, they could probably add a new event for your request without having the cursor part of the game state. It's like a build event, just not successful.
I could be wrong, on_load from my understanding at least when reading the API docs only runs on the connecting players client when loading? That's what I mean by does not fire across all clients, and similar vein an event like this could be local to a client.Stringweasel wrote: ↑Sat Oct 29, 2022 7:20 amIf you're curious when those `on_load` events fire then you can read more here. And those are events when opening Factorio and joining a game, not how Factorio actually runs in multiplayer. For that you can read more in this Alt-F4 article which explains why all things happen for all players on all clients. Although if you don't trust my opinion then it might not help because I helped write it
- Stringweasel
- Filter Inserter
- Posts: 367
- Joined: Thu Apr 27, 2017 8:22 pm
- Contact:
Re: Add something along the lines pre_final_build_collision_check
That may not happen though. If that were the case in the event handler you could teleport your player a 100 tiles away for example. But for all the other clients your player is still in the same position, because they did not receive the event. And BAM! desync.
Alt-F4 Author | Factorio Modder
My Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock |Weasel's Demolition Derby
Official Contributor to Space Exploration
My Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock |
Official Contributor to Space Exploration
Re: Add something along the lines pre_final_build_collision_check
Sorry but this isn't likely to ever happen. Build-check events happen outside the game state 99% of the time and mods are not allowed to touch read non-game-state things. Additionally all build-check related pieces of C++ code are not setup to handle if a mod modifies or invalidates the game state in any way and even beginning to think about changing that would be completely outside the scope of anything we want to work on for modding.
If you want to get ahold of me I'm almost always on Discord.