This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Aug 2nd, 2011  |  syntax: None  |  size: 41.55 KB  |  views: 33  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. --------------------------------------------------------------------------------
  2. --------------------------------------------------------------------------------
  3.  
  4. function gadget:GetInfo()
  5.   return {
  6.     name      = "BA Chicken Spawner",
  7.     desc      = "Spawns burrows and chickens",
  8.     author    = "TheFatController/quantum",
  9.     date      = "16 November, 2008",
  10.     license   = "GNU GPL, v2 or later",
  11.     layer     = 0,
  12.     enabled   = true --  loaded by default?
  13.   }
  14. end
  15.  
  16. --------------------------------------------------------------------------------
  17. --------------------------------------------------------------------------------
  18. if (gadgetHandler:IsSyncedCode()) then
  19. -- BEGIN SYNCED
  20. --------------------------------------------------------------------------------
  21. --------------------------------------------------------------------------------
  22.  
  23. --------------------------------------------------------------------------------
  24. --------------------------------------------------------------------------------
  25. --
  26. -- Speed-ups
  27. --
  28.  
  29. local GetUnitNeutral       = Spring.GetUnitNeutral
  30. local GetTeamList          = Spring.GetTeamList
  31. local GetTeamLuaAI         = Spring.GetTeamLuaAI
  32. local GetGaiaTeamID        = Spring.GetGaiaTeamID
  33. local SetGameRulesParam    = Spring.SetGameRulesParam
  34. local GetGameRulesParam    = Spring.GetGameRulesParam
  35. local GetTeamUnitsCounts   = Spring.GetTeamUnitsCounts
  36. local GetTeamUnitCount     = Spring.GetTeamUnitCount
  37. local GetGameSpeed         = Spring.GetGameSpeed
  38. local GetGameFrame         = Spring.GetGameFrame
  39. local GetPlayerList        = Spring.GetPlayerList
  40. local GetPlayerInfo        = Spring.GetPlayerInfo
  41. local GetGameSeconds       = Spring.GetGameSeconds
  42. local DestroyUnit          = Spring.DestroyUnit
  43. local GetTeamUnits         = Spring.GetTeamUnits
  44. local GetUnitsInCylinder   = Spring.GetUnitsInCylinder
  45. local GetUnitNearestEnemy  = Spring.GetUnitNearestEnemy
  46. local GetUnitPosition      = Spring.GetUnitPosition
  47. local GiveOrderToUnit      = Spring.GiveOrderToUnit
  48. local TestBuildOrder       = Spring.TestBuildOrder
  49. local GetGroundBlocked     = Spring.GetGroundBlocked
  50. local CreateUnit           = Spring.CreateUnit
  51. local SetUnitBlocking      = Spring.SetUnitBlocking
  52. local GetGroundHeight      = Spring.GetGroundHeight
  53. local GetUnitTeam          = Spring.GetUnitTeam
  54. local GetUnitHealth        = Spring.GetUnitHealth
  55. local GetUnitCommands      = Spring.GetUnitCommands
  56. local SetUnitExperience    = Spring.SetUnitExperience
  57. local GetUnitDefID         = Spring.GetUnitDefID
  58. local SetUnitHealth        = Spring.SetUnitHealth
  59. local GetUnitIsDead        = Spring.GetUnitIsDead
  60. local GetCommandQueue      = Spring.GetCommandQueue
  61. local GetUnitDirection     = Spring.GetUnitDirection
  62.  
  63. local mRandom             = math.random
  64. local math                = math
  65. local Game                = Game
  66. local table               = table
  67. local ipairs              = ipairs
  68. local pairs               = pairs
  69.  
  70. local MAPSIZEX = Game.mapSizeX
  71. local MAPSIZEZ = Game.mapSizeZ
  72.  
  73. --------------------------------------------------------------------------------
  74. --------------------------------------------------------------------------------
  75.  
  76. local queenLifePercent    = 100
  77. local maxTries            = 100
  78. local currentWave         = 1
  79. local targetCache         = 1
  80. local minBurrows          = 1
  81. local timeOfLastSpawn     = 0
  82. local timeOfLastFakeSpawn = 0
  83. local highTeam            = 0
  84. local timeOfLastWave      = 0
  85. local expMod              = 0
  86. local burrowTarget        = 0
  87. local qDamage             = 0
  88. local lastTeamID          = 0
  89. local targetCacheCount    = 0
  90. local nextSquadSize       = 0
  91. local chickenCount        = 0
  92. local t                   = 0
  93. local timeCounter         = 0
  94. local queenAnger          = 0
  95. local bonusPoints         = 0
  96. local burrowSpawnProgress = 0
  97. local queenMaxHP          = 0
  98. local chickenDebtCount    = 0
  99. local firstSpawn          = true
  100. local showScoresOnce      = true
  101. local gameOver            = false
  102. local qMove               = false
  103. local warningMessage      = false
  104. local computerTeams       = {}
  105. local humanTeams          = {}
  106. local disabledUnits       = {}
  107. local spawnQueue          = {}
  108. local idleOrderQueue      = {}
  109. local stunList            = {}
  110. local endScores           = {}
  111. local queenResistance     = {}
  112. local queenID
  113. local chickenTeamID
  114. local luaAI
  115. local lsx1,lsz1,lsx2,lsz2
  116. local turrets             = {}
  117. local chickenBirths       = {}
  118. local failChickens        = {}
  119. local chickenTargets      = {}
  120. local burrows             = {}
  121.  
  122. --------------------------------------------------------------------------------
  123. --------------------------------------------------------------------------------
  124. --
  125. -- Teams
  126. --
  127.  
  128. local VERYEASY = "Chicken: Very Easy"
  129. local EASY = "Chicken: Easy"
  130. local NORMAL = "Chicken: Normal"
  131. local HARD = "Chicken: Hard"
  132. local VERYHARD = "Chicken: Very Hard"
  133. local CUSTOM = "Chicken: Custom"
  134. local SURVIVAL = "Chicken: Survival"
  135.  
  136. local modes = {
  137.     [1] = VERYEASY,
  138.     [2] = EASY,
  139.     [3] = NORMAL,
  140.     [4] = HARD,
  141.     [5] = VERYHARD,
  142.     [6] = CUSTOM,
  143.     [7] = SURVIVAL,
  144. }
  145.  
  146. for i, v in ipairs(modes) do -- make it bi-directional
  147.   modes[v] = i
  148. end
  149.  
  150. local teams = GetTeamList()
  151. local highestLevel = 0
  152. for _, teamID in ipairs(teams) do
  153.   local teamLuaAI = GetTeamLuaAI(teamID)
  154.   if (teamLuaAI and teamLuaAI ~= "") then
  155.     luaAI = teamLuaAI
  156.     if (modes[teamLuaAI] > highestLevel) then
  157.       highestLevel = modes[teamLuaAI]
  158.     end
  159.     chickenTeamID = teamID
  160.     computerTeams[teamID] = true
  161.   else
  162.     humanTeams[teamID]    = true
  163.   end
  164. end
  165.  
  166. if (modes[highestLevel] == SURVIVAL) then
  167.   return false
  168. end
  169.  
  170. luaAI = modes[highestLevel]
  171.  
  172. local gaiaTeamID         = GetGaiaTeamID()
  173. if not chickenTeamID then
  174.   chickenTeamID = gaiaTeamID
  175.   warningMessage = true
  176. else
  177.   computerTeams[gaiaTeamID] = nil
  178. end
  179.  
  180. humanTeams[gaiaTeamID]    = nil
  181.  
  182. if (gameMode and luaAI == 0) then
  183.   return false
  184. end
  185.  
  186. --------------------------------------------------------------------------------
  187. --------------------------------------------------------------------------------
  188. --
  189. -- Utility
  190. --
  191.  
  192. local function SetToList(set)
  193.   local list = {}
  194.   for k in pairs(set) do
  195.     table.insert(list, k)
  196.   end
  197.   return list
  198. end
  199.  
  200.  
  201. local function SetCount(set)
  202.   local count = 0
  203.   for k in pairs(set) do
  204.     count = count + 1
  205.   end
  206.   return count
  207. end
  208.  
  209. function comma_value(amount)
  210.   local formatted = amount
  211.   while true do  
  212.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  213.     if (k==0) then
  214.       break
  215.     end
  216.   end
  217.   return formatted
  218. end
  219.  
  220. local function getSqrDistance(x1,z1,x2,z2)
  221.   local dx,dz = x1-x2,z1-z2
  222.   return (dx*dx)+(dz*dz)
  223. end
  224.  
  225.  
  226. --------------------------------------------------------------------------------
  227. --------------------------------------------------------------------------------
  228. --
  229. -- Difficulty
  230. --
  231.  
  232. do -- load config file
  233.   local CONFIG_FILE = "LuaRules/Configs/spawn_defs.lua"
  234.   local VFSMODE = VFS.RAW_FIRST
  235.   local s = assert(VFS.LoadFile(CONFIG_FILE, VFSMODE))
  236.   local chunk = assert(loadstring(s, file))
  237.   setfenv(chunk, gadget)
  238.   chunk()
  239. end
  240.  
  241. local function SetGlobals(difficulty)
  242.   for key, value in pairs(gadget.difficulties[difficulty]) do
  243.     gadget[key] = value
  244.   end
  245.   gadget.difficulties = nil
  246. end
  247.  
  248. SetGlobals(luaAI or "Chicken: Custom") -- set difficulty
  249.  
  250. local expIncrement = ((SetCount(humanTeams) * expStep) / queenTime)
  251. if expStep < 0 then expIncrement = ((expStep * -1) / queenTime) end
  252. local nextWave = ((queenTime / 8) / 60)
  253. local gracePenalty = math.max(math.floor(((gracePeriod - 220) / burrowSpawnRate) + 0.5), 0)
  254. chickensPerPlayer = (chickensPerPlayer * SetCount(humanTeams))
  255. maxBurrows = maxBurrows + math.floor(SetCount(humanTeams) * 1.334)
  256. queenTime = (queenTime + gracePeriod)
  257. chickenDebtCount = math.ceil((math.max((gracePeriod - 220),0) / 3))
  258.          
  259. --------------------------------------------------------------------------------
  260. --------------------------------------------------------------------------------
  261. --
  262. -- Game Rules
  263. --
  264.  
  265. local UPDATE = 10
  266.  
  267. local unitCounts = {}
  268.  
  269. local chickenDefTypes = {}
  270. for unitName in pairs(chickenTypes) do
  271.   Spring.Echo("unit names with resulting id")
  272.   Spring.Echo(unitName)
  273.   Spring.Echo(UnitDefNames[unitName].id)
  274.   chickenDefTypes[UnitDefNames[unitName].id] = unitName
  275.   unitCounts[string.sub(unitName,1,-2)] = {count = 0, lastCount = 0}
  276. end
  277.  
  278. local defendersDefs = {}
  279. for unitName in pairs(defenders) do
  280.   defendersDefs[UnitDefNames[unitName].id] = unitName
  281. end
  282.  
  283. local function SetupUnit(unitName)
  284.   SetGameRulesParam(unitName.."Count", 0)
  285.   SetGameRulesParam(unitName.."Kills", 0)
  286. end
  287.  
  288. SetGameRulesParam("queenTime",        queenTime)
  289. SetGameRulesParam("queenLife",        queenLifePercent)
  290. SetGameRulesParam("queenAnger",       queenAnger)
  291. SetGameRulesParam("gracePeriod",      gracePeriod)
  292.  
  293. for unitName in pairs(chickenTypes) do
  294.   SetupUnit(string.sub(unitName,1,-2))
  295. end
  296.  
  297. for unitName in pairs(defenders) do
  298.   SetupUnit(string.sub(unitName,1,-2))
  299. end
  300.  
  301. SetupUnit(burrowName)
  302.  
  303. local difficulty = modes[luaAI] or 6
  304. SetGameRulesParam("difficulty", difficulty)
  305.  
  306. local function UpdateUnitCount()
  307.   local teamUnitCounts = GetTeamUnitsCounts(chickenTeamID)
  308.   local total = 0
  309.  
  310.   for shortName in pairs(unitCounts) do
  311.     unitCounts[shortName].count = 0
  312.   end
  313.  
  314.   for unitDefID, number in pairs(teamUnitCounts) do
  315.     if UnitDefs[unitDefID] then
  316.       local shortName = string.sub(UnitDefs[unitDefID].name,1,-2)
  317.       if unitCounts[shortName] then
  318.         unitCounts[shortName].count = unitCounts[shortName].count + number
  319.       end
  320.     end
  321.   end
  322.  
  323.   for shortName, counts in pairs(unitCounts) do
  324.     if (counts.count ~= counts.lastCount) then
  325.       SetGameRulesParam(shortName.."Count", counts.count)
  326.       counts.lastCount = counts.count
  327.     end
  328.     total = total + counts.count
  329.   end
  330.  
  331.   return total
  332. end
  333.  
  334. local EMP_GOO = {}
  335. EMP_GOO[WeaponDefNames['chickenr1_goolauncher'].id] = WeaponDefNames['chickenr1_goolauncher'].damages[1]
  336. EMP_GOO[WeaponDefNames['weaver_death'].id] = WeaponDefNames['weaver_death'].damages[1]
  337. local LOBBER = UnitDefNames["chickenr1"].id
  338. local SKIRMISH = { [UnitDefNames["chickens1"].id] = 270, [UnitDefNames["chickens2"].id] = 620 }
  339. local COWARD = { [UnitDefNames["chickenh1"].id] = 150, [UnitDefNames["chickenr1"].id] = 300 }
  340. local JUNO = WeaponDefNames["juno_pulse"].id
  341. local KROW_ID = UnitDefNames["corcrw"].id
  342. local KROW_LASER = "krow_laser_index"
  343. local SMALL_UNIT = UnitDefNames["corllt"].id
  344. local MEDIUM_UNIT = UnitDefNames["armwin"].id
  345. local LARGE_UNIT = UnitDefNames["armsolar"].id
  346.  
  347. --------------------------------------------------------------------------------
  348. --------------------------------------------------------------------------------
  349. --
  350. -- Clean up
  351. --
  352.  
  353. local function KillOldChicken()
  354.   for unitID, birthDate in pairs(chickenBirths) do
  355.     local age = (t - birthDate)
  356.     if (age > maxAge) then
  357.       if (unitID ~= queenID) then
  358.         DestroyUnit(unitID)
  359.         chickenCount = chickenCount - 1
  360.         chickenDebtCount = chickenDebtCount + 1
  361.       end
  362.     end
  363.   end
  364. end
  365.  
  366. --------------------------------------------------------------------------------
  367. --------------------------------------------------------------------------------
  368. --
  369. -- Game End Stuff
  370. --
  371.  
  372. local function KillAllChicken()
  373.   local chickenUnits = GetTeamUnits(chickenTeamID)
  374.   for _, unitID in pairs(chickenUnits) do
  375.     if disabledUnits[unitID] then
  376.       DestroyUnit(unitID, false, true)
  377.     else
  378.       DestroyUnit(unitID, true)
  379.     end
  380.   end
  381. end
  382.  
  383.  
  384. local function KillAllComputerUnits()
  385.   for teamID in pairs(computerTeams) do
  386.     local teamUnits = GetTeamUnits(teamID)
  387.     for _, unitID in pairs(teamUnits) do
  388.       if disabledUnits[unitID] then
  389.         DestroyUnit(unitID, false, true)
  390.       else
  391.         DestroyUnit(unitID, true)
  392.       end
  393.     end
  394.   end
  395. end
  396.  
  397. local function CalculateScores()
  398.   local totalDamage = 0
  399.   local teamScores = {}
  400.   local totalScore = bonusPoints
  401.   for teamID in pairs(humanTeams) do
  402.     local foo = Spring.GetTeamStatsHistory(teamID,0,t)
  403.     teamScores[teamID] = foo[#foo].damageDealt
  404.     totalDamage = totalDamage + teamScores[teamID]
  405.     local teamUnits = GetTeamUnits(teamID)
  406.     for _, unitID in pairs(teamUnits) do
  407.       local defID = GetUnitDefID(unitID)
  408.       if defID then
  409.         totalScore = totalScore + UnitDefs[defID].metalCost
  410.         totalScore = totalScore + (UnitDefs[defID].energyCost / 50)
  411.       end
  412.     end
  413.   end
  414.   for teamID in pairs(humanTeams) do
  415.     teamScores[teamID] = (teamScores[teamID] / totalDamage)
  416.     teamScores[teamID] = math.ceil((totalScore * teamScores[teamID]))
  417.     if (teamID > highTeam) then highTeam = teamID end
  418.   end
  419.   highTeam = (highTeam + 1)
  420.   return teamScores
  421. end
  422.  
  423. local function DisplayScores(scoreList)
  424.   Spring.Echo("           ")
  425.   Spring.Echo("Team Scores")
  426.   Spring.Echo("-----------")
  427.   for i = 0,highTeam,1 do
  428.     if scoreList[i] then
  429.       local teamID = i
  430.       local teamScore = scoreList[i]
  431.       local playerList = Spring.GetPlayerList(teamID, false)
  432.       if playerList[1] then
  433.         Spring.SendMessage("Team" .. teamID .. " (<PLAYER" .. playerList[1] .. ">) - " .. comma_value(teamScore) .. " Points")
  434.       else
  435.         Spring.Echo("Team" .. teamID .. " (AI) - " .. comma_value(teamScore) .. " Points")
  436.       end
  437.     end
  438.   end
  439. end
  440.  
  441. --------------------------------------------------------------------------------
  442. --------------------------------------------------------------------------------
  443. --
  444. -- Spawn Dynamics
  445. --
  446.  
  447. local function addChickenTarget(chickenID, targetID)
  448.   if (not targetID) or (GetUnitTeam(targetID) == chickenTeamID) or (GetUnitTeam(chickenID) ~= chickenTeamID) then return end
  449.   --debug--Spring.Echo(t .. " addChickenTarget " .. chickenID .. "," .. targetID)
  450.   if chickenTargets[chickenID] and chickenTargets[chickenTargets[chickenID]] then
  451.     chickenTargets[chickenTargets[chickenID]][chickenID] = nil
  452.   end
  453.   if (chickenTargets[targetID] == nil) then
  454.     chickenTargets[targetID] = {}
  455.     chickenTargets[targetID][chickenID] = targetID
  456.     chickenTargets[chickenID] = targetID
  457.   else
  458.     chickenTargets[targetID][chickenID] = targetID
  459.     chickenTargets[chickenID] = targetID
  460.   end
  461. end
  462.  
  463. local function AttackNearestEnemy(unitID, unitDefID, unitTeam)
  464.   local targetID = GetUnitNearestEnemy(unitID)
  465.   if (targetID) and (not GetUnitIsDead(targetID)) and (not GetUnitNeutral(targetID)) then
  466.     --debug--Spring.Echo(t .. " AttackNearestEnemy " .. unitID .. " -> " .. targetID)
  467.     local defID = GetUnitDefID(targetID)
  468.     if UnitDefs[defID] and (UnitDefs[defID].speed > 75) then
  469.       return false
  470.     end
  471.     local x,y,z = GetUnitPosition(targetID)
  472.     idleOrderQueue[unitID] = {cmd = CMD.FIGHT, params = {x,y,z}, opts = {}}
  473.     addChickenTarget(unitID, targetID)
  474.     return true
  475.   else
  476.     return false
  477.   end
  478. end
  479.  
  480. local function ChooseTarget()
  481.   local humanTeamList = SetToList(humanTeams)
  482.   if (#humanTeamList == 0) then
  483.     return {mRandom(1,MAPSIZEX-1),0,mRandom(1,MAPSIZEZ-1)}
  484.   end
  485.  
  486.   if (targetCacheCount >= nextSquadSize) or GetUnitIsDead(targetCache) then
  487.     local tries = 0
  488.     repeat
  489.       local teamID = humanTeamList[mRandom(#humanTeamList)]
  490.       if (teamID == lastTeamID) then teamID = humanTeamList[mRandom(#humanTeamList)] end
  491.       lastTeamID = teamID
  492.       local units = GetTeamUnits(teamID)
  493.       if units[2] then
  494.         targetCache = units[mRandom(1,#units)]
  495.       else
  496.         targetCache = units[1]
  497.       end
  498.       local slowunit = true
  499.       if tries < 5 then
  500.         local defID = GetUnitDefID(targetCache)
  501.         if UnitDefs[defID] and (UnitDefs[defID].speed > 75) then
  502.           slowunit = false
  503.         end
  504.       end      
  505.       tries = (tries + 1)
  506.     until ((not GetUnitIsDead(targetCache)) and (not GetUnitNeutral(targetCache)) and slowunit) or (tries > maxTries)
  507.     targetCacheCount = 0
  508.     nextSquadSize = 6 + mRandom(0,4)
  509.   else
  510.     targetCacheCount = targetCacheCount + 1
  511.   end  
  512.   --debug--Spring.Echo(t .. " ChooseTarget " .. targetCache)
  513.   return {GetUnitPosition(targetCache)}
  514.  
  515. end
  516.  
  517. local function getChickenSpawnLoc(burrowID, size)
  518.   local x, y, z
  519.   local bx, by, bz    = GetUnitPosition(burrowID)
  520.   if (not bx or not bz) then
  521.     return false
  522.   end
  523.  
  524.   local tries         = 0
  525.   local s             = spawnSquare
  526.      
  527.   repeat
  528.     x = mRandom(bx - s, bx + s)
  529.     z = mRandom(bz - s, bz + s)
  530.     s = s + spawnSquareIncrement
  531.     tries = tries + 1
  532.     if (x >= MAPSIZEX) then x = (MAPSIZEX - mRandom(1,40)) elseif (x <= 0) then x = mRandom(1,40) end
  533.     if (z >= MAPSIZEZ) then z = (MAPSIZEZ - mRandom(1,40)) elseif (z <= 0) then z = mRandom(1,40) end
  534.   until ((TestBuildOrder(size, x, by, z, 1) == 2) and (not GetGroundBlocked(x, z))) or (tries > maxTries)
  535.            
  536.   y = GetGroundHeight(x,z)
  537.   return x, y, z
  538.    
  539. end
  540.  
  541.  
  542. local function SpawnTurret(burrowID, turret)
  543.  
  544.   if (mRandom() > defenderChance) or (not turret) or (burrows[burrowID] >= maxTurrets) then
  545.     return
  546.   end
  547.  
  548.   local x, y, z
  549.   local bx, by, bz    = GetUnitPosition(burrowID)
  550.   local tries         = 0
  551.   local s             = spawnSquare
  552.  
  553.   repeat
  554.     x = mRandom(bx - s, bx + s)
  555.     z = mRandom(bz - s, bz + s)
  556.     s = s + spawnSquareIncrement
  557.     tries = tries + 1
  558.     if (x >= MAPSIZEX) then x = (MAPSIZEX - mRandom(1,40)) elseif (x <= 0) then x = mRandom(1,40) end
  559.     if (z >= MAPSIZEZ) then z = (MAPSIZEZ - mRandom(1,40)) elseif (z <= 0) then z = mRandom(1,40) end
  560.   until (not GetGroundBlocked(x, z) or tries > maxTries)
  561.  
  562.   y = GetGroundHeight(x,z)
  563.   local unitID = CreateUnit(turret, x, y, z, "n", chickenTeamID)
  564.   idleOrderQueue[unitID] = {cmd = CMD.GUARD, params ={burrowID}, opts = {}}
  565.   SetUnitBlocking(unitID, false)
  566.   turrets[unitID] = {burrowID, t}
  567.   burrows[burrowID] = burrows[burrowID] + 1
  568.    
  569. end
  570.  
  571.  
  572. local function SpawnBurrow(number)
  573.  
  574.   if (queenID) then
  575.     return
  576.   end
  577.  
  578.   local unitDefID = UnitDefNames[burrowName].id
  579.    
  580.   for i=1, (number or 1) do
  581.     local x, z
  582.     local tries = 0
  583.   repeat
  584.     if (burrowSpawnType == "initialbox") then
  585.       x = mRandom(lsx1, lsx2)
  586.       z = mRandom(lsz1, lsz2)
  587.     elseif ((burrowSpawnType == "alwaysbox") and (tries < (maxTries / 2))) then
  588.       x = mRandom(lsx1, lsx2)
  589.       z = mRandom(lsz1, lsz2)
  590.     else
  591.       x = mRandom(spawnSquare, MAPSIZEX - spawnSquare)
  592.       z = mRandom(spawnSquare, MAPSIZEZ - spawnSquare)
  593.     end
  594.    
  595.     local y = GetGroundHeight(x, z)
  596.     tries = tries + 1
  597.     local blocking = TestBuildOrder(MEDIUM_UNIT, x, y, z, 1)
  598.     if (blocking == 2) and (burrowSpawnType == "avoid") then
  599.       local proximity = GetUnitsInCylinder(x, z, minBaseDistance)
  600.       local vicinity = GetUnitsInCylinder(x, z, maxBaseDistance)
  601.       local humanUnitsInVicinity = false
  602.       local humanUnitsInProximity = false
  603.       for i=1,vicinity['n'],1 do
  604.         if (GetUnitTeam(vicinity[i]) ~= chickenTeamID) then
  605.           humanUnitsInVicinity = true
  606.           break
  607.         end
  608.       end
  609.  
  610.       for i=1,proximity['n'],1 do
  611.         if (GetUnitTeam(proximity[i]) ~= chickenTeamID) then
  612.           humanUnitsInProximity = true
  613.           break
  614.         end
  615.       end
  616.  
  617.       if (humanUnitsInProximity or not humanUnitsInVicinity) then
  618.         blocking = 1
  619.       end
  620.     end
  621.   until (blocking == 2 or tries > maxTries)
  622.  
  623.     local unitID = CreateUnit(burrowName, x, 0, z, "n", chickenTeamID)
  624.     burrows[unitID] = 0
  625.     SetUnitBlocking(unitID, false)
  626.   end
  627.  
  628. end
  629.  
  630. local function updateQueenLife()
  631.   if not queenID then return end
  632.   local curH, maxH = GetUnitHealth(queenID)
  633.   local lifeCheck = math.ceil(((curH/maxH)*100)-0.5)
  634.   if queenLifePercent ~= lifeCheck then
  635.     queenLifePercent = lifeCheck
  636.     SetGameRulesParam("queenLife",        queenLifePercent)
  637.   end
  638. end
  639.  
  640. local function stunUnit(unitID, seconds)
  641.   seconds = (seconds * 30)
  642.   if stunList[unitID] then seconds = math.max(stunList[unitID], seconds) end
  643.   stunList[unitID] = seconds
  644.   SetUnitHealth(unitID, {paralyze=99999999})
  645. end
  646.  
  647. local function SpawnQueen()
  648.  
  649.   local x, y, z
  650.   local tries = 0
  651.  
  652.   repeat
  653.     x = mRandom(1, (MAPSIZEX-1))
  654.     z = mRandom(1, (MAPSIZEZ-1))
  655.     y = GetGroundHeight(x, z)
  656.     tries = tries + 1
  657.     local blocking = TestBuildOrder(LARGE_UNIT, x, y, z, 1)
  658.    
  659.     local proximity = GetUnitsInCylinder(x, z, minBaseDistance)
  660.     local vicinity = GetUnitsInCylinder(x, z, maxBaseDistance)
  661.     local humanUnitsInVicinity = false
  662.     local humanUnitsInProximity = false
  663.    
  664.     for i=1,vicinity['n'],1 do
  665.       if (GetUnitTeam(vicinity[i]) ~= chickenTeamID) then
  666.         humanUnitsInVicinity = true
  667.         break
  668.       end
  669.     end
  670.  
  671.     for i=1,proximity['n'],1 do
  672.       if (GetUnitTeam(proximity[i]) ~= chickenTeamID) then
  673.         humanUnitsInProximity = true
  674.         break
  675.       end
  676.     end
  677.  
  678.     if (humanUnitsInProximity or not humanUnitsInVicinity) then
  679.       blocking = 1
  680.     end
  681.  
  682.   until (blocking == 2 or tries > maxTries)
  683.  
  684.   return CreateUnit(queenName, x, y, z, "n", chickenTeamID)
  685.  
  686. end
  687.  
  688.  
  689. local function Wave()
  690.   --debug--Spring.Echo(t .. "Wave()")
  691.  
  692.   if gameOver then return end
  693.  
  694.   currentWave = math.min(math.ceil((((t-gracePeriod+queenAnger) / 60) / nextWave)), 8)
  695.  
  696.   if currentWave > #waves then currentWave = #waves end
  697.  
  698.   if (currentWave == 8) then
  699.     COWARD[UnitDefNames["chickenc1"].id] = (150 + mRandom(1,150))
  700.     COWARD[UnitDefNames["chicken2"].id] = (200 + mRandom(1,200))
  701.   end
  702.  
  703.   local cCount = 0
  704.  
  705.   if queenID then
  706.     if queenSpawnMult > 0 then
  707.       for i = 1,queenSpawnMult,1 do
  708.         local squad = waves[9][mRandom(1,#waves[9])]
  709.         for i,sString in pairs(squad) do
  710.           local nEnd,_     = string.find(sString, " ")
  711.           local unitNumber = string.sub(sString,1,(nEnd-1))
  712.           local chickenName  = string.sub(sString,(nEnd+1))
  713.           for i = 1,unitNumber,1 do
  714.             table.insert(spawnQueue, {burrow = queenID, unitName = chickenName, team = chickenTeamID})
  715.           end
  716.           cCount = cCount + unitNumber
  717.         end
  718.       end
  719.     end
  720.     return cCount
  721.   end
  722.  
  723.   for burrowID in pairs(burrows) do
  724.     if (t > (queenTime * 0.33)) then SpawnTurret(burrowID, bonusTurret) end
  725.     local squad = waves[currentWave][mRandom(1,#waves[currentWave])]
  726.     for i,sString in pairs(squad) do
  727.       local skipSpawn = false
  728.       if (cCount > chickensPerPlayer) and (mRandom() > spawnChance) then skipSpawn = true end
  729.       if skipSpawn and (chickenDebtCount > 0) and (mRandom() > 0.5) then
  730.         chickenDebtCount = (chickenDebtCount - 1)
  731.         skipSpawn = false
  732.       end
  733.       if not skipSpawn then
  734.         local nEnd,_     = string.find(sString, " ")
  735.         local unitNumber = string.sub(sString,1,(nEnd-1))
  736.         local chickenName  = string.sub(sString,(nEnd+1))
  737.         for i = 1,unitNumber,1 do
  738.           table.insert(spawnQueue, {burrow = burrowID, unitName = chickenName, team = chickenTeamID})
  739.         end
  740.         cCount = cCount + unitNumber
  741.       end
  742.     end
  743.   end
  744.   return cCount
  745. end
  746.  
  747. --------------------------------------------------------------------------------
  748. --------------------------------------------------------------------------------
  749. --
  750. -- Get rid of the AI
  751. --
  752.  
  753. local function DisableUnit(unitID)
  754.   Spring.MoveCtrl.Enable(unitID)
  755.   Spring.MoveCtrl.SetNoBlocking(unitID, true)
  756.   Spring.MoveCtrl.SetPosition(unitID, Game.mapSizeX, 10000, Game.mapSizeZ)
  757.   Spring.SetUnitCloak(unitID, true)
  758.   Spring.SetUnitHealth(unitID, {paralyze=99999999})
  759.   Spring.SetUnitNoDraw(unitID, true)
  760.   Spring.SetUnitStealth(unitID, true)
  761.   Spring.SetUnitNoSelect(unitID, true)
  762.   Spring.SetUnitNoMinimap(unitID, true)
  763.   disabledUnits[unitID] = true
  764. end
  765.  
  766. local function DisableComputerUnits()
  767.   for teamID in pairs(computerTeams) do
  768.     local teamUnits = GetTeamUnits(teamID)
  769.     for _, unitID in ipairs(teamUnits) do
  770.       DisableUnit(unitID)
  771.     end
  772.   end
  773. end
  774.  
  775.  
  776. --------------------------------------------------------------------------------
  777. --------------------------------------------------------------------------------
  778. --
  779. -- Call-ins
  780. --
  781.  
  782. function gadget:UnitMoveFailed(unitID, unitDefID, unitTeam)
  783.   if gameOver then return end
  784.   if (unitTeam ~= chickenTeamID) or (unitID == queenID) or idleOrderQueue[unitID] then return end
  785.   local px,py,pz = GetUnitPosition(unitID)
  786.   if failChickens[unitID] then
  787.     if (getSqrDistance(failChickens[unitID].x,failChickens[unitID].z,px,pz) < 5000) then
  788.       failChickens[unitID].count = failChickens[unitID].count + 1
  789.       if (failChickens[unitID].count > 3) then
  790.         DestroyUnit(unitID, false, true)
  791.         chickenDebtCount = chickenDebtCount + 1
  792.         chickenCount = chickenCount - 1
  793.         return
  794.       end
  795.     else
  796.       failChickens[unitID].x = px
  797.       failChickens[unitID].z = pz
  798.     end
  799.   else
  800.     failChickens[unitID] = { x = px, z = pz, count = 1 }
  801.   end
  802.   if (mRandom(1,2) == 1) then px = px + mRandom(50,100) else px = px - mRandom(50,100) end
  803.   if (mRandom(1,2) == 1) then pz = pz + mRandom(50,100) else pz = pz - mRandom(50,100) end
  804.   idleOrderQueue[unitID] = {cmd = CMD.MOVE, params = {px,py,pz}, opts = {}}
  805. end
  806.  
  807. function gadget:UnitIdle(unitID, unitDefID, unitTeam)
  808.   if gameOver then return end
  809.   if (unitTeam == chickenTeamID) and (chickenDefTypes[unitDefID] or (unitID == queenID)) then
  810.     --debug--Spring.Echo(t .. " unitIdle " .. unitID)
  811.     if not AttackNearestEnemy(unitID, unitDefID, unitTeam) then
  812.       local chickenParams = ChooseTarget()
  813.           if targetCache and (unitID ~= queenID) and (mRandom(1,10) == 5) then
  814.             idleOrderQueue[unitID] = {cmd = CMD.ATTACK, params = {targetCache}, opts = {}}
  815.           else
  816.         idleOrderQueue[unitID] = {cmd = CMD.FIGHT, params = chickenParams, opts = {}}
  817.       end
  818.       if targetCache then
  819.         if GetUnitNeutral(targetCache) then
  820.           idleOrderQueue[unitID] = {cmd = CMD.ATTACK, params = {targetCache}, opts = {}}
  821.         end
  822.         addChickenTarget(unitID, targetCache)
  823.       end
  824.     end
  825.   end
  826. end
  827.  
  828. function gadget:UnitCreated(unitID, unitDefID, unitTeam)
  829.   if chickenTargets[unitID] then
  830.     chickenTargets[unitID] = nil
  831.   end
  832. end
  833.  
  834. function gadget:UnitPreDamaged(unitID, unitDefID, unitTeam, damage, paralyzer,
  835.                             weaponID, attackerID, attackerDefID, attackerTeam)
  836.  
  837.   if (attackerTeam == chickenTeamID) then
  838.     return (damage * damageMod)
  839.   end
  840.   if (unitID == queenID) then
  841.     if (weaponID == -1) and (damage > 25000) then
  842.           return 25000
  843.     end
  844.     if attackerDefID then
  845.       if (attackerDefID == KROW_ID) then
  846.         weaponID = KROW_LASER
  847.       end
  848.       if not queenResistance[weaponID] then
  849.         queenResistance[weaponID] = {}
  850.         queenResistance[weaponID].damage = damage
  851.         queenResistance[weaponID].notify = 0
  852.       end
  853.       local resistPercent = (math.min(queenResistance[weaponID].damage/queenMaxHP, 0.75) + 0.2)
  854.       if resistPercent > 0.35 then
  855.         if queenResistance[weaponID].notify == 0 then
  856.           local weaponName
  857.           if (attackerDefID == KROW_ID) then
  858.             weaponName = "HighEnergyLaser"
  859.           else
  860.             weaponName = WeaponDefs[weaponID].description
  861.           end
  862.           Spring.Echo("Queen is becoming resistant to " .. UnitDefs[attackerDefID].humanName .. "'s attacks (" .. weaponName .. ")")
  863.           queenResistance[weaponID].notify = 1
  864.           for i = 1,30,1 do
  865.             table.insert(spawnQueue, {burrow = queenID, unitName = "chickenh4", team = chickenTeamID})
  866.           end
  867.         end
  868.         damage = damage - (damage * resistPercent)
  869.       end
  870.       queenResistance[weaponID].damage = queenResistance[weaponID].damage + damage
  871.       return damage
  872.     end
  873.   end
  874.   return damage        
  875. end
  876.  
  877. function gadget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer,
  878.                             weaponID, attackerID, attackerDefID, attackerTeam)
  879.                              
  880.   if EMP_GOO[weaponID] and (unitTeam ~= chickenTeamID) and (lobberEMPTime > 0) then
  881.     stunUnit(unitID, ((damage / EMP_GOO[weaponID]) * lobberEMPTime))
  882.   end
  883.      
  884.   if chickenBirths[attackerID] then chickenBirths[attackerID] = t end
  885.   if failChickens[attackerID] then failChickens[attackerID] = nil end
  886.   if failChickens[unitID] then failChickens[unitID] = nil end
  887.  
  888.   if SKIRMISH[attackerDefID] and (unitTeam ~= chickenTeamID) and attackerID then
  889.     local ux,_,uz = GetUnitPosition(unitID)
  890.     local x,y,z = GetUnitPosition(attackerID)
  891.     if x and ux then
  892.           local angle = math.atan2(ux-x,uz-z)
  893.           idleOrderQueue[attackerID] = {cmd = CMD.MOVE, params = {x - (math.sin(angle) * SKIRMISH[attackerDefID]),y,z - (math.cos(angle) * SKIRMISH[attackerDefID])}, opts = {}}
  894.         end
  895.   elseif COWARD[unitDefID] and (not idleOrderQueue[unitID]) and (unitTeam == chickenTeamID) and attackerID then
  896.     local curH, maxH = GetUnitHealth(attackerID)
  897.     if curH < (maxH * 0.8) then
  898.       local ax,_,az = GetUnitPosition(attackerID)
  899.       local x,y,z = GetUnitPosition(unitID)
  900.       if x and ax then
  901.             local angle = math.atan2(ax-x,az-z)
  902.             idleOrderQueue[unitID] = {cmd = CMD.MOVE, params = {x - (math.sin(angle) * COWARD[unitDefID]),y,z - (math.cos(angle) * COWARD[unitDefID])}, opts = {}}
  903.           end
  904.         end
  905.   end
  906.  
  907.   if (unitDefID == LOBBER) then
  908.     local nSpawn = false
  909.     if ((GetUnitHealth(unitID) < 2475) and (damage < (2000 + mRandom(1,500)))) then nSpawn = true end
  910.     if ((weaponID == JUNO) and (mRandom(1,2) == 1)) then nSpawn = true end
  911.     if nSpawn then
  912.       local bx, by, bz = GetUnitPosition(unitID)
  913.       local h = Spring.GetUnitHeading(unitID)
  914.       SetUnitBlocking(unitID, false)
  915.       local newUnitID = CreateUnit("chickenr2", bx, by, bz, "n", unitTeam)
  916.       Spring.SetUnitNoDraw(newUnitID,true)
  917.       Spring.MoveCtrl.Enable(newUnitID)
  918.       Spring.MoveCtrl.SetHeading(newUnitID, h)
  919.       Spring.MoveCtrl.Disable(newUnitID)
  920.       SetUnitExperience(newUnitID, expMod)
  921.       Spring.SetUnitNoDraw(newUnitID,false)
  922.       DestroyUnit(unitID, false, true)
  923.       idleOrderQueue[newUnitID] = {cmd = CMD.STOP, params = {}, opts = {}}
  924.     return
  925.     end
  926.   elseif (unitID == queenID) then
  927.     if paralyzer then
  928.       SetUnitHealth(unitID, {paralyze=0})
  929.       return
  930.     end
  931.     qDamage = (qDamage + damage)
  932.     if (qDamage > (queenMaxHP/10)) then
  933.       if qMove then
  934.         idleOrderQueue[queenID] = {cmd = CMD.STOP, params = {}, opts = {}}
  935.         qMove = false
  936.         qDamage = 0 - mRandom(1,25000)
  937.       else
  938.         local cC = ChooseTarget()
  939.         local xQ, _, zQ = GetUnitPosition(queenID)
  940.         if cC then
  941.           local angle = math.atan2((cC[1]-xQ), (cC[3]-zQ))
  942.           local dist = math.sqrt(((cC[1]-xQ)*(cC[1]-xQ))+((cC[3]-zQ)*(cC[3]-zQ))) * 0.75
  943.           if (dist < 1500) then
  944.             GiveOrderToUnit(queenID, CMD.MOVE, {(xQ + (math.sin(angle) * dist)), cC[2], (zQ + (math.cos(angle) * dist))}, {})
  945.             GiveOrderToUnit(queenID, CMD.FIGHT, cC, {"shift"})
  946.             if targetCache then addChickenTarget(queenID, targetCache) end
  947.             qDamage = (0 - mRandom(1,25000))
  948.             Wave()
  949.             qMove = true
  950.           else
  951.             idleOrderQueue[queenID] = {cmd = CMD.STOP, params = {}, opts = {}}
  952.             qDamage = 0
  953.             Wave()
  954.           end
  955.         else
  956.           idleOrderQueue[queenID] = {cmd = CMD.STOP, params = {}, opts = {}}
  957.           qDamage = 0
  958.         end
  959.       end
  960.     end
  961.   end
  962. end
  963.  
  964. function gadget:GameStart()
  965.   if warningMessage then
  966.     Spring.Echo("Warning: No Chicken team available, add a Chicken bot")
  967.     Spring.Echo("(Assigning Chicken Team to Gaia - AI: Custom)")
  968.   end
  969.   if (burrowSpawnType == "initialbox") or (burrowSpawnType == "alwaysbox") then
  970.     local _,_,_,_,_,luaAllyID = Spring.GetTeamInfo(chickenTeamID)
  971.     if luaAllyID then
  972.       lsx1,lsz1,lsx2,lsz2 = Spring.GetAllyTeamStartBox(luaAllyID)
  973.       if (not lsx1) or (not lsz1) or (not lsx2) or (not lsz2) then
  974.         burrowSpawnType = "avoid"
  975.         Spring.Echo("No Chicken start box available, Burrow Placement set to 'Avoid Players'")
  976.       elseif (lsx1 == 0) and (lsz1 == 0) and (lsx2 == Game.mapSizeX) and (lsz2 == Game.mapSizeX) then
  977.         burrowSpawnType = "avoid"
  978.         Spring.Echo("No Chicken start box available, Burrow Placement set to 'Avoid Players'")
  979.       end
  980.     end
  981.   end
  982. end
  983.  
  984. function gadget:GameFrame(n)
  985.      
  986.   if gameOver then return end
  987.  
  988.   if n == 15 then
  989.     DisableComputerUnits()
  990.   end
  991.  
  992.   if (chickenCount < maxChicken) then
  993.     local i,defs = next(spawnQueue)
  994.     if i and defs then
  995.       local x, y, z = getChickenSpawnLoc(defs.burrow, SMALL_UNIT)
  996.       if x and y and z then
  997.         local unitID = CreateUnit(defs.unitName, x,y,z, "n", defs.team)
  998.         if unitID then
  999.            SetUnitExperience(unitID, expMod)
  1000.            if (queenID) then
  1001.              local angle = math.rad(mRandom(1,360))
  1002.              idleOrderQueue[unitID] = {cmd = CMD.MOVE, params = {x - (math.sin(angle) * 360),y,z - (math.cos(angle) * 360)}, opts = {}}          
  1003.            else
  1004.              local chickenParams = ChooseTarget()
  1005.                      if targetCache and (unitID ~= queenID) and (mRandom(1,15) == 5) then
  1006.                    idleOrderQueue[unitID] = {cmd = CMD.ATTACK, params = {targetCache}, opts = {}}
  1007.                  else
  1008.                idleOrderQueue[unitID] = {cmd = CMD.FIGHT, params = chickenParams, opts = {}}
  1009.              end
  1010.              if targetCache then
  1011.                if GetUnitNeutral(targetCache) then
  1012.                  idleOrderQueue[unitID] = {cmd = CMD.ATTACK, params = {targetCache}, opts = {}}
  1013.                end
  1014.                addChickenTarget(unitID, targetCache)
  1015.              end
  1016.            end
  1017.            chickenBirths[unitID] = t
  1018.            chickenCount = chickenCount + 1
  1019.         end
  1020.       end
  1021.       spawnQueue[i] = nil
  1022.     end
  1023.   end
  1024.    
  1025.   for unitID in pairs(stunList) do
  1026.     stunList[unitID] = (stunList[unitID] - 1)
  1027.     if (stunList[unitID] <= 0) then
  1028.       SetUnitHealth(unitID, {paralyze=0})
  1029.       stunList[unitID] = nil
  1030.     end
  1031.   end  
  1032.  
  1033.   if (n >= timeCounter) then
  1034.     timeCounter = (n + UPDATE)
  1035.        
  1036.     t = GetGameSeconds()
  1037.     KillOldChicken()
  1038.    
  1039.     if (t < gracePeriod) then return end
  1040.    
  1041.     expMod = (expMod + expIncrement)
  1042.    
  1043.     if next(idleOrderQueue) then
  1044.       local processOrderQueue = {}
  1045.       for unitID,order in pairs(idleOrderQueue) do
  1046.         if GetUnitDefID(unitID) then
  1047.           processOrderQueue[unitID] = order
  1048.         end
  1049.       end
  1050.       idleOrderQueue = {}
  1051.       for unitID,order in pairs(processOrderQueue) do
  1052.         GiveOrderToUnit(unitID, order.cmd, order.params, order.opts)
  1053.         GiveOrderToUnit(unitID, CMD.MOVE_STATE, { mRandom(0,2) }, {})
  1054.       end
  1055.     end
  1056.          
  1057.     if (t >= (queenTime - queenAnger)) then
  1058.       if (not queenID) and (not gameOver) then
  1059.         queenID = SpawnQueen()
  1060.         idleOrderQueue[queenID] = {cmd = CMD.STOP, params = {}, opts = {}}
  1061.         _G.chickenEventArgs = {type="queen"}
  1062.         SendToUnsynced("ChickenEvent")
  1063.         _G.chickenEventArgs = nil
  1064.         _,queenMaxHP = GetUnitHealth(queenID)
  1065.         SetUnitExperience(queenID, (expMod*1.5))
  1066.         timeOfLastWave = t
  1067.         SKIRMISH[UnitDefNames["chickenc1"].id] = 150
  1068.         SKIRMISH[UnitDefNames["chickenf1"].id] = 1200
  1069.         SKIRMISH[UnitDefNames["chickenw1"].id] = 1800
  1070.                 COWARD[UnitDefNames["chicken2"].id] = nil
  1071.                 COWARD[UnitDefNames["chicken_dodo1"].id] = 300
  1072.             for i = 1,80,1 do
  1073.           table.insert(spawnQueue, {burrow = queenID, unitName = "chickenh4", team = chickenTeamID})
  1074.         end
  1075.       end
  1076.       updateQueenLife()
  1077.     end
  1078.        
  1079.     local quicken = 0
  1080.     local burrowCount = SetCount(burrows)
  1081.      
  1082.     if (burrowTarget > 0) and (burrowTarget ~= burrowCount) then
  1083.       quicken = (burrowSpawnRate * (1 - (burrowCount / burrowTarget)))
  1084.     end
  1085.        
  1086.     local burrowSpawnTime = (burrowSpawnRate - quicken)
  1087.              
  1088.     if (burrowSpawnRate < (t - timeOfLastFakeSpawn) and burrowCount < maxBurrows) then
  1089.       if firstSpawn then    
  1090.         minBurrows = SetCount(humanTeams)
  1091.         local hteamID = next(humanTeams)
  1092.         local ranCount = GetTeamUnitCount(hteamID)
  1093.         for i = 1,ranCount,1 do
  1094.           mRandom()
  1095.         end
  1096.         burrowTarget = math.min(math.ceil(minBurrows * 1.5) + gracePenalty, 40)
  1097.       else
  1098.         burrowTarget = burrowTarget + 1
  1099.       end
  1100.       timeOfLastFakeSpawn = t
  1101.     end
  1102.    
  1103.     if (burrowCount < minBurrows) or (burrowSpawnTime <  (t - timeOfLastSpawn) and burrowCount < maxBurrows) then
  1104.       if firstSpawn then
  1105.         for i = 1,math.min(math.ceil((SetCount(humanTeams) * 1.5)) + gracePenalty, 40),1 do
  1106.           SpawnBurrow()
  1107.         end
  1108.         timeOfLastWave = (t - (chickenSpawnRate - 6))
  1109.         firstSpawn = false
  1110.         if (burrowSpawnType == "initialbox") then burrowSpawnType = "avoid" end
  1111.       else
  1112.         SpawnBurrow()
  1113.       end
  1114.       if (burrowCount >= minBurrows) then timeOfLastSpawn = t end
  1115.       _G.chickenEventArgs = {type="burrowSpawn"}
  1116.       SendToUnsynced("ChickenEvent")
  1117.       _G.chickenEventArgs = nil
  1118.       SetGameRulesParam("roostCount", SetCount(burrows))
  1119.     end
  1120.    
  1121.     if (burrowCount > 0) and (chickenSpawnRate < (t - timeOfLastWave)) then
  1122.       local cCount = Wave()
  1123.       if cCount and cCount > 0 and (not queenID) then
  1124.         _G.chickenEventArgs = {type="wave", number=cCount, tech=currentWave}
  1125.         SendToUnsynced("ChickenEvent")
  1126.         _G.chickenEventArgs = nil
  1127.       end
  1128.       timeOfLastWave = t
  1129.     end
  1130.    
  1131.     chickenCount = UpdateUnitCount()
  1132.    
  1133.   end
  1134.  
  1135. end
  1136.  
  1137. function gadget:UnitDestroyed(unitID, unitDefID, unitTeam)
  1138.   --debug--Spring.Echo(t .. " UnitDestroyed " .. unitID)
  1139.   if gameOver then return end
  1140.    
  1141.   if chickenBirths[unitID] then chickenBirths[unitID] = nil end
  1142.   if turrets[unitID] then turrets[unitID] = nil end
  1143.   if idleOrderQueue[unitID] then idleOrderQueue[unitID] = nil end
  1144.   if failChickens[unitID] then failChickens[unitID] = nil end
  1145.  
  1146.   if chickenTargets[unitID] then
  1147.     if (unitTeam ~= chickenTeamID) then
  1148.       --debug--Spring.Echo(t .. " chickenTargets " .. unitID)
  1149.       for chickenID in pairs(chickenTargets[unitID]) do
  1150.         --debug--Spring.Echo(t .. " stopChicken " .. chickenID)
  1151.         if GetUnitDefID(chickenID) then
  1152.           idleOrderQueue[chickenID] = {cmd = CMD.STOP, params = {}, opts = {}}    
  1153.         end
  1154.       end
  1155.     elseif chickenTargets[chickenTargets[unitID]] then
  1156.       chickenTargets[chickenTargets[unitID]][unitID] = nil
  1157.     end
  1158.     chickenTargets[unitID] = nil
  1159.   end
  1160.  
  1161.   if (unitID == targetCache) then
  1162.     targetCache = nil
  1163.     targetCacheCount = math.huge
  1164.   end
  1165.  
  1166.   if (unitTeam == chickenTeamID) and chickenDefTypes[unitDefID] then
  1167.     local name = UnitDefs[unitDefID].name
  1168.     if unitDefID ~= burrowDef then name = string.sub(name,1,-2) end
  1169.     local kills = GetGameRulesParam(name.."Kills")
  1170.     SetGameRulesParam(name.."Kills", kills + 1)
  1171.     chickenCount = chickenCount - 1
  1172.   end
  1173.  
  1174.   if (unitID == queenID) then
  1175.     queenID = nil
  1176.     gameOver = true
  1177.     endScores = CalculateScores()
  1178.     endScores.type = "scores"
  1179.     _G.chickenEventArgs = endScores
  1180.     SendToUnsynced("ChickenEvent")
  1181.     _G.chickenEventArgs = nil
  1182.     KillAllComputerUnits()
  1183.     KillAllChicken()
  1184.   end
  1185.  
  1186.   if (unitDefID == burrowDef) and (not gameOver) then
  1187.  
  1188.     local kills = GetGameRulesParam(burrowName.."Kills")
  1189.     SetGameRulesParam(burrowName.."Kills", kills + 1)
  1190.    
  1191.     burrows[unitID] = nil
  1192.     if (addQueenAnger == 1) then
  1193.       queenAnger = (queenAnger + angerBonus)
  1194.       expMod = (expMod + (expIncrement * angerBonus))
  1195.       SetGameRulesParam("queenAnger", queenAnger)
  1196.       bonusPoints = (bonusPoints + 2500)    
  1197.     end
  1198.    
  1199.     for turretID,v in pairs(turrets) do
  1200.       if (v[1] == unitID) then
  1201.         DestroyUnit(turretID, true)
  1202.         turrets[turretID] = nil
  1203.       end
  1204.     end
  1205.      
  1206.     for burrowID in pairs(burrows) do
  1207.         SpawnTurret(burrowID, bonusTurret)
  1208.     end
  1209.        
  1210.     for i,defs in pairs(spawnQueue) do
  1211.       if (defs.burrow == unitID) then
  1212.         spawnQueue[i] = nil
  1213.       end
  1214.     end
  1215.    
  1216.     SetGameRulesParam("roostCount", SetCount(burrows))
  1217.   end
  1218.  
  1219. end
  1220.  
  1221. function gadget:TeamDied(teamID)
  1222.   if humanTeams[teamID] then
  1223.     if (minBurrows > 1) then minBurrows = (minBurrows - 1) end
  1224.   end
  1225.   humanTeams[teamID] = nil
  1226.   computerTeams[teamID] = nil
  1227.   if gameOver and showScoresOnce then
  1228.     DisplayScores(endScores)
  1229.     showScoresOnce = false
  1230.   end
  1231. end
  1232.  
  1233. function gadget:UnitTaken(unitID, unitDefID, oldTeam, newTeam)
  1234.   if (oldTeam == chickenTeamID) then
  1235.     DestroyUnit(unitID, true)
  1236.   end
  1237. end
  1238.  
  1239. function gadget:AllowUnitTransfer(unitID, unitDefID, oldTeam, newTeam, capture)
  1240.   if (newTeam == chickenTeamID) then
  1241.     return false
  1242.   else
  1243.     return true
  1244.   end
  1245. end
  1246.  
  1247. --------------------------------------------------------------------------------
  1248. --------------------------------------------------------------------------------
  1249. else
  1250. -- END SYNCED
  1251. -- BEGIN UNSYNCED
  1252. --------------------------------------------------------------------------------
  1253. --------------------------------------------------------------------------------
  1254.  
  1255. local Script = Script
  1256. local SYNCED = SYNCED
  1257.  
  1258. function WrapToLuaUI()
  1259.   if (Script.LuaUI('ChickenEvent')) then
  1260.     local chickenEventArgs = {}
  1261.     for k, v in spairs(SYNCED.chickenEventArgs) do
  1262.       chickenEventArgs[k] = v
  1263.     end
  1264.     Script.LuaUI.ChickenEvent(chickenEventArgs)
  1265.   end
  1266. end
  1267.  
  1268. function gadget:Initialize()
  1269.   gadgetHandler:AddSyncAction('ChickenEvent', WrapToLuaUI)
  1270. end
  1271.  
  1272. --------------------------------------------------------------------------------
  1273. --------------------------------------------------------------------------------
  1274. end
  1275. -- END UNSYNCED
  1276. --------------------------------------------------------------------------------
clone this paste RAW Paste Data