1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-30 04:30:58 +02:00

Merge pull request #289 from iltar/coin-artefact-system

Added a "comeptitive" system, which lets Diggy "matches" compete
This commit is contained in:
Lynn 2018-11-11 19:39:07 +01:00 committed by GitHub
commit 0244e63213
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 383 additions and 69 deletions

View File

@ -35,6 +35,11 @@ local Config = {
manual_mining_speed_modifier = 1000,
character_inventory_slots_bonus = 1000,
character_running_speed_modifier = 2,
starting_items = {
{name = 'modular-armor', count = 1},
{name = 'submachine-gun', count = 1},
{name = 'uranium-rounds-magazine', count = 200},
},
},
},
@ -91,6 +96,58 @@ local Config = {
}
},
-- Adds the ability to drop coins and track how many are sent into space
ArtefactHunting = {
enabled = true,
-- value between 0 and 1, higher value means stronger variance between coordinates
noise_variance = 0.75,
-- minimum noise value to spawn a treasure chest, works best with a very high noise variance,
-- otherwise you risk spawning a lot of chests together
treasure_chest_noise_threshold = 0.69,
-- minimum distance from spawn where a chest can spawn
minimal_treasure_chest_distance = 25,
-- chances to receive a coin when mining
mining_artefact_chance = 0.10,
mining_artefact_amount = {min = 1, max = 4},
-- lets you set the coin modifiers for aliens
-- the modifier value increases the upper random limit that biters can drop
alien_coin_modifiers = {
['small-biter'] = 1,
['small-spitter'] = 1,
['medium-biter'] = 2,
['medium-spitter'] = 2,
['big-biter'] = 4,
['big-spitter'] = 4,
['behemoth-biter'] = 6,
['behemoth-spitter'] = 6,
},
-- Shows the chest locations, only use when debugging
display_chest_locations = false,
treasure_chest_raffle = {
['coin'] = {chance = 1.00, min = 20, max = 255},
['steel-axe'] = {chance = 0.55, min = 1, max = 2},
['stone'] = {chance = 0.50, min = 25, max = 75},
['copper-ore'] = {chance = 0.25, min = 30, max = 60},
['copper-plate'] = {chance = 0.10, min = 12, max = 25},
['iron-ore'] = {chance = 0.20, min = 10, max = 55},
['iron-plate'] = {chance = 0.10, min = 5, max = 25},
['steel-plate'] = {chance = 0.05, min = 3, max = 14},
['steel-furnace'] = {chance = 0.02, min = 1, max = 1},
['steam-engine'] = {chance = 0.02, min = 1, max = 1},
['coal'] = {chance = 0.40, min = 30, max = 55},
['concrete'] = {chance = 0.14, min = 10, max = 50},
['stone-brick'] = {chance = 0.14, min = 25, max = 75},
['stone-wall'] = {chance = 0.50, min = 1, max = 3},
}
},
-- replaces the chunks with void
RefreshMap = {
enabled = true,

View File

@ -30,7 +30,7 @@ end
global.message_count = 0
--[[--
Shows the given message if _DEBUG == true.
Shows the given message if debug is enabled.
@param message string
]]
@ -43,6 +43,15 @@ function Debug.print(message)
end
end
--[[--
Shows the given message with serpent enabled, if debug is enabled.
@param message string
]]
function Debug.print_serpent(message)
Debug.print(serpent.line(message))
end
--[[--
Shows the given message if _DEBUG == true for a given position.
@ -50,7 +59,7 @@ end
@param y number
@param message string
]]
function Debug.printPosition(position, message)
function Debug.print_position(position, message)
message = message or ''
if type(message) ~= 'string' and type(message) ~= 'number' and type(message) ~= 'boolean' then message = type(message) end
global.message_count = global.message_count + 1
@ -60,7 +69,7 @@ function Debug.printPosition(position, message)
end
--[[--
Executes the given callback if _DIGGY_CHEATS == true.
Executes the given callback if cheating is enabled.
@param callback function
]]
@ -76,45 +85,59 @@ end
@param value between -1 and 1
@param surface LuaSurface
@param position Position {x, y}
@param scale float
@param offset float
@param immutable bool if immutable, only set, never do a surface lookup, values never change
]]
function Debug.print_grid_value(value, surface, position, scale, offset)
scale = scale or 1
offset = offset or 0
position = {x = position.x + offset, y = position.y + offset}
local r = max(1, value) / scale
local g = 1 - abs(value) / scale
local b = min(1, value) / scale
function Debug.print_grid_value(value, surface, position, scale, offset, immutable)
local is_string = type(value) == 'string'
local color = {r = 1, g = 1, b = 1}
text = value
if (r > 0) then
r = 0
if type(immutable) ~= 'boolean' then
immutable = false
end
if (b < 0) then
b = 0
if not is_string then
scale = scale or 1
offset = offset or 0
position = {x = position.x + offset, y = position.y + offset}
local r = max(1, value) / scale
local g = 1 - abs(value) / scale
local b = min(1, value) / scale
if (r > 0) then
r = 0
end
if (b < 0) then
b = 0
end
if (g < 0) then
g = 0
end
r = abs(r)
color = { r = r, g = g, b = b}
-- round at precision of 2
text = floor(100 * value) * 0.01
if (0 == text) then
text = '0.00'
end
end
if (g < 0) then
g = 0
end
if not immutable then
local text_entity = surface.find_entity('flying-text', position)
r = abs(r)
local color = { r = r, g = g, b = b}
-- round at precision of 2
local text = floor(100 * value) / 100
if (0 == text) then
text = '0.00'
end
local text_entity = surface.find_entity('flying-text', position)
if text_entity then
text_entity.text = text
text_entity.color = color
return
if text_entity then
text_entity.text = text
text_entity.color = color
return
end
end
surface.create_entity{

View File

@ -37,11 +37,12 @@ end
function AlienSpawner.register(config)
local alien_minimum_distance_square = config.alien_minimum_distance ^ 2
Event.add(Template.events.on_void_removed, function(event)
Event.add(Template.events.on_void_removed, function (event)
game.forces.enemy.evolution_factor = game.forces.enemy.evolution_factor + 0.0000012
local x = event.old_tile.position.x
local y = event.old_tile.position.y
local position = event.position
local x = position.x
local y = position.y
if (x * x + y * y < alien_minimum_distance_square or config.alien_probability < random()) then
return

View File

@ -0,0 +1,155 @@
--[[-- info
Provides the ability to collect artefacts and send them to space.
]]
-- dependencies
local Event = require 'utils.event'
local Game = require 'utils.game'
local ScoreTable = require 'map_gen.Diggy.ScoreTable'
local Debug = require 'map_gen.Diggy.Debug'
local Template = require 'map_gen.Diggy.Template'
local Perlin = require 'map_gen.shared.perlin_noise'
local random = math.random
local ceil = math.ceil
-- this
local ArtefactHunting = {}
--[[--
Registers all event handlers.
]]
function ArtefactHunting.register(config)
ScoreTable.reset('Artefacts sent to space')
local seed
local function get_noise(surface, x, y)
seed = seed or surface.map_gen_settings.seed + surface.index + 300
return Perlin.noise(x * config.noise_variance * 0.9, y * config.noise_variance * 1.1, seed)
end
local distance_required = config.minimal_treasure_chest_distance * config.minimal_treasure_chest_distance
Event.add(defines.events.on_rocket_launched, function (event)
local coins = event.rocket.get_inventory(defines.inventory.rocket).get_item_count('coin')
if coins > 0 then
local sum = ScoreTable.add('Artefacts sent to space', coins)
game.print('sent ' .. coins .. ' artefacts into space! The space station is now holding ' .. sum .. ' artefacts.')
end
end)
Event.add(Template.events.on_void_removed, function (event)
local position = event.position
local x = position.x
local y = position.y
if (x * x + y * y <= distance_required) then
return
end
local surface = event.surface
if get_noise(surface, x, y) < config.treasure_chest_noise_threshold then
return
end
local chest = surface.create_entity({name = 'steel-chest', position = position, force = game.forces.player})
if not chest then
return
end
for name, prototype in pairs(config.treasure_chest_raffle) do
if random() <= prototype.chance then
chest.insert({name = name, count = random(prototype.min, prototype.max)})
end
end
end)
local modifiers = config.alien_coin_modifiers
local function picked_up_coins(player_index, count)
local text
if count == 1 then
text = '+1 coin'
else
text = '+' .. count ..' coins'
end
Game.print_player_floating_text(player_index, text, {r = 255, g = 215, b = 0})
end
Event.add(defines.events.on_entity_died, function (event)
local entity = event.entity
local force = entity.force
if force.name ~= 'enemy' then
return
end
local cause = event.cause
if not cause or cause.type ~= 'player' or not cause.valid then
return
end
local modifier = modifiers[entity.name] or 1
local evolution_multiplier = force.evolution_factor * 11
local count = random(
ceil(2 * evolution_multiplier * 0.1),
ceil(5 * (evolution_multiplier * evolution_multiplier + modifier) * 0.1)
)
entity.surface.create_entity({
name = 'item-on-ground',
position = entity.position,
stack = {name = 'coin', count = count}
})
end)
Event.add(defines.events.on_picked_up_item, function (event)
local stack = event.item_stack
if stack.name ~= 'coin' then
return
end
picked_up_coins(event.player_index, stack.count)
end)
Event.add(defines.events.on_pre_player_mined_item, function (event)
if event.entity.type ~= 'simple-entity' then
return
end
if random() > config.mining_artefact_chance then
return
end
local count = random(config.mining_artefact_amount.min, config.mining_artefact_amount.max)
local player_index = event.player_index
Game.get_player_by_index(player_index).insert({name = 'coin', count = count})
picked_up_coins(player_index, count)
end)
if (config.display_chest_locations) then
Event.add(defines.events.on_chunk_generated, function (event)
local surface = event.surface
local area = event.area
for x = area.left_top.x, area.left_top.x + 31 do
local sq_x = x * x
for y = area.left_top.y, area.left_top.y + 31 do
if sq_x + y * y >= distance_required and get_noise(surface, x, y) >= config.treasure_chest_noise_threshold then
Debug.print_grid_value('chest', surface, {x = x, y = y}, nil, nil, true)
end
end
end
end)
end
end
function ArtefactHunting.get_extra_map_info(config)
return 'Artefact Hunting, find precious coins while mining and launch them to the surface!'
end
return ArtefactHunting

View File

@ -6,6 +6,7 @@ require 'utils.list_utils'
local Event = require 'utils.event'
local Template = require 'map_gen.Diggy.Template'
local ScoreTable = require 'map_gen.Diggy.ScoreTable'
local Debug = require 'map_gen.Diggy.Debug'
local Task = require 'utils.Task'
local Token = require 'utils.global_token'
@ -50,8 +51,7 @@ local deconstruction_alert_message_shown = {}
local stress_map_storage = {}
local new_tile_map = {}
local collapse_positions_storage = {}
local cave_collapse_disabled = nil
local cave_collapse_disabled
Global.register({
new_tile_map = new_tile_map,
@ -94,7 +94,6 @@ local function create_collapse_template(positions, surface)
if strength then
do_insert = false
else
local position = entity.position
entity.die()
end
end)
@ -136,6 +135,7 @@ local function collapse(args)
)
local entities = create_collapse_template(positions, surface)
Template.insert(surface, {}, entities)
ScoreTable.increment('Cave collapse')
end
local on_collapse_timeout_finished = Token.register(collapse)
@ -164,7 +164,6 @@ local function spawn_cracking_sound_text(surface, position)
end
local function on_collapse_triggered(event)
if cave_collapse_disabled then return end --kill switch
local surface = event.surface
@ -259,7 +258,7 @@ end)
local function on_void_removed(event)
local strength = support_beam_entities['out-of-map']
local position = event.old_tile.position
local position = event.position
if strength then
stress_map_add(event.surface, position, strength)
end
@ -289,6 +288,8 @@ function DiggyCaveCollapse.register(cfg)
config = cfg
support_beam_entities = config.support_beam_entities
ScoreTable.reset('Cave collapse')
Event.add(DiggyCaveCollapse.events.on_collapse_triggered, on_collapse_triggered)
Event.add(defines.events.on_robot_built_entity, on_built_entity)
Event.add(defines.events.on_robot_built_tile, function (event)
@ -313,7 +314,7 @@ function DiggyCaveCollapse.register(cfg)
end
end)
Event.add(defines.events.on_pre_player_mined_item, function(event)
Event.add(defines.events.on_pre_player_mined_item, function (event)
local player_index = event.player_index
if (nil ~= deconstruction_alert_message_shown[player_index]) then
return
@ -449,7 +450,7 @@ local function add_fraction(stress_map, x, y, fraction)
return value
end
on_surface_created = function(event)
on_surface_created = function (event)
stress_map_storage[event.surface_index] = {}
local map = stress_map_storage[event.surface_index]

View File

@ -7,6 +7,7 @@
local Event = require 'utils.event'
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 insert = table.insert
local random = math.random
@ -67,6 +68,8 @@ end
Registers all event handlers.
]]
function DiggyHole.register(config)
ScoreTable.reset('Void removed')
Event.add(defines.events.on_entity_died, function (event)
local entity = event.entity
diggy_hole(entity)
@ -92,14 +95,18 @@ function DiggyHole.register(config)
diggy_hole(event.entity)
end)
Event.add(defines.events.on_robot_mined_tile, function(event)
Event.add(defines.events.on_robot_mined_tile, function (event)
on_mined_tile(event.robot.surface, event.tiles)
end)
Event.add(defines.events.on_player_mined_tile, function(event)
Event.add(defines.events.on_player_mined_tile, function (event)
on_mined_tile(game.surfaces[event.surface_index], event.tiles)
end)
Event.add(Template.events.on_void_removed, function ()
ScoreTable.increment('Void removed')
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

@ -185,8 +185,8 @@ local function on_research_finished(event)
end
local function comma_value(n) -- credit http://richard.warburton.it
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
local left,num,right = string.match(n, '^([^%d]*%d)(%d*)(.-)$')
return left .. (num:reverse():gsub('(%d%d%d)', '%1,'):reverse()) .. right
end
local function redraw_title(data)
@ -567,7 +567,7 @@ function MarketExchange.register(cfg)
local markets = find_entities_filtered({name = 'market', position = config.market_spawn_position, limit = 1})
if (#markets == 0) then
Debug.printPosition(config.market_spawn_position, 'Unable to find a market')
Debug.print_position(config.market_spawn_position, 'Unable to find a market')
return
end

View File

@ -81,15 +81,16 @@ function ScatteredResources.register(config)
error('Expected a sum of 1.00, got \'' .. richness_sum .. '\' for config.feature.ScatteredResources.resource_richness_probability.')
end
Event.add(Template.events.on_void_removed, function(event)
local x = event.old_tile.position.x
local y = event.old_tile.position.y
Event.add(Template.events.on_void_removed, function (event)
local position = event.position
local x = position.x
local y = position.y
local surface = event.surface
local distance = floor(sqrt(x * x + y * y))
if (config.cluster_mode and get_noise(surface, x, y) > config.noise_resource_threshold) then
spawn_resource(config, event.surface, x, y, distance)
spawn_resource(config, surface, x, y, distance)
return
end
@ -106,7 +107,7 @@ function ScatteredResources.register(config)
end
if (probability > random()) then
spawn_resource(config, event.surface, x, y, distance)
spawn_resource(config, surface, x, y, distance)
end
end)
@ -117,7 +118,7 @@ function ScatteredResources.register(config)
for x = area.left_top.x, area.left_top.x + 31 do
for y = area.left_top.y, area.left_top.y + 31 do
Debug.print_grid_value(get_noise(surface, x, y), surface, {x = x, y = y})
Debug.print_grid_value(get_noise(surface, x, y), surface, {x = x, y = y}, nil, nil, true)
end
end
end)

View File

@ -20,11 +20,12 @@ global.SetupPlayer = {
function SetupPlayer.register(config)
Event.add(defines.events.on_player_created, function (event)
local player = Game.get_player_by_index(event.player_index)
local player_insert = player.insert
local position = {0, 0}
local surface = player.surface
for _, item in pairs(config.starting_items) do
player.insert(item)
player_insert(item)
end
if (global.SetupPlayer.first_player_spawned) then
@ -37,9 +38,14 @@ function SetupPlayer.register(config)
player.teleport(position)
Debug.cheat(function()
player.force.manual_mining_speed_modifier = config.cheats.manual_mining_speed_modifier
player.force.character_inventory_slots_bonus = config.cheats.character_inventory_slots_bonus
player.character_running_speed_modifier = config.cheats.character_running_speed_modifier
local cheats = config.cheats
player.force.manual_mining_speed_modifier = cheats.manual_mining_speed_modifier
player.force.character_inventory_slots_bonus = cheats.character_inventory_slots_bonus
player.force.character_running_speed_modifier = cheats.character_running_speed_modifier
for _, item in pairs(cheats.starting_items) do
player_insert(item)
end
end)
end)
end

View File

@ -21,7 +21,7 @@ local do_mine = Token.register(function(params)
local sand_rocks = params.surface.find_entities_filtered({position = params.position, name = 'sand-rock-big'})
if (0 == #sand_rocks) then
Debug.printPosition(params.position, 'missing rock when trying to mine.')
Debug.print_position(params.position, 'missing rock when trying to mine.')
return
end
@ -49,7 +49,6 @@ end
--[[--
Registers all event handlers.
]]
function SimpleRoomGenerator.register(config)
local room_noise_minimum_distance_sq = config.room_noise_minimum_distance * config.room_noise_minimum_distance
@ -60,7 +59,7 @@ function SimpleRoomGenerator.register(config)
end
Event.add(Template.events.on_void_removed, function (event)
local position = event.old_tile.position
local position = event.position
local x = position.x
local y = position.y
@ -87,7 +86,7 @@ function SimpleRoomGenerator.register(config)
for x = area.left_top.x, area.left_top.x + 31 do
for y = area.left_top.y, area.left_top.y + 31 do
Debug.print_grid_value(get_noise(surface, x, y), surface, {x = x, y = y})
Debug.print_grid_value(get_noise(surface, x, y), surface, {x = x, y = y}, nil, nil, true)
end
end
end)

View File

@ -0,0 +1,67 @@
-- dependencies
local Global = require 'utils.global'
-- this
local ScoreTable = {}
local scores = {}
Global.register({
scores = scores,
}, function (tbl)
scores = tbl.scores
end)
--[[--
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
]]
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.
@param name String
@return int 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
]]
function ScoreTable.get(name)
return scores[name] or 0
end
--[[--
Returns all scores.
@return table {[string] = int}
]]
function ScoreTable.all()
return scores
end
return ScoreTable

View File

@ -45,10 +45,7 @@ local function insert_next_tiles(data)
local new_is_void = new_tile.name == 'out-of-map'
if (current_is_void and not new_is_void) then
insert(
void_removed,
{surface = surface, old_tile = {name = current_tile.name, position = current_tile.position}}
)
insert(void_removed, {surface = surface, position = current_tile.position})
end
end
end
@ -164,7 +161,7 @@ function Template.units(surface, units, non_colliding_distance)
entity.position = position
create_entity(entity)
else
Debug.printPosition(entity.position, "Failed to spawn '" .. entity.name .. "'")
Debug.print_position(entity.position, "Failed to spawn '" .. entity.name .. "'")
end
end
end