unit GI;

interface

uses
  Windows, SysUtils, classes, Graphics, Controls,
  trigu,Baseclass,cont,EnemyHeap,mission,scanline_mask;

const
 LeftBar = 100;
 BottomBar = 18;

type
  TButtonUI = (tbN,tbE,tbA,tbAd,tbK,tbGO);
  Tkeys = record
    _new:boolean;
    _exit:boolean;
    _about:boolean;
    _add:boolean;
    _kill_all:boolean;
  end;

  TI_pos = (tiLeft,tiRight,tiUp,tidown);

  TCur = record
   show:boolean;
   x,y:integer;
  end;

  TweapPanel = record
   w1,w2,w3,w4,w5,w6:boolean;
   b1,b2,b3,b4,b5,b6:boolean;
  end;

  TFocus = record
   use:boolean;
   i:integer;
   _use_weap:boolean;
   _TW:TWEAP;
  end;

  TInterface = class
   private
     Cur:TCur;
     Focus:TFocus;
     _active_w:boolean;
     _x,_y:integer;
     _weap_count_buf:byte;
     
     tag:integer;
     _K:Tkeys;
     SB:TSoundBlock;
     procedure Draw_interface;
     procedure Draw_map;
     procedure Bux;
     procedure Draw_cur;
     procedure Draw_panel;
     procedure chPossmall(TI:TI_pos);
     procedure ClickMap(x,y:integer);
     procedure ClickPanel(x,y:integer);
     procedure Build_weap(TW:TWEAP; x,y:integer);
     procedure Draw_panel_weapons;
     procedure Draw_panel_base;
     procedure calculateBase(i:integer);
     procedure calculateEnergy(i:integer);
     procedure draw_GO;
     procedure Draw_Button;
     procedure ClickButton(x,y:integer);
     procedure CheckClickButton(_Button:TButtonUI);
   public
     _conter:TContener;
     _contE:TContenerEnemy;
     _BH:TBaseHouse;
     _Handle:HWND;
     
     _TBase:TBaseCenter;
     FOffscreenBuffer: TBitmap;
     F_back_buffer: TBitmap;
     FOutBuffer: TBitmap;
     _Missions:TMissions;
     _exit:boolean;
     Direc:string;
     procedure init(Direc:string;Handle:HWND);
     procedure bux_init;
     procedure Load_back(s:string);
     procedure AI;
     procedure chPos(TI:TI_pos);
     procedure draw(canvas:Tcanvas; Rect:Trect);
     procedure MoveCursor(x,y:integer);
     procedure ClickCursor(x,y:integer; Button: TMouseButton);
     procedure ClickKey(x:integer);
     procedure New_game;
     procedure SaveBitmap;
  end;

var  _Shadow_I:TShadow_sprite;  //бабах
     _Shadow_II:TShadow_sprite; //еще бабах
     _Shadow_Ext:TShadow_sprite; //бабах покруче
     _Shadow_Mine:TShadow_sprite; //фугасный бабах
     weapPanel:TweapPanel;
     _xx,_yy,_xz,_yz:integer;
implementation

uses main;


procedure FillTextrutka2canvas( Acanvas:Tcanvas; _Texturka:Tbitmap);
var i,ii:integer;
begin
for i:=0 to (Acanvas.ClipRect.Right div _Texturka.Width) do
 for ii:=0 to (Acanvas.ClipRect.Bottom div _Texturka.Height) do
 Acanvas.CopyRect(rect(i*_Texturka.Width,ii*_Texturka.Height,(i+1)*_Texturka.Width,(ii+1)*_Texturka.Height), _Texturka.Canvas ,rect(0,0,_Texturka.Width,_Texturka.Height));
end;

function InRect(x,y,left,top,right,down:integer):boolean;
begin
if (x>left)and(x<right)and(y>top)and(y<down) then InRect:=true else InRect:=false;
end;

procedure  TInterface.CheckClickButton(_Button:TButtonUI);
begin
case _button of  //  tbN,tbE,tbA,tbAd,tbK
  tbN: begin
     _k._new:=true;
     _k._exit:=true;
     _k._about:=true;
     _k._add:=true;
     _k._kill_all:=true;
  end;
  tbGO: begin
    _k._new:=true;
    _k._exit:=true;
    _k._about:=true;
    _k._add:=false;
    _k._kill_all:=false;
  end;
end;
end;

procedure  TInterface.SaveBitmap;
begin
FOutBuffer.SaveToFile('C:\sa'+floattostr(time)+'.bmp');
end;

procedure  TInterface.bux;
begin
if _xz>0 then begin if (_xz div 2)*2=_xz then _xx:=_xz else _xx:=-_xz; _xz:=_xz-1; end;
if _yz>0 then begin if (_yz div 2)*2=_yz then _yy:=_yz else _yy:=-_yz; _yz:=_yz-1; end;
end;

procedure  TInterface.Build_weap(TW:TWEAP; x,y:integer);
var i:integer;
begin
for i:=0 to COUNT_WEAP-1 do
 if not(_conter._List[i]._visible) then begin
  _conter.addweap(i,x,y,TW);
  _conter._List[i]._wait:=false;
  exit;
end;
end;

procedure  TInterface.bux_init;
begin
_xz:=5+random(2);  _yz:=5+random(2);
end;

procedure  TInterface.chPossmall(TI:TI_pos);
begin
case TI of
  tiLeft: _x:=_x-1;
  tiRight: _x:=_x+1;
  tiUp:  _y:=_y-1;
  tidown: _y:=_y+1;
end;
if _x>(FOffscreenBuffer.Width-FOutBuffer.Width) then _x:=FOffscreenBuffer.Width-FOutBuffer.Width;
if _x<0 then _x:=0;
if _y>(FOffscreenBuffer.Height-FOutBuffer.Height) then _y:=FOffscreenBuffer.Height-FOutBuffer.Height;
if _y<0 then _y:=0;
end;

procedure  TInterface.chPos(TI:TI_pos);
begin
case TI of
  tiLeft: _x:=_x-10;
  tiRight: _x:=_x+10;
  tiUp:  _y:=_y-10;
  tidown: _y:=_y+10;
end;
if _x>(FOffscreenBuffer.Width-FOutBuffer.Width) then _x:=FOffscreenBuffer.Width-FOutBuffer.Width;
if _x<-LeftBar then _x:=-LeftBar;
if _y>(FOffscreenBuffer.Height-FOutBuffer.Height) then _y:=FOffscreenBuffer.Height-FOutBuffer.Height;
if _y<0 then _y:=0;
end;

procedure  TInterface.MoveCursor(x,y:integer);
begin
if not(Focus._use_weap) then _Tbase.CurRotate(x+_x,y+_y);
if not(cur.show) then exit;
Cur.x:=x;
Cur.y:=y;
end;

procedure  TInterface.ClickMap(x,y:integer);
var ii,iii:integer;
begin
 _x:=round(((x-5)-(((FOutBuffer.Width+LeftBar) div 2)*(90/1280) ))*(1280/90));
 _y:=round(((y-5)-(((FOutBuffer.Height) div 2)*(72/1024) ))*(1024/72));
 if _x>(FOffscreenBuffer.Width-FOutBuffer.Width) then _x:=FOffscreenBuffer.Width-FOutBuffer.Width;
 if _x<-LeftBar then _x:=-LeftBar;
 if _y>(FOffscreenBuffer.Height-FOutBuffer.Height) then _y:=FOffscreenBuffer.Height-FOutBuffer.Height;
 if _y<0 then _y:=0;
end;

procedure  TInterface.ClickPanel(x,y:integer);
var i_num:integer; _b:boolean;
begin
 i_num:=((x-5) div 45)+((y-87) div 45)*2;
 _b:=false;
 case i_num of
 0: begin _b:=weappanel.w1; Focus._TW:=twgun; end;
 1: begin _b:=weappanel.w2; Focus._TW:=twdgun; end;
 2: begin _b:=weappanel.w3; Focus._TW:=twliser; end;
 3: begin _b:=weappanel.w4; Focus._TW:=twdliser; end;
 4: begin _b:=weappanel.w5; Focus._TW:=twsnipergun; end;
 5: begin _b:=weappanel.w6; Focus._TW:=twbazooka; end;
 end;
 if _b then begin
 Focus.use:=true;
 Focus._use_weap:=true;
 Cur.show:=true;
 end;
end;

procedure  TInterface.ClickButton(x,y:integer);
var i_num:integer;
begin
 if InRect(x,y,10,FOutBuffer.Height-104,LeftBar-10,FOutBuffer.Height-88) then begin New_game; CheckClickButton(tbN); end;
 if InRect(x,y,10,FOutBuffer.Height-84,LeftBar-10,FOutBuffer.Height-68)and(_active_w) then  _conte.Rushing(7);
// if InRect(x,y,10,FOutBuffer.Height-64,LeftBar-10,FOutBuffer.Height-48) then _Missions.Rush( teMeloch,tpu1,50 );//
 if InRect(x,y,10,FOutBuffer.Height-44,LeftBar-10,FOutBuffer.Height-28) then _conte.clear;
 if InRect(x,y,10,FOutBuffer.Height-24,LeftBar-10,FOutBuffer.Height-8) then _exit:=true;
end;

procedure  TInterface.ClickKey(x:integer);
begin
 //Focus.use:=false;
 case x of
 0: begin Focus._use_weap:=false; Cur.show:=false; end;
 end;
end;

procedure  TInterface.ClickCursor(x,y:integer; Button: TMouseButton);
begin
if InRect(x,y,0,0,LeftBar,FOutBuffer.Height) then begin
 if InRect(x,y,5,5,LeftBar-5,72)and(_active_w) then begin ClickMap(x,y); exit; end;
 if InRect(x,y,5,87,LeftBar-5,222)and(_active_w) then begin ClickPanel(x,y); exit; end;
 if InRect(x,y,10,FOutBuffer.Height-104,LeftBar-10,FOutBuffer.Height-8) then begin ClickButton(x,y);  exit;end;
end else begin

 if InRect(x,y,LeftBar,0,FOutBuffer.Width,FOutBuffer.Height-BottomBar) then begin

 if (Focus.use)and(Focus._use_weap)and(_active_w) then begin
  Build_weap(Focus._TW,(((_x+x)div 24)*24)+12,(((_y+y)div 24)*24)+12);
 end;                  //(((_x+x)div 24)*24) (((_y+y)div 24)*24)
 if (not(Focus._use_weap))and(_active_w) then
   if Button=mbLeft then
    _Tbase.Fire(_contE,x+_x,y+_y,FOffscreenBuffer.Canvas,F_back_buffer.Canvas) else
    _Tbase.Add_mine(x+_x,y+_y);

 Cur.show:=false;
 Focus.use:=false;
 Focus._use_weap:=false;

 end;
end;

end;

procedure  TInterface.Load_back(s:string);
var BT:Tbitmap;
begin
BT:=TBitmap.Create;
try
if fileexists(s) then begin BT.LoadFromFile(s);
FillTextrutka2canvas(F_back_buffer.Canvas,bt); end;
finally
BT.Free;
end;
end;

procedure TInterface.init(Direc:string;Handle:HWND);
begin
FOutBuffer:=TBitmap.Create;
FOffscreenBuffer:=TBitmap.Create;
FOffscreenBuffer.Width:=1280;
FOffscreenBuffer.Height:=1024;
F_back_buffer:=TBitmap.Create;
F_back_buffer.Width:=1280;
F_back_buffer.Height:=1024;
_Conter:= TContener.Create;
_ContE:=TContenerEnemy.Create;
_BH:=TBaseHouse.Create;
_TBase:=TBaseCenter.Create;
_Conter.init;
_ContE.init;
//_ContE._Bt.LoadFromFile(direc+'al.bmp');
_TBase.Init;
_BH.Init;
_BH.XPos:=640;
_TBase.YPos:=512;
_TBase.XPos:=640;
_TBase._level:=0;
_BH.YPos:=512;
_exit:=false;
_Handle:=Handle;
_Shadow_I:=TShadow_sprite.Create;
_Shadow_I.Init;
_Shadow_I.Fill_shadow(direc+'gfx/I.bmp');

_Shadow_Mine:=TShadow_sprite.Create;
_Shadow_Mine.Init;
_Shadow_Mine.Fill_shadow(direc+'gfx/mine.bmp');

_Shadow_II:=TShadow_sprite.Create;
_Shadow_II.Init;
_Shadow_II.Fill_shadow(direc+'gfx/II.bmp');

_Shadow_Ext:=TShadow_sprite.Create;
_Shadow_Ext.Init;
_Shadow_Ext.Fill_shadow(direc+'gfx/Is1.bmp');

_xx:=0;
_yy:=0;
_xz:=0;
_yz:=0;
Cur.show:=false;
_weap_count_buf:=0;
_active_w:=true;
_K._new:=true;
_K._exit:=true;
_K._about:=true;
_K._add:=true;
_K._kill_all:=true;
SB:=TsoundBlock.Create;
SB._on:=true;
SB._loop:=false;
SB._playing:=false;
sb._Handle:=_Handle;
sb._direc:=Direc;
_Missions:=TMissions.Create;
_Missions.Init(_ContE);
_x:=0;
_y:=0;
end;

procedure  TInterface.AI; //мдя...
begin
//
end;

procedure  TInterface.New_game;
var i:integer;
begin
 for i:=0 to COUNT_WEAP-1 do begin
 _conter._List[i]._visible:=false;
 _conter._list[i].XPos:=0;
 _conter._list[i].YPos:=0;
 _conter._list[i]._radius:=10;
 _conter._list[i]._speed_rot:=1;
 _conter._list[i]._visible:=false;

 _conter._list[i].Angle:=0;
 _conter._list[i].Ammo:=0;
 _conter._list[i]._sector:=2;
 _conter._list[i]._Type:=twgun;
 _conter._list[i]._wait:=true;
 _conter._list[i]._target:=-1;
 _conter._list[i]._OnTarget:=false;
 _conter._list[i]._level:=0;
 _conter._list[i]._killed:=0;
 end;
 weapPanel.w1:=false; weapPanel.w2:=false;weapPanel.w3:=false;weapPanel.w4:=false;weapPanel.w5:=false;weapPanel.w6:=false;
 weapPanel.b1:=false; weapPanel.b2:=false;weapPanel.b3:=false;weapPanel.b4:=false;weapPanel.b5:=false;weapPanel.b6:=false;
 _conte.clear;
 _missions._timer:=1300;
 _BH.helth:=100;
 _BH._active:=true;
_BH._energy:=true;
_tbase._killed:=0;
_tbase._level:=0;
load_back(direc+'gfx/main.bmp');
_active_w:=true;
_x:=640-(FOutBuffer.Width+leftbar) div 2;
_y:=512-(FOutBuffer.Height+bottombar) div 2;
for i:=0 to 7 do
 _tbase._mine[i].use:=false;
end;

procedure TInterface.Draw_map;
var i:integer; TE:TEnemy;
begin
if not(_active_w) then exit;
if _BH._radar then begin
 FOutBuffer.Canvas.Brush.Color:=$232F4A;
 FOutBuffer.Canvas.Brush.Style:=bssolid;
 FOutBuffer.Canvas.Pen.Color:=clmaroon;//$8B614E;
 FOutBuffer.Canvas.Rectangle(5,5,LeftBar-5,72);
 FOffscreenBuffer.Canvas.Brush.Style:=bsClear;
 FOffscreenBuffer.Canvas.Pen.Color:=clyellow;

if _BH._energy_level>700 then begin
for i:=0 to (_contE.List.Size div sizeof(TEnemy))-1 do begin
  _contE.List.Position:=sizeof(TEnemy)*i;
  _contE.List.ReadBuffer(te,sizeof(TEnemy));
  if te._use then
  if inrect(trunc(te.XPos),round(te.yPos),0,0,FOffscreenBuffer.Width,FOffscreenBuffer.Height) then
  FOutBuffer.Canvas.Pixels[ trunc(te.XPos*(90/1280))+5,trunc(te.yPos*(67/1024))+5]:=clred;
end;
end;

if _BH._energy_level>350 then begin
for i:=0 to COUNT_WEAP-1 do begin
  if _conter._List[i]._visible then
  if (_conter._List[i].XPos>0)and(_conter._List[i].yPos>0) then
  FOutBuffer.Canvas.Pixels[ trunc(_conter._List[i].XPos*(90/1280))+5,trunc(_conter._List[i].yPos*(90/1280))+5]:=cllime;
end;
end;

for i:=0 to mine_count-1 do
 if (_Tbase._mine[i].use)and(_Tbase._mine[i].enabled) then
 FOutBuffer.Canvas.Pixels[ trunc(_Tbase._mine[i].Coo.X*(90/1280))+5 ,trunc(_Tbase._mine[i].Coo.Y*(90/1280))+5]:=clblue;



 FOutBuffer.Canvas.Pen.Color:=$262115;
 FOutBuffer.Canvas.MoveTo(round((_x+LeftBar)*(90/1280))+5,round(_y*(72/1024))+5);
 FOutBuffer.Canvas.lineTo(round(((_x+LeftBar)+FOutBuffer.Width-30)*(90/1280)),round(_y*(72/1024))+5);
 FOutBuffer.Canvas.lineTo(round(((_x+LeftBar)+FOutBuffer.Width-30)*(90/1280)),round((_y+FOutBuffer.Height)*(72/1024)));
 FOutBuffer.Canvas.lineTo(round((_x+LeftBar)*(90/1280))+5,round((_y+FOutBuffer.Height)*(72/1024)));
 FOutBuffer.Canvas.lineTo(round((_x+LeftBar)*(90/1280))+5,round(_y*(72/1024))+5);
 end;
