// when moving on the map

function CheckForCombat()
{
   %rand = getRandom(1, 100);
   
   if($mastertime <= 12)
      return;
   
   if(player.road $= true)
   {
      $RoadCombatChance++;
      %chance = $RoadCombatChance;
   }
   else
   {
      $CountryCombatChance++;
      %chance = $CountryCombatChance;
   }
      
   if(%rand <= %chance)
   {
      echo("COMBAT ROLL " @ %rand SPC "COMBAT CHANCE " @ %chance);
      StartCombat();
      return true;
   }
}

// attempt a retreat

function Retreat()
{
   %rand = getRandom(1, 100);
   
   %scout = CheckScout();   
   
   %retreat = %rand-%scout;
   
   if(%retreat <= 30)
   {
      canvas.popdialog(BattleConfig);  
      $combat = "";
      DecreaseMorale(2);
   }
   else
   {
      vehicleretreat.Visible = false;
      vehicleretreattext.Visible = true;
      alxPlay(buzzer);
   }
}

function StartCombat()
{
   $enemyAmmo = 0;   // needs to be reset before battle

   // reset these variables
   
   if($SiloCapture $= true)
      retreatbutton.Visible = false;
   else
      retreatbutton.Visible = true;
   
   // enemy ////////////////
   
   if($SiloCapture $= true)
      GenerateNukeGang();
   else
      GenerateEnemyVehicles();
   
   vehicleretreattext.Visible = false;
   
   canvas.pushdialog(BattleConfig);

   DisplayTutorial(StartCombat);   
}

function PrepBattle1()
{
   // reset the combat numbers
   
   $RoadCombatChance = 3;
   $CountryCombatChance = 1;
      
   AutoSave();
   SaveSpecialLocationsToTemp();
   SaveNukesToTemp();
   canvas.cursorOff();
   schedule(500, 0, "PrepBattle2");
}

// so we have a splash screen

function PrepBattle2()
{      
   // pick a battlefield at random, if the player is on a city tile then only choose the city map

   %map = getRandom(1, 6);
   
   %list = sg.PickPoint(player.GetPosition());   
   %count = getWordCount(%list);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%list, %i);
      
      if(%obj.tilemap $= "" && %obj.spawn $= "")
      {
         if(%obj.city !$= "")
            %map = 7;      
         if(%obj.GetImageMap() $= "skinnyroad1imagemap")
            %map = 8;
         if(%obj.GetImageMap() $= "skinnyroad1_1imagemap")
            %map = 9;
      }
         
   }
      
   canvas.SetContent(loadingscreen);   
   canvas.cursorOff();
   ResetLoadingBar();
   ChangeLoadingBar(100);
   
   // decide if it's real time or turn based
   
   schedule(500, 0, "StartBattle", %map);  
}

function StartBattle(%map)
{   
   $EndGame = "";
   $EndBattle = "";
   $lastCombat = $mastertime;
   $combat = true;
   $CameraLevel = 0;
   $TurnBased = "";
   alxStop($Wind);
   alxStop($MoveSound);
   alxStopAll();
   
   $playerPosition = player.getPosition();
   
   UpdateTime();
   
   switch$ (%map)
   {
      case 1:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle1.t2d"));
      case 2:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle2.t2d"));
      case 3:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle3.t2d"));
      case 4:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle4.t2d"));
      case 5:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle5.t2d"));
      case 6:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle6.t2d"));
      case 7:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle7.t2d"));
         %addrocks = false;
      case 8:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle8.t2d"));
         %addrocks = false;
      case 9:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle9.t2d"));
         %addrocks = false;
   }
   
   $Battlegrid = "battle" @ %map;
   
   echo("BATTLEGRID " @ $Battlegrid);
   
   for(%i=1;%i<=$Enemyvehicles[numvehicles];%i++)
   {
      CreateEnemyVehicle($Enemyvehicles[%i, vehicle], %i);
   }
   
   echo("gen vehicles");   
   GenerateEnemyPoints();
   echo("gen enemy points");
   GenerateEnemyPersonnel(); // needs to come last, battleid is needed
   echo("gen people");
   GenerateEnemyGuns(); // needs battle id too and needs personel first
   echo("gen enemy guns");

   // see if there needs to be any special weapons mounted
   
   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, mountedweapon] !$= "")
         MountSpecialWeapon($Enemyvehicles[%i, vehicle], $Enemyvehicles[%i, battleid], $Enemyvehicles[%i, mountedweapon]);          
   }
   
   
   // player /////////////////////////////////////////////////////////////////////////////////////////////////
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] !$= "")   
         CreatePlayerVehicle($vehicles[%i, vehicle], %i);
   }
   echo("spawn player vehicles");
   BuildPlayerVehicles();
   echo("build player vehicles");
   
   // see if there needs to be any special weapons mounted
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, mountedweapon] !$= "")
         MountSpecialWeapon($vehicles[%i, vehicle], $vehicles[%i, battleid], $vehicles[%i, mountedweapon]);          
   }

   PlaceVehicles();
      
   if(%addrocks $= "")
   {
      AddRocks();
      AddFoilage();
   }

   battlewindow.Visible = true;
   mainbar.Visible = false;
   
   canvas.popdialog(BattleConfig);
 
   UpdateBattleShortCuts();
  
   $CombatVehicles = 0; // use for getmaxtriggerdistance function
   
   // start enemy logic 
   
   %start = 1000;
   
  for(%i=1;%i<=10;%i++)
  {
      if($Enemyvehicles[%i, vehicle] !$= "")
      {
         $Enemyvehicles[%i, battleid].SetForwardMovementOnly(true);
         $Enemyvehicles[%i, battleid].setForwardSpeed(GetSpeedTranslation(GetVehicleCruiseSpeed($Enemyvehicles[%i, battleid])));         
         $Enemyvehicles[%i, battleid].currentspeed = GetVehicleCruiseSpeed($Enemyvehicles[%i, battleid]);       
         
         schedule(%start, 0, "EnemyVehicleLogic", %i);
         %start = %start+200;
         $CombatVehicles++;
         %enemyvehicles++;
         
         // debug
         
         echo("STARTING ENEMY VEHICLE " @ $Enemyvehicles[%i, battleid].vehiclename @ " COORDINATES " @ $Enemyvehicles[%i, battleid].GetPosition()); 
      }
      
      if($Enemyvehicles[%i, mountedweapon] !$= "")
      {
         schedule(%start, 0, "EnemyAutoFireSpecialWeapon", $Enemyvehicles[%i, battleid]);
         %start=%start+100;
      }
  }
   
  echo("STARTING ENEMY VEHICLES " @ %enemyvehicles);
  
  // start player logic
   
   %start = 500;
   
  for(%i=1;%i<=10;%i++)
  {
      if($vehicles[%i, vehicle] !$= "")
      {
         $vehicles[%i, battleid].SetForwardMovementOnly(true);
         $vehicles[%i, battleid].setForwardSpeed(GetSpeedTranslation(GetVehicleCruiseSpeed($vehicles[%i, battleid])));         
         $vehicles[%i, battleid].currentspeed = GetVehicleCruiseSpeed($vehicles[%i, battleid]);       
         
         schedule(%start, 0, "PlayerVehicleLogic", %i);
         %start=%start+100;
         $CombatVehicles++;
         %playervehicles++;

         echo("STARTING PLAYER VEHICLE " @ $vehicles[%i, battleid].vehiclename @ " COORDINATES " @ $vehicles[%i, battleid].GetPosition());

      }
      
      if($vehicles[%i, mountedweapon] !$= "")
      {
         schedule(%start, 0, "PlayerAutoFireSpecialWeapon", $vehicles[%i, battleid]);
         %start=%start+100;
      }
  }

  echo("STARTING PLAYER VEHICLES " @ %playervehicles);
   
   // call this last so it updates the battlewindow with car selected
   SetupCombatCamera();
   
   DisplayEnemyGang();
   DisplayPlayerGang();
   
   // hide the flags
   
   greenflag.visible = false;
   redflag.visible = false;
   
   canvas.CursorOn();   
   ChangeLoadingBar(200);
   schedule(500, 0, "KillSplashScreen");
   
   UpdateCameraThread();
   
   schedule(1000, 0, "DisplayTutorial", "VehicleBattle");
   
   setAcceptableScreenCombatRatio();

   Infowindow.Visible = false;
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid].vehiclename $= "trailertruck" || $vehicles[%i, battleid].vehiclename $= "flatbedtruck")
         AttachTrailer($vehicles[%i, battleid]);
   }

   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, battleid].vehiclename $= "trailertruck" || $Enemyvehicles[%i, battleid].vehiclename $= "flatbedtruck")
         AttachTrailer($Enemyvehicles[%i, battleid]);
   }
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] !$= "")
      {
         %pos = $vehicles[%i, battleid].GetPosition();
         
         %x = getWord(%pos, 0);
         %y = getWord(%pos, 1);
                        
         %list = sg.pickrect(%x-15 SPC %y-15, %x+15 SPC %y+15);
         
         %count = getWordCount(%list);
         
         for(%i=0;%i<%count;%i++)
         {
            %obj = getWord(%list, %i);
            
            if(%obj.barrel == 1)
            {
               %obj.SafeDelete();
               echo("BARREL DELETED");
            }
         }
      }      
   }

   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, battleid] !$= "")
      {
         %pos = $Enemyvehicles[%i, battleid].GetPosition();
         
         %x = getWord(%pos, 0);
         %y = getWord(%pos, 1);
                        
         %list = sg.pickrect(%x-15 SPC %y-15, %x+15 SPC %y+15);
         
         %count = getWordCount(%list);
         
         for(%i=0;%i<%count;%i++)
         {
            %obj = getWord(%list, %i);
            
            if(%obj.barrel == 1)
            {
               %obj.SafeDelete();
               echo("BARREL DELETED");
            }
         }
      }      
   }
   
   
   echo("FINISH START BATTLE FUNCTION");
}

