Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


scheduled function call

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Extensions
View previous topic :: View next topic  
Author Message
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 938

PostPosted: Mon Jan 23, 2017 2:32 am    Post subject: scheduled function call Reply with quote

origin http://forum.cheatengine.org/viewtopic.php?t=602587

add callStepGroup here, may be more intuitive to use.

Try don't use a too small result time/scale, the overhead may turn the call sequence not as expected.

Code:

local function callnext(n,f,...) -- make local so not mixed with may be another version of callnext
  local pk,upk,t,p = table.pack,table.unpack,createTimer()
  if type(n)~='number'then n,f,p=1,n,pk(f,...)else p = pk(...)end
  t.Interval,t.OnTimer = n,function(tm)
    tm.Destroy()
    pcall(f,upk(p))
  end
  return t
end

function callstep(n)
  assert(type(n)=='number' and n>0,"Input not a positive number: "..tostring(n))
  local pk,upk,p = table.pack,table.unpack,{}
  local function collect(f,...)
    if f == nil then
      for i=1,#p do if p[i][1] then callnext(1+(i-1)*n,p[i][1],upk(p[i],2)) end end
    else
      p[1+#p]= pk(f,...)
      return collect
    end
  end
  return collect
end


local msev = mouse_event
local function mclick(down,up,int)
  local down,up,int = down,up,int or 35
  return function()
    local t = createTimer(),msev(down)
    t.Interval,t.OnTimer = int,function(tm)tm.Destroy();msev(up)end
  end
end
local mouse2func = {
 ['+m0']= {msev,2},['-m0']= {msev,4},['^m0']={mclick(2,4)},
 ['+m1']= {msev,8},['-m1']= {msev,16},['^m1']={mclick(8,16)},
 ['+m2']= {msev,32},['-m2']= {msev,64},['^m2']={mclick(32,64)}
}
local key2func = {['^']=doKeyPress,['+']=keyDown,['-']=keyUp}
function callStepGroup(scale)
  assert(type(scale)=='number' and scale>0,"Scale is not positive number: "..tostring(scale))
  local pk,upk,fmt,int,p = table.pack,table.unpack,string.format,math.floor,{}
  local function collect(maintime,...)
    if maintime == nil then
      local callset,mainacc,subacc = {},1
      for i=1,#p do
        local spec = p[i]
-- spec in format :  maintime, { func1, ... }, subtime1, { func2, ... }, subtime2,...{ funcN, ...}
        local maintime = spec[1]
        if maintime == false then break end
        assert(type(maintime)=='number',
          fmt("maintime in step %d is not number: %s",i,tostring(maintime)))
        mainacc = int(mainacc + maintime * scale)
        subacc = mainacc
        for j=2,#spec,2 do
          local group = spec[j] -- in format { func, ... }
          if group == false then break end
          if type(group)=='string' and group:match"^[-+^]%w+$" then --key-related shortcut
            local mev = mouse2func[group:lower()]
            if not mev then
              local func, key = group:match"([-+^])(%w+)"
              func = key2func[func]
              key = _G["VK_"..key:upper()]
              group = {func,key}
            else
              group = mev
            end
          end
          assert(type(group)=='table',
            fmt("%d(th) group in step %d is not table: %s",j,i,tostring(group)))
          callset[1+#callset] = pk(subacc,upk(group))
          if j<#spec then -- if not last subgroup
            local subtime = spec[j+1]
            if subtime == false then break end
            assert(type(subtime)=='number',
              fmt("%d(th) subtime in step %d is not number: %s",j,i,tostring(subtime)))
            subacc = int(mainacc + subtime * scale - 1)
          end
        end-- j
      end--i
      return function(extraScale)
        if type(extraScale)=='number' and extraScale~=1 then
          for i=1,#callset do callnext(callset[i][1]*extraScale,upk(callset[i],2))end -- fire
        else
          for i=1,#callset do callnext(upk(callset[i]))end -- fire
        end
      end
    else
      p[1+#p]= pk(maintime,...)
      return collect
    end
  end
  return collect
end

-- test callStepGroup

local preGeneratedCallSet =   callStepGroup(10) -- scale
    (  0,{keyDown,VK_A},25,{keyUp,VK_A}) -- first number is relative to whole sequence, after scale multiplied
    ( 25,{keyDown,VK_W},25,{keyUp,VK_W}) -- subsequence number is time relative to the parent group, after scale multiplied
    ( 50,{keyDown,VK_D},25,{keyUp,VK_D})
    ( 75,{keyDown,VK_S},25,{keyUp,VK_S})
    (100,{keyDown,VK_LSHIFT},100,{keyUp,VK_LSHIFT}) -- sprint?
    (100,"+A",25,"-A") -- shortcuts
    (125,"+W",25,"-W")
    (150,"+D",25,"-D")
    (175,"+S",25,"-S")
() -- generate the callset here
-- not fire yet, so that the callset is generated once,
   and can be execute elsewhere any time.


local hk = createHotkey(function()-- the function to do on hotkey

  preGeneratedCallSet(2) -- fire now with an extra time scaling

end,
VK_NUMPAD0)

-- or

local hk = createHotkey(preGeneratedCallSet,VK_NUMPAD0) -- better if no extra scaling for lesser one function wrap

callnext(20000,function()
  hk.Destroy()
  print("destroying")
end)


ADDED:
added shortcut to 3 key-related function call: doKeyPress ('^'), keyDown ('+'), keyUp ('-'). eg.
{keyDown,VK_D} -- a table, can be shortcut as
"+D" -- a string, see test example.

ADDED:
add mouse click too.
m0 is left mouse button, m1 is right and m2 is middle.
"+m0" means press left mouse button down,
"-m1" means release right mouse button (up),
"^m0", simulate a left mouse button click, it is made by a sequence of

1.left mouse button down,
2.wait ~35 ms (there is overhead)
3.left mouse button up,

There should be no other left mouse event send during the interval, should assume at least 40ms in between I guess.

_________________
- Retarded.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Extensions All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites