unit Model;

interface

uses
  SysUtils, Graphics, GLTexture, GLScene,
  GLObjects, GLGeomObjects, Forms,
  Jpeg, OpenGL1x, GLMisc, GLfileSMD,
  loader, GLVectorFileObjects,
  Map, GLProxyObjects, MForm;
type

  TEtDecObjs=record
  F : TGLFreeForm;
  Name : string;
  C  : byte;
  R  : real;
  end;

  TDekor=record
     F : TGLFreeFormProxy;
     Et : byte;
  end;

  TPoint=record
    X, Y, Z : real;
  end;

  TOpt=record
    live, u, speed  : byte;
  end;

  TliveObj=record
    Name, ModelFN : String;
    NPC : boolean;
    D: array[1..3] of String;
    A : TGLActor;
    O : Topt;
    Au : Tpoint;
    R, H : real;
  end;

  TPers=record
    A : TGLActorProxy;
    Au : TGLCylinder;

    O : Topt;
    Et, Talk : byte;
    
    MA : integer;
    MP : Tpoint;
    Moved  : boolean;
  end;

  TTruba=record
    F, O : TGLFreeFormProxy;
    Au : TGLCylinder;
    Et : byte;
  end;

  Tcorob=record
    F  : TGLActor;
    L  : string;
    Br : boolean;
  end;

  var
    EtLiveObj : array[1..10] of TliveObj;
    Pers: array[1..10] of Tpers;

    EtDecO : array[1..20] of   TEtDecObjs;
    DecO : array[1..40] of TDekor;
    mr : TGLFreeForm;
    Truba: array[1..40] of TTruba;
    Corob : array[1..10] of TCorob;
    EtCorob : TGLActor;
    Etalon : TGLDummyCube;
    MofEt, MofP, MofED, MofD, MofT, MofCrb : byte;

procedure loadft(N : byte);
procedure loadlo(FN : string);
procedure creatPers(T : byte; X, Y : real);
procedure loaddecor(FN : string);
procedure creatdecor(T : byte; X, Y : real);
procedure creatTruba(T : byte; X, Y : real);
Procedure CreatCorob(X,Y : real; S : string);

implementation
uses logaut;

procedure loadft(N : byte);
var
  i : byte;
