Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Craft Docking Control Script
2017-06-18, 11:44 PM (This post was last modified: 2017-09-26 09:20 AM by MortusEclipse.)
Post: #1
Craft Docking Control Script
Based on the drone docking script by Albino, with some tinkering I came up with a smooth, clean and fully functional script without the logic traps that could prevent Albino's from working correctly. Perfect for drone and carrier based aircraft and eliminates many of the ACBs that would be needed to provide the same functionality.

NOTE: Updated the code to allow a range for health.

NOTE 2: Updated the code to allow a range for all quantifiable docking conditions. Also added Mothership Managed functionality for allow a mothership to control the launch and retrieval or it's craft. Mainly to take advantage of ZerothAngel's Docking Manager code. When the "MothershipManaged" setting is enabled craft will not launch during combat or dock after combat till docking is set by the mothership, but will still dock for ammo, fuel or repairs, though this is not smooth as the mothership may try to continue launching craft while docking.

Code:
-- Craft docking control script.

-- The purpose of this script is to replace the many ACBs you would otherwise require for your craft to dock with a companion craft under various situations.
-- The script will allow the craft to dock when no enemies are detected, or when ammo, fuel or health are low and only requires a LUA box and 2 ACBs.

-- Setup:
-- Place two ACBs somewhere on the craft you want to dock with a companion craft, and a LUA box with this script.
-- Set one ACB to "Dock With Mothership" and use "set custom controller inputs" to assign the input to the "up" key.
-- Set the other ACB to "Undock From Mothership" and use "set custom controller inputs" to assign the input to the "down" key.
-- NOTE: You may want to set a short delay on the undock ACB to allow time for repairs.
-- Select this craft on one of the docking station on the companion craft.
-- NOTE: Only one docking station retains it's selection when the craft is pulled out of play.

-- User Settings Variables --

-- Set if launch and retrieval of the craft after combat is managed by the mothership. (Set to "true" to allow the motherships to manage launch and retrieval, otherwise set to "false".)
MothershipManaged = true

-- Set if the craft will dock when no enemies are detected. *Function overridden if Mothership Managed is set to "true".* (Set to "true" to dock when no enemies are detected, otherwise set to "false".)
ReturnWhenIdle = true

-- Set if the craft will dock when ammo is low. (Set to "true" to dock when ammo is low, otherwise set to "false".)
ReturnForAmmo = true
-- Set the decimal fraction at which the craft will dock when ammo reaches or drops below this level. (Set between 0.1 and 0.9)
AmmoToReturn = 0.1
-- Set the decimal fraction at which the craft will leave when ammo reaches this level. (Set between 0.1 and 1.0, this value should be set higher than "AmmoToReturn")
AmmoToLeave = 1.0

-- Set if the craft will dock when fuel is low. (Set to "true" to dock when ammo is low, otherwise set to "false".)
ReturnForFuel = true
-- Set the decimal fraction at which the craft will dock if fuel reaches or drops below this level. (Set between 0.1 and 0.9)
FuelToReturn = 0.1
-- Set the decimal fraction at which the craft will leave when fuel reaches this level. (Set between 0.1 and 1.0, this value should be set higher than "FuelToReturn")
FuelToLeave = 1.0

-- Set if the craft will dock when health is low. (Set to "true" to dock when ammo is low, otherwise set to "false".)
ReturnForHealth = true
-- Set the decimal fraction at which the craft will dock if health reaches or drops below this level. (Set between 0.1 and 0.9)
HealthToReturn = 0.7
-- Set the decimal fraction at which the craft will leave when health reaches this level. (Set between 0.1 and 1.0, this value should be set higher than "HealthToReturn")
HealthToLeave = 1.0

-- Machine Code --
Launch = false

function ShouldDock(I)

  I:ClearLogs()
  if ( MothershipManaged and not Launch ) and ( I:GetNumberOfTargets(0) ~= 0 and not I:IsDocked() ) then
    I:Log('Launch')
    Launch = true
  end
  if MothershipManaged and not Launch then
    I:Log('Holding till launch.')
    return I:IsDocked()
  elseif ( ReturnWhenIdle or MothershipManaged) and I:GetNumberOfTargets(0) == 0 then
    if MothershipManaged then
      Launch = false
    else
      I:Log('Docking because idle.')
      return true
    end
  elseif ReturnForAmmo and ( ( I:GetAmmoFraction() <= AmmoToReturn and not I:IsDocked() ) or ( I:GetAmmoFraction() < AmmoToLeave and I:IsDocked() ) ) then
    I:Log('Docking for ammo.')
    return true
  elseif ReturnForFuel == true and ( ( I:GetFuelFraction() <= FuelToReturn and not I:IsDocked() ) or ( I:GetFuelFraction() < FuelToLeave and I:IsDocked() ) ) then
    I:Log('Docking for fuel.')
    return true
  elseif ReturnForHealth and ( ( I:GetHealthFraction() <= HealthToReturn and not I:IsDocked() ) or ( I:GetHealthFraction() < HealthToLeave and I:IsDocked() ) ) then
    I:Log('Docking for repairs.')
    return true
  else
    return false
  end
end

function Update(I)

  if I:GetNumberOfMainframes() > 0 then
    if ShouldDock(I) then
      I:Log('Engage dock.')
      I:RequestComplexControllerStimulus(11)
    else
      I:Log('Release dock.')
      I:RequestComplexControllerStimulus(12)
    end
  end
end
Find all posts by this user
Quote this message in a reply
2017-06-20, 11:17 PM
Post: #2
RE: Craft Docking Control Script
Cool.

Knowledge is knowing a tomato is a fruit wisdom is not putting it in a fruit salad - anonymous

Don't believe everything you think - anounymous
Find all posts by this user
Quote this message in a reply
2017-08-27, 04:18 AM
Post: #3
RE: Craft Docking Control Script
think you can make a version that shuts off the crafts engines aswell?
Find all posts by this user
Quote this message in a reply
2017-08-27, 08:48 AM
Post: #4
RE: Craft Docking Control Script
TBH, this is really a beginning effort. I am not really to knowledgeable yet. I am mostly learning by pulling other peoples code apart that is not working/working how I like and trying to make it so. If you are referring to fuel engines, steam boilers, etc then at current the easiest solution would be adding two additional ACBs set up identical to the docking ACBs but that would control the engines on the appropriate complex controller command. Of by engines you mean propulsion though it seems to me that this would be best controlled by the actual AI it's self. I may be wrong though.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)