FieldPartyInstruction.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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: code_gen->WriteTodo(md.GetEntityName(), "SPTYE"); break;
  32. case OPCODES::GTPYE: code_gen->WriteTodo(md.GetEntityName(), "GTPYE"); 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: code_gen->WriteTodo(md.GetEntityName(), "MHMMX"); break;
  38. case OPCODES::HMPMAX3: code_gen->WriteTodo(md.GetEntityName(), "HMPMAX3"); break;
  39. case OPCODES::MPU: code_gen->WriteTodo(md.GetEntityName(), "MPU"); break;
  40. case OPCODES::MPD: code_gen->WriteTodo(md.GetEntityName(), "MPD"); break;
  41. case OPCODES::HPU: code_gen->WriteTodo(md.GetEntityName(), "HPU"); break;
  42. case OPCODES::HPD: code_gen->WriteTodo(md.GetEntityName(), "HPD"); break;
  43. case OPCODES::STITM: ProcessSTITM(code_gen); break;
  44. case OPCODES::DLITM: ProcessDLITM(code_gen); break;
  45. case OPCODES::CKITM: ProcessCKITM(code_gen); break;
  46. case OPCODES::SMTRA: code_gen->WriteTodo(md.GetEntityName(), "SMTRA"); break;
  47. case OPCODES::DMTRA: code_gen->WriteTodo(md.GetEntityName(), "DMTRA"); break;
  48. case OPCODES::CMTRA: code_gen->WriteTodo(md.GetEntityName(), "CMTRA"); break;
  49. case OPCODES::GETPC: code_gen->WriteTodo(md.GetEntityName(), "GETPC"); break;
  50. case OPCODES::PRTYP:
  51. // Adds a character to the party, in the first free place.
  52. code_gen->AddOutputLine(
  53. (boost::format("add_party_character(%1%)") % params_[0]->GetString()).str()
  54. );
  55. break;
  56. case OPCODES::PRTYM:
  57. // Removes a character from the party.
  58. code_gen->AddOutputLine(
  59. (boost::format("remove_party_character(%1%)") % params_[0]->GetString()).str()
  60. );
  61. break;
  62. case OPCODES::PRTYE: ProcessPRTYE(code_gen); break;
  63. case OPCODES::MMBUD: ProcessMMBUD(code_gen); break;
  64. case OPCODES::MMBLK: ProcessMMBLK(code_gen); break;
  65. case OPCODES::MMBUK: ProcessMMBUK(code_gen); break;
  66. case OPCODES::CHGLD: ProcessCHGLD(code_gen); break;
  67. default:
  68. code_gen->AddOutputLine(FieldCodeGenerator::FormatInstructionNotImplemented(
  69. md.GetEntityName(), address_, opcode_
  70. ));
  71. }
  72. }
  73. namespace{
  74. /**
  75. * Writes the amount of gil of a GOLDU or GOLDD.
  76. *
  77. * The opcode is read as a byte of two nibbles (two parameters, the bank is the first) followed
  78. * by four bytes. With a bank of zero, the four bytes are the amount, as a little endian dword.
  79. * Otherwise, the first of the four bytes is the address in the bank, and the rest are zeroes.
  80. */
  81. std::string GilAmount(const std::vector<ValuePtr>& params){
  82. if (params[0]->GetUnsigned() == 0){
  83. const long amount = params[2]->GetUnsigned() + params[3]->GetUnsigned() * 256
  84. + params[4]->GetUnsigned() * 256 * 256 + params[5]->GetUnsigned() * 256 * 256 * 256;
  85. return std::to_string(amount);
  86. }
  87. return (
  88. boost::format("Banks[%1%][%2%]") % params[0]->GetUnsigned() % params[2]->GetUnsigned()
  89. ).str();
  90. }
  91. }
  92. void FieldPartyInstruction::ProcessGOLDU(CodeGenerator* code_gen){
  93. code_gen->AddOutputLine("Inventory.add_money(" + GilAmount(params_) + ")");
  94. }
  95. void FieldPartyInstruction::ProcessGOLDD(CodeGenerator* code_gen){
  96. code_gen->AddOutputLine("Inventory.remove_money(" + GilAmount(params_) + ")");
  97. }
  98. void FieldPartyInstruction::ProcessSTITM(CodeGenerator* code_gen){
  99. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  100. const auto& item_id = FieldCodeGenerator::FormatValueOrVariable(
  101. cg->GetFormatter(), params_[0]->GetUnsigned(), params_[2]->GetUnsigned()
  102. );
  103. const auto& amount = FieldCodeGenerator::FormatValueOrVariable(
  104. cg->GetFormatter(), params_[1]->GetUnsigned(), params_[3]->GetUnsigned()
  105. );
  106. code_gen->AddOutputLine(
  107. (boost::format("Inventory.add_item(%1%, %2%)") % item_id % amount).str()
  108. );
  109. }
  110. void FieldPartyInstruction::ProcessDLITM(CodeGenerator* code_gen){
  111. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  112. const auto& item_id = FieldCodeGenerator::FormatValueOrVariable(
  113. cg->GetFormatter(), params_[0]->GetUnsigned(), params_[2]->GetUnsigned()
  114. );
  115. const auto& amount = FieldCodeGenerator::FormatValueOrVariable(
  116. cg->GetFormatter(), params_[1]->GetUnsigned(), params_[3]->GetUnsigned()
  117. );
  118. code_gen->AddOutputLine(
  119. (boost::format("Inventory.remove_item(%1%, %2%)") % item_id % amount).str()
  120. );
  121. }
  122. void FieldPartyInstruction::ProcessCKITM(CodeGenerator* code_gen){
  123. code_gen->AddOutputLine((
  124. boost::format("Banks[%1%][%2%] = Inventory.get_item_quantity(%3%)")
  125. % params_[0]->GetUnsigned() % params_[2]->GetUnsigned() % params_[1]->GetUnsigned()
  126. ).str());
  127. }
  128. void FieldPartyInstruction::ProcessPRTYE(CodeGenerator* code_gen){
  129. std::string chars[3];
  130. for (int i = 0; i < 3; i ++){
  131. if (params_[i]->GetUnsigned() < 254) chars[i] = params_[i]->GetString();
  132. else chars[i] = "nil";
  133. }
  134. code_gen->AddOutputLine((
  135. boost::format("set_party(%1%, %2%, %3%)") % chars[0] % chars[1] % chars[2]
  136. ).str());
  137. }
  138. void FieldPartyInstruction::ProcessMMBUD(CodeGenerator* code_gen){
  139. // Param 0 is the character, param 1 is 0 to hide it in the PHS and anything else to show it.
  140. code_gen->AddOutputLine((
  141. boost::format("set_character_enable(%1%, %2%)") % params_[0]->GetString()
  142. % (params_[1]->GetUnsigned() != 0 ? "true" : "false")
  143. ).str());
  144. }
  145. void FieldPartyInstruction::ProcessMMBLK(CodeGenerator* code_gen){
  146. // Locks the character in the party: it can't be swapped in the PHS.
  147. code_gen->AddOutputLine((
  148. boost::format("set_party_lock(%1%, true)") % params_[0]->GetString()
  149. ).str());
  150. }
  151. void FieldPartyInstruction::ProcessMMBUK(CodeGenerator* code_gen){
  152. // Unlocks the character, so it can be swapped in the PHS again.
  153. code_gen->AddOutputLine((
  154. boost::format("set_party_lock(%1%, false)") % params_[0]->GetString()
  155. ).str());
  156. }
  157. void FieldPartyInstruction::ProcessCHGLD(CodeGenerator* code_gen){
  158. code_gen->AddOutputLine((
  159. boost::format("Inventory.money_to_banks(%1%, %2%, %3%, %4%)")
  160. % params_[0]->GetUnsigned() % params_[1]->GetUnsigned()
  161. % params_[2]->GetUnsigned() % params_[3]->GetUnsigned()
  162. ).str());
  163. }