initial commit, lordsawar source, slightly modified
[lordsawar] / src / Triumphs.cpp
1 //  Copyright (C) 2008 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
16 //  02110-1301, USA.
17
18 #include <iostream>
19 #include <sstream>
20 #include <string.h>
21
22 #include "Triumphs.h"
23 #include "playerlist.h"
24
25 #include "xmlhelper.h"
26
27 std::string Triumphs::d_tag = "triumphs";
28
29 using namespace std;
30
31 //#define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<flush;}
32 #define debug(x)
33
34
35 Triumphs::Triumphs()
36 {
37   memset(d_triumph, 0, sizeof(d_triumph));
38 }
39
40 Triumphs::Triumphs(XML_Helper* helper)
41 {
42   for (unsigned int i = 0; i < 5; i++)
43     {
44       std::string tally;
45       std::stringstream stally;
46       guint32 val;
47       switch (TriumphType(i))
48         {
49         case TALLY_HERO:
50           helper->getData(tally, "hero");
51           break;
52         case TALLY_NORMAL:
53           helper->getData(tally, "normal");
54           break;
55         case TALLY_SPECIAL:
56           helper->getData(tally, "special");
57           break;
58         case TALLY_SHIP:
59           helper->getData(tally, "ship");
60           break;
61         case TALLY_FLAG:
62           helper->getData(tally, "flag");
63           break;
64         }
65       stally.str(tally);
66       for (unsigned int j = 0; j < MAX_PLAYERS; j++)
67         {
68           stally >> val;
69           d_triumph[j][i] = val;
70         }
71     }
72 }
73
74 Triumphs::Triumphs(const Triumphs& triumphs)
75 {
76   memcpy (d_triumph, triumphs.d_triumph, sizeof (d_triumph));
77 }
78
79 Triumphs::~Triumphs()
80 {
81 }
82
83 bool Triumphs::save(XML_Helper* helper) const
84 {
85   bool retval = true;
86
87   retval &= helper->openTag(Triumphs::d_tag);
88   for (unsigned int i = 0; i < 5; i++)
89     {
90       std::stringstream tally;
91       for (unsigned int j = 0; j < MAX_PLAYERS; j++)
92         tally << d_triumph[j][i] << " ";
93       switch (TriumphType(i))
94         {
95         case TALLY_HERO:
96           retval &= helper->saveData("hero", tally.str());
97           break;
98         case TALLY_NORMAL:
99           retval &= helper->saveData("normal", tally.str());
100           break;
101         case TALLY_SPECIAL:
102           retval &= helper->saveData("special", tally.str());
103           break;
104         case TALLY_SHIP:
105           retval &= helper->saveData("ship", tally.str());
106           break;
107         case TALLY_FLAG:
108           retval &= helper->saveData("flag", tally.str());
109           break;
110         }
111     }
112
113   retval &= helper->closeTag();
114
115   return retval;
116 }
117
118 void Triumphs::tallyTriumph(Player *p, TriumphType type)
119 {
120   //ignore monsters in a ruin who aren't owned by a player
121   if (!p) 
122     return;
123   guint32 id = p->getId();
124   //let's not tally neutrals
125   if (p == Playerlist::getInstance()->getNeutral()) 
126     return;
127   //we (this player) have killed P's army. it was of type TYPE.
128   d_triumph[id][type]++;
129 }
130
131 // End of file