@@ -505,13 +505,20 @@ fn explosively_depressurize(initial_index: TurfID, equalize_hard_turf_limit: usi
505505 Ok ( ( ) )
506506}
507507
508+ enum FloodFillResult {
509+ ZoneIgnored ,
510+ Overtime ,
511+ Complete ( DiGraphMap < NodeIndex , Cell < f32 > > , f32 ) ,
512+ }
513+
508514#[ cfg_attr( feature = "tracy" , tracing:: instrument( skip_all) ) ]
509515fn flood_fill_zones (
510516 ( index_node, index_turf) : ( NodeIndex , TurfID ) ,
511517 equalize_hard_turf_limit : usize ,
512518 found_turfs : & mut HashSet < TurfID , FxBuildHasher > ,
513519 arena : & TurfGases ,
514- ) -> Option < ( DiGraphMap < NodeIndex , Cell < f32 > > , f32 ) > {
520+ ( start_time, remaining_time) : ( & Instant , Duration ) ,
521+ ) -> FloodFillResult {
515522 let mut turf_graph: DiGraphMap < NodeIndex , Cell < f32 > > = Default :: default ( ) ;
516523 let mut border_turfs: std:: collections:: VecDeque < NodeIndex > = Default :: default ( ) ;
517524 let sender = byond_callback_sender ( ) ;
@@ -533,6 +540,11 @@ fn flood_fill_zones(
533540 }
534541 total_moles += cur_turf. total_moles ( ) ;
535542
543+ //we are already overtime, bail NOW
544+ if start_time. elapsed ( ) >= remaining_time {
545+ return FloodFillResult :: Overtime ;
546+ }
547+
536548 for ( weight, adj_index, adj_mixture) in arena
537549 . graph
538550 . edges ( cur_index)
@@ -572,7 +584,11 @@ fn flood_fill_zones(
572584 }
573585 }
574586 }
575- ( !ignore_zone) . then_some ( ( turf_graph, total_moles) )
587+ if !ignore_zone {
588+ FloodFillResult :: Complete ( turf_graph, total_moles)
589+ } else {
590+ FloodFillResult :: ZoneIgnored
591+ }
576592}
577593
578594#[ cfg_attr( feature = "tracy" , tracing:: instrument( skip_all) ) ]
@@ -765,47 +781,54 @@ fn equalize(
765781 let turfs_processed: AtomicUsize = AtomicUsize :: new ( 0 ) ;
766782 let is_cancelled = with_turf_gases_read ( |arena| {
767783 let mut found_turfs: HashSet < TurfID , FxBuildHasher > = Default :: default ( ) ;
768- let zoned_turfs = high_pressure_turfs
769- . iter ( )
770- . filter_map ( |& cur_index_turf| {
771- //is this turf already visited?
772- if found_turfs. contains ( & cur_index_turf) {
773- return None ;
774- } ;
784+ let mut zoned_turfs = vec ! [ ] ;
785+ for & cur_index_turf in high_pressure_turfs {
786+ //is this turf already visited?
787+ if found_turfs. contains ( & cur_index_turf) {
788+ continue ;
789+ } ;
775790
776- //does this turf exists/enabled/have adjacencies?
777- let cur_mixture = arena. get_from_id ( cur_index_turf) ?;
778- let cur_index_node = arena. get_id ( cur_index_turf) ?;
779- if !cur_mixture. enabled ( )
780- || arena. adjacent_node_ids ( cur_index_node) . next ( ) . is_none ( )
781- {
782- return None ;
783- }
791+ //does this turf exists/enabled/have adjacencies?
792+ let Some ( cur_mixture) = arena. get_from_id ( cur_index_turf) else {
793+ continue ;
794+ } ;
795+ let Some ( cur_index_node) = arena. get_id ( cur_index_turf) else {
796+ continue ;
797+ } ;
798+ if !cur_mixture. enabled ( ) || arena. adjacent_node_ids ( cur_index_node) . next ( ) . is_none ( ) {
799+ continue ;
800+ }
784801
785- let is_unshareable = GasArena :: with_all_mixtures ( |all_mixtures| {
786- let our_moles = all_mixtures[ cur_mixture. mix ] . read ( ) . total_moles ( ) ;
787- our_moles < 10.0
788- || arena
789- . adjacent_mixes ( cur_index_node, all_mixtures)
790- . all ( |lock| {
791- ( lock. read ( ) . total_moles ( ) - our_moles) . abs ( )
792- < MINIMUM_MOLES_DELTA_TO_MOVE
793- } )
794- } ) ;
795-
796- //does this turf or its adjacencies have enough moles to share?
797- if is_unshareable {
798- return None ;
799- }
802+ let is_unshareable = GasArena :: with_all_mixtures ( |all_mixtures| {
803+ let our_moles = all_mixtures[ cur_mixture. mix ] . read ( ) . total_moles ( ) ;
804+ our_moles < 10.0
805+ || arena
806+ . adjacent_mixes ( cur_index_node, all_mixtures)
807+ . all ( |lock| {
808+ ( lock. read ( ) . total_moles ( ) - our_moles) . abs ( )
809+ < MINIMUM_MOLES_DELTA_TO_MOVE
810+ } )
811+ } ) ;
812+
813+ //does this turf or its adjacencies have enough moles to share?
814+ if is_unshareable {
815+ continue ;
816+ }
800817
801- flood_fill_zones (
802- ( cur_index_node, cur_index_turf) ,
803- equalize_hard_turf_limit,
804- & mut found_turfs,
805- arena,
806- )
807- } )
808- . collect :: < Vec < _ > > ( ) ;
818+ match flood_fill_zones (
819+ ( cur_index_node, cur_index_turf) ,
820+ equalize_hard_turf_limit,
821+ & mut found_turfs,
822+ arena,
823+ ( start_time, remaining_time) ,
824+ ) {
825+ FloodFillResult :: Complete ( zone, num) => {
826+ zoned_turfs. push ( ( zone, num) ) ;
827+ }
828+ FloodFillResult :: Overtime => return true ,
829+ FloodFillResult :: ZoneIgnored => ( ) ,
830+ }
831+ }
809832
810833 if start_time. elapsed ( ) >= remaining_time {
811834 return true ;
0 commit comments