1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-16 10:19:27 +02:00
RedMew/map_gen/shared/nightfall.lua

175 lines
6.1 KiB
Lua
Raw Normal View History

2018-10-22 16:51:40 +02:00
--[[
Softmod rewrite of https://mods.factorio.com/mod/Nightfall written by Yehn and used under the MIT license
2018-10-23 04:47:30 +02:00
Copyright 2018 Yehn
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
----
With Nightfall, biters in polluted areas become more aggressive at night.
2018-10-22 16:51:40 +02:00
TODO: Look into triggering existing unit groups to attack in unison with the groups we generate.
]] --
2018-11-21 15:35:47 +02:00
-- Dependencies
2018-10-22 16:51:40 +02:00
local Event = require 'utils.event'
local Global = require 'utils.global'
local RS = require 'map_gen.shared.redmew_surface'
local table = require 'utils.table'
local random = math.random
local insert = table.insert
-- config settings
-- basic interval for checks
2018-10-24 03:52:52 +02:00
local timeinterval = 2689 --2700 is ~45 seconds at 60 UPS
-- how many chunks to process in a tick
2018-10-22 16:51:40 +02:00
local processchunk = 5
-- end of config
2018-10-22 16:51:40 +02:00
-- states
2018-10-22 16:51:40 +02:00
local IDLE = 1
local BASE_SEARCH = 2
local ATTACKING = 3
-- create globals
local chunklist = {}
local data = {bases = {}, c_index = 1, state = 1, lastattack = 0}
Global.register(
{
chunklist = chunklist,
data = data
},
function(tbl)
chunklist = tbl.chunklist
data = tbl.data
end
)
2018-10-22 16:51:40 +02:00
--- Called each tick when in ATTACKING state, scans through _processchunk_ chunks
-- looking for biters and adding them to a group
2018-10-22 16:51:40 +02:00
local function biter_attack()
local maxindex = #data.bases
local surface = RS.get_surface()
for i = data.c_index, data.c_index + processchunk, 1 do
2018-10-23 20:46:23 +02:00
if i > maxindex then
-- we reached the end of the table
data.state = IDLE
2018-10-23 20:46:23 +02:00
break
end
2018-10-24 03:53:54 +02:00
if random() < surface.darkness then
local base = data.bases
2018-11-21 15:35:47 +02:00
local group = surface.create_unit_group {position = base}
for _, biter in pairs(surface.find_enemy_units(base, 16)) do
2018-10-23 20:46:23 +02:00
group.add_member(biter)
end
2018-11-21 15:35:47 +02:00
if #group.members == 0 then
2018-10-23 20:46:23 +02:00
group.destroy()
else
--autonomous groups will attack polluted areas independently
group.set_autonomous()
if _DEBUG then
game.print('[NIGHTFALL] sending biters to attack')
2018-10-23 20:46:23 +02:00
end
end
end
end
data.c_index = data.c_index + processchunk
2018-10-23 20:46:23 +02:00
--Reset if we're moving to the next state.
if data.state == IDLE then
data.c_index = 1
data.lastattack = game.tick
if _DEBUG then
game.print('[NIGHTFALL] attack complete')
end
2018-10-22 16:51:40 +02:00
end
end
--- Called each tick when in BASE_SEARCH state, scans through _processchunk_ chunks
-- looking for unit spawners and adding them to the bases table, when done iterating
-- through chunklist it sets the state to ATTACKING
2018-10-22 16:51:40 +02:00
local function find_bases()
local get_pollution = RS.get_surface().get_pollution
local count_entities_filtered = RS.get_surface().count_entities_filtered
if data.c_index == 1 then
data.bases = {}
2018-10-23 20:46:23 +02:00
end
local maxindex = #chunklist
for i = data.c_index, data.c_index + processchunk, 1 do
2018-10-23 20:46:23 +02:00
if i > maxindex then
-- we're done with the search
data.state = ATTACKING
2018-10-23 20:46:23 +02:00
break
end
if get_pollution(chunklist[i]) > 0.1 then
local chunkcoord = chunklist[i]
2018-11-21 15:35:47 +02:00
if
(count_entities_filtered {
area = {{chunkcoord.x - 16, chunkcoord.y - 16}, {chunkcoord.x + 16, chunkcoord.y + 16}},
type = 'unit-spawner',
limit = 1
2018-11-21 15:35:47 +02:00
}) > 0
then
insert(data.bases, chunkcoord)
2018-10-23 20:46:23 +02:00
end
end
end
data.c_index = data.c_index + processchunk
--Reset our index and shuffle the table if we're moving to the next state.
if data.state == ATTACKING then
data.c_index = 1
if #data.bases > 0 then
table.shuffle_table(data.bases)
end
2018-10-23 20:46:23 +02:00
if _DEBUG then
game.print('[NIGHTFALL] bases added: ' .. tostring(#data.bases))
game.print('[NIGHTFALL] entering ATTACKING state')
2018-10-23 20:46:23 +02:00
end
end
2018-10-22 16:51:40 +02:00
end
--- When a chunk is generated, add it to the chunklist
2018-10-22 16:51:40 +02:00
local function on_chunk_generated(event)
if event.surface == RS.get_surface() then
2018-10-23 20:46:23 +02:00
local chunk = {}
local coords = event.area.left_top
2018-11-21 15:35:47 +02:00
chunk.x = coords.x + 16
chunk.y = coords.y + 16
insert(chunklist, chunk)
2018-10-23 20:46:23 +02:00
end
2018-10-22 16:51:40 +02:00
end
--- Every tick, choose between searching for bases, preparing an attack, or doing nothing
-- See the definitions of the called function for further information
2018-10-26 23:45:34 +02:00
local function on_tick()
if data.state == BASE_SEARCH then
2018-10-23 20:46:23 +02:00
find_bases()
elseif data.state == ATTACKING then
2018-10-23 20:46:23 +02:00
biter_attack()
end
2018-10-22 16:51:40 +02:00
end
--- Change us from idle to searching for bases if the conditions are met.
2018-10-26 23:45:34 +02:00
local function on_interval()
if
RS.get_surface().darkness > 0.5 and random() > 0.5 and data.state == IDLE and
game.tick >= data.lastattack + timeinterval
then
data.state = BASE_SEARCH
2018-10-26 23:45:34 +02:00
if _DEBUG then
RS.get_surface().print('[NIGHTFALL] entering BASE_SEARCH state') --for debug
2018-10-26 23:45:34 +02:00
end
end
end
2018-10-22 16:51:40 +02:00
Event.add(defines.events.on_chunk_generated, on_chunk_generated)
Event.add(defines.events.on_tick, on_tick)
2018-10-26 23:45:34 +02:00
Event.on_nth_tick(timeinterval, on_interval)