Skip to content

Instantly share code, notes, and snippets.

@TDC-bob
Created June 19, 2013 13:41
Show Gist options
  • Save TDC-bob/5814417 to your computer and use it in GitHub Desktop.
Save TDC-bob/5814417 to your computer and use it in GitHub Desktop.
Transport script
-- Original script by Deck. Modified by Angel(Geloxo)
-- To enable units to use the script set them in the list of pilots
-- Compatible with air and ground units
-- Units will drop troops types based on the coalition they belong to
-- Pickup and drop smoke markers can be enabled or disabled
-- Human pilots need to use F10 radio action to load and unload troops when inside the triggers
-- AI pilots will automatically load troops when inside the pickup triggers
-- AI pilots will automatically unload troops when inside the drop triggers if they are close enough to trigger (500m for ground units and 10km for air units) to allow wider air drop areas for big battles but allow ground units to enter triggers without dropping
-- Dropped troops will move to a random point within 200-400m from drop triggers to avoid crowding the drop zones
groupCounter = 100
unitCounter = 100
function DropoffGroupDirect(count, radius, xCenter, yCenter, xDest, yDest, groupside)
-- random distance for troops to move
local random1 = math.random(200,400)
local random2 = math.random(200,400)
local group = {
["visible"] = false,
["taskSelected"] = true,
["groupId"] = groupCounter,
["hidden"] = false,
["units"] = {},
["y"] = yCenter,
["x"] = xCenter,
["name"] = "GroupName" .. groupCounter,
["start_time"] = 0,
["task"] = "Ground Nothing",
["route"] = {
["points"] =
{
[1] =
{
["alt"] = 41,
["type"] = "Turning Point",
["ETA"] = 0,
["alt_type"] = "BARO",
["formation_template"] = "",
["y"] = yCenter,
["x"] = xCenter,
["ETA_locked"] = true,
["speed"] = 5.5555555555556,
["action"] = "Diamond",
["task"] =
{
["id"] = "ComboTask",
["params"] =
{
["tasks"] =
{
}, -- end of ["tasks"]
}, -- end of ["params"]
}, -- end of ["task"]
["speed_locked"] = false,
}, -- end of [1]
[2] =
{
["alt"] = 54,
["type"] = "Turning Point",
["ETA"] = 52.09716824195,
["alt_type"] = "BARO",
["formation_template"] = "",
["y"] = yDest + random1 - random2,
["x"] = xDest + random1 - random2,
["ETA_locked"] = false,
["speed"] = 5.5555555555556,
["action"] = "Diamond",
["task"] =
{
["id"] = "ComboTask",
["params"] =
{
["tasks"] =
{
}, -- end of ["tasks"]
}, -- end of ["params"]
}, -- end of ["task"]
["speed_locked"] = false,
}, -- end of [2]
}, -- end of ["points"]
}, -- end of ["route"]
}
groupCounter = groupCounter + 1
for i = 1,count do
local angle = math.pi * 2 * (i-1) / count
local xofs = math.cos(angle) * radius
local yofs = math.sin(angle) * radius
local unitType = "Soldier AK"
if groupside == 2 then
unitType = "Soldier M4"
if i <= 2 then
unitType = "Soldier M249"
end
else
if groupside == 1 then
unitType = "Infantry AK"
if i <= 2 then
unitType = "Paratrooper AKS-74"
end
end
end
group.units[i] = NewSoldierUnit(xCenter + xofs, yCenter + yofs, angle, unitType)
end
return group
end
function NewSoldierUnit(x, y, heading, unitType)
local unit = {
["y"] = y,
["type"] = unitType,
["name"] = "Unitname" .. unitCounter,
["unitId"] = unitCounter,
["heading"] = heading,
["playerCanDrive"] = true,
["skill"] = "Excellent",
["x"] = x,
}
unitCounter = unitCounter + 1
return unit
end
-- ****************** Enable Smoke markers *********************
smokepick = true
smokedrop = true
-- ***************** Pickup and dropoff zones *****************
PickupZones = {
[1] = {
ZoneName = "PickupZone1",
SmokeColor = trigger.smokeColor.Blue,
},
[2] = {
ZoneName = "PickupZone2",
SmokeColor = trigger.smokeColor.Blue,
},
}
DropoffZones = {
[1] = {
ZoneName = "DropoffZone1",
SmokeColor = trigger.smokeColor.Red,
DropFunction = DropoffGroupDirect
},
[2] = {
ZoneName = "DropoffZone2",
SmokeColor = trigger.smokeColor.Red,
DropFunction = DropoffGroupDirect
},
}
-- ********************** Human pilot names ************************
function AddRadioCommands(arg, time)
AddRadioCommand("helicargo1")
AddRadioCommand("helicargo2")
AddRadioCommand("helicargo3")
AddRadioCommand("helicargo4")
AddRadioCommand("helicargo5")
AddRadioCommand("helicargo6")
AddRadioCommand("helicargo7")
AddRadioCommand("helicargo8")
AddRadioCommand("helicargo9")
AddRadioCommand("helicargo10")
AddRadioCommand("helicargo11")
AddRadioCommand("helicargo12")
AddRadioCommand("helicargo13")
AddRadioCommand("helicargo14")
AddRadioCommand("helicargo15")
AddRadioCommand("helicargo16")
AddRadioCommand("helicargo17")
AddRadioCommand("helicargo18")
AddRadioCommand("helicargo19")
AddRadioCommand("helicargo20")
AddRadioCommand("helicargo21")
AddRadioCommand("helicargo22")
AddRadioCommand("helicargo23")
AddRadioCommand("helicargo24")
AddRadioCommand("helicargo25")
return time + 5
end
-- ********************** AI pilot names ************************
function AddRadioCommandsAI(arg, time)
AddRadioCommandAI("transport1")
AddRadioCommandAI("transport2")
AddRadioCommandAI("transport3")
AddRadioCommandAI("transport4")
AddRadioCommandAI("transport5")
AddRadioCommandAI("transport6")
AddRadioCommandAI("transport7")
AddRadioCommandAI("transport8")
AddRadioCommandAI("transport9")
AddRadioCommandAI("transport10")
AddRadioCommandAI("transport11")
AddRadioCommandAI("transport12")
AddRadioCommandAI("transport13")
AddRadioCommandAI("transport14")
AddRadioCommandAI("transport15")
AddRadioCommandAI("transport16")
AddRadioCommandAI("transport17")
AddRadioCommandAI("transport18")
AddRadioCommandAI("transport19")
AddRadioCommandAI("transport20")
AddRadioCommandAI("transport21")
AddRadioCommandAI("transport22")
AddRadioCommandAI("transport23")
AddRadioCommandAI("transport24")
AddRadioCommandAI("transport25")
return time + 5
end
-- ***********************************************************
function SpawnSmoke(smokeX, smokeY, smokeColor)
local pos2 = { x = smokeX, y = smokeY }
local alt = land.getHeight(pos2)
local pos3 = {x=pos2.x, y=alt, z=pos2.y}
trigger.action.smoke(pos3, smokeColor)
end
function SmokeTimer(arg, time)
if smokepick == true then
for i=1,#PickupZones do
local zone = trigger.misc.getZone(PickupZones[i].ZoneName)
SpawnSmoke(zone.point.x, zone.point.z, PickupZones[i].SmokeColor)
end
end
if smokedrop == true then
for i=1,#DropoffZones do
local zone = trigger.misc.getZone(DropoffZones[i].ZoneName)
SpawnSmoke(zone.point.x, zone.point.z, DropoffZones[i].SmokeColor)
end
end
return time + 270
end
UnitStateTable = {}
UnitStateTableAI = {}
function UnitRadioCommand(unitName)
local unit = Unit.getByName(unitName)
if unit == nil then
UnitStateTable[unitName] = false
return
end
local unitId = unit:getID()
local group = unit:getGroup()
local groupName = group:getName()
local groupside = group:getCoalition()
local playerName = unit:getPlayerName()
if UnitStateTable[unitName] == nil then
UnitStateTable[unitName] = false
end
local pickupZone = UnitInAnyPickupZone(unit)
local dropoffZone = UnitInAnyDropoffZone(unit)
if pickupZone ~= nil then
UnitStateTable[unitName] = true
trigger.action.outText("Transport loaded troops on board", 4)
trigger.action.outText(playerName .. " loaded troops on board", 5)
else
if dropoffZone ~= nil then
if UnitStateTable[unitName] == true then
local unitpos = unit:getPoint()
local triggerZone = trigger.misc.getZone(dropoffZone.ZoneName)
local newGroup = dropoffZone.DropFunction(10, 15, unitpos.x, unitpos.z, triggerZone.point.x, triggerZone.point.z, groupside)
if groupside == 2 then
coalition.addGroup(country.id.USA, Group.Category.GROUND, newGroup)
elseif groupside == 1 then
coalition.addGroup(country.id.RUSSIA, Group.Category.GROUND, newGroup)
else
coalition.addGroup(country.id.INSURGENTS, Group.Category.GROUND, newGroup)
end
UnitStateTable[unitName] = false
trigger.action.outText("Transport dropped troops", 4)
trigger.action.outText(playerName .. " dropped troops", 5)
else
trigger.action.outText("Transport didn't have any troops to drop", 4)
trigger.action.outText(playerName .. " didn't have any troops to drop", 5)
end
else
-- landed in nomands
trigger.action.outText("Transport isn't in a pickup or dropoff zone", 4)
trigger.action.outText(playerName .. " isn't in a pickup or dropoff zone", 5)
end
end
end
function UnitRadioCommandAI(unitName)
local unit = Unit.getByName(unitName)
if unit == nil then
UnitStateTableAI[unitName] = false
return
end
local unitId = unit:getID()
local group = unit:getGroup()
local groupName = group:getName()
local groupside = group:getCoalition()
if UnitStateTableAI[unitName] == nil then
UnitStateTableAI[unitName] = false
end
local pickupZone = UnitInAnyPickupZone(unit)
local dropoffZone = UnitInAnyDropoffZone(unit)
local neardropZone = FindNearestDropoffZone(unit)
if pickupZone ~= nil then
UnitStateTableAI[unitName] = true
--trigger.action.outText("AI transport loaded troops on board", 5)
else
if (dropoffZone ~= nil and neardropZone ~= nil) then
if UnitStateTableAI[unitName] == true then
local unitpos = unit:getPoint()
local triggerZone = trigger.misc.getZone(dropoffZone.ZoneName)
local newGroup = dropoffZone.DropFunction(6, 15, unitpos.x, unitpos.z, triggerZone.point.x, triggerZone.point.z, groupside)
if groupside == 2 then
coalition.addGroup(country.id.USA, Group.Category.GROUND, newGroup)
elseif groupside == 1 then
coalition.addGroup(country.id.RUSSIA, Group.Category.GROUND, newGroup)
else
coalition.addGroup(country.id.INSURGENTS, Group.Category.GROUND, newGroup)
end
UnitStateTableAI[unitName] = false
--trigger.action.outText("AI transport dropped troops", 5)
end
end
end
end
RadioCommandTable = {}
RadioCommandTableAI = {}
function AddRadioCommand(unitName)
if RadioCommandTable[unitName] == nil then
local unit = Unit.getByName(unitName)
if unit == nil then
return
end
local group = unit:getGroup()
if group == nil then
return
end
local gid = group:getID()
missionCommands.addCommandForGroup(gid, "Load/Unload Troops", nil, UnitRadioCommand, unitName)
RadioCommandTable[unitName] = true
end
end
function AddRadioCommandAI(unitName)
if RadioCommandTableAI[unitName] == nil then
local unit = Unit.getByName(unitName)
if unit == nil then
return
end
local group = unit:getGroup()
if group == nil then
return
end
local gid = group:getID()
RadioCommandTableAI[unitName] = true
end
UnitRadioCommandAI(unitName)
end
function GetDistance(xUnit, yUnit, xZone, yZone)
local xDiff = xUnit - xZone
local yDiff = yUnit - yZone
return math.sqrt(xDiff * xDiff + yDiff * yDiff)
end
function FindNearestDropoffZone(unit)
local minDist = 10000
local minZone = nil
local unitpos = unit:getPoint()
local group = unit:getGroup()
local gtype = group:getCategory()
-- min distance to trigger dropping for AI ground vehicles
if gtype == 2 then
minDist = 500
end
for i=1,#DropoffZones do
local zone = DropoffZones[i]
local triggerZone = trigger.misc.getZone(zone.ZoneName)
local dist = GetDistance(unitpos.x, unitpos.z, triggerZone.point.x, triggerZone.point.z)
if dist < minDist then
minDist = dist
minZone = zone
end
end
return minZone
end
do
timer.scheduleFunction(SmokeTimer, nil, timer.getTime() + 5)
timer.scheduleFunction(AddRadioCommands, nil, timer.getTime() + 5)
timer.scheduleFunction(AddRadioCommandsAI, nil, timer.getTime() + 5)
end
function UnitInAnyPickupZone(unit)
for i=1,#PickupZones do
if UnitInZone(unit, PickupZones[i]) then
return PickupZones[i]
end
end
return nil
end
function UnitInAnyDropoffZone(unit)
for i=1,#DropoffZones do
if UnitInZone(unit, DropoffZones[i]) then
return DropoffZones[i]
end
end
return nil
end
function UnitInZone(unit, zone)
if unit:inAir() then
return false
end
local triggerZone = trigger.misc.getZone(zone.ZoneName)
local group = unit:getGroup()
local groupid = group:getID()
local unitpos = unit:getPoint()
local xDiff = unitpos.x - triggerZone.point.x
local yDiff = unitpos.z - triggerZone.point.z
local dist = math.sqrt(xDiff * xDiff + yDiff * yDiff)
if dist > triggerZone.radius then
return false
end
return true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment