hedgewars/uAIActions.pas
author unc0rr
Mon, 22 Aug 2005 13:35:41 +0000
changeset 1 30f2d1037d5d
child 4 bcbd7adb4e4b
permissions -rw-r--r--
Add current sources. hw, hwserv and runhelper are compilable under Windows and *nix with FreePascal (and use -Od option) and run well on these OSes Hedge.dpr can be run only in Windows... to be ported
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     1
(*
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     2
 * Hedgewars, a worms-like game
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     4
 *
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     5
 * Distributed under the terms of the BSD-modified licence:
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     6
 *
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     8
 * of this software and associated documentation files (the "Software"), to deal
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
     9
 * with the Software without restriction, including without limitation the
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    11
 * sell copies of the Software, and to permit persons to whom the Software is
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    12
 * furnished to do so, subject to the following conditions:
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    13
 *
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    14
 * 1. Redistributions of source code must retain the above copyright notice,
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    15
 *    this list of conditions and the following disclaimer.
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    17
 *    this list of conditions and the following disclaimer in the documentation
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    18
 *    and/or other materials provided with the distribution.
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    19
 * 3. The name of the author may not be used to endorse or promote products
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    20
 *    derived from this software without specific prior written permission.
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    21
 *
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    25
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    28
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    29
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    30
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    31
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    32
 *)
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    33
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    34
unit uAIActions;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    35
interface
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    36
{$INCLUDE options.inc}
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    37
const aia_none       = 0;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    38
      aia_Left       = 1;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    39
      aia_Right      = 2;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    40
      aia_Timer      = 3;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    41
      aia_Slot       = 4;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    42
      aia_attack     = 5;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    43
      aia_Up         = 6;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    44
      aia_Down       = 7;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    45
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    46
      aia_Weapon     = $80000000;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    47
      aia_WaitX      = $80000001;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    48
      aia_WaitY      = $80000002;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    49
      aia_LookLeft   = $80000003;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    50
      aia_LookRight  = $80000004;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    51
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    52
      aim_push       = $80000000;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    53
      aim_release    = $80000001;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    54
      ai_specmask    = $80000000;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    55
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    56
type PAction = ^TAction;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    57
     TAction = record
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    58
               Action, Param: Longword;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    59
               Time: Longword;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    60
               Next: PAction;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    61
               end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    62
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    63
function AddAction(Action, Param, TimeDelta: Longword): PAction;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    64
procedure FreeActionsList;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    65
function IsActionListEmpty: boolean;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    66
procedure ProcessAction;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    67
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    68
implementation
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    69
uses uMisc, uConsts, uConsole, uTeams;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    70
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    71
const ActionIdToStr: array[0..7] of string[16] = (
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    72
{aia_none}           '',
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    73
{aia_Left}           'left',
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    74
{aia_Right}          'right',
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    75
{aia_Timer}          'timer',
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    76
{aia_slot}           'slot',
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    77
{aia_attack}         'attack',
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    78
{aia_Up}             'up',
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    79
{aia_Down}           'down'
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    80
      );
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    81
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    82
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    83
var ActionList,
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    84
    FinAction: PAction;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    85
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    86
function AddAction(Action, Param, TimeDelta: Longword): PAction;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    87
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    88
New(Result);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    89
TryDo(Result <> nil, errmsgDynamicVar, true);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    90
FillChar(Result^, sizeof(TAction), 0);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    91
Result.Action:= Action;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    92
Result.Param:= Param;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    93
if ActionList = nil then
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    94
   begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    95
   Result.Time:= GameTicks + TimeDelta;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    96
   ActionList:= Result;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    97
   FinAction := Result
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    98
   end else
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
    99
   begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   100
   Result.Time:= TimeDelta;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   101
   FinAction.Next:= Result;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   102
   FinAction:= Result
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   103
   end
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   104
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   105
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   106
procedure DeleteCurrAction;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   107
var t: PAction;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   108
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   109
t:= ActionList;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   110
ActionList:= ActionList.Next;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   111
if ActionList = nil then FinAction:= nil
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   112
                    else inc(ActionList.Time, t.Time);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   113
Dispose(t)
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   114
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   115
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   116
function IsActionListEmpty: boolean;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   117
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   118
Result:= ActionList = nil
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   119
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   120
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   121
procedure FreeActionsList;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   122
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   123
while ActionList <> nil do DeleteCurrAction;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   124
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   125
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   126
procedure SetWeapon(weap: Longword);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   127
var t: integer;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   128
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   129
t:= 0;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   130
with CurrentTeam^ do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   131
     with Hedgehogs[CurrHedgehog] do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   132
          while Ammo[CurSlot, CurAmmo].AmmoType <> TAmmotype(weap) do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   133
                begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   134
                ParseCommand('/slot ' + chr(49 + Ammoz[TAmmoType(weap)].Slot));
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   135
                inc(t);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   136
                if t > 10 then OutError('AI: incorrect try to change weapon!', true)
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   137
                end
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   138
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   139
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   140
procedure ProcessAction;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   141
var s: shortstring;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   142
begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   143
if ActionList = nil then exit;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   144
with ActionList^ do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   145
     begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   146
     if Time > GameTicks then exit;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   147
     if (Action and ai_specmask) <> 0 then
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   148
        case Action of
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   149
           aia_Weapon: SetWeapon(Param);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   150
            aia_WaitX: with CurrentTeam^ do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   151
                            with Hedgehogs[CurrHedgehog] do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   152
                                 if round(Gear.X) = Param then Time:= GameTicks
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   153
                                                          else exit;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   154
            aia_WaitY: with CurrentTeam^ do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   155
                            with Hedgehogs[CurrHedgehog] do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   156
                                 if round(Gear.Y) = Param then Time:= GameTicks
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   157
                                                          else exit;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   158
         aia_LookLeft: with CurrentTeam^ do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   159
                            with Hedgehogs[CurrHedgehog] do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   160
                                 if Gear.dX >= 0 then
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   161
                                    begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   162
                                    ParseCommand('+left');
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   163
                                    exit
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   164
                                    end else ParseCommand('-left');
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   165
        aia_LookRight: with CurrentTeam^ do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   166
                            with Hedgehogs[CurrHedgehog] do
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   167
                                 if Gear.dX < 0 then
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   168
                                    begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   169
                                    ParseCommand('+right');
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   170
                                    exit
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   171
                                    end else ParseCommand('-right');
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   172
             end else
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   173
        begin
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   174
        s:= ActionIdToStr[Action];
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   175
        if (Param and ai_specmask) <> 0 then
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   176
           case Param of
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   177
             aim_push: s:= '+' + s;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   178
          aim_release: s:= '-' + s;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   179
             end
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   180
          else if Param <> 0 then s:= s + ' ' + inttostr(Param);
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   181
        ParseCommand(s)
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   182
        end
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   183
     end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   184
DeleteCurrAction
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   185
end;
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   186
30f2d1037d5d Add current sources.
unc0rr
parents:
diff changeset
   187
end.