]> ruin.nu Git - hbs.git/blob - bs/bsconf.cpp
some changes..
[hbs.git] / bs / bsconf.cpp
1 /***************************************************************************
2                           bsconf.cpp  -  description
3                              -------------------
4     begin                : Sat Mar 9 2002
5     copyright            : (C) 2002 by Michael Andreen
6     email                : whale@linux.nu
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18
19
20 #include <qsettings.h>
21 #include <qfile.h>
22 #include <qtextstream.h>
23
24 #include <iostream>
25 #include <map>
26 #include <vector>
27 using namespace std;
28
29 #include "bsconf.h"
30 #include "fleet.h"
31
32 BSConf::BSConf()
33 {
34         QSettings settings;
35         //settings.insertSearchPath(QSettings::Unix);
36
37         m_sRaceFilename = settings.readEntry("/hbs/ConfFiles/RaceFilename", "/usr/share/hbs/race.conf");
38         m_sStatsFilename = settings.readEntry("/hbs/ConfFiles/StatsFilename", "/usr/share/hbs/stats.conf");
39         
40         loadRace();
41         loadStats();
42 }
43
44 BSConf::~BSConf()
45 {
46 }
47
48 /////////////////////////////////////////////////////////////////////
49 //
50 bool BSConf::load()
51 {
52         return true;
53 }
54
55 /////////////////////////////////////////////////////////////////////
56 //
57 bool BSConf::save()
58 {
59         return true;
60 }
61
62 /////////////////////////////////////////////////////////////////////
63 //
64 bool BSConf::loadStats()
65 {
66         UnitList units;
67         QFile f(m_sStatsFilename);
68
69         if ( f.open(IO_ReadOnly) )
70     {    // file opened successfully
71         QTextStream t( &f );        // use a text stream
72         QString s;
73                 QString r;
74                 string race;
75                 while ( !t.eof() )
76             {
77                         r = t.readLine();       // line of text excluding '\n'
78                         race = (const char*) r;
79                         s = t.readLine();       // line of text excluding '\n'
80                         QTextStream t1(s,IO_ReadOnly );
81                         t1.skipWhiteSpace();
82                         while ( !t1.eof() )
83                 {
84                                 QString temp;
85                                 units[race].setName(race);
86                                 t1 >> temp;
87                                 units[race].setRace(temp.toInt());
88                                 t1 >> temp;
89                                 units[race].setUnitClass((const char*) temp);
90                                 t1 >> temp;
91                                 units[race].addTarget((const char*) temp);
92                                 t1 >> temp;
93                                 units[race].addTarget((const char*) temp);
94                                 t1 >> temp;
95                                 units[race].addTarget((const char*) temp);
96                                 t1 >> temp;
97                                 units[race].setInitiative(temp.toInt());
98                                 t1 >> temp;
99                                 units[race].setAgility(temp.toInt());
100                                 t1 >> temp;
101                                 units[race].setWeaponSpeed(temp.toInt());
102                                 t1 >> temp;
103                                 units[race].setGuns(temp.toInt());
104                                 t1 >> temp;
105                                 units[race].setPower(temp.toInt());
106                                 t1 >> temp;
107                                 units[race].setArmor(temp.toInt());
108                                 t1 >> temp;
109                                 units[race].setEMP(temp.toInt());
110                                 QString metal;
111                                 QString crystal;
112                                 QString eonium;
113                                 t1 >> metal;
114                                 t1 >> crystal;
115                                 t1 >> eonium;
116                                 units[race].setResources(tr("metal").latin1(), metal.toInt());
117                                 units[race].setResources(tr("crystal").latin1(), crystal.toInt());
118                                 units[race].setResources(tr("eonium").latin1(), eonium.toInt());
119                                 //units[race].setTotalResources(metal.toInt() + crystal.toInt() + eonium.toInt());
120                                 t1 >> temp;
121                                 units[race].setFuel(temp.toInt());
122                                 t1 >> temp;
123                                 units[race].setETA(temp.toInt());
124                                 t1 >> temp;
125                                 units[race].setType((const char*) temp);
126                 }
127             }
128                 Fleet::setUnits(units);
129         }
130         return true;
131 }
132
133 /////////////////////////////////////////////////////////////////////
134 //
135 bool BSConf::saveStats()
136 {
137         return true;
138 }
139
140 /////////////////////////////////////////////////////////////////////
141 //
142 bool BSConf::loadRace()
143 {
144         QFile f(m_sRaceFilename);
145         std::map<std::string, std::vector<int> > races;
146
147         if ( f.open(IO_ReadOnly) ) 
148         {    // file opened successfully
149                 QTextStream t( &f );        // use a text stream
150                 QString s;
151                 while ( !t.eof() ) 
152                 {        // until end of file...
153                         s = t.readLine();       // line of text excluding '\n'
154                         QTextStream t1(s,IO_ReadOnly );
155                         t1.skipWhiteSpace();
156                         QString race;
157                         t1 >> race;
158                         while ( !t1.eof() )
159                         {
160                                 QString units;
161                                 t1 >> units;
162                                 races[(const char*)race].push_back(units.toInt());
163                         }
164                 }
165                 Fleet::setRaces(races);
166         }
167         else 
168                 return false;
169         emit configurationChanged();
170         return true;
171 }
172
173 /////////////////////////////////////////////////////////////////////
174 //
175 bool BSConf::saveRace()
176 {
177         return true;
178 }
179
180