unit uEnemy;

interface
uses
  vectorgeometry,vectortypes,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, GLWin32Viewer, GLCrossPlatform, BaseClasses, GLScene, jpeg,
  GLFilePNG, glKeyboard, GLDCE, uWorld,
  GLHUDObjects, GLObjects, GLCoordinates, GLSound, GLMaterial, GLCadencer;

Type
TEnemyBashnya=class
private
Public
Obj, Bashnyas: TGLCube;
BTimer: Array[1..15] of real;
Lives: Array[0..150] of integer;
Visibled: array[0..70] of boolean;
Died: array[0..70] of boolean;
BList: TList;      //bashni
BPula: TGLSphere;//patron
BPList: TList;  //patron
Procedure CreateBashnya;
Procedure LookBashnya;
Procedure Pli_Bashnya(num: integer);
Procedure Move_Bashnya_Pula;
Procedure Check_Enemy_Bashnya_Live(num: integer);
end;

TEnemyTank=class
private
public
EnTankTarget: TGLCube;
left,right,Up,Down,
left1,right1,
Up2,Down2,
left3,right3,Up3,Down3,
Up4,Down4: Boolean;
EnemyTank,En_Pushka: TGLCube;
EnTankList: TLIst;
TankPula: TGLSphere;
TPulaList: TList;
Dis: single;
TTTimer: Array[1..18] of real;
Lives,Kol: Array[0..150] of integer;
Died: array[0..70] of boolean;
Visibled: array[0..70] of boolean;
Procedure CreateEnemyTank;
Procedure LookEnemyTank;
Procedure Pli_Enemy_Tank(num: integer);
Procedure Move_EnTank_Pula;
Procedure Move_EnTank;
Procedure Check_Enemy_Tank_Live(ni: integer);
Procedure Destroy_Enemy_Tank;
end;


implementation
Uses Unit1,Tank;

{ TEnemyBashnya }
procedure TEnemyBashnya.Check_Enemy_Bashnya_Live(num: integer);
var i: integer;
    vzriv: TGLSprite;
begin
Lives[num]:=Lives[num]-1;
if lives[num]<=0 then begin
died [num] := true;
TGLCube(Blist[num-30]).position.y:=200;
Vzriv:=TGLSprite.CreateAsChild(Form1.GLScene1.Objects);
with Vzriv do begin
Position.AsAffineVector:=AffineVectorMAke(TGLCube(Blist[num-30]).Position.X,0.11,TGLCube(Blist[num-30]).Position.Z);
Width:=4;
Height:=4;
Material.Texture.Disabled:=false;
Material.MaterialLibrary:=Form1.GLMaterialLibrary1;
Material.LibMaterialName:='Vzriv';
end;
end;



end;

procedure TEnemyBashnya.CreateBashnya;
var i,t: integer;
begin
t:=30;
for i:=0 to 13 do begin
Bashnyas:=TGLCube.CreateAsChild(Form1.GLScene1.Objects);
with bashnyas do begin
  position.AsAffineVector:=affineVectorMake(20,0.02,-0.5);
  cubeWidth:=3;
  cubedepth:=5;
  CubeHeight:=0;
  Direction.SetVector(0,0,-1);
  Up.SetVector(0,1,0);
  tag:=t+i;
  Material.MaterialLibrary:=form1.GLMaterialLibrary1;
Material.Texture.Disabled:=false;
Material.LibMaterialName:='Pushka1';
end;
with GetOrCreateDCEDynamic(Bashnyas) do begin
manager:=form1.GLDCEManager1;
Size.SetVector(3,1,5);
end;

BList.Add(Bashnyas);
end;

for i:=30 to 43 do begin
lives[i]:=100;
Died[i]:=false;
Visibled[i]:=false;
end;

TGLCube(Blist[0]).position.x:=-200;

TGLCube(Blist[1]).position.x:=-20;

