Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.69 KB | None | 0 0
  1. --==========================================================================================================================
  2. -- VARIABLES
  3. --==========================================================================================================================
  4. -- DB stuff
  5. ----------------------------------------------------------------------------------------------------------------------------
  6. local promotionRedDevilAfterRandom          = GameInfoTypes["PROMOTION_DMS_RED_DEVIL_AFTERRANDOM"]
  7. local promotionRedDevil                     = GameInfoTypes["PROMOTION_DMS_RED_DEVIL"]
  8. ----------------------------------------------------------------------------------------------------------------------------
  9. -- Adjustables
  10. ----------------------------------------------------------------------------------------------------------------------------
  11. local splashDamageRange                     = 1
  12. local splashDamageUnitAmount                = 15
  13. local splashDamageCityAmount                = 15
  14. ----------------------------------------------------------------------------------------------------------------------------
  15. -- debug
  16. ----------------------------------------------------------------------------------------------------------------------------
  17. local printDebug                            = true
  18. ----------------------------------------------------------------------------------------------------------------------------
  19. -- other stuff
  20. ----------------------------------------------------------------------------------------------------------------------------
  21. local activePlayer                          = Game.GetActivePlayer()
  22. local mathMin                               = math.min
  23. ----------------------------------------------------------------------------------------------------------------------------
  24. -- GameEvents / Events
  25. ----------------------------------------------------------------------------------------------------------------------------
  26. local UnitPrekill                           = GameEvents.UnitPrekill.Add
  27. local PlayerDoTurn                          = GameEvents.PlayerDoTurn.Add
  28. local SerialEventUnitSetDamage              = Events.SerialEventUnitSetDamage.Add
  29. --==========================================================================================================================
  30. -- DEBUG
  31. --==========================================================================================================================
  32. -- DMS_Print
  33. ----------------------------------------------------------------------------------------------------------------------------
  34. local function DMS_Print(str)
  35.     if printDebug == false then return end
  36.     return print(str)
  37. end
  38. --==========================================================================================================================
  39. -- FUNCTIONS
  40. --==========================================================================================================================
  41. -- DMS_Biafra_UU_IsRedDevil
  42. ----------------------------------------------------------------------------------------------------------------------------
  43. local function DMS_Biafra_UU_IsRedDevil(plot)
  44.     local numUnits = plot:GetNumUnits()
  45.     local unit = "NONE"
  46.     if numUnits == 0 then DMS_Print("DMS_Biafra_UU_IsRedDevil: no units found on plot (" .. plot:GetX() .. ", " .. plot:GetY() .. ")..") return false end
  47.     for i = 0, numUnits - 1 do
  48.         local unit = plot:GetUnit(i)
  49.         if unit ~= nil and unit ~= -1 then
  50.             DMS_Print("DMS_Biafra_UU_IsRedDevil: found unit (" .. unit:GetName() .. ") on plot (" .. plot:GetX() .. ", " .. plot:GetY() .. ")..")
  51.             if unit:IsHasPromotion(promotionRedDevilAfterRandom) then
  52.                 DMS_Print("DMS_Biafra_UU_IsRedDevil: unit has RedDevilAfterRandom promotion, return info to DMS_Biafra_UU_CombatListener..")
  53.                 return unit, true
  54.             end
  55.         end
  56.     end
  57.     DMS_Print("DMS_Biafra_UU_IsRedDevil: no unit or no unit with RedDevilAfterRandom promotion found, return info to DMS_Biafra_UU_CombatListener..")
  58.     return unit, false
  59. end
  60. ----------------------------------------------------------------------------------------------------------------------------
  61. -- DMS_Biafra_UU_SplashDamageUnit
  62. ----------------------------------------------------------------------------------------------------------------------------
  63. local function DMS_Biafra_UU_SplashDamageUnit(player, unit, plot)
  64.     local numUnits = plot:GetNumUnits()
  65.     if numUnits == 0 then return end
  66.     DMS_Print("DMS_Biafra_UU_SplashDamageUnit: plot (" .. plot:GetX() .. ", " .. plot:GetY() .. ") has unit to be 'splash damaged'..")
  67.     for i = 0, numUnits - 1 do
  68.         local plotUnit = plot:GetUnit(i)
  69.         if plotUnit ~= nil and plotUnit ~= -1 then
  70.             DMS_Print("DMS_Biafra_UU_SplashDamageUnit: unit (" .. unit:GetName() .. ") is owned by player " .. Players[unit:GetOwner()]:GetCivilizationShortDescription() .. "..")
  71.             if unit ~= plotUnit then
  72.                 local damage = mathMin(plotUnit:GetMaxHitPoints() - plotUnit:GetDamage(), splashDamageUnitAmount)
  73.                 plotUnit:ChangeDamage(damage)
  74.                 DMS_Print("DMS_Biafra_UU_SplashDamageUnit: damage dealt is: " .. damage .. "..")
  75.             else
  76.                 DMS_Print("DMS_Biafra_UU_SplashDamageUnit: unit (" .. unit:GetName() .. ") is actually the unit performing the splash attack, return..")
  77.             end
  78.         end
  79.     end
  80. end
  81. ----------------------------------------------------------------------------------------------------------------------------
  82. -- DMS_Biafra_UU_SplashDamageCities
  83. ----------------------------------------------------------------------------------------------------------------------------
  84. local function DMS_Biafra_UU_SplashDamageCities(player, plot)
  85.     if not plot:IsCity() then return end
  86.     DMS_Print("DMS_Biafra_UU_SplashDamageCities: plot (" .. plot:GetX() .. ", " .. plot:GetY() .. ") has city to be 'splash damaged'..")
  87.     local city = plot:GetPlotCity()
  88.     if city == nil or city == -1 then return end
  89.     DMS_Print("DMS_Biafra_UU_SplashDamageCities: city (" .. city:GetName() .. ") is owned by player " .. Players[city:GetOwner()]:GetCivilizationShortDescription() .. "..")
  90.     local damage = mathMin(city:GetMaxHitPoints() - city:GetDamage(), splashDamageCityAmount)
  91.     city:ChangeDamage(damage)
  92.     DMS_Print("DMS_Biafra_UU_SplashDamageCities: damage dealt is: " .. damage .. "..")
  93. end
  94. ----------------------------------------------------------------------------------------------------------------------------
  95. -- DMS_Biafra_UU_DoSplashDamage
  96. ----------------------------------------------------------------------------------------------------------------------------
  97. local function DMS_Biafra_UU_DoSplashDamage(player, unit, plot)
  98.     DMS_Print("DMS_Biafra_UU_DoSplashDamage: perform splash damage from attack on plot (" .. plot:GetX() .. ", " .. plot:GetY() .. "), iterate nearby plots..")
  99.     for nearbyPlot in PlotAreaSweepIterator(plot, splashDamageRange, SECTOR_NORTH, DIRECTION_CLOCKWISE, DIRECTION_OUTWARDS, CENTRE_EXCLUDE) do
  100.         DMS_Print("DMS_Biafra_UU_DoSplashDamage: checking nearby plot (" .. nearbyPlot:GetX() .. ", " .. nearbyPlot:GetY() .. ")..")
  101.         DMS_Biafra_UU_SplashDamageUnit(player, unit, plot)
  102.         DMS_Biafra_UU_SplashDamageCities(player, plot)
  103.     end
  104. end
  105. ----------------------------------------------------------------------------------------------------------------------------
  106. -- DMS_Biafra_UU_ResetPromotions
  107. ----------------------------------------------------------------------------------------------------------------------------
  108. local function DMS_Biafra_UU_ResetPromotions(unit)
  109.     if unit:IsHasPromotion(promotionRedDevilAfterRandom) then
  110.         DMS_Print("DMS_Biafra_UU_ResetPromotions: found unit with RedDevilAfterRandom promotion..")
  111.         unit:SetHasPromotion(promotionRedDevilAfterRandom, false)
  112.         unit:SetHasPromotion(promotionRedDevil, true)
  113.         DMS_Print("DMS_Biafra_UU_ResetPromotions: promotions reset..")
  114.     end
  115. end
  116. ----------------------------------------------------------------------------------------------------------------------------
  117. -- DMS_Biafra_UU_CombatListener
  118. ----------------------------------------------------------------------------------------------------------------------------
  119. local function DMS_Biafra_UU_CombatListener(player, plot, isRedDevil)
  120.     local unit, redDevil = isRedDevil
  121.     DMS_Print("DMS_Biafra_UU_CombatListener: info from DMS_Biafra_UU_IsRedDevil received..")
  122.     if unit == "NONE" then DMS_Print("DMS_Biafra_UU_CombatListener: no unit found..") return false end
  123.     if redDevil == false then DMS_Print("DMS_Biafra_UU_CombatListener: unit did not have correct promotion, return..") return false end
  124.     DMS_Print("DMS_Biafra_UU_CombatListener: call DMS_Biafra_UU_DoSplashDamage..")
  125.     DMS_Biafra_UU_DoSplashDamage(player, unit, plot)
  126.     DMS_Print("DMS_Biafra_UU_CombatListener: call DMS_Biafra_UU_ResetPromotions..")
  127.     DMS_Biafra_UU_ResetPromotions(unit)
  128.     DMS_Print("DMS_Biafra_UU_CombatListener: return culprit found..")
  129.     return redDevil
  130. end
  131. --==========================================================================================================================
  132. -- MAIN FUNCTIONS
  133. --==========================================================================================================================
  134. -- DMS_Biafra_UnitPrekill
  135. ----------------------------------------------------------------------------------------------------------------------------
  136. local UPK_RedDevil = "NONE"
  137. local function DMS_Biafra_UnitPrekill(killedUnitPlayerID, unitID, unitType, x, y, bDelay, killerUnitPlayerID)
  138.     if bDelay then UPK_RedDevil = killerUnitPlayerID; DMS_Print("DMS_Biafra_UnitPrekill: bDelay is true, return..") return end
  139.     if UPK_RedDevil == -1 then UPK_RedDevil= "NONE"; DMS_Print("DMS_Biafra_UnitPrekill: UPK_RedDevil is nil, return..") return end
  140.     local killedUnitPlayer = Players[killedUnitPlayerID]
  141.     if killedUnitPlayer == nil or killedUnitPlayer == -1 then DMS_Print("DMS_Biafra_UnitPrekill: killedUnitPlayer is nil, return..") return end
  142.     local killedUnit = killedUnitPlayer:GetUnitByID(unitID)
  143.     if killedUnit == nil or killedUnit == -1 then DMS_Print("DMS_Biafra_UnitPrekill: killedUnit is nil, return..") return end
  144.     local killerUnitPlayer = Players[UPK_RedDevil]
  145.     UPK_RedDevil = "NONE"
  146.     if killerUnitPlayer == nil or killerUnitPlayer == -1 then DMS_Print("DMS_Biafra_UnitPrekill: killerUnitPlayer is nil, return..") return end
  147.     DMS_Print("DMS_Biafra_UnitPrekill: killerUnitPlayer is " .. killerUnitPlayer:GetCivilizationShortDescription() .. "..")
  148.     local plot = Map.GetPlot(x, y)
  149.     if plot == nil or plot == -1 then return end
  150.     DMS_Print("DMS_Biafra_UnitPrekill: killed unit is on plot (" .. plot:GetX() .. ", " .. plot:GetY() .. "), iterate nearby plots..")
  151.     for nearbyPlot in PlotAreaSweepIterator(plot, 4, SECTOR_NORTH, DIRECTION_CLOCKWISE, DIRECTION_OUTWARDS, CENTRE_INCLUDE) do
  152.         DMS_Print("DMS_Biafra_UnitPrekill: checking nearby plot (" .. nearbyPlot:GetX() .. ", " .. nearbyPlot:GetY() .. ")..")
  153.         splashDamagePerformed = DMS_Biafra_UU_CombatListener(killerUnitPlayer, nearbyPlot, DMS_Biafra_UU_IsRedDevil(nearbyPlot))
  154.         if splashDamagePerformed then DMS_Print("DMS_Biafra_UnitPrekill: culprit found on plot (" .. nearbyPlot:GetX() .. ", " .. nearbyPlot:GetY() .. "), break plot iteration..") break end
  155.     end
  156. end
  157. ----------------------------------------------------------------------------------------------------------------------------
  158. -- DMS_Biafra_SerialEventUnitSetDamage
  159. ----------------------------------------------------------------------------------------------------------------------------
  160. local function DMS_Biafra_SerialEventUnitSetDamage(playerID, unitID, newDamage, oldDamage)
  161.     if newDamage < oldDamage then return end
  162.     local player = Players[playerID]
  163.     DMS_Print("DMS_Biafra_SerialEventUnitSetDamage: iterate units for player " .. player:GetCivilizationShortDescription() .. " (ID " .. playerID .. ") to see if attack is by Red Devil Unit..")
  164.     for iterUnit in player:Units() do
  165.         if iterUnit:GetID() == unitID then
  166.             local plot = iterUnit:GetPlot()
  167.             if plot ~= nil and plot ~= -1 then
  168.                 DMS_Print("DMS_Biafra_SerialEventUnitSetDamage: damaged unit is on plot (" .. plot:GetX() .. ", " .. plot:GetY() .. "), iterate nearby plots..")
  169.                 for nearbyPlot in PlotAreaSweepIterator(plot, 4, SECTOR_NORTH, DIRECTION_CLOCKWISE, DIRECTION_OUTWARDS, CENTRE_INCLUDE) do
  170.                     DMS_Print("DMS_Biafra_SerialEventUnitSetDamage: checking nearby plot (" .. nearbyPlot:GetX() .. ", " .. nearbyPlot:GetY() .. ")..")
  171.                     splashDamagePerformed = DMS_Biafra_UU_CombatListener(player, nearbyPlot, DMS_Biafra_UU_IsRedDevil(nearbyPlot))
  172.                     if splashDamagePerformed then DMS_Print("DMS_Biafra_SerialEventUnitSetDamage: culprit found on plot (" .. nearbyPlot:GetX() .. ", " .. nearbyPlot:GetY() .. "), break plot iteration..") break end
  173.                 end
  174.             end
  175.         end
  176.     end
  177.     DMS_Print("DMS_Biafra_SerialEventUnitSetDamage: unit iteration done for player " .. player:GetCivilizationShortDescription() .. " (ID " .. playerID .. ")..")
  178. end
  179. ----------------------------------------------------------------------------------------------------------------------------
  180. -- DMS_Biafra_PlayerDoTurn
  181. ----------------------------------------------------------------------------------------------------------------------------
  182. local function DMS_Biafra_PlayerDoTurn(playerID)
  183.     local player = Players[playerID]
  184.     if player == nil or player == -1 then return end
  185.     DMS_Print("DMS_Biafra_PlayerDoTurn: iterate units for player " .. player:GetCivilizationShortDescription() .. " (ID " .. playerID .. ") to reset possible Red Devil Unit Promotions..")
  186.     for unit in player:Units() do
  187.         DMS_Biafra_UU_ResetPromotions(unit)
  188.     end
  189.     DMS_Print("DMS_Biafra_PlayerDoTurn: unit iteration done for player " .. player:GetCivilizationShortDescription() .. " (ID " .. playerID .. ")..")
  190. end
  191. --==========================================================================================================================
  192. -- GAMEEVENTS / EVENTS HOOKS
  193. --==========================================================================================================================
  194. UnitPrekill(DMS_Biafra_UnitPrekill)
  195. PlayerDoTurn(DMS_Biafra_PlayerDoTurn)
  196. SerialEventUnitSetDamage(DMS_Biafra_SerialEventUnitSetDamage)
  197. --==========================================================================================================================
  198. --==========================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement