shipvitalsbox.lua

--- Gauges that sit at the bottom and do gauge things related to things to do on a ship.
--@classmod ShipVitalsBox

ShipVitalsBox = {}





--------------------------------------------------------------------------------
--- Initialisation.
-- For creating and Initialising ShipCompass object.
-- @section initialisation
--------------------------------------------------------------------------------

----------------------------------------
--- Creates a new instance of the ShipVitalsBox class.
-- @tparam string _name Instance name, should be identical to variable instance is assigned to.
-- @tparam number _elementX X coordinate of ShipVitalsBox, in screen coordinates.
-- @tparam number _elementY Y coordinate of ShipVitalsBox, in screen coordinates.
-- @treturn table A new instance of the ShipVitalsBox class.
-- @usage vitals = ShipVitalsBox:new("shipVitals", 964, 955)
function ShipVitalsBox:new(_name, _elementX, _elementY)

    local _adjustmentFactor = -33

    cloudDir = cloudDir or [[D:\Achaea\]]
    self.__index = ShipVitalsBox
    local self = setmetatable({}, ShipVitalsBox)

    --===============================
    -- BASIC ATTRIBUTES

    self._instanceName = _name

    if self._updateHandler then killAnonymousEventHandler(self._updateHandler) self._updateHandler = false end
    self._updateHandler = self._updateHandler or registerAnonymousEventHandler("svo done with prompt", self._instanceName..":update")

    --===============================
    -- MAIN CONTAINER

    self._container = Geyser.Label:new({ name = self._instanceName.."._container", x = _elementX, y = _elementY, width = 942, height = 32 })

    --===============================
    -- LABELS

    --self._labelBackground  = Geyser.Label:new({ name = self._instanceName.."._labelBackground",  x = 0,   y = 0, width  = 980, height = 26 }, self._container )
    self._labelBalance     = Geyser.Label:new({ name = self._instanceName.."._balance",          x = 445 + _adjustmentFactor, y = 0, width  = 26,  height = 32, color = "#007700" }, self._container )
    self._labelCrewBalance = Geyser.Label:new({ name = self._instanceName.."._labelCrewBalance", x = 511 + _adjustmentFactor, y = 0, width  = 52,  height = 32, color = "#007700" }, self._container )
    self._labelEquilibrium = Geyser.Label:new({ name = self._instanceName.."._equilibrium",      x = 471 + _adjustmentFactor, y = 0, width  = 26,  height = 32, color = "#007700" }, self._container )

    --===============================
    -- LABEL STYLESHEETS AND IMAGES

    --self._labelBackground:setStyleSheet(  [[background-color: #0000FF;]]   )
    self._container:setStyleSheet(        [[background-color: #00000000;]]   )
    self._labelBalance:setStyleSheet(     gui.css.ship.balanceGreen        )
    self._labelCrewBalance:setStyleSheet( gui.css.ship.balanceBlue         )
    self._labelEquilibrium:setStyleSheet( gui.css.ship.balanceRed          )

    --===============================
    -- LABEL ECHOES

    self._labelBalance:echo(     "B", "white", "14" )
    self._labelCrewBalance:echo( "C", "white", "14" )
    self._labelEquilibrium:echo( "E", "white", "14" )

    --===============================
    -- GAUGES

    self._gaugeEndurance  = Geyser.Gauge:new({ name = self._instanceName.."._gaugeEndurance",  x = 239 + _adjustmentFactor, y = 0, height = 32, width = 89  }, self._container )
    self._gaugeFigurehead = Geyser.Gauge:new({ name = self._instanceName.."._gaugeFigurehead", x = 577 + _adjustmentFactor, y = 0, height = 32, width = 192 }, self._container )
    self._gaugeHealth     = Geyser.Gauge:new({ name = self._instanceName.."._gaugeHealth",     x = 33  + _adjustmentFactor, y = 0, height = 32, width = 89  }, self._container )
    self._gaugeMana       = Geyser.Gauge:new({ name = self._instanceName.."._gaugeMana",       x = 136 + _adjustmentFactor, y = 0, height = 32, width = 89  }, self._container )
    self._gaugeSeamonster = Geyser.Gauge:new({ name = self._instanceName.."._gaugeSeamonster", x = 783 + _adjustmentFactor, y = 0, height = 32, width = 192 }, self._container )
    self._gaugeWillpower  = Geyser.Gauge:new({ name = self._instanceName.."._gaugeWillpower",  x = 342 + _adjustmentFactor, y = 0, height = 32, width = 89  }, self._container )

    --===============================
    -- GAUGE STYLESHEETS

    self._gaugeEndurance.back:setStyleSheet(   gui.css.ship.defaultGaugeBack    )
    self._gaugeEndurance.front:setStyleSheet(  gui.css.ship.defaultGaugeYellow  )

    self._gaugeFigurehead.back:setStyleSheet(  gui.css.ship.defaultGaugeBack    )
    self._gaugeFigurehead.front:setStyleSheet( gui.css.ship.defaultGaugeSeafoam )

    self._gaugeHealth.back:setStyleSheet(      gui.css.ship.defaultGaugeBack    )
    self._gaugeHealth.front:setStyleSheet(     gui.css.ship.defaultGaugeYellow  )

    self._gaugeMana.back:setStyleSheet(        gui.css.ship.defaultGaugeBack    )
    self._gaugeMana.front:setStyleSheet(       gui.css.ship.defaultGaugeYellow  )

    self._gaugeSeamonster.back:setStyleSheet(  gui.css.ship.defaultGaugeBack    )
    self._gaugeSeamonster.front:setStyleSheet( gui.css.ship.defaultGaugeOrange  )

    self._gaugeWillpower.back:setStyleSheet(   gui.css.ship.defaultGaugeBack    )
    self._gaugeWillpower.front:setStyleSheet(  gui.css.ship.defaultGaugeYellow  )

    --===============================
    -- GAUGE ECHOES AND SETVALUES

    self._gaugeEndurance:setValue(  50,100 )
    self._gaugeFigurehead:setValue( 50,100 )
    self._gaugeSeamonster:setValue( 50,100 )
    self._gaugeHealth:setValue(     50,100 )
    self._gaugeMana:setValue(       50,100 )
    self._gaugeWillpower:setValue(  50,100 )

    self._gaugeHealth.front:echo(    " HP: ---%","grey","b10" )
    self._gaugeMana.front:echo(      " MP: ---%","grey","b10" )
    self._gaugeEndurance.front:echo( " EN: ---%","grey","b10" )
    self._gaugeWillpower.front:echo( " WP: ---%","grey","b10" )

    self._gaugeHealth.back:echo(    " HP: ---%","grey","b10" )
    self._gaugeMana.back:echo(      " MP: ---%","grey","b10" )
    self._gaugeEndurance.back:echo( " EN: ---%","grey","b10" )
    self._gaugeWillpower.back:echo( " WP: ---%","grey","b10" )

    self._container:raiseAll()

    return self

end





--------------------------------------------------------------------------------
--- Gauge methods
-- For creating and Initialising ShipCompass object.
-- @section gauge
--------------------------------------------------------------------------------

----------------------------------------
--- Sets endurance gauge and text.
-- @tparam int _currentEnd Current endurance
-- @tparam int _maxEnd Maximum endurance
function ShipVitalsBox:setEndurance(_currentEnd, _maxEnd)
    self._gaugeEndurance:setValue(_currentEnd, _maxEnd)
    self._gaugeEndurance.front:echo(" EN: " .. round((_currentEnd/_maxEnd)*100).."%", "white", "b10" )
    self._gaugeEndurance.back:echo(" EN: " .. round((_currentEnd/_maxEnd)*100).."%", "white", "b10" )
end

----------------------------------------
--- UNDOCUMENTED
-- @todo You should most *certainly* fix this!
function ShipVitalsBox:setFigurehead(_currentCharge)

    if not thisFigurehead then
        self._gaugeFigurehead:setValue(0,10000)
        self._gaugeFigurehead.front:echo("(no tracker)", "red", "cb14" )
        self._gaugeFigurehead.back:echo("(no tracker)", "red", "cb14" )
        return
    end

    if not thisShip then
        self._gaugeFigurehead:setValue(_currentCharge,10000)
        self._gaugeFigurehead.front:echo(" FH: " .. _currentCharge, "white", "b10" )
        self._gaugeFigurehead.back:echo(" FH: " .. _currentCharge, "white", "b10" )
        return
    end

    if not thisFigurehead:isMelded() and thisShip:isDocked() then
        self._gaugeFigurehead:setValue(0,10000)
        self._gaugeFigurehead.front:echo("DOCKED", "SeaGreen", "c14" )
        self._gaugeFigurehead.back:echo("DOCKED", "SeaGreen", "c14" )
    elseif not thisFigurehead:isMelded() then
        self._gaugeFigurehead:setValue(0,10000)
        self._gaugeFigurehead.front:echo("UNMELDED", "gold", "cb14" )
        self._gaugeFigurehead.back:echo("UNMELDED", "gold", "cb14" )
    else
        _currentCharge = math.min( 10000, _currentCharge )
        self._gaugeFigurehead:setValue(_currentCharge,10000)
        self._gaugeFigurehead.front:echo(" FH: " .. _currentCharge, "white", "b10" )
        self._gaugeFigurehead.back:echo(" FH: " .. _currentCharge, "white", "b10" )
    end

end

----------------------------------------
--- Sets HP gauge and text.
-- @tparam string _currentHP Current HP
-- @tparam string _maxHP Maximum HP
-- @usage shipVitalsBox:setHealth(3723, 5200)
function ShipVitalsBox:setHealth(_currentHP, _maxHP)
    self._gaugeHealth:setValue(_currentHP, _maxHP)
    self._gaugeHealth.front:echo(" HP: " .. _currentHP .. " / " .. _maxHP, "white", "b10" )
    self._gaugeHealth.back:echo(" HP: " .. _currentHP .. " / " .. _maxHP, "white", "b10" )
end

----------------------------------------
--- Sets MP gauge and text.
-- @tparam string _currentMP Current MP
-- @tparam string _maxMP Maximum MP
-- @usage shipVitalsBox:setMana(4158, 4700)
function ShipVitalsBox:setMana(_currentMP, _maxMP)
    self._gaugeMana:setValue(_currentMP, _maxMP)
    self._gaugeMana.front:echo(" MP: " .. _currentMP .. " / " .. _maxMP, "white", "b10" )
    self._gaugeMana.back:echo(" MP: " .. _currentMP .. " / " .. _maxMP, "white", "b10" )
end

----------------------------------------
--- UNDOCUMENTED
-- @todo You should most *certainly* fix this!
function ShipVitalsBox:setSeamonsterGauge(currentHealth, maxHealth)
  self._gaugeSeamonster:setValue(currentHealth,maxHealth)
  self._gaugeSeamonster.front:echo(" Seamonster: " .. currentHealth .."/".. maxHealth, "white", "b10" )
  self._gaugeSeamonster.back:echo(" Seamonster: " .. currentHealth .."/".. maxHealth, "white", "b10" )
end

----------------------------------------
--- Sets target HP gauge and readout.
-- @tparam int _value Target HP (percent)
-- @usage shipVitalsBox:setTargetHealth(57)
function ShipVitalsBox:setTargetHealth(_value)
    self._gaugeTargetHP:setValue(_value,100)
    self._gaugeTargetHP.front:echo(" " .. _value .. "%","white","b12")
    self._gaugeTargetHP.back:echo(" " .. _value .. "%","white","b12")
end

----------------------------------------
--- Sets willpower gauge and text.
-- @tparam int _currentWP Current willpower
-- @tparam int _maxWP Maximum willpower
function ShipVitalsBox:setWillpower(_currentWP, _maxWP)
    self._gaugeWillpower:setValue(_currentWP, _maxWP)
    self._gaugeWillpower.front:echo(" WP: " .. round((_currentWP/_maxWP)*100).."%", "white", "b10" )
    self._gaugeWillpower.back:echo(" WP: " .. round((_currentWP/_maxWP)*100).."%", "white", "b10" )
end





--------------------------------------------------------------------------------
--- Label methods
-- For creating and Initialising ShipCompass object.
-- @section label
--------------------------------------------------------------------------------

----------------------------------------
--- Sets balance indicator.
-- @tparam Boolean _haveBalance True if on balance.
-- @usage shipVitalsBox:setBalance(true)
function ShipVitalsBox:setBalance(_haveBalance)
    if _haveBalance then
        self._labelBalance:setStyleSheet(gui.css.balanceGreen:getCSS())
    else
        self._labelBalance:setStyleSheet(gui.css.balanceRed:getCSS())
    end
end

----------------------------------------
--- UNDOCUMENTED
function ShipVitalsBox:setCrewBalanceLabel(_haveBalance)
    if _haveBalance then
        self._labelCrewBalance:setStyleSheet(gui.css.ship.balanceBlue)
    else
        self._labelCrewBalance:setStyleSheet(gui.css.ship.balanceRed)
    end
end

----------------------------------------
--- Sets equilibrium indicator.
-- @tparam boolean _haveEquilibrium True if on balance.
-- @usage shipVitalsBox:setEquilibrium(true)
function ShipVitalsBox:setEquilibrium(_haveEquilibrium)

    if _haveEquilibrium then
        self._labelEquilibrium:setStyleSheet(gui.css.ship.balanceGreen)
    else
        self._labelEquilibrium:setStyleSheet(gui.css.ship.balanceRed)
    end

end

----------------------------------------
--- Sets target name to be displayed.
-- @tparam string _name Set target name
-- @usage shipVitalsBox:setTargetName("orc48192")
function ShipVitalsBox:setTargetName(_name)
    self._labelTargetName:echo(" " .. _name)
end

----------------------------------------
--- Handles updating after SVO ship prompt.
--
-- There's no point in updating the entire thing every go, so let's just run this every time "svo done with prompt" is raised. Consistent and clean, with no external dependencies outside of svof.
-- @usage shipVitalsBox:setTargetName("orc48192")
function ShipVitalsBox:promptUpdate()

    if not svo.stats then return end
    if not loginComplete then return end

    self:setHealth   ( svo.stats.currenthealth,    svo.stats.maxhealth    )
    self:setMana     ( svo.stats.currentmana,      svo.stats.maxmana      )
    self:setEndurance( svo.stats.currentendurance, svo.stats.maxendurance )
    self:setWillpower( svo.stats.currentwillpower, svo.stats.maxwillpower )

    self:setBalance    ( svo.bals.balance     )
    self:setEquilibrium( svo.bals.equilibrium )

    local _isLowHP = not (svo.stats.hp >= 40) or (svo.stats.hp == 0)
    self:setLowHP(_isLowHP)

end





--------------------------------------------------------------------------------
--- Misc methods
-- For creating and Initialising ShipCompass object.
-- @section misc
--------------------------------------------------------------------------------

----------------------------------------
--- Shows or hides the whole object on the screen.
-- Just a shortcut for things like getting into my ship UI.
-- @tparam bool _visible True to show object, false to hide.
-- @usage shipVitalsBox:setVisible(true)
function ShipVitalsBox:setVisible(_visible)
    if _visible then
        self._container:show()
    else
        self._container:hide()
    end
end

----------------------------------------
--- Handles audio alerts for low HP. Plays sounds if true, and conditions within met.
-- Unlike VitalsBox counterpart, does *not* have flashers.
-- @tparam boolean _value True, if in low HP state.
-- @usage shipVitalsBox:setLowHP(true)
function ShipVitalsBox:setLowHP(_value)

    if _value and svo.stats.hp >= 0 and not soundCooldown then
        playSoundFile(cloudDir..[[Sounds\warning.wav]])
        soundCooldown = true
        tempTimer( 1.0, function() soundCooldown = false end )
    end

end

----------------------------------------
--- Updates all prompt-tagged elements in a single call.
-- Fires off of the event "svo done with prompt" by default.
-- @return None.
function ShipVitalsBox:update(...)

    if not svo.stats then return end
    if not loginComplete then return end
    if not jcs then return end

    self:setHealth(    svo.stats.currenthealth,    svo.stats.maxhealth    )
    self:setMana(      svo.stats.currentmana,      svo.stats.maxmana      )
    self:setEndurance( svo.stats.currentendurance, svo.stats.maxendurance )
    self:setWillpower( svo.stats.currentwillpower, svo.stats.maxwillpower )

    self:setBalance(     svo.bals.balance     )
    self:setEquilibrium( svo.bals.equilibrium )

    self:setLowHP(not (svo.stats.hp >= 40) or (svo.stats.hp == 0))

    --self:showTargetHP(svo.me.gametargethp)
    --self:setTargetName(svo.me.gametarget or "")
    --self:setTargetHealth(svo.me.gametargethp or 0 )

    self:setCrewBalanceLabel(self.crewBalance)
    self:setFigurehead(self.figurehead)

end
generated by LDoc 1.4.3 Last updated 2021-01-24 20:08:44