//       Simba script, go here if u need help http://villavu.com/forum/showthread.php?t=103209                                             //
//  *--        Script by: Ryutoh                 --*  //
//  *--        5/24/2013                         --*  //
//  *--        Barrows Fighter V1                --*  //
//                                                    //

program barrowsfighter;
{$DEFINE SMART}
{$I SRL-OSR/SRL.Simba}
{$i SRL-OSR\SRL\misc\SmartGraphics.simba}

  //Please fill in the configuration below.
  //*I also recommend that you kill the barrows brothers before starting

const      
  //Email or Username
  username = '';

  //Password
  password = '';

  //HP to eat food at
  hpToEat = 60;

  //Teleport out when out of food or if hp drops too low? (Use any teletab)
  teleportOut = 'false'; //(True/False)

  //HP to teleport out at if almost dead?
  hpToTeleAt = 15;

  //Avalible pre-set food types: trout, salmon, tuna, lobster, monkfish, shark.
  foodName = 'shark'; //*Recommend monkfish or sharks.

  //1 top left box, 2 top right, 3 bottom left, 4 bottom right
  trainingmode = 2;

  //Spec weapons avalible: excalibur, dba(dragon battle axe)
  specWeapon = 'excalibur'; //Leave empty if none

  //If you're using spec, fill in main weapon info
  mainWeaponName = 'cimitar'; //Use short part of weapon name
  mainWepColor1 = 858228; //Color info
  mainWepColor2 = 858748;

  //Only right click monsters? Might prevent misclicks of opening doors
  //but averages about 3 - 5k less exp/hour
  rightClickOnly = 'false'; //(True/False)

  //How fast to move the mouse?
  customMouseSpeed = 16; //1-10 slow, 11-20 med, 20+ very fast

  //Use a faster but less accurate method of fighting?
  //Can improve exp rates by about 3 - 5k exp/hour
  //(Works best with fast attacking weapons)
  newFightMethod = 'true'; //(True/False)


//////////////////////////////////////////////////////////////////
var
  X, Y, T, Clicked, teletab, skillwindow, changeSpecTime, blueHitSplat, redHitSplat, dbaDTM, KC, KillsPerHour, TimeGone, lost, totalExp, hitcount, counter, excalDTM : integer;
  detectHitSplat : boolean;
  npcToKill : string;

procedure declarePlayers;
begin
  HowManyPlayers := 1;
  NumberOfPlayers(HowManyPlayers);
  CurrentPlayer := 0;
  with Players[0] do
  begin
    Name        := username;
    Pass        := password;
    Active      := True;
  end;
end;

procedure proggy;
begin
  if not (KC < 1) then
    begin
      SMART_ClearCanvas;
      TimeGone := GetTimeRunning / 1000;
      KillsPerHour := 3600 * KC / TimeGone;

      SMART_DrawText(8, 23, LoginChars, 'Barrows Fighter by Ryutoh', clwhite);
      SMART_DrawText(8, 40, UpChars07, 'Kills per hour around:', clwhite);
      SMART_DrawText(154, 40, UpChars07, '' + IntToStr(KillsPerHour) + ', or ' + IntToStr(KillsPerHour * 180) +' experience/hr', clyellow);

      if (specWeapon = 'excalibur') or (specWeapon = 'dba') then
      begin
        SMART_DrawText(8, 54, UpChars07, 'Spec timer: ', clwhite);
        SMART_DrawText(87, 54, UpChars07, '' + inttostr(TimeFromMark(T) / 1000) + ' / ' + IntToStr(changeSpecTime / 1000) + ' seconds.', clyellow);
      end
    end else
  exit;
end;

procedure findMonster;
begin
    writeln('Monsters lost, looking around.');
    if lost > 10 then
    begin
      sleep(7000 + random(4000));
      logout;
      if (LoggedIn) then
        begin
          RunAway('e', True, 2, 3000);
          sleep(3000 + random(1000))
          logout;
        end;
      writeln('Unable to find monsters after 10 tries, logging out.');
      writeln('Ran for: ' + TimeRunning);
      totalExp := KC * 180;
      writeln('Killcount: ' + IntToStr(KC) + ', thats ' + IntToStr(totalExp) + ' experience!');
      terminatescript;
    end else
    begin
      writeln('No monsters found, spinning.');
      SetAngle(SRL_ANGLE_HIGH);
      makecompass(randomrange(0, 360))
      wait(200 + random(300))
      Inc(lost)
    end;
