triangle_triggers.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. System.WorldMap = {
  2. triangle_triggers = {},
  3. triangle_functions = {},
  4. field_table = {},
  5. --[[
  6. Registers a callback for a WM0 mesh function.
  7. @param mesh_x World-map mesh X coordinate (0-35).
  8. @param mesh_y World-map mesh Y coordinate (0-27).
  9. @param function_id Triangle mesh function ID (0-4).
  10. @param callback Function called when the player enters the trigger.
  11. @return True when registered, false for invalid input.
  12. ]]
  13. register_triangle_trigger = function(self, mesh_x, mesh_y, function_id, callback)
  14. if type(mesh_x) ~= "number" or mesh_x < 0 or mesh_x > 35
  15. or type(mesh_y) ~= "number" or mesh_y < 0 or mesh_y > 27
  16. or type(function_id) ~= "number" or function_id < 0 or function_id > 4
  17. or type(callback) ~= "function" then
  18. return false
  19. end
  20. local mesh_triggers = self.triangle_triggers[mesh_y]
  21. if not mesh_triggers then
  22. mesh_triggers = {}
  23. self.triangle_triggers[mesh_y] = mesh_triggers
  24. end
  25. local function_triggers = mesh_triggers[mesh_x]
  26. if not function_triggers then
  27. function_triggers = {}
  28. mesh_triggers[mesh_x] = function_triggers
  29. end
  30. function_triggers[function_id] = callback
  31. return true
  32. end,
  33. --[[
  34. Dispatches an entered WM0 mesh function to its registered callback.
  35. @param self World-map system script table.
  36. @param mesh_index Linear mesh index (mesh_y * 36 + mesh_x).
  37. @param function_id Triangle mesh function ID.
  38. ]]
  39. on_triangle_enter = function(self, mesh_index, function_id)
  40. mesh_index = tonumber(mesh_index)
  41. function_id = tonumber(function_id)
  42. if not mesh_index or not function_id then
  43. return
  44. end
  45. local mesh_x = mesh_index % 36
  46. local mesh_y = math.floor(mesh_index / 36)
  47. local mesh_triggers = self.triangle_triggers[mesh_y]
  48. local function_triggers = mesh_triggers and mesh_triggers[mesh_x]
  49. local callback = function_triggers and function_triggers[function_id]
  50. if callback then
  51. callback(mesh_x, mesh_y, function_id)
  52. return
  53. end
  54. local mesh_functions = self.triangle_functions[mesh_index]
  55. local function_index = mesh_functions and mesh_functions[function_id]
  56. local world_function = function_index
  57. and self["world_function_" .. tostring(function_index)]
  58. if world_function then
  59. -- A place can only be entered by the vehicles that the settings say (WorldMapSettings.entry).
  60. local record = WorldVM and WorldVM.find_field_entry(world_function)
  61. if record and not WorldVM.can_enter(record, self.vehicle, self.landing) then
  62. return
  63. end
  64. world_function(self)
  65. else
  66. print(
  67. "No world-map function for mesh " .. mesh_index
  68. .. ", function " .. function_id
  69. )
  70. end
  71. end,
  72. }
  73. --[[
  74. Memory of the world map scripts (Read, ReadWord, ReadBit and their Write functions).
  75. The variables of the game are the savemap from 0xBA4, the same as the memory banks of the field
  76. scripts: the address (or the bit, for ReadBit) goes through 256 bytes for each pair of banks, 1 and 2,
  77. 3 and 4, 11 and 12, 13 and 14, and 7 and 15. The first one of the pair is the one for bytes and bits
  78. (Banks[1][127] and the bit 3 of it are Read(127) and ReadBit(1019)), and the second
  79. one the one for words (Banks[2][0], the story counter, is ReadWord(0)). That way what the fields give
  80. (the vehicles, the progress of the story) is what the world has.
  81. ReadLocal and ReadSpecial are not game variables: the first is the memory of the scripts and the second
  82. what the engine gives them (the way in which the world was entered, in System.WorldMap.specials: 5 is 0
  83. when entering from a field and 6 the FIELD.TBL record of the exit).
  84. ]]
  85. local BYTE_BANKS = {[0] = 1, 3, 11, 13, 7}
  86. local WORD_BANKS = {[0] = 2, 4, 12, 14, 15}
  87. --- Journal of the writes, when System.WorldMap.journal is a table: WorldVM undoes the changes of the
  88. -- functions that it runs only to see what they do.
  89. local function remember(container, key)
  90. local journal = System.WorldMap.journal
  91. if journal then journal[#journal + 1] = {container, key, container[key]} end
  92. end
  93. --- Where a byte address of the game variables is.
  94. -- @return The bank for bytes, the one for words and the address in them. nil if it is not in a bank.
  95. local function locate(address)
  96. local index = math.floor(address / 256)
  97. if index < 0 or index > 4 then return nil end
  98. return BYTE_BANKS[index], WORD_BANKS[index], address % 256
  99. end
  100. --- Reads a byte of the game variables.
  101. Read = function(address)
  102. local bank, _, offset = locate(address)
  103. return bank and Banks[bank][offset] or 0
  104. end
  105. --- Reads a word of the game variables.
  106. ReadWord = function(address)
  107. local _, bank, offset = locate(address)
  108. return bank and Banks[bank][offset] or 0
  109. end
  110. --- Reads a bit of the game variables, as the address in bits.
  111. ReadBit = function(bit_address)
  112. local bank, _, offset = locate(math.floor(bit_address / 8))
  113. if not bank then return 0 end
  114. return get_byte_nth_bit(Banks[bank][offset], bit_address % 8)
  115. end
  116. --- Writes a byte of the game variables.
  117. Write = function(address, value)
  118. local bank, _, offset = locate(address)
  119. if not bank then return end
  120. remember(Banks[bank], offset)
  121. Banks[bank][offset] = value
  122. end
  123. --- Writes a word of the game variables.
  124. WriteWord = function(address, value)
  125. local _, bank, offset = locate(address)
  126. if not bank then return end
  127. remember(Banks[bank], offset)
  128. Banks[bank][offset] = value
  129. end
  130. --- Writes a bit of the game variables, as the address in bits.
  131. WriteBit = function(bit_address, value)
  132. local bank, _, offset = locate(math.floor(bit_address / 8))
  133. if not bank then return end
  134. -- The bits are the ones of the bytes (the fields set them with BITON/set_bank_bit on Banks).
  135. remember(Banks[bank], offset)
  136. Banks[bank][offset] = set_byte_nth_bit(Banks[bank][offset], bit_address % 8, value ~= 0 and 1 or 0)
  137. end
  138. System.WorldMap.locals = {}
  139. System.WorldMap.specials = {}
  140. --- Reads a value of the memory of the scripts.
  141. ReadLocal = function(address)
  142. return System.WorldMap.locals[address] or 0
  143. end
  144. --- Writes a value of the memory of the scripts.
  145. WriteLocal = function(address, value)
  146. System.WorldMap.locals[address] = value
  147. end
  148. --- Reads a value that the engine gives to the scripts.
  149. ReadSpecial = function(address)
  150. return System.WorldMap.specials[address] or 0
  151. end
  152. --- Writes a value that the engine gives to the scripts.
  153. WriteSpecial = function(address, value)
  154. System.WorldMap.specials[address] = value
  155. end
  156. --- The map of the module (0 WM0, 1 WM2, 2 WM3) that a field exit to the world map goes to.
  157. -- It is what the game does with the record: some of them are the underwater map (the submarine, 30, 31,
  158. -- 42 and 56) and from 60 on the snowstorm one.
  159. -- @param point_name Spawn point name of the exit (it has the coordinates).
  160. -- @param record FIELD.TBL record of the exit.
  161. -- @return The map.
  162. local function world_map_for_record(record)
  163. if record == 30 or record == 31 or record == 42 or record == 56 then return 1 end
  164. if record >= 60 then return 2 end
  165. return 0
  166. end
  167. --- Goes from a field to the world map by a field exit.
  168. -- @param record FIELD.TBL record of the exit. The scripts of the world know where the player and the
  169. -- vehicles are by it.
  170. enter_world_map = function(record, point_name)
  171. -- The "wm<N>" of the fields is one less than the record that the scripts of the world use (the exit of
  172. -- Fort Condor is wm5 and its record is 6, the one of the submarine station wm41 and its record 42).
  173. record = record + 1
  174. -- Every field script starts with EntityContainer = {}, which deletes the scripts of the vehicles.
  175. local vehicles = loadfile("./engine/scripts/world/vehicles.lua")
  176. if vehicles then vehicles() else print("Could not reload the scripts of the vehicles") end
  177. System.WorldMap.arrival = record
  178. System.WorldMap.arrival_name = point_name
  179. console("camera_free true")
  180. MenuSettings.pause_available = true
  181. world_map_module:enter(world_map_for_record(record))
  182. end
  183. EnterFieldScene = function(field_table_id, scenario)
  184. local field_table = System.WorldMap.field_table[tonumber(field_table_id)]
  185. local entry = field_table and field_table[tonumber(scenario)]
  186. if not entry then
  187. print("No installed field entry for FIELD.TBL record " .. tostring(field_table_id))
  188. return 0
  189. end
  190. System.MapChanger.world_map_entry = entry
  191. -- Where the player was when entering the field: some exits (the chocobo records, such as the one
  192. -- of sichi) place the player where he entered, and the scripts read it as the values 0, 1 (the
  193. -- mesh) and 4 (the direction) that the engine gives them.
  194. local player = entity_manager:get_player_entity()
  195. if player ~= nil and WorldVM ~= nil then
  196. local x, _, z = player:get_position()
  197. System.WorldMap.entry_position = {
  198. x = x,
  199. z = z,
  200. mesh_x = math.floor(x / WorldVM.MESH_SIZE),
  201. mesh_y = math.floor(z / WorldVM.MESH_SIZE),
  202. direction = world_map_module:get_model_heading(player:get_name()),
  203. }
  204. end
  205. console("camera_free false")
  206. load_field_map_request(entry.field, "")
  207. return 0
  208. end