14 #define GETTEXT_DOMAIN "wesnoth-editor" 33 #include <boost/regex.hpp> 40 , name(t.user_team_name())
47 , share_vision(t.share_vision())
49 , no_leader(t.no_leader())
64 , actions_since_save_(0)
65 , starting_position_label_locs_()
66 , needs_reload_(false)
67 , needs_terrain_rebuild_(false)
68 , needs_labels_reset_(false)
69 , changed_locations_()
70 , everything_changed_(false)
73 , scenario_description_()
75 , victory_defeated_(true)
83 , game_classification_()
151 if(file_string.empty()) {
152 std::string message =
_(
"Empty file");
157 boost::regex rexpression_map_data(R
"""(map_data\s*=\s*"(.+?)")"""); 158 boost::smatch matched_map_data; 160 if(!boost::regex_search(
161 file_string, matched_map_data, rexpression_map_data, boost::regex_constants::match_not_dot_null)
171 const std::string& map_data = matched_map_data[1];
173 boost::regex rexpression_macro(R
"""(\{(.+?)\})"""); 174 boost::smatch matched_macro; 176 if(!boost::regex_search(map_data, matched_macro, rexpression_macro)) {
179 boost::regex rexpression_scenario(R
"""(\[(scenario|test|multiplayer|tutorial)\])"""); 180 if(!boost::regex_search(file_string, rexpression_scenario)) {
181 LOG_ED <<
"Loading generated scenario file" << std::endl;
190 LOG_ED <<
"Loading embedded map file" << std::endl;
201 const std::string& macro_argument = matched_macro[1];
202 LOG_ED <<
"Map looks like a scenario, trying {" << macro_argument <<
"}" << std::endl;
207 if(new_filename.empty()) {
208 std::string message =
_(
"The map file looks like a scenario, " 209 "but the map_data value does not point to an existing file")
210 + std::string(
"\n") + macro_argument;
214 LOG_ED <<
"New filename is: " << new_filename << std::endl;
235 cfg[
"side"] =
teams_.size();
236 cfg[
"hidden"] =
false;
246 assert(
teams_.size() >=
static_cast<unsigned int>(info.
side));
267 const std::string& name,
268 const std::string& description,
331 xp_mod_ = experience_modifier->to_int();
334 random_time_ = scenario[
"random_start_time"].to_bool(
false);
391 LOG_ED <<
"Attempted to draw terrain off the map (" << loc <<
")\n";
397 if(terrain != old_terrain) {
400 }
else if(one_layer_only) {
480 scenario[
"experience_modifier"] = *
xp_mod_;
490 if(scenario[
"turns"].to_int() == -1) {
498 for(
const auto& overlay_pair :
overlays_) {
499 for(
const overlay& o : overlay_pair.second) {
503 overlay_pair.first.write(item);
506 item[
"image"] = o.
image;
510 item[
"id"].write_if_not_empty(o.
id);
511 item[
"name"].write_if_not_empty(o.
name);
512 item[
"team_name"].write_if_not_empty(o.
team_name);
513 item[
"halo"].write_if_not_empty(o.
halo);
518 track.second.write(scenario,
true);
521 for(std::vector<team>::const_iterator
t =
teams_.begin();
t !=
teams_.end(); ++
t) {
522 int side_num =
t -
teams_.begin() + 1;
526 side[
"side"] = side_num;
527 side[
"hidden"] =
t->hidden();
529 side[
"controller"] =
t->controller();
530 side[
"no_leader"] =
t->no_leader();
532 side[
"team_name"] =
t->team_name();
533 side[
"user_team_name"].write_if_not_empty(
t->user_team_name());
538 side[
"fog"] =
t->uses_fog();
539 side[
"shroud"] =
t->uses_shroud();
540 side[
"share_vision"] =
t->share_vision();
542 side[
"gold"] =
t->gold();
543 side[
"income"] =
t->base_income();
546 village.write(side.
add_child(
"village"));
551 if(
i->side() == side_num) {
554 i->get_location().write(u);
556 u[
"type"] =
i->type_id();
557 u[
"name"].write_if_not_empty(
i->name());
560 if(!boost::regex_match(
i->id(), boost::regex(
".*-[0-9]+"))) {
564 if(
i->can_recruit()) {
565 u[
"canrecruit"] =
i->can_recruit();
568 if(
i->unrenamable()) {
569 u[
"unrenamable"] =
i->unrenamable();
572 if(!
i->recruits().empty()) {
595 std::stringstream wml_stream;
597 <<
"# This file was generated using the scenario editor.\n" 599 <<
"# If you edit this file by hand, then you shouldn't use the\n" 600 <<
"# scenario editor on it afterwards. The editor completely\n" 601 <<
"# rewrites the file when it saves it, which will lose any WML\n" 602 <<
"# that the editor doesn't support.\n" 609 if(!wml_stream.str().empty()) {
616 symbols[
"msg"] = e.
what();
617 const std::string
msg =
VGETTEXT(
"Could not save the scenario: $msg", symbols);
640 boost::regex rexpression_map_data(R
"""((.*map_data\s*=\s*")(.+?)(".*))"""); 641 boost::smatch matched_map_data; 643 if(boost::regex_search(map_string, matched_map_data, rexpression_map_data,
644 boost::regex_constants::match_not_dot_null)) {
645 std::stringstream ss;
646 ss << matched_map_data[1];
648 ss << matched_map_data[3];
661 symbols[
"msg"] = e.
what();
662 const std::string
msg =
VGETTEXT(
"Could not save the map: $msg", symbols);
685 LOG_ED <<
"Performing action " << action.
get_id() <<
": " << action.
get_name() <<
", actions count is " 705 LOG_ED <<
"Performing (partial) action " << action.
get_id() <<
": " << action.
get_name() <<
", actions count is " 712 if(undo_chain ==
nullptr) {
777 WRN_ED <<
"undo() called with an empty undo stack" << std::endl;
791 WRN_ED <<
"redo() called with an empty redo stack" << std::endl;
805 if(undo_chain ==
nullptr) {
812 if(undo_chain->
empty()) {
817 redo_stack_.emplace_back(first_action_in_chain->perform(*
this));
836 assert(!from.empty());
838 std::unique_ptr<editor_action> action;
839 action.swap(from.back());
843 auto reverse_action = action->perform(*
this);
844 to.emplace_back(std::move(reverse_action));
void add_changed_location(const map_location &loc)
void set_side_setup(editor_team_info &info)
TODO.
int actions_since_save_
Number of actions performed since the map was saved.
action_stack undo_stack_
The undo stack.
std::string map_data_key_
When a scenario file is loaded, the referenced map is loaded instead.
editor_action * last_redo_action()
void undo()
Un-does the last action, and puts it in the redo stack for a possible redo.
editor_map map_
The map object of this map_context.
::tod_manager * tod_manager
void set_shroud(bool shroud)
game_classification game_classification_
std::set< map_location > starting_position_label_locs_
Cache of set starting position labels.
bool needs_terrain_rebuild_
Refresh flag indicating the terrain in the map has changed and requires a rebuild.
void write(const config &cfg)
static editor_map from_string(const std::string &data)
Wrapper around editor_map(cfg, data) that catches possible exceptions and wraps them in a editor_map_...
std::map< std::string, t_string > string_map
void set_terrain(const map_location &loc, const terrain_code &terrain, const terrain_type_data::merge_mode mode=terrain_type_data::BOTH, bool replace_if_failed=false) override
Clobbers over the terrain at location 'loc', with the given terrain.
void append(const config &cfg)
Append data from another config object to this one.
void perform_action_between_stacks(action_stack &from, action_stack &to)
Perform an action at the back of one stack, and then move it to the back of the other stack...
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
team::SHARE_VISION share_vision
std::set< map_location > set_starting_position_labels(display &disp)
Set labels for staring positions in the given display object.
umap_retval_pair_t insert(unit_ptr p)
Inserts the unit pointed to by p into the map.
virtual const std::string & get_name() const
Variant for storing WML attributes.
void new_side()
Adds a new side to the map.
bool save_scenario()
Saves the scenario under the current filename.
virtual ~map_context()
Map context destructor.
std::unique_ptr< editor_action > pop_first_action()
Remove the first added action and return it, transferring ownership to the caller.
static bool file_exists(const bfs::path &fpath)
const std::string & get_filename() const
child_itors child_range(config_key_type key)
bool needs_reload_
Refresh flag indicating the map in this context should be completely reloaded by the display...
const attribute_value * get(config_key_type key) const
Returns a pointer to the attribute with the given key or nullptr if it does not exist.
void draw_terrain(const t_translation::terrain_code &terrain, const map_location &loc, bool one_layer_only=false)
Draw a terrain on a single location on the map.
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
const terrain_type & get_terrain_info(const t_translation::terrain_code &terrain) const
static int get_instance_count()
Debugging aid.
void redo()
Re-does a previously undid action, and puts it back in the undo stack.
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
lg::log_domain log_editor
Container action wrapping several actions into one.
static std::string _(const char *str)
void change_controller(const std::string &new_controller)
void set_share_vision(const std::string &vision_status)
void set_hidden(bool value)
void remove_attribute(config_key_type key)
std::string scenario_description_
std::string scenario_name_
void set_needs_reload(bool value=true)
Setter for the reload flag.
std::deque< std::unique_ptr< editor_action > > action_stack
Action stack typedef.
void clear_modified()
Clear the modified state.
void partial_undo()
Un-does a single step from a undo action chain.
std::optional< int > xp_mod_
static unit_ptr create(const config &cfg, bool use_traits=false, const vconfig *vcfg=nullptr)
Initializes a unit from a config.
bool embedded_
Whether the map context refers to a map embedded in a scenario file.
void set_scenario_setup(const std::string &id, const std::string &name, const std::string &description, int turns, int xp_mod, bool victory_defeated, bool random_time)
TODO.
void remove_area(int index)
void set_map(const editor_map &map)
bool pure_map_
Whether the map context refers to a file containing only the pure map data.
std::string write() const
This class stores all the data for a single 'side' (in game nomenclature).
void have_leader(bool value=true)
std::unique_ptr< tod_manager > tod_manager_
std::vector< team > teams_
editor_team_info(const team &t)
int w() const
Effective map width.
void read(config &cfg, std::istream &in, abstract_validator *validator)
Class for writing a config out to a file in pieces.
terrain_code get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
void reset_starting_position_labels(display &disp)
void write_file(const std::string &fname, const std::string &data)
Throws io_exception if an error occurs.
bool on_board_with_border(const map_location &loc) const
int get_id() const
Debugging aid.
void prepend_action(std::unique_ptr< editor_action > a)
Add an action at the beginning of the chain.
void replace_schedule(const std::vector< time_of_day > &schedule)
void add_to_recent_files()
Adds the map to the editor's recent files list.
bool is_directory(const std::string &fname)
Returns true if the given file is a directory.
void write(config &res) const
void set_starting_time(int time)
TODO.
bool save_map()
Saves the map under the current filename.
void set_village_support(int support)
std::string get_short_wml_path(const std::string &filename)
Returns a short path to filename, skipping the (user) data directory.
#define log_scope2(domain, description)
std::optional< bool > victory_defeated_
map_display and display: classes which take care of displaying the map and game-data on the screen...
editor_action * last_undo_action()
Manage the empty-palette in the editor.
void perform_action(const editor_action &action)
Performs an action (thus modifying the map).
const char * what() const noexcept
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
void set_needs_terrain_rebuild(bool value=true)
Setter for the terrain rebuild flag.
bool needs_labels_reset_
Refresh flag indicating the labels in the map have changed.
Encapsulates the map of the game.
void set_village_gold(int income)
void perform_partial_action(const editor_action &action)
Performs a partial action, assumes that the top undo action has been modified to maintain coherent st...
void clear_changed_locations()
void set_starting_position_labels(display &disp)
void set_everything_changed()
This class adds extra editor-specific functionality to a normal gamemap.
void set_gold(int amount)
std::string get_wml_location(const std::string &filename, const std::string ¤t_dir)
Returns a complete path to the actual WML file or directory or an empty string if the file isn't pres...
Game configuration data as global variables.
map_context(const map_context &)=delete
t_translation::terrain_code terrain_with_default_base() const
An exception object used when an IO error occurs.
Base class for all editor actions.
Internal representation of music tracks.
void draw_terrain_actual(const t_translation::terrain_code &terrain, const map_location &loc, bool one_layer_only=false)
Actual drawing function used by both overloaded variants of draw_terrain.
Declarations for File-IO.
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
config & add_child(config_key_type key)
action_stack redo_stack_
The redo stack.
std::string base_name(const std::string &file, const bool remove_extension)
Returns the base filename of a file, with directory name stripped.
virtual std::unique_ptr< editor_action > perform(map_context &) const
Perform the action, returning an undo action that, when performed, shall reverse any effects of this ...
bool victory_defeated() const
void read(const config &cfg)
virtual const editor_map & map() const override
Const map accessor.
team::CONTROLLER controller
void clear_undo_redo()
Clear the undo and redo stacks.
void set_needs_labels_reset(bool value=true)
Setter for the labels reset flag.
static const std::size_t max_action_stack_size_
Action stack (i.e.
void replace_local_schedule(const std::vector< time_of_day > &schedule)
Replace the [time]s of the currently active area.
std::string filename_
The actual filename of this map.
void change_team(const std::string &name, const t_string &user_name)
void trim_stack(action_stack &stack)
Checks if an action stack reached its capacity and removes the front element if so.
void set_base_income(int amount)
void add_recent_files_entry(const std::string &path)
Adds an entry to the recent files list.
std::set< map_location > changed_locations_
void clear_starting_position_labels(display &disp)
A config object defines a single node in a WML file, with access to child nodes.
filesystem::scoped_istream preprocess_file(const std::string &fname, preproc_map *defines)
Function to use the WML preprocessor on a file.
int h() const
Effective map height.
static std::string write_direction(DIRECTION dir)
mp_game_settings mp_settings_
bool set_selection(const std::set< map_location > &area)
Select the given area.
std::string directory_name(const std::string &file)
Returns the directory name of a file, with filename stripped.
std::pair< std::string, unsigned > item
const t_string get_default_context_name() const
bool select_area(int index)
Select the nth tod area.
bool everything_changed() const