
// speed control //////////////////////////////////////////////////////////////////////////////////////

function IncreasePlayerSpeedTB()
{
   if($combat $= "")
      return;

   DisplayTutorial(Acceleration);

   %id = $vehicles[$CombatCamera, battleid];
   
   // if setspeed is blank then this is the first time the player is adjusting the speed for this vehicle
     
   if(%id.setspeed $= "")
   {
      %id.setspeed = %id.currentspeed;
      %id.setspeed = %id.setspeed+10;
   }
   else
      %id.setspeed = %id.setspeed+10;
   
   // should play a beep or modify icon to let the player know vehicle can't be set for any faster
   
   if(%id.setspeed > %id.topspeed)
   {
      alxPlay(buzzer);
      echo("SET TO MAX SPEED");
      %id.setspeed = %id.setspeed-10;
   }
   else
      alxPlay(goodbeep);
}

function DecreasePlayerSpeedTB()
{
   if($combat $= "")
      return;
      
   %id = $vehicles[$CombatCamera, battleid];
   
   // if setspeed is blank then this is the first time the player is adjusting the speed for this vehicle
     
   if(%id.setspeed $= "")
   {
      %id.setspeed = %id.currentspeed;
      %id.setspeed = %id.setspeed-10;
   }
   else
      %id.setspeed = %id.setspeed-10;
   
   // should play a beep or modify icon to let the player know vehicle can't be set for any faster
   
   if(%id.setspeed < 0)
   {
      alxPlay(buzzer);
      echo("Vehicle is stopped");
      %id.setspeed = 0;
   }
   else
      alxPlay(goodbeep);
}

function CustomAttackOrderTB(%attack)
{
   if($combat $= "")
      return;
   
   if(%attack $= "DoNotFire")
      DisplayTutorial("DoNotFire");
      
   if(attackcrew.bitmap $= "game/data/images/gui/red.png")
      %previous = "attackcrew";        
   if(attackengine.bitmap $= "game/data/images/gui/red.png")
      %previous = "attackengine";        
   if(attackgastank.bitmap $= "game/data/images/gui/red.png")
      %previous = "attackgastank";        
   if(attackmountedweapon.bitmap $= "game/data/images/gui/red.png")
      %previous = "attackmountedweapon";        
   if(attacktires.bitmap $= "game/data/images/gui/red.png")
      %previous = "attacktires";        
   if(ramvehicle.bitmap $= "game/data/images/gui/red.png")
      %previous = "ramvehicle";        
   if(BoardVehicle.bitmap $= "game/data/images/gui/red.png")
      %previous = "boardvehicle";        
   if(donotfire.bitmap $= "game/data/images/gui/red.png")
      %previous = "donotfire";        
   
   // deselecting attack orders
      
   if(%previous $= %attack)
   {   
      %previous.SetBitmap(expandfilename("~/data/images/gui/sq_border.png"));      
      canvas.SetCursor(GameCursor);         
      $vehicles[$CombatCamera, battleid].attackorder = "";
      
      // do not fire is a bit special
      
      if(%attack $= "donotfire")
      {      
         for(%i=1;%i<=10;%i++)
         {
            if($vehicles[%i, battleid] !$= "")
               $vehicles[%i, battleid].donotfire = "";         
         }
         
         $DoNotFire = "";
      }
   }
   
   // new order that what is currently selected
   
   if(%previous !$= "" && %previous !$= %attack)
   {   
      %previous.SetBitmap(expandfilename("~/data/images/gui/sq_border.png"));      
      canvas.SetCursor(GameCursor);         
      $vehicles[$CombatCamera, battleid].attackorder = "";
      %previous = "";
   }
   
   // new order
      
   if(%previous $= "")
   {
      %attack.SetBitmap(expandfilename("~/data/images/gui/red.png"));      
      canvas.SetCursor(TargetCursor);         
      
      if(%attack !$= "donotfire")
         $vehicles[$CombatCamera, battleid].attackorder = %attack;
      else
         $DoNotFire = true;
   }
   
   $vehicles[$CombatCamera, battleid].firelocation = "";
   $vehicles[$CombatCamera, battleid].BoardVehicle = "";
   $vehicles[$CombatCamera, battleid].RamVehicle = "";
}

// play a beep if the player can't attack that particular section on an enemy vehicle

