The Battle for Wesnoth  1.15.1+dev
game_lua_kernel.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
3  Part of the Battle for Wesnoth Project https://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 #pragma once
16 
17 #include "scripting/lua_kernel_base.hpp" // for lua_kernel_base
18 
19 #include "game_events/action_wml.hpp" // for wml_action, etc
20 
21 #include <stack>
22 #include <string> // for string
23 
24 class config;
25 class unit;
26 class vconfig;
27 namespace ai { class engine_lua; }
28 namespace ai { class lua_ai_action_handler; }
29 namespace ai { class lua_ai_context; }
30 namespace game_events { struct queued_event; }
31 
32 class game_display;
33 class game_state;
34 class game_board;
35 class unit_map;
36 class gamemap;
37 class team;
38 class game_data;
39 class tod_manager;
40 class play_controller;
41 class reports;
42 
43 struct map_location;
44 typedef int (*lua_CFunction) (lua_State *L);
45 
47 {
52 
53  // Private functions to ease access to parts of game_state
54  unit_map & units();
55  game_data & gamedata();
56  tod_manager & tod_man();
57 
59 
60  std::stack<game_events::queued_event const * > queued_events_;
61 
62  const game_events::queued_event & get_event_info();
63 
64  static void extract_preload_scripts(const config& game_config);
65  static std::vector<config> preload_scripts;
67 
68  friend class game_config_manager; // to allow it to call extract_preload_scripts
69 
70  // Private lua callbacks
71  int intf_allow_end_turn(lua_State *);
72  int intf_allow_undo(lua_State *);
73  int intf_cancel_action(lua_State *);
74  int intf_add_time_area(lua_State *);
75  int intf_remove_time_area(lua_State *);
76  int intf_animate_unit(lua_State *);
77  int intf_gamestate_inspector(lua_State *);
78  int impl_run_animation(lua_State *);
79  int intf_create_animator(lua_State *);
80  int intf_get_unit(lua_State *);
81  int intf_get_units(lua_State *);
82  int intf_get_displayed_unit(lua_State*);
83  int intf_match_unit(lua_State *L);
84  int intf_get_recall_units(lua_State *L);
85  int intf_get_variable(lua_State *L);
86  int intf_get_side_variable(lua_State *L);
87  int intf_set_variable(lua_State *L);
88  int intf_set_side_variable(lua_State *L);
89  int intf_highlight_hex(lua_State *L);
90  int intf_is_enemy(lua_State *L);
91  int intf_unit_ability(lua_State *L);
92  int intf_view_locked(lua_State *L);
93  int intf_lock_view(lua_State *L);
96  int intf_terrain_mask(lua_State *L);
97  int intf_get_terrain_info(lua_State *L);
98  int intf_get_time_of_day(lua_State *L);
99  int intf_get_village_owner(lua_State *L);
100  int intf_set_village_owner(lua_State *L);
101  int intf_get_map_size(lua_State *L);
102  int intf_get_mouseover_tile(lua_State *L);
103  int intf_get_selected_tile(lua_State *L);
104  int intf_get_starting_location(lua_State* L);
105  int impl_game_config_get(lua_State *L) override;
106  int impl_game_config_set(lua_State *L) override;
107  int impl_current_get(lua_State *L);
108  int intf_clear_messages(lua_State*);
109  int intf_end_level(lua_State*);
110  int impl_end_level_data_set(lua_State*);
111  int intf_get_end_level_data(lua_State*);
112  int intf_end_turn(lua_State*);
113  int intf_find_path(lua_State *L);
114  int intf_find_reach(lua_State *L);
115  int intf_find_cost_map(lua_State *L);
116  int intf_heal_unit(lua_State *L);
117  int intf_message(lua_State *L);
118  int intf_open_help(lua_State *L);
119  int intf_play_sound(lua_State *L);
120  int intf_print(lua_State *L);
121  void put_unit_helper(const map_location& loc);
122  int intf_put_unit(lua_State *L);
123  int intf_erase_unit(lua_State *L);
124  int intf_put_recall_unit(lua_State *L);
125  int intf_extract_unit(lua_State *L);
126  int intf_find_vacant_tile(lua_State *L);
127  int intf_float_label(lua_State *L);
128  int intf_set_end_campaign_credits(lua_State *L);
129  int intf_set_end_campaign_text(lua_State *L);
130  int intf_clear_menu_item(lua_State *L);
131  int intf_set_menu_item(lua_State *L);
132  int intf_set_next_scenario(lua_State *L);
133  int intf_shroud_op(lua_State *L, bool place_shroud);
134  int intf_simulate_combat(lua_State *L);
135  int intf_scroll_to_tile(lua_State *L);
136  int intf_select_hex(lua_State *L);
137  int intf_select_unit(lua_State *L);
138  int intf_deselect_hex(lua_State *L);
139  int intf_is_skipping_messages(lua_State *L);
140  int intf_skip_messages(lua_State *L);
141  int intf_get_locations(lua_State *L);
142  int intf_get_villages(lua_State *L);
143  int intf_match_location(lua_State *L);
144  int intf_match_side(lua_State *L);
145  int intf_set_side_id(lua_State *L);
146  int intf_modify_ai_wml(lua_State *L);
147  int intf_get_sides(lua_State* L);
148  int intf_add_tile_overlay(lua_State *L);
149  int intf_remove_tile_overlay(lua_State *L);
150  int intf_add_event(lua_State *L);
151  int intf_remove_event(lua_State *L);
152  int intf_color_adjust(lua_State *L);
153  int intf_delay(lua_State *L);
154  int intf_label(lua_State *L);
155  int intf_redraw(lua_State *L);
156  int intf_replace_schedule(lua_State *l);
157  int intf_set_time_of_day(lua_State *L);
158  int intf_scroll(lua_State *L);
159  int intf_get_all_vars(lua_State *L);
160  int impl_theme_item(lua_State *L, std::string name);
161  int impl_theme_items_get(lua_State *L);
162  int impl_theme_items_set(lua_State *L);
163  int cfun_builtin_effect(lua_State *L);
164  int cfun_wml_action(lua_State *L);
165  int intf_fire_event(lua_State *L, const bool by_id);
166  int intf_fire_wml_menu_item(lua_State *L);
167  int intf_teleport(lua_State *L);
168  int intf_remove_sound_source(lua_State *L);
169  int intf_add_sound_source(lua_State *L);
170  int intf_get_sound_source(lua_State *L);
171  int intf_log(lua_State *L);
172  int intf_toggle_fog(lua_State *L, const bool clear);
173  int intf_get_fog_or_shroud(lua_State *L, bool fog);
174  int intf_log_replay(lua_State* L);
175  int intf_zoom(lua_State* L);
176 
177  //private helpers
178  std::string synced_state();
179  void lua_chat(const std::string& caption, const std::string& msg);
180  std::vector<int> get_sides_vector(const vconfig& cfg);
181 
182 public:
183  game_board & board();
184  std::vector<team> & teams();
185  const gamemap & map() const;
186  game_display * get_display() const { return game_display_; }
187  /**
188  A value != 0 means that the shouldn't remove any units from the map, usually because
189  we are currently operating on a unit& and removing it might cause memory corruptions
190  note that we don't check for the dtor of lua owned units because we assume that
191  we operate on such a unit that the lua function that invoked the operation on that unit
192  (like wesnoth.add_modification, wesnoth.match_unit ..) have a local copy of that
193  lua_unit* userdata in its stack that prevents it from being collected.
194  */
197 
198  void set_game_display(game_display * gd);
199 
200  virtual std::string my_name() override { return "Game Lua Kernel"; }
201 
202  std::string apply_effect(const std::string& name, unit& u, const config& cfg, bool need_apply);
203  void initialize(const config& level);
204  void save_game(config & level);
205  void load_game(const config& level);
206  bool run_event(const game_events::queued_event&);
207  void custom_command(const std::string&, const config&);
208  void push_builtin_effect();
209  void set_wml_action(const std::string&, game_events::wml_action::handler);
210  void set_wml_condition(const std::string&, bool(*)(const vconfig&));
211  bool run_wml_action(const std::string&, const vconfig&,
213  bool run_filter(char const *name, const unit& u);
214  bool run_filter(char const *name, const map_location& l);
215  bool run_filter(char const *name, int nArgs);
216  bool run_wml_conditional(const std::string&, const vconfig&);
217 
218  virtual void log_error(char const* msg, char const* context = "Lua error") override;
219 
220  ai::lua_ai_context* create_lua_ai_context(char const *code, ai::engine_lua *engine);
221  ai::lua_ai_action_handler* create_lua_ai_action_handler(char const *code, ai::lua_ai_context &context);
222 
223  void mouse_over_hex_callback(const map_location& loc);
224  void select_hex_callback(const map_location& loc);
225 };
std::stack< game_events::queued_event const *> queued_events_
Game board class.
Definition: game_board.hpp:50
int map_locked_
A value != 0 means that the shouldn&#39;t remove any units from the map, usually because we are currently...
game_display * game_display_
This class represents a single unit of a specific type.
Definition: unit.hpp:99
int(* lua_CFunction)(lua_State *L)
void clear(const std::string &key)
Definition: general.cpp:205
virtual std::string my_name() override
User-visible name of the lua kernel that they are talking to.
Define actions for the game&#39;s events mechanism.
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
void(* handler)(const queued_event &, const vconfig &)
Definition: action_wml.hpp:49
game_data * gamedata
Definition: resources.cpp:22
Unit and team statistics.
bool fog()
Definition: game.cpp:552
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
static int intf_delay(lua_State *L)
Proxy table for the AI context.
Definition: core.hpp:33
Encapsulates the map of the game.
Definition: map.hpp:36
play_controller & play_controller_
Encapsulates the map of the game.
Definition: location.hpp:42
Domain specific events.
Definition: action_wml.cpp:88
Game configuration data as global variables.
Definition: build_info.cpp:49
static int intf_set_terrain(lua_State *L)
Sets a terrain code.
Proxy class for calling AI action handlers defined in Lua.
Definition: core.hpp:69
static int intf_find_path(lua_State *L)
Finds a path between two locations.
A variable-expanding proxy for the config class.
Definition: variable.hpp:42
static config preload_config
Container associating units to locations.
Definition: map.hpp:99
game_display * get_display() const
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
std::string custom_command()
Definition: game.cpp:962
static int intf_get_terrain(lua_State *L)
Gets a terrain code.
game_state & game_state_
static std::vector< config > preload_scripts
static int intf_log(lua_State *L)
Logs a message Arg 1: (optional) Logger Arg 2: Message.