end;

procedure spec;
begin
if (specWeapon = 'excalibur') or (specWeapon = 'dba') then
  begin
  excalDTM := DTMFromString('mWAAAAHicY2FgYEhlYmDIB+JsIM4B4k9AsUdA/ACIHzOAACMc9zY3MyADLhRZCAYBAKQ4BvM=');
  dbaDTM := DTMFromString('mggAAAHicY2NgYFADYikglgRiHiAWA2IZIE4C4kAgdgPiICAOBmJfBhBgxMAFEjwMXEAWOpbEqhqCIQAAKTMDcw==');
  if (TimeFromMark(T) >= changeSpecTime) then
    begin
    gametab(tab_inv)
    writeln('Time for spec!')
      case (specWeapon) of
        'excalibur':
        if FindDTM(excalDTM, X, Y, MIX1, MIY1, MIX2, MIY2) then
        begin
          MarkTime(T)
          Mouse(X + random(2), Y + random(2), 2, 2, 1);
          wait(500 + random(300))
        end;
        'dba':
        if FindDTM(dbaDTM, X, Y, MIX1, MIY1, MIX2, MIY2) then
        begin
          MarkTime(T)
          Mouse(X + random(2), Y + random(2), 2, 2, 1);
          wait(500 + random(300))
        end;
      end;
      gametab(tab_Combat)
      wait(300)

      if FindColorTolerance(X, Y, 4229183, 700, 417, 712, 427, 20) then
      begin
        wait(100)
        Mouse(647 + random(2), 423 + random(2), 2, 2, 1);
        wait(200 + random(100))
        changeSpecTime := 340000 + random(40000);
      end else
      begin
        writeln('Not ready for spec yet, waiting longer.')
        changeSpecTime := 60000;
      end;

      gametab(tab_inv)
      wait(500)

      if FindObjEx(X, Y, ['ield', mainWeaponName], [mainWepColor1, mainWepColor2], 12, 50, MIX1, MIY1, MIX2, MIY2) then
      begin
        writeln('Wielding main weapon back.')
        Mouse(X + random(2), Y + random(2), 2, 2, 1);
      end else
      begin
        writeln('Something is wrong with main weapon color or name.')
        writeln('Please fix this, or dont use spec.')
        sleep(15000)
        terminatescript;
      end
    end;
  FreeDTM(excalDTM)
  FreeDTM(dbaDTM)
  end
end;

Function attackLeft : boolean;
var
  MonsterTPA, GreenHpTPA : TPointArray;
  MonsterATPA : T2DPointArray;
  MonsterBox : TBox;
  r, i : integer;
begin
  case (npcToKill) of
    'bloodworms':
    begin
      if not FindObjEx(X, Y, ['ttak', 'loodwor'], [1000804, 4103847, 674403, 1206922, 1144980, 1596800], 12, 50, MSX1, MSY1, MSX2, MSY2) then
      begin
        findMonster;
        exit;
      end
    end;

    'rats':  //Not done yet
    begin
    if FindColorsTolerance(MonsterTPA, 7178348, MSX1, MSY1, MSX2, MSY2, 10) then
      begin
        MonsterATPA := SplitTPAEx(MonsterTPA, 10, 10);
        SortATPASize(MonsterATPA, True);
        for i := 0 to high(MonsterATPA) do
        begin
          MonsterBox := GetTPABounds(MonsterATPA[i]);
          if not FindColor(X, Y, 255, MonsterTPA[i].x-12, MonsterTPA[i].y-17, MonsterTPA[i].x+12, MonsterTPA[i].y+17) or not FindColor(X, Y, 65280, MonsterTPA[i].x-12, MonsterTPA[i].y-17, MonsterTPA[i].x+12, MonsterTPA[i].y+17) THEN
              writeln('hp box not found');
          if i = high(MonsterATPA) then
          begin
            writeln('None found');
          end;
        end;
        r := random(length(MonsterTPA));
      end else
      begin
        findMonster;
        exit;
      end
    end;

    'skeletons':
    begin
    end;

  end;
  Clicked := DTMFromString('mWAAAAHicY2FgYNBhZGAwhWIQ+xgDBJ+H4v9oGB0womEQAAC5ewf1');
  writeln('Monster found at: ' + IntToStr(x) + ', ' + IntToStr(y));
  if not IsUpText('oor') then
    begin
      ClickMouse(X, Y, mouse_left);
      wait(50);
      if FindDTM(Clicked, X, Y, MSX1, MSY1, MSX2, MSY2) then
        begin
          makecompass(randomrange(0, 360))
          wait(2500 + random(1050))
          Inc(KC)
          lost := 0;
          FreeDTM(Clicked);
        end else
        begin
          writeln('Miss clicked, attempting to click again.');
          wait(250 + random(50))
          FreeDTM(Clicked);
          attackLeft;
        end;
    end else
    attackLeft;
