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>
|
|
22 |
#include "team.h"
|
|
23 |
#include "hwform.h"
|
|
24 |
#include "predefteams.h"
|
|
25 |
#include "pages.h"
|
|
26 |
#include "hwconsts.h"
|
|
27 |
|
|
28 |
HWTeam::HWTeam(const QString & teamname)
|
|
29 |
{
|
|
30 |
TeamName = teamname;
|
|
31 |
for (int i = 0; i < 8; i++) HHName[i].sprintf("hedgehog %d", i);
|
|
32 |
Grave = "Simple";
|
|
33 |
Fort = "Barrelhouse";
|
|
34 |
for(int i = 0; i < BINDS_NUMBER; i++)
|
|
35 |
{
|
|
36 |
binds[i].action = cbinds[i].action;
|
|
37 |
binds[i].strbind = cbinds[i].strbind;
|
|
38 |
}
|
|
39 |
}
|
|
40 |
|
|
41 |
HWTeam::HWTeam(quint8 num)
|
|
42 |
{
|
|
43 |
num %= PREDEFTEAMS_COUNT;
|
|
44 |
TeamName = QApplication::translate("teams", pteams[num].TeamName);
|
|
45 |
HHName[0] = QApplication::translate("teams", pteams[num].hh0name);
|
|
46 |
HHName[1] = QApplication::translate("teams", pteams[num].hh1name);
|
|
47 |
HHName[2] = QApplication::translate("teams", pteams[num].hh2name);
|
|
48 |
HHName[3] = QApplication::translate("teams", pteams[num].hh3name);
|
|
49 |
HHName[4] = QApplication::translate("teams", pteams[num].hh4name);
|
|
50 |
HHName[5] = QApplication::translate("teams", pteams[num].hh5name);
|
|
51 |
HHName[6] = QApplication::translate("teams", pteams[num].hh6name);
|
|
52 |
HHName[7] = QApplication::translate("teams", pteams[num].hh7name);
|
|
53 |
Grave = pteams[num].Grave;
|
|
54 |
Fort = pteams[num].Fort;
|
|
55 |
for(int i = 0; i < BINDS_NUMBER; i++)
|
|
56 |
{
|
|
57 |
binds[i].action = cbinds[i].action;
|
|
58 |
binds[i].strbind = cbinds[i].strbind;
|
|
59 |
}
|
|
60 |
}
|
|
61 |
|
|
62 |
|
|
63 |
bool HWTeam::LoadFromFile()
|
|
64 |
{
|
|
65 |
QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg");
|
|
66 |
if (!cfgfile.open(QIODevice::ReadOnly)) return false;
|
|
67 |
QTextStream stream(&cfgfile);
|
|
68 |
stream.setCodec("UTF-8");
|
|
69 |
QString str;
|
|
70 |
QString action;
|
|
71 |
|
|
72 |
while (!stream.atEnd())
|
|
73 |
{
|
|
74 |
str = stream.readLine();
|
|
75 |
if (str.startsWith(";")) continue;
|
|
76 |
if (str.startsWith("name team "))
|
|
77 |
{
|
|
78 |
str.remove(0, 10);
|
|
79 |
TeamName = str;
|
|
80 |
} else
|
|
81 |
if (str.startsWith("name hh"))
|
|
82 |
{
|
|
83 |
str.remove(0, 7);
|
|
84 |
long i = str.left(1).toLong();
|
|
85 |
if ((i < 0) || (i > 7)) continue;
|
|
86 |
str.remove(0, 2);
|
|
87 |
HHName[i] = str;
|
|
88 |
} else
|
|
89 |
if (str.startsWith("grave "))
|
|
90 |
{
|
|
91 |
str.remove(0, 6);
|
|
92 |
Grave = str;
|
|
93 |
} else
|
|
94 |
if (str.startsWith("fort "))
|
|
95 |
{
|
|
96 |
str.remove(0, 5);
|
|
97 |
Fort = str;
|
|
98 |
} else
|
|
99 |
if (str.startsWith("bind "))
|
|
100 |
{
|
|
101 |
str.remove(0, 5);
|
|
102 |
action = str.section(' ', 1);
|
|
103 |
str = str.section(' ', 0, 0);
|
|
104 |
str.truncate(15);
|
|
105 |
for (int i = 0; i < BINDS_NUMBER; i++)
|
|
106 |
if (action == binds[i].action)
|
|
107 |
{
|
|
108 |
binds[i].strbind = str;
|
|
109 |
break;
|
|
110 |
}
|
|
111 |
}
|
|
112 |
}
|
|
113 |
cfgfile.close();
|
|
114 |
return true;
|
|
115 |
}
|
|
116 |
|
|
117 |
bool HWTeam::SaveToFile()
|
|
118 |
{
|
|
119 |
QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg");
|
|
120 |
if (!cfgfile.open(QIODevice::WriteOnly)) return false;
|
|
121 |
QTextStream stream(&cfgfile);
|
|
122 |
stream.setCodec("UTF-8");
|
|
123 |
stream << "; Generated by Hedgewars, do not modify" << endl;
|
|
124 |
stream << "name team " << TeamName << endl;
|
|
125 |
for (int i = 0; i < 8; i++)
|
|
126 |
stream << "name hh" << i << " " << HHName[i] << endl;
|
|
127 |
stream << "grave " << Grave << endl;
|
|
128 |
stream << "fort " << Fort << endl;
|
|
129 |
for(int i = 0; i < BINDS_NUMBER; i++)
|
|
130 |
{
|
|
131 |
stream << "bind " << binds[i].strbind << " " << binds[i].action << endl;
|
|
132 |
}
|
|
133 |
cfgfile.close();
|
|
134 |
return true;
|
|
135 |
}
|
|
136 |
|
|
137 |
void HWTeam::SetToPage(HWForm * hwform)
|
|
138 |
{
|
|
139 |
hwform->ui.pageEditTeam->TeamNameEdit->setText(TeamName);
|
|
140 |
for(int i = 0; i < 8; i++)
|
|
141 |
{
|
|
142 |
hwform->ui.pageEditTeam->HHNameEdit[i]->setText(HHName[i]);
|
|
143 |
}
|
|
144 |
hwform->ui.pageEditTeam->CBGrave->setCurrentIndex(hwform->ui.pageEditTeam->CBGrave->findText(Grave));
|
|
145 |
hwform->ui.pageEditTeam->CBGrave_activated(Grave);
|
|
146 |
|
|
147 |
hwform->ui.pageEditTeam->CBFort->setCurrentIndex(hwform->ui.pageEditTeam->CBFort->findText(Fort));
|
|
148 |
hwform->ui.pageEditTeam->CBFort_activated(Fort);
|
|
149 |
|
|
150 |
for(int i = 0; i < BINDS_NUMBER; i++)
|
|
151 |
{
|
|
152 |
hwform->ui.pageEditTeam->CBBind[i]->setCurrentIndex(hwform->ui.pageEditTeam->CBBind[i]->findText(binds[i].strbind));
|
|
153 |
}
|
|
154 |
}
|
|
155 |
|
|
156 |
void HWTeam::GetFromPage(HWForm * hwform)
|
|
157 |
{
|
|
158 |
TeamName = hwform->ui.pageEditTeam->TeamNameEdit->text();
|
|
159 |
for(int i = 0; i < 8; i++)
|
|
160 |
{
|
|
161 |
HHName[i] = hwform->ui.pageEditTeam->HHNameEdit[i]->text();
|
|
162 |
}
|
|
163 |
|
|
164 |
Grave = hwform->ui.pageEditTeam->CBGrave->currentText();
|
|
165 |
Fort = hwform->ui.pageEditTeam->CBFort->currentText();
|
|
166 |
for(int i = 0; i < 8; i++)
|
|
167 |
{
|
|
168 |
binds[i].strbind = hwform->ui.pageEditTeam->CBBind[i]->currentText();
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
QByteArray HWTeam::IPCTeamInfo() const
|
|
173 |
{
|
|
174 |
QByteArray buf;
|
|
175 |
#define ADD(a) { \
|
|
176 |
QByteArray strmsg = a.toUtf8(); \
|
|
177 |
quint8 sz = strmsg.size(); \
|
|
178 |
buf.append(QByteArray((char *)&sz, 1)); \
|
|
179 |
buf.append(strmsg); \
|
|
180 |
}
|
|
181 |
|
|
182 |
ADD(QString("ename team " + TeamName));
|
|
183 |
for (int i = 0; i < 8; i++)
|
|
184 |
ADD(QString("ename hh%1 ").arg(i).append(HHName[i]));
|
|
185 |
ADD(QString("egrave " + Grave));
|
|
186 |
ADD(QString("efort " + Fort));
|
|
187 |
for(int i = 0; i < BINDS_NUMBER; i++)
|
|
188 |
{
|
|
189 |
ADD(QString("ebind " + binds[i].strbind + " " + binds[i].action));
|
|
190 |
}
|
|
191 |
#undef ADD
|
|
192 |
return buf;
|
|
193 |
}
|
|
194 |
|
|
195 |
bool HWTeam::operator==(const HWTeam& t1) const {
|
|
196 |
return TeamName==t1.TeamName;
|
|
197 |
}
|
|
198 |
|
|
199 |
bool HWTeam::operator<(const HWTeam& t1) const {
|
|
200 |
return TeamName<t1.TeamName;
|
|
201 |
}
|