PageRenderTime 33ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/evolutiondev.sdd/LuaRules/Gadgets/share_levels.lua

http://evolutionrts.googlecode.com/
Lua | 231 lines | 171 code | 40 blank | 20 comment | 47 complexity | 6758452c042c61c300ec7f26479c029d MD5 | raw file
  1. --------------------------------------------------------------------------------
  2. --------------------------------------------------------------------------------
  3. --
  4. -- file: share_levels.lua
  5. -- brief: control resource sharing
  6. -- author: Dave Rodgers
  7. --
  8. -- Copyright (C) 2007.
  9. -- Licensed under the terms of the GNU GPL, v2 or later.
  10. --
  11. --------------------------------------------------------------------------------
  12. --------------------------------------------------------------------------------
  13. function gadget:GetInfo()
  14. return {
  15. name = "ShareLevels",
  16. desc = "Control resource sharing",
  17. author = "trepan",
  18. date = "Apr 22, 2007",
  19. license = "GNU GPL, v2 or later",
  20. layer = -5,
  21. enabled = true -- loaded by default?
  22. }
  23. end
  24. --------------------------------------------------------------------------------
  25. --------------------------------------------------------------------------------
  26. local metalForce = true
  27. local metalLevel = 0.95
  28. local energyForce = true
  29. local energyLevel = 0.9
  30. -- for toggling
  31. local prevMetal = false
  32. local prevEnery = false
  33. local cmdName = "sharelevels"
  34. local help = " <on|off|list|toggle|<<e|m|*> [<on|off|%>]>]>"
  35. .. ": control automatic team resource sharing"
  36. --------------------------------------------------------------------------------
  37. local function AllowAction(playerID)
  38. if (playerID ~= 0) then
  39. Spring.SendMessageToPlayer(playerID, "Must be the host player")
  40. return false
  41. end
  42. if (not Spring.IsCheatingEnabled()) then
  43. Spring.SendMessageToPlayer(playerID, "Cheating must be enabled")
  44. return false
  45. end
  46. return true
  47. end
  48. local function PrintHelp()
  49. Spring.Echo(help)
  50. return true
  51. end
  52. local function PrintState()
  53. if (not metalForce) then
  54. Spring.Echo('forced metal sharing is disabled')
  55. else
  56. Spring.Echo(string.format(
  57. 'forced metal sharing level set to: %i%%',
  58. ((1 - metalLevel) * 100)
  59. ))
  60. end
  61. if (not energyForce) then
  62. Spring.Echo('forced energy sharing is disabled')
  63. else
  64. Spring.Echo(string.format(
  65. 'forced energy sharing level set to: %i%%',
  66. ((1 - energyLevel) * 100)
  67. ))
  68. end
  69. return true
  70. end
  71. local function ToggleBoth()
  72. local hadPrev = (prevMetal or prevEnergy)
  73. if (metalForce or energyForce) then
  74. prevMetal = metalForce
  75. prevEnergy = energyForce
  76. metalForce = false
  77. energyForce = false
  78. elseif (hadPrev) then
  79. metalForce = prevMetal
  80. energyForce = prevEnergy
  81. else
  82. metalForce = true
  83. energyForce = true
  84. end
  85. end
  86. local function ChatControl(cmd, line, words, playerID)
  87. if (not AllowAction(playerID)) then
  88. PrintState()
  89. return true
  90. end
  91. local w1, w2 = words[1], words[2]
  92. if ((w1 == 'list') or (w1 == 'l')) then
  93. return PrintState()
  94. elseif ((w1 == 'toggle') or (w1 == 't')) then
  95. ToggleBoth()
  96. return PrintState()
  97. elseif (w1 == 'on') then
  98. metalForce = true
  99. energyForce = true
  100. return PrintState()
  101. elseif (w1 == 'off') then
  102. metalForce = false
  103. energyForce = false
  104. return PrintState()
  105. end
  106. if (not (w1 == 'e') and
  107. not (w1 == 'm') and
  108. not (w1 == '*')) then
  109. return PrintHelp()
  110. end
  111. if (w2 == nil) then
  112. if (w1 == 'm') then
  113. metalForce = not metalForce
  114. return PrintState()
  115. elseif (w1 == 'e') then
  116. energyForce = not energyForce
  117. return PrintState()
  118. else
  119. ToggleBoth()
  120. return PrintState()
  121. end
  122. end
  123. if ((w2 == 'on') or (w2 == 'off')) then
  124. local state = (w2 == 'on')
  125. if (w1 == 'm') then
  126. metalForce = state
  127. return PrintState()
  128. elseif (w1 == 'e') then
  129. energyForce = state
  130. return PrintState()
  131. else
  132. metalForce = state
  133. energyForce = state
  134. return PrintState()
  135. end
  136. return PrintHelp()
  137. end
  138. local level = tonumber(w2)
  139. if (level == nil) then
  140. PrintHelp()
  141. Spring.Echo("bad sharing level: " .. w2)
  142. return true
  143. end
  144. level = (100 - level) * 0.01
  145. if (level < 0) then level = 0 end
  146. if (level > 1) then level = 1 end
  147. if ((w1 == 'm') or (w1 == '*')) then
  148. metalLevel = level
  149. metalForce = true
  150. end
  151. if ((w1 == 'e') or (w1 == '*')) then
  152. energyLevel = level
  153. energyForce = true
  154. end
  155. return PrintState()
  156. end
  157. --------------------------------------------------------------------------------
  158. function gadget:Initialize()
  159. if (not gadgetHandler:IsSyncedCode()) then
  160. gadgetHandler:RemoveGadget()
  161. return
  162. end
  163. gadgetHandler:AddChatAction(cmdName, ChatControl, help)
  164. Script.AddActionFallback(cmdName .. ' ', help)
  165. end
  166. function gadget:Shutdown()
  167. gadgetHandler:RemoveChatAction(cmdName, ChatControl)
  168. end
  169. --------------------------------------------------------------------------------
  170. local GetTeamList = Spring.GetTeamList
  171. local SetTeamShareLevel = Spring.SetTeamShareLevel
  172. function gadget:GameFrame()
  173. if (not (metalForce or energyForce)) then
  174. return
  175. end
  176. for _,team in ipairs(GetTeamList()) do
  177. if (metalForce) then SetTeamShareLevel(team, 'm', metalLevel) end
  178. if (energyForce) then SetTeamShareLevel(team, 'e', energyLevel) end
  179. end
  180. return false
  181. end
  182. function gadget:AllowResourceLevel(teamID, type, level)
  183. if (type == 'm') then
  184. return not metalForce
  185. elseif (type == 'e') then
  186. return not energyForce
  187. end
  188. return true
  189. end
  190. --------------------------------------------------------------------------------
  191. --------------------------------------------------------------------------------