def flag_not(inst: Entity, coll: Collisions, flag: Property): """The NOT group inverts the value of it's one sub-flag.""" try: [subflag] = flag except ValueError: return False return not check_flag(subflag, coll, inst)
def add_group(inst: Entity) -> None: """Place the group.""" rng = rand.seed(b'shufflegroup', conf_seed, inst) pools = all_pools.copy() for (flags, value, potential_pools) in conf_selectors: for flag in flags: if not conditions.check_flag(vmf, flag, inst): break else: # Succeeded. allowed_inst = [(name, inst) for (name, inst) in pools if name in potential_pools] name, filename = rng.choice(allowed_inst) pools.remove((name, filename)) vmf.create_ent( 'func_instance', targetname=inst['targetname'], file=filename, angles=inst['angles'], origin=inst['origin'], fixup_style='0', ).fixup[conf_variable] = value
def place_template(inst: Entity) -> None: """Place a template.""" temp_id = inst.fixup.substitute(orig_temp_id) # Special case - if blank, just do nothing silently. if not temp_id: return temp_name, visgroups = template_brush.parse_temp_name(temp_id) try: template = template_brush.get_template(temp_name) except template_brush.InvalidTemplateName: # If we did lookup, display both forms. if temp_id != orig_temp_id: LOGGER.warning('{} -> "{}" is not a valid template!', orig_temp_id, temp_name) else: LOGGER.warning('"{}" is not a valid template!', temp_name) # We don't want an error, just quit. return for vis_flag_block in visgroup_instvars: if all( conditions.check_flag(flag, coll, inst) for flag in vis_flag_block): visgroups.add(vis_flag_block.real_name) force_colour = conf_force_colour if color_var == '<editor>': # Check traits for the colour it should be. traits = instance_traits.get(inst) if 'white' in traits: force_colour = texturing.Portalable.white elif 'black' in traits: force_colour = texturing.Portalable.black else: LOGGER.warning( '"{}": Instance "{}" ' "isn't one with inherent color!", temp_id, inst['file'], ) elif color_var: color_val = conditions.resolve_value(inst, color_var).casefold() if color_val == 'white': force_colour = texturing.Portalable.white elif color_val == 'black': force_colour = texturing.Portalable.black # else: no color var if srctools.conv_bool(conditions.resolve_value(inst, invert_var)): force_colour = template_brush.TEMP_COLOUR_INVERT[conf_force_colour] # else: False value, no invert. if ang_override is not None: orient = ang_override else: orient = rotation @ Angle.from_str(inst['angles', '0 0 0']) origin = conditions.resolve_offset(inst, offset) # If this var is set, it forces all to be included. if srctools.conv_bool( conditions.resolve_value(inst, visgroup_force_var)): visgroups.update(template.visgroups) elif visgroup_func is not None: visgroups.update( visgroup_func( rand.seed(b'temp', template.id, origin, orient), list(template.visgroups), )) LOGGER.debug('Placing template "{}" at {} with visgroups {}', template.id, origin, visgroups) temp_data = template_brush.import_template( vmf, template, origin, orient, targetname=inst['targetname'], force_type=force_type, add_to_map=True, coll=coll, additional_visgroups=visgroups, bind_tile_pos=bind_tile_pos, align_bind=align_bind_overlay, ) if key_block is not None: conditions.set_ent_keys(temp_data.detail, inst, key_block) br_origin = Vec.from_str(key_block.find_key('keys')['origin']) br_origin.localise(origin, orient) temp_data.detail['origin'] = br_origin move_dir = temp_data.detail['movedir', ''] if move_dir.startswith('<') and move_dir.endswith('>'): move_dir = Vec.from_str(move_dir) @ orient temp_data.detail['movedir'] = move_dir.to_angle() for out in outputs: out = out.copy() out.target = conditions.local_name(inst, out.target) temp_data.detail.add_out(out) template_brush.retexture_template( temp_data, origin, inst.fixup, replace_tex, force_colour, force_grid, surf_cat, sense_offset, ) for picker_name, picker_var in picker_vars: picker_val = temp_data.picker_results.get(picker_name, None) if picker_val is not None: inst.fixup[picker_var] = picker_val.value else: inst.fixup[picker_var] = ''
def find_group_quotes( vmf: VMF, group: Property, mid_quotes, allow_mid_voices, use_dings, conf, mid_name: str, player_flag_set: Set[str], ) -> Iterator[PossibleQuote]: """Scan through a group, looking for applicable quote options.""" is_mid = (group.name == 'midchamber') if is_mid: group_id = 'MIDCHAMBER' else: group_id = group['name'].upper() all_quotes = list(group.find_all('quote')) valid_quotes = 0 for quote in all_quotes: valid_quote = True for flag in quote: name = flag.name if name in ('priority', 'name', 'id', 'line') or name.startswith('line_'): # Not flags! continue if not conditions.check_flag(vmf, flag, fake_inst): valid_quote = False break if not valid_quote: continue valid_quotes += 1 poss_quotes = [] line_mid_quotes = [] for line in mode_quotes(quote, player_flag_set): line_id = line['id', line['name', '']].casefold() # Check if the ID is enabled! if conf.get_bool(group_id, line_id, True): if allow_mid_voices and is_mid: line_mid_quotes.append((line, use_dings, mid_name)) else: poss_quotes.append(line) else: LOGGER.info( 'Line "{}" is disabled..', line['name', '??'], ) if line_mid_quotes: mid_quotes.append(line_mid_quotes) if poss_quotes: yield PossibleQuote( quote['priority', '0'], poss_quotes, ) LOGGER.info('"{}": {}/{} quotes..', group_id, valid_quotes, len(all_quotes))
def res_import_template(vmf: VMF, inst: Entity, res: Property): """Import a template VMF file, retexturing it to match orientation. It will be placed overlapping the given instance. If no block is used, only ID can be specified. Options: - `ID`: The ID of the template to be inserted. Add visgroups to additionally add after a colon, comma-seperated (`temp_id:vis1,vis2`). Either section, or the whole value can be a `$fixup`. - `force`: a space-seperated list of overrides. If 'white' or 'black' is present, the colour of tiles will be overridden. If `invert` is added, white/black tiles will be swapped. If a tile size (`2x2`, `4x4`, `wall`, `special`) is included, all tiles will be switched to that size (if not a floor/ceiling). If 'world' or 'detail' is present, the brush will be forced to that type. - `replace`: A block of template material -> replacement textures. This is case insensitive - any texture here will not be altered otherwise. If the material starts with a `#`, it is instead a list of face IDs separated by spaces. If the result evaluates to "", no change occurs. Both can be $fixups (parsed first). - `bindOverlay`: Bind overlays in this template to the given surface, and bind overlays on a surface to surfaces in this template. The value specifies the offset to the surface, where 0 0 0 is the floor position. It can also be a block of multiple positions. - `keys`/`localkeys`: If set, a brush entity will instead be generated with these values. This overrides force world/detail. Specially-handled keys: - `"origin"`, offset automatically. - `"movedir"` on func_movelinear - set a normal surrounded by `<>`, this gets replaced with angles. - `colorVar`: If this fixup var is set to `white` or `black`, that colour will be forced. If the value is `<editor>`, the colour will be chosen based on the color of the surface for ItemButtonFloor, funnels or entry/exit frames. - `invertVar`: If this fixup value is true, tile colour will be swapped to the opposite of the current force option. This applies after colorVar. - `visgroup`: Sets how visgrouped parts are handled. Several values are possible: - A property block: Each name should match a visgroup, and the value should be a block of flags that if true enables that group. - 'none' (default): All extra groups are ignored. - 'choose': One group is chosen randomly. - a number: The percentage chance for each visgroup to be added. - `visgroup_force_var`: If set and True, visgroup is ignored and all groups are added. - `pickerVars`: If this is set, the results of colorpickers can be read out of the template. The key is the name of the picker, the value is the fixup name to write to. The output is either 'white', 'black' or ''. - `outputs`: Add outputs to the brush ent. Syntax is like VMFs, and all names are local to the instance. - `senseOffset`: If set, colorpickers and tilesetters will be treated as being offset by this amount. """ ( orig_temp_id, replace_tex, force_colour, force_grid, force_type, surf_cat, bind_tile_pos, invert_var, color_var, visgroup_func, visgroup_force_var, visgroup_instvars, key_block, picker_vars, outputs, sense_offset, ) = res.value temp_id = inst.fixup.substitute(orig_temp_id) if srctools.conv_bool(conditions.resolve_value(inst, visgroup_force_var)): def visgroup_func(group): """Use all the groups.""" yield from group # Special case - if blank, just do nothing silently. if not temp_id: return temp_name, visgroups = template_brush.parse_temp_name(temp_id) try: template = template_brush.get_template(temp_name) except template_brush.InvalidTemplateName: # If we did lookup, display both forms. if temp_id != orig_temp_id: LOGGER.warning('{} -> "{}" is not a valid template!', orig_temp_id, temp_name) else: LOGGER.warning('"{}" is not a valid template!', temp_name) # We don't want an error, just quit. return for vis_flag_block in visgroup_instvars: if all( conditions.check_flag(vmf, flag, inst) for flag in vis_flag_block): visgroups.add(vis_flag_block.real_name) if color_var.casefold() == '<editor>': # Check traits for the colour it should be. traits = instance_traits.get(inst) if 'white' in traits: force_colour = texturing.Portalable.white elif 'black' in traits: force_colour = texturing.Portalable.black else: LOGGER.warning( '"{}": Instance "{}" ' "isn't one with inherent color!", temp_id, inst['file'], ) elif color_var: color_val = conditions.resolve_value(inst, color_var).casefold() if color_val == 'white': force_colour = texturing.Portalable.white elif color_val == 'black': force_colour = texturing.Portalable.black # else: no color var if srctools.conv_bool(conditions.resolve_value(inst, invert_var)): force_colour = template_brush.TEMP_COLOUR_INVERT[force_colour] # else: False value, no invert. origin = Vec.from_str(inst['origin']) angles = Angle.from_str(inst['angles', '0 0 0']) temp_data = template_brush.import_template( vmf, template, origin, angles, targetname=inst['targetname', ''], force_type=force_type, visgroup_choose=visgroup_func, add_to_map=True, additional_visgroups=visgroups, bind_tile_pos=bind_tile_pos, ) if key_block is not None: conditions.set_ent_keys(temp_data.detail, inst, key_block) br_origin = Vec.from_str(key_block.find_key('keys')['origin']) br_origin.localise(origin, angles) temp_data.detail['origin'] = br_origin move_dir = temp_data.detail['movedir', ''] if move_dir.startswith('<') and move_dir.endswith('>'): move_dir = Vec.from_str(move_dir) @ angles temp_data.detail['movedir'] = move_dir.to_angle() for out in outputs: # type: Output out = out.copy() out.target = conditions.local_name(inst, out.target) temp_data.detail.add_out(out) template_brush.retexture_template( temp_data, origin, inst.fixup, replace_tex, force_colour, force_grid, surf_cat, sense_offset, ) for picker_name, picker_var in picker_vars: picker_val = temp_data.picker_results.get( picker_name, None, ) # type: Optional[texturing.Portalable] if picker_val is not None: inst.fixup[picker_var] = picker_val.value else: inst.fixup[picker_var] = ''
def flag_and(vmf: VMF, inst: Entity, flag: Property): """The AND group evaluates True if all sub-flags are True.""" for sub_flag in flag: if not check_flag(vmf, sub_flag, inst): return False return True
def flag_xor(vmf: VMF, inst: Entity, flag: Property): """The XOR group returns True if the number of true sub-flags is odd.""" return sum([check_flag(vmf, sub_flag, inst) for sub_flag in flag]) % 2 == 1
def flag_not(vmf: VMF, inst: Entity, flag: Property): """The NOT group inverts the value of it's one sub-flag.""" if len(flag.value) == 1: return not check_flag(vmf, flag[0], inst) return False
def flag_or(vmf: VMF, inst: Entity, flag: Property): """The OR group evaluates True if any sub-flags are True.""" for sub_flag in flag: if check_flag(vmf, sub_flag, inst): return True return False
def flag_and(inst: Entity, coll: Collisions, flag: Property): """The AND group evaluates True if all sub-flags are True.""" for i, sub_flag in enumerate(flag): if not check_flag(sub_flag, coll, inst, can_skip=i == 0): return False return True
def flag_nand(inst: Entity, coll: Collisions, flag: Property) -> bool: """The NAND group evaluates True if all sub-flags are False.""" for sub_flag in flag: if not check_flag(sub_flag, coll, inst): return True return False
def flag_xor(inst: Entity, coll: Collisions, flag: Property): """The XOR group returns True if the number of true sub-flags is odd.""" return sum([check_flag(sub_flag, coll, inst) for sub_flag in flag]) % 2 == 1
def flag_or(inst: Entity, coll: Collisions, flag: Property): """The OR group evaluates True if any sub-flags are True.""" for sub_flag in flag: if check_flag(sub_flag, coll, inst): return True return False