The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
game_lua_kernel.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Guillaume Melquiond <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program 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 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef SCRIPTING_LUA_HPP
16 #define SCRIPTING_LUA_HPP
17 
18 #include "scripting/lua_kernel_base.hpp" // for lua_kernel_base
19 
20 #include "game_events/action_wml.hpp" // for wml_action, etc
21 
22 #include <stack>
23 #include <string> // for string
24 
25 class config;
26 class CVideo;
27 class unit;
28 class vconfig;
29 namespace ai { class engine_lua; }
30 namespace ai { class lua_ai_action_handler; }
31 namespace ai { class lua_ai_context; }
32 namespace game_events { struct queued_event; }
33 
34 class game_display;
35 class game_state;
36 class game_board;
37 class unit_map;
38 class gamemap;
39 class team;
40 class game_data;
41 class tod_manager;
42 class play_controller;
43 class reports;
44 
45 struct map_location;
46 typedef int (*lua_CFunction) (lua_State *L);
47 
49 {
54 
55  // Private functions to ease access to parts of game_state
56  game_board & board();
57  unit_map & units();
58  game_data & gamedata();
59  tod_manager & tod_man();
60 
62 
63  std::stack<game_events::queued_event const * > queued_events_;
64 
66 
67  static void extract_preload_scripts(config const & game_config);
68  static std::vector<config> preload_scripts;
70 
71  friend class game_config_manager; // to allow it to call extract_preload_scripts
72 
73  // Private lua callbacks
80  int intf_get_unit(lua_State *);
83  int intf_match_unit(lua_State *L);
87  int intf_random(lua_State *L);
91  int intf_is_enemy(lua_State *L);
93  int intf_lock_view(lua_State *L);
106  int impl_current_get(lua_State *L);
110  int intf_end_turn(lua_State*);
111  int intf_find_path(lua_State *L);
112  int intf_find_reach(lua_State *L);
114  int intf_heal_unit(lua_State *L);
115  int intf_message(lua_State *L);
116  int intf_open_help(lua_State *L);
117  int intf_play_sound(lua_State *L);
118  int intf_print(lua_State *L);
119  int intf_put_unit(lua_State *L);
120  int intf_erase_unit(lua_State *L);
124  int intf_float_label(lua_State *L);
130  int intf_shroud_op(lua_State *L, bool place_shroud);
133  int intf_select_hex(lua_State *L);
134  int intf_select_unit(lua_State *L);
141  int intf_match_side(lua_State *L);
143  int intf_modify_side(lua_State *L);
144  int intf_get_sides(lua_State* L);
147  int intf_add_event(lua_State *L);
150  int intf_delay(lua_State *L);
151  int intf_kill(lua_State *L);
152  int intf_label(lua_State *L);
153  int intf_redraw(lua_State *L);
156  int intf_scroll(lua_State *L);
162  int cfun_wml_action(lua_State *L);
163  int intf_fire_event(lua_State *L);
165  int intf_teleport(lua_State *L);
169  int intf_log(lua_State *L);
170  int intf_toggle_fog(lua_State *L, const bool clear);
171 
172  //private helpers
174  void lua_chat(std::string const &caption, std::string const &msg);
175  std::vector<int> get_sides_vector(const vconfig& cfg);
176 
177 public:
178  std::vector<team> & teams();
179  const gamemap & map() const;
180  /**
181  A value != 0 means that the shouldn't remove any units from the map, usually because
182  we are currently operating on a unit& and removing it might cause memory corruptions
183  note that we don't check for the dtor of lua owned units because we assume that
184  we operate on such a unit that the lua function that invoked the operation on that unit
185  (like wesnoth.add_modification, wesnoth.match_unit ..) have a local copy of that
186  lua_unit* userdata in its stack that prevents it from beeing collected.
187  */
190 
191  void set_game_display(game_display * gd);
192 
193  virtual std::string my_name() { return "Game Lua Kernel"; }
194 
195  std::string apply_effect(const std::string& name, unit& u, const config& cfg, bool need_apply);
196  void initialize(const config& level);
197  void save_game(config & level);
198  void load_game(const config& level);
199  bool run_event(game_events::queued_event const &);
200  void push_builtin_effect();
202  bool run_wml_action(std::string const &, vconfig const &,
203  game_events::queued_event const &);
204  bool run_filter(char const *name, unit const &u);
205  bool run_filter(char const *name, map_location const &l);
206  bool run_filter(char const *name, int nArgs);
207  bool run_wml_conditional(std::string const &, vconfig const &);
208 
209  virtual void log_error(char const* msg, char const* context = "Lua error");
210 
211  ai::lua_ai_context* create_lua_ai_context(char const *code, ai::engine_lua *engine);
213  int return_unit_method(lua_State *L, char const *m);
214 
215  void mouse_over_hex_callback(const map_location& loc);
216  void select_hex_callback(const map_location& loc);
217 };
218 
219 #endif
int intf_get_terrain_info(lua_State *L)
Gets details about a terrain.
int intf_kill(lua_State *L)
const gamemap & map() const
std::stack< game_events::queued_event const * > queued_events_
Game board class.
Definition: game_board.hpp:55
int map_locked_
A value != 0 means that the shouldn't remove any units from the map, usually because we are currently...
std::string apply_effect(const std::string &name, unit &u, const config &cfg, bool need_apply)
void(* handler)(const queued_event &, const vconfig &)
Definition: action_wml.hpp:51
int intf_remove_sound_source(lua_State *L)
Removes a sound source by its ID Arg 1: sound source ID.
int intf_allow_end_turn(lua_State *)
Allow undo sets the flag saying whether the event has mutated the game to false.
int intf_find_path(lua_State *L)
Finds a path between two locations.
int intf_set_end_campaign_credits(lua_State *L)
game_display * game_display_
int intf_get_time_of_day(lua_State *L)
Gets time of day information.
int intf_get_unit(lua_State *)
Gets the unit at the given location or with the given id.
Definition: unit.hpp:95
GLint level
Definition: glew.h:1220
int intf_get_variable(lua_State *L)
Gets a WML variable.
int intf_modify_ai_wml(lua_State *L)
int intf_match_location(lua_State *L)
Matches a location against the given filter.
std::vector< int > get_sides_vector(const vconfig &cfg)
Gets a vector of sides from side= attribute in a given config node.
int impl_theme_items_get(lua_State *L)
Creates a field of the theme_items table and returns it (__index metamethod).
int intf_replace_schedule(lua_State *l)
Replacing the current time of day schedule.
bool run_wml_action(std::string const &, vconfig const &, game_events::queued_event const &)
Runs a command from an event handler.
int intf_view_locked(lua_State *L)
Gets whether gamemap scrolling is disabled for the user.
void set_wml_action(std::string const &, game_events::wml_action::handler)
Registers a function for use as an action handler.
std::vector< team > & teams()
int intf_set_terrain(lua_State *L)
Sets a terrain code.
int intf_log(lua_State *L)
Logs a message Arg 1: (optional) Logger; "wml" for WML errors or deprecations Arg 2: Message Arg 3: W...
int intf_clear_messages(lua_State *)
Removes all messages from the chat window.
Definition: video.hpp:58
int intf_play_sound(lua_State *L)
Plays a sound, possibly repeated.
int intf_label(lua_State *L)
int intf_extract_unit(lua_State *L)
Extracts a unit from the map or a recall list and gives it to Lua.
int intf_get_all_vars(lua_State *L)
Gets all the WML variables currently set.
int intf_find_cost_map(lua_State *L)
Is called with one or more units and builds a cost map.
void clear(const std::string &key)
int intf_set_time_of_day(lua_State *L)
Define actions for the game's events mechanism.
int intf_set_village_owner(lua_State *L)
Sets the owner of a village.
int intf_match_unit(lua_State *L)
Matches a unit against the given filter.
int intf_set_next_scenario(lua_State *L)
void push_builtin_effect()
Registers a function for use as an effect handler.
int intf_skip_messages(lua_State *L)
Set whether to skip messages Arg 1 (optional) - boolean.
int intf_scroll(lua_State *L)
int impl_theme_items_set(lua_State *L)
Sets a field of the theme_items table (__newindex metamethod).
int intf_get_sides(lua_State *L)
Returns a proxy table array for all sides matching the given SSF.
void lua_chat(std::string const &caption, std::string const &msg)
int intf_gamestate_inspector(lua_State *)
bool run_wml_conditional(std::string const &, vconfig const &)
Runs a command from an event handler.
const game_events::queued_event & get_event_info()
int intf_get_locations(lua_State *L)
Gets all the locations matching a given filter.
Unit and team statistics.
GLdouble l
Definition: glew.h:6966
static void extract_preload_scripts(config const &game_config)
int intf_find_reach(lua_State *L)
Finds all the locations reachable by a unit.
void set_game_display(game_display *gd)
int intf_set_variable(lua_State *L)
Sets a WML variable.
int intf_delay(lua_State *L)
Delays engine for a while.
int(* lua_CFunction)(lua_State *L)
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:50
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:57
void select_hex_callback(const map_location &loc)
int intf_get_map_size(lua_State *L)
Returns the map size.
int intf_simulate_combat(lua_State *L)
Simulates a combat between two units.
int intf_select_unit(lua_State *L)
Selects and highlights the given location on the map.
int intf_remove_tile_overlay(lua_State *L)
Removes an overlay from a tile.
int intf_remove_time_area(lua_State *)
Removing new time_areas dynamically with Standard Location Filters.
tod_manager & tod_man()
Proxy table for the AI context.
Definition: core.hpp:35
int intf_get_villages(lua_State *L)
Gets all the villages matching a given filter, or all the villages on the map if no filter is given...
int intf_fire_wml_menu_item(lua_State *L)
Fires a wml menu item.
int intf_set_end_campaign_text(lua_State *L)
int intf_get_displayed_unit(lua_State *)
Gets the unit displayed in the sidebar.
std::string synced_state()
converts synced_context::get_synced_state() to a string.
virtual void log_error(char const *msg, char const *context="Lua error")
int intf_get_recall_units(lua_State *L)
Gets the numeric ids of all the units matching a given filter on the recall lists.
Encapsulates the map of the game.
Definition: map.hpp:37
void initialize(const config &level)
int intf_teleport(lua_State *L)
Teeleports a unit to a location.
int intf_get_village_owner(lua_State *L)
Gets the side of a village owner.
int cfun_builtin_effect(lua_State *L)
Applies its upvalue as an effect Arg 1: The unit to apply to Arg 3: The [effect] tag contents Arg 3: ...
int intf_is_enemy(lua_State *L)
Returns whether the first side is an enemy of the second one.
int intf_is_skipping_messages(lua_State *L)
Return true if a replay is in progress but the player has chosen to skip it.
int intf_get_sound_source(lua_State *L)
Get an existing sound source Arg 1: The sound source ID Return: Config of sound source info...
int intf_put_unit(lua_State *L)
Places a unit on the map.
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
int intf_redraw(lua_State *L)
int intf_add_tile_overlay(lua_State *L)
Adds an overlay on a tile.
int impl_current_get(lua_State *L)
Gets some data about current point of game (__index metamethod).
void load_game(const config &level)
Executes the game_events.on_load function and passes to it all the scenario tags not yet handled...
play_controller & play_controller_
int intf_modify_side(lua_State *L)
int impl_theme_item(lua_State *L, std::string name)
Executes its upvalue as a theme item generator.
void save_game(config &level)
Executes the game_events.on_save function and adds to cfg the returned tags.
Encapsulates the map of the game.
Definition: location.hpp:38
ai::lua_ai_context * create_lua_ai_context(char const *code, ai::engine_lua *engine)
int intf_add_event(lua_State *L)
Adding new events.
Domain specific events.
Definition: action_wml.cpp:93
int intf_message(lua_State *L)
Displays a message in the chat window and in the logs.
int intf_heal_unit(lua_State *L)
int impl_game_config_set(lua_State *L)
Sets some game_config data (__newindex metamethod).
game_data & gamedata()
game_board & board()
int intf_end_turn(lua_State *)
int intf_remove_event(lua_State *L)
int intf_get_units(lua_State *)
Gets all the units matching a given filter.
Game configuration data as global variables.
Definition: build_info.cpp:38
int intf_set_side_variable(lua_State *L)
Gets a side specific WML variable.
int intf_animate_unit(lua_State *)
int intf_scroll_to_tile(lua_State *L)
Scrolls to given tile.
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
ai::lua_ai_action_handler * create_lua_ai_action_handler(char const *code, ai::lua_ai_context &context)
bool run_filter(char const *name, unit const &u)
Runs a script from a unit filter.
int intf_lock_view(lua_State *L)
Sets whether gamemap scrolling is disabled for the user.
int intf_add_time_area(lua_State *)
Adding new time_areas dynamically with Standard Location Filters.
int intf_get_starting_location(lua_State *L)
Returns the starting position of a side.
int impl_game_config_get(lua_State *L)
Gets some game_config data (__index metamethod).
int intf_get_selected_tile(lua_State *L)
Returns the currently selected tile.
int intf_set_menu_item(lua_State *L)
int intf_add_sound_source(lua_State *L)
Add a new sound source Arg 1: Table containing keyword arguments.
int intf_shroud_op(lua_State *L, bool place_shroud)
Proxy class for calling AI action handlers defined in Lua.
Definition: core.hpp:71
int intf_allow_undo(lua_State *)
Allow undo sets the flag saying whether the event has mutated the game to false.
int intf_match_side(lua_State *L)
Matches a side against the given filter.
GLuint const GLchar * name
Definition: glew.h:1782
int intf_toggle_fog(lua_State *L, const bool clear)
Implements the lifting and resetting of fog via WML.
bool run_event(game_events::queued_event const &)
Executes the game_events.on_event function.
virtual std::string my_name()
int intf_erase_unit(lua_State *L)
Erases a unit from the map.
int intf_color_adjust(lua_State *L)
const GLdouble * m
Definition: glew.h:6968
int intf_open_help(lua_State *L)
int intf_clear_menu_item(lua_State *L)
int intf_get_mouseover_tile(lua_State *L)
Returns the currently overed tile.
A variable-expanding proxy for the config class.
Definition: variable.hpp:36
static config preload_config
Container associating units to locations.
Definition: map.hpp:90
int intf_print(lua_State *L)
void mouse_over_hex_callback(const map_location &loc)
int intf_put_recall_unit(lua_State *L)
Puts a unit on a recall list.
int cfun_wml_action(lua_State *L)
Executes its upvalue as a wml action.
int intf_get_side_variable(lua_State *L)
Gets a side specific WML variable.
int intf_get_terrain(lua_State *L)
Gets a terrain code.
int intf_deselect_hex(lua_State *L)
Deselects any highlighted hex on the map.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
int intf_check_end_level_disabled(lua_State *L)
int intf_find_vacant_tile(lua_State *L)
Finds a vacant tile.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
int intf_fire_event(lua_State *L)
Fires an event.
int intf_random(lua_State *L)
Returns a random numer, same interface as math.random.
game_state & game_state_
int intf_select_hex(lua_State *L)
static std::vector< config > preload_scripts
int return_unit_method(lua_State *L, char const *m)
int intf_float_label(lua_State *L)
Floats some text on the map.
int intf_end_level(lua_State *)
int intf_highlight_hex(lua_State *L)
Highlights the given location on the map.
game_lua_kernel(CVideo *, game_state &, play_controller &, reports &)