hedgewars/uAIActions.pas
author unc0rr
Sat, 08 Jul 2006 21:40:03 +0000
changeset 74 42257fee61ae
parent 71 5f56c6979496
child 75 d2b737858ff7
permissions -rw-r--r--
- Unicode support for team and hedgehogs names - Clouds move in vertical direction also - Many fixes to hedgehog physics - Support for different images in 16 and 32 bit screen resolution - Fixed bots behavior after attack - Fixed camera behavior right after the game start
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
 *
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
     5
 * Distributed under the terms of the BSD-modified licence:
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
     6
 *
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
     7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
     8
 * of this software and associated documentation files (the "Software"), to deal
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
     9
 * with the Software without restriction, including without limitation the
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    11
 * sell copies of the Software, and to permit persons to whom the Software is
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    12
 * furnished to do so, subject to the following conditions:
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    13
 *
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    14
 * 1. Redistributions of source code must retain the above copyright notice,
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    15
 *    this list of conditions and the following disclaimer.
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    17
 *    this list of conditions and the following disclaimer in the documentation
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    18
 *    and/or other materials provided with the distribution.
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    19
 * 3. The name of the author may not be used to endorse or promote products
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    20
 *    derived from this software without specific prior written permission.
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    21
 *
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    25
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    28
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    29
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    30
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    31
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    32
 *)
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    33
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    34
unit uAIActions;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    35
interface
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    36
uses uGears;
74
42257fee61ae - Unicode support for team and hedgehogs names
unc0rr
parents: 71
diff changeset
    37
const MAXACTIONS = 32;
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    38
      aia_none       = 0;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    39
      aia_Left       = 1;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    40
      aia_Right      = 2;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    41
      aia_Timer      = 3;
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    42
      aia_attack     = 4;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    43
      aia_Up         = 5;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    44
      aia_Down       = 6;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    45
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    46
      aia_Weapon     = $80000000;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    47
      aia_WaitX      = $80000001;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    48
      aia_WaitY      = $80000002;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    49
      aia_LookLeft   = $80000003;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    50
      aia_LookRight  = $80000004;
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    51
      aia_AwareExpl  = $80000005;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    52
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    53
      aim_push       = $80000000;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    54
      aim_release    = $80000001;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    55
      ai_specmask    = $80000000;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    56
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    57
type TAction = record
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    58
               Action, Param: Longword;
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    59
               X, Y: integer;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    60
               Time: Longword;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    61
               end;
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    62
     TActions = record
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    63
                Count, Pos: Longword;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    64
                actions: array[0..Pred(MAXACTIONS)] of TAction;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    65
                Score: integer;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    66
                end;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    67
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    68
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
    69
procedure ProcessAction(var Actions: TActions; Me: PGear);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    70
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    71
implementation
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    72
uses uMisc, uTeams, uConsts, uConsole, uAIMisc;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    73
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    74
const ActionIdToStr: array[0..6] of string[16] = (
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    75
{aia_none}           '',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    76
{aia_Left}           'left',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    77
{aia_Right}          'right',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    78
{aia_Timer}          'timer',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    79
{aia_attack}         'attack',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    80
{aia_Up}             'up',
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    81
{aia_Down}           'down'
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    82
                     );
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    83
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    84
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
    85
begin
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    86
with Actions do
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    87
     begin
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    88
     actions[Count].Action:= Action;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    89
     actions[Count].Param:= Param;
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    90
     actions[Count].X:= X;
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
    91
     actions[Count].Y:= Y;
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    92
     if Count > 0 then actions[Count].Time:= TimeDelta
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    93
                  else actions[Count].Time:= GameTicks + TimeDelta;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    94
     inc(Count);
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    95
     TryDo(Count < MAXACTIONS, 'AI: actions overflow', true);
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
    96
     end
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    97
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    98
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    99
procedure SetWeapon(weap: Longword);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   100
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   101
with CurrentTeam^ do
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   102
     with Hedgehogs[CurrHedgehog] do
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   103
          while Ammo[CurSlot, CurAmmo].AmmoType <> TAmmoType(weap) do
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   104
                ParseCommand('/slot ' + chr(49 + Ammoz[TAmmoType(weap)].Slot));
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   105
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   106
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   107
procedure ProcessAction(var Actions: TActions; Me: PGear);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   108
var s: shortstring;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   109
begin
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   110
if Actions.Pos >= Actions.Count then exit;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   111
with Actions.actions[Actions.Pos] do
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   112
     begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   113
     if Time > GameTicks then exit;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   114
     if (Action and ai_specmask) <> 0 then
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   115
        case Action of
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   116
           aia_Weapon: SetWeapon(Param);
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   117
            aia_WaitX: if round(Me.X) = Param then Time:= GameTicks
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   118
                                              else exit;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   119
            aia_WaitY: if round(Me.Y) = Param then Time:= GameTicks
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   120
                                              else exit;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   121
         aia_LookLeft: if Me.dX >= 0 then
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   122
                          begin
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   123
                          ParseCommand('+left');
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   124
                          exit
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   125
                          end else ParseCommand('-left');
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   126
        aia_LookRight: if Me.dX < 0 then
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   127
                          begin
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   128
                          ParseCommand('+right');
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   129
                          exit
9df467527ae5 - Start AI rewrite
unc0rr
parents: 17
diff changeset
   130
                          end else ParseCommand('-right');
71
5f56c6979496 - Changed falling damage scoring
unc0rr
parents: 70
diff changeset
   131
        aia_AwareExpl: AwareOfExplosion(X, Y, Param);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   132
             end else
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   133
        begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   134
        s:= ActionIdToStr[Action];
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   135
        if (Param and ai_specmask) <> 0 then
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   136
           case Param of
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   137
             aim_push: s:= '+' + s;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   138
          aim_release: s:= '-' + s;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   139
             end
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   140
          else if Param <> 0 then s:= s + ' ' + inttostr(Param);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   141
        ParseCommand(s)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   142
        end
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   143
     end;
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   144
inc(Actions.Pos);
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   145
if Actions.Pos <= Actions.Count then
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   146
   inc(Actions.actions[Actions.Pos].Time, GameTicks)
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   147
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   148
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   149
end.