Results 1 to 15 of 15

Thread: improve my script

  1. #1
    Join Date
    Feb 2008
    Posts
    517
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default improve my script

    hey peoples im making a walker for reflection and was wondering if anyone could help me improve my scripts, weather it be improving the execution time or simply shortening the code (or even making the code more readable)


    basically to all you people who wonder wtf this is, it's a script that uses a linked list of paths, that will get the characters location (right now you give the location and it finds the closest path its at), and begin moving it to its destination along the connected paths, it uses angles to find the paths so in the final version of the script ill need to add many exceptions but thats for the future

    SCAR Code:
    program New;

    {.include paths.scar}
    {.include llist.scar}

    // Basic variables
    var charx,chary, destx, desty:integer;
        arrived: boolean;

    function TPt(x,y:integer):TPoint;
    begin
         result.x := x;
         result.y := y;
    end;

    // Basic math, used for path finding
    function AngleBetweenPoints(x1,y1,x2,y2:integer):extended;
    begin
         result := Arctan2(y2-y1,x2-x1) * 180 / Pi;
         if result < 0 then
            result := Abs(result) + 180;
    end;

    // Test function, dupes move to function from reflection
    function MoveTo(p:TPoint):boolean;
    begin
         if (charx = p.x) and (chary = p.y) then
         begin
            result := false;
            exit;
         end;
         charx := p.x;
         chary := p.y;
         writeln('Moved to :' + inttostr(p.x) + ',' + inttostr(p.y));
         result := true;
    end;

    // Required to create and link paths, might make the paths into an ini
    // file that will update itself automatically, but this is a test version
    procedure Initialize();
    begin
         CreatePath(path1());
         CreatePath(path2());
         CreatePath(path3());
         CreatePath(path4());
         CreatePath(path5());
         LinkPath(0,1);
         LinkPath(0,2);
         LinkPath(0,4);
         LinkPath(1,2);
         LinkPath(1,3);
         LinkPath(1,4);
         LinkPath(2,3);
         LinkPath(3,4);
    end;

    // Loops through all of the TPA's and checks which path has the closest average point
    function FindClosestPath():integer;
    var i:integer;
        length: extended;
    begin
         length := 5000;
         for i := 0 to high(paths) do
         begin
              if length > Distance(charx, chary, paths[i].avg.x, paths[i].avg.y) then
              begin
                 length := Distance(charx, chary, paths[i].avg.x, paths[i].avg.y);
                 result := i;
              end;
         end;
    end;

    // Like previous function, but this one finds the closest point in an array
    function FindClosestPointInTPA(arr:TPointArray):integer;
    var i,y:integer;
        z:extended;
    begin
         z := 5000;
         for i := 0 to High(arr) do
         begin
              if z > Distance(charx, chary, arr[i].x, arr[i].y) then
              begin
                 y := i;
                 z := Distance(charx, chary, arr[i].x, arr[i].y);
              end;
         end;
         result := y;
    end;

    // Will move through the TPA, checking if it's at the destination yet, or if it finished going through the TPA
    procedure AutoMoveThroughTPA(start,inc:integer; node:TPointArray);
    var i:integer;
    begin
         i := start - inc;
         repeat
               i := i + inc;
               if MoveTo(node[i]) = true then
                  wait(100);
               if (node[i].x = destx) and (node[i].y = desty) then
               begin
                    arrived := true;
                    exit;
               end;
         until (((i = 0) and (i <> start)) or ((i = high(node)) and (high(node) <> start)))
    end;

    // Will check if the path is accesable, because it might be only connected in the opposite side of a path
    function Pathable(path:integer):boolean;
    begin
         result := false;
         if Distance(charx, chary, paths[path].first.x, paths[path].first.y) < Distance(charx, chary, paths[path].last.x, paths[path].last.y)then
         begin
              if (charx = paths[path].first.x) and (chary = paths[path].first.y) then
                 result := true
         end;
         if Distance(charx, chary, paths[path].first.x, paths[path].first.y) > Distance(charx, chary, paths[path].last.x, paths[path].last.y)then
         begin
              if (charx = paths[path].last.x) and (chary = paths[path].last.y) then
                 result := true
         end;
    end;

    // Since we don't know how the character will go through the path (increasing or decreasing) we call this function
    procedure TakePath(path:integer);
    begin
         if Distance(charx, chary, paths[path].first.x, paths[path].first.y) < Distance(charx, chary, paths[path].last.x, paths[path].last.y) then
         begin
             AutoMoveThroughTPA(0, 1, paths[path].node)
         end else
         begin
             AutoMoveThroughTPA(high(paths[path].node), -1, paths[path].node)
         end;
    end;

    // Function that will find the farthest end of a path, takes path index
    function FarthestEndX(path:integer):integer;
    begin
         if Distance(charx, chary, paths[path].first.x, paths[path].first.y) > Distance(charx, chary, paths[path].last.x, paths[path].last.y) then
             result := paths[path].first.x
         else
             result := paths[path].last.x;
    end;

    // Function that will find the farthest end of a path, takes path index
    function FarthestEndY(path:integer):integer;
    begin
         if Distance(charx, chary, paths[path].first.x, paths[path].first.y) > Distance(charx, chary, paths[path].last.x, paths[path].last.y) then
             result := paths[path].first.y
         else
             result := paths[path].last.y;
    end;

    // Checks how far away are the angles
    function DegreeDiff(A, B:extended):extended;
    begin
      result:= Abs(A-B);
      if result>180.0 then
        result:= 180.0-(result-180.0);
    end;

    // Dick and balls of the script, it gets the shit done
    function move(x,y:integer):boolean;
    var p, n, curpath, lastpath:integer;
        c, cd: extended;
        node: TPointArray;
    begin
         c := 360;
         arrived := false;
         destx := x;
         desty := y;
         lastpath := -1;
         curpath := FindClosestPath;
         node := paths[curpath].node;
         p := FindClosestPointinTPA(paths[curpath].node);
         cd := AngleBetweenPoints(charx, chary, x, y);
         if  (p <> 0) and (p <> high(paths[curpath].node)) then
         begin
         if DegreeDiff(cd,AngleBetweenPoints(charx, chary, node[p+1].x,node[p+1].y)) < DegreeDiff(cd,AngleBetweenPoints(charx, chary, node[p-1].x, node[p-1].y)) then
         begin
             AutoMoveThroughTPA(p+1, 1, node);
         end else
         begin
             AutoMoveThroughTPA(p-1, -1, node);
         end;
         end;

         while (arrived = false) do
         begin
               cd := AngleBetweenPoints(charx, chary, x, y);
               for p := 0 to high(paths[curpath].connection) do
               begin
                    if (c > DegreeDiff(cd, AngleBetweenPoints(FarthestEndX(paths[paths[curpath].connection[p]].index), FarthestEndY(paths[paths[curpath].connection[p]].index), x, y))) and (p <> lastpath) and (Pathable(paths[paths[curpath].connection[p]].index) = true) then
                    begin
                       c := AngleBetweenPoints(FarthestEndX(paths[paths[curpath].connection[p]].index), FarthestEndY(paths[paths[curpath].connection[p]].index), x, y);
                       n := p;
                    end;
               end;
               c := 360;
               TakePath(paths[paths[curpath].connection[n]].index);
               lastpath := curpath;
               curpath := n;
         end;
    end;

    begin
         Initialize;
         ClearDebug;
         MoveTo(TPt(5,0));
         Move(0,7);
    end.

    SCAR Code:
    // llist.scar

    // Checks if the integer is in an integer array
    function IntinArr(int:integer; comp:array of integer):boolean;
    var i:integer;
    begin
         result := false;
         for i := 0 to High(comp) do
         begin
              if comp[i] = int then
              begin
                 result := true;
                 exit;
              end;
         end;
    end;

    // Custom variable called path
    type path = record
         node: TPointArray;
         avg, first,last: TPoint;
         index: integer;
         connection: array of integer;
    end;

    // Array of previous variable
    var paths:array of path;
        curindex: integer;

    // Calculates average point in the TPA
    function CalcAvginTPA(tpa:TPointArray):TPoint;
    var i:integer;
    begin
         for i := 0 to high(tpa) do
         begin
              result.x := result.x + tpa[i].x;
              result.y := result.y + tpa[i].y;
         end;
         result.x := Round(result.x / getarraylength(tpa));
         result.y := Round(result.y / getarraylength(tpa));
    end;

    // Creates a path
    function CreatePath(node:TPointArray):integer;
    begin
        // Increases the array length
        SetArrayLength(paths, curindex+1);
        paths[curindex].node := node;
        // Sets the index to curindex variable
        paths[curindex].index := curindex;
        paths[curindex].avg := CalcAvginTPA(node);
        paths[curindex].first := node[0];
        paths[curindex].last := node[high(node)];
        result := curindex;
        // Increases curent index value
        curindex := curindex + 1;
    end;

    // Links two paths
    procedure LinkPath(i,y:integer);
    begin
         // If the two paths are already linked, it will display so and exit before changing anything
         if IntinArr(i, paths[y].connection) = true then
         begin
            writeln('Following paths are already linked: ' + inttostr(i) + ':' + inttostr(y) + ' so fix your script because you''re losing efficiency!');
            exit;
         end;
         // Increases connection array length, and places the values in each others connection arrays
         SetArrayLength(paths[i].connection, GetArrayLength(paths[i].connection)+1);
         SetArrayLength(paths[y].connection, GetArrayLength(paths[y].connection)+1);
         paths[i].connection[High(paths[i].connection)] := paths[y].index;
         paths[y].connection[High(paths[y].connection)] := paths[i].index;
    end;

    SCAR Code:
    // paths.scar

    function path1: TPointArray;
    begin
      SetLength(Result, 8);

      Result[0] := Point(0, 0);
      Result[1] := Point(0, 1);
      Result[2] := Point(0, 2);
      Result[3] := Point(0, 3);
      Result[4] := Point(0, 4);
      Result[5] := Point(0, 5);
      Result[6] := Point(0, 6);
      Result[7] := Point(0, 7);
    end;

    function path2: TPointArray;
    begin
      SetLength(Result, 8);

      Result[0] := Point(0, 0);
      Result[1] := Point(1, 1);
      Result[2] := Point(2, 2);
      Result[3] := Point(3, 3);
      Result[4] := Point(4, 4);
      Result[5] := Point(5, 5);
      Result[6] := Point(6, 6);
      Result[7] := Point(7, 7);
    end;

    function path3: TPointArray;
    begin
      SetLength(Result, 8);

      Result[0] := Point(0, 0);
      Result[1] := Point(1, 0);
      Result[2] := Point(2, 0);
      Result[3] := Point(3, 0);
      Result[4] := Point(4, 0);
      Result[5] := Point(5, 0);
      Result[6] := Point(6, 0);
      Result[7] := Point(7, 0);
    end;

    function path4: TPointArray;
    begin
      SetLength(Result, 8);

      Result[0] := Point(7, 0);
      Result[1] := Point(7, 1);
      Result[2] := Point(7, 2);
      Result[3] := Point(7, 3);
      Result[4] := Point(7, 4);
      Result[5] := Point(7, 5);
      Result[6] := Point(7, 6);
      Result[7] := Point(7, 7);
    end;

    function path5: TPointArray;
    begin
      SetLength(Result, 8);

      Result[0] := Point(0, 7);
      Result[1] := Point(1, 7);
      Result[2] := Point(2, 7);
      Result[3] := Point(3, 7);
      Result[4] := Point(4, 7);
      Result[5] := Point(5, 7);
      Result[6] := Point(6, 7);
      Result[7] := Point(7, 7);
    end;

  2. #2
    Join Date
    Feb 2007
    Posts
    3,616
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Didn't see any areas to improve on execution really. If you want to cut down on code, then just look at some of your unnecessary begins and ends. You have a few begins/ends after if/thens that are only doing 1 statement (unnecessary). Also, making the result = true or false depending on a condition can make for cleaner code in many cases too. Example:

    SCAR Code:
    function Pathable(path:integer):boolean;
    begin
         result := false;
         if Distance(charx, chary, paths[path].first.x, paths[path].first.y) < Distance(charx, chary, paths[path].last.x, paths[path].last.y)then
         begin
              if (charx = paths[path].first.x) and (chary = paths[path].first.y) then
                 result := true
         end;
         if Distance(charx, chary, paths[path].first.x, paths[path].first.y) > Distance(charx, chary, paths[path].last.x, paths[path].last.y)then
         begin
              if (charx = paths[path].last.x) and (chary = paths[path].last.y) then
                 result := true
         end;
    end;

    Could be changed to:

    SCAR Code:
    function Pathable(path:integer):boolean;
    begin
      Result := Distance(charx, chary, paths[path].first.x, paths[path].first.y) < Distance(charx, chary, paths[path].last.x, paths[path].last.y)and
                        (charx = paths[path].first.x) and (chary = paths[path].first.y);
      if(not(Result))then
      Result := Distance(charx, chary, paths[path].first.x, paths[path].first.y) > Distance(charx, chary, paths[path].last.x, paths[path].last.y)and
                        (charx = paths[path].last.x) and (chary = paths[path].last.y);
    end;

    Much better looking to me.

    Just try to cut down on begins/ends wherever possible for shorter code.

    Good job though Looks good.



    Edit:

    SCAR Code:
    function IntinArr(int:integer; comp:array of integer):boolean;
    var i:integer;
    begin
         result := false;
         for i := 0 to High(comp) do
         begin
              if comp[i] = int then
              begin
                 result := true;
                 exit;
              end;
         end;
    end;

    Change to:

    SCAR Code:
    function IntinArr(int:integer; comp:array of integer):boolean;
    var i:integer;
    begin
         for i := 0 to High(comp) do
         begin
              Result := comp[i];
              if(Result)then break;
         end;
    end;

    Just little things like that.

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

    Default

    Your path funtions can easily be written in one function, and can be much shorter. Something like this:
    SCAR Code:
    function WalkPaths(Path: Integer): TPointArray;
    begin
      case Path of
        0: Result := [Point(0, 0), Point(0, 1), Point(0, 2), Point(0, 3),
                            Point(0, 4), Point(0, 5), Point(0, 6), Point(0, 7)];
                     
        1: Result := [Point(0, 0), Point(1, 1), Point(2, 2), Point(3, 3),
                            Point(4, 4), Point(5, 5), Point(6, 6), Point(7, 7)];
                     
        2: Result := // ... Get the idea? =P
      end;
    end;
    Looks much better, and is much more efficient. Also, by writing it like that, you don't have to set the length. It automatically sets it for you.

    @JAD: LOL "InIntArr", look familiar?
    Last edited by Coh3n; 09-13-2009 at 01:29 AM.

  4. #4
    Join Date
    Oct 2007
    Location
    http://ushort.us/oqmd65
    Posts
    2,605
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    http://www.villavu.com/forum/showthread.php?t=48638.

    You had a few pointers on basically the same script about shortening code, and things that make it cleaner, Hell, Coh3n might not remember, but he pretty much made that same post in that thread.

    The only ass-face response you can give me is that you didn't ask for tips back then.
    I do visit every 2-6 months

  5. #5
    Join Date
    Feb 2009
    Location
    AZ, USA
    Posts
    460
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by JAD View Post

    SCAR Code:
    function IntinArr(int:integer; comp:array of integer):boolean;
    var i:integer;
    begin
         for i := 0 to High(comp) do
         begin
              Result := comp[i];
              if(Result)then break;
         end;
    end;
    Shouldn't that be

    SCAR Code:
    function IntinArr(int:integer; comp:array of integer):boolean;
    var i:integer;
    begin
         for i := 0 to High(comp) do
         begin
              Result := comp[i] = int;
              if(Result)then break;
         end;
    end;
    Is your account in an old-school random? Help SRL-OSR solve randoms!

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

    Default

    Quote Originally Posted by ZaSz View Post
    http://www.villavu.com/forum/showthread.php?t=48638.

    You had a few pointers on basically the same script about shortening code, and things that make it cleaner, Hell, Coh3n might not remember, but he pretty much made that same post in that thread.

    The only ass-face response you can give me is that you didn't ask for tips back then.
    Wow, didn't remember that AT ALL, and it was only a month ago. Still, it made me lol.

  7. #7
    Join Date
    Feb 2008
    Posts
    517
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    My problem with the code was completely different at the time, which I solved by myself and a few debugging tips from method.

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

    Default

    Quote Originally Posted by Feroc1ty View Post
    My problem with the code was completely different at the time, which I solved by myself and a few debugging tips from method.
    I don't think that's what he meant. He just meant that the same things were posted on your other thread, and you apparently didn't take that into consideration.

  9. #9
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default

    lol, i havnt used reflection for ever, especially since its down

    but yeah, you can make your paths into the shortend array, and make them all into a case(i'll show in 1sec, im at the wrong cpu)

    edit: cohen beat me to it although it should be a TPointArray, not TIntegerArray
    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

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

    Default

    Quote Originally Posted by Awkwardsaw View Post
    lol, i havnt used reflection for ever, especially since its down

    but yeah, you can make your paths into the shortend array, and make them all into a case(i'll show in 1sec, im at the wrong cpu)

    edit: cohen beat me to it although it should be a TPointArray, not TIntegerArray
    Gah, you're right, lol, I'll change it.

  11. #11
    Join Date
    Feb 2008
    Posts
    517
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    It's an include, why would I want to slow down the execution time to gain a few extra lines of space on an include that isn't even gonna be looked at. Even if I did, it would be pointless as after I transfer the code into reflection, most of the paths will be in an ini file, and I don't believe I'll be the one making the paths; I'll simply upload the dick of the script and let people create their own paths and share them, because I don't want to have the commitment of updating all the paths, I'll simply make it simple to update the paths to whatever you want, because versatility and uniqueness of scripts is what makes it good and also makes it harder for jagex to catch you.

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

    Default

    Quote Originally Posted by Feroc1ty View Post
    It's an include, why would I want to slow down the execution time to gain a few extra lines of space on an include that isn't even gonna be looked at. Even if I did, it would be pointless as after I transfer the code into reflection, most of the paths will be in an ini file, and I don't believe I'll be the one making the paths; I'll simply upload the dick of the script and let people create their own paths and share them, because I don't want to have the commitment of updating all the paths, I'll simply make it simple to update the paths to whatever you want, because versatility and uniqueness of scripts is what makes it good and also makes it harder for jagex to catch you.
    Whether you write is as an array or not isn't going to increase/decrease you changes of getting caught. Also, it's more efficient writing it the way I told you, but it's your script, it's up to you. I was just doing what you asked me - letting you know how you can improve your script.

  13. #13
    Join Date
    Feb 2008
    Posts
    517
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Quote Originally Posted by Coh3n View Post
    Whether you write is as an array or not isn't going to increase/decrease you changes of getting caught. Also, it's more efficient writing it the way I told you, but it's your script, it's up to you. I was just doing what you asked me - letting you know how you can improve your script.
    Letting people create their own paths will decrease the chance of getting caught.

    Adding more parameters to a function call slows down the execution speed, it's common programing knowledge.

  14. #14
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default

    Quote Originally Posted by Feroc1ty View Post
    Letting people create their own paths will decrease the chance of getting caught.

    Adding more parameters to a function call slows down the execution speed, it's common programing knowledge.
    the first part is true, yes, but having execive, un-nessesary lines will make it slower than having 1-2 parameters
    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

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

    Default

    Quote Originally Posted by Feroc1ty View Post
    Letting people create their own paths will decrease the chance of getting caught.
    I most likely won't matter because no matter what path you create, SCAR will always click the minimap with a randomness of that point. If each player makes their own path it will generally be in the same area, and will click in the same area regardless. But again, it's up to you.

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
  •