def _add_decay_override_callbacks(self): if not self._decay_rate_overrides: return for override in self._decay_override_list: threshold = Threshold(override.lower_bound, operator.ge) self.add_callback(threshold, self._on_decay_rate_override_changed) threshold = Threshold(override.upper_bound, operator.lt) self.add_callback(threshold, self._on_decay_rate_override_changed)
def _add_decay_override_callbacks(self, override_data, callback): if not override_data: return self._decay_override_calllback_handles = [] value = self.get_value() for override in override_data: if value >= override.lower_bound: if value < override.upper_bound: threshold = Threshold(override.lower_bound, operator.lt) self._decay_override_calllback_handles.append(self.create_and_add_callback_listener(threshold, callback)) threshold = Threshold(override.upper_bound, operator.ge) self._decay_override_calllback_handles.append(self.create_and_add_callback_listener(threshold, callback)) break
def on_activate(self, reader=None): super().on_activate(reader=reader) pet = self.owner.get_pet() wait_stat = pet.commodity_tracker.get_statistic( self.wait_stat_and_value.stat, add=True) wait_stat.set_value(self.wait_stat_and_value.initial_value) op = operator.le if wait_stat.get_decay_rate() <= 0 else operator.ge threshold = Threshold(wait_stat.convergence_value, op) if threshold.compare(wait_stat.get_value()): self.on_wait_stat_zero(wait_stat) else: self.wait_listener = wait_stat.create_and_add_callback_listener( threshold, self.on_wait_stat_zero)
def refresh_threshold_callback(self): if self._decay_callback_handle is not None: self.remove_callback_listener(self._decay_callback_handle) self._decay_callback_handle = None if self.decay_enabled: self._decay_callback_handle = self.create_and_add_callback_listener( Threshold(self._get_previous_level_bound(), operator.lt), self._callback_handler, should_seed=False) self._destory_callback_handle() self._callback_handle = self.create_and_add_callback_listener( Threshold(self._get_next_level_bound(), operator.ge), self._callback_handler, should_seed=False)
def _add_statistic_metric_listeners(self): metrics = self.current_level_tuning.performance_metrics for metric in metrics.statistic_metrics: tracker = self._sim_info.get_tracker(metric.statistic) value = tracker.get_value(metric.statistic) (lower_threshold, upper_threshold) = self._get_statistic_progress_thresholds(metric.statistic, value) if lower_threshold: threshold = Threshold(lower_threshold.threshold, operator.lt) handle = tracker.create_and_activate_listener(metric.statistic, threshold, self._on_statistic_metric_changed) self._statistic_down_listeners[metric.statistic] = handle while upper_threshold: threshold = Threshold(upper_threshold.threshold, operator.ge) handle = tracker.create_and_activate_listener(metric.statistic, threshold, self._on_statistic_metric_changed) self._statistic_up_listeners[metric.statistic] = handle
def load(self, load_data): super().load(load_data) self.ranked_stat = services.statistic_manager().get( load_data.ranked_stat_id) self.rank_threshold = Threshold( value=load_data.threshold_value, comparison=Operator(load_data.threshold_comparison).function)
def _activate_whimset(self, whimset, target=None, chained=False): if chained: new_priority = whimset.chained_priority else: new_priority = whimset.activated_priority if new_priority == 0: return self._sim_info.set_stat_value(whimset.priority_commodity, new_priority, add=True) whimset_data = self._active_whimsets_data.get(whimset) if whimset_data is None: stat = self._sim_info.get_stat_instance(whimset.priority_commodity) threshold = Threshold(whimset.priority_commodity.convergence_value, operator.le) def remove_active_whimset(_): self._deactivate_whimset(whimset) callback_data = stat.create_and_add_callback_listener( threshold, remove_active_whimset) self._active_whimsets_data[whimset] = _ActiveWhimsetData( target, callback_data) stat.decay_enabled = True logger.info('Setting whimset {} to active at priority {}.', whimset, new_priority) else: logger.info( 'Setting whimset {} which is already active to new priority value {}.', whimset, new_priority)
def on_initial_startup(self, from_init=False, activate_convergence_callback=True): self._setup_commodity_distress() if self._distress_callback_data is not None: self.add_callback_data(self._distress_callback_data) self._setup_commodity_failure() if self._failure_callback_data is not None: self.add_callback_data(self._failure_callback_data) if self.commodity_states: self._remove_state_callback() current_value = self.get_value() new_state_index = self._find_state_index(current_value) if self._current_state_index != new_state_index: self._set_state(new_state_index, current_value, from_init=from_init, send_client_update=False) self._add_state_callback() self.decay_enabled = not self.tracker.owner.is_locked(self) self.force_apply_buff_on_start_up = False if self.force_buff_reason is not None and self._buff_handle is not None: current_state = self.commodity_states[self._current_state_index] self.tracker.owner.set_buff_reason(current_state.buff.buff_type, self.force_buff_reason, use_replacement=True) self.force_buff_reason = None if self.remove_on_convergence and self._convergence_callback_data is None: self._convergence_callback_data = self.create_callback(Threshold(self.convergence_value, operator.eq), self._remove_self_from_tracker) if activate_convergence_callback: self.add_callback_data(self._convergence_callback_data) self._allow_convergence_callback_to_activate = False else: self._allow_convergence_callback_to_activate = True
def _set_initial_decay(self): if self._should_decay(): self.decay_enabled = True self._convergence_callback_data = self.add_callback( Threshold(self.convergence_value, operator.eq), self._on_convergence_callback) logger.debug('Setting decay on track {} to {} for {}', self, self.decay_enabled, self.tracker.relationship)
def _create_convergence_callback(self): if self._convergence_callback_data is None: self._convergence_callback_data = self.create_and_add_callback_listener( Threshold(self.convergence_value, operator.eq), self._on_convergence_callback) else: logger.error( 'Track {} attempted to create convergence callback twice.'. format(self))
def on_add(self): super().on_add() if self._should_initialize_first_same_sex_relationship_callback(): self._first_same_sex_relationship_callback_data = self.create_and_add_callback_listener( Threshold( sims.global_gender_preference_tuning. GlobalGenderPreferenceTuning. ENABLE_AUTOGENERATION_SAME_SEX_PREFERENCE_THRESHOLD, operator.ge), self._first_same_sex_relationship_callback)
def refresh_level_up_callback(self): self._destory_callback_handle() def _on_level_up_callback(stat_inst): new_level = stat_inst.get_user_value() old_level = new_level - 1 stat_inst.on_skill_level_up(old_level, new_level) stat_inst.refresh_level_up_callback() self._callback_handle = self.create_and_add_callback_listener(Threshold(self._get_next_level_bound(), operator.ge), _on_level_up_callback)
def _setup_callback_listeners_for_track(bit_set_index, bit_set_list, track, track_update_add_callback, track_update_remove_callback): if bit_set_index < 0: return (None, None) if bit_set_index >= len(bit_set_list): logger.error('BitSetIndex: {}: is out of bounds of bit set list for track:{} cannot setup callbacks: {}: {}', bit_set_index, track, track_update_add_callback, track_update_remove_callback) return (None, None) node = bit_set_list[bit_set_index] alarm_op = operator.lt if node.remove_value >= 0 else operator.ge threshold = Threshold(node.remove_value, alarm_op) remove_callback_listener_data = track.tracker.create_and_add_listener(track.stat_type, threshold, track_update_remove_callback) add_callback_listener_data = None next_node_index = bit_set_index + 1 if node.remove_value >= 0 else bit_set_index - 1 if next_node_index >= 0: if next_node_index < len(bit_set_list): next_node = bit_set_list[next_node_index] alarm_op = operator.ge if next_node.add_value >= 0 else operator.lt threshold = Threshold(next_node.add_value, alarm_op) add_callback_listener_data = track.tracker.create_and_add_listener(track.stat_type, threshold, track_update_add_callback) return (add_callback_listener_data, remove_callback_listener_data)
def passes_threshold(cls, resolver: Resolver, threshold: Threshold) -> bool: """ True if the threshold is passed. """ if resolver is not None: cls.get_verbose_log().format( resolver_type=type(resolver), resolver_class=resolver.__class__, resolver_class_name=resolver.__class__.__name__ ) if threshold is not None: cls.get_verbose_log().format( threshold_type=type(threshold), threshold_class=threshold.__class__ ) return threshold.compare(cls.get_score(resolver))
def on_add(self): if not self.tracker.suppress_callback_setup_during_load: self._per_instance_data.setup_callbacks() self.update_instance_data() if self._add_bit_on_threshold is not None: self.add_callback(self._add_bit_on_threshold.threshold, self._on_add_bit_from_threshold_callback) if self._should_initialize_first_same_sex_relationship_callback(): self._first_same_sex_relationship_callback_data = self.add_callback( Threshold( sims.global_gender_preference_tuning. GlobalGenderPreferenceTuning. ENABLE_AUTOGENERATION_SAME_SEX_PREFERENCE_THRESHOLD, operator.ge), self._first_same_sex_relationship_callback)
def _enter_distress(self, stat_instance): if self.tracker.owner.get_sim_instance() is None: return if self.commodity_distress.buff.buff_type is not None: if self._distress_buff_handle is None: self._distress_buff_handle = self.tracker.owner.add_buff(self.commodity_distress.buff.buff_type, self.commodity_distress.buff.buff_reason, commodity_guid=self.guid64) else: logger.error('Distress Buff Handle is not none when entering Commodity Distress for {}.', self, owner='jjacobson') if self._exit_distress_callback_data is None: self._exit_distress_callback_data = self.add_callback(Threshold(self.commodity_distress.threshold_value, operator.gt), self._exit_distress) else: logger.error('Exit Distress Callback Data is not none when entering Commodity Distress for {}.', self, owner='jjacobson') self.tracker.owner.enter_distress(self) sim = self.tracker.owner.get_sim_instance() for si in itertools.chain(sim.si_state, sim.queue): while self.stat_type in si.commodity_flags: return context = interactions.context.InteractionContext(self.tracker.owner.get_sim_instance(), interactions.context.InteractionContext.SOURCE_AUTONOMY, interactions.priority.Priority.High, insert_strategy=QueueInsertStrategy.NEXT, bucket=interactions.context.InteractionBucketType.DEFAULT) self.tracker.owner.get_sim_instance().push_super_affordance(self.commodity_distress.distress_interaction, None, context)
def register_calbacks(self): if self.update_on_game_option_changed: services.get_event_manager().register_single_event( self, TestEvent.TestedGameOptionChanged) if self.update_if_stat_or_buck_changes: resolver = SingleObjectResolver(self.owner) always_true_threshold = Threshold(-1, operator.ne) for custom_tooltip in self.custom_tooltips: for tooltip_value in custom_tooltip.tooltip_fields.values(): if tooltip_value.text_tokens is not None: for text_token in tooltip_value.text_tokens.tokens: if text_token.token_type == LocalizationTokens.TOKEN_STATISTIC: participant = text_token.participant statistic = text_token.statistic for obj in resolver.get_participants( participant): if obj.has_component( objects.components.types. STATISTIC_COMPONENT): statistic_tracker = obj.get_component( objects.components.types. STATISTIC_COMPONENT ).get_statistic_tracker() statistic_listener = statistic_tracker.create_and_add_listener( statistic, always_true_threshold, lambda _: self. update_object_tooltip()) self._stat_update_listeners[ statistic_tracker].append( statistic_listener) if text_token.token_type == LocalizationTokens.TOKEN_BUCK: participant = text_token.participant bucks_type = text_token.bucks_type for obj in resolver.get_participants( participant): tracker = BucksUtils.get_tracker_for_bucks_type( bucks_type, owner_id=obj.id) callback = lambda: self.update_object_tooltip( ) tracker.add_bucks_modified_callback( bucks_type, callback) self._buck_callback_datas.append( (tracker, bucks_type, callback))
def __init__(self, tunable, description='A tunable blacklist.', **kwargs): super().__init__( blacklist=TunableSet( description= '\n Blacklisted items.\n ', tunable=tunable), threshold=OptionalTunable( description= '\n Tunable option for how many items must be in the blacklist\n for the blacklist to fail when testing a collection of items.\n By default, only one object needs to be in the list.\n ', tunable=TunableThreshold( description= '\n When testing a collection of items, the number of items in\n that collection that are in the blacklist must pass this\n threshold test for the blacklist to disallow them all.\n ', value=TunableRange(tunable_type=int, default=1, minimum=0), default=Threshold(1, operator.ge)), enabled_by_default=True, disabled_name='all_must_match', enabled_name='threshold'), description=description, **kwargs)
def _setup_commodity_distress(self): if self.commodity_distress is not None: self._distress_callback_data = self.create_callback(Threshold(self.commodity_distress.threshold_value, operator.le), self._enter_distress)
def _add_state_callback(self): next_state_index = self._current_state_index + 1 if next_state_index < len(self.commodity_states): self._current_state_ge_callback_data = self.add_callback(Threshold(self.commodity_states[next_state_index].value, operator.ge), self._update_state_up, on_callback_alarm_reset=self._state_reset_callback) if self.commodity_states[self._current_state_index].value > self.min_value: self._current_state_lt_callback_data = self.add_callback(Threshold(self.commodity_states[self._current_state_index].value, operator.lt), self._update_state_down, on_callback_alarm_reset=self._state_reset_callback)
def __init__(self, items, threshold=Threshold(1, operator.ge)): self._items = frozenset(items) self._threshold = threshold