1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-03-11 14:49:24 +02:00

Merge pull request #20 from Gerkiz/modules

Modules
This commit is contained in:
MewMew 2019-03-11 16:35:22 +01:00 committed by GitHub
commit 533db3e83e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 102 additions and 0 deletions

View File

@ -0,0 +1,35 @@
local event = require 'utils.event'
local function is_depleted(drill, entity)
local position = drill.position
local area
if drill.name == 'electric-mining-drill' then
area = {{position.x - 2.5, position.y - 2.5}, {position.x + 2.5, position.y + 2.5}}
else
area = {{position.x - 1, position.y - 1}, {position.x + 1, position.y + 1}}
end
for _, resource in pairs(drill.surface.find_entities_filtered {type = 'resource', area = area}) do
if resource ~= entity and resource.name ~= 'crude-oil' then
return false
end
end
return true
end
local function on_resource_depleted(event)
local entity = event.entity
if entity.name == 'uranium-ore' then
return nil
end
local position = entity.position
local area = {{position.x - 1, position.y - 1}, {position.x + 1, position.y + 1}}
local drills = event.entity.surface.find_entities_filtered {area = area, type = 'mining-drill'}
for _, drill in ipairs(drills) do
if drill.name ~= 'pumpjack' and is_depleted(drill, entity) then
drill.order_deconstruction(drill.force)
end
end
end
event.add(defines.events.on_resource_depleted, on_resource_depleted)

View File

@ -0,0 +1,41 @@
local event = require 'utils.event'
DIVERSITY_QUOTA = 0.20
EXEMPT_AREA = 200 --This is the radius of the starting area that can't be affected.
STONE_BYPRODUCT = false --Delete patches of stone. Stone only appears as a byproduct.
STONE_BYPRODUCT_RATIO = 0.25 --If math.random() is between DIVERSITY_QUOTA and this, it's stone.
--Build a table of potential ores to pick from. Uranium is exempt from popping up randomly.
local function init()
global.diverse_ores = {}
for k,v in pairs(game.entity_prototypes) do
if v.type == "resource" and v.resource_category == "basic-solid" and v.mineable_properties.required_fluid == nil then
table.insert(global.diverse_ores, v.name)
end
end
end
local function scramble(event)
local ores = event.surface.find_entities_filtered{type="resource", area=event.area}
for k,v in pairs(ores) do
if math.abs(v.position.x) > EXEMPT_AREA or math.abs(v.position.y) > EXEMPT_AREA then
if v.prototype.resource_category == "basic-solid" then
local random = math.random()
if v.name == "stone" and STONE_BYPRODUCT then
v.destroy()
elseif random < DIVERSITY_QUOTA then --Replace!
local refugee = global.diverse_ores[math.random(#global.diverse_ores)]
event.surface.create_entity{name=refugee, position=v.position, amount=v.amount}
v.destroy()
elseif STONE_BYPRODUCT and random < STONE_BYPRODUCT_RATIO then --Replace with stone!
event.surface.create_entity{name="stone", position=v.position, amount=v.amount}
v.destroy()
end
end
end
end
end
event.on_init(init)
event.add(defines.events.on_chunk_generated, scramble)

View File

@ -0,0 +1,26 @@
local event = require 'utils.event'
local game = require 'utils.game'
event.add(defines.events.on_entity_damaged, function(event)
if event.entity.name ~= 'player' then return end
local player = game.get_player(event.entity.player)
if player.character then
if player.character.health == nil then return end
local index = player.index
local health = math.ceil(player.character.health)
if global.player_health == nil then global.player_health = {} end
if global.player_health[index] == nil then global.player_health[index] = health end
if global.player_health[index] ~= health then
if health < global.player_health[index] then
local text = health..' (-'..math.floor(event.final_damage_amount)..')'
if health > 200 then
player.surface.create_entity{name="flying-text", color={b = 0.2, r= 0.1, g = 1, a = 0.8}, text=text, position= {player.position.x, player.position.y-2}}
elseif health > 100 then
player.surface.create_entity{name="flying-text", color={r = 1, g = 1, b = 0}, text=text, position= {player.position.x, player.position.y-2}}
else
player.surface.create_entity{name="flying-text", color={b = 0.1, r= 1, g = 0, a = 0.8}, text=text, position= {player.position.x, player.position.y-2}}
end
end
global.player_health[index] = health
end
end
end)