end;

procedure attackRight;
begin
  case (npcToKill) of
    'bloodworms':
    begin
      if not FindObjEx(X, Y, ['ttak', 'loodwor'], [1000804, 4103847, 674403, 1206922, 1144980, 1596800], 12, 50, MSX1, MSY1, MSX2, MSY2) then
      begin
        findMonster;
        exit;
      end;
    end;

    'rats':
    begin
      if not FindObjEx(X, Y, ['ttak', 'iant', 'rypt', 'at'], [8358276, 6975078, 8358271, 6058844, 7244398, 6190685, 7371120, 9278344, 5136973, 4738632], 3, 50, MSX1, MSY1, MSX2, MSY2) then
      begin
        findMonster;
        exit;
      end;
    end;

    'skeletons':
    begin
    end;
  end;
  ClickMouse(X, Y, mouse_right);
  wait(200+random(100));
  if ChooseOptionMulti(['tta']) then
    begin
      ChooseOptionMulti(['tta']);
      makecompass(randomrange(0, 360))
      wait(2500 + random(1050))
      Inc(KC)
      lost := 0;
    end else
    begin
      writeln('Miss clicked, attempting to click again.');
      attackRight;
    end
end;

procedure attackMonster;
begin
  if not LoggedIn then LoginPlayer;
    if (rightClickOnly = 'true') then
      begin
        attackRight;
      end else
      begin
        case random(5) of
          0..3 : begin attackLeft; end;
          4 : begin attackLeft; end;    //right
        end
      end
end;

procedure antiBan;
begin
  case random(160) of
    0..20: begin hoverskill(skill_strength, false) sleep(random(300)) end;
    21..30: begin hoverskill(skill_hitpoints, false) sleep(random(300))  end;
    31..40: begin hoverskill(skill_attack, false) sleep(random(300)) end;
    41..50: begin hoverskill(skill_defence, false) sleep(random(300)) end;
    51..53: begin hoverskill(skill_range, false) sleep(random(300)) end;
    54..56: begin hoverskill(skill_prayer, false) sleep(random(300)) end;
    57: begin hoverskill(skill_slayer, false) sleep(random(300)) end;
    58: begin hoverskill(skill_woodcutting, false) sleep(random(300)) end;
    59: begin hoverskill(skill_cooking, false) sleep(random(300)) end;
    60: begin hoverskill(skill_fishing, false) sleep(random(300)) end;
    61: begin hoverskill(skill_firemaking, false) sleep(random(300)) end;
    62: begin hoverskill(skill_mining, false) sleep(random(300)) end;
    63..65: begin HoverOnlineFriend; sleep(100 + random(100)) end;
    66:
    begin
      case random(3) of
        1:
        begin
        writeln('Taking short 20 - 50 second break.');
        wait(20000 + random(30000))
        end;
      end
    end;
    //67: begin adjustmousespeed(2, 10, 20) sleep(random(300)) end;
    67..69: begin randomRClick; sleep(random(300)) end;
    70..71: begin hovermovingobject; sleep(random(300)) end;
    72..75: begin  MMouseOffClient('top'); sleep(random(300)) end;
    76..79: begin MMouseOffClient('left'); sleep(random(300)) end;
    80..83: begin MMouseOffClient('right'); sleep(random(300)) end;
    84..87:begin MMouseOffClient('bottom'); sleep(random(300)) end;
    88..89:
    begin
    if FindObjEx(X, Y, ['ttak', 'loodwor'], [1000804, 4103847, 674403, 1206922, 1144980, 1596800], 8, 50, MSX1, MSY1, MSX2, MSY2)  then
      Mouse(X, Y, 5, 5, 0);
      wait(500 + random(500))
      ChooseOptionMulti(['xamine']);
      sleep(random(100))
    end;
    90..100: begin RandomTab(True); makecompass(randomrange(0, 360)) wait(500 + random(300)) end;
    101..110: begin RandomMovement; sleep(random(500)) end;
    111..145: begin makecompass(randomrange(0, 360)) wait(300 + random(100)) end;
    146..147: begin BoredHuman; end;
    148: begin ExamineInv; end;
    149: begin RandomFKeys(True); end;
    150: begin DragRandomInvSlot; end;
    151..160: begin makecompass(randomrange(0, 360)) wait(300 + random(300)) end;
  end
end;

procedure terminate(logoutMessage:string);
begin
  if (logoutMessage = 'food') then
    begin
      writeln('No food found, attempting to logout.');
    end
  else if (logoutMessage = 'teletab') then
    begin
      writeln('Low hp found, attempting to logout.');
    end;
  sleep(7000 + random(4000))
  logout;
  if (LoggedIn) then
  begin
    RunAway('e', True, 2, 3000);
    sleep(3000 + random(1000))
    logout;
  end;
  writeln('Ran for: ' + TimeRunning);
  totalExp := KC * 180;
  writeln('Killcount: ' + IntToStr(KC) + ', thats ' + IntToStr(totalExp) + ' experience!');
  terminatescript;
end;

procedure teleport;
begin
  gametab(tab_inv)
  wait(300 + random (200))
  if gametab(tab_inv) then
    begin
      teletab := DTMFromString('mlwAAAHicY2dgYLBnYmDwA+IAKO0IxGZAbArE14Hyt4D4CRB/AOJPUPopAwgw4sG4ARG6AKj+CEc=');
      if FindDTM(teletab, X, Y, MIX1, MIY1, MIX2, MIY2) then
        begin
          writeln('Teleporting.');
          MouseBox(X, Y, X, Y, mouse_right);
          if ChooseOptionMulti(['rea']) then
            begin
              ChooseOptionMulti(['rea'])
              wait(500)
              FreeDTM(teletab);
            end else
            FreeDTM(teletab);
            teleport;
        end else
        writeln('No teletab found.');
    end
end;

procedure checkHealth;
begin
  if GetSkillInfo('hitpoints', true) <= hpToTeleAt then
    begin
      if (teleportOut = 'true') then
        begin
          teleport;
          terminate('teletab')
        end
    end;
    if GetSkillInfo('hitpoints', true) > hpToEat then
      begin
        case random(6) of
          0:
          begin
            writeln('Random antiban.');
            antiBan;
          end;
        end
      end else
      begin
        gametab(tab_inv)
        wait(300 + random (200))
        if gametab(tab_inv) then
          begin
            case (foodName) of
              'trout':
              if  FindObjEx(X, Y, ['at', 'rout'], [7304076, 8949164, 5593195], 10, 50, MIX1, MIY1, MIX2, MIY2) THEN
              begin
                writeln('Eatting trout.');
                MouseBox(X + 3, Y + 3, X + 3, Y + 3, mouse_left);
                sleep(2000 + random(500))
              end else
              begin
                if (teleportOut = 'true') then
                  begin
                    teleport;
                  end;
                terminate('food');
              end;
              'salmon':
              if  FindObjEx(X, Y, ['at', 'almon'], [1459962, 6912203, 864166], 10, 50, MIX1, MIY1, MIX2, MIY2) THEN
              begin
                writeln('Eatting salmon.');
                MouseBox(X + 3, Y + 3, X + 3, Y + 3, mouse_left);
                sleep(2000 + random(500))
              end else
              begin
                if (teleportOut = 'true') then
                  begin
                    teleport;
                  end;
                terminate('food');
              end;
              'tuna':
              if  FindObjEx(X, Y, ['at', 'una'], [7238282, 8686246, 5527401], 10, 50, MIX1, MIY1, MIX2, MIY2) THEN
              begin
                writeln('Eatting tuna.');
                MouseBox(X + 3, Y + 3, X + 3, Y + 3, mouse_left);
                sleep(2000 + random(500))
              end else
              begin
                if (teleportOut = 'true') then
                  begin
                    teleport;
                  end;
                terminate('food');
              end;
              'lobster':
              if  FindObjEx(X, Y, ['at', 'obster'], [1664960, 1467065, 1200019], 10, 50, MIX1, MIY1, MIX2, MIY2) THEN
              begin
                writeln('Eatting lobster.');
                MouseBox(X + 3, Y + 3, X + 3, Y + 3, mouse_left);
                sleep(2000 + random(500))
              end else
              begin
                if (teleportOut = 'true') then
                  begin
                    teleport;
                  end;
                terminate('food');
              end;
              'monkfish':
              if  FindObjEx(X, Y, ['at', 'onkfish'], [4478822, 2436663, 3293259], 10, 50, MIX1, MIY1, MIX2, MIY2) THEN
              begin
                writeln('Eatting monkfish.');
                MouseBox(X + 3, Y + 3, X + 3, Y + 3, mouse_left);
                sleep(2000 + random(500))
              end else
              begin
                if (teleportOut = 'true') then
                  begin
                    teleport;
                  end;
                terminate('food');
              end;
              'shark':
              if  FindObjEx(X, Y, ['at', 'hark'], [5860483, 7375014, 6584980], 10, 50, MIX1, MIY1, MIX2, MIY2) THEN
              begin
                writeln('Eatting shark.');
                MouseBox(X + 3, Y + 3, X + 3, Y + 3, mouse_left);
                sleep(2000 + random(500))
              end else
              begin
                if (teleportOut = 'true') then
                  begin
                    teleport;
                  end;
                terminate('food');
              end;
            end;
          end;
      end;
