WorldInstaller.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <iostream>
  16. #include <algorithm>
  17. #include <fstream>
  18. #include <sstream>
  19. #include <tinyxml.h>
  20. #include <OgreRoot.h>
  21. #include <OgreMesh.h>
  22. #include <OgreMeshSerializer.h>
  23. #include <boost/filesystem.hpp>
  24. #include "WorldInstaller.h"
  25. #include "ModelMaterials.h"
  26. #include "TexFile.h"
  27. #include "common/Lzs.h"
  28. #include "common/FinalFantasy7/FF7NameLookup.h"
  29. #include "common/VGearsStringUtil.h"
  30. #include "data/VGearsLGPArchive.h"
  31. #include "data/WorldMapWalkmesh.h"
  32. #include "data/VGearsHRCFileManager.h"
  33. #include "data/VGearsAFileManager.h"
  34. #include "WorldModelTable.h"
  35. #include "data/FF7Data.h"
  36. #include "decompiler/ControlFlow.h"
  37. #include "decompiler/DecompilerException.h"
  38. #include "decompiler/world/WorldEngine.h"
  39. std::string WorldInstaller::ELEMENT_MODELS_DIR("models/world/element");
  40. std::string WorldInstaller::TERRAIN_MODELS_DIR("models/world/terrain");
  41. WorldInstaller::WorldInstaller(
  42. const DiskImage& disk_image, const std::string output_dir,
  43. Ogre::ResourceGroupManager* res_mgr
  44. ): disk_image_(disk_image), output_dir_(output_dir), data_dir_(output_dir + "data/"), res_mgr_(res_mgr) {}
  45. WorldInstaller::~WorldInstaller(){}
  46. unsigned int WorldInstaller::Initialize(){
  47. if (wm_map_.size() > 0) wm_map_.clear();
  48. processed_maps_ = 0;
  49. wm_map_.push_back(File(*disk_image_.filePath("data/wm/WM0.MAP")));
  50. wm_map_.push_back(File(*disk_image_.filePath("data/wm/WM2.MAP")));
  51. wm_map_.push_back(File(*disk_image_.filePath("data/wm/WM3.MAP")));
  52. // The game reads the original map files at runtime (data/wm), so install a copy.
  53. boost::filesystem::create_directories(data_dir_ + "wm/");
  54. for (const char* map : {"WM0.MAP", "WM2.MAP", "WM3.MAP"}){
  55. boost::filesystem::copy_file(
  56. *disk_image_.filePath(std::string("data/wm/") + map), data_dir_ + "wm/" + map,
  57. boost::filesystem::copy_options::overwrite_existing
  58. );
  59. }
  60. return wm_map_.size();
  61. }
  62. void WorldInstaller::GenerateMaterials(){
  63. // First, populate the texture list with static data.
  64. texture_.push_back({0, "pond", 32, 32, 0, 352});
  65. texture_.push_back({1, "riv_m2", 32, 32, 128, 64});
  66. texture_.push_back({2, "was_gs", 64, 64, 64, 192});
  67. texture_.push_back({3, "wpcltr", 32, 128, 0, 256});
  68. texture_.push_back({4, "wpcltr2", 32, 32, 160, 64});
  69. texture_.push_back({5, "bzdun", 64, 64, 192, 192});
  70. texture_.push_back({6, "bone", 32, 32, 224, 384});
  71. texture_.push_back({7, "bone2", 32, 32, 224, 416});
  72. texture_.push_back({8, "bornwd", 64, 64, 160, 320});
  73. texture_.push_back({9, "bridge", 32, 64, 192, 0});
  74. texture_.push_back({10, "bridge2", 32, 32, 224, 0});
  75. texture_.push_back({11, "cave", 32, 32, 224, 448});
  76. texture_.push_back({12, "cave2", 32, 32, 224, 320});
  77. texture_.push_back({13, "cave_s", 32, 32, 160, 224});
  78. texture_.push_back({14, "cdl_cl2", 64, 32, 96, 96});
  79. texture_.push_back({15, "cf01", 64, 32, 192, 288});
  80. texture_.push_back({16, "clf_bgs", 64, 32, 192, 384});
  81. texture_.push_back({17, "clf_ggl", 64, 64, 128, 256});
  82. texture_.push_back({18, "clf_ggs", 64, 32, 192, 352});
  83. texture_.push_back({19, "clf_l", 64, 64, 0, 0});
  84. texture_.push_back({20, "clf_ld", 64, 64, 64, 0});
  85. texture_.push_back({21, "clf_lf", 64, 64, 128, 0});
  86. texture_.push_back({22, "clf_lg", 32, 64, 0, 96});
  87. texture_.push_back({23, "clf_lr", 32, 64, 128, 0});
  88. texture_.push_back({24, "clf_lsg", 32, 64, 64, 64});
  89. texture_.push_back({25, "clf_r", 32, 32, 0, 96});
  90. texture_.push_back({26, "clf_s", 64, 32, 192, 0});
  91. texture_.push_back({27, "clf_sd", 64, 32, 192, 32});
  92. texture_.push_back({28, "clf_sf", 64, 32, 0, 64});
  93. texture_.push_back({29, "clf_sg", 32, 32, 32, 96});
  94. texture_.push_back({30, "clf_sg2", 32, 32, 0, 160});
  95. texture_.push_back({31, "clf_sr", 32, 32, 32, 96});
  96. texture_.push_back({32, "clf_ss", 32, 32, 32, 128});
  97. texture_.push_back({33, "clf_ssd", 32, 32, 0, 224});
  98. texture_.push_back({34, "clf_ssw", 32, 32, 224, 32});
  99. texture_.push_back({35, "clf_sw", 32, 32, 192, 32});
  100. texture_.push_back({36, "clf_w02", 64, 64, 128, 64});
  101. texture_.push_back({37, "clf_w03", 64, 64, 192, 64});
  102. texture_.push_back({38, "clf_was", 64, 32, 64, 64});
  103. texture_.push_back({39, "clfeg", 32, 32, 192, 320});
  104. texture_.push_back({40, "clfegd", 32, 32, 0, 320});
  105. texture_.push_back({41, "clftop", 64, 32, 192, 64});
  106. texture_.push_back({42, "clftop2", 32, 32, 128, 64});
  107. texture_.push_back({43, "cndl_cl", 64, 32, 96, 128});
  108. texture_.push_back({44, "cndlf", 64, 64, 160, 64});
  109. texture_.push_back({45, "cndlf02", 64, 64, 208, 64});
  110. texture_.push_back({46, "comtr", 16, 32, 144, 96});
  111. texture_.push_back({47, "cosinn", 32, 32, 224, 416});
  112. texture_.push_back({48, "cosinn2", 32, 32, 192, 448});
  113. texture_.push_back({49, "csmk", 32, 32, 64, 64});
  114. texture_.push_back({50, "csmk2", 32, 32, 96, 64});
  115. texture_.push_back({51, "cstds01", 32, 32, 224, 160});
  116. texture_.push_back({52, "cstds02", 64, 64, 0, 448});
  117. texture_.push_back({53, "des01", 32, 32, 160, 320});
  118. texture_.push_back({54, "desert", 64, 64, 128, 128});
  119. texture_.push_back({55, "desor", 64, 32, 160, 64});
  120. texture_.push_back({56, "ds1", 32, 32, 0, 256});
  121. texture_.push_back({57, "ds_s1", 32, 32, 192, 288});
  122. texture_.push_back({58, "dsee1", 32, 32, 96, 288});
  123. texture_.push_back({59, "dsrt_d", 32, 32, 224, 0});
  124. texture_.push_back({60, "dsrt_e", 64, 128, 64, 128});
  125. texture_.push_back({61, "edes01", 32, 32, 224, 320});
  126. texture_.push_back({62, "elm01", 32, 32, 160, 0});
  127. texture_.push_back({63, "elm02", 32, 32, 64, 96});
  128. texture_.push_back({64, "elm_gro", 64, 64, 0, 96});
  129. texture_.push_back({65, "elm_r", 32, 32, 192, 0});
  130. texture_.push_back({66, "elm_r2", 32, 32, 224, 0});
  131. texture_.push_back({67, "fall1", 32, 32, 128, 256});
  132. texture_.push_back({68, "farm01", 32, 32, 160, 32});
  133. texture_.push_back({69, "farm02", 32, 32, 192, 32});
  134. texture_.push_back({70, "farm_g", 32, 32, 128, 64});
  135. texture_.push_back({71, "farm_r", 32, 16, 128, 48});
  136. texture_.push_back({72, "fld", 64, 64, 64, 96});
  137. texture_.push_back({73, "fld_02", 64, 64, 0, 64});
  138. texture_.push_back({74, "fld_s", 64, 64, 0, 160});
  139. texture_.push_back({75, "fld_s2", 32, 32, 224, 256});
  140. texture_.push_back({76, "fld_sw", 64, 64, 128, 192});
  141. texture_.push_back({77, "fld_v", 128, 128, 0, 128});
  142. texture_.push_back({78, "fld_vd", 32, 64, 96, 128});
  143. texture_.push_back({79, "fld_vd2", 32, 64, 192, 416});
  144. texture_.push_back({80, "fvedge", 32, 64, 0, 0});
  145. texture_.push_back({81, "gclf_d", 128, 64, 128, 128});
  146. texture_.push_back({82, "gclf_g", 32, 64, 224, 128});
  147. texture_.push_back({83, "gclfwa", 128, 64, 64, 320});
  148. texture_.push_back({84, "gclfwa2", 32, 64, 160, 320});
  149. texture_.push_back({85, "gclfwag", 32, 64, 32, 320});
  150. texture_.push_back({86, "gg_gro", 64, 64, 64, 448});
  151. texture_.push_back({87, "gg_mts", 64, 128, 0, 128});
  152. texture_.push_back({88, "ggmk", 64, 64, 128, 448});
  153. texture_.push_back({89, "ggmt", 128, 128, 0, 0});
  154. texture_.push_back({90, "ggmt_e", 128, 32, 0, 96});
  155. texture_.push_back({91, "ggmt_ed", 128, 32, 128, 96});
  156. texture_.push_back({92, "ggmt_eg", 32, 32, 96, 224});
  157. texture_.push_back({93, "ggmtd", 128, 128, 128, 0});
  158. texture_.push_back({94, "ggs_g", 32, 32, 32, 64});
  159. texture_.push_back({95, "ggshr", 32, 32, 192, 96});
  160. texture_.push_back({96, "ggshrg", 32, 32, 224, 96});
  161. texture_.push_back({97, "gia", 64, 32, 64, 224});
  162. texture_.push_back({98, "gia2", 64, 32, 0, 224});
  163. texture_.push_back({99, "gia_d", 64, 32, 128, 224});
  164. texture_.push_back({100, "gia_d2", 64, 32, 64, 224});
  165. texture_.push_back({101, "gia_g", 32, 32, 192, 224});
  166. texture_.push_back({102, "gia_g2", 32, 32, 128, 224});
  167. texture_.push_back({103, "gmt_eda", 32, 32, 0, 352});
  168. texture_.push_back({104, "gonclf", 128, 64, 96, 64});
  169. texture_.push_back({105, "gredge", 32, 32, 192, 192});
  170. texture_.push_back({106, "hyouga", 64, 64, 192, 64});
  171. texture_.push_back({107, "iceclf", 64, 32, 64, 96});
  172. texture_.push_back({108, "iceclfd", 64, 32, 128, 96});
  173. texture_.push_back({109, "iceclfg", 32, 32, 32, 224});
  174. texture_.push_back({110, "jun", 64, 64, 192, 192});
  175. texture_.push_back({111, "jun_d", 64, 64, 128, 192});
  176. texture_.push_back({112, "jun_e", 64, 16, 0, 240});
  177. texture_.push_back({113, "jun_gro", 64, 64, 0, 64});
  178. texture_.push_back({114, "junmk", 32, 32, 0, 96});
  179. texture_.push_back({115, "junn01", 32, 32, 160, 112});
  180. texture_.push_back({116, "junn02", 32, 32, 192, 112});
  181. texture_.push_back({117, "junn03", 32, 32, 224, 112});
  182. texture_.push_back({118, "junn04", 32, 32, 64, 128});
  183. texture_.push_back({119, "jutmpl01", 64, 64, 128, 192});
  184. texture_.push_back({120, "lake-e", 32, 32, 96, 192});
  185. texture_.push_back({121, "lake_ef", 32, 32, 128, 224});
  186. texture_.push_back({122, "lake_fl", 128, 32, 160, 224});
  187. texture_.push_back({123, "lostclf", 32, 64, 128, 384});
  188. texture_.push_back({124, "lostmt", 128, 32, 128, 448});
  189. texture_.push_back({125, "lostmtd", 128, 32, 128, 480});
  190. texture_.push_back({126, "lostmts", 64, 32, 160, 384});
  191. texture_.push_back({127, "lostwd_e", 32, 32, 64, 480});
  192. texture_.push_back({128, "lostwod", 64, 64, 0, 448});
  193. texture_.push_back({129, "lst1", 32, 32, 192, 256});
  194. texture_.push_back({130, "lstwd_e2", 32, 32, 96, 480});
  195. texture_.push_back({131, "mzes", 32, 32, 224, 128});
  196. texture_.push_back({132, "mzmt_e", 128, 64, 128, 64});
  197. texture_.push_back({133, "mzmt_ed", 128, 32, 128, 128});
  198. texture_.push_back({134, "mzmt_edw", 128, 32, 128, 160});
  199. texture_.push_back({135, "mzmt_ew", 128, 32, 0, 128});
  200. texture_.push_back({136, "mzmt_o", 128, 32, 64, 416});
  201. texture_.push_back({137, "mzmt_od", 128, 32, 64, 448});
  202. texture_.push_back({138, "mzmt_s", 128, 32, 0, 192});
  203. texture_.push_back({139, "mzmt_sd", 128, 32, 0, 160});
  204. texture_.push_back({140, "md01", 32, 32, 96, 16});
  205. texture_.push_back({141, "md02", 128, 128, 0, 0});
  206. texture_.push_back({142, "md03", 16, 16, 112, 64});
  207. texture_.push_back({143, "md04", 32, 32, 128, 16});
  208. texture_.push_back({144, "md05", 64, 16, 96, 0});
  209. texture_.push_back({145, "md06", 16, 32, 96, 48});
  210. texture_.push_back({146, "md07", 16, 16, 112, 48});
  211. texture_.push_back({147, "md_mt", 128, 128, 128, 0});
  212. texture_.push_back({148, "md_mtd", 128, 128, 0, 0});
  213. texture_.push_back({149, "md_mts", 64, 128, 64, 160});
  214. texture_.push_back({150, "md_snow", 128, 32, 128, 0});
  215. texture_.push_back({151, "md_snw2", 128, 32, 128, 32});
  216. texture_.push_back({152, "md_snwd", 128, 64, 0, 128});
  217. texture_.push_back({153, "md_snwe", 64, 64, 96, 320});
  218. texture_.push_back({154, "md_snws", 64, 64, 128, 128});
  219. texture_.push_back({155, "md_snwt", 128, 32, 0, 192});
  220. texture_.push_back({156, "md_snww", 32, 32, 224, 224});
  221. texture_.push_back({157, "md_sw_s", 128, 128, 0, 0});
  222. texture_.push_back({158, "md_swd2", 32, 32, 192, 256});
  223. texture_.push_back({159, "md_swnp", 128, 128, 0, 96});
  224. texture_.push_back({160, "mdsrt_e", 128, 32, 128, 192});
  225. texture_.push_back({161, "mdsrt_ed", 128, 32, 128, 224});
  226. texture_.push_back({162, "mdsrt_eg", 32, 32, 64, 224});
  227. texture_.push_back({163, "midil", 32, 32, 32, 192});
  228. texture_.push_back({164, "midild", 32, 32, 224, 192});
  229. texture_.push_back({165, "mt_ewg", 32, 32, 64, 96});
  230. texture_.push_back({166, "mt_road", 64, 64, 192, 128});
  231. texture_.push_back({167, "mt_se", 32, 32, 160, 416});
  232. texture_.push_back({168, "mt_se2", 64, 64, 128, 256});
  233. texture_.push_back({169, "mt_sg01", 32, 32, 0, 224});
  234. texture_.push_back({170, "mt_sg02", 32, 32, 32, 224});
  235. texture_.push_back({171, "mt_sg03", 32, 32, 0, 192});
  236. texture_.push_back({172, "mt_sg04", 32, 32, 160, 384});
  237. texture_.push_back({173, "mtcoin", 64, 64, 0, 256});
  238. texture_.push_back({174, "mtwas_e", 128, 32, 0, 224});
  239. texture_.push_back({175, "mtwas_ed", 128, 32, 0, 224});
  240. texture_.push_back({176, "ncol_gro", 64, 64, 64, 384});
  241. texture_.push_back({177, "ncole01", 32, 32, 224, 384});
  242. texture_.push_back({178, "ncole02", 32, 32, 192, 416});
  243. texture_.push_back({179, "nivl_gro", 64, 64, 128, 384});
  244. texture_.push_back({180, "nivl_mt", 128, 64, 0, 0});
  245. texture_.push_back({181, "nivl_top", 32, 32, 0, 64});
  246. texture_.push_back({182, "nivlr", 32, 32, 192, 384});
  247. texture_.push_back({183, "port", 32, 32, 96, 224});
  248. texture_.push_back({184, "port_d", 32, 32, 160, 0});
  249. texture_.push_back({185, "rzclf02", 64, 64, 128, 128});
  250. texture_.push_back({186, "rct_gro", 64, 128, 0, 416});
  251. texture_.push_back({187, "riv_cls", 64, 64, 64, 0});
  252. texture_.push_back({188, "riv_l1", 32, 32, 96, 320});
  253. texture_.push_back({189, "riv_m", 32, 32, 0, 224});
  254. texture_.push_back({190, "rivr", 32, 32, 64, 224});
  255. texture_.push_back({191, "rivrclf", 64, 64, 128, 192});
  256. texture_.push_back({192, "rivs1", 32, 32, 128, 320});
  257. texture_.push_back({193, "rivshr", 64, 64, 192, 192});
  258. texture_.push_back({194, "rivssr", 64, 32, 192, 224});
  259. texture_.push_back({195, "rivstrt", 32, 32, 192, 160});
  260. texture_.push_back({196, "rm1", 32, 32, 32, 288});
  261. texture_.push_back({197, "rocet", 32, 32, 128, 160});
  262. texture_.push_back({198, "rs_ss", 32, 32, 96, 224});
  263. texture_.push_back({199, "sango", 32, 32, 224, 320});
  264. texture_.push_back({200, "sango2", 32, 32, 224, 352});
  265. texture_.push_back({201, "sango3", 32, 32, 128, 384});
  266. texture_.push_back({202, "sango4", 64, 64, 0, 384});
  267. texture_.push_back({203, "sdun", 64, 64, 0, 160});
  268. texture_.push_back({204, "sdun02", 64, 64, 64, 160});
  269. texture_.push_back({205, "sh1", 32, 32, 32, 256});
  270. texture_.push_back({206, "sh_s1", 32, 32, 224, 288});
  271. texture_.push_back({207, "shedge", 32, 64, 160, 160});
  272. texture_.push_back({208, "shlm_1", 32, 32, 192, 320});
  273. texture_.push_back({209, "shol", 128, 128, 128, 96});
  274. texture_.push_back({210, "shol_s", 64, 64, 192, 192});
  275. texture_.push_back({211, "shor", 128, 128, 0, 0});
  276. texture_.push_back({212, "shor_s", 64, 64, 128, 192});
  277. texture_.push_back({213, "shor_s2", 32, 32, 224, 416});
  278. texture_.push_back({214, "shor_v", 32, 32, 192, 0});
  279. texture_.push_back({215, "silo", 32, 32, 224, 32});
  280. texture_.push_back({216, "slope", 128, 32, 0, 384});
  281. texture_.push_back({217, "snow_es", 32, 32, 192, 480});
  282. texture_.push_back({218, "snow_es2", 32, 32, 224, 480});
  283. texture_.push_back({219, "snow_es3", 32, 32, 224, 448});
  284. texture_.push_back({220, "snw_mt", 128, 128, 0, 0});
  285. texture_.push_back({221, "snw_mtd", 128, 128, 128, 0});
  286. texture_.push_back({222, "snw_mte", 64, 32, 0, 96});
  287. texture_.push_back({223, "snw_mted", 64, 32, 64, 96});
  288. texture_.push_back({224, "snw_mts", 64, 128, 64, 0});
  289. texture_.push_back({225, "snw_mts2", 64, 32, 128, 192});
  290. texture_.push_back({226, "snwfld", 64, 64, 0, 64});
  291. texture_.push_back({227, "snwfld_s", 64, 32, 128, 128});
  292. texture_.push_back({228, "snwgra", 64, 64, 192, 192});
  293. texture_.push_back({229, "snwhm01", 32, 32, 32, 0});
  294. texture_.push_back({230, "snwhm02", 32, 32, 32, 32});
  295. texture_.push_back({231, "snwods", 32, 32, 224, 192});
  296. texture_.push_back({232, "snwood", 64, 64, 192, 128});
  297. texture_.push_back({233, "snwtrk", 32, 64, 96, 256});
  298. texture_.push_back({234, "sse_s1", 32, 32, 32, 320});
  299. texture_.push_back({235, "ssee1", 32, 32, 64, 288});
  300. texture_.push_back({236, "sst1", 32, 32, 224, 256});
  301. texture_.push_back({237, "stown_r", 32, 32, 192, 256});
  302. texture_.push_back({238, "stw_gro", 64, 64, 0, 384});
  303. texture_.push_back({239, "subrg2", 32, 32, 224, 160});
  304. texture_.push_back({240, "susbrg", 64, 64, 192, 96});
  305. texture_.push_back({241, "sw_se", 64, 64, 0, 0});
  306. texture_.push_back({242, "swclf_l", 64, 64, 64, 128});
  307. texture_.push_back({243, "swclf_ld", 64, 64, 192, 128});
  308. texture_.push_back({244, "swclf_lg", 32, 64, 0, 192});
  309. texture_.push_back({245, "swclf_s", 64, 32, 128, 96});
  310. texture_.push_back({246, "swclf_sd", 64, 32, 192, 96});
  311. texture_.push_back({247, "swclf_sg", 32, 32, 32, 192});
  312. texture_.push_back({248, "swclf_wg", 32, 32, 192, 192});
  313. texture_.push_back({249, "swfld_s2", 64, 32, 128, 160});
  314. texture_.push_back({250, "swfld_s3", 32, 32, 160, 192});
  315. texture_.push_back({251, "swmd_cg", 32, 32, 128, 192});
  316. texture_.push_back({252, "swmd_clf", 64, 32, 64, 192});
  317. texture_.push_back({253, "swp1", 32, 32, 0, 288});
  318. texture_.push_back({254, "trk", 64, 64, 128, 0});
  319. texture_.push_back({255, "tyo_f", 128, 128, 128, 128});
  320. texture_.push_back({256, "tyosnw", 64, 128, 64, 384});
  321. texture_.push_back({257, "uf1", 32, 32, 160, 256});
  322. texture_.push_back({258, "utai01", 32, 32, 32, 96});
  323. texture_.push_back({259, "utai02", 32, 32, 224, 64});
  324. texture_.push_back({260, "utai_gro", 64, 64, 128, 96});
  325. texture_.push_back({261, "utaimt", 32, 32, 0, 128});
  326. texture_.push_back({262, "utaimtd", 32, 32, 96, 96});
  327. texture_.push_back({263, "utaimtg", 32, 32, 96, 128});
  328. texture_.push_back({264, "wa1", 32, 32, 192, 320});
  329. texture_.push_back({265, "wzs1", 32, 32, 128, 288});
  330. texture_.push_back({266, "wzshr", 32, 32, 160, 32});
  331. texture_.push_back({267, "wzshr2", 32, 32, 32, 128});
  332. texture_.push_back({268, "wzshrs", 32, 32, 32, 160});
  333. texture_.push_back({269, "was", 128, 128, 0, 96});
  334. texture_.push_back({270, "was_d", 64, 32, 0, 224});
  335. texture_.push_back({271, "was_g", 64, 64, 0, 192});
  336. texture_.push_back({272, "was_s", 128, 128, 128, 0});
  337. texture_.push_back({273, "wasfld", 64, 64, 64, 256});
  338. texture_.push_back({274, "wdedge", 64, 64, 64, 160});
  339. texture_.push_back({275, "we1", 32, 32, 96, 256});
  340. texture_.push_back({276, "we_s1", 32, 32, 160, 288});
  341. texture_.push_back({277, "wedged", 32, 64, 128, 160});
  342. texture_.push_back({278, "wod-e2", 32, 32, 64, 224});
  343. texture_.push_back({279, "wood", 64, 64, 192, 0});
  344. texture_.push_back({280, "wood_d", 64, 64, 192, 160});
  345. texture_.push_back({281, "wtrk", 32, 64, 64, 96});
  346. Ogre::MaterialSerializer mSer;
  347. for (Texture t : texture_){
  348. Ogre::MaterialPtr mat = Ogre::MaterialManager::getSingleton().create(t.name, "General", true);
  349. Ogre::Technique* tech = mat->getTechnique(0);
  350. Ogre::Pass* pass = tech->getPass(0);
  351. pass->setDiffuse(0.7, 0.7, 0.7, 0.7);
  352. mSer.exportMaterial(mat, data_dir_ + "models/world/" + t.name + ".material");
  353. }
  354. }
  355. void WorldInstaller::ProcessModels(){
  356. // Open world_us.lgp
  357. File world_file(*disk_image_.filePath("data/wm/world_us.lgp"));
  358. boost::filesystem::create_directories(output_dir_ + "temp/world_models/");
  359. boost::filesystem::create_directories(data_dir_ + "models/world/");
  360. VGears::LGPArchive world_lgp(*disk_image_.filePath("data/wm/world_us.lgp"), "LGP");
  361. world_lgp.open(*disk_image_.filePath("data/wm/world_us.lgp"), true);
  362. world_lgp.load();
  363. VGears::LGPArchive::FileList file_list = world_lgp.GetFiles();
  364. std::vector<std::string> hrc_file_names;
  365. // First pass: extract all files so HRC sidecar dependencies (.rsd, .p, .tex) exist.
  366. for (int i = 0; i < file_list.size(); i ++){
  367. VGears::LGPArchive::FileEntry f = file_list.at(i);
  368. if (f.data_offset + f.data_size <= world_file.GetFileSize()){
  369. File w_lgp_file(&world_file, f.data_offset, f.data_size);
  370. std::string file_name = f.file_name;
  371. VGears::StringUtil::toLowerCase(file_name);
  372. w_lgp_file.WriteFile(output_dir_ + "temp/world_models/" + file_name);
  373. if (file_name.size() >= 7 && file_name.substr(4, 3) == "hrc") hrc_file_names.push_back(file_name);
  374. }
  375. }
  376. // Second pass: convert HRC models once all dependencies are available on disk.
  377. for (int i = 0; i < hrc_file_names.size(); i ++){
  378. const std::string& file_name = hrc_file_names.at(i);
  379. // TODO: Can this be done with declare resource?
  380. // TODO: If not, do aloop to save all the hrc files, then call this once, then
  381. // another loop to process them.
  382. res_mgr_->declareResource(output_dir_ + "temp/world_models/" + file_name, "Skeleton", "FFVII");
  383. res_mgr_->removeResourceLocation(output_dir_ + "temp/world_models/", "FFVII");
  384. res_mgr_->addResourceLocation(output_dir_ + "temp/world_models/", "FileSystem", "FFVII", true, true);
  385. try{
  386. // TODO: This needs work to assemble all the pieces. Similar to field models.
  387. Ogre::ResourcePtr hrc = VGears::HRCFileManager::GetSingleton().load(file_name, "FFVII");
  388. Ogre::String hrc_base_name;
  389. VGears::StringUtil::splitBase(file_name, hrc_base_name);
  390. auto mesh_name = VGears::NameLookup::model(hrc_base_name) + ".mesh";
  391. Ogre::MeshPtr mesh(Ogre::MeshManager::getSingleton().load(mesh_name, "FFVII"));
  392. Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
  393. // The animations of the model, from the tables of ff7.exe (see WorldModelTable.h). Their names
  394. // are the ones of the A files, in lower case.
  395. for (const WorldModelTable::Model& model : WorldModelTable::MODELS){
  396. if (hrc_base_name != model.hrc) continue;
  397. for (unsigned int i = 0; i < model.animation_count; ++ i){
  398. const std::string animation_file = std::string(model.animations[i]) + ".a";
  399. try{
  400. VGears::AFilePtr a = VGears::AFileManager::GetSingleton().load(
  401. animation_file, "FFVII"
  402. ).staticCast<VGears::AFile>();
  403. a->AddTo(skeleton, model.animations[i]);
  404. }
  405. catch (const std::exception& ex){
  406. std::cerr << "[ERROR] Exception converting animation " << animation_file << " of world model "
  407. << file_name << ": " << ex.what() << std::endl;
  408. }
  409. }
  410. break;
  411. }
  412. ExportMesh(data_dir_ + "models/world/", mesh);
  413. // Models can share a skeleton name with different bones (also with field models), so drop this one
  414. // (with its skeleton and mesh) before converting the next.
  415. VGears::HRCFileManager::GetSingleton().remove(file_name, "FFVII");
  416. }
  417. catch (const std::exception& ex){
  418. std::cerr << "[ERROR] Exception converting world model " << file_name << ": " << ex.what() << std::endl;
  419. if (VGears::HRCFileManager::GetSingleton().resourceExists(file_name, "FFVII"))
  420. VGears::HRCFileManager::GetSingleton().remove(file_name, "FFVII");
  421. }
  422. }
  423. }
  424. bool WorldInstaller::ProcessMap(){
  425. if (processed_maps_ >= wm_map_.size()) return true;
  426. // Initialize the xml file.
  427. TiXmlDocument doc;
  428. std::unique_ptr<TiXmlElement> xml(new TiXmlElement("world_map"));
  429. // Script.
  430. std::unique_ptr<TiXmlElement> xml_script_element(new TiXmlElement("script"));
  431. xml_script_element->SetAttribute("file_name", std::to_string(processed_maps_) + "/script.lua");
  432. xml->LinkEndChild(xml_script_element.release());
  433. // Background.
  434. std::unique_ptr<TiXmlElement> xml_background_2d(new TiXmlElement("background2d"));
  435. xml_background_2d->SetAttribute("file_name", std::to_string(processed_maps_) + "/bg.xml");
  436. xml->LinkEndChild(xml_background_2d.release());
  437. // Texts.
  438. std::unique_ptr<TiXmlElement> xml_texts(new TiXmlElement("texts"));
  439. xml_texts->SetAttribute("file_name", std::to_string(processed_maps_) + "/text.xml");
  440. xml->LinkEndChild(xml_texts.release());
  441. // Walkmesh.
  442. std::unique_ptr<TiXmlElement> xml_walkmesh(new TiXmlElement("walkmesh"));
  443. xml_walkmesh->SetAttribute("file_name", std::to_string(processed_maps_) + "/wm.xml");
  444. xml->LinkEndChild(xml_walkmesh.release());
  445. // Terrain (don't close yet).
  446. std::unique_ptr<TiXmlElement> xml_terrain(new TiXmlElement("terrain"));
  447. // Initialize the walkmesh data.
  448. WorldMapWalkmesh walkmesh(processed_maps_, processed_maps_ == 0 ? true : false);
  449. // Read WM*.MAP file.
  450. Map map;
  451. Ogre::MeshSerializer mesh_serializer;
  452. boost::filesystem::path p(wm_map_[processed_maps_].GetFileName());
  453. std::string map_name = p.stem().native(); // w/o path or extension.
  454. for (int b = 0; b < wm_map_[processed_maps_].GetFileSize() / 0xB800; b ++){
  455. // Calculate offsets for walkmesh.
  456. // WM0 is 9x7 blocks, WM2 probably is too, WM3 no idea?
  457. int wm_offset_x = 32 * (b % 9);
  458. int wm_offset_y = 32 * (b / 9);
  459. Block block;
  460. for (int m = 0; m < 16; m ++){
  461. // Extract lzss compressed mesh to file.
  462. std::vector<unsigned char> lzss;
  463. wm_map_[processed_maps_].SetOffset(0xB800 * b + m * 4);
  464. u32 mesh_offset = 0xB800 * b + wm_map_[processed_maps_].readU32LE();
  465. wm_map_[processed_maps_].SetOffset(mesh_offset);
  466. u32 mesh_size = wm_map_[processed_maps_].readU32LE();
  467. // Add size to lzss
  468. wm_map_[processed_maps_].SetOffset(mesh_offset);
  469. for (int d = 0; d < 4; d ++) lzss.push_back(wm_map_[processed_maps_].readU8());
  470. for (int d = 0; d < mesh_size ; d ++) lzss.push_back(wm_map_[processed_maps_].readU8());
  471. // Extract the lzss file.
  472. std::vector<unsigned char> data = Lzs::Decompress(lzss);
  473. if (data.size() == 0) continue;
  474. std::string dat_name = output_dir_ + "temp/wm/" + map_name + "_"
  475. + std::to_string(b) + "_" + std::to_string(m) + ".dat";
  476. std::ofstream dat(dat_name, std::ios::out | std::ios::binary);
  477. if (!dat){
  478. std::cerr << "Cannot create temporary world map file " << dat_name << std::endl;
  479. processed_maps_ ++;
  480. if (processed_maps_ >= wm_map_.size()) return true;
  481. else return false;
  482. }
  483. for (int d = 0; d < data.size(); d ++) dat.write((char *) &data[d], sizeof(unsigned char));
  484. dat.close();
  485. // Open and read the decompressed file.
  486. File mesh_data(dat_name);
  487. block.mesh[m].triangle_count = mesh_data.readU16LE();
  488. block.mesh[m].vertex_count = mesh_data.readU16LE();
  489. for (int tri = 0; tri < block.mesh[m].triangle_count; tri ++){
  490. Triangle t;
  491. t.vertex_index[0] = mesh_data.readU8();
  492. t.vertex_index[1] = mesh_data.readU8();
  493. t.vertex_index[2] = mesh_data.readU8();
  494. u8 walkability_function = mesh_data.readU8();
  495. //t.walkability = walkability_function >> 5; // 5 bits
  496. //t.function_id = walkability_function & 0x7; // 3 bits
  497. t.walkability = walkability_function & 0x1F; // Lowest 5 bits
  498. t.function_id = walkability_function >> 3; // 3 bits
  499. // FIXME: I think function_id is wrong. It must be 3 bits
  500. t.vertex_coord[0].u = mesh_data.readU8();
  501. t.vertex_coord[0].v = mesh_data.readU8();
  502. t.vertex_coord[1].u = mesh_data.readU8();
  503. t.vertex_coord[1].v = mesh_data.readU8();
  504. t.vertex_coord[2].u = mesh_data.readU8();
  505. t.vertex_coord[2].v = mesh_data.readU8();
  506. u16 texture_location = mesh_data.readU16LE();
  507. t.texture = texture_location >> 9; // 9 bytes
  508. t.location = texture_location & ((1 << 7) - 1); // 7 bytes.
  509. block.mesh[m].triangles.push_back(t);
  510. }
  511. for (int ver = 0; ver < block.mesh[m].vertex_count; ver ++){
  512. Vertex v;
  513. v.x = mesh_data.readU16LE();
  514. v.z = mesh_data.readU16LE();
  515. v.y = mesh_data.readU16LE();
  516. v.unknown = mesh_data.readU16LE();
  517. block.mesh[m].vertices.push_back(v);
  518. }
  519. for (int nor = 0; nor < block.mesh[m].vertex_count; nor ++){
  520. Vertex v;
  521. v.x = mesh_data.readU16LE();
  522. v.z = mesh_data.readU16LE();
  523. v.y = mesh_data.readU16LE();
  524. v.unknown = mesh_data.readU16LE();
  525. block.mesh[m].normals.push_back(v);
  526. }
  527. // Add data to the walkmesh
  528. for (int tri = 0; tri < block.mesh[m].triangle_count; tri ++){
  529. Triangle t = block.mesh[m].triangles.at(tri);
  530. walkmesh.addTriangle(
  531. Ogre::Vector3(
  532. block.mesh[m].vertices.at(t.vertex_index[0]).x + wm_offset_x,
  533. block.mesh[m].vertices.at(t.vertex_index[0]).y + wm_offset_y,
  534. block.mesh[m].vertices.at(t.vertex_index[0]).z
  535. ), // a
  536. Ogre::Vector3(
  537. block.mesh[m].vertices.at(t.vertex_index[1]).x + wm_offset_x,
  538. block.mesh[m].vertices.at(t.vertex_index[1]).y + wm_offset_y,
  539. block.mesh[m].vertices.at(t.vertex_index[1]).z
  540. ), // b
  541. Ogre::Vector3(
  542. block.mesh[m].vertices.at(t.vertex_index[2]).x + wm_offset_x,
  543. block.mesh[m].vertices.at(t.vertex_index[2]).y + wm_offset_y,
  544. block.mesh[m].vertices.at(t.vertex_index[2]).z
  545. ), // c
  546. t.walkability,
  547. t.function_id
  548. );
  549. }
  550. }
  551. map.blocks.push_back(block);
  552. }
  553. // TODO: Generate material.
  554. // Generate manual meshes.
  555. for (int b = 0; b < map.blocks.size(); b ++){
  556. int wm_offset_x = 32 * (b % 9);
  557. int wm_offset_y = 32 * (b / 9);
  558. //std::cout << "[MAN] Block " << b << "/" << map.blocks.size() << std::endl;
  559. std::string name = map_name + "_" + std::to_string(b);
  560. std::string mat_name = texture_.at(0).name;
  561. Ogre::ManualObject man = Ogre::ManualObject(name);
  562. //man.setBoundingBox(Ogre::AxisAlignedBox({-100,-100,0}, {100,100,0})); //TODO;
  563. //= Ogre::Root::getSingleton().getSceneManager("Scene")->createManualObject(name);
  564. man.begin(mat_name, Ogre::RenderOperation::OT_TRIANGLE_LIST);
  565. for (int m = 0; m < 16; m ++){
  566. //std::cout << "[MAN] Mesh " << m << " triangles: "
  567. // << map.blocks[b].mesh[m].triangle_count << "\n";
  568. // Add vertices.
  569. for (int v = 0; v < map.blocks[b].mesh[m].vertex_count; v ++){
  570. man.position(
  571. map.blocks[b].mesh[m].vertices[v].x, map.blocks[b].mesh[m].vertices[v].y,
  572. map.blocks[b].mesh[m].vertices[v].z
  573. );
  574. man.normal(
  575. map.blocks[b].mesh[m].normals[v].x, map.blocks[b].mesh[m].normals[v].y,
  576. map.blocks[b].mesh[m].normals[v].z
  577. );
  578. // TODO: How to add texture coordinates at this point?
  579. //man->textureCoord(0, 0);
  580. }
  581. // Conform faces from vertices
  582. for (int t = 0; t < map.blocks[b].mesh[m].triangle_count; t ++){
  583. //std::cout << "Triangle " << t << " vertizes: "
  584. // << (int) map.blocks[b].mesh[m].triangles[t].vertex_index[0] << ", "
  585. // << (int) map.blocks[b].mesh[m].triangles[t].vertex_index[1] << ", "
  586. // << (int) map.blocks[b].mesh[m].triangles[t].vertex_index[2] << std::endl;
  587. // TODO: El index deberia incrementar por triangulo por cada material
  588. man.setMaterialName(0, texture_.at(map.blocks[b].mesh[m].triangles[t].texture).name);
  589. man.triangle(
  590. map.blocks[b].mesh[m].triangles[t].vertex_index[0],
  591. map.blocks[b].mesh[m].triangles[t].vertex_index[1],
  592. map.blocks[b].mesh[m].triangles[t].vertex_index[2]
  593. );
  594. }
  595. }
  596. // Export the mesh.
  597. man.end();
  598. Ogre::MeshPtr mesh = man.convertToMesh(name);
  599. //std::cout << "[WI] Export mesh " << (data_dir_ + "wm/" + mesh->getName() + ".mesh")
  600. // << std::endl;
  601. mesh_serializer.exportMesh(
  602. mesh.getPointer(),
  603. data_dir_ + TERRAIN_MODELS_DIR + "/" + std::to_string(processed_maps_) + "/" + mesh->getName() + ".mesh"
  604. );
  605. // WM0 is te main worldmap, its shape changes according to the history progress, it has
  606. // 68 blocks but only 63 are used at a time
  607. if (processed_maps_ > 0 || b < 63){
  608. std::unique_ptr<TiXmlElement> xml_block(new TiXmlElement("block"));
  609. xml_block->SetAttribute("index", std::to_string(b));
  610. xml_block->SetAttribute(
  611. "file_name", "terrain/" + std::to_string(processed_maps_) + "/" + mesh->getName() + ".mesh"
  612. );
  613. xml_block->SetAttribute("position", std::to_string(wm_offset_x) + " " + std::to_string(wm_offset_y) + " 0");
  614. if (processed_maps_ == 0){
  615. // TODO: Correct history points for each block.
  616. switch (b){
  617. case 50: // Block 50 replaced by block 63
  618. xml_block->SetAttribute("alt_file_name", "terrain/0/WM0_63.mesh");
  619. xml_block->SetAttribute("alt_story_change", "1000");
  620. break;
  621. case 41: // Block 41 replaced by block 64
  622. xml_block->SetAttribute("alt_file_name", "terrain/0/WM0_64.mesh");
  623. xml_block->SetAttribute("alt_story_change", "1000");
  624. break;
  625. case 42: // Block 42 replaced by block 65
  626. xml_block->SetAttribute("alt_file_name", "terrain/0/WM0_65.mesh");
  627. xml_block->SetAttribute("alt_story_change", "1000");
  628. break;
  629. case 60: // Block 60 replaced by block 66
  630. xml_block->SetAttribute("alt_file_name", "terrain/0/WM0_66.mesh");
  631. xml_block->SetAttribute("alt_story_change", "1000");
  632. break;
  633. case 47: // Block 50 replaced by block 67
  634. xml_block->SetAttribute("alt_file_name", "terrain/0/WM0_67.mesh");
  635. xml_block->SetAttribute("alt_story_change", "1000");
  636. break;
  637. case 48: // Block 50 replaced by block 68
  638. xml_block->SetAttribute("alt_file_name", "terrain/0/WM0_68.mesh");
  639. xml_block->SetAttribute("alt_story_change", "1000");
  640. break;
  641. }
  642. }
  643. xml_terrain->LinkEndChild(xml_block.release());
  644. }
  645. }
  646. // Add other data to the XML file.
  647. // Close and save XML file,
  648. xml->LinkEndChild(xml_terrain.release());
  649. doc.LinkEndChild(xml.release());
  650. // TODO: Add models, entities...
  651. doc.SaveFile(
  652. data_dir_ + "/world/" + std::to_string(processed_maps_) + "/world" + std::to_string(processed_maps_) + ".xml"
  653. );
  654. // Generate the walkmesh xml file
  655. walkmesh.generate(data_dir_ + "/world/" + std::to_string(processed_maps_) + "/wm.xml");
  656. // TODO: Generate other files: background, script, texts...
  657. // Ready for next map or next step.
  658. processed_maps_ ++;
  659. if (processed_maps_ >= wm_map_.size()) return true;
  660. else return false;
  661. }
  662. void WorldInstaller::ExportEncounters(
  663. File& archive_file, const VGears::LGPArchive::FileEntry& entry
  664. ){
  665. // ENC_W.BIN: 160 bytes of chocobo encounters and battle data, then 64 records of 16 words.
  666. // Record = region * 4 + terrain group. Word 0: enabled (low byte bit 0) and rate (high byte,
  667. // the lower the more often). Words 1-6: standard battles, 7-10: special battles
  668. // (back attack x2, side, pincer) and 11-15: others. A word is a battle ID (10 bits) and a
  669. // probability out of 64 (6 bits).
  670. static const unsigned int HEADER_SIZE = 0xA0;
  671. static const unsigned int RECORD_COUNT = 64;
  672. static const unsigned int RECORD_SIZE = 32;
  673. if (entry.data_size < HEADER_SIZE + RECORD_COUNT * RECORD_SIZE){
  674. std::cerr << "ENC_W.BIN has an unexpected size." << std::endl;
  675. return;
  676. }
  677. std::vector<u8> data(entry.data_size);
  678. File file(&archive_file, entry.data_offset, entry.data_size);
  679. file.GetFileBuffer(data.data(), 0, entry.data_size);
  680. // Walkability types that select each of the 4 terrain groups of every region. This table is
  681. // in the executable of the game, not in the data files. The region of a triangle is
  682. // (location & 0x1F), and a region over 15 is 15.
  683. static const unsigned int TERRAIN_TYPES[16][4] = {
  684. {0, 9, 0, 17}, {0, 0, 0, 17}, {0, 9, 1, 17}, {0, 20, 0, 17},
  685. {0, 9, 8, 17}, {0, 0, 25, 17}, {0, 9, 19, 17}, {0, 0, 1, 17},
  686. {0, 0, 1, 17}, {0, 9, 14, 17}, {0, 9, 25, 17}, {0, 10, 9, 17},
  687. {0, 9, 25, 17}, {0, 0, 8, 11}, {0, 0, 8, 0}, {0, 0, 1, 0}
  688. };
  689. boost::filesystem::create_directories(data_dir_ + "/gamedata");
  690. TiXmlDocument doc;
  691. std::unique_ptr<TiXmlElement> root(new TiXmlElement("world_encounters"));
  692. for (unsigned int region = 0; region < 16; ++ region){
  693. for (unsigned int group = 0; group < 4; ++ group){
  694. const unsigned int index = region * 4 + group;
  695. const unsigned int offset = HEADER_SIZE + index * RECORD_SIZE;
  696. std::unique_ptr<TiXmlElement> record(new TiXmlElement("record"));
  697. record->SetAttribute("region", region);
  698. record->SetAttribute("terrain", TERRAIN_TYPES[region][group]);
  699. record->SetAttribute("enabled", data[offset] & 0x01);
  700. record->SetAttribute("rate", data[offset + 1]);
  701. std::string words;
  702. for (unsigned int w = 1; w < 16; ++ w){
  703. const unsigned int word = data[offset + w * 2] | (data[offset + w * 2 + 1] << 8);
  704. words += (w > 1 ? " " : "") + std::to_string(word);
  705. }
  706. record->SetAttribute("words", words);
  707. root->LinkEndChild(record.release());
  708. }
  709. }
  710. doc.LinkEndChild(new TiXmlDeclaration("1.0", "", ""));
  711. doc.LinkEndChild(root.release());
  712. doc.SaveFile(data_dir_ + "/gamedata/world_encounters.xml");
  713. }
  714. bool WorldInstaller::GenerateScripts(
  715. const std::vector<std::string>& field_names,
  716. const std::map<u16, float>& field_scale_factors,
  717. const std::vector<std::string>& converted_fields,
  718. std::string& error_message
  719. ){
  720. error_message.clear();
  721. if (field_names.empty() || converted_fields.empty()){
  722. error_message = "Field maps were not installed; enable field map extraction.";
  723. return false;
  724. }
  725. std::unique_ptr<std::string> archive_path
  726. = disk_image_.filePath("data/wm/world_us.lgp");
  727. if (!archive_path){
  728. error_message = "WORLD_US.LGP was not found on the game disc.";
  729. return false;
  730. }
  731. File archive_file(*archive_path);
  732. VGears::LGPArchive archive(*archive_path, "LGP");
  733. archive.open(*archive_path, true);
  734. archive.load();
  735. const VGears::LGPArchive::FileList& archive_entries = archive.GetFiles();
  736. std::vector<const VGears::LGPArchive::FileEntry*> event_entries;
  737. const VGears::LGPArchive::FileEntry* field_table_entry = nullptr;
  738. const VGears::LGPArchive::FileEntry* encounter_entry = nullptr;
  739. bool has_wm0 = false;
  740. for (const VGears::LGPArchive::FileEntry& entry : archive_entries){
  741. std::string lower_name = entry.file_name;
  742. VGears::StringUtil::toLowerCase(lower_name);
  743. if (
  744. lower_name.size() > 5 && lower_name.compare(0, 2, "wm") == 0
  745. && lower_name.compare(lower_name.size() - 3, 3, ".ev") == 0
  746. ){
  747. const std::string map_number
  748. = lower_name.substr(2, lower_name.size() - 5);
  749. if (!map_number.empty() && std::all_of(
  750. map_number.begin(), map_number.end(), [](char character){
  751. return character >= '0' && character <= '9';
  752. }
  753. )){
  754. event_entries.push_back(&entry);
  755. if (lower_name == "wm0.ev") has_wm0 = true;
  756. }
  757. }
  758. else if (lower_name == "field.tbl"){
  759. field_table_entry = &entry;
  760. }
  761. else if (lower_name == "enc_w.bin"){
  762. encounter_entry = &entry;
  763. }
  764. }
  765. if (encounter_entry != nullptr){
  766. ExportEncounters(archive_file, *encounter_entry);
  767. }
  768. if (event_entries.empty() || !has_wm0 || field_table_entry == nullptr){
  769. error_message = "WM0.EV, other world event files, or FIELD.TBL were not found inside WORLD_US.LGP.";
  770. return false;
  771. }
  772. File field_table_file(
  773. &archive_file, field_table_entry->data_offset, field_table_entry->data_size
  774. );
  775. std::vector<u8> field_table_data(field_table_entry->data_size);
  776. if (field_table_data.size() != 64 * 24){
  777. error_message = "FIELD.TBL has an unexpected size.";
  778. return false;
  779. }
  780. field_table_file.GetFileBuffer(
  781. field_table_data.data(), 0, field_table_entry->data_size
  782. );
  783. const std::string script_directory = data_dir_ + "/scripts/worldmap";
  784. boost::filesystem::create_directories(script_directory);
  785. for (const VGears::LGPArchive::FileEntry* event_entry : event_entries){
  786. File event_file(&archive_file, event_entry->data_offset, event_entry->data_size);
  787. std::vector<u8> event_data(event_entry->data_size);
  788. if (event_data.empty()){
  789. error_message = event_entry->file_name + " inside WORLD_US.LGP is empty.";
  790. return false;
  791. }
  792. event_file.GetFileBuffer(event_data.data(), 0, event_entry->data_size);
  793. std::string map_file_name = event_entry->file_name;
  794. VGears::StringUtil::toLowerCase(map_file_name);
  795. const std::string map_name = map_file_name.substr(0, map_file_name.size() - 3);
  796. std::ofstream script_file(script_directory + "/" + map_name + ".lua");
  797. if (!script_file){
  798. error_message = "Cannot create script output in " + script_directory + ".";
  799. return false;
  800. }
  801. script_file << "-- Decompiled from the user's " << event_entry->file_name << " data.\n\n";
  802. std::vector<bool> generated_functions(256, false);
  803. unsigned int generated_function_count = 0;
  804. for (int script_index = 0; script_index < 256; ++script_index){
  805. try{
  806. WorldEngine engine(script_index);
  807. InstVec instructions;
  808. std::unique_ptr<Disassembler> disassembler = engine.GetDisassembler(instructions);
  809. disassembler->Open(event_data);
  810. disassembler->Disassemble();
  811. if (instructions.empty()) continue;
  812. ControlFlow control_flow(instructions, engine);
  813. control_flow.CreateGroups();
  814. std::ostringstream generated_script;
  815. std::unique_ptr<CodeGenerator> code_generator
  816. = engine.GetCodeGenerator(instructions, generated_script);
  817. code_generator->Generate(instructions, control_flow.GetGraph());
  818. script_file << generated_script.str() << "\n";
  819. generated_functions[script_index] = true;
  820. ++generated_function_count;
  821. }
  822. catch (const DecompilerException&){
  823. std::cerr << "[WARNING] World function " << script_index << " of " << map_name
  824. << " could not be decompiled." << std::endl;
  825. script_file << "-- world function " << script_index
  826. << " could not be decompiled.\n";
  827. }
  828. catch (const std::exception& exception){
  829. std::cerr << "[WARNING] World function " << script_index << " of " << map_name
  830. << " failed: " << exception.what() << std::endl;
  831. script_file << "-- world function " << script_index
  832. << " failed: " << exception.what() << ".\n";
  833. }
  834. }
  835. script_file << "-- " << map_name << " mesh-function call-table mapping.\n";
  836. for (size_t function_index = 0; function_index < generated_functions.size(); ++function_index){
  837. if (!generated_functions[function_index]) continue;
  838. const size_t type_offset = function_index * 4;
  839. if (type_offset + 1 >= event_data.size()) continue;
  840. const uint16 function_type = static_cast<uint16>(event_data[type_offset])
  841. | static_cast<uint16>(event_data[type_offset + 1] << 8);
  842. if ((function_type >> 14) != 2) continue;
  843. const uint16 mesh_index = (function_type >> 4) & 0x03FF;
  844. const uint8 mesh_function_id = function_type & 0x0F;
  845. if (mesh_index >= 36 * 28 || mesh_function_id > 4) continue;
  846. script_file << "System.WorldMap.triangle_functions[" << mesh_index << "]"
  847. << " = System.WorldMap.triangle_functions[" << mesh_index << "] or {}\n"
  848. << "System.WorldMap.triangle_functions[" << mesh_index << "]["
  849. << static_cast<unsigned int>(mesh_function_id) << "] = " << function_index << "\n";
  850. }
  851. // The functions that run when an entity of a type is created, and for the other events of the
  852. // entities. The type is in the bits 8 to 13 of the type of the function, and the event in the low byte.
  853. script_file << "System.WorldMap.entity_functions = {}\n";
  854. for (size_t function_index = 0; function_index < generated_functions.size(); ++function_index){
  855. if (!generated_functions[function_index]) continue;
  856. const size_t type_offset = function_index * 4;
  857. if (type_offset + 1 >= event_data.size()) continue;
  858. const uint16 function_type = static_cast<uint16>(event_data[type_offset])
  859. | static_cast<uint16>(event_data[type_offset + 1] << 8);
  860. if ((function_type >> 14) != 1) continue;
  861. const unsigned int entity_type = (function_type >> 8) & 0x3F;
  862. const unsigned int event = function_type & 0xFF;
  863. script_file << "System.WorldMap.entity_functions[" << entity_type << "] = "
  864. << "System.WorldMap.entity_functions[" << entity_type << "] or {}\n"
  865. << "System.WorldMap.entity_functions[" << entity_type << "][" << event << "] = "
  866. << function_index << "\n";
  867. }
  868. script_file << "System.WorldMap.field_table = {}\n";
  869. for (size_t record_index = 0; record_index < 64; ++record_index){
  870. for (size_t scenario = 0; scenario < 2; ++scenario){
  871. const size_t offset = record_index * 24 + scenario * 12;
  872. const uint16 raw_x = static_cast<uint16>(field_table_data[offset])
  873. | (static_cast<uint16>(field_table_data[offset + 1]) << 8);
  874. const uint16 raw_y = static_cast<uint16>(field_table_data[offset + 2])
  875. | (static_cast<uint16>(field_table_data[offset + 3]) << 8);
  876. const uint16 raw_triangle = static_cast<uint16>(field_table_data[offset + 4])
  877. | (static_cast<uint16>(field_table_data[offset + 5]) << 8);
  878. const uint16 field_id = static_cast<uint16>(field_table_data[offset + 6])
  879. | (static_cast<uint16>(field_table_data[offset + 7]) << 8);
  880. if (field_id >= field_names.size()) continue;
  881. const std::string& field_name = field_names[field_id];
  882. if (std::find(converted_fields.begin(), converted_fields.end(), field_name)
  883. == converted_fields.end()){
  884. continue;
  885. }
  886. const auto scale_factor = field_scale_factors.find(field_id);
  887. if (scale_factor == field_scale_factors.end() || scale_factor->second <= 0){
  888. continue;
  889. }
  890. const float downscale = 128.0f * scale_factor->second;
  891. const float x = static_cast<int16>(raw_x) / downscale;
  892. const float y = static_cast<int16>(raw_y) / downscale;
  893. const int16 triangle = static_cast<int16>(raw_triangle);
  894. const uint8 direction = field_table_data[offset + 8];
  895. const float direction_degrees
  896. = 360.0f * static_cast<float>(direction) / 256.0f;
  897. const size_t table_id = record_index + 1;
  898. script_file << "System.WorldMap.field_table[" << table_id << "]"
  899. << " = System.WorldMap.field_table[" << table_id << "] or {}\n"
  900. << "System.WorldMap.field_table[" << table_id << "][" << scenario << "] = {"
  901. << "field = \"" << field_name << "\", x = " << x << ", y = " << y
  902. << ", triangle = " << triangle << ", direction = "
  903. << direction_degrees << "}\n";
  904. }
  905. }
  906. script_file.flush();
  907. if (!script_file){
  908. error_message = "Failed while writing " + script_directory + "/" + map_name + ".lua.";
  909. return false;
  910. }
  911. if (generated_function_count == 0){
  912. error_message = "No functions in " + event_entry->file_name + " could be decompiled.";
  913. return false;
  914. }
  915. }
  916. return true;
  917. }
  918. void WorldInstaller::ExportMesh(const std::string outdir, const Ogre::MeshPtr &mesh){
  919. if (mesh.isNull()){
  920. std::cerr << "[ERROR] ExportMesh called with null mesh." << std::endl;
  921. return;
  922. }
  923. boost::filesystem::create_directories(outdir);
  924. // TODO: Share function with pc model exporter
  925. VGears::String base_mesh_name;
  926. VGears::StringUtil::splitFull(mesh->getName(), base_mesh_name);
  927. MakeMaterialsUnique(mesh, base_mesh_name);
  928. Ogre::MeshSerializer mesh_serializer;
  929. Ogre::SkeletonPtr skeleton(mesh->getSkeleton());
  930. if (!skeleton.isNull()){
  931. Ogre::SkeletonSerializer skeleton_serializer;
  932. skeleton_serializer.exportSkeleton(skeleton.getPointer(), outdir + base_mesh_name + ".skeleton");
  933. mesh->setSkeletonName(base_mesh_name + ".skeleton");
  934. }
  935. mesh_serializer.exportMesh(mesh.getPointer(), outdir + mesh->getName());
  936. Ogre::Mesh::SubMeshIterator it(mesh->getSubMeshIterator());
  937. Ogre::MaterialSerializer mat_ser;
  938. size_t i(0);
  939. std::set<std::string> queued_materials;
  940. bool has_queued_materials = false;
  941. while (it.hasMoreElements()){
  942. Ogre::SubMesh *sub_mesh(it.getNext());
  943. Ogre::MaterialPtr mat(Ogre::MaterialManager::getSingleton().getByName(sub_mesh->getMaterialName()));
  944. if (mat != nullptr){
  945. for (size_t techs = 0; techs < mat->getNumTechniques(); techs ++){
  946. Ogre::Technique* tech = mat->getTechnique(techs);
  947. if (tech){
  948. for (size_t pass_num = 0; pass_num < tech->getNumPasses(); pass_num ++){
  949. Ogre::Pass* pass = tech->getPass(pass_num);
  950. if (pass){
  951. for (
  952. size_t texture_unit_num = 0;
  953. texture_unit_num < pass->getNumTextureUnitStates();
  954. texture_unit_num ++
  955. ){
  956. Ogre::TextureUnitState* unit = pass->getTextureUnitState(texture_unit_num);
  957. if (unit && unit->getTextureName().empty() == false){
  958. // Materials are shared by name across all model types, so this one may have
  959. // already been converted for a field or battle model (its texture is then a png).
  960. if (!boost::filesystem::exists(output_dir_ + "temp/world_models/" + unit->getTextureName())){
  961. std::cerr << "[WARNING] Texture " << unit->getTextureName()
  962. << " not found in temp/world_models; skipping it." << std::endl;
  963. continue;
  964. }
  965. try{
  966. // Convert the texture from .tex to .png.
  967. TexFile tex(output_dir_ + "temp/world_models/" + unit->getTextureName());
  968. // Ensure the output material script references png files
  969. // rather than tex files.
  970. Ogre::String base_name;
  971. VGears::StringUtil::splitBase(unit->getTextureName(), base_name);
  972. unit->setTextureName(base_mesh_name + "_" + base_name + ".png");
  973. const auto plain_png = outdir + base_name + ".png";
  974. const auto model_png = outdir + base_mesh_name + "_" + base_name + ".png";
  975. tex.SavePng(plain_png, 0);
  976. boost::filesystem::copy_file(
  977. plain_png, model_png, boost::filesystem::copy_options::overwrite_existing
  978. );
  979. }
  980. catch (const std::exception& ex){
  981. std::cerr << "[ERROR] Texture export failed for "
  982. << unit->getTextureName() << ": " << ex.what() << std::endl;
  983. }
  984. }
  985. }
  986. }
  987. }
  988. }
  989. }
  990. // Queue each material once. Exporting with an empty queue throws in Ogre.
  991. if (queued_materials.insert(sub_mesh->getMaterialName()).second){
  992. mat_ser.queueForExport(mat);
  993. has_queued_materials = true;
  994. }
  995. }
  996. ++ i;
  997. }
  998. if (has_queued_materials) mat_ser.exportQueued(outdir + base_mesh_name + VGears::EXT_MATERIAL);
  999. }