FieldModuleInstruction.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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: code_gen->WriteTodo(md.GetEntityName(), "LSTMP"); 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::ProcessBATTLE(CodeGenerator* code_gen){
  103. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  104. // Operands: two bank nibbles, one of them always zero, and then the battle ID or the address
  105. // to read it from.
  106. const auto& battle_id = FieldCodeGenerator::FormatValueOrVariable(
  107. cg->GetFormatter(), params_[0]->GetUnsigned() + params_[1]->GetUnsigned(),
  108. params_[2]->GetUnsigned()
  109. );
  110. code_gen->AddOutputLine((boost::format("Battle.start(%1%)") % battle_id).str());
  111. }
  112. void FieldModuleInstruction::ProcessBTLMD(CodeGenerator* code_gen){
  113. // Battle mode: flags for the next battles, see BattleManager::ScriptSetMode.
  114. code_gen->AddOutputLine(
  115. (boost::format("battle_manager:set_mode(%1%)") % params_[0]->GetUnsigned()).str()
  116. );
  117. }
  118. void FieldModuleInstruction::ProcessBTLON(CodeGenerator* code_gen){
  119. code_gen->AddOutputLine((
  120. boost::format("entity_manager:random_encounters_on(%1%)")
  121. % FieldCodeGenerator::FormatInvertedBool(params_[0]->GetUnsigned())
  122. ).str());
  123. }
  124. void FieldModuleInstruction::ProcessMAPJUMP(CodeGenerator* code_gen, Function& func){
  125. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  126. const auto target_map_id = params_[0]->GetUnsigned();
  127. FunctionMetaData md(func.metadata);
  128. const std::string source_spawn_point_name = cg->GetFormatter().GetSpawnPointName(
  129. target_map_id, md.GetEntityName(), func.name, address_
  130. );
  131. cg->GetFormatter().AddSpawnPoint(
  132. target_map_id, md.GetEntityName(), func.name, address_,
  133. params_[1]->GetSigned(), // X
  134. params_[2]->GetSigned(), // Y
  135. params_[3]->GetSigned(), // Walk mesh triangle ID
  136. params_[4]->GetSigned() // Angle
  137. );
  138. const std::string target_map_name = cg->GetFormatter().GetMapName(target_map_id);
  139. code_gen->AddOutputLine(
  140. "load_field_map_request(\"" + target_map_name + "\", \"" + source_spawn_point_name + "\")"
  141. );
  142. }