From f4ec6aa1966244c9d287b4ae6bf3b9cd78411d84 Mon Sep 17 00:00:00 2001 From: plague006 Date: Mon, 21 Jan 2019 14:28:21 -0500 Subject: [PATCH] Remove get_random, add get_random_dictionary_entry, and may we never speak of it again. --- features/gui/player_list.lua | 4 +++- features/market.lua | 4 +--- features/redmew_qol.lua | 6 +++--- map_gen/Diggy/Feature/DiggyCaveCollapse.lua | 4 +--- map_gen/misc/silly_player_names.lua | 7 ++++--- map_gen/misc/wreck_items.lua | 11 ++++++----- utils/table.lua | 12 ++++-------- 7 files changed, 22 insertions(+), 26 deletions(-) diff --git a/features/gui/player_list.lua b/features/gui/player_list.lua index 0df94e2d..e25781d7 100644 --- a/features/gui/player_list.lua +++ b/features/gui/player_list.lua @@ -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 diff --git a/features/market.lua b/features/market.lua index 9dd35238..5f1ab8f3 100644 --- a/features/market.lua +++ b/features/market.lua @@ -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 diff --git a/features/redmew_qol.lua b/features/redmew_qol.lua index 36f4ea86..7070a7b8 100644 --- a/features/redmew_qol.lua +++ b/features/redmew_qol.lua @@ -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}, diff --git a/map_gen/Diggy/Feature/DiggyCaveCollapse.lua b/map_gen/Diggy/Feature/DiggyCaveCollapse.lua index 35652d17..361f5fdc 100644 --- a/map_gen/Diggy/Feature/DiggyCaveCollapse.lua +++ b/map_gen/Diggy/Feature/DiggyCaveCollapse.lua @@ -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 diff --git a/map_gen/misc/silly_player_names.lua b/map_gen/misc/silly_player_names.lua index bac0fc10..5a2f91bf 100644 --- a/map_gen/misc/silly_player_names.lua +++ b/map_gen/misc/silly_player_names.lua @@ -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 diff --git a/map_gen/misc/wreck_items.lua b/map_gen/misc/wreck_items.lua index 0d518099..07bb1414 100644 --- a/map_gen/misc/wreck_items.lua +++ b/map_gen/misc/wreck_items.lua @@ -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 diff --git a/utils/table.lua b/utils/table.lua index 9e36b78a..496d912f 100644 --- a/utils/table.lua +++ b/utils/table.lua @@ -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 to select an element from +-- @param key 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