-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgameVal.lua
266 lines (231 loc) · 5.45 KB
/
gameVal.lua
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
--[[this file use to automation generate gameValue designed by a csv file(Excel export)
I had wirtten this before£¬but lua only 200 lines, and just one day work
--]]
function csv_read_line(line,i,names)
local name,chinese,func = string.match(line,"(.-),(.-),(.+)")
--print(name,chinese,func)
local t = {}
t.name = name
t.chinese = chinese
local num = tonumber(func)
if(num == nil) then
t.funcname = func
else
t.init_value = num
end
t.fathers = {}
t.childs = {}
names[name] = i
return t;
end
function insert_table_onlyone(t,value)
for _,j in ipairs(t) do
if (j == value) then
return
end
end
table.insert(t,value)
end
function csv_set_father(csv,index,father)
insert_table_onlyone(csv[index].fathers,father)
insert_table_onlyone(csv[father].childs,index)
end
function bind_func(csv,index,t,file)
--[[if (kind == 2) then return level*1.5 else return level*2 end
--> function (val) if (val[2] == 2) then return val[1]*1.5 else return val[1]*2 end end--]]
if (t.funcname) then --not '0'
local s = t.funcname
for token in string.gmatch(s,"val_([%w_]+)") do
local f = csv.csv_name[token]
if (f == nil) then
error("not find token ["..token.."] at ".. s)
end
csv_set_father(csv,index,f)
end
for _,j in ipairs(t.fathers) do
local src = "val_"..csv[j].name
local des = "val["..j.."]"
s = string.gsub(s,src,des)
end
s = t.name.." = function (val) " .. s .. " end,\n"
--loadstring not good than require -- for error info
file:write(s)
end
end
function csv_remove_child(csv,f,c)
for i,j in ipairs(csv[f].childs) do
if (j == c) then
table.remove(csv[f].childs,i)
break
end
end
for i,j in ipairs(csv[c].fathers) do
if (j == f) then
table.remove(csv[c].fathers,i)
break
end
end
end
function csv_is_child(csv,f,c)
for i,j in ipairs(csv[f].childs) do
if (j == c) then
return true
end
end
return false
end
function csv_is_child_recursive(csv,f,c)
if (csv_is_child(csv,f,c)) then
return true
end
for _,k in ipairs(csv[c].fathers) do
if (csv_is_child_recursive(csv,f,k)) then
return true;
end
end
return false;
end
function get_father_level(csv,i)
local t = csv[i]
if (t.fathers == nil or #t.fathers == 0) then
return 0
end
local level = 0
for _,f in ipairs(t.fathers) do
local l = get_father_level(csv,f)
if (l > level) then
level = l
end
end
return level + 1
end
function csv_read(csv_file,csv_name)
local firstline = 1
local line = 1
local csv = {}
csv.csv_name = {}
for l in io.lines(csv_file) do
if (firstline == 1) then
firstline = 0
else
t = csv_read_line(l,line,csv.csv_name)
table.insert(csv,t)
line = line + 1
end
end
local tempfile = csv_name.."_val_func.lua"
local file = io.open(tempfile,"w")
--print(tempfile,file)
file:write("--begin val\n")
for i,t in ipairs(csv) do
--print(i,t.name)
file:write("val_"..t.name.." = "..i.."\n")
end
file:write("--end val\n\n")
file:write("return {\n")
for i,t in ipairs(csv) do
bind_func(csv,i,t,file)
end
file:write("}")
file:close()
csv.table_func = dofile(tempfile)
for _,t in ipairs(csv) do
if (t.funcname) then --not '0'
t.func = csv.table_func[t.name]
assert(t.func ~= nil)
end
end
--remove redundancy relationship£¬a-->b,c b-->c simplfy to: a-->b b-->c so less function call
for f,t in ipairs(csv) do
local rm = {}
for i,j in ipairs(t.childs) do --for all child if any (chlld->father) also is child then remove this child
for _,k in ipairs(csv[j].fathers) do
if csv_is_child_recursive(csv,f,k) then
rm[i] = j
break
end
end
end
for i,j in pairs(rm) do
print(f,t.name,i,j,csv[j].name," can reduced!")
csv_remove_child(csv,f,j)
end
end
csv.inits = {};
for level = 1,#csv do
for i,t in ipairs(csv) do
if (get_father_level(csv,i) == level) then
table.insert(csv.inits,i)
end
end
end
return csv
end
function make_table_size(t,size,value)
for i=1,size do
table.insert(t,value)
end
end
function csv_init(person,csv)
local val = {}
person.val = val
person.val.csv = csv
make_table_size(person.val,#csv,0)
for i,t in ipairs(csv) do
if (t.init_value ~= nil and t.init_value ~= 0) then
val[i] = t.init_value
--csv_change_value(person,i,t.init_value)
end
end
for _,i in ipairs(csv.inits) do
val[i] = csv[i].func(val)
end
end
function update_value(person,index,father)
local csv = person.val.csv
assert(csv[index].func ~= nil)
local val = person.val
val[index] = csv[index].func(val);
print(father.. " -> " .. index,csv[index].name.." = "..val[index])
for _,j in ipairs(csv[index].childs) do
update_value(person,j,index)
end
end
function csv_change_value(person,index,value)
local csv = person.val.csv
assert(csv[index].func == nil)
person.val[index] = value
print("csv_change_value",index,csv[index].name.." = "..value)
for _,j in ipairs(csv[index].childs) do
update_value(person,j,index)
end
print("csv_change_value end",index,value)
end
function csv_get_value(person,index)
return person.val[index]
end
function csv_set_value(person,index,value)
person.val[index] = value
end
function csv_print_val(person)
print("csv_print_val(person)",person)
local csv = person.val.csv
for i,j in ipairs(person.val) do
if (j ~= 0) then
print(csv[i].name.."("..csv[i].chinese ..") = "..j)
end
end
end
function csv_print(csv)
for i,t in ipairs(csv) do
s = ""
for i,j in ipairs(t.childs) do
s = s..j..","
end
f = ""
for i,j in ipairs(t.fathers) do
f = f..j..","
end
print (i,t.name,t.func,s,f)
end
end