MUSH Auto-sipper

Okay, so I run MUSH versus Mudlet. Only reason is because my system's too old, need to upgrade. Anyway, for MUSH, I wrote an auto-sipper(written in Lua). Works fairly well, except for one thing: As soon as health or mana hits X% (X being the percent I set it, in the case 75% health, 65% Mana), it sips without regard to the sip balance. Now, I've no actually coding expereince (got the help of a friend who codes for a living), so I don't know what the issue is within the code, or how to fix it. Any help would be appreciated.

The code is as follows:

SetVariable('WorldScriptCompiled', "False")

function sleep(sec)
    socket.select(nil, nil, sec)
end

Health = "health"
Vitality = "vitality"
Mana = "mana"
Mentality = "mentality"

dbg = function(t,m)
  if core.Debug() then
    ColourNote("red", "black", "sipper::" .. t .. ": " .. m)
  end
end

HeartBeatInterval = tonumber(GetVariable('HeartBeatInterval'))
if HeartBeatInterval < 1 then
  HeartBeatInterval = 10
  SetVariable('HeartBeatInterval', 10)
end 

-- Heartbeat routines
stats = {
  healthText = "health",
  manaText = "mana",
  healthHeal = 0.75,
  manaHeal = 0.85,
}

potions = {
   BG = "Indigo",
   Warn = "Yellow",
   Out = "Red",
   types = {
        Health,
        Vitality,
        Mana,
        Mentality,
   }
}

UseHealth = "usehealth"
UseMana = "usemana"

potions[Health] = 0
potions[Vitality] = 0
potions[Mana] = 0
potions[Mentality] = 0
potions[UseHealth] = Vitality
potions[UseMana] = Mentality


potions.set = function (h, v, ma, me)  
   potions[Health] = h
   potions[Vitality] = v
   potions[Mana] = ma
   potions[Mentality] = me
end

potions.setcount = function(typeof, amount)
  potions[typeof] = amount
end

potions.inc = function(typeof)
  potions[typeof] = potions[typeof] + 1
end

potions.add = function(typeof, count)
  potions[typeof] = potions[typeof] + tonumber(count)
end

potions.dec = function(typeof)
  potions[typeof] = potions[typeof] - 1
end

potions.clear = function(typeof, amount)
   potions[Health] = 0
   potions[Vitality] = 0
   potions[Mana] = 0
   potions[Mentality] = 0
end

potions.setpotions = function(primary, secondary, setof)
  potions[setof] = 0
  if (potions[primary] > 0) then
    potions[setof] = primary
  elseif (potions[secondary] > 0) then
    ColourNote(potions.Warn, potions.BG, "You are out of primary " .. primary .. ", using " .. secondary)
    potions[setof] = secondary
  else
    ColourNote(potions.Out, potions.BG, "You are OUT OF " .. primary .. " and " .. secondary .. " potions!")
  end
  return potions[setof]
end

potions.count = function()
  for typeof = 1, #potions.types  do
    ColourNote("green", "black", potions.types[typeof] .. " left: " .. potions[potions.types[typeof]])
  end
end

potions.list = function()
  SendImmediate("elist")
end

core = {
  paused = false,
  interval = HeartBeatInterval,
  current = HeartBeatInterval,
  priority = Health,
}

core.sethb = function(val)
  core.paused = false
  if val == true then
    core.paused = true
  end
end

core.debugflag = false
core.Debug = function()
  return core.debugflag
end

core.togglehb = function ()
  if (core.paused == true) then
    dbg ("core::togglebh", "Setting pause to false")
    core.sethb(false)
    return
  end
  dbg ("core::togglebh", "Setting pause to true")
  core.sethb (true)
end
 
core.setinterval = function(int)
  core.interval = int
  core.current = int
end

core.tick = function()
  core.current = core.current - 1
  if core.current < 1 then
    core.current = core.interval
    return true
  end
  return false
end

core.elixBal = true;

-- curing functions

heartbeat = function (health, mana)
  if core.paused then
    return
  end
  if core.tick() then
    check.hm(health, mana)
  end
  dbg("heartbeat", "Interval is now " .. core.current)
end -- func

check = {
  -- check stats, fix if necessary
  stats = function (current, maxa, percent, typeof)
    dbg ("check::stats", "Checking " .. typeof .. "...");
    if (core.elixBal) and (current < maxa*percent) then
        if potions[typeof] > 0 then
          dbg("check::stats:", current .. "<" .. maxa*percent)                
          Send ("sip " .. typeof)
          ColourNote("red", "#555555", typeof .. " potion was sipped")
          potions.dec(typeof)
          return true
        end
    end
    return false
  end
  ,
  hm = function (health, mana)
    mh = tonumber(GetVariable('MaximumHealth'))
    mm = tonumber(GetVariable('MaximumMana'))
    stats.manaText = potions[UseMana]
    stats.healthText = potions[UseHealth]
    dbg ("check::hm", stats.manaText)
    dbg ("check::hm", stats.healthText)
    potions.setpotions(Vitality, Health, UseHealth);
    potions.setpotions(Mentality, Mana, UseMana);
    if core.priority == Mana then
      if check.stats(mana, mm, stats.manaHeal, stats.manaText) == false then
        check.stats(health, mh, stats.healthHeal, stats.healthText)
      end
    else
      -- fallthrough to health, default
      if check.stats(health, mh, stats.healthHeal, stats.healthText) == false then
        check.stats(mana, mm, stats.manaHeal, stats.manaText)
      end
    end
    potions.count()
  end
}

dbg ("nil", "core.paused=" .. tostring(core.paused))
SetVariable('WorldScriptCompiled', "True")
-- ColourNote("white", "black", "World script sipper is ready.")
potions.list()
Send("elist")
--The tonic heals and soothes you.
--Walnut vial457551             a tonic of mentality     166        30

Comments

  • KeiKei
    edited March 2014
    One thing that jumps out right away is that when you send("sip "..typeof);, you don't then set (or ever check) some control flag that says "Hey, I'm trying to sip, don't just keep spamming the command until I see the sip line when the first goes through". Of course, you need to have a check that then unsets this if the sip fails or just takes too long, though.

    Edit: Now that I have more than a minute to type this...I would personally make your sip balance a trinary value, or just an integer, rather than binary, and do something like on balance = 1, off balance = 0, trying to expend balance = 0.5, and then you know the state of what you're doing and attempting to do at any given moment and only have to check one variable for a specific state rather than have your if checks contain more and more conditions that gets progressively harder to digest at once.

    The other thing is that I don't know what your triggers look like. For example, do you have both elixirs and tonics accounted for in your sipped trigger, and does that properly set the sip balance variable to false? Really simple, but easy to overlook if the Regex got mangled trying to account for both elixirs and tonics. Should look something like (going off of memory)...

    ^The (?:elixir|tonic) heals and soothes you.$
    ^The (?:elixir|tonic) flows down your throat with no effect.$
    ^Your mind feels stronger and more alert.$

    And all of those feed into the same that sets your sip balance off.

    Bydar, a garish-looking trader says, "I'm not a man, I'm an experience."
Sign In or Register to comment.