Advertisement
Guest User

Untitled

a guest
Aug 10th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. require "defines"
  2. local update_com_count = 80
  3. local agro_area_rad = 40
  4. local call_back_area_rad = agro_area_rad + 15
  5. local max_unit_count = 20
  6.  
  7.  
  8. game.on_init(function()
  9. if global.evolution == nil then
  10. global.evolution = {}
  11. end
  12. end)
  13.  
  14. game.on_load(function()
  15. if global.evolution == nil then
  16. global.evolution = {}
  17. end
  18. for i = 1, #game.players, 1 do
  19. if game.players[i].force.technologies["Hive-mind"].researched then
  20. game.players[i].force.recipes["attractor-on2"].enabled = true
  21. end
  22. end
  23. end)
  24.  
  25. game.on_event(defines.events.on_tick, function(event)
  26. if event.tick % update_com_count == 0 then
  27. for index, player in ipairs(game.players) do
  28. if player.character then
  29. UpdateUnitsCommands(index)
  30. end
  31. end
  32. end
  33. end)
  34.  
  35. function UpdateUnitsCommands(player_index)
  36. local player = game.players[player_index].character
  37. local pos = player.position
  38. local aggression_area = {{pos.x - agro_area_rad, pos.y - agro_area_rad}, {pos.x + agro_area_rad, pos.y + agro_area_rad}}
  39. if not player.surface.valid then return end
  40. local targets = player.surface.find_entities(aggression_area)
  41. local min_dist = agro_area_rad + 10;
  42. local closest_index = -1
  43. local surface = player.surface
  44.  
  45. for index, target in ipairs(targets) do
  46. if target.health then
  47. if target.force == game.forces.enemy and target.type ~= "turret" and target.type ~= "unit" then
  48. local dist = GetDistance(target.position, pos)
  49. if min_dist > dist then
  50. min_dist = dist
  51. closest_index = index
  52. end
  53. end
  54. end
  55. end
  56.  
  57. local unit_count = 0
  58. if closest_index == -1 then
  59.  
  60. local attOn = game.players[player_index].get_item_count("attractor-on")
  61. local attOff = game.players[player_index].get_item_count("attractor-off")
  62. local lastState = nil
  63. if global.evolution[game.players[player_index].name] and global.evolution[game.players[player_index].name].lastState then
  64. lastState = global.evolution[game.players[player_index].name].lastState
  65. else
  66. if global.evolution[game.players[player_index].name] == nil then
  67. global.evolution[game.players[player_index].name] = {}
  68. end
  69. global.evolution[game.players[player_index].name].lastState = nil
  70. end
  71.  
  72. if attOn > 0 and attOff == 0 then
  73. if attOn > 1 then
  74. game.players[player_index].removeitem({name="attractor-on", count=(attOn - 1)})
  75. end
  76. lastState = "on"
  77. elseif attOn == 0 and attOff > 0 then
  78. if attOff > 1 then
  79. game.players[player_index].removeitem({name="attractor-off", count=(attOff - 1)})
  80. end
  81. lastState = "off"
  82. elseif attOn > 0 and attOff > 0 then
  83. if lastState ~= nil and lastState == "off" then
  84. game.players[player_index].removeitem({name="attractor-off", count=attOff})
  85. if attOn > 1 then
  86. game.players[player_index].removeitem({name="attractor-on", count=(attOn - 1)})
  87. end
  88. lastState = "on"
  89. else
  90. game.players[player_index].removeitem({name="attractor-on", count=attOn})
  91. if attOn > 1 then
  92. game.players[player_index].removeitem({name="attractor-on", count=(attOn - 1)})
  93. end
  94. lastState = "off"
  95. end
  96. else
  97. lastState = "off"
  98. end
  99. global.evolution[game.players[player_index].name].lastState = lastState
  100.  
  101. if lastState == "off" then return end
  102. local call_back_area = {{pos.x - call_back_area_rad, pos.y - call_back_area_rad}, {pos.x + call_back_area_rad, pos.y + call_back_area_rad}}
  103. local biters = surface.find_entities_filtered{area = call_back_area, type = "unit"}
  104. for index, biter in ipairs(biters) do
  105. if biter.force == (player.force) then
  106. biter.set_command({type = defines.command.go_to_location, destination = pos, radius = 10, distraction = defines.distraction.byanything});
  107. unit_count = unit_count + 1
  108.  
  109. end
  110. if unit_count > max_unit_count then return end
  111. end
  112. else
  113. local call_back_area = {{pos.x - call_back_area_rad, pos.y - call_back_area_rad}, {pos.x + call_back_area_rad, pos.y + call_back_area_rad}}
  114. local biters = player.surface.find_entities_filtered{area = call_back_area, type = "unit"}
  115. for index, biter in ipairs(biters) do
  116. if biter.force == player.force then
  117. biter.set_command({type = defines.command.attack, target = targets[closest_index], distraction = defines.distraction.byanything});
  118. unit_count = unit_count + 1
  119. end
  120. if unit_count > max_unit_count then return end
  121. end
  122. end
  123. end
  124.  
  125. function GetDistance(pos1 , pos2)
  126. return math.sqrt((pos1.x - pos2.x)^2 + (pos1.y - pos2.y)^2)
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement