1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-07 23:01:39 +02:00
Rampant/control.lua

230 lines
7.7 KiB
Lua
Raw Normal View History

-- imports
local entityUtils = require("libs/EntityUtils")
2016-08-07 05:38:47 +02:00
local unitGroupUtils = require("libs/UnitGroupUtils")
local chunkProcessor = require("libs/ChunkProcessor")
local mapProcessor = require("libs/MapProcessor")
local constants = require("libs/Constants")
local pheromoneUtils = require("libs/PheromoneUtils")
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")
-- imported functions
local processPendingChunks = chunkProcessor.processPendingChunks
local processMap = mapProcessor.processMap
2016-08-29 02:05:28 +02:00
local scanMap = mapProcessor.scanMap
local accumulatePoints = aiBuilding.accumulatePoints
local removeScout = aiBuilding.removeScout
2016-09-13 00:33:00 +02:00
-- local scouting = aiBuilding.scouting
local playerScent = pheromoneUtils.playerScent
local deathScent = pheromoneUtils.deathScent
local regroupSquads = unitGroupUtils.regroupSquads
local convertUnitGroupToSquad = unitGroupUtils.convertUnitGroupToSquad
local squadAttack = aiAttack.squadAttack
local squadBeginAttack = aiAttack.squadBeginAttack
local retreatUnits = aiDefense.retreatUnits
local addRemoveEntity = entityUtils.addRemoveEntity
2016-08-25 02:05:20 +02:00
-- local references to global
local regionMap
local natives
2016-08-29 02:05:28 +02:00
local pheromoneTotals
2016-08-25 02:05:20 +02:00
local pendingChunks
-- hook functions
2016-09-13 00:33:00 +02:00
local function onLoad()
-- print("load")
2016-08-25 02:05:20 +02:00
regionMap = global.regionMap
natives = global.natives
pendingChunks = global.pendingChunks
2016-08-29 02:05:28 +02:00
pheromoneTotals = global.pheromoneTotals
end
2016-09-13 00:33:00 +02:00
local function onChunkGenerated(event)
-- 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.
if (event.surface.index == 1) then
pendingChunks[#pendingChunks+1] = event
end
2016-08-25 02:05:20 +02:00
end
2016-09-13 00:33:00 +02:00
local function onConfigChanged()
-- print("reprocess")
2016-08-25 02:05:20 +02:00
if (global.version == nil) then
2016-08-29 02:05:28 +02:00
-- removed in version 9
-- 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
2016-08-25 02:05:20 +02:00
natives.squads = {}
natives.scouts = {}
natives.tunnels = {}
natives.points = 0
pendingChunks = {}
global.version = constants.VERSION_5
end
if (global.version < constants.VERSION_9) then
2016-08-29 02:05:28 +02:00
-- remove version 5 references
regionMap.pQ = nil
regionMap.pI = nil
regionMap.pP = nil
regionMap.pR = nil
regionMap.processQueue = {}
regionMap.processPointer = 1
regionMap.scanPointer = 1
regionMap.processRoll = -1
pheromoneTotals[constants.DEATH_PHEROMONE] = 0
pheromoneTotals[constants.ENEMY_BASE_PHEROMONE] = 0
pheromoneTotals[constants.PLAYER_PHEROMONE] = 0
pheromoneTotals[constants.PLAYER_BASE_PHEROMONE] = 0
pheromoneTotals[constants.PLAYER_DEFENSE_PHEROMONE] = 0
pheromoneTotals[constants.MOVEMENT_PHEROMONE] = 0
-- 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
global.version = constants.VERSION_9
2016-08-26 00:20:06 +02:00
end
end
2016-09-13 00:33:00 +02:00
local function onTick(event)
2016-08-20 20:24:22 +02:00
if (event.tick % 20 == 0) then
2016-09-13 00:33:00 +02:00
local surface = game.surfaces[1]
2016-09-13 00:33:00 +02:00
processPendingChunks(regionMap, surface, pendingChunks)
scanMap(regionMap, surface)
2016-08-20 21:04:04 +02:00
if (event.tick % 40 == 0) then
2016-08-20 20:24:22 +02:00
2016-09-09 12:19:31 +02:00
accumulatePoints(natives)
2016-08-20 20:24:22 +02:00
-- put down player pheromone for player hunters
2016-09-09 12:19:31 +02:00
playerScent(regionMap, game.players)
2016-08-20 20:24:22 +02:00
regroupSquads(natives)
2016-08-26 00:20:06 +02:00
-- scouting(regionMap, natives)
2016-09-09 12:19:31 +02:00
squadBeginAttack(natives, game.players, game.evolution_factor)
squadAttack(regionMap, surface, natives)
2016-08-20 20:24:22 +02:00
end
2016-09-13 00:33:00 +02:00
processMap(regionMap, surface, natives, game.evolution_factor)
2016-08-20 20:24:22 +02:00
end
end
2016-09-13 00:33:00 +02:00
local function onBuild(event)
addRemoveEntity(regionMap, event.created_entity, natives, true)
end
2016-09-13 00:33:00 +02:00
local function onPickUp(event)
addRemoveEntity(regionMap, event.entity, natives, false)
end
2016-09-13 00:33:00 +02:00
local function onDeath(event)
local entity = event.entity
local surface = entity.surface
if (surface.index == 1) then
if (entity.force.name == "enemy") then
if (entity.type == "unit") then
local entityPosition = entity.position
-- drop death pheromone where unit died
2016-08-29 02:05:28 +02:00
deathScent(regionMap, entityPosition, pheromoneTotals)
if (event.force ~= nil) and (event.force.name == "player") then
retreatUnits(entityPosition,
convertUnitGroupToSquad(natives,
entity.unit_group),
regionMap,
surface,
natives)
end
2016-08-26 00:20:06 +02:00
removeScout(entity, global.natives)
elseif (entity.type == "unit-spawner") then
addRemoveEntity(regionMap, entity, natives, false)
end
elseif (entity.force.name == "player") then
addRemoveEntity(regionMap, entity, natives, false)
end
end
end
local function onSurfaceTileChange(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
end
2016-09-13 00:33:00 +02:00
local function onInit()
-- print("init")
global.regionMap = {}
global.pendingChunks = {}
global.natives = {}
global.pheromoneTotals = {}
regionMap = global.regionMap
natives = global.natives
pendingChunks = global.pendingChunks
pheromoneTotals = global.pheromoneTotals
onConfigChanged()
end
-- hooks
script.on_init(onInit)
script.on_load(onLoad)
2016-08-25 02:05:20 +02:00
script.on_configuration_changed(onConfigChanged)
script.on_event(defines.events.on_player_built_tile, onSurfaceTileChange)
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-09-09 12:02:50 +02:00
script.on_event(defines.events.on_entity_died, onDeath)
script.on_event(defines.events.on_tick, onTick)
script.on_event(defines.events.on_chunk_generated, onChunkGenerated)
remote.add_interface("rampant", {
test1 = tests.test1,
test2 = tests.test2,
test3 = tests.test3,
2016-08-19 04:02:13 +02:00
test4 = tests.test4,
test5 = tests.test5,
test6 = tests.test6,
test7 = tests.test7,
2016-08-26 00:20:06 +02:00
test8 = tests.test8,
test9 = tests.test9,
test10 = tests.test10,
test11 = tests.test11
})