summaryrefslogtreecommitdiff
path: root/src/units.cpp
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2009-03-26 00:27:05 +0100
committerBjørn Lindeijer <bjorn@lindeijer.nl>2009-03-26 00:32:19 +0100
commitdfcc6397848d4597b386b688f689352de6c19ae2 (patch)
treea6e8c9afd6a49ea96e4498080a96d3e06ecaebd7 /src/units.cpp
parentcd20fb3432498b8871401bdd65a143197e2a6538 (diff)
downloadmana-dfcc6397848d4597b386b688f689352de6c19ae2.tar.gz
mana-dfcc6397848d4597b386b688f689352de6c19ae2.tar.bz2
mana-dfcc6397848d4597b386b688f689352de6c19ae2.tar.xz
mana-dfcc6397848d4597b386b688f689352de6c19ae2.zip
Remove redundancy, fix variable names and other code cleanups
Diffstat (limited to 'src/units.cpp')
-rw-r--r--src/units.cpp32
1 files changed, 17 insertions, 15 deletions
diff --git a/src/units.cpp b/src/units.cpp
index 7e8d8e6a..acdbd564 100644
--- a/src/units.cpp
+++ b/src/units.cpp
@@ -21,16 +21,16 @@
#include "units.h"
-#include <cmath>
-#include <climits>
-#include <vector>
-
#include "log.h"
#include "utils/strprintf.h"
#include "utils/stringutils.h"
#include "utils/xml.h"
+#include <cmath>
+#include <climits>
+#include <vector>
+
struct UnitLevel {
std::string symbol;
int count;
@@ -53,11 +53,6 @@ struct UnitDescription units[UNIT_END];
void Units::loadUnits()
{
- int level;
- std::string type;
- XML::Document doc("units.xml");
- xmlNodePtr root = doc.rootNode();
-
{ // Setup default weight
struct UnitDescription ud;
@@ -97,6 +92,9 @@ void Units::loadUnits()
units[UNIT_CURRENCY] = ud;
}
+ XML::Document doc("units.xml");
+ xmlNodePtr root = doc.rootNode();
+
if (!root || !xmlStrEqual(root->name, BAD_CAST "units"))
{
logger->log("Error loading unit definition file: units.xml");
@@ -108,8 +106,8 @@ void Units::loadUnits()
if (xmlStrEqual(node->name, BAD_CAST "unit"))
{
struct UnitDescription ud;
- level = 1;
- type = XML::getProperty(node, "type", "");
+ int level = 1;
+ const std::string type = XML::getProperty(node, "type", "");
ud.conversion = XML::getProperty(node, "conversion", 1.0);
ud.mix = XML::getProperty(node, "mix", "no") == "yes";
@@ -151,9 +149,12 @@ void Units::loadUnits()
ud.levels.push_back(ll);
- if (type == "weight") units[UNIT_WEIGHT] = ud;
- else if (type =="currency") units[UNIT_CURRENCY] = ud;
- else logger->log("Error unknown unit type: %s", type.c_str());
+ if (type == "weight")
+ units[UNIT_WEIGHT] = ud;
+ else if (type == "currency")
+ units[UNIT_CURRENCY] = ud;
+ else
+ logger->log("Error unknown unit type: %s", type.c_str());
}
}
}
@@ -162,13 +163,14 @@ std::string formatUnit(int value, int type)
{
struct UnitDescription ud = units[type];
struct UnitLevel ul;
- double amount = ud.conversion * value;
// Shortcut for 0; do the same for values less than 0 (for now)
if (value <= 0) {
ul = ud.levels[0];
return strprintf("0%s", ul.symbol.c_str());
} else {
+ double amount = ud.conversion * value;
+
// If only the first level is needed, act like mix if false
if (ud.mix && ud.levels.size() > 0 && ud.levels[1].count < amount)
{