end;

procedure TInterface.Draw_panel;
var i:integer;
begin
if not(_active_w) then exit;
  FOutBuffer.Canvas.Brush.Style:=bssolid;
  FOutBuffer.Canvas.Pen.Color:=$8B614E;

  FOutBuffer.Canvas.Font.Size:=7;
 for i:=0 to 5 do begin
  case i of
   0: begin
      FOutBuffer.Canvas.Rectangle(5,87,((LeftBar-10) div 2)+5,45+87);
      if weappanel.w1 then FOutBuffer.Canvas.TextOut(5+ (((LeftBar-10) div 2)-FOutBuffer.Canvas.TextWidth('Gun')) div 2, 87+((45-FOutBuffer.Canvas.TextHeight('G')) div 2) ,'Gun' );
      end;
   1: begin
      FOutBuffer.Canvas.Rectangle(((LeftBar-10) div 2)+5,87,LeftBar-5,45+87);
      if weappanel.w2 then FOutBuffer.Canvas.TextOut(((LeftBar-10) div 2)+5+(((LeftBar-10) div 2)-FOutBuffer.Canvas.TextWidth('DGun')) div 2, 87+((45-FOutBuffer.Canvas.TextHeight('G')) div 2) ,'DGun' );
      end;
   2: begin
      FOutBuffer.Canvas.Rectangle(5,87+45,((LeftBar-10) div 2)+5,45*2+87);
      if weappanel.w3 then FOutBuffer.Canvas.TextOut(5+(((LeftBar-10) div 2)-FOutBuffer.Canvas.TextWidth('Liser')) div 2, 87+45+((45-FOutBuffer.Canvas.TextHeight('G')) div 2) ,'Liser' );
      end;
   3: begin
      FOutBuffer.Canvas.Rectangle(((LeftBar-10) div 2)+5,87+45,LeftBar-5,45*2+87);
      if weappanel.w4 then FOutBuffer.Canvas.TextOut(((LeftBar-10) div 2)+5+(((LeftBar-10) div 2)-FOutBuffer.Canvas.TextWidth('DLiser')) div 2, 87+45+((45-FOutBuffer.Canvas.TextHeight('G')) div 2) ,'DLiser' );
      end;
   4: begin
      FOutBuffer.Canvas.Rectangle(5,87+90,((LeftBar-10) div 2)+5,45*3+87);
      if weappanel.w5 then FOutBuffer.Canvas.TextOut(5+(((LeftBar-10) div 2)-FOutBuffer.Canvas.TextWidth('Sniper')) div 2, 87+90+((45-FOutBuffer.Canvas.TextHeight('G')) div 2) ,'Sniper' );
      end;
   5: begin
      FOutBuffer.Canvas.Rectangle(((LeftBar-10) div 2)+5,87+90,LeftBar-5,45*3+87);
      if weappanel.w6 then FOutBuffer.Canvas.TextOut(((LeftBar-10) div 2)+5+(((LeftBar-10) div 2)-FOutBuffer.Canvas.TextWidth('Bazooka')) div 2, 87+90+((45-FOutBuffer.Canvas.TextHeight('G')) div 2) ,'Bazooka' );
      end;
  end;
 end;

end;

procedure TInterface.Draw_Button;
var iix:integer;
begin
  if _K._new then begin
  FOutBuffer.Canvas.Brush.Style:=bssolid;
  FOutBuffer.Canvas.Brush.Color:=clblack;
  FOutBuffer.Canvas.Pen.Color:=clsilver;
  FOutBuffer.Canvas.Rectangle(10,FOutBuffer.Height-104,LeftBar-10,FOutBuffer.Height-88);
  FOutBuffer.Canvas.Font.Size:=7;
  FOutBuffer.Canvas.Font.Color:=clsilver;
  iix:=((LeftBar-FOutBuffer.Canvas.TextWidth('NEW GAME')) div 2);
  FOutBuffer.Canvas.TextOut(iix,FOutBuffer.Height-101,'NEW GAME');
  end;
  if _K._add then begin
  FOutBuffer.Canvas.Brush.Style:=bssolid;
  FOutBuffer.Canvas.Brush.Color:=clblack;
  FOutBuffer.Canvas.Pen.Color:=clsilver;
  FOutBuffer.Canvas.Rectangle(10,FOutBuffer.Height-84,LeftBar-10,FOutBuffer.Height-68);
  FOutBuffer.Canvas.Font.Size:=7;
  FOutBuffer.Canvas.Font.Color:=clsilver;
  iix:=((LeftBar-FOutBuffer.Canvas.TextWidth('RUSH!')) div 2);
  FOutBuffer.Canvas.TextOut(iix,FOutBuffer.Height-81,'RUSH!');
  end;
  if _K._about then begin
  FOutBuffer.Canvas.Brush.Style:=bssolid;
  FOutBuffer.Canvas.Brush.Color:=clblack;
  FOutBuffer.Canvas.Pen.Color:=clsilver;
  FOutBuffer.Canvas.Rectangle(10,FOutBuffer.Height-64,LeftBar-10,FOutBuffer.Height-48);
  FOutBuffer.Canvas.Font.Size:=7;
  FOutBuffer.Canvas.Font.Color:=clsilver;
  iix:=((LeftBar-FOutBuffer.Canvas.TextWidth('ABOUT')) div 2);
  FOutBuffer.Canvas.TextOut(iix,FOutBuffer.Height-61,'ABOUT');
  end;
  if _K._kill_all then begin
  FOutBuffer.Canvas.Brush.Style:=bssolid;
  FOutBuffer.Canvas.Brush.Color:=clblack;
  FOutBuffer.Canvas.Pen.Color:=clsilver;
  FOutBuffer.Canvas.Rectangle(10,FOutBuffer.Height-44,LeftBar-10,FOutBuffer.Height-28);
  FOutBuffer.Canvas.Font.Size:=7;
  FOutBuffer.Canvas.Font.Color:=clsilver;
  iix:=((LeftBar-FOutBuffer.Canvas.TextWidth('KILL')) div 2);
  FOutBuffer.Canvas.TextOut(iix,FOutBuffer.Height-41,'KILL');
  end;
  if _K._exit then begin
  FOutBuffer.Canvas.Brush.Style:=bssolid;
  FOutBuffer.Canvas.Brush.Color:=clblack;
  FOutBuffer.Canvas.Pen.Color:=clsilver;               //476
  FOutBuffer.Canvas.Rectangle(10,FOutBuffer.Height-24,LeftBar-10,FOutBuffer.Height-8);
  FOutBuffer.Canvas.Font.Size:=7;
  FOutBuffer.Canvas.Font.Color:=clsilver;
  iix:=((LeftBar-FOutBuffer.Canvas.TextWidth('EXIT')) div 2);
  FOutBuffer.Canvas.TextOut(iix,FOutBuffer.Height-21,'EXIT');
  end;
