Show rules.cpp syntax highlighted
/***************************************************************************
rules.cpp
-------------------
begin : Sun Jul 18 2004
copyright : (C) 2004 by Brendon Higgins
email : freepop-devel@lists.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include <rules.h>
Rules::Missing::Missing(const std::string& key):
runtime_error("\"" + key + "\" rule is missing.") {
}
Rules::Missing::~Missing() throw() {
}
Rules::Corrupt::Corrupt(const std::string& key):
runtime_error("Bad format of \"" + key + "\" rule.") {
}
Rules::Corrupt::~Corrupt() throw() {
}
Rules::Bad::Bad(const std::string& key, const std::string& range):
runtime_error("Rule \"" + key + "\" is outside the allowed range (" + range
+ ").") {
}
Rules::Bad::~Bad() throw() {
}
Rules::Rules(const VarMap& vars) {
parse(&width, vars, "width");
check(0 < width && width <= 512, "0 < width <= 512", "width");
parse(&height, vars, "height");
check(0 < height && height <= 512, "0 < height <= 512", "height");
parse(&generator, vars, "generator");
check(/*0 <= generator &&*/ generator < 2, "0 or 1", "generator");
parse(&theater, vars, "theater");
check(theater == "blank" || theater == "temperate" || theater == "snow",
"blank, temperate, snow", "theater");
parse(&firecolumn_lifetime, vars, "firecolumn_lifetime");
// check(0 <= firecolumn_lifetime, "0 <= firecolumn_lifetime",
// "firecolumn_lifetime");
parse(&tree_burnrate, vars, "tree_burnrate");
// check(0 <= tree_burnrate, "0 <= tree_burnrate",
// "tree_burnrate");
parse(&tree_flamerange, vars, "tree_flamerange");
check(0 <= tree_flamerange, "0 <= tree_flamerange",
"tree_flamerange");
parse(&tree_flameduration, vars, "tree_flameduration");
// check(0 <= tree_flameduration, "0 <= tree_flameduration",
// "tree_flameduration");
parse(&tree_decayduration, vars, "tree_decayduration");
// check(0 <= tree_decayduration, "0 <= tree_decayduration",
// "tree_decayduration");
parse(&peep_burnrate, vars, "peep_burnrate");
// check(0 <= peep_burnrate, "0 <= peep_burnrate",
// "peep_burnrate");
parse(&peep_flamerange, vars, "peep_flamerange");
check(0 <= peep_flamerange, "0 <= peep_flamerange",
"peep_flamerange");
parse(&peep_flameduration, vars, "peep_flameduration");
// check(0 <= peep_flameduration, "0 <= peep_flameduration",
// "peep_flameduration");
parse(&peep_exhaustrate, vars, "peep_exhaustrate");
// check(0 <= peep_exhaustrate, "0 <= peep_exhaustrate",
// "peep_exhaustrate");
parse(&peep_walkspeed, vars, "peep_walkspeed");
// check(0 <= peep_walkspeed, "0 <= peep_walkspeed",
// "peep_walkspeed");
parse(&town_burnrate, vars, "town_burnrate");
// check(0 <= town_burnrate, "0 <= town_burnrate",
// "town_burnrate");
parse(&town_flamerange, vars, "town_flamerange");
check(0 <= town_flamerange, "0 <= town_flamerange",
"town_flamerange");
parse(&town_growrate, vars, "town_growrate");
// check(0 <= town_growrate, "0 <= town_growrate",
// "town_growrate");
parse(&town_maxstrength, vars, "town_maxstrength");
check(0.0f < town_maxstrength, "0 < town_maxstrength", "town_maxstrength");
parse(&town_spawnstrength, vars, "town_spawnstrength");
check(0.0f < town_spawnstrength, "0 < town_spawnstrength",
"town_spawnstrength");
parse(&swamp_range, vars, "swamp_range");
check(0 <= swamp_range, "0 <= swamp_range", "swamp_range");
}
Rules::Rules(CL_InputSource& is) {
width = is.read_uint32();
height = is.read_uint32();
generator = is.read_uint32();
theater = is.read_string();
firecolumn_lifetime = is.read_uint32();
tree_burnrate = is.read_uint32();
tree_flamerange = is.read_float32();
tree_flameduration = is.read_uint32();
tree_decayduration = is.read_uint32();
peep_burnrate = is.read_uint32();
peep_flamerange = is.read_float32();
peep_flameduration = is.read_uint32();
peep_exhaustrate = is.read_uint32();
peep_walkspeed = is.read_uint32();
town_burnrate = is.read_uint32();
town_flamerange = is.read_float32();
town_growrate = is.read_uint32();
town_maxstrength = is.read_float32();
town_spawnstrength = is.read_float32();
swamp_range = is.read_float32();
}
Rules::~Rules() {
}
void Rules::inject(CL_OutputSource& os) const {
os.write_uint32(width);
os.write_uint32(height);
os.write_uint32(generator);
os.write_string(theater);
os.write_uint32(firecolumn_lifetime);
os.write_uint32(tree_burnrate);
os.write_float32(tree_flamerange);
os.write_uint32(tree_flameduration);
os.write_uint32(tree_decayduration);
os.write_uint32(peep_burnrate);
os.write_float32(peep_flamerange);
os.write_uint32(peep_flameduration);
os.write_uint32(peep_exhaustrate);
os.write_uint32(peep_walkspeed);
os.write_uint32(town_burnrate);
os.write_float32(town_flamerange);
os.write_uint32(town_growrate);
os.write_float32(town_maxstrength);
os.write_float32(town_spawnstrength);
os.write_float32(swamp_range);
}
std::string Rules::help() {
return "width\tMap width.\n"
"height\tMap height.\n"
"generator\tMap generator type:\n"
"\t0: Flat sea. No generator.\n"
"\t1: Simple generator.\n"
"theater\tThe map theater type:\n"
"\tblank: Non-descript generic.\n"
"\ttemperate: Temperate and grassy.\n"
"\tsnow: Snowy. (WIP)\n"
"firecolumn_lifetime\tTime a firecolumn lives.\n"
"tree_burnrate\tThe rate a tree burns, ms per strength point.\n"
"tree_flamerange\tThe radius^2 at which a flame can spread to"
" trees.\n"
"tree_flameduration\tThe duration a tree must be within range to"
" catch fire.\n"
"tree_decayduration\tThe duration before a dead tree decays.\n"
"peep_burnrate\tThe rate a peep burns, ms per strength point.\n"
"peep_flamerange\tThe range at which a flame can spread to a"
" peep.\n"
"peep_flameduration\tThe duration a peep must be within range to"
" catch fire.\n"
"peep_exhaustrate\tThe rate a peep dies of exposure while"
" walking.\n"
"peep_walkspeed\tThe speed a peep walks (ms per tile unit).\n"
"town_burnrate\tThe rate a town burns.\n"
"town_flamerange\tThe range a flame can spread to a town.\n"
"town_growrate\tThe rate a town grows. Higher = slower.\n"
"town_maxstrength\tThe strength at which a town will spawn a"
" peep.\n"
"town_spawnstrength\tThe strength of a peep when spawned.\n"
"swamp_range\tThe radius^2 that a swamp can trap a peep.";
}
std::string Rules::lookup(const VarMap& map, const std::string& key) {
std::map<std::string, std::string>::const_iterator i = map.find(key);
if (i == map.end()) {
throw Missing(key);
}
return i->second;
}
void Rules::check(bool range, const std::string& r, const std::string& k) {
if (!range) {
throw Bad(k, r);
}
}
See more files for this project here