Advertisement
Guest User

Untitled

a guest
May 14th, 2013
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.79 KB | None | 0 0
  1.  
  2. groupCounter = 100
  3. unitCounter = 100
  4.  
  5. function DropoffGroupDirect(count, radius, xCenter, yCenter, xDest, yDest)
  6.     local group = {
  7.         ["visible"] = false,
  8.         ["taskSelected"] = true,
  9.         ["groupId"] = groupCounter,
  10.         ["hidden"] = false,
  11.         ["units"] = {},
  12.         ["y"] = yCenter,
  13.         ["x"] = xCenter,
  14.         ["name"] = "GroupName" .. groupCounter,
  15.         ["start_time"] = 0,
  16.         ["task"] = "Ground Nothing",
  17.         ["route"] = {
  18.             ["points"] =
  19.             {
  20.                 [1] =
  21.                 {
  22.                     ["alt"] = 41,
  23.                     ["type"] = "Turning Point",
  24.                     ["ETA"] = 0,
  25.                     ["alt_type"] = "BARO",
  26.                     ["formation_template"] = "",
  27.                     ["y"] = yCenter,
  28.                     ["x"] = xCenter,
  29.                     ["ETA_locked"] = true,
  30.                     ["speed"] = 5.5555555555556,
  31.                     ["action"] = "Diamond",
  32.                     ["task"] =
  33.                     {
  34.                         ["id"] = "ComboTask",
  35.                         ["params"] =
  36.                         {
  37.                             ["tasks"] =
  38.                             {
  39.                             }, -- end of ["tasks"]
  40.                         }, -- end of ["params"]
  41.                     }, -- end of ["task"]
  42.                     ["speed_locked"] = false,
  43.                 }, -- end of [1]
  44.                 [2] =
  45.                 {
  46.                     ["alt"] = 54,
  47.                     ["type"] = "Turning Point",
  48.                     ["ETA"] = 52.09716824195,
  49.                     ["alt_type"] = "BARO",
  50.                     ["formation_template"] = "",
  51.                     ["y"] = yDest,
  52.                     ["x"] = xDest,
  53.                     ["ETA_locked"] = false,
  54.                     ["speed"] = 5.5555555555556,
  55.                     ["action"] = "Diamond",
  56.                     ["task"] =
  57.                     {
  58.                         ["id"] = "ComboTask",
  59.                         ["params"] =
  60.                         {
  61.                             ["tasks"] =
  62.                             {
  63.                             }, -- end of ["tasks"]
  64.                         }, -- end of ["params"]
  65.                     }, -- end of ["task"]
  66.                     ["speed_locked"] = false,
  67.                 }, -- end of [2]
  68.             }, -- end of ["points"]
  69.         }, -- end of ["route"]
  70.     }
  71.  
  72.     groupCounter = groupCounter + 1
  73.    
  74.     for i = 1,count do  
  75.         local angle = math.pi * 2 * (i-1) / count
  76.         local xofs = math.cos(angle) * radius
  77.         local yofs = math.sin(angle) * radius
  78.         local unitType = "Soldier M4"
  79.         if i < 4 then
  80.             unitType = "Soldier M249"
  81.         end
  82.         group.units[i] = NewSoldierUnit(xCenter + xofs, yCenter + yofs, angle, unitType)
  83.        
  84.     end
  85.    
  86.     return group
  87. end
  88.  
  89. function NewSoldierUnit(x, y, heading, unitType)
  90.     local unit = {
  91.         ["y"] = y,
  92.         ["type"] = unitType,
  93.         ["name"] = "Unitname" .. unitCounter,
  94.         ["unitId"] = unitCounter,
  95.         ["heading"] = heading,
  96.         ["playerCanDrive"] = true,
  97.         ["skill"] = "Excellent",
  98.         ["x"] = x,
  99.     }
  100.    
  101.     unitCounter = unitCounter + 1
  102.    
  103.     return unit    
  104. end
  105.  
  106.  
  107. PickupZones = {
  108.     [1] = {
  109.         ZoneName = "PickupZone",
  110.         SmokeColor = trigger.smokeColor.Blue,
  111.     },
  112. }
  113.  
  114. DropoffZones = {
  115.     [1] = {
  116.         Name = "Nuclear Plant (Red)",
  117.         ZoneName = "DropoffZone1",
  118.         SmokeColor = trigger.smokeColor.Red,
  119.         DropFunction = DropoffGroupDirect
  120.     },
  121.     [2] = {
  122.         Name = "Rail Station (Orange)",
  123.         ZoneName = "DropoffZone2",
  124.         SmokeColor = trigger.smokeColor.Orange,
  125.         DropFunction = DropoffGroupDirect
  126.     },
  127.     [3] = {
  128.         Name = "City Center (Green)",
  129.         ZoneName = "DropoffZone3",
  130.         SmokeColor = trigger.smokeColor.Green,
  131.         DropFunction = DropoffGroupDirect
  132.     },
  133.     [4] = {
  134.         Name = "Hospitals (Blue)",
  135.         ZoneName = "DropoffZone4",
  136.         SmokeColor = trigger.smokeColor.Blue,
  137.         DropFunction = DropoffGroupDirect
  138.     },
  139. }
  140.  
  141. function SpawnSmoke(smokeX, smokeY, smokeColor)
  142.     local pos2 = { x = smokeX, y = smokeY }
  143.     local alt = land.getHeight(pos2)
  144.     local pos3 = {x=pos2.x, y=alt, z=pos2.y}
  145.     trigger.action.smoke(pos3, smokeColor)
  146. end
  147.  
  148. function SmokeTimer(arg, time)    
  149.     for i=1,#PickupZones do
  150.        local zone = trigger.misc.getZone(PickupZones[i].ZoneName)
  151.        SpawnSmoke(zone.point.x, zone.point.z, PickupZones[i].SmokeColor)
  152.     end
  153.    
  154.     for i=1,#DropoffZones do
  155.        local zone = trigger.misc.getZone(DropoffZones[i].ZoneName)
  156.        SpawnSmoke(zone.point.x, zone.point.z, DropoffZones[i].SmokeColor)
  157.     end
  158.    
  159.     return time + 270
  160. end
  161.  
  162. UnitStateTable = {}
  163.  
  164. function UnitRadioCommand(unitName)
  165.     local unit = Unit.getByName(unitName)
  166.    
  167.     if unit == nil then
  168.         UnitStateTable[unitName] = false
  169.         return
  170.     end
  171.    
  172.     local unitId = unit:getID()
  173.     local group = unit:getGroup()
  174.     local groupName = group:getName()
  175.     local playerName = unit:getPlayerName()
  176.    
  177.     if UnitStateTable[unitName] == nil then
  178.         UnitStateTable[unitName] = false
  179.     end
  180.    
  181.     local pickupZone = UnitInAnyPickupZone(unit)
  182.     local dropoffZone = UnitInAnyDropoffZone(unit)
  183.    
  184.     if pickupZone ~= nil then
  185.         UnitStateTable[unitName] = true
  186.         trigger.action.outText(playerName .. " (" .. groupName .. ") has 11 troops on board.", 10)
  187.     else
  188.         if dropoffZone ~= nil then
  189.             if UnitStateTable[unitName] == true then
  190.                 local unitpos = unit:getPoint()
  191.                 local triggerZone = trigger.misc.getZone(dropoffZone.ZoneName)
  192.                 local newGroup = dropoffZone.DropFunction(11, 15, unitpos.x, unitpos.z, triggerZone.point.x, triggerZone.point.z)
  193.                 coalition.addGroup(country.id.USA, Group.Category.GROUND, newGroup)
  194.                
  195.                 UnitStateTable[unitName] = false
  196.                
  197.                 trigger.action.outText(playerName .. " (" .. groupName .. ") just dropped 11 troops.", 10)
  198.             else
  199.                 trigger.action.outText(playerName .. " (" .. groupName .. ") didn't have any troops to drop.", 10)
  200.             end
  201.         else
  202.             -- landed in nomands
  203.             trigger.action.outText(playerName .. " (" .. groupName .. ") isn't in a pickup or dropoff zone.", 10)
  204.         end
  205.     end      
  206. end
  207.  
  208. RadioCommandTable = {}
  209.  
  210. function AddRadioCommand(unitName)
  211.     if RadioCommandTable[unitName] == nil then
  212.         local unit = Unit.getByName(unitName)
  213.         if unit == nil then
  214.             return
  215.         end
  216.        
  217.         local group = unit:getGroup()
  218.         if group == nil then
  219.             return
  220.         end
  221.        
  222.         local gid = group:getID()
  223.        
  224.         missionCommands.addCommandForGroup(gid, "Load/unload Troops", nil, UnitRadioCommand, unitName)
  225.         RadioCommandTable[unitName] = true
  226.     end
  227. end
  228.  
  229.  
  230. function AddRadioCommands(arg, time)
  231.     AddRadioCommand("HueyPilot1")
  232.     AddRadioCommand("HueyPilot2")
  233.     AddRadioCommand("HueyPilot3")
  234.     AddRadioCommand("HueyPilot4")
  235.     AddRadioCommand("HueyPilot5")
  236.     AddRadioCommand("HueyPilot6")
  237.     AddRadioCommand("HueyPilot7")
  238.     AddRadioCommand("HueyPilot8")
  239.     AddRadioCommand("HueyPilot9")      
  240.     return time + 5
  241. end
  242.  
  243. function GetDistance(xUnit, yUnit, xZone, yZone)
  244.     local xDiff = xUnit - xZone
  245.     local yDiff = yUnit - yZone
  246.     return math.sqrt(xDiff * xDiff + yDiff * yDiff)    
  247. end
  248.  
  249.  
  250. function FindNearestDropoffZone(unit)
  251.     local minDist = 5000
  252.     local minZone = nil
  253.     local unitpos = unit:getPoint()
  254.    
  255.     for i=1,#DropoffZones do
  256.         local zone = DropoffZones[i]
  257.         local triggerZone = trigger.misc.getZone(zone.ZoneName)
  258.         local dist = GetDistance(unitpos.x, unitpos.z, triggerZone.point.x, triggerZone.point.z)
  259.         if dist < minDist then
  260.             minDist = dist
  261.             minZone = zone
  262.         end
  263.     end
  264.    
  265.     return minZone    
  266. end
  267.  
  268.  
  269. function StatusReport(arg, time)
  270.     -- array of Group function coalition.getGroups(enum coalition.side coalition, enum Group.Category groupCategory or nil)
  271.     -- array of Unit function Group.getUnits(Group self)
  272.     -- enum country.id CoalitionObject.getCountry(CoalitionObject self)
  273.  
  274.     local zoneTable = {}
  275.  
  276.     for i=1,#DropoffZones do
  277.         zoneTable[DropoffZones[i].Name] = 0
  278.     end
  279.  
  280.     local groups = coalition.getGroups(coalition.side.RED, Group.Category.GROUND)
  281.     env.info("Got groups: " .. #groups, false)
  282.     for i=1,#groups do
  283.         local group = groups[i]
  284.         if group ~= nil then
  285.             local units = group:getUnits()
  286.             for j=1,#units do
  287.                 local unit = units[j]
  288.                 if unit ~= nil then
  289.                     local country = unit:getCountry()
  290.                     if country == 17 then       -- if INSURGENT
  291.                         local zone = FindNearestDropoffZone(unit)
  292.                         if zone ~= nil then
  293.                             zoneTable[zone.Name] = zoneTable[zone.Name] + 1
  294.                         end
  295.                     end
  296.                 end
  297.             end
  298.         end
  299.     end
  300.  
  301.     local text = "MISSION STATUS  -  See mission briefing for details\n\n"
  302.  
  303.     for k,v in pairs(zoneTable) do
  304.         env.info("Append status [" .. tostring(k) .. "] [" .. tostring(v) .. "]", false)
  305.         text = text .. tostring(k) .. ": " .. tostring(v) .. " insurgent units remain.\n"
  306.     end
  307.    
  308.    
  309.     trigger.action.outText(text, 20)
  310.    
  311.     return time + 45
  312. end
  313.    
  314.  
  315. do
  316.     timer.scheduleFunction(SmokeTimer, nil, timer.getTime() + 5)
  317.     timer.scheduleFunction(AddRadioCommands, nil, timer.getTime() + 5)
  318.     timer.scheduleFunction(StatusReport, nil, timer.getTime() + 45)
  319. end
  320.  
  321.  
  322. function UnitInAnyPickupZone(unit)
  323.     for i=1,#PickupZones do
  324.         if UnitInZone(unit, PickupZones[i]) then
  325.             return PickupZones[i]
  326.         end
  327.     end
  328.    
  329.     return nil
  330. end
  331.  
  332. function UnitInAnyDropoffZone(unit)
  333.     for i=1,#DropoffZones do
  334.         if UnitInZone(unit, DropoffZones[i]) then
  335.             return DropoffZones[i]
  336.         end
  337.     end
  338.    
  339.     return nil
  340. end
  341.  
  342.  
  343. function UnitInZone(unit, zone)
  344.     if unit:inAir() then
  345.         return false
  346.     end
  347.    
  348.     local triggerZone = trigger.misc.getZone(zone.ZoneName)
  349.     local group = unit:getGroup()
  350.     local groupid = group:getID()
  351.     local unitpos = unit:getPoint()
  352.     local xDiff = unitpos.x - triggerZone.point.x
  353.     local yDiff = unitpos.z - triggerZone.point.z
  354.     local dist = math.sqrt(xDiff * xDiff + yDiff * yDiff)
  355.    
  356.     if dist > triggerZone.radius then
  357.         return false
  358.     end
  359.    
  360.     return true
  361. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement