1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
--Prototype for the item:
items =
{
	[300] = {
		name = "leather helmet",
		desc = "Protect yourself from headshots!",
		r = 128, g = 64, b = 0,
		action = "equip",
		slot = 1,
		eimage = "gfx/weiwen/helmet.png",
		fimage = "gfx/weiwen/helmet.png",
		def = 0.05,
		speed = -1,
		func = equip,
	}
}
prefix_num = 3
prefix_names = {"Hard","Light","Shiny"}
prefix_desc = {"+10% def", "+3 speed","Has a nice white color"}
prefix_stat = {
	{10, "%", "def"},
	{3, "+", "speed"},
	{255, "=", "r,g,b"}
}
function generate_instance(prototype) --prototype should be 300 for this example
	local t = table.copy(items[prototype])
	local bonuses = {}
	for i=1, math.random(0,prefix_num) do
		local newstat = math.random(1,prefix_num)
		while bonuses[newstat] do
			newstat = newstat%prefix_num+1
		end
		bonuses[newstat] = true
		t.name = prefix_names[newstat] .. t.name
		t.desc = t.desc .." ".. prefix_desc[newstat]
		for k, v in pairs(string.split(prefix_stat[newstat][3],",")) do --for each variable separated by comma
			local operator = prefix_stat[newstat][2]
			local ammount = prefix_stat[newstat][1]
			if operator == "%" then
				t[v] = t[v] + t[v]*ammount/100
			elseif operator == "+" then
				t[v] = t[v] + ammount
			elseif operator == "=" then
				t[v] = ammount
			end
		end
	end
	return t
end
--
--OUTPUT
--
local ins = generate_instance(300)
for k,v in pairs(ins) do
	print(k..": "..v)
end