# This is a file describing a standard autoconfiguration, do not edit.
# See custom/sample.conf for a more generic template and explanation on the syntax.
name: ui_cu_autoconf_construct_flying
slots:
core:
class: CoreUnit
antigrav:
class: AntiGravityGeneratorUnit
warpdrive:
class: WarpDriveUnit
atmofueltank:
class: AtmoFuelContainer
select: all
spacefueltank:
class: SpaceFuelContainer
select: all
rocketfueltank:
class: RocketFuelContainer
select: all
gyro:
class: GyroUnit
shield:
class: ShieldGeneratorUnit
weapon:
class: WeaponUnit
select: manual
radar:
class: RadarPVPUnit
select: manual
handlers:
unit:
onStart:
lua: |
pitchInput = 0
rollInput = 0
yawInput = 0
brakeInput = 0
Nav = Navigator.new(system, core, unit)
Nav.axisCommandManager:setupCustomTargetSpeedRanges(axisCommandId.longitudinal, {1000, 5000, 10000, 20000, 30000})
Nav.axisCommandManager:setTargetGroundAltitude(4)
-- Parenting widget
parentingPanelId = system.createWidgetPanel("Docking")
parentingWidgetId = system.createWidget(parentingPanelId,"parenting")
system.addDataToWidget(unit.getDataId(),parentingWidgetId)
-- Combat stress widget
coreCombatStressPanelId = system.createWidgetPanel("Core combat stress")
coreCombatStressgWidgetId = system.createWidget(coreCombatStressPanelId,"core_stress")
system.addDataToWidget(core.getDataId(),coreCombatStressgWidgetId)
-- element widgets
-- For now we have to alternate between PVP and non-PVP widgets to have them on the same side.
_autoconf.displayCategoryPanel(weapon, weapon_size, L_TEXT("ui_lua_widget_weapon", "Weapons"), "weapon", true)
core.show()
_autoconf.displayCategoryPanel(radar, radar_size, L_TEXT("ui_lua_widget_periscope", "Periscope"), "periscope")
placeRadar = true
if atmofueltank_size > 0 then
_autoconf.displayCategoryPanel(atmofueltank, atmofueltank_size, L_TEXT("ui_lua_widget_atmofuel", "Atmo Fuel"), "fuel_container")
if placeRadar then
_autoconf.displayCategoryPanel(radar, radar_size, L_TEXT("ui_lua_widget_radar", "Radar"), "radar")
placeRadar = false
end
end
if spacefueltank_size > 0 then
_autoconf.displayCategoryPanel(spacefueltank, spacefueltank_size, L_TEXT("ui_lua_widget_spacefuel", "Space Fuel"), "fuel_container")
if placeRadar then
_autoconf.displayCategoryPanel(radar, radar_size, L_TEXT("ui_lua_widget_radar", "Radar"), "radar")
placeRadar = false
end
end
_autoconf.displayCategoryPanel(rocketfueltank, rocketfueltank_size, L_TEXT("ui_lua_widget_rocketfuel", "Rocket Fuel"), "fuel_container")
if placeRadar then -- We either have only rockets or no fuel tanks at all, uncommon for usual vessels
_autoconf.displayCategoryPanel(radar, radar_size, L_TEXT("ui_lua_widget_radar", "Radar"), "radar")
placeRadar = false
end
if antigrav ~= nil then antigrav.show() end
if warpdrive ~= nil then warpdrive.show() end
if gyro ~= nil then gyro.show() end
if shield ~= nil then shield.show() end
-- freeze the player in he is remote controlling the construct
if Nav.control.isRemoteControlled() == 1 then
system.freeze(1)
end
-- landing gear
-- make sure every gears are synchonized with the first
gearExtended = (Nav.control.isAnyLandingGearExtended() == 1) -- make sure it's a lua boolean
if gearExtended then
Nav.control.extendLandingGears()
else
Nav.control.retractLandingGears()
end
onStop:
lua: |
_autoconf.hideCategoryPanels()
if antigrav ~= nil then antigrav.hide() end
if warpdrive ~= nil then warpdrive.hide() end
if gyro ~= nil then gyro.hide() end
core.hide()
Nav.control.switchOffHeadlights()
system:
onFlush:
lua: |
-- constants: use 'myvar = defaultValue --export: description' to expose the variable in context menu
local pitchSpeedFactor = 0.8 --export: This factor will increase/decrease the player input along the pitch axis
(higher value may be unstable)
Valid values: Superior or equal to 0.01
local yawSpeedFactor = 1 --export: This factor will increase/decrease the player input along the yaw axis
(higher value may be unstable)
Valid values: Superior or equal to 0.01
local rollSpeedFactor = 1.5 --export: This factor will increase/decrease the player input along the roll axis
(higher value may be unstable)
Valid values: Superior or equal to 0.01
local brakeSpeedFactor = 3 --export: When braking, this factor will increase the brake force by brakeSpeedFactor * velocity
Valid values: Superior or equal to 0.01
local brakeFlatFactor = 1 --export: When braking, this factor will increase the brake force by a flat brakeFlatFactor * velocity direction>
(higher value may be unstable)
Valid values: Superior or equal to 0.01
local autoRoll = false --export: [Only in atmosphere]
When the pilot stops rolling, flight model will try to get back to horizontal (no roll)
local autoRollFactor = 2 --export: [Only in atmosphere]
When autoRoll is engaged, this factor will increase to strength of the roll back to 0
Valid values: Superior or equal to 0.01
local turnAssist = true --export: [Only in atmosphere]
When the pilot is rolling, the flight model will try to add yaw and pitch to make the construct turn better
The flight model will start by adding more yaw the more horizontal the construct is and more pitch the more vertical it is
local turnAssistFactor = 2 --export: [Only in atmosphere]
This factor will increase/decrease the turnAssist effect
(higher value may be unstable)
Valid values: Superior or equal to 0.01
local torqueFactor = 2 -- Force factor applied to reach rotationSpeed
(higher value may be unstable)
Valid values: Superior or equal to 0.01
-- validate params
pitchSpeedFactor = math.max(pitchSpeedFactor, 0.01)
yawSpeedFactor = math.max(yawSpeedFactor, 0.01)
rollSpeedFactor = math.max(rollSpeedFactor, 0.01)
torqueFactor = math.max(torqueFactor, 0.01)
brakeSpeedFactor = math.max(brakeSpeedFactor, 0.01)
brakeFlatFactor = math.max(brakeFlatFactor, 0.01)
autoRollFactor = math.max(autoRollFactor, 0.01)
turnAssistFactor = math.max(turnAssistFactor, 0.01)
-- final inputs
local finalPitchInput = pitchInput + system.getControlDeviceForwardInput()
local finalRollInput = rollInput + system.getControlDeviceYawInput()
local finalYawInput = yawInput - system.getControlDeviceLeftRightInput()
local finalBrakeInput = brakeInput
-- Axis
local worldVertical = vec3(core.getWorldVertical()) -- along gravity
local constructUp = vec3(core.getConstructWorldOrientationUp())
local constructForward = vec3(core.getConstructWorldOrientationForward())
local constructRight = vec3(core.getConstructWorldOrientationRight())
local constructVelocity = vec3(core.getWorldVelocity())
local constructVelocityDir = vec3(core.getWorldVelocity()):normalize()
local currentRollDeg = getRoll(worldVertical, constructForward, constructRight)
local currentRollDegAbs = math.abs(currentRollDeg)
local currentRollDegSign = utils.sign(currentRollDeg)
-- Rotation
local constructAngularVelocity = vec3(core.getWorldAngularVelocity())
local targetAngularVelocity = finalPitchInput * pitchSpeedFactor * constructRight
+ finalRollInput * rollSpeedFactor * constructForward
+ finalYawInput * yawSpeedFactor * constructUp
-- In atmosphere?
if worldVertical:len() > 0.01 and unit.getAtmosphereDensity() > 0.0 then
local autoRollRollThreshold = 1.0
-- autoRoll on AND currentRollDeg is big enough AND player is not rolling
if autoRoll == true and currentRollDegAbs > autoRollRollThreshold and finalRollInput == 0 then
local targetRollDeg = utils.clamp(0,currentRollDegAbs-30, currentRollDegAbs+30); -- we go back to 0 within a certain limit
if (rollPID == nil) then
rollPID = pid.new(autoRollFactor * 0.01, 0, autoRollFactor * 0.1) -- magic number tweaked to have a default factor in the 1-10 range
end
rollPID:inject(targetRollDeg - currentRollDeg)
local autoRollInput = rollPID:get()
targetAngularVelocity = targetAngularVelocity + autoRollInput * constructForward
end
local turnAssistRollThreshold = 20.0
-- turnAssist AND currentRollDeg is big enough AND player is not pitching or yawing
if turnAssist == true and currentRollDegAbs > turnAssistRollThreshold and finalPitchInput == 0 and finalYawInput == 0 then
local rollToPitchFactor = turnAssistFactor * 0.1 -- magic number tweaked to have a default factor in the 1-10 range
local rollToYawFactor = turnAssistFactor * 0.025 -- magic number tweaked to have a default factor in the 1-10 range
-- rescale (turnAssistRollThreshold -> 180) to (0 -> 180)
local rescaleRollDegAbs = ((currentRollDegAbs - turnAssistRollThreshold) / (180 - turnAssistRollThreshold)) * 180
local rollVerticalRatio = 0
if rescaleRollDegAbs < 90 then
rollVerticalRatio = rescaleRollDegAbs / 90
elseif rescaleRollDegAbs < 180 then
rollVerticalRatio = (180 - rescaleRollDegAbs) / 90
end
rollVerticalRatio = rollVerticalRatio * rollVerticalRatio
local turnAssistYawInput = - currentRollDegSign * rollToYawFactor * (1.0 - rollVerticalRatio)
local turnAssistPitchInput = rollToPitchFactor * rollVerticalRatio
targetAngularVelocity = targetAngularVelocity
+ turnAssistPitchInput * constructRight
+ turnAssistYawInput * constructUp
end
end
-- Engine commands
local keepCollinearity = 1 -- for easier reading
local dontKeepCollinearity = 0 -- for easier reading
local tolerancePercentToSkipOtherPriorities = 1 -- if we are within this tolerance (in%), we don't go to the next priorities
-- Rotation
local angularAcceleration = torqueFactor * (targetAngularVelocity - constructAngularVelocity)
local airAcceleration = vec3(core.getWorldAirFrictionAngularAcceleration())
angularAcceleration = angularAcceleration - airAcceleration -- Try to compensate air friction
Nav:setEngineTorqueCommand('torque', angularAcceleration, keepCollinearity, 'airfoil', '', '', tolerancePercentToSkipOtherPriorities)
-- Brakes
local brakeAcceleration = -finalBrakeInput * (brakeSpeedFactor * constructVelocity + brakeFlatFactor * constructVelocityDir)
Nav:setEngineForceCommand('brake', brakeAcceleration)
-- AutoNavigation regroups all the axis command by 'TargetSpeed'
local autoNavigationEngineTags = ''
local autoNavigationAcceleration = vec3()
local autoNavigationUseBrake = false
-- Longitudinal Translation
local longitudinalEngineTags = 'thrust analog longitudinal'
local longitudinalCommandType = Nav.axisCommandManager:getAxisCommandType(axisCommandId.longitudinal)
if (longitudinalCommandType == axisCommandType.byThrottle) then
local longitudinalAcceleration = Nav.axisCommandManager:composeAxisAccelerationFromThrottle(longitudinalEngineTags,axisCommandId.longitudinal)
Nav:setEngineForceCommand(longitudinalEngineTags, longitudinalAcceleration, keepCollinearity)
elseif (longitudinalCommandType == axisCommandType.byTargetSpeed) then
local longitudinalAcceleration = Nav.axisCommandManager:composeAxisAccelerationFromTargetSpeed(axisCommandId.longitudinal)
autoNavigationEngineTags = autoNavigationEngineTags .. ' , ' .. longitudinalEngineTags
autoNavigationAcceleration = autoNavigationAcceleration + longitudinalAcceleration
if (Nav.axisCommandManager:getTargetSpeed(axisCommandId.longitudinal) == 0 or -- we want to stop
Nav.axisCommandManager:getCurrentToTargetDeltaSpeed(axisCommandId.longitudinal) < - Nav.axisCommandManager:getTargetSpeedCurrentStep(axisCommandId.longitudinal) * 0.5) -- if the longitudinal velocity would need some braking
then
autoNavigationUseBrake = true
end
end
-- Lateral Translation
local lateralStrafeEngineTags = 'thrust analog lateral'
local lateralCommandType = Nav.axisCommandManager:getAxisCommandType(axisCommandId.lateral)
if (lateralCommandType == axisCommandType.byThrottle) then
local lateralStrafeAcceleration = Nav.axisCommandManager:composeAxisAccelerationFromThrottle(lateralStrafeEngineTags,axisCommandId.lateral)
Nav:setEngineForceCommand(lateralStrafeEngineTags, lateralStrafeAcceleration, keepCollinearity)
elseif (lateralCommandType == axisCommandType.byTargetSpeed) then
local lateralAcceleration = Nav.axisCommandManager:composeAxisAccelerationFromTargetSpeed(axisCommandId.lateral)
autoNavigationEngineTags = autoNavigationEngineTags .. ' , ' .. lateralStrafeEngineTags
autoNavigationAcceleration = autoNavigationAcceleration + lateralAcceleration
end
-- Vertical Translation
local verticalStrafeEngineTags = 'thrust analog vertical'
local verticalCommandType = Nav.axisCommandManager:getAxisCommandType(axisCommandId.vertical)
if (verticalCommandType == axisCommandType.byThrottle) then
local verticalStrafeAcceleration = Nav.axisCommandManager:composeAxisAccelerationFromThrottle(verticalStrafeEngineTags,axisCommandId.vertical)
Nav:setEngineForceCommand(verticalStrafeEngineTags, verticalStrafeAcceleration, keepCollinearity, 'airfoil', 'ground', '', tolerancePercentToSkipOtherPriorities)
elseif (verticalCommandType == axisCommandType.byTargetSpeed) then
local verticalAcceleration = Nav.axisCommandManager:composeAxisAccelerationFromTargetSpeed(axisCommandId.vertical)
autoNavigationEngineTags = autoNavigationEngineTags .. ' , ' .. verticalStrafeEngineTags
autoNavigationAcceleration = autoNavigationAcceleration + verticalAcceleration
end
-- Auto Navigation (Cruise Control)
if (autoNavigationAcceleration:len() > constants.epsilon) then
if (brakeInput ~= 0 or autoNavigationUseBrake or math.abs(constructVelocityDir:dot(constructForward)) < 0.95) -- if the velocity is not properly aligned with the forward
then
autoNavigationEngineTags = autoNavigationEngineTags .. ', brake'
end
Nav:setEngineForceCommand(autoNavigationEngineTags, autoNavigationAcceleration, dontKeepCollinearity, '', '', '', tolerancePercentToSkipOtherPriorities)
end
-- Rockets
Nav:setBoosterCommand('rocket_engine')
onUpdate:
lua: Nav:update()
onActionStart:
args: [gear]
lua: |
gearExtended = not gearExtended
if gearExtended then
Nav.control.extendLandingGears()
else
Nav.control.retractLandingGears()
end
onActionStart:
args: [light]
lua: |
if Nav.control.isAnyHeadlightSwitchedOn() == 1 then
Nav.control.switchOffHeadlights()
else
Nav.control.switchOnHeadlights()
end
onActionStart:
args: [forward]
lua: pitchInput = pitchInput - 1
onActionStop:
args: [forward]
lua: pitchInput = pitchInput + 1
onActionStart:
args: [backward]
lua: pitchInput = pitchInput + 1
onActionStop:
args: [backward]
lua: pitchInput = pitchInput - 1
onActionStart:
args: [left]
lua: rollInput = rollInput - 1
onActionStop:
args: [left]
lua: rollInput = rollInput + 1
onActionStart:
args: [right]
lua: rollInput = rollInput + 1
onActionStop:
args: [right]
lua: rollInput = rollInput - 1
onActionStart:
args: [straferight]
lua: Nav.axisCommandManager:updateCommandFromActionStart(axisCommandId.lateral, 1.0)
onActionStop:
args: [straferight]
lua: Nav.axisCommandManager:updateCommandFromActionStop(axisCommandId.lateral, -1.0)
onActionStart:
args: [strafeleft]
lua: Nav.axisCommandManager:updateCommandFromActionStart(axisCommandId.lateral, -1.0)
onActionStop:
args: [strafeleft]
lua: Nav.axisCommandManager:updateCommandFromActionStop(axisCommandId.lateral, 1.0)
onActionStart:
args: [up]
lua: |
Nav.axisCommandManager:deactivateGroundEngineAltitudeStabilization()
Nav.axisCommandManager:updateCommandFromActionStart(axisCommandId.vertical, 1.0)
onActionStop:
args: [up]
lua: |
Nav.axisCommandManager:updateCommandFromActionStop(axisCommandId.vertical, -1.0)
Nav.axisCommandManager:activateGroundEngineAltitudeStabilization(currentGroundAltitudeStabilization)
onActionStart:
args: [down]
lua: |
Nav.axisCommandManager:deactivateGroundEngineAltitudeStabilization()
Nav.axisCommandManager:updateCommandFromActionStart(axisCommandId.vertical, -1.0)
onActionStop:
args: [down]
lua: |
Nav.axisCommandManager:updateCommandFromActionStop(axisCommandId.vertical, 1.0)
Nav.axisCommandManager:activateGroundEngineAltitudeStabilization(currentGroundAltitudeStabilization)
onActionStart:
args: [groundaltitudeup]
lua: Nav.axisCommandManager:updateTargetGroundAltitudeFromActionStart(1.0)
onActionLoop:
args: [groundaltitudeup]
lua: Nav.axisCommandManager:updateTargetGroundAltitudeFromActionLoop(1.0)
onActionStart:
args: [groundaltitudedown]
lua: Nav.axisCommandManager:updateTargetGroundAltitudeFromActionStart(-1.0)
onActionLoop:
args: [groundaltitudedown]
lua: Nav.axisCommandManager:updateTargetGroundAltitudeFromActionLoop(-1.0)
onActionStart:
args: [yawright]
lua: yawInput = yawInput - 1
onActionStop:
args: [yawright]
lua: yawInput = yawInput + 1
onActionStart:
args: [yawleft]
lua: yawInput = yawInput + 1
onActionStop:
args: [yawleft]
lua: yawInput = yawInput - 1
onActionStart:
args: [brake]
lua: |
brakeInput = brakeInput + 1
local longitudinalCommandType = Nav.axisCommandManager:getAxisCommandType(axisCommandId.longitudinal)
if (longitudinalCommandType == axisCommandType.byTargetSpeed) then
local targetSpeed = Nav.axisCommandManager:getTargetSpeed(axisCommandId.longitudinal)
if (math.abs(targetSpeed) > constants.epsilon) then
Nav.axisCommandManager:updateCommandFromActionStart(axisCommandId.longitudinal, - utils.sign(targetSpeed))
end
end
onActionStop:
args: [brake]
lua: brakeInput = brakeInput - 1
onActionLoop:
args: [brake]
lua: |
local longitudinalCommandType = Nav.axisCommandManager:getAxisCommandType(axisCommandId.longitudinal)
if (longitudinalCommandType == axisCommandType.byTargetSpeed) then
local targetSpeed = Nav.axisCommandManager:getTargetSpeed(axisCommandId.longitudinal)
if (math.abs(targetSpeed) > constants.epsilon) then
Nav.axisCommandManager:updateCommandFromActionLoop(axisCommandId.longitudinal, - utils.sign(targetSpeed))
end
end
onActionStart:
args: [booster]
lua: Nav:toggleBoosters()
onActionStart:
args: [stopengines]
lua: Nav.axisCommandManager:resetCommand(axisCommandId.longitudinal)
onActionStart:
args: [speedup]
lua: Nav.axisCommandManager:updateCommandFromActionStart(axisCommandId.longitudinal, 5.0)
onActionLoop:
args: [speedup]
lua: Nav.axisCommandManager:updateCommandFromActionLoop(axisCommandId.longitudinal, 1.0)
onActionStart:
args: [speeddown]
lua: Nav.axisCommandManager:updateCommandFromActionStart(axisCommandId.longitudinal, -5.0)
onActionLoop:
args: [speeddown]
lua: Nav.axisCommandManager:updateCommandFromActionLoop(axisCommandId.longitudinal, -1.0)
onActionStart:
args: [antigravity]
lua: if antigrav ~= nil then antigrav.toggle() end