Jump to content

Tanker Respawn Script


St3v3f

Recommended Posts

Baron's Tanker (and AWACS) Respawn Script; Version 2.1

 

If you have a long-running mission and want to have unlimited tanker support without having to place multiple tankers in the mission or are tired of being afraid of idiots killing the tanker, this is for you.

 

The Tanker will respawn when one of three conditions is met:

  1. The Tanker is destroyed or damaged
  2. If it landed
  3. If it's fuel is low

 

Steps:

  1. Add Mist to the mission. The script is working with DCS 1.5.2 and Mist 4.0.57
  2. Place a tanker in the mission just like you always do. Give him waypoints and orbit instructions etc. It should have a RTB-Waypoint as well
  3. Create a MISSION START Trigger.
    For Action, select DO SCRIPT
    Paste the following code
    --RESPAWN SCRIPT; MISSION START -> DO SCRIPT
    local oldGroupRemovalTime = 2700 -- Time (seconds) after which old groups are removed
    
    barons_respawn_script = {}
    barons_respawn_script.predicate = function(groupName, lowFuelThreshold, lowHealthThreshold)
    if barons_respawn_script and barons_respawn_script[groupName] then
    	groupName = groupName .. barons_respawn_script[groupName]
    end
    local group = Group.getByName(groupName)
    if not group then
    	return true
    end
    
    group = group:getUnits()[1]
    if not group or Unit.inAir(group) == false then
    	return true
    end
    if group:getFuel() < lowFuelThreshold or group:getLife() / group:getLife0() < lowHealthThreshold then
    	return true
    end
    return false
    end
    
    barons_respawn_script.removeGroup = function(group)
    group = Group.getByName(group)
    if group then
    	trigger.action.deactivateGroup(group)
    end
    end
    
    barons_respawn_script.action = function(groupName)
    local oldName = groupName
    if barons_respawn_script[groupName] == nil then
    	barons_respawn_script[groupName] = 0
    else
    	oldName = groupName .. barons_respawn_script[groupName]
    end
    barons_respawn_script[groupName] = barons_respawn_script[groupName] + 1
    local newName = groupName .. barons_respawn_script[groupName]
    
    mist.scheduleFunction(barons_respawn_script.removeGroup, {oldName}, timer.getTime() + oldGroupRemovalTime)
    local group = Group.getByName(oldName)
    if group then
    	group = group:getController()
    	Controller.setCommand(group, {id = 'DeactivateBeacon', params = {}})
    	Controller.setTask(group, {id = 'NoTask', params = {}})
    end
    
    group = mist.getGroupData(groupName)
    group.route = { points = mist.getGroupRoute(groupName, true) }
    group.groupName = newName
    group.groupId = nil
    group.units[1].unitId = nil
    group.units[1].unitName = newName
    group.category = 'AIRPLANE'
    
    mist.dynAdd(group)
    
    group = Group.getByName(newName)
    group = group:getController()
    end
    
    barons_respawn_script.checkstate = function(groupName, lowFuelThreshold, lowHealthThreshold, oldresult)
    local newresult = barons_respawn_script.predicate(groupName, lowFuelThreshold, lowHealthThreshold)
    if oldresult == false and newresult == true then
    	barons_respawn_script.action(groupName)
    end
    
    mist.scheduleFunction(barons_respawn_script.checkstate, {groupName, lowFuelThreshold, lowHealthThreshold, newresult},  timer.getTime() + 1)
    end
    


  4. Change the variable at the top as you desire
  5. Add another DO SCRIPT action to the trigger
  6. Paste the following code
    --RESPAWN SCRIPT; MISSION START -> DO SCRIPT
    local groupName = 'Tanker_Texaco' -- Name of the group in the ME
    local lowFuelThreshold = 0.08 -- RTB when less then this amount of fuel
    local lowHealthThreshold = 0.75 -- RTB when less then this amount of health
    barons_respawn_script.checkstate(groupName, lowFuelThreshold, lowHealthThreshold, true)
    


  7. Edit the first three lines to fit into your mission
  8. Repeat steps 5 - 7 for other tankers


