1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-10 00:43:27 +02:00
ComfyFactorio/modules/spaghett_challenge.lua

181 lines
6.3 KiB
Lua
Raw Normal View History

2021-03-24 17:46:00 +02:00
-- too many same entities close together will explode
2019-07-19 17:26:10 +02:00
2021-03-24 21:14:55 +02:00
local Event = require 'utils.event'
2019-07-19 17:26:10 +02:00
local math_random = math.random
require 'utils.table'
local search_radius = 6
2021-03-24 17:46:00 +02:00
local explosions = {'explosion', 'explosion', 'explosion', 'explosion', 'explosion', 'explosion', 'medium-explosion', 'uranium-cannon-explosion', 'uranium-cannon-explosion'}
2019-07-19 17:26:10 +02:00
local entity_limits = {
2021-03-24 17:46:00 +02:00
['transport-belt'] = search_radius * 2 + 1,
['pipe'] = search_radius * 2 + 2,
['pipe-to-ground'] = 2,
['heat-pipe'] = search_radius * 2,
['inserter'] = 3,
['underground-belt'] = 3,
['electric-pole'] = 3,
['generator'] = 1,
['assembling-machine'] = 4,
['accumulator'] = 4,
['container'] = search_radius * 2,
['furnace'] = 4,
['mining-drill'] = 1
2019-07-23 17:14:17 +02:00
}
local ignore_list = {
2021-03-24 17:46:00 +02:00
['curved-rail'] = true,
['straight-rail'] = true,
['rail-signal'] = true,
['rail-chain-signal'] = true,
['car'] = true,
['cargo-wagon'] = true,
['locomotive'] = true,
['land-mine'] = true,
['entity-ghost'] = true,
['character'] = true,
['gate'] = true,
['wall'] = true
2019-07-19 17:26:10 +02:00
}
local function error_message()
2021-03-24 17:46:00 +02:00
local errors = {
'spaghett',
'not found',
'cat',
'error',
'not enough',
'pylons',
'out of',
'limit reached',
'void',
'balance',
'operation',
'alert',
'not responding',
'angry',
'sad',
'not working',
'can not compute',
'unstable location',
'invalid',
'gearwheel',
'malfunctioning',
'unknown',
'can not',
'wrong',
'dog',
'ocelot',
'failure',
'division by',
'zero',
'bathwater',
'faulty',
'conveyor',
'too many',
'ravioli',
'pasta',
'overflow',
'major'
}
table.shuffle_table(errors)
local message = table.concat({errors[1], ' ', errors[2], ' ', errors[3], '!'})
return message
2019-07-19 17:26:10 +02:00
end
local function count_same_entities(entity)
2021-03-24 17:46:00 +02:00
local same_entity_count = 0
for _, e in pairs(
entity.surface.find_entities_filtered(
{
type = entity.type,
area = {{entity.position.x - search_radius, entity.position.y - search_radius}, {entity.position.x + search_radius, entity.position.y + search_radius}}
}
)
) do
if entity.type == e.type and entity.direction == e.direction then
same_entity_count = same_entity_count + 1
end
end
return same_entity_count
2019-07-19 17:26:10 +02:00
end
local function spaghett(surface, entity, inventory, player)
2021-03-24 17:46:00 +02:00
if ignore_list[entity.type] then
return
end
local limit = entity_limits[entity.type]
if not limit then
limit = 2
end
if count_same_entities(entity) > limit then
inventory.insert({name = entity.name, count = 1})
surface.create_entity({name = explosions[math_random(1, #explosions)], position = entity.position})
if player then
if not global.last_spaghett_error then
global.last_spaghett_error = {}
end
if not global.last_spaghett_error[player.index] then
global.last_spaghett_error[player.index] = 0
end
if game.tick - global.last_spaghett_error[player.index] > 30 then
surface.create_entity(
{
name = 'flying-text',
position = entity.position,
text = error_message(),
color = {r = math.random(200, 255), g = math.random(0, 125), b = math.random(0, 125)}
}
)
global.last_spaghett_error[player.index] = game.tick
end
end
entity.die('player')
end
2019-07-19 17:26:10 +02:00
end
local function on_built_entity(event)
2021-03-24 17:46:00 +02:00
spaghett(event.created_entity.surface, event.created_entity, game.players[event.player_index].get_main_inventory(), game.players[event.player_index])
2019-07-19 17:26:10 +02:00
end
local function on_player_rotated_entity(event)
2021-03-24 17:46:00 +02:00
spaghett(event.entity.surface, event.entity, game.players[event.player_index].get_main_inventory(), game.players[event.player_index])
2019-07-19 17:26:10 +02:00
end
local function on_robot_built_entity(event)
2021-03-24 17:46:00 +02:00
spaghett(event.robot.surface, event.created_entity, event.robot.get_inventory(defines.inventory.robot_cargo))
2019-07-19 17:26:10 +02:00
end
2021-03-24 21:14:55 +02:00
local function on_player_created(event)
2021-03-24 17:46:00 +02:00
local force = game.players[event.player_index].force
force.technologies['logistic-system'].enabled = false
force.technologies['construction-robotics'].enabled = false
force.technologies['logistic-robotics'].enabled = false
force.technologies['robotics'].enabled = false
force.technologies['personal-roboport-equipment'].enabled = false
force.technologies['personal-roboport-mk2-equipment'].enabled = false
force.technologies['character-logistic-trash-slots-1'].enabled = false
force.technologies['character-logistic-trash-slots-2'].enabled = false
force.technologies['auto-character-logistic-trash-slots'].enabled = false
force.technologies['worker-robots-storage-1'].enabled = false
force.technologies['worker-robots-storage-2'].enabled = false
force.technologies['worker-robots-storage-3'].enabled = false
force.technologies['character-logistic-slots-1'].enabled = false
force.technologies['character-logistic-slots-2'].enabled = false
force.technologies['character-logistic-slots-3'].enabled = false
force.technologies['character-logistic-slots-4'].enabled = false
force.technologies['character-logistic-slots-5'].enabled = false
force.technologies['character-logistic-slots-6'].enabled = false
force.technologies['worker-robots-speed-1'].enabled = false
force.technologies['worker-robots-speed-2'].enabled = false
force.technologies['worker-robots-speed-3'].enabled = false
force.technologies['worker-robots-speed-4'].enabled = false
force.technologies['worker-robots-speed-5'].enabled = false
force.technologies['worker-robots-speed-6'].enabled = false
2019-07-23 17:14:17 +02:00
end
2021-03-24 21:14:55 +02:00
Event.add(defines.events.on_player_created, on_player_created)
Event.add(defines.events.on_robot_built_entity, on_robot_built_entity)
Event.add(defines.events.on_built_entity, on_built_entity)
Event.add(defines.events.on_player_rotated_entity, on_player_rotated_entity)