1 2 /* 3 * Copyright (C) 2006-2019 Christopho, Solarus - http://www.solarus-games.org 4 * 5 * Solarus is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * Solarus is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License along 16 * with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 #ifndef SOLARUS_LUA_CONTEXT_H 19 #define SOLARUS_LUA_CONTEXT_H 20 21 #include "solarus/core/Common.h" 22 #include "solarus/core/Ability.h" 23 #include "solarus/core/Debug.h" 24 #include "solarus/core/GameCommands.h" 25 #include "solarus/core/InputEvent.h" 26 #include "solarus/core/TimerPtr.h" 27 #include "solarus/entities/Camera.h" 28 #include "solarus/entities/EnemyAttack.h" 29 #include "solarus/entities/EntityPtr.h" 30 #include "solarus/entities/EntityType.h" 31 #include "solarus/entities/Ground.h" 32 #include "solarus/entities/HeroPtr.h" 33 #include "solarus/graphics/DrawablePtr.h" 34 #include "solarus/graphics/ShaderPtr.h" 35 #include "solarus/graphics/SpritePtr.h" 36 #include "solarus/graphics/SurfacePtr.h" 37 #include "solarus/lua/ExportableToLuaPtr.h" 38 #include "solarus/lua/ScopedLuaRef.h" 39 #include "solarus/lua/LuaTools.h" 40 #include <lua.hpp> 41 #include <list> 42 #include <map> 43 #include <memory> 44 #include <set> 45 #include <string> 46 #include <vector> 47 #include <queue> 48 #include <functional> 49 50 namespace Solarus { 51 52 class Block; 53 class Camera; 54 class CarriedObject; 55 class Chest; 56 class CircleMovement; 57 class Color; 58 class CustomEntity; 59 class CustomState; 60 class Destination; 61 class Destructible; 62 class Dialog; 63 class Door; 64 class Drawable; 65 class DynamicTile; 66 class Enemy; 67 class EnemyReaction; 68 class Entity; 69 class EntityData; 70 class ExportableToLua; 71 class EquipmentItem; 72 class Game; 73 class JumpMovement; 74 class MainLoop; 75 class Map; 76 class Movement; 77 class Npc; 78 class PathFindingMovement; 79 class PathMovement; 80 class PixelMovement; 81 class Pickable; 82 class Point; 83 class RandomMovement; 84 class RandomPathMovement; 85 class Sensor; 86 class Separator; 87 class Shader; 88 class ShopTreasure; 89 class Sprite; 90 class StraightMovement; 91 class Stairs; 92 class Stream; 93 class Switch; 94 class TargetMovement; 95 class Teletransporter; 96 class TextSurface; 97 class Timer; 98 class Treasure; 99 100 class Arguments; 101 102 using EntityVector = std::vector<EntityPtr>; 103 104 /** 105 * \brief This class represents a living Lua context that can execute quest 106 * scripts at runtime. 107 * 108 * Such scripts include the quest main script, map scripts, enemy behaviors, 109 * etc. This class implements the Solarus scripting API that scripts can use. 110 * 111 * All these scripts run in the same Lua context. This means that they share 112 * global values and other global mechanisms like the registry and metatables. 113 * 114 * On the contrary, data files that happen to have a Lua-compatible syntax 115 * (like maps) are always parsed in their own, independent Lua states. 116 * These files are considered as pure data (not code) and only use the 117 * LuaTools class, not this class. 118 */ 119 class LuaContext { 120 121 public: 122 123 // Functions and types. 124 static const std::string main_module_name; 125 static const std::string audio_module_name; 126 static const std::string video_module_name; 127 static const std::string input_module_name; 128 static const std::string file_module_name; 129 static const std::string timer_module_name; 130 static const std::string game_module_name; 131 static const std::string map_module_name; 132 static const std::string item_module_name; 133 static const std::string surface_module_name; 134 static const std::string text_surface_module_name; 135 static const std::string sprite_module_name; 136 static const std::string menu_module_name; 137 static const std::string language_module_name; 138 static const std::string shader_module_name; 139 static const std::string state_module_name; 140 static const std::string movement_module_name; 141 static const std::string movement_straight_module_name; 142 static const std::string movement_random_module_name; 143 static const std::string movement_target_module_name; 144 static const std::string movement_path_module_name; 145 static const std::string movement_random_path_module_name; 146 static const std::string movement_path_finding_module_name; 147 static const std::string movement_circle_module_name; 148 static const std::string movement_jump_module_name; 149 static const std::string movement_pixel_module_name; 150 151 explicit LuaContext(MainLoop& main_loop); 152 ~LuaContext(); 153 154 static LuaContext& get(); 155 lua_State* get_internal_state(); 156 lua_State* get_main_state(); 157 158 MainLoop& get_main_loop(); 159 160 // Main loop from C++. 161 void initialize(const Arguments &args); 162 void exit(); 163 void update(); 164 bool notify_input(const InputEvent& event); 165 void notify_map_suspended(Map& map, bool suspended); 166 void notify_shop_treasure_interaction(ShopTreasure& shop_treasure); 167 void notify_hero_brandish_treasure( 168 const Treasure& treasure, 169 const ScopedLuaRef& callback_ref 170 ); 171 bool notify_dialog_started( 172 Game& game, 173 const Dialog& dialog, 174 const ScopedLuaRef& info_ref 175 ); 176 void notify_dialog_finished( 177 Game& game, 178 const Dialog& dialog, 179 const ScopedLuaRef& callback_ref, 180 const ScopedLuaRef& status_ref 181 ); 182 void run_item(EquipmentItem& item); 183 void run_map(Map& map, const std::shared_ptr<Destination>& destination); 184 void run_enemy(Enemy& enemy); 185 void run_custom_entity(CustomEntity& custom_entity); 186 187 void warning_deprecated( 188 const std::pair<int, int>& version_deprecating, 189 const std::string& function_name, 190 const std::string& message 191 ); 192 static void print_stack(lua_State* current_l); 193 194 // Lua refs. 195 ScopedLuaRef create_ref(); 196 static void push_ref(lua_State* current_l, const ScopedLuaRef& ref); 197 198 // Executing Lua code. 199 bool load_file(const std::string& script_name); 200 void do_file(const std::string& script_name); 201 bool do_file_if_exists(const std::string& script_name); 202 bool do_string(const std::string& code, const std::string& chunk_name); 203 bool do_string_with_easy_env(const std::string& code, const std::string& chunk_name); 204 205 //TODO put those templates impl in a LuaContext.inl 206 //Getting across coroutines state 207 template<typename Callable> 208 /** 209 * @brief run given closure on main lua thread 210 * @param current current lua state 211 * @param func a void(lua_State* main) closure 212 */ run_on_main(Callable && func)213 static void run_on_main(Callable&& func) { 214 auto& c = LuaContext::get(); 215 if (c.current_l == c.main_l) { 216 func(c.current_l); 217 } else { 218 c.cross_state_callbacks.push(func); 219 } 220 } 221 222 template<typename Callable> state_boundary_handle(lua_State * l,Callable && func)223 static int state_boundary_handle(lua_State* l, Callable&& func) { 224 lua_State* previous = lua_context->get_internal_state(); 225 lua_context->set_current_state(l); 226 int result = LuaTools::exception_boundary_handle(l,func); 227 lua_context->set_current_state(previous); 228 return result; 229 } 230 231 static void set_current_state(lua_State* l); 232 233 // Calling Lua functions. 234 bool call_function( 235 int nb_arguments, 236 int nb_results, 237 const char* function_name 238 ); 239 240 static bool is_solarus_userdata( 241 lua_State* current_l, 242 int index, 243 std::string& module_name 244 ); 245 bool userdata_has_field( 246 const ExportableToLua& userdata, 247 const char* key 248 ) const; 249 bool userdata_has_field( 250 const ExportableToLua& userdata, 251 const std::string& key 252 ) const; 253 void notify_userdata_destroyed(ExportableToLua& userdata); 254 void userdata_close_lua(); 255 256 // Timers. 257 void add_timer( 258 const TimerPtr& timer, 259 int context_index, 260 const ScopedLuaRef& callback_index 261 ); 262 void remove_timer(const TimerPtr& timer); 263 void remove_timers(int context_index); 264 void destroy_timers(); 265 void update_timers(); 266 void notify_timers_map_suspended(bool suspended); 267 void set_entity_timers_suspended_as_map(Entity& entity, bool suspended); 268 void do_timer_callback(const TimerPtr& timer); 269 270 // Menus. 271 void add_menu( 272 const ScopedLuaRef& menu_ref, 273 int context_index, 274 bool on_top 275 ); 276 void remove_menus(int context_index); 277 void remove_menus(); 278 void destroy_menus(); 279 void update_menus(); 280 281 // Drawable objects. 282 bool has_drawable(const DrawablePtr& drawable); 283 void add_drawable(const DrawablePtr& drawable); 284 void remove_drawable(const DrawablePtr& drawable); 285 void destroy_drawables(); 286 void update_drawables(); 287 288 // Movements. 289 void start_movement_on_point( 290 const std::shared_ptr<Movement>& movement, 291 int point_index 292 ); 293 void stop_movement_on_point(const std::shared_ptr<Movement>& movement); 294 void update_movements(); 295 296 // Maps. 297 static void check_map_has_game(lua_State* current_l, const Map& map); 298 299 // Entities. 300 static const std::string& get_entity_internal_type_name(EntityType entity_type); 301 bool create_map_entity_from_data(Map& map, const EntityData& entity_data); 302 303 void do_entity_draw_override_function( 304 const ScopedLuaRef& draw_override, 305 Entity& entity, 306 Camera& camera 307 ); 308 bool do_traversable_test_function( 309 const ScopedLuaRef& traversable_test_ref, 310 ExportableToLua& userdata, 311 Entity& other_entity 312 ); 313 bool do_custom_entity_collision_test_function( 314 const ScopedLuaRef& collision_test_ref, 315 CustomEntity& custom_entity, 316 Entity& other_entity 317 ); 318 void do_custom_entity_collision_callback( 319 const ScopedLuaRef& callback_ref, 320 CustomEntity& custom_entity, 321 Entity& other_entity 322 ); 323 void do_custom_entity_collision_callback( 324 const ScopedLuaRef& callback_ref, 325 CustomEntity& custom_entity, 326 Entity& other_entity, 327 Sprite& custom_entity_sprite, 328 Sprite& other_entity_sprite 329 ); 330 void do_state_draw_override_function( 331 const ScopedLuaRef& draw_override, 332 CustomState& state, 333 Camera& camera 334 ); 335 bool do_state_can_be_hurt_function( 336 const ScopedLuaRef& can_be_hurt, 337 CustomState& state, 338 Entity* attacker 339 ); 340 bool do_state_can_cut_function( 341 const ScopedLuaRef& can_cut, 342 CustomState& state, 343 Entity* entity 344 ); 345 346 // Main loop events (sol.main). 347 void main_on_started(); 348 void main_on_finished(); 349 void main_on_update(); 350 void main_on_draw(const SurfacePtr& dst_surface); 351 bool main_on_input(const InputEvent& event); 352 353 // Video events. 354 void video_on_draw(const SurfacePtr& screen); 355 356 // Menu events. 357 void menu_on_started(const ScopedLuaRef& menu_ref); 358 void menu_on_finished(const ScopedLuaRef& menu_ref); 359 void menu_on_update(const ScopedLuaRef& menu_ref); 360 void menu_on_draw(const ScopedLuaRef& menu_ref, const SurfacePtr& dst_surface); 361 bool menu_on_input(const ScopedLuaRef& menu_ref, const InputEvent& event); 362 bool menu_on_command_pressed( 363 const ScopedLuaRef& menu_ref, 364 GameCommand command 365 ); 366 bool menu_on_command_released( 367 const ScopedLuaRef& menu_ref, 368 GameCommand command 369 ); 370 void menus_on_update(int context_index); 371 void menus_on_draw(int context_index, const SurfacePtr& dst_surface); 372 bool menus_on_input(int context_index, const InputEvent& event); 373 bool menus_on_command_pressed(int context_index, GameCommand command); 374 bool menus_on_command_released(int context_index, GameCommand command); 375 376 // Sprite events. 377 void sprite_on_animation_finished( 378 Sprite& sprite, const std::string& animation); 379 void sprite_on_animation_changed( 380 Sprite& sprite, const std::string& animation); 381 void sprite_on_direction_changed( 382 Sprite& sprite, const std::string& animation, int direction); 383 void sprite_on_frame_changed( 384 Sprite& sprite, const std::string& animation, int frame); 385 386 // Movement events. 387 void movement_on_position_changed(Movement& movement, const Point& xy); 388 void movement_on_obstacle_reached(Movement& movement); 389 void movement_on_changed(Movement& movement); 390 void movement_on_finished(Movement& movement); 391 392 // Equipment item events. 393 void item_on_created(EquipmentItem& item); 394 void item_on_started(EquipmentItem& item); 395 void item_on_finished(EquipmentItem& item); 396 void item_on_update(EquipmentItem& item); 397 void item_on_suspended(EquipmentItem& item, bool suspended); 398 void item_on_map_changed(EquipmentItem& item, Map& map); 399 void item_on_pickable_created(EquipmentItem& item, Pickable& pickable); 400 void item_on_obtaining(EquipmentItem& item, const Treasure& treasure); 401 void item_on_obtained(EquipmentItem& item, const Treasure& treasure); 402 void item_on_variant_changed(EquipmentItem& item, int variant); 403 void item_on_amount_changed(EquipmentItem& item, int amount); 404 void item_on_using(EquipmentItem& item); 405 void item_on_ability_used(EquipmentItem& item, Ability ability); 406 void item_on_npc_interaction(EquipmentItem& item, Npc& npc); 407 bool item_on_npc_interaction_item(EquipmentItem& item, Npc& npc, 408 EquipmentItem& item_used); 409 void item_on_npc_collision_fire(EquipmentItem& item, Npc& npc); 410 411 // Game events. 412 void game_on_started(Game& game); 413 void game_on_finished(Game& game); 414 void game_on_update(Game& game); 415 void game_on_draw(Game& game, const SurfacePtr& dst_surface); 416 void game_on_map_changed(Game& game, Map& map); 417 void game_on_world_changed( 418 Game& game, 419 const std::string& previous_world, 420 const std::string& new_world 421 ); 422 void game_on_paused(Game& game); 423 void game_on_unpaused(Game& game); 424 bool game_on_dialog_started( 425 Game& game, 426 const Dialog& dialog, 427 const ScopedLuaRef& info_ref 428 ); 429 void game_on_dialog_finished(Game& game, const Dialog& dialog); 430 bool game_on_game_over_started(Game& game); 431 void game_on_game_over_finished(Game& game); 432 bool game_on_input(Game& game, const InputEvent& event); 433 bool game_on_command_pressed(Game& game, GameCommand command); 434 bool game_on_command_released(Game& game, GameCommand command); 435 436 // Map events. 437 void map_on_started(Map& map, const std::shared_ptr<Destination>& destination); 438 void map_on_finished(Map& map); 439 void map_on_update(Map& map); 440 void map_on_draw(Map& map, const SurfacePtr& dst_surface); 441 void map_on_suspended(Map& map, bool suspended); 442 void map_on_opening_transition_finished(Map& map, 443 const std::shared_ptr<Destination>& destination); 444 void map_on_obtaining_treasure(Map& map, const Treasure& treasure); 445 void map_on_obtained_treasure(Map& map, const Treasure& treasure); 446 bool map_on_input(Map& map, const InputEvent& event); 447 bool map_on_command_pressed(Map& map, GameCommand command); 448 bool map_on_command_released(Map& map, GameCommand command); 449 450 // Map entity events. 451 void entity_on_update(Entity& entity); 452 void entity_on_suspended(Entity& entity, bool suspended); 453 void entity_on_created(Entity& entity); 454 void entity_on_removed(Entity& entity); 455 void entity_on_enabled(Entity& entity); 456 void entity_on_disabled(Entity& entity); 457 void entity_on_pre_draw(Entity& entity, Camera& camera); 458 void entity_on_post_draw(Entity& entity, Camera& camera); 459 void entity_on_position_changed(Entity& entity, const Point& xy, int layer); 460 void entity_on_obstacle_reached(Entity& entity, Movement& movement); 461 void entity_on_movement_started(Entity& entity, Movement& movement); 462 void entity_on_movement_changed(Entity& entity, Movement& movement); 463 void entity_on_movement_finished(Entity& entity); 464 bool entity_on_interaction(Entity& entity); 465 bool entity_on_interaction_item(Entity& entity, EquipmentItem& item_used); 466 void entity_on_state_changing( 467 Entity& entity, 468 const std::string& state_name, 469 const std::string& next_state_name); 470 void entity_on_state_changed(Entity& entity, const std::string& new_state_name); 471 void entity_on_lifting( 472 Entity& entity, 473 Entity& carrier, 474 CarriedObject& carried_object); 475 bool hero_on_taking_damage(Hero& hero, int damage); 476 void destination_on_activated(Destination& destination); 477 void teletransporter_on_activated(Teletransporter& teletransporter); 478 void npc_on_collision_fire(Npc& npc); 479 void carried_object_on_lifted(CarriedObject& carried_object); 480 void carried_object_on_thrown(CarriedObject& carried_object); 481 void carried_object_on_breaking(CarriedObject& carried_object); 482 bool chest_on_opened(Chest& chest, const Treasure& treasure); 483 void block_on_moving(Block& block); 484 void block_on_moved(Block& block); 485 void switch_on_activated(Switch& sw); 486 void switch_on_inactivated(Switch& sw); 487 void switch_on_left(Switch& sw); 488 void sensor_on_activated(Sensor& sensor); 489 void sensor_on_activated_repeat(Sensor& sensor); 490 void sensor_on_left(Sensor& sensor); 491 void sensor_on_collision_explosion(Sensor& sensor); 492 void separator_on_activating(Separator& separator, int direction4); 493 void separator_on_activated(Separator& separator, int direction4); 494 void door_on_opened(Door& door); 495 void door_on_closed(Door& door); 496 bool shop_treasure_on_buying(ShopTreasure& shop_treasure); 497 void shop_treasure_on_bought(ShopTreasure& shop_treasure); 498 void destructible_on_looked(Destructible& destructible); 499 void destructible_on_cut(Destructible& destructible); 500 void destructible_on_exploded(Destructible& destructible); 501 void destructible_on_regenerating(Destructible& destructible); 502 void enemy_on_restarted(Enemy& enemy); 503 void enemy_on_collision_enemy(Enemy& enemy, 504 Enemy& other_enemy, Sprite& other_sprite, Sprite& this_sprite); 505 void enemy_on_custom_attack_received(Enemy& enemy, 506 EnemyAttack attack, Sprite* sprite); 507 bool enemy_on_hurt_by_sword(Enemy& enemy, Hero& hero, Sprite& enemy_sprite); 508 void enemy_on_hurt(Enemy& enemy, EnemyAttack attack); 509 void enemy_on_dying(Enemy& enemy); 510 void enemy_on_dead(Enemy& enemy); 511 void enemy_on_immobilized(Enemy& enemy); 512 bool enemy_on_attacking_hero(Enemy& enemy, Hero& hero, Sprite* enemy_sprite); 513 void custom_entity_on_ground_below_changed( 514 CustomEntity& custom_entity, Ground ground_below); 515 void state_on_started( 516 CustomState& state, 517 const std::string& previous_state_name, 518 CustomState* previous_state); 519 void state_on_finished( 520 CustomState& state, 521 const std::string& next_state_name, 522 CustomState* next_state); 523 void state_on_update(CustomState& state); 524 void state_on_suspended(CustomState& state, bool suspended); 525 void state_on_pre_draw(CustomState& state, Camera& camera); 526 void state_on_post_draw(CustomState& state, Camera& camera); 527 void state_on_map_started(CustomState& state, Map& map, const std::shared_ptr<Destination>& destination); 528 void state_on_map_opening_transition_finished(CustomState& state, Map& map, const std::shared_ptr<Destination>& destination); 529 void state_on_map_finished(CustomState& state); 530 void state_on_position_changed(CustomState& state, const Point& xy, int layer); 531 void state_on_ground_below_changed(CustomState& state, Ground ground_below); 532 void state_on_obstacle_reached(CustomState& state, Movement& movement); 533 void state_on_movement_started(CustomState& state, Movement& movement); 534 void state_on_movement_changed(CustomState& state, Movement& movement); 535 void state_on_movement_finished(CustomState& state); 536 void state_on_attacked_enemy( 537 CustomState& state, 538 Enemy& enemy, 539 Sprite* enemy_sprite, 540 EnemyAttack attack, 541 const EnemyReaction::Reaction& reaction 542 ); 543 bool state_on_input(CustomState& state, const InputEvent& event); 544 bool state_on_command_pressed(CustomState& state, GameCommand command); 545 bool state_on_command_released(CustomState& state, GameCommand command); 546 547 // Implementation of the API. 548 549 /** 550 * \brief Type of the functions that can be called by Lua. 551 */ 552 using FunctionExportedToLua = int(lua_State* current_l); 553 554 // All functions named <type>_api_<name> can be called by Lua. 555 static FunctionExportedToLua 556 557 // Main API. 558 main_api_get_solarus_version, 559 main_api_get_quest_version, 560 main_api_get_quest_format, 561 main_api_load_file, 562 main_api_do_file, 563 main_api_reset, 564 main_api_exit, 565 main_api_get_elapsed_time, 566 main_api_get_quest_write_dir, 567 main_api_set_quest_write_dir, 568 main_api_load_settings, 569 main_api_save_settings, 570 main_api_get_distance, 571 main_api_get_angle, 572 main_api_get_resource_ids, 573 main_api_resource_exists, 574 main_api_get_resource_description, 575 main_api_add_resource, 576 main_api_remove_resource, 577 main_api_get_type, 578 main_api_get_metatable, 579 main_api_get_os, 580 main_api_get_game, 581 582 // Audio API. 583 audio_api_get_sound_volume, 584 audio_api_set_sound_volume, 585 audio_api_play_sound, 586 audio_api_preload_sounds, 587 audio_api_get_music_volume, 588 audio_api_set_music_volume, 589 audio_api_play_music, 590 audio_api_stop_music, 591 audio_api_get_music, 592 audio_api_get_music_format, 593 audio_api_get_music_num_channels, 594 audio_api_get_music_channel_volume, 595 audio_api_set_music_channel_volume, 596 audio_api_get_music_tempo, 597 audio_api_set_music_tempo, 598 599 // Video API. 600 video_api_get_window_title, 601 video_api_set_window_title, 602 video_api_get_mode, 603 video_api_set_mode, 604 video_api_switch_mode, 605 video_api_is_mode_supported, 606 video_api_get_modes, 607 video_api_is_fullscreen, 608 video_api_set_fullscreen, 609 video_api_is_cursor_visible, 610 video_api_set_cursor_visible, 611 video_api_get_quest_size, 612 video_api_get_window_size, 613 video_api_set_window_size, 614 video_api_reset_window_size, 615 video_api_get_shader, 616 video_api_set_shader, 617 618 // Input API. 619 input_api_is_joypad_enabled, 620 input_api_set_joypad_enabled, 621 input_api_is_key_pressed, 622 input_api_get_key_modifiers, 623 input_api_is_joypad_button_pressed, 624 input_api_get_joypad_axis_state, 625 input_api_get_joypad_hat_direction, 626 input_api_is_mouse_button_pressed, 627 input_api_get_mouse_position, 628 input_api_is_finger_pressed, 629 input_api_get_finger_position, 630 input_api_get_finger_pressure, 631 input_api_simulate_key_pressed, 632 input_api_simulate_key_released, 633 634 // File API. 635 file_api_open, 636 file_api_exists, 637 file_api_remove, 638 file_api_mkdir, 639 file_api_is_dir, 640 file_api_list_dir, 641 642 // Menu API. 643 menu_api_start, 644 menu_api_stop, 645 menu_api_stop_all, 646 menu_api_is_started, 647 menu_api_bring_to_front, 648 menu_api_bring_to_back, 649 650 // Timer API. 651 timer_api_start, 652 timer_api_stop, 653 timer_api_stop_all, 654 timer_api_is_with_sound, 655 timer_api_set_with_sound, 656 timer_api_is_suspended, 657 timer_api_set_suspended, 658 timer_api_is_suspended_with_map, 659 timer_api_set_suspended_with_map, 660 timer_api_get_remaining_time, 661 timer_api_set_remaining_time, 662 // TODO deprecate is_with_sound, set_with_sound (do this in pure Lua, possibly with a second timer) 663 664 // Language API. 665 language_api_get_language, 666 language_api_set_language, 667 language_api_get_language_name, 668 language_api_get_languages, 669 language_api_get_string, 670 language_api_get_dialog, 671 672 // Drawable API (i.e. common to surfaces, text surfaces and sprites). 673 drawable_api_draw, 674 drawable_api_draw_region, 675 drawable_api_get_blend_mode, 676 drawable_api_set_blend_mode, 677 drawable_api_get_shader, 678 drawable_api_set_shader, 679 drawable_api_get_color_modulation, 680 drawable_api_set_color_modulation, 681 drawable_api_get_opacity, 682 drawable_api_set_opacity, 683 drawable_api_fade_in, 684 drawable_api_fade_out, 685 drawable_api_get_xy, 686 drawable_api_set_xy, 687 drawable_api_get_rotation, 688 drawable_api_set_rotation, 689 drawable_api_get_scale, 690 drawable_api_set_scale, 691 drawable_api_get_transformation_origin, 692 drawable_api_set_transformation_origin, 693 drawable_api_get_movement, 694 drawable_api_stop_movement, 695 drawable_meta_gc, 696 697 // Surface API. 698 surface_api_create, 699 surface_api_get_size, 700 surface_api_clear, 701 surface_api_fill_color, 702 surface_api_get_opacity, 703 surface_api_set_opacity, 704 surface_api_get_pixels, 705 surface_api_set_pixels, 706 surface_api_gl_bind_as_texture, 707 surface_api_gl_bind_as_target, 708 709 // Text surface API. 710 text_surface_api_create, 711 text_surface_api_get_predicted_size, 712 text_surface_api_get_horizontal_alignment, 713 text_surface_api_set_horizontal_alignment, 714 text_surface_api_get_vertical_alignment, 715 text_surface_api_set_vertical_alignment, 716 text_surface_api_get_font, 717 text_surface_api_set_font, 718 text_surface_api_get_rendering_mode, 719 text_surface_api_set_rendering_mode, 720 text_surface_api_get_color, 721 text_surface_api_set_color, 722 text_surface_api_get_font_size, 723 text_surface_api_set_font_size, 724 text_surface_api_get_text, 725 text_surface_api_set_text, 726 text_surface_api_set_text_key, 727 text_surface_api_get_size, 728 729 // Sprite API. 730 sprite_api_create, 731 sprite_api_get_animation_set, 732 sprite_api_has_animation, 733 sprite_api_get_animation, 734 sprite_api_set_animation, 735 sprite_api_stop_animation, 736 sprite_api_is_animation_started, 737 sprite_api_get_direction, 738 sprite_api_set_direction, 739 sprite_api_get_num_directions, 740 sprite_api_get_frame, 741 sprite_api_set_frame, 742 sprite_api_get_num_frames, 743 sprite_api_get_frame_delay, 744 sprite_api_set_frame_delay, 745 sprite_api_get_size, 746 sprite_api_get_origin, 747 sprite_api_get_frame_src_xy, 748 sprite_api_is_paused, 749 sprite_api_set_paused, 750 sprite_api_get_ignore_suspend, 751 sprite_api_set_ignore_suspend, 752 sprite_api_synchronize, 753 754 // Shader API. 755 shader_api_create, 756 shader_api_get_opengl_version, 757 shader_api_get_shading_language_version, 758 shader_api_get_id, 759 shader_api_get_vertex_file, 760 shader_api_get_vertex_source, 761 shader_api_get_fragment_file, 762 shader_api_get_fragment_source, 763 shader_api_get_scaling_factor, 764 shader_api_set_scaling_factor, 765 shader_api_set_uniform, 766 767 // Movement API. 768 movement_api_create, 769 movement_api_get_xy, 770 movement_api_set_xy, 771 movement_api_is_suspended, 772 movement_api_get_ignore_suspend, 773 movement_api_set_ignore_suspend, 774 movement_api_get_ignore_obstacles, 775 movement_api_set_ignore_obstacles, 776 movement_api_start, 777 movement_api_stop, 778 movement_api_get_direction4, 779 straight_movement_api_get_speed, 780 straight_movement_api_set_speed, 781 straight_movement_api_get_angle, 782 straight_movement_api_set_angle, 783 straight_movement_api_get_max_distance, 784 straight_movement_api_set_max_distance, 785 straight_movement_api_is_smooth, 786 straight_movement_api_set_smooth, 787 random_movement_api_get_speed, 788 random_movement_api_set_speed, 789 random_movement_api_get_angle, 790 random_movement_api_get_max_distance, 791 random_movement_api_set_max_distance, 792 random_movement_api_is_smooth, 793 random_movement_api_set_smooth, 794 target_movement_api_set_target, 795 target_movement_api_get_speed, 796 target_movement_api_set_speed, 797 target_movement_api_get_angle, 798 target_movement_api_is_smooth, 799 target_movement_api_set_smooth, 800 path_movement_api_get_path, 801 path_movement_api_set_path, 802 path_movement_api_get_speed, 803 path_movement_api_set_speed, 804 path_movement_api_get_angle, 805 path_movement_api_get_loop, 806 path_movement_api_set_loop, 807 path_movement_api_get_snap_to_grid, 808 path_movement_api_set_snap_to_grid, 809 random_path_movement_api_get_speed, 810 random_path_movement_api_set_speed, 811 random_path_movement_api_get_angle, 812 path_finding_movement_api_set_target, 813 path_finding_movement_api_get_speed, 814 path_finding_movement_api_set_speed, 815 path_finding_movement_api_get_angle, 816 circle_movement_api_get_center, 817 circle_movement_api_set_center, 818 circle_movement_api_get_radius, 819 circle_movement_api_set_radius, 820 circle_movement_api_get_radius_speed, 821 circle_movement_api_set_radius_speed, 822 circle_movement_api_is_clockwise, 823 circle_movement_api_set_clockwise, 824 circle_movement_api_get_angle_from_center, 825 circle_movement_api_set_angle_from_center, 826 circle_movement_api_get_initial_angle, 827 circle_movement_api_set_initial_angle, 828 circle_movement_api_get_angular_speed, 829 circle_movement_api_set_angular_speed, 830 circle_movement_api_get_angle_speed, 831 circle_movement_api_set_angle_speed, 832 circle_movement_api_get_max_rotations, 833 circle_movement_api_set_max_rotations, 834 circle_movement_api_get_duration, 835 circle_movement_api_set_duration, 836 circle_movement_api_get_loop_delay, 837 circle_movement_api_set_loop_delay, 838 jump_movement_api_get_direction8, 839 jump_movement_api_set_direction8, 840 jump_movement_api_get_distance, 841 jump_movement_api_set_distance, 842 jump_movement_api_get_speed, 843 jump_movement_api_set_speed, 844 pixel_movement_api_get_trajectory, 845 pixel_movement_api_set_trajectory, 846 pixel_movement_api_get_loop, 847 pixel_movement_api_set_loop, 848 pixel_movement_api_get_delay, 849 pixel_movement_api_set_delay, 850 851 // Game API. 852 game_api_exists, 853 game_api_delete, 854 game_api_load, 855 game_api_save, // TODO allow to change the file name (e.g. to copy) 856 game_api_start, 857 game_api_is_started, 858 game_api_is_suspended, 859 game_api_set_suspended, 860 game_api_is_paused, 861 game_api_set_paused, 862 game_api_is_pause_allowed, 863 game_api_set_pause_allowed, 864 game_api_is_dialog_enabled, 865 game_api_start_dialog, 866 game_api_stop_dialog, 867 game_api_is_game_over_enabled, 868 game_api_start_game_over, 869 game_api_stop_game_over, 870 game_api_get_map, 871 game_api_get_hero, 872 game_api_get_value, 873 game_api_set_value, 874 game_api_get_starting_location, 875 game_api_set_starting_location, 876 game_api_get_transition_style, 877 game_api_set_transition_style, 878 game_api_get_life, 879 game_api_set_life, 880 game_api_add_life, 881 game_api_remove_life, 882 game_api_get_max_life, 883 game_api_set_max_life, 884 game_api_add_max_life, 885 game_api_get_money, 886 game_api_set_money, 887 game_api_add_money, 888 game_api_remove_money, 889 game_api_get_max_money, 890 game_api_set_max_money, 891 game_api_get_magic, 892 game_api_set_magic, 893 game_api_add_magic, 894 game_api_remove_magic, 895 game_api_get_max_magic, 896 game_api_set_max_magic, 897 game_api_has_ability, 898 game_api_get_ability, 899 game_api_set_ability, 900 game_api_get_item, 901 game_api_has_item, 902 game_api_get_item_assigned, 903 game_api_set_item_assigned, 904 game_api_is_command_pressed, 905 game_api_get_commands_direction, 906 game_api_get_command_effect, // TODO also return "run" for action command 907 game_api_get_command_keyboard_binding, 908 game_api_set_command_keyboard_binding, 909 game_api_get_command_joypad_binding, 910 game_api_set_command_joypad_binding, 911 game_api_capture_command_binding, 912 game_api_simulate_command_pressed, 913 game_api_simulate_command_released, 914 915 // Equipment item API. 916 item_api_get_name, 917 item_api_get_game, 918 item_api_get_map, 919 item_api_get_savegame_variable, 920 item_api_set_savegame_variable, 921 item_api_get_amount_savegame_variable, 922 item_api_set_amount_savegame_variable, 923 item_api_is_being_used, 924 item_api_is_obtainable, 925 item_api_set_obtainable, 926 item_api_is_assignable, // TODO remove 927 item_api_set_assignable, // TODO remove 928 item_api_get_can_disappear, 929 item_api_set_can_disappear, 930 item_api_get_brandish_when_picked, 931 item_api_set_brandish_when_picked, 932 item_api_get_shadow, 933 item_api_set_shadow, 934 item_api_get_sound_when_picked, 935 item_api_set_sound_when_picked, 936 item_api_get_sound_when_brandished, 937 item_api_set_sound_when_brandished, 938 item_api_has_variant, 939 item_api_get_variant, 940 item_api_set_variant, 941 item_api_has_amount, 942 item_api_get_amount, 943 item_api_set_amount, 944 item_api_add_amount, 945 item_api_remove_amount, 946 item_api_get_max_amount, 947 item_api_set_max_amount, 948 item_api_set_finished, 949 950 // Map API. 951 map_api_get_id, 952 map_api_get_game, 953 map_api_get_world, 954 map_api_set_world, 955 map_api_get_floor, 956 map_api_set_floor, 957 map_api_get_min_layer, 958 map_api_get_max_layer, 959 map_api_get_size, 960 map_api_get_location, 961 map_api_get_tileset, 962 map_api_set_tileset, 963 map_api_get_music, 964 map_api_get_camera, 965 map_api_get_camera_position, 966 map_api_move_camera, 967 map_api_get_ground, 968 map_api_draw_visual, 969 map_api_draw_sprite, 970 map_api_get_crystal_state, 971 map_api_set_crystal_state, 972 map_api_change_crystal_state, 973 map_api_open_doors, 974 map_api_close_doors, 975 map_api_set_doors_open, 976 map_api_get_entity, 977 map_api_has_entity, 978 map_api_get_entities, 979 map_api_get_entities_count, 980 map_api_has_entities, 981 map_api_get_entities_by_type, 982 map_api_get_entities_in_rectangle, 983 map_api_get_entities_in_region, 984 map_api_get_hero, 985 map_api_set_entities_enabled, 986 map_api_remove_entities, 987 map_api_create_entity, // Same function used for all entity types. 988 989 // Map entity API. 990 entity_api_get_type, 991 entity_api_get_map, 992 entity_api_get_game, 993 entity_api_get_name, 994 entity_api_exists, 995 entity_api_remove, 996 entity_api_is_enabled, 997 entity_api_set_enabled, 998 entity_api_get_size, 999 entity_api_set_size, 1000 entity_api_get_origin, 1001 entity_api_set_origin, 1002 entity_api_get_position, 1003 entity_api_set_position, 1004 entity_api_get_center_position, 1005 entity_api_get_facing_position, 1006 entity_api_get_facing_entity, 1007 entity_api_get_ground_position, 1008 entity_api_get_ground_below, 1009 entity_api_get_bounding_box, 1010 entity_api_get_max_bounding_box, 1011 entity_api_get_layer, 1012 entity_api_set_layer, 1013 entity_api_overlaps, 1014 entity_api_get_distance, 1015 entity_api_get_angle, 1016 entity_api_get_direction4_to, 1017 entity_api_get_direction8_to, 1018 entity_api_bring_to_front, 1019 entity_api_bring_to_back, 1020 entity_api_is_drawn_in_y_order, 1021 entity_api_set_drawn_in_y_order, 1022 entity_api_snap_to_grid, 1023 entity_api_get_sprite, 1024 entity_api_get_sprites, 1025 entity_api_create_sprite, 1026 entity_api_remove_sprite, 1027 entity_api_bring_sprite_to_front, 1028 entity_api_bring_sprite_to_back, 1029 entity_api_is_visible, 1030 entity_api_set_visible, 1031 entity_api_get_draw_override, 1032 entity_api_set_draw_override, 1033 entity_api_get_weight, 1034 entity_api_set_weight, 1035 entity_api_get_controlling_stream, 1036 entity_api_get_movement, 1037 entity_api_stop_movement, 1038 entity_api_has_layer_independent_collisions, 1039 entity_api_set_layer_independent_collisions, 1040 entity_api_test_obstacles, 1041 entity_api_get_optimization_distance, 1042 entity_api_set_optimization_distance, 1043 entity_api_is_in_same_region, 1044 entity_api_get_state, 1045 entity_api_get_property, 1046 entity_api_set_property, 1047 entity_api_get_properties, 1048 entity_api_set_properties, 1049 hero_api_teleport, 1050 hero_api_get_direction, 1051 hero_api_set_direction, 1052 hero_api_get_walking_speed, 1053 hero_api_set_walking_speed, 1054 hero_api_save_solid_ground, 1055 hero_api_reset_solid_ground, 1056 hero_api_get_solid_ground_position, 1057 hero_api_get_animation, 1058 hero_api_set_animation, 1059 hero_api_get_tunic_sprite_id, 1060 hero_api_set_tunic_sprite_id, 1061 hero_api_get_sword_sprite_id, 1062 hero_api_set_sword_sprite_id, 1063 hero_api_get_sword_sound_id, 1064 hero_api_set_sword_sound_id, 1065 hero_api_get_shield_sprite_id, 1066 hero_api_set_shield_sprite_id, 1067 hero_api_is_blinking, 1068 hero_api_set_blinking, 1069 hero_api_is_invincible, 1070 hero_api_set_invincible, 1071 hero_api_get_carried_object, 1072 hero_api_freeze, 1073 hero_api_unfreeze, 1074 hero_api_walk, 1075 hero_api_start_attack, 1076 hero_api_start_attack_loading, 1077 hero_api_start_item, 1078 hero_api_start_grabbing, 1079 hero_api_start_jumping, 1080 hero_api_start_treasure, 1081 hero_api_start_victory, 1082 hero_api_start_boomerang, 1083 hero_api_start_bow, 1084 hero_api_start_hookshot, 1085 hero_api_start_running, 1086 hero_api_start_hurt, 1087 hero_api_start_state, 1088 hero_api_get_state_object, 1089 camera_api_get_position_on_screen, 1090 camera_api_set_position_on_screen, 1091 camera_api_get_state, 1092 camera_api_start_tracking, 1093 camera_api_start_manual, 1094 camera_api_get_position_to_track, 1095 camera_api_get_tracked_entity, 1096 camera_api_get_surface, 1097 destination_api_get_starting_location_mode, 1098 destination_api_set_starting_location_mode, 1099 teletransporter_api_get_sound, 1100 teletransporter_api_set_sound, 1101 teletransporter_api_get_transition, 1102 teletransporter_api_set_transition, 1103 teletransporter_api_get_destination_map, 1104 teletransporter_api_set_destination_map, 1105 teletransporter_api_get_destination_name, 1106 teletransporter_api_set_destination_name, 1107 npc_api_is_traversable, 1108 npc_api_set_traversable, 1109 chest_api_is_open, 1110 chest_api_set_open, 1111 chest_api_get_treasure, 1112 chest_api_set_treasure, 1113 block_api_reset, 1114 block_api_is_pushable, 1115 block_api_set_pushable, 1116 block_api_is_pullable, 1117 block_api_set_pullable, 1118 block_api_get_max_moves, 1119 block_api_set_max_moves, 1120 block_api_get_maximum_moves, 1121 block_api_set_maximum_moves, 1122 switch_api_is_activated, 1123 switch_api_set_activated, 1124 switch_api_is_locked, 1125 switch_api_set_locked, 1126 switch_api_is_walkable, 1127 stream_api_get_direction, 1128 stream_api_set_direction, 1129 stream_api_get_speed, 1130 stream_api_set_speed, 1131 stream_api_get_allow_movement, 1132 stream_api_set_allow_movement, 1133 stream_api_get_allow_attack, 1134 stream_api_set_allow_attack, 1135 stream_api_get_allow_item, 1136 stream_api_set_allow_item, 1137 door_api_is_open, 1138 door_api_is_opening, 1139 door_api_is_closed, 1140 door_api_is_closing, 1141 door_api_open, 1142 door_api_close, 1143 door_api_set_open, 1144 stairs_api_get_direction, 1145 stairs_api_is_inner, 1146 pickable_api_get_followed_entity, 1147 pickable_api_get_falling_height, 1148 pickable_api_get_treasure, 1149 destructible_api_get_treasure, 1150 destructible_api_set_treasure, 1151 destructible_api_get_destruction_sound, 1152 destructible_api_set_destruction_sound, 1153 destructible_api_get_can_be_cut, 1154 destructible_api_set_can_be_cut, 1155 destructible_api_get_can_explode, 1156 destructible_api_set_can_explode, 1157 destructible_api_get_can_regenerate, 1158 destructible_api_set_can_regenerate, 1159 destructible_api_get_damage_on_enemies, 1160 destructible_api_set_damage_on_enemies, 1161 destructible_api_get_modified_ground, 1162 dynamic_tile_api_get_pattern_id, 1163 dynamic_tile_api_get_modified_ground, 1164 dynamic_tile_api_get_tileset, 1165 dynamic_tile_api_set_tileset, 1166 carried_object_api_get_carrier, 1167 carried_object_api_get_destruction_sound, 1168 carried_object_api_set_destruction_sound, 1169 carried_object_api_get_damage_on_enemies, 1170 carried_object_api_set_damage_on_enemies, 1171 enemy_api_get_breed, 1172 enemy_api_get_life, 1173 enemy_api_set_life, 1174 enemy_api_add_life, 1175 enemy_api_remove_life, 1176 enemy_api_get_damage, 1177 enemy_api_set_damage, 1178 enemy_api_is_pushed_back_when_hurt, 1179 enemy_api_set_pushed_back_when_hurt, 1180 enemy_api_get_push_hero_on_sword, 1181 enemy_api_set_push_hero_on_sword, 1182 enemy_api_get_can_hurt_hero_running, 1183 enemy_api_set_can_hurt_hero_running, 1184 enemy_api_get_hurt_style, 1185 enemy_api_set_hurt_style, 1186 enemy_api_get_dying_sprite_id, 1187 enemy_api_set_dying_sprite_id, 1188 enemy_api_get_can_attack, 1189 enemy_api_set_can_attack, 1190 enemy_api_get_minimum_shield_needed, 1191 enemy_api_set_minimum_shield_needed, 1192 enemy_api_get_attack_consequence, 1193 enemy_api_set_attack_consequence, 1194 enemy_api_get_attack_consequence_sprite, 1195 enemy_api_set_attack_consequence_sprite, 1196 enemy_api_set_default_attack_consequences, 1197 enemy_api_set_default_attack_consequences_sprite, 1198 enemy_api_set_invincible, 1199 enemy_api_set_invincible_sprite, 1200 enemy_api_get_treasure, 1201 enemy_api_set_treasure, 1202 enemy_api_is_traversable, 1203 enemy_api_set_traversable, 1204 enemy_api_get_attacking_collision_mode, 1205 enemy_api_set_attacking_collision_mode, 1206 enemy_api_get_obstacle_behavior, 1207 enemy_api_set_obstacle_behavior, 1208 enemy_api_restart, 1209 enemy_api_hurt, 1210 enemy_api_is_immobilized, 1211 enemy_api_immobilize, 1212 enemy_api_create_enemy, 1213 custom_entity_api_get_model, 1214 custom_entity_api_get_direction, 1215 custom_entity_api_set_direction, 1216 custom_entity_api_is_tiled, 1217 custom_entity_api_set_tiled, 1218 custom_entity_api_set_traversable_by, 1219 custom_entity_api_set_can_traverse, 1220 custom_entity_api_can_traverse_ground, 1221 custom_entity_api_set_can_traverse_ground, 1222 custom_entity_api_add_collision_test, 1223 custom_entity_api_clear_collision_tests, 1224 custom_entity_api_get_modified_ground, 1225 custom_entity_api_set_modified_ground, 1226 custom_entity_api_get_follow_streams, 1227 custom_entity_api_set_follow_streams, 1228 1229 // State API. 1230 state_api_create, 1231 state_api_get_description, 1232 state_api_set_description, 1233 state_api_get_entity, 1234 state_api_get_map, 1235 state_api_get_game, 1236 state_api_is_started, 1237 state_api_is_visible, 1238 state_api_set_visible, 1239 state_api_get_draw_override, 1240 state_api_set_draw_override, 1241 state_api_get_can_control_direction, 1242 state_api_set_can_control_direction, 1243 state_api_get_can_control_movement, 1244 state_api_set_can_control_movement, 1245 state_api_set_can_traverse, 1246 state_api_get_can_traverse_ground, 1247 state_api_set_can_traverse_ground, 1248 state_api_is_gravity_enabled, 1249 state_api_set_gravity_enabled, 1250 state_api_is_affected_by_ground, 1251 state_api_set_affected_by_ground, 1252 state_api_get_can_come_from_bad_ground, 1253 state_api_set_can_come_from_bad_ground, 1254 state_api_get_can_be_hurt, 1255 state_api_set_can_be_hurt, 1256 state_api_get_can_use_sword, 1257 state_api_set_can_use_sword, 1258 state_api_get_can_cut, 1259 state_api_set_can_cut, 1260 state_api_get_can_use_shield, 1261 state_api_set_can_use_shield, 1262 state_api_get_can_use_item, 1263 state_api_set_can_use_item, 1264 state_api_get_can_interact, 1265 state_api_set_can_interact, 1266 state_api_get_can_grab, 1267 state_api_set_can_grab, 1268 state_api_get_can_push, 1269 state_api_set_can_push, 1270 state_api_get_pushing_delay, 1271 state_api_set_pushing_delay, 1272 state_api_get_can_pick_treasure, 1273 state_api_set_can_pick_treasure, 1274 state_api_get_can_use_teletransporter, 1275 state_api_set_can_use_teletransporter, 1276 state_api_get_can_use_switch, 1277 state_api_set_can_use_switch, 1278 state_api_get_can_use_stream, 1279 state_api_set_can_use_stream, 1280 state_api_get_can_use_stairs, 1281 state_api_set_can_use_stairs, 1282 state_api_get_can_use_jumper, 1283 state_api_set_can_use_jumper, 1284 state_api_get_jumper_delay, 1285 state_api_set_jumper_delay, 1286 state_api_get_carried_object_action, 1287 state_api_set_carried_object_action, 1288 1289 // available to all userdata types 1290 userdata_meta_gc, 1291 userdata_meta_newindex_as_table, 1292 userdata_meta_index_as_table, 1293 1294 // Lua backtrace error function 1295 l_backtrace; 1296 1297 private: 1298 1299 /** 1300 * \brief Data associated to any Lua menu. 1301 */ 1302 struct LuaMenuData { 1303 ScopedLuaRef ref; /**< Lua ref of the table of the menu. 1304 * LUA_REFNIL means that the menu will be removed. */ 1305 ScopedLuaRef context; /**< Lua table or userdata the menu is attached to. */ 1306 bool recently_added; /**< Used to avoid elements added during an iteration. */ 1307 LuaMenuDataLuaMenuData1308 LuaMenuData(const ScopedLuaRef& ref, const ScopedLuaRef& context): 1309 ref(ref), 1310 context(context), 1311 recently_added(true) { 1312 } 1313 }; 1314 1315 /** 1316 * \brief Data associated to any Lua timer. 1317 */ 1318 struct LuaTimerData { 1319 ScopedLuaRef callback_ref; /**< Lua ref of the function to call after the timer. */ 1320 ScopedLuaRef context; /**< Lua table or userdata the timer is attached to. */ 1321 }; 1322 1323 // Executing Lua code. 1324 bool userdata_has_metafield( 1325 const ExportableToLua& userdata, const char* key) const; 1326 bool find_method(int index, const char* function_name); 1327 bool find_method(const char* function_name); 1328 void print_lua_version(); 1329 1330 // Initialization of modules. 1331 void register_functions( 1332 const std::string& module_name, 1333 std::vector<luaL_Reg> functions 1334 ); 1335 void register_type( 1336 const std::string& module_name, 1337 std::vector<luaL_Reg> functions, 1338 std::vector<luaL_Reg> methods, 1339 std::vector<luaL_Reg> metamethods 1340 ); 1341 void register_modules(); 1342 void register_main_module(); 1343 void register_audio_module(); 1344 void register_video_module(); 1345 void register_input_module(); 1346 void register_file_module(); 1347 void register_timer_module(); 1348 void register_item_module(); 1349 void register_surface_module(); 1350 void register_text_surface_module(); 1351 void register_sprite_module(); 1352 void register_shader_module(); 1353 void register_movement_module(); 1354 void register_menu_module(); 1355 void register_language_module(); 1356 void register_game_module(); 1357 void register_map_module(); 1358 void register_entity_module(); 1359 void register_state_module(); 1360 1361 // Pushing objects to Lua. 1362 static void push_main(lua_State* current_l); 1363 static void push_video(lua_State* current_l); 1364 static void push_string(lua_State* current_l, const std::string& text); 1365 static void push_color(lua_State* current_l, const Color& color); 1366 public: 1367 static void push_userdata(lua_State* current_l, ExportableToLua& userdata); 1368 private: 1369 static void push_dialog(lua_State* current_l, const Dialog& dialog); 1370 static void push_timer(lua_State* current_l, const TimerPtr& timer); 1371 static void push_surface(lua_State* current_l, Surface& surface); 1372 static void push_text_surface(lua_State* current_l, TextSurface& text_surface); 1373 static void push_sprite(lua_State* current_l, Sprite& sprite); 1374 static void push_shader(lua_State* current_l, Shader& shader); 1375 static void push_item(lua_State* current_l, EquipmentItem& item); 1376 static void push_movement(lua_State* current_l, Movement& movement); 1377 static void push_game(lua_State* current_l, Savegame& game); 1378 static void push_map(lua_State* current_l, Map& map); 1379 static void push_state(lua_State* current_l, CustomState& state); 1380 static void push_entity(lua_State* current_l, Entity& entity); 1381 static void push_entity_iterator(lua_State* current_l, const EntityVector& entities); 1382 static void push_named_sprite_iterator( 1383 lua_State* current_l, 1384 const std::vector<Entity::NamedSprite>& sprites 1385 ); 1386 static void push_hero(lua_State* current_l, Hero& hero); 1387 static void push_camera(lua_State* current_l, Camera& camera); 1388 static void push_npc(lua_State* current_l, Npc& npc); 1389 static void push_destination(lua_State* current_l, Destination& destination); 1390 static void push_teletransporter(lua_State* current_l, Teletransporter& teletransporter); 1391 static void push_chest(lua_State* current_l, Chest& chest); 1392 static void push_block(lua_State* current_l, Block& block); 1393 static void push_switch(lua_State* current_l, Switch& sw); 1394 static void push_stream(lua_State* current_l, Stream& stream); 1395 static void push_door(lua_State* current_l, Door& door); 1396 static void push_stairs(lua_State* current_l, Stairs& stairs); 1397 static void push_shop_treasure(lua_State* current_l, ShopTreasure& shop_treasure); 1398 static void push_pickable(lua_State* current_l, Pickable& pickable); 1399 static void push_destructible(lua_State* current_l, Destructible& destructible); 1400 static void push_carried_object(lua_State* current_l, CarriedObject& carried_object); 1401 static void push_dynamic_tile(lua_State* current_l, DynamicTile& dynamic_tile); 1402 static void push_enemy(lua_State* current_l, Enemy& enemy); 1403 static void push_custom_entity(lua_State* current_l, CustomEntity& entity); 1404 1405 // Getting objects from Lua. 1406 static bool is_main(lua_State* current_l, int index); 1407 static bool is_menu(lua_State* current_l, int index); 1408 static void* test_userdata(lua_State* current_l, int index, 1409 const char* module_name); 1410 static bool is_userdata(lua_State* current_l, int index, 1411 const std::string& module_name); 1412 static const ExportableToLuaPtr& check_userdata( 1413 lua_State* current_l, 1414 int index, 1415 const std::string& module_name 1416 ); 1417 static bool is_timer(lua_State* current_l, int index); 1418 static TimerPtr check_timer(lua_State* current_l, int index); 1419 static bool is_drawable(lua_State* current_l, int index); 1420 static DrawablePtr check_drawable(lua_State* current_l, int index); 1421 static bool is_surface(lua_State* current_l, int index); 1422 static SurfacePtr check_surface(lua_State* current_l, int index); 1423 static bool is_text_surface(lua_State* current_l, int index); 1424 static std::shared_ptr<TextSurface> check_text_surface(lua_State* current_l, int index); 1425 static bool is_sprite(lua_State* current_l, int index); 1426 static SpritePtr check_sprite(lua_State* current_l, int index); 1427 static bool is_shader(lua_State* current_l, int index); 1428 static ShaderPtr check_shader(lua_State* current_l, int index); 1429 static bool is_item(lua_State* current_l, int index); 1430 static std::shared_ptr<EquipmentItem> check_item(lua_State* current_l, int index); 1431 static bool is_movement(lua_State* current_l, int index); 1432 static std::shared_ptr<Movement> check_movement(lua_State* current_l, int index); 1433 static bool is_straight_movement(lua_State* current_l, int index); 1434 static std::shared_ptr<StraightMovement> check_straight_movement(lua_State* current_l, int index); 1435 static bool is_random_movement(lua_State* current_l, int index); 1436 static std::shared_ptr<RandomMovement> check_random_movement(lua_State* current_l, int index); 1437 static bool is_target_movement(lua_State* current_l, int index); 1438 static std::shared_ptr<TargetMovement> check_target_movement(lua_State* current_l, int index); 1439 static bool is_path_movement(lua_State* current_l, int index); 1440 static std::shared_ptr<PathMovement> check_path_movement(lua_State* current_l, int index); 1441 static bool is_random_path_movement(lua_State* current_l, int index); 1442 static std::shared_ptr<RandomPathMovement> check_random_path_movement(lua_State* current_l, int index); 1443 static bool is_path_finding_movement(lua_State* current_l, int index); 1444 static std::shared_ptr<PathFindingMovement> check_path_finding_movement(lua_State* current_l, int index); 1445 static bool is_circle_movement(lua_State* current_l, int index); 1446 static std::shared_ptr<CircleMovement> check_circle_movement(lua_State* current_l, int index); 1447 static bool is_jump_movement(lua_State* current_l, int index); 1448 static std::shared_ptr<JumpMovement> check_jump_movement(lua_State* current_l, int index); 1449 static bool is_pixel_movement(lua_State* current_l, int index); 1450 static std::shared_ptr<PixelMovement> check_pixel_movement(lua_State* current_l, int index); 1451 static bool is_game(lua_State* current_l, int index); 1452 static std::shared_ptr<Savegame> check_game(lua_State* current_l, int index); 1453 static bool is_map(lua_State* current_l, int index); 1454 static std::shared_ptr<Map> check_map(lua_State* current_l, int index); 1455 static bool is_state(lua_State* current_l, int index); 1456 static std::shared_ptr<CustomState> check_state(lua_State* current_l, int index); 1457 static bool is_entity(lua_State* current_l, int index); 1458 static EntityPtr check_entity(lua_State* current_l, int index); 1459 static bool is_hero(lua_State* current_l, int index); 1460 static HeroPtr check_hero(lua_State* current_l, int index); 1461 static bool is_camera(lua_State* current_l, int index); 1462 static std::shared_ptr<Camera> check_camera(lua_State* current_l, int index); 1463 static bool is_destination(lua_State* current_l, int index); 1464 static std::shared_ptr<Destination> check_destination(lua_State* current_l, int index); 1465 static bool is_teletransporter(lua_State* current_l, int index); 1466 static std::shared_ptr<Teletransporter> check_teletransporter(lua_State* current_l, int index); 1467 static bool is_npc(lua_State* current_l, int index); 1468 static std::shared_ptr<Npc> check_npc(lua_State* current_l, int index); 1469 static bool is_chest(lua_State* current_l, int index); 1470 static std::shared_ptr<Chest> check_chest(lua_State* current_l, int index); 1471 static bool is_block(lua_State* current_l, int index); 1472 static std::shared_ptr<Block> check_block(lua_State* current_l, int index); 1473 static bool is_switch(lua_State* current_l, int index); 1474 static std::shared_ptr<Switch> check_switch(lua_State* current_l, int index); 1475 static bool is_stream(lua_State* current_l, int index); 1476 static std::shared_ptr<Stream> check_stream(lua_State* current_l, int index); 1477 static bool is_door(lua_State* current_l, int index); 1478 static std::shared_ptr<Door> check_door(lua_State* current_l, int index); 1479 static bool is_stairs(lua_State* current_l, int index); 1480 static std::shared_ptr<Stairs> check_stairs(lua_State* current_l, int index); 1481 static bool is_shop_treasure(lua_State* current_l, int index); 1482 static std::shared_ptr<ShopTreasure> check_shop_treasure(lua_State* current_l, int index); 1483 static bool is_pickable(lua_State* current_l, int index); 1484 static std::shared_ptr<Pickable> check_pickable(lua_State* current_l, int index); 1485 static bool is_destructible(lua_State* current_l, int index); 1486 static std::shared_ptr<Destructible> check_destructible(lua_State* current_l, int index); 1487 static bool is_carried_object(lua_State* current_l, int index); 1488 static std::shared_ptr<CarriedObject> check_carried_object(lua_State* current_l, int index); 1489 static bool is_dynamic_tile(lua_State* current_l, int index); 1490 static std::shared_ptr<DynamicTile> check_dynamic_tile(lua_State* current_l, int index); 1491 static bool is_enemy(lua_State* current_l, int index); 1492 static std::shared_ptr<Enemy> check_enemy(lua_State* current_l, int index); 1493 static bool is_custom_entity(lua_State* current_l, int index); 1494 static std::shared_ptr<CustomEntity> check_custom_entity(lua_State* current_l, int index); 1495 1496 // Events. 1497 void check_callback_thread() const; 1498 1499 void on_started(); 1500 void on_started(const std::string& previous_state_name, CustomState* previous_state); 1501 void on_finished(); 1502 void on_finished(const std::string& next_state_name, CustomState* next_state); 1503 void on_update(); 1504 void on_draw(const SurfacePtr& dst_surface); 1505 void on_suspended(bool suspended); 1506 void on_paused(); 1507 void on_unpaused(); 1508 bool on_dialog_started(const Dialog& dialog, const ScopedLuaRef& info_ref); 1509 void on_dialog_finished(const Dialog& dialog); 1510 bool on_game_over_started(); 1511 void on_game_over_finished(); 1512 bool on_input(const InputEvent& event); 1513 bool on_key_pressed(const InputEvent& event); 1514 bool on_key_released(const InputEvent& event); 1515 bool on_character_pressed(const InputEvent& event); 1516 bool on_joypad_button_pressed(const InputEvent& event); 1517 bool on_joypad_button_released(const InputEvent& event); 1518 bool on_joypad_axis_moved(const InputEvent& event); 1519 bool on_joypad_hat_moved(const InputEvent& event); 1520 bool on_mouse_button_pressed(const InputEvent& event); 1521 bool on_mouse_button_released(const InputEvent& event); 1522 bool on_finger_pressed(const InputEvent& event); 1523 bool on_finger_released(const InputEvent& event); 1524 bool on_finger_moved(const InputEvent& event); 1525 bool on_command_pressed(GameCommand command); 1526 bool on_command_released(GameCommand command); 1527 void on_animation_finished(const std::string& animation); 1528 void on_animation_changed(const std::string& animation); 1529 void on_direction_changed(const std::string& animation, int direction); 1530 void on_frame_changed(const std::string& animation, int frame); 1531 void on_position_changed(const Point& xy); 1532 void on_obstacle_reached(); 1533 void on_changed(); 1534 void on_started(const std::shared_ptr<Destination>& destination); 1535 void on_opening_transition_finished(const std::shared_ptr<Destination>& destination); 1536 void on_obtaining_treasure(const Treasure& treasure); 1537 void on_obtained_treasure(const Treasure& treasure); 1538 void on_state_changing(const std::string& state_name, const std::string& next_state_name); 1539 void on_state_changed(const std::string& new_state_name); 1540 bool on_taking_damage(int damage); 1541 void on_activating(); 1542 void on_activating(int direction4); 1543 void on_activated(); 1544 void on_activated(int direction4); 1545 void on_activated_repeat(); 1546 void on_inactivated(); 1547 void on_left(); 1548 bool on_interaction(); 1549 bool on_interaction_item(EquipmentItem& item_used); 1550 void on_npc_interaction(Npc& npc); 1551 bool on_npc_interaction_item(Npc& npc, EquipmentItem& item_used); 1552 void on_npc_collision_fire(Npc& npc); 1553 void on_collision_fire(); 1554 void on_collision_explosion(); 1555 void on_collision_enemy(Enemy& enemy, Sprite& other_sprite, Sprite& this_sprite); 1556 void on_lifted(); 1557 void on_thrown(); 1558 void on_breaking(); 1559 bool on_buying(); 1560 void on_bought(); 1561 void on_opened(); 1562 bool on_opened(const Treasure& treasure); 1563 void on_closed(); 1564 void on_moving(); 1565 void on_moved(); 1566 void on_map_changed(Map& map); 1567 void on_world_changed(const std::string& previous_world, const std::string& new_world); 1568 void on_pickable_created(Pickable& pickable); 1569 void on_variant_changed(int variant); 1570 void on_amount_changed(int amount); 1571 void on_obtaining(const Treasure& treasure); 1572 void on_obtained(const Treasure& treasure); 1573 void on_using(); 1574 void on_ability_used(Ability ability); 1575 void on_created(); 1576 void on_removed(); 1577 void on_enabled(); 1578 void on_disabled(); 1579 void on_restarted(); 1580 void on_pre_draw(Camera& camera); 1581 void on_post_draw(Camera& camera); 1582 void on_position_changed(const Point& xy, int layer); 1583 void on_obstacle_reached(Movement& movement); 1584 void on_movement_started(Movement& movement); 1585 void on_movement_changed(Movement& movement); 1586 void on_movement_finished(); 1587 void on_looked(); 1588 void on_cut(); 1589 void on_lifting( 1590 Entity& carrier, 1591 CarriedObject& carried_object); 1592 void on_exploded(); 1593 void on_regenerating(); 1594 void on_custom_attack_received(EnemyAttack attack, Sprite* sprite); 1595 bool on_hurt_by_sword(Hero& hero, Sprite& enemy_sprite); 1596 void on_hurt(EnemyAttack attack); 1597 void on_dying(); 1598 void on_dead(); 1599 void on_immobilized(); 1600 bool on_attacking_hero(Hero& hero, Sprite* attacker_sprite); 1601 void on_attacked_enemy( 1602 Enemy& enemy, 1603 Sprite* enemy_sprite, 1604 EnemyAttack attack, 1605 const EnemyReaction::Reaction& reaction 1606 ); 1607 void on_ground_below_changed(Ground ground_below); 1608 void on_map_started( 1609 Map& map, const std::shared_ptr<Destination>& destination); 1610 void on_map_opening_transition_finished( 1611 Map& map, const std::shared_ptr<Destination>& destination); 1612 void on_map_finished(); 1613 1614 // Functions exported to Lua for internal needs. 1615 static FunctionExportedToLua 1616 l_panic, 1617 l_loader, 1618 l_get_map_entity_or_global, 1619 l_easy_index, 1620 l_hero_teleport, 1621 l_entity_iterator_next, 1622 l_named_sprite_iterator_next, 1623 l_treasure_brandish_finished, 1624 l_shop_treasure_description_dialog_finished, 1625 l_shop_treasure_question_dialog_finished, 1626 l_create_tile, 1627 l_create_destination, 1628 l_create_teletransporter, 1629 l_create_pickable, 1630 l_create_destructible, 1631 l_create_chest, 1632 l_create_jumper, 1633 l_create_enemy, 1634 l_create_npc, 1635 l_create_block, 1636 l_create_dynamic_tile, 1637 l_create_switch, 1638 l_create_wall, 1639 l_create_sensor, 1640 l_create_crystal, 1641 l_create_crystal_block, 1642 l_create_shop_treasure, 1643 l_create_stream, 1644 l_create_door, 1645 l_create_stairs, 1646 l_create_separator, 1647 l_create_custom_entity, 1648 l_create_bomb, 1649 l_create_explosion, 1650 l_create_fire; 1651 1652 // Script data. 1653 lua_State* main_l; /**< The MAIN Lua state encapsulated. */ 1654 lua_State* current_l; /**< The presumed current Lua state running */ 1655 MainLoop& main_loop; /**< The Solarus main loop. */ 1656 1657 std::list<LuaMenuData> menus; /**< The menus currently running in their context. 1658 * Invalid ones are to be removed at the next cycle. */ 1659 std::map<TimerPtr, LuaTimerData> 1660 timers; /**< The timers currently running, with 1661 * their context and callback. */ 1662 std::list<TimerPtr> 1663 timers_to_remove; /**< Timers to be removed at the next cycle. */ 1664 1665 std::set<DrawablePtr> 1666 drawables; /**< All drawable objects created by 1667 * this script. */ 1668 std::set<DrawablePtr> 1669 drawables_to_remove; /**< Drawable objects to be removed at the 1670 * next cycle. */ 1671 std::map<const ExportableToLua*, std::set<std::string>> 1672 userdata_fields; /**< Existing string keys created on each 1673 * userdata with our __newindex. This is 1674 * only for performance, to avoid Lua 1675 * lookups for callbacks like on_update. */ 1676 std::set<std::string> 1677 warning_deprecated_functions; /**< Names of deprecated functions of 1678 * the API for which a warning was emitted. */ 1679 1680 std::queue<std::function<void(lua_State*)>> 1681 cross_state_callbacks; /**< Callbacks that must be executed on main from other coroutines */ 1682 1683 static const std::map<EntityType, lua_CFunction> 1684 entity_creation_functions; /**< Creation function of each entity type. */ 1685 static LuaContext* 1686 lua_context; /**< Singleton context */ 1687 }; 1688 1689 } 1690 1691 #endif 1692 1693