hedgewars/uAIThinkStack.pas
author displacer
Sat, 27 Jan 2007 17:41:08 +0000
changeset 372 4bac77f8cd38
parent 371 731ad6d27bd1
permissions -rw-r--r--
network colors (alpha)
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
369
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    21
uses uAIActions, uGears, uFloat;
191
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
371
731ad6d27bd1 integer -> LongInt
unc0rr
parents: 369
diff changeset
    35
function  Push(Ticks: Longword; const Actions: TActions; const Me: TGear; Dir: LongInt): boolean;
369
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    36
function  Pop(var Ticks: Longword; var Actions: TActions; var Me: TGear): boolean;
191
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
371
731ad6d27bd1 integer -> LongInt
unc0rr
parents: 369
diff changeset
    42
function Push(Ticks: Longword; const Actions: TActions; const Me: TGear; Dir: LongInt): boolean;
369
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    43
var Result: boolean;
191
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    44
begin
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    45
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
    46
if Result then
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    47
   with ThinkStack.States[ThinkStack.Count] do
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    48
        begin
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    49
        WastedTicks:= Ticks;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    50
        MadeActions:= Actions;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    51
        Hedgehog:= Me;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    52
        Hedgehog.Message:= Dir;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    53
        inc(ThinkStack.Count)
369
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    54
        end;
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    55
Push:= Result
191
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    56
end;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    57
369
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    58
function  Pop(var Ticks: Longword; var Actions: TActions; var Me: TGear): boolean;
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    59
var Result: boolean;
191
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
Result:= ThinkStack.Count > 0;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    62
if Result then
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
   dec(ThinkStack.Count);
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    65
   with ThinkStack.States[ThinkStack.Count] do
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    66
        begin
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    67
        Ticks:= WastedTicks;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    68
        Actions:= MadeActions;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    69
        Me:= Hedgehog
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    70
        end
369
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    71
   end;
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    72
Pop:= Result
191
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    73
end;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    74
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    75
function PosInThinkStack(Me: PGear): boolean;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    76
var i: Longword;
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
i:= 0;
369
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    79
while (i < ThinkStack.Count) do
191
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    80
      begin
369
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    81
      if (not (2 < hwAbs(ThinkStack.States[i].Hedgehog.X - Me^.X) +
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    82
                   hwAbs(ThinkStack.States[i].Hedgehog.Y - Me^.Y)))
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    83
          and (ThinkStack.States[i].Hedgehog.Message = Me^.Message) then exit(true);
191
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    84
      inc(i)
369
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    85
      end;
2aed85310727 AI compiles, but doesn't work
unc0rr
parents: 191
diff changeset
    86
PosInThinkStack:= false
191
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    87
end;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    88
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    89
procedure ClearThinkStack;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    90
begin
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    91
ThinkStack.Count:= 0
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    92
end;
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    93
a03c2d037e24 Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
diff changeset
    94
end.