ship.lua
Ship = {}
function Ship:new(_instanceName)
Ship.__index = Ship
self = setmetatable({}, Ship)
local _anchorRaised = false
local _autorepairType = -1
local _course = false
local _crewBalance = true
local _deckFire = false
local _gangplankRaised = false
local _hullHealth = -1
local _instanceName = _instanceName
local _isLeaking = false
local _isRowing = false
local _isSinking = false
local _isTangled = false
local _promptType = -1
local _repairCooldown = false
local _repairingHull = false
local _repairingSails = false
local _requestedCourse = false
local _sailFire = false
local _sailHealth = 100
local _sailSetting = -1
local _seaState = -1
local _speed = -1
local _type = -1
local _windDirection = false
local _windSpeed = -1
_handlers = _handlers or {}
if _instanceName then
if _handlers[self._instanceName] then killAnonymousEventHandler(_handlers[self._instanceName]) end
_handlers[self._instanceName] = registerAnonymousEventHandler("ship prompt captured", "_handlers["..self._instanceName.."]")
end
return self
end
function Ship:runAutorepair()
if svo.conf.paused then return end
local _repairHull = false
local _repairSails = false
if self._autoRepair == 0 then return end
if self._repairCooldown then return end
if self._hullHealth < 95 and not self._repairingHull then
_repairHull = true
end
if self._sailHealth < 95 and not self._repairingSails and self._autoRepair == 2 then
_repairSails = true
end
if _repairHull and not _repairSails then
send("ship repair hull")
self._repairCooldown = true
tempTimer(1.0, function() self._repairCooldown = false end)
elseif not _repairHull and _repairSails then
send("ship repair sails")
self._repairCooldown = true
tempTimer(1.0, function() self._repairCooldown = false end)
elseif _repairHull and _repairSails then
send("ship repair all")
self._repairCooldown = true
tempTimer(1.0, function() self._repairCooldown = false end)
end
end
function Ship:getAutorepairType()
return self._autorepairType
end
function Ship:getRepairCooldown()
return self._repairCooldown
end
function Ship:isRepairingHull()
return self._repairingHull
end
function Ship:isRepairingSails()
return self._repairingSails
end
function Ship:setAutorepairType(autorepairType)
self._autorepairType = autoRepairType
end
function Ship:setRepairCooldown(_cooldown)
self._repairCooldown = _coolDown or false
if _cooldown then tempTimer(3, function() self:setRepairCooldown(false) end) end
end
function Ship:repairHull(_keepRepairing)
self._repairingHull = _keepRepairing or false
end
function Ship:repairSails(_keepRepairing)
self._repairingSails = _keepRepairing or false
end
function Ship:processPrompt(_promptString)
local _searchString = ""
if string.match(_promptString, "(%w+)") == "Sl" then
_searchString = "^= Sl (.*) %- hp (.*),Hl: (.*),Wd (%w+)@(%d+)kts,Cr/Sp (%w+)@(%d+),Sea (%w+)(.*)$"
elseif string.match(_promptString, "(%w+)") == "S" then
_searchString = "^= S(.*)@h(.*),H(.*),W<%-(%w*)@(%d*)kts,C/S%->(%w+)@(%d+),(%w+)(.*)"
end
local promptCaptures = { string.match( _promptString, _searchString ) }
local promptData = {} promptData._sailSetting = promptCaptures[1] or "error" promptData._sailHealth = promptCaptures[2] or "error" promptData._hullHealth = promptCaptures[3] or "error" promptData._windDirection = promptCaptures[4] or "error" promptData._windSpeed = promptCaptures[5] or "error" promptData._course = promptCaptures[6] or "error" promptData._speed = promptCaptures[7] or "error" promptData._seaState = promptCaptures[8] or "error"
promptData._rowing = false
if promptCaptures[9] then
if string.find(promptCaptures[9], "R%w?w") then
promptData._rowing = true
end
end
self:setCourse(promptData._course)
self:setHullHealth(promptData._hullHealth)
self:setRowing(promptData._rowing)
self:setSailHealth(promptData._sailHealth)
self:setSailSetting(promptData._sailSetting)
self:setSeaState(promptData._seaState)
self:setSpeed(promptData._speed)
self:setWindDirection(promptData._windDirection)
self:setWindSpeed(promptData._windSpeed)
promptData._course = self:getCourse()
promptData._hullHealth = self:getHullHealth()
promptData._rowing = self:isRowing()
promptData._sailHealth = self:getSailHealth()
promptData._sailSetting = self:getSailSetting()
promptData._seaState = self:getSeaState()
promptData._speed = self:getSpeed()
promptData._windDirection = self:getWindDirection()
promptData._windSpeed = self:getWindSpeed()
raiseEvent("ship done with prompt", promptData)
end
function Ship:isAnchorRaised()
return self._anchorRaised or false
end
function Ship:isDocked()
local _error = self:getError()
if (_error >= 1) or (_error <= -1) then
return false
end
if _error == 0 then
for k, v in ipairs(uacs:getWaypointTable()) do
if (v.type == "H") and (uacs:getEasting() == v.easting) and (uacs:getSouthing() == v.southing) then
return true
end
end
end
end
function Ship:isGangplankRaised()
return self._gangplankRaised or false
end
function Ship:isGrappled()
return false, {["nope"] = 0, }
end
function Ship:isLeaking()
return false
end
function Ship:isOnFire()
local _fireLevel = false
if self._deckFire then _fireLevel = (_fireLevel + 1) or 1 end
if self._sailFire then _fireLevel = (_fireLevel + 2) or 2 end
return _fireLevel
end
function Ship:isRowing()
return self._isRowing
end
function Ship:isSailing()
return (self._sailSetting > 0)
end
function Ship:isSinking()
return(false)
end
function Ship:isTurning()
return self_.requestedCourse
end
function Ship:getCourse()
return self._course
end
function Ship:getCrewBalance()
return self._crewBalance
end
function Ship:getHullHealth()
return self._hullHealth
end
function Ship:getSailHealth()
return self._sailHealth
end
function Ship:getSailSetting()
return self._sailSetting
end
function Ship:getSeaState()
return self._seaState
end
function Ship:getSpeed()
return self._speed
end
function Ship:getWindSpeed()
return self._windSpeed
end
function Ship:getWindDirection()
return self._windDirection
end
function Ship:setAnchorRaised(_isRaised)
self._anchorRaised = _isRaised or false
end
function Ship:setCrewBalance(_onBalance)
self._crewBalance = _onBalance or false
end
function Ship:setCourse(_captureGroup)
_captureGroup = string.lower(_captureGroup)
local _validDirs = {
["n"] = true,
["nne"] = true,
["ne"] = true,
["ene"] = true,
["e"] = true,
["ese"] = true,
["se"] = true,
["sse"] = true,
["s"] = true,
["ssw"] = true,
["sw"] = true,
["wsw"] = true,
["w"] = true,
["wnw"] = true,
["nw"] = true,
["nnw"] = true,
}
assert(_validDirs[_captureGroup], "Ship:setCourse() - _captureGroup (".._captureGroup..") not in _validDirs!")
self._course = _captureGroup
return true
end
function Ship:setGangplankRaised(_isRaised) self._gangplankRaised = _isRaised or false
end
function Ship:setHullHealth(_captureGroup)
local _captureGroup_type = type(_captureGroup)
if _captureGroup_type == "string" then
local _stringValues = {
["full"] = 100,
["++"] = 100,
["--"] = 0,
}
_captureGroup = _stringValues[_captureGroup] or tonumber(string.match(_captureGroup, "(%d+)%%")) or tonumber(_captureGroup)
_captureGroup_type = type(_captureGroup)
end
assert((_captureGroup_type == "number"), "Ship:setHullHealth() - _captureGroup (".._captureGroup..") failed to return number!")
assert((_captureGroup <= 100) and (_captureGroup >= 0), "Ship:setHullHealth() - _captureGroup (".._captureGroup..") is type number, but out of bounds!")
self._hullHealth = _captureGroup
return true, tostring(_captureGroup)
end
function Ship:setRequestedCourse(_captureGroup)
if type(_captureGroup) == "string" then
self._isRowing = string.find(_captureGroup, "Row") or string.find(_captureGroup, "Rw") or false
else
return false
end
end
function Ship:setRowing(_captureGroup)
if type(_captureGroup) == "boolean" then
self._isRowing = _captureGroup
elseif type(_captureGroup) == "string" then
self._isRowing = string.find(_captureGroup, "Row") or string.find(_captureGroup, "Rw") or false
else
return false
end
end
function Ship:setSailHealth(_captureGroup)
local _captureGroup_type = type(_captureGroup)
if _captureGroup_type == "string" then
_captureGroup = string.lower(_captureGroup)
local _stringValues = {
["full"] = 100,
["++"] = 100,
["--"] = 0,
}
_captureGroup = _stringValues[_captureGroup] or tonumber(string.match(_captureGroup, "(%d+)")) or tonumber(_captureGroup)
_captureGroup_type = type(_captureGroup)
end
assert((_captureGroup_type == "number"), "Ship:setSailHealth() - _captureGroup (".._captureGroup..") failed to return number!")
assert((_captureGroup <= 100) and (_captureGroup >= 0), "Ship:setSailHealth() - _captureGroup (".._captureGroup..") is type number, but out of bounds!")
self._sailHealth = _captureGroup
return true, _captureGroup
end
function Ship:setSailSetting(_captureGroup)
local _captureGroup = string.lower( _captureGroup )
local _captureGroup_type = type(_captureGroup)
if _captureGroup_type == "string" then
local _stringValues = {
["full"] = 100,
["furl"] = 0,
["++"] = 100,
["--"] = 0,
}
_captureGroup = _stringValues[_captureGroup] or tonumber(string.match(_captureGroup, "(%d+)%%")) or tonumber(_captureGroup)
_captureGroup_type = type(_captureGroup)
end
assert((_captureGroup_type == "number"), "Ship:setSailSetting() - _captureGroup (".._captureGroup..") failed to return number!")
assert((_captureGroup <= 100) and (_captureGroup >= 0), "Ship:setSailSetting() - _captureGroup (".._captureGroup..") is type number, but out of bounds!")
self._sailSetting = _captureGroup
return true, _captureGroup
end
function Ship:setSeaState(_captureGroup)
local _seaStateChart = {
["glassy"] = 1,
["smooth"] = 2,
["calm"] = 3,
["choppy"] = 4,
["whitecapped"] = 5,
["rough"] = 6,
["stormy"] = 7,
["tempestuous"] = 8,
["raging"] = 9,
["i"] = 1,
["ii"] = 2,
["iii"] = 3,
["iv"] = 4,
["v"] = 5,
["vi"] = 6,
["vii"] = 7,
["viii"] = 8,
["ix"] = 9,
}
_captureGroup = tonumber(_captureGroup) or _seaStateChart[string.lower(_captureGroup)] or - 1
assert((type(_captureGroup) == "number") and (_captureGroup <= 100) and (_captureGroup >= 0), "Ship:setSeaState() - _captureGroup (".._captureGroup..") not a real sea state!")
self._seaState = _captureGroup
return true, _captureGroup
end
function Ship:setSpeed(_captureGroup)
_captureGroup = tonumber(_captureGroup)
assert((type(_captureGroup) == "number") and (_captureGroup <= 100) and (_captureGroup >= 0), "Ship:setSpeed() - _captureGroup (".._captureGroup..") isn't a number, oops!")
self._speed = _captureGroup
end
function Ship:setWindDirection(_captureGroup)
_captureGroup = string.lower(_captureGroup)
local _validDirs = {
["n"] = true,
["nne"] = true,
["ne"] = true,
["ene"] = true,
["e"] = true,
["ese"] = true,
["se"] = true,
["sse"] = true,
["s"] = true,
["ssw"] = true,
["sw"] = true,
["wsw"] = true,
["w"] = true,
["wnw"] = true,
["nw"] = true,
["nnw"] = true,
}
assert(_validDirs[_captureGroup], "Ship:setWindDirection() - _captureGroup (".._captureGroup..") not in _validDirs!")
self._windDirection = _captureGroup
return true
end
function Ship:setWindSpeed (_captureGroup)
self._windSpeed = tonumber(_captureGroup)
end