end;

function PaintWeaponPanel(Canvas:Tcanvas; TB:TBaseCenter; TTB:TBase; x:integer):boolean;
var _b:boolean;  i:integer;
begin
 _b:=true;
  Canvas.Font.Size:=6;
  Canvas.Font.Color:=clsilver;
  Canvas.brush.Color:=clblack;
  Canvas.Pen.Color:=clgray;   //359
  Canvas.Rectangle(10,x,LeftBar-10,x+23);
  Canvas.Font.Size:=5;
 if TB._reload<35 then  begin
  _b:=false;
  Canvas.TextOut(14,x+2,'RELOAD');
  Canvas.Pen.Color:=$51;
  Canvas.brush.Color:=$51;
   end else begin
  Canvas.TextOut(14,x+2,'AMMO: '+inttostr(TB.Ammo));
  Canvas.Pen.Color:=$606D0;
  Canvas.brush.Color:=$606D0;
   end;
  Canvas.Rectangle(LeftBar-30,x+15,LeftBar-24,x+19);
  Canvas.brush.Color:=clblack;
  for i:=1 to ((LeftBar-60)div 2) do begin
  if ((35-TB._reload)*(LeftBar-60)/35)>=i*2 then
   Canvas.Pen.Color:=clsilver else
   Canvas.Pen.Color:=clgray;
    Canvas.MoveTo(i*2+45, x+5);
    Canvas.LineTo(i*2+45, x+9);
  end;
 if TTB._rot then begin
  Canvas.TextOut(14,x+11,'ROTATE');
  Canvas.Pen.Color:=$51;
  Canvas.brush.Color:=$51;
  _b:=false;
  end else begin
  Canvas.Font.Color:=$AA00;
  Canvas.TextOut(14,x+11,'READY');
  Canvas.Pen.Color:=$606D0;
  Canvas.brush.Color:=$606D0;
  end;
  Canvas.Rectangle(LeftBar-20,x+15,LeftBar-14,x+19);
  Canvas.Brush.Color:=clblack;
  Canvas.Pen.Color:=clgray;
  result:=_b;
end;