TGLCube(Blist[2]).position.x:=-10;
TGLCube(Blist[2]).position.z:=115; //

TGLCube(Blist[3]).position.x:=0;
TGLCube(Blist[3]).position.z:=115; //

TGLCube(Blist[4]).position.x:=10;
TGLCube(Blist[4]).position.z:=115;  //

TGLCube(Blist[5]).position.x:=-30;  //
TGLCube(Blist[5]).position.z:=100;   //

TGLCube(Blist[6]).position.x:=-30;   //
TGLCube(Blist[6]).position.z:=90;    //

TGLCube(Blist[7]).position.x:=-30;   //
TGLCube(Blist[7]).position.z:=80;    //

TGLCube(Blist[8]).position.x:=20;
TGLCube(Blist[8]).position.z:=15;

TGLCube(Blist[9]).position.x:=20;
TGLCube(Blist[9]).position.z:=40;

TGLCube(Blist[10]).position.x:=20;
TGLCube(Blist[10]).position.z:=65;

TGLCube(Blist[11]).position.x:=20;
TGLCube(Blist[11]).position.z:=90;

TGLCube(Blist[12]).position.x:=20;
TGLCube(Blist[12]).position.z:=115;

TGLCube(Blist[1]).Material.LibMaterialName:='Pushka3';
TGLCube(Blist[12]).Material.LibMaterialName:='Pushka3';
TGLCube(Blist[2]).Material.LibMaterialName:='Pushka3';
TGLCube(Blist[11]).Material.LibMaterialName:='Pushka3';

end;

procedure TEnemyBashnya.LookBashnya;
var V1,x,y,z: TVector;
    Av1: TAffinevector;
    res,s1,s2,s3: Single;
    i,t: integer;
begin
for i:=0 to BList.count-1 do begin
t:=30+i;
if Died[t]=false then begin
s1:=TGLCube(Blist[i]).DistanceTo(UPlayerTank.Player); //player
s2:=TGLCube(Blist[i]).DistanceTo(UPlayerTank.Player2); //player 2)
s3:=TGLCube(Blist[i]).DistanceTo(UPlayerTank.Player3); //player 2)
if (s1<s2) and (s1<s3)  then obj:=UPlayerTank.Player;
if (s2<s1) and (s2<s3) then obj:=UPlayerTank.Player2;
if (s3<s1) and (s3<s2)  then obj:=UPlayerTank.Player3;

V1:=VectorSubtract(obj.AbsolutePosition,TGLCube(Blist[i]).AbsolutePosition);
NormalizeVector(V1);
MakeVector(Av1,v1[0],v1[1],v1[2]);
res:=VectorGeometry.VectorAngleCosine(TGLCube(Blist[i]).Direction.AsAffineVector,Av1);
res:=res-(TGLCube(Blist[i]).DistanceTo(obj)*0.01);
if (res>0) then begin
s1:=TGLCube(Blist[i]).DistanceTo(UPlayerTank.Player); //player
s2:=TGLCube(Blist[i]).DistanceTo(UPlayerTank.Player2); //player 2)
s3:=TGLCube(Blist[i]).DistanceTo(UPlayerTank.Player3); //player 2)
if (s1<s2) and (s1<s3)  then obj:=UPlayerTank.Player;
if (s2<s1) and (s2<s3) then obj:=UPlayerTank.Player2;
if (s3<s1) and (s3<s2)  then obj:=UPlayerTank.Player3;
if TGLCube(BList[i]).DistanceTo(obj)<40 then begin
MakeVector(x,obj.AbsolutePosition);
MakeVector(y,TGLCube(Blist[i]).AbsolutePosition);
z:=VectorSubtract(y,x);
NormalizeVector(z);
TGLCube(Blist[i]).Direction.X:=-z[0];
TGLCube(Blist[i]).Direction.z:=-z[2];
if TGLCube(BList[i]).DistanceTo(obj)<40 then
Pli_Bashnya(i);
end;
end;
end;

