Brings back the old map format.

This commit is contained in:
Fabian Müller 2012-08-24 18:49:30 +00:00
parent c62cccd0ab
commit 84b8dbafd2
17 changed files with 400 additions and 358 deletions

View file

@ -402,7 +402,8 @@ editor_action_create_mask* editor_action_create_mask::clone() const
}
void editor_action_create_mask::perform_without_undo(map_context& mc) const
{
mc.get_map() = mc.get_map().mask_to(target_);
//TODO
//mc.get_map() = mc.get_map().mask_to(target_);
mc.set_needs_terrain_rebuild();
}

View file

@ -34,22 +34,23 @@ editor_action_label* editor_action_label::clone() const
editor_action* editor_action_label::perform(map_context& mc) const
{
std::auto_ptr<editor_action> undo;
const terrain_label *old_label = mc.get_map().get_map_labels().get_label(loc_);
if (old_label) {
undo.reset(new editor_action_label(loc_, old_label->text(), old_label->team_name(), old_label->color()
, old_label->visible_in_fog(), old_label->visible_in_shroud(), old_label->immutable()) );
} else {
undo.reset(new editor_action_label_delete(loc_));
}
//TODO
// const terrain_label *old_label = mc.get_map().get_map_labels().get_label(loc_);
// if (old_label) {
// undo.reset(new editor_action_label(loc_, old_label->text(), old_label->team_name(), old_label->color()
// , old_label->visible_in_fog(), old_label->visible_in_shroud(), old_label->immutable()) );
// } else {
// undo.reset(new editor_action_label_delete(loc_));
// }
perform_without_undo(mc);
return undo.release();
}
void editor_action_label::perform_without_undo(map_context& mc) const
void editor_action_label::perform_without_undo(map_context& /*mc*/) const
{
mc.get_map().get_map_labels()
.set_label(loc_, text_, team_name_, color_, visible_fog_, visible_shroud_, immutable_);
// mc.get_map().get_map_labels()
// .set_label(loc_, text_, team_name_, color_, visible_fog_, visible_shroud_, immutable_);
}
editor_action_label_delete* editor_action_label_delete::clone() const
@ -61,18 +62,19 @@ editor_action* editor_action_label_delete::perform(map_context& mc) const
{
std::auto_ptr<editor_action> undo;
const terrain_label* deleted = mc.get_map().get_map_labels().get_label(loc_);
undo.reset(new editor_action_label(loc_, deleted->text(), deleted->team_name()
, deleted->color(), deleted->visible_in_fog(), deleted->visible_in_shroud(), deleted->immutable()));
//TODO
// const terrain_label* deleted = mc.get_map().get_map_labels().get_label(loc_);
//
// undo.reset(new editor_action_label(loc_, deleted->text(), deleted->team_name()
// , deleted->color(), deleted->visible_in_fog(), deleted->visible_in_shroud(), deleted->immutable()));
perform_without_undo(mc);
return undo.release();
}
void editor_action_label_delete::perform_without_undo(map_context& mc) const
void editor_action_label_delete::perform_without_undo(map_context& /*mc*/) const
{
mc.get_map().get_map_labels().set_label(loc_, "");
//mc.get_map().get_map_labels().set_label(loc_, "");
}

View file

@ -45,8 +45,8 @@ editor_action* editor_action_unit::perform(map_context& mc) const
void editor_action_unit::perform_without_undo(map_context& mc) const
{
mc.get_map().get_units().add(loc_,u_);
mc.get_map().get_units().find(loc_)->set_location(loc_);
// mc.get_map().get_units().add(loc_,u_);
// mc.get_map().get_units().find(loc_)->set_location(loc_);
mc.add_changed_location(loc_);
}
@ -56,30 +56,32 @@ editor_action_unit_delete* editor_action_unit_delete::clone() const
return new editor_action_unit_delete(*this);
}
editor_action* editor_action_unit_delete::perform(map_context& mc) const
editor_action* editor_action_unit_delete::perform(map_context& /*mc*/) const
{
std::auto_ptr<editor_action> undo;
//TODO
//boost::scoped_ptr<editor_action> undo;
unit_map& units = mc.get_map().get_units();
unit_map::const_unit_iterator unit_it = units.find(loc_);
//unit_map& units = mc.get_map().get_units();
//unit_map::const_unit_iterator unit_it = units.find(loc_);
if (unit_it != units.end()) {
undo.reset(new editor_action_unit(loc_, *unit_it));
perform_without_undo(mc);
return undo.release();
} else { return NULL; }
//if (unit_it != units.end()) {
// undo.reset(new editor_action_unit(loc_, *unit_it));
// perform_without_undo(mc);
// return undo.release();
//} else {
return NULL;
//}
}
void editor_action_unit_delete::perform_without_undo(map_context& mc) const
void editor_action_unit_delete::perform_without_undo(map_context& /*mc*/) const
{
unit_map& units = mc.get_map().get_units();
if (!units.erase(loc_)) {
ERR_ED << "Could not delete unit on " << loc_.x << "/" << loc_.y << "\n";
} else {
mc.add_changed_location(loc_);
}
// unit_map& units = mc.get_map().get_units();
// if (!units.erase(loc_)) {
// ERR_ED << "Could not delete unit on " << loc_.x << "/" << loc_.y << "\n";
// } else {
// mc.add_changed_location(loc_);
// }
}
@ -98,13 +100,13 @@ editor_action* editor_action_unit_replace::perform(map_context& mc) const
void editor_action_unit_replace::perform_without_undo(map_context& mc) const
{
unit_map& units = mc.get_map().get_units();
units.move(loc_, new_loc_);
//unit_map& units = mc.get_map().get_units();
//units.move(loc_, new_loc_);
unit::clear_status_caches();
unit& u = *units.find(new_loc_);
//unit& u = *units.find(new_loc_);
//TODO do we still need set_standing?
u.set_standing();
//u.set_standing();
mc.add_changed_location(loc_);
mc.add_changed_location(new_loc_);
@ -134,15 +136,15 @@ editor_action* editor_action_unit_facing::perform(map_context& mc) const
return undo.release();
}
void editor_action_unit_facing::perform_without_undo(map_context& mc) const
void editor_action_unit_facing::perform_without_undo(map_context& /*mc*/) const
{
unit_map& units = mc.get_map().get_units();
unit_map::unit_iterator unit_it = units.find(loc_);
//unit_map& units = mc.get_map().get_units();
//unit_map::unit_iterator unit_it = units.find(loc_);
if (unit_it != units.end()) {
unit_it->set_facing(new_direction_);
unit_it->set_standing();
}
//if (unit_it != units.end()) {
// unit_it->set_facing(new_direction_);
// unit_it->set_standing();
//}
}

