Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: ea276a7e58c0806f8d5dc47155366ee5af9290ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
   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"
#include "core/application.h"

namespace core {
	
Reputation::Reputation()
{
	reputation_dirty = false;
}

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();
	
	reputation_dirty = false;
}

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);
		
	}

}

void Reputation::set_dirty(const bool dirty)
{
	reputation_dirty = dirty;
	if (reputation_dirty) {
		reputation_timestamp = game()->timestamp();
	}
}

void Reputation::set_timestamp(const unsigned long timestamp)
{
	reputation_timestamp = timestamp;
}

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)) {
			if ((*it)->reputation() != reputation) {
				(*it)->set_reputation(reputation);
				reputation_dirty = true;
			}
			return;
		}
	}
	
	FactionRep *factionrep = new FactionRep(label, reputation);
	reputation_factionreps.push_back(factionrep);
	reputation_dirty = true;
}

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)) {
			if ((*it)->reputation() != reputation) {
				(*it)->set_reputation(reputation);
				reputation_dirty = true;
			}
			return;
		}
	}
	
	FactionRep *factionrep = new FactionRep(faction, reputation);
	reputation_factionreps.push_back(factionrep);
	reputation_dirty = true;
}

void Reputation::receive_server_update(std::istream &is)
{
	unsigned long id;
	float reputation;

	while ((is >> id) && ( is >> reputation)) {
		// this is client side and has to create info records as required
		set_reputation(game()->request_info(id), reputation);
	}
}
	
void Reputation::serialize_server_update(std::ostream & os) const
{
	for (FactionReps::const_iterator it = reputation_factionreps.begin(); it != reputation_factionreps.end(); ++it) {
		os << (*it)->faction()->id() << " " << roundf((*it)->reputation()) << " ";
	}
}

} // namespace core