FieldModuleInstruction.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * V-Gears
  3. * Copyright (C) 2022 V-Gears Team
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <iostream>
  19. #include <sstream>
  20. #include <boost/format.hpp>
  21. #include "decompiler/field/instruction/FieldModuleInstruction.h"
  22. #include "decompiler/field/FieldEngine.h"
  23. #include "decompiler/field/FieldCodeGenerator.h"
  24. #include "decompiler/field/FieldDisassembler.h"
  25. void FF7::FieldModuleInstruction::ProcessInst(
  26. Function& func, ValueStack&, Engine* engine, CodeGenerator *code_gen
  27. ){
  28. FunctionMetaData md(func.metadata);
  29. switch (opcode_){
  30. case OPCODES::DSKCG: code_gen->WriteTodo(md.GetEntityName(), "DSKCG"); break;
  31. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::ARROW:
  32. code_gen->AddOutputLine((
  33. boost::format("game:pointer_enable(%1%)")
  34. % (params_[0]->GetUnsigned() ? "true" : "false")
  35. ).str());
  36. break;
  37. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::PNAME:
  38. code_gen->WriteTodo(md.GetEntityName(), "PNAME");
  39. break;
  40. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::GMSPD:
  41. code_gen->WriteTodo(md.GetEntityName(), "GMSPD");
  42. break;
  43. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::SMSPD:
  44. code_gen->WriteTodo(md.GetEntityName(), "SMSPD");
  45. break;
  46. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::FLMAT:
  47. code_gen->AddOutputLine("game:fill_materia()");
  48. break;
  49. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::FLITM:
  50. code_gen->WriteTodo(md.GetEntityName(), "FLITM");
  51. break;
  52. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::BTLCK:
  53. code_gen->AddOutputLine((
  54. boost::format("game:battle_enable(%1%)")
  55. % (params_[0]->GetUnsigned() ? "true" : "false")
  56. ).str());
  57. break;
  58. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::MVLCK:
  59. code_gen->AddOutputLine((
  60. boost::format("game:movie_enable(%1%)")
  61. % (params_[0]->GetUnsigned() ? "true" : "false")
  62. ).str());
  63. break;
  64. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::SPCNM:
  65. code_gen->WriteTodo(md.GetEntityName(), "SPCNM");
  66. break;
  67. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::RSGLB:
  68. code_gen->AddOutputLine("game:global_reset()");
  69. break;
  70. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::CLITM:
  71. code_gen->WriteTodo(md.GetEntityName(), "CLITM");
  72. break;
  73. case OPCODES::MINIGAME: code_gen->WriteTodo(md.GetEntityName(), "MINIGAME"); break;
  74. case OPCODES::BTMD2: code_gen->WriteTodo(md.GetEntityName(), "BTMD2"); break;
  75. case OPCODES::BTRLD: code_gen->WriteTodo(md.GetEntityName(), "BTRLD"); break;
  76. case OPCODES::BTLTB: code_gen->WriteTodo(md.GetEntityName(), "BTLTB"); break;
  77. case OPCODES::MAPJUMP: ProcessMAPJUMP(code_gen, func); break;
  78. case OPCODES::LSTMP: code_gen->WriteTodo(md.GetEntityName(), "LSTMP"); break;
  79. case OPCODES::BATTLE: ProcessBATTLE(code_gen); break;
  80. case OPCODES::BTLON: ProcessBTLON(code_gen); break;
  81. case OPCODES::BTLMD: code_gen->WriteTodo(md.GetEntityName(), "BTLMD"); break;
  82. case OPCODES::MPJPO:
  83. // Gateway function will do nothing if this is set to true
  84. code_gen->AddOutputLine(
  85. std::string("FFVII.Data.DisableGateways=")
  86. + (params_[0]->GetUnsigned() ? "true" : "false")
  87. );
  88. break;
  89. // Prepare to change map, don't need to output anything for this.
  90. case OPCODES::PMJMP: code_gen->AddOutputLine("-- Prepare map change"); break;
  91. // Prepare to change map, don't need to output anything for this,
  92. // seems to be the same thing as PMJMP.
  93. case OPCODES::PMJMP2: code_gen->AddOutputLine("-- Prepare map change 2"); break;
  94. case OPCODES::GAMEOVER: code_gen->WriteTodo(md.GetEntityName(), "GAMEOVER"); break;
  95. default:
  96. code_gen->AddOutputLine(FF7::FieldCodeGenerator::FormatInstructionNotImplemented(
  97. md.GetEntityName(), address_, opcode_
  98. ));
  99. }
  100. }
  101. void FF7::FieldModuleInstruction::ProcessBATTLE(CodeGenerator* code_gen){
  102. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  103. const auto& battle_id = FF7::FieldCodeGenerator::FormatValueOrVariable(
  104. cg->GetFormatter(), params_[0]->GetUnsigned(), params_[1]->GetUnsigned()
  105. );
  106. code_gen->AddOutputLine((boost::format("entity_manager:battle_run(%1%)") % battle_id).str());
  107. }
  108. void FF7::FieldModuleInstruction::ProcessBTLON(CodeGenerator* code_gen){
  109. code_gen->AddOutputLine((
  110. boost::format("entity_manager:random_encounters_on(%1%)")
  111. % FF7::FieldCodeGenerator::FormatInvertedBool(params_[0]->GetUnsigned())
  112. ).str());
  113. }
  114. void FF7::FieldModuleInstruction::ProcessMAPJUMP(CodeGenerator* code_gen, Function& func){
  115. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  116. const auto target_map_id = params_[0]->GetUnsigned();
  117. FunctionMetaData md(func.metadata);
  118. const std::string source_spawn_point_name = cg->GetFormatter().GetSpawnPointName(
  119. target_map_id, md.GetEntityName(), func.name, address_
  120. );
  121. cg->GetFormatter().AddSpawnPoint(
  122. target_map_id, md.GetEntityName(), func.name, address_,
  123. params_[1]->GetSigned(), // X
  124. params_[2]->GetSigned(), // Y
  125. params_[3]->GetSigned(), // Walk mesh triangle ID
  126. params_[4]->GetSigned() // Angle
  127. );
  128. const std::string target_map_name = cg->GetFormatter().GetMapName(target_map_id);
  129. code_gen->AddOutputLine(
  130. "load_field_map_request(\"" + target_map_name + "\", \"" + source_spawn_point_name + "\")"
  131. );
  132. }