function KillSplashScreen()
{
   canvas.SetContent(mainmap);
}

// used in boarding a vehicle function

function GetSoldierAttackBonus(%soldier)
{
   switch$ (%soldier)
   {
      case "refuges":
         return 0;
      case "brigands":
         return 1;
      case "militia":
         return 2;
      case "police":
         return 3;
      case "soldiers":
         return 4;
      case "mercenaries":
         return 5;
   }
}

// used in boarding a vehicle function

function GetSoldierDefenseBonus(%soldier)
{
   switch$ (%soldier)
   {
      case "refuges":
         return 0;
      case "brigands":
         return 1;
      case "militia":
         return 2;
      case "police":
         return 3;
      case "soldiers":
         return 4;
      case "mercenaries":
         return 5;
   }
}

function RemoveRotateBlock(%id)
{
   %id.rotateblock = "";
}

function RemoveSpecialRotateBlock(%id)
{
   %id.specialrotateblock = "";
}

function RemoveBoardBlock(%id)
{
   %id.boardnavlock = "";
}

/////////////////////////////////////////////////////////////////////////////////
// General combat functions
/////////////////////////////////////////////////////////////////////////////////



// to make things easier for the player speed will be measured in increments of 10 in terms of 
// current speed, top speed, upgrades, etc but this will translate that number to an actual 
// speed to move the piece

function GetSpeedTranslation(%speed)
{
   switch (%speed)
   {
      case 10:      
         return 2;
      case 20:
         return 3.5;
      case 30:
         return 5;
      case 40:
         return 6.5;
      case 50:
         return 8;
      case 60:
         return 9.5;
      case 70:
         return 11;
      case 80:
         return 12.5;
      case 90:
         return 14;
      case 100:
         return 15.5;
      case 110:
         return 17;
      case 120:
         return 18.5;
      case 130:
         return 20;
      case 140:
         return 21.5;
      case 150:
         return 23;
   }
}

function GetAccelerationTime(%id)
{
   %vehiclename = $vehicles[%id.slot, vehicle];
   %acclevel = $vehicles[%id.slot, acceleration];
   
   switch$ (%vehiclename)
   {
      case "dunebuggy":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 2500;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 3000;      
         }
      case "bike":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 2500;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 3000;      
         }
      case "jeep":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 2500;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 3000;      
         }
      case "compact":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 2500;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 3000;      
         }
      case "minivan":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 2500;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 3000;      
         }
      case "towtruck":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 3000;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 4000;      
         }      
      case "van":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 3000;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 4000;      
         }      
      case "limo":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 3000;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 4000;      
         }      
      case "sedan":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 3000;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 4000;      
         }
      case "sportscar":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 2500;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 3000;      
         }
      case "pickuptruck":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 3000;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 4000;      
         }
      case "musclecar":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 3000;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 4000;      
         }
      case "stationwagon":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 3000;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 4000;      
         }
      case "humvee":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 3000;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 4000;      
         }
      case "suv":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 3000;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 4000;      
         }
      case "flatbedtruck":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 5000;            
            case 2:
               %acc = 4000;            
            case 3:
               %acc = 3000;            
            case 4:
               %acc = 2000;            
            case 5:
               %acc = 1000;            
            default:
               %acc = 6000;      
         }
      case "trailertruck":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 5000;            
            case 2:
               %acc = 4000;            
            case 3:
               %acc = 3000;            
            case 4:
               %acc = 2000;            
            case 5:
               %acc = 1000;            
            default:
               %acc = 6000;      
         }
      case "fireengine":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 5000;            
            case 2:
               %acc = 4000;            
            case 3:
               %acc = 3000;            
            case 4:
               %acc = 2000;            
            case 5:
               %acc = 1000;            
            default:
               %acc = 6000;      
         }
      case "garbagetruck":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 5000;            
            case 2:
               %acc = 4000;            
            case 3:
               %acc = 3000;            
            case 4:
               %acc = 2000;            
            case 5:
               %acc = 1000;            
            default:
               %acc = 6000;      
         }
      case "bus":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 5000;            
            case 2:
               %acc = 4000;            
            case 3:
               %acc = 3000;            
            case 4:
               %acc = 2000;            
            case 5:
               %acc = 1000;            
            default:
               %acc = 6000;      
         }
      case "boxtruck":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 5000;            
            case 2:
               %acc = 4000;            
            case 3:
               %acc = 3000;            
            case 4:
               %acc = 2000;            
            case 5:
               %acc = 1000;            
            default:
               %acc = 6000;      
         }
      case "cargovan":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 3000;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 4000;      
         }
      case "ambulance":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 3000;            
            case 2:
               %acc = 2000;            
            case 3:
               %acc = 1500;            
            case 4:
               %acc = 1000;            
            case 5:
               %acc = 500;            
            default:
               %acc = 4000;      
         }
      case "construction":
         switch$ (%acclevel)
         {
            case 1:
               %acc = 5000;            
            case 2:
               %acc = 4000;            
            case 3:
               %acc = 3000;            
            case 4:
               %acc = 2000;            
            case 5:
               %acc = 1000;            
            default:
               %acc = 6000;      
         }
   }
   
   // if the vehicle has more than half engine damage it accelerates by double
   
   %halfdamage = $vehicles[%id.slot, stockenginehp] / 2;
   %haldamage = mCeil(%halfdamage);
   
   if($vehicles[%id.slot, currentenginehp] < %halfdamage)
      %acc = %acc * 2;
      
   return %acc;
}

function GetBrakeTime(%vehiclename, %brakelevel)
{
   switch$ (%vehiclename)
   {
      case "dunebuggy":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 2500;            
            case 2:
               %brake = 2000;            
            case 3:
               %brake = 1500;            
            case 4:
               %brake = 1000;            
            case 5:
               %brake = 500;            
            default:
               %brake = 3000;      
         }
      case "bike":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 2500;            
            case 2:
               %brake = 2000;            
            case 3:
               %brake = 1500;            
            case 4:
               %brake = 1000;            
            case 5:
               %brake = 500;            
            default:
               %brake = 3000;      
         }
      case "jeep":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 2500;            
            case 2:
               %brake = 2000;            
            case 3:
               %brake = 1500;            
            case 4:
               %brake = 1000;            
            case 5:
               %brake = 500;            
            default:
               %brake = 3000;      
         }
      case "compact":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 4000;            
            case 2:
               %brake = 3000;            
            case 3:
               %brake = 2000;            
            case 4:
               %brake = 1500;            
            case 5:
               %brake = 1000;            
            default:
               %brake = 5000;      
         }
      case "minivan":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 4000;            
            case 2:
               %brake = 3000;            
            case 3:
               %brake = 2000;            
            case 4:
               %brake = 1500;            
            case 5:
               %brake = 1000;            
            default:
               %brake = 5000;      
         }
      case "towtruck":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 4000;            
            case 2:
               %brake = 3000;            
            case 3:
               %brake = 2500;            
            case 4:
               %brake = 1500;            
            case 5:
               %brake = 1000;            
            default:
               %brake = 5000;      
         }      
      case "van":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 4000;            
            case 2:
               %brake = 3000;            
            case 3:
               %brake = 2500;            
            case 4:
               %brake = 1500;            
            case 5:
               %brake = 1000;            
            default:
               %brake = 5000;      
         }      
      case "limo":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 4000;            
            case 2:
               %brake = 3000;            
            case 3:
               %brake = 2500;            
            case 4:
               %brake = 1500;            
            case 5:
               %brake = 1000;            
            default:
               %brake = 5000;      
         }      
      case "sedan":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 5000;            
            case 2:
               %brake = 4000;            
            case 3:
               %brake = 3000;            
            case 4:
               %brake = 2000;            
            case 5:
               %brake = 1000;            
            default:
               %brake = 6000;      
         }
      case "sportscar":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 1600;            
            case 2:
               %brake = 1200;            
            case 3:
               %brake = 800;            
            case 4:
               %brake = 500;            
            case 5:
               %brake = 300;            
            default:
               %brake = 2000;      
         }
      case "pickuptruck":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 5000;            
            case 2:
               %brake = 4000;            
            case 3:
               %brake = 3000;            
            case 4:
               %brake = 2000;            
            case 5:
               %brake = 1000;            
            default:
               %brake = 6000;      
         }
      case "musclecar":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 1600;            
            case 2:
               %brake = 1200;            
            case 3:
               %brake = 800;            
            case 4:
               %brake = 500;            
            case 5:
               %brake = 300;            
            default:
               %brake = 2000;      
         }
      case "stationwagon":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 5500;            
            case 2:
               %brake = 4500;            
            case 3:
               %brake = 3500;            
            case 4:
               %brake = 2500;            
            case 5:
               %brake = 1500;            
            default:
               %brake = 6500;      
         }
      case "humvee":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 6000;            
            case 2:
               %brake = 5000;            
            case 3:
               %brake = 4000;            
            case 4:
               %brake = 2000;            
            case 5:
               %brake = 1000;            
            default:
               %brake = 7000;      
         }
      case "suv":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 5000;            
            case 2:
               %brake = 4000;            
            case 3:
               %brake = 3000;            
            case 4:
               %brake = 2000;            
            case 5:
               %brake = 1000;            
            default:
               %brake = 6000;      
         }
      case "flatbedtruck":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 10000;            
            case 2:
               %brake = 9000;            
            case 3:
               %brake = 7000;            
            case 4:
               %brake = 5000;            
            case 5:
               %brake = 3000;            
            default:
               %brake = 11000;      
         }
      case "trailertruck":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 10000;            
            case 2:
               %brake = 9000;            
            case 3:
               %brake = 7000;            
            case 4:
               %brake = 5000;            
            case 5:
               %brake = 3000;            
            default:
               %brake = 11000;      
         }
      case "fireengine":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 10000;            
            case 2:
               %brake = 9000;            
            case 3:
               %brake = 7000;            
            case 4:
               %brake = 5000;            
            case 5:
               %brake = 3000;            
            default:
               %brake = 11000;      
         }
      case "garbagetruck":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 10000;            
            case 2:
               %brake = 9000;            
            case 3:
               %brake = 7000;            
            case 4:
               %brake = 5000;            
            case 5:
               %brake = 3000;            
            default:
               %brake = 11000;      
         }
      case "bus":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 10000;            
            case 2:
               %brake = 9000;            
            case 3:
               %brake = 7000;            
            case 4:
               %brake = 5000;            
            case 5:
               %brake = 3000;            
            default:
               %brake = 11000;      
         }
      case "boxtruck":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 10000;            
            case 2:
               %brake = 9000;            
            case 3:
               %brake = 7000;            
            case 4:
               %brake = 5000;            
            case 5:
               %brake = 3000;            
            default:
               %brake = 11000;      
         }
      case "cargovan":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 5000;            
            case 2:
               %brake = 4000;            
            case 3:
               %brake = 3000;            
            case 4:
               %brake = 2000;            
            case 5:
               %brake = 1000;            
            default:
               %brake = 6000;      
         }
      case "ambulance":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 2500;            
            case 2:
               %brake = 2000;            
            case 3:
               %brake = 1500;            
            case 4:
               %brake = 1000;            
            case 5:
               %brake = 500;            
            default:
               %brake = 3000;      
         }
      case "construction":
         switch$ (%brakelevel)
         {
            case 1:
               %brake = 10000;            
            case 2:
               %brake = 9000;            
            case 3:
               %brake = 7000;            
            case 4:
               %brake = 5000;            
            case 5:
               %brake = 3000;            
            default:
               %brake = 11000;      
         }
   }
   
   return %brake;
}

function GetVehicleFacing(%id)
{
   %facing = %id.GetRotation();
   
   if(%facing < 0)
      %facing = 0;
   
   // north 1
            
   if(%facing > 340 && %facing <= 360)
      return "north";
   
   // north 2
            
   if(%facing >= 0 && %facing <= 25)
      return "north";

   // north east

   if(%facing > 25 && %facing <= 71)
      return "northeast";
   
   // east
   
   if(%facing > 71 && %facing <= 116)
      return "east";

   // south east
   
   if(%facing > 116 && %facing <= 161)
      return "southeast";

   // south
   
   if(%facing > 161 && %facing <= 206)
      return "south";

   // south west
   
   if(%facing > 206 && %facing <= 252)
      return "southwest";

   // west
   
   if(%facing > 252 && %facing <= 298)
      return "west";
      
   // north west

   if(%facing > 298 && %facing <= 340)
      return "northwest";
}

