increpare / PuzzleScript

Open Source HTML5 Puzzle Game Engine
MIT License
903 stars 159 forks source link

Q: why does compiler only keep property-objects together in rule if they're in the same layer? #728

Closed increpare closed 3 years ago

increpare commented 3 years ago

https://www.puzzlescript.net/editor.html?hack=f913f440b9789b26401034d47a92d425

[target_or_crate]->[]
[player_or_crate]->[]

gets compiled to this:

(85) DOWN [ target ] -> [ ]
(85) DOWN [ crate ] -> [ ]
(87) DOWN [ player_or_crate ] -> [ ]

Why?

ClementSparrow commented 3 years ago

Because the matching functions can deal with objects in a same layer

ClementSparrow commented 3 years ago

longer explanation: matching a cell content like :

[ moving_A object_A moving_B object_B ]

needs to test the presence of (object_A && moving_A) && (object_B && moving_B). Now, if B is a property "C or D", the test becomes (object_A && moving_A) && ( (object_C && moving_C) || (object_D && moving_D) ).

If C and D are in the same layer, moving_C == moving_D and you can simplify the test into (object_A && moving_A) && ( (object_C || object_D) && moving_D), which is what the generated matching functions do (the attribute anyObjectPresent of the rule is a list of masks to encode things like (object_C || object_D) in this example).

If C and D are not in the same layer, you cannot do that optimization and the engine creates two rules (in the function concretizePropertyRule), one for dealing with C and one for dealing with D.

increpare commented 3 years ago

Yeah, that makes sense (disadvantage of runtime code generation: can't be bothered to see exactly what's getting generated right now ^^ ) - for single-layer stuff you can check if all the layers match in parallel, but multi-layer properties need more coordination between layers to determine if they match.

Thanks for reminding me how it works :)