function SetupTBCombatCamera()
{
   Screen2D.dismount();
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] !$= "")
      {
         $CombatCamera = %i;
         break;               
      }
   }

   Screen2D.setCurrentCameraArea(0 SPC 0 SPC 150 SPC 125);      
   
   $CombatCamera = %i;
   
   Screen2D.mount($vehicles[%i, battleid]);
   
   PlayVehicleSound($vehicles[%i, battleid].vehiclename);
   
   // update the battle window
   
   UpdateBattleWindow(%i);
}

// rotate camera to next vehicle

function RotateTBCamera()
{
   echo("ROTATE CAMERA CLICK");
   alxPlay(click);
      
   // reset the attack action when they switch vehicles
   
   Screen2D.dismount();
         
   Screen2D.mount($turnOrder[$CurrentTurn]);
}

function MouseWheelTB(%value)
{
   if($combat $= "")
      return;

   if($peoplebattle $= true)
      return;
       
   if(%value > 0)
   {
      %current = Screen2d.getCurrentCameraArea();
      %x = getWord(%current, 0);
      %y = getWord(%current, 1);
      %width = getWord(%current, 2);
      %height = getWord(%current, 3);            

      %width = %width - 100;
      %height = %height - 75;

      $CameraLevel--;   
      
      if($CameraLevel == -1)
      {
         $CameraLevel = 0;   
         return;
      }
      
      Screen2D.dismount();
      Screen2D.setCurrentCameraArea(%x SPC %y SPC %width SPC %height);      
      Screen2D.mount($turnOrder[$CurrentTurn]);
   }
   
   if(%value < 0)
   {
      %current = Screen2D.getCurrentCameraArea();
      %x = getWord(%current, 0);
      %y = getWord(%current, 1);
      %width = getWord(%current, 2);
      %height = getWord(%current, 3);      

      %width = %width + 100;
      %height = %height + 75;

      $CameraLevel++;
      
      if($CameraLevel > 2)
      {
         $CameraLevel = 2;   
         return;
      }
      
      Screen2D.dismount();
      Screen2D.setCurrentCameraArea(%x SPC %y SPC %width SPC %height);      
      Screen2D.mount($turnOrder[$CurrentTurn]);      
   }
}

function UpdateBattleWindowTB(%slot)
{
   // the battle window will only get updated if the vehicle is the same as the camera is on
   
   %id = $turnOrder[$CurrentTurn];
   
   if(%id.player $= 1)   
      battlewindow.Visible = true;
   else
      battlewindow.Visible = false;
   
   // get the men for this vehicle
   
   %count = getWordCount(%id.crew);
   
   for(%i=0;%i<%count;%i++)
   {
      %word = getWord(%id.crew, %i);      
      
      switch$ (%word)
      {
         case "refuges":
            %refuges++;
         case "brigands":
            %brigands++;
         case "militia":
            %militia++;
         case "police": 
            %police++;
         case "soldiers":
            %soldiers++;
         case "mercenaries":        
            %mercenaries++;
      }      
   }
   
   if(%refuges $= "")
      %refuges = 0;
   if(%brigands $= "")
      %brigands = 0;
   if(%militia $= "")
      %militia = 0;
   if(%police $= "")
      %police = 0;
   if(%soldiers $= "")
      %soldiers = 0;
   if(%mercenaries $= "")
      %mercenaries = 0;
      
   bw_vehicle.setText(GetVehicleDisplayName($vehicles[%slot, vehicle]));
   bw_speed.setText("Speed   " @ $vehicles[%slot, battleid].currentspeed SPC "/" SPC $vehicles[%slot, battleid].topspeed);
   bw_refuges.setText("Refuges   " @ %refuges);    
   bw_brigands.setText("Brigands   " @ %brigands);
   bw_militia.setText("Militia   " @ %militia);
   bw_police.setText("Police   " @ %police);
   bw_soldiers.setText("Soldiers   " @ %soldiers);
   bw_mercenaries.setText("Mercenaries   " @ %mercenaries);
   
   if($vehicles[%slot, battleid].target !$= "")
   {
      %distance = VectorDist($vehicles[%slot, battleid].GetPosition(), $vehicles[%slot, battleid].target.GetPosition());   
      %distance = mCeil(%distance);
      
      bw_distance.setText("Target Distance " @ %distance);
   }

   // update the distance for the rest of the vehicles
   
   for(%i=1;%i<=10;%i++)
   {
      %control = "bw_enemy" @ %i;
      
      if($Enemyvehicles[%i, battleid] !$= "")
      {
         %distance = VectorDist($vehicles[%slot, battleid].GetPosition(), $Enemyvehicles[%i, battleid].GetPosition());   
         %distance = mCeil(%distance);

         %control.tooltip = GetVehicleDisplayName($Enemyvehicles[%i, vehicle]) @ " and the distance is " @ %distance;                   
      }
   }


   if(%id.Ammo > 0)
   {
      bw_ammo.setProfile(Gui20BlackText);
      bw_ammo.setText("Ammo   " @ %id.ammo);
   }
   else
   {
      bw_ammo.setProfile(Gui20Red);
      bw_ammo.setText("Ammo   " @ %id.ammo);
   }
   
   bw_armor.setText("Armor   " @ $vehicles[%slot, currentarmor]);
   
   bw_vehicledamage.setText("Chassis   " @ $vehicles[%slot, currentvehiclehp]);
   
   bw_windowarmor.setText("Window Armor   " @ $vehicles[%slot, currentwindowarmor]);    

   bw_tirearmor.setText("Tire Armor   " @ $vehicles[%slot, currenttirearmor]);   

   bw_gastankarmor.setText("Gas Tank Armor   " @ $vehicles[%slot, currentgastankarmor]);
   
   bw_ram.setText("Ram   " @ $vehicles[%slot, Currentramhp]);
      
   bw_enginedamage.setText("Engine Health   " @ $vehicles[%slot, currentenginehp]);      

   if($vehicles[%slot, mountedweapon] !$= "")
      bw_mountedweapon.setText($vehicles[%slot, mountedweapon]);
   else
      bw_mountedweapon.setText("No Weapon");
         
   
   %tires = $vehicles[%slot, tires]-$vehicles[%slot, badtires];
   
   bw_tires.setText("Tires   " @ %tires SPC "/" SPC $vehicles[%slot, tires]);
   
   // update player 
   
}

function UpdateEnemyGroupTB()
{   
   // a target won't match if the target is the ground
      
   if(%i <= 10)
   {
      %control = "bw_enemy" @ %i;
      %position = %control.GetPosition();
      targetpin.Visible = true;           
      targetpin.setPosition(getWord(%position, 0),getWord(%position, 1)); 
      targetpin.command = "TargetEnemyVehicle(" @ %i @ ");";   
      UpdateTargetIcon();
   }
}

// places the vehicle icons in the battlewindow

function DisplayEnemyGangTB()
{
   for(%i=1;%i<=10;%i++)
   {
      %control = "bw_enemy" @ %i;
      
      if($Enemyvehicles[%i, battleid] !$= "")
      {
         %control.SetBitMap(expandfilename("~/data/images/icons/r_" @ $Enemyvehicles[%i, vehicle] @ ".png"));         
         %control.tooltip = GetVehicleDisplayName($Enemyvehicles[%i, vehicle]);                   
         %control.command = "TargetEnemyVehicle(" @ %i @ ");";
      }
      else
      {
         %control.SetBitMap(expandfilename("~/data/images/gui/grey.png"));         
         %control.tooltip = "";                            
         %control.command = "";
      }
   }
   
}

function DisplayPlayerGangTB()
{
   for(%i=1;%i<=10;%i++)
   {
      %control = "bw_player" @ %i;
      
      if($vehicles[%i, battleid] !$= "")
      {
         if($CombatCamera $= %i)
         {
            %control.SetBitMap(expandfilename("~/data/images/icons/" @ $vehicles[%i, vehicle] @ ".png"));                  
            %pos = %control.GetPosition();
            %x = getWord(%pos, 0);
            %y = getWord(%pos, 1);
            battle_friendlyselector.setPosition(%x, %y);
         }
         else
            %control.SetBitMap(expandfilename("~/data/images/icons/" @ $vehicles[%i, vehicle] @ ".png"));                  
         
         %control.tooltip = GetVehicleDisplayName($vehicles[%i, vehicle]);                   
         %control.command = "SetVehicleCamera(" @ %i @ ");";
      }
      else
      {
         %control.SetBitMap(expandfilename("~/data/images/gui/grey.png"));         
         %control.tooltip = "";       
         %control.command = "";            
      }
   }

   UpdateEnemyGroup();
}