if Visibled[t]=true then begin
s1:=TGLCube(Blist[i]).DistanceTo(UPlayerTank.Player); //player
s2:=TGLCube(Blist[i]).DistanceTo(UPlayerTank.Player2); //player 2)
s3:=TGLCube(Blist[i]).DistanceTo(UPlayerTank.Player3); //player 2)
if (s1<s2) and (s1<s3)  then obj:=UPlayerTank.Player;
if (s2<s1) and (s2<s3) then obj:=UPlayerTank.Player2;
if (s3<s1) and (s3<s2)  then obj:=UPlayerTank.Player3;

V1:=VectorSubtract(obj.AbsolutePosition,TGLCube(Blist[i]).AbsolutePosition);
NormalizeVector(V1);
MakeVector(Av1,v1[0],v1[1],v1[2]);
res:=VectorGeometry.VectorAngleCosine(TGLCube(Blist[i]).Direction.AsAffineVector,Av1);
res:=res-(TGLCube(Blist[i]).DistanceTo(obj)*0.01);
if (res>0) then begin
s1:=TGLCube(Blist[i]).DistanceTo(UPlayerTank.Player); //player
s2:=TGLCube(Blist[i]).DistanceTo(UPlayerTank.Player2); //player 2)
s3:=TGLCube(Blist[i]).DistanceTo(UPlayerTank.Player3); //player 2)
if (s1<s2) and (s1<s3)  then obj:=UPlayerTank.Player;
if (s2<s1) and (s2<s3) then obj:=UPlayerTank.Player2;
if (s3<s1) and (s3<s2)  then obj:=UPlayerTank.Player3;
if TGLCube(BList[i]).DistanceTo(obj)<50 then begin
MakeVector(x,obj.AbsolutePosition);
MakeVector(y,TGLCube(Blist[i]).AbsolutePosition);
z:=VectorSubtract(y,x);
NormalizeVector(z);
TGLCube(Blist[i]).Direction.X:=-z[0];
TGLCube(Blist[i]).Direction.z:=-z[2];
if TGLCube(BList[i]).DistanceTo(obj)<50 then
Pli_Bashnya(i);
end;
end;
end;
end;

end;

procedure TEnemyBashnya.Move_Bashnya_Pula;
var i: integer;
begin
for i:=0 to BPList.count-1 do begin
TGLSphere(BPlist[i]).Move(0.2);
end;
end;


procedure TEnemyBashnya.Pli_Bashnya(num: integer);
begin
if BTimer[num]>3 then begin
BTimer[num]:=0;
BPula:=TGLSphere.CreateAsChild(form1.GLScene1.Objects);
BPula.AbsolutePosition:=TGLCube(Blist[num]).AbsolutePosition;
BPula.AbsoluteDirection:=TGLCube(Blist[num]).AbsoluteDirection;
BPula.Radius:=0.61;
BPList.add(BPula);
BPlist.Add(Bpula);
BPula.Tag:=20;

with GetOrCreateDCEDynamic(BPula) do begin
manager:=form1.GLDCEManager1;
Size.SetVector(1,1,1);
end;
end;
end;



//Создание танков и их логики

{ TEnemyTank }
procedure TEnemyTank.Check_Enemy_Tank_Live(ni: integer);
var i: integer;
begin
Lives[ni]:=Lives[ni]-1;
if Lives[ni]<=0 then begin
Died[ni]:=True;
Destroy_Enemy_Tank;
end;
end;