procedure TInterface.Draw_panel_base;
var iix,i:integer;  _b:boolean;
begin
if not(_active_w) then exit;
  FOutBuffer.Canvas.Brush.Style:=bssolid;
  FOutBuffer.Canvas.Brush.Color:=clblack;
  FOutBuffer.Canvas.Pen.Color:=clgray;
  FOutBuffer.Canvas.Rectangle(5,276,LeftBar-5,492);

  FOutBuffer.Canvas.Font.Name:='lusida console';
  FOutBuffer.Canvas.Font.Size:=7;
  FOutBuffer.Canvas.Pen.Color:=$40;
  iix:=((LeftBar-FOutBuffer.Canvas.TextWidth('BASE')) div 2);
  FOutBuffer.Canvas.Font.Color:=$D7;
  FOutBuffer.Canvas.TextOut(iix,280,'BASE');

  FOutBuffer.Canvas.Font.Size:=5;
  FOutBuffer.Canvas.Font.Color:=clsilver;
  FOutBuffer.Canvas.TextOut(10,294,'ENERGY');

  for i:=0 to ((LeftBar-20)div 2) do begin
  if (_BH._energy_level*(LeftBar-20)/1024)>=i*2 then
   FOutBuffer.Canvas.Pen.Color:=$AA00 else
   FOutBuffer.Canvas.Pen.Color:=$5100;
    FOutBuffer.Canvas.MoveTo(i*2+10, 304);
    FOutBuffer.Canvas.LineTo(i*2+10, 308);
  end;

  FOutBuffer.Canvas.TextOut(10,315,'HELTH');

  for i:=0 to ((LeftBar-20)div 2) do begin
  if (_BH.helth*(LeftBar-20)/1024)>=i*2 then
   FOutBuffer.Canvas.Pen.Color:=$AA00 else
   FOutBuffer.Canvas.Pen.Color:=$5100;
    FOutBuffer.Canvas.MoveTo(i*2+10, 325);
    FOutBuffer.Canvas.LineTo(i*2+10, 329);
  end;

  FOutBuffer.Canvas.TextOut(10,334,'SHIELD');

  for i:=0 to ((LeftBar-20)div 2) do begin
  if (_BH.shield*(LeftBar-20)/1024)>=i*2 then
   FOutBuffer.Canvas.Pen.Color:=$AA00 else
   FOutBuffer.Canvas.Pen.Color:=$5100;
    FOutBuffer.Canvas.MoveTo(i*2+10, 344);
    FOutBuffer.Canvas.LineTo(i*2+10, 348);
  end;
   _b:=true;

 if PaintWeaponPanel(FOutBuffer.Canvas,_Tbase,_tbase._Weapon2,359) then begin
  FOutBuffer.Canvas.Pen.Color:=$CC00; FOutBuffer.Canvas.brush.Color:=$CC00; end else begin
  FOutBuffer.Canvas.Pen.Color:=clgreen; FOutBuffer.Canvas.brush.Color:=clgreen; _b:=false; end;
  FOutBuffer.Canvas.Rectangle(14,465,26,468);

 if _tbase._level>1 then begin
 if PaintWeaponPanel(FOutBuffer.Canvas,_Tbase,_tbase._Weapon1,385) then begin
  FOutBuffer.Canvas.Pen.Color:=$CC00; FOutBuffer.Canvas.brush.Color:=$CC00; end else begin
  FOutBuffer.Canvas.Pen.Color:=clgreen; FOutBuffer.Canvas.brush.Color:=clgreen; _b:=false; end;
  FOutBuffer.Canvas.Rectangle(34,465,46,468);
 end else begin
  FOutBuffer.Canvas.Brush.Color:=clblack;
  FOutBuffer.Canvas.Pen.Color:=clgray;
  FOutBuffer.Canvas.Rectangle(10,385,LeftBar-10,408); end;

 if _tbase._level>4 then begin
 if PaintWeaponPanel(FOutBuffer.Canvas,_Tbase,_tbase._Weapon3,411) then begin
  FOutBuffer.Canvas.Pen.Color:=$CC00; FOutBuffer.Canvas.brush.Color:=$CC00; end else begin
  FOutBuffer.Canvas.Pen.Color:=clgreen; FOutBuffer.Canvas.brush.Color:=clgreen; _b:=false; end;
  FOutBuffer.Canvas.Rectangle(54,465,66,468);
 end else begin
  FOutBuffer.Canvas.Brush.Color:=clblack;
  FOutBuffer.Canvas.Pen.Color:=clgray;
  FOutBuffer.Canvas.Rectangle(10,411,LeftBar-10,434); end;

 if _tbase._level>8 then begin
 if PaintWeaponPanel(FOutBuffer.Canvas,_Tbase,_tbase._Weapon4,437)then begin
  FOutBuffer.Canvas.Pen.Color:=$CC00; FOutBuffer.Canvas.brush.Color:=$CC00; end else begin
  FOutBuffer.Canvas.Pen.Color:=clgreen; FOutBuffer.Canvas.brush.Color:=clgreen; _b:=false; end;
  FOutBuffer.Canvas.Rectangle(74,465,86,468);
  end else begin
  FOutBuffer.Canvas.Brush.Color:=clblack;
  FOutBuffer.Canvas.Pen.Color:=clgray;
  FOutBuffer.Canvas.Rectangle(10,437,LeftBar-10,460); end;

  if _b then begin
   FOutBuffer.Canvas.Brush.Color:=clblack;
   FOutBuffer.Canvas.Pen.Color:=clgray;
   FOutBuffer.Canvas.Rectangle(10,472,LeftBar-10,488);
   FOutBuffer.Canvas.Font.Size:=6;
   FOutBuffer.Canvas.Font.Color:=cllime;
   FOutBuffer.Canvas.TextOut( (leftbar-FOutBuffer.Canvas.TextWidth('ACTIVATED')) div 2 ,475,'ACTIVATED'); //писец всем
  if _TBase._level>1 then
   if SB._on then SB.play(1);
  end else begin
   FOutBuffer.Canvas.Brush.Color:=clblack;
   FOutBuffer.Canvas.Pen.Color:=clgray;
   FOutBuffer.Canvas.Rectangle(10,472,LeftBar-10,488);
   FOutBuffer.Canvas.Font.Size:=6;
   FOutBuffer.Canvas.Font.Color:=clmaroon;
   SB._playing:=true;
   FOutBuffer.Canvas.TextOut( (leftbar-FOutBuffer.Canvas.TextWidth('PREPARATION')) div 2 ,475,'PREPARATION');
  end;
end;

procedure TInterface.Draw_panel_weapons;
const count_2_line = 12;
var i,iix:integer;
begin
if not(_active_w) then exit;
  FOutBuffer.Canvas.Brush.Style:=bssolid;
  FOutBuffer.Canvas.Pen.Color:=$40;
  FOutBuffer.Canvas.brush.Color:=clblack;
  FOutBuffer.Canvas.Rectangle(5,232,LeftBar-5,270);

  FOutBuffer.Canvas.Font.Name:='lusida console';
  FOutBuffer.Canvas.Font.Size:=6;
  iix:=((LeftBar-FOutBuffer.Canvas.TextWidth('ACTIVE WEAPON')) div 2);

  if _BH._active then  FOutBuffer.Canvas.Font.Color:=$D7 else FOutBuffer.Canvas.Font.Color:=$51;
  FOutBuffer.Canvas.TextOut(iix,235,'ACTIVE WEAPON');
  FOutBuffer.Canvas.Pen.Color:=$80;

  iix:=((LeftBar-count_2_line*7) div 2);

  for i:=0 to COUNT_WEAP-1 do begin
  if _conter._List[i]._visible then begin
  FOutBuffer.Canvas.Pen.Color:=$606D0; FOutBuffer.Canvas.brush.Color:=$51; end else begin
  FOutBuffer.Canvas.brush.Color:=$180C08; FOutBuffer.Canvas.Pen.Color:=$80; end;
  FOutBuffer.Canvas.Rectangle(iix+i*6+i,260,iix+(i+1)*6+i,260+6);

  if _conter._List[i]._wait then begin
  FOutBuffer.Canvas.Pen.Color:=$5100; FOutBuffer.Canvas.brush.Color:=$2200; end else begin
  FOutBuffer.Canvas.brush.Color:=$7700; FOutBuffer.Canvas.Pen.Color:=$CC00; end;
  FOutBuffer.Canvas.Ellipse(iix+i*6+i,250,iix+(i+1)*6+i,250+6);
  end;
