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