function TargetEnemyVehicleTB(%slot)
{
   // mark the target and the location on the player vehicle
   
   %id = $vehicles[$CombatCamera, battleid];

   // for a manual target, the attack order will be blank so it will return
   
   switch$ ($vehicles[$CombatCamera, battleid].attackorder)
   {
      case "attackcrew":
         %id.firelocation = "crew";      
         %id.donotfire = "";   
         alxPlay(goodbeep);
      case "attacktires":
         if($Enemyvehicles[%slot, badtires] $= GetMaxVehicleTires($Enemyvehicles[%slot, vehicle]))
         {
            alxPlay(buzzer);
            return;            
         }
         %id.donotfire = "";   
         alxPlay(goodbeep);
         %id.firelocation = "tires";            
      case "attackengine":
         if($Enemyvehicles[%slot, currentenginehp] == 0)
         {
            alxPlay(buzzer);
            return;            
         }
         %id.donotfire = "";   
         alxPlay(goodbeep);
         %id.firelocation = "engine";            
      case "attackmountedweapon":
         if($Enemyvehicles[%slot, mountedweapon] $= "")
         {
            alxPlay(buzzer);
            return;            
         }
         %id.donotfire = "";   
         alxPlay(goodbeep);
         %id.firelocation = "mountedweapon";            
      case "attackgastank":      
         %id.firelocation = "gastank";      
         %id.donotfire = "";   
         alxPlay(goodbeep);
      case "boardvehicle":
         %count = getWordCount(%id.crew);      

         if(%count <= 2)
         {
            %id.boardvehicle = "";
            DisplayTutorial("MayNotBoard");
            alxPlay(buzzer);
            return;      
         }

         %id.boardvehicle = true;
         alxPlay(goodbeep);
      case "ramvehicle":
         %id.ramvehicle = true;
         alxPlay(goodbeep);
   }

   canvas.SetCursor(GameCursor);
   
   if($DoNotFire $= true)
   {
      for(%i=1;%i<=10;%i++)
      {
         if($vehicles[%i, battleid] !$= "")
            $vehicles[%i, battleid].donotfire = $Enemyvehicles[%slot, battleid];
      }

      alxPlay(goodbeep);
      
      $DoNotFire = "";
   }
   else
   {
      // make this the new target for the vehicle to drive around
      
      %id.target = $Enemyvehicles[%slot, battleid];
      %id.coords = "";  // if they have coords assigned, clear them   
      greenflag.visible = false;

      UpdateEnemyGroup();
   }
}

// When the player makes a selection during combat

function CombatSelectionTB(%coords)
{
   echo("COMBAT SELECTION");
   
   %objects = sg.pickPoint(%coords);   
   
   %count = getWordCount(%objects);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%objects, %i);
      
      // custom attack order
      
      if(%obj.enemy $= 1 && $vehicles[$CombatCamera, battleid].attackorder !$= "")
      {
         echo("TARGET ENEMY VEHICLE");
         TargetEnemyVehicle(%obj.slot);        
         return;
      }
      
      // do not fire
      
      if(%obj.enemy $= 1 && $DoNotFire $= true)
      {
         echo("DO NOT FIRE ENEMY VEHICLE");
         TargetEnemyVehicle(%obj.slot);        
         return;
      }
      
      // select target
      
      if(%obj.enemy $= 1 && $vehicles[$CombatCamera, battleid].targetorder $= true)
      {
         SelectVehicleTarget(%obj);
         return;
      }
      
      // friendly selection
      
      if(%obj.player $= 1)
         SetVehicleCamera(%obj.slot);
   }
   
   // they are selecting the ground but it will check to see if there is a building there
   
   if($vehicles[$CombatCamera, battleid].targetorder $= true)
      SelectGround(%coords);
}

// this is to either target a vehicle for a general attack or target the ground for the vehicle to rotate around

function TargetOrderTB()
{   
   DisplayTutorial(selecttarget);
   
   if(TargetOrder.bitmap $= "game/data/images/gui/red.png")
   {
      $vehicles[$CombatCamera, battleid].targetorder = "";       
      canvas.SetCursor(GameCursor);
      TargetOrder.SetBitmap(expandfilename("game/data/images/gui/sq_border.png"));    
      greenflag.visible = false;   
   }
   else
   {
      $vehicles[$CombatCamera, battleid].targetorder = true;       
      canvas.SetCursor(TargetCursor);
      TargetOrder.SetBitmap(expandfilename("game/data/images/gui/red.png"));      
   }
}

// will be either an enemy vehicle or coords on the map
// if they select a target clear the coords so the nav function knows to follow the target

function SelectVehicleTargetTB(%obj)
{   
   canvas.SetCursor(GameCursor);
   
   $vehicles[$CombatCamera, battleid].target = %obj;
   $vehicles[$CombatCamera, battleid].coords = "";   
   greenflag.visible = false;
}

function SelectGroundTB(%coords)
{
   %objects = sg.pickPoint(%coords);   
   
   %count = getWordCount(%objects);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%objects, %i);
      
      if(%obj.block $= 1)
      {
         $LastBlockedPick = %obj;   // this is to make sure the player can't keep selecting the same building over and over again
         alxPlay(buzzer);
         return;            
      }
            
      if(%obj.GetName() $= $Battlegrid)
         %grid = true;
   }
   
   // check for a border
   
   %line = sg.pickLine(%coords, $vehicles[$CombatCamera, battleid].GetPosition());
   
   %linecount = getWordCount(%line);
   
   for(%j=0;%j<%linecount;%j++)
   {
      %obj = getWord(%line, %j);
      
      if(%obj.border $= 1)
      {
         alxPlay(buzzer);
         return;            
      }
   }
   
   // will rotate arond these coordinates
   if(%grid $= true)
   {
      $vehicles[$CombatCamera, battleid].coords = %coords;  
      greenflag.SetPosition($vehicles[$CombatCamera, battleid].coords);
      greenflag.visible = true;
      $vehicles[$CombatCamera, battleid].targetorder = "";
      canvas.SetCursor(GameCursor);
      TargetOrder.SetBitmap(expandfilename("game/data/images/gui/sq_border.png"));    
   }
   else
      alxPlay(buzzer);
}

function UpdateCustomOrdersTB()
{
   attackcrew.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   targetOrder.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   attackengine.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   attackmountedweapon.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   attackgastank.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   attacktires.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   boardvehicle.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   ramvehicle.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   
   switch$ ($vehicles[$CombatCamera, battleid].firelocation)
   {
      case "crew":
         attackcrew.SetBitMap(expandfilename("~/data/images/gui/red.png"));         
      case "tires":
         attacktires.SetBitMap(expandfilename("~/data/images/gui/red.png"));         
      case "engine":
         attackengine.SetBitMap(expandfilename("~/data/images/gui/red.png"));         
      case "mountedweapon":
         attackmountedweapon.SetBitMap(expandfilename("~/data/images/gui/red.png"));         
      case "gastank":      
         attackgastank.SetBitMap(expandfilename("~/data/images/gui/red.png"));         
   }
   
   if($vehicles[$CombatCamera, battleid].boardvehicle $= true)
      boardvehicle.SetBitMap(expandfilename("~/data/images/gui/red.png"));         
   if($vehicles[$CombatCamera, battleid].ramvehicle $= true)
      ramvehicle.SetBitMap(expandfilename("~/data/images/gui/red.png"));         
   if($vehicles[$CombatCamera, battleid].donotfire !$= "")
      donotfire.SetBitMap(expandfilename("~/data/images/gui/red.png"));         
   
   canvas.SetCursor(GameCursor);
}

function ClearCustomOrdersTB(%slot)
{
   attackcrew.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   targetOrder.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   attackengine.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   attackmountedweapon.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   attackgastank.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   attacktires.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   boardvehicle.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   ramvehicle.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   donotfire.SetBitMap(expandfilename("~/data/images/gui/sq_border.png"));         
   
   $vehicles[%slot, battleid].firelocation = "";
   $vehicles[%slot, battleid].boardvehicle = "";
   $vehicles[%slot, battleid].ramvehicle = "";   
   $vehicles[%slot, battleid].coords = "";
   greenflag.visible = false;

   canvas.SetCursor(GameCursor);
}


//////////// display enemy text and enemy seletion //////////////////////////////////////////////////////

function DisplayEnemyVehicleTB(%this, %mod, %coords, %click)
{
   %objects = sg.pickPoint(%coords);   
   
   %count = getWordCount(%objects);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%objects, %i);
      
      if(%obj.enemy $= 1)
      {
         %x = getWord(%obj.GetPosition(), 0);
         %y = getWord(%obj.GetPosition(), 1);

         switch$ ($Enemyvehicles[%obj.slot, vehicle])
         {
            case "dunebuggy":
               %text = "Dune Buggy";           
            case "bike":
               %text = "Bike";           
            case "jeep":
               %text = "Jeep";           
            case "compact":
               %text = "Compact";           
            case "minivan":
               %text = "Mini Van";           
            case "towtruck":
               %text = "Tow Truck";           
            case "van":
               %text = "Van";           
            case "limo":
               %text = "Limo";           
            case "sedan":
               %text = "Sedan";           
            case "sportscar":
               %text = "Sports Car";           
            case "pickuptruck":
               %text = "Pickup Truck";           
            case "musclecar":
               %text = "Muscle Car";           
            case "stationwagon":
               %text = "Station Wagon";           
            case "humvee":
               %text = "Humvee";           
            case "suv":
               %text = "SUV";           
            case "flatbedtruck":
               %text = "Flatbed Truck";           
            case "trailertruck":
               %text = "Trailer Truck";           
            case "fireengine":
               %text = "Fire Engine";           
            case "garbagetruck":
               %text = "Garbage Truck";           
            case "bus":
               %text = "Bus";           
            case "boxtruck":
               %text = "Box Truck";           
            case "cargovan":
               %text = "Cargo Van";           
            case "ambulance":
               %text = "Ambulance";           
            case "construction":
               %text = "Construction Vehicle";                 
         }
         
         textdisplay.Text = %text;
         
         textdisplay.SetPosition(%x SPC %y-10);
         textdisplay.visible = true;
         schedule(500, 0, "checkCombatText", %coords);          
         %display = true;
      }
   }
   
   if(%display $= "")
      textdisplay.visible = false;
}

// if a player just leaves a mouse cursor over the enemy it will continue to display the name
// this will keep checking and once the enemy vehicle is not under the cursor hide the text

function CheckCombatTextTB(%coords)
{
   %objects = sg.pickPoint(%coords);   
   
   %count = getWordCount(%objects);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%objects, %i);
      
      if(%obj.enemy $= 1)
      {
         %enemy = true;
         schedule(500, 0, "CheckCombatText", %coords);          
      }
   }
   
   if(%enemy $= "")
      textdisplay.visible = false;
}