end;

procedure TInterface.Draw_cur;
begin
if Cur.show then begin
  FOffscreenBuffer.Canvas.Brush.Style:=bsClear;
 if Focus._use_weap then begin
  FOffscreenBuffer.Canvas.Pen.Color:=claqua;
 case Focus._TW of
  twgun: FOffscreenBuffer.Canvas.Ellipse((((_x+cur.x)div 24)*24)-238,(((_y+cur.y)div 24)*24)-238,(((_x+cur.x)div 24)*24+24)+238,(((_y+cur.y)div 24)*24+24)+238);
  twliser: FOffscreenBuffer.Canvas.Ellipse((((_x+cur.x)div 24)*24)-258,(((_y+cur.y)div 24)*24)-258,(((_x+cur.x)div 24)*24+24)+258,(((_y+cur.y)div 24)*24+24)+258);
  twbazooka: FOffscreenBuffer.Canvas.Ellipse((((_x+cur.x)div 24)*24)-298,(((_y+cur.y)div 24)*24)-298,(((_x+cur.x)div 24)*24+24)+298,(((_y+cur.y)div 24)*24+24)+298);
  twdgun: FOffscreenBuffer.Canvas.Ellipse((((_x+cur.x)div 24)*24)-178,(((_y+cur.y)div 24)*24)-178,(((_x+cur.x)div 24)*24+24)+178,(((_y+cur.y)div 24)*24+24)+178);
  twsnipergun: FOffscreenBuffer.Canvas.Ellipse((((_x+cur.x)div 24)*24)-308,(((_y+cur.y)div 24)*24)-308,(((_x+cur.x)div 24)*24+24)+308,(((_y+cur.y)div 24)*24+24)+308);
  twdliser: FOffscreenBuffer.Canvas.Ellipse((((_x+cur.x)div 24)*24)-198,(((_y+cur.y)div 24)*24)-198,(((_x+cur.x)div 24)*24+24)+198,(((_y+cur.y)div 24)*24+24)+198);
  end;
 end;
  FOffscreenBuffer.Canvas.Pen.Color:=clyellow;

  FOffscreenBuffer.Canvas.Rectangle(
    (((_x+cur.x)div 24)*24),  
    (((_y+cur.y)div 24)*24),
    (((_x+cur.x)div 24)*24+24),
    (((_y+cur.y)div 24)*24+24)
    );
 end;
end;

procedure TInterface.Draw_interface;
var i:integer;
begin
 i:=BottomBar div 2;//trunc(FOutBuffer.Canvas.TextHeight('8') div 2)+3;
 FOutBuffer.Canvas.Brush.Color:=$180C08;
 FOutBuffer.Canvas.Brush.Style:=bssolid;
 FOutBuffer.Canvas.Pen.Color:=clblack;  //5D3B31
 FOutBuffer.Canvas.Font.Color:=$E2A388;
 FOutBuffer.Canvas.Font.Size:=8;
 FOutBuffer.Canvas.Rectangle(LeftBar-1,FOutBuffer.Height-i*2,FOutBuffer.Width,FOutBuffer.Height);
 FOutBuffer.Canvas.TextOut(LeftBar+3,FOutBuffer.Height-i*2+2, 'Кол-во противников: '+inttostr(_contE._count));
 FOutBuffer.Canvas.TextOut(LeftBar+148,FOutBuffer.Height-i*2+2, 'Безжалостно убито: '+inttostr(_contE._more));
 FOutBuffer.Canvas.Rectangle(0,0,LeftBar,FOutBuffer.Height);
 Draw_panel;
 Draw_panel_weapons;
 Draw_panel_base;
 Draw_map;
 Draw_Button;
end;

procedure TInterface.calculateBase(i:integer);
var ii:integer;
begin
if not(_BH._active) then exit;
//_BH._energy_level:=_BH._energy_level-i;
 _BH.shield:=_BH.shield-(i div 2);
 if _BH.shield>128 then
 _BH.helth:=_BH.helth-(i div 5) else _BH.helth:=_BH.helth-i;
{if _BH._energy_level<400 then begin
  for ii:=0 to COUNT_WEAP-1 do
  if _conter._List[ii]._visible then
   case _conter._List[ii]._Type of
     twliser: if _BH._energy_level<210 then _conter._List[ii]._wait:=true else _conter._List[ii]._wait:=false;
     twdliser: if _BH._energy_level<380 then _conter._List[ii]._wait:=true else _conter._List[ii]._wait:=false;
   end;
end;   }

 if _BH.helth<0 then begin
 //,kz
 randomize;
 _active_w:=false;
 _BH._active:=false;
 CheckClickButton(tbGO);
 _missions._timer:=-1;
 tag:=random(7)+1;
 _BH.helth:=-1;
 bux_init; //мож бахнем?
 end;
end;

procedure TInterface.calculateEnergy(i:integer);
var ii:integer;
begin
 _BH._energy_level:=_BH._energy_level-i;
if _BH._energy_level<400 then begin
  for ii:=0 to COUNT_WEAP-1 do
  if _conter._List[ii]._visible then
   case _conter._List[ii]._Type of
     twliser: if _BH._energy_level<210 then _conter._List[ii]._wait:=true else _conter._List[ii]._wait:=false;
     twdliser: if _BH._energy_level<380 then _conter._List[ii]._wait:=true else _conter._List[ii]._wait:=false;
   end;
