diff --git a/control.lua b/control.lua index 45287d7..521cd72 100755 --- a/control.lua +++ b/control.lua @@ -1,19 +1,36 @@ -require "libs/chunkUtils" +require "libs/RegionMap" +require "libs/ChunkUtils" +require "tests" local pheromoneMaps local regionMaps +local DEATH_PHEROMONE = 1 +local BASE_PHEROMONE = 2 +local PLAYER_PHEROMONE = 3 -- hook functions function onInit() global.regionMaps = {} regionMaps = global.regionMaps - local mapSettings = game.surfaces[1].map_gen_settings - local surfaceSettings = { - - } - surfaceSettings.autoplace_controls = {} - game.create_surface("death", surfaceSettings) + game.surfaces[1].peaceful_mode = true + -- game.map_settings.enemy_expansion.enabled = false + -- game.forces.enemy.ai_controllable = false + -- local mapSettings = game.surfaces[1].map_gen_settings + -- local surfaceSettings = { + -- terrain_segmentation = mapSettings.terrain_segmentation, + -- water = mapSettings.water, + -- autoplace_controls = {}, + -- seed = mapSettings.seed, + -- shift = mapSettings.shift, + -- width = mapSettings.width, + -- height = mapSettings.height, + -- starting_area = mapSettings.starting_area, + -- peaceful_mode = true + -- } + -- game.create_surface("death", game.surfaces[1].map_gen_settings) + -- game.create_surface("death1", game.surfaces[1].map_gen_settings) + -- game.create_surface("death2", game.surfaces[1].map_gen_settings) end function onLoad() @@ -22,43 +39,51 @@ end function onChunkGenerated(event) local surface = event.surface - - if (regionMaps[surface.index] == nil) then - regionMaps[surface.index] = {} + if (surface.index == 1) then + if (regionMaps[surface.index] == nil) then + regionMaps[surface.index] = {} + end + + addChunkToRegionMap(regionMaps[surface.index], + event.area.left_top.x, + event.area.left_top.y, + surface) end - - addChunkToRegionMap(regionMaps[surface.index], - event.area.left_top.x, - event.area.left_top.y, - surface) end --- function onTick(event) - -- if (event.tick % 360 == 0) then - -- print("here") - -- local playerPosition = game.players[1].position - -- local entites = game.surfaces[1].find_entities_filtered({type="unit-spawner", - -- area={{x = playerPosition.x - 208, y = playerPosition.y - 208}, - -- {x = playerPosition.x + 208, y = playerPosition.y + 208}}, - -- force="enemy"}) - -- print("done") - -- end +function onTick(event) + if (event.tick % 120 == 0) then + local player = game.players[1] + placePheromone(regionMaps[player.surface.index], player.position.x, player.position.y, PLAYER_PHEROMONE, 100) + processPheromone(regionMaps[player.surface.index], player.surface, 2) + end +end + +-- function pheromones(event) + -- local player = event.player + -- end -- hooks -function onTick() +function onInitialTick(event) + -- initTester() --tester() - script.on_event(defines.events.on_tick, nil) + script.on_event(defines.events.on_tick, onTick) end script.on_init(onInit) script.on_load(onLoad) -script.on_event(defines.events.on_tick, onTick) ---remote.add_interface("rampant", {showGrid=showStandardGrid}) +script.on_event(defines.events.on_tick, onInitialTick) script.on_event(defines.events.on_chunk_generated, onChunkGenerated) +remote.add_interface("rampant", { + test1 = test1, + test2 = test2, + test3 = test3, + }) + -- aux -- function printDebug(o) diff --git a/libs/RegionMap.lua b/libs/RegionMap.lua new file mode 100755 index 0000000..3209ff6 --- /dev/null +++ b/libs/RegionMap.lua @@ -0,0 +1,111 @@ +local DIFFUSION_AMOUNT = 0.04 +local MAX_PLAYER_PHEROMONE = 7000 +local neighborArray = {1,2,3,4,5,6,7,8} +local mExp = math.exp +local mFloor = math.floor +local mMin = math.min +local mMax = math.max + +function addChunkToRegionMap(regionMap, x, y, surface) + + local chunkX = mFloor(x * 0.03125) + local chunkY = mFloor(y * 0.03125) + + local chunk = createChunk(x, y, surface) + chunk.x = chunkX + chunk.y = chunkY + if (regionMap == nil) then + regionMap = {} + end + if regionMap[chunkX] == nil then + regionMap[chunkX] = {} + end + regionMap[chunkX][chunkY] = chunk +end + +function getChunkByPosition(regionMap, x, y) + local chunkX = mFloor(x * 0.03125) + local chunkY = mFloor(y * 0.03125) + + return regionMap[chunkX][chunkY] +end + +function getChunkByIndex(regionMap, chunkX, chunkY) + return regionMap[chunkX][chunkY] +end + +function processPheromone(regionMap, surface) + local mathMin = mMin + local mathExp = mExp + local neighbors = neighborArray + for _,ys in pairs(regionMap) do + for _,chunk in pairs(ys) do + local chunks + for x=1,3 do + if (chunk[x] > 50) then + if (chunks == nil) then + chunks = getNeighborChunks(regionMap, chunk.x, chunk.y, neighbors) + end + local totalDiffused = 0 + for i=1,8 do + local neighborChunk = chunks[i] + if (neighborChunk ~= nil) then + local diffusedAmount = (chunk[x] * DIFFUSION_AMOUNT) + totalDiffused = totalDiffused + diffusedAmount + neighborChunk[x] = mathMin(MAX_PLAYER_PHEROMONE, neighborChunk[x] + diffusedAmount) + end + end + chunk[x] = chunk[x] - totalDiffused + end + chunk[x] = chunk[x] * 0.95 + if (chunk[x] < 2) then + chunk[x] = 0 + end + end + -- local printMe = false + -- for x=1,3 do + -- if (chunk[x] > 0) then + -- printMe = true + -- end + -- end + -- if (printMe) then + -- print(serpent.dump(chunk)) + -- end + end + end + -- print("--") +end + +function placePheromone(regionMap, x, y, pType, amount) + local chunk = getChunkByPosition(regionMap, x, y) + chunk[pType] = mMin(MAX_PLAYER_PHEROMONE, chunk[pType] + amount) +end + +--[[ + 1 2 3 + \|/ + 4- -5 + /|\ + 6 7 8 +]]-- +function getNeighborChunks(regionMap, chunkX, chunkY, neighbors) + local xChunks = regionMap[chunkX-1] + if (xChunks ~= nil) then + neighbors[1] = xChunks[chunkY-1] + neighbors[4] = xChunks[chunkY] + neighbors[6] = xChunks[chunkY+1] + end + + xChunks = regionMap[chunkX+1] + if (xChunks ~= nil) then + neighbors[3] = xChunks[chunkY-1] + neighbors[5] = xChunks[chunkY] + neighbors[8] = xChunks[chunkY+1] + end + + xChunks = regionMap[chunkX] + neighbors[2] = xChunks[chunkY-1] + neighbors[7] = xChunks[chunkY+1] + + return neighbors +end diff --git a/libs/chunkUtils.lua b/libs/chunkUtils.lua index c7b5e2e..a2e5ce5 100755 --- a/libs/chunkUtils.lua +++ b/libs/chunkUtils.lua @@ -52,15 +52,12 @@ function checkChunkPassability(x, y, surface) end function checkChunkValues(x, y, surface) - local pV = 0 - local pDV = 0 - local eV = 0 - local eDV = 0 + local spawnerCount = surface.count_entities_filtered({area={{x, y}, + {x+32, y+32}}, + type="unit-spawner", + force="enemy"}) return { - pV = pV, - pDV = pDV, - eV = eV, - eDV = eDV, + base = spawnerCount } end @@ -68,37 +65,23 @@ function createChunk(topX, topY, surface) local directions = checkChunkPassability(topX, topY, surface) local scores = checkChunkValues(topX, topY, surface) return { - pNS = directions.northSouth, -- passable north_south - pEW = directions.eastWest, -- passable east_west - pV = scores.pV, -- value of player structures - pDV = scores.pDV, -- value of player defenses - eV = scores.eV, -- value of enemy structures - eDV = scores.eDV, -- value of enemy defenses - rV = scores.rV -- value of resources + 0, + 0, + 0, + nS = directions.northSouth, -- passable north_south + eW = directions.eastWest, -- passable east_west + bG = scores.base, -- value of pheromone base generator } end -function addChunkToRegionMap(regionMap, x, y, surface) - - local chunkX = x * 0.03125 - local chunkY = y * 0.03125 - - local key = hashChunkCoordinates(chunkX, chunkY) - regionMap[key] = createChunk(x, y, surface) -end - -function hashChunkCoordinates(x, y) - return tostring(x) .. "," .. tostring(y) -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 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 colorChunk(x, y, tileType, surface) local tiles = {} diff --git a/tests.lua b/tests.lua new file mode 100755 index 0000000..4b7257b --- /dev/null +++ b/tests.lua @@ -0,0 +1,22 @@ + +-- local regionMaps + +-- function initTester() + -- regionMaps = global.regionMaps +-- end + +-- function test1() + -- local playerPosition = game.players[1].position + -- placePheromone(regionMaps[1], playerPosition.x, playerPosition.y, 2, 100) +-- end + +-- function test2() + -- local playerPosition = game.players[1].position + -- spreadPheromone(regionMaps[1], playerPosition.x, playerPosition.y) +-- end + +-- function test3() + -- local playerPosition = game.players[1].position + -- decayPheromone(regionMaps[1], playerPosition.x, playerPosition.y, 3) +-- end +