2016-08-04 00:22:20 +02:00
|
|
|
local chunkUtils = require("libs/ChunkUtils")
|
|
|
|
local regionUtils = require("libs/RegionUtils")
|
|
|
|
local constants = require("libs/Constants")
|
2016-08-04 02:22:27 +02:00
|
|
|
local ai = require("libs/AI")
|
|
|
|
local tests = require("Tests")
|
2016-08-04 00:22:20 +02:00
|
|
|
|
2016-08-04 02:22:27 +02:00
|
|
|
local pheromoneRoutine --coroutine holding state of in progress processing
|
2016-08-04 00:22:20 +02:00
|
|
|
local chunkRoutine
|
2016-07-30 00:44:31 +02:00
|
|
|
|
2016-08-04 02:22:27 +02:00
|
|
|
local regionMaps -- chunk based map
|
|
|
|
local chunkProcessingQueue -- pending chunks to be processed
|
|
|
|
local units -- units that are being commanded
|
2016-08-04 00:22:20 +02:00
|
|
|
|
2016-07-30 00:44:31 +02:00
|
|
|
|
|
|
|
-- hook functions
|
|
|
|
|
|
|
|
function onInit()
|
2016-08-04 02:22:27 +02:00
|
|
|
print("init")
|
2016-07-30 00:44:31 +02:00
|
|
|
global.regionMaps = {}
|
2016-08-04 00:22:20 +02:00
|
|
|
global.chunkProcessingQueue = {}
|
2016-08-04 02:22:27 +02:00
|
|
|
global.units = {}
|
2016-08-04 00:22:20 +02:00
|
|
|
|
2016-07-30 00:44:31 +02:00
|
|
|
regionMaps = global.regionMaps
|
2016-08-04 00:22:20 +02:00
|
|
|
chunkProcessingQueue = global.chunkProcessingQueue
|
2016-08-04 02:22:27 +02:00
|
|
|
units = global.units
|
2016-08-04 00:22:20 +02:00
|
|
|
|
2016-08-04 02:22:27 +02:00
|
|
|
-- turn off enemy ai
|
2016-08-03 17:54:21 +02:00
|
|
|
game.surfaces[1].peaceful_mode = true
|
2016-08-04 02:22:27 +02:00
|
|
|
-- remove enemies that aren't off
|
|
|
|
game.forces.enemy.kill_all_units()
|
|
|
|
|
|
|
|
-- 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,
|
|
|
|
area={left_top={x=chunk.x * 32,
|
|
|
|
y=chunk.y * 32}}})
|
|
|
|
end
|
2016-07-30 00:44:31 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function onLoad()
|
2016-08-04 02:22:27 +02:00
|
|
|
print("load")
|
2016-07-30 00:44:31 +02:00
|
|
|
regionMaps = global.regionMaps
|
2016-08-04 00:22:20 +02:00
|
|
|
chunkProcessingQueue = global.chunkProcessingQueue
|
2016-08-04 02:22:27 +02:00
|
|
|
units = global.units
|
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-04 00:22:20 +02:00
|
|
|
chunkProcessingQueue[#chunkProcessingQueue+1] = event
|
|
|
|
end
|
|
|
|
|
2016-08-03 17:54:21 +02:00
|
|
|
function onTick(event)
|
2016-08-04 00:22:20 +02:00
|
|
|
if (event.tick % 45 == 0) then
|
2016-08-04 02:22:27 +02:00
|
|
|
-- using coroutines to keep the cpu load time managable will still being able to work large maps
|
|
|
|
|
2016-08-04 00:22:20 +02:00
|
|
|
if (#chunkProcessingQueue > 0) and ((chunkRoutine == nil) or (coroutine.status(chunkRoutine)=="dead")) then
|
2016-08-04 02:22:27 +02:00
|
|
|
-- coroutines start suspended, so you have to resume them after creation
|
|
|
|
chunkRoutine = coroutine.create(chunkUtils.chunkProcess)
|
|
|
|
end
|
|
|
|
if (chunkRoutine ~= nil) then
|
|
|
|
coroutine.resume(chunkRoutine)
|
2016-08-04 00:22:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if (pheromoneRoutine == nil) or (coroutine.status(pheromoneRoutine)=="dead") then
|
2016-08-04 02:22:27 +02:00
|
|
|
pheromoneRoutine = coroutine.create(regionUtils.pheromoneProcess)
|
|
|
|
end
|
|
|
|
if (pheromoneRoutine ~= nil) then
|
|
|
|
coroutine.resume(pheromoneRoutine)
|
2016-08-04 00:22:20 +02:00
|
|
|
end
|
2016-08-04 02:22:27 +02:00
|
|
|
|
|
|
|
|
2016-08-03 17:54:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-04 02:22:27 +02:00
|
|
|
-- setup variables in the various modules
|
2016-08-03 17:54:21 +02:00
|
|
|
function onInitialTick(event)
|
2016-08-04 02:22:27 +02:00
|
|
|
ai.init(regionMaps, units)
|
|
|
|
regionUtils.init(regionMaps)
|
|
|
|
chunkUtils.init(regionMaps, chunkProcessingQueue)
|
|
|
|
|
|
|
|
-- used for debugging
|
|
|
|
tests.initTester()
|
|
|
|
|
|
|
|
-- swap to real on tick function
|
2016-08-03 17:54:21 +02:00
|
|
|
script.on_event(defines.events.on_tick, onTick)
|
2016-07-30 00:44:31 +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-03 17:54:21 +02:00
|
|
|
script.on_event(defines.events.on_tick, onInitialTick)
|
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-04 02:22:27 +02:00
|
|
|
test1 = tests.test1
|
2016-08-03 17:54:21 +02:00
|
|
|
})
|