begin
  SetCurrentDir(ExtractFilePath(Application.ExeName)+'Data\temp\');
  EtLiveObj[MofEt].A:=TGLActor(Etalon.AddNewChild(TGLActor));
  EtLiveObj[MofEt].A.Name:='ETA'+inttostr(1000+MofEt);
  EtLiveObj[MofEt].A.MaterialLibrary:=Oform.ML3;
  EtLiveObj[MofEt].A.LoadFromFile('m.smd');
  
//  EtLiveObj[MofEt].A.Material.Texture.Image.LoadFromFile('jpg.jpg');
  EtLiveObj[MofEt].A.Material.Texture.Disabled:=false;
  EtLiveObj[MofEt].A.Scale.SetVector(0.01,0.01,0.01);
  for i:=0 to N do
  if FileExists(inttostr(i)+'.smd') then
  EtLiveObj[MofEt].A.AddDataFromFile(inttostr(i)+'.smd');
  EtLiveObj[MofEt].A.SwitchToAnimation('1');
  EtLiveObj[MofEt].A.AnimationMode:=aamLoop;
end;

procedure loadlo(FN : string);
var
  F : textfile;
  i : integer;
  S : string;
begin
  assign(F,FN);
  reset(F);
  readln(F,S);
  if S='__cdmlex live obj__' then
  begin
    MofEt:=MofEt+1;
    readln(F,  EtLiveObj[MofEt].Name);
    readln(F,  EtLiveObj[MofEt].ModelFN);
    if UnZ(EtLiveObj[MofEt].ModelFN) then
    if EtLiveObj[MofEt].Name='Герой' then
    loadft(5) else loadft(5);
    readln(F,  EtLiveObj[MofEt].O.live);
    readln(F,  S);
    if S='TRUE' then
    EtLiveObj[MofEt].NPC:=true else
    EtLiveObj[MofEt].NPC:=false;

    readln(F,  EtLiveObj[MofEt].O.u);
    readln(F,  EtLiveObj[MofEt].O.speed);

    readln(F,  EtLiveObj[MofEt].R);
    EtLiveObj[MofEt].R:=EtLiveObj[MofEt].R/35;
    readln(F,  EtLiveObj[MofEt].H);
    EtLiveObj[MofEt].H:=EtLiveObj[MofEt].H/35;

    readln(F,  EtLiveObj[MofEt].Au.X);
    EtLiveObj[MofEt].Au.X:=EtLiveObj[MofEt].Au.X/70;
    readln(F,  EtLiveObj[MofEt].Au.Y);
    EtLiveObj[MofEt].Au.X:=EtLiveObj[MofEt].Au.Y/70;
    readln(F,  EtLiveObj[MofEt].Au.Z);
    EtLiveObj[MofEt].Au.X:=EtLiveObj[MofEt].Au.Z/70;


    for i:=1 to 3 do
    readln(F, EtLiveObj[MofEt].D[i]);

   end;
   closefile(F);
end;

procedure creatPers(T : byte; X, Y : real);
begin
   MofP:=MofP+1;
   Pers[MofP].Et:=T;
   Pers[MofP].O:=EtLiveObj[T].O;
   Pers[MofP].A:=TGLActorProxy(L.GLVis.AddNewChild(TGLActorProxy));
   Pers[MofP].A.Name:='MPP'+inttostr(1000+MofP);
    if EtLiveObj[T].A<>nil then
    With Pers[MofP].A do
    begin
      ProxyOptions:=[pooObjects];
      MasterObject:=EtLiveObj[T].A;
      Direction:=EtLiveObj[T].A.Direction;
      Up:=EtLiveObj[T].A.Up;
      Position.SetPoint(X,Y,0);
      Scale.SetVector(0.021,0.021,0.021);
      Animation:='1';
    end;
    Pers[MofP].Moved:=false;
    Pers[MofP].Au:=TGLCylinder(L.GLUnVis.AddNewChild(TGLCylinder));
    Pers[MofP].Au.Name:='APP'+inttostr(1000+MofP);
    Pers[MofP].Au.PitchAngle:=90;
    Pers[MofP].Au.TopRadius:=EtLiveObj[T].R;
    Pers[MofP].Au.BottomRadius:=Pers[MofP].Au.TopRadius;
    Pers[MofP].Au.Height:=EtLiveObj[T].H;
    Pers[MofP].Au.Position.X:=Pers[MofP].A.Position.X-EtLiveObj[T].Au.X;
    Pers[MofP].Au.Position.Y:=Pers[MofP].A.Position.Y-EtLiveObj[T].Au.Y;
    Pers[MofP].Au.Position.Z:=Pers[MofP].Au.Height/2-EtLiveObj[T].Au.Z;
    Pers[MofP].Talk:=1;

end;

procedure loaddecor(FN : string);
var
  F : textfile;
  i : integer;
  S : string;
begin
  MofED:=MofED+1;
  EtDecO[MofED].F:=TGLFreeForm(Etalon.AddNewChild(TGLFreeForm));
  EtDecO[MofED].F.Name:='ETD'+inttostr(1000+MofED);
  EtDecO[MofED].F.LoadFromFile(FN+'.3ds');
  if FileExists(FN+'.jpg') then
  EtDecO[MofED].F.Material.Texture.Image.LoadFromFile(FN+'.jpg');
  EtDecO[MofED].F.Material.Texture.Disabled:=false;
  if FileExists(FN+'.txt') then
  begin
    assign(F,FN+'.txt');
    reset(F);
    readln(F, EtDecO[MofED].Name);
    readln(F, S);
    if S='r' then EtDecO[MofED].C:=1;
    if S='c' then EtDecO[MofED].C:=2;
    readln(F, EtDecO[MofED].R);
    closefile(F);
    EtDecO[MofED].R:=EtDecO[MofED].R/25;
    
  end;
end;

procedure creatdecor(T : byte; X, Y : real);
begin
   MofD:=MofD+1;
   DecO[MofD].Et:=T;
   DecO[MofD].F:=TGLFreeFormProxy(L.GLVis.AddNewChild(TGLFreeFormProxy));
   DecO[MofD].F.Name:='MPD'+inttostr(1000+MofD);
    if EtDecO[T].F<>nil then
    With DecO[MofD].F do
    begin
      ProxyOptions:=[pooObjects];
      MasterObject:=EtDecO[T].F;
      Direction:=EtDecO[T].F.Direction;
      Up:=EtDecO[T].F.Up;
      Scale.SetVector(0.3,0.3,0.3);
      Position.SetPoint(X,Y,0);
    end;
end;

procedure creatTruba(T : byte; X, Y : real);
begin
   MofT:=MofT+1;
   Truba[MofT].Et:=T;
   Truba[MofT].F:=TGLFreeFormProxy(L.GLVis.AddNewChild(TGLFreeFormProxy));
   Truba[MofT].F.Name:='MTR'+inttostr(1000+MofT);

    if trm[T]<>nil then
    With Truba[MofT].F do
    begin
      ProxyOptions:=[pooObjects];
      MasterObject:=trm[T];
      Direction:=trm[T].Direction;
      Up:=trm[T].Up;
      Scale.SetVector(0.01,0.01,0.01);
      Position.SetPoint(X,Y,0);
    end;

    Truba[MofT].O:=TGLFreeFormProxy(Truba[MofT].F.AddNewChild(TGLFreeFormProxy));
    Truba[MofT].O.Name:='MMR'+inttostr(1000+MofT);

    With Truba[MofT].O do
    begin
      ProxyOptions:=[pooObjects];
      MasterObject:=mr;
      Direction:=mr.Direction;
      Up:=mr.Up;
    end;
{    Truba[MofT].Au:=TGLCylinder(L.GLUnVis.AddNewChild(TGLCylinder));
    Truba[MofT].Au.Name:='ATR'+inttostr(1000+MofT);
    Truba[MofT].Au.PitchAngle:=90;
    Truba[MofT].Au.TopRadius:=1;
    Truba[MofT].Au.BottomRadius:=1;
    Truba[MofT].Au.Position:=Truba[MofT].F.Position; }
end;

Procedure CreatCorob(X,Y : real; S : string);
var
FN : string;
begin
  MofCrb:=MofCrb+1;
  With Corob[MofCrb] do
  begin
    L:=S;
    F:=TGLActor(Map.L.GLWorld.AddNewChild(TGLActor));

    Unz('\Data\Models\cor.cdmlex');

    FN:=extractfilepath(Application.ExeName)+ '\Data\temp\cor.SMD';
    if FileExists(FN) then
    F.LoadFromFile(FN);
    F.Material.MaterialLibrary:=GLMlg;
    F.Material.LibMaterialName:='corob';
    FN:=extractfilepath(Application.ExeName)+ '\Data\temp\';
    if FileExists(FN+'cor1.SMD') then
    F.AddDataFromFile('cor1.SMD');
    if FileExists(FN+'cor2.SMD') then
    F.AddDataFromFile('cor2.SMD');
    Br:=true;
    F.Name:='COR'+inttostr(1000+MofCrb);
    F.Scale.SetVector(0.01,0.01,0.01);
    F.Position.SetPoint(X,Y,0);
   end; 

end;

end.
