AHK - How to speed up this function? menu

User Tag List

Results 1 to 5 of 5
  1. #1
    pete2k's Avatar Private
    Reputation
    1
    Join Date
    Jul 2012
    Posts
    10
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AHK - How to speed up this function?

    Hi guys.
    I wrote a funktion to detect Numbers ingame. You need to input the path where the images 0...9.png are located. It takes several seconds to get the gold in inventory or the mony in AH. Can you help me to speed this up? what am i missing.

    lowering the Tolerance helps beeing faster but it wont recognize anymore. at AH i need 60 (because of the shine trough gackground).

    Code:
    ReadNumber(path = "img\num\inv_gold\", x1=1455,y1=510 , x2=1572, width_digit=9, height_digit=12,tolerance=5){ ; default : gold in inventory
    	amount:=0
    	SleepRand(100,10)
    	y2:=y1+height_digit
    	s1:=path . "1.png", s2:=path . "2.png", s3:=path . "3.png", s4:=path . "4.png", s5:=path . "5.png"
    	s6:=path . "6.png", s7:=path . "7.png", s0:=path . "0.png", s8:=path . "8.png", s9:=path . "9.png"
    	If !(FileExist(s1) AND FileExist(s2) AND FileExist(s3) AND FileExist(s4) AND FileExist(s5) AND FileExist(s6) AND FileExist(s7) AND FileExist(s8) AND FileExist(s9) AND FileExist(s0)){
    		return -1
    	}
    	Loop {
    		d:=-1
    		xtemp:=x2
    		if ImageSearchArea(s1, x, y, tolerance, x1, y1, x2, y2) {
    			if (x < xtemp) {
    				xtemp:=x
    				d:=1
    			}
    		}
    		if ImageSearchArea(s2, x, y, tolerance, x1, y1, x2, y2) {
    			if (x < xtemp) {
    				xtemp:=x
    				d:=2
    			}
    		}
    		if ImageSearchArea(s3, x, y, tolerance, x1, y1, x2, y2) {
    			if (x < xtemp) {
    				xtemp:=x
    				d:=3
    			}
    		}
    		if ImageSearchArea(s4, x, y, tolerance, x1, y1, x2, y2) {
    			if (x < xtemp) {
    				xtemp:=x
    				d:=4
    			}
    		}
    		if ImageSearchArea(s5, x, y,tolerance, x1, y1, x2, y2) {
    			if (x < xtemp) {
    				xtemp:=x
    				d:=5
    			}
    		}
    		if ImageSearchArea(s6, x, y, tolerance, x1, y1, x2, y2) {
    			if (x < xtemp) {
    				xtemp:=x
    				d:=6
    			}
    		}
    		if ImageSearchArea(s7, x, y, tolerance, x1, y1, x2, y2) {
    			if (x < xtemp) {
    				xtemp:=x
    				d:=7
    			}
    		}
    		if ImageSearchArea(s8, x, y, tolerance, x1, y1, x2, y2) {
    			if (x < xtemp) {
    				xtemp:=x
    				d:=8
    			}
    		}
    		if ImageSearchArea(s9, x, y, tolerance, x1, y1, x2, y2) {
    			if (x < xtemp) {
    				xtemp:=x
    				d:=9
    			}
    		}
    		if ImageSearchArea(s0, x, y, tolerance,  x1, y1, x2, y2) {
    			if (x < xtemp) {
    				xtemp:=x
    				d:=0
    			}
    		}
    		if (d=-1) { ;no digit found
    			break
    		}
    		x1:=xtemp+width_digit-1
    		if (amount=0) {
    			amount:=d
    		}
    		else {
    			amount := 10 * amount + d
    		}
    	}
    	return amount
    }
    btw, this is my ImageFind function;
    Code:
    ImageSearchArea(img, ByRef x, ByRef y, tolerance=0, x1=0, y1=0, x2=1920, y2=1080) { ; return 1 if found
    	If (tolerance > 0) img := "*" . tolerance . " " . img
    	img := "*TRANSBLACK " . img
    	ImageSearch, x, y, x1, y1, x2, y2, %img%
    	if (ErrorLevel = 0) {
    		return 1
    	}
    	else {
    		return 0
    	}
    }

    AHK - How to speed up this function?
  2. #2
    Nablamax's Avatar Master Sergeant
    Reputation
    5
    Join Date
    Jun 2012
    Posts
    76
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you make a match in one of your gazillions ifs it will still check all the other ifs before looping back, is that desirable? Also, amount := 10*amount + d is equivalent to amount := d when amount = 0 so your last if/else is not necessary. I would also suggest using arrays to make the code smaller, easier to read and more extensible.

  3. #3
    pete2k's Avatar Private
    Reputation
    1
    Join Date
    Jul 2012
    Posts
    10
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Nablamax View Post
    If you make a match in one of your gazillions ifs it will still check all the other ifs before looping back, is that desirable?
    Yes.
    it may find an "1" but there is another digit before it so it will continue until it finds the digit with the lowes x-coord. This is where i need
    Code:
    if (x < xtemp) {
    				xtemp:=x
    				d:=1
    			}
    xtemp is the lowest x coor found (so far)

    Originally Posted by Nablamax View Post
    Also, amount := 10*amount + d is equivalent to amount := d when amount = 0 so your last if/else is not necessary.
    That is right thanks, but it will not speed it up

    Originally Posted by Nablamax View Post
    I would also suggest using arrays to make the code smaller, easier to read and more extensible.
    what exactly do you have in mind? make the pathes in one array and loop it?
    Last edited by pete2k; 07-29-2012 at 11:35 AM.

  4. #4
    Nablamax's Avatar Master Sergeant
    Reputation
    5
    Join Date
    Jun 2012
    Posts
    76
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make an array with the path, the index would also be the digit and just do something like:

    Code:
    Loop{
    d:=-1
    xtemp:=x2
    For i=0 to 9
    if ImageSearchArea(s[i], x, y, tolerance, x1, y1, x2, y2) {
    			if (x < xtemp) {
    				xtemp:=x
    				d:=i
    			}
    		}
    Next
    
    if (d=-1) { ;no digit found
    			break
    		}
    		x1:=xtemp+width_digit-1
    amount := 10 * amount + d
    
    }
    That's not proper AHK (I don't code in AHK), but it should work the same. It seems like your function is wasting a lot of compute time, since you probably find more than one digit per loop and you're dismissing them to find the first one and then crop the image and repeat. Maybe you will have more luck doing something recursive that doesnt dismiss found digits, like (in pseudocode):

    Code:
    String find_number(Image){
     String digit = find_digit(Image)
     if(digit=="-1") return ""
    else{
    Image image_left = crop_image_left(Image)
    Image image_right = crop_image_right(Image)
     return find_number(image_left) + digit + find_number(image_right)
    }
    }
    This should be much more efficient since you will need around half the ImageSearch calls per digit and these calls will be made on smaller images (best scenario is you're spliting the image in 2 in every call). ImageSearch should be sensitive to the image size.

    Also, you can add a filter at the beginning of the function to mark the digits that actually are in the image and cycle through those digits instead of all the digits.
    Last edited by Nablamax; 07-29-2012 at 12:37 PM.

  5. #5
    pete2k's Avatar Private
    Reputation
    1
    Join Date
    Jul 2012
    Posts
    10
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i will try this tomorrow thx

    edit: i can not try it this week, since i go to vacation now. thx anyway!
    Last edited by pete2k; 07-30-2012 at 06:34 AM.

Similar Threads

  1. Replies: 22
    Last Post: 05-29-2008, 03:52 PM
  2. how do people do this?
    By mchugh in forum Community Chat
    Replies: 5
    Last Post: 05-14-2007, 11:23 PM
  3. HOW Do i download this game?
    By leoqazza in forum Community Chat
    Replies: 4
    Last Post: 04-18-2007, 10:48 PM
  4. How Do I Get This Guy Arrested?!
    By Keonei in forum World of Warcraft General
    Replies: 3
    Last Post: 03-14-2007, 11:12 PM
  5. How do i do this spell?wow emu
    By mchugh in forum World of Warcraft General
    Replies: 2
    Last Post: 12-27-2006, 03:58 AM
All times are GMT -5. The time now is 01:10 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search