1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-22 03:08:51 +02:00
Rampant/control.lua

215 lines
7.2 KiB
Lua
Raw Normal View History

-- imports
local chunkUtils = require("libs/ChunkUtils")
local mapUtils = require("libs/MapUtils")
2016-08-06 20:38:47 -07: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-18 19:02:13 -07:00
local aiBuilding = require("libs/AIBuilding")
2016-08-21 15:59:17 -07:00
local tests = require("tests")
-- 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
local addRemoveObject = mapUtils.addRemoveObject
2016-08-24 17:05:20 -07:00
-- local references to global
local regionMap
local natives
local pendingChunks
-- hook functions
function onInit()
-- print("init")
2016-08-24 17:05:20 -07:00
global.version = constants.VERSION_5
global.regionMap = {}
global.pendingChunks = {}
global.natives = {}
2016-08-24 17:05:20 -07:00
regionMap = global.regionMap
natives = global.natives
pendingChunks = global.pendingChunks
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
2016-08-21 15:48:28 -07:00
-- game.forces.player.research_all_technologies()
2016-08-20 11:24:22 -07:00
-- queue all current chunks that wont be generated during play
local surface = game.surfaces[1]
for chunk in surface.get_chunks() do
onChunkGenerated({ surface = surface,
2016-08-20 11:24:22 -07:00
area = { left_top = { x = chunk.x * 32,
y = chunk.y * 32 }}})
end
end
function onLoad()
-- print("load")
2016-08-24 17:05:20 -07:00
regionMap = global.regionMap
natives = global.natives
pendingChunks = global.pendingChunks
end
function onConfigChanged()
if (global.version == nil) then
-- print("reprocess")
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 = {}
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_5
end
end
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
2016-08-24 17:05:20 -07:00
pendingChunks[#pendingChunks+1] = event
end
end
function onTick(event)
2016-08-20 11:24:22 -07:00
if (event.tick % 20 == 0) then
local surface = game.surfaces[1]
2016-08-20 12:04:04 -07:00
if (event.tick % 40 == 0) then
2016-08-21 15:48:28 -07:00
-- if not game.players[1].cheat_mode then
-- game.players[1].cheat_mode = true
-- end
2016-08-20 11:24:22 -07:00
2016-08-24 17:05:20 -07:00
accumulatePoints(natives)
2016-08-20 11:24:22 -07:00
-- put down player pheromone for player hunters
2016-08-24 17:05:20 -07:00
playerScent(regionMap, game.players)
2016-08-20 11:24:22 -07:00
2016-08-24 17:05:20 -07:00
regroupSquads(natives)
2016-08-20 11:24:22 -07:00
-- scouting(regionMap, surface, natives)
2016-08-20 11:24:22 -07:00
2016-08-24 17:05:20 -07:00
squadAttackPlayer(natives, game.players)
2016-08-20 11:24:22 -07:00
2016-08-24 17:05:20 -07:00
squadBeginAttack(natives)
squadAttackLocation(regionMap, surface, natives)
2016-08-20 11:24:22 -07:00
end
2016-08-21 14:48:55 -07:00
2016-08-24 17:05:20 -07:00
processPendingChunks(regionMap, surface, natives, pendingChunks)
2016-08-24 17:05:20 -07:00
processMap(regionMap, surface, natives, game.evolution_factor)
2016-08-20 11:24:22 -07:00
end
end
function onBuild(event)
2016-08-24 17:05:20 -07:00
addRemoveObject(regionMap, event.created_entity, natives, true)
end
function onPickUp(event)
2016-08-24 17:05:20 -07:00
addRemoveObject(regionMap, event.entity, natives, false)
end
function onDeath(event)
local entity = event.entity
if (entity.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-24 17:05:20 -07:00
deathScent(regionMap, entityPosition.x, entityPosition.y)
if (event.force ~= nil) and (event.force.name == "player") then
2016-08-24 17:05:20 -07:00
local squad = convertUnitGroupToSquad(natives, entity.unit_group)
retreatUnits(entityPosition, squad, regionMap, game.surfaces[1], natives)
end
-- removeScout(entity, global.natives)
elseif (entity.type == "unit-spawner") then
2016-08-24 17:05:20 -07:00
addRemoveObject(regionMap, entity, natives, false)
end
elseif (entity.force.name == "player") then
2016-08-24 17:05:20 -07:00
addRemoveObject(regionMap, entity, natives, false)
end
end
end
function onPutItem(event)
2016-08-21 14:48:55 -07: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
-- hooks
script.on_init(onInit)
script.on_load(onLoad)
2016-08-24 17:05:20 -07:00
script.on_configuration_changed(onConfigChanged)
script.on_event(defines.events.on_player_built_tile, onPutItem)
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)
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-18 19:02:13 -07:00
test4 = tests.test4,
test5 = tests.test5,
test6 = tests.test6,
test7 = tests.test7,
test8 = tests.test8
})