hedgewars/uAIActions.pas
changeset 64 9df467527ae5
parent 17 a6bed8d31444
child 66 9643d75baf1e
--- a/hedgewars/uAIActions.pas	Fri Feb 24 16:06:12 2006 +0000
+++ b/hedgewars/uAIActions.pas	Wed Jun 14 15:50:22 2006 +0000
@@ -1,40 +1,8 @@
-(*
- * Hedgewars, a worms-like game
- * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
- *
- * Distributed under the terms of the BSD-modified licence:
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * with the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- *    this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- *    this list of conditions and the following disclaimer in the documentation
- *    and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
- * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *)
-
 unit uAIActions;
 interface
-{$INCLUDE options.inc}
-const aia_none       = 0;
+uses uGears;
+const MAXACTIONS = 256;
+      aia_none       = 0;
       aia_Left       = 1;
       aia_Right      = 2;
       aia_Timer      = 3;
@@ -53,20 +21,21 @@
       aim_release    = $80000001;
       ai_specmask    = $80000000;
 
-type PAction = ^TAction;
-     TAction = record
+type TAction = record
                Action, Param: Longword;
                Time: Longword;
-               Next: PAction;
                end;
+     TActions = record
+                Count, Pos: Longword;
+                actions: array[0..Pred(MAXACTIONS)] of TAction;
+                Score: integer;
+                end;
 
-function AddAction(Action, Param, TimeDelta: Longword): PAction;
-procedure FreeActionsList;
-function IsActionListEmpty: boolean;
-procedure ProcessAction;
+procedure AddAction(var Actions: TActions; Action, Param, TimeDelta: Longword);
+procedure ProcessAction(var Actions: TActions; Me: PGear);
 
 implementation
-uses uMisc, uConsts, uConsole, uTeams;
+uses uMisc, uTeams, uConsts, uConsole;
 
 const ActionIdToStr: array[0..7] of string[16] = (
 {aia_none}           '',
@@ -77,98 +46,53 @@
 {aia_attack}         'attack',
 {aia_Up}             'up',
 {aia_Down}           'down'
-      );
-
+                     );
 
-var ActionList,
-    FinAction: PAction;
-
-function AddAction(Action, Param, TimeDelta: Longword): PAction;
+procedure AddAction(var Actions: TActions; Action, Param, TimeDelta: Longword);
 begin
-New(Result);
-TryDo(Result <> nil, 'AddAction: Result = nil', true);
-FillChar(Result^, sizeof(TAction), 0);
-Result.Action:= Action;
-Result.Param:= Param;
-if ActionList = nil then
-   begin
-   Result.Time:= GameTicks + TimeDelta;
-   ActionList:= Result;
-   FinAction := Result
-   end else
-   begin
-   Result.Time:= TimeDelta;
-   FinAction.Next:= Result;
-   FinAction:= Result
-   end
-end;
-
-procedure DeleteCurrAction;
-var t: PAction;
-begin
-t:= ActionList;
-ActionList:= ActionList.Next;
-if ActionList = nil then FinAction:= nil
-                    else inc(ActionList.Time, t.Time);
-Dispose(t)
-end;
-
-function IsActionListEmpty: boolean;
-begin
-Result:= ActionList = nil
-end;
-
-procedure FreeActionsList;
-begin
-while ActionList <> nil do DeleteCurrAction;
+with Actions do
+     begin
+     actions[Count].Action:= Action;
+     actions[Count].Param:= Param;
+     if Count > 0 then actions[Count].Time:= actions[Pred(Count)].Time + TimeDelta
+                  else actions[Count].Time:= GameTicks + TimeDelta;
+     inc(Count);
+     TryDo(Count < MAXACTIONS, 'AI: actions overflow', true);
+     end
 end;
 
 procedure SetWeapon(weap: Longword);
-var t: integer;
 begin
-t:= 0;
 with CurrentTeam^ do
      with Hedgehogs[CurrHedgehog] do
           while Ammo[CurSlot, CurAmmo].AmmoType <> TAmmotype(weap) do
-                begin
                 ParseCommand('/slot ' + chr(49 + Ammoz[TAmmoType(weap)].Slot));
-                inc(t);
-                if t > 10 then OutError('AI: incorrect try to change weapon!', true)
-                end
 end;
 
-procedure ProcessAction;
+procedure ProcessAction(var Actions: TActions; Me: PGear);
 var s: shortstring;
 begin
-if ActionList = nil then exit;
-with ActionList^ do
+if Actions.Pos >= Actions.Count then exit;
+with Actions.actions[Actions.Pos] do
      begin
      if Time > GameTicks then exit;
      if (Action and ai_specmask) <> 0 then
         case Action of
            aia_Weapon: SetWeapon(Param);
-            aia_WaitX: with CurrentTeam^ do
-                            with Hedgehogs[CurrHedgehog] do
-                                 if round(Gear.X) = Param then Time:= GameTicks
-                                                          else exit;
-            aia_WaitY: with CurrentTeam^ do
-                            with Hedgehogs[CurrHedgehog] do
-                                 if round(Gear.Y) = Param then Time:= GameTicks
-                                                          else exit;
-         aia_LookLeft: with CurrentTeam^ do
-                            with Hedgehogs[CurrHedgehog] do
-                                 if Gear.dX >= 0 then
-                                    begin
-                                    ParseCommand('+left');
-                                    exit
-                                    end else ParseCommand('-left');
-        aia_LookRight: with CurrentTeam^ do
-                            with Hedgehogs[CurrHedgehog] do
-                                 if Gear.dX < 0 then
-                                    begin
-                                    ParseCommand('+right');
-                                    exit
-                                    end else ParseCommand('-right');
+            aia_WaitX: if round(Me.X) = Param then Time:= GameTicks
+                                              else exit;
+            aia_WaitY: if round(Me.Y) = Param then Time:= GameTicks
+                                              else exit;
+         aia_LookLeft: if Me.dX >= 0 then
+                          begin
+                          ParseCommand('+left');
+                          exit
+                          end else ParseCommand('-left');
+        aia_LookRight: if Me.dX < 0 then
+                          begin
+                          ParseCommand('+right');
+                          exit
+                          end else ParseCommand('-right');
              end else
         begin
         s:= ActionIdToStr[Action];
@@ -181,7 +105,7 @@
         ParseCommand(s)
         end
      end;
-DeleteCurrAction
+inc(Actions.Pos)
 end;
 
 end.