end;
 if _BH._energy_level<0 then _BH._energy_level:=0;
 if _BH._energy_level>1024 then _BH._energy_level:=1024;
 if _BH._energy_level>256 then _BH._radar:=true;
 _BH._energy_level:=_BH._energy_level+12;
end;

procedure TInterface.draw_GO;
begin
 FOutBuffer.Canvas.Font.Size:=15;
 FOutBuffer.Canvas.Brush.Style:=bsClear;
 FOutBuffer.Canvas.Font.Color:=clwhite;
 FOutBuffer.Canvas.TextOut( (((FOutBuffer.Width-leftbar)-FOutBuffer.Canvas.TextWidth('GAME OVER')) div 2) +leftbar, ((FOutBuffer.Height-BottomBar)-FOutBuffer.Canvas.TextHeight('G')) div 2 , 'GAME OVER');
 FOutBuffer.Canvas.Font.Size:=7;
 case tag of
   1:  FOutBuffer.Canvas.TextOut( (((FOutBuffer.Width-leftbar)-FOutBuffer.Canvas.TextWidth('бывает :)')) div 2)+leftbar , ((FOutBuffer.Height-BottomBar) div 2)+ (FOutBuffer.Canvas.TextHeight('G') div 2)+4, 'бывает :)');
   2:  FOutBuffer.Canvas.TextOut( (((FOutBuffer.Width-leftbar)-FOutBuffer.Canvas.TextWidth('что, опять?!')) div 2)+leftbar , ((FOutBuffer.Height-BottomBar) div 2)+ (FOutBuffer.Canvas.TextHeight('G') div 2)+4, 'что, опять?!');
   3:  FOutBuffer.Canvas.TextOut( (((FOutBuffer.Width-leftbar)-FOutBuffer.Canvas.TextWidth('может еще раз?')) div 2)+leftbar , ((FOutBuffer.Height-BottomBar) div 2)+ (FOutBuffer.Canvas.TextHeight('G') div 2)+4, 'может еще раз?');
   4:  FOutBuffer.Canvas.TextOut( (((FOutBuffer.Width-leftbar)-FOutBuffer.Canvas.TextWidth('блин...')) div 2)+leftbar , ((FOutBuffer.Height-BottomBar) div 2)+ (FOutBuffer.Canvas.TextHeight('G') div 2)+4, 'блин...');
   5:  FOutBuffer.Canvas.TextOut( (((FOutBuffer.Width-leftbar)-FOutBuffer.Canvas.TextWidth('м-дя, нехорошо получилось')) div 2)+leftbar , ((FOutBuffer.Height-BottomBar) div 2)+ (FOutBuffer.Canvas.TextHeight('G') div 2)+4, 'м-дя, нехорошо получилось');
   6:  FOutBuffer.Canvas.TextOut( (((FOutBuffer.Width-leftbar)-FOutBuffer.Canvas.TextWidth('может сменить тактику?')) div 2)+leftbar , ((FOutBuffer.Height-BottomBar) div 2)+ (FOutBuffer.Canvas.TextHeight('G') div 2)+4, 'может сменить тактику?');
   7:  FOutBuffer.Canvas.TextOut( (((FOutBuffer.Width-leftbar)-FOutBuffer.Canvas.TextWidth('ну и ладно :)')) div 2)+leftbar , ((FOutBuffer.Height-BottomBar) div 2)+ (FOutBuffer.Canvas.TextHeight('G') div 2)+4, 'ну и ладно :)');
 end;
end;

procedure TInterface.draw(canvas:Tcanvas; Rect:Trect);
var ii:integer;
begin
 With FOffscreenBuffer.Canvas do begin
		    	   Brush.Color:=clBlack;
		    	   Brush.Style:=bsSolid;
		    	   Fillrect(Cliprect);
 end;
 With FOutBuffer.Canvas do begin
		    	   Brush.Color:=clBlack;
		    	   Brush.Style:=bsSolid;
		    	   Fillrect(Cliprect);
 end;
  BitBlt(FOffscreenBuffer.Canvas.Handle,0,0,FOffscreenBuffer.Width,FOffscreenBuffer.Height,F_back_buffer.Canvas.Handle,0,0,SRCCOPY);

  calculateBase(_contE.AI(_bh.x,_bh.y));

  calculateEnergy(_conter.AI_mozgoed(_contE,FOffscreenBuffer.Canvas,F_back_buffer.Canvas));
 _Tbase.AI;
 _Tbase.Mine_AI(_contE);
 _BH.Energy(true);

 _conter.draw(FOffscreenBuffer.Canvas,F_back_buffer.Canvas);
 _contE.draw(FOffscreenBuffer.Canvas,classes.Rect(Rect.Left+_x+_xx,Rect.Top+_y+_yy,Rect.Right+_x+_xx,Rect.Bottom+_y+_yy));
 _TBase.Draw(FOffscreenBuffer.Canvas,F_back_buffer.Canvas);
 _BH.Draw(FOffscreenBuffer.Canvas);
 _missions.process;

 FOutBuffer.Width:=Rect.Right;
 FOutBuffer.Height:=Rect.Bottom;
 bux;
 Draw_cur;

 FOutBuffer.canvas.CopyRect(Rect,FOffscreenBuffer.Canvas,classes.Rect(Rect.Left+_x+_xx,Rect.Top+_y+_yy,Rect.Right+_x+_xx,Rect.Bottom+_y+_yy));
 Draw_interface;
 if (tag>0)and not(_BH._active) then draw_GO;

 BitBlt(Canvas.Handle,0,0,canvas.ClipRect.Right,canvas.ClipRect.Bottom,FOutBuffer.Canvas.Handle,0,0,SRCCOPY);
end;

end.