function GetMiddleSensor(%class)
{
   %obj = new t2dTrigger() {
      canSaveDynamicFields = "1";
      Position = "3506.514 2511.583";
      size = "5 63.5";
      class = %class;
//      CollisionActiveSend = "1";
      CollisionActiveReceive = "1";
      CollisionPolyList = "-1.000 -1.000 1.000 -1.000 1.000 -0.005 -1.000 -0.005";
         mountID = "155";
   };

   return %obj;   
}

function GetRightForwardSensor(%class)
{
   %obj = new t2dTrigger() {
      canSaveDynamicFields = "1";
      Position = "4727.742 3056.215";
      size = "10.000 80.000";
      class = %class;
      Rotation = "45";
//      CollisionActiveSend = "1";
      CollisionActiveReceive = "1";
      CollisionPolyList = "-1.000 -1.000 1.000 -1.000 1.000 -0.005 -1.000 -0.005";
         mountID = "157";
   };
   
   return %obj;   
   
}

function GetLeftForwardSensor(%class)
{
   %obj = new t2dTrigger() {
      canSaveDynamicFields = "1";
      Position = "3795.384 2510.525";
      size = "10.000 80.000";
      class = %class;
      Rotation = "315";
//      CollisionActiveSend = "1";
      CollisionActiveReceive = "1";
      CollisionPolyList = "-1.000 -1.000 1.000 -1.000 1.000 -0.005 -1.000 -0.005";
         mountID = "156";
   };
   
   return %obj;   
   
}

function GetLeftSensor(%class)
{
   %obj = new t2dTrigger() {
      canSaveDynamicFields = "1";
      Position = "1444.036 -195.322";
      size = "68.561 3.163";
//      CollisionActiveSend = "1";
      CollisionActiveReceive = "1";
      CollisionPolyList = "-1.000 -1.000 0.000 -1.000 0.005 1.000 -1.000 1.000";
         mountID = "128";
         mountToID = "126";
   };
   
   return %obj;   
}

function GetRightSensor(%class)
{
   %obj = new t2dTrigger(right) {
      canSaveDynamicFields = "1";
      Position = "1444.036 -195.322";
      size = "68.561 3.163";
//      CollisionActiveSend = "1";
      CollisionActiveReceive = "1";
      CollisionPolyList = "-0.010 -1.000 1.000 -1.000 1.000 1.000 0.005 1.000";
         mountID = "127";
         mountToID = "126";
   };
   
   return %obj;      
}

function GetFrontBumper(%class, %vehicle)
{
   %size = %vehicle.GetSize();
   %x = getWord(%size, 0);
   %y = getWord(%size, 1);
   
   %obj = new t2dTrigger() {
      canSaveDynamicFields = "1";
      size = %x SPC ".5";
      class = %class;
//      CollisionActiveSend = "1";
      CollisionActiveReceive = "1";
         mountID = "156";
   };
   
   return %obj;      
}

// after a vehicle fires it's guns it won't be able to fire for X amount of seconds

function ClearToFireGuns(%id, %count)
{
   for(%i=0;%i<%count;%i++)
   {
      %id.crewfired--;            
   }
}

function ClearSpecialWeapon(%id)
{
   %id.specialclearfire = "";   
}

function ClearSide(%id, %side)
{
   switch$ (%side)
   {
      case "front":
         %id.front = "";      
      case "back":
         %id.back = "";
      case "right":
         %id.right = "";
      case "left":      
         %id.left = "";
   }   
}

// return the base armor class of any object or vehicle in the game

function GetArmorClass(%item)
{
   switch$ (%item)
   {
      // vehicles
      
      case "dunebuggy":
         return 17;
      case "bike":
         return 17;
      case "jeep":
         return 16;
      case "compact":
         return 16;
      case "minivan":
         return 16;
      case "towtruck":
         return 15;
      case "van":
         return 15;
      case "limo":
         return 14;
      case "sedan":
         return 14;
      case "sportscar":
         return 17;
      case "pickuptruck":
         return 16;
      case "musclecar":
         return 15;
      case "stationwagon":
         return 15;
      case "humvee":
         return 15;
      case "suv":
         return 15;
      case "flatbedtruck":
         return 14;
      case "trailertruck":
         return 14;
      case "fireengine":
         return 14;
      case "garbagetruck":
         return 13;
      case "bus":
         return 14;
      case "boxtruck":
         return 14;
      case "cargovan":
         return 14;
      case "ambulance":
         return 14;
      case "construction":
         return 14;
               
      // other stuff

      case "tires":
         return 16;
      case "window":
         return 17;
      case "gastank":
         return 15;
      case "mountedweapon":
         return 12;
   }
}

// for each point of armor a vehicle has it will get a .05 bonus

function GetArmorBonus(%armor)
{
   %bonus = %armor * ".05";
   
   // less than 1

   if(%bonus < 1) 
   {
      %bonus = 1;
      return %bonus;
   }
      
   // 1
   
   if(%bonus >= 1.0 && %bonus < 1.5) 
   {
      %bonus = mfloor(%bonus);
      return %bonus;
   }
   if(%bonus >= 1.5 && %bonus < 1.9) 
   {
      %bonus = mceil(%bonus);
      return %bonus;
   }

   // 2
   
   if(%bonus >= 2.0 && %bonus < 2.5) 
   {
      %bonus = mfloor(%bonus);
      return %bonus;
   }
   if(%bonus >= 2.5 && %bonus < 2.9) 
   {
      %bonus = mceil(%bonus);
      return %bonus;
   }

   // 3
   
   if(%bonus >= 3.0 && %bonus < 3.5) 
   {
      %bonus = mfloor(%bonus);
      return %bonus;
   }
   if(%bonus >= 3.5 && %bonus < 3.9) 
   {
      %bonus = mceil(%bonus);
      return %bonus;
   }

   // 4
   
   if(%bonus >= 4.0 && %bonus < 4.5) 
   {
      %bonus = mfloor(%bonus);
      return %bonus;
   }
   
   if(%bonus >= 4.5 && %bonus < 4.9) 
   {
      %bonus = mceil(%bonus);
      return %bonus;      
   }
   
}

// the damage done by guns, called by player and enemy

function GetGunDamage()
{
   return getRandom(1, 3);   
}

