FieldPartyInstruction.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/FieldPartyInstruction.h"
  22. #include "decompiler/field/FieldEngine.h"
  23. #include "decompiler/field/FieldCodeGenerator.h"
  24. #include "decompiler/field/FieldDisassembler.h"
  25. void FieldPartyInstruction::ProcessInst(
  26. Function& func, ValueStack&, Engine* engine, CodeGenerator *code_gen
  27. ){
  28. FunctionMetaData md(func.metadata);
  29. //std::cout << "Party opcode: " << opcode_ << "\n";
  30. switch (opcode_){
  31. case OPCODES::SPTYE: ProcessPartyVariables(code_gen, true); break;
  32. case OPCODES::GTPYE: ProcessPartyVariables(code_gen, false); break;
  33. case OPCODES::GOLDU: ProcessGOLDU(code_gen); break;
  34. case OPCODES::GOLDD: ProcessGOLDD(code_gen); break;
  35. case OPCODES::HMPMAX1: code_gen->WriteTodo(md.GetEntityName(), "HMPMAX1"); break;
  36. case OPCODES::HMPMAX2: code_gen->WriteTodo(md.GetEntityName(), "HMPMAX2"); break;
  37. case OPCODES::MHMMX:
  38. // Full HP and MP of all the characters, and no status.
  39. code_gen->AddOutputLine("restore_all_characters()");
  40. break;
  41. case OPCODES::HMPMAX3: code_gen->WriteTodo(md.GetEntityName(), "HMPMAX3"); break;
  42. case OPCODES::MPU: code_gen->WriteTodo(md.GetEntityName(), "MPU"); break;
  43. case OPCODES::MPD: code_gen->WriteTodo(md.GetEntityName(), "MPD"); break;
  44. case OPCODES::HPU: code_gen->WriteTodo(md.GetEntityName(), "HPU"); break;
  45. case OPCODES::HPD: code_gen->WriteTodo(md.GetEntityName(), "HPD"); break;
  46. case OPCODES::STITM: ProcessSTITM(code_gen); break;
  47. case OPCODES::DLITM: ProcessDLITM(code_gen); break;
  48. case OPCODES::CKITM: ProcessCKITM(code_gen); break;
  49. case OPCODES::SMTRA: ProcessSMTRA(code_gen, md.GetEntityName()); break;
  50. case OPCODES::DMTRA: code_gen->WriteTodo(md.GetEntityName(), "DMTRA"); break;
  51. case OPCODES::CMTRA: code_gen->WriteTodo(md.GetEntityName(), "CMTRA"); break;
  52. case OPCODES::GETPC: code_gen->WriteTodo(md.GetEntityName(), "GETPC"); break;
  53. case OPCODES::PRTYP:
  54. // Adds a character to the party, in the first free place.
  55. code_gen->AddOutputLine(
  56. (boost::format("add_party_character(%1%)") % params_[0]->GetString()).str()
  57. );
  58. break;
  59. case OPCODES::PRTYM:
  60. // Removes a character from the party.
  61. code_gen->AddOutputLine(
  62. (boost::format("remove_party_character(%1%)") % params_[0]->GetString()).str()
  63. );
  64. break;
  65. case OPCODES::PRTYE: ProcessPRTYE(code_gen); break;
  66. case OPCODES::MMBUD: ProcessMMBUD(code_gen); break;
  67. case OPCODES::MMBLK: ProcessMMBLK(code_gen); break;
  68. case OPCODES::MMBUK: ProcessMMBUK(code_gen); break;
  69. case OPCODES::CHGLD: ProcessCHGLD(code_gen); break;
  70. default:
  71. code_gen->AddOutputLine(FieldCodeGenerator::FormatInstructionNotImplemented(
  72. md.GetEntityName(), address_, opcode_
  73. ));
  74. }
  75. }
  76. namespace{
  77. /**
  78. * Writes the amount of gil of a GOLDU or GOLDD.
  79. *
  80. * The opcode is read as a byte of two nibbles (two parameters, the bank is the first) followed
  81. * by four bytes. With a bank of zero, the four bytes are the amount, as a little endian dword.
  82. * Otherwise, the first of the four bytes is the address in the bank, and the rest are zeroes.
  83. */
  84. std::string GilAmount(const std::vector<ValuePtr>& params){
  85. if (params[0]->GetUnsigned() == 0){
  86. const long amount = params[2]->GetUnsigned() + params[3]->GetUnsigned() * 256
  87. + params[4]->GetUnsigned() * 256 * 256 + params[5]->GetUnsigned() * 256 * 256 * 256;
  88. return std::to_string(amount);
  89. }
  90. return (
  91. boost::format("Banks[%1%][%2%]") % params[0]->GetUnsigned() % params[2]->GetUnsigned()
  92. ).str();
  93. }
  94. }
  95. void FieldPartyInstruction::ProcessGOLDU(CodeGenerator* code_gen){
  96. code_gen->AddOutputLine("Inventory.add_money(" + GilAmount(params_) + ")");
  97. }
  98. void FieldPartyInstruction::ProcessGOLDD(CodeGenerator* code_gen){
  99. code_gen->AddOutputLine("Inventory.remove_money(" + GilAmount(params_) + ")");
  100. }
  101. void FieldPartyInstruction::ProcessSTITM(CodeGenerator* code_gen){
  102. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  103. const auto& item_id = FieldCodeGenerator::FormatValueOrVariable(
  104. cg->GetFormatter(), params_[0]->GetUnsigned(), params_[2]->GetUnsigned()
  105. );
  106. const auto& amount = FieldCodeGenerator::FormatValueOrVariable(
  107. cg->GetFormatter(), params_[1]->GetUnsigned(), params_[3]->GetUnsigned()
  108. );
  109. code_gen->AddOutputLine(
  110. (boost::format("Inventory.add_item(%1%, %2%)") % item_id % amount).str()
  111. );
  112. }
  113. void FieldPartyInstruction::ProcessDLITM(CodeGenerator* code_gen){
  114. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  115. const auto& item_id = FieldCodeGenerator::FormatValueOrVariable(
  116. cg->GetFormatter(), params_[0]->GetUnsigned(), params_[2]->GetUnsigned()
  117. );
  118. const auto& amount = FieldCodeGenerator::FormatValueOrVariable(
  119. cg->GetFormatter(), params_[1]->GetUnsigned(), params_[3]->GetUnsigned()
  120. );
  121. code_gen->AddOutputLine(
  122. (boost::format("Inventory.remove_item(%1%, %2%)") % item_id % amount).str()
  123. );
  124. }
  125. void FieldPartyInstruction::ProcessCKITM(CodeGenerator* code_gen){
  126. // Operands: the bank of the destination (in the two nibbles of the first byte), the ID of the
  127. // item and the address, in that bank, that receives how many of the item there are.
  128. code_gen->AddOutputLine((
  129. boost::format("Banks[%1%][%2%] = Inventory.get_item_quantity(%3%)")
  130. % (params_[0]->GetUnsigned() * 16 + params_[1]->GetUnsigned()) % params_[3]->GetUnsigned()
  131. % params_[2]->GetUnsigned()
  132. ).str());
  133. }
  134. void FieldPartyInstruction::ProcessSMTRA(CodeGenerator* code_gen, const std::string& entity){
  135. // Operands: four bank nibbles (all of them zero in the original fields: the values are
  136. // literal), the ID of the materia and its AP, in three bytes (255, 255, 255 is the most).
  137. for (int i = 0; i < 4; i ++){
  138. if (params_[i]->GetUnsigned() != 0){
  139. code_gen->WriteTodo(entity, "SMTRA");
  140. return;
  141. }
  142. }
  143. const long ap = params_[5]->GetUnsigned() + params_[6]->GetUnsigned() * 256
  144. + params_[7]->GetUnsigned() * 256 * 256;
  145. code_gen->AddOutputLine((
  146. boost::format("Materia.add(%1%, %2%)") % params_[4]->GetUnsigned() % ap
  147. ).str());
  148. }
  149. void FieldPartyInstruction::ProcessPartyVariables(CodeGenerator* code_gen, const bool set){
  150. // Operands: the banks of three variables (a nibble for each, plus an unused one) and their
  151. // addresses. GTPYE stores the characters of the party in them (255 for an empty place) and
  152. // SPTYE sets the party from them. The scenes that change the party (a flashback) keep it that
  153. // way and restore it.
  154. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  155. std::string variables[3];
  156. for (int i = 0; i < 3; i ++){
  157. variables[i] = FieldCodeGenerator::FormatValueOrVariable(
  158. cg->GetFormatter(), params_[i]->GetUnsigned(), params_[4 + i]->GetUnsigned()
  159. );
  160. }
  161. if (set){
  162. code_gen->AddOutputLine((
  163. boost::format("set_party_from_values(%1%, %2%, %3%)")
  164. % variables[0] % variables[1] % variables[2]
  165. ).str());
  166. }
  167. else{
  168. for (int i = 0; i < 3; i ++){
  169. code_gen->AddOutputLine(
  170. (boost::format("%1% = Party[%2%] or 255") % variables[i] % (i + 1)).str()
  171. );
  172. }
  173. }
  174. }
  175. void FieldPartyInstruction::ProcessPRTYE(CodeGenerator* code_gen){
  176. std::string chars[3];
  177. for (int i = 0; i < 3; i ++){
  178. if (params_[i]->GetUnsigned() < 254) chars[i] = params_[i]->GetString();
  179. else chars[i] = "nil";
  180. }
  181. code_gen->AddOutputLine((
  182. boost::format("set_party(%1%, %2%, %3%)") % chars[0] % chars[1] % chars[2]
  183. ).str());
  184. }
  185. void FieldPartyInstruction::ProcessMMBUD(CodeGenerator* code_gen){
  186. // Param 0 is 0 to hide the character in the PHS and anything else to show it, param 1 is the
  187. // character (checked in the opcode handler of ff7.exe, 0x61c782).
  188. code_gen->AddOutputLine((
  189. boost::format("set_character_enable(%1%, %2%)") % params_[1]->GetString()
  190. % (params_[0]->GetUnsigned() != 0 ? "true" : "false")
  191. ).str());
  192. }
  193. void FieldPartyInstruction::ProcessMMBLK(CodeGenerator* code_gen){
  194. // Locks the character in the party: it can't be swapped in the PHS.
  195. code_gen->AddOutputLine((
  196. boost::format("set_party_lock(%1%, true)") % params_[0]->GetString()
  197. ).str());
  198. }
  199. void FieldPartyInstruction::ProcessMMBUK(CodeGenerator* code_gen){
  200. // Unlocks the character, so it can be swapped in the PHS again.
  201. code_gen->AddOutputLine((
  202. boost::format("set_party_lock(%1%, false)") % params_[0]->GetString()
  203. ).str());
  204. }
  205. void FieldPartyInstruction::ProcessCHGLD(CodeGenerator* code_gen){
  206. code_gen->AddOutputLine((
  207. boost::format("Inventory.money_to_banks(%1%, %2%, %3%, %4%)")
  208. % params_[0]->GetUnsigned() % params_[1]->GetUnsigned()
  209. % params_[2]->GetUnsigned() % params_[3]->GetUnsigned()
  210. ).str());
  211. }