77#if SERVER
88public class ServerKnockbackStabilizer
99{
10- public void StabilizeKnockbacks ( Dictionary < ActorData , ServerKnockbackManager . KnockbackHits > incomingKnockbacks , List < BoardSquare > additionalInvalidSquares )
10+ public void StabilizeKnockbacks (
11+ Dictionary < ActorData , ServerKnockbackManager . KnockbackHits > incomingKnockbacks ,
12+ List < BoardSquare > additionalInvalidSquares )
1113 {
12- bool flag = true ;
13- while ( flag )
14+ while ( StabilizeKnockbacksForValidDestination ( incomingKnockbacks , additionalInvalidSquares )
15+ || StabilizeKnockbacksVsObstacles ( incomingKnockbacks )
16+ || StabilizeKnockbacksVsStationaries ( incomingKnockbacks )
17+ || StabilizeKnockbacksVsKnockbackees ( incomingKnockbacks ) )
1418 {
15- bool flag2 = StabilizeKnockbacksForValidDestination ( incomingKnockbacks , additionalInvalidSquares ) ;
16- if ( ! flag2 )
17- {
18- flag2 = StabilizeKnockbacksVsObstacles ( incomingKnockbacks ) ;
19- }
20- if ( ! flag2 )
21- {
22- flag2 = StabilizeKnockbacksVsStationaries ( incomingKnockbacks ) ;
23- }
24- if ( ! flag2 )
25- {
26- flag2 = StabilizeKnockbacksVsKnockbackees ( incomingKnockbacks ) ;
27- }
28- flag = flag2 ;
2919 }
3020 }
3121
32- private bool StabilizeKnockbacksForValidDestination ( Dictionary < ActorData , ServerKnockbackManager . KnockbackHits > incomingKnockbacks , List < BoardSquare > additionalInvalidSquares )
22+ private bool StabilizeKnockbacksForValidDestination (
23+ Dictionary < ActorData , ServerKnockbackManager . KnockbackHits > incomingKnockbacks ,
24+ List < BoardSquare > additionalInvalidSquares )
3325 {
3426 bool flag = false ;
3527 foreach ( KeyValuePair < ActorData , ServerKnockbackManager . KnockbackHits > knockbackEntry in incomingKnockbacks )
@@ -39,35 +31,42 @@ private bool StabilizeKnockbacksForValidDestination(Dictionary<ActorData, Server
3931 return flag ;
4032 }
4133
42- private bool StabilizeKnockbackEntryForValidDestination ( KeyValuePair < ActorData , ServerKnockbackManager . KnockbackHits > knockbackEntry , List < BoardSquare > additionalInvalidSquares )
34+ private bool StabilizeKnockbackEntryForValidDestination (
35+ KeyValuePair < ActorData , ServerKnockbackManager . KnockbackHits > knockbackEntry ,
36+ List < BoardSquare > additionalInvalidSquares )
4337 {
44- bool result = false ;
45- BoardSquarePathInfo boardSquarePathInfo = knockbackEntry . Value . GetKnockbackPath ( ) . GetPathEndpoint ( ) ;
46- BoardSquarePathInfo boardSquarePathInfo2 = boardSquarePathInfo ;
47- bool flag = boardSquarePathInfo . square != null && boardSquarePathInfo . square . IsValidForGameplay ( ) && ! additionalInvalidSquares . Contains ( boardSquarePathInfo . square ) ;
48- while ( ! flag )
38+ BoardSquarePathInfo endpoint = knockbackEntry . Value . GetKnockbackPath ( ) . GetPathEndpoint ( ) ;
39+ BoardSquarePathInfo initialEndpoint = endpoint ;
40+ bool isValidEndpoint = endpoint . square != null
41+ && endpoint . square . IsValidForGameplay ( )
42+ && ! additionalInvalidSquares . Contains ( endpoint . square ) ;
43+ while ( ! isValidEndpoint )
4944 {
50- if ( boardSquarePathInfo . prev == null )
45+ if ( endpoint . prev == null )
5146 {
52- flag = true ;
47+ isValidEndpoint = true ;
5348 }
5449 else
5550 {
56- boardSquarePathInfo . square = null ;
57- boardSquarePathInfo . prev . next = null ;
58- boardSquarePathInfo = boardSquarePathInfo . prev ;
59- flag = ( boardSquarePathInfo . square != null && boardSquarePathInfo . square . IsValidForGameplay ( ) ) ;
51+ endpoint . square = null ;
52+ endpoint . prev . next = null ;
53+ endpoint = endpoint . prev ;
54+ isValidEndpoint = endpoint . square != null && endpoint . square . IsValidForGameplay ( ) ;
6055 }
6156 }
62- if ( boardSquarePathInfo2 != boardSquarePathInfo )
57+ if ( initialEndpoint != endpoint )
6358 {
64- knockbackEntry . Value . OnKnockbackPathStabilized ( boardSquarePathInfo . square ) ;
65- result = true ;
59+ knockbackEntry . Value . OnKnockbackPathStabilized ( endpoint . square ) ;
60+ return true ;
61+ }
62+ else
63+ {
64+ return false ;
6665 }
67- return result ;
6866 }
6967
70- private bool StabilizeKnockbacksVsObstacles ( Dictionary < ActorData , ServerKnockbackManager . KnockbackHits > incomingKnockbacks )
68+ private bool StabilizeKnockbacksVsObstacles (
69+ Dictionary < ActorData , ServerKnockbackManager . KnockbackHits > incomingKnockbacks )
7170 {
7271 bool flag = false ;
7372 foreach ( KeyValuePair < ActorData , ServerKnockbackManager . KnockbackHits > knockbackEntry in incomingKnockbacks )
@@ -77,112 +76,122 @@ private bool StabilizeKnockbacksVsObstacles(Dictionary<ActorData, ServerKnockbac
7776 return flag ;
7877 }
7978
80- private bool StabilizeKnockbackEntryVsObstacles ( KeyValuePair < ActorData , ServerKnockbackManager . KnockbackHits > knockbackEntry )
79+ private bool StabilizeKnockbackEntryVsObstacles (
80+ KeyValuePair < ActorData , ServerKnockbackManager . KnockbackHits > knockbackEntry )
8181 {
8282 bool result = false ;
8383 ActorData key = knockbackEntry . Key ;
84- BoardSquarePathInfo boardSquarePathInfo = knockbackEntry . Value . GetKnockbackPath ( ) ;
85- BoardSquarePathInfo boardSquarePathInfo2 = boardSquarePathInfo . next ;
86- BoardSquarePathInfo prev = boardSquarePathInfo . prev ;
87- while ( boardSquarePathInfo2 != null )
84+ BoardSquarePathInfo step = knockbackEntry . Value . GetKnockbackPath ( ) ;
85+ BoardSquarePathInfo next = step . next ;
86+ BoardSquarePathInfo prev = step . prev ;
87+ while ( next != null )
8888 {
89- bool flag = ( prev != null && BarrierManager . Get ( ) != null && BarrierManager . Get ( ) . IsMovementBlockedOnCrossover ( key , prev . square , boardSquarePathInfo . square ) ) || ( BarrierManager . Get ( ) != null && BarrierManager . Get ( ) . IsMovementBlocked ( key , boardSquarePathInfo . square , boardSquarePathInfo2 . square ) ) ;
90- if ( flag )
89+ if ( ( prev != null
90+ && BarrierManager . Get ( ) != null
91+ && BarrierManager . Get ( ) . IsMovementBlockedOnCrossover ( key , prev . square , step . square ) )
92+ || ( BarrierManager . Get ( ) != null
93+ && BarrierManager . Get ( ) . IsMovementBlocked ( key , step . square , next . square ) ) )
9194 {
92- boardSquarePathInfo . next = null ;
93- boardSquarePathInfo2 . prev = null ;
94- boardSquarePathInfo2 = null ;
95- knockbackEntry . Value . OnKnockbackPathStabilized ( boardSquarePathInfo . square ) ;
95+ step . next = null ;
96+ next . prev = null ;
97+ next = null ;
98+ knockbackEntry . Value . OnKnockbackPathStabilized ( step . square ) ;
9699 result = true ;
97100 }
98101 else
99102 {
100- boardSquarePathInfo = boardSquarePathInfo2 ;
101- prev = boardSquarePathInfo2 . prev ;
102- boardSquarePathInfo2 = boardSquarePathInfo2 . next ;
103+ step = next ;
104+ prev = next . prev ;
105+ next = next . next ;
103106 }
104107 }
105108 return result ;
106109 }
107110
108- private bool StabilizeKnockbacksVsStationaries ( Dictionary < ActorData , ServerKnockbackManager . KnockbackHits > incomingKnockbacks )
111+ private bool StabilizeKnockbacksVsStationaries (
112+ Dictionary < ActorData , ServerKnockbackManager . KnockbackHits > incomingKnockbacks )
109113 {
110114 bool result = false ;
111115 foreach ( ActorData actorData in GetStationaryActorsWrtKnockback ( incomingKnockbacks ) )
112116 {
113- if ( ! actorData . IsDead ( ) )
117+ if ( actorData . IsDead ( ) )
114118 {
115- BoardSquare currentBoardSquare = actorData . GetCurrentBoardSquare ( ) ;
116- foreach ( KeyValuePair < ActorData , ServerKnockbackManager . KnockbackHits > keyValuePair in incomingKnockbacks )
119+ continue ;
120+ }
121+ BoardSquare currentBoardSquare = actorData . GetCurrentBoardSquare ( ) ;
122+ foreach ( KeyValuePair < ActorData , ServerKnockbackManager . KnockbackHits > keyValuePair in incomingKnockbacks )
123+ {
124+ ServerKnockbackManager . KnockbackHits value = keyValuePair . Value ;
125+ if ( value . GetKnockbackEndSquare ( ) == currentBoardSquare )
117126 {
118- ServerKnockbackManager . KnockbackHits value = keyValuePair . Value ;
119- if ( value . GetKnockbackEndSquare ( ) == currentBoardSquare )
127+ BoardSquarePathInfo boardSquarePathInfo = value . GetKnockbackPath ( ) . BackUpOnceFromEnd ( ) ;
128+ if ( value . GetKnockbackEndSquare ( ) != boardSquarePathInfo . square )
129+ {
130+ value . OnKnockbackPathStabilized ( boardSquarePathInfo . square ) ;
131+ result = true ;
132+ }
133+ else
120134 {
121- BoardSquarePathInfo boardSquarePathInfo = value . GetKnockbackPath ( ) . BackUpOnceFromEnd ( ) ;
122- if ( value . GetKnockbackEndSquare ( ) != boardSquarePathInfo . square )
123- {
124- value . OnKnockbackPathStabilized ( boardSquarePathInfo . square ) ;
125- result = true ;
126- }
127- else
128- {
129- Log . Error ( $ "Failed to back up { keyValuePair . Key } in StabilizeKnockbacksVsStationaries.") ;
130- }
135+ Log . Error ( $ "Failed to back up { keyValuePair . Key } in StabilizeKnockbacksVsStationaries.") ;
131136 }
132137 }
133138 }
134139 }
135140 return result ;
136141 }
137142
138- private bool StabilizeKnockbacksVsKnockbackees ( Dictionary < ActorData , ServerKnockbackManager . KnockbackHits > incomingKnockbacks )
143+ private bool StabilizeKnockbacksVsKnockbackees (
144+ Dictionary < ActorData , ServerKnockbackManager . KnockbackHits > incomingKnockbacks )
139145 {
140- bool flag = false ;
141- List < KeyValuePair < ActorData , ServerKnockbackManager . KnockbackHits > > list = incomingKnockbacks . ToList < KeyValuePair < ActorData , ServerKnockbackManager . KnockbackHits > > ( ) ;
142- list . Sort ( delegate ( KeyValuePair < ActorData , ServerKnockbackManager . KnockbackHits > x , KeyValuePair < ActorData , ServerKnockbackManager . KnockbackHits > y )
146+ List < KeyValuePair < ActorData , ServerKnockbackManager . KnockbackHits > > incomingKnockbacksSorted = incomingKnockbacks . ToList ( ) ;
147+ incomingKnockbacksSorted . Sort ( ( x , y ) =>
143148 {
144- float num2 = x . Value . GetKnockbackEndSquare ( ) . HorizontalDistanceOnBoardTo ( x . Key . GetCurrentBoardSquare ( ) ) ;
145- float value3 = y . Value . GetKnockbackEndSquare ( ) . HorizontalDistanceOnBoardTo ( y . Key . GetCurrentBoardSquare ( ) ) ;
146- return num2 . CompareTo ( value3 ) ;
149+ float xDist = x . Value . GetKnockbackEndSquare ( ) . HorizontalDistanceOnBoardTo ( x . Key . GetCurrentBoardSquare ( ) ) ;
150+ float yDist = y . Value . GetKnockbackEndSquare ( ) . HorizontalDistanceOnBoardTo ( y . Key . GetCurrentBoardSquare ( ) ) ;
151+ return xDist . CompareTo ( yDist ) ;
147152 } ) ;
148- int num = 0 ;
149- while ( num < list . Count && ! flag )
153+
154+ bool stabilized = false ;
155+ for ( int i = 0 ; i < incomingKnockbacksSorted . Count ; i ++ )
150156 {
151- ServerKnockbackManager . KnockbackHits value = list [ num ] . Value ;
152- for ( int i = num + 1 ; i < list . Count ; i ++ )
157+ ServerKnockbackManager . KnockbackHits knockback1 = incomingKnockbacksSorted [ i ] . Value ;
158+ for ( int j = i + 1 ; j < incomingKnockbacksSorted . Count ; j ++ )
153159 {
154- KeyValuePair < ActorData , ServerKnockbackManager . KnockbackHits > keyValuePair = list [ i ] ;
155- ServerKnockbackManager . KnockbackHits value2 = keyValuePair . Value ;
156- if ( value . GetKnockbackEndSquare ( ) == value2 . GetKnockbackEndSquare ( ) )
160+ ServerKnockbackManager . KnockbackHits knockback2 = incomingKnockbacksSorted [ j ] . Value ;
161+ if ( knockback1 . GetKnockbackEndSquare ( ) == knockback2 . GetKnockbackEndSquare ( ) )
157162 {
158- BoardSquarePathInfo boardSquarePathInfo = value2 . GetKnockbackPath ( ) . BackUpOnceFromEnd ( ) ;
159- if ( boardSquarePathInfo . square != value2 . GetKnockbackEndSquare ( ) )
163+ BoardSquarePathInfo stepBack = knockback2 . GetKnockbackPath ( ) . BackUpOnceFromEnd ( ) ;
164+ if ( stepBack . square != knockback2 . GetKnockbackEndSquare ( ) )
160165 {
161- value2 . OnKnockbackPathStabilized ( boardSquarePathInfo . square ) ;
162- flag = true ;
166+ knockback2 . OnKnockbackPathStabilized ( stepBack . square ) ;
167+ stabilized = true ;
163168 }
164169 else
165170 {
166- Log . Error ( $ "Failed to back up { keyValuePair . Key } in StabilizeKnockbacksVsKnockbackees.") ;
171+ Log . Error ( $ "Failed to back up { incomingKnockbacksSorted [ j ] . Key } in StabilizeKnockbacksVsKnockbackees.") ;
167172 }
168173 }
169174 }
170- num ++ ;
175+ if ( stabilized )
176+ {
177+ return true ;
178+ }
171179 }
172- return flag ;
180+ return false ;
173181 }
174182
175- private List < ActorData > GetStationaryActorsWrtKnockback ( Dictionary < ActorData , ServerKnockbackManager . KnockbackHits > incomingKnockbacks )
183+ private List < ActorData > GetStationaryActorsWrtKnockback (
184+ Dictionary < ActorData , ServerKnockbackManager . KnockbackHits > incomingKnockbacks )
176185 {
177- List < ActorData > list = new List < ActorData > ( GameFlowData . Get ( ) . GetActors ( ) ) ;
186+ List < ActorData > actors = new List < ActorData > ( GameFlowData . Get ( ) . GetActors ( ) ) ;
178187 foreach ( ActorData item in incomingKnockbacks . Keys )
179188 {
180- if ( list . Contains ( item ) )
189+ if ( actors . Contains ( item ) )
181190 {
182- list . Remove ( item ) ;
191+ actors . Remove ( item ) ;
183192 }
184193 }
185- return list ;
194+ return actors ;
186195 }
187196}
188197#endif
0 commit comments