function DestroyEnemyVehicle(%id)
{   
   echo("START DESTROYED ENEMY VEHICLE");
   
   if(VehicleVerification(%id) $= false)
   {
      echo("ENEMY VEHICLE DOES NOT EXIST");
      return;      
   }

   DestroySensors(%id);
         
   $CombatVehicles--;
   
   %id.SetAtRest();
   
   if(isObject(%id.marker) $= true)
      %id.marker.SafeDelete();

   if(%id.smoke !$= "")
      %id.smoke.SafeDelete();

   if(%id.enginefiresprite !$= "")
      %id.enginefiresprite.SafeDelete();
   
   if(%id.gastankfire !$= "")
      %id.gastankfire.SafeDelete();
      
   if(%id.gastankexplosion !$= "")
      cancel(%id.gastankexplosion);

   // add the guns and ammo it add to the pool
   
   $enemyAmmo = $enemyAmmo+%id.ammo;   
   
   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, battleid] $= %id)
         $Enemyvehicles[%i, battleid] = "";
   }
   
   // if any player vehicle has this vehicle as a target now eliminate it
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid].target $= %id)
      {
         $vehicles[%i, battleid].target = "";
         
         // eliminate custom attack orders if any
                  
         if($CombatCamera $= %i && $vehicles[%i, battleid].firelocation !$= "" || $vehicles[%i, battleid].ramvehicle !$= "" || $vehicles[%i, battleid].boardvehicle !$= "")
         {
            $vehicles[%i, battleid].firelocation = "";            
            $vehicles[%i, battleid].ramvehicle = "";
            $vehicles[%i, battleid].BoardVehicle = "";
            targetpin.Visible = false;           
            UpdateCustomOrders();
            UpdateEnemyGroup();
         }

         $vehicles[%i, battleid].firelocation = ""; 
      }
   }

   %id.enemy = "";
   %id.SetClassNameSpace("");
   %id.block = true;
   
   CreateDeathExplosion(%id);
   DisplayEnemyGang();

   if(%id.rotateschedule !$= "")
   {
      cancel(%id.rotateschedule);
      %id.trailer.Safedelete();   
      AttachTrailerAfterDeath(%id);
   }

   // check to see if the battle is over
   
   $EndBattle = true;
   
   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, battleid] !$= "")
         $EndBattle = false;
   }
   
   if($EndBattle $= true)
   {            
      if($quickbattle $= true)
      {
         DeleteBattle();
         EndQuickBattleText.setText("You have won the battle!");
         canvas.pushdialog(EndQuickBattle);
         alxStop($vehicleSound);
         return;
      }
      else
      {
         $combat = "";
         alxStop($vehicleSound);
         alxPlay(EndInfantryBattle);
         schedule(3000, 0, "AfterCombat");
         return;
      }
   }
   
   echo("END DESTROYED ENEMY VEHICLE - VEHICLES LEFT " @ %counter);
}

function StopEnemyVehicle(%id)
{
   echo("START STOP ENEMY VEHICLE");
   
   $CombatVehicles--;

   if(isObject(%id.marker) $= true)
      %id.marker.SafeDelete();
   
   if(%id.gastankexplosion !$= "")
      cancel(%id.gastankexplosion);

   DestroySensors(%id);
   
   %id.enemy = "";
   %id.SetClassNameSpace("");
   %id.block = true;
         
   // eliminat the battle id
   
   for(%i=1;%i<=10;%i++)
   {
      if($enemyvehicles[%i, battleid] $= %id)
      {
         $enemyvehicles[%i, battleid] = "";
         $enemyvehicles[%i, vehicle] = "";
      }
   }

   // if any player vehicle has this vehicle as a target now eliminate it
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid].target $= %id)
      {
         $vehicles[%i, battleid].target = "";

         // eliminate custom attack orders if any
                  
         if($CombatCamera $= %i && $vehicles[%i, battleid].firelocation !$= "" || $vehicles[%i, battleid].ramvehicle !$= "" || $vehicles[%i, battleid].boardvehicle !$= "")
         {
            $vehicles[%i, battleid].firelocation = ""; 
            $vehicles[%i, battleid].ramvehicle = "";
            $vehicles[%i, battleid].BoardVehicle = "";
            targetpin.Visible = false;           
            UpdateCustomOrders();
            UpdateEnemyGroup();
         }         
      }
   }
   
   // check count to see if this was the last player vehicle
   
   // End battle will also be used to stop ai threads
   
   $EndBattle = true;
   
   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, battleid] !$= "")
         $EndBattle = false;
   }
   
   DisplayEnemyGang();

   if($EndBattle $= true)
   {            
      if($quickbattle $= true)
      {
         DeleteBattle();
         EndQuickBattleText.setText("You have won the battle!");
         canvas.pushdialog(EndQuickBattle);         
         %id.SetAtRest();
         alxStop($vehicleSound);
         return;
      }
      else
      {
         $combat = "";
         %id.SetAtRest();
         alxStop($vehicleSound);
         alxPlay(EndInfantryBattle);
         schedule(3000, 0, "AfterCombat");
         return;
      }
   }

   schedule(1000, 0, "StopVehicle", %id);
   
   // put some dead guys around it 
   
   %rand = getRandom(1, 4);
   
   if(%id.vehiclename $= "bike")
      %rand = getRandom(1, 2);
      
   for(%i=1;%i<=%rand;%i++)
   {
      %body =    new t2dStaticSprite() {
               canSaveDynamicFields = "1";
               imageMap = "deadsoldier";
               scenegraph = "sg";
               size = "4 4";
               layer = 6;  
            };
            
      $ObjectList = $ObjectList SPC %body;
      
      switch$ (%i)
      {
         case 1:
            %x = getWord(%id.GetPosition(), 0)-2;
            %y = getWord(%id.GetPosition(), 1)+2;         
         case 2:
            %x = getWord(%id.GetPosition(), 0)+2;
            %y = getWord(%id.GetPosition(), 1)-2;                  
         case 3:
            %x = getWord(%id.GetPosition(), 0)+4;
            %y = getWord(%id.GetPosition(), 1)-4;                  
         case 4:
            %x = getWord(%id.GetPosition(), 0)-4;
            %y = getWord(%id.GetPosition(), 1)+4;                  
      }
      
      %body.SetPosition(%x SPC %y);
   }

   if(%id.rotateschedule !$= "")
   {
      cancel(%id.rotateschedule);
      %id.trailer.Safedelete();   
      AttachTrailerAfterDeath(%id);
   }

   // strictly for debug
   
   for(%i=1;%i<=10;%i++)
   {
      if($enemyvehicles[%i, battleid] !$= "")
         %counter++;      
   }

   echo("END STOP ENEMY VEHICLE - VEHICLES LEFT " @ %counter);
}

