triangle_triggers.lua 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. world_function(self)
  60. else
  61. print(
  62. "No world-map function for mesh " .. mesh_index
  63. .. ", function " .. function_id
  64. )
  65. end
  66. end,
  67. }
  68. EnterFieldScene = function(field_table_id, scenario)
  69. local field_table = System.WorldMap.field_table[tonumber(field_table_id)]
  70. local entry = field_table and field_table[tonumber(scenario)]
  71. if not entry then
  72. print("No installed field entry for FIELD.TBL record " .. tostring(field_table_id))
  73. return 0
  74. end
  75. System.MapChanger.world_map_entry = entry
  76. load_field_map_request(entry.field, "")
  77. return 0
  78. end