def load_layer_immediately(self, conditional_layer): if conditional_layer not in self._loaded_layers: self._loaded_layers.append(conditional_layer) services.conditional_layer_service().load_conditional_layer( conditional_layer, callback=self.on_layer_loaded, speed=ConditionalLayerRequestSpeedType.IMMEDIATELY)
def load_layer_gradually(self, conditional_layer): if conditional_layer not in self._loaded_layers: self._loaded_layers.append(conditional_layer) services.conditional_layer_service().load_conditional_layer( conditional_layer, callback=self.on_layer_loaded, speed=ConditionalLayerRequestSpeedType.GRADUALLY, timer_interval=OpenStreetDirectorBase.LAYER_OBJECTS_ALARM_TIME, timer_object_count=OpenStreetDirectorBase.LAYER_OBJECTS_TO_LOAD)
def remove_layer_objects(self, conditional_layer): speed = ConditionalLayerRequestSpeedType.GRADUALLY if services.current_zone( ).is_zone_running else ConditionalLayerRequestSpeedType.IMMEDIATELY services.conditional_layer_service().destroy_conditional_layer( conditional_layer, callback=self.on_layer_objects_destroyed, speed=speed, timer_interval=OpenStreetDirectorBase.LAYER_OBJECTS_ALARM_TIME, timer_object_count=OpenStreetDirectorBase.LAYER_OBJECTS_TO_DESTROY)
def load_objects_callback(_): (complete, object_ids) = load_conditional_objects( zone_id, self.conditional_layer.layer_name, self.timer_object_count) self._set_up_loaded_objects(object_ids) if is_archive_enabled(): archive_layer_request_culling(self, LayerRequestAction.EXECUTING, objects_in_layer_count=len( layer_info.objects_loaded)) if complete: alarms.cancel_alarm(self.alarm_handle) self._alarm_handle = None services.conditional_layer_service().complete_current_request()
def _destroy_layer_immediately(self): object_manager = services.object_manager() layer_info = services.conditional_layer_service()._get_layer_info( self.conditional_layer) for object_id in layer_info.objects_loaded: obj = object_manager.get(object_id) if obj is not None: obj.destroy( source=self, cause='Destroying object from conditional layer service.', fade_duration=ClientObjectMixin.FADE_DURATION) layer_info.objects_loaded.clear() conditional_layer_destroyed(services.current_zone_id(), self.conditional_layer.layer_name) services.conditional_layer_service().complete_current_request()
def _load_layer_gradually(self): zone_id = services.current_zone_id() layer_info = services.conditional_layer_service()._get_layer_info( self.conditional_layer) mark_conditional_objects_loaded(zone_id, self.conditional_layer.layer_name, layer_info.objects_loaded) def load_objects_callback(_): (complete, object_ids) = load_conditional_objects( zone_id, self.conditional_layer.layer_name, self.timer_object_count) self._set_up_loaded_objects(object_ids) if is_archive_enabled(): archive_layer_request_culling(self, LayerRequestAction.EXECUTING, objects_in_layer_count=len( layer_info.objects_loaded)) if complete: alarms.cancel_alarm(self.alarm_handle) self._alarm_handle = None services.conditional_layer_service().complete_current_request() self.alarm_handle = alarms.add_alarm( self, TimeSpan.ZERO, load_objects_callback, repeating=True, repeating_time_span=create_time_span(minutes=self.timer_interval))
def on_enter(self): super().on_enter() zone = services.current_zone() zone.venue_service.build_buy_edit_mode = True services.household_manager().load_households() zone.on_households_and_sim_infos_loaded() client = zone.zone_spin_up_service._client_connect_data.client club_service = services.get_club_service() if club_service is not None: club_service.on_all_households_and_sim_infos_loaded(client) relationship_service = services.relationship_service() if relationship_service is not None: relationship_service.on_all_households_and_sim_infos_loaded(client) roommate_service = services.get_roommate_service() if roommate_service is not None: roommate_service.initialize_editmode_roommates() situation_manager = services.get_zone_situation_manager() situation_manager.spin_up_for_edit_mode() object_manager = services.object_manager() water_terrain_object_cache = object_manager.water_terrain_object_cache build_buy.register_build_buy_exit_callback( water_terrain_object_cache.refresh) conditional_layer_service = services.conditional_layer_service() conditional_layer_service.on_zone_load(editmode=True) for obj in object_manager.values(): footprint_component = obj.get_component(FOOTPRINT_COMPONENT) if footprint_component is not None: footprint_component.on_finalize_load() services.game_clock_service().restore_saved_clock_speed() return _ZoneSpinUpStateResult.DONE
def on_layer_loaded(self, conditional_layer): layer_objects = services.conditional_layer_service().get_layer_objects( conditional_layer) for obj in layer_objects: if obj.environmentscore_component is not None: obj.remove_component( objects.components.types.ENVIRONMENT_SCORE_COMPONENT)
def _do_change_when_appropriate(self, from_finalize=False): if self._change_alarm is not None: alarms.cancel_alarm(self._change_alarm) self._change_alarm = None if self._last_change_start_time is None: logger.error( 'Attempt to (re)start conditional layer without start time set for {}', self) return if self.delay_from_change is not None: time_of_change = self._last_change_start_time + self.delay_from_change( ) else: time_of_change = self._last_change_start_time now = services.time_service().sim_now time_until_change = time_of_change - now if time_until_change == TimeSpan.ZERO: self._do_change(from_finalize=from_finalize) elif time_until_change > TimeSpan.ZERO: self._change_alarm = alarms.add_alarm(self, time_until_change, lambda _: self._do_change(), cross_zone=True) elif self.layer.conditional_layer.client_only and self._is_on_active_street( ): conditional_layer_service = services.conditional_layer_service() show_layer = self.layer.show_conditional_layer invert_show = not self.policy.enacted if invert_show: show_layer = not show_layer if show_layer != conditional_layer_service.is_layer_loaded( self.layer.conditional_layer): self.layer.change_conditional_layer(invert_show=invert_show)
def load_conditional_layer(conditional_layer:TunableInstanceParam(sims4.resources.Types.CONDITIONAL_LAYER), immediate:bool=True, timer_interval:int=1, timer_object_count:int=5): if conditional_layer is None: sims4.commands.output('Unable to find the conditional_layer instance specified.') return conditional_layer_service = services.conditional_layer_service() speed = ConditionalLayerRequestSpeedType.IMMEDIATELY if immediate else ConditionalLayerRequestSpeedType.GRADUALLY conditional_layer_service.load_conditional_layer(conditional_layer, speed=speed, timer_interval=timer_interval, timer_object_count=timer_object_count)
def get_all_layer_created_objects(self): conditional_object_service = services.conditional_layer_service() objects = [] for conditional_layer in self._loaded_layers: objects.extend( conditional_object_service.get_layer_objects( conditional_layer)) return objects
def on_zone_load(self): with services.conditional_layer_service().defer_conditional_layer_event_processing(): startup_narratives = set(self.INITIAL_NARRATIVES) startup_narratives -= self._active_narratives.keys() | self._completed_narratives for narrative_to_start in startup_narratives: self._active_narratives[narrative_to_start] = narrative_to_start() self._send_narrative_start_telemetry(narrative_to_start) self._handle_narrative_updates(custom_keys=startup_narratives, immediate=True) for narrative_instance in self._active_narratives.values(): narrative_instance.on_zone_load()
def _load_layer_immediately(self): zone_id = services.current_zone_id() layer_info = services.conditional_layer_service()._get_layer_info( self.conditional_layer) mark_conditional_objects_loaded(zone_id, self.conditional_layer.layer_name, layer_info.objects_loaded) (complete, object_ids) = load_conditional_objects( zone_id, self.conditional_layer.layer_name, -1) if not complete: logger.error( 'Error when trying to load layer {}. Attempted to load entire layer at once and it did not completely load everything.', self.conditional_layer) self._set_up_loaded_objects(object_ids) if is_archive_enabled(): archive_layer_request_culling(self, LayerRequestAction.EXECUTING, objects_in_layer_count=len( layer_info.objects_loaded)) services.conditional_layer_service().complete_current_request()
def set_global_policy_progress(self, global_policy, new_value, resolver=None): with services.conditional_layer_service( ).defer_conditional_layer_event_processing(): old_state = global_policy.progress_state new_state = global_policy.set_progress_value(new_value) if new_state != old_state and new_state == GlobalPolicyProgressEnum.COMPLETE: self.set_up_decay_handler(global_policy) global_policy.apply_policy_loot_to_active_sim( global_policy.loot_on_complete, resolver)
def start_narrative(self, narrative, do_handle_updates=True): if narrative in self._active_narratives or narrative.guid64 in self._locked_narratives: return with services.conditional_layer_service().defer_conditional_layer_event_processing(): for active_narrative in tuple(self._active_narratives): if active_narrative.narrative_groups & narrative.narrative_groups: self.end_narrative(active_narrative) narrative_instance = narrative() narrative_instance.start() self._active_narratives[narrative] = narrative_instance self._send_narrative_start_telemetry(narrative) if do_handle_updates: self._handle_narrative_updates(custom_keys=(narrative,))
def cleanup_old_open_street_director(self): if self._prior_open_street_director_proto is None: return conditional_layer_service = services.conditional_layer_service() conditional_layer_manager = services.get_instance_manager(sims4.resources.Types.CONDITIONAL_LAYER) for layer_guid in self._prior_open_street_director_proto.loaded_layer_guids: layer = conditional_layer_manager.get(layer_guid) if layer is not None: conditional_layer_service.destroy_conditional_layer(layer) if self._prior_open_street_director_proto.HasField('resource_key'): previous_resource_key = sims4.resources.get_key_from_protobuff(self._prior_open_street_director_proto.resource_key) if previous_resource_key is not None: open_street_director_type = services.get_instance_manager(sims4.resources.Types.OPEN_STREET_DIRECTOR).get(previous_resource_key.instance) if open_street_director_type is not None: open_street_director_type.run_lot_cleanup()
def generate_conditional_layer_service_data(zone_id: int = None): layer_data = [] conditional_layer_service = services.conditional_layer_service() if conditional_layer_service is None: return layer_data object_manager = services.object_manager() for (conditional_layer, layer_info) in conditional_layer_service._layer_infos.items(): object_data = [] for object_id in layer_info.objects_loaded: obj = object_manager.get(object_id) object_data.append({ 'object_id': str(object_id), 'object': str(obj) }) request_data = [] for request in conditional_layer_service.requests: if request.conditional_layer is conditional_layer: request_data.append({ 'request': str(request), 'speed': request.speed.name, 'timer_interval': str(request.timer_interval), 'timer_object_count': str(request.timer_object_count) }) layer_data.append({ 'layer_hash': str(conditional_layer.layer_name), 'conditional_layer': str(conditional_layer), 'objects_created': str(len(layer_info.objects_loaded)), 'requests_waiting': str(len(request_data)), 'last_request': str(layer_info.last_request_type), 'Objects': object_data, 'Requests': request_data }) return layer_data
def __call__(self, *args, **kwargs): conditional_layer_service = services.conditional_layer_service() if conditional_layer_service is None: return TestResult(False, 'No Conditional Layer Service available', tooltip=self.tooltip) layer_loaded = conditional_layer_service.is_layer_loaded( self.conditional_layer, self.consider_queued_requests) if layer_loaded: if self.negate: return TestResult(False, 'Conditional Layer loaded', tooltip=self.tooltip) elif not self.negate: return TestResult(False, 'Conditional Layer not loaded', tooltip=self.tooltip) return TestResult.TRUE
def _load_layer_as_client_only(self): conditional_layer_service = services.conditional_layer_service() conditional_layer_service._set_client_layer(self.conditional_layer, True, speed=self.speed) conditional_layer_service.complete_current_request()
def _destroy_objects_callback(self, _): object_manager = services.object_manager() layer_info = services.conditional_layer_service()._get_layer_info( self.conditional_layer) objects_destroyed = 0 objects_index = 0 now = services.time_service().sim_now timeout_time = now + create_time_span( minutes=DestroyConditionalLayerRequest.DESTRUCTION_TIMEOUT) objects_loaded = list(layer_info.objects_loaded) while objects_destroyed < self.timer_object_count: if not objects_loaded: alarms.cancel_alarm(self.alarm_handle) self.alarm_handle = None conditional_layer_destroyed(services.current_zone_id(), self.conditional_layer.layer_name) services.conditional_layer_service().complete_current_request() return if objects_index >= len(objects_loaded): return object_id = objects_loaded[objects_index] obj = object_manager.get(object_id) if obj is None: del objects_loaded[objects_index] layer_info.objects_loaded.remove(object_id) else: timeout = self._destory_object_timeouts.get(object_id) if timeout is not None and timeout <= now: obj.destroy( source=self, cause= 'Destroying object from conditional layer service.', fade_duration=ClientObjectMixin.FADE_DURATION) del self._destory_object_timeouts[object_id] objects_destroyed += 1 else: users = obj.get_users() if users: objects_index += 1 if object_id in self._destory_object_timeouts: continue for sim in users: for interaction in sim.si_state: if interaction.target is None: continue if not interaction.target is obj: if interaction.target.is_part and interaction.target.part_owner is obj: interaction.cancel( FinishingType.TARGET_DELETED, cancel_reason_msg= 'Removing conditional object.') interaction.cancel( FinishingType.TARGET_DELETED, cancel_reason_msg= 'Removing conditional object.') self._destory_object_timeouts[object_id] = timeout_time else: obj.destroy( source=self, cause= 'Destroying object from conditional layer service.', fade_duration=ClientObjectMixin.FADE_DURATION) del objects_loaded[objects_index] layer_info.objects_loaded.remove(object_id) if object_id in self._destory_object_timeouts: del self._destory_object_timeouts[object_id] objects_destroyed += 1
def reload_conditional_layer(conditional_layer:TunableInstanceParam(sims4.resources.Types.CONDITIONAL_LAYER), immediate:bool=True, timer_interval:int=1, timer_object_count:int=5): conditional_layer_service = services.conditional_layer_service() speed = ConditionalLayerRequestSpeedType.IMMEDIATELY if immediate else ConditionalLayerRequestSpeedType.GRADUALLY conditional_layer_service.destroy_conditional_layer(conditional_layer, speed=speed, timer_interval=timer_interval, timer_object_count=timer_object_count) conditional_layer_service.load_conditional_layer(conditional_layer, speed=speed, timer_interval=timer_interval, timer_object_count=timer_object_count)
def _destroy_layer_as_client_only(self, speed): conditional_layer_service = services.conditional_layer_service() conditional_layer_service._set_client_layer(self.conditional_layer, False, speed=speed) conditional_layer_service.complete_current_request()
def _set_up_loaded_objects(self, object_ids): should_show_blocking_object_notification = False object_manager = services.object_manager() conditional_layer_service = services.conditional_layer_service() layer_info = conditional_layer_service._get_layer_info( self.conditional_layer) for object_id in object_ids: if object_id in layer_info.objects_loaded: logger.error( 'Trying to setup object of id {} which was already setup.', object_id) else: obj = object_manager.get(object_id) if obj is None: logger.error( 'Error when trying to setup objects loaded by the layer. Open street director was given object id {} of object not in the object manager.', object_id) else: (result, errors, blocking_objects) = test_location_for_object( obj=obj, return_blocking_object_ids=True) if blocking_objects: for blocking_obj_id in blocking_objects: blocking_obj_id = blocking_obj_id[0] blocking_obj = object_manager.get(blocking_obj_id) if blocking_obj is None: continue household_id = blocking_obj.get_household_owner_id( ) if household_id is None: continue active_household_id = services.active_household_id( ) if blocking_obj.has_component( objects.components.types.CRAFTING_COMPONENT ): blocking_obj.destroy( source=self, cause= 'Destroying object with Crafting Component from conditional layer service.', fade_duration=ClientObjectMixin. FADE_DURATION) else: tracker = blocking_obj.get_tracker( CraftingTuning.SERVINGS_STATISTIC) if tracker is not None and tracker.has_statistic( CraftingTuning.SERVINGS_STATISTIC): blocking_obj.destroy( source=self, cause= 'Destroying object with servings statistic from conditional layer service.', fade_duration=ClientObjectMixin. FADE_DURATION) else: if household_id == active_household_id: should_show_blocking_object_notification = True move_object_to_household_inventory( blocking_obj) if self.fade_in: obj.opacity = 0 obj.fade_in() if obj.has_component(SPAWNER_COMPONENT): obj.spawner_component.initialize_spawning() layer_info.objects_loaded.add(object_id) if should_show_blocking_object_notification: early_exit_notification = LoadConditionalLayerRequest.OBJECT_MOVED_TO_HOUSEHOLD_INVENTORY_NOTIFICATION( services.active_sim_info()) early_exit_notification.show_dialog()
def can_be_added_dynamically(cls, obj): return not services.conditional_layer_service().is_object_in_conditional_layer(obj)