procedure TEnemyTank.CreateEnemyTank;
var i,t,s: Integer;
begin
if World.MissionNumber=1 then begin
for i:=0 to 9 do begin
EnemyTank:=TGLCube.CreateAsChild(form1.GLScene1.Objects);
with EnemyTank do begin
CubeWidth:=2;
CubeDepth:=3;
CubeHeight:=0;
Direction.SetVector(0,0,1);
Up.SetVector(0,1,0);
Material.MaterialLibrary:=form1.GLMaterialLibrary1;
Material.Texture.Disabled:=false;
Material.LibMaterialName:='Tank2';
Position.AsAffineVector:=AffineVectorMAke(0,0,20);
with GetOrCreateDCEDynamic(EnemyTank) do begin
manager:=form1.GLDCEManager1;
Size.SetVector(1,1,2);
end;
end;
EntankList.Add(EnemyTank);



En_Pushka:=TGLCube.CreateAsChild(TGLCube(EntankList[i]));
with En_Pushka do begin
CubeWidth:=2;
CubeDepth:=4;
CubeHeight:=0;
Direction.SetVector(0,0,1);
Up.SetVector(0,1,0);
Position.Z:=0.5;
Material.MaterialLibrary:=form1.GLMaterialLibrary1;
Material.Texture.Disabled:=false;
Material.LibMaterialName:='Pushka2';
end;
end;

for i:=0 to EnTAnkList.Count-1 do begin
TGLCube(EntankList[i]).tag:=i;
Died[i]:=false;
Lives[i]:=100;
Visibled[i]:=false;
Kol[i]:=0;
TGLCube(EntankList[0]).position.asaffinevector:=affinevectorMake(15,0,10);  //1
TGLCube(EntankList[0]).direction.setvector(-1,0,-1);
TGLCube(EntankList[1]).position.asaffinevector:=affinevectorMake(0,0,25);  //2
TGLCube(EntankList[2]).position.asaffinevector:=affinevectorMake(0,0,20);  //3
TGLCube(EntankList[2]).direction.setvector(0,0,-1);

TGLCube(EntankList[3]).position.asaffinevector:=affinevectorMake(0,0,100);  //4  +

TGLCube(EntankList[4]).position.asaffinevector:=affinevectorMake(-15,0,10);  //5
TGLCube(EntankList[4]).direction.setvector(1,0,-1);

TGLCube(EntankList[5]).position.asaffinevector:=affinevectorMake(-31,0,110);  //6
TGLCube(EntankList[5]).direction.setvector(0,0,-1);

TGLCube(EntankList[6]).position.asaffinevector:=affinevectorMake(-11,0,30);  //7  +
TGLCube(EntankList[7]).position.asaffinevector:=affinevectorMake(-11,0,60);  //8  +

TGLCube(EntankList[8]).position.asaffinevector:=affinevectorMake(11,0,30);  //9    +

TGLCube(EntankList[9]).position.asaffinevector:=affinevectorMake(-20,0,120);  //10 -
TGLCube(EntankList[9]).direction.setvector(0,0,-1);
end;
end;

if World.MissionNumber=2 then begin
for i:=0 to 6 do begin
EnemyTank:=TGLCube.CreateAsChild(form1.GLScene1.Objects);
with EnemyTank do begin
CubeWidth:=2;
CubeDepth:=3;
CubeHeight:=0;
Direction.SetVector(0,0,1);
Up.SetVector(0,1,0);
Material.MaterialLibrary:=form1.GLMaterialLibrary1;
Material.Texture.Disabled:=false;
Material.LibMaterialName:='Tank2';
Position.AsAffineVector:=AffineVectorMAke(0,0,20);
with GetOrCreateDCEDynamic(EnemyTank) do begin
manager:=form1.GLDCEManager1;
Size.SetVector(1,1,2);
end;
end;
EntankList.Add(EnemyTank);
end;


for i:=0 to EnTAnkList.Count-1 do begin
TGLCube(EntankList[i]).tag:=i;
Died[i]:=false;
Lives[i]:=100;
Visibled[i]:=false;
Kol[i]:=0;
TGLCube(EntankList[0]).position.asaffinevector:=affinevectorMake(-10,0,90);  //1
TGLCube(EntankList[0]).direction.setvector(0,0,-1);
TGLCube(EntankList[1]).position.asaffinevector:=affinevectorMake(10,0,90);  //1
TGLCube(EntankList[1]).direction.setvector(0,0,-1);
TGLCube(EntankList[2]).position.asaffinevector:=affinevectorMake(-14,0,80);  //1
TGLCube(EntankList[2]).direction.setvector(0,0,-1);
TGLCube(EntankList[3]).position.asaffinevector:=affinevectorMake(14,0,80);  //1
TGLCube(EntankList[3]).direction.setvector(0,0,-1);
TGLCube(EntankList[4]).position.asaffinevector:=affinevectorMake(-10,0,70);  //1
TGLCube(EntankList[4]).direction.setvector(0,0,-1);
TGLCube(EntankList[5]).position.asaffinevector:=affinevectorMake(10,0,70);  //1
TGLCube(EntankList[5]).direction.setvector(0,0,-1);
TGLCube(EntankList[6]).position.asaffinevector:=affinevectorMake(0,0,65);  //1
TGLCube(EntankList[6]).direction.setvector(0,0,-1);

end;
end;


end;



procedure TEnemyTank.Destroy_Enemy_Tank;
var i: integer;
    Vzriv: TGLSprite;
begin
for i:=0 to EntankList.Count-1 do begin
if Lives[i]=0 then begin
TGLCube(EnTankList[i]).position.y:=60;
Vzriv:=TGLSprite.CreateAsChild(Form1.GLScene1.Objects);
with Vzriv do begin
Position.AsAffineVector:=AffineVectorMAke(TGLCube(EnTankList[i]).Position.X,0.11,TGLCube(EnTankList[i]).Position.Z);
Width:=4;
Height:=4;
Material.Texture.Disabled:=false;
Material.MaterialLibrary:=Form1.GLMaterialLibrary1;
Material.LibMaterialName:='Vzriv';
end;

end;
end;

end;

procedure TEnemyTank.LookEnemyTank;
label no,noz;
var V1,x,y,z,pPoint: TVector;
    Av1: TAffinevector;
    res,s1,s2,s3,dist,distz: Single;
    obj: TGLCube;
    i,j,s,zd,d: integer;
    vs: boolean;
begin
for i:=0 to EnTankList.count-1 do begin
if Died[i]=false then begin

s1:=TGLCube(EnTankList[i]).DistanceTo(UPlayerTank.Player); //player
s2:=TGLCube(EnTankList[i]).DistanceTo(UPlayerTank.Player2); //player 2)
s3:=TGLCube(EnTankList[i]).DistanceTo(UPlayerTank.Player3); //player 2)
if (s1<s2) and (s1<s3)  then EnTankTarget:=UPlayerTank.Player;
if (s2<s1) and (s2<s3)  then EnTankTarget:=UPlayerTank.Player2;
if (s3<s1) and (s3<s2)  then EnTankTarget:=UPlayerTank.Player3;

if (EntankTarget<>Form1.DownSizer) then begin
V1:=VectorSubtract(EnTankTarget.AbsolutePosition,TGLCube(EnTankList[i]).AbsolutePosition);
NormalizeVector(V1);
MakeVector(Av1,v1[0],v1[1],v1[2]);
res:=VectorGeometry.VectorAngleCosine(TGLCube(EnTankList[i]).Direction.AsAffineVector,Av1);
res:=res-(TGLCube(EnTankList[i]).DistanceTo(EnTankTarget)*0.01);
if res>0.45 then begin
MakeVector(x,EnTankTarget.AbsolutePosition);
MakeVector(y,TGLCube(EnTankList[i]).AbsolutePosition);
z:=VectorSubtract(y,x);
NormalizeVector(z);
TGLCube(EnTankList[i]).Direction.X:=-z[0];
TGLCube(EnTankList[i]).Direction.z:=-z[2];
if TGLCube(EnTankList[i]).DistanceTo(EnTankTarget)<41 then  begin
if TGLCube(World.StenaList[0]).RayCastIntersect(TGLCube(EnTankList[i]).AbsolutePosition,TGLCube(EnTankList[i]).AbsoluteDirection,@pPoint,nil)
then begin
if TGLCube(EntankList[i]).DistanceTo(pPoint)>TGLCube(EnTAnkList[i]).DistanceTo(EnTAnkTarget) then  vs:=true else vs:=false;
end else vs:=true;

