Skip to content

Commit

Permalink
(#145): Factor out comp_relay's ctrl_type/ctrl_value keyvalue behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSpen210 committed Jul 31, 2022
1 parent 8868c14 commit 295d3fe
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
10 changes: 10 additions & 0 deletions fgd/bases/ControlEnables.fgd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@BaseClass = ControlEnables: "A pair of keyvalues for specifying if a comp_ entity is enabled."
[
ctrl_type[engine](boolean) : "Invert Value?" : 0
ctrl_type(choices) : "Control Type" : 0 : "Controls how the Control Value is interpreted" =
[
0 : "Is Enabled?"
1 : "Is Disabled?"
]
ctrl_value(boolean) : "Control Value" : 1 : "Decides whether this entity is enabled. If disabled, the entity has no effect."
]
11 changes: 1 addition & 10 deletions fgd/point/comp/comp_relay.fgd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@PointClass base(StaticTargetName)
@PointClass base(StaticTargetName, ControlEnables)
iconsprite("editor/comp_relay")
appliesto(srctools)
= comp_relay:
Expand All @@ -8,15 +8,6 @@
"Inputs only work if given directly from entities (or via instance redirection), not ingame or dynamically." +
"All inputs/outputs may pass through any parameter, if no override is specified."
[
ctrl_type[engine](boolean) : "Invert Value?" : 0
ctrl_type(choices) : "Control Type" : 0 : "Controls how the value is interpreted for whether to break the connection from this." =
[
0 : "Is Enabled?"
1 : "Is Disabled?"
]
ctrl_value(boolean) : "Control Value" : 1 : "Decides whether to skip using the outputs from this. " +
"If the entity is disabled, all outputs are removed entirely."

delay(float) : "Extra Delay" : 0.0 : "Add this delay to all outputs. This allows you to control this via $fixup values, for instance."

input Trigger(string): "Trigger the relay and fire the output."
Expand Down
21 changes: 19 additions & 2 deletions src/hammeraddons/bsp_transform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Optional, Callable, Awaitable, Dict, Mapping, Tuple, List
import inspect

from srctools import EmptyMapping, FileSystem, Property, VMF, Output, Entity, FGD
from srctools import EmptyMapping, FileSystem, Property, VMF, Output, Entity, FGD, conv_bool
from srctools.bsp import BSP
from srctools.logger import get_logger
from srctools.packlist import PackList
Expand All @@ -12,7 +12,24 @@

LOGGER = get_logger(__name__, 'bsp_trans')

__all__ = ['Context', 'trans', 'run_transformations']
__all__ = [
'check_control_enabled',
'Context', 'trans', 'run_transformations',
]


def check_control_enabled(ent: Entity) -> bool:
"""Implement the bahaviour of ControlEnables - control_type and control_value.
This allows providing a fixup value, and optionally inverting it.
"""
# If ctrl_type is 0, ctrl_value needs to be 1 to be enabled.
# If ctrl_type is 1, ctrl_value needs to be 0 to be enabled.
if 'ctrl_type' in ent:
return conv_bool(ent['ctrl_type'], False) != conv_bool(ent['ctrl_value'], True)
else:
# Missing, assume true if ctrl_value also isn't present.
return conv_bool(ent['ctrl_value'], True)


class Context:
Expand Down
10 changes: 2 additions & 8 deletions transforms/comp_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from srctools import conv_bool, conv_float
from srctools.logger import get_logger

from hammeraddons.bsp_transform import trans, Context
from hammeraddons.bsp_transform import trans, Context, check_control_enabled


LOGGER = get_logger(__name__)
Expand Down Expand Up @@ -32,13 +32,7 @@ def comp_relay(ctx: Context):
ent['classname'].casefold() != 'comp_relay'
for ent in ctx.vmf.by_target[relay_name]
)
# If ctrl_type is 0, ctrl_value needs to be 1 to be enabled.
# If ctrl_type is 1, ctrl_value needs to be 0 to be enabled.
if 'ctrl_type' in relay:
enabled = conv_bool(relay['ctrl_type'], False) != conv_bool(relay['ctrl_value'], True)
else:
# Missing, assume true if ctrl_value also isn't present.
enabled = conv_bool(relay['ctrl_value'], True)
enabled = check_control_enabled(relay)

extra_delay = conv_float(relay['delay'])

Expand Down

0 comments on commit 295d3fe

Please sign in to comment.