field.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. player:set_position(x, y, z)
  65. player:set_rotation(rotation)
  66. player:set_solid(true)
  67. player:set_visible(true)
  68. end
  69. end
  70. entity_manager:set_player_entity(entity_name)
  71. local world_map_entry = System.MapChanger.world_map_entry
  72. if world_map_entry then
  73. entity_manager:place_player_at_field_entry(
  74. world_map_entry.x, world_map_entry.y,
  75. world_map_entry.triangle, world_map_entry.direction
  76. )
  77. System.MapChanger.world_map_entry = nil
  78. end
  79. end
  80. --- Assigns an entity to a playable character.
  81. --
  82. -- @param entity_name The entity name.
  83. -- @param character_id Id of the character to assign.
  84. set_entity_to_character = function(entity_name, character_id)
  85. if character_id == Party[1] and entity_name ~= "" then
  86. place_player_at_map_entry(entity_name)
  87. end
  88. entity_manager:set_entity_to_character(entity_name, character_id)
  89. end
  90. --- Joins the party members in the field.
  91. --
  92. -- @param speed Speed at which the characters join
  93. join_party = function(speed)
  94. local player = entity_manager:get_player_entity()
  95. if (player == nil) then
  96. do return end
  97. end
  98. local x, y, z = player:get_position()
  99. for c = 2, 3 do
  100. if Party[c] ~= nil then
  101. local character = entity_manager:get_entity_from_character_id(Party[c])
  102. --print(" Name: " .. character:get_name())
  103. if character ~= nil then
  104. local cur_speed = character:get_move_auto_speed()
  105. character:set_solid(false)
  106. -- Approximated speed, good enough for now.
  107. -- TODO: Calculate speed based on time, as the orignal opcode.
  108. character:set_move_auto_speed(speed / 16)
  109. character:move_to_position(x, y)
  110. character:move_sync()
  111. character:set_move_auto_speed(cur_speed)
  112. character:set_visible(false)
  113. character:set_talkable(false)
  114. end
  115. end
  116. end
  117. end
  118. --- Splits the party members in the field.
  119. --
  120. -- @param ax_addr Bank for ax, or zero if ax is specified as a literal value.
  121. -- @param ay_addr Bank for ay, or zero if ay is specified as a literal value.
  122. -- @param ar_addr Bank for ar, or zero if ar is specified as a literal value.
  123. -- @param bx_addr Bank for bx, or zero if bx is specified as a literal value.
  124. -- @param by_addr Bank for by, or zero if by is specified as a literal value.
  125. -- @param br_addr Bank for br, or zero if by is specified as a literal value.
  126. -- @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.
  127. -- @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.
  128. -- @param ar Direction the second character faces after the split, or address for the value if ar_addr is non-zero.
  129. -- @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.
  130. -- @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.
  131. -- @param br Direction the third character faces after the split, or address for the value if br_addr is non-zero.
  132. -- @param speed Speed at which the characters split.
  133. split_party = function(ax, ay, ar, bx, by, br, speed)
  134. local player = entity_manager:get_player_entity()
  135. local x, y, z = player:get_position()
  136. for c = 2, 3 do
  137. if Party[c] ~= nil then
  138. local character = entity_manager:get_entity_from_character_id(Party[c])
  139. if character ~= nil then
  140. local cur_speed = character:get_move_auto_speed()
  141. -- Approximated speed, good enough for now.
  142. -- TODO: Calculate speed based on time, as the orignal opcode.
  143. character:set_move_auto_speed(speed / 16)
  144. --if character:is_visible() == false then
  145. -- In theory, a member is never visible before a split, but some maps
  146. -- are wrong. In these cases, don't change position and move the entity
  147. -- from it's current position.
  148. character:set_position(x, y, z)
  149. character:set_visible(true)
  150. --end
  151. character:set_solid(false)
  152. if c == 2 then
  153. character:move_to_position(ax, ay)
  154. elseif c == 3 then
  155. character:move_to_position(bx, by)
  156. end
  157. character:move_sync()
  158. if c == 2 then
  159. character:set_rotation(ar)
  160. elseif c == 3 then
  161. character:set_rotation(br)
  162. end
  163. character:set_move_auto_speed(cur_speed)
  164. character:set_solid(true)
  165. character:set_talkable(true)
  166. end
  167. end
  168. end
  169. end
  170. --- Locks or unlocks player movement and menu access.
  171. -- @param lock True to lock, false to unlock.
  172. player_lock = function(lock)
  173. entity_manager:player_lock(lock)
  174. MenuSettings.available = (lock == false)
  175. end
  176. --- Sets a bit variable on (1).
  177. --
  178. -- @param bank Memory bank (0-index, as in game).
  179. -- @param variable Bank address (0-index, as in game).
  180. -- @param bit Address bit (0-index, as in game).
  181. bit_on = function(bank, variable, bit)
  182. BitBanks[bank][variable][bit] = 1
  183. end
  184. --- Sets a bit variable off (0).
  185. --
  186. -- @param bank Memory bank (0-index, as in game).
  187. -- @param variable Bank address (0-index, as in game).
  188. -- @param bit Address bit (0-index, as in game).
  189. bit_off = function(bank, variable, bit)
  190. BitBanks[bank][variable][bit] = 0
  191. end
  192. --- Retrieves a bit variable.
  193. --
  194. -- @param bank Memory bank (0-index, as in game).
  195. -- @param variable Bank address (0-index, as in game).
  196. -- @param bit Address bit (0-index, as in game).
  197. -- @return 1 or 0
  198. bit = function(bank, variable, bit)
  199. return BitBanks[bank][variable][bit]
  200. end
  201. --- Stores the coordinates and walkmesh triangle in memory.
  202. --
  203. -- Stores the values as in the original game, without scaling.
  204. -- @param bx Bank at which to store the X coordinate.
  205. -- @param by Bank at which to store the Y coordinate.
  206. -- @param bz Bank at which to store the Z coordinate.
  207. -- @param bt Bank at which to store the triangle.
  208. -- @param id The entity ID.
  209. -- @param ax Address at which to store the X coordinate.
  210. -- @param ay Address at which to store the Y coordinate.
  211. -- @param az Address at which to store the Z coordinate.
  212. -- @param at Address at which to store the triangle.
  213. -- @param scale Map scale to multiply values
  214. axyzi = function(bx, by, bz, bt, id, ax, ay, az, at, scale)
  215. local entity = entity_manager:get_entity_from_index(id)
  216. if entity == nil then
  217. do return end
  218. end
  219. local x, y, z = entity:get_position()
  220. local t = entity:get_move_triangle_id()
  221. Banks[bx][ax] = math.floor(x * scale)
  222. Banks[by][ay] = math.floor(y * scale)
  223. Banks[bz][az] = math.floor(z * scale)
  224. Banks[bt][at] = t
  225. return 0
  226. end
  227. --- Stores the X and Y position of an entity in two variables.
  228. --
  229. -- @param bx Bank for X.
  230. -- @param by Bank for Y.
  231. -- @param id Index of the entity in the field.
  232. -- @param ax Address at which to store the X coordinate.
  233. -- @param ay Address at which to store the Y coordinate.
  234. -- @param scale Map scale to multiply values
  235. getaxy = function(bx, by, id, ax, ay, scale)
  236. local entity = entity_manager:get_entity_from_index(id)
  237. if entity == nil then
  238. do return end
  239. end
  240. local x, y = entity:get_position()
  241. Banks[bx][ax] = math.floor(x * scale)
  242. Banks[by][ay] = math.floor(y * scale)
  243. return 0
  244. end
  245. --- Shows the ending credits.
  246. ending_credits = function()
  247. -- TODO: Implement. Probably in a looooong loooong time.
  248. end
  249. --- Displays a window where a name can be assigned to a character.
  250. --
  251. -- @param id ID of the character to name.
  252. name_character = function(id)
  253. -- TODO: Implement.
  254. if Characters[id] == nil then
  255. print("Tried to rename invalid character with ID " .. tostring(id))
  256. return 0
  257. end
  258. -- While the name menu is not enabled, the character just gets its default name.
  259. if not UiContainer.NameMenu.enabled then
  260. UiContainer.NameMenu:apply_default_name(id)
  261. return 0
  262. end
  263. UiContainer.NameMenu.id = id
  264. UiContainer.NameMenu.chocobo = false
  265. script:request(Script.UI, "NameMenu", "show", 0)
  266. end
  267. --- Displays a window where a name can be assigned to a chocobo.
  268. name_chocobo = function()
  269. -- TODO: Implement.
  270. end
  271. --- Automatically opens a menu.
  272. --
  273. -- @param menu The name of the menu.
  274. open_menu = function(menu)
  275. if menu == "party" then
  276. -- TODO
  277. elseif menu == "save" then
  278. -- TODO
  279. end
  280. end
  281. --- Opens a shop.
  282. --
  283. -- @param id Shop ID.
  284. open_shop = function(id)
  285. -- TODO: Implement
  286. end
  287. --- Plays one of the tracks assigned to the field.
  288. --
  289. -- @param id ID of the track in the field.
  290. play_map_music = function(id)
  291. local track_id = audio_manager:get_track_id(id)
  292. if track_id ~= -1 then
  293. audio_manager:play_music(tostring(track_id))
  294. else
  295. print("Requested non-existent music track ID " .. tostring(id))
  296. end
  297. end
  298. --- Sets the map name
  299. --
  300. -- It's not set from a literal text, but from a dialog identifier.
  301. --
  302. -- @param text_id ID of the dialog text that holds the map name.
  303. set_map_name = function(text_id)
  304. dialog:set_map_name(text_id)
  305. System["MapChanger"].location_name = dialog:get_map_name()
  306. end
  307. --- Shows a choice dialog and stores the result in the selected bank address.
  308. --
  309. -- @param window_id ID of the window to show the dialog in.
  310. -- @param message_id ID of the text to show in the window.
  311. -- @param first First selectable line.
  312. -- @param last Last selectable line.
  313. -- @param bank Data bank to store the selected line in.
  314. -- @param address Addres of the bank at which to store the selected line in.
  315. dialog_ask = function(window_id, message_id, first, last, bank, address)
  316. dialog:set_clickable(window_id, true)
  317. dialog:set_cursor(window_id, first, last)
  318. dialog:dialog_set_text(window_id, message_id)
  319. dialog:dialog_wait_for_close(window_id)
  320. Banks[bank][address] = dialog:get_cursor(window_id)
  321. end
  322. --- Utility to change fields and enter battles.
  323. System["MapChanger"] = {
  324. --- ID of the map to change to.
  325. map_name = "",
  326. --- Point of the map to change to.
  327. point_name = "",
  328. world_map_entry = nil,
  329. --- ID of the current field.
  330. current_map_name = "",
  331. --- Location name, to display in the main menu and in save slots.
  332. location_name = "",
  333. --- Jumps to a new map.
  334. ffvii_field = function(self)
  335. if self.map_name ~= "" then
  336. player_lock(true) -- disable menu and pc movement while load map
  337. script:request_end_sync(Script.UI, "Fade", "fade_out", 0)
  338. -- The fields go to the world map as "wm<n>", n being the FIELD.TBL record of the exit.
  339. local world_record = tonumber(string.match(self.map_name, "^wm(%d+)$"))
  340. if world_record then
  341. enter_world_map(world_record, self.point_name)
  342. else
  343. map(self.map_name)
  344. end
  345. player_lock(false) -- enable menu and pc movement after load map
  346. self.current_map_name = self.map_name
  347. self.map_name = ""
  348. script:request_end_sync(Script.UI, "Fade", "fade_in", 0)
  349. end
  350. return 0
  351. end,
  352. --- Starts a battle.
  353. --
  354. -- @todo Implement.
  355. ffvii_battle = function(self)
  356. if self.map_name ~= "" then
  357. player_lock(true) -- disable menu and pc movement while load map
  358. script:request_end_sync(Script.UI, "Fade", "fade_out", 0)
  359. map(self.map_name)
  360. -- load battle ui
  361. -- load player entity
  362. --self.map_name = ""
  363. script:request_end_sync(Script.UI, "Fade", "fade_in", 0)
  364. end
  365. return 0
  366. end,
  367. }