hedgewars/uAIThinkStack.pas
author unc0rr
Mon, 16 Oct 2006 19:20:13 +0000
changeset 200 a36102728776
parent 191 a03c2d037e24
child 369 2aed85310727
permissions -rw-r--r--
Small fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
191
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
     1
(*
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
     2
 * Hedgewars, a worms-like game
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2006 Andrey Korotaev <unC0Rr@gmail.com>
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
     4
 *
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
     8
 *
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    12
 * GNU General Public License for more details.
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    13
 *
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    17
 *)
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    18
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    19
unit uAIThinkStack;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    20
interface
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    21
uses uAIActions, uGears;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    22
{$INCLUDE options.inc}
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    23
const cBranchStackSize = 12;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    24
type TStackEntry = record
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    25
                   WastedTicks: Longword;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    26
                   MadeActions: TActions;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    27
                   Hedgehog: TGear;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    28
                   end;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    29
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    30
var ThinkStack: record
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    31
                Count: Longword;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    32
                States: array[0..Pred(cBranchStackSize)] of TStackEntry;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    33
                end;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    34
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    35
function  Push(Ticks: Longword; const Actions: TActions; const Me: TGear; Dir: integer): boolean;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    36
function  Pop(out Ticks: Longword; out Actions: TActions; out Me: TGear): boolean;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    37
function  PosInThinkStack(Me: PGear): boolean;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    38
procedure ClearThinkStack;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    39
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    40
implementation
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    41
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    42
function Push(Ticks: Longword; const Actions: TActions; const Me: TGear; Dir: integer): boolean;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    43
begin
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    44
Result:= (ThinkStack.Count < cBranchStackSize) and (Actions.Count < MAXACTIONS - 5);
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    45
if Result then
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    46
   with ThinkStack.States[ThinkStack.Count] do
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    47
        begin
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    48
        WastedTicks:= Ticks;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    49
        MadeActions:= Actions;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    50
        Hedgehog:= Me;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    51
        Hedgehog.Message:= Dir;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    52
        inc(ThinkStack.Count)
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    53
        end
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    54
end;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    55
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    56
function Pop(out Ticks: Longword; out Actions: TActions; out Me: TGear): boolean;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    57
begin
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    58
Result:= ThinkStack.Count > 0;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    59
if Result then
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    60
   begin
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    61
   dec(ThinkStack.Count);
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    62
   with ThinkStack.States[ThinkStack.Count] do
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    63
        begin
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    64
        Ticks:= WastedTicks;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    65
        Actions:= MadeActions;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    66
        Me:= Hedgehog
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    67
        end
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    68
   end
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    69
end;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    70
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    71
function PosInThinkStack(Me: PGear): boolean;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    72
var i: Longword;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    73
begin
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    74
i:= 0;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    75
Result:= false;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    76
while (i < ThinkStack.Count) and not Result do
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    77
      begin
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    78
      Result:= (abs(ThinkStack.States[i].Hedgehog.X - Me.X) +
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    79
                abs(ThinkStack.States[i].Hedgehog.Y - Me.Y) <= 2)
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    80
                and (ThinkStack.States[i].Hedgehog.Message = Me.Message);
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    81
      inc(i)
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    82
      end
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    83
end;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    84
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    85
procedure ClearThinkStack;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    86
begin
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    87
ThinkStack.Count:= 0
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    88
end;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    89
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    90
end.