Skip to content

Commit

Permalink
Add a value mode option to comp_kv_setter / comp_adv_output
Browse files Browse the repository at this point in the history
This fixes an issue where position would override the other options and makes them more controllable.
  • Loading branch information
TeamSpen210 committed Apr 3, 2024
1 parent 21d9584 commit e872e6d
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Allow comp_entity_finder to rotate the target in addition to teleporting.
* Fix various incorrect usages of the FGD `frustum()` helper.
* Fix a compile failure if prop ropes were placed in a group with no connections.
* Added a value mode option to `comp_kv_setter` and `comp_adv_output` to more clearly control which key is used.
* A new "snippets" system allows FGD files to easily repeat descriptions and other small sections.


Expand Down
22 changes: 21 additions & 1 deletion fgd/point/comp/comp_adv_output.fgd
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,43 @@
params_fmt(string) : "Parameter" : "{1}" : "Sets the parameter to use. This can contain placeholders like {1},{2} etc which will be filled by values in the" +
" following parameters. Use two braces like {{ or }} if you need them in the parameter directly."

params_mode1[engine](integer) : "Parameter 1 - Mode" : "legacy"
params_mode1(choices) : "Parameter 1 - Mode" : "legacy" : "Which value to use to replace {2} in the parameter. " +
"This allows using values altered in different ways by the instance. " +
"In legacy mode Ent Name then String is checked." = #snippet "CompKVMode"
params_global1(target_destination) : "Parameter 1 - String" : : "A value which will replace {1} in the parameter."
params_local1(target_destination) : "Parameter 1 - Ent Name" : : "If set, this is a fixed up entity name which will be used in the parameter, replacing {1}."
// Has to be "origin" type to get transformed in 2013 engine games
params_pos1(origin) : "Parameter 1 - Position" : : "If set, this is a XYZ position which will be used in the parameter, replacing {1}. This will be offset by instancing."

params_mode2[engine](integer) : "Parameter 2 - Mode" : "legacy"
params_mode2(choices) : "Parameter 2 - Mode" : "legacy" : "Which value to use to replace {2} in the parameter. " +
"This allows using values altered in different ways by the instance. " +
"In legacy mode Ent Name then String is checked." = #snippet "CompKVMode"
params_global2(target_destination) : "Parameter 2 - String" : : "A value which will replace {2} in the parameter."
params_local2(target_destination) : "Parameter 2 - Ent Name" : : "If set, this is a fixed up entity name which will be used in the parameter, replacing {1}."
params_local2(target_destination) : "Parameter 2 - Ent Name" : : "If set, this is a fixed up entity name which will be used in the parameter, replacing {2}."
params_pos2(origin) : "Parameter 2 - Position" : : "If set, this is a XYZ position which will be used in the parameter, replacing {2}. This will be offset by instancing."

params_mode3[engine](integer) : "Parameter 3 - Mode" : "legacy"
params_mode3(choices) : "Parameter 3 - Mode" : "legacy" : "Which value to use to replace {3} in the parameter. " +
"This allows using values altered in different ways by the instance. " +
"In legacy mode Ent Name then String is checked." = #snippet "CompKVMode"
params_global3(target_destination) : "Parameter 3 - String" : : "A value which will replace {3} in the parameter."
params_local3(target_destination) : "Parameter 3 - Ent Name" : : "If set, this is a fixed up entity name which will be used in the parameter, replacing {3}."
params_pos3(origin) : "Parameter 3 - Position" : : "If set, this is a XYZ position which will be used in the parameter, replacing {3}. This will be offset by instancing."

params_mode4[engine](integer) : "Parameter 4 - Mode" : "legacy"
params_mode4(choices) : "Parameter 4 - Mode" : "legacy" : "Which value to use to replace {4} in the parameter. " +
"This allows using values altered in different ways by the instance. " +
"In legacy mode Ent Name then String is checked." = #snippet "CompKVMode"
params_global4(target_destination) : "Parameter 4 - Ent Name" : : "A value which will replace {4} in the parameter."
params_local4(target_destination) : "Parameter 4 - Ent Name" : : "If set, this is a fixed up entity name which will be used in the parameter, replacing {4}."
params_pos4(origin) : "Parameter 4 - Position" : : "If set, this is a XYZ position which will be used in the parameter, replacing {4}. This will be offset by instancing."

