ff7_field_engine.h 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  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. #pragma once
  19. #include "decompiler/decompiler_engine.h"
  20. #include <string>
  21. #include <vector>
  22. #include "decompiler/sudm.h"
  23. //namespace FF7{
  24. /**
  25. * Represents the FF7 Field engine.
  26. */
  27. class FF7FieldEngine : public Engine{
  28. public:
  29. /**
  30. * FF7FieldEngine constructor.
  31. *
  32. * Generates a FF7FieldEngine.
  33. *
  34. * @param formatter[in] The formatter to be used by the engine.
  35. * @param scriptName The script name.
  36. */
  37. FF7FieldEngine(SUDM::IScriptFormatter& formatter, std::string scriptName) :
  38. mFormatter(formatter), mScriptName(scriptName)
  39. {setOutputStackEffect(false);}
  40. /**
  41. * Destructor.
  42. */
  43. FF7FieldEngine(const FF7FieldEngine&) = delete;
  44. /**
  45. * Destructor.
  46. */
  47. FF7FieldEngine& operator = (const FF7FieldEngine&) = delete;
  48. /**
  49. * Represents an entity.
  50. *
  51. * An entity can be almost anything in a field map: the playable
  52. * character, an NPC, an item, a line...
  53. */
  54. class Entity{
  55. public:
  56. /**
  57. * Entity constructor.
  58. *
  59. * It doesn't initialize any of the fields.
  60. */
  61. Entity() = default;
  62. /**
  63. * Entity constructor.
  64. *
  65. * Instantiates an entity with a name.
  66. *
  67. * @param name[in] Entity name.
  68. */
  69. Entity(const std::string& name, size_t index):
  70. mName(name), index_(index), is_line_(false)
  71. {}
  72. /**
  73. * Retrieves the entity name.
  74. *
  75. * @return The entity name.
  76. */
  77. std::string Name() const{return mName;}
  78. /**
  79. * Retrieves the entity index.
  80. *
  81. * The index is the one at which appears in the original
  82. * game script.
  83. *
  84. * @return The entity index.
  85. */
  86. size_t GetIndex() const{return index_;}
  87. /**
  88. * Retrieves a function.
  89. *
  90. * Retrieves the name of a function from it's index.
  91. *
  92. * @param index[in] Function index.
  93. * @return Function name.
  94. * @throws InternalDecompilerError if there is no function
  95. * with the specified index.
  96. * @todo What is a function here? An Opcode?
  97. */
  98. std::string FunctionByIndex(size_t index) const{
  99. auto it = mFunctions.find(index);
  100. if (it == std::end(mFunctions)) throw InternalDecompilerError();
  101. return it->second;
  102. }
  103. /**
  104. * Adds a function to the engine.
  105. *
  106. * Must be added by name and index.
  107. *
  108. * @param name[in] Function name. If the entity is a line,
  109. * the name will be overridden.
  110. * @param index[in] Function index.
  111. * @todo What is a function here? An Opcode?
  112. */
  113. void AddFunction(const std::string& name, size_t index){
  114. mFunctions[index] = name;
  115. }
  116. /**
  117. * Marks the entity as a line.
  118. *
  119. * @param line[in] True to mark the entity as a line,
  120. * false to unmark it.
  121. * @param point_a[in] First point of the line. Can be null
  122. * if line is false.
  123. * @param point_b[in] Second point of the line. Can be
  124. * null if line is false.
  125. */
  126. void MarkAsLine(
  127. bool line, std::vector<float> point_a, std::vector<float> point_b
  128. ){
  129. is_line_ = line;
  130. point_a_.clear();
  131. point_b_.clear();
  132. if (line){
  133. if (point_a.size() >= 3 && point_b.size() >= 3){
  134. point_a_.push_back(point_a[0]);
  135. point_a_.push_back(point_a[1]);
  136. point_a_.push_back(point_a[2]);
  137. point_b_.push_back(point_b[0]);
  138. point_b_.push_back(point_b[1]);
  139. point_b_.push_back(point_b[2]);
  140. }
  141. // TODO: Notify on else.
  142. }
  143. // TODO: These are not getting to the final script.
  144. // Maybe this can be removed?
  145. AddFunction("on_enter_line", 1);
  146. AddFunction("on_move_to_line", 2);
  147. AddFunction("on_cross_line", 3);
  148. AddFunction("on_leave_line", 4);
  149. }
  150. /**
  151. * Checks if the entity is a line.
  152. *
  153. * Note that an entity is not considered to be a line
  154. * until a function has been found containing the opcode
  155. * LINE and {@see MarkAsLine} has been called.
  156. *
  157. * @return true if the entity is a line.
  158. */
  159. bool IsLine(){return is_line_;}
  160. /**
  161. * Retrieves the first point of the line entity.
  162. *
  163. * If the entity is not a line, the behavior is undefined.
  164. *
  165. * @return The first point of the line entity.
  166. */
  167. std::vector<float> GetLinePointA(){return point_a_;}
  168. /**
  169. * Retrieves the second point of the line entity.
  170. *
  171. * If the entity is not a line, the behavior is undefined.
  172. *
  173. * @return The second point of the line entity.
  174. */
  175. std::vector<float> GetLinePointB(){return point_b_;}
  176. private:
  177. /**
  178. * Entity name.
  179. */
  180. std::string mName;
  181. /**
  182. * Entity index.
  183. */
  184. size_t index_;
  185. /**
  186. * Function list.
  187. * @todo What is a function here? An Opcode?
  188. */
  189. std::map< size_t, std::string > mFunctions;
  190. /**
  191. * Indicates if the entity is a line.
  192. */
  193. bool is_line_;
  194. /**
  195. * The first point of a line entity.
  196. *
  197. * If the entity is not a line, it may not be initializer.
  198. */
  199. std::vector<float> point_a_;
  200. /**
  201. * The second point of a line entity.
  202. *
  203. * If the entity is not a line, it may not be initializer.
  204. */
  205. std::vector<float> point_b_;
  206. };
  207. /**
  208. * Retrieves the dissasembler.
  209. *
  210. * @param insts[in] List of instructions.
  211. * @param raw_script_data[in] Script data, raw format.
  212. * @return Pointer to the dissasembler.
  213. * @todo Understand and document properly.
  214. */
  215. virtual std::unique_ptr<Disassembler> getDisassembler(
  216. InstVec &insts, const std::vector<unsigned char>& raw_script_data
  217. ) override;
  218. /**
  219. * Retrieves the dissasembler.
  220. *
  221. * @param insts[in] List of instructions.
  222. * @return Pointer to the dissasembler.
  223. * @todo Understand and document properly.
  224. */
  225. virtual std::unique_ptr<Disassembler> getDisassembler(InstVec &insts) override;
  226. /**
  227. * Retrieves the code generator.
  228. *
  229. * @param insts[in] List of instructions.
  230. * @param output[in] Pointer to the output (file, stream...).
  231. * @return Pointer to the generator.
  232. * @todo Understand and document properly.
  233. */
  234. virtual std::unique_ptr<CodeGenerator> getCodeGenerator(
  235. const InstVec& insts, std::ostream &output
  236. ) override;
  237. /**
  238. * Postprocessing actions to apply to the scripts.
  239. *
  240. * @param insts[in] Instruction list.
  241. * @param g[in] Code graph.
  242. * @todo Understand and document properly.
  243. * @todo What is the graph used to?
  244. */
  245. virtual void postCFG(InstVec &insts, Graph g) override;
  246. /**
  247. * Indicates if instructions are purely grouped.
  248. *
  249. * @return True if instructions are purely grouped.
  250. * @todo What is pure grouping?
  251. */
  252. virtual bool usePureGrouping() const override{return false;}
  253. /**
  254. * Retrieves all entities in the map.
  255. *
  256. * @return A map of entities, with the name and index.
  257. */
  258. std::map<std::string, int> GetEntities() const;
  259. /**
  260. * Retrieves all non-line entities in the map.
  261. *
  262. * @return A list of non-line entities.
  263. */
  264. std::vector<SUDM::Field::FieldEntity> GetEntityList() const;
  265. /**
  266. * Retrieves all line entities in the map.
  267. *
  268. * @param A list of line entities.
  269. */
  270. std::vector<SUDM::Field::Line> GetLineList() const;
  271. /**
  272. * Retrieves all entities in the map.
  273. *
  274. * @return A map of entities, with the name and index.
  275. */
  276. std::map<size_t, Entity> GetEntityIndexMap() const{return mEntityIndexMap;}
  277. /**
  278. * Adds a function to an entity.
  279. *
  280. * @param entity_name Name of the entity.
  281. * @param entity_index Index of the entity.
  282. * @param func_name Name of the function.
  283. * @param func_index Index of the function.
  284. */
  285. void AddEntityFunction(
  286. const std::string& entity_name, size_t entity_index,
  287. const std::string& func_name, size_t func_index
  288. );
  289. /**
  290. * Marks an entity as a line.
  291. *
  292. * @param entity_index Index of the entity.
  293. * @param line[in] True to mark the entity as a line, false to
  294. * unmark it.
  295. * @param point_a[in] First point of the line. Can be null if line
  296. * is false.
  297. * @param point_b[in] Second point of the line. Can be null if
  298. * line is false.
  299. */
  300. void MarkEntityAsLine(
  301. size_t entity_index, bool line,
  302. std::vector<float> point_a, std::vector<float> point_b
  303. );
  304. /**
  305. * Checks if an entity has been marked as a line.
  306. *
  307. * @param entity_index[in] Index of the entity to check.
  308. * @return True if the entity is a line. False if it isn't, or if
  309. * there is no such entity.
  310. */
  311. bool EntityIsLine(size_t entity_index);
  312. /**
  313. * Retrieves an entity.
  314. *
  315. * @param index[in] Index of the entity to retrieve.
  316. * @throws InternalDecompilerError if there is no entity at the
  317. * specified index.
  318. */
  319. const Entity& EntityByIndex(size_t index) const{
  320. auto it = mEntityIndexMap.find(index);
  321. if (it == std::end(mEntityIndexMap)) throw InternalDecompilerError();
  322. return it->second;
  323. }
  324. /**
  325. * Retrieves the scale factor for the map.
  326. *
  327. * @return Map scale factor.
  328. */
  329. float GetScaleFactor() const {return scale_factor_;}
  330. /**
  331. * Retrieves the script name.
  332. *
  333. * @return The script name.
  334. */
  335. const std::string& ScriptName() const {return mScriptName;}
  336. private:
  337. /**
  338. * Removes extraneous return statements.
  339. *
  340. * Usefull for scripts that only contain one one return
  341. * statement.
  342. *
  343. * @param insts[in|out] List of instructions to proccess.
  344. * Extraneous return statements will be deleted from the
  345. * instructions.
  346. * @param g[in] Code graph. Unused.
  347. */
  348. void RemoveExtraneousReturnStatements(InstVec& insts, Graph g);
  349. /**
  350. * Removes trailing infinite loops.
  351. *
  352. * In FF7 some scripts ends with an infinite loop to keep it
  353. * alive. in VGears this isn't required, and can cause infinite
  354. * loops, so they can be removed.
  355. *
  356. * @param insts[in|out] List of instructions to proccess. Trailing
  357. * infinite loops will be deleted from the instructions.
  358. * @param g[in] Code graph.
  359. * @todo What is the graph used to?
  360. */
  361. void RemoveTrailingInfiniteLoops(InstVec& insts, Graph g);
  362. /**
  363. * Tries to detect scripts with trailing infinite loops.
  364. *
  365. * In FF7 some scripts ends with an infinite loop to keep it
  366. * alive. in VGears this isn't required, and can cause infinite
  367. * loops, so they can be removed. This function marks them, so
  368. * they can be deleted with
  369. * @{see FF7FieldEngine::RemoveTrailingInfiniteLoops}.
  370. */
  371. void MarkInfiniteLoopGroups(InstVec& insts, Graph g);
  372. /**
  373. * The script formatter.
  374. */
  375. SUDM::IScriptFormatter& mFormatter;
  376. /**
  377. * The entity index map for the field.
  378. */
  379. std::map<size_t, Entity> mEntityIndexMap;
  380. /**
  381. * The map scale factor.
  382. */
  383. float scale_factor_ = 1.0f;
  384. /**
  385. * The script name.
  386. */
  387. std::string mScriptName;
  388. };
  389. /**
  390. * An unconditional map jump instruction.
  391. */
  392. class FF7UncondJumpInstruction : public UncondJumpInstruction{
  393. public:
  394. // TODO: Make private and add accessors.
  395. /**
  396. * Whether or not this is really a call to a script function.
  397. */
  398. bool _isCall;
  399. /**
  400. * Constructor.
  401. */
  402. FF7UncondJumpInstruction() : _isCall(false) {}
  403. /**
  404. * Indicates if the instruction is a function call.
  405. *
  406. * @return true if the instruction is a function call, false if not.
  407. */
  408. virtual bool isFuncCall() const;
  409. /**
  410. * Indicates if the instruction is an unconditional jump.
  411. *
  412. * @return true if the instruction is an unconditional jump, false
  413. * if it's not.
  414. */
  415. virtual bool isUncondJump() const;
  416. /**
  417. * Retrieves the destination address of the jump.
  418. *
  419. * @return The offset (number of bytes) to jump from the beginning
  420. * of the instruction.
  421. */
  422. virtual uint32 getDestAddress() const;
  423. /**
  424. * Processes the instruction.
  425. *
  426. * @param func[in] Function to process.
  427. * @param stack[out] Function stack.
  428. * @param engine[in] Engine.
  429. * @param code_gen[in] Code generator.
  430. * @todo Func and engine are unused?
  431. * @todo Understand and document properly.
  432. */
  433. virtual void processInst(
  434. Function& func, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  435. ) override;
  436. /**
  437. * Prints the instruction
  438. *
  439. * @param output The stram to print the instruction to.
  440. * @todo Understand and document properly.
  441. */
  442. virtual std::ostream& print(std::ostream &output) const override;
  443. };
  444. /**
  445. * A conditional map jump instruction.
  446. */
  447. class FF7CondJumpInstruction : public CondJumpInstruction{
  448. public:
  449. /**
  450. * Processes a conditional jump instruction.
  451. *
  452. * Checks if the condition is a function or a comparison, and
  453. * adds the function to the stack.
  454. *
  455. * @param function[in] Function to process. Unused.
  456. * @param stack[out] Function stack. The Instruction will be added
  457. * here.
  458. * @param engine[in] Engine. Unused.
  459. * @param code_gen[in] Code generator.
  460. */
  461. virtual void processInst(
  462. Function& function, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  463. ) override;
  464. /**
  465. * Retrieves the destination address of the jump.
  466. *
  467. * @return The offset (number of bytes) to jump from the beginning
  468. * of the instruction.
  469. */
  470. virtual uint32 getDestAddress() const override;
  471. /**
  472. * Prints the instruction
  473. *
  474. * @param output The stram to print the instruction to.
  475. * @todo Understand and document properly.
  476. */
  477. virtual std::ostream& print(std::ostream &output) const override;
  478. };
  479. /**
  480. * A script flow control instruction.
  481. */
  482. class FF7ControlFlowInstruction : public KernelCallInstruction{
  483. public:
  484. /**
  485. * Create a FF7ControlFlowInstruction.
  486. *
  487. * @return Pointer to the newly created instruction.
  488. */
  489. static InstPtr Create(){return new FF7ControlFlowInstruction();}
  490. /**
  491. * Processes the instruction.
  492. *
  493. * @param func[in] Function to process.
  494. * @param stack[out] Function stack.
  495. * @param engine[in] Engine.
  496. * @param code_gen[in] Code generator.
  497. */
  498. virtual void processInst(
  499. Function& func, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  500. ) override;
  501. private:
  502. /**
  503. * Processes a REQ command.
  504. *
  505. * Opcode: 0x01
  506. * Short name: REQ
  507. * Long name: Request remote execution (asynchronous,
  508. * non-guaranteed)
  509. *
  510. * Memory layout
  511. * 0x01
  512. * E
  513. * P/F
  514. *
  515. * Arguments
  516. *
  517. * const UByte E: The ID of the target entity.
  518. * const Bit[3] P: The priority at which we want to execute the
  519. * remote script (high 3 bits of byte).
  520. * const Bit[5] F: The ID of the specific member function of E to
  521. * be executed (low 5 bits of byte).
  522. *
  523. * Requests that a remote entity executes one of its member
  524. * functions at a specified priority. The request is asynchronous
  525. * and returns immediately without waiting for the remote
  526. * execution to start or finish. If the specified priority is
  527. * already busy executing, the request will fail silently.
  528. *
  529. * @param codegen[in|out] Code generator. Output lines are
  530. * appended to it.
  531. * @param engine[in] The engine instance to fetch entities.
  532. */
  533. void processREQ(CodeGenerator* code_gen, const FF7FieldEngine& engine);
  534. /**
  535. * Processes a REQSW command.
  536. *
  537. * Opcode: 0x02
  538. * Short name: REQSW
  539. * Long name: Request remote execution (asynchronous execution,
  540. * guaranteed)
  541. *
  542. * Memory layout
  543. * 0x02
  544. * E
  545. * P/F
  546. *
  547. * Arguments
  548. *
  549. * const UByte E: The ID of the target entity.
  550. * const Bit[3] P: The priority at which we want to execute the
  551. * remote script (high 3 bits of byte).
  552. * const Bit[5] F: The ID of the specific member function of E to
  553. * be executed (low 5 bits of byte).
  554. *
  555. * Requests that a remote entity executes one of its member
  556. * functions at a specified priority. If the specified priority is
  557. * already busy executing, the request will block until it becomes
  558. * available and only then return. The remote execution is still
  559. * carried out asynchronously, with no notification of completion.
  560. *
  561. * @param codegen[in|out] Code generator. Output lines are
  562. * appended to it.
  563. * @param engine[in] The engine instance to fetch entities.
  564. */
  565. void processREQSW(CodeGenerator* code_gen, const FF7FieldEngine& engine);
  566. /**
  567. * Processes a REQEW command.
  568. *
  569. * Opcode: 0x03
  570. * Short name: REQEW
  571. * Long name: Request remote execution (synchronous, guaranteed)
  572. *
  573. * Memory layout
  574. * 0x03
  575. * E
  576. * P/F
  577. *
  578. * Arguments
  579. *
  580. * const UByte E: The ID of the target entity.
  581. * const Bit[3] P: The priority at which we want to execute the
  582. * remote script (high 3 bits of byte).
  583. * const Bit[5] F: The ID of the specific member function of E to
  584. * be executed (low 5 bits of byte).
  585. *
  586. * Requests that a remote entity executes one of its member
  587. * functions at a specified priority. The request will block until
  588. * remote execution has finished before returning.
  589. *
  590. * @param codegen[in|out] Code generator. Output lines are
  591. * appended to it.
  592. * @param engine[in] The engine instance to fetch entities.
  593. */
  594. void processREQEW(CodeGenerator* code_gen, const FF7FieldEngine& engine);
  595. /**
  596. * Processes a RETTO command.
  597. *
  598. * Opcode: 0x07
  599. * Short name: RETTO
  600. * Long name: Return To
  601. *
  602. * Memory layout
  603. * 0x07
  604. * P/F
  605. *
  606. * Arguments
  607. * const Bit[3] P: The priority at which we want to execute the
  608. * remote script (high 3 bits of byte).
  609. * const Bit[5] F: The ID of the specific member function of the
  610. * current entity to be executed to (low 5 bits
  611. * of byte).
  612. *
  613. * Stops the active script loop for this entity and also any
  614. * script loops (except the main) that are queuing to be executed
  615. * after the current script. This is essentially the same as
  616. * adding a RET onto each of the active / queued scripts next
  617. * execution position and returning the current op index to index
  618. * for each script. Then the script control is passed to the
  619. * script F within the current entity with the priority P.
  620. *
  621. * @param codegen[in|out] Code generator. Output lines are
  622. * appended to it.
  623. */
  624. void processRETTO(CodeGenerator* code_gen);
  625. /**
  626. * Processes a WAIT command.
  627. *
  628. * Opcode: 0x24
  629. * Short name: WAIT
  630. * Long name: Wait
  631. *
  632. * Memory layout
  633. * 0x24
  634. * A
  635. *
  636. * Arguments
  637. * const UShort A: Amount (number of frames) to wait.
  638. *
  639. * Pauses current script execution for a specific amount of time.
  640. * Rather than a specific time value in milliseconds/seconds,
  641. * the amount specifies the number of frames that must be drawn
  642. * before execution resumes. Since the game runs at 30fps,
  643. * WAIT(0x1E) (or WAIT(30) in decimal) will pause script execution
  644. * for 1 second, WAIT(0x96) will pause for 5 seconds, and so on.
  645. *
  646. * @param codegen[in|out] Code generator. Output lines are
  647. * appended to it.
  648. */
  649. void processWAIT(CodeGenerator* code_gen);
  650. };
  651. /**
  652. * A module instruction.
  653. */
  654. class FF7ModuleInstruction : public KernelCallInstruction{
  655. public:
  656. /**
  657. * Processes the instruction.
  658. *
  659. * @param func[in] Function to process.
  660. * @param stack[out] Function stack.
  661. * @param engine[in] Engine. Unused.
  662. * @param code_gen[in] Code generator.
  663. */
  664. virtual void processInst(
  665. Function& func, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  666. ) override;
  667. private:
  668. /**
  669. * Processes a BATTLE opcode.
  670. *
  671. * Opcode: 0x70
  672. * Short name: BATTLE
  673. * Long name: Start battle
  674. *
  675. * Memory layout
  676. * 0x70
  677. * B
  678. * N
  679. * N
  680. *
  681. * Arguments
  682. * const UByte B: Bank (16-bit) to retrieve the address of the
  683. * battle ID, or zero if it is given as a literal
  684. * value.
  685. * const UWord N: Battle ID, or address to find ID if B is
  686. * non-zero.
  687. *
  688. * This launches the battle module with whatever battle number is
  689. * used in the argument, or the value retrieved from memory location
  690. * N if B is non-zero. Battle 1, 2, and 999 (0x03E7) are debug
  691. * battles.
  692. *
  693. * @param codegen[in|out] Code generator. Output lines are appended
  694. * to it.
  695. */
  696. void processBATTLE(CodeGenerator* code_gen);
  697. /**
  698. * Processes a BTLON opcode.
  699. *
  700. * Opcode: 0x71
  701. * Short name: BTLON
  702. * Long name: Battle switch
  703. *
  704. * Memory layout
  705. * 0x71
  706. * S
  707. *
  708. * Arguments
  709. * const UByte S: Switch battles on/off (0/1, respectively).
  710. *
  711. * Turns random encounters on or off for this field. Note that if
  712. * a field does not have any Encounter Data set in its field file,
  713. * battles will not occur regardless of the argument passed with
  714. * this opcode.
  715. *
  716. * @param codegen[in|out] Code generator. Output lines are
  717. * appended to it.
  718. */
  719. void processBTLON(CodeGenerator* code_gen);
  720. /**
  721. * Processes a MAPJUMP opcode.
  722. *
  723. * Opcode: 0x60
  724. * Short name: BTLON
  725. * Long name: Change Field
  726. *
  727. * Memory layout
  728. * 0x60
  729. * I
  730. * I
  731. * X
  732. * X
  733. * Y
  734. * Y
  735. * Z
  736. * Z
  737. * D
  738. *
  739. * Arguments
  740. * const UShort I: Field ID of the map to jump to.
  741. * const Short X: X-coordinate of the player on the next field.
  742. * const Short Y: Y-coordinate of the player on the next field.
  743. * const Short Z: Z-coordinate of the player on the next field.
  744. * const UByte D: Direction the character will be facing on the
  745. * next field, in the standard game format.
  746. *
  747. * Switches fields to the one indicated by I, and places the
  748. * character at the coordinates and direction specified. This is
  749. * an alternative to using a gateway, and can complement their
  750. * usage as it allows for more than 12 gateways by simulating
  751. * their behavior through a LINE which, when crossed, executes a
  752. * MAPJUMP.
  753. *
  754. * @param codegen[in|out] Code generator. Output lines are
  755. * appended to it.
  756. * @param func[in] Function
  757. * @todo What is func for?
  758. */
  759. void processMAPJUMP(CodeGenerator* code_gen, Function& func);
  760. };
  761. /**
  762. * A math instruction.
  763. */
  764. class FF7MathInstruction : public StoreInstruction{
  765. public:
  766. /**
  767. * Processes the instruction.
  768. *
  769. * @param func[in] Function to process.
  770. * @param stack[out] Function stack.
  771. * @param engine[in] Engine. Unused.
  772. * @param code_gen[in] Code generator.
  773. */
  774. virtual void processInst(
  775. Function& func, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  776. ) override;
  777. private:
  778. void processSaturatedPLUS(CodeGenerator* code_gen);
  779. void processSaturatedPLUS2(CodeGenerator* code_gen);
  780. void processSaturatedMINUS(CodeGenerator* code_gen);
  781. void processSaturatedMINUS2(CodeGenerator* code_gen);
  782. void processSaturatedINC(CodeGenerator* code_gen);
  783. void processSaturatedINC2(CodeGenerator* code_gen);
  784. void processSaturatedDEC(CodeGenerator* code_gen);
  785. void processSaturatedDEC2(CodeGenerator* code_gen);
  786. void processRDMSD(CodeGenerator* code_gen);
  787. void processSETBYTE_SETWORD(CodeGenerator* code_gen);
  788. /**
  789. * Processes a BITON opcode.
  790. *
  791. * Opcode: 0x82
  792. * Short name: BITON
  793. * Long name: Set Bit
  794. *
  795. * Memory layout (4 bytes)
  796. * |0x82|D/S|A|B|
  797. *
  798. * Arguments
  799. * - const Bit[4] D: Destination bank.
  800. * - const Bit[4] S: Source bank.
  801. * - const UByte A: Destination address.
  802. * - const UByte Bit: The number of the bit to turn on.
  803. *
  804. * Sets the nth bit in the "A" location, where n is a number
  805. * between 0-7 supplied in B. A value of zero in B will set the
  806. * least significant bit. If the Source Bank is 0 then the bit to
  807. * be set is taken from "Bit". If the Source Bank is an 8 bit
  808. * bank, then the bit is the address in that bank where the
  809. * operand is.
  810. *
  811. * @param code_gen[in|out] Code generator. Output lines are
  812. * appended to it.
  813. */
  814. void processBITON(CodeGenerator* code_gen);
  815. /**
  816. * Processes a BITON opcode.
  817. *
  818. * Opcode: 0x83
  819. * Short name: BITOFF
  820. * Long name: Reset Bit
  821. *
  822. * Memory layout (4 bytes)
  823. * |0x83|D/S|A|B|
  824. *
  825. * Arguments
  826. * - const Bit[4] D: Destination bank.
  827. * - const Bit[4] S: Source bank.
  828. * - const UByte A: Destination address.
  829. * - const UByte Bit: The number of the bit to turn off.
  830. *
  831. * Sets the nth bit in the "A" location, where n is a number
  832. * between 0-7 supplied in B. A value of zero in B will reset the
  833. * least significant bit. If the Source Bank is 0 then the bit to
  834. * be set is taken from "Bit". If the Source Bank is an 8 bit
  835. * bank, then the bit is the address in that bank where the
  836. * operand is.
  837. *
  838. * @param code_gen[in|out] Code generator. Output lines are
  839. * appended to it.
  840. */
  841. void processBITOFF(CodeGenerator* code_gen);
  842. void processPLUSx_MINUSx(CodeGenerator* code_gen, const std::string& op);
  843. void processINCx_DECx(CodeGenerator* code_gen, const std::string& op);
  844. void processRANDOM(CodeGenerator* code_gen);
  845. };
  846. /**
  847. * A window instruction.
  848. */
  849. class FF7WindowInstruction : public KernelCallInstruction{
  850. public:
  851. /**
  852. * Processes the instruction.
  853. *
  854. * @param func[in] Function to process.
  855. * @param stack[out] Function stack.
  856. * @param engine[in] Engine.
  857. * @param code_gen[in] Code generator.
  858. */
  859. virtual void processInst(
  860. Function& func, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  861. ) override;
  862. private:
  863. void processMESSAGE(CodeGenerator* code_gen, const std::string& script_name);
  864. void processMPNAM(CodeGenerator* code_gen);
  865. void processMENU2(CodeGenerator* code_gen);
  866. void processWINDOW(CodeGenerator* code_gen);
  867. void processWCLSE(CodeGenerator* code_gen);
  868. };
  869. /**
  870. * A party instruction
  871. */
  872. class FF7PartyInstruction : public KernelCallInstruction{
  873. public:
  874. /**
  875. * Processes the instruction.
  876. *
  877. * @param func[in] Function to process.
  878. * @param stack[out] Function stack.
  879. * @param engine[in] Engine. Unused
  880. * @param code_gen[in] Code generator.
  881. */
  882. virtual void processInst(
  883. Function& func, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  884. ) override;
  885. private:
  886. void processSTITM(CodeGenerator* code_gen);
  887. void processPRTYE(CodeGenerator* code_gen);
  888. };
  889. /**
  890. * A model instruction.
  891. */
  892. class FF7ModelInstruction : public KernelCallInstruction{
  893. public:
  894. /**
  895. * Processes the instruction.
  896. *
  897. * @param func[in] Function to process.
  898. * @param stack[out] Function stack.
  899. * @param engine[in] Engine.
  900. * @param code_gen[in] Code generator.
  901. */
  902. virtual void processInst(
  903. Function& func, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  904. ) override;
  905. private:
  906. /**
  907. * Processes a JOIN opcode.
  908. *
  909. * Opcode: 0x08
  910. * Short name: JOIN
  911. * Long name: Party Field Join
  912. * Memory layout (2 bytes)
  913. * |0x08|S|
  914. *
  915. * Arguments
  916. * - const UByte S: Speed that the characters join back together.
  917. *
  918. * Causes seperated party characters that have previously been
  919. * SPLIT onto the field, to be joined back together again; that
  920. * is, only the party leader becomes visible on the field. This
  921. * should be called if a previous SPLIT has completed (the party
  922. * members have finished speaking, or performing their actions,
  923. * for example). As with SPLIT, the speed of the join is
  924. * specified, from a scale of 1 (almost instant) to FF (very slow
  925. * walk), and must be non-zero. In contrast to most MOVE related
  926. * op codes, the speed is this setting is actually the total
  927. * number of frames required. Depending on the distance from the
  928. * player character and the number of frames required, the entity
  929. * plays a run or walk animation. Also, all characters take the
  930. * same time irrespective of distance. Calling JOIN without having
  931. * previously SPLIT the characters will cause the party members to
  932. * appear at the walkmesh origin and attempt to JOIN from there.
  933. * This is not normally the required behaviour and should be
  934. * avoided.
  935. *
  936. * @param code_gen The code generator.
  937. */
  938. void processJOIN(CodeGenerator* code_gen);
  939. /**
  940. * Processes a SPLIT opcode.
  941. *
  942. * Opcode: 0x09
  943. * Short name: SPLIT
  944. * Long name: Party Field Split
  945. *
  946. * Memory layout (15 bytes)
  947. * |0x20|B1/B2|B3/B4|B5/B6|XA|XA|YA|YA|DA|XB|XB|YB|YB|DB|S|
  948. *
  949. * Arguments
  950. * - const Bit[4] B1: Bank for XA, or zero if XA is specified as a
  951. * literal value.
  952. * - const Bit[4] B2: Bank for YA, or zero if YA is specified as a
  953. * literal value.
  954. * - const Bit[4] B3: Bank for DA, or zero if DA is specified as a
  955. * literal value.
  956. * - const Bit[4] B4: Bank for XB, or zero if XB is specified as a
  957. * literal value.
  958. * - const Bit[4] B5: Bank for YB, or zero if YB is specified as a
  959. * literal value.
  960. * - const Bit[4] B6: Bank for DB, or zero if DB is specified as a
  961. * literal value.
  962. * - const Short XA: X-coordinate of the second character in the
  963. * party after the split, or address for the value if B1 is
  964. * non-zero.
  965. * - const Short YA: Y-coordinate of the second character in the
  966. * party after the split, or address for the value if B2 is
  967. * non-zero.
  968. * - const UByte DA: Direction the second character faces after the
  969. * split, or address for the value if B3 is non-zero.
  970. * - const Short XB: X-coordinate of the third character in the
  971. * party after the split, or address for the value if B4 is
  972. * non-zero.
  973. * - const Short YB: Y-coordinate of the third character in the
  974. * party after the split, or address for the value if B5 is
  975. * non-zero.
  976. * - const UByte DB: Direction the third character faces after the
  977. * split, or address for the value if B6 is non-zero.
  978. * - const UByte S: Speed that the characters split.
  979. *
  980. * Causes the common 'split effect' whereby the second and third
  981. * characters in the current party 'come out' from the party
  982. * leader. That is, they become visible in the field, starting
  983. * from the center of the party leader, and move out to the
  984. * coordinates specified in the argument list. This is commonly
  985. * used when the other characters in the current party have an
  986. * action or dialog to perform and must be individually visible in
  987. * the field. As well as specifying final coordinates for the two
  988. * other party characters, the directions each character faces
  989. * after the split are specified as a byte, using the common
  990. * direction values found throughout the game. Speed is also given
  991. * and is used to specify the rate at which the characters leave
  992. * the party leader, using a scale from 1 (almost instant) to FF
  993. * (extremely slow walk); this must be non-zero. In contrast to
  994. * most MOVE related op codes, the speed is this setting is
  995. * actually the total number of frames required. Depending on the
  996. * distance from the player character and the number of frames
  997. * required, the entity plays a run or walk animation. Also, all
  998. * characters take the same time irrespective of distance.
  999. *
  1000. * @param code_gen The code generator.
  1001. */
  1002. void processSPLIT(CodeGenerator* code_gen);
  1003. void processTLKON(CodeGenerator* code_gen, const std::string& entity);
  1004. void processPC(CodeGenerator* code_gen, const std::string& entity);
  1005. void processCHAR(CodeGenerator* code_gen, const std::string& entity);
  1006. void processDFANM(CodeGenerator* code_gen, const std::string& entity, int char_id);
  1007. void processANIME1(CodeGenerator* code_gen, const std::string& entity, int char_id);
  1008. void processVISI(CodeGenerator* code_gen, const std::string& entity);
  1009. void processXYZI(CodeGenerator* code_gen, const std::string& entity);
  1010. void processMOVE(CodeGenerator* code_gen, const std::string& entity);
  1011. void processMSPED(CodeGenerator* code_gen, const std::string& entity);
  1012. void processDIR(CodeGenerator* code_gen, const std::string& entity);
  1013. void processTURNGEN(CodeGenerator* code_gen, const std::string& entity);
  1014. void processGETAI(CodeGenerator* code_gen, const FF7FieldEngine& engine);
  1015. void processANIM_2(CodeGenerator* code_gen, const std::string& entity, int char_id);
  1016. void processCANIM2(CodeGenerator* code_gen, const std::string& entity, int char_id);
  1017. void processCANM_2(CodeGenerator* code_gen, const std::string& entity, int char_id);
  1018. void processCC(CodeGenerator* code_gen, const FF7FieldEngine& engine);
  1019. /**
  1020. * Processes a JUMP opcode.
  1021. *
  1022. * Opcode: 0xC0
  1023. * Short name: JUMP
  1024. * Long name: Jump
  1025. *
  1026. * Memory layout (7 bytes)
  1027. * |0xC2|B1/B2|B3/B4|X|Y|I|Steps|
  1028. *
  1029. * Arguments
  1030. * - const Bit[4] B1: Bank to retrieve X-coordinate, or zero if
  1031. * specifying X as a literal value.
  1032. * - const Bit[4] B2: Bank to retrieve Y-coordinate, or zero if
  1033. * specifying Y as a literal value.
  1034. * - const Bit[4] B3: Bank to retrieve triangle ID, or zero if
  1035. * specifying Z as a literal value.
  1036. * - const Bit[4] B4: Bank to retrieve jump height, or zero if
  1037. * specifying H as a literal value.
  1038. * - const Short X: X-coordinate of the target to jump to, or
  1039. * lower byte specifying address if B1 is non-zero.
  1040. * - const Short Y: Y-coordinate of the target to jump to, or
  1041. * lower byte specifying address if B2 is non-zero.
  1042. * - const Short I: Triangle ID of the target to jump to, or
  1043. * lower byte specifying address if B3 is non-zero.
  1044. * - const UShort Steps: Steps in jump. Must be non-zero if a
  1045. * literal value. Alternatively, lower byte specifies address if
  1046. * B4 is non-zero.
  1047. *
  1048. * Causes the character to jump to the specified point and
  1049. * triangle ID, with the jump curve peaking at a height which is
  1050. * increased by using a larger value for the H argument. In
  1051. * addition, the larger the number, the longer the jump will take
  1052. * to complete. A "normal" value is around 0x15, 0x01 is fast and
  1053. * instantaneous; the argument must not be zero or the game will
  1054. * crash. Whilst this is an unsigned two-byte number, a large
  1055. * value (beyond around 0x60) will not only cause a vast jump
  1056. * height, but also cause the screen to scroll erratically (the
  1057. * larger the number, the more erratic).
  1058. * Main update function go through all entity with JUMP state and
  1059. * if stage is 0 it calculates final Z point according to triangle
  1060. * id. It sets current coords as start coords. The main thing this
  1061. * function does is set B coefficient for later calculation. It
  1062. * defines as follows:
  1063. * B = (Z_final - Z_start) / steps - steps * 1.45;
  1064. * Then it set current step to 0 and stage to 1. On next update
  1065. * other part of function works. It's calculate real position.
  1066. * First it increment current step number. Then it calculate X and
  1067. * Y. They change linear so nothing interesting here. The Z
  1068. * calculation is as follows:
  1069. * Z_current = - step^2 * 1.45 + step * B + Z_start;
  1070. * If current substep equal number of steps then we set current
  1071. * triangle to final triangle and set stage to 2. Which finalizes
  1072. * the routine on next opcode call. Neither animation nor sound is
  1073. * specified in this opcode. An animation is played by using an
  1074. * animation opcode such as DFANM, and a SOUND played, before the
  1075. * jump.
  1076. *
  1077. * @param code_gen[in] The code generator.
  1078. * @param entity[in] The name of the entity.
  1079. */
  1080. void processJUMP(CodeGenerator* code_gen, const std::string& entity);
  1081. /**
  1082. * Processes a AXYZI opcode
  1083. *
  1084. * Opcode: 0xC1
  1085. * Short name: AXYZI
  1086. * Long name: Entity Get Position
  1087. *
  1088. * Memory layout (8 bytes)
  1089. * |0xC1|B1/B2|B3/B4|A|X|Y|Z|I|
  1090. *
  1091. * Arguments
  1092. * - const Bit[4] B1: Bank to store X.
  1093. * - const Bit[4] B2: Bank to store Y.
  1094. * - const Bit[4] B3: Bank to store Z.
  1095. * - const Bit[4] B4: Bank to store I.
  1096. * - const UByte A: Entity ID whose field object will have its
  1097. * position retrieved from.
  1098. * - const UByte X: Address to store the X-coordinate.
  1099. * - const UByte Y: Address to store the Y-coordinate.
  1100. * - const UByte Z: Address to store the Z-coordinate.
  1101. * - const UByte I: Address to store the ID of the walkmesh
  1102. * triangle the object is standing on.
  1103. *
  1104. * Retrieves the coordinates of the field object that the entity,
  1105. * whose ID specified in A, is associated with. This opcode uses
  1106. * an entity ID, not a field object offset; as such, if an entity
  1107. * ID is given that does not have a field object, this opcode will
  1108. * store zero in each of the four address specified.
  1109. */
  1110. void processAXYZI(CodeGenerator* code_gen);
  1111. /**
  1112. * Processes a LADER opcode.
  1113. *
  1114. * Opcode: 0xC2
  1115. * Short name: LADER
  1116. * Long name: Ladder
  1117. *
  1118. * Memory layout (15 bytes)
  1119. * |0xC2|B1/B2|B3/B4|X|X|Y|Y|Z|Z|I|I|K|A|D|S|
  1120. *
  1121. * Arguments
  1122. * - const Bit[4] B1: Bank to retrieve X-coordinate, or zero if X
  1123. * is specified as a literal value.
  1124. * - const Bit[4] B2: Bank to retrieve Y-coordinate, or zero if Y
  1125. * is specified as a literal value.
  1126. * - const Bit[4] B3: Bank to retrieve Z-coordinate, or zero if Z
  1127. * is specified as a literal value.
  1128. * - const Bit[4] B4: Bank to retrieve ID, or zero if I is
  1129. * specified as a literal value.
  1130. * - const Short X: X-coordinate of the end of the ladder, or
  1131. * address to find X-coordinate if B1 is non-zero.
  1132. * - const Short Y: Y-coordinate of the end of the ladder, or
  1133. * address to find Y-coordinate if B2 is non-zero.
  1134. * - const Short Z: Z-coordinate of the end of the ladder, or
  1135. * address to find Z-coordinate if B3 is non-zero.
  1136. * - const UShort I: ID of the walkmesh triangle found at the end
  1137. * of the ladder, or address to find ID if B4 is non-zero.
  1138. * - const UByte K: The keys used to move the character on the
  1139. * ladder.
  1140. * - const UByte A: Animation ID for the field object's movement
  1141. * animation.
  1142. * - const UByte D: Direction the character faces when climbing
  1143. * the ladder.
  1144. * - const UByte S: Speed of the animation whilst climbing the
  1145. * ladder.
  1146. *
  1147. * Causes the character to climb a ladder; that is, switching from
  1148. * standard walkmesh movement, to climbing along a line connecting
  1149. * two points on the walkmesh. If B1, B2, B3 or B4 is non-zero,
  1150. * then the value for that particular component is taken from
  1151. * memory using the corresponding bank and address specified,
  1152. * rather than as a literal value. Both retrieved values and
  1153. * literals can be used for different components. If using X, Y, Z
  1154. * or I as addresses, the lower byte should hold the address
  1155. * whilst the higher byte should be zero. The coordinates specify
  1156. * the end-point of the ladder; the current position of the
  1157. * character is used as the start point. The ID of the walkmesh
  1158. * triangle must be specified; this is the triangle the character
  1159. * will step onto after reaching the end point of the ladder. The
  1160. * K value specifies the keys used to move the character across
  1161. * the ladder; keys outside the range found in the table will
  1162. * cause unpredictable behaviour. The animation ID specifies an
  1163. * offset into the field object's animation list; this animation
  1164. * is played at the speed specified by S whilst the character
  1165. * climbs. Finally, the D argument is a direction value in the
  1166. * game's standard direction format, which orients the character
  1167. * on the ladder. This opcode is used as part of the character's
  1168. * entity, rather than in a seperate entity, as with a LINE. A
  1169. * LINE is used to set the start point of the ladder on the
  1170. * walkmesh. When this LINE is crossed by the player, a script in
  1171. * the LINE then uses a PREQ (or one of its variants), calling the
  1172. * script in the party leader that defines the LADER, causing the
  1173. * character to switch to 'climbing mode'. To set up a two-way
  1174. * ladder, two LINEs are used at either end, with different values
  1175. * for the LADER arguments, such as differing end points. If this
  1176. * opcode is used as part of a non-playable character entity, the
  1177. * NPC object will automatically climb from the start to the end
  1178. * point without need for player interaction.
  1179. *
  1180. * @param code_gen[in] The code generator.
  1181. * @param entity[in] The name of the entity.
  1182. */
  1183. void processLADER(CodeGenerator* code_gen, const std::string& entity);
  1184. void processSOLID(CodeGenerator* code_gen, const std::string& entity);
  1185. /**
  1186. * Processes an OFST opcode.
  1187. *
  1188. * Opcode: 0xC3
  1189. * Short name: OFST
  1190. * Long name: Offset Object
  1191. *
  1192. * Memory layout (8 bytes)
  1193. * |0xC3|B1/B2|B3/B4|T|X|Y|Z|S|
  1194. *
  1195. * Arguments:
  1196. * - const Bit[4] B1: Bank to retrieve X offset, or zero if X is
  1197. * specified as a literal.
  1198. * - const Bit[4] B2: Bank to retrieve Y offset, or zero if Y is
  1199. * specified as a literal.
  1200. * - const Bit[4] B3: Bank to retrieve Z offset, or zero if Z is
  1201. * specified as a literal.
  1202. * - const Bit[4] B4: Bank to retrieve speed, or zero if S is
  1203. * specified as a literal.
  1204. * - const UByte T: Type of movement.
  1205. * - const Short X: X offset amount, relative to current position,
  1206. * or address to find X offset, if B1 is non-zero.
  1207. * - const Short Y: Y offset amount, relative to current position,
  1208. * or address to find Y offset, if B2 is non-zero.
  1209. * - const Short Z: Z offset amount, relative to current position,
  1210. * or address to find Z offset, if B3 is non-zero.
  1211. * - const UShort S: Speed of the offset movement, if type is
  1212. * non-zero, or address to find speed, if B4 is non-zero.
  1213. *
  1214. * Offsets the field object, belonging to the entity whose script
  1215. * this opcode resides in, by a certain amount. After being
  1216. * offset, the character continues to be constrained in movement
  1217. * as defined by the walkmesh's shape, but at a certain distance
  1218. * away from the normal walkmesh position. Other field objects are
  1219. * unaffected, and their position or movements are maintained on
  1220. * the walkmesh's original position. If B1, B2, B3 or B4 is
  1221. * non-zero, then the value for that particular component is taken
  1222. * from memory using the corresponding bank and address specified,
  1223. * rather than as a literal value. Both retrieved values and
  1224. * literals can be used for different components. If using T, X, Y
  1225. * or S as addresses, the lower byte should hold the address
  1226. * whilst the higher byte should be zero. The amount to offset is
  1227. * specified relative to the current position. If Type is
  1228. * specified, the object moves gradually from its current point to
  1229. * the offset position; this can be used to simulate movements
  1230. * such as elevators. Any type outside the range in the table will
  1231. * cause the offset not to occur. If the object is set to move
  1232. * gradually, then the speed of offset can be set; the greater the
  1233. * number, the slower the object moves to its target offset.
  1234. * Script execution may also be halted until the gradual offset
  1235. * has been completed. For this, see OFSTW.
  1236. *
  1237. * @param codegen[in] The code generator.
  1238. * @param entity[in] The entity name.
  1239. */
  1240. void processOFST(CodeGenerator* codegen, const std::string& entity);
  1241. };
  1242. /**
  1243. * A walkmesh instruction.
  1244. */
  1245. class FF7WalkmeshInstruction : public KernelCallInstruction{
  1246. public:
  1247. /**
  1248. * Processes the instruction.
  1249. *
  1250. * @param func[in] Function to process.
  1251. * @param stack[out] Function stack.
  1252. * @param engine[in] Engine. Unused.
  1253. * @param code_gen[in] Code generator.
  1254. */
  1255. virtual void processInst(
  1256. Function& func, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  1257. ) override;
  1258. private:
  1259. void processUC(CodeGenerator* code_gen);
  1260. /**
  1261. * Processes a LINE opcode.
  1262. *
  1263. * Opcode: 0xD0
  1264. * Short name: LINE
  1265. * Long name: Line definition
  1266. *
  1267. * Memory layout (7 bytes)
  1268. * |0xD0|XA|YA|ZA|XB|YB|ZB|
  1269. *
  1270. * Arguments:
  1271. * - const Short XA: X-coordinate of the first point of the line.
  1272. * - const Short YA: Y-coordinate of the first point of the line.
  1273. * - const Short ZA: Z-coordinate of the first point of the line.
  1274. * - const Short XB: X-coordinate of the second point of the line.
  1275. * - const Short YB: Y-coordinate of the second point of the line.
  1276. * - const Short ZB: Z-coordinate of the second point of the line.
  1277. *
  1278. * Defines a line on the walkmesh that, when crossed by a playable
  1279. * character, causes one of the entity's scripts to be executed.
  1280. * These are similar to the triggers in Section 8. All the lines
  1281. * in the current field can be turned on or off by using the LINON
  1282. * opcode.
  1283. *
  1284. * There are generally 6 scripts (other than the init and main) if
  1285. * the entity is a LINE.
  1286. * - script index 2 -> S1 - [OK].
  1287. * - script index 3 -> S2 - Move.
  1288. * - script index 4 -> S3 - Move.
  1289. * - script index 5 -> S4 - Go.
  1290. * - script index 6 -> S5 - Go 1x.
  1291. * - script index 7 -> S6 - Go away.
  1292. *
  1293. * @param code_gen The code generator.
  1294. * @param entity[in] The entity name.
  1295. */
  1296. void processLINE(CodeGenerator* code_gen, const std::string& entity);
  1297. };
  1298. /**
  1299. * A background instruction.
  1300. */
  1301. class FF7BackgroundInstruction : public KernelCallInstruction{
  1302. public:
  1303. /**
  1304. * Processes the instruction.
  1305. *
  1306. * @param func[in] Function to process.
  1307. * @param stack[out] Function stack.
  1308. * @param engine[in] Engine. Unused
  1309. * @param code_gen[in] Code generator.
  1310. */
  1311. virtual void processInst(
  1312. Function& func, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  1313. ) override;
  1314. private:
  1315. void processBGON(CodeGenerator* code_gen);
  1316. void processBGOFF(CodeGenerator* code_gen);
  1317. void processBGCLR(CodeGenerator* code_gen);
  1318. void processSTPAL(CodeGenerator* code_gen);
  1319. void processLDPAL(CodeGenerator* code_gen);
  1320. void processCPPAL(CodeGenerator* code_gen);
  1321. void processADPAL(CodeGenerator* code_gen);
  1322. void processMPPAL2(CodeGenerator* code_gen);
  1323. void processSTPLS(CodeGenerator* code_gen);
  1324. void processLDPLS(CodeGenerator* code_gen);
  1325. };
  1326. /**
  1327. * A camera instruction.
  1328. */
  1329. class FF7CameraInstruction : public KernelCallInstruction{
  1330. public:
  1331. /**
  1332. * Processes the instruction.
  1333. *
  1334. * @param func[in] Function to process.
  1335. * @param stack[out] Function stack.
  1336. * @param engine[in] Engine. Unused
  1337. * @param code_gen[in] Code generator.
  1338. */
  1339. virtual void processInst(
  1340. Function& func, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  1341. ) override;
  1342. private:
  1343. void processNFADE(CodeGenerator* code_gen);
  1344. void processSCR2D(CodeGenerator* code_gen);
  1345. void processSCR2DC(CodeGenerator* code_gen);
  1346. void processFADE(CodeGenerator* code_gen);
  1347. };
  1348. /**
  1349. * An audio or video (or both) instruction.
  1350. */
  1351. class FF7AudioVideoInstruction : public KernelCallInstruction{
  1352. public:
  1353. /**
  1354. * Processes the instruction.
  1355. *
  1356. * @param func[in] Function to process.
  1357. * @param stack[out] Function stack.
  1358. * @param engine[in] Engine. Unused
  1359. * @param code_gen[in] Code generator.
  1360. */
  1361. virtual void processInst(
  1362. Function& func, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  1363. ) override;
  1364. private:
  1365. void processAKAO2(CodeGenerator* code_gen);
  1366. void processMUSIC(CodeGenerator* code_gen);
  1367. void processSOUND(CodeGenerator* code_gen);
  1368. void processAKAO(CodeGenerator* code_gen);
  1369. void processMULCK(CodeGenerator* code_gen);
  1370. void processPMVIE(CodeGenerator* code_gen);
  1371. void processMOVIE(CodeGenerator* code_gen);
  1372. void processMVIEF(CodeGenerator* code_gen);
  1373. };
  1374. /**
  1375. * An instructions that doesn't fall in any other category.
  1376. */
  1377. class FF7UncategorizedInstruction : public KernelCallInstruction{
  1378. public:
  1379. /**
  1380. * Processes the instruction.
  1381. *
  1382. * @param func[in] Function to process.
  1383. * @param stack[out] Function stack.
  1384. * @param engine[in] Engine. Unused.
  1385. * @param code_gen[in] Code generator.
  1386. */
  1387. virtual void processInst(
  1388. Function& func, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  1389. ) override;
  1390. };
  1391. /**
  1392. * An instruction that does nothing.
  1393. */
  1394. class FF7NoOperationInstruction : public Instruction{
  1395. public:
  1396. /**
  1397. * Generates a instruction that does nothing.
  1398. *
  1399. * @return The generated instruction.
  1400. */
  1401. static InstPtr Create(){return new FF7NoOperationInstruction();}
  1402. /**
  1403. * Processes the instruction.
  1404. *
  1405. * It doesn't do anything.
  1406. *
  1407. * @param func[in] Function to process. Unused.
  1408. * @param stack[out] Function stack. Unused.
  1409. * @param engine[in] Engine. Unused.
  1410. * @param code_gen[in] Code generator. Unused.
  1411. */
  1412. virtual void processInst(
  1413. Function& func, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  1414. ) override;
  1415. };
  1416. //}