184
|
1 |
/*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation; version 2 of the License
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
15 |
* along with this program; if not, write to the Free Software
|
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
17 |
*/
|
|
18 |
|
|
19 |
#include <QFile>
|
|
20 |
#include <QTextStream>
|
|
21 |
#include <QApplication>
|
231
|
22 |
#include <QSpinBox>
|
184
|
23 |
#include "team.h"
|
|
24 |
#include "hwform.h"
|
|
25 |
#include "predefteams.h"
|
|
26 |
#include "pages.h"
|
|
27 |
#include "hwconsts.h"
|
|
28 |
|
231
|
29 |
HWTeam::HWTeam(const QString & teamname) :
|
|
30 |
difficulty(0)
|
184
|
31 |
{
|
|
32 |
TeamName = teamname;
|
|
33 |
for (int i = 0; i < 8; i++) HHName[i].sprintf("hedgehog %d", i);
|
|
34 |
Grave = "Simple";
|
|
35 |
Fort = "Barrelhouse";
|
|
36 |
for(int i = 0; i < BINDS_NUMBER; i++)
|
|
37 |
{
|
|
38 |
binds[i].action = cbinds[i].action;
|
|
39 |
binds[i].strbind = cbinds[i].strbind;
|
|
40 |
}
|
|
41 |
}
|
|
42 |
|
231
|
43 |
HWTeam::HWTeam(quint8 num) :
|
|
44 |
difficulty(0)
|
184
|
45 |
{
|
|
46 |
num %= PREDEFTEAMS_COUNT;
|
|
47 |
TeamName = QApplication::translate("teams", pteams[num].TeamName);
|
|
48 |
HHName[0] = QApplication::translate("teams", pteams[num].hh0name);
|
|
49 |
HHName[1] = QApplication::translate("teams", pteams[num].hh1name);
|
|
50 |
HHName[2] = QApplication::translate("teams", pteams[num].hh2name);
|
|
51 |
HHName[3] = QApplication::translate("teams", pteams[num].hh3name);
|
|
52 |
HHName[4] = QApplication::translate("teams", pteams[num].hh4name);
|
|
53 |
HHName[5] = QApplication::translate("teams", pteams[num].hh5name);
|
|
54 |
HHName[6] = QApplication::translate("teams", pteams[num].hh6name);
|
|
55 |
HHName[7] = QApplication::translate("teams", pteams[num].hh7name);
|
|
56 |
Grave = pteams[num].Grave;
|
|
57 |
Fort = pteams[num].Fort;
|
|
58 |
for(int i = 0; i < BINDS_NUMBER; i++)
|
|
59 |
{
|
|
60 |
binds[i].action = cbinds[i].action;
|
|
61 |
binds[i].strbind = cbinds[i].strbind;
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
|
65 |
|
|
66 |
bool HWTeam::LoadFromFile()
|
|
67 |
{
|
|
68 |
QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg");
|
|
69 |
if (!cfgfile.open(QIODevice::ReadOnly)) return false;
|
|
70 |
QTextStream stream(&cfgfile);
|
|
71 |
stream.setCodec("UTF-8");
|
|
72 |
QString str;
|
|
73 |
QString action;
|
|
74 |
|
|
75 |
while (!stream.atEnd())
|
|
76 |
{
|
|
77 |
str = stream.readLine();
|
|
78 |
if (str.startsWith(";")) continue;
|
|
79 |
if (str.startsWith("name team "))
|
|
80 |
{
|
|
81 |
str.remove(0, 10);
|
|
82 |
TeamName = str;
|
|
83 |
} else
|
|
84 |
if (str.startsWith("name hh"))
|
|
85 |
{
|
|
86 |
str.remove(0, 7);
|
|
87 |
long i = str.left(1).toLong();
|
|
88 |
if ((i < 0) || (i > 7)) continue;
|
|
89 |
str.remove(0, 2);
|
|
90 |
HHName[i] = str;
|
|
91 |
} else
|
|
92 |
if (str.startsWith("grave "))
|
|
93 |
{
|
|
94 |
str.remove(0, 6);
|
|
95 |
Grave = str;
|
|
96 |
} else
|
|
97 |
if (str.startsWith("fort "))
|
|
98 |
{
|
|
99 |
str.remove(0, 5);
|
|
100 |
Fort = str;
|
|
101 |
} else
|
|
102 |
if (str.startsWith("bind "))
|
|
103 |
{
|
|
104 |
str.remove(0, 5);
|
|
105 |
action = str.section(' ', 1);
|
|
106 |
str = str.section(' ', 0, 0);
|
|
107 |
str.truncate(15);
|
|
108 |
for (int i = 0; i < BINDS_NUMBER; i++)
|
|
109 |
if (action == binds[i].action)
|
|
110 |
{
|
|
111 |
binds[i].strbind = str;
|
|
112 |
break;
|
|
113 |
}
|
231
|
114 |
} else
|
|
115 |
if (str.startsWith("difficulty "))
|
|
116 |
{
|
|
117 |
str.remove(0, 11);
|
|
118 |
difficulty=str.toUInt();
|
|
119 |
if (difficulty>5) difficulty=0; // this shouldn't normally happen
|
184
|
120 |
}
|
|
121 |
}
|
|
122 |
cfgfile.close();
|
|
123 |
return true;
|
|
124 |
}
|
|
125 |
|
|
126 |
bool HWTeam::SaveToFile()
|
|
127 |
{
|
|
128 |
QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg");
|
|
129 |
if (!cfgfile.open(QIODevice::WriteOnly)) return false;
|
|
130 |
QTextStream stream(&cfgfile);
|
|
131 |
stream.setCodec("UTF-8");
|
|
132 |
stream << "; Generated by Hedgewars, do not modify" << endl;
|
|
133 |
stream << "name team " << TeamName << endl;
|
|
134 |
for (int i = 0; i < 8; i++)
|
|
135 |
stream << "name hh" << i << " " << HHName[i] << endl;
|
|
136 |
stream << "grave " << Grave << endl;
|
|
137 |
stream << "fort " << Fort << endl;
|
|
138 |
for(int i = 0; i < BINDS_NUMBER; i++)
|
|
139 |
{
|
|
140 |
stream << "bind " << binds[i].strbind << " " << binds[i].action << endl;
|
|
141 |
}
|
231
|
142 |
stream << "difficulty " << difficulty << endl;
|
184
|
143 |
cfgfile.close();
|
|
144 |
return true;
|
|
145 |
}
|
|
146 |
|
|
147 |
void HWTeam::SetToPage(HWForm * hwform)
|
|
148 |
{
|
|
149 |
hwform->ui.pageEditTeam->TeamNameEdit->setText(TeamName);
|
231
|
150 |
hwform->ui.pageEditTeam->difficultyBox->setValue(difficulty);
|
184
|
151 |
for(int i = 0; i < 8; i++)
|
|
152 |
{
|
|
153 |
hwform->ui.pageEditTeam->HHNameEdit[i]->setText(HHName[i]);
|
|
154 |
}
|
|
155 |
hwform->ui.pageEditTeam->CBGrave->setCurrentIndex(hwform->ui.pageEditTeam->CBGrave->findText(Grave));
|
|
156 |
hwform->ui.pageEditTeam->CBGrave_activated(Grave);
|
|
157 |
|
|
158 |
hwform->ui.pageEditTeam->CBFort->setCurrentIndex(hwform->ui.pageEditTeam->CBFort->findText(Fort));
|
|
159 |
hwform->ui.pageEditTeam->CBFort_activated(Fort);
|
|
160 |
|
|
161 |
for(int i = 0; i < BINDS_NUMBER; i++)
|
|
162 |
{
|
|
163 |
hwform->ui.pageEditTeam->CBBind[i]->setCurrentIndex(hwform->ui.pageEditTeam->CBBind[i]->findText(binds[i].strbind));
|
|
164 |
}
|
|
165 |
}
|
|
166 |
|
|
167 |
void HWTeam::GetFromPage(HWForm * hwform)
|
|
168 |
{
|
|
169 |
TeamName = hwform->ui.pageEditTeam->TeamNameEdit->text();
|
231
|
170 |
difficulty = hwform->ui.pageEditTeam->difficultyBox->value();
|
184
|
171 |
for(int i = 0; i < 8; i++)
|
|
172 |
{
|
|
173 |
HHName[i] = hwform->ui.pageEditTeam->HHNameEdit[i]->text();
|
|
174 |
}
|
|
175 |
|
|
176 |
Grave = hwform->ui.pageEditTeam->CBGrave->currentText();
|
|
177 |
Fort = hwform->ui.pageEditTeam->CBFort->currentText();
|
|
178 |
for(int i = 0; i < 8; i++)
|
|
179 |
{
|
|
180 |
binds[i].strbind = hwform->ui.pageEditTeam->CBBind[i]->currentText();
|
|
181 |
}
|
|
182 |
}
|
|
183 |
|
|
184 |
QByteArray HWTeam::IPCTeamInfo() const
|
|
185 |
{
|
|
186 |
QByteArray buf;
|
|
187 |
#define ADD(a) { \
|
|
188 |
QByteArray strmsg = a.toUtf8(); \
|
|
189 |
quint8 sz = strmsg.size(); \
|
|
190 |
buf.append(QByteArray((char *)&sz, 1)); \
|
|
191 |
buf.append(strmsg); \
|
|
192 |
}
|
|
193 |
|
|
194 |
ADD(QString("ename team " + TeamName));
|
|
195 |
for (int i = 0; i < 8; i++)
|
|
196 |
ADD(QString("ename hh%1 ").arg(i).append(HHName[i]));
|
|
197 |
ADD(QString("egrave " + Grave));
|
|
198 |
ADD(QString("efort " + Fort));
|
|
199 |
for(int i = 0; i < BINDS_NUMBER; i++)
|
|
200 |
{
|
|
201 |
ADD(QString("ebind " + binds[i].strbind + " " + binds[i].action));
|
|
202 |
}
|
|
203 |
#undef ADD
|
|
204 |
return buf;
|
|
205 |
}
|
|
206 |
|
|
207 |
bool HWTeam::operator==(const HWTeam& t1) const {
|
|
208 |
return TeamName==t1.TeamName;
|
|
209 |
}
|
|
210 |
|
|
211 |
bool HWTeam::operator<(const HWTeam& t1) const {
|
|
212 |
return TeamName<t1.TeamName;
|
|
213 |
}
|