@@ -59,7 +59,7 @@ def __init__(
5959 )
6060 )
6161
62- self ._target_entity_id = None
62+ self ._target_entity_id : Optional [ EntityID ] = None
6363 self ._move_point_cache : dict [EntityID , Optional [set [tuple [float , float ]]]] = {}
6464 self ._old_clear_x = 0
6565 self ._old_clear_y = 0
@@ -135,14 +135,14 @@ def calculate(self) -> ExtendAction:
135135 return self
136136
137137 agent_position_entity_id = police_force .get_position ()
138+ if agent_position_entity_id is None :
139+ return self
138140 target_entity = self .world_info .get_entity (self ._target_entity_id )
139141 position_entity = self .world_info .get_entity (agent_position_entity_id )
140142 if target_entity is None or isinstance (target_entity , Area ) is False :
141143 return self
142144 if isinstance (position_entity , Road ):
143- self .result = self ._get_rescue_action (
144- police_force , cast (Road , position_entity )
145- )
145+ self .result = self ._get_rescue_action (police_force , position_entity )
146146 if self .result is not None :
147147 return self
148148
@@ -180,7 +180,7 @@ def calculate(self) -> ExtendAction:
180180 police_force , cast (Area , entity )
181181 )
182182 if self .result is not None and isinstance (self .result , ActionMove ):
183- action_move = cast ( ActionMove , self .result )
183+ action_move = self .result
184184 if action_move .is_destination_defined ():
185185 self .result = None
186186
@@ -193,15 +193,16 @@ def _need_rest(self, police_force: PoliceForce) -> bool:
193193 hp = police_force .get_hp ()
194194 damage = police_force .get_damage ()
195195
196- if hp == 0 or damage == 0 :
196+ if hp is None or damage is None or hp == 0 or damage == 0 :
197197 return False
198198
199199 active_time = (hp / damage ) + (1 if (hp % damage ) != 0 else 0 )
200200 if self ._kernel_time == - 1 :
201201 self ._kernel_time = self .scenario_info .get_value ("kernel.timesteps" , - 1 )
202202
203- return damage >= self ._threshold_rest or (
204- active_time + self .agent_info .get_time () < self ._kernel_time
203+ return damage is not None and (
204+ damage >= self ._threshold_rest
205+ or (active_time + self .agent_info .get_time () < self ._kernel_time )
205206 )
206207
207208 def _calc_rest (
@@ -211,6 +212,8 @@ def _calc_rest(
211212 target_entity_ids : list [EntityID ],
212213 ) -> Optional [Action ]:
213214 position_entity_id = police_force .get_position ()
215+ if position_entity_id is None :
216+ return None
214217 refuges = self .world_info .get_entity_ids_of_types ([Refuge ])
215218 current_size = len (refuges )
216219 if position_entity_id in refuges :
@@ -244,12 +247,13 @@ def _calc_rest(
244247 def _get_rescue_action (
245248 self , police_entity : PoliceForce , road : Road
246249 ) -> Optional [Action ]:
250+ road_blockades = road .get_blockades ()
247251 blockades = set (
248252 []
249- if road . get_blockades () is None
253+ if road_blockades is None
250254 else [
251255 cast (Blockade , self .world_info .get_entity (blockade_entity_id ))
252- for blockade_entity_id in road . get_blockades ()
256+ for blockade_entity_id in road_blockades
253257 ]
254258 )
255259 agent_entities = set (
@@ -263,15 +267,30 @@ def _get_rescue_action(
263267
264268 for agent_entity in agent_entities :
265269 human = cast (Human , agent_entity )
266- if human .get_position ().get_value () != road .get_entity_id ().get_value ():
270+ human_position = human .get_position ()
271+ if (
272+ human_position is None
273+ or human_position .get_value () != road .get_entity_id ().get_value ()
274+ ):
267275 continue
268276
269277 human_x = human .get_x ()
270278 human_y = human .get_y ()
279+ if (
280+ human_x is None
281+ or human_y is None
282+ or police_x is None
283+ or police_y is None
284+ ):
285+ continue
286+
271287 action_clear : Optional [ActionClear | ActionClearArea ] = None
272288 clear_blockade : Optional [Blockade ] = None
273289 for blockade in blockades :
274- if not self ._is_inside (human_x , human_y , blockade .get_apexes ()):
290+ blockade_apexes = blockade .get_apexes ()
291+ if blockade_apexes is None or not self ._is_inside (
292+ human_x , human_y , blockade_apexes
293+ ):
275294 continue
276295
277296 distance = self ._get_distance (police_x , police_y , human_x , human_y )
@@ -373,7 +392,10 @@ def _get_distance(self, x1: float, y1: float, x2: float, y2: float) -> float:
373392 def _is_intersecting_area (
374393 self , agent_x : float , agent_y : float , point_x : float , point_y : float , area : Area
375394 ) -> bool :
376- for edge in area .get_edges ():
395+ edges = area .get_edges ()
396+ if edges is None :
397+ return False
398+ for edge in edges :
377399 start_x = edge .get_start_x ()
378400 start_y = edge .get_start_y ()
379401 end_x = edge .get_end_x ()
@@ -483,11 +505,13 @@ def _get_move_points(self, road: Road) -> set[tuple[float, float]]:
483505 if self ._is_inside (mid_x , mid_y , apex ):
484506 points .add ((mid_x , mid_y ))
485507
486- for edge in road .get_edges ():
487- mid_x = (edge .get_start_x () + edge .get_end_x ()) / 2.0
488- mid_y = (edge .get_start_y () + edge .get_end_y ()) / 2.0
489- if (mid_x , mid_y ) in points :
490- points .remove ((mid_x , mid_y ))
508+ edges = road .get_edges ()
509+ if edges is not None :
510+ for edge in edges :
511+ mid_x = (edge .get_start_x () + edge .get_end_x ()) / 2.0
512+ mid_y = (edge .get_start_y () + edge .get_end_y ()) / 2.0
513+ if (mid_x , mid_y ) in points :
514+ points .remove ((mid_x , mid_y ))
491515
492516 self ._move_point_cache [road .get_entity_id ()] = points
493517
@@ -518,6 +542,8 @@ def _is_intersecting_blockade(
518542 blockade : Blockade ,
519543 ) -> bool :
520544 apexes = blockade .get_apexes ()
545+ if apexes is None or len (apexes ) < 4 :
546+ return False
521547 for i in range (0 , len (apexes ) - 3 , 2 ):
522548 line1 = LineString (
523549 [(apexes [i ], apexes [i + 1 ]), (apexes [i + 2 ], apexes [i + 3 ])]
@@ -532,6 +558,8 @@ def _is_intersecting_blockades(
532558 ) -> bool :
533559 apexes1 = blockade1 .get_apexes ()
534560 apexes2 = blockade2 .get_apexes ()
561+ if apexes1 is None or apexes2 is None or len (apexes1 ) < 4 or len (apexes2 ) < 4 :
562+ return False
535563 for i in range (0 , len (apexes1 ) - 2 , 2 ):
536564 for j in range (0 , len (apexes2 ) - 2 , 2 ):
537565 line1 = LineString (
@@ -593,20 +621,26 @@ def _get_area_clear_action(
593621 if min_distance < self ._clear_distance :
594622 return ActionClear (clear_blockade )
595623 else :
596- return ActionMove (
597- [police_entity .get_position ()],
598- clear_blockade .get_x (),
599- clear_blockade .get_y (),
600- )
624+ position = police_entity .get_position ()
625+ if position is not None :
626+ return ActionMove (
627+ [position ],
628+ clear_blockade .get_x (),
629+ clear_blockade .get_y (),
630+ )
601631
602632 agent_x = police_entity .get_x ()
603633 agent_y = police_entity .get_y ()
634+ if agent_x is None or agent_y is None :
635+ return None
604636 clear_blockade = None
605637 min_point_distance = sys .float_info .max
606638 clear_x = 0
607639 clear_y = 0
608640 for blockade in blockades :
609641 apexes = blockade .get_apexes ()
642+ if apexes is None or len (apexes ) < 4 :
643+ continue
610644 for i in range (0 , len (apexes ) - 2 , 2 ):
611645 distance = self ._get_distance (
612646 agent_x , agent_y , apexes [i ], apexes [i + 1 ]
@@ -625,7 +659,9 @@ def _get_area_clear_action(
625659 clear_x = int (agent_x + vector [0 ])
626660 clear_y = int (agent_y + vector [1 ])
627661 return ActionClearArea (clear_x , clear_y )
628- return ActionMove ([police_entity .get_position ()], clear_x , clear_y )
662+ position = police_entity .get_position ()
663+ if position is not None :
664+ return ActionMove ([position ], clear_x , clear_y )
629665
630666 return None
631667
@@ -637,7 +673,12 @@ def _get_neighbour_position_action(
637673 ) -> Optional [Action ]:
638674 agent_x = police_entity .get_x ()
639675 agent_y = police_entity .get_y ()
640- position = self .world_info .get_entity (police_entity .get_position ())
676+ if agent_x is None or agent_y is None :
677+ return None
678+ position_id = police_entity .get_position ()
679+ if position_id is None :
680+ return None
681+ position = self .world_info .get_entity (position_id )
641682 if position is None :
642683 return None
643684
@@ -646,7 +687,7 @@ def _get_neighbour_position_action(
646687 return None
647688
648689 if isinstance (position , Road ):
649- road = cast ( Road , position )
690+ road = position
650691 if road .get_blockades () != []:
651692 mid_x = (edge .get_start_x () + edge .get_end_x ()) / 2.0
652693 mid_y = (edge .get_start_y () + edge .get_end_y ()) / 2.0
@@ -716,7 +757,7 @@ def _get_neighbour_position_action(
716757 return action_move
717758
718759 if isinstance (target , Road ):
719- road = cast ( Road , target )
760+ road = target
720761 if road .get_blockades () == []:
721762 return ActionMove ([position .get_entity_id (), target .get_entity_id ()])
722763
@@ -726,6 +767,8 @@ def _get_neighbour_position_action(
726767 clear_y = 0
727768 for blockade in self .world_info .get_blockades (road ):
728769 apexes = blockade .get_apexes ()
770+ if apexes is None or len (apexes ) < 4 :
771+ continue
729772 for i in range (0 , len (apexes ) - 2 , 2 ):
730773 distance = self ._get_distance (
731774 agent_x , agent_y , apexes [i ], apexes [i + 1 ]
0 commit comments