Is there a way to make a timer count up?

Posted by BellisColdwine on Sat 15 Jul 2017 03:39 AM — 5 posts, 21,733 views.

#0
Sorry for the rookie question, but I was trying to make some triggers that would "learn" the duration of some buffs. I can't figure out how to make a timer count up though.

My desired outcome:
cast stoneskin
"Your skin toughens" <-- trigger fires on this text, enabling a timer that counts up
"Your skin softens" <-- 2nd trigger fires on this text, stopping the timer, and ideally writing that value to a variable.

(Then I'd like use that variable as the duration of a regular timer that counts down and warns me when the spell is about to wear off in the future, but one step at a time.)

Is this possible? TIA
USA Global Moderator #1
I probably wouldn't use a timer for that. I'd store the current time when you detect the first message, and then subtract it from the current time when you get the second message.

See:
https://www.mushclient.com/scripts/doc.php?lua=os.difftime and https://www.mushclient.com/scripts/doc.php?lua=os.time
or
https://www.mushclient.com/scripts/doc.php?lua=utils.timer


If you really want to use timers, then set the timer duration to be excessively high (like 1000000s) and then use GetTimerInfo to find the elapsed time by subtracting from that known value.
Amended on Sat 15 Jul 2017 01:54 PM by Fiendish
#2
Ah, makes good sense. Thank you.

Here's what I've got:

<aliases>
  <alias
   match="TestScriptOn"
   send_to="12"
   sequence="100"
  >
  <send>EnableTriggerGroup ("LearnMode", true")</send>
  </alias>

  <alias
   match="TestScriptOff"
   send_to="12"
   sequence="100"
  >
  <send>EnableTriggerGroup ("LearnMode", false")</send>
  </alias>
</aliases>

<triggers>
  <trigger
   enabled="y"
   group="LearnMode"
   match="*Your skin toughens.*"
   send_to="12"
   sequence="100"
   variable="StoneStart"
  >
  <send>SetVariable(StoneStart, utils.timer ())</send>
  </trigger>
  <trigger
   enabled="y"
   group="LearnMode"
   match="*Your skin softens*"
   send_to="12"
   sequence="100"
   variable="StoneEnd"
  >
  <send>SetVariable(StoneEnd, utils.timer ())</send>
  </trigger>
</triggers>

That all seems to be fine. Next I want to calculate the duration:

<aliases>
  <alias
   match="LearnDuration"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>/SetVariable("StoneDuration", tostring(tonumber(GetVariable("StoneEnd")) - GetVariable("StoneStart")))</send>
  </alias>
</aliases>

(Is there a better way to do that arithmetic? That seems awkward.)

Finally, I want to throw that into a Miniwindow as a cooldown button or bar or something. I'm still reading up on how to do that, but any advice on that would be appreciated!

Thanks,
Amended on Sun 16 Jul 2017 12:02 AM by BellisColdwine
Australia Forum Administrator #3
For short-term calculations Lua variables are simpler than client variables, particularly as they can be various types.

eg.

Start of event:


StoneStart = utils.timer ()


End of event:


StoneEnd = utils.timer ()


Duration:


StoneDuration = StoneEnd - StoneStart
#4
Alright, so I have a rudimentary version working.

As above, I get a timestamp when the effect starts, then again when it stops. I am tracking 8 buffs this way, so I combined the math all into one alias:


<aliases>
  <alias
   match="LearnDuration"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>SetVariable("D1", math.floor(tonumber(GetVariable("1End")) - GetVariable("1Start")))
SetVariable("D2", math.floor(tonumber(GetVariable("2End")) - GetVariable("2Start")))
SetVariable("D3", math.floor(tonumber(GetVariable("3End")) - GetVariable("3Start")))
...etc
Note ("Calculated durations!")</send>
  </alias>
</aliases>


I have a trigger set up for a string of text received on login that creates the BuffBar miniwindow:


<triggers>
  <trigger
   enabled="y"
   match="Welcome to some stuff*"
   send_to="12"
   sequence="100"
  >
  <send>require "InfoBox"

BB = InfoBox:New("Buffs")
</send>
  </trigger>
</triggers>


And then a trigger for the buff activation that adds the bar:


<triggers>
  <trigger
   enabled="y"
   match="*Your skin toughens*"
   send_to="12"
   sequence="110"
  >
  <send>Duration = (GetVariable("SSDuration") * GetVariable("Extend"))
DMax = Duration
BBHA = BB:AddBar("Stoneskin: ", 100)

require "wait"

wait.make (function ()

 for i = Duration, 2, -2 do
  Duration = (Duration - 2)
  BBHA.value = (Duration/DMax*100)
  BB:Update()
  wait.time(2)
 end

 BB:RemoveBar(1)
end)</send>
  </trigger>
</triggers>

("Extend" is another tracked buff that increases buff duration by 50%. The value of the variable is 1 when not present and 1.5 when it is.)

This mostly works fine. There is a bit of lag somewhere in this that sometimes results in the buff wearing off before the bar thinks it should -- It was very noticible with a 1s wait time, but 2s gets it right 95% of the time.

So now my question is how to scale this up to all 8 buffs? I assume the lag is going to get worse when multiple scripts are waiting and firing at once, and does the index of existing bars change when one is removed? If so, do I just need to create all 8 on login and have them stick around all the time?

(Again, sorry for the newbie-ness of this. I'm not a scripter at heart so I'm mostly stumbling around to find something that works.)