View file

@ -35,30 +35,30 @@ editor_action* editor_action_village::perform(map_context& mc) const
std::auto_ptr<editor_action> undo;
if(!mc.get_map().is_village(loc_)) return NULL;
std::vector<team>& teams = mc.get_map().get_teams();
team *t = unsigned(side_number_) < teams.size() ? &teams[side_number_] : NULL;
if (t && t->owns_village(loc_)) return NULL;
//std::vector<team>& teams = mc.get_map().get_teams();
//team *t = unsigned(side_number_) < teams.size() ? &teams[side_number_] : NULL;
//if (t && t->owns_village(loc_)) return NULL;
undo.reset(new editor_action_village_delete(loc_));
for(std::vector<team>::iterator i = teams.begin(); i != teams.end(); ++i) {
if (i->owns_village(loc_))
undo.reset(new editor_action_village(loc_, i->side() -1));
}
//for(std::vector<team>::iterator i = teams.begin(); i != teams.end(); ++i) {
// if (i->owns_village(loc_))
// undo.reset(new editor_action_village(loc_, i->side() -1));
//}
perform_without_undo(mc);
return undo.release();
}
void editor_action_village::perform_without_undo(map_context& mc) const
void editor_action_village::perform_without_undo(map_context& /*mc*/) const
{
std::vector<team>& teams = mc.get_map().get_teams();
for(std::vector<team>::iterator i = teams.begin(); i != teams.end(); ++i) {
if (i->owns_village(loc_))
i->lose_village(loc_);
}
//std::vector<team>& teams = mc.get_map().get_teams();
//for(std::vector<team>::iterator i = teams.begin(); i != teams.end(); ++i) {
// if (i->owns_village(loc_))
// i->lose_village(loc_);
//}
teams[side_number_].get_village(loc_, 0, false);
//teams[side_number_].get_village(loc_, 0, false);
}
@ -71,26 +71,26 @@ editor_action* editor_action_village_delete::perform(map_context& mc) const
{
std::auto_ptr<editor_action> undo;
const std::vector<team>& teams = mc.get_map().get_teams();
for(std::vector<team>::const_iterator i = teams.begin(); i != teams.end(); ++i) {
if (i->owns_village(loc_)) {
//const std::vector<team>& teams = mc.get_map().get_teams();
//for(std::vector<team>::const_iterator i = teams.begin(); i != teams.end(); ++i) {
// if (i->owns_village(loc_)) {
perform_without_undo(mc);
undo.reset(new editor_action_village(loc_, i->side() -1));
}
}
// undo.reset(new editor_action_village(loc_, i->side() -1));
// }
//}
return undo.release();
}
void editor_action_village_delete::perform_without_undo(map_context& mc) const
void editor_action_village_delete::perform_without_undo(map_context& /*mc*/) const
{
std::vector<team>& teams = mc.get_map().get_teams();
for(std::vector<team>::iterator i = teams.begin(); i != teams.end(); ++i) {
if (i->owns_village(loc_)) {
i->lose_village(loc_);
mc.add_changed_location(loc_);
}
}
//std::vector<team>& teams = mc.get_map().get_teams();
//for(std::vector<team>::iterator i = teams.begin(); i != teams.end(); ++i) {
// if (i->owns_village(loc_)) {
// i->lose_village(loc_);
// mc.add_changed_location(loc_);
// }
//}
}

View file

@ -33,7 +33,7 @@ editor_action* mouse_action_map_label::click_left(editor_display& disp, int x, i
}
editor_action* mouse_action_map_label::drag_left(editor_display& disp, int x, int y
, bool& partial, editor_action* /*last_undo*/)
, bool& /*partial*/, editor_action* /*last_undo*/)
{
map_location hex = disp.hex_clicked_on(x, y);
@ -43,14 +43,14 @@ editor_action* mouse_action_map_label::drag_left(editor_display& disp, int x, in
click_ = false;
editor_action_chain* chain = NULL;
const terrain_label* label = disp.map().get_map_labels().get_label(last_draged_);
//const terrain_label* label = disp.map().get_map_labels().get_label(last_draged_);
if (label) {
partial = true;
chain = new editor_action_chain(new editor_action_label_delete(last_draged_));
chain->append_action(new editor_action_label(hex, label->text(), label->team_name(), label->color(),
label->visible_in_shroud(), label->visible_in_fog(), label->immutable()));
}
// if (label) {
// partial = true;
// chain = new editor_action_chain(new editor_action_label_delete(last_draged_));
// chain->append_action(new editor_action_label(hex, label->text(), label->team_name(), label->color(),
// label->visible_in_shroud(), label->visible_in_fog(), label->immutable()));
// }
last_draged_ = hex;
return chain;
@ -66,21 +66,21 @@ editor_action* mouse_action_map_label::up_left(editor_display& disp, int x, int
return NULL;
}
const terrain_label* old_label = disp.map().get_map_labels().get_label(hex);
std::string label = old_label ? old_label->text() : "";
std::string team_name = old_label ? old_label->team_name() : "";
bool visible_shroud = old_label ? old_label->visible_in_shroud() : false;
bool visible_fog = old_label ? old_label->visible_in_fog() : true;
bool immutable = old_label ? old_label->immutable() : true;
//const terrain_label* old_label = disp.map().get_map_labels().get_label(hex);
//std::string label = old_label ? old_label->text() : "";
//std::string team_name = old_label ? old_label->team_name() : "";
//bool visible_shroud = old_label ? old_label->visible_in_shroud() : false;
//bool visible_fog = old_label ? old_label->visible_in_fog() : true;
//bool immutable = old_label ? old_label->immutable() : true;
gui2::teditor_edit_label d(label, immutable, visible_fog, visible_shroud);
//gui2::teditor_edit_label d(label, immutable, visible_fog, visible_shroud);
editor_action* a = NULL;
if(d.show(disp.video())) {
a = new editor_action_label(hex, label, team_name, font::NORMAL_COLOR
, visible_fog, visible_shroud, immutable);
update_brush_highlights(disp, hex);
}
// if(d.show(disp.video())) {
// a = new editor_action_label(hex, label, team_name, font::NORMAL_COLOR
// , visible_fog, visible_shroud, immutable);
// update_brush_highlights(disp, hex);
// }
return a;
}
@ -93,9 +93,9 @@ editor_action* mouse_action_map_label::up_right(editor_display& disp, int x, int
{
map_location hex = disp.hex_clicked_on(x, y);
const terrain_label* clicked_label = disp.map().get_map_labels().get_label(hex);
if (!clicked_label)
return NULL;
// const terrain_label* clicked_label = disp.map().get_map_labels().get_label(hex);
//if (!clicked_label)
// return NULL;
return new editor_action_label_delete(hex);
}

View file

@ -18,6 +18,7 @@
#include "mouse_action.hpp"
#include "editor/palette/empty_palette.hpp"
#include "../../../map_label.hpp"
class CKey;

View file

@ -31,17 +31,17 @@ editor_action* mouse_action_unit::click_left(editor_display& disp, int x, int y)
return NULL;
}
const unit_map& units = disp.map().get_units();
const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
if (unit_it != units.end()) {
// const unit_map& units = disp.map().get_units();
// const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
// if (unit_it != units.end()) {
std::stringstream filename;
filename << unit_it->absolute_image() << unit_it->image_mods();
// std::stringstream filename;
// filename << unit_it->absolute_image() << unit_it->image_mods();
surface image(image::get_image(filename.str()));
disp.set_mouseover_hex_overlay(image);
// surface image(image::get_image(filename.str()));
// disp.set_mouseover_hex_overlay(image);
//TODO set the mouse pointer to a dragging one.
}
//}
click_ = true;
return NULL;
@ -89,10 +89,10 @@ editor_action* mouse_action_unit::drag_end_left(editor_display& disp, int x, int
if (!disp.get_map().on_board(hex))
return NULL;
const unit_map& units = disp.map().get_units();
const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
if (unit_it == units.end())
return NULL;
//const unit_map& units = disp.map().get_units();
//const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
//if (unit_it == units.end())
// return NULL;
action = new editor_action_unit_replace(start_hex_, hex);
return action;
@ -125,32 +125,32 @@ editor_action* mouse_action_unit::drag_right(editor_display& disp, int x, int y,
click_ = (start_hex_ == hex);
previous_move_hex_ = hex;
const unit_map& units = disp.map().get_units();
//const unit_map& units = disp.map().get_units();
const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
if (unit_it != units.end()) {
for (map_location::DIRECTION new_direction = map_location::NORTH;
new_direction <= map_location::NORTH_WEST;
new_direction = map_location::DIRECTION(new_direction +1)){
if (unit_it->get_location().get_direction(new_direction, 1) == hex) {
return new editor_action_unit_facing(start_hex_, new_direction, old_direction_);
}
}
}
//const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
//if (unit_it != units.end()) {
// for (map_location::DIRECTION new_direction = map_location::NORTH;
// new_direction <= map_location::NORTH_WEST;
// new_direction = map_location::DIRECTION(new_direction +1)){
// if (unit_it->get_location().get_direction(new_direction, 1) == hex) {
// return new editor_action_unit_facing(start_hex_, new_direction, old_direction_);
// }
// }
//}
return NULL;
}
editor_action* mouse_action_unit::up_right(editor_display& disp, int /*x*/, int /*y*/)
editor_action* mouse_action_unit::up_right(editor_display& /*disp*/, int /*x*/, int /*y*/)
{
if (!click_) return NULL;
click_ = false;
const unit_map& units = disp.map().get_units();
const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
if (unit_it != units.end()) {
return new editor_action_unit_delete(start_hex_);
}
//const unit_map& units = disp.map().get_units();
//const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
//if (unit_it != units.end()) {
// return new editor_action_unit_delete(start_hex_);
//}
return NULL;
}
@ -165,11 +165,11 @@ editor_action* mouse_action_unit::drag_end_right(editor_display& disp, int x, in
if(new_direction_ != old_direction_) {
const unit_map& units = disp.map().get_units();
const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
if (unit_it != units.end()) {
return new editor_action_unit_facing(start_hex_, new_direction_, old_direction_);
}
//const unit_map& units = disp.map().get_units();
//const unit_map::const_unit_iterator unit_it = units.find(start_hex_);
// if (unit_it != units.end()) {
// return new editor_action_unit_facing(start_hex_, new_direction_, old_direction_);
// }
}
return NULL;

View file

@ -104,14 +104,14 @@ void editor_controller::init_gui()
{
//TODO duplicate code, see the map_context_refresher
resources::game_map = &context_manager_->get_map();
resources::units = &context_manager_->get_map().get_units();
resources::tod_manager = &context_manager_->get_map().get_time_manager();
resources::teams = &context_manager_->get_map().get_teams();
resources::state_of_game = &context_manager_->get_map().get_game_state();
resources::units = &context_manager_->get_map_context().get_units();
resources::tod_manager = &context_manager_->get_map_context().get_time_manager();
resources::teams = &context_manager_->get_map_context().get_teams();
resources::state_of_game = &context_manager_->get_map_context().get_game_state();
gui_->change_map(&context_manager_->get_map());
gui_->change_units(&context_manager_->get_map().get_units());
gui_->change_teams(&context_manager_->get_map().get_teams());
gui_->change_units(&context_manager_->get_map_context().get_units());
gui_->change_teams(&context_manager_->get_map_context().get_teams());
gui_->set_grid(preferences::grid());
prefs_disp_manager_.reset(new preferences::display_manager(&gui()));
gui_->add_redraw_observer(boost::bind(&editor_controller::display_redraw_callback, this, _1));
@ -293,7 +293,7 @@ bool editor_controller::can_execute_command(hotkey::HOTKEY_COMMAND command, int
return true;
case HOTKEY_EDITOR_TOOL_UNIT:
case HOTKEY_EDITOR_TOOL_VILLAGE:
return !context_manager_->get_map().get_teams().empty();
// return !context_manager_->get_map().get_teams().empty();
case HOTKEY_EDITOR_CUT:
case HOTKEY_EDITOR_COPY:
case HOTKEY_EDITOR_EXPORT_SELECTION_COORDS:
@ -500,7 +500,7 @@ bool editor_controller::execute_command(hotkey::HOTKEY_COMMAND command, int inde
context_manager_->new_map_dialog();
return true;
case HOTKEY_EDITOR_SIDE_NEW:
context_manager_->get_map().new_side();
// context_manager_->get_map().new_side();
gui_->init_flags();
return true;
case HOTKEY_EDITOR_MAP_SAVE:

View file

@ -58,15 +58,15 @@ public:
void refresh() {
ec_.gui().change_map(&ec_.get_map());
resources::game_map = &ec_.get_map();
ec_.gui().change_units(&ec_.get_map().get_units());
resources::units = &ec_.get_map().get_units();
//ec_.gui().change_units(&ec_.get_map().get_units());
//resources::units = &ec_.get_map().get_units();
// TODO register the tod_manager with the gui?
resources::tod_manager = &ec_.get_map().get_time_manager();
ec_.gui().change_teams(&ec_.get_map().get_teams());
resources::teams = &ec_.get_map().get_teams();
//resources::tod_manager = &ec_.get_map().get_time_manager();
//ec_.gui().change_teams(&ec_.get_map().get_teams());
//resources::teams = &ec_.get_map().get_teams();
ec_.reload_map();
resources::state_of_game = &ec_.get_map().get_game_state();
//resources::state_of_game = &ec_.get_map().get_game_state();
ec_.gui().init_flags();
}
private:
@ -210,6 +210,7 @@ void context_manager::expand_sides_menu(std::vector<std::string>& items)
if (items[i] == "editor-side-switch") {
items.erase(items.begin() + i);
std::vector<std::string> contexts;
/*
for (size_t mci = 0; mci < get_map().get_teams().size(); ++mci) {
const team& t = get_map().get_teams()[mci];
@ -218,6 +219,7 @@ void context_manager::expand_sides_menu(std::vector<std::string>& items)
label << (t.name().empty() ? _("(New Side)") : t.name());
contexts.push_back(label.str());
}
*/
items.insert(items.begin() + i, contexts.begin(), contexts.end());
break;
}
@ -439,7 +441,7 @@ void context_manager::generate_map_dialog()
if (map_string.empty()) {
gui2::show_transient_message(gui_.video(), "", _("Map creation failed."));
} else {
editor_map new_map(game_config_, map_string, gui_);
editor_map new_map(game_config_, map_string);
editor_action_whole_map a(new_map);
perform_refresh(a);
}
@ -466,7 +468,7 @@ int context_manager::add_map_context(map_context* mc)
void context_manager::create_default_context()
{
if(saved_windows_.empty()) {
map_context* mc = new map_context(editor_map(game_config_, 44, 33, t_translation::GRASS_LAND, gui_));
map_context* mc = new map_context(editor_map(game_config_, 44, 33, t_translation::GRASS_LAND), gui_);
add_map_context(mc);
} else {
BOOST_FOREACH(const std::string& filename, saved_windows_) {
@ -643,12 +645,12 @@ void context_manager::revert_map()
void context_manager::new_map(int width, int height, t_translation::t_terrain fill, bool new_context)
{
editor_map m(game_config_, width, height, fill, gui_);
editor_map m(game_config_, width, height, fill);
if (new_context) {
int new_id = add_map_context(new map_context(m));
int new_id = add_map_context(new map_context(m, gui_));
switch_context(new_id);
} else {
replace_map_context(new map_context(m));
replace_map_context(new map_context(m, gui_));
}
}

View file

@ -14,16 +14,16 @@
*/
#define GETTEXT_DOMAIN "wesnoth-editor"
#include "editor/action/action_base.hpp"
#include "../action/action_base.hpp"
#include "editor_map.hpp"
#include "formula_string_utils.hpp"
#include "display.hpp"
#include "filesystem.hpp"
#include "gettext.hpp"
#include "map_exception.hpp"
#include "map_label.hpp"
#include "wml_exception.hpp"
#include "../../display.hpp"
#include "../../filesystem.hpp"
#include "../../gettext.hpp"
#include "../../map_exception.hpp"
#include "../../map_label.hpp"
#include "../../wml_exception.hpp"
#include <boost/foreach.hpp>
@ -41,64 +41,23 @@ editor_map_load_exception wrap_exc(const char* type, const std::string& e_msg, c
return editor_map_load_exception(filename, msg);
}
editor_map::editor_map(const config& terrain_cfg, const display& disp)
: gamemap(terrain_cfg, "")
editor_map::editor_map(const config& terrain_cfg)
: gamemap(terrain_cfg, gamemap::default_map_header)
, selection_()
, labels_(disp, NULL)
, units_()
, teams_()
, tod_manager_()
, state_()
{
}
editor_map::editor_map(const config& terrain_cfg, const config& level, const display& disp)
: gamemap(terrain_cfg, level)
, selection_()
, labels_(disp, NULL)
, units_()
, teams_()
, tod_manager_(level)
, state_()
{
labels_.read(level);
resources::teams = &teams_;
BOOST_FOREACH(const config& side, level.child_range("side"))
{
team t;
t.build(side, *this, 100);
//this is done because we don't count as observer is there is a human controller
//t.change_controller("null");
//TODO alternative? : gamestatus::teambuilder
teams_.push_back(t);
BOOST_FOREACH(const config &a_unit, side.child_range("unit")) {
map_location loc(a_unit, NULL);
units_.add(loc, unit(a_unit, true));
}
}
sanity_check();
}
editor_map::editor_map(const config& terrain_cfg, const std::string& data, const display& disp)
editor_map::editor_map(const config& terrain_cfg, const std::string& data)
: gamemap(terrain_cfg, data)
, selection_()
, labels_(disp, NULL)
, units_()
, teams_()
, tod_manager_()
, state_()
{
sanity_check();
}
editor_map editor_map::from_string(const config& terrain_cfg, const std::string& data, const display& disp)
editor_map editor_map::from_string(const config& terrain_cfg, const std::string& data)
{
try {
return editor_map(terrain_cfg, data, disp);
return editor_map(terrain_cfg, data);
} catch (incorrect_map_format_error& e) {
throw wrap_exc("format", e.message, "");
} catch (twml_exception& e) {
@ -108,35 +67,17 @@ editor_map editor_map::from_string(const config& terrain_cfg, const std::string&
}
}
editor_map::editor_map(
const config& terrain_cfg
, size_t width
, size_t height
, t_translation::t_terrain filler
, const display& disp)
: gamemap(
terrain_cfg
, t_translation::write_game_map(t_translation::t_map(
width + 2
, t_translation::t_list(height + 2, filler))))
editor_map::editor_map(const config& terrain_cfg, size_t width, size_t height, t_translation::t_terrain filler)
: gamemap(terrain_cfg, gamemap::default_map_header + t_translation::write_game_map(
t_translation::t_map(width + 2, t_translation::t_list(height + 2, filler))))
, selection_()
, labels_(disp, NULL)
, units_()
, teams_()
, tod_manager_()
, state_()
{
sanity_check();
}
editor_map::editor_map(const gamemap& map, const display& disp)
editor_map::editor_map(const gamemap& map)
: gamemap(map)
, selection_()
, labels_(disp, NULL)
, units_()
, teams_()
, tod_manager_()
, state_()
{
sanity_check();
}
@ -310,12 +251,12 @@ void editor_map::resize(int width, int height, int x_offset, int y_offset,
sanity_check();
}
editor_map editor_map::mask_to(const editor_map& target) const
gamemap editor_map::mask_to(const gamemap& target) const
{
if (target.w() != w() || target.h() != h()) {
throw editor_action_exception(_("The size of the target map is different from the current map"));
}
editor_map mask(target);
gamemap mask(target);
map_location iter;
for (iter.x = -border_size(); iter.x < w() + border_size(); ++iter.x) {
for (iter.y = -border_size(); iter.y < h() + border_size(); ++iter.y) {
@ -445,41 +386,6 @@ void editor_map::shrink_bottom(int count)
total_height_ -= count;
}
void editor_map::write(config& cfg) const {
config& map = cfg.add_child("map");
gamemap::write(map);
labels_.write(cfg);
std::stringstream buf;
for(std::vector<team>::const_iterator t = teams_.begin(); t != teams_.end(); ++t) {
int side_num = t - teams_.begin() + 1;
config& side = cfg.add_child("side");
t->write(side);
// TODO make this customizable via gui
side["no_leader"] = "no";
side["allow_player"] = "yes";
side.remove_attribute("color");
side.remove_attribute("recruit");
side.remove_attribute("recall_cost");
side.remove_attribute("gold");
side.remove_attribute("start_gold");
side.remove_attribute("hidden");
buf.str(std::string());
buf << side_num;
side["side"] = buf.str();
//current visible units
for(unit_map::const_iterator i = units_.begin(); i != units_.end(); ++i) {
if(i->side() == side_num) {
config& u = side.add_child("unit");
i->get_location().write(u); // TODO: Needed?
i->write(u);
}
}
}
}
} //end namespace editor

View file

@ -19,10 +19,6 @@
#include "../editor_common.hpp"
#include "../../map.hpp"
#include "../../map_label.hpp"
#include "unit_map.hpp"
#include "tod_manager.hpp"
#include "gamestatus.hpp"
#include <deque>
@ -76,62 +72,32 @@ class editor_map : public gamemap
{
public:
/** Adds a new side to the map */
void new_side() {
teams_.push_back(team());
}
/** Get the team from the current map context object */
std::vector<team>& get_teams() {
return teams_;
}
/** Get the unit map from the current map context object */
unit_map& get_units() {
return units_;
}
const unit_map& get_units() const {
return units_;
}
tod_manager& get_time_manager() {
return tod_manager_;
}
game_state& get_game_state() {
return state_;
}
/**
* Empty map constructor
*/
explicit editor_map(const config& terrain_cfg, const display& disp);
explicit editor_map(const config& terrain_cfg);
/**
* Create an editor map from a map data string
*/
editor_map(const config& terrain_cfg, const config& level, const display& disp);
editor_map(const config& terrain_cfg, const std::string& data, const display& disp);
editor_map(const config& terrain_cfg, const std::string& data);
/**
* Wrapper around editor_map(cfg, data) that catches possible exceptions
* and wraps them in a editor_map_load_exception
*/
static editor_map from_string(const config& terrain_cfg, const std::string& data, const display& disp);
static editor_map from_string(const config& terrain_cfg, const std::string& data);
/**
* Create an editor map with the given dimensions and filler terrain
*/
editor_map(const config& terrain_cfg, size_t width, size_t height, t_translation::t_terrain filler, const display& disp);
editor_map(const config& terrain_cfg, size_t width, size_t height, t_translation::t_terrain filler);
/**
* Create an editor_map by upgrading an existing gamemap. The map data is
* copied. Marked "explicit" to avoid potentially harmful automatic conversions.
* copied. Marked "explicit" to avoid potentially harmful autmatic conversions.
*/
explicit editor_map(const gamemap& map, const display& disp);
explicit editor_map(const gamemap& map);
/**
* editor_map destructor
@ -150,14 +116,6 @@ public:
*/
std::set<map_location> get_contiguous_terrain_tiles(const map_location& start) const;
/**
* @return the map labels of the map
*/
map_labels& get_map_labels() { return labels_; };
const map_labels& get_map_labels() const { return labels_; };
/**
* Set labels for staring positions in the given display object.
* @return the locations where the labels were added
@ -217,7 +175,7 @@ public:
* A sort-of diff operation returning a mask that, when applied to the current editor_map,
* will transform it into the target map.
*/
editor_map mask_to(const editor_map& target) const;
gamemap mask_to(const gamemap& target) const;
/**
* A precondition to several map operations
@ -225,8 +183,6 @@ public:
*/
bool same_size_as(const gamemap& other) const;
void write(config&) const;
protected:
t_translation::t_list clone_column(int x, t_translation::t_terrain filler);
@ -244,25 +200,6 @@ protected:
* The selected hexes
*/
std::set<map_location> selection_;
private:
/**
* The labels of this map.
*/
map_labels labels_;
/**
* TODO
*/
unit_map units_;
std::vector<team> teams_;
tod_manager tod_manager_;
game_state state_;
};

View file

@ -37,7 +37,7 @@ namespace editor {
const size_t map_context::max_action_stack_size_ = 100;
map_context::map_context(const editor_map& map)
map_context::map_context(const editor_map& map, const display& disp)
: filename_()
, map_data_key_()
, embedded_(false)
@ -51,6 +51,7 @@ map_context::map_context(const editor_map& map)
, needs_labels_reset_(false)
, changed_locations_()
, everything_changed_(false)
, labels_(disp, NULL)
{
}
@ -58,7 +59,7 @@ map_context::map_context(const config& game_config, const std::string& filename,
: filename_(filename)
, map_data_key_()
, embedded_(false)
, map_(game_config, disp)
, map_(game_config)
, undo_stack_()
, redo_stack_()
, actions_since_save_(0)
@ -68,29 +69,33 @@ map_context::map_context(const config& game_config, const std::string& filename,
, needs_labels_reset_(false)
, changed_locations_()
, everything_changed_(false)
, labels_(disp, NULL)
{
/*
* Overview of situations possibly found in the file:
*
* 0. Not a scenario or map file.
* 0.1 File not found
* 0.2 Map file empty
* 0.3
* 1. It's a file containing only pure map data.
* * embedded_ = false
* *
* 2. A scenario embedding the map
* The data/scenario-test.cfg for example.
* The map is written back to the file.
*
* 3. The file contains a [map]
*
*/
log_scope2(log_editor, "Loading map " + filename);
// Case 0.1
if (!file_exists(filename) || is_directory(filename)) {
throw editor_map_load_exception(filename, _("File not found"));
}
std::string map_string = read_file(filename);
if (map_string.empty()) {
std::string message = _("Empty file");
throw editor_map_load_exception(filename, message);
}
try {
config file;
::read(file, map_string);
map_ = editor_map(game_config, file, disp);
return;
} catch (config::error&) {
}
boost::regex re("data\\s*=\\s*\"(.+?)\"");
boost::regex re("map_data\\s*=\\s*\"(.+?)\"");
boost::smatch m;
if (boost::regex_search(map_string, m, re, boost::regex_constants::match_not_dot_null)) {
boost::regex re2("\\{(.+?)\\}");
@ -115,12 +120,65 @@ map_context::map_context(const config& game_config, const std::string& filename,
map_string = m[1];
}
}
// Case 0.2
if (map_string.empty()) {
std::string message = _("Empty map file");
throw editor_map_load_exception(filename, message);
}
map_ = editor_map::from_string(game_config, map_string); //throws on error
map_ = editor_map::from_string(game_config, map_string, disp); //throws on error
// log_scope2(log_editor, "Loading map " + filename);
// if (!file_exists(filename) || is_directory(filename)) {
// throw editor_map_load_exception(filename, _("File not found"));
// }
//
// std::string map_string = read_file(filename);
//
// if (map_string.empty()) {
// std::string message = _("Empty file");
// throw editor_map_load_exception(filename, message);
// }
//
// try {
// config file;
// ::read(file, map_string);
//
// map_ = editor_map(game_config, file, disp);
// return;
// } catch (config::error&) {
// }
//
// boost::regex re("data\\s*=\\s*\"(.+?)\"");
// boost::smatch m;
// if (boost::regex_search(map_string, m, re, boost::regex_constants::match_not_dot_null)) {
// boost::regex re2("\\{(.+?)\\}");
// boost::smatch m2;
// std::string m1 = m[1];
// if (boost::regex_search(m1, m2, re2)) {
// map_data_key_ = m1;
// LOG_ED << "Map looks like a scenario, trying {" << m2[1] << "}\n";
// std::string new_filename = get_wml_location(m2[1], directory_name(m2[1]));
// if (new_filename.empty()) {
// std::string message = _("The map file looks like a scenario, "
// "but the map_data value does not point to an existing file")
// + std::string("\n") + m2[1];
// throw editor_map_load_exception(filename, message);
// }
// LOG_ED << "New filename is: " << new_filename << "\n";
// filename_ = new_filename;
// map_string = read_file(filename_);
// } else {
// LOG_ED << "Loading embedded map file\n";
// embedded_ = true;
// map_string = m[1];
// }
// }
// if (map_string.empty()) {
// std::string message = _("Empty map file");
// throw editor_map_load_exception(filename, message);
// }
//
// map_ = editor_map::from_string(game_config, map_string, disp); //throws on error
}
map_context::~map_context()
@ -222,9 +280,42 @@ void map_context::reset_starting_position_labels(display& disp)
bool map_context::save()
{
std::string data = map_.write();
try {
if (!is_embedded()) {
write_file(get_filename(), data);
} else {
std::string map_string = read_file(get_filename());
boost::regex re("(.*map_data\\s*=\\s*\")(.+?)(\".*)");
boost::smatch m;
if (boost::regex_search(map_string, m, re, boost::regex_constants::match_not_dot_null)) {
std::stringstream ss;
ss << m[1];
ss << data;
ss << m[3];
write_file(get_filename(), ss.str());
} else {
throw editor_map_save_exception(_("Could not save into scenario"));
}
}
clear_modified();
} catch (io_exception& e) {
utils::string_map symbols;
symbols["msg"] = e.what();
const std::string msg = vgettext("Could not save the map: $msg", symbols);
throw editor_map_save_exception(msg);
}
return true;
//TODO the return value of this method does not need to be bool.
//We either return true or there is an exception thrown.
/*
config data;
map_.write(data);
@ -273,6 +364,7 @@ bool map_context::save()
throw editor_map_save_exception(msg);
}
return true;
*/
}
void map_context::set_map(const editor_map& map)

View file

@ -18,6 +18,9 @@
#include "editor_map.hpp"
#include "map_label.hpp"
#include "unit_map.hpp"
#include "tod_manager.hpp"
#include "gamestatus.hpp"
#include <boost/utility.hpp>
@ -38,7 +41,7 @@ public:
* empty, indicating a new map.
* Marked "explicit" to avoid automatic conversions.
*/
explicit map_context(const editor_map& map);
explicit map_context(const editor_map& map, const display& disp);
/**
* Create map_context from a map file. If the map cannot be loaded, an
@ -65,6 +68,33 @@ public:
*/
const editor_map& get_map() const { return map_; }
/** Adds a new side to the map */
void new_side() {
teams_.push_back(team());
}
/** Get the team from the current map context object */
std::vector<team>& get_teams() {
return teams_;
}
/** Get the unit map from the current map context object */
unit_map& get_units() {
return units_;
}
const unit_map& get_units() const {
return units_;
}
tod_manager& get_time_manager() {
return tod_manager_;
}
game_state& get_game_state() {
return state_;
}
/**
* Draw a terrain on a single location on the map.
* Sets the refresh flags accordingly.
@ -300,6 +330,15 @@ protected:
std::set<map_location> changed_locations_;
bool everything_changed_;
private:
map_labels labels_;
unit_map units_;
std::vector<team> teams_;
tod_manager tod_manager_;
game_state state_;
};

View file

@ -37,6 +37,7 @@ static lg::log_domain log_config("config");
#define LOG_G LOG_STREAM(info, lg::general)
#define DBG_G LOG_STREAM(debug, lg::general)
const std::string gamemap::default_map_header = "usage=map\nborder_size=1\n\n";
const gamemap::tborder gamemap::default_border = gamemap::SINGLE_TILE_BORDER;
const t_translation::t_list& gamemap::underlying_mvt_terrain(t_translation::t_terrain terrain) const
@ -338,6 +339,28 @@ int gamemap::read_header(const std::string& data)
}
std::string gamemap::write() const
{
// Convert the starting positions to a map
std::map<int, t_translation::coordinate> starting_positions;
for (int i = 0; i < MAX_PLAYERS + 1; ++i)
{
if (!on_board(startingPositions_[i])) continue;
t_translation::coordinate position(
startingPositions_[i].x + border_size_
, startingPositions_[i].y + border_size_);
starting_positions[i] = position;
}
// Let the low level convertor do the conversion
std::ostringstream s;
s << "border_size=" << border_size_ << "\nusage="
<< (usage_ == IS_MAP ? "map" : "mask") << "\n\n"
<< t_translation::write_game_map(tiles_, starting_positions);
return s.str();
}
/*
void gamemap::write(config& cfg) const
{
// Convert the starting positions to a map
@ -358,6 +381,7 @@ void gamemap::write(config& cfg) const
s << t_translation::write_game_map(tiles_, starting_positions);
cfg["data"] = s.str();
}
*/
void gamemap::overlay(const gamemap& m, const config& rules_cfg, int xpos, int ypos, bool border)
{

View file

@ -119,7 +119,7 @@ public:
void read(const std::string& data, const bool allow_invalid = true, const int border_size = 1, const std::string usage = "map");
virtual void write(config&) const;
std::string write() const;
/** Overlays another map onto this one at the given position. */
void overlay(const gamemap& m, const config& rules, int x=0, int y=0, bool border=false);
@ -225,6 +225,12 @@ public:
/** Returns the usage of the map. */
tusage get_usage() const { return usage_; }
/**
* The default map header, needed for maps created with
* terrain_translation::write_game_map().
*/
static const std::string default_map_header;
/** The default border style for a map. */
static const tborder default_border;

View file

@ -612,6 +612,36 @@ void menu_handler::save_map()
}
} while (res == 0 && overwrite != 0);
// Try to save the map, if it fails we reset the filename.
if (res == 0) {
try {
write_file(input_name, map_.write());
gui2::show_transient_message(gui_->video(), "", _("Map saved."));
} catch (io_exception& e) {
utils::string_map symbols;
symbols["msg"] = e.what();
const std::string msg = vgettext("Could not save the map: $msg",symbols);
gui2::show_transient_message(gui_->video(), "", msg);
}
}
/*
std::string input_name = get_dir(get_dir(get_user_data_dir() + "/editor") + "/maps/");
int res = 0;
int overwrite = 1;
do {
res = dialogs::show_file_chooser_dialog_save(*gui_, input_name, _("Save the Map As"));
if (res == 0) {
if (file_exists(input_name)) {
const int res = gui2::show_message((*gui_).video(), "", _("The map already exists. Do you want to overwrite it?"), gui2::tmessage::yes_no_buttons);
overwrite = res == gui2::twindow::CANCEL ? 1 : 0;
}
else
overwrite = 0;
}
} while (res == 0 && overwrite != 0);
// Try to save the map, if it fails we reset the filename.
if (res == 0) {
try {
@ -634,6 +664,7 @@ void menu_handler::save_map()
gui2::show_transient_message(gui_->video(), "", msg);
}
}
*/
}
void menu_handler::preferences()

View file

@ -718,10 +718,8 @@ config play_controller::to_config() const
}
//write out the current state of the map
config& map = cfg.add_child("map");
map_.write(map);
//write out the current state of the map
cfg["map_data"] = map_.write();
cfg.merge_with(pathfind_manager_->to_config());
config display;
@ -729,6 +727,7 @@ config play_controller::to_config() const
cfg.add_child("display", display);
return cfg;
}
void play_controller::finish_side_turn(){