author | unc0rr |
Tue, 25 Jul 2006 20:19:46 +0000 | |
changeset 86 | 664b536a1c27 |
parent 26 | e32fa14529f8 |
child 87 | ff213e443336 |
permissions | -rw-r--r-- |
26 | 1 |
/* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
5 |
* Distributed under the terms of the BSD-modified licence: |
|
6 |
* |
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 |
* of this software and associated documentation files (the "Software"), to deal |
|
9 |
* with the Software without restriction, including without limitation the |
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is |
|
12 |
* furnished to do so, subject to the following conditions: |
|
13 |
* |
|
14 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
15 |
* this list of conditions and the following disclaimer. |
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
17 |
* this list of conditions and the following disclaimer in the documentation |
|
18 |
* and/or other materials provided with the distribution. |
|
19 |
* 3. The name of the author may not be used to endorse or promote products |
|
20 |
* derived from this software without specific prior written permission. |
|
21 |
* |
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 |
*/ |
|
33 |
||
34 |
#include <QFile> |
|
35 |
#include <QTextStream> |
|
36 |
#include "team.h" |
|
37 |
#include "hwform.h" |
|
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
38 |
#include "gameuiconfig.h" |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
39 |
#include "predefteams.h" |
26 | 40 |
|
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
41 |
HWTeam::HWTeam(const QString & teamname, GameUIConfig * config) |
26 | 42 |
{ |
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
43 |
this->config = config; |
26 | 44 |
TeamName = teamname; |
45 |
for (int i = 0; i < 8; i++) HHName[i].sprintf("hedgehog %d", i); |
|
46 |
Grave = "Simple"; |
|
47 |
Fort = "Barrelhouse"; |
|
48 |
for(int i = 0; i < BINDS_NUMBER; i++) |
|
49 |
{ |
|
50 |
binds[i].action = cbinds[i].action; |
|
51 |
binds[i].strbind = cbinds[i].strbind; |
|
52 |
} |
|
53 |
} |
|
54 |
||
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
55 |
HWTeam::HWTeam(quint8 num, GameUIConfig * config) |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
56 |
{ |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
57 |
this->config = config; |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
58 |
num %= PREDEFTEAMS_COUNT; |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
59 |
TeamName = pteams[num].TeamName; |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
60 |
HHName[0] = pteams[num].hh0name; |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
61 |
HHName[1] = pteams[num].hh1name; |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
62 |
HHName[2] = pteams[num].hh2name; |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
63 |
HHName[3] = pteams[num].hh3name; |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
64 |
HHName[4] = pteams[num].hh4name; |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
65 |
HHName[5] = pteams[num].hh5name; |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
66 |
HHName[6] = pteams[num].hh6name; |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
67 |
HHName[7] = pteams[num].hh7name; |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
68 |
Grave = pteams[num].Grave; |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
69 |
Fort = pteams[num].Fort; |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
70 |
} |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
71 |
|
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
72 |
|
26 | 73 |
bool HWTeam::LoadFromFile() |
74 |
{ |
|
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
75 |
QFile cfgfile(config->cfgdir.absolutePath() + "/" + TeamName + ".cfg"); |
26 | 76 |
if (!cfgfile.open(QIODevice::ReadOnly)) return false; |
77 |
QTextStream stream(&cfgfile); |
|
78 |
stream.setCodec("UTF-8"); |
|
79 |
QString str; |
|
80 |
QString action; |
|
81 |
||
82 |
while (!stream.atEnd()) |
|
83 |
{ |
|
84 |
str = stream.readLine(); |
|
85 |
if (str.startsWith(";")) continue; |
|
86 |
if (str.startsWith("name team ")) |
|
87 |
{ |
|
88 |
str.remove(0, 10); |
|
89 |
TeamName = str; |
|
90 |
} else |
|
91 |
if (str.startsWith("name hh")) |
|
92 |
{ |
|
93 |
str.remove(0, 7); |
|
94 |
long i = str.left(1).toLong(); |
|
95 |
if ((i < 0) || (i > 7)) continue; |
|
96 |
str.remove(0, 2); |
|
97 |
HHName[i] = str; |
|
98 |
} else |
|
99 |
if (str.startsWith("grave ")) |
|
100 |
{ |
|
101 |
str.remove(0, 6); |
|
102 |
Grave = str; |
|
103 |
} else |
|
104 |
if (str.startsWith("fort ")) |
|
105 |
{ |
|
106 |
str.remove(0, 5); |
|
107 |
Fort = str; |
|
108 |
} else |
|
109 |
if (str.startsWith("bind ")) |
|
110 |
{ |
|
111 |
str.remove(0, 5); |
|
112 |
action = str.section(' ', 1); |
|
113 |
str = str.section(' ', 0, 0); |
|
114 |
str.truncate(15); |
|
115 |
for (int i = 0; i < BINDS_NUMBER; i++) |
|
116 |
if (action == binds[i].action) |
|
117 |
{ |
|
118 |
binds[i].strbind = str; |
|
119 |
break; |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
123 |
cfgfile.close(); |
|
124 |
return true; |
|
125 |
} |
|
126 |
||
127 |
bool HWTeam::SaveToFile() |
|
128 |
{ |
|
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
129 |
QFile cfgfile(config->cfgdir.absolutePath() + "/" + TeamName + ".cfg"); |
26 | 130 |
if (!cfgfile.open(QIODevice::WriteOnly)) return false; |
131 |
QTextStream stream(&cfgfile); |
|
132 |
stream.setCodec("UTF-8"); |
|
133 |
stream << "; Generated by Hedgewars, do not modify" << endl; |
|
134 |
stream << "name team " << TeamName << endl; |
|
135 |
for (int i = 0; i < 8; i++) |
|
136 |
stream << "name hh" << i << " " << HHName[i] << endl; |
|
137 |
stream << "grave " << Grave << endl; |
|
138 |
stream << "fort " << Fort << endl; |
|
139 |
for(int i = 0; i < BINDS_NUMBER; i++) |
|
140 |
{ |
|
141 |
stream << "bind " << binds[i].strbind << " " << binds[i].action << endl; |
|
142 |
} |
|
143 |
cfgfile.close(); |
|
144 |
return true; |
|
145 |
} |
|
146 |
||
147 |
void HWTeam::SetToPage(HWForm * hwform) |
|
148 |
{ |
|
149 |
hwform->TeamNameEdit->setText(TeamName); |
|
150 |
for(int i = 0; i < 8; i++) |
|
151 |
{ |
|
152 |
hwform->HHNameEdit[i]->setText(HHName[i]); |
|
153 |
} |
|
154 |
hwform->ui.CBGrave->setCurrentIndex(hwform->ui.CBGrave->findText(Grave)); |
|
155 |
hwform->CBGrave_activated(Grave); |
|
156 |
||
157 |
hwform->ui.CBFort->setCurrentIndex(hwform->ui.CBFort->findText(Fort)); |
|
158 |
hwform->CBFort_activated(Fort); |
|
159 |
||
160 |
for(int i = 0; i < BINDS_NUMBER; i++) |
|
161 |
{ |
|
162 |
hwform->CBBind[i]->setCurrentIndex(hwform->CBBind[i]->findText(binds[i].strbind)); |
|
163 |
} |
|
164 |
} |
|
165 |
||
166 |
void HWTeam::GetFromPage(HWForm * hwform) |
|
167 |
{ |
|
168 |
TeamName = hwform->TeamNameEdit->text(); |
|
169 |
for(int i = 0; i < 8; i++) |
|
170 |
{ |
|
171 |
HHName[i] = hwform->HHNameEdit[i]->text(); |
|
172 |
} |
|
173 |
||
174 |
Grave = hwform->ui.CBGrave->currentText(); |
|
175 |
Fort = hwform->ui.CBFort->currentText(); |
|
176 |
for(int i = 0; i < 8; i++) |
|
177 |
{ |
|
178 |
binds[i].strbind = hwform->CBBind[i]->currentText(); |
|
179 |
} |
|
180 |
} |
|
181 |
||
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
182 |
QByteArray HWTeam::IPCTeamInfo() const |
26 | 183 |
{ |
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
184 |
QByteArray buf; |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
185 |
#define ADD(a) { \ |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
186 |
QByteArray strmsg = a.toUtf8(); \ |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
187 |
quint8 sz = strmsg.size(); \ |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
188 |
buf.append(QByteArray((char *)&sz, 1)); \ |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
189 |
buf.append(strmsg); \ |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
190 |
} |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
191 |
|
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
192 |
ADD(QString("ename team " + TeamName)); |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
193 |
for (int i = 0; i < 8; i++) |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
194 |
ADD(QString("ename hh%1 ").arg(i).append(HHName[i])); |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
195 |
ADD(QString("egrave " + Grave)); |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
196 |
ADD(QString("efort " + Fort)); |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
197 |
for(int i = 0; i < BINDS_NUMBER; i++) |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
198 |
{ |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
199 |
ADD(QString("ebind " + binds[i].strbind + " " + binds[i].action)); |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
200 |
} |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
201 |
#undef ADD |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
202 |
return buf; |
26 | 203 |
} |
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
26
diff
changeset
|
204 |