|  | 
|  | 1 | +from typing import Optional, cast | 
|  | 2 | + | 
|  | 3 | +from rcrs_core.entities.area import Area | 
|  | 4 | +from rcrs_core.entities.blockade import Blockade | 
|  | 5 | +from rcrs_core.entities.entity import Entity | 
|  | 6 | +from rcrs_core.entities.human import Human | 
|  | 7 | +from rcrs_core.worldmodel.entityID import EntityID | 
|  | 8 | + | 
|  | 9 | +from adf_core_python.core.agent.action.common.action_move import ActionMove | 
|  | 10 | +from adf_core_python.core.agent.communication.message_manager import MessageManager | 
|  | 11 | +from adf_core_python.core.agent.develop.develop_data import DevelopData | 
|  | 12 | +from adf_core_python.core.agent.info.agent_info import AgentInfo | 
|  | 13 | +from adf_core_python.core.agent.info.scenario_info import Mode, ScenarioInfo | 
|  | 14 | +from adf_core_python.core.agent.info.world_info import WorldInfo | 
|  | 15 | +from adf_core_python.core.agent.module.module_manager import ModuleManager | 
|  | 16 | +from adf_core_python.core.agent.precompute.precompute_data import PrecomputeData | 
|  | 17 | +from adf_core_python.core.component.extaction.ext_action import ExtAction | 
|  | 18 | +from adf_core_python.core.component.module.algorithm.path_planning import PathPlanning | 
|  | 19 | + | 
|  | 20 | + | 
|  | 21 | +class DefaultExtendActionMove(ExtAction): | 
|  | 22 | +    def __init__( | 
|  | 23 | +        self, | 
|  | 24 | +        agent_info: AgentInfo, | 
|  | 25 | +        world_info: WorldInfo, | 
|  | 26 | +        scenario_info: ScenarioInfo, | 
|  | 27 | +        module_manager: ModuleManager, | 
|  | 28 | +        develop_data: DevelopData, | 
|  | 29 | +    ) -> None: | 
|  | 30 | +        super().__init__( | 
|  | 31 | +            agent_info, world_info, scenario_info, module_manager, develop_data | 
|  | 32 | +        ) | 
|  | 33 | +        self._target_entity_id: Optional[EntityID] = None | 
|  | 34 | +        self._threshold_to_rest: int = develop_data.get_value("threshold_to_rest", 100) | 
|  | 35 | + | 
|  | 36 | +        match self.scenario_info.get_mode(): | 
|  | 37 | +            case Mode.NON_PRECOMPUTE: | 
|  | 38 | +                self._path_planning: PathPlanning = cast( | 
|  | 39 | +                    PathPlanning, | 
|  | 40 | +                    self.module_manager.get_module( | 
|  | 41 | +                        "DefaultExtendActionMove.PathPlanning", | 
|  | 42 | +                        "adf_core_python.implement.module.astar_path_planning.AStarPathPlanning", | 
|  | 43 | +                    ), | 
|  | 44 | +                ) | 
|  | 45 | +            case Mode.PRECOMPUTATION: | 
|  | 46 | +                pass | 
|  | 47 | +            case Mode.PRECOMPUTED: | 
|  | 48 | +                pass | 
|  | 49 | + | 
|  | 50 | +    def precompute(self, precompute_data: PrecomputeData) -> ExtAction: | 
|  | 51 | +        super().precompute(precompute_data) | 
|  | 52 | +        if self.get_count_precompute() > 1: | 
|  | 53 | +            return self | 
|  | 54 | +        self._path_planning.precompute(precompute_data) | 
|  | 55 | +        return self | 
|  | 56 | + | 
|  | 57 | +    def resume(self, precompute_data: PrecomputeData) -> ExtAction: | 
|  | 58 | +        super().resume(precompute_data) | 
|  | 59 | +        if self.get_count_resume() > 1: | 
|  | 60 | +            return self | 
|  | 61 | +        self._path_planning.resume(precompute_data) | 
|  | 62 | +        return self | 
|  | 63 | + | 
|  | 64 | +    def prepare(self) -> ExtAction: | 
|  | 65 | +        super().prepare() | 
|  | 66 | +        if self.get_count_prepare() > 1: | 
|  | 67 | +            return self | 
|  | 68 | +        self._path_planning.prepare() | 
|  | 69 | +        return self | 
|  | 70 | + | 
|  | 71 | +    def update_info(self, message_manager: MessageManager) -> ExtAction: | 
|  | 72 | +        super().update_info(message_manager) | 
|  | 73 | +        if self.get_count_update_info() > 1: | 
|  | 74 | +            return self | 
|  | 75 | +        self._path_planning.update_info(message_manager) | 
|  | 76 | +        return self | 
|  | 77 | + | 
|  | 78 | +    def set_target_entity_id(self, target_entity_id: EntityID) -> ExtAction: | 
|  | 79 | +        entity: Optional[Entity] = self.world_info.get_entity(target_entity_id) | 
|  | 80 | +        self._target_entity_id = None | 
|  | 81 | + | 
|  | 82 | +        if entity is None: | 
|  | 83 | +            return self | 
|  | 84 | + | 
|  | 85 | +        if isinstance(entity, Blockade): | 
|  | 86 | +            entity = self.world_info.get_entity(cast(Blockade, entity).get_position()) | 
|  | 87 | +        elif isinstance(entity, Human): | 
|  | 88 | +            entity = entity.get_position() | 
|  | 89 | + | 
|  | 90 | +        if entity is None and isinstance(entity, Area): | 
|  | 91 | +            self._target_entity_id = None | 
|  | 92 | + | 
|  | 93 | +        return self | 
|  | 94 | + | 
|  | 95 | +    def calc(self) -> ExtAction: | 
|  | 96 | +        self._result = None | 
|  | 97 | +        agent: Human = cast(Human, self.agent_info.get_myself()) | 
|  | 98 | + | 
|  | 99 | +        path: list[EntityID] = self._path_planning.get_path( | 
|  | 100 | +            agent.get_position(), self._target_entity_id | 
|  | 101 | +        ) | 
|  | 102 | + | 
|  | 103 | +        if path is not None or len(path) != 0: | 
|  | 104 | +            self.result = ActionMove(path) | 
|  | 105 | + | 
|  | 106 | +        return self | 
0 commit comments