@@ -150,25 +150,8 @@ private GoapResult SolveAstar(GoapState stateCurrent, ConditionInterface goal, i
150
150
// if arrived at goal...
151
151
if ( goal . IsSatisfied ( current . state ) )
152
152
{
153
- // ...return the Action path
154
-
155
- // action list
156
- List < GoapAction > actions = new List < GoapAction > ( ) ;
157
- while ( current . action != null )
158
- {
159
- actions . Add ( current . action ?? throw new InvalidOperationException ( ) ) ;
160
- current = current . parent ;
161
- }
162
- actions . Reverse ( ) ;
163
-
164
- // create GoapResult
165
- GoapResult result = new GoapResult (
166
- actions . ToArray ( ) ,
167
- current . currentCost ,
168
- true
169
- ) ;
170
-
171
- return result ;
153
+ // ...early return result
154
+ return CreateSuccessResult ( current ) ;
172
155
}
173
156
174
157
// close the current node
@@ -201,6 +184,26 @@ private GoapResult SolveAstar(GoapState stateCurrent, ConditionInterface goal, i
201
184
}
202
185
}
203
186
187
+ return CreateFailureResult ( ) ;
188
+ }
189
+
190
+ private GoapResult CreateSuccessResult ( AstarQueue latestQueue )
191
+ {
192
+ // action list
193
+ List < GoapAction > actions = new List < GoapAction > ( ) ;
194
+ while ( latestQueue . action != null )
195
+ {
196
+ actions . Add ( latestQueue . action ?? throw new InvalidOperationException ( ) ) ;
197
+ latestQueue = latestQueue . parent ;
198
+ }
199
+ actions . Reverse ( ) ;
200
+
201
+ // create GoapResult
202
+ return new GoapResult ( actions . ToArray ( ) , latestQueue . currentCost , true ) ;
203
+ }
204
+
205
+ private GoapResult CreateFailureResult ( )
206
+ {
204
207
return new GoapResult ( null , - 1 , false ) ;
205
208
}
206
209
0 commit comments