params_mode5[engine](integer) : "Parameter 5 - Mode" : "legacy"
params_mode5(choices) : "Parameter 5 - Mode" : "legacy" : "Which value to use to replace {5} in the parameter. " +
"This allows using values altered in different ways by the instance. " +
"In legacy mode Ent Name then String is checked." = #snippet "CompKVMode"
params_global5(target_destination) : "Parameter 5 - String" : : "A value which will replace {5} in the parameter."
params_local5(target_destination) : "Parameter 5 - Ent Name" : : "If set, this is a fixed up entity name which will be used in the parameter, replacing {5}."
params_pos5(origin) : "Parameter 5 - Position" : : "If set, this is a XYZ position which will be used in the parameter, replacing {5}. This will be offset by instancing."
Expand Down
4 changes: 4 additions & 0 deletions fgd/point/comp/comp_kv_setter.fgd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

kv_name(string) : "Name / Mask" : : "The name of the keyvalue to set, or the bits to change for spawnflags."

kv_value_mode[engine](string) : "Value - Mode" : "legacy"
kv_value_mode(choices) : "Value - Mode" : "legacy" : "Which value to use. " +
"This allows using values altered in different ways by the instance." +
"In legacy mode Ent Name then String is checked." = #snippet "CompKVMode"
kv_value_global(string) : "Value - String" : : "The value to apply."
kv_value_local(target_destination) : "Value - Ent Name" : : "If set, use this fixed-up entity name."
// Has to be "origin" type to get transformed in 2013 engine games
Expand Down
8 changes: 8 additions & 0 deletions fgd/snippets/comp.fgd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Shared values from postcompiler entities.

@snippet choices "CompKVMode" = [
"legacy": "Legacy (Entity Name or String)"
"global": "String"
"local": "Entity Name"
"pos": "Position"
]
21 changes: 21 additions & 0 deletions src/hammeraddons/bsp_transform/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,24 @@ def create(cls, vmf: VMF, pos: Union[Vec, FrozenVec], name: str) -> Iterator[Sel
).make_unique(name)
for inp, out in user_outs:
yield cls(ent, inp, out)


def get_multimode_value(ent: Entity, *, prefix: str='', suffix: str='', desc: str) -> str:
"""Read from differerent typed keyvalues, specified by a mode option.
The mode was originally not present, which is why local/global is doubled up.
"""
mode = ent[f'{prefix}mode{suffix}', 'string'].casefold()
if mode == 'legacy':
return ent[f'{prefix}global{suffix}'] or ent[f'{prefix}_local']
elif mode == 'global':
return ent[f'{prefix}global{suffix}']
elif mode == 'local':
return ent[f'{prefix}local{suffix}']
elif mode == 'position':
return ent[f'{prefix}pos{suffix}']
else:
LOGGER.warning(
'Invalid {} mode "{}" for {}!',
desc, mode, ent_description(ent),
)
9 changes: 3 additions & 6 deletions transforms/comp_adv_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from srctools.vmf import Output, Entity
from srctools.logger import get_logger

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


class SimpleFormatter(string.Formatter):
Expand Down Expand Up @@ -69,11 +70,7 @@ def advanced_output(ctx: Context) -> None:

param_args: List[str] = []
for ind in itertools.count(1):
val = (
adv_out[f'params_pos{ind}']
or adv_out[f'params_local{ind}']
or adv_out[f'params_global{ind}']
)
val = get_multimode_value(adv_out, prefix='params_', suffix=str(ind), desc=f'Param {ind}')
if not val:
break
param_args.append(val)
Expand Down
6 changes: 3 additions & 3 deletions transforms/comp_kv_setter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from srctools import Angle, conv_int, conv_bool, Vec
from srctools.logger import get_logger

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

LOGGER = get_logger(__name__)

Expand All @@ -33,8 +34,7 @@ def kv_setter(ctx: Context) -> None:
continue

kv_name = setter['kv_name']
# Use fixup name if actually set.
kv_value = setter['kv_value_pos'] or setter['kv_value_local'] or setter['kv_value_global']
kv_value = get_multimode_value(setter, prefix='kv_value_', desc='value')

if conv_bool(setter['invert']):
kv_value = '0' if conv_bool(kv_value) else '1'
Expand Down

0 comments on commit e872e6d

Please sign in to comment.