[Tut]Fun with TPoints, TBoxes, and More!

Discussion in 'Archives' started by Infantry001, May 26, 2007.

[Tut]Fun with TPoints, TBoxes, and More!
  1. Unread #1 - May 26, 2007 at 12:10 AM
  2. Infantry001
    Joined:
    Mar 19, 2006
    Posts:
    90
    Referrals:
    0
    Sythe Gold:
    0

    Infantry001 Member

    [Tut]Fun with TPoints, TBoxes, and More!

    Fun with TPoints, TBoxes, and More!

    So, im guessing you came here knowing nothing about tpoints? Well, today im here to teach you about TPoints, TPointArrays, TBoxes, and TBoxArrays. Make sure you understand the basics of SCAR before attempting this tutorial.

    First off, open up scar.

    Set these global variables:

    Code:
    var
    i : integer;
    MyTpoint : tpoint;
    MyTpointArray : TPointArray;
    MyTBox : tbox;
    MyTBoxArray : TBoxArray;
    And include SRL (you need this for TBox).

    Alright, now let me describe each variable type.

    TPoint - Used to store an x and y coordinate within one variable. Lets say you are using MyTpoint as your tpoint. The "x" coordinate would be MyTpoint.x, and the "y" coordinate would be MyTpoint.y.

    TPointArray - Store multiple TPoints within an array. Using the first TPoint would be, say, MyTPointArray[0].x and MyTPointArray[0].y. Don't forget to set the array length!

    TBox - Pretty much the same as TPoint, except uses x1,y1,x2, and y2 as arguments. x1,y1 would be the top left corner of the box made, and x2,y2 would be bottom right corner.

    TBoxArray - Stores multiple TBoxes in an array.

    To use these, lets start off with a midpoint function:

    TPoint

    Code:
    //Fx,fy - first coords
    //sx,sy - second coords
    function Midpoint(fx,fy,sx,sy : integer) : tpoint;
    begin
      result.x := Round((fx + sx) / 2);
      result.y := Round((fy + sy) / 2);
    end;
    Now, try this in your main loop:

    Code:
    begin
      SetupSRL;
      MyTPoint := MidPoint(0,0,500,500);
      MMouse(MyTPoint.x,MyTPoint.y,0,0);
    end.
    As you can see, midpoint will result in a TPoint, and MyTPoint is set as the MidPoint of 0,0 and 500,500. Run the script and watch the mouse move to 250,250.

    Next, lets look at a TPoint Array!

    TPointArray

    Now, you should know a bit about arrays, but if not, let me take a small detour...

    Arrays are a way of making one variable have many different values. Each value of the array is shown by the
    number. The format is this: Array[0]. Say you want array to hold 3 different colors. First, you would need to
    set the array length (setarraylength(Array,3)). This would result in an array of 0-2, not 1-3. Then, do this:
    Array[0] := 0;
    Array[1] := 65336;
    Array[2] := 1667438;
    Then, you could find the color Array[0] by FindColor(x,y,Array[0],MSX1,MSY1,MSX2,MSY2). This has been a quick
    detour on arrays.


    Within the same script, add this procedure:

    Code:
    procedure SetMidpointArray;
    var i : integer;
    begin
      setarraylength(MyTPointArray,4);
      for i := 0 to 3 do
        MyTPointArray[i] := MidPoint(0,0,i * 100,i * 100);
    end;
    This will set each TPoint to a midpoint using a for..to..do statement. If you do not understand, check out some
    other tutorials!

    Now, add to your main loop:
    Code:
    SetMidpointArray;
      for i := 0 to 3 do
        MMouse(MyTPointArray[i].x,MyTPointArray[i].y,0,0);
    This will set the midpoint array, and then move the mouse to each midpoint!

    TBox

    Time to move onto TBoxes!

    So, TBoxes are pretty much a set of 2 TPoints, with the parameters being x1,y1,x2, and y2. They are useful for many things, but TBoxArrays are mor commonly used.

    Lets say you want to look for a color around a certain point. First, you would use FindColor, right? From there, what do you do? That's right! Make a TBox.

    Put this into scar:

    Code:
    function FindColorInBox(x,y,color : integer) : boolean;
    begin
      MyTBox.x1 := x - 30;
      MyTBox.y1 := y - 30;
      MyTBox.x2 := x + 30;
      MyTBox.y2 := y + 30;
      if FindColor(x,y,color,MyTBox.x1,MyTBox.y1,MyTBox.x2,MyTBox.y2) then
        result := true;
    end;
    First off, this creates a box around the x,y coordinate that is 30 pixels by 30 pixels. Then, if it finds "color" inside that box, it will result true. Used in conjunction with FindColorSkipBox, you can do some cool things.

    Code:
    function FindDifferentColor : boolean;
    begin
      if FindColor(x,y,9724949,MSX1,MSY1,MSX2,MSY2) then
        if FindColorInBox(x,y,65366) then
          if FindColorSkipBox(x,y,65366,MSX1,MSY1,MSX2,MSY2,MyTBox) then
            result := true;
    end;
    Combining this procedure with the last one, check out what it does. If it finds the color 9724949, then it looks in a box around that for the color 65366. If it is found, then FindColorSkipBox will look for the color 65366
    again, but without looking into MyTBox. Using this concept, you could determine whether an object is what you are looking for, or just something with a similar color.

    For example, maybe a script wants to mine a rock but someone has an ores. It could use FindDIfferentColor, except it substitutes the first FindColor with the rock color, the second with the outer
    parts of the ore, and the last one with the rock color. If it finds the rock color (which could either be the actual rock, or an ore) it looks in a TBox around the color to see if it finds the color that makes it an
    ore. If it does, it will look again for the rock color, but skipping the box it found the dropped ore in! Useful, huh?

    And... this leads me into TBoxArrays!

    TBoxArrays

    If you haven't read the last section, READ IT! Only then will you understand this part.

    Lets say there is more than one ore dropped, and it is in many different locations. Now what? We can make the script skip every location the dropped ores are in.

    We are basically using the same function as before, except looped and modified a bit.

    Code:
    function FindAllBoxes : boolean;
    var
      a : integer;
    begin
      SetArrayLength(MyTBoxArray,0)
      repeat
        if FindColorSkipBoxArray(x,y,RockColor,MSX1,MSY1,MSX2,MSY2,MyTBoxArray) then
        begin
          if FindColorInBox(x,y,OreColor) then
          begin
            SetArrayLength(MyTBoxArray,a + 1)
            MyTBoxArray[a].x1 := MyTBox.x1;
            MyTBoxArray[a].y1 := MyTBox.y1;
            MyTBoxArray[a].x2 := MyTBox.x2;
            MyTBoxArray[a].y2 := MyTBox.y2;
            a := a + 1;
          end else
            break;
        end;
      until(false)
      if FindColorSkipBoxArray(x,y,RockColor,MSX1,MSY1,MSX2,MSY2,MyTBoxArray) then
      begin
        MMouse(x,y,3,3);
        Writeln('This should be our rock!');
      end;
    end;
    So...let's go over this step by step.

    First of all, we must Set our arraylength so we dont get a runtime error.
    Second, We have FindColorSkipBoxArray to look for a color everywhere in the mainscreen except the TBoxes we told it to skip. We need TBoxArray to make it look for other instances of the color outside the ore. If we just put
    FindColorSkipBox, We would constantly find the same 2 boxes.
    Third, the FindColor is there to see if the orecolor if found. If it isnt, then we know our boxes are all made, therefore, we break from the loop.
    Fourth, we find the middle of the function. You must add 1 to the arraylength if we are going to add a box to skip. Next, we declare the points of the tboxarray as the points we found our color inside of. Then, we add 1 so
    the array increases next time we find the ore color. (and normally, that repeat loop would have a failsafe, but for demonstration purposes, i wont add one.
    Finally, We look at the whole screen, skipping the boxes where the orecolor was found. We move our mouse there, and TADA! Our rock should be right under!

    This is just one method of applying a TBoxArray. You can use them for looking for items in the inventory, in the bank, and on the floor. There are tons of uses for TBoxArrays!
     
  3. Unread #2 - May 26, 2007 at 5:03 AM
  4. Gofez0r
    Joined:
    Jan 21, 2007
    Posts:
    1,820
    Referrals:
    1
    Sythe Gold:
    0

    Gofez0r Guru

    [Tut]Fun with TPoints, TBoxes, and More!

    Nice tut! This was the only thing i didnt understand well at SCAR scripting :)
     
  5. Unread #3 - May 26, 2007 at 10:05 AM
  6. WhoCares357
    Joined:
    Jan 21, 2007
    Posts:
    608
    Referrals:
    1
    Sythe Gold:
    0

    WhoCares357 Forum Addict

    [Tut]Fun with TPoints, TBoxes, and More!

    Very good tutorial. Easy to understand and well written. I too could not grasp TPoints. You have made everything very clear. Thanks.
     
  7. Unread #4 - May 27, 2007 at 2:03 PM
  8. Jukka
    Joined:
    Nov 23, 2005
    Posts:
    244
    Referrals:
    0
    Sythe Gold:
    0

    Jukka Active Member

    [Tut]Fun with TPoints, TBoxes, and More!

    looks good, u gave a little help there while creating a function for one of to-be future released scripts.
     
< Pin | ~Buying Pins~ >

Users viewing this thread
1 guest


 
 
Adblock breaks this site