Results 1 to 2 of 2

Thread: [Easy] Debugging FindColorsTolerance

  1. #1
    Join Date
    Nov 2011
    Posts
    1,532
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default [Easy] Debugging FindColorsTolerance

    Seeing an increased number of people now using FindColorsTolerance to implement their object finders (and also an increased number of people complaining colors are not found, esp. with my scripts ), I think it would be nice to start a tutorial to show scripters some simple techniques you can speed up debugging using SRL includes. Script users should also find this tutorial useful in case the script does not find the colors - you should be able to update them by yourself.

    We'll be looking at two includes. I won't be going through all the functions - an interested scripter can always explore new possibilities. I'll just show the ones I use often.

    The includes and use of directives
    Simba Code:
    {$I SRL/SRL/misc/debug.simba}
    {$I SRL/SRL/misc/paintsmart.simba}

    These are two useful includes that come with the SRL providing TPA/ATPA debugging capabilities.

    Before we go on, I would like to show the way I use these debug includes. I'll explain in due course.
    Simba Code:
    {$DEFINE DEBUG_DRAW} //uncomment if you need debug info

    {$IFDEF DEBUG_DRAW}
    {$I SRL/SRL/misc/debug.simba}
    {$I SRL/SRL/misc/paintsmart.simba}
    {$ENDIF}

    What I did here is to set up a define called DEBUG_DRAW which can be turned on/off by commenting it. Then I went on further to include the SRL includes I'll be using. Note I used the IFDEF directives between these includes. That means if I did not define DEBUG_DRAW, then these lines will not be interpreted as well.

    Using IFDEF directives is purely personal, and you can just go on and include everything you want as it doesn't really matter. Or you can declare a boolean constant to turn debug on/off. Most people do that. I like directives however because they are very visible and they must be constructed in block pairs, so I can easily tell where I'm trying to debug my code.

    The functions

    Now I'll explain why we need these two includes. They actually do similar things. Functions on paintsmart.simba however, will paint the debug info onto your SMART client, whereas debug.simba functions will output a separate bitmap showing the info.

    Depending on your style you might want to stick to one of them, but for completeness I usually just use the both of them.

    Now onto the functions. If you need TPA/ATPA debug info, these you will use a lot.
    Simba Code:
    procedure SMART_ClearCanvas;
    procedure SMART_DebugATPA(Clear: Boolean; ATPA: T2DPointArray);
    procedure SMART_DebugTPA(Clear: Boolean; TPA: TPointArray);
    procedure SMART_DrawBox(Box: TBox);

    SMART_ClearCanvas is for clear everything you painted onto SMART. Useful if you don't want your screen to be full of dots and boxes.

    The other three functions are clear by their names, you put the necessary arguments and run it, you will get the boxes on the screen. The clear parameter is a boolean asking whether you want to clear the canvas before printing again, usually I put true because I don't want the screen to have too many boxes.

    Another Good thing about paintsmart.simba vs debug.simba is, the TPA/ATPA debugs on paintsmart will print a cross on the MiddleTPA point of that TPA. Very useful if you want to see where your function is going to click on (if you use MiddleTPA).
    Simba Code:
    procedure SMART_ClearCanvas;
    procedure SMART_DebugATPA(Clear: Boolean; ATPA: T2DPointArray);
    procedure SMART_DebugTPA(Clear: Boolean; TPA: TPointArray);
    procedure SMART_DrawBox(Box: TBox);

    Debug.simba on the other hand has only a handful of functions. But I like them nevertheless because they're just as powerful as paintsmart.simba. You'll most probably need these functions.

    Simba Code:
    // * procedure TakeScreenshot(Path: String);                       // by Nava2
    // * function DebugTPA(Points: TPointArray; BmpName: String): Boolean; // by Wizzup?
    // * function DebugATPA(Points: TPointArray; BmpName: String): Boolean; // by Wizzup?
    // * procedure DebugATPABounds(aPoints: Array Of TPointArray);     // by Wizzup?, lordsaturn, & caused

    TakeScreenshot is self-explanatory, but you might be wondering why you need it. I used it back then when I had to debug my TPA walking and I couldn't babysit it. So I just made the script take screenshot everytime it's supposed to finish walking and come back to see where it's walked to. Neat.

    The difference of debug.simba functions here is that, they can put the results into a bitmap if you specify a nonempty string in the BmpName argument. If you put an empty string, you'll get a popup when the function is called, showing a debugImage of what Simba is supposed to see with that TPA.

    Finally, debugATPABounds in my opinion is the most useful of all, for debugging and education purposes. It shows the boxes of the ATPA you've submitted, and this you can see all the *objects* FindColorsTolerance has found.

    Using these functions to get color debug info

    This is where the the directives come in handy. To illustrate with,
    Simba Code:
    tcts:=GetToleranceSpeed;
      SetColorToleranceSpeed(2);
      SetColorSpeed2Modifiers(0.2, 0.2);
      FindColorsTolerance(tpa, 4017231, MSX1, MSY1, MSX2, MSY2, 15);
      SetColorToleranceSpeed(tcts);
      if length(tpa) < 1 then Exit;
      atpa:= TPAtoATPAEx(tpa, 20, 20);
      if length(atpa) < 1 then Exit;
      setLength(tpa2, length(atpa));
      for i:=0 to high(tpa2) do
        tpa2[i]:= MiddleTPA(atpa[i]);
      SortTPAFrom(tpa2, Point(MSCX, MSCY)); //this does the trick
      {$IFDEF DEBUG_DRAW}
    //  SortATPAFrom(atpa, Point(MSCX, MSCY));
      debugATPABounds(atpa);
      {$ENDIF}

    That is part of a snippet that I made to find the edgeville bank. You'll see very clearly where I'm debugging the color info using debugATPABounds due to the IFDEF blocks. Here it is marked blue, but on simba it is bright red, even better.

    Not only it's good for your eyes, but your users can also turn this on/off easily and have visual debug info before having to go to ACA to see whether the colors are on the screen.

    A common pitfall

    A very common pitfall that I get into from time to time is by switching CTS in my scripts. For objects, I like CTS2, but for minimap walking, I usually use CTS1. Now if you don't switch correctly between the two, you'll end up finding nothing and your script will behave strangely.
    Simba Code:
    tcts:=GetToleranceSpeed;
      SetColorToleranceSpeed(2);
      SetColorSpeed2Modifiers(0.2, 0.2);
      FindColorsTolerance(tpa, 4017231, MSX1, MSY1, MSX2, MSY2, 15);
      SetColorToleranceSpeed(tcts); //important!

    That's why the commented line here is very important. You should try to switch back to the original CTS after you are done with using FindColorsTolerance. If you don't, chances are you'll forget to do it later, or the script execution will escape the function without resetting it to original CTS, and your other color finding functions using different CTS will fail miserably.

    Another easy way to fix this is, if you're certain what CTS you're using, just force it at the beginning of the function. e.g. If you're always using CTS1 for RadialWalk, then add SetToleranceSpeed(1); before calling your walking. This saves a lot of hassle to figure out the program flow, but might not work in all applications.

    And that's about it. Attached is the snippet used in the illustration. Maybe play around with it. Learn what's good and what's bad from it. Enjoy.
    Current activity: Recovering from vacation
    - Nulla pars vitae vacare officio potest -
    SRL membership? Can I buy that?
    Scripts - AGS - SWF - WAR - EMS - W100S-EM
    If you need scripting help, you can pm me. Remember, if you need help you have to ask for it properly though

  2. #2
    Join Date
    Mar 2012
    Location
    Shenzhen China
    Posts
    19
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    helpful to me,thanks!

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
  •