Inquiry on auto perform/cast script

Posted by Hidelensman on Fri 25 Aug 2023 12:52 PM — 4 posts, 11,738 views.

#0
Hi Nick and all,

Thanks for your previous advice on the scripting and I'm playing the MUD much better than 20 years ago (yes the same MUD I played when I was a child lol)

Recently I found some old Lua code and want to revise it for my char, while it cannot work so want to consult you:

  • Goal: auto perform/cast when enter a fight
  • If the 1st attempt fails, then auto perform/cast again until succeeds
  • Multiple criteria of failure and success


The following are my codes and appreciate your help to advise where goes wrong:

New self-defined function 1: if ts/tw matched, return true; if fs/fw matched, return false
function mywait(ts, fs, tw, fw)
	local expr = "^(" .. table.concat(ts, "|") .. "|" .. table.concat(fs, "|") .. ")$"
	local l, w = wait.regexp(expr)
	if not l then return false end
	if tw then
		for _, v in pairs(tw) do
			if string.find(l, v) then return true end
		end
	end
	if fw then
		for _, v in pairs(fw) do
			if string.find(l, v) then return false end
		end
	end
	for _, v in pairs(fs) do
		if string.find(l, v) then return false end
	end
	for _, v in pairs(ts) do
		if string.find(l, v) then return true end
	end
	return true
end


New self-defined function 2

function myrepeat(cmd, ts, fs, tw, fw, t)
	repeat
		Execute(cmd)
		local rpt = mywait(ts, fs, tw, fw)
		if rpt and t then
			wait.time(t)
		end
	until not rpt
end


New Mushclient variable "pfm": pfm boundary

Auto perform script: same for auto_cast


function auto_pfm()
	local p = "p"
	if var.pfm and var.pfm ~= "" then p = var.pfm end
	myrepeat(p, {"xxx1", "xxx2"}, {"yyy1", "yyy2", "yyy3", "yyy4"}, nil, nil, 0.5)
	if f then f() end
end


Note
  • xxx1, xxx2: criteria of failed perform (i.e. the auto perform script should continue executing if "xxx1" and "xxx2" appear)
  • yyy1, yyy2, yyy3, yyy4: criteria of successful perform (i.e. the auto perform script should stop if "yyy1~4" appear)
  • xxx1, xxx2, yyy1, yyy2, yyy3, yyy4 are in traditional Chinese (with regular expression) so I don't put the original words here, while I tested them via regex101 as well as new Muchclient triggers and both methods work fine



After trial and errors for 2 weeks, I still cannot make the script work as what I expect so would like to seek for your opinion to improve them. Thanks.
Amended on Fri 25 Aug 2023 01:44 PM by Hidelensman
Australia Forum Administrator #1
It's hard to say without more context. Is this all in an alias? Or what? Do you use the "wait" module? We can't test or reproduce with snippets like this.

Your use of tiny variable names isn't helping. This sort of stuff doesn't tell us what you are trying to do:

Quote:

function mywait(ts, fs, tw, fw)
...
function auto_pfm()
local p = "p"
if var.pfm and var.pfm ~= "" then p = var.pfm end
myrepeat(p, {"xxx1", "xxx2"}, {"yyy1", "yyy2", "yyy3", "yyy4"}, nil, nil, 0.5)


It looks somewhat like line noise.

There is no benefit from using small variable names. Lua is pre-compiled before it is executed (into bytecode). Using small variable names doesn't make it any faster, it is just confusing for everyone including, I dare say, yourself.
#2
Nick Gammon said:

It's hard to say without more context. Is this all in an alias? Or what? Do you use the "wait" module? We can't test or reproduce with snippets like this.

Your use of tiny variable names isn't helping. This sort of stuff doesn't tell us what you are trying to do:


Thanks Nick - sorry some info might be lost when I typed the post.

Yes I already require "wait" in the game so don't specifically call "wait" module in these self-defined functions again.

I use these scripts when enter into a fight: when encountering a mob, call "auto_pfm()", and intend to continuously execute the command included in "auto_pfm()" until the stop criteria matched.

Nick Gammon said:

It looks somewhat like line noise.

There is no benefit from using small variable names. Lua is pre-compiled before it is executed (into bytecode). Using small variable names doesn't make it any faster, it is just confusing for everyone including, I dare say, yourself.


In fact the xxx1, xxx2, yyy1, yyy2, yyy3, yyy4 are not variables - they are the criteria to continue or stop the auto perform, while because they are in Chinese so I didn't paste the words here.

Admittedly the idea/original codes are from a post in another forum (posted about 10 years ago) - I think they can fulfill my need so try to adjust for my purpose.

Any good suggestion on how to debug or revise my codes? Or any advice on how to create an auto perform script for fighting? Thanks.
Amended on Sun 27 Aug 2023 11:35 AM by Hidelensman
Australia Forum Administrator #3
Hidelensman said:

In fact the xxx1, xxx2, yyy1, yyy2, yyy3, yyy4 are not variables - they are the criteria to continue or stop the auto perform, while because they are in Chinese so I didn't paste the words here.



I was thinking more of these variables:


 function mywait(ts, fs, tw, fw)


ts, fs, tw, fw?

What the heck?

Hidelensman said:

Admittedly the idea/original codes are from a post in another forum ...


OK, so this isn't your code, so you aren't sure what it does. That makes two of us.

We can't help you if we can't reproduce the problem. You need to provide a minimal example that actually demonstrates the issue.

Template:copying
For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.


We can't debug code, written by someone else, provided in snippets and not in context, without any test data.

You need to show:

  • The actual triggers/aliases/timers you are using for this activity
  • The output from the MUD that is supposed to make things happen
  • What actually happens
  • What you expect to happen