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.
|
|
|
|
----
|
|
|
|
|
2018-12-09 17:50:16 +02:00
|
|
|
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-12-09 17:50:16 +02:00
|
|
|
]] --
|
2018-11-21 15:35:47 +02:00
|
|
|
|
2018-12-09 17:50:16 +02:00
|
|
|
-- Dependencies
|
2018-10-22 16:51:40 +02:00
|
|
|
local Event = require 'utils.event'
|
2019-01-15 15:27:48 +02:00
|
|
|
local Global = require 'utils.global'
|
2019-01-16 20:44:55 +02:00
|
|
|
local RS = require 'map_gen.shared.redmew_surface'
|
2019-01-15 15:27:48 +02:00
|
|
|
local table = require 'utils.table'
|
|
|
|
|
2018-12-09 17:50:16 +02:00
|
|
|
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
|
2018-12-09 17:50:16 +02:00
|
|
|
-- how many chunks to process in a tick
|
2018-10-22 16:51:40 +02:00
|
|
|
local processchunk = 5
|
2018-12-09 17:50:16 +02:00
|
|
|
-- end of config
|
2018-10-22 16:51:40 +02:00
|
|
|
|
2018-12-09 17:50:16 +02:00
|
|
|
-- states
|
2018-10-22 16:51:40 +02:00
|
|
|
local IDLE = 1
|
|
|
|
local BASE_SEARCH = 2
|
|
|
|
local ATTACKING = 3
|
|
|
|
|
2018-12-09 17:50:16 +02:00
|
|
|
-- 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
|
|
|
|
2018-12-09 17:50:16 +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()
|
2018-12-09 17:50:16 +02:00
|
|
|
local maxindex = #data.bases
|
2019-01-16 20:44:55 +02:00
|
|
|
local surface = RS.get_surface()
|
2018-12-09 17:50:16 +02:00
|
|
|
for i = data.c_index, data.c_index + processchunk, 1 do
|
2018-10-23 20:46:23 +02:00
|
|
|
if i > maxindex then
|
2018-12-09 17:50:16 +02:00
|
|
|
-- 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
|
2018-12-09 17:50:16 +02:00
|
|
|
local base = data.bases
|
2018-11-21 15:35:47 +02:00
|
|
|
local group = surface.create_unit_group {position = base}
|
2018-12-09 17:50:16 +02:00
|
|
|
|
|
|
|
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-12-09 17:50:16 +02:00
|
|
|
|
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
|
2018-12-09 17:50:16 +02:00
|
|
|
game.print('[NIGHTFALL] sending biters to attack')
|
2018-10-23 20:46:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-12-09 17:50:16 +02:00
|
|
|
data.c_index = data.c_index + processchunk
|
2018-10-23 20:46:23 +02:00
|
|
|
--Reset if we're moving to the next state.
|
2018-12-09 17:50:16 +02:00
|
|
|
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
|
|
|
|
|
2018-12-09 17:50:16 +02:00
|
|
|
--- 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()
|
2019-01-16 20:44:55 +02:00
|
|
|
local get_pollution = RS.get_surface().get_pollution
|
|
|
|
local count_entities_filtered = RS.get_surface().count_entities_filtered
|
2018-12-09 17:50:16 +02:00
|
|
|
if data.c_index == 1 then
|
|
|
|
data.bases = {}
|
2018-10-23 20:46:23 +02:00
|
|
|
end
|
2018-12-09 17:50:16 +02:00
|
|
|
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
|
2018-12-09 17:50:16 +02:00
|
|
|
data.state = ATTACKING
|
2018-10-23 20:46:23 +02:00
|
|
|
break
|
|
|
|
end
|
2018-12-09 17:50:16 +02:00
|
|
|
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}},
|
2018-12-09 17:50:16 +02:00
|
|
|
type = 'unit-spawner',
|
|
|
|
limit = 1
|
2018-11-21 15:35:47 +02:00
|
|
|
}) > 0
|
|
|
|
then
|
2018-12-09 17:50:16 +02:00
|
|
|
insert(data.bases, chunkcoord)
|
2018-10-23 20:46:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-12-09 17:50:16 +02:00
|
|
|
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
|
2018-12-09 17:50:16 +02:00
|
|
|
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
|
|
|
|
|
2018-12-09 17:50:16 +02:00
|
|
|
--- When a chunk is generated, add it to the chunklist
|
2018-10-22 16:51:40 +02:00
|
|
|
local function on_chunk_generated(event)
|
2019-01-16 20:44:55 +02:00
|
|
|
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
|
2018-12-09 17:50:16 +02:00
|
|
|
insert(chunklist, chunk)
|
2018-10-23 20:46:23 +02:00
|
|
|
end
|
2018-10-22 16:51:40 +02:00
|
|
|
end
|
|
|
|
|
2018-12-09 17:50:16 +02:00
|
|
|
--- 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()
|
2018-12-09 17:50:16 +02:00
|
|
|
if data.state == BASE_SEARCH then
|
2018-10-23 20:46:23 +02:00
|
|
|
find_bases()
|
2018-12-09 17:50:16 +02:00
|
|
|
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
|
|
|
|
|
2018-12-09 17:50:16 +02:00
|
|
|
--- 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()
|
2018-12-09 17:50:16 +02:00
|
|
|
if
|
2019-01-16 20:44:55 +02:00
|
|
|
RS.get_surface().darkness > 0.5 and random() > 0.5 and data.state == IDLE and
|
2018-12-09 17:50:16 +02:00
|
|
|
game.tick >= data.lastattack + timeinterval
|
|
|
|
then
|
|
|
|
data.state = BASE_SEARCH
|
2018-10-26 23:45:34 +02:00
|
|
|
if _DEBUG then
|
2019-01-16 20:44:55 +02:00
|
|
|
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)
|