field.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. -- When V-Gears is run, export character names.
  2. export_character_names()
  3. --- Helper function that changes field maps through fade in and fadeout
  4. --
  5. -- @param map_name The target map name.
  6. -- @param point_name Spawn point name in the target map for the PC.
  7. load_field_map_request = function(map_name, point_name)
  8. if type(map_name) == "string" and map_name ~= "" then
  9. System["MapChanger"].map_name = map_name
  10. System["MapChanger"].point_name = point_name
  11. script:request(Script.SYSTEM, "MapChanger", "ffvii_field", 0)
  12. end
  13. end
  14. dispatch_menu = function(menu_id, parameter)
  15. menu_id = tonumber(menu_id)
  16. parameter = tonumber(parameter)
  17. if not menu_id or not parameter then
  18. return
  19. end
  20. if menu_id == 5 then
  21. ending_credits()
  22. elseif menu_id == 6 then
  23. if parameter == 64 then
  24. name_chocobo()
  25. else
  26. name_character(parameter)
  27. end
  28. elseif menu_id == 7 then
  29. if parameter == 1 then
  30. Party.split_teams(3)
  31. elseif parameter == 2 then
  32. Party.split_teams(2)
  33. else
  34. Party.choose_party()
  35. end
  36. elseif menu_id == 8 then
  37. open_shop(parameter)
  38. elseif menu_id == 9 then
  39. open_menu("party")
  40. elseif menu_id == 14 then
  41. open_menu("save")
  42. elseif menu_id == 15 then
  43. Party.steal_materia()
  44. elseif menu_id == 18 then
  45. Characters.remove_materia(parameter)
  46. elseif menu_id == 19 then
  47. Characters.restore_materia(parameter)
  48. end
  49. end
  50. --- Makes an entity the player entity and places it where the previous map sent it to.
  51. --
  52. -- This is the entry point of the map (when it came from another field) or the field entry of the
  53. -- world map. Fields that don't have a PC opcode for the player need it too, or the player would
  54. -- stay at the origin of the field, out of its walkmesh.
  55. --
  56. -- @param entity_name The name of the entity that the player controls.
  57. place_player_at_map_entry = function(entity_name)
  58. if System.MapChanger.point_name ~= "" then
  59. local point = entity_manager:get_entity_point(System.MapChanger.point_name)
  60. if point ~= nil then
  61. local x, y, z = point:get_position()
  62. local rotation = point:get_rotation()
  63. local player = entity_manager:get_entity(entity_name)
  64. if player ~= nil then
  65. player:set_position(x, y, z)
  66. player:set_rotation(rotation)
  67. player:set_solid(true)
  68. player:set_visible(true)
  69. else
  70. print("place_player_at_map_entry: there is no entity '" .. entity_name .. "'.")
  71. end
  72. end
  73. end
  74. entity_manager:set_player_entity(entity_name)
  75. local world_map_entry = System.MapChanger.world_map_entry
  76. if world_map_entry then
  77. entity_manager:place_player_at_field_entry(
  78. world_map_entry.x, world_map_entry.y,
  79. world_map_entry.triangle, world_map_entry.direction
  80. )
  81. System.MapChanger.world_map_entry = nil
  82. end
  83. end
  84. --- Assigns an entity to a playable character.
  85. --
  86. -- @param entity_name The entity name.
  87. -- @param character_id Id of the character to assign.
  88. set_entity_to_character = function(entity_name, character_id)
  89. if character_id == Party[1] and entity_name ~= "" then
  90. place_player_at_map_entry(entity_name)
  91. end
  92. entity_manager:set_entity_to_character(entity_name, character_id)
  93. -- The model of a playable character is shown only if it is the leader of the party. The other
  94. -- members are not drawn following the leader (join_party hides them too), and the scenes show
  95. -- their models when they need them. Models are visible by default, so the ones of the
  96. -- characters that are not the leader, such as Cid in mds6_1 or Aerith in mrkt2 after the
  97. -- first scene, have to be hidden here. With no party at all (a new game that has not set it
  98. -- yet) nothing is hidden.
  99. if character_id ~= Party[1] and entity_name ~= "" and Party[1] ~= nil then
  100. local entity = entity_manager:get_entity(entity_name)
  101. if entity ~= nil then
  102. entity:set_visible(false)
  103. entity:set_solid(false)
  104. entity:set_talkable(false)
  105. end
  106. end
  107. end
  108. --- Joins the party members in the field.
  109. --
  110. -- @param speed Speed at which the characters join
  111. join_party = function(speed)
  112. local player = entity_manager:get_player_entity()
  113. if (player == nil) then
  114. do return end
  115. end
  116. local x, y, z = player:get_position()
  117. for c = 2, 3 do
  118. if Party[c] ~= nil then
  119. local character = entity_manager:get_entity_from_character_id(Party[c])
  120. --print(" Name: " .. character:get_name())
  121. if character ~= nil then
  122. local cur_speed = character:get_move_auto_speed()
  123. character:set_solid(false)
  124. -- Approximated speed, good enough for now.
  125. -- TODO: Calculate speed based on time, as the orignal opcode.
  126. character:set_move_auto_speed(speed / 16)
  127. character:move_to_position(x, y)
  128. character:move_sync()
  129. character:set_move_auto_speed(cur_speed)
  130. character:set_visible(false)
  131. character:set_talkable(false)
  132. end
  133. end
  134. end
  135. end
  136. --- Splits the party members in the field.
  137. --
  138. -- @param ax_addr Bank for ax, or zero if ax is specified as a literal value.
  139. -- @param ay_addr Bank for ay, or zero if ay is specified as a literal value.
  140. -- @param ar_addr Bank for ar, or zero if ar is specified as a literal value.
  141. -- @param bx_addr Bank for bx, or zero if bx is specified as a literal value.
  142. -- @param by_addr Bank for by, or zero if by is specified as a literal value.
  143. -- @param br_addr Bank for br, or zero if by is specified as a literal value.
  144. -- @param ax X-coordinate of the second character in the party after the split, or address for the value if ax_addr is non-zero.
  145. -- @param ay Y-coordinate of the second character in the party after the split, or address for the value if ax_addr is non-zero.
  146. -- @param ar Direction the second character faces after the split, or address for the value if ar_addr is non-zero.
  147. -- @param bx X-coordinate of the third character in the party after the split, or address for the value if bx_addr is non-zero.
  148. -- @param by Y-coordinate of the third character in the party after the split, or address for the value if bx_addr is non-zero.
  149. -- @param br Direction the third character faces after the split, or address for the value if br_addr is non-zero.
  150. -- @param speed Speed at which the characters split.
  151. split_party = function(ax, ay, ar, bx, by, br, speed)
  152. local player = entity_manager:get_player_entity()
  153. local x, y, z = player:get_position()
  154. for c = 2, 3 do
  155. if Party[c] ~= nil then
  156. local character = entity_manager:get_entity_from_character_id(Party[c])
  157. if character ~= nil then
  158. local cur_speed = character:get_move_auto_speed()
  159. -- Approximated speed, good enough for now.
  160. -- TODO: Calculate speed based on time, as the orignal opcode.
  161. character:set_move_auto_speed(speed / 16)
  162. --if character:is_visible() == false then
  163. -- In theory, a member is never visible before a split, but some maps
  164. -- are wrong. In these cases, don't change position and move the entity
  165. -- from it's current position.
  166. character:set_position(x, y, z)
  167. character:set_visible(true)
  168. --end
  169. character:set_solid(false)
  170. if c == 2 then
  171. character:move_to_position(ax, ay)
  172. elseif c == 3 then
  173. character:move_to_position(bx, by)
  174. end
  175. character:move_sync()
  176. if c == 2 then
  177. character:set_rotation(ar)
  178. elseif c == 3 then
  179. character:set_rotation(br)
  180. end
  181. character:set_move_auto_speed(cur_speed)
  182. character:set_solid(true)
  183. character:set_talkable(true)
  184. end
  185. end
  186. end
  187. end
  188. --- Locks or unlocks player movement and menu access.
  189. -- @param lock True to lock, false to unlock.
  190. player_lock = function(lock)
  191. entity_manager:player_lock(lock)
  192. MenuSettings.available = (lock == false)
  193. end
  194. --- Sets a bit variable on (1).
  195. --
  196. -- @param bank Memory bank (0-index, as in game).
  197. -- @param variable Bank address (0-index, as in game).
  198. -- @param bit Address bit (0-index, as in game).
  199. bit_on = function(bank, variable, bit)
  200. BitBanks[bank][variable][bit] = 1
  201. end
  202. --- Sets a bit variable off (0).
  203. --
  204. -- @param bank Memory bank (0-index, as in game).
  205. -- @param variable Bank address (0-index, as in game).
  206. -- @param bit Address bit (0-index, as in game).
  207. bit_off = function(bank, variable, bit)
  208. BitBanks[bank][variable][bit] = 0
  209. end
  210. --- Retrieves a bit variable.
  211. --
  212. -- @param bank Memory bank (0-index, as in game).
  213. -- @param variable Bank address (0-index, as in game).
  214. -- @param bit Address bit (0-index, as in game).
  215. -- @return 1 or 0
  216. bit = function(bank, variable, bit)
  217. return BitBanks[bank][variable][bit]
  218. end
  219. --- Stores the coordinates and walkmesh triangle in memory.
  220. --
  221. -- Stores the values as in the original game, without scaling.
  222. -- @param bx Bank at which to store the X coordinate.
  223. -- @param by Bank at which to store the Y coordinate.
  224. -- @param bz Bank at which to store the Z coordinate.
  225. -- @param bt Bank at which to store the triangle.
  226. -- @param id The entity ID.
  227. -- @param ax Address at which to store the X coordinate.
  228. -- @param ay Address at which to store the Y coordinate.
  229. -- @param az Address at which to store the Z coordinate.
  230. -- @param at Address at which to store the triangle.
  231. -- @param scale Map scale to multiply values
  232. axyzi = function(bx, by, bz, bt, id, ax, ay, az, at, scale)
  233. local entity = entity_manager:get_entity_from_index(id)
  234. if entity == nil then
  235. do return end
  236. end
  237. local x, y, z = entity:get_position()
  238. local t = entity:get_move_triangle_id()
  239. Banks[bx][ax] = math.floor(x * scale)
  240. Banks[by][ay] = math.floor(y * scale)
  241. Banks[bz][az] = math.floor(z * scale)
  242. Banks[bt][at] = t
  243. return 0
  244. end
  245. --- Stores the X and Y position of an entity in two variables.
  246. --
  247. -- @param bx Bank for X.
  248. -- @param by Bank for Y.
  249. -- @param id Index of the entity in the field.
  250. -- @param ax Address at which to store the X coordinate.
  251. -- @param ay Address at which to store the Y coordinate.
  252. -- @param scale Map scale to multiply values
  253. getaxy = function(bx, by, id, ax, ay, scale)
  254. local entity = entity_manager:get_entity_from_index(id)
  255. if entity == nil then
  256. do return end
  257. end
  258. local x, y = entity:get_position()
  259. Banks[bx][ax] = math.floor(x * scale)
  260. Banks[by][ay] = math.floor(y * scale)
  261. return 0
  262. end
  263. --- Shows the ending credits.
  264. ending_credits = function()
  265. -- TODO: Implement. Probably in a looooong loooong time.
  266. end
  267. --- Displays a window where a name can be assigned to a character.
  268. --
  269. -- @param id ID of the character to name.
  270. name_character = function(id)
  271. -- TODO: Implement.
  272. if Characters[id] == nil then
  273. print("Tried to rename invalid character with ID " .. tostring(id))
  274. return 0
  275. end
  276. -- While the name menu is not enabled, the character just gets its default name.
  277. if not UiContainer.NameMenu.enabled then
  278. UiContainer.NameMenu:apply_default_name(id)
  279. return 0
  280. end
  281. UiContainer.NameMenu.id = id
  282. UiContainer.NameMenu.chocobo = false
  283. script:request(Script.UI, "NameMenu", "show", 0)
  284. end
  285. --- Displays a window where a name can be assigned to a chocobo.
  286. name_chocobo = function()
  287. -- TODO: Implement.
  288. end
  289. --- Automatically opens a menu.
  290. --
  291. -- @param menu The name of the menu.
  292. open_menu = function(menu)
  293. if menu == "party" then
  294. -- TODO
  295. elseif menu == "save" then
  296. -- TODO
  297. end
  298. end
  299. --- Opens a shop.
  300. --
  301. -- @param id Shop ID.
  302. open_shop = function(id)
  303. -- TODO: Implement
  304. end
  305. --- Plays one of the tracks assigned to the field.
  306. --
  307. -- @param id ID of the track in the field.
  308. play_map_music = function(id)
  309. local track_id = audio_manager:get_track_id(id)
  310. if track_id ~= -1 then
  311. audio_manager:play_music(tostring(track_id))
  312. else
  313. print("Requested non-existent music track ID " .. tostring(id))
  314. end
  315. end
  316. --- Changes the music of the field to one of its tracks (CMUSC).
  317. --
  318. -- The original opcode can fade the current music out and the new one in. Here the change is at once:
  319. -- a track that is changed without fades (the usual jingle of an inn) is played once, so the script
  320. -- that waits for the music to end (CHMST) goes on; one with fades is played looped.
  321. -- TODO: Implement the fades.
  322. --
  323. -- @param id ID of the track in the field.
  324. -- @param fade_out Fade out parameter of the original opcode (20 means no fade).
  325. -- @param fade_in Fade in parameter of the original opcode (0 means no fade).
  326. change_map_music = function(id, fade_out, fade_in)
  327. local track_id = audio_manager:get_track_id(id)
  328. if track_id == -1 then
  329. print("Requested non-existent music track ID " .. tostring(id))
  330. return
  331. end
  332. if fade_in == 0 then
  333. audio_manager:play_music_once(tostring(track_id))
  334. else
  335. audio_manager:play_music(tostring(track_id))
  336. end
  337. end
  338. --- Sets the map name
  339. --
  340. -- It's not set from a literal text, but from a dialog identifier.
  341. --
  342. -- @param text_id ID of the dialog text that holds the map name.
  343. set_map_name = function(text_id)
  344. dialog:set_map_name(text_id)
  345. System["MapChanger"].location_name = dialog:get_map_name()
  346. end
  347. --- Shows a choice dialog and stores the result in the selected bank address.
  348. --
  349. -- @param window_id ID of the window to show the dialog in.
  350. -- @param message_id ID of the text to show in the window.
  351. -- @param first First selectable line.
  352. -- @param last Last selectable line.
  353. -- @param bank Data bank to store the selected line in.
  354. -- @param address Addres of the bank at which to store the selected line in.
  355. dialog_ask = function(window_id, message_id, first, last, bank, address)
  356. dialog:set_clickable(window_id, true)
  357. dialog:set_cursor(window_id, first, last)
  358. dialog:dialog_set_text(window_id, message_id)
  359. dialog:dialog_wait_for_close(window_id)
  360. Banks[bank][address] = dialog:get_cursor(window_id)
  361. end
  362. --- Utility to change fields and enter battles.
  363. System["MapChanger"] = {
  364. --- ID of the map to change to.
  365. map_name = "",
  366. --- Point of the map to change to.
  367. point_name = "",
  368. world_map_entry = nil,
  369. --- ID of the current field.
  370. current_map_name = "",
  371. --- Location name, to display in the main menu and in save slots.
  372. location_name = "",
  373. --- Jumps to a new map.
  374. ffvii_field = function(self)
  375. if self.map_name ~= "" then
  376. local target = self.map_name
  377. player_lock(true) -- disable menu and pc movement while load map
  378. script:request_end_sync(Script.UI, "Fade", "fade_out", 0)
  379. -- The fields go to the world map as "wm<n>", n being the FIELD.TBL record of the exit.
  380. local world_record = tonumber(string.match(target, "^wm(%d+)$"))
  381. if world_record then
  382. enter_world_map(world_record, self.point_name)
  383. else
  384. map(target)
  385. end
  386. player_lock(false) -- enable menu and pc movement after load map
  387. self.current_map_name = target
  388. -- A field can ask for another change as soon as it starts (blin60_1 goes on to
  389. -- blin60_2 at once). That request must not be erased here.
  390. if self.map_name == target then self.map_name = "" end
  391. script:request_end_sync(Script.UI, "Fade", "fade_in", 0)
  392. end
  393. return 0
  394. end,
  395. --- Starts a battle.
  396. --
  397. -- @todo Implement.
  398. ffvii_battle = function(self)
  399. if self.map_name ~= "" then
  400. player_lock(true) -- disable menu and pc movement while load map
  401. script:request_end_sync(Script.UI, "Fade", "fade_out", 0)
  402. map(self.map_name)
  403. -- load battle ui
  404. -- load player entity
  405. --self.map_name = ""
  406. script:request_end_sync(Script.UI, "Fade", "fade_in", 0)
  407. end
  408. return 0
  409. end,
  410. }
  411. --- Runs a script of the entity of a character (the PREQ, PRQSW and PRQEW opcodes).
  412. --
  413. -- @param character ID of the character, the one that the entity was given with the PC opcode.
  414. -- @param script_name Name of the script function.
  415. -- @param priority Priority of the script.
  416. -- @param mode "request" does not wait, "start_sync" waits for the script to start and "end_sync" for
  417. -- it to end.
  418. party_request = function(character, script_name, priority, mode)
  419. local entity = entity_manager:get_entity_from_character_id(character)
  420. if entity == nil then
  421. print("party_request: there is no entity for the character " .. tostring(character) .. ".")
  422. return
  423. end
  424. local name = entity:get_name()
  425. if mode == "start_sync" then
  426. script:request_start_sync(Script.ENTITY, name, script_name, priority)
  427. elseif mode == "end_sync" then
  428. script:request_end_sync(Script.ENTITY, name, script_name, priority)
  429. else
  430. script:request(Script.ENTITY, name, script_name, priority)
  431. end
  432. end