1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-03-03 14:53:01 +02:00

Added bot mining and particles

This commit is contained in:
Lynn 2018-11-23 22:04:55 +01:00
parent 22c21011ab
commit 83eee2b48a
7 changed files with 164 additions and 79 deletions

View File

@ -17,6 +17,7 @@ All notable changes to this project will be documented in this file.
### Bugfixes
- [Diggy] Stones killed by damage no longer spill. #395
- [Core] Fix /kill non-functional if walkabout is disabled. Fix walkabout giving from variable definition. #425
- [Diggy] Improved biter spawning algorithm #408
- [Core] Fix null reference in chat_triggers #431
- [Core] Fix nil ref in train_station_names #441

View File

@ -56,6 +56,11 @@ local Config = {
{name = 'power-armor-mk2', count = 1},
{name = 'submachine-gun', count = 1},
{name = 'uranium-rounds-magazine', count = 1000},
{name = 'roboport', count = 2},
{name = 'construction-robot', count = 50},
{name = 'electric-energy-interface', count = 1},
{name = 'medium-electric-pole', count = 50},
{name = 'logistic-chest-storage', count = 50},
},
},
},
@ -70,6 +75,12 @@ local Config = {
-- enables commands like /clear-void
enable_debug_commands = false,
-- initial damage per tick it damages a rock to mine, can be enhanced by robot_damage_per_mining_prod_level
robot_initial_mining_damage = 4,
-- damage added per level of mining productivity level research
robot_damage_per_mining_prod_level = 1,
},
-- adds the ability to collapse caves

View File

@ -0,0 +1,37 @@
local random = math.random
local CreateParticles = {}
---@param create_entity function a reference to a surface.create_entity
---@param particle_count number particle count to spawn
---@param position Position
function CreateParticles.destroy_rock(create_entity, particle_count, position)
for _ = particle_count, 1, -1 do
create_entity({
position = position,
name = 'stone-particle',
movement = {random(-5, 5) * 0.01, random(-5, 5) * 0.01},
frame_speed = 1,
vertical_speed = random(12, 14) * 0.01,
height = random(9, 11) * 0.1,
})
end
end
---@param create_entity function a reference to a surface.create_entity
---@param particle_count number particle count to spawn
---@param position Position
function CreateParticles.mine_rock(create_entity, particle_count, position)
for _ = particle_count, 1, -1 do
create_entity({
position = position,
name = 'stone-particle',
movement = {random(-5, 5) * 0.01, random(-5, 5) * 0.01},
frame_speed = 1,
vertical_speed = random(8, 10) * 0.01,
height = random(5, 8) * 0.1,
})
end
end
return CreateParticles

View File

@ -10,6 +10,7 @@ local Task = require 'utils.task'
local AlienEvolutionProgress = require 'map_gen.Diggy.AlienEvolutionProgress'
local Debug = require 'map_gen.Diggy.Debug'
local Template = require 'map_gen.Diggy.Template'
local CreateParticles = require 'map_gen.Diggy.CreateParticles'
local random = math.random
local floor = math.floor
local ceil = math.ceil
@ -54,16 +55,7 @@ local do_alien_mining = Token.register(function(params)
local particle_count = 16 - ((#rocks - 1) * 5)
for _, rock in pairs(rocks) do
raise_event(defines.events.on_entity_died, {entity = rock})
for _ = particle_count, 1, -1 do
create_entity({
position = rock.position,
name = 'stone-particle',
movement = {random(-5, 5) * 0.01, random(-5, 5) * 0.01},
frame_speed = 1,
vertical_speed = random(12, 14) * 0.01,
height = random(9, 11) * 0.1,
})
end
CreateParticles.destroy_rock(create_entity, particle_count, rock.position)
rock.destroy()
end
end

View File

@ -24,17 +24,13 @@ local function redraw_table(data)
data.frame.caption = 'Scoretable'
local score_keys = ScoreTable.all_keys()
for _, data in pairs(score_keys) do
local val = ScoreTable.get(data)
for name, value in pairs(ScoreTable.all()) do
local table = list.add({type = 'table', column_count = 2})
local key = table.add({type = 'label', name = 'Diggy.ArtefactHunting.Frame.List.Key', caption = data})
local key = table.add({type = 'label', name = 'Diggy.ArtefactHunting.Frame.List.Key', caption = name})
key.style.minimal_width = 175
local val = table.add({type = 'label', name = 'Diggy.ArtefactHunting.Frame.List.Val', caption = utils.comma_value(val)})
local val = table.add({type = 'label', name = 'Diggy.ArtefactHunting.Frame.List.Val', caption = utils.comma_value(value)})
val.style.minimal_width = 225
end
end

View File

@ -11,6 +11,7 @@ local Scanner = require 'map_gen.Diggy.Scanner'
local Template = require 'map_gen.Diggy.Template'
local ScoreTable = require 'map_gen.Diggy.ScoreTable'
local Debug = require 'map_gen.Diggy.Debug'
local CreateParticles = require 'map_gen.Diggy.CreateParticles'
local insert = table.insert
local random = math.random
local raise_event = script.raise_event
@ -24,19 +25,67 @@ local Simplex = require 'map_gen.shared.simplex_noise'
-- this
local DiggyHole = {}
--[[--
Triggers a diggy diggy hole for a given sand-rock-big or rock-huge.
-- keeps track of the amount of times per player when they mined with a full inventory in a row
local full_inventory_mining_cache = {}
Will return true even if the tile behind it is immune.
-- keeps track of the buffs for the bot mining mining_efficiency
local robot_mining = {
damage = 0,
active_modifier = 0,
research_modifier = 0,
}
@param entity LuaEntity
]]
local function diggy_hole(entity)
local name = entity.name
if name ~= 'sand-rock-big' and name ~= 'rock-huge' then
Global.register({
full_inventory_mining_cache = full_inventory_mining_cache,
bot_mining_damage = robot_mining,
}, function (tbl)
full_inventory_mining_cache = tbl.full_inventory_mining_cache
robot_mining = tbl.bot_mining_damage
end)
local function update_robot_mining_damage()
-- remove the current buff
local old_modifier = robot_mining.damage - robot_mining.active_modifier
-- update the active modifier
robot_mining.active_modifier = robot_mining.research_modifier
-- add the new active modifier to the non-buffed modifier
robot_mining.damage = old_modifier + robot_mining.active_modifier
ScoreTable.set('Robot mining damage', robot_mining.damage)
end
local function reset_player_full_inventory_cache(player)
if not full_inventory_mining_cache[player.index] then
return
end
full_inventory_mining_cache[player.index] = nil
end
local full_inventory_message = 'Miner, you have a full inventory!\n\nMake sure to empty it before you continue digging.'
local function trigger_inventory_warning(player)
local player_index = player.index
local count = full_inventory_mining_cache[player_index]
if not count then
full_inventory_mining_cache[player_index] = 1
player.print('## - ' .. full_inventory_message, {r = 1, g = 1, b = 0, a = 1})
player.play_sound{path='utility/new_objective', volume_modifier = 1 }
return
end
full_inventory_mining_cache[player_index] = count + 1
if count % 5 == 0 then
require 'features.gui.popup'.player(player, full_inventory_message)
end
end
---Triggers a diggy diggy hole for a given sand-rock-big or rock-huge.
---@param entity LuaEntity
local function diggy_hole(entity)
local tiles = {}
local rocks = {}
local surface = entity.surface
@ -116,8 +165,6 @@ local function diggy_hole(entity)
local huge_rock_inserted = false
for _, position in pairs(out_of_map_found) do
insert(tiles, {name = 'dirt-' .. random(1, 7), position = position})
-- if (random() > 0.50) then
-- insert(rocks, {name = 'rock-huge', position = position})
if c_mode then
@ -190,10 +237,20 @@ end
Registers all event handlers.
]]
function DiggyHole.register(config)
robot_mining.damage = config.robot_initial_mining_damage
ScoreTable.set('Robot mining damage', robot_mining.damage)
ScoreTable.reset('Void removed')
Event.add(defines.events.on_entity_died, function (event)
diggy_hole(event.entity)
local entity = event.entity
local name = entity.name
if name ~= 'sand-rock-big' and name ~= 'rock-huge' then
return
end
diggy_hole(entity)
if event.cause then
CreateParticles.destroy_rock(entity.surface.create_entity, 10, entity.position)
end
end)
Event.add(defines.events.on_entity_damaged, function (event)
@ -223,7 +280,7 @@ function DiggyHole.register(config)
local health = entity.health
local remove = event.buffer.remove
health = health - 10
health = health - robot_mining.damage
remove({name = 'stone', count = 100})
remove({name = 'coal', count = 100})
@ -234,12 +291,14 @@ function DiggyHole.register(config)
if health < 1 then
raise_event(defines.events.on_entity_died, {entity = entity, force = force})
CreateParticles.mine_rock(create_entity, 6, position)
entity.destroy()
return
end
entity.destroy()
local rock = create_entity({name = name, position = position})
CreateParticles.mine_rock(create_entity, 1, position)
rock.graphics_variation = graphics_variation
rock.order_deconstruction(force)
rock.health = health
@ -248,10 +307,12 @@ function DiggyHole.register(config)
Event.add(defines.events.on_player_mined_entity, function (event)
local entity = event.entity
local name = entity.name
if name ~= 'sand-rock-big' and name ~= 'rock-huge' then
return
end
if name == 'sand-rock-big' or name == 'rock-huge' then
event.buffer.remove({name = 'coal', count = 100})
event.buffer.remove({name = 'stone', count = 100})
event.buffer.remove({name = 'coal', count = 100})
event.buffer.remove({name = 'stone', count = 100})
--[[ this logic can be replaced once we've fully replaced the stone to surface functionality
if enable_digging_warning then
@ -267,6 +328,7 @@ function DiggyHole.register(config)
end
diggy_hole(entity)
CreateParticles.mine_rock(entity.surface.create_entity, 6, entity.position)
end)
Event.add(defines.events.on_robot_mined_tile, function (event)
@ -281,6 +343,18 @@ function DiggyHole.register(config)
ScoreTable.increment('Void removed')
end)
Event.add(defines.events.on_research_finished, function (event)
local new_modifier = event.research.force.mining_drill_productivity_bonus * 50 * config.robot_damage_per_mining_prod_level
if (robot_mining.research_modifier == new_modifier) then
-- something else was researched
return
end
robot_mining.research_modifier = new_modifier
update_robot_mining_damage()
end)
if config.enable_debug_commands then
commands.add_command('clear-void', '<left top x> <left top y> <width> <height> <surface index> triggers Template.insert for the given area.', function(cmd)
local params = {}

View File

@ -12,73 +12,47 @@ Global.register({
scores = tbl.scores
end)
--[[--
Resets the score 0 for the given name
@param name String
]]
---Resets the score 0 for the given name
---@param name string
function ScoreTable.reset(name)
scores[name] = 0
end
--[[--
Adds score.
@param name String
@param value int amount to add
@return int the sum for the score added by name
]]
---Adds score.
---@param name string
---@param value number the sum for the score added by name
---@return number the sum for the score added by the name
function ScoreTable.add(name, value)
local new = (scores[name] or 0) + value
scores[name] = new
return new
end
--[[--
Increments the score by 1 for name.
---Sets score.
---@param name string
---@param value number the sum for the score added by name
function ScoreTable.set(name, value)
scores[name] = value
end
@param name String
@return int the sum for the score incremented by name
]]
---Increments the score by 1 for name.
---@param name string
---@return number the sum for the score incremented by name
function ScoreTable.increment(name)
return ScoreTable.add(name, 1)
end
--[[--
Returns the score for a single key.
@param
]]
---Returns the score for a single key.
---@param name string
---@return number the sum for the score by name
function ScoreTable.get(name)
return scores[name] or 0
end
--[[--
Returns all scores.
@return table {[string] = int}
]]
---Returns all scores.
---@return table {[string] = int}
function ScoreTable.all()
return scores
end
--[[--
Returns all keys of table scores.
@return table {[string] = name of key}
]]
function ScoreTable.all_keys()
local keyset = {}
local n = 0
for k, v in pairs(scores) do
n = n + 1
keyset[n] = k
end
return keyset
end
return ScoreTable