mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-03-11 14:49:59 +02:00
Add regulars to potential train station names (#561)
* Add regulars to potential train station names * Decrease odds of in-game player and regular, increase odds of backer * Clarify that _player_ is not the player that placed the entity * Specify random's use, cover case of nil random_player
This commit is contained in:
parent
014b53aa42
commit
074ea00c54
@ -1,5 +1,19 @@
|
||||
local Event = require 'utils.event'
|
||||
|
||||
local function pick_name(event)
|
||||
-- Create a weight table comprised of the backer name, a player's name, and a regular's name
|
||||
local random_player = table.get_random(game.players, true)
|
||||
if not random_player then
|
||||
return
|
||||
end
|
||||
local name_table = {
|
||||
{event.created_entity.backer_name, 8},
|
||||
{random_player.name, 1},
|
||||
{table.get_random(global.regulars, false, true), 1},
|
||||
}
|
||||
return table.get_random_weighted(name_table)
|
||||
end
|
||||
|
||||
local function player_built_entity(event)
|
||||
local entity = event.created_entity
|
||||
if not entity or not entity.valid then
|
||||
@ -7,11 +21,7 @@ local function player_built_entity(event)
|
||||
end
|
||||
|
||||
if entity.name == 'train-stop' then
|
||||
local y = math.random(1, 3)
|
||||
if y ~= 1 then
|
||||
local player = table.get_random(game.players, true)
|
||||
event.created_entity.backer_name = player.name
|
||||
end
|
||||
event.created_entity.backer_name = pick_name(event) or event.created_entity.backer_name
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -73,28 +73,36 @@ table.set = function(t, index, element)
|
||||
end
|
||||
|
||||
--- Chooses a random entry from a table
|
||||
--@param t table to select an element from
|
||||
--@param sorted boolean to indicate whether the table is sorted by numerical index or not
|
||||
--@return a random element of table t
|
||||
table.get_random = function(t, sorted)
|
||||
-- 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
|
||||
-- @return a random element of table t
|
||||
table.get_random = function(t, sorted, key)
|
||||
if sorted then
|
||||
return t[random(#t)]
|
||||
end
|
||||
local target_index = random(1, table_size(t))
|
||||
local count = 1
|
||||
for _, v in pairs(t) do
|
||||
for k, v in pairs(t) do
|
||||
if target_index == count then
|
||||
return t[v]
|
||||
if key then
|
||||
return k
|
||||
else
|
||||
return t[v]
|
||||
end
|
||||
end
|
||||
count = count + 1
|
||||
end
|
||||
end
|
||||
|
||||
--- Chooses a random entry from a weighted table
|
||||
-- @param weight_table a table of tables with items and their weights
|
||||
-- @param item_index the index of the items, defaults to 1
|
||||
-- @param weight_index the index of the weights, defaults to 2
|
||||
-- @returns a random item with weighting
|
||||
-- @see features.chat_triggers.hodor
|
||||
-- because this uses math.random, it cannot be used outside of events
|
||||
-- @param weight_table table of tables with items and their weights
|
||||
-- @param item_index number of the index of items, defaults to 1
|
||||
-- @param weight_index number of the index of the weights, defaults to 2
|
||||
-- @returns a table entry
|
||||
-- @see features.chat_triggers::hodor
|
||||
table.get_random_weighted = function(weighted_table, item_index, weight_index)
|
||||
local total_weight = 0
|
||||
item_index = item_index or 1
|
||||
@ -115,6 +123,7 @@ table.get_random_weighted = function(weighted_table, item_index, weight_index)
|
||||
end
|
||||
|
||||
--- Creates a fisher-yates shuffle of a sequential number-indexed table
|
||||
-- because this uses math.random, it cannot be used outside of events if no rng is supplied
|
||||
-- from: http://www.sdknews.com/cross-platform/corona/tutorial-how-to-shuffle-table-items
|
||||
-- @param t table to shuffle
|
||||
table.shuffle_table = function(t, rng)
|
||||
|
Loading…
x
Reference in New Issue
Block a user