conv_lvs.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. # Desc: 将用到的关卡文件压缩成程序使用的protobuf格式
  2. # Date: 2017-04-18
  3. import os
  4. import sys
  5. import json
  6. import levelData_pb2
  7. def read_json(file_path):
  8. with open(file_path, 'r') as file:
  9. return json.load(file)
  10. def write_json(data, file_path):
  11. with open(file_path, 'w+') as file:
  12. json.dump(data, file, indent=4)
  13. # 将 ../conf/levelInstInfo.csv 里面的每一个实例的模板名加上“tf_”前缀
  14. def add_prefix_to_inst_name():
  15. with open('../conf/levelInstInfo.csv', 'r') as f:
  16. hdr_done = False
  17. with open('../conf/levelInstInfo_new.csv', 'w+') as f_new:
  18. for line in f.readlines():
  19. if line.startswith('#'):
  20. f_new.write(line)
  21. continue
  22. if not hdr_done:
  23. f_new.write(line)
  24. hdr_done = True
  25. continue
  26. line = line.split(',')
  27. if line[1].startswith('tf_'):
  28. f_new.write(line)
  29. continue
  30. line[1] = 'tf_' + line[1]
  31. f_new.write(','.join(line))
  32. # 解析 ../conf/levelInstInfo.csv 文件,得到所有被用到的模板列表
  33. def parse_level_inst_info(groups):
  34. temp_list = set()
  35. for grp in groups:
  36. fn = '../conf/levelInstInfo.csv'
  37. if len(grp) != 0:
  38. fn = '../conf/levelInstInfo-' + grp + '.csv'
  39. with open(fn, 'r') as f:
  40. # 跳过第一行
  41. f.readline()
  42. for line in f.readlines():
  43. if line.startswith('#'):
  44. continue
  45. temp_list.add(line.split(',')[1])
  46. return temp_list
  47. # 将某个tiled的json文件转换成protobuf格式
  48. def conver_json_to_proto(tDir, tFileName, outDir):
  49. global lvFileInfo, lvInfo
  50. jsonFile = os.path.join(tDir, tFileName + '.json')
  51. with open(jsonFile, 'r') as f:#, encoding='utf-8'
  52. jsonData = json.load(f)
  53. levelData = levelData_pb2.LevelData()
  54. levelData.tileWidth = jsonData['width']
  55. levelData.tileHeight = jsonData['height']
  56. cntUndefined = 0
  57. tilesByPos = {}
  58. maxZ = 0
  59. maxX = 0
  60. maxY = 0
  61. # 首先得到stacked的坐标数据
  62. stacked_map = {}
  63. for item in jsonData['layers']:
  64. name = item['name']
  65. if name == 'Marks':
  66. data = item['data']
  67. for i in range(0, len(data)):
  68. if data[i] == 0:
  69. continue
  70. x = (int)(i % levelData.tileWidth)
  71. y = (int)(i / (int)(levelData.tileWidth))
  72. stacked_map[(x, y)] = 0
  73. # 处理各层的tile数据
  74. cntTiles = 0
  75. for item in jsonData['layers']:
  76. name = item['name']
  77. if not name.startswith('Tile_'):
  78. # stacked
  79. continue
  80. z = int(name[5:])
  81. data = item['data']
  82. for i in range(0, len(data)):
  83. if data[i] == 0:
  84. continue
  85. tile = levelData.tiles.add()
  86. tile.x = (int)(i % levelData.tileWidth)
  87. tile.y = (int)(i / (int)(levelData.tileWidth))
  88. tile.z = z
  89. if (tile.x, tile.y) in stacked_map:
  90. stacked_map[(x, y)] += 1
  91. tiledata = tile.tileData
  92. tiledata.zv = 0
  93. tiledata.weight = 0
  94. tiledata.id = data[i]
  95. if tiledata.id == -10:
  96. cntUndefined += 1
  97. tiledata.type = 1
  98. tiledata.subtype = 0
  99. # 一些统计信息
  100. tilesByPos[(tile.x, tile.y, tile.z)] = tile
  101. if tile.z > maxZ:
  102. maxZ = tile.z
  103. if tile.x > maxX:
  104. maxX = tile.x
  105. if tile.y > maxY:
  106. maxY = tile.y
  107. cntTiles += 1
  108. if cntTiles%3 != 0:
  109. print('Error: tile count is not a multiple of 3: %d' % (cntTiles,))
  110. exit(1)
  111. # 根据上面的布局信息,计算每个tile的几个信息:
  112. # 1. 每个tile的视觉层级(不同于上面z的信息,那是一个布局信息,相同的z可能出在不同的视觉层级)
  113. # 从maxZ开始,逐层向下计算;对于每一个位置,如果该位置有tile,则计算该tile的视觉层级
  114. # 对于每一个tile,如果其上方(从该tile的z+1层,一直到maxZ层)有tile,则其视觉层级为上方tile的视觉层级+1
  115. for z in range(0, maxZ+1):
  116. z = maxZ - z
  117. for x in range(0, maxX+1):
  118. for y in range(0, maxY+1):
  119. if (x,y,z) in tilesByPos:
  120. tile = tilesByPos[(x, y, z)]
  121. adjs = [(x,y),(x,y+1),(x,y-1),(x+1,y),(x+1,y+1),(x+1,y-1),(x-1,y),(x-1,y-1),(x-1,y+1)]
  122. # 确定视觉层级
  123. zvMax = -1
  124. for zup in range(z+1, maxZ+1):
  125. for adj in adjs:
  126. if (adj[0],adj[1],zup) in tilesByPos:
  127. zv = tilesByPos[adj[0],adj[1],zup].tileData.zv
  128. if zv > zvMax:
  129. zvMax = zv
  130. tile.tileData.zv = zvMax + 1
  131. # 计算权重
  132. # 从最底下开始,逐层向上计算
  133. # 每个tile的初始权重为4,然后加上其下方压住的tile的权重:如果压了全部,则该加上被压住的tile权重;
  134. # 如果压住了一半,则加上被压住的tile权重的一半;如果是压住1/4,则加上被压住的tile权重的1/4
  135. for z in range(0, maxZ+1):
  136. for x in range(0, maxX+1):
  137. for y in range(0, maxY+1):
  138. if (x,y,z) in tilesByPos:
  139. tile = tilesByPos[(x,y,z)]
  140. adjs = [(x,y,1.0),(x,y+1,0.5),(x,y-1,0.5),(x+1,y,0.5),(x+1,y+1,0.25),(x+1,y-1,0.25),(x-1,y,0.5),(x-1,y-1,0.25),(x-1,y+1,0.25)]
  141. weight = 4
  142. for adj in adjs:
  143. # 从临近层往下,在某个位置找到了tile,则该tile被压住了,就不再找了
  144. for zb in range(0, z):
  145. zb = z-1-zb
  146. if (adj[0], adj[1], zb) in tilesByPos:
  147. tileB = tilesByPos[(adj[0], adj[1], zb)]
  148. weight += tileB.tileData.weight * adj[2]
  149. break
  150. tile.tileData.weight = int(weight)
  151. pass
  152. for item in stacked_map:
  153. stack_data = levelData.stacks.add()
  154. stack_data.x = int(item[0])
  155. stack_data.y = int(item[1])
  156. stack_data.direction = 0 # 先默认都是0
  157. # 数据序列化
  158. level_protobuf_data = levelData.SerializeToString()
  159. protoFile = os.path.join(outDir, tFileName + '.bin')
  160. with open(protoFile, 'w+b') as f:
  161. f.write(level_protobuf_data)
  162. def gen_index_and_data(binDir, group = None):
  163. lvelsIndex = levelData_pb2.LevelsIndex()
  164. if group is not None:
  165. protoDataTrunk = os.path.join(binDir, 'levels-' + str(group) + '.bin')
  166. protoDataIndex = os.path.join(binDir, 'levelsIndex-' + str(group) + '.bin')
  167. else:
  168. protoDataTrunk = os.path.join(binDir, 'levels' + '.bin')
  169. protoDataIndex = os.path.join(binDir, 'levelsIndex' + '.bin')
  170. offset = 0
  171. for filename in os.listdir(binDir):
  172. my_message = levelData_pb2.LevelData()
  173. binFile = os.path.splitext(filename)[0] + '.bin'
  174. binFile = os.path.join(binDir, binFile)
  175. if os.path.exists(binFile) :
  176. # print(binFile)
  177. with open(binFile, "rb") as f:
  178. binary_data = f.read()
  179. my_message.ParseFromString(binary_data)
  180. os.remove(binFile)
  181. with open(protoDataTrunk, 'ab') as f:
  182. f.write(binary_data)
  183. lvelsIndex.LevelsIndex[os.path.splitext(filename)[0]].len = len(binary_data)
  184. lvelsIndex.LevelsIndex[os.path.splitext(filename)[0]].offset = offset
  185. offset += len(binary_data)
  186. level_protobuf_index = lvelsIndex.SerializePartialToString()
  187. with open(protoDataIndex, 'w+b') as f:
  188. f.write(level_protobuf_index)
  189. # 将所有的模板文件转换成protobuf格式
  190. def convert_all_templates():
  191. # 清空loadable目录
  192. for root, dirs, files in os.walk('../loadable'):
  193. for name in files:
  194. os.remove(os.path.join(root, name))
  195. temp_list = parse_level_inst_info(['','3'])
  196. dirs = ['../tf_templates', '../miniGame', '../templates']
  197. for temp_name in temp_list:
  198. print(f"Converting template {temp_name}")
  199. # 判断正确的目录
  200. for d in dirs:
  201. if os.path.exists(os.path.join(d, temp_name + '.json')):
  202. conver_json_to_proto(d, temp_name, '../loadable')
  203. break
  204. gen_index_and_data('../loadable')
  205. # 修改levelInstInfo.csv文件: 对于每一行,如果其try1一直到try10的随机数都是100,则一次将其加1
  206. def correct_random_seed():
  207. for f in ['../conf/levelInstInfo.csv', '../conf/levelInstInfo-2.csv']:
  208. lines = None
  209. with open(f, 'r') as file:
  210. lines = file.readlines()
  211. with open(f, 'w') as file:
  212. for line in lines:
  213. if line.startswith('#'):
  214. file.write(line)
  215. continue
  216. fs = line.split(',')
  217. if len(fs) < 12:
  218. file.write(line)
  219. continue
  220. for i in range(2, 12):
  221. if fs[i].split('|')[1] == '100':
  222. fs[i] = fs[i].replace('100', '%d' % (100+i))
  223. file.write(','.join(fs))
  224. # 将关卡实例每个关卡单独保存一个,避免改动的时候相互影响
  225. def split_level_inst_info():
  226. # 首先读取levelInfo.csv文件,对于每一个关卡,解析其所使用的实例名字信息
  227. # 然后读取levelInstInfo.csv文件,得到每一个实例的详细信息
  228. # 新建一个levelInstInfo-new.csv文件,对于每一个关卡,将其所使用的实例的信息改名成inst-关卡名,并存入上一步的详细信息
  229. # 最后将levelInstInfo-new.csv文件保存
  230. lvs_inst = {}
  231. with open('../conf/levelInfo-2.csv', 'r') as file:
  232. for line in file.readlines():
  233. if line.startswith('#'):
  234. continue
  235. fs = line.split(',')
  236. lvs_inst[fs[0]] = (fs[1], fs)
  237. inst_info = {}
  238. with open('../conf/levelInstInfo-2.csv', 'r') as file:
  239. for line in file.readlines():
  240. if line.startswith('#'):
  241. continue
  242. fs = line.split(',')
  243. inst_info[fs[0]] = ",".join(fs[1:])
  244. # 更新info
  245. with open('../conf/levelInfo-2-new.csv', 'w') as file:
  246. for lv, (inst,li) in lvs_inst.items():
  247. li[1] = 'inst-%s' % (lv,)
  248. file.write(",".join(li))
  249. # 写入实例信息
  250. with open('../conf/levelInstInfo-2-new.csv', 'w') as file:
  251. for lv, (inst,li) in lvs_inst.items():
  252. if inst in inst_info:
  253. file.write("inst-%s,%s" % (lv, inst_info[inst]))
  254. else:
  255. print(f"Error: {inst} not found in levelInstInfo.csv")
  256. pass
  257. # 更新关卡实例的随机值
  258. def updateRandomSeed(lvInfoFN, instInfoFN, seedsInfo):
  259. # seedsInfo每一行的格式:[Tile] resovler id: 31,37710|539110|14697|465925|662106|151463|667120|234313|60714|840794|
  260. # 解析seedsInfo,得到关卡和实例的时机值
  261. seeds = {}
  262. for line in seedsInfo.split('\n'):
  263. fs = line.split(' ')
  264. if len(fs) < 4:
  265. continue
  266. fs = fs[3].split(',')
  267. seeds[fs[0]] = fs[1].split('|')
  268. # 先通过lvInfoFN文件,得到所有的关卡对应的实例名称
  269. seeds4Inst = {}
  270. with open(lvInfoFN, 'r') as file:
  271. for line in file.readlines():
  272. if line.startswith('#'):
  273. continue
  274. fs = line.split(',')
  275. if fs[0] in seeds:
  276. seeds4Inst[fs[1]] = seeds[fs[0]]
  277. # 对于instInfoFN文件,对于每一行,如果lvid在seeds里面,则用上面的随机值来更新一下;否则原封不动写入新文件
  278. with open(instInfoFN, 'r') as infile:
  279. lines = infile.readlines()
  280. with open(instInfoFN + '-new', 'w') as outfile:
  281. for line in lines:
  282. fs = line.split(',')
  283. if len(fs) < 2:
  284. outfile.write(line)
  285. continue
  286. if fs[0] in seeds4Inst:
  287. for i in range(2, 12):
  288. ps = fs[i].split('|')
  289. ps[1] = seeds4Inst[fs[0]][i-2]
  290. fs[i] = '|'.join(ps)
  291. outfile.write(','.join(fs))
  292. # 将instInfoFN-new文件重命名为instInfoFN
  293. os.rename(instInfoFN + '-new', instInfoFN)
  294. pass
  295. if __name__ == '__main__':
  296. # 获取当前工作目录
  297. current_dir = os.getcwd()
  298. script_dir = os.path.dirname(os.path.abspath(__file__))
  299. os.chdir(script_dir)
  300. # 得到可以发布的protobuf文件
  301. #convert_all_templates()
  302. # correct_random_seed()
  303. # split_level_inst_info()
  304. # 更新随机信息
  305. seedsInfo = """
  306. [Tile] resovler id: 1,741994|782049|245401|586494|585515|844935|473832|560738|37700|334867|
  307. [Tile] resovler id: 2,673339|884121|575932|529672|871572|33514|756797|556553|383379|845974|
  308. [Tile] resovler id: 3,398075|153640|619852|832136|393503|20714|358987|110175|411774|542299|
  309. [Tile] resovler id: 4,712341|324580|941555|467896|494440|62707|399955|283406|639585|175493|
  310. [Tile] resovler id: 5,280717|189716|80411|853095|705220|546715|147854|523319|77937|314125|
  311. [Tile] resovler id: 6,564790|841391|971530|892729|188546|644199|696504|189275|48872|116772|
  312. [Tile] resovler id: 7,777849|331638|327732|602887|464807|895902|902564|68139|718117|67306|
  313. [Tile] resovler id: 8,987217|760748|96643|112824|570189|213610|462739|286855|562912|629566|
  314. [Tile] resovler id: 9,455598|453585|48738|28577|902432|675019|7499|885812|333875|598762|
  315. [Tile] resovler id: 10,530104|137449|879593|710229|245086|713169|262496|927742|112079|803424|
  316. [Tile] resovler id: 11,665940|847758|689087|49328|524860|728633|378918|855388|947680|598144|
  317. [Tile] resovler id: 12,533285|573613|226767|444329|137922|287942|854151|91001|209449|632312|
  318. [Tile] resovler id: 13,361958|2210|647076|680115|146546|129620|521030|303552|420691|158391|
  319. [Tile] resovler id: 14,77623|200485|352885|240955|190714|505685|436547|704161|832960|792325|
  320. [Tile] resovler id: 15,940941|130963|299015|526270|372169|353500|603281|507977|924894|335899|
  321. [Tile] resovler id: 16,559609|527858|24507|186088|353521|716308|834310|525708|458887|331968|
  322. [Tile] resovler id: 17,676321|843974|928908|264904|289351|857467|546935|328708|298631|374807|
  323. [Tile] resovler id: 18,31296|376484|257141|323041|542832|205748|45980|698444|431287|674208|
  324. [Tile] resovler id: 19,930135|522800|253430|831862|868088|783176|335919|374418|419249|181204|
  325. [Tile] resovler id: 20,455247|769048|409068|208122|185371|510388|503733|971820|846701|354383|
  326. [Tile] resovler id: 21,813311|783923|694902|753811|501860|472671|60359|786797|139673|345116|
  327. [Tile] resovler id: 22,694891|232972|455781|545755|795491|634323|756211|402966|509917|676323|
  328. [Tile] resovler id: 23,520833|681401|763437|851187|605886|786625|702376|516798|540748|90732|
  329. [Tile] resovler id: 24,111958|141292|938262|917249|422143|490826|257325|172344|42726|192422|
  330. [Tile] resovler id: 25,279272|877884|426040|838509|635941|222308|674208|75601|682109|793416|
  331. [Tile] resovler id: 26,655715|808350|41341|497273|297963|451667|617768|825016|222230|954515|
  332. [Tile] resovler id: 27,862461|274898|302007|669673|788609|715031|400599|454925|604808|891875|
  333. [Tile] resovler id: 28,953105|193904|867021|585873|571105|456798|76809|726947|975077|226670|
  334. [Tile] resovler id: 29,943954|495078|521909|778146|642957|513735|753960|51905|194416|232135|
  335. [Tile] resovler id: 30,134069|327536|931813|243629|195985|601044|977382|503294|47141|804275|
  336. [Tile] resovler id: 31,37710|539110|14697|465925|662106|151463|667120|234313|60714|840794|
  337. [Tile] resovler id: 32,593205|634340|958793|573271|383911|340306|242516|634033|898667|543886|
  338. [Tile] resovler id: 33,367610|560109|408542|183872|22081|141640|657756|727536|136080|687632|
  339. [Tile] resovler id: 34,477682|959591|121778|99899|399009|422919|900992|777701|991801|419563|
  340. [Tile] resovler id: 35,622027|417733|853517|554771|327225|935566|147369|680205|421683|612060|
  341. [Tile] resovler id: 36,729062|407063|538743|952108|606011|732614|690807|445646|572511|438099|
  342. [Tile] resovler id: 37,632522|385290|905640|371743|290561|498417|391781|940609|571385|90211|
  343. [Tile] resovler id: 38,170333|571685|978381|332382|305518|598214|16112|474203|690524|205857|
  344. [Tile] resovler id: 39,618601|699482|575303|25315|334114|501904|436188|947954|955591|894621|
  345. [Tile] resovler id: 40,522993|397185|399491|566062|684047|401688|651392|78906|913301|27998|
  346. [Tile] resovler id: 41,967717|701069|289445|724583|199114|723827|491676|237734|505409|286152|
  347. [Tile] resovler id: 42,516555|863942|694354|558588|535895|551956|299334|185801|342688|995820|
  348. [Tile] resovler id: 43,13042|509566|158747|308094|411446|379696|202985|613109|469669|97125|
  349. [Tile] resovler id: 44,473254|794981|962880|331739|253120|791682|876221|358862|295101|319777|
  350. [Tile] resovler id: 45,873172|562727|718642|338860|462551|796609|628745|136923|464748|329762|
  351. [Tile] resovler id: 46,758477|506785|82081|249014|6453|901498|675435|717693|882069|627399|
  352. [Tile] resovler id: 47,832961|254122|353220|126552|188723|965968|145410|742039|327848|62456|
  353. [Tile] resovler id: 48,787090|458228|720595|591408|765640|484171|573153|986987|673973|165149|
  354. [Tile] resovler id: 49,619003|375498|950166|611038|449557|852723|168237|956036|682230|363220|
  355. [Tile] resovler id: 50,978466|675863|444711|133748|598425|852067|799426|993761|249127|514111|
  356. """
  357. updateRandomSeed('../conf/levelInfo-3.csv', '../conf/levelInstInfo-3.csv', seedsInfo)
  358. # 恢复当前工作目录
  359. os.chdir(current_dir)