hedgewars/uAIActions.pas
author unc0rr
Tue, 20 Jun 2006 21:18:49 +0000
changeset 66 9643d75baf1e
parent 64 9df467527ae5
child 70 82d93eeecebe
permissions -rw-r--r--
Many AI improvements, bots do think in separate thread
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     1
unit uAIActions;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     2
interface
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
     3
uses uGears;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
     4
const MAXACTIONS = 256;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
     5
      aia_none       = 0;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     6
      aia_Left       = 1;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     7
      aia_Right      = 2;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     8
      aia_Timer      = 3;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     9
      aia_Slot       = 4;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    10
      aia_attack     = 5;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    11
      aia_Up         = 6;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    12
      aia_Down       = 7;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    13
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    14
      aia_Weapon     = $80000000;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    15
      aia_WaitX      = $80000001;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    16
      aia_WaitY      = $80000002;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    17
      aia_LookLeft   = $80000003;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    18
      aia_LookRight  = $80000004;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    19
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    20
      aim_push       = $80000000;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    21
      aim_release    = $80000001;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    22
      ai_specmask    = $80000000;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    23
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    24
type TAction = record
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    25
               Action, Param: Longword;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    26
               Time: Longword;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    27
               end;
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    28
     TActions = record
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    29
                Count, Pos: Longword;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    30
                actions: array[0..Pred(MAXACTIONS)] of TAction;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    31
                Score: integer;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    32
                end;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    33
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    34
procedure AddAction(var Actions: TActions; Action, Param, TimeDelta: Longword);
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    35
procedure ProcessAction(var Actions: TActions; Me: PGear);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    36
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    37
implementation
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    38
uses uMisc, uTeams, uConsts, uConsole;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    39
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    40
const ActionIdToStr: array[0..7] of string[16] = (
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    41
{aia_none}           '',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    42
{aia_Left}           'left',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    43
{aia_Right}          'right',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    44
{aia_Timer}          'timer',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    45
{aia_slot}           'slot',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    46
{aia_attack}         'attack',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    47
{aia_Up}             'up',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    48
{aia_Down}           'down'
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    49
                     );
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    50
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    51
procedure AddAction(var Actions: TActions; Action, Param, TimeDelta: Longword);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    52
begin
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    53
with Actions do
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    54
     begin
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    55
     actions[Count].Action:= Action;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    56
     actions[Count].Param:= Param;
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    57
     if Count > 0 then actions[Count].Time:= TimeDelta
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    58
                  else actions[Count].Time:= GameTicks + TimeDelta;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    59
     inc(Count);
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    60
     TryDo(Count < MAXACTIONS, 'AI: actions overflow', true);
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    61
     end
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    62
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    63
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    64
procedure SetWeapon(weap: Longword);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    65
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    66
with CurrentTeam^ do
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    67
     with Hedgehogs[CurrHedgehog] do
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    68
          while Ammo[CurSlot, CurAmmo].AmmoType <> TAmmotype(weap) do
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    69
                ParseCommand('/slot ' + chr(49 + Ammoz[TAmmoType(weap)].Slot));
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    70
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    71
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    72
procedure ProcessAction(var Actions: TActions; Me: PGear);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    73
var s: shortstring;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    74
begin
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    75
if Actions.Pos >= Actions.Count then exit;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    76
with Actions.actions[Actions.Pos] do
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    77
     begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    78
     if Time > GameTicks then exit;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    79
     if (Action and ai_specmask) <> 0 then
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    80
        case Action of
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    81
           aia_Weapon: SetWeapon(Param);
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    82
            aia_WaitX: if round(Me.X) = Param then Time:= GameTicks
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    83
                                              else exit;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    84
            aia_WaitY: if round(Me.Y) = Param then Time:= GameTicks
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    85
                                              else exit;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    86
         aia_LookLeft: if Me.dX >= 0 then
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    87
                          begin
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    88
                          ParseCommand('+left');
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    89
                          exit
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    90
                          end else ParseCommand('-left');
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    91
        aia_LookRight: if Me.dX < 0 then
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    92
                          begin
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    93
                          ParseCommand('+right');
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    94
                          exit
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    95
                          end else ParseCommand('-right');
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    96
             end else
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    97
        begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    98
        s:= ActionIdToStr[Action];
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    99
        if (Param and ai_specmask) <> 0 then
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   100
           case Param of
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   101
             aim_push: s:= '+' + s;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   102
          aim_release: s:= '-' + s;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   103
             end
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   104
          else if Param <> 0 then s:= s + ' ' + inttostr(Param);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   105
        ParseCommand(s)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   106
        end
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   107
     end;
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   108
inc(Actions.Pos);
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   109
if Actions.Pos <= Actions.Count then
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   110
   inc(Actions.actions[Actions.Pos].Time, GameTicks)
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   111
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   112
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   113
end.