Модуль:Prototypes/Хранилище/Предмет: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 59: | Строка 59: | ||
end | end | ||
return result | return result | ||
end | |||
-- Функция выбора элемента на основе веса | |||
local function pickByWeight(children) | |||
local totalWeight = 0 | |||
for _, child in ipairs(children) do | |||
totalWeight = totalWeight + (child.weight or 1) -- Если вес не указан, принимаем за 1 | |||
end | |||
local rand = math.random() * totalWeight -- Случайное число от 0 до totalWeight | |||
local cumulativeWeight = 0 | |||
for _, child in ipairs(children) do | |||
cumulativeWeight = cumulativeWeight + (child.weight or 1) | |||
if rand <= cumulativeWeight then | |||
return child | |||
end | |||
end | |||
return nil -- На случай, если ничего не выбрано (теоретически невозможно) | |||
end | end | ||
-- Обработка вложенных таблиц | -- Обработка вложенных таблиц | ||
local function processNestedSelectors(children) | local function processNestedSelectors(children, rolls) | ||
local result = '' | |||
for i = 1, rolls do | |||
local selectedChild = pickByWeight(children) | |||
if selectedChild then | |||
if selectedChild.id then | |||
result = result .. formatContent(selectedChild) | |||
elseif selectedChild["!type"] == "GroupSelector" and selectedChild.children then | |||
result = result .. processNestedSelectors(selectedChild.children, 1) | |||
elseif selectedChild["!type"] == "AllSelector" and selectedChild.children then | |||
result = result .. processAllSelectors(selectedChild.children) | |||
end | |||
end | |||
end | |||
return result | |||
end | |||
-- Обработка AllSelector | |||
local function processAllSelectors(children) | |||
local result = '' | local result = '' | ||
for _, child in ipairs(children) do | for _, child in ipairs(children) do | ||
if child.id then | if child.id then | ||
result = result .. formatContent(child) | result = result .. formatContent(child) | ||
elseif child["!type"] == " | elseif child["!type"] == "GroupSelector" and child.children then | ||
result = result .. | result = result .. processNestedSelectors(child.children, 1) | ||
elseif child["!type"] == "AllSelector" and child.children then | |||
result = result .. processAllSelectors(child.children) | |||
end | end | ||
end | end | ||
| Строка 75: | Строка 115: | ||
-- Обработка таблиц | -- Обработка таблиц | ||
local function getTableOutput(tableId) | local function getTableOutput(tableId, rolls) | ||
local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json', 'selectors') | local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json', 'selectors') | ||
local tableData = findDataById(allSelectors, tableId) | local tableData = findDataById(allSelectors, tableId) | ||
local children = tableData and tableData['!type:AllSelector'] and tableData['!type:AllSelector'].children | local children = tableData and (tableData['!type:GroupSelector'] and tableData['!type:GroupSelector'].children or tableData['!type:AllSelector'] and tableData['!type:AllSelector'].children) | ||
if not children then return 'Таблица не содержит элементов.' end | if not children then return 'Таблица не содержит элементов.' end | ||
return processNestedSelectors(children) | if tableData['!type:AllSelector'] then | ||
return processAllSelectors(children) | |||
else | |||
return processNestedSelectors(children, rolls) | |||
end | |||
end | end | ||
| Строка 98: | Строка 142: | ||
or containers.entity_storage and containers.entity_storage.tableId | or containers.entity_storage and containers.entity_storage.tableId | ||
or containers.other_storage and containers.other_storage.tableId | or containers.other_storage and containers.other_storage.tableId | ||
local rolls = containers.storagebase and containers.storagebase.rolls and containers.storagebase.rolls.value | |||
or containers.entity_storage and containers.entity_storage.rolls and containers.entity_storage.rolls.value | |||
or 1 | |||
if tableId then | if tableId then | ||
result = result .. getTableOutput(tableId) | result = result .. getTableOutput(tableId, rolls) | ||
else | else | ||
result = result .. 'Таблица не найдена.' | result = result .. 'Таблица не найдена.' | ||