1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/map_gen/misc/nightfall.lua

160 lines
5.8 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.
----
2018-10-22 16:51:40 +02:00
Biters in polluted areas become more aggressive at night.
TODO: Look into triggering existing unit groups to attack in unison with the groups we generate.
]]--
2018-11-21 15:35:47 +02:00
2018-10-22 16:51:40 +02:00
local Event = require 'utils.event'
--basic interval for checks
2018-10-24 03:52:52 +02:00
local timeinterval = 2689 --2700 is ~45 seconds at 60 UPS
2018-10-22 16:51:40 +02:00
--how many chunks to process in a tick
local processchunk = 5
--states
local IDLE = 1
local BASE_SEARCH = 2
local ATTACKING = 3
2018-10-24 03:53:54 +02:00
local random = math.random
2018-10-24 03:55:44 +02:00
local insert = table.insert
2018-10-22 16:51:40 +02:00
local function biter_attack()
local maxindex = #global.bases
2018-10-23 20:46:23 +02:00
local surface = game.surfaces[1]
2018-11-21 15:35:47 +02:00
for i = global.c_index, global.c_index + processchunk, 1 do
2018-10-23 20:46:23 +02:00
if i > maxindex then
-- we're done here
global.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 = global.bases[i]
2018-11-21 15:35:47 +02:00
local group = surface.create_unit_group {position = base}
2018-10-23 20:46:23 +02:00
for _, biter in ipairs(surface.find_enemy_units(base, 16)) do
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
2018-11-21 15:35:47 +02:00
game.print('sending biters')
2018-10-23 20:46:23 +02:00
end
--group.set_command{ type=defines.command.attack_area, destination=game.players[1].position, radius=200, distraction=defines.distraction.by_anything }
end
end
end
global.c_index = global.c_index + processchunk
2018-10-23 20:46:23 +02:00
--Reset if we're moving to the next state.
if global.state == IDLE then
global.c_index = 1
global.lastattack = game.tick
2018-10-23 20:46:23 +02:00
end
2018-10-22 16:51:40 +02:00
end
2018-11-21 15:35:47 +02:00
local function shuffle_table(t)
assert(t, 'shuffle_table() expected a table, got nil')
2018-10-22 16:51:40 +02:00
local iterations = #t
local j
2018-11-21 15:35:47 +02:00
2018-10-22 16:51:40 +02:00
for i = iterations, 2, -1 do
2018-10-24 03:53:54 +02:00
j = random(i)
2018-10-22 16:51:40 +02:00
t[i], t[j] = t[j], t[i]
end
end
local function find_bases()
2018-10-26 23:45:34 +02:00
local get_pollution = game.surfaces[1].get_pollution
local count_entities_filtered = game.surfaces[1].count_entities_filtered
if global.c_index == 1 then
global.bases = {}
2018-10-23 20:46:23 +02:00
end
local maxindex = #global.chunklist
2018-11-21 15:35:47 +02:00
for i = global.c_index, global.c_index + processchunk, 1 do
2018-10-23 20:46:23 +02:00
if i > maxindex then
-- we're done with the search
global.state = ATTACKING
2018-10-23 20:46:23 +02:00
break
end
if get_pollution(global.chunklist[i]) > 0.1 then
local chunkcoord = global.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'
}) > 0
then
insert(global.bases, chunkcoord)
2018-10-23 20:46:23 +02:00
end
end
end
global.c_index = global.c_index + processchunk
2018-10-23 20:46:23 +02:00
--Reset if we're moving to the next state.
if global.state == ATTACKING then
global.c_index = 1
shuffle_table(global.bases)
2018-10-23 20:46:23 +02:00
if _DEBUG then
2018-11-21 15:35:47 +02:00
game.print('bases added: ' .. tostring(#global.bases))
2018-10-23 20:46:23 +02:00
end
end
2018-10-22 16:51:40 +02:00
end
local function on_chunk_generated(event)
2018-10-23 20:46:23 +02:00
-- Track when new chunks are generated and add them on.
-- NOTE: The game's debug menu can show potentially hundreds of ungenerated chunks
-- It's normal for this count to lag behind chunks in the debug menu.
if event.surface == game.surfaces[1] then
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(global.chunklist, chunk)
2018-10-23 20:46:23 +02:00
end
2018-10-22 16:51:40 +02:00
end
2018-10-26 23:45:34 +02:00
local function on_tick()
if global.state == BASE_SEARCH then
2018-10-23 20:46:23 +02:00
-- This is called every tick while in this state
-- But only a small amount of work is done per call.
-- State will change when it's finished.
find_bases()
elseif global.state == ATTACKING then
2018-10-23 20:46:23 +02:00
biter_attack()
end
2018-10-22 16:51:40 +02:00
end
2018-10-26 23:45:34 +02:00
local function on_interval()
2018-11-21 15:35:47 +02:00
if game.surfaces[1].darkness > 0.5 and global.state == IDLE and game.tick >= global.lastattack + timeinterval and random() > 0.5 then
2018-10-26 23:45:34 +02:00
-- Search for bases, then attack
global.state = BASE_SEARCH
2018-10-26 23:45:34 +02:00
if _DEBUG then
2018-11-21 15:35:47 +02:00
game.surfaces[1].print('entering attack mode') --for debug
2018-10-26 23:45:34 +02:00
end
end
end
2018-10-22 16:51:40 +02:00
local function on_init()
2018-11-21 15:35:47 +02:00
global.bases = {}
global.chunklist = {}
global.state = IDLE
--prevents attacks from happening too often
global.lastattack = 0
global.c_index = 1
2018-10-22 16:51:40 +02:00
end
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)
2018-10-23 04:47:30 +02:00
Event.on_init(on_init)