function StopPlayerVehicle(%id)
{
   echo("START STOP PLAYER VEHICLE");

   $CombatVehicles--;

   $vehicles[%id.slot, currentvehiclehp] = 0;   // just so it doesn't display a negative hp number
   
   UpdateBattleWindow($CombatCamera);
   
   if(isObject(%id.marker) $= true)
      %id.marker.SafeDelete();
   
   if(%id.gastankexplosion !$= "")
      cancel(%id.gastankexplosion);

   if(%id.smoke !$= "")
      %id.smoke.SafeDelete();

   if(%id.enginefiresprite !$= "")
      %id.enginefiresprite.SafeDelete();

   DestroySensors(%id);
   
   %id.player = "";
   %id.SetClassNameSpace("");
   %id.block = true;
         
   // eliminat the battle id
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] $= %id)
      {
         $vehicles[%i, battleid] = "";
         $vehicles[%i, vehicle] = "";
      }
   }

   // if any enemy vehicle has this vehicle as a target now eliminate it
   
   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, battleid].target $= %id)
         $Enemyvehicles[%i, battleid].target = "";
   }
   
   // check count to see if this was the last player vehicle
   
   // End battle will also be used to stop ai threads
   
   $EndBattle= true;
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] !$= "")
         $EndBattle = false;
   }
   
   DisplayPlayerGang();

   if($EndBattle $= true)
   {            
      if($quickbattle $= true)
      {
         DeleteBattle();
         %id.SetAtRest(); // stop the vehicle before we return
         EndQuickBattleText.setText("You have lost the battle!");
         canvas.pushdialog(EndQuickBattle);
         alxStop($vehicleSound);
         return;
      }
      else
      {
         $combat = "";
         %id.SetatRest();
         alxStop($vehicleSound);
         EndPlayerGame();
         return;
      }
   }

   if($CombatCamera $= %id.slot)
      RotateCamera();
   
   schedule(1000, 0, "StopVehicle", %id);
   
   // put some dead guys around it 
   
   %rand = getRandom(1, 4);

   if(%id.vehiclename $= "bike")
      %rand = getRandom(1, 2);
   
   for(%i=1;%i<=%rand;%i++)
   {
      %body =    new t2dStaticSprite() {
               canSaveDynamicFields = "1";
               imageMap = "deadsoldier";
               scenegraph = "sg";
               size = "4 4";
               layer = 6;  
            };
            
      $ObjectList = $ObjectList SPC %body;
      
      switch$ (%i)
      {
         case 1:
            %x = getWord(%id.GetPosition(), 0)-2;
            %y = getWord(%id.GetPosition(), 1)+2;         
         case 2:
            %x = getWord(%id.GetPosition(), 0)+2;
            %y = getWord(%id.GetPosition(), 1)-2;                  
         case 3:
            %x = getWord(%id.GetPosition(), 0)+4;
            %y = getWord(%id.GetPosition(), 1)-4;                  
         case 4:
            %x = getWord(%id.GetPosition(), 0)-4;
            %y = getWord(%id.GetPosition(), 1)+4;                  
      }
      
      %body.SetPosition(%x SPC %y);
   }

   // strictly for debug
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] !$= "")
         %counter++;      
   }

   if(%id.rotateschedule !$= "")
   {
      cancel(%id.rotateschedule);
      %id.trailer.Safedelete();   
      AttachTrailerAfterDeath(%id);
   }

   echo("END STOP PLAYER VEHICLE - VEHICLES LEFT " @ %counter);
}

function DestroyPlayerVehicle(%id, %board)
{
   echo("START DESTROYED PLAYER VEHICLE");

   if(VehicleVerification(%id) $= false)
   {
      echo("PLAYER VEHICLE DOES NOT EXIST");
      return;      
   }
   
   $CombatVehicles--;

   $vehicles[%id.slot, currentvehiclehp] = 0;   // just so it doesn't display a negative hp number
   
   UpdateBattleWindow($CombatCamera);

   %id.SetAtRest();
   
   if(isObject(%id.marker) $= true)
      %id.marker.SafeDelete();
   
   if(%id.gastankexplosion !$= "")
      cancel(%id.gastankexplosion);
   
   if(%id.smoke !$= "")
      %id.smoke.SafeDelete();

   if(%id.enginefiresprite !$= "")
      %id.enginefiresprite.SafeDelete();

   if(%id.gastankfire !$= "")
      %id.gastankfire.SafeDelete();
         
   DestroySensors(%id);
   
   %id.player = "";
   %id.SetClassNameSpace("");
   %id.block = true;
         
   // eliminat the battle id
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] $= %id)
      {
         $vehicles[%i, battleid] = "";
         $vehicles[%i, vehicle] = "";
      }
   }

   // if any enemy vehicle has this vehicle as a target now eliminate it
   
   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, battleid].target $= %id)
         $Enemyvehicles[%i, battleid].target = "";
   }
   
   // check count to see if this was the last player vehicle
   
   // End battle will also be used to stop ai threads
   
   $EndBattle = true;
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] !$= "")
         $EndBattle = false;
   }
   
   DisplayPlayerGang();

   CreateDeathExplosion(%id);
      
   if($EndBattle $= true)
   {
      if($quickbattle $= true)
      {
         DeleteBattle();
         EndQuickBattleText.setText("You have lost the battle!");
         canvas.pushdialog(EndQuickBattle);
         alxStop($vehicleSound);               
         return;
      }
      else
      {
         $combat = "";
         alxStop($vehicleSound);
         EndPlayerGame();
         return;
      }
   }

   if($CombatCamera $= %id.slot)
      RotateCamera();
   
   // strictly for debug
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] !$= "")
         %counter++;      
   }

   if(%id.rotateschedule !$= "")
   {
      cancel(%id.rotateschedule);
      %id.trailer.Safedelete();   
      AttachTrailerAfterDeath(%id);
   }

   echo("END DESTROYED PLAYER VEHICLE - VEHICLES LEFT " @ %counter);
}

function EndPlayerGame()
{
   echo("START END PLAYER GAME");

   canvas.SetCursor(GameCursor);
   canvas.pushdialog(endGamemenu);
   
   $combat = "";

   echo("END END PLAYER GAME");
}

function StopVehicle(%id)
{
   echo("STOP VEHICLE");
   %id.SetAtRest();   
}

// the attacker will get a modifier based on how big or small the vehicle is they are attacking

function GetSizeAttackMod(%defendvehicle)
{
   switch$ (%defendvehicle)
   {
      case "dunebuggy":
         return -1;

      case "bike":
         return -1;

      case "jeep":
         return -1;
      
      case "compact":
         return -1;

      case "minivan":
         return -1;

      case "towtruck":
         return 0;
      
      case "van":
         return 0;

      case "limo":
         return 1;

      case "sedan":
         return 0;

      case "sportscar":
         return -1;

      case "pickuptruck":
         return 0;

      case "musclecar":
         return 0;

      case "stationwagon":
         return 0;

      case "humvee":
         return 1;

      case "suv":
         return 1;
      
      case "flatbedtruck":
         return 2;

      case "trailertruck":
         return 2;

      case "fireengine":
         return 2;

      case "garbagetruck":
         return 1;

      case "bus":
         return 2;
      
      case "boxtruck":
         return 1;

      case "cargovan":
         return 1;

      case "ambulance":
         return 1;

      case "construction":
         return 1;
   }   
}

// this will be a mod based on the speed of the attacker and defender

function GetSpeedAttackMod(%attackerspeed, %defenderspeed)
{
   if(%attackerspeed > %defenderspeed)
      return 1;
   else
      return -1;
}

// used in combat, find out what the max tires are on a vehicle so the AI can see how many bad tires
// the enemy car has and if it should still fire at the tires

function GetMaxVehicleTires(%vehiclename)
{
   switch$ (%vehiclename)
   {
      case "dunebuggy":
         return 4;

      case "bike":
         return 2;

      case "jeep":
         return 3;
      
      case "compact":
         return 4;

      case "minivan":
         return 4;

      case "towttuck":
         return 6;
      
      case "van":
         return 4;

      case "limo":
         return 4;

      case "sedan":
         return 4;

      case "sportscar":
         return 4;

      case "pickuptruck":
         return 4;

      case "musclecar":
         return 4;

      case "stationwagon":
         return 4;

      case "humvee":
         return 4;

      case "suv":
         return 4;
      
      case "flatbedtruck":
         return 14;

      case "trailertruck":
         return 18;

      case "fireengine":
         return 6;

      case "garbagetruck":
         return 6;

      case "bus":
         return 10;
      
      case "boxtruck":
         return 4;

      case "policevan":
         return 4;

      case "ambulance":
         return 4;

      case "construction":
         return 0;
   }   
}


