wesnoth/src/generators/map_create.cpp
gfgtdf 484d926c30 fixup use of operator !=(attribute_value, const char*)
since we don't use t_string's == operator anymore in this case, c["a"]
!= "" will evaluate to true in case of "a" beeing not existent. To get
the desired behaviour we need to use the .empty() method.

I also add an assert in map_generation for a possible segfault that i
noticed during fixing this.
2014-06-27 19:16:35 +02:00

75 lines
2.5 KiB
C++

/*
Copyright (C) 2003 - 2014 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "global.hpp"
#include "map_create.hpp"
#include "generators/cavegen.hpp"
#include "generators/yamg/ya_mapgen.hpp"
#include "log.hpp"
#include "mapgen_dialog.hpp"
#include "scoped_resource.hpp"
#include "serialization/string_utils.hpp"
#include <cassert>
static lg::log_domain log_config("config");
#define ERR_CF LOG_STREAM(err, log_config)
map_generator* create_map_generator(const std::string& name, const config &cfg)
{
if(name == "default" || name == "") {
return new default_map_generator(cfg);
} else if(name == "cave") {
return new cave_map_generator(cfg);
} else if(name == "yamg") {
return new ya_mapgen(cfg);
} else {
return NULL;
}
}
//function to generate a random map, from a string which describes
//the generator to use and its arguments
std::string random_generate_map(const std::string& parms, const config &cfg)
{
//the first token is the name of the generator, tokens after
//that are arguments to the generator
std::vector<std::string> parameters = utils::split(parms, ' ');
assert(!parameters.empty()); //we use parameters.front() in the next line.
util::scoped_ptr<map_generator> generator(create_map_generator(parameters.front(),cfg));
if(generator == NULL) {
ERR_CF << "could not find map generator '" << parameters.front() << "'" << std::endl;
return std::string();
}
parameters.erase(parameters.begin());
return generator.get()->create_map(parameters);
}
config random_generate_scenario(const std::string& parms, const config &cfg)
{
//the first token is the name of the generator, tokens after
//that are arguments to the generator
std::vector<std::string> parameters = utils::split(parms, ' ');
util::scoped_ptr<map_generator> generator(create_map_generator(parameters.front(),cfg));
if(generator == NULL) {
ERR_CF << "could not find map generator '" << parameters.front() << "'" << std::endl;
return config();
}
parameters.erase(parameters.begin());
return generator->create_scenario(parameters);
}