if vs=true then begin
Visibled[i]:=true;
Pli_Enemy_Tank(i);
end else
if World.MissionNumber=2 then begin
TGLCube(EnTankList[i]).Direction.SetVector(0,0,-1);
TGLCube(EntankList[i]).Move(0.01);
end;
end else begin
if World.MissionNumber=2 then begin
TGLCube(EnTankList[i]).Direction.SetVector(0,0,-1);
TGLCube(EntankList[i]).Move(0.01);
end;
end;
end;
end;

end;
end;


end;

procedure TEnemyTank.Move_EnTank;
var   x,y,z,pPoint: TVector;
      s1,s2,s3,s4: single;
      obj: TGLCube;
      i,s: integer;
      b,b1: boolean;
begin

if World.MissionNumber=1 then begin
//Tank-1
if (Lives[3]=100)and (Died[3]=false) and (Visibled[3]=false) then begin
if right=true then begin
TGLCube(EntankList[3]).direction.x:=-1;
TGLCube(EntankList[3]).direction.z:=0;
TGLCube(EntankList[3]).move(0.01);
if TGLCube(EntankList[3]).position.x<=-15 then begin Right:=false; down:=true; end;
end;
if down=true then begin
TGLCube(EntankList[3]).direction.x:=0;
TGLCube(EntankList[3]).direction.z:=-1;
TGLCube(EntankList[3]).move(0.01) ;
if TGLCube(EntankList[3]).position.z<=80 then begin Down:=false; Left:=true; end;
end;
if left=true then begin
TGLCube(EntankList[3]).direction.x:=1;
TGLCube(EntankList[3]).direction.z:=0;
TGLCube(EntankList[3]).move(0.01);
if TGLCube(EntankList[3]).position.x>=10 then begin left:=false; up:=true; end;
end;
if Up=true then begin
TGLCube(EntankList[3]).direction.x:=0;
TGLCube(EntankList[3]).direction.z:=1;
TGLCube(EntankList[3]).move(0.01);
if TGLCube(EntankList[3]).position.z>=100 then begin Up:=false; right:=true; end;
end;
end;
//Tank-2
if (Lives[7]=100) and (Died[7]=false) and (Visibled[7]=false) then begin
if right1=true then begin
TGLCube(EntankList[7]).direction.x:=-1;
TGLCube(EntankList[7]).direction.z:=0;
TGLCube(EntankList[7]).move(0.01);
if TGLCube(EntankList[7]).position.x<=-20 then begin Right1:=false; left1:=true; end;
end;

if left1=true then begin
TGLCube(EntankList[7]).direction.x:=1;
TGLCube(EntankList[7]).direction.z:=0;
TGLCube(EntankList[7]).move(0.01);
if TGLCube(EntankList[7]).position.x>=10 then begin left1:=false; right1:=true; end;
end;
end;

//Tank3
if (Lives[8]=100)and (Died[8]=false)and (Visibled[8]=false)then begin
if down2=true then begin
TGLCube(EntankList[8]).direction.x:=0;
TGLCube(EntankList[8]).direction.z:=-1;
TGLCube(EntankList[8]).move(0.01) ;
if TGLCube(EntankList[8]).position.z<=30 then begin Down2:=false; up2:=true; end;
end;
if Up2=true then begin
TGLCube(EntankList[8]).direction.x:=0;
TGLCube(EntankList[8]).direction.z:=1;
TGLCube(EntankList[8]).move(0.01);
if TGLCube(EntankList[8]).position.z>=56 then begin Up2:=false; down2:=true; end;
end;
end;

