Advertisement
Guest User

Excess Fail Save v0.1

a guest
Feb 23rd, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. local versionNumber = "v0.1"
  2. function widget:GetInfo()
  3.   return {
  4.     name      = "Excess Fail Save".. versionNumber,
  5.     desc      = "Distribute unused metal to the next best player",
  6.     author    = "author: MoriturusMortus",
  7.     date      = "Aug 04, 2013",
  8.     license   = "GNU GPL, v2 or later",
  9.     layer     = 0,
  10.     enabled   = false -- loaded by default?
  11.   }
  12. end
  13. --------------------------------------------------------------------------------
  14. --Constant:
  15. local uPDATE            = 0.8       --determine coophelper's temporal spacing (original=0.2 sec)
  16. local EchoLimit         = 8     --limits echo outpput
  17. --------------------------------------------------------------------------------
  18. --Variable:
  19. local timeCounter       = 0
  20. local myTeamID          = -1
  21. local myAllyTeamID      = -1
  22. local PlayerList        = {}
  23. local allyList          = {}
  24. local start         = true
  25. local GCount        = 0
  26. --------------------------------------------------------------------------------
  27. --Functions:
  28. local GetTeamResources      = Spring.GetTeamResources
  29. local GetMyTeamID           = Spring.GetMyTeamID
  30. local GetMyAllyTeamID       = Spring.GetMyAllyTeamID
  31. local GetPlayerList     = Spring.GetPlayerList
  32. local GetPlayerInfo     = Spring.GetPlayerInfo
  33. local GetGameSeconds        = Spring.GetGameSeconds
  34. local Echo          = Spring.Echo
  35. --------------------------------------------------------------------------------
  36. --Methods:
  37.  
  38. function widget:Initialize()
  39.     local _, _, spec = Spring.GetPlayerInfo(Spring.GetMyPlayerID())
  40.     if spec then widgetHandler:RemoveWidget() return false end
  41.     myTeamID = GetMyTeamID()
  42.     myAllyTeamID = GetMyAllyTeamID()
  43.     PlayerList = GetPlayerList()
  44. end
  45.  
  46. function widget:Update(deltaTime)
  47.     if (timeCounter < uPDATE) then
  48.         timeCounter = timeCounter + deltaTime
  49.         return
  50.     end
  51.     if start and GetGameSeconds() >= 1 then
  52.         local count = 1
  53.                 local allyListElo = {}
  54.                 for i = 1, #PlayerList do
  55.                     local PlayerID=PlayerList[i]
  56.                     local Name,Active,Spectator,TeamID,AllyTeamID,_,_,_,_,CustomKeys = GetPlayerInfo(PlayerID)
  57.                     if CustomKeys and Active and not Spectator and TeamID ~= myTeamID and AllyTeamID == myAllyTeamID then
  58.                         allyList[count] =  {
  59.                                             ID = PlayerID,
  60.                                             ELO = CustomKeys.elo,
  61.                                             }
  62.                         Echo (count)
  63.                         count = count + 1
  64.                         Echo ("Player"..Name.." added!")
  65.                     end
  66.                 end
  67.                 table.sort(allyList, function(a,b)
  68.                         return a.ELO > b.ELO
  69.                     end
  70.                 )
  71.                 for i = 1, #allyList do
  72.                     local Name,Active,_,allyID,_,_,_,_,_,_ = GetPlayerInfo(allyList[i].ID)
  73.                     Echo ("Player"..Name.." "..i)
  74.                 end
  75.         start = false
  76.     end
  77.    
  78.     local mCur,mMax,_,mIncome,mExpense,_,_,_ = GetTeamResources(myTeamID, "metal")
  79.     local mPlus=mIncome-mExpense
  80.     local mSeer=mMax-mPlus
  81.    
  82.     if mCur >= mSeer then
  83.         for i = 1, #allyList do
  84.             local Name,Active,_,allyID,_,_,_,_,_,_ = GetPlayerInfo(allyList[i].ID)
  85.             local mAllyCur,mAllyMax,_,mAllyIncome,mAllyExpense,_,_,_ = GetTeamResources(allyID, "metal")
  86.             local mAllyPlus = mAllyIncome - mAllyExpense
  87.             local mAllySeer = mAllyMax - (mAllyPlus + mIncome)
  88.             if Active and mAllyCur<100 then
  89.                 Spring.ShareResources(allyID, "metal", mIncome)
  90.                 if (GCount >= EchoLimit) then
  91.                     GCount = GCount + 1
  92.                 else
  93.                     Echo ("Insufficient usage of your resources, your overhead will be distributed to:"..Name)
  94.                     GCount = 0
  95.                 end
  96.                 break
  97.             end
  98.         end
  99.     end
  100.    
  101.     timeCounter = 0
  102.    
  103. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement