hedgewars/uAIActions.pas
author unc0rr
Wed, 11 Oct 2006 19:07:38 +0000
changeset 194 88652abdce9a
parent 193 56885ea29202
child 195 edae828322a0
permissions -rw-r--r--
Fixed weird bots behavior
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
     1
(*
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
     2
 * Hedgewars, a worms-like game
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
     3
 * Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com>
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
     4
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 165
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
57c2ef19f719 Relicense to GPL
unc0rr
parents: 165
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
57c2ef19f719 Relicense to GPL
unc0rr
parents: 165
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
     8
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 165
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
57c2ef19f719 Relicense to GPL
unc0rr
parents: 165
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57c2ef19f719 Relicense to GPL
unc0rr
parents: 165
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
57c2ef19f719 Relicense to GPL
unc0rr
parents: 165
diff changeset
    12
 * GNU General Public License for more details.
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    13
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 165
diff changeset
    14
 * You should have received a copy of the GNU General Public License
57c2ef19f719 Relicense to GPL
unc0rr
parents: 165
diff changeset
    15
 * along with this program; if not, write to the Free Software
57c2ef19f719 Relicense to GPL
unc0rr
parents: 165
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    17
 *)
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    18
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    19
unit uAIActions;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    20
interface
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    21
uses uGears;
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    22
{$INCLUDE options.inc}
75
d2b737858ff7 - New First Aid powerup
unc0rr
parents: 74
diff changeset
    23
const MAXACTIONS = 96;
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    24
      aia_none       = 0;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    25
      aia_Left       = 1;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    26
      aia_Right      = 2;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    27
      aia_Timer      = 3;
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    28
      aia_attack     = 4;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    29
      aia_Up         = 5;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    30
      aia_Down       = 6;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    31
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    32
      aia_Weapon     = $80000000;
193
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
    33
      aia_WaitXL     = $80000001;
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
    34
      aia_WaitXR     = $80000002;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    35
      aia_LookLeft   = $80000003;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    36
      aia_LookRight  = $80000004;
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    37
      aia_AwareExpl  = $80000005;
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    38
      aia_HJump      = $80000006;
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    39
      aia_LJump      = $80000007;
143
3dacbd83209b - Many fixes to AI
unc0rr
parents: 80
diff changeset
    40
      aia_Skip       = $80000008;
193
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
    41
      aia_Wait       = $80000009;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    42
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    43
      aim_push       = $80000000;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    44
      aim_release    = $80000001;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    45
      ai_specmask    = $80000000;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    46
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    47
type TAction = record
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    48
               Action, Param: Longword;
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    49
               X, Y: integer;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    50
               Time: Longword;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    51
               end;
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    52
     TActions = record
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    53
                Count, Pos: Longword;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    54
                actions: array[0..Pred(MAXACTIONS)] of TAction;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    55
                Score: integer;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    56
                end;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    57
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    58
procedure AddAction(var Actions: TActions; Action, Param, TimeDelta: Longword; const X: integer = 0; Y: integer = 0);
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    59
procedure ProcessAction(var Actions: TActions; Me: PGear);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    60
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    61
implementation
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    62
uses uMisc, uTeams, uConsts, uConsole, uAIMisc;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    63
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    64
const ActionIdToStr: array[0..6] of string[16] = (
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    65
{aia_none}           '',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    66
{aia_Left}           'left',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    67
{aia_Right}          'right',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    68
{aia_Timer}          'timer',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    69
{aia_attack}         'attack',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    70
{aia_Up}             'up',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    71
{aia_Down}           'down'
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    72
                     );
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    73
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    74
{$IFDEF TRACEAIACTIONS}
193
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
    75
const SpecActionIdToStr: array[$80000000..$80000009] of string[16] = (
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    76
{aia_Weapon}             'aia_Weapon',
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    77
{aia_WaitX}              'aia_WaitX',
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    78
{aia_WaitY}              'aia_WaitY',
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    79
{aia_LookLeft}           'aia_LookLeft',
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    80
{aia_LookRight}          'aia_LookRight',
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    81
{aia_AwareExpl}          'aia_AwareExpl',
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    82
{aia_HJump}              'aia_HJump',
144
e6084b0c9316 - Fix 'new team' regression
unc0rr
parents: 143
diff changeset
    83
{aia_LJump}              'aia_LJump',
193
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
    84
{aia_Skip}               'aia_Skip',
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
    85
{aia_Wait}               'aia_Wait'
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    86
);
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    87
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    88
procedure DumpAction(Action: TAction; Me: PGear);
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    89
begin
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    90
if (Action.Action and ai_specmask) = 0 then
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    91
   WriteLnToConsole('AI action: '+ActionIdToStr[Action.Action])
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    92
else begin
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    93
   WriteLnToConsole('AI action: '+SpecActionIdToStr[Action.Action]);