//Tank 4
if (Lives[6]=100)and (Died[6]=false)and (Visibled[6]=false)then begin
if down2=true then begin
TGLCube(EntankList[6]).direction.x:=0;
TGLCube(EntankList[6]).direction.z:=-1;
TGLCube(EntankList[6]).move(0.01) ;
if TGLCube(EntankList[6]).position.z<=30 then begin Down2:=false; up2:=true; end;
end;
if Up2=true then begin
TGLCube(EntankList[6]).direction.x:=0;
TGLCube(EntankList[6]).direction.z:=1;
TGLCube(EntankList[6]).move(0.01);
if TGLCube(EntankList[6]).position.z>=56 then begin Up2:=false; down2:=true; end;
end;
end;

//Tank-5
if (Lives[1]=100)and (Died[1]=false)and (Visibled[1]=false) then begin
if right3=true then begin
TGLCube(EntankList[1]).direction.x:=-1;
TGLCube(EntankList[1]).direction.z:=0;
TGLCube(EntankList[1]).move(0.01);
if TGLCube(EntankList[1]).position.x<=-25 then begin Right3:=false; down3:=true; end;
end;
if down3=true then begin
TGLCube(EntankList[1]).direction.x:=0;
TGLCube(EntankList[1]).direction.z:=-1;
TGLCube(EntankList[1]).move(0.01) ;
if TGLCube(EntankList[1]).position.z<=70 then begin Down3:=false; Left3:=true; end;
end;
if left3=true then begin
TGLCube(EntankList[1]).direction.x:=1;
TGLCube(EntankList[1]).direction.z:=0;
TGLCube(EntankList[1]).move(0.01);
if TGLCube(EntankList[1]).position.x>=10 then begin left3:=false; up3:=true; end;
end;
if Up3=true then begin
TGLCube(EntankList[1]).direction.x:=0;
TGLCube(EntankList[1]).direction.z:=1;
TGLCube(EntankList[1]).move(0.01);
if TGLCube(EntankList[1]).position.z>=75 then begin Up3:=false; right3:=true; end;
end;
end;



for i:=0 to EntankList.Count-1 do begin
if (lives[i]<>100) or (Visibled[i]=true) then begin
s1:=TGLCube(EntankList[i]).DistanceTo(UPlayerTank.Player); //player
s2:=TGLCube(EntankList[i]).DistanceTo(UPlayerTank.Player2); //player 2)
s3:=TGLCube(EntankList[i]).DistanceTo(UPlayerTank.Player3); //player 2)
if (s1<s2) and (s1<s3)  then obj:=UPlayerTank.Player;
if (s2<s1) and (s2<s3) then obj:=UPlayerTank.Player2;
if (s3<s1) and (s3<s2)  then obj:=UPlayerTank.Player3;
MakeVector(x,obj.AbsolutePosition);
MakeVector(y,TGLCube(EntankList[i]).AbsolutePosition);
z:=VectorSubtract(y,x);
NormalizeVector(z);
TGLCube(EntankList[i]).Direction.X:=-z[0];
TGLCube(EntankList[i]).Direction.z:=-z[2];
s4:=TGLCube(EntankList[i]).distanceTo(obj);
if s4>40 then
if (TGLCube(EntankList[i]).Position.X<30) and (TGLCube(EntankList[i]).Position.X>-30)
    and (TGLCube(EntankList[i]).Position.z>-58) and (TGLCube(EntankList[i]).Position.z<223) and (died[i]=false) then begin

if (TGLCube(World.StenaList[0]).RayCastIntersect(TGLCube(EnTankList[i]).AbsolutePosition,TGLCube(EnTankList[i]).AbsoluteDirection,@pPoint,nil))
then begin
if TGLCube(EnTankList[i]).DistanceTo(TGLCube(World.StenaList[0]))>=TGLCube(EnTankList[i]).DistanceTo(obj) then b:=true else b:=false;
end else
b:=true;

