Please or Register to create posts and topics.

Logic Entity Optimization

I just got this idea, but I'm sure someone else has thought of it previously.

In PTI maps, you get a lot of redundant entities because the Instances are designed to deal with all kinds of different cases that might or might not be. For example, the piston_platform has tons of entities to deal with the myriad of ways that the 4-element piston might rise.
And I think it's possible to optimize that by a simple preprocessor:

Inline all func_instance entities.
* Deal with func_instance_parms: replace all $parameters with their values given in func_instance.
* Translate all entities within according to the origin and the angles.
* Perform the name substitutions.
* Deal with func_instance_io_proxy elements.

Merge all logic_auto entities into one. This removes all logic_auto entities except one.

Replace all references to logic_proxy by the signal invocations from the logic_proxy. This removes all logic_proxy entities.

Process all signals that can be preprocessed, sorted by their delay parameter:
* Remove all logic* entities other than logic_auto that do not have any incoming signals remaining.
* In logic_auto, process all connections to a logic_branch element where the signal is a Test, Toggle or ToggleTest. If the signal is Toggle or ToggleTest, toggle their value. If the signal is Test or ToggleTest, copy all of its OnTrue or OnFalse outputs (depending on the value of the logic_branch) into logic_auto. Remove the connection between the logic_auto and the logic_branch.
* In logic_auto, process all connections to a logic_case element where the signal is InValue. Remove the connection from the logic_auto. Pick the relevant OnCase* or OnDefault (depending on the value passed in InValue), and copy it into logic_auto.
* For each InValue connection to a logic_case from any element, where the InValue parameter is a constant, remove the connection between that element and the logic_case, and copy all the relevant OnCase* or OnDefault (depending on the value passed in InValue) outputs into the source element.
* For each Test connection to a logic_branch from any element, where the logic_branch has no remaining Toggle or ToggleTest or SetValue or SetValueTest inputs from anywhere, remove the connection between that element and the logic_branch, and choose the OnTrue or OnFalse output (depending on the value of the logic_branch) and copy it into the source element.
* In logic_auto, for any Kill signal directed at any entity, remove the connection between the logic_auto and the target element and copy their OnKilled outputs into the logic_auto, and delete the target entity, regardless of whether it has remaining inputs or not. (TODO: Verify when this should not be done.)
* Repeat this set of rules as many times as necessary until no further changes happen.

When copying signals:
* My Output = from the originating side (whose targets we began to trace, i.e. OnMapSpawn for logic_auto rather than the OnTrue from the logic_branch it was connected to)
* Target, Via and Parameter = from target side
* Delay = sum of delays from originating and target

Take care to deal with !self, !activator targets properly when inlining signals, and to not do anything that causes the meaning to change.

So, has anyone done this yet? If not, why not? :-) Any reason why it could not be done? Or should not? Should I try it?

You could do this, but I'd imagine it'd be hard to program. It'd be easy to miss some case where this breaks an instance, like when using vscripts or the like. A better way might be to create "new" entities in a FGD that are similar to logic (they won't acually compile properly but they should be there in the vmf). You could then open each instance, process those entities and then save it in a temp directory. Relink the instances in the original file to the temp instances and let VBSP do the work of changing all the names. You could add an entity that does the job of a logic_case, but with a input variable. The program could run the outputs of that item depending on the variable passed.

My stuff:
[spoiler]- BEE2 Addons | (BEE2)
- Hammer Addons
Maps:
- Crushed Gel
- Gel is Not Always Helpful[/spoiler]