1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-06 00:23:49 +02:00
ComfyFactorio/modules/rocks_yield_ore.lua

156 lines
4.7 KiB
Lua
Raw Normal View History

2019-02-10 10:56:23 +02:00
--destroying and mining rocks yields ore -- load as last module
2019-11-02 17:09:58 +02:00
local max_spill = 40
2019-02-09 10:25:08 +02:00
local rock_yield = {
["rock-big"] = 1,
["rock-huge"] = 2,
["sand-rock-big"] = 1
}
local rock_mining_chance_weights = {
{"iron-ore", 25},
2019-10-10 21:33:54 +02:00
{"copper-ore",17},
{"coal",13},
2019-11-01 17:24:44 +02:00
{"stone",10},
2019-10-10 21:33:54 +02:00
{"uranium-ore",2}
2019-02-09 10:25:08 +02:00
}
local texts = {
["iron-ore"] = {"Iron ore", {r = 200, g = 200, b = 180}},
["copper-ore"] = {"Copper ore", {r = 221, g = 133, b = 6}},
["uranium-ore"] = {"Uranium ore", {r= 50, g= 250, b= 50}},
["coal"] = {"Coal", {r = 0, g = 0, b = 0}},
["stone"] = {"Stone", {r = 200, g = 160, b = 30}},
}
2019-08-06 03:04:35 +02:00
local particles = {
["iron-ore"] = "iron-ore-particle",
["copper-ore"] = "copper-ore-particle",
["uranium-ore"] = "coal-particle",
["coal"] = "coal-particle",
["stone"] = "stone-particle"
}
2019-02-09 10:25:08 +02:00
local ore_raffle = {}
for _, t in pairs (rock_mining_chance_weights) do
for x = 1, t[2], 1 do
table.insert(ore_raffle, t[1])
end
end
2019-10-15 04:20:40 +02:00
local function create_particles(surface, name, position, amount, cause_position)
2019-10-20 12:52:19 +02:00
local direction_mod = (-100 + math.random(0,200)) * 0.0004
local direction_mod_2 = (-100 + math.random(0,200)) * 0.0004
2019-08-06 03:04:35 +02:00
if cause_position then
direction_mod = (cause_position.x - position.x) * 0.025
direction_mod_2 = (cause_position.y - position.y) * 0.025
end
for i = 1, amount, 1 do
2019-10-20 12:52:19 +02:00
local m = math.random(4, 10)
2019-08-06 03:04:35 +02:00
local m2 = m * 0.005
surface.create_entity({
name = name,
position = position,
frame_speed = 1,
vertical_speed = 0.130,
height = 0,
movement = {
2019-10-20 12:52:19 +02:00
(m2 - (math.random(0, m) * 0.01)) + direction_mod,
(m2 - (math.random(0, m) * 0.01)) + direction_mod_2
2019-08-06 03:04:35 +02:00
}
})
end
end
2019-02-09 10:25:08 +02:00
local function get_amount(entity)
2019-10-09 03:25:00 +02:00
local distance_to_center = math.floor(math.sqrt(entity.position.x ^ 2 + entity.position.y ^ 2))
2019-08-06 03:04:35 +02:00
2019-10-09 03:25:00 +02:00
local distance_modifier = 0.25
local base_amount = 35
local maximum_amount = 100
if global.rocks_yield_ore_distance_modifier then distance_modifier = global.rocks_yield_ore_distance_modifier end
if global.rocks_yield_ore_base_amount then base_amount = global.rocks_yield_ore_base_amount end
if global.rocks_yield_ore_maximum_amount then maximum_amount = global.rocks_yield_ore_maximum_amount end
2019-08-06 03:04:35 +02:00
2019-10-09 03:25:00 +02:00
local amount = base_amount + (distance_to_center * distance_modifier)
if amount > maximum_amount then amount = maximum_amount end
2019-08-06 03:04:35 +02:00
2019-10-20 12:52:19 +02:00
local m = (70 + math.random(0, 60)) * 0.01
2019-10-09 03:25:00 +02:00
amount = math.floor(amount * rock_yield[entity.name] * m)
if amount < 1 then amount = 1 end
2019-02-09 10:25:08 +02:00
return amount
end
local function on_player_mined_entity(event)
local entity = event.entity
2019-02-09 12:42:02 +02:00
if not entity.valid then return end
2019-10-10 21:33:54 +02:00
if not rock_yield[entity.name] then return end
event.buffer.clear()
2019-10-20 12:52:19 +02:00
local ore = ore_raffle[math.random(1, #ore_raffle)]
2019-10-10 21:33:54 +02:00
local player = game.players[event.player_index]
2019-11-02 17:09:58 +02:00
--[[
2019-10-10 21:33:54 +02:00
local inventory = player.get_inventory(defines.inventory.character_main)
if not inventory.can_insert({name = ore, count = 1}) then
local e = entity.surface.create_entity({name = entity.name, position = entity.position})
e.health = entity.health
player.print("Inventory full.", {200, 200, 200})
return
2019-02-09 10:25:08 +02:00
end
2019-11-02 17:09:58 +02:00
]]
2019-10-10 21:33:54 +02:00
local count = get_amount(entity)
local position = {x = entity.position.x, y = entity.position.y}
player.surface.create_entity({name = "flying-text", position = position, text = "+" .. count .. " [img=item/" .. ore .. "]", color = {r = 200, g = 160, b = 30}})
2019-10-15 04:20:40 +02:00
create_particles(player.surface, particles[ore], position, 64, {x = player.position.x, y = player.position.y})
2019-10-10 21:33:54 +02:00
entity.destroy()
if count > max_spill then
player.surface.spill_item_stack(position,{name = ore, count = max_spill}, true)
count = count - max_spill
local inserted_count = player.insert({name = ore, count = count})
count = count - inserted_count
if count > 0 then
player.surface.spill_item_stack(position,{name = ore, count = count}, true)
end
else
player.surface.spill_item_stack(position,{name = ore, count = count}, true)
end
2019-02-09 10:25:08 +02:00
end
2019-02-09 12:42:02 +02:00
local function on_entity_died(event)
local entity = event.entity
2019-10-10 21:33:54 +02:00
if not entity.valid then return end
if not rock_yield[entity.name] then return end
2019-10-09 03:25:00 +02:00
2019-10-10 21:33:54 +02:00
local surface = entity.surface
2019-10-20 12:52:19 +02:00
local ore = ore_raffle[math.random(1, #ore_raffle)]
2019-10-10 21:33:54 +02:00
local pos = {entity.position.x, entity.position.y}
2019-10-15 05:56:30 +02:00
create_particles(surface, particles[ore], pos, 16, false)
2019-10-10 21:33:54 +02:00
if event.cause then
if event.cause.valid then
2019-10-12 02:19:37 +02:00
if event.cause.force.index == 2 or event.cause.force.index == 3 then
2019-10-10 21:33:54 +02:00
entity.destroy()
return
2019-10-09 03:25:00 +02:00
end
2019-10-10 21:33:54 +02:00
end
end
2019-10-10 02:50:00 +02:00
2019-10-25 11:37:56 +02:00
--local amount = get_amount(entity)
--amount = math.ceil(amount * 0.1)
--if amount > 12 then amount = 12 end
2019-10-10 21:33:54 +02:00
entity.destroy()
2019-10-25 11:37:56 +02:00
surface.spill_item_stack(pos,{name = ore, count = math.random(8,12)}, true)
2019-02-09 12:42:02 +02:00
end
2019-10-09 03:25:00 +02:00
local event = require 'utils.event'
2019-02-09 12:42:02 +02:00
event.add(defines.events.on_entity_died, on_entity_died)
2019-02-09 10:25:08 +02:00
event.add(defines.events.on_player_mined_entity, on_player_mined_entity)