File content:

  1. modname = "balance"
  2. version = "0.1"
  3.  
  4. function et_InitGame(levelTime,randomSeed,restart)
  5. et.RegisterModname(modname .. " " .. version)
  6. end
  7.  
  8. unevenDiff = 2
  9. max_unevenTime = 30
  10. max_unevenDiff = 4
  11.  
  12. axisPlayers = {}
  13. alliedPlayers = {}
  14. unevenTime = -1
  15.  
  16. function et_RunFrame( levelTime )
  17. local numAlliedPlayers = table.getn( alliedPlayers )
  18. local numAxisPlayers = table.getn( axisPlayers )
  19. if numAlliedPlayers >= numAxisPlayers + max_unevenDiff then
  20. local clientNum = alliedPlayers[ numAlliedPlayers ]
  21. et.trap_SendConsoleCommand( et.EXEC_APPEND, "ref putaxis " .. clientNum .. " r" )
  22. et.trap_SendServerCommand(-1, "chat \"balancing teams... " .. et.gentity_get( clientNum, "pers.netname" ) .. "^7 moved to ^1AXIS\"" )
  23. elseif numAxisPlayers >= numAlliedPlayers + max_unevenDiff then
  24. local clientNum = axisPlayers[ numAxisPlayers ]
  25. et.trap_SendConsoleCommand( et.EXEC_APPEND, "ref putallies " .. clientNum .. " b" )
  26. et.trap_SendServerCommand(-1, "chat \"balancing teams... " .. et.gentity_get( clientNum, "pers.netname" ) .. "^7 moved to ^4ALLIES\"" )
  27. elseif numAlliedPlayers >= numAxisPlayers + unevenDiff then
  28. if unevenTime > 0 then
  29. if tonumber( levelTime ) - unevenTime >= max_unevenTime * 1000 then
  30. local clientNum = alliedPlayers[ numAlliedPlayers ]
  31. et.trap_SendConsoleCommand( et.EXEC_APPEND, "ref putaxis " .. clientNum .. " r" )
  32. et.trap_SendServerCommand(-1, "chat \"balancing teams... " .. et.gentity_get( clientNum, "pers.netname" ) .. "^7 moved to ^1AXIS\"" )
  33. end
  34. else
  35. unevenTime = tonumber( levelTime )
  36. end
  37. elseif numAxisPlayers >= numAlliedPlayers + unevenDiff then
  38. if unevenTime > 0 then
  39. if tonumber( levelTime ) - unevenTime >= max_unevenTime * 1000 then
  40. local clientNum = axisPlayers[ numAxisPlayers ]
  41. et.trap_SendConsoleCommand( et.EXEC_APPEND, "ref putallies " .. clientNum .. " b" )
  42. et.trap_SendServerCommand(-1, "chat \"balancing teams... " .. et.gentity_get( clientNum, "pers.netname" ) .. "^7 moved to ^4ALLIES\"" )
  43. end
  44. else
  45. unevenTime = tonumber( levelTime )
  46. end
  47. else
  48. unevenTime = -1
  49. end
  50. end
  51.  
  52. function et_ClientSpawn( clientNum, revived, teamChange, restoreHealth )
  53. if teamChange ~= 0 then
  54. local team = tonumber( et.gentity_get( clientNum, "sess.sessionTeam" ) )
  55. -- these were the teamnumbers prior to the move
  56. local numAlliedPlayers = table.getn( alliedPlayers )
  57. local numAxisPlayers = table.getn( axisPlayers )
  58. if team == 1 then
  59. for i, num in ipairs( alliedPlayers ) do
  60. if num == clientNum then
  61. table.remove( alliedPlayers, i )
  62. break
  63. end
  64. end
  65. -- this should not happen but still check for it to avoid doubles
  66. for i, num in ipairs( axisPlayers ) do
  67. if num == clientNum then
  68. return
  69. end
  70. end
  71. -- make sure a player who (got) moved when teams were uneven doesn't get moved right back
  72. if numAlliedPlayers >= numAxisPlayers + unevenDiff then
  73. table.insert( axisPlayers, 1, clientNum )
  74. else
  75. table.insert( axisPlayers, clientNum )
  76. end
  77. elseif team == 2 then
  78. for i, num in ipairs( axisPlayers ) do
  79. if num == clientNum then
  80. table.remove( axisPlayers, i )
  81. break
  82. end
  83. end
  84. for i, num in ipairs( alliedPlayers ) do
  85. if num == clientNum then
  86. return
  87. end
  88. end
  89. if numAxisPlayers >= numAlliedPlayers + unevenDiff then
  90. table.insert( alliedPlayers, 1, clientNum )
  91. else
  92. table.insert( alliedPlayers, clientNum )
  93. end
  94. else
  95. for i, num in ipairs( alliedPlayers ) do
  96. if num == clientNum then
  97. table.remove( alliedPlayers, i )
  98. return
  99. end
  100. end
  101. for i, num in ipairs( axisPlayers ) do
  102. if num == clientNum then
  103. table.remove( axisPlayers, i )
  104. return
  105. end
  106. end
  107. end
  108. end
  109. end
  110.  
  111. function et_ClientDisconnect( clientNum )
  112. for i, num in ipairs( alliedPlayers ) do
  113. if num == clientNum then
  114. table.remove( alliedPlayers, i )
  115. return
  116. end
  117. end
  118. for i, num in ipairs( axisPlayers ) do
  119. if num == clientNum then
  120. table.remove( axisPlayers, i )
  121. return
  122. end
  123. end
  124. end