Edited by St3v3f
Fixed and Updated for DCS 1.5.2
  • Like 2

aka: Baron

[sIGPIC][/sIGPIC]

Link to comment
Share on other sites

I just had a thought.

Originally I said the triggerzone is optional. You have to keep in mind though, that you should always place a triggerzone over the takeoff airfield, if the tanker does a ramp start.

 

Reason:

If it respawns and starts to taxi, it's speed goes above the 1 m/s threshold. If he then holds short of the runway or stops on the runway, the speed drops back below that threshold and it will respawn again.

 

So either have a triggerzone over the airfield and/or increase the 'respawnSpeed' variable to above taxi speed

aka: Baron

[sIGPIC][/sIGPIC]

Link to comment
Share on other sites

Understood... with the current issues refueling online we have decided to keep the tankers out of this new mission for now. Once the refueling is fixed we'll get this added in and let you know how it goes!

 

Thanks again

 

S!

[sIGPIC][/sIGPIC]



104th Phoenix Wing Commander / Total Poser / Elitist / Hero / Chad

Link to comment
Share on other sites

Updated the script

Thanks to Grimes giving us Mist 3.1, it's all working lovely now. When the respawn is triggered, a new tanker will appear while the old one remains existent and will RTB

 

I also removed the issue with the speed and respawning when holding short by changing the condition from 'speed less than' to 'on ground'

aka: Baron

[sIGPIC][/sIGPIC]

Link to comment
Share on other sites

Sure, go ahead

 

Thankyou!

 

 

 

Please dont laugh, I know there is a professional way to achieve this one but this is my state of the art :P

and dont hesitate to point out a problem this code may cause.

 

Time based spawn (spawn group 5 minutes after it gets in the air)

1. Add following code in Condition

if lock_the_time == nil then
lock_the_time = 1
activation_time = timer.getTime() + 300 -- Change number accordingly to your mission routine
elseif timer.getTime() >= activation_time then
   lock_the_time = nil
   return true
end

2. Comment out the first controller section in "Do Action" to prevent the former group from RTBing after the spawn of the takeover. (do not comment out for tanker for obvious reason, overlapping TACAN and AR role)


Edited by verana_ss
small change
Link to comment
Share on other sites

It's not going to work like that because the switched condition trigger only performs the action once when the condition changes. try wrapping the dynadd into mist.scheduleFunction instead

 

 

Hi, St3v3f

Thank you for your advice.

 

Do you mean I should pack all the dynadd code into switch condition section using mist.scheduleFunction?

I'll try that way thanks again! :)

Link to comment
Share on other sites

no just wrap the call of dynadd in the action into scheduleFunction

 

 

is this right?

fucntion letspackthedynadd()
dynadd codes
end

local lets-respawn-planes mist.scheduleFunction(letspackthedynadd, {}, timer.GetTimer() + 300)

I could be totally wrong but wouldn't this take reservation of the next respawn when the original group lands or gets destroyed..?

 

 

My explanation was poor that might fail to make myself understood, this is the video of outcome of the additional code that I posted to achieve following objective

1- 24/7 CAP over at AO, cloned group succeed to CAP role when original leaves the area. <-- main purpose of this additional code

2- Respawn CAP group when it gets wiped out for whatever reason <-- Already achieved by Baron's code

3- Easy to alter, add and maintainance flights in mission editor <-- Already achieved by Baron's code

 

Link to comment
Share on other sites

  • 4 months later...

bumping this amazing script with a question.

 

St3v3- What I have happening with the script is I do have it set to respawn when it returns to a zone- but what's happening now is the tanker takes off, leaves the zone and then comes back in automatically... disregarding its route / waypoints... then lands or just flies around. In the meantime the next tanker in line takes off, returns... then flies around and respawns... each one does this.

 

Any idea what I'm doing wrong? If I disable that "return zone" will it still respawn just by stopping?

 

 

 

EDIT:

 

I have resolved it by disabling the zone- however having a zone enabled may be causing some issues now in 1.2.7. Just a heads up for those of you who may encounter the same problem.

 

Again- AWESOME script and thank you.


Edited by ENO

"ENO"

Type in anger and you will make the greatest post you will ever regret.

 

"Sweetest's" Military Aviation Art

Link to comment
Share on other sites

Basically to duplicate it all I've needed to do is set up a tanker on the ground, then a zone around it that it needs to leave and come back to (you know the script of course- I'm just saying that I understand the conditions). The waypoints essentially are set up to "switch waypoint" and the tanker shouldn't RTB until it's Bingo.

 

But instead, it just turns quickly and either goes to land or doesn't. Amazingly, no matter how big the zone is it just seems to want to leave and reenter. As soon as I removed the "trigger zone" and just left it as '' it worked fine (though granted I haven't actually seen one respawn that way yet.) "Worked fine" as in the tanker went about its duties.

 

EDIT:

 

Actually- just noticed I haven't been running these in a "switched condition" and instead have been running them "run once" (not knowing any better.)

 

It just occurred to me that I may need to do it in switched condition- and I went back to the first page and noticed the switched condition requirement.

 

I'm not sure if that would affect performance or not- or just prevent a respawn... but it is a mistake I made that may be contributing. FYI


Edited by ENO

"ENO"

Type in anger and you will make the greatest post you will ever regret.

 

"Sweetest's" Military Aviation Art

Link to comment
Share on other sites

Got around to it earlier than I thought.

 

local returnZone = 'Tanker_RTB' --Respawn if in this triggerzone. Set to '' if you don't want this
--Make sure that the tanker does not exit and reenter this zone, especially during departure

Are you absolutely positive about the second line?

 

I could reproduce the effect you describe when it does that. Reason is that the script sets the task of the old tanker to 'No Task' in order to remove it from the radio list and that seems to have the side effect of making it RTB, even though it happens in the middle of the flight plan.

 

Aside from that, I could not discover any issues with 1.2.7 and both Mist 3.1 and 3.2

 

On the note of the 'Once' trigger, you'll obviously need to use 'Switched Condition', otherwise it'll only run once and therefore spawn just one tanker and no more.

I made a quick try with Once and it did something weird. A respawn occured immediately after mission start. For that, I don't have an explanation but didn't pursue the matter because it forfeits the sense of using this script anyways.

 

I would regard the performance impact as negligible. I've been running scripts that are way more complex at intervals of 10ms and less without issues, running the lua predicate part once a second won't hurt the performance.


Edited by St3v3f

aka: Baron

[sIGPIC][/sIGPIC]

Link to comment
Share on other sites

Sorry- by performance I wasn't concerned about "performance" as in speed or anything like that as much as "function" of the script.

 

That must have seemed like an odd concern!

 

Thanks for checking into it!

 

About being certain about the second line... I'm not sure exactly what you mean- but when I was running it I did make sure the zone was spelled properly etc...


Edited by ENO

"ENO"

Type in anger and you will make the greatest post you will ever regret.

 

"Sweetest's" Military Aviation Art

Link to comment
Share on other sites

Well if you place the trigger zone over the airfield that the tanker takes off from and you don't make it large enough, it tends to depart the airfield and leave the zone. Then the flight plan kicks in, the tanker turns around and crosses over the airfield again to follow the route to the first waypoint.

Doing that, it enters the zone again, making the script think it is RTBing, causing it to spawn a new tanker, set 'no task' for the old which makes that one land then.

aka: Baron

[sIGPIC][/sIGPIC]

Link to comment
Share on other sites

  • 2 weeks later...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...