hedgewars/uAIActions.pas
author unc0rr
Thu, 05 Oct 2006 16:33:18 +0000
changeset 183 57c2ef19f719
parent 165 9b9144948668
child 193 56885ea29202
permissions -rw-r--r--
Relicense to GPL
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;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    33
      aia_WaitX      = $80000001;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    34
      aia_WaitY      = $80000002;
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;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    41
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    42
      aim_push       = $80000000;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    43
      aim_release    = $80000001;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    44
      ai_specmask    = $80000000;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    45
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    46
type TAction = record
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    47
               Action, Param: Longword;
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    48
               X, Y: integer;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    49
               Time: Longword;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    50
               end;
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    51
     TActions = record
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    52
                Count, Pos: Longword;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    53
                actions: array[0..Pred(MAXACTIONS)] of TAction;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    54
                Score: integer;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    55
                end;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    56
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    57
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
    58
procedure ProcessAction(var Actions: TActions; Me: PGear);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    59
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    60
implementation
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    61
uses uMisc, uTeams, uConsts, uConsole, uAIMisc;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    62
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    63
const ActionIdToStr: array[0..6] of string[16] = (
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    64
{aia_none}           '',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    65
{aia_Left}           'left',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    66
{aia_Right}          'right',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    67
{aia_Timer}          'timer',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    68
{aia_attack}         'attack',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    69
{aia_Up}             'up',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    70
{aia_Down}           'down'
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    71
                     );
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    72
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    73
{$IFDEF TRACEAIACTIONS}
144
e6084b0c9316 - Fix 'new team' regression
unc0rr
parents: 143
diff changeset
    74
const SpecActionIdToStr: array[$80000000..$80000008] of string[16] = (
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    75
{aia_Weapon}             'aia_Weapon',
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    76
{aia_WaitX}              'aia_WaitX',
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    77
{aia_WaitY}              'aia_WaitY',
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    78
{aia_LookLeft}           'aia_LookLeft',
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    79
{aia_LookRight}          'aia_LookRight',
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    80
{aia_AwareExpl}          'aia_AwareExpl',
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    81
{aia_HJump}              'aia_HJump',
144
e6084b0c9316 - Fix 'new team' regression
unc0rr
parents: 143
diff changeset
    82
{aia_LJump}              'aia_LJump',
e6084b0c9316 - Fix 'new team' regression
unc0rr
parents: 143
diff changeset
    83
{aia_Skip}               'aia_Skip'
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    84
);
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    85
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    86
procedure DumpAction(Action: TAction; Me: PGear);
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    87
begin
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    88
if (Action.Action and ai_specmask) = 0 then
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    89
   WriteLnToConsole('AI action: '+ActionIdToStr[Action.Action])
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    90
else begin
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    91
   WriteLnToConsole('AI action: '+SpecActionIdToStr[Action.Action]);
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    92
   if Action.Action = aia_WaitX then
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    93
      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
    94
   end
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    95
end;
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    96
{$ENDIF}
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
    97
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    98
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
    99
begin
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   100
with Actions do
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   101
     begin
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   102
     actions[Count].Action:= Action;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   103
     actions[Count].Param:= Param;
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
   104
     actions[Count].X:= X;
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
   105
     actions[Count].Y:= Y;
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   106
     if Count > 0 then actions[Count].Time:= TimeDelta
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   107
                  else actions[Count].Time:= GameTicks + TimeDelta;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   108
     inc(Count);
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   109
     TryDo(Count < MAXACTIONS, 'AI: actions overflow', true);
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   110
     end
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
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   113
procedure ProcessAction(var Actions: TActions; Me: PGear);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   114
var s: shortstring;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   115
begin
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   116
if Actions.Pos >= Actions.Count then exit;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   117
with Actions.actions[Actions.Pos] do
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   118
     begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   119
     if Time > GameTicks then exit;
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
   120
     {$IFDEF TRACEAIACTIONS}
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
   121
     DumpAction(Actions.actions[Actions.Pos], Me);
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
   122
     {$ENDIF}
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   123
     if (Action and ai_specmask) <> 0 then
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   124
        case Action of
165
9b9144948668 Many small fixes
unc0rr
parents: 144
diff changeset
   125
           aia_Weapon: SetWeapon(TAmmoType(Param));
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   126
            aia_WaitX: if round(Me.X) = Param then Time:= GameTicks
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   127
                                              else exit;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   128
            aia_WaitY: if round(Me.Y) = Param then Time:= GameTicks
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   129
                                              else exit;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   130
         aia_LookLeft: if Me.dX >= 0 then
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   131
                          begin
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   132
                          ParseCommand('+left');
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   133
                          exit
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   134
                          end else ParseCommand('-left');
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   135
        aia_LookRight: if Me.dX < 0 then
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   136
                          begin
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   137
                          ParseCommand('+right');
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   138
                          exit
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   139
                          end else ParseCommand('-right');
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
   140
        aia_AwareExpl: AwareOfExplosion(X, Y, Param);
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
   141
            aia_HJump: ParseCommand('hjump');
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 75
diff changeset
   142
            aia_LJump: ParseCommand('ljump');
143
3dacbd83209b - Many fixes to AI
unc0rr
parents: 80
diff changeset
   143
             aia_Skip: ParseCommand('skip');
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   144
             end else
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   145
        begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   146
        s:= ActionIdToStr[Action];
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   147
        if (Param and ai_specmask) <> 0 then
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   148
           case Param of
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   149
             aim_push: s:= '+' + s;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   150
          aim_release: s:= '-' + s;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   151
             end
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   152
          else if Param <> 0 then s:= s + ' ' + inttostr(Param);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   153
        ParseCommand(s)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   154
        end
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   155
     end;
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   156
inc(Actions.Pos);
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   157
if Actions.Pos <= Actions.Count then
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   158
   inc(Actions.actions[Actions.Pos].Time, GameTicks)
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   159
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   160
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   161
end.