vehicles.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. EntityContainer = EntityContainer or {}
  2. local function show_vehicle_error(message)
  3. print(message)
  4. if not dialog:show_message(message) then
  5. return false
  6. end
  7. return true
  8. end
  9. local Vehicle = {}
  10. Vehicle.__index = Vehicle
  11. Vehicle.terrains = {}
  12. Vehicle.board = function(self)
  13. show_vehicle_error("Vehicle.board must be implemented by the vehicle.")
  14. return false
  15. end
  16. Vehicle.unboard = function(self)
  17. show_vehicle_error("Vehicle.unboard must be implemented by the vehicle.")
  18. return false
  19. end
  20. Vehicle.on_interact = function(self)
  21. self:board()
  22. end
  23. --[[
  24. Creates a vehicle that inherits the Vehicle interface.
  25. @param entity_name Managed world-map entity name.
  26. @param terrains Walkmap terrain type names this vehicle can travel.
  27. @param rise_amount Height offset applied when boarding.
  28. @return The vehicle script table.
  29. ]]
  30. local function extend_vehicle(entity_name, terrains, rise_amount, unboarding_terrains)
  31. return setmetatable({
  32. entity_name = entity_name,
  33. terrains = terrains,
  34. unboarding_terrains = unboarding_terrains or terrains,
  35. rise_amount = rise_amount,
  36. }, Vehicle)
  37. end
  38. local function is_unboarding_terrain_allowed(vehicle)
  39. local terrain_name = world_map_module:get_terrain_type_under_entity(vehicle.entity_name)
  40. for _, allowed_terrain in ipairs(vehicle.unboarding_terrains) do
  41. if terrain_name == allowed_terrain then
  42. return true
  43. end
  44. end
  45. return false, terrain_name
  46. end
  47. --[[
  48. Copies a terrain list so a Chocobo color change does not mutate a shared profile.
  49. @param terrains Terrain type names to copy.
  50. @return A new terrain list.
  51. ]]
  52. local function copy_terrain_list(terrains)
  53. local copy = {}
  54. for _, terrain_name in ipairs(terrains) do
  55. table.insert(copy, terrain_name)
  56. end
  57. return copy
  58. end
  59. local yellow_chocobo_terrains = {
  60. "GRASS", "FOREST", "SWAMP", "DESERT", "WASTELAND", "SNOW", "RIVERSIDE",
  61. "COREL_BRIDGE", "WUTAI_BRIDGE", "HILL_SIDE", "BEACH", "CANYON", "MOUNTAIN_PASS",
  62. "GOLD_SAUCER_DESERT_BORDER", "BRIDGE_HEAD",
  63. }
  64. local green_chocobo_terrains = copy_terrain_list(yellow_chocobo_terrains)
  65. table.insert(green_chocobo_terrains, "MOUNTAIN")
  66. table.insert(green_chocobo_terrains, "CLIFF")
  67. local blue_chocobo_terrains = copy_terrain_list(yellow_chocobo_terrains)
  68. table.insert(blue_chocobo_terrains, "RIVER_CROSSING")
  69. table.insert(blue_chocobo_terrains, "RIVER")
  70. table.insert(blue_chocobo_terrains, "WATER")
  71. local black_chocobo_terrains = copy_terrain_list(green_chocobo_terrains)
  72. table.insert(black_chocobo_terrains, "RIVER_CROSSING")
  73. table.insert(black_chocobo_terrains, "RIVER")
  74. table.insert(black_chocobo_terrains, "WATER")
  75. local gold_chocobo_terrains = {
  76. "GRASS", "FOREST", "MOUNTAIN", "SEA_DEEP_WATER", "RIVER_CROSSING", "RIVER", "WATER",
  77. "SWAMP", "DESERT", "WASTELAND", "SNOW", "RIVERSIDE", "CLIFF", "COREL_BRIDGE",
  78. "WUTAI_BRIDGE", "UNUSED_1", "HILL_SIDE", "BEACH", "SUBMARINE_PEN", "CANYON",
  79. "MOUNTAIN_PASS", "UNKNOWN_1", "WATERFALL", "UNUSED_2", "GOLD_SAUCER_DESERT", "JUNGLE",
  80. "SEA", "NORTHERND_CAVE", "GOLD_SAUCER_DESERT_BORDER", "BRIDGE_HEAD", "BACK_ENTRANCE",
  81. "UNUSED_3",
  82. }
  83. local chocobo_terrains_by_color = {
  84. YELLOW = yellow_chocobo_terrains,
  85. GREEN = green_chocobo_terrains,
  86. BLUE = blue_chocobo_terrains,
  87. BLACK = black_chocobo_terrains,
  88. GOLD = gold_chocobo_terrains,
  89. }
  90. EntityContainer["WorldMapHighwind"] = extend_vehicle(
  91. "WorldMapHighwind",
  92. {
  93. "GRASS", "FOREST", "MOUNTAIN", "SEA_DEEP_WATER", "RIVER_CROSSING", "RIVER",
  94. "WATER", "SWAMP", "DESERT", "WASTELAND", "SNOW", "RIVERSIDE", "CLIFF",
  95. "COREL_BRIDGE", "WUTAI_BRIDGE", "HILL_SIDE", "BEACH", "SUBMARINE_PEN", "CANYON",
  96. "MOUNTAIN_PASS", "UNKNOWN_1", "WATERFALL", "GOLD_SAUCER_DESERT", "JUNGLE", "SEA",
  97. "NORTHERND_CAVE", "GOLD_SAUCER_DESERT_BORDER", "BRIDGE_HEAD", "BACK_ENTRANCE",
  98. },
  99. 0.75,
  100. {"GRASS", "NORTHERND_CAVE"}
  101. )
  102. EntityContainer["WorldMapHighwind"].board = function(self)
  103. local player_entity = entity_manager:get_player_entity()
  104. local vehicle_entity = entity_manager:get_entity(self.entity_name)
  105. if not player_entity or not vehicle_entity then
  106. return false
  107. end
  108. local player_name = player_entity:get_name()
  109. if player_name ~= "WorldMapCloud" and player_name ~= "WorldMapChocobo" then
  110. show_vehicle_error("Leave your current vehicle before boarding another.")
  111. return false
  112. end
  113. vehicle_entity:move_by(0, self.rise_amount, 0)
  114. player_entity:set_visible(false)
  115. player_entity:set_solid(false)
  116. player_entity:set_talkable(false)
  117. world_map_module:clear_vehicle_terrains()
  118. for _, terrain_name in ipairs(self.terrains) do
  119. world_map_module:add_vehicle_terrain(terrain_name)
  120. end
  121. entity_manager:set_player_entity(self.entity_name)
  122. return true
  123. end
  124. EntityContainer["WorldMapHighwind"].unboard = function(self)
  125. if not is_unboarding_terrain_allowed(self) then
  126. return false
  127. end
  128. -- The module lands the Highwind on the ground, wherever it was flying.
  129. if not world_map_module:unboard_vehicle(self.entity_name) then
  130. return false
  131. end
  132. return true
  133. end
  134. EntityContainer["WorldMapBuggy"] = extend_vehicle(
  135. "WorldMapBuggy",
  136. {
  137. "GRASS", "FOREST", "SWAMP", "DESERT", "WASTELAND", "SNOW", "RIVERSIDE",
  138. "COREL_BRIDGE", "WUTAI_BRIDGE", "HILL_SIDE", "BEACH", "RIVER_CROSSING", "CANYON",
  139. "MOUNTAIN_PASS", "GOLD_SAUCER_DESERT", "JUNGLE", "GOLD_SAUCER_DESERT_BORDER",
  140. "BRIDGE_HEAD", "BACK_ENTRANCE",
  141. },
  142. 0,
  143. {
  144. "GRASS", "FOREST", "WASTELAND", "SNOW", "SWAMP", "DESERT", "COREL_BRIDGE",
  145. "HILL_SIDE", "BEACH", "CANYON", "MOUNTAIN_PASS", "JUNGLE",
  146. }
  147. )
  148. EntityContainer["WorldMapBuggy"].board = function(self)
  149. local player_entity = entity_manager:get_player_entity()
  150. local vehicle_entity = entity_manager:get_entity(self.entity_name)
  151. if not player_entity or not vehicle_entity then
  152. return false
  153. end
  154. if player_entity:get_name() ~= "WorldMapCloud" then
  155. show_vehicle_error("Leave your current vehicle before boarding another.")
  156. return false
  157. end
  158. player_entity:set_visible(false)
  159. player_entity:set_solid(false)
  160. player_entity:set_talkable(false)
  161. world_map_module:clear_vehicle_terrains()
  162. for _, terrain_name in ipairs(self.terrains) do
  163. world_map_module:add_vehicle_terrain(terrain_name)
  164. end
  165. entity_manager:set_player_entity(self.entity_name)
  166. return true
  167. end
  168. EntityContainer["WorldMapBuggy"].unboard = function(self)
  169. if not is_unboarding_terrain_allowed(self) then
  170. return false
  171. end
  172. return world_map_module:unboard_vehicle(self.entity_name)
  173. end
  174. EntityContainer["WorldMapChocobo"] = extend_vehicle(
  175. "WorldMapChocobo",
  176. copy_terrain_list(chocobo_terrains_by_color.YELLOW),
  177. 0,
  178. {
  179. "GRASS", "FOREST", "WASTELAND", "SNOW", "SWAMP", "DESERT", "COREL_BRIDGE",
  180. "HILL_SIDE", "BEACH", "CANYON", "MOUNTAIN_PASS", "JUNGLE",
  181. }
  182. )
  183. EntityContainer["WorldMapChocobo"].color = "YELLOW"
  184. EntityContainer["WorldMapChocobo"].set_color = function(self, color)
  185. if type(color) ~= "string" then
  186. return false
  187. end
  188. local color_name = string.upper(color)
  189. local terrains = chocobo_terrains_by_color[color_name]
  190. if not terrains then
  191. show_vehicle_error("Unknown Chocobo color: " .. color_name)
  192. return false
  193. end
  194. self.color = color_name
  195. self.terrains = copy_terrain_list(terrains)
  196. local player_entity = entity_manager:get_player_entity()
  197. if player_entity and player_entity:get_name() == self.entity_name then
  198. world_map_module:clear_vehicle_terrains()
  199. for _, terrain_name in ipairs(self.terrains) do
  200. world_map_module:add_vehicle_terrain(terrain_name)
  201. end
  202. end
  203. return true
  204. end
  205. EntityContainer["WorldMapChocobo"].board = function(self)
  206. local player_entity = entity_manager:get_player_entity()
  207. local vehicle_entity = entity_manager:get_entity(self.entity_name)
  208. if not player_entity or not vehicle_entity then
  209. return false
  210. end
  211. if player_entity:get_name() ~= "WorldMapCloud" then
  212. show_vehicle_error("Leave your current vehicle before boarding another.")
  213. return false
  214. end
  215. player_entity:set_visible(false)
  216. player_entity:set_solid(false)
  217. player_entity:set_talkable(false)
  218. world_map_module:clear_vehicle_terrains()
  219. for _, terrain_name in ipairs(self.terrains) do
  220. world_map_module:add_vehicle_terrain(terrain_name)
  221. end
  222. entity_manager:set_player_entity(self.entity_name)
  223. return true
  224. end
  225. EntityContainer["WorldMapChocobo"].unboard = function(self)
  226. if not is_unboarding_terrain_allowed(self) then
  227. return false
  228. end
  229. return world_map_module:unboard_vehicle(self.entity_name)
  230. end
  231. EntityContainer["WorldMapSubmarine"] = extend_vehicle(
  232. "WorldMapSubmarine",
  233. {"SEA_DEEP_WATER", "SEA", "SUBMARINE_PEN"},
  234. 0,
  235. {"SUBMARINE_PEN"}
  236. )
  237. EntityContainer["WorldMapSubmarine"].board = function(self)
  238. local player_entity = entity_manager:get_player_entity()
  239. local vehicle_entity = entity_manager:get_entity(self.entity_name)
  240. if not player_entity or not vehicle_entity then
  241. return false
  242. end
  243. if player_entity:get_name() ~= "WorldMapCloud" then
  244. show_vehicle_error("Leave your current vehicle before boarding another.")
  245. return false
  246. end
  247. player_entity:set_visible(false)
  248. player_entity:set_solid(false)
  249. player_entity:set_talkable(false)
  250. world_map_module:clear_vehicle_terrains()
  251. for _, terrain_name in ipairs(self.terrains) do
  252. world_map_module:add_vehicle_terrain(terrain_name)
  253. end
  254. entity_manager:set_player_entity(self.entity_name)
  255. return true
  256. end
  257. EntityContainer["WorldMapSubmarine"].unboard = function(self)
  258. if not is_unboarding_terrain_allowed(self) then
  259. return false
  260. end
  261. return world_map_module:unboard_vehicle(self.entity_name)
  262. end
  263. EntityContainer["WorldMapTinyBronco"] = extend_vehicle(
  264. "WorldMapTinyBronco",
  265. {
  266. "RIVERSIDE", "BEACH", "RIVER_CROSSING", "RIVER", "WATER",
  267. },
  268. 0,
  269. {"BEACH", "RIVERSIDE"}
  270. )
  271. EntityContainer["WorldMapTinyBronco"].board = function(self)
  272. local player_entity = entity_manager:get_player_entity()
  273. local vehicle_entity = entity_manager:get_entity(self.entity_name)
  274. if not player_entity or not vehicle_entity then
  275. return false
  276. end
  277. if player_entity:get_name() ~= "WorldMapCloud" then
  278. show_vehicle_error("Leave your current vehicle before boarding another.")
  279. return false
  280. end
  281. player_entity:set_visible(false)
  282. player_entity:set_solid(false)
  283. player_entity:set_talkable(false)
  284. world_map_module:clear_vehicle_terrains()
  285. for _, terrain_name in ipairs(self.terrains) do
  286. world_map_module:add_vehicle_terrain(terrain_name)
  287. end
  288. entity_manager:set_player_entity(self.entity_name)
  289. return true
  290. end
  291. EntityContainer["WorldMapTinyBronco"].unboard = function(self)
  292. if not is_unboarding_terrain_allowed(self) then
  293. return false
  294. end
  295. return world_map_module:unboard_vehicle(self.entity_name)
  296. end