// when a vehicle loses a tire it may lose some top speed
// the speed bump will keep track of how many tires they lose before suffering a decrease in speed
function AdjustSpeedWithBadTires(%id)
{
   switch$ (%id.vehiclename)
   {
      case "dunebuggy":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "bike":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "jeep":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "compact":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "minivan":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "towtruck":
         switch$($vehicles[%id.slot, badtires])
         {
            case 0:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename);                        
            case 1:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 2: 
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 3:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;            
            case 4:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;            
            case 5:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 6:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
         }
      case "van":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "limo":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "sedan":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "sportscar":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "pickuptruck":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "musclecar":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "stationwagon":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "humvee":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "suv":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "flatbedtruck":
         switch$($vehicles[%id.slot, badtires])
         {
            case 0:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename);                        
            case 1:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 2: 
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 3:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 4:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 5:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;                        
            case 6:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;                        
            case 7:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;                        
            case 8:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;                        
            case 9:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 10:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 11:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 12:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 13:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-40;                        
            case 14:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-40;                        
         }
      case "trailertruck":
         switch$($vehicles[%id.slot, badtires])
         {
            case 0:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename);                        
            case 1:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 2: 
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 3:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 4:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 5:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;                        
            case 6:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;                        
            case 7:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;                        
            case 8:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;                        
            case 9:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 10:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 11:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 12:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 13:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-40;                        
            case 14:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-40;                        
         }
      case "fireengine":
         switch$($vehicles[%id.slot, badtires])
         {
            case 0:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename);                        
            case 1:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 2: 
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 3:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;            
            case 4:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;            
            case 5:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 6:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
         }
      case "garbagetruck":
         switch$($vehicles[%id.slot, badtires])
         {
            case 0:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename);                        
            case 1:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 2: 
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 3:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;            
            case 4:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;            
            case 5:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 6:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
         }
      case "bus":
         switch$($vehicles[%id.slot, badtires])
         {
            case 0:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename);                        
            case 1:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 2: 
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 3:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 4:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;            
            case 5:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;                        
            case 6:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;                        
            case 7:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 8:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 9:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 10:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-40;                        
         }
      case "boxtruck":
         switch$($vehicles[%id.slot, badtires])
         {
            case 0:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename);                        
            case 1:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 2: 
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-10;            
            case 3:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;            
            case 4:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-20;            
            case 5:
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
            case 6:           
               %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-30;                        
         }
      case "cargovan":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "ambulance":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
      case "construction":
         %penalty = $vehicles[%id.slot, badtires]*10;
         %id.topspeed = GetVehicleTopSpeed(%id.vehiclename)-%penalty;
   }
}

function CreateRedBar()
{
   %id = new t2dStaticSprite() {
      imageMap = "redbarImageMap";
      frame = "0";
      canSaveDynamicFields = "1";
      size = "5 5";
         mountID = "3";
   };
   
   %id.AddToScene(sg);
   return %id;
}

function CreateGreenBar()
{
   %id = new t2dStaticSprite() {
      imageMap = "greenbarImageMap";
      frame = "0";
      canSaveDynamicFields = "1";
      size = "5 5";
         mountID = "4";
   };
   
   %id.AddToScene(sg);
   return %id;
}

// updates the health bar for each combat vehicle

function UpdateHealthBar(%id)
{
   if(isObject(%id.marker) $= false)
      return;
      
   %num = GetHealthBarConversion(%id.vehiclename);
   
   if(%id.player $= 1)
      %size = $vehicles[%id.slot, currentvehiclehp] / %num;
   else
      %size = $enemyvehicles[%id.slot, currentvehiclehp] / %num;   
      
   // bike is different because it's conversion is 1 hit point
   
   if(%id.vehiclename $= "bike")
      %size = $enemyvehicles[%id.slot, currentvehiclehp];
             
   %id.marker.SetSize(%size SPC 5);   
}

// only for the player vehicles, if the crew is less than 10% it will flash

function FlashPlayerHealthBar(%id)
{
   if(%id.marker.visible $= true)
   {      
      %id.marker.visible = false;
      schedule(2000, 0, "FlashPlayerHealthBar", %id);
      return;
   }
   
   if(%id.marker.visible $= false)
   {
      schedule(2000, 0, "FlashPlayerHealthBar", %id);
      %id.marker.visible = true;   
   }
}

function RemoveBorderLock(%id)
{
   %id.borderlock = "";   
}

function AttachTrailer(%id)
{
   if(%id.vehiclename $= "flatbedtruck")   
   {
      %list = GetVehiclePolyList("flatbed_trailer");
      %name = "flatbedtrailer";
      %size = GetSize("flatbedtrailer");
   }
   
   if(%id.vehiclename $= "trailertruck")
   {
      %list = GetVehiclePolyList("trailertruck_trailer");
      %name = "trailertrucktrailer";
      %size = GetSize("trailertrucktrailer");
   }
   
   %trailer =    new t2dStaticSprite() {
                  canSaveDynamicFields = "1";
                  layer = 1; 
                  CollisionPolyList = %list;
                  scenegraph = "sg";
                  imageMap = %name;
               };
   
   %trailer.SetCollisionActive(true, true);               
   %trailer.SetSize(%size);
   %trailer.Mount(%id, "0 2", 0, false, true, true, true);   
   %id.trailer = %trailer;
   %trailer.slot = %id.slot;    
   %trailer.cab = %id;     
   %trailer.SetRotation(%id.GetRotation());
   schedule(1000, 0, "RotateTrailer", %id);
}

function AttachTrailerAfterDeath(%id)
{
   if(%id.vehiclename $= "flatbedtruck")   
   {
      %list = GetVehiclePolyList("flatbed_trailer");
      %name = "flatbedtrailer";
      %size = GetSize("flatbedtrailer");
   }
   
   if(%id.vehiclename $= "trailertruck")
   {
      %list = GetVehiclePolyList("trailertruck_trailer");
      %name = "trailertrucktrailer";
      %size = GetSize("trailertrucktrailer");
   }
   
   %trailer =    new t2dStaticSprite() {
                  canSaveDynamicFields = "1";
                  layer = 1; 
                  CollisionPolyList = %list;
                  scenegraph = "sg";
                  imageMap = %name;
               };
   
   %trailer.SetCollisionActive(true, true);               
   %trailer.SetSize(%size);
   %x = getWord(%id.GetPosition(), 0);
   %y = getWord(%id.GetPosition(), 1);
   %x = %x+getRandom(-10, 10);
   %y = %y+getRandom(-10, 10);
      
   %trailer.SetPosition(%x SPC %y);
   %trailer.SetRotation(getRandom(0, 360));
   %trailer.block = 1;
}

