mirror of
https://github.com/veden/Rampant.git
synced 2025-09-16 09:16:43 +02:00
adding some comments, will do more later
This commit is contained in:
106
control.lua
106
control.lua
@@ -1,72 +1,94 @@
|
||||
local chunkUtils = require("libs/ChunkUtils")
|
||||
local regionUtils = require("libs/RegionUtils")
|
||||
local constants = require("libs/Constants")
|
||||
local ai = require("libs/AI")
|
||||
local tests = require("Tests")
|
||||
|
||||
local pheromoneRoutine
|
||||
local pheromoneRoutine --coroutine holding state of in progress processing
|
||||
local chunkRoutine
|
||||
|
||||
local regionMaps
|
||||
local chunkProcessingQueue
|
||||
local regionMaps -- chunk based map
|
||||
local chunkProcessingQueue -- pending chunks to be processed
|
||||
local units -- units that are being commanded
|
||||
|
||||
|
||||
-- hook functions
|
||||
|
||||
function onInit()
|
||||
print("init")
|
||||
global.regionMaps = {}
|
||||
global.chunkProcessingQueue = {}
|
||||
global.units = {}
|
||||
|
||||
regionMaps = global.regionMaps
|
||||
chunkProcessingQueue = global.chunkProcessingQueue
|
||||
units = global.units
|
||||
|
||||
-- turn off enemy ai
|
||||
game.surfaces[1].peaceful_mode = true
|
||||
-- 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
|
||||
end
|
||||
|
||||
function onLoad()
|
||||
print("load")
|
||||
regionMaps = global.regionMaps
|
||||
chunkProcessingQueue = global.chunkProcessingQueue
|
||||
units = global.units
|
||||
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.
|
||||
chunkProcessingQueue[#chunkProcessingQueue+1] = event
|
||||
end
|
||||
|
||||
function chunkProcess()
|
||||
chunkUtils.processChunks(regionMaps, chunkProcessingQueue)
|
||||
end
|
||||
|
||||
function pheromoneProcess()
|
||||
local player = game.players[1]
|
||||
regionUtils.placePheromone(regionMaps[player.surface.index], player.position.x, player.position.y, constants.PLAYER_PHEROMONE, 100)
|
||||
regionUtils.processPheromone(regionMaps[player.surface.index], player.surface, 2)
|
||||
end
|
||||
|
||||
function onTick(event)
|
||||
if (event.tick % 45 == 0) then
|
||||
-- using coroutines to keep the cpu load time managable will still being able to work large maps
|
||||
|
||||
if (#chunkProcessingQueue > 0) and ((chunkRoutine == nil) or (coroutine.status(chunkRoutine)=="dead")) then
|
||||
chunkRoutine = coroutine.create(chunkProcess)
|
||||
-- coroutines start suspended, so you have to resume them after creation
|
||||
chunkRoutine = coroutine.create(chunkUtils.chunkProcess)
|
||||
end
|
||||
if (chunkRoutine ~= nil) then
|
||||
coroutine.resume(chunkRoutine)
|
||||
end
|
||||
coroutine.resume(chunkRoutine)
|
||||
|
||||
if (pheromoneRoutine == nil) or (coroutine.status(pheromoneRoutine)=="dead") then
|
||||
pheromoneRoutine = coroutine.create(pheromoneProcess)
|
||||
pheromoneRoutine = coroutine.create(regionUtils.pheromoneProcess)
|
||||
end
|
||||
coroutine.resume(pheromoneRoutine)
|
||||
if (pheromoneRoutine ~= nil) then
|
||||
coroutine.resume(pheromoneRoutine)
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
-- function pheromones(event)
|
||||
-- local player = event.player
|
||||
|
||||
-- end
|
||||
|
||||
-- hooks
|
||||
|
||||
-- setup variables in the various modules
|
||||
function onInitialTick(event)
|
||||
-- initTester()
|
||||
--tester()
|
||||
ai.init(regionMaps, units)
|
||||
regionUtils.init(regionMaps)
|
||||
chunkUtils.init(regionMaps, chunkProcessingQueue)
|
||||
|
||||
-- used for debugging
|
||||
tests.initTester()
|
||||
|
||||
-- swap to real on tick function
|
||||
script.on_event(defines.events.on_tick, onTick)
|
||||
end
|
||||
|
||||
-- hooks
|
||||
|
||||
script.on_init(onInit)
|
||||
script.on_load(onLoad)
|
||||
|
||||
@@ -74,35 +96,5 @@ script.on_event(defines.events.on_tick, onInitialTick)
|
||||
script.on_event(defines.events.on_chunk_generated, onChunkGenerated)
|
||||
|
||||
remote.add_interface("rampant", {
|
||||
test1 = function ()
|
||||
local player = game.players[1]
|
||||
local regionMap = regionMaps[player.surface.index]
|
||||
local playerChunkX = math.floor(player.position.x / 32)
|
||||
local playerChunkY = math.floor(player.position.y / 32)
|
||||
print("------")
|
||||
print(playerChunkX .. ", " .. playerChunkY)
|
||||
print("--")
|
||||
for x=playerChunkX-3, playerChunkX+3 do
|
||||
for y=playerChunkY-3, playerChunkY+3 do
|
||||
if (regionMap[x] ~= nil) then
|
||||
local chunk = regionMap[x][y]
|
||||
if (chunk ~= nil) then
|
||||
print(serpent.dump(chunk))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
test2 = test2,
|
||||
test3 = test3,
|
||||
test1 = tests.test1
|
||||
})
|
||||
|
||||
-- aux
|
||||
|
||||
-- function printDebug(o)
|
||||
-- if (type(o) == "table") then
|
||||
-- print(serpent.dump(o))
|
||||
-- else
|
||||
-- print(o)
|
||||
-- end
|
||||
-- end
|
19
libs/AI.lua
Executable file
19
libs/AI.lua
Executable file
@@ -0,0 +1,19 @@
|
||||
local ai = {}
|
||||
|
||||
local regionMaps
|
||||
local units
|
||||
|
||||
function ai.attackPlayerNearNest(regionMap, player)
|
||||
|
||||
end
|
||||
|
||||
function ai.initializeScouts(regionMap)
|
||||
|
||||
end
|
||||
|
||||
function ai.init(maps, troops)
|
||||
units = troops
|
||||
regionMaps = maps
|
||||
end
|
||||
|
||||
return ai
|
@@ -2,6 +2,7 @@ local regionUtils = {}
|
||||
|
||||
local constants = require("Constants")
|
||||
|
||||
local regionMaps
|
||||
local neighborArray = {1,2,3,4,5,6,7,8}
|
||||
local mExp = math.exp
|
||||
local mFloor = math.floor
|
||||
@@ -42,14 +43,14 @@ function regionUtils.processPheromone(regionMap, surface)
|
||||
local getNeighborChunks = regionUtils.getNeighborChunks
|
||||
local DIFFUSION_AMOUNT = constants.DIFFUSION_AMOUNT
|
||||
local MAX_PHEROMONE = constants.MAX_PHEROMONE
|
||||
local BASE_PHEROMONE = constants.BASE_PHEROMONE
|
||||
|
||||
local count = 0
|
||||
|
||||
for _,ys in pairs(regionMap) do
|
||||
for _,chunk in pairs(ys) do
|
||||
if (chunk.bG > 0) then
|
||||
chunk[2] = chunk[2] + (chunk.bG * 10)
|
||||
--surface.pollute({chunk.x *32, chunk.y *32}, 10)
|
||||
chunk[BASE_PHEROMONE] = chunk[BASE_PHEROMONE] + (chunk.bG * 10)
|
||||
end
|
||||
local chunks
|
||||
for x=1,3 do
|
||||
@@ -73,29 +74,17 @@ function regionUtils.processPheromone(regionMap, surface)
|
||||
chunk[x] = 0
|
||||
end
|
||||
end
|
||||
-- local printMe = false
|
||||
-- for x=1,3 do
|
||||
-- if (chunk[x] > 0) thena
|
||||
-- printMe = true
|
||||
-- end
|
||||
-- end
|
||||
-- if (printMe) then
|
||||
-- print(serpent.dump(chunk))
|
||||
-- end
|
||||
count = count + 1
|
||||
if (count % 1250 == 0) then
|
||||
coroutine.yield()
|
||||
end
|
||||
end
|
||||
end
|
||||
-- print("--")
|
||||
end
|
||||
|
||||
function regionUtils.placePheromone(regionMap, x, y, pType, amount)
|
||||
local MAX_PHEROMONE = constants.MAX_PHEROMONE
|
||||
|
||||
local chunk = regionUtils.getChunkByPosition(regionMap, x, y)
|
||||
chunk[pType] = mMin(MAX_PHEROMONE, chunk[pType] + amount)
|
||||
chunk[pType] = mMin(constants.MAX_PHEROMONE, chunk[pType] + amount)
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -127,4 +116,14 @@ function regionUtils.getNeighborChunks(regionMap, chunkX, chunkY, neighbors)
|
||||
return neighbors
|
||||
end
|
||||
|
||||
function regionUtils.init(maps)
|
||||
regionMaps = maps
|
||||
end
|
||||
|
||||
function regionUtils.pheromoneProcess()
|
||||
local player = game.players[1]
|
||||
regionUtils.placePheromone(regionMaps[player.surface.index], player.position.x, player.position.y, constants.PLAYER_PHEROMONE, 100)
|
||||
regionUtils.processPheromone(regionMaps[player.surface.index], player.surface, 2)
|
||||
end
|
||||
|
||||
return regionUtils
|
15
libs/Utils.lua
Executable file
15
libs/Utils.lua
Executable file
@@ -0,0 +1,15 @@
|
||||
local utils = {}
|
||||
|
||||
function euclideanDistanceNamed(p1, p2)
|
||||
local xs = p1.x - p2.x
|
||||
local ys = p1.y - p2.y
|
||||
return ((xs * xs) + (ys * ys)) ^ 0.5
|
||||
end
|
||||
|
||||
function euclideanDistanceArray(p1, p2)
|
||||
local xs = p1[1] - p2[1]
|
||||
local ys = p1[2] - p2[2]
|
||||
return ((xs * xs) + (ys * ys)) ^ 0.5
|
||||
end
|
||||
|
||||
return utils
|
@@ -2,7 +2,8 @@ local chunkUtils = {}
|
||||
|
||||
local regionUtils = require("RegionUtils")
|
||||
local constants = require("Constants")
|
||||
|
||||
local regionMaps
|
||||
local chunkProcessingQueue
|
||||
|
||||
function chunkUtils.checkForDeadendTiles(constantCoordinate, iteratingCoordinate, direction, chunkSize, surface)
|
||||
local NORTH_SOUTH = constants.NORTH_SOUTH
|
||||
@@ -124,15 +125,6 @@ function chunkUtils.createChunk(topX, topY, directions, scores)
|
||||
}
|
||||
end
|
||||
|
||||
-- aux
|
||||
|
||||
-- function showGrid(regionMap, surface)
|
||||
-- for i, chunk in pairs(regionMap) do
|
||||
-- local x = chunk.x --math.floor(game.players[1].position.x / 32) * 32
|
||||
-- local y = chunk.y --math.floor(game.players[1].position.y / 32) * 32
|
||||
-- end
|
||||
-- end
|
||||
|
||||
function chunkUtils.colorChunk(x, y, tileType, surface)
|
||||
local CHUNK_SIZE = constants.CHUNK_SIZE
|
||||
|
||||
@@ -145,4 +137,13 @@ function chunkUtils.colorChunk(x, y, tileType, surface)
|
||||
surface.set_tiles(tiles, false)
|
||||
end
|
||||
|
||||
function chunkUtils.init(maps, chunkQueue)
|
||||
regionMaps = maps
|
||||
chunkProcessingQueue = chunkQueue
|
||||
end
|
||||
|
||||
function chunkUtils.chunkProcess()
|
||||
chunkUtils.processChunks(regionMaps, chunkProcessingQueue)
|
||||
end
|
||||
|
||||
return chunkUtils
|
33
tests.lua
33
tests.lua
@@ -1,14 +1,30 @@
|
||||
local tests = {}
|
||||
|
||||
-- local regionMaps
|
||||
local regionMaps
|
||||
|
||||
-- function initTester()
|
||||
-- regionMaps = global.regionMaps
|
||||
-- end
|
||||
function tests.initTester()
|
||||
regionMaps = global.regionMaps
|
||||
end
|
||||
|
||||
-- function test1()
|
||||
-- local playerPosition = game.players[1].position
|
||||
-- placePheromone(regionMaps[1], playerPosition.x, playerPosition.y, 2, 100)
|
||||
-- end
|
||||
function tests.test1()
|
||||
local player = game.players[1]
|
||||
local regionMap = regionMaps[player.surface.index]
|
||||
local playerChunkX = math.floor(player.position.x / 32)
|
||||
local playerChunkY = math.floor(player.position.y / 32)
|
||||
print("------")
|
||||
print(playerChunkX .. ", " .. playerChunkY)
|
||||
print("--")
|
||||
for x=playerChunkX-3, playerChunkX+3 do
|
||||
for y=playerChunkY-3, playerChunkY+3 do
|
||||
if (regionMap[x] ~= nil) then
|
||||
local chunk = regionMap[x][y]
|
||||
if (chunk ~= nil) then
|
||||
print(serpent.dump(chunk))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- function test2()
|
||||
-- local playerPosition = game.players[1].position
|
||||
@@ -20,3 +36,4 @@
|
||||
-- decayPheromone(regionMaps[1], playerPosition.x, playerPosition.y, 3)
|
||||
-- end
|
||||
|
||||
return tests
|
Reference in New Issue
Block a user