FieldModuleInstruction.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 FieldModuleInstruction::ProcessInst(
  26. Function& func, ValueStack&, Engine* engine, CodeGenerator *code_gen
  27. ){
  28. FunctionMetaData md(func.metadata);
  29. //std::cout << "Module opcode: " << opcode_ << "\n";
  30. switch (opcode_){
  31. case OPCODES::DSKCG: code_gen->WriteTodo(md.GetEntityName(), "DSKCG"); break;
  32. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::ARROW:
  33. code_gen->AddOutputLine((
  34. boost::format("game:pointer_enable(%1%)")
  35. % (params_[0]->GetUnsigned() ? "true" : "false")
  36. ).str());
  37. break;
  38. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::PNAME:
  39. code_gen->WriteTodo(md.GetEntityName(), "PNAME");
  40. break;
  41. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::GMSPD:
  42. code_gen->WriteTodo(md.GetEntityName(), "GMSPD");
  43. break;
  44. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::SMSPD:
  45. code_gen->WriteTodo(md.GetEntityName(), "SMSPD");
  46. break;
  47. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::FLMAT:
  48. code_gen->AddOutputLine("game:fill_materia()");
  49. break;
  50. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::FLITM:
  51. code_gen->WriteTodo(md.GetEntityName(), "FLITM");
  52. break;
  53. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::BTLCK:
  54. code_gen->AddOutputLine((
  55. boost::format("game:battle_enable(%1%)")
  56. % (params_[0]->GetUnsigned() ? "true" : "false")
  57. ).str());
  58. break;
  59. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::MVLCK:
  60. code_gen->AddOutputLine((
  61. boost::format("game:movie_enable(%1%)")
  62. % (params_[0]->GetUnsigned() ? "true" : "false")
  63. ).str());
  64. break;
  65. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::SPCNM:
  66. code_gen->WriteTodo(md.GetEntityName(), "SPCNM");
  67. break;
  68. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::RSGLB:
  69. code_gen->AddOutputLine("game:global_reset()");
  70. break;
  71. case (OPCODES::SPECIAL << 8) | OPCODES_SPECIAL::CLITM:
  72. code_gen->WriteTodo(md.GetEntityName(), "CLITM");
  73. break;
  74. case OPCODES::MINIGAME: code_gen->WriteTodo(md.GetEntityName(), "MINIGAME"); break;
  75. case OPCODES::BTMD2: code_gen->WriteTodo(md.GetEntityName(), "BTMD2"); break;
  76. case OPCODES::BTRLD: code_gen->WriteTodo(md.GetEntityName(), "BTRLD"); break;
  77. case OPCODES::BTLTB: code_gen->WriteTodo(md.GetEntityName(), "BTLTB"); break;
  78. case OPCODES::MAPJUMP: ProcessMAPJUMP(code_gen, func); break;
  79. case OPCODES::LSTMP: ProcessLSTMP(code_gen); break;
  80. case OPCODES::BATTLE: ProcessBATTLE(code_gen); break;
  81. case OPCODES::BTLON: ProcessBTLON(code_gen); break;
  82. case OPCODES::BTLMD: ProcessBTLMD(code_gen); break;
  83. case OPCODES::MPJPO:
  84. // Gateway function will do nothing if this is set to true
  85. code_gen->AddOutputLine(
  86. std::string("Data.DisableGateways=")
  87. + (params_[0]->GetUnsigned() ? "true" : "false")
  88. );
  89. break;
  90. // Prepare to change map, don't need to output anything for this.
  91. case OPCODES::PMJMP: code_gen->AddOutputLine("-- Prepare map change"); break;
  92. // Prepare to change map, don't need to output anything for this,
  93. // seems to be the same thing as PMJMP.
  94. case OPCODES::PMJMP2: code_gen->AddOutputLine("-- Prepare map change 2"); break;
  95. case OPCODES::GAMEOVER: code_gen->WriteTodo(md.GetEntityName(), "GAMEOVER"); break;
  96. default:
  97. code_gen->AddOutputLine(FieldCodeGenerator::FormatInstructionNotImplemented(
  98. md.GetEntityName(), address_, opcode_
  99. ));
  100. }
  101. }
  102. void FieldModuleInstruction::ProcessLSTMP(CodeGenerator* code_gen){
  103. // Stores the ID of the field the player comes from (see the header of the scripts).
  104. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  105. // Bank 0 is not a variable (some fields use it with no destination): nothing to store.
  106. if (params_[0]->GetUnsigned() * 16 + params_[1]->GetUnsigned() == 0){
  107. code_gen->AddOutputLine("-- LSTMP without a destination");
  108. return;
  109. }
  110. code_gen->AddOutputLine((
  111. boost::format("%1% = Data.PreviousFieldId or 0")
  112. % FieldCodeGenerator::FormatValueOrVariable(
  113. cg->GetFormatter(), params_[0]->GetUnsigned() * 16 + params_[1]->GetUnsigned(),
  114. params_[2]->GetUnsigned()
  115. )
  116. ).str());
  117. }
  118. void FieldModuleInstruction::ProcessBATTLE(CodeGenerator* code_gen){
  119. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  120. // Operands: two bank nibbles, one of them always zero, and then the battle ID or the address
  121. // to read it from.
  122. const auto& battle_id = FieldCodeGenerator::FormatValueOrVariable(
  123. cg->GetFormatter(), params_[0]->GetUnsigned() + params_[1]->GetUnsigned(),
  124. params_[2]->GetUnsigned()
  125. );
  126. code_gen->AddOutputLine((boost::format("Battle.start(%1%)") % battle_id).str());
  127. }
  128. void FieldModuleInstruction::ProcessBTLMD(CodeGenerator* code_gen){
  129. // Battle mode: flags for the next battles, see BattleManager::ScriptSetMode.
  130. code_gen->AddOutputLine(
  131. (boost::format("battle_manager:set_mode(%1%)") % params_[0]->GetUnsigned()).str()
  132. );
  133. }
  134. void FieldModuleInstruction::ProcessBTLON(CodeGenerator* code_gen){
  135. code_gen->AddOutputLine((
  136. boost::format("entity_manager:random_encounters_on(%1%)")
  137. % FieldCodeGenerator::FormatInvertedBool(params_[0]->GetUnsigned())
  138. ).str());
  139. }
  140. void FieldModuleInstruction::ProcessMAPJUMP(CodeGenerator* code_gen, Function& func){
  141. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  142. const auto target_map_id = params_[0]->GetUnsigned();
  143. FunctionMetaData md(func.metadata);
  144. const std::string source_spawn_point_name = cg->GetFormatter().GetSpawnPointName(
  145. target_map_id, md.GetEntityName(), func.name, address_
  146. );
  147. cg->GetFormatter().AddSpawnPoint(
  148. target_map_id, md.GetEntityName(), func.name, address_,
  149. params_[1]->GetSigned(), // X
  150. params_[2]->GetSigned(), // Y
  151. params_[3]->GetSigned(), // Walk mesh triangle ID
  152. params_[4]->GetSigned() // Angle
  153. );
  154. const std::string target_map_name = cg->GetFormatter().GetMapName(target_map_id);
  155. code_gen->AddOutputLine(
  156. "load_field_map_request(\"" + target_map_name + "\", \"" + source_spawn_point_name + "\")"
  157. );
  158. }