From c8336c0fb110f8b23707c755e7ebaabdde62c8ea Mon Sep 17 00:00:00 2001
From: Stijn Buys <ingar@osirion.org>
Date: Fri, 8 Nov 2013 18:25:59 +0000
Subject: Added missing core::reputation files.

---
 src/core/reputation.cc | 103 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/core/reputation.h  | 104 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 207 insertions(+)
 create mode 100644 src/core/reputation.cc
 create mode 100644 src/core/reputation.h

(limited to 'src')

diff --git a/src/core/reputation.cc b/src/core/reputation.cc
new file mode 100644
index 0000000..013fd9e
--- /dev/null
+++ b/src/core/reputation.cc
@@ -0,0 +1,103 @@
+/*
+   core/reputation.cc
+   This file is part of the Osirion project and is distributed under
+   the terms of the GNU General Public License version 2
+*/
+
+#include "core/reputation.h"
+
+namespace core {
+	
+Reputation::Reputation()
+{
+}
+
+Reputation::~Reputation()
+{
+	clear();
+}
+
+void Reputation::clear()
+{
+	for (FactionReps::iterator it = reputation_factionreps.begin(); it != reputation_factionreps.end(); ++it) {
+		delete (*it);
+		(*it) = 0;
+	}
+	reputation_factionreps.clear();
+}
+
+void Reputation::assign(const Reputation &other)
+{
+	clear();
+	
+	for (FactionReps::const_iterator it = other.reputation_factionreps.begin(); it != other.reputation_factionreps.end(); ++it) {
+		
+		FactionRep *factionrep = new FactionRep((*it)->faction(), (*it)->reputation());
+		reputation_factionreps.push_back(factionrep);
+		
+	}
+
+}
+
+const Info *Reputation::find(const Info *faction) const
+{
+	for (FactionReps::const_iterator it = reputation_factionreps.begin(); it != reputation_factionreps.end(); ++it) {
+		if ((*it)->faction() == faction) {
+			return (*it)->faction();
+		}
+	}
+	
+	return 0;
+}
+
+const float Reputation::reputation(const Info *faction) const
+{
+	if (!faction) {
+		return 0.0f;
+	}
+	
+	for (FactionReps::const_iterator it = reputation_factionreps.begin(); it != reputation_factionreps.end(); ++it) {
+		if ((*it)->faction() == faction) {
+			return (*it)->reputation();
+		}
+	}
+	
+	return 0.0f;
+}
+
+void Reputation::set_faction(const std::string &label, const Info *faction)
+{
+	for (FactionReps::const_iterator it = reputation_factionreps.begin(); it != reputation_factionreps.end(); ++it) {
+		if ((*it)->label().compare(label) == 0) {
+			(*it)->set_faction(faction);
+		}
+	}
+}
+
+void Reputation::set_reputation(const std::string &label, const float reputation)
+{
+	for (FactionReps::const_iterator it = reputation_factionreps.begin(); it != reputation_factionreps.end(); ++it) {
+		if (((*it)->faction() && (label.compare((*it)->faction()->label()) == 0)) || ((*it)->label().compare(label) == 0)) {
+			(*it)->set_reputation(reputation);
+			return;
+		}
+	}
+	
+	FactionRep *factionrep = new FactionRep(label, reputation);
+	reputation_factionreps.push_back(factionrep);
+}
+
+void Reputation::set_reputation(const Info *faction, const float reputation)
+{
+	for (FactionReps::const_iterator it = reputation_factionreps.begin(); it != reputation_factionreps.end(); ++it) {
+		if ((!(*it)->faction() && ((*it)->label().compare(faction->label()) == 0)) || ((*it)->faction() == faction)) {
+			(*it)->set_reputation(reputation);
+			return;
+		}
+	}
+	
+	FactionRep *factionrep = new FactionRep(faction, reputation);
+	reputation_factionreps.push_back(factionrep);
+}
+
+} // namespace core
\ No newline at end of file
diff --git a/src/core/reputation.h b/src/core/reputation.h
new file mode 100644
index 0000000..87d3ea9
--- /dev/null
+++ b/src/core/reputation.h
@@ -0,0 +1,104 @@
+/*
+   core/reputation.h
+   This file is part of the Osirion project and is distributed under
+   the terms of the GNU General Public License version 2
+*/
+
+#ifndef __INCLUDED_CORE_REPUTATION_H__
+#define __INCLUDED_CORE_REPUTATION_H__
+
+#include <list>
+
+#include "core/info.h"
+
+namespace core {
+	
+class Reputation {
+
+	class FactionRep {
+	public:
+		inline FactionRep(const std::string &label, const float reputation = 0)
+		{
+			factionrep_label.assign(label);
+			factionrep_faction = 0;
+			factionrep_reputation = reputation;
+		}
+		
+		inline FactionRep(const Info *faction, const float reputation = 0)
+		{
+			factionrep_label.assign(faction->label());
+			factionrep_faction = faction;
+			factionrep_reputation = reputation;
+		}
+				
+		inline ~FactionRep()
+		{
+		}
+		
+		inline const std::string & label() const
+		{
+			return factionrep_label;
+		}
+		
+		inline const Info *faction() const
+		{
+			return factionrep_faction;
+		}
+		
+		/**
+		 * @brief reputation with the specified faction.
+		 * Return value is between -100 (hostile) and 100 (friendly), 0 means neutral.
+		 * */
+		inline const float reputation() const
+		{
+			return factionrep_reputation;
+		}
+		
+		inline void set_faction(const Info *faction)
+		{
+			factionrep_faction = faction;
+		}
+		
+		inline void set_reputation(const float reputation)
+		{
+			factionrep_reputation = reputation;
+			math::clamp(factionrep_reputation, -100.0f, 100.0f);
+		}
+				
+	private:
+		std::string	factionrep_label;
+		const Info	*factionrep_faction;
+		float		factionrep_reputation;
+	};
+	
+public:
+	typedef std::list<FactionRep *> FactionReps;
+	
+	Reputation();
+	~Reputation();
+	
+	const float reputation(const Info *faction) const;
+	
+	const Info *find(const Info *faction) const;
+		
+	void set_faction(const std::string &label, const Info *action);
+		
+	void set_reputation(const std::string &label, const float reputation);
+	
+	void set_reputation(const Info *faction, const float reputation);
+	
+	void clear();
+	
+	void assign(const Reputation &other);
+	
+	inline FactionReps & factionreps()
+	{
+		return reputation_factionreps;
+	}
+	
+private:
+	FactionReps		reputation_factionreps;
+};
+
+} // namespace core
+#endif // __INCLUDED_CORE_REPUTATION_H__
-- 
cgit v1.2.3