193
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
    94
   if (Action.Action = aia_WaitXL) or (Action.Action = aia_WaitXR) then
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    95
      WriteLnToConsole('AI action Wait X = '+inttostr(Action.Param)+', current X = '+inttostr(round(Me.X)));
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    96
   end
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    97
end;
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    98
{$ENDIF}
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    99
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
   100
procedure AddAction(var Actions: TActions; Action, Param, TimeDelta: Longword; const X: integer = 0; Y: integer = 0);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   101
begin
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   102
with Actions do
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   103
     begin
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   104
     actions[Count].Action:= Action;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   105
     actions[Count].Param:= Param;
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
   106
     actions[Count].X:= X;
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
   107
     actions[Count].Y:= Y;
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   108
     if Count > 0 then actions[Count].Time:= TimeDelta
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   109
                  else actions[Count].Time:= GameTicks + TimeDelta;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   110
     inc(Count);
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   111
     TryDo(Count < MAXACTIONS, 'AI: actions overflow', true);
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   112
     end
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   113
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   114
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   115
procedure ProcessAction(var Actions: TActions; Me: PGear);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   116
var s: shortstring;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   117
begin
194
88652abdce9a Fixed weird bots behavior
unc0rr
parents: 193
diff changeset
   118
repeat
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   119
if Actions.Pos >= Actions.Count then exit;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   120
with Actions.actions[Actions.Pos] do
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   121
     begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   122
     if Time > GameTicks then exit;
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
   123
     {$IFDEF TRACEAIACTIONS}
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
   124
     DumpAction(Actions.actions[Actions.Pos], Me);
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
   125
     {$ENDIF}
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   126
     if (Action and ai_specmask) <> 0 then
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   127
        case Action of
165
9b9144948668 Many small fixes
unc0rr
parents: 144
diff changeset
   128
           aia_Weapon: SetWeapon(TAmmoType(Param));
193
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
   129
           aia_WaitXL: if round(Me.X) = Param then Time:= GameTicks
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
   130
                          else if Round(Me.X) < Param then
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
   131
                               begin
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
   132
                               OutError('AI: WaitXL assert');
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
   133
                               Actions.Count:= 0
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
   134
                               end
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
   135
                          else exit;
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
   136
           aia_WaitXR: if round(Me.X) = Param then Time:= GameTicks
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
   137
                          else if Round(Me.X) > Param then
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
   138
                               begin
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
   139
                               OutError('AI: WaitXR assert');
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
   140
                               Actions.Count:= 0
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
   141
                               end
56885ea29202 Fix bots regressions
unc0rr
parents: 183
diff changeset
   142
                          else exit;
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   143
         aia_LookLeft: if Me.dX >= 0 then
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   144
                          begin
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   145
                          ParseCommand('+left');
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   146
                          exit
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   147
                          end else ParseCommand('-left');
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   148
        aia_LookRight: if Me.dX < 0 then
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   149
                          begin
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   150
                          ParseCommand('+right');
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   151
                          exit
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   152
                          end else ParseCommand('-right');
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
   153
        aia_AwareExpl: AwareOfExplosion(X, Y, Param);
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
   154
            aia_HJump: ParseCommand('hjump');
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
   155
            aia_LJump: ParseCommand('ljump');
143
3dacbd83209b - Many fixes to AI
unc0rr
parents: 80
diff changeset
   156
             aia_Skip: ParseCommand('skip');
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   157
             end else
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   158
        begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   159
        s:= ActionIdToStr[Action];
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   160
        if (Param and ai_specmask) <> 0 then
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   161
           case Param of
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   162
             aim_push: s:= '+' + s;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   163
          aim_release: s:= '-' + s;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   164
             end
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   165
          else if Param <> 0 then s:= s + ' ' + inttostr(Param);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   166
        ParseCommand(s)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   167
        end
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   168
     end;
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   169
inc(Actions.Pos);
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   170
if Actions.Pos <= Actions.Count then
194
88652abdce9a Fixed weird bots behavior
unc0rr
parents: 193
diff changeset
   171
   inc(Actions.actions[Actions.Pos].Time, GameTicks);
88652abdce9a Fixed weird bots behavior
unc0rr
parents: 193
diff changeset
   172
until false
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   173
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   174
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   175
end.