if b=true then begin TGLCube(EntankList[i]).move(0.03);  end;

end;
end;
end;
end;

If World.MissionNumber=2 then begin
for i:=0 to EntankList.Count-1 do begin
if (died[i]=false) and ((lives[i]<>100) or (Visibled[i]=true)) then begin
s1:=TGLCube(EntankList[i]).DistanceTo(UPlayerTank.Player); //player
s2:=TGLCube(EntankList[i]).DistanceTo(UPlayerTank.Player2); //player 2)
s3:=TGLCube(EntankList[i]).DistanceTo(UPlayerTank.Player3); //player 2)
if (s1<s2) and (s1<s3)  then obj:=UPlayerTank.Player;
if (s2<s1) and (s2<s3) then obj:=UPlayerTank.Player2;
if (s3<s1) and (s3<s2)  then obj:=UPlayerTank.Player3;

MakeVector(x,obj.AbsolutePosition);
MakeVector(y,TGLCube(EntankList[i]).AbsolutePosition);
z:=VectorSubtract(y,x);
NormalizeVector(z);
TGLCube(EntankList[i]).Direction.X:=-z[0];
TGLCube(EntankList[i]).Direction.z:=-z[2];

s4:=TGLCube(EntankList[i]).distanceTo(obj);
if s4>40 then

if (TGLCube(World.StenaList[0]).RayCastIntersect(TGLCube(EnTankList[i]).AbsolutePosition,TGLCube(EnTankList[i]).AbsoluteDirection,@pPoint,nil))
then begin
if TGLCube(EnTankList[i]).DistanceTo(TGLCube(World.StenaList[0]))>=TGLCube(EnTankList[i]).DistanceTo(obj) then b:=true else b:=false;
end else
b:=true;

if (b=true) or (b1=true) then begin if s4>40 then  TGLCube(EntankList[i]).move(0.03);  end;

end else begin
TGLCube(EnTAnkList[i]).Direction.SetVector(0,0,-1);
TGLCube(EnTankList[i]).Move(0.02);

end;



If (UPlayerTAnk.Live1=0) and (UPlayerTAnk.Live1=0) and (UPlayerTAnk.Live1=0) then begin
TGLCube(EnTAnkList[i]).Direction.SetVector(0,0,-1);
TGLCube(EnTankList[i]).Move(0.02);
end;

end;
end;


end;




procedure TEnemyTank.Move_EnTank_Pula;
var i,j,k: integer;
    Dist: single;
begin
for j:=0 to EntankList.Count-1 do begin
k:=0;
for i:=0 to TPulaList.Count-1 do begin
dist:=TGLCube(EnTankList[k+j]).DistanceTo(TGLSphere(TPulaList[i]));
if dist<250 then begin
TGLSphere(TPulaList[i]).Move(0.05);
end else begin
TGLSphere(TPulaList[i]).Free;
TPulaList[i]:=nil;
end;
end;
TPulaList.Pack;
end;



end;

procedure TEnemyTank.Pli_Enemy_Tank(num: integer);
begin
if TTTimer[num]>13 then begin
TTTImer[num]:=0;
kol[num]:=kol[num]+1;
TankPula:=TGLSphere.CreateAsChild(form1.GLScene1.Objects);
with TankPula do begin
AbsolutePosition:=TGLCube(EnTankList[num]).AbsolutePosition;
AbsoluteDirection:=TGLCube(EnTankList[num]).AbsoluteDirection;
Radius:=0.6;
tag:=18;
end;

with GetOrCreateDCEDynamic(TankPula) do begin
manager:=form1.GLDCEManager1;
Size.SetVector(0.21,0.21,0.21);
end;
TPulaList.add(TankPula);
end;

end;


end.

