FieldCameraInstruction.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/FieldCameraInstruction.h"
  22. #include "decompiler/field/FieldEngine.h"
  23. #include "decompiler/field/FieldCodeGenerator.h"
  24. #include "decompiler/field/FieldDisassembler.h"
  25. void FieldCameraInstruction::ProcessInst(
  26. Function& func, ValueStack&, Engine* engine, CodeGenerator *code_gen
  27. ){
  28. FunctionMetaData md(func.metadata);
  29. //std::cout << "Camera opcode: " << opcode_ << "\n";
  30. switch (opcode_){
  31. case OPCODES::NFADE: ProcessNFADE(code_gen); break;
  32. case OPCODES::SHAKE: ProcessSHAKE(code_gen); break;
  33. case OPCODES::SCRLO: code_gen->WriteTodo(md.GetEntityName(), "SCRLO"); break;
  34. case OPCODES::SCRLC: ProcessSCRLC(code_gen); break;
  35. case OPCODES::SCR2D: ProcessSCR2D(code_gen); break;
  36. case OPCODES::SCRCC: ProcessSCRCC(code_gen); break;
  37. case OPCODES::SCR2DC: ProcessSCR2DC(code_gen); break;
  38. case OPCODES::SCRLW: code_gen->AddOutputLine("background2d:scroll_sync()"); break;
  39. case OPCODES::SCR2DL: ProcessSCR2DL(code_gen); break;
  40. case OPCODES::VWOFT: ProcessVWOFT(code_gen); break;
  41. case OPCODES::FADE: ProcessFADE(code_gen); break;
  42. case OPCODES::FADEW: code_gen->AddOutputLine("screen_fade:wait()"); break;
  43. case OPCODES::SCRLP: code_gen->WriteTodo(md.GetEntityName(), "SCRLP"); break;
  44. case OPCODES::MVCAM: code_gen->WriteTodo(md.GetEntityName(), "MVCAM"); break;
  45. case OPCODES::SCRLA: ProcessSCRLA(code_gen, static_cast<FieldEngine&>(*engine)); break;
  46. default:
  47. code_gen->AddOutputLine(FieldCodeGenerator::FormatInstructionNotImplemented(
  48. md.GetEntityName(), address_, opcode_
  49. ));
  50. }
  51. }
  52. void FieldCameraInstruction::ProcessNFADE(CodeGenerator* code_gen){
  53. // Operands: the banks of R, G and B (the third nibble is always zero), the type, R, G, B
  54. // and the duration in frames.
  55. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  56. const auto& r = FieldCodeGenerator::FormatValueOrVariable(
  57. cg->GetFormatter(), params_[0]->GetUnsigned(), params_[5]->GetUnsigned()
  58. );
  59. const auto& g = FieldCodeGenerator::FormatValueOrVariable(
  60. cg->GetFormatter(), params_[1]->GetUnsigned(), params_[6]->GetUnsigned()
  61. );
  62. const auto& b = FieldCodeGenerator::FormatValueOrVariable(
  63. cg->GetFormatter(), params_[3]->GetUnsigned(), params_[7]->GetUnsigned()
  64. );
  65. code_gen->AddOutputLine((
  66. boost::format("screen_fade:nfade(%1%, %2%, %3%, %4%, %5%)")
  67. % params_[4]->GetUnsigned() % r % g % b % params_[8]->GetUnsigned()
  68. ).str());
  69. }
  70. void FieldCameraInstruction::ProcessSHAKE(CodeGenerator* code_gen){
  71. // Operands: two banks for the amplitudes (always zero in the game data), the type, and the
  72. // amplitude and frames between bounces of each axis.
  73. if (params_[0]->GetUnsigned() != 0 || params_[1]->GetUnsigned() != 0){
  74. code_gen->WriteTodo("FieldCameraInstruction", "SHAKE with variable parameters");
  75. return;
  76. }
  77. code_gen->AddOutputLine((
  78. boost::format("background2d:shake(%1%, %2%, %3%, %4%, %5%)")
  79. % params_[2]->GetUnsigned() % params_[3]->GetUnsigned() % params_[4]->GetUnsigned()
  80. % params_[5]->GetUnsigned() % params_[6]->GetUnsigned()
  81. ).str());
  82. }
  83. void FieldCameraInstruction::ProcessSCRLC(CodeGenerator* code_gen){
  84. std::string mode;
  85. switch (params_[3]->GetUnsigned()){
  86. case 2: mode = "Background2D.LINEAR"; break;
  87. case 3: mode = "Background2D.SMOOTH"; break;
  88. default:
  89. // The other types don't move the camera, nor change what it is following.
  90. code_gen->AddOutputLine("-- SCRLC without effect");
  91. return;
  92. }
  93. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  94. const auto& seconds = FieldCodeGenerator::FormatValueOrVariable(
  95. cg->GetFormatter(), params_[0]->GetUnsigned(), params_[1]->GetUnsigned(),
  96. FieldCodeGenerator::ValueType::Float, 30.0f
  97. );
  98. code_gen->AddOutputLine("background2d:scroll_to_player(" + mode + ", " + seconds + ")");
  99. }
  100. void FieldCameraInstruction::ProcessSCRLA(CodeGenerator* code_gen, const FieldEngine& engine){
  101. std::string mode;
  102. switch (params_[4]->GetUnsigned()){
  103. case 0:
  104. code_gen->AddOutputLine("-- SCRLA without effect");
  105. return;
  106. case 1: mode = "Background2D.NONE"; break;
  107. case 2: mode = "Background2D.LINEAR"; break;
  108. case 3: mode = "Background2D.SMOOTH"; break;
  109. default:
  110. code_gen->WriteTodo("FieldCameraInstruction", "SCRLA with unsupported scroll type");
  111. return;
  112. }
  113. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  114. const auto& seconds = FieldCodeGenerator::FormatValueOrVariable(
  115. cg->GetFormatter(), params_[1]->GetUnsigned(), params_[2]->GetUnsigned(),
  116. FieldCodeGenerator::ValueType::Float, 30.0f
  117. );
  118. const auto& entity = engine.EntityByIndex(params_[3]->GetUnsigned());
  119. code_gen->AddOutputLine((
  120. boost::format("background2d:scroll_to_entity(entity_manager:get_entity(\"%1%\"), %2%, %3%)")
  121. % entity.GetName() % mode % seconds
  122. ).str());
  123. }
  124. void FieldCameraInstruction::ProcessSCR2D(CodeGenerator* code_gen){
  125. // kUpScaler.
  126. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  127. const auto& x = FieldCodeGenerator::FormatValueOrVariable(
  128. cg->GetFormatter(), params_[0]->GetUnsigned(), params_[2]->GetSigned()
  129. );
  130. const auto& y = FieldCodeGenerator::FormatValueOrVariable(
  131. cg->GetFormatter(), params_[1]->GetUnsigned(), params_[3]->GetSigned()
  132. );
  133. code_gen->AddOutputLine((
  134. boost::format("background2d:scroll_to_position(%1% * 3, %2% * 3, Background2D.NONE, 0)")
  135. % x % y
  136. ).str());
  137. }
  138. void FieldCameraInstruction::ProcessSCRCC(CodeGenerator* code_gen){
  139. code_gen->AddOutputLine("background2d:scroll_to_player(Background2D.NONE, 0)");
  140. }
  141. void FieldCameraInstruction::ProcessSCR2DL(CodeGenerator* code_gen){
  142. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  143. const auto& x = FieldCodeGenerator::FormatValueOrVariable(
  144. cg->GetFormatter(), params_[0]->GetUnsigned(), params_[4]->GetSigned()
  145. );
  146. const auto& y = FieldCodeGenerator::FormatValueOrVariable(
  147. cg->GetFormatter(), params_[1]->GetUnsigned(), params_[5]->GetSigned()
  148. );
  149. const auto& speed = FieldCodeGenerator::FormatValueOrVariable(
  150. cg->GetFormatter(), params_[3]->GetUnsigned(), params_[6]->GetUnsigned(),
  151. FieldCodeGenerator::ValueType::Float, 30.0f
  152. );
  153. code_gen->AddOutputLine((
  154. boost::format("background2d:scroll_to_position(%1% * 3, %2% * 3, Background2D.LINEAR, %3%)")
  155. % x % y % speed
  156. ).str());
  157. }
  158. void FieldCameraInstruction::ProcessSCR2DC(CodeGenerator* code_gen){
  159. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  160. const auto& x = FieldCodeGenerator::FormatValueOrVariable(
  161. cg->GetFormatter(), params_[0]->GetUnsigned(), params_[4]->GetSigned()
  162. );
  163. const auto& y = FieldCodeGenerator::FormatValueOrVariable(
  164. cg->GetFormatter(), params_[1]->GetUnsigned(), params_[5]->GetSigned()
  165. );
  166. const auto& speed = FieldCodeGenerator::FormatValueOrVariable(
  167. cg->GetFormatter(), params_[3]->GetUnsigned(), params_[6]->GetUnsigned(),
  168. FieldCodeGenerator::ValueType::Float, 30.0f
  169. );
  170. code_gen->AddOutputLine((
  171. boost::format("background2d:scroll_to_position(%1% * 3, %2% * 3, Background2D.SMOOTH, %3%)")
  172. % x % y % speed
  173. ).str());
  174. }
  175. void FieldCameraInstruction::ProcessVWOFT(CodeGenerator* code_gen){
  176. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  177. const auto& y = FieldCodeGenerator::FormatValueOrVariable(
  178. cg->GetFormatter(), params_[0]->GetUnsigned(), params_[2]->GetSigned()
  179. );
  180. const auto& x = FieldCodeGenerator::FormatValueOrVariable(
  181. cg->GetFormatter(), params_[1]->GetUnsigned(), params_[3]->GetSigned()
  182. );
  183. const auto& speed = params_[4]->GetUnsigned();
  184. code_gen->AddOutputLine((
  185. boost::format("background2d:offset(%1%, %2%) -- Speed %3%")
  186. % x % y % speed
  187. ).str());
  188. }
  189. void FieldCameraInstruction::ProcessFADE(CodeGenerator* code_gen){
  190. // Operands: the banks of R, G and B (the third nibble is always zero), R, G, B, the speed,
  191. // the type and the adjust, which doesn't change what is seen.
  192. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  193. const auto& r = FieldCodeGenerator::FormatValueOrVariable(
  194. cg->GetFormatter(), params_[0]->GetUnsigned(), params_[4]->GetUnsigned()
  195. );
  196. const auto& g = FieldCodeGenerator::FormatValueOrVariable(
  197. cg->GetFormatter(), params_[1]->GetUnsigned(), params_[5]->GetUnsigned()
  198. );
  199. const auto& b = FieldCodeGenerator::FormatValueOrVariable(
  200. cg->GetFormatter(), params_[3]->GetUnsigned(), params_[6]->GetUnsigned()
  201. );
  202. code_gen->AddOutputLine((
  203. boost::format("screen_fade:fade(%1%, %2%, %3%, %4%, %5%)")
  204. % params_[8]->GetUnsigned() % r % g % b % params_[7]->GetUnsigned()
  205. ).str());
  206. }