Results 1 to 5 of 5

Thread: Make this function faster!

  1. #1
    Join Date
    May 2007
    Location
    in a pineapple under the sea
    Posts
    1,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Make this function faster!

    These are some sweet functions ive made and i want to speed it up.

    First one to do it gets a cookie

    Also tell me how i decrease the radius of a point on the minimap. (center of the circle being 247,85)

    SCAR Code:
    function MovePointCloser(Point: TPoint; Number :integer):TPoint;
    var
      ax,ay,ax2,ay2,bx,by:integer;
      SqrRadius,Radius,CosAngle,TempAngle,SinAngle,Angle: extended;
    begin
      ax:= Point.X;
      ay:= Point.Y;
      ax2:= ax*ax;
      ay2:= ay*ay;
      SqrRadius:= ax2 + ay2;
      radius:= sqrt(SqrRadius);
      radius:= radius - number;
      TempAngle:= ax/radius;
      Angle:= radius*Cos(CosAngle);
      CosAngle:= cos(Angle);
      SinAngle:= sin(Angle);
      bx:= round(radius*CosAngle);
      by:= round(radius*SinAngle);
      result:= inttopoint(bx,by);
    end;

    SCAR Code:
    function CaveWalk(dir:string; modx,mody,FlagNum:integer):boolean;
    var
      i,Lengthen,NewOne,GreatestDist,Dist: Integer;
      CenterPoint,ClickPoint: TPoint;
      NTPA,STPA,ETPA,WTPA,NeTPA,NwTPA,SeTPA,SwTPA,BlackMM,WalkTPA: TPointArray;
      Mark: LongInt;
    begin
      MarkTime(Mark);
      CenterPoint:= inttopoint(627,85);
      FindColorsTolerance(BlackMM,0,MMX1,MMY1,MMX2,MMY2,0);
      for i:= 0 to length(BlackMM)-1 do
      begin
        if BlackMM[i].X < CenterPoint.X then
        begin
          if BlackMM[i].Y > CenterPoint.Y then
          begin
            lengthen:= length(SwTPA)+1;
            SetArrayLength(SwTPA,lengthen);
            newone:= Length(SwTPA)-1;
            SwTPA[newone]:= BlackMM[i];
          end;
          if BlackMM[i].Y < CenterPoint.Y then
          begin
            lengthen:= length(NwTPA)+1;
            SetArrayLength(NwTPA,lengthen);
            newone:= Length(NwTPA)-1;
            NwTPA[newone]:= BlackMM[i];
          end;
        end;
        if BlackMM[i].X > CenterPoint.X then
        begin
          if BlackMM[i].Y < CenterPoint.Y then
          begin
            lengthen:= length(NeTPA)+1;
            SetArrayLength(NeTPA,lengthen);
            newone:= Length(NeTPA)-1;
            NeTPA[newone]:= BlackMM[i];
          end;
          if BlackMM[i].Y > CenterPoint.Y then
          begin
            lengthen:= length(SeTPA)+1;
            SetArrayLength(SeTPA,lengthen);
            newone:= Length(SeTPA)-1;
            SeTPA[newone]:= BlackMM[i];
          end;
        end;
        if BlackMM[i].X = CenterPoint.X then
        begin
          if BlackMM[i].Y < CenterPoint.Y then
          begin
            lengthen:= length(NTPA)+1;
            SetArrayLength(NTPA,lengthen);
            newone:= Length(NTPA)-1;
            NTPA[newone]:= BlackMM[i];
          end;
          if BlackMM[i].Y > CenterPoint.Y then
          begin
            lengthen:= length(STPA)+1;
            SetArrayLength(STPA,lengthen);
            newone:= Length(STPA)-1;
            STPA[newone]:= BlackMM[i];
          end;
        end;
        if BlackMM[i].Y = CenterPoint.Y then
        begin
          if BlackMM[i].X < CenterPoint.X then
          begin
            lengthen:= length(WTPA)+1;
            SetArrayLength(WTPA,lengthen);
            newone:= Length(WTPA)-1;
            WTPA[newone]:= BlackMM[i];
          end;
          if BlackMM[i].X > CenterPoint.X then
          begin
            lengthen:= length(ETPA)+1;
            SetArrayLength(ETPA,lengthen);
            newone:= Length(ETPA)-1;
            ETPA[newone]:= BlackMM[i];
          end;
        end;
      end;
      case lowercase(dir) of
        'nw' : WalkTPA:= NwTPA;
        'n' : WalkTPA:= NTPA;
        'ne' : WalkTPA:= NeTPA;
        'e' : WalkTPA:= ETPA;
        'se' : WalkTPA:= SeTPA;
        's' : WalkTPA:= STPA;
        'sw' : WalkTPA:= SwTPA;
        'w' : WalkTPA:= WTPA;
      end;
      for i:= 0 to length(WalkTPA)-1 do
      begin
        Dist:= Distance(WalkTPA[i].X,WalkTPA[i].Y,CenterPoint.X,CenterPoint.Y);
        if Dist > GreatestDist then
        begin
          GreatestDist:= Dist;
          ClickPoint:= WalkTPA[i];
        end;
      end;
      repeat
        ClickPoint:= MovePointCloser(ClickPoint,5);
      until(Distance(ClickPoint.X,ClickPoint.Y,CenterPoint.X,CenterPoint.Y) < 68);
      if not ClickPoint.X = 0 then
      begin
        result:= true;
        Mouse(ClickPoint.X,ClickPoint.Y,modx,mody,true);
        FFlag(FlagNum);
      end;
      writeln('Took '+inttostr(TimeFromMark(Mark))+' ms');
    end;
    [SIZE="4"][CENTER][URL="http://www.youtube.com/watch?v=5YsGJz3j4os"]LOL u mad bro?[/URL][/CENTER][/SIZE]

  2. #2
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,692
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Quote Originally Posted by Timothegreat View Post
    These are some sweet functions ive made and i want to speed it up.

    First one to do it gets a cookie

    Also tell me how i decrease the radius of a point on the minimap. (center of the circle being 247,85)

    SCAR Code:
    function MovePointCloser(Point: TPoint; Number :integer):TPoint;
    var
      ax,ay,ax2,ay2,bx,by:integer;
      SqrRadius,Radius,CosAngle,TempAngle,SinAngle,Angle: extended;
    begin
      ax:= Point.X;
      ay:= Point.Y;
      ax2:= ax*ax;
      ay2:= ay*ay;
      SqrRadius:= ax2 + ay2;
      radius:= sqrt(SqrRadius);
      radius:= radius - number;
      TempAngle:= ax/radius;
      Angle:= radius*Cos(CosAngle);
      CosAngle:= cos(Angle);
      SinAngle:= sin(Angle);
      bx:= round(radius*CosAngle);
      by:= round(radius*SinAngle);
      result:= inttopoint(bx,by);
    end;
    I think you could definately speed this up.
    This is not how I would make this function, however.
    I'd give it a centerpoint, too? That way you actually 'move' it closer to some origin.
    Tan should help you get the degree quickly.

    From Wizzyplugin:

    SCAR Code:
    const
      i180Pi = 57.29577951;
      D := (ArcTan2(Points[I].Y - My, Points[I].X - Mx) * i180Pi) + 90; //Returns degrees.
     // You don't need the next two lines if you will only put the degree back in sin and cos to get the point
      if D < 0.0 then
        D := D + 360.0;

    You should probably remove the +90, that is only to get 'north' at 0 degrees, to get it work like the old radialwalk did.

    Source: Wizzyplugin.dpr (FilterPointsPie)
    For a more simple ArcTan2 function, check out rs_GetCompassAngleDegrees (Mapwalk.scar, overwritten SCAR function)



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  3. #3
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    Wizzup - I use -90 which works, you use +90 which doesn't work, for me atleast

    You might want to use the Uberfunction I made for JuKKa -

    SCAR Code:
    Procedure GetCloser(Var Point: TPoint; Origin: TPoint; HowMuchCloser: Integer);

    Var
       Angle, Dist: Integer;
    Begin
      Angle := Round(Degrees(ArcTan2(Origin.y - Point.y, Origin.x - Point.x))) - 90;
      If Angle < 0 Then
        Angle := Angle + 360;
      Dist := Distance(Point.x, Point.y, Origin.x, Origin.y) - HowMuchCloser;
      Point.x := Origin.x + Round(Dist * Cos(Radians(Angle - 90)));
      Point.y := Origin.y + Round(Dist * Sin(Radians(Angle - 90)));
    End;

    And use the search button

  4. #4
    Join Date
    May 2007
    Location
    in a pineapple under the sea
    Posts
    1,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Quote Originally Posted by n3ss3s View Post
    Wizzup - I use -90 which works, you use +90 which doesn't work, for me atleast

    You might want to use the Uberfunction I made for JuKKa -

    SCAR Code:
    Procedure GetCloser(Var Point: TPoint; Origin: TPoint; HowMuchCloser: Integer);

    Var
       Angle, Dist: Integer;
    Begin
      Angle := Round(Degrees(ArcTan2(Origin.y - Point.y, Origin.x - Point.x))) - 90;
      If Angle < 0 Then
        Angle := Angle + 360;
      Dist := Distance(Point.x, Point.y, Origin.x, Origin.y) - HowMuchCloser;
      Point.x := Origin.x + Round(Dist * Cos(Radians(Angle - 90)));
      Point.y := Origin.y + Round(Dist * Sin(Radians(Angle - 90)));
    End;

    And use the search button
    i used search button 5 times, searched move point closer, and nothing came up. Thank you though, fixed it.
    [SIZE="4"][CENTER][URL="http://www.youtube.com/watch?v=5YsGJz3j4os"]LOL u mad bro?[/URL][/CENTER][/SIZE]

  5. #5
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,692
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Quote Originally Posted by n3ss3s View Post
    Wizzup - I use -90 which works, you use +90 which doesn't work, for me atleast

    You might want to use the Uberfunction I made for JuKKa -

    SCAR Code:
    Procedure GetCloser(Var Point: TPoint; Origin: TPoint; HowMuchCloser: Integer);

    Var
       Angle, Dist: Integer;
    Begin
      Angle := Round(Degrees(ArcTan2(Origin.y - Point.y, Origin.x - Point.x))) - 90;
      If Angle < 0 Then
        Angle := Angle + 360;
      Dist := Distance(Point.x, Point.y, Origin.x, Origin.y) - HowMuchCloser;
      Point.x := Origin.x + Round(Dist * Cos(Radians(Angle - 90)));
      Point.y := Origin.y + Round(Dist * Sin(Radians(Angle - 90)));
    End;

    And use the search button
    From my previous post:

    SCAR Code:
    // You don't need the next two lines if you will only put the degree back in sin and cos to get the point
      if D < 0.0 then
        D := D + 360.0;



    Sin (-360) = Sin(-180) = Sin(0) = Sin(180) = Sin(360) And so on...



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 12
    Last Post: 10-13-2008, 02:57 AM
  2. Is there anyway to make this function faster?
    By Claymore in forum OSR Help
    Replies: 10
    Last Post: 07-22-2008, 06:09 PM
  3. How could I make this faster?
    By Infantry001 in forum OSR Help
    Replies: 0
    Last Post: 11-23-2007, 02:35 AM

Posting Permissions

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