hedgewars/uAIThinkStack.pas
changeset 369 2aed85310727
parent 191 a03c2d037e24
child 371 731ad6d27bd1
equal deleted inserted replaced
368:fe71e55d2d7b 369:2aed85310727
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  *)
    17  *)
    18 
    18 
    19 unit uAIThinkStack;
    19 unit uAIThinkStack;
    20 interface
    20 interface
    21 uses uAIActions, uGears;
    21 uses uAIActions, uGears, uFloat;
    22 {$INCLUDE options.inc}
    22 {$INCLUDE options.inc}
    23 const cBranchStackSize = 12;
    23 const cBranchStackSize = 12;
    24 type TStackEntry = record
    24 type TStackEntry = record
    25                    WastedTicks: Longword;
    25                    WastedTicks: Longword;
    26                    MadeActions: TActions;
    26                    MadeActions: TActions;
    31                 Count: Longword;
    31                 Count: Longword;
    32                 States: array[0..Pred(cBranchStackSize)] of TStackEntry;
    32                 States: array[0..Pred(cBranchStackSize)] of TStackEntry;
    33                 end;
    33                 end;
    34 
    34 
    35 function  Push(Ticks: Longword; const Actions: TActions; const Me: TGear; Dir: integer): boolean;
    35 function  Push(Ticks: Longword; const Actions: TActions; const Me: TGear; Dir: integer): boolean;
    36 function  Pop(out Ticks: Longword; out Actions: TActions; out Me: TGear): boolean;
    36 function  Pop(var Ticks: Longword; var Actions: TActions; var Me: TGear): boolean;
    37 function  PosInThinkStack(Me: PGear): boolean;
    37 function  PosInThinkStack(Me: PGear): boolean;
    38 procedure ClearThinkStack;
    38 procedure ClearThinkStack;
    39 
    39 
    40 implementation
    40 implementation
    41 
    41 
    42 function Push(Ticks: Longword; const Actions: TActions; const Me: TGear; Dir: integer): boolean;
    42 function Push(Ticks: Longword; const Actions: TActions; const Me: TGear; Dir: integer): boolean;
       
    43 var Result: boolean;
    43 begin
    44 begin
    44 Result:= (ThinkStack.Count < cBranchStackSize) and (Actions.Count < MAXACTIONS - 5);
    45 Result:= (ThinkStack.Count < cBranchStackSize) and (Actions.Count < MAXACTIONS - 5);
    45 if Result then
    46 if Result then
    46    with ThinkStack.States[ThinkStack.Count] do
    47    with ThinkStack.States[ThinkStack.Count] do
    47         begin
    48         begin
    48         WastedTicks:= Ticks;
    49         WastedTicks:= Ticks;
    49         MadeActions:= Actions;
    50         MadeActions:= Actions;
    50         Hedgehog:= Me;
    51         Hedgehog:= Me;
    51         Hedgehog.Message:= Dir;
    52         Hedgehog.Message:= Dir;
    52         inc(ThinkStack.Count)
    53         inc(ThinkStack.Count)
    53         end
    54         end;
       
    55 Push:= Result
    54 end;
    56 end;
    55 
    57 
    56 function Pop(out Ticks: Longword; out Actions: TActions; out Me: TGear): boolean;
    58 function  Pop(var Ticks: Longword; var Actions: TActions; var Me: TGear): boolean;
       
    59 var Result: boolean;
    57 begin
    60 begin
    58 Result:= ThinkStack.Count > 0;
    61 Result:= ThinkStack.Count > 0;
    59 if Result then
    62 if Result then
    60    begin
    63    begin
    61    dec(ThinkStack.Count);
    64    dec(ThinkStack.Count);
    63         begin
    66         begin
    64         Ticks:= WastedTicks;
    67         Ticks:= WastedTicks;
    65         Actions:= MadeActions;
    68         Actions:= MadeActions;
    66         Me:= Hedgehog
    69         Me:= Hedgehog
    67         end
    70         end
    68    end
    71    end;
       
    72 Pop:= Result
    69 end;
    73 end;
    70 
    74 
    71 function PosInThinkStack(Me: PGear): boolean;
    75 function PosInThinkStack(Me: PGear): boolean;
    72 var i: Longword;
    76 var i: Longword;
    73 begin
    77 begin
    74 i:= 0;
    78 i:= 0;
    75 Result:= false;
    79 while (i < ThinkStack.Count) do
    76 while (i < ThinkStack.Count) and not Result do
       
    77       begin
    80       begin
    78       Result:= (abs(ThinkStack.States[i].Hedgehog.X - Me.X) +
    81       if (not (2 < hwAbs(ThinkStack.States[i].Hedgehog.X - Me^.X) +
    79                 abs(ThinkStack.States[i].Hedgehog.Y - Me.Y) <= 2)
    82                    hwAbs(ThinkStack.States[i].Hedgehog.Y - Me^.Y)))
    80                 and (ThinkStack.States[i].Hedgehog.Message = Me.Message);
    83           and (ThinkStack.States[i].Hedgehog.Message = Me^.Message) then exit(true);
    81       inc(i)
    84       inc(i)
    82       end
    85       end;
       
    86 PosInThinkStack:= false
    83 end;
    87 end;
    84 
    88 
    85 procedure ClearThinkStack;
    89 procedure ClearThinkStack;
    86 begin
    90 begin
    87 ThinkStack.Count:= 0
    91 ThinkStack.Count:= 0