Results 1 to 4 of 4

Thread: Function always returning "-1"

  1. #1
    Join Date
    Apr 2008
    Location
    Marquette, MI
    Posts
    15,252
    Mentioned
    138 Post(s)
    Quoted
    680 Post(s)

    Default Function always returning "-1"

    Hey guys, I've been having trouble with this function made to get the experience each player has gained. However, it always returns -1 instead of the exp.

    SCAR Code:
    function Prg_ExpGained: Extended;
    var
      XpGain : TStringArray;
      i : Integer;
    begin
      XpGain := ['tree', IntToStr(25), 'oak', FloatToStr(37.5),
                 'willow', FloatToStr(67.5), 'yew', IntToStr(175)];

      if InStrArrEx(Lowercase(Players[CurrentPlayer].Strings[0]), XpGain, i) then
        Result := (StrToInt(XpGain[i + 1]) * Players[CurrentPlayer].Integers[10])
      else
      begin
        Writeln('String not in DeclarePlayers.');
        S_NextPlayer;
      end;
    end;

    I have tried making it return an Integer, Variant, Extended, and I've tried using Round, but nothing seems to work. I will <3 you forever if you can fix this. Here is the whole script if you need it:

    SCAR Code:
    program PowerChopper;
      {.Include SRL\SRL\Misc\SMART.SCAR}
      {.include SRL\SRL.scar}
      //{.include SRL\SRL\Misc\Debug.scar}

    const
      ScriptVersion = 'V.1.4';
      SMARTWorld = 152;
      Members = False;
      SignedClient = True;

    var
      LevelUp, Loads, NumOfLogs, Breaks, c : Integer;
      XpGain : TExtendedArray;
      TP : TPoint;

    procedure DeclarePlayers;
    begin
      HowManyPlayers := 1;
      NumberOfPlayers(HowManyPlayers);
      CurrentPlayer := 0;

      with Players[0] do
      begin
        Name := '';
        Pass := '';
        Nick := '';//3-4 lowercase letters from username.
        Active := True;//Use this player?
        Strings[0] := 'willow';//Which tree to cut?
        Integers[0] := 28;//How many logs to cut?
        Booleans[0] := True;//Is hatchet equiped?
        BoxRewards := ['Xp', 'mote', 'ostume', 'oins', 'unes'];
      end;
     
     {with Players[1] do
      begin
        Name := '';
        Pass := '';
        Nick := '';
        Active := True;
        Strings[0] := '';
        Integers[0] := 000;
        Booleans[0] := True;
        BoxRewards := ['Xp', 'mote', 'ostume', 'oins', 'unes'];
      end;
     
      with Players[2] do
      begin
        Name := '';
        Pass := '';
        Nick := '';
        Active := True;
        Strings[0] := '';
        Integers[0] := 000;
        Booleans[0] := True;
        BoxRewards := ['Xp', 'mote', 'ostume', 'oins', 'unes'];
      end;
     
      with Players[3] do
      begin
        Name := 'Username';
        Pass := 'Password';
        Nick := 'Nickname';
        Active := True;
        Strings[0] := '';
        Integers[0] := 000;
        Booleans[0] := True;
        BoxRewards := ['Xp', 'mote', 'ostume', 'oins', 'unes'];
      end;
     
      with Players[4] do
      begin
        Name := 'Username';
        Pass := 'Password';
        Nick := 'Nickname';
        Active := True;
        Strings[0] := '';
        Integers[0] := 000;
        Booleans[0] := True;
        BoxRewards := ['Xp', 'mote', 'ostume', 'oins', 'unes'];
      end;}

    end;

    procedure S_Signature;
    begin
      ClearDebug;
      Writeln('                                            ___          ');
      Wait(250);
      Writeln('                       ____                /\__\         ');
      Wait(250);
      Writeln('                      /\___\     ____      \/ _/         ');
      Wait(250);
      Writeln('       _____          \/_  /    /\___\      \/ _____     ');
      Wait(250);
      Writeln('      /\____\  _______/ / /___ _\/___ \_____  /\____\    ');
      Wait(250);
      Writeln('     / /  ___\/\______\/ /____\\____/ /_____\/ / ____\   ');
      Wait(250);
      Writeln('    / /  /__ / / ___  / ____  //___  / ____  \/ /___\    ');
      Wait(250);
      Writeln('   / /  /___\ / /__/ / / / / /____/ / /  / / /____  /    ');
      Wait(250);
      Writeln('   \/_______//______/_/ /_/_/______/_/__/_/_/____/ /     ');
      Wait(250);
      Writeln('                             \/___________________/      ');
      Wait(250);
      Writeln(PadR('                       Anywhere Powerchopper ' + ScriptVersion +'!', 57));
    end;

    procedure S_FindRandoms;
    begin
      if not LoggedIn then Exit;
      LampSkill := 'woodcutting';
      FindNormalRandoms;
    end;

    procedure S_MyAntiBan;
    begin
      if not LoggedIn then Exit;
      case Random(120) of
        0: RandomMovement;
        1: BoredHuman;
        2: HoverSkill('random', false);
        3: RandomRClick;
        4: begin
             case Random(2) of
               0: begin
                    MakeCompass(20 + Random(40));
                    MakeCompass(320 - Random(40));
                  end;
               1: begin
                    MakeCompass(50 + Random(30));
                    MakeCompass(300 - Random(30));
                  end;
              end;
           end;
        5: begin
             MakeCompass(80 + Random(20));
             MakeCompass(280 - Random(80));
           end;
        6: HoverSkill('woodcutting', false);
        7: PickUpMouse;
      end;
    end;

    procedure S_NextPlayer;
    begin
      Players[CurrentPlayer].Active := False;
      Logout;
      Exit;
    end;

    function Lv_GetLevel: Integer;
    var
      Req : Integer;
    begin
      if not LoggedIn then Exit;
      Result := GetSkillLevel('woodcutting');
      Writeln('Woodcutting level is ' + IntToStr(Result) + '.');
      case Players[CurrentPlayer].Strings[0] of
        'oak': Req := 15;
        'willow': Req := 30;
        'yew': Req := 60;
      else
        Writeln('Tree isn''t in DeclarePlayers.');
      end;
      if (Result < Req) then
      begin
        Writeln('Your woodcutting level isn''t high enough to cut '
                + Players[CurrentPlayer].Strings[0] + 's, logging out.');
        S_NextPlayer;
      end;
    end;

    function Lv_LevelUp: Boolean;
    begin
      if not LoggedIn then Exit;
      Result := FindNPCChatText('ongrat', Nothing) or FindNPCChatText('dvanced', Nothing) or
                FindNPCChatText('ulatio', Nothing) or FindNPCChatText('oodcuti', Nothing);
      if Result then
      begin
        Writeln('Congratulations, you have gained a Woodcutting level!');
        ClickToContinue;
        Wait(80 + Random(100));
        HoverSkill('woodcutting', True);
        Inc(LevelUp);
        Players[CurrentPlayer].Integers[11] := LevelUp;
      end;
    end;

    procedure Brk_TakeBreak;
    var
      BrkTime : Integer;
    begin
      if not LoggedIn then Exit;
      BrkTime := 780000 + Random(240000);
      if Players[CurrentPlayer].Booleans[1] then
      begin
        Writeln('Taking a 15 minute break.');
        Logout;
        Wait(BrkTime);
        Writeln('Break over, logging back in.');
        LoginPlayer;
        Inc(Breaks);
      end;
    end;

    procedure Cut_FindHatchet;
    var
      x, y, i : Integer;
      HatchetDTM : Array of Integer;
    begin
      if not LoggedIn then Exit;
      SetArrayLength(HatchetDTM, 2);
      //All except dragon.
      HatchetDTM[0] := DTMFromString('78DA63CC6664605805C448A0354590811F488' +
              '344FF0301631690B509550D44164602E934206B23013520739611' +
              '50930F642D24A0A61CC85A44404D2990B51ABF1A00963F0CC9');
      //Dragon.
      HatchetDTM[1] := DTMFromString('78DA63F4676460B800C448205C989B810B488' +
              '344FF0301A30790751A550D44164602696720EB2201355E40D655' +
              '026A6C81AC2704D404035937F1AB01006E8F0B4B');
      if Players[CurrentPlayer].Booleans[0] then
        GameTab(tab_Equip)
      else
        GameTab(tab_Inv);
      if FindDTM(HatchetDTM[i], x, y, MIX1, MIY1, MIX2, MIY2) then
        Writeln('Found hatchet.')
      else
      begin
        Writeln('Didn''t find hatchet, logging out...');
        S_NextPlayer;
      end;
      FreeDTM(HatchetDTM[i]);
    end;

    function Col_Trees : Integer;
    var
      Colors : TStringArray;
      i : Integer;
    begin
      if not LoggedIn then Exit;
      Colors := ['tree', IntToStr(2450006), 'oak', IntToStr(3764069),
                 'willow', IntToStr(4481105), 'yew', IntToStr(4162165)];
                       
      if InStrArrEx(Lowercase(Players[CurrentPlayer].Strings[0]), Colors, i) then
        Result := StrToInt(Colors[i + 1])
      else
      begin
        Writeln('String not in DeclarePlayers.');
        S_NextPlayer;
      end;
    end;

    procedure Col_Modifiers;
    begin
      if not LoggedIn then Exit;
      case Lowercase(Players[CurrentPlayer].Strings[0]) of
        'tree' : SetColorSpeed2Modifiers(0.05, 1.37);
        'oak' : SetColorSpeed2Modifiers(0.10, 1.63);
        'willow' : SetColorSpeed2Modifiers(0.18, 0.28);
        'yew' : SetColorSpeed2Modifiers(0.01, 0.76);
      end;
    end;

    function Col_Tolerance: Integer;
    begin
      if not LoggedIn then Exit;
      case Lowercase(Players[CurrentPlayer].Strings[0]) of
        'tree' : Result := 6;
        'oak' : Result := 5;
        'willow' : Result := 14;
        'yew' : Result := 14;
      end;
    end;

    function Cut_TreeCutDown: Boolean;
    var
      TPA : TPointArray;
    begin
      if not LoggedIn then Exit;
      ColorToleranceSpeed(2);
      Col_Modifiers;
      FindColorsTolerance(TPA, Col_Trees, TP.x - 15, TP.y - 15, TP.x + 15, TP.y + 15, Col_Tolerance);
      if (Length(TPA) < 5) then
      begin
        //Writeln('Length inside box: ' + IntToStr(Length(TPA)));
        Result := True;
      end;
      SetColorSpeed2Modifiers(0.2, 0.2);
      ColorToleranceSpeed(1);
    end;

    procedure Cut_WhileChopping;
    var
      ClickTree, NCTime, LastCount : Integer;
    begin
      MarkTime(ClickTree);
      NCTime := 15000 + Random(10000);
      LastCount := InvCount;
      while (InvCount < 28) and LoggedIn do
      begin
        if Cut_TreeCutDown then Break;
        if TimeFromMark(ClickTree) > NCTime then
          if (InvCount = LastCount) then Break;
        S_MyAntiBan;
        Wait(80 + Random(1000));
        Lv_LevelUp;
        S_FindRandoms;
      end;
    end;

    procedure Cut_ChopTrees;
    var
      i, x, y: Integer;
      t : String;
      TPA : TPointArray;
      ATPA : T2DPointArray;
    begin
      if not LoggedIn then Exit;
      SetRun(True);
      ColorToleranceSpeed(2);
      Col_Modifiers;
      repeat
        FindColorsSpiralTolerance(MSCX, MSCY, TPA, Col_Trees, MSX1, MSY1, MSX2, MSY2, Col_Tolerance);
        ATPA := SplitTPAEx(TPA, 5, 5);
        //DebugATPA(ATPA, '');
       
        t := Players[CurrentPlayer].Strings[0];
        if (Length(ATPA) = 0) then
        begin
          if FindObj(x, y, Copy(t, 2, Length(t)), Col_Trees, Col_Tolerance) then
          begin
            Writeln('Color finding failed, found tree using FindObj...');
            Mouse(x, y, 0, 0, True);
          end else
          begin
            Writeln('Couldn''t find trees, logging out.');
            S_NextPlayer;
          end;
        end;

        SortATPAFromFirstPoint(ATPA, Point(MSCX, MSCY));
        for i := 0 to High(ATPA) do
        begin
          //Writeln('Length:' + IntToStr(Length(ATPA[i])));
          if (Length(ATPA[i]) < 30) then Continue;
          MiddleTPAEx(ATPA[i], x, y);
          MMouse(x, y, 4, 4);
          Wait(200 + Random(300));
          t := Players[CurrentPlayer].Strings[0];
          if WaitUpText(Copy(t, 2, Length(t)), 500) then
          begin
            GetMousePos(x, y);
            Wait(80 + Random(100));
            Mouse(x, y, 0, 0, True);
            TP := IntToPoint(x, y);
            Cut_WhileChopping;
          end;
        end;
      until(InvFull or not LoggedIn);
      if InvFull then
        Writeln('Inventory full, dropping logs...');
      SetColorSpeed2Modifiers(0.2, 0.2);
      ColorToleranceSpeed(1);
    end;

    function Cut_DropLogs: Integer;
    var
      Logs, x, y, a : Integer;
    begin
      MakeCompass('n');
      Logs := DTMFromString('78DA630C6364607804C44820C84B96811F48C' +
           '34419C381AC2BA86A60B270358940D675026AA280AC6704D4B803' +
           '598FF1AB0100F99906E6');
      if FindDTM(Logs, x, y, MIX1, MIY1, MIX2, MIY2) then
        Result := CountItems('dtm', Logs, []);
      if Players[CurrentPlayer].Booleans[0] then
      begin
        Wait(80 + Random(100));
        for a := 1 to 28 do
          DropItem(a);
      end else
      begin
        Wait(80 + Random(100));
        for a := 2 to 28 do
          DropItem(a);
      end;
      Inc(Loads);
      Players[CurrentPlayer].Integers[10] := NumOfLogs + Result;
      Players[CurrentPlayer].Integers[9] := Loads;

      if (Players[CurrentPlayer].Integers[10] >= Players[CurrentPlayer].Integers[0]) then
      begin
        Writeln('Cut desired number of logs, logging out.');
        S_NextPlayer;
      end;
      FreeDTM(Logs);
    end;

    function Prg_PlayerActive(i : Integer): String;
    begin
      if Players[i].Active then Result := 'T' else Result := 'F';
    end;

    function Prg_ExpGained: Extended;
    var
      XpGain : TStringArray;
      i : Integer;
    begin
      XpGain := ['tree', IntToStr(25), 'oak', FloatToStr(37.5),
                 'willow', FloatToStr(67.5), 'yew', IntToStr(175)];

      if InStrArrEx(Lowercase(Players[CurrentPlayer].Strings[0]), XpGain, i) then
        Result := (StrToInt(XpGain[i + 1]) * Players[CurrentPlayer].Integers[10])
      else
      begin
        Writeln('String not in DeclarePlayers.');
        S_NextPlayer;
      end;
    end;

    procedure Prg_ProgReport;
    var
      i : Integer;
    begin
      Writeln('[===============================================]');
      Writeln(PadR('[        Coh3n''s Anywhere PowerChopper', 48) + ']');
      Writeln('[==================== ' + ScriptVersion + ' ====================]');
      if (HowManyPlayers <= 1) then
      begin
        Writeln('[                                               ]');
        Writeln(PadR('[  Ran For: ' + TimeRunning, 48) + ']');
        Writeln(PadR('[  Loads Done: ' + IntToStr(Players[CurrentPlayer].Integers[9]), 48) + ']');
        Writeln(PadR('[  Logs Chopped: ' + IntToStr(Players[CurrentPlayer].Integers[10]), 48) + ']');
        Writeln(PadR('[  Exp Gained: ' + FloatToStr(Prg_ExpGained), 48) + ']');
        Writeln(PadR('[  Levels Gained: ' + IntToStr(Players[CurrentPlayer].Integers[11]), 48) + ']');
        Writeln(PadR('[  Breaks: ' + IntToStr(Breaks), 48) + ']');
        Writeln('[                                               ]');
      end else
      begin
        Writeln(PadR('[  Ran For: ' + TimeRunning, 48) + ']');
        Writeln('[                                               ]');
        Writeln('[    Nick     T/F     Loads     Logs    Lvls    ]');
        Writeln('[    ¯¯¯¯     ¯¯¯     ¯¯¯¯¯     ¯¯¯¯    ¯¯¯¯    ]');
        for i := 0 to High(Players) do
        begin
          for i := 9 to 11 do
          begin
            if (Players[CurrentPlayer].Integers[i] > 0) then
              Players[CurrentPlayer].Integers[i] := Players[CurrentPlayer].Integers[i]
            else
              Players[CurrentPlayer].Integers[i] := 0;
          end;
          Writeln('[    ' + PadR(Players[i].Nick, 9) + PadR(Prg_PlayerActive(i), 9)
                          + PadR(IntToStr(Players[i].Integers[9]), 9)
                          + PadR(IntToStr(Players[i].Integers[10]), 9)
                          + PadR(IntToStr(Players[i].Integers[11]), 7) + ']');
        end;
      end;
      Writeln('[===============================================]');
      Writeln('[              Thank-you for using              ]');
      Writeln('[        Coh3n''s Anywhere Powerchopper!         ]');
      Writeln('[===============================================]');
      Writeln('[     Be sure to post your progress reports     ] ');
      Writeln('[             at VillaVu/SRL-Forums!            ]');
      Writeln('[===============================================]');
    end;

    procedure F_PlayerSetup;
    begin
      LoginPlayer;
      SetAngle(True);
      Cut_FindHatchet;
      Lv_GetLevel;
    end;

    procedure F_ScriptSetup;
    begin
      ClearDebug;
      Disguise('Coh3n''s PowerChopper');
      S_Signature;
      Smart_Server := SMARTWorld;
      Smart_Members := Members;
      Smart_Signed := SignedClient;
      Smart_SuperDetail := False;
      Writeln('Loading, please wait...');
      SetupSRL;
      DeclarePlayers;
    end;

    function F_KillScript: Boolean;
    var
      Start, x, y : Integer;
    begin
      Result := False;
      if not LoggedIn then
      begin
        MarkTime(Start);
        Writeln('Found Login screen...');
        if (TimeFromMark(Start) >= 1100000) then
        begin
          Writeln('We have been logged out for longer than 25 minutes, terminating script...');
          Result := True;
          if LoggedIn then Exit;
        end;
      end else Exit;
    end;

    procedure F_MainLoop;
    var
      Start : Integer;
    begin
      F_ScriptSetup;
      repeat
        F_PlayerSetup;
        MarkTime(Start);
        repeat
          Col_Trees;
          repeat
            Cut_ChopTrees;
          until(InvFull);
          Cut_DropLogs;
          Prg_ProgReport;
          if (TimeFromMark(Start) > 3600000) then
          begin
            if (HowManyPlayers = 1) then
              Brk_TakeBreak
            else
              NextPlayer(Players[CurrentPlayer].Active);
          end;
        until((Players[CurrentPlayer].Integers[10] >= Players[CurrentPlayer].Integers[0]) or not LoggedIn);

        NextPlayer(Players[CurrentPlayer].Active);
        if F_KillScript then
          TerminateScript;
       
      until(AllPlayersInactive);
    end;

    procedure ScriptTerminate;
    begin
      Prg_ProgReport;
    end;

    begin
      F_MainLoop;
    end.
    Also, if someone feels like looking through my MultiPlayer progress report, I'm pretty sure it doesn't work properly. It doesn't record for each player properly. If you see the error please let me know.

    Thanks,

    Coh3n
    Last edited by Coh3n; 08-22-2009 at 01:07 PM.

  2. #2
    Join Date
    Feb 2009
    Posts
    1,447
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    SCAR Code:
    if InStrArr(XpGain, Lowercase(Players[CurrentPlayer].Strings[0]), False) then

    Try that?

    Edit: Nevermind, that doesnt work

    Edit2: Its something to do with the "." in the xp gain in every log.
    Last edited by TRiLeZ; 08-22-2009 at 01:36 PM.

  3. #3
    Join Date
    Oct 2006
    Location
    ithurtsithurtsithurtsithurts
    Posts
    2,930
    Mentioned
    7 Post(s)
    Quoted
    135 Post(s)

    Default

    Changing this
    SCAR Code:
    Result := (StrToInt(XpGain[i + 1]) * Players[CurrentPlayer].Integers[10])
    to this
    SCAR Code:
    Result := (StrToFloat(XpGain[i + 1]) * Players[CurrentPlayer].Integers[10])
    should fix your problem.

    Edit: A bit of explanation. The problem was that calling StrToInt on the oak or willow experience values returned a -1 because 37.5 and 67.5 are not valid integer values.

  4. #4
    Join Date
    Apr 2008
    Location
    Marquette, MI
    Posts
    15,252
    Mentioned
    138 Post(s)
    Quoted
    680 Post(s)

    Default

    Quote Originally Posted by senrath View Post
    Changing this
    SCAR Code:
    Result := (StrToInt(XpGain[i + 1]) * Players[CurrentPlayer].Integers[10])
    to this
    SCAR Code:
    Result := (StrToFloat(XpGain[i + 1]) * Players[CurrentPlayer].Integers[10])
    should fix your problem.

    Edit: A bit of explanation. The problem was that calling StrToInt on the oak or willow experience values returned a -1 because 37.5 and 67.5 are not valid integer values.
    Ah yes, good call! Thanks very much.

    It works. <3 you senrath!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •