Optimize code, make it run faster

Ask gaming related questions
kyuuuri
Posts: 54
Joined: 09 Jan 2016, 19:20

Optimize code, make it run faster

16 Jul 2017, 08:51

Hi, first of all thank you!.
I made an autopot/autobuff for a game, (it's 100% legal as long as you don't mess with games delays, etc.), they gave us an autopot but on virustotal it detects 44/47 virus. And i don't really want to use it.
I prefer to learn how to do those things and do my own.
I want it to run as fast as possible, and i don't know what things i can change. I mean, i read eveything about ahk and how to make scripts faster, but now i don't know what should i change, what parts of the code can be reduced. This is why i'm asking for help, my script works, but slow.

So here is the code:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



What it does:
1_ Opens gui, you input the keys for potions, % of hp/sp, the button for a cure item, and the map where you don't want it to run.
2_ press f8 to start, when you press f8 it does the same:
    1. Checks current map and if chat is opened.
    2. If its on, and chat is not opened and map is not selected then it checks actual hp and maximum hp.
    3. If it's lower than the % selected then it goes to pothp that works like a loop, it sends the key and goes back to the check until hp is higher than % selected.
    4. Then if hp is above the % selected, it checks sp, same as hp check but with potsp.
    5. Then if both hp and sp are above the % selected, checks for status to cure, same as hp but with potstt
    6. Then if hp, sp and stt are okay, it checks buffs.
    7. This part is the one i have to explain the most: buffs are asigned to a range of memory address that never vary (for example, if initial one is 20 then the following is 24 then 28 then 30 then 34 then 38, etc). Buffs are not always assigned to the same memory address, let's say i have buff 1 on address 20, then somebody gives me buff 2, and now buff 1 goes to address 24 and buff 2 goes to 20. That's why i check the entire range of addresses and do a +1 on a variable everytime it detects that the buff is not there. If it reachs the maximum amount then it's because the buff wasn't found on any address.
    8_ Checks what buffs were not found on the range of addresses, when one was not found it goes to bufff. But then it comes back and checks the rest of the buffs (that's why i use gosub and not goto there).
    9_ Then all starts again.

Right now i don't know how to optimize this. Feel free to ask everything, I changed all the address because they don't really matter to optimize the code.
User avatar
Exaskryz
Posts: 2013
Joined: 17 Oct 2015, 20:28

Re: Optimize code, make it run faster

16 Jul 2017, 21:43

Look into what is repeated, but never changes. I notice that you have a part in the ReadMemory() function that chekc sot see if OLDPROC and PROGRAM differ -- but does that variable ever change in your script? I mean, do you ever pass a different value to the PROGRAM parameter? I'm not seeing that you do; I see "program" for all the ReadMemory() function calls at a glance. But if there are multiple programs you're calling, and in this published script in your post you changed the name of the program to a generic "program", then maybe you do still need it.

Also for the ReadMemoryString() function, something similar goes on - do you need to keep getting the pid for the program with winget? If it's the same program every time, then the pid should always be the same. So it looks like you should be able to move the winget and ProcessHandle := lines into the auto-execute section of your script and just make them global variables for the function to access when it does the loop to populate teststr.

Also while I don't know the significance of it's impact on performance, do you need the Output := "x" bit? The comment here says it would be used if Output becomes null, where the loop would break. However, you commented out the if Output = and break. Though if you still need the Output variable as a delimiter, that should be OK.

You might consider just using teststr.=output to append the variable output to the current value of teststr (see operators), rather than the teststr = %teststr%%Output%.

That's what I'm seeing at a glance, though I was not thorough.
kyuuuri
Posts: 54
Joined: 09 Jan 2016, 19:20

Re: Optimize code, make it run faster

17 Jul 2017, 05:22

Exaskryz wrote:Look into what is repeated, but never changes. I notice that you have a part in the ReadMemory() function that chekc sot see if OLDPROC and PROGRAM differ -- but does that variable ever change in your script? I mean, do you ever pass a different value to the PROGRAM parameter? I'm not seeing that you do; I see "program" for all the ReadMemory() function calls at a glance. But if there are multiple programs you're calling, and in this published script in your post you changed the name of the program to a generic "program", then maybe you do still need it.

Also for the ReadMemoryString() function, something similar goes on - do you need to keep getting the pid for the program with winget? If it's the same program every time, then the pid should always be the same. So it looks like you should be able to move the winget and ProcessHandle := lines into the auto-execute section of your script and just make them global variables for the function to access when it does the loop to populate teststr.

Also while I don't know the significance of it's impact on performance, do you need the Output := "x" bit? The comment here says it would be used if Output becomes null, where the loop would break. However, you commented out the if Output = and break. Though if you still need the Output variable as a delimiter, that should be OK.

You might consider just using teststr.=output to append the variable output to the current value of teststr (see operators), rather than the teststr = %teststr%%Output%.

That's what I'm seeing at a glance, though I was not thorough.


First of all thank you!, those functions i used are not mine, i found them on the forum when i had the problem that i didn't know how to read process memory address.
Made the changes you said and came up with this:

Code: [Select all] [Download] GeSHi © Codebox Plus

WinGet, pid, pid, program
ProcessHandle := ( ProcessHandle ? 0*(closed:=DllCall("CloseHandle","UInt",ProcessHandle)) : 0 )+(pid ? DllCall("OpenProcess","Int",16,"Int",0,"UInt",pid) : 0)

ReadMemory(MADDRESS=0,PROGRAM="")
{
VarSetCapacity(MVALUE,4,0)
return *(&MVALUE+3)<<24 | *(&MVALUE+2)<<16 | *(&MVALUE+1)<<8 | *(&MVALUE)
}


I moved outside WinGet, and ProcessHandle declaration. Removed the if checks because it won't fail, and if it does i don't want to know :D, as said those are not my functions so idk if the changes i made are okay, i'm not at home right now to check it.

And on the other function changed:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



I moved out ProcessHandle1 declaration. Didn't remove x var because it ends the loop when its null (forgot to remove the ";"), but now that i think about what string i'm reading it can only contain up to 15 characters. I think that 15 loops without the output = x and the if to check that, is faster than X loops with those things.

Idk if i should use ProcessHandle1 or if i can use the same i used for the other function. I don't understand the difference between them).

That will improve the speed for sure, thanks for that.

I think that the code still can be optimized, i mean when i look in all those "if" i have to use i feel like i'm losing a lot of speed there.

Return to “Gaming”

Who is online

Users browsing this forum: Bing [Bot] and 5 guests