1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00

Remove get_random, add get_random_dictionary_entry, and may we never speak of it again.

This commit is contained in:
plague006 2019-01-21 14:28:21 -05:00
parent e88c72f972
commit f4ec6aa196
7 changed files with 22 additions and 26 deletions

View File

@ -13,6 +13,8 @@ local table = require 'utils.table'
local poke_messages = require 'resources.poke_messages'
local player_sprites = require 'resources.player_sprites'
local random = math.random
local poke_cooldown_time = 240 -- in ticks.
local sprite_time_step = 54000 -- in ticks
local symbol_asc = ''
@ -716,7 +718,7 @@ Gui.on_click(
local count = (player_pokes[poke_player_index] or 0) + 1
player_pokes[poke_player_index] = count
local poke_str = table.get_random(poke_messages, true)
local poke_str = poke_messages[random(#poke_messages)]
local message = table.concat({'>> ', player.name, ' has poked ', poke_player.name, ' with ', poke_str, ' <<'})
for _, p in ipairs(game.connected_players) do

View File

@ -6,7 +6,6 @@ local PlayerStats = require 'features.player_stats'
local Game = require 'utils.game'
local Command = require 'utils.command'
local Retailer = require 'features.retailer'
local table = require 'utils.table'
local market_items = require 'resources.market_items'
local fish_market_bonus_message = require 'resources.fish_messages'
@ -15,7 +14,6 @@ local fish_market_bonus_message = require 'resources.fish_messages'
local pairs = pairs
local random = math.random
local format = string.format
local get_random = table.get_random
local currency = global.config.market.currency
-- local vars
@ -70,7 +68,7 @@ local function fish_earned(event, amount)
PlayerStats.change_coin_earned(player_index, amount)
if PlayerStats.get_coin_earned(player_index) % 70 == 0 and player and player.valid then
local message = get_random(fish_market_bonus_message, true)
local message = fish_market_bonus_message[random(#fish_market_bonus_message)]
player.print(message)
end
end

View File

@ -15,7 +15,7 @@ local table = require 'utils.table'
local config = global.config.redmew_qol
-- Localized functions
local get_random = table.get_random
local random = math.random
-- Local vars
local Public = {}
@ -70,7 +70,7 @@ local restrict_chest =
--- Selects a name from the entity backer name, game.players, and regulars
local function pick_name()
-- Create a weight table comprised of the backer name, a player's name, and a regular's name
local random_player = get_random(game.players, true)
local random_player = game.players[random(#game.players)]
if not random_player then
return
end
@ -80,7 +80,7 @@ local function pick_name()
if table.size(regulars) == 0 then
reg = nil
else
reg = {table.get_random(regulars, false, true), 1}
reg = {table.get_random_dictionary_entry(regulars, true), 1}
end
local name_table = {
{false, 8},

View File

@ -2,7 +2,6 @@
Provides the ability to collapse caves when digging.
]]
-- dependencies
local table = require 'utils.table'
local Event = require 'utils.event'
local Template = require 'map_gen.Diggy.Template'
local ScoreTable = require 'map_gen.Diggy.ScoreTable'
@ -18,7 +17,6 @@ local random = math.random
local floor = math.floor
local pairs = pairs
local pcall = pcall
local get_random = table.get_random
local is_diggy_rock = Template.is_diggy_rock
local increment_score = ScoreTable.increment
local template_insert = Template.insert
@ -176,7 +174,7 @@ local function spawn_collapse_text(surface, position)
surface.create_entity({
name = 'tutorial-flying-text',
color = color,
text = get_random(config.cracking_sounds, true),
text = config.cracking_sounds[random(#config.cracking_sounds)],
position = position,
})
end

View File

@ -9,6 +9,7 @@ local ScenarioInfo = require 'features.gui.info'
local Command = require 'utils.command'
local format = string.format
local random = math.random
ScenarioInfo.add_map_extra_info('- On this map you will be assigned a silly name.\n' .. '- If you dislike your name you can /name-restore or /name-roll for a new one')
@ -50,9 +51,9 @@ end
-- TODO: Config option to set the name style
local function create_name(words_table, player_name)
local adverb, adjective --, noun
adverb = table.get_random(words_table.adverbs, true)
adjective = table.get_random(words_table.adjectives, true)
--noun = table.get_random(words_table.nouns, true)
adverb = words_table[random(#words_table)]
adjective = words_table[random(#words_table)]
--noun = words_table[random(#words_table)]
local name = format('%s_%s_%s', adverb, adjective, player_name)
return string.gsub(name, "%s+", "_")
end

View File

@ -1,7 +1,8 @@
-- adds some wrecked items around the map, good for MP, reduces total resources pulled from factory, and adds incentive to push out
local table = require 'utils.table'
local Token = require 'utils.token'
local random = math.random
local wreck_item_pool = {
{name = 'iron-gear-wheel', count = 32},
{name = 'iron-plate', count = 64},
@ -44,14 +45,14 @@ local callback =
function(entity)
entity.health = math.random(entity.health)
entity.insert(wreck_item_pool[table.get_random(wreck_item_pool, true)])
entity.insert(wreck_item_pool[table.get_random(wreck_item_pool, true)])
entity.insert(wreck_item_pool[table.get_random(wreck_item_pool, true)])
entity.insert(wreck_item_pool[random(#wreck_item_pool)])
entity.insert(wreck_item_pool[random(#wreck_item_pool)])
entity.insert(wreck_item_pool[random(#wreck_item_pool)])
end
)
return function()
local ship = table.get_random(entity_list, true)
local ship = entity_list[random(#entity_list)]
if math.random(ship.chance) ~= 1 then
return nil

View File

@ -90,14 +90,10 @@ end
--- Chooses a random entry from a table
-- because this uses math.random, it cannot be used outside of events
-- @param t table to select an element from
-- @param sorted boolean to indicate whether the table is sorted by numerical index or not
-- @param key boolean to indicate whether to return the key or value
-- @param t <table> to select an element from
-- @param key <boolean> to indicate whether to return the key or value
-- @return a random element of table t
function table.get_random(t, sorted, key)
if sorted then
return t[random(#t)]
end
function table.get_random_dictionary_entry(t, key)
local target_index = random(1, table_size(t))
local count = 1
for k, v in pairs(t) do
@ -105,7 +101,7 @@ function table.get_random(t, sorted, key)
if key then
return k
else
return t[v]
return v
end
end
count = count + 1