end;

procedure isInCombat;
begin
if newFightMethod = ('true') then
  begin
    repeat
    blueHitSplat := DTMFromString('mQwAAAHicY2ZgYOBkZGBgAeL/QPZuID4JxHuAmJm1FI6RASMSBgIA1cUEzA==');
    redHitSplat := DTMFromString('mQwAAAHicY2ZgYPgIxC+A+B0QrwHidUC8C4izGRnhGBkwImEgAAAzRQYp');
    if FindDTM(blueHitSplat, X, Y, 179, 126, 345, 255) or FindDTM(redHitSplat, X, Y, 179, 126, 345, 255) THEN
      begin
        Inc(hitcount)
        Inc(counter)
        //writeln('combat: true '+inttostr(counter)+'');//checking for combat
        detectHitSplat := true;
        wait(200)
        FreeDTM(blueHitSplat)
        FreeDTM(redHitSplat)
      end else
      begin
        Inc(hitcount)
        Dec(counter)
        wait(200)
        //writeln('combat: false '+inttostr(counter)+'');//checking for combat
        FreeDTM(blueHitSplat)
        FreeDTM(redHitSplat)
      end;
    until(hitcount = 4)
    if not (counter <= -1) then
      begin
        proggy;
        checkHealth;
        wait(700 + random(300))
      end else if (counter <= -1) then
      begin
        attackmonster;
      end;
      detectHitSplat := false;
      hitcount := 0;
      counter := 0;
    end else
    if FindColor(X, Y, 255, 213, 117, 305, 185) or FindColor(X, Y, 65280, 213, 117, 305, 185) THEN
    begin
      proggy;
      checkHealth;
      wait(1200 + random(500))
    end else
    begin
      attackmonster;
    end;
end;

procedure failsafes;
begin
skillwindow := DTMFromString('mWAAAAHicY2FgYHjByMDwF4pBbG6gmDwDgn6wowoFowNGNAwCALUWDHM=');
if FindDTM(skillwindow, X, Y, MSX1, MSY1, MSX2, MSY2) then
  begin
    writeln('Skill window found open, attempting to close.');
    MouseBox(X + 3, Y + 3, X + 3, Y + 3, mouse_left);
    FreeDTM(skillwindow)
  end;
FreeDTM(skillwindow)
end;

begin
  declarePlayers;
  SetupSRL();
  ActivateClient;
  writeln('Barrows Fighter by Ryutoh')
  writeln('Currently V1, please report bugs and errors on forum.')
  MarkTime(T)
  changeSpecTime := 1;
  npcToKill := 'bloodworms'; //Still not complete, keep as bloodworms
  repeat
  if (not LoggedIn) then
    begin
      writeln('Logging player in.');
      LogInPlayer;
      setangle(0)
      setrun(true)
      setfightmode(trainingmode)
      gametab(tab_stats)
      writeln('Starting.');
      MouseSpeed := customMouseSpeed;
    end;
    if (LoggedIn) then
    begin
      failsafes;
      spec;
      isInCombat;
    end;
  until(false)
end.