2
我输入的格式如下表:填写表格保存规则
_input = {
["Item1"] = {
min = 1,
max = 1,
pos = {
[1] = nil,
[2] = {--[[somedata]]},
[3] = nil,
[4] = {--[[somedata]]},
[5] = nil,
[6] = {--[[somedata]]},
[7] = nil,
[8] = {--[[somedata]]},
},
},
["Item2"] = {
min = 1,
max = 1,
pos = {
[1] = nil,
[2] = nil,
[3] = nil,
[4] = {--[[somedata]]},
[5] = {--[[somedata]]},
[6] = {--[[somedata]]},
[7] = nil,
[8] = nil,
},
},
["Item3"] = {
min = 1,
max = 2,
pos = {
[1] = nil,
[2] = {--[[somedata]]},
[3] = nil,
[4] = {--[[somedata]]},
[5] = {--[[somedata]]},
[6] = {--[[somedata]]},
[7] = nil,
[8] = nil,
},
},
["Item4"] = {
min = 1,
max = 3,
pos = {
[1] = {--[[somedata]]},
[2] = {--[[somedata]]},
[3] = {--[[somedata]]},
[4] = nil,
[5] = nil,
[6] = nil,
[7] = {--[[somedata]]},
[8] = {--[[somedata]]},
},
},
}
在_input
每个条目的领域min
,max
和pos
,而pos
本身包含8个条目,要么nil
或填充与数据。 在_input
中并不总是给出四个项目。可以有更多的项目或更少的项目。
我的目标是从_input
创建可以生成单个表的算法,填充有适当的值和保存min
/max
规则(即:在最后的表从pos
数据项的最小/最大数量。最终输出中必须有min
项目,最终输出中可能有max
项目)。
给定上述输入,输出可能看起来像这样:
_output = {
[1] = {
type = "Item4",
data = {--[[the data from _input["Item4"].pos[1] ]]},
},
[2] = {
type = "Item1",
data = {--[[the data from _input["Item1"].pos[2] ]]},
},
[3] = {
type = "Item4",
data = {--[[the data from _input["Item4"].pos[3] ]]},
},
[4] = {
type = "Item3",
data = {--[[the data from _input["Item3"].pos[4] ]]},
},
[5] = nil,
[6] = {
type = "Item2",
data = {--[[the data from _input["Item2"].pos[6] ]]},
},
[7] = {
type = "Item4",
data = {--[[the data from _input["Item4"].pos[7] ]]},
},
[8] = nil,
}
不是在输出的每个字段具有被填充:
5
和8
是在上面的例子中为零。
5
无法填充,因为唯一可能的项目将是Item2
和Item3
。 Item2
已达到最大金额,Item3
不必达到最大金额。
8
无法填充,因为可能的项目Item1
和Item4
都已达到其最大数量。
这是迄今为止我的方法,但它不保留所有规则并产生“错误”输出。此外,我希望每次都不要从相同的输入中获得相同的结果。
local _output = {
[1] = nil,
[2] = nil,
[3] = nil,
[4] = nil,
[5] = nil,
[6] = nil,
[7] = nil,
[8] = nil,
}
for key in pairs(_input) do
local _item = _input[key]
for i=0,math.random(_item.min, _item.max),1 do
-- I omit deepCopy() for readability
local _possibleCopy = deepCopy(_item.pos)
for i=1,8,1 do
if _output[i] ~= nil then
_possibleCopy[i] = nil
end
end
local _possibleSlots = {}
for i=1,8,1 do
if _possibleCopy[i] ~= nil then
_possibleSlots[#_possibleSlots+1] = i
end
end
local _slot = _possibleSlots[math.random(1,#_possibleSlots)]
if _slot then
_output[_slot] = {
type = key,
data = _item.pos[_slot],
}
end
end
end
我欠你一个啤酒。你救了我很多头痛!谢谢!出于好奇的一个问题:你写了'不是v.pos [i]' - 是否有双重原因? –
这是显式转换为布尔值(数组将仅包含布尔值而不是nils和表)。当然,你可以省略'not not',那不会改变任何东西。 –