FieldModelInstruction.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Copyright (C) 2022 V-Gears Team
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #pragma once
  18. #include "decompiler/instruction/KernelCallInstruction.h"
  19. #include "decompiler/field/FieldEngine.h"
  20. /**
  21. * A model instruction.
  22. */
  23. class FieldModelInstruction : public KernelCallInstruction{
  24. public:
  25. /**
  26. * Processes the instruction.
  27. *
  28. * @param[in] func Function to process.
  29. * @param[out] stack Function stack.
  30. * @param[in] engine Engine.
  31. * @param[in] code_gen Code generator.
  32. */
  33. virtual void ProcessInst(
  34. Function& func, ValueStack &stack, Engine* engine, CodeGenerator* code_gen
  35. ) override;
  36. private:
  37. /**
  38. * Processes a JOIN opcode.
  39. *
  40. * Opcode: 0x08
  41. * Short name: JOIN
  42. * Long name: Party Field Join
  43. * Memory layout (2 bytes)
  44. * |0x08|S|
  45. *
  46. * Arguments
  47. * - const UByte S: Speed that the characters join back together.
  48. *
  49. * Causes seperated party characters that have previously been SPLIT onto the field, to be
  50. * joined back together again; that is, only the party leader becomes visible on the field.
  51. * This should be called if a previous SPLIT has completed (the party members have finished
  52. * speaking, or performing their actions, for example). As with SPLIT, the speed of the
  53. * join is specified, from a scale of 1 (almost instant) to FF (very slow walk), and must
  54. * be non-zero. In contrast to most MOVE related op codes, the speed is this setting is
  55. * actually the total number of frames required. Depending on the distance from the player
  56. * character and the number of frames required, the entity plays a run or walk animation.
  57. * Also, all characters take the same time irrespective of distance. Calling JOIN without
  58. * having previously SPLIT the characters will cause the party members to appear at the
  59. * walkmesh origin and attempt to JOIN from there. This is not normally the required
  60. * behavior and should be avoided.
  61. *
  62. * @param[in,out] code_gen Code generator to append lines.
  63. */
  64. void ProcessJOIN(CodeGenerator* code_gen);
  65. /**
  66. * Processes a SPLIT opcode.
  67. *
  68. * Opcode: 0x09
  69. * Short name: SPLIT
  70. * Long name: Party Field Split
  71. *
  72. * Memory layout (15 bytes)
  73. * |0x20|B1/B2|B3/B4|B5/B6|XA|XA|YA|YA|DA|XB|XB|YB|YB|DB|S|
  74. *
  75. * Arguments
  76. * - const Bit[4] B1: Bank for XA, or zero if XA is specified as a literal value.
  77. * - const Bit[4] B2: Bank for YA, or zero if YA is specified as a literal value.
  78. * - const Bit[4] B3: Bank for DA, or zero if DA is specified as a literal value.
  79. * - const Bit[4] B4: Bank for XB, or zero if XB is specified as a literal value.
  80. * - const Bit[4] B5: Bank for YB, or zero if YB is specified as a literal value.
  81. * - const Bit[4] B6: Bank for DB, or zero if DB is specified as a literal value.
  82. * - const Short XA: X-coordinate of the second character in the party after the split, or
  83. * address for the value if B1 is non-zero.
  84. * - const Short YA: Y-coordinate of the second character in the party after the split, or
  85. * address for the value if B2 is non-zero.
  86. * - const UByte DA: Direction the second character faces after the split, or address for
  87. * the value if B3 is non-zero.
  88. * - const Short XB: X-coordinate of the third character in the party after the split, or
  89. * address for the value if B4 is non-zero.
  90. * - const Short YB: Y-coordinate of the third character in the party after the split, or
  91. * address for the value if B5 is non-zero.
  92. * - const UByte DB: Direction the third character faces after the split, or address for
  93. * the value if B6 is non-zero.
  94. * - const UByte S: Speed that the characters split.
  95. *
  96. * Causes the common 'split effect' whereby the second and third characters in the current
  97. * party 'come out' from the party leader. That is, they become visible in the field,
  98. * starting from the center of the party leader, and move out to the coordinates specified
  99. * in the argument list. This is commonly used when the other characters in the current
  100. * party have an action or dialog to perform and must be individually visible in the field.
  101. * As well as specifying final coordinates for the two other party characters, the
  102. * directions each character faces after the split are specified as a byte, using the
  103. * common direction values found throughout the game. Speed is also given and is used to
  104. * specify the rate at which the characters leave the party leader, using a scale from 1
  105. * (almost instant) to FF (extremely slow walk); this must be non-zero. In contrast to most
  106. * MOVE related op codes, the speed is this setting is actually the total number of frames
  107. * required. Depending on the distance from the player character and the number of frames
  108. * required, the entity plays a run or walk animation. Also, all characters take the same
  109. * time irrespective of distance.
  110. *
  111. * @param[in,out] code_gen Code generator to append lines.
  112. */
  113. void ProcessSPLIT(CodeGenerator* code_gen);
  114. /**
  115. * Processes a TLKON opcode.
  116. *
  117. * Opcode: 0x7E
  118. * Short name: TLKON
  119. * Long name: Talk Switch
  120. *
  121. * Memory layout (2 bytes)
  122. * |0x7E|S
  123. *
  124. * Arguments:
  125. *
  126. * - const UByte B: Switch on/off (0/1, respectively).
  127. *
  128. * Turns on or off, for an entity, the ability for the playable character to interact with
  129. * the entity by pressing the [X] button. More precisely, this enables or disables the On
  130. * Press script (script 2); if set to off, script 2 will not execute when the button is
  131. * pressed and the player is facing the entity's object. If set to on, the script will
  132. * execute, as normal.
  133. */
  134. void ProcessTLKON(CodeGenerator* code_gen, const std::string& entity);
  135. void ProcessPC(CodeGenerator* code_gen, const std::string& entity, int char_id);
  136. void ProcessCHAR(CodeGenerator* code_gen, const std::string& entity);
  137. void ProcessDFANM(CodeGenerator* code_gen, const std::string& entity, int char_id);
  138. void ProcessANIME1(CodeGenerator* code_gen, const std::string& entity, int char_id);
  139. void ProcessVISI(CodeGenerator* code_gen, const std::string& entity);
  140. void ProcessXYZI(CodeGenerator* code_gen, const std::string& entity);
  141. void ProcessMOVE(CodeGenerator* code_gen, const std::string& entity);
  142. /**
  143. * Processes a TURA opcode.
  144. *
  145. * Opcode: 0xAB
  146. * Short name: TURA
  147. * Long name: Turn to entity
  148. *
  149. * Memory layout (4 bytes)
  150. * |0xB4|Entity id|Rotate side type|Steps in rotation
  151. *
  152. * Arguments
  153. *
  154. * - const Bit[4] Entity id: Entity id to which model we calculate direction during first
  155. * opcode call.
  156. * - const Short Rotate side type: Specify how model will be rotated. (0 - clockwise/ 1 -
  157. * anti-clockwise/ 2 - closest)
  158. * - const Short Steps in rotation: Set number of steps in rotation.
  159. *
  160. * Rotation calculated like in TUGNGEN, except end direction calculated during first opcode
  161. * call. Rotation always calculated smoothly. Like in TURNGEN(XX,XX,XX,XX,02); This opcode
  162. * will be called until turn is over and then continue script execution.
  163. *
  164. * @param[in,out] code_gen Code generator to append lines.
  165. * @param[in] entity The name of the entity.
  166. * @param[in] engine The field engine.
  167. */
  168. void ProcessTURA(
  169. CodeGenerator* code_gen, const std::string& entity, const FieldEngine& engine
  170. );
  171. void ProcessMSPED(CodeGenerator* code_gen, const std::string& entity);
  172. void ProcessDIR(CodeGenerator* code_gen, const std::string& entity);
  173. void ProcessTURNGEN(CodeGenerator* code_gen, const std::string& entity);
  174. void ProcessGETAI(CodeGenerator* code_gen, const FieldEngine& engine);
  175. void ProcessANIM_2(CodeGenerator* code_gen, const std::string& entity, int char_id);
  176. void ProcessCANIM2(CodeGenerator* code_gen, const std::string& entity, int char_id);
  177. void ProcessCANM_2(CodeGenerator* code_gen, const std::string& entity, int char_id);
  178. void ProcessCC(CodeGenerator* code_gen, const FieldEngine& engine);
  179. /**
  180. * Processes a JUMP opcode.
  181. *
  182. * Opcode: 0xC0
  183. * Short name: JUMP
  184. * Long name: Jump
  185. *
  186. * Memory layout (7 bytes)
  187. * |0xC2|B1/B2|B3/B4|X|Y|I|Steps|
  188. *
  189. * Arguments
  190. * - const Bit[4] B1: Bank to retrieve X-coordinate, or zero if specifying X as a literal.
  191. * - const Bit[4] B2: Bank to retrieve Y-coordinate, or zero if specifying Y as a literal.
  192. * - const Bit[4] B3: Bank to retrieve triangle ID, or zero if specifying Z as a literal.
  193. * - const Bit[4] B4: Bank to retrieve jump height, or zero if specifying H as a literal.
  194. * - const Short X: X-coordinate of the target to jump to, or lower byte specifying address
  195. * if B1 is non-zero.
  196. * - const Short Y: Y-coordinate of the target to jump to, or lower byte specifying address
  197. * if B2 is non-zero.
  198. * - const Short I: Triangle ID of the target to jump to, or lower byte specifying address
  199. * if B3 is non-zero.
  200. * - const UShort Steps: Steps in jump. Must be non-zero if a literal value. Alternatively,
  201. * lower byte specifies address if B4 is non-zero.
  202. *
  203. * Causes the character to jump to the specified point and triangle ID, with the jump curve
  204. * peaking at a height which is increased by using a larger value for the H argument. In
  205. * addition, the larger the number, the longer the jump will take to complete. A "normal"
  206. * value is around 0x15, 0x01 is fast and instantaneous; the argument must not be zero or
  207. * the game will crash. Whilst this is an unsigned two-byte number, a large value (beyond
  208. * around 0x60) will not only cause a vast jump height, but also cause the screen to scroll
  209. * erratically (the larger the number, the more erratic). Main update function go through
  210. * all entity with JUMP state and if stage is 0 it calculates final Z point according to
  211. * triangle id. It sets current coords as start coords. The main thing this function does
  212. * is set B coefficient for later calculation. It defines as follows:
  213. * B = (Z_final - Z_start) / steps - steps * 1.45;
  214. * Then it set current step to 0 and stage to 1. On next update other part of function
  215. * works. It's calculate real position. First it increment current step number. Then it
  216. * calculate X and Y. They change linear so nothing interesting here. The Z calculation is
  217. * as follows:
  218. * Z_current = - step^2 * 1.45 + step * B + Z_start;
  219. * If current substep equal number of steps then we set current triangle to final triangle
  220. * and set stage to 2. Which finalizes the routine on next opcode call. Neither animation
  221. * nor sound is specified in this opcode. An animation is played by using an animation
  222. * opcode such as DFANM, and a SOUND played, before the jump.
  223. *
  224. * @param[in,out] code_gen Code generator to append lines.
  225. * @param[in] entity The name of the entity.
  226. */
  227. void ProcessJUMP(CodeGenerator* code_gen, const std::string& entity);
  228. /**
  229. * Processes a AXYZI opcode
  230. *
  231. * Opcode: 0xC1
  232. * Short name: AXYZI
  233. * Long name: Entity Get Position
  234. *
  235. * Memory layout (8 bytes)
  236. * |0xC1|B1/B2|B3/B4|A|X|Y|Z|I|
  237. *
  238. * Arguments
  239. * - const Bit[4] B1: Bank to store X.
  240. * - const Bit[4] B2: Bank to store Y.
  241. * - const Bit[4] B3: Bank to store Z.
  242. * - const Bit[4] B4: Bank to store I.
  243. * - const UByte A: Entity ID whose field object will have its position retrieved from.
  244. * - const UByte X: Address to store the X-coordinate.
  245. * - const UByte Y: Address to store the Y-coordinate.
  246. * - const UByte Z: Address to store the Z-coordinate.
  247. * - const UByte I: Address to store the ID of the walkmesh triangle the object is standing
  248. * on.
  249. *
  250. * Retrieves the coordinates of the field object that the entity, whose ID specified in A,
  251. * is associated with. This opcode uses an entity ID, not a field object offset; as such,
  252. * if an entity ID is given that does not have a field object, this opcode will store zero
  253. * in each of the four address specified.
  254. *
  255. * @param[in,out] code_gen Code generator to append lines.
  256. */
  257. void ProcessAXYZI(CodeGenerator* code_gen);
  258. /**
  259. * Processes a LADER opcode.
  260. *
  261. * Opcode: 0xC2
  262. * Short name: LADER
  263. * Long name: Ladder
  264. *
  265. * Memory layout (15 bytes)
  266. * |0xC2|B1/B2|B3/B4|X|X|Y|Y|Z|Z|I|I|K|A|D|S|
  267. *
  268. * Arguments
  269. * - const Bit[4] B1: Bank to retrieve X-coordinate, or zero if X is specified as a literal
  270. * value.
  271. * - const Bit[4] B2: Bank to retrieve Y-coordinate, or zero if Y is specified as a literal
  272. * value.
  273. * - const Bit[4] B3: Bank to retrieve Z-coordinate, or zero if Z is specified as a literal
  274. * value.
  275. * - const Bit[4] B4: Bank to retrieve ID, or zero if I is specifiedas a literal value.
  276. * - const Short X: X-coordinate of the end of the ladder, or address to find X-coordinate
  277. * if B1 is non-zero.
  278. * - const Short Y: Y-coordinate of the end of the ladder, or address to find Y-coordinate
  279. * if B2 is non-zero.
  280. * - const Short Z: Z-coordinate of the end of the ladder, or address to find Z-coordinate
  281. * if B3 is non-zero.
  282. * - const UShort I: ID of the walkmesh triangle found at the end of the ladder, or address
  283. * to find ID if B4 is non-zero.
  284. * - const UByte K: The keys used to move the character on the ladder.
  285. * - const UByte A: Animation ID for the field object's movement animation.
  286. * - const UByte D: Direction the character faces when climbing the ladder.
  287. * - const UByte S: Speed of the animation whilst climbing the ladder.
  288. *
  289. * Causes the character to climb a ladder; that is, switching from standard walkmesh
  290. * movement, to climbing along a line connecting two points on the walkmesh. If B1, B2, B3
  291. * or B4 is non-zero, then the value for that particular component is taken from memory
  292. * using the corresponding bank and address specified, rather than as a literal value. Both
  293. * retrieved values and literals can be used for different components. If using X, Y, Z or
  294. * I as addresses, the lower byte should hold the address whilst the higher byte should be
  295. * zero. The coordinates specify the end-point of the ladder; the current position of the
  296. * character is used as the start point. The ID of the walkmesh triangle must be specified;
  297. * this is the triangle the character will step onto after reaching the end point of the
  298. * ladder. The K value specifies the keys used to move the character across the ladder;
  299. * keys outside the range found in the table will cause unpredictable behavior. The
  300. * animation ID specifies an offset into the field object's animation list; this animation
  301. * is played at the speed specified by S whilst the character climbs. Finally, the D
  302. * argument is a direction value in the game's standard direction format, which orients the
  303. * character on the ladder. This opcode is used as part of the character's entity, rather
  304. * than in a separate entity, as with a LINE. A LINE is used to set the start point of
  305. * the ladder on the walkmesh. When this LINE is crossed by the player, a script in the
  306. * LINE then uses a PREQ (or one of its variants), calling the script in the party leader
  307. * that defines the LADER, causing the character to switch to 'climbing mode'. To set up a
  308. * two-way ladder, two LINEs are used at either end, with different values for the LADER
  309. * arguments, such as differing end points. If this opcode is used as part of a
  310. * non-playable character entity, the NPC object will automatically climb from the start to
  311. * the end point without need for player interaction.
  312. *
  313. * @param[in,out] code_gen Code generator to append lines.
  314. * @param[in] entity The name of the entity.
  315. */
  316. void ProcessLADER(CodeGenerator* code_gen, const std::string& entity);
  317. /**
  318. * Processes an SLIDR opcode.
  319. *
  320. * Opcode: 0xC6
  321. * Short name: SLIDR
  322. * Long name: Solid Range
  323. *
  324. * Memory layout (3 bytes)
  325. * |0xC6|B|R|
  326. *
  327. * Arguments:
  328. *
  329. * - const UByte B: Bank to retrieve R, or zero if R is specified as a literal value.
  330. * - const UByte R: Range value.
  331. *
  332. * Adjusts the range of the collision circle for the entity's field object, changing the
  333. * distance threshold for collisions between the object, and both other objects and the
  334. * walkmesh boundaries. Lower values produce a lower circle for the object; higher values
  335. * increase the circle size.
  336. */
  337. void ProcessSLIDR(CodeGenerator* code_gen, const std::string& entity);
  338. /**
  339. * Processes a SOLID opcode.
  340. *
  341. * Opcode: 0xC7
  342. * Short name: SOLID
  343. * Long name: Solid object
  344. *
  345. * Memory layout (2 bytes)
  346. * |0xC7|S|
  347. *
  348. * Arguments:
  349. *
  350. * const UByte S: Switch on/off (0/1, respectively).
  351. *
  352. * Switches the solidity of the field object associated with this entity; that is, turns
  353. * collision detection on or off. When off, the playable character will be able to walk
  354. * through the entity's object, as if it were not there. This may be used for such objects
  355. * as save points, where the character must be able to walk through the object to be able
  356. * to access the save menu item.
  357. */
  358. void ProcessSOLID(CodeGenerator* code_gen, const std::string& entity);
  359. /**
  360. * Processes an OFST opcode.
  361. *
  362. * Opcode: 0xC3
  363. * Short name: OFST
  364. * Long name: Offset Object
  365. *
  366. * Memory layout (8 bytes)
  367. * |0xC3|B1/B2|B3/B4|T|X|Y|Z|S|
  368. *
  369. * Arguments:
  370. * - const Bit[4] B1: Bank to retrieve X offset, or zero if X is specified as a literal.
  371. * - const Bit[4] B2: Bank to retrieve Y offset, or zero if Y is specified as a literal.
  372. * - const Bit[4] B3: Bank to retrieve Z offset, or zero if Z is specified as a literal.
  373. * - const Bit[4] B4: Bank to retrieve speed, or zero if S is specified as a literal.
  374. * - const UByte T: Type of movement.
  375. * - const Short X: X offset amount, relative to current position, or address to find X
  376. * offset, if B1 is non-zero.
  377. * - const Short Y: Y offset amount, relative to current position, or address to find Y
  378. * offset, if B2 is non-zero.
  379. * - const Short Z: Z offset amount, relative to current position, or address to find Z
  380. * offset, if B3 is non-zero.
  381. * - const UShort S: Speed of the offset movement, if type is non-zero, or address to find
  382. * speed, if B4 is non-zero.
  383. *
  384. * Offsets the field object, belonging to the entity whose script this opcode resides in,
  385. * by a certain amount. After being offset, the character continues to be constrained in
  386. * movement as defined by the walkmesh's shape, but at a certain distance away from the
  387. * normal walkmesh position. Other field objects are unaffected, and their position or
  388. * movements are maintained on the walkmesh's original position. If B1, B2, B3 or B4 is
  389. * non-zero, then the value for that particular component is taken from memory using the
  390. * corresponding bank and address specified, rather than as a literal value. Both retrieved
  391. * values and literals can be used for different components. If using T, X, Y or S as
  392. * addresses, the lower byte should hold the address whilst the higher byte should be zero.
  393. * The amount to offset is specified relative to the current position. If Type is
  394. * specified, the object moves gradually from its current point to the offset position;
  395. * this can be used to simulate movements such as elevators. Any type outside the range in
  396. * the table will cause the offset not to occur. If the object is set to move gradually,
  397. * then the speed of offset can be set; the greater the number, the slower the object moves
  398. * to its target offset. Script execution may also be halted until the gradual offset has
  399. * been completed. For this, see OFSTW.
  400. *
  401. * @param[in,out] code_gen Code generator to append lines.
  402. * @param[in] entity The entity name.
  403. */
  404. void ProcessOFST(CodeGenerator* code_gen, const std::string& entity);
  405. };