2016-08-20 04:52:27 +02:00
|
|
|
-- imports
|
|
|
|
|
2016-08-27 08:44:17 +02:00
|
|
|
local entityUtils = require("libs/EntityUtils")
|
2016-08-07 05:38:47 +02:00
|
|
|
local unitGroupUtils = require("libs/UnitGroupUtils")
|
2016-08-05 06:47:51 +02:00
|
|
|
local chunkProcessor = require("libs/ChunkProcessor")
|
|
|
|
local mapProcessor = require("libs/MapProcessor")
|
2016-08-04 00:22:20 +02:00
|
|
|
local constants = require("libs/Constants")
|
2016-08-05 06:47:51 +02:00
|
|
|
local pheromoneUtils = require("libs/PheromoneUtils")
|
2016-08-18 07:55:08 +02:00
|
|
|
local aiDefense = require("libs/AIDefense")
|
|
|
|
local aiAttack = require("libs/AIAttack")
|
2016-08-19 04:02:13 +02:00
|
|
|
local aiBuilding = require("libs/AIBuilding")
|
2016-08-22 00:59:17 +02:00
|
|
|
local tests = require("tests")
|
2016-08-04 00:22:20 +02:00
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
-- imported functions
|
|
|
|
|
|
|
|
local processPendingChunks = chunkProcessor.processPendingChunks
|
|
|
|
|
|
|
|
local processMap = mapProcessor.processMap
|
|
|
|
|
|
|
|
local accumulatePoints = aiBuilding.accumulatePoints
|
|
|
|
local removeScout = aiBuilding.removeScout
|
|
|
|
local scouting = aiBuilding.scouting
|
|
|
|
|
|
|
|
local playerScent = pheromoneUtils.playerScent
|
|
|
|
local deathScent = pheromoneUtils.deathScent
|
|
|
|
|
|
|
|
local regroupSquads = unitGroupUtils.regroupSquads
|
|
|
|
local convertUnitGroupToSquad = unitGroupUtils.convertUnitGroupToSquad
|
|
|
|
|
|
|
|
local squadAttackPlayer = aiAttack.squadAttackPlayer
|
|
|
|
local squadAttackLocation = aiAttack.squadAttackLocation
|
|
|
|
local squadBeginAttack = aiAttack.squadBeginAttack
|
|
|
|
|
|
|
|
local retreatUnits = aiDefense.retreatUnits
|
|
|
|
|
2016-08-27 08:44:17 +02:00
|
|
|
local addRemoveEntity = entityUtils.addRemoveEntity
|
2016-08-20 04:52:27 +02:00
|
|
|
|
2016-08-25 02:05:20 +02:00
|
|
|
-- local references to global
|
|
|
|
|
|
|
|
local regionMap
|
|
|
|
local natives
|
|
|
|
local pendingChunks
|
|
|
|
|
2016-07-30 00:44:31 +02:00
|
|
|
-- hook functions
|
|
|
|
|
|
|
|
function onInit()
|
2016-08-20 04:52:27 +02:00
|
|
|
-- print("init")
|
2016-08-05 06:47:51 +02:00
|
|
|
global.regionMap = {}
|
|
|
|
global.pendingChunks = {}
|
|
|
|
global.natives = {}
|
2016-08-04 00:22:20 +02:00
|
|
|
|
2016-08-25 02:05:20 +02:00
|
|
|
regionMap = global.regionMap
|
|
|
|
natives = global.natives
|
|
|
|
pendingChunks = global.pendingChunks
|
2016-08-27 08:44:17 +02:00
|
|
|
|
|
|
|
onConfigChanged()
|
2016-07-30 00:44:31 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function onLoad()
|
2016-08-20 04:52:27 +02:00
|
|
|
-- print("load")
|
2016-08-25 02:05:20 +02:00
|
|
|
regionMap = global.regionMap
|
|
|
|
natives = global.natives
|
|
|
|
pendingChunks = global.pendingChunks
|
|
|
|
end
|
|
|
|
|
|
|
|
function onConfigChanged()
|
2016-08-27 08:44:17 +02:00
|
|
|
-- print("reprocess")
|
2016-08-25 02:05:20 +02:00
|
|
|
if (global.version == nil) then
|
|
|
|
regionMap.pQ = {{}} -- processing queue
|
|
|
|
regionMap.pI = 1 -- insertion location for chunk processing
|
|
|
|
regionMap.pP = 1 -- index for the chunk set to process
|
|
|
|
regionMap.pR = -1 -- current processing roll
|
|
|
|
natives.squads = {}
|
|
|
|
natives.scouts = {}
|
|
|
|
natives.tunnels = {}
|
|
|
|
natives.points = 0
|
|
|
|
pendingChunks = {}
|
|
|
|
|
2016-08-27 08:44:17 +02:00
|
|
|
global.version = constants.VERSION_5
|
|
|
|
end
|
|
|
|
if (global.version < constants.VERSION_8) then
|
|
|
|
|
|
|
|
-- queue all current chunks that wont be generated during play
|
2016-08-25 02:05:20 +02:00
|
|
|
local surface = game.surfaces[1]
|
|
|
|
for chunk in surface.get_chunks() do
|
|
|
|
onChunkGenerated({ surface = surface,
|
|
|
|
area = { left_top = { x = chunk.x * 32,
|
|
|
|
y = chunk.y * 32 }}})
|
|
|
|
end
|
2016-08-27 08:44:17 +02:00
|
|
|
global.version = constants.VERSION_8
|
2016-08-26 00:20:06 +02:00
|
|
|
end
|
2016-07-30 00:44:31 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function onChunkGenerated(event)
|
2016-08-04 02:22:27 +02:00
|
|
|
-- queue generated chunk for delayed processing, queuing is required because some mods (RSO) mess with chunk as they
|
|
|
|
-- are generated, which messes up the scoring.
|
2016-08-05 06:47:51 +02:00
|
|
|
if (event.surface.index == 1) then
|
2016-08-25 02:05:20 +02:00
|
|
|
pendingChunks[#pendingChunks+1] = event
|
2016-08-05 06:47:51 +02:00
|
|
|
end
|
2016-08-04 00:22:20 +02:00
|
|
|
end
|
|
|
|
|
2016-08-03 17:54:21 +02:00
|
|
|
function onTick(event)
|
2016-08-20 20:24:22 +02:00
|
|
|
if (event.tick % 20 == 0) then
|
2016-08-20 04:52:27 +02:00
|
|
|
local surface = game.surfaces[1]
|
2016-08-04 00:22:20 +02:00
|
|
|
|
2016-08-20 21:04:04 +02:00
|
|
|
if (event.tick % 40 == 0) then
|
2016-08-20 20:24:22 +02:00
|
|
|
|
2016-08-25 02:05:20 +02:00
|
|
|
accumulatePoints(natives)
|
2016-08-20 20:24:22 +02:00
|
|
|
|
|
|
|
-- put down player pheromone for player hunters
|
2016-08-25 02:05:20 +02:00
|
|
|
playerScent(regionMap, game.players)
|
2016-08-20 20:24:22 +02:00
|
|
|
|
2016-08-25 02:05:20 +02:00
|
|
|
regroupSquads(natives)
|
2016-08-26 00:20:06 +02:00
|
|
|
|
|
|
|
-- scouting(regionMap, natives)
|
2016-08-27 08:44:17 +02:00
|
|
|
|
|
|
|
squadBeginAttack(natives, game.players, game.evolution_factor)
|
2016-08-25 02:05:20 +02:00
|
|
|
squadAttackLocation(regionMap, surface, natives)
|
2016-08-27 08:44:17 +02:00
|
|
|
squadAttackPlayer(regionMap, surface, natives)
|
2016-08-20 20:24:22 +02:00
|
|
|
end
|
2016-08-21 23:48:55 +02:00
|
|
|
|
2016-08-25 02:05:20 +02:00
|
|
|
processPendingChunks(regionMap, surface, natives, pendingChunks)
|
2016-08-20 04:52:27 +02:00
|
|
|
|
2016-08-25 02:05:20 +02:00
|
|
|
processMap(regionMap, surface, natives, game.evolution_factor)
|
2016-08-20 20:24:22 +02:00
|
|
|
end
|
2016-08-05 06:47:51 +02:00
|
|
|
end
|
|
|
|
|
2016-08-18 07:55:08 +02:00
|
|
|
function onBuild(event)
|
2016-08-27 08:44:17 +02:00
|
|
|
addRemoveEntity(regionMap, event.created_entity, natives, true)
|
2016-08-18 07:55:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function onPickUp(event)
|
2016-08-27 08:44:17 +02:00
|
|
|
addRemoveEntity(regionMap, event.entity, natives, false)
|
2016-08-18 07:55:08 +02:00
|
|
|
end
|
|
|
|
|
2016-08-05 06:47:51 +02:00
|
|
|
function onDeath(event)
|
|
|
|
local entity = event.entity
|
2016-08-27 08:44:17 +02:00
|
|
|
local surface = entity.surface
|
|
|
|
if (surface.index == 1) then
|
2016-08-25 01:30:45 +02:00
|
|
|
if (entity.force.name == "enemy") then
|
|
|
|
if (entity.type == "unit") then
|
|
|
|
local entityPosition = entity.position
|
|
|
|
|
|
|
|
-- drop death pheromone where unit died
|
2016-08-27 08:44:17 +02:00
|
|
|
deathScent(regionMap, entityPosition)
|
2016-08-25 01:30:45 +02:00
|
|
|
|
2016-08-27 08:44:17 +02:00
|
|
|
if (event.force ~= nil) and (event.force.name == "player") then
|
|
|
|
retreatUnits(entityPosition,
|
|
|
|
convertUnitGroupToSquad(natives,
|
|
|
|
entity.unit_group),
|
|
|
|
regionMap,
|
|
|
|
surface,
|
|
|
|
natives)
|
2016-08-25 01:30:45 +02:00
|
|
|
end
|
|
|
|
|
2016-08-26 00:20:06 +02:00
|
|
|
removeScout(entity, global.natives)
|
2016-08-25 01:30:45 +02:00
|
|
|
elseif (entity.type == "unit-spawner") then
|
2016-08-27 08:44:17 +02:00
|
|
|
addRemoveEntity(regionMap, entity, natives, false)
|
2016-08-18 07:55:08 +02:00
|
|
|
end
|
2016-08-25 01:30:45 +02:00
|
|
|
elseif (entity.force.name == "player") then
|
2016-08-27 08:44:17 +02:00
|
|
|
addRemoveEntity(regionMap, entity, natives, false)
|
2016-08-09 04:54:41 +02:00
|
|
|
end
|
2016-08-03 17:54:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-21 02:28:35 +02:00
|
|
|
function onPutItem(event)
|
2016-08-21 23:48:55 +02:00
|
|
|
-- local player = game.players[event.player_index]
|
|
|
|
-- if (player.surface.index==1) then
|
|
|
|
-- aiBuilding.fillTunnel(global.regionMap, player.surface, global.natives, event.positions)
|
|
|
|
-- end
|
2016-08-21 02:28:35 +02:00
|
|
|
end
|
|
|
|
|
2016-08-04 02:22:27 +02:00
|
|
|
-- hooks
|
|
|
|
|
2016-07-30 00:44:31 +02:00
|
|
|
script.on_init(onInit)
|
|
|
|
script.on_load(onLoad)
|
2016-08-25 02:05:20 +02:00
|
|
|
script.on_configuration_changed(onConfigChanged)
|
2016-07-30 00:44:31 +02:00
|
|
|
|
2016-08-21 02:28:35 +02:00
|
|
|
script.on_event(defines.events.on_player_built_tile, onPutItem)
|
|
|
|
|
2016-08-18 07:55:08 +02:00
|
|
|
script.on_event({defines.events.on_preplayer_mined_item,
|
|
|
|
defines.events.on_robot_pre_mined},
|
|
|
|
onPickUp)
|
|
|
|
script.on_event({defines.events.on_built_entity,
|
|
|
|
defines.events.on_robot_built_entity},
|
|
|
|
onBuild)
|
|
|
|
|
2016-08-05 06:47:51 +02:00
|
|
|
script.on_event(defines.events.on_entity_died, onDeath)
|
2016-08-20 04:52:27 +02:00
|
|
|
script.on_event(defines.events.on_tick, onTick)
|
2016-07-30 00:44:31 +02:00
|
|
|
script.on_event(defines.events.on_chunk_generated, onChunkGenerated)
|
|
|
|
|
2016-08-03 17:54:21 +02:00
|
|
|
remote.add_interface("rampant", {
|
2016-08-05 06:47:51 +02:00
|
|
|
test1 = tests.test1,
|
2016-08-18 07:55:08 +02:00
|
|
|
test2 = tests.test2,
|
2016-08-19 00:14:40 +02:00
|
|
|
test3 = tests.test3,
|
2016-08-19 04:02:13 +02:00
|
|
|
test4 = tests.test4,
|
2016-08-21 02:28:35 +02:00
|
|
|
test5 = tests.test5,
|
|
|
|
test6 = tests.test6,
|
|
|
|
test7 = tests.test7,
|
2016-08-26 00:20:06 +02:00
|
|
|
test8 = tests.test8,
|
|
|
|
test9 = tests.test9,
|
2016-08-27 08:44:17 +02:00
|
|
|
test10 = tests.test10
|
2016-08-03 17:54:21 +02:00
|
|
|
})
|