function AttachRam(%id)
{
   if(%id.player $= 1)
   {
      %name = "batteringram1";
      %size = "5 5";
   }
   else
   {
      %name = "batteringram2";
      %size = "5.5 5";
   }
   
   %ram =    new t2dStaticSprite() {
                  canSaveDynamicFields = "1";
                  layer = 1; 
                  scenegraph = "sg";
                  imageMap = %name;
               };
   
   %ram.SetSize(%size);
   %id.ram = %ram;

   switch$ (%id.vehiclename)
   {
      case "minivan":
         %offset = "-.8";
      case "towtruck":
         %offset = "-.75";
      case "van":
         %offset = "-.9";
      case "limo":
         %offset = "-.7";
      case "sedan":
         %offset = "-.7";
      case "sportscar":
         %offset = "-.8";
      case "pickuptruck":
         %offset = "-.6";
      case "musclecar":
         %offset = "-.8";
      case "stationwagon":
         %offset = "-.7";
      case "humvee":
         %offset = "-.8";
      case "suv":
         %offset = "-.6";
      case "flatbedtruck":
         %offset = "-.9";
      case "trailertruck":
         %offset = "-.75";
      case "fireengine":
         %offset = "-.6";
      case "garbagetruck":
         %offset = "-.7";
      case "bus":
         %offset = "-.9";
      case "boxtruck":
         %offset = "-.8";
      case "cargovan":
         %offset = "-.8";
      case "ambulance":
         %offset = "-.8";
      case "construction":
         %offset = "-.60";
   }         

   %ram.Mount(%id, "0" SPC %offset, 0, true, true, true, true);      
}

function PutGasTankFireOut(%id)
{
   %rand = getRandom(1, 8);
   
   if(%rand == 8)
   {
      cancel(%id.gastankexplosion);     
      
      if(isObject(%id.gastankfire) $= true) 
         %id.gastankfire.Safedelete();
   }
}

// this is the distance a vehicle with keep within it's target, this will scale according to how many vehicles are in the game

function GetMaxTriggerDistance()
{
   if($CombatVehicles > 1 && $CombatVehicles <= 5)
      return 30;
   
   if($CombatVehicles > 6 && $CombatVehicles <= 10)
      return 40;

   if($CombatVehicles > 11 && $CombatVehicles <= 15)
      return 50;

   if($CombatVehicles > 16 && $CombatVehicles <= 20)
      return 60;
}

// when a vehicle is destroyed delete the sensors

function DestroySensors(%vehicle)
{
   if(isObject(%vehicle.bumper) $= true)
      %vehicle.bumper.Safedelete();
               
   if(isObject(%vehicle.middlesensor) $= true)
      %vehicle.middlesensor.Safedelete();
   
   if(isObject(%vehicle.rightforwardsensor) $= true)
      %vehicle.rightforwardsensor.Safedelete();

   if(isObject(%vehicle.leftforwardsensor) $= true)
      %vehicle.leftforwardsensor.Safedelete();
   
}

function UpdateBattleShortCuts()
{
   button_accelerate.tooltip = "Shortcut is " @ $keys_Accelerate @ " - Accelerate"; 
   button_attackcrew.ToolTip = "Shortcut is " @ $Keys_AttackCrew @ " - Attack Crew";
   button_attackengine.tooltip = "Shortcut is " @ $keys_AttackEngine @ " - Attack Engine";
   button_attackgastank.ToolTip = "Shortcut is " @ $Keys_AttackGasTank @ " - Attack Gas Tank";
   button_attackmountedweapon.tooltip = "Shortcut is " @ $Keys_AttackWeapon @ " - Attack Mounted Weapon";
   button_attacktires.tooltip = "Shortcut is " @ $Keys_AttackTires @ " - Attack Tires";
   button_boardvehicle.tooltip = "Shortcut is " @ $Keys_BoardVehicle @ " - Board Enemy Vehicle";
   button_brake.tooltip = "Shortcut is " @ $keys_Brake @ " - Brake";
   button_selecttarget.tooltip = "Shortcut is " @ $Keys_SelectTarget @ " - Select Target";
   button_ramvehicle.tooltip = "Shortcut is " @ $keys_RamVehicle @ " - Ram Vehicle";
}

// this will be a check for various function to make sure the vehicle is still in the game, not just
// as an object but a vehicle still fighting

function VehicleVerification(%id)
{
   if(isObject(%id) $= false)   
      return false;
   
   if(%id.player $= "" && %id.enemy $= "")
      return false;
      
   if($combat $= "")
      return false;
      
   return true;
}

function RotateTrailer(%vehicleid)
{
   if(%vehicleid.player $= "" && %vehicleid.enemy $= "")
      return;
   
   if(isObject(%vehicleid) $= 0)
      return;
      
   %vehicleid.trailer.RotateTo(%vehicleid.GetRotation(), 80);
   %vehicleid.rotateschedule = schedule(500, 0, "RotateTrailer", %vehicleid);
}

function SpinCheck::OnEnter(%this, %obj)
{
   // so the vehicle sensors don't trigger this
   
   if(%obj.vehiclename !$= "")
   {
      if(%obj.player $= 1)
         CheckForSpin($vehicles[%obj.slot, battleid]);   
      else
         CheckForSpin($Enemyvehicles[%obj.slot, battleid]);         
   }
}

function AddVehicleCrossbowIcons(%id)
{
   %id.crossbowicons = true;
   
   switch$ (%id.vehiclename)
   {
      case "compact":
         %num = 1;
      case "bike":
         %num = 1;
      case "jeep":
         %num = 1;
      case "dunebuggy":
         %num = 1;
      case "minivan":
         %num = 1;
      case "towtruck":
         %num = 2;
      case "van":
         %num = 2;
      case "limo":
         %num = 3;
      case "sedan":
         %num = 2;
      case "sportscar":
         %num = 1;
      case "pickuptruck":
         %num = 2;
      case "musclecar":
         %num = 2;
      case "stationwagon":
         %num = 2;
      case "humvee":
         %num = 2;
      case "suv":
         %num = 2;
      case "flatbedtruck":
         %num = 3;
      case "trailertruck":
         %num = 3;
      case "fireengine":
         %num = 3;
      case "garbagetruck":
         %num = 2;
      case "bus":
         %num = 3;
      case "boxtruck":
         %num = 3;
      case "cargovan":
         %num = 2;
      case "ambulance":
         %num = 2;
      case "construction":
         %num = 2;
   }         
   
   // right
   
   for(%i=1;%i<=%num;%i++)
   {
      if(%i $= 1)
         %offset = 0;
      if(%i $= 2)
         %offset = ".6";
      if(%i $= 3)
         %offset = "-.6";
         
      %icon =    new t2dStaticSprite() {
                     layer = 5; 
                     scenegraph = "sg";
                     imageMap = "crossbow";
                     size = "4 4";
                  };
      
      if(%id.trailer !$= "")
         %id = %id.trailer;
                           
      %icon.SetRotation(90);
      %icon.Mount(%id, ".8" SPC %offset, 0, true, true, true, true);            
   }
   
   // left

   for(%i=1;%i<=%num;%i++)
   {
      if(%i $= 1)
         %offset = 0;
      if(%i $= 2)
         %offset = ".6";
      if(%i $= 3)
         %offset = "-.6";

      %icon =    new t2dStaticSprite() {
                     layer = 6; 
                     scenegraph = "sg";
                     imageMap = "crossbow";
                     size = "4 4";
                  };

      if(%id.trailer !$= "")
         %id = %id.trailer;

      %icon.SetRotation(270);
      %icon.Mount(%id, "-.8" SPC %offset, 0, true, true, true, true);      
   }
}

// variables used for both sides of combat

$NavBlockTime = 6000;   // when a vehicle rotates to re-adjust to keep within range of the it's target, this stops it from constantly zig zagging
$ClearToFireguns = 4000;   // how much time has to pass before a vehicle guns again
$ClearToFireSide = 7000;   // when a vehicle is able to fire out of that side again
$specialWeaponRotation = 2000;   // the amount of time the ai will check to see if a mounted weapon needs to rotate
$boardnavtime = 3000; // when a vehicle is going in for a boarding attack and turning