hedgewars/uAI.pas
changeset 1 30f2d1037d5d
child 4 bcbd7adb4e4b
equal deleted inserted replaced
0:475c0f2f9d17 1:30f2d1037d5d
       
     1 (*
       
     2  * Hedgewars, a worms-like game
       
     3  * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * Distributed under the terms of the BSD-modified licence:
       
     6  *
       
     7  * Permission is hereby granted, free of charge, to any person obtaining a copy
       
     8  * of this software and associated documentation files (the "Software"), to deal
       
     9  * with the Software without restriction, including without limitation the
       
    10  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
       
    11  * sell copies of the Software, and to permit persons to whom the Software is
       
    12  * furnished to do so, subject to the following conditions:
       
    13  *
       
    14  * 1. Redistributions of source code must retain the above copyright notice,
       
    15  *    this list of conditions and the following disclaimer.
       
    16  * 2. Redistributions in binary form must reproduce the above copyright notice,
       
    17  *    this list of conditions and the following disclaimer in the documentation
       
    18  *    and/or other materials provided with the distribution.
       
    19  * 3. The name of the author may not be used to endorse or promote products
       
    20  *    derived from this software without specific prior written permission.
       
    21  *
       
    22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
       
    23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
       
    24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
       
    25  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
       
    28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
       
    29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
       
    30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
       
    31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    32  *)
       
    33 
       
    34 unit uAI;
       
    35 interface
       
    36 {$INCLUDE options.inc}
       
    37 procedure ProcessBot;
       
    38 
       
    39 implementation
       
    40 uses uAIActions, uAIMisc, uMisc, uTeams, uConsts, uAIAmmoTests, uGears, SDLh;
       
    41 
       
    42 function Go(Gear: PGear; Times: Longword): boolean;
       
    43 begin
       
    44 Result:= false
       
    45 end;
       
    46 
       
    47 procedure Think;
       
    48 var Targets: TTargets;
       
    49     Angle, Power: integer;
       
    50     Time: Longword;
       
    51 
       
    52     procedure FindTarget(Flags: Longword);
       
    53     var t: integer;
       
    54         a, aa: TAmmoType;
       
    55         Me: TPoint;
       
    56     begin
       
    57     t:= 0;
       
    58     with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
       
    59          begin
       
    60          Me.X:= round(Gear.X);
       
    61          Me.Y:= round(Gear.Y);
       
    62          end;
       
    63     repeat
       
    64       if isInMultiShoot then with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
       
    65                              a:= Ammo[CurSlot, CurAmmo].AmmoType
       
    66                         else a:= TAmmoType(random(ord(High(TAmmoType))));
       
    67       aa:= a;
       
    68       repeat
       
    69         if Assigned(AmmoTests[a].Test)
       
    70            and ((Flags = 0) or ((Flags and AmmoTests[a].Flags) <> 0)) then
       
    71            if AmmoTests[a].Test(Me, Targets.ar[t], Flags, Time, Angle, Power) then
       
    72               begin
       
    73               AddAction(aia_Weapon, ord(a), 1000);
       
    74               if Time <> 0 then AddAction(aia_Timer, Time div 1000, 400);
       
    75               exit
       
    76               end;
       
    77       if a = High(TAmmoType) then a:= Low(TAmmoType)
       
    78                              else inc(a)
       
    79       until isInMultiShoot or (a = aa);
       
    80     inc(t)
       
    81     until (t >= Targets.Count)
       
    82     end;
       
    83 
       
    84     procedure TryGo(lvl, Flags: Longword);
       
    85     var tmpGear: TGear;
       
    86         i, t: integer;
       
    87     begin
       
    88     with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
       
    89     for t:= aia_Left to aia_Right do
       
    90         if IsActionListEmpty then
       
    91            begin
       
    92            tmpGear:= Gear^;
       
    93            i:= 0;
       
    94            Gear.Message:= t;
       
    95            while HHGo(Gear) do
       
    96                  begin
       
    97                  if (i mod 5 = 0) then
       
    98                     begin
       
    99                     FindTarget(Flags);
       
   100                     if not IsActionListEmpty then
       
   101                        begin
       
   102                        if i > 0 then
       
   103                           begin
       
   104                           AddAction(t, aim_push, 1000);
       
   105                           AddAction(aia_WaitX, round(Gear.X), 0);
       
   106                           AddAction(t, aim_release, 0)
       
   107                           end;
       
   108                        Gear^:= tmpGear;
       
   109                        exit
       
   110                        end
       
   111                     end;
       
   112                  inc(i)
       
   113                  end;
       
   114            Gear^:= tmpGear
       
   115            end
       
   116     end;
       
   117 
       
   118 begin
       
   119 with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
       
   120      if ((Gear.State and (gstAttacked or gstAttacking or gstMoving or gstFalling)) <> 0) then exit;
       
   121 
       
   122 FillTargets(Targets);
       
   123 
       
   124 TryGo(0, 0);
       
   125 
       
   126 if IsActionListEmpty then
       
   127    TryGo(0, ctfNotFull);
       
   128 if IsActionListEmpty then
       
   129    TryGo(0, ctfBreach);
       
   130 
       
   131 if IsActionListEmpty then
       
   132    begin
       
   133    AddAction(aia_Weapon, ord(amSkip), 1000);
       
   134    AddAction(aia_Attack, aim_push, 1000);
       
   135    exit
       
   136    end;
       
   137 
       
   138 with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
       
   139      begin
       
   140      if (Angle > 0) then AddAction(aia_LookRight, 0, 200)
       
   141         else if (Angle < 0) then AddAction(aia_LookLeft, 0, 200);
       
   142      Angle:= integer(Gear.Angle) - Abs(Angle);
       
   143      if Angle > 0 then
       
   144         begin
       
   145         AddAction(aia_Up, aim_push, 500);
       
   146         AddAction(aia_Up, aim_release, Angle)
       
   147         end else if Angle < 0 then
       
   148         begin
       
   149         AddAction(aia_Down, aim_push, 500);
       
   150         AddAction(aia_Down, aim_release, -Angle)
       
   151         end;
       
   152      AddAction(aia_attack, aim_push, 300);
       
   153      AddAction(aia_attack, aim_release, Power);
       
   154      end
       
   155 end;
       
   156 
       
   157 procedure ProcessBot;
       
   158 begin
       
   159 with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
       
   160      if (Gear <> nil)and((Gear.State and gstHHDriven) <> 0) then
       
   161         begin
       
   162         if IsActionListEmpty then Think;
       
   163         ProcessAction
       
   164         end
       
   165 end;
       
   166 
       
   167 end.