unit AI;

interface

uses
  perso,
  System.Collections,
  System.ComponentModel,
  System.Data,
  System.Drawing,
  System.Windows.Forms,
  System.IO,
  Microsoft.DirectX,
  Microsoft.DirectX.DirectInput,
  Microsoft.DirectX.Direct3D;

type
  TCop = record
    pos : Tpos;
    pos_central : Tpos;
    mesh : mesh;
  end;

  TAI = class
    Cops : array[0..20] of TCop;
    nbCops : integer;
    mesh : mesh;
    materialarray : array of ExtendedMaterial;
    meshmaterials : array[0..0] of material;
    meshtextures : array[0..0] of texture;
    constructor create;
    procedure initialize(vecteur : array of string;TailleX,TailleZ:integer; device : device);
    procedure ia();
  end;

implementation

constructor TAI.create;
begin
  inherited;
end;

procedure TAI.initialize(vecteur : array of string; TailleX,TailleZ:integer;device:device);
var
  x,z : integer;
  vertices : vertexbuffer;
  stream : GraphicsStream;
begin
  z := 0;
  nbCops := 0;
  while z < TailleZ do
  begin
    x:= 1;
    while x <= TailleX do
    begin
      if vecteur[z][x] = 'a' then
      begin
        Cops[nbCops].pos_central.x := x;
        Cops[nbCops].pos_central.z := z;
        Cops[nbCops].pos_central.y := 0;
        Cops[nbCops].pos.x := Cops[nbCops].pos_central.x-1;
        Cops[nbCops].pos.z := Cops[nbCops].pos_central.z-1;
        Cops[nbCops].pos.y := 0;
        Cops[nbCops].pos.angle := 2*math.pi;
        nbCops := nbCops +1;
      end;
      x:=x+1;
    end;
    z:=z+1;
  end;
  mesh := Mesh.FromFile('texture/mesh/' + 'tiger.x', MeshFlags.Managed, device, materialarray);

  meshmaterials[0] := materialarray[0].Material3D;
  meshmaterials[0].Ambient := meshmaterials[0].Diffuse;

  if ((materialarray[0].TextureFilename <> nil) and (materialarray[0].TextureFilename <> '')) then
    meshtextures[0] := TextureLoader.FromFile(device,'texture/mesh/' + materialarray[0].TextureFilename);

  mesh := mesh.Clone(mesh.Options.Value, CustomVertex.PositionNormalTextured.Format, device);
  mesh.ComputeNormals();
  vertices := mesh.VertexBuffer;
  stream := vertices.Lock(0, 0, LockFlags.None);

  vertices.Unlock();
end;

procedure TAI.ia();
var
  i: integer;
begin
  i := 0;
  while i <= nbCops do
  begin
    if (Cops[i].pos.x <= Cops[i].pos_central.x-1) and (Cops[i].pos.z > Cops[i].pos_central.z-1) and (Cops[i].pos.z < Cops[i].pos_central.z+1) then
      Cops[i].pos.z := Cops[i].pos.z+0.05;
    if (Cops[i].pos.x <= Cops[i].pos_central.x-1) and (Cops[i].pos.z >= Cops[i].pos_central.z+1) and (Cops[i].pos.angle < math.pi/2) then
      Cops[i].pos.angle := Cops[i].pos.angle+0.05;
    if (Cops[i].pos.x <= Cops[i].pos_central.x-1) and (Cops[i].pos.z >= Cops[i].pos_central.z+1) and (Cops[i].pos.angle >= math.pi/2) then
    begin
      Cops[i].pos.angle := math.pi/2;
      Cops[i].pos.x := Cops[i].pos.x+0.05
    end;

    if (Cops[i].pos.z >= Cops[i].pos_central.z+1) and (Cops[i].pos.x > Cops[i].pos_central.x-1) and (Cops[i].pos.x < Cops[i].pos_central.x+1) then
      Cops[i].pos.x := Cops[i].pos.x+0.05;
    if (Cops[i].pos.x >= Cops[i].pos_central.x+1) and (Cops[i].pos.z >= Cops[i].pos_central.z+1) and (Cops[i].pos.angle < math.pi) then
      Cops[i].pos.angle := Cops[i].pos.angle+0.05;
    if (Cops[i].pos.x >= Cops[i].pos_central.x+1) and (Cops[i].pos.z >= Cops[i].pos_central.z+1) and (Cops[i].pos.angle >= math.pi) then
    begin
      Cops[i].pos.angle := math.pi;
      Cops[i].pos.z := Cops[i].pos.z-0.05
    end;

    if (Cops[i].pos.x >= Cops[i].pos_central.x+1) and (Cops[i].pos.z > Cops[i].pos_central.z-1) and (Cops[i].pos.z < Cops[i].pos_central.z+1) then
      Cops[i].pos.z := Cops[i].pos.z-0.05;
    if (Cops[i].pos.x >= Cops[i].pos_central.x+1) and (Cops[i].pos.z <= Cops[i].pos_central.z-1) and (Cops[i].pos.angle < 3*math.pi/2) then
      Cops[i].pos.angle := Cops[i].pos.angle+0.05;
    if (Cops[i].pos.x >= Cops[i].pos_central.x+1) and (Cops[i].pos.z <= Cops[i].pos_central.z-1) and (Cops[i].pos.angle >= 3*math.pi/2) then
    begin
      Cops[i].pos.angle := 3*math.pi/2;
      Cops[i].pos.x := Cops[i].pos.x-0.05
    end;

    if (Cops[i].pos.z <= Cops[i].pos_central.z-1) and (Cops[i].pos.x > Cops[i].pos_central.x-1) and (Cops[i].pos.x < Cops[i].pos_central.x+1) then
      Cops[i].pos.x := Cops[i].pos.x-0.05;
    if (Cops[i].pos.x <= Cops[i].pos_central.x-1) and (Cops[i].pos.z <= Cops[i].pos_central.z-1) and (Cops[i].pos.angle < 2*math.pi) then
      Cops[i].pos.angle := Cops[i].pos.angle+0.05;
    if (Cops[i].pos.x <= Cops[i].pos_central.x-1) and (Cops[i].pos.z <= Cops[i].pos_central.z-1) and (Cops[i].pos.angle >= 2*math.pi) then
    begin
      Cops[i].pos.angle := 0;
      Cops[i].pos.z := Cops[i].pos.z+0.05
    end;

    i := i +1;
  end;
end;

end.
