field.lua 20 KB

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