author | unc0rr |
Sun, 14 Jan 2007 13:23:15 +0000 | |
changeset 328 | 95625db21bf8 |
parent 327 | a281ff0803f1 |
child 329 | 4c3aad46baa5 |
permissions | -rw-r--r-- |
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 <QtGui> |
|
20 |
#include <QStringList> |
|
21 |
#include <QProcess> |
|
22 |
#include <QDir> |
|
23 |
#include <QPixmap> |
|
24 |
#include <QRegExp> |
|
25 |
#include <QIcon> |
|
26 |
#include <QFile> |
|
27 |
#include <QTextStream> |
|
28 |
||
29 |
#include "hwform.h" |
|
30 |
#include "game.h" |
|
31 |
#include "team.h" |
|
32 |
#include "netclient.h" |
|
33 |
#include "teamselect.h" |
|
34 |
#include "gameuiconfig.h" |
|
35 |
#include "pages.h" |
|
36 |
#include "hwconsts.h" |
|
314 | 37 |
#include "newnetclient.h" |
184 | 38 |
|
39 |
HWForm::HWForm(QWidget *parent) |
|
314 | 40 |
: QMainWindow(parent), pnetserver(0) |
184 | 41 |
{ |
42 |
ui.setupUi(this); |
|
43 |
||
301 | 44 |
config = new GameUIConfig(this, cfgdir->absolutePath() + "/hedgewars.ini"); |
184 | 45 |
|
245 | 46 |
UpdateTeamsLists(); |
184 | 47 |
|
48 |
connect(ui.pageMain->BtnSinglePlayer, SIGNAL(clicked()), this, SLOT(GoToSinglePlayer())); |
|
49 |
connect(ui.pageMain->BtnSetup, SIGNAL(clicked()), this, SLOT(GoToSetup())); |
|
50 |
connect(ui.pageMain->BtnMultiplayer, SIGNAL(clicked()), this, SLOT(GoToMultiplayer())); |
|
51 |
connect(ui.pageMain->BtnDemos, SIGNAL(clicked()), this, SLOT(GoToDemos())); |
|
52 |
connect(ui.pageMain->BtnNet, SIGNAL(clicked()), this, SLOT(GoToNet())); |
|
187 | 53 |
connect(ui.pageMain->BtnInfo, SIGNAL(clicked()), this, SLOT(GoToInfo())); |
184 | 54 |
connect(ui.pageMain->BtnExit, SIGNAL(clicked()), this, SLOT(close())); |
55 |
||
289 | 56 |
connect(ui.pageLocalGame->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 57 |
connect(ui.pageLocalGame->BtnSimpleGame, SIGNAL(clicked()), this, SLOT(SimpleGame())); |
58 |
||
59 |
connect(ui.pageEditTeam->BtnTeamSave, SIGNAL(clicked()), this, SLOT(TeamSave())); |
|
60 |
connect(ui.pageEditTeam->BtnTeamDiscard, SIGNAL(clicked()), this, SLOT(TeamDiscard())); |
|
61 |
||
289 | 62 |
connect(ui.pageMultiplayer->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 63 |
connect(ui.pageMultiplayer->BtnStartMPGame, SIGNAL(clicked()), this, SLOT(StartMPGame())); |
64 |
||
289 | 65 |
connect(ui.pagePlayDemo->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 66 |
connect(ui.pagePlayDemo->BtnPlayDemo, SIGNAL(clicked()), this, SLOT(PlayDemo())); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
245
diff
changeset
|
67 |
connect(ui.pagePlayDemo->DemosList, SIGNAL(doubleClicked (const QModelIndex &)), this, SLOT(PlayDemo())); |
184 | 68 |
|
289 | 69 |
connect(ui.pageOptions->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 70 |
connect(ui.pageOptions->BtnNewTeam, SIGNAL(clicked()), this, SLOT(NewTeam())); |
71 |
connect(ui.pageOptions->BtnEditTeam, SIGNAL(clicked()), this, SLOT(EditTeam())); |
|
72 |
connect(ui.pageOptions->BtnSaveOptions, SIGNAL(clicked()), config, SLOT(SaveOptions())); |
|
312 | 73 |
connect(ui.pageOptions->BtnSaveOptions, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 74 |
|
289 | 75 |
connect(ui.pageNet->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 76 |
connect(ui.pageNet->BtnNetConnect, SIGNAL(clicked()), this, SLOT(NetConnect())); |
314 | 77 |
connect(ui.pageNet->BtnNetSvrStart, SIGNAL(clicked()), this, SLOT(NetStartServer())); |
184 | 78 |
|
289 | 79 |
connect(ui.pageNetGame->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 80 |
connect(ui.pageNetGame->BtnGo, SIGNAL(clicked()), this, SLOT(NetStartGame())); |
81 |
||
82 |
connect(ui.pageNetChat->BtnDisconnect, SIGNAL(clicked()), this, SLOT(NetDisconnect())); |
|
83 |
connect(ui.pageNetChat->BtnJoin, SIGNAL(clicked()), this, SLOT(NetJoin())); |
|
84 |
connect(ui.pageNetChat->BtnCreate, SIGNAL(clicked()), this, SLOT(NetCreate())); |
|
85 |
||
289 | 86 |
connect(ui.pageInfo->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
187 | 87 |
|
306 | 88 |
connect(ui.pageGameStats->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
89 |
||
311 | 90 |
connect(ui.pageMultiplayer->teamsSelect, SIGNAL(NewTeam()), this, SLOT(NewTeam())); |
91 |
||
290 | 92 |
GoToPage(ID_PAGE_MAIN); |
184 | 93 |
} |
94 |
||
245 | 95 |
void HWForm::UpdateTeamsLists() |
96 |
{ |
|
97 |
QStringList teamslist = config->GetTeamsList(); |
|
98 |
||
99 |
if(teamslist.empty()) { |
|
100 |
HWTeam defaultTeam("DefaultTeam"); |
|
101 |
defaultTeam.SaveToFile(); |
|
102 |
teamslist.push_back("DefaultTeam"); |
|
103 |
} |
|
104 |
||
105 |
ui.pageOptions->CBTeamName->clear(); |
|
106 |
ui.pageOptions->CBTeamName->addItems(teamslist); |
|
107 |
} |
|
108 |
||
184 | 109 |
void HWForm::GoToMain() |
110 |
{ |
|
289 | 111 |
GoToPage(ID_PAGE_MAIN); |
184 | 112 |
} |
113 |
||
114 |
void HWForm::GoToSinglePlayer() |
|
115 |
{ |
|
289 | 116 |
GoToPage(ID_PAGE_SINGLEPLAYER); |
184 | 117 |
} |
118 |
||
119 |
void HWForm::GoToSetup() |
|
120 |
{ |
|
289 | 121 |
GoToPage(ID_PAGE_SETUP); |
184 | 122 |
} |
123 |
||
187 | 124 |
void HWForm::GoToInfo() |
125 |
{ |
|
289 | 126 |
GoToPage(ID_PAGE_INFO); |
187 | 127 |
} |
128 |
||
184 | 129 |
void HWForm::GoToMultiplayer() |
130 |
{ |
|
290 | 131 |
GoToPage(ID_PAGE_MULTIPLAYER); |
184 | 132 |
} |
133 |
||
134 |
void HWForm::GoToDemos() |
|
135 |
{ |
|
136 |
QDir tmpdir; |
|
137 |
tmpdir.cd(cfgdir->absolutePath()); |
|
138 |
tmpdir.cd("Demos"); |
|
139 |
tmpdir.setFilter(QDir::Files); |
|
140 |
ui.pagePlayDemo->DemosList->clear(); |
|
271 | 141 |
ui.pagePlayDemo->DemosList->addItems(tmpdir.entryList(QStringList("*.hwd_" + cProtoVer)) |
142 |
.replaceInStrings(QRegExp("^(.*).hwd_" + cProtoVer), "\\1")); |
|
290 | 143 |
GoToPage(ID_PAGE_DEMOS); |
184 | 144 |
} |
145 |
||
146 |
void HWForm::GoToNet() |
|
147 |
{ |
|
289 | 148 |
GoToPage(ID_PAGE_NET); |
184 | 149 |
} |
150 |
||
151 |
void HWForm::GoToNetChat() |
|
152 |
{ |
|
289 | 153 |
GoToPage(ID_PAGE_NETCHAT); |
154 |
} |
|
155 |
||
311 | 156 |
void HWForm::OnPageShown(quint8 id) |
157 |
{ |
|
322 | 158 |
if (id == ID_PAGE_MULTIPLAYER || id == ID_PAGE_NETCFG) { |
311 | 159 |
QStringList tmNames=config->GetTeamsList(); |
322 | 160 |
TeamSelWidget* curTeamSelWidget; |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
323
diff
changeset
|
161 |
id == ID_PAGE_MULTIPLAYER ? curTeamSelWidget=ui.pageMultiplayer->teamsSelect : |
322 | 162 |
curTeamSelWidget=ui.pageNetGame->pNetTeamsWidget; |
311 | 163 |
QList<HWTeam> teamsList; |
164 |
for(QStringList::iterator it=tmNames.begin(); it!=tmNames.end(); it++) { |
|
165 |
HWTeam team(*it); |
|
166 |
team.LoadFromFile(); |
|
167 |
teamsList.push_back(team); |
|
168 |
} |
|
322 | 169 |
curTeamSelWidget->resetPlayingTeams(teamsList); |
311 | 170 |
} |
171 |
} |
|
172 |
||
289 | 173 |
void HWForm::GoToPage(quint8 id) |
174 |
{ |
|
175 |
PagesStack.push(ui.Pages->currentIndex()); |
|
311 | 176 |
OnPageShown(id); |
289 | 177 |
ui.Pages->setCurrentIndex(id); |
178 |
} |
|
179 |
||
180 |
void HWForm::GoBack() |
|
181 |
{ |
|
182 |
quint8 id = PagesStack.isEmpty() ? ID_PAGE_MAIN : PagesStack.pop(); |
|
311 | 183 |
OnPageShown(id); |
289 | 184 |
ui.Pages->setCurrentIndex(id); |
184 | 185 |
} |
186 |
||
187 |
void HWForm::NewTeam() |
|
188 |
{ |
|
245 | 189 |
editedTeam = new HWTeam("unnamed"); |
190 |
editedTeam->SetToPage(this); |
|
290 | 191 |
GoToPage(ID_PAGE_SETUP_TEAM); |
184 | 192 |
} |
193 |
||
194 |
void HWForm::EditTeam() |
|
195 |
{ |
|
245 | 196 |
editedTeam = new HWTeam(ui.pageOptions->CBTeamName->currentText()); |
197 |
editedTeam->LoadFromFile(); |
|
198 |
editedTeam->SetToPage(this); |
|
290 | 199 |
GoToPage(ID_PAGE_SETUP_TEAM); |
184 | 200 |
} |
201 |
||
202 |
void HWForm::TeamSave() |
|
203 |
{ |
|
245 | 204 |
editedTeam->GetFromPage(this); |
205 |
editedTeam->SaveToFile(); |
|
206 |
delete editedTeam; |
|
207 |
UpdateTeamsLists(); |
|
290 | 208 |
GoBack(); |
184 | 209 |
} |
210 |
||
211 |
void HWForm::TeamDiscard() |
|
212 |
{ |
|
245 | 213 |
delete editedTeam; |
290 | 214 |
GoBack(); |
184 | 215 |
} |
216 |
||
217 |
void HWForm::SimpleGame() |
|
218 |
{ |
|
306 | 219 |
CreateGame(ui.pageLocalGame->gameCFG); |
184 | 220 |
game->StartQuick(); |
221 |
} |
|
222 |
||
223 |
void HWForm::PlayDemo() |
|
224 |
{ |
|
225 |
QListWidgetItem * curritem = ui.pagePlayDemo->DemosList->currentItem(); |
|
226 |
if (!curritem) |
|
227 |
{ |
|
228 |
QMessageBox::critical(this, |
|
229 |
tr("Error"), |
|
230 |
tr("Please, select demo from the list above"), |
|
231 |
tr("OK")); |
|
232 |
return ; |
|
233 |
} |
|
306 | 234 |
CreateGame(0); |
271 | 235 |
game->PlayDemo(cfgdir->absolutePath() + "/Demos/" + curritem->text() + ".hwd_" + cProtoVer); |
184 | 236 |
} |
237 |
||
314 | 238 |
void HWForm::_NetConnect(const QString & hostName, quint16 port, const QString & nick) |
184 | 239 |
{ |
314 | 240 |
hwnet = new HWNewNet(config); |
184 | 241 |
connect(hwnet, SIGNAL(AddGame(const QString &)), this, SLOT(AddGame(const QString &))); |
242 |
connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter())); |
|
243 |
connect(hwnet, SIGNAL(ChangeInTeams(const QStringList &)), this, SLOT(ChangeInNetTeams(const QStringList &))); |
|
327 | 244 |
connect(ui.pageNetGame->pNetTeamsWidget, SIGNAL(teamWillPlay(HWTeam)), hwnet, SLOT(AddTeam(HWTeam))); |
314 | 245 |
hwnet->Connect(hostName, port, nick); |
184 | 246 |
config->SaveOptions(); |
247 |
} |
|
248 |
||
314 | 249 |
void HWForm::NetConnect() |
250 |
{ |
|
251 |
_NetConnect(ui.pageNet->editIP->text(), 46631, ui.pageNet->editNetNick->text()); |
|
252 |
} |
|
253 |
||
254 |
void HWForm::NetStartServer() |
|
255 |
{ |
|
256 |
pnetserver = new HWNetServer; |
|
257 |
pnetserver->StartServer(); |
|
258 |
_NetConnect(pnetserver->getRunningHostName(), pnetserver->getRunningPort(), ui.pageNet->editNetNick->text()); |
|
259 |
} |
|
260 |
||
184 | 261 |
void HWForm::NetDisconnect() |
262 |
{ |
|
314 | 263 |
hwnet->Disconnect(); |
264 |
GoBack(); |
|
265 |
delete hwnet; |
|
266 |
hwnet=0; |
|
267 |
if(pnetserver) { |
|
268 |
pnetserver->StopServer(); |
|
269 |
delete pnetserver; |
|
270 |
pnetserver=0; |
|
271 |
} |
|
184 | 272 |
} |
273 |
||
274 |
void HWForm::AddGame(const QString & chan) |
|
275 |
{ |
|
276 |
ui.pageNetChat->ChannelsList->addItem(chan); |
|
277 |
} |
|
278 |
||
279 |
void HWForm::NetGameEnter() |
|
280 |
{ |
|
290 | 281 |
GoToPage(ID_PAGE_NETCFG); |
184 | 282 |
} |
283 |
||
284 |
void HWForm::NetJoin() |
|
285 |
{ |
|
286 |
hwnet->JoinGame("#hw"); |
|
287 |
} |
|
288 |
||
289 |
void HWForm::NetCreate() |
|
290 |
{ |
|
291 |
hwnet->JoinGame("#hw"); |
|
292 |
} |
|
293 |
||
294 |
void HWForm::NetStartGame() |
|
295 |
{ |
|
296 |
hwnet->StartGame(); |
|
297 |
} |
|
298 |
||
299 |
void HWForm::ChangeInNetTeams(const QStringList & teams) |
|
300 |
{ |
|
322 | 301 |
QStringList addedTeams=teams; |
302 |
list<HWTeam> lstPlaying=ui.pageNetGame->pNetTeamsWidget->getPlayingTeams(); |
|
303 |
for(list<HWTeam>::iterator it=lstPlaying.begin(); it!=lstPlaying.end(); ++it) { |
|
304 |
QString nm=it->TeamName; |
|
305 |
QStringList::iterator itt=std::find(addedTeams.begin(), addedTeams.end(), nm); |
|
306 |
if(itt!=addedTeams.end()) addedTeams.erase(itt); |
|
307 |
} |
|
308 |
for(QStringList::iterator it=addedTeams.begin(); it!=addedTeams.end(); ++it) { |
|
309 |
ui.pageNetGame->pNetTeamsWidget->addTeam(*it, true); |
|
310 |
} |
|
323 | 311 |
|
312 |
/* |
|
313 |
QStringList lstQSPlaying; |
|
314 |
std::for_each(lstPlaying.begin(), lstPlaying.end(), lstQSPlaying.push_back); |
|
315 |
QStringList removedTeams=teams; |
|
316 |
for(QStringList::iterator it=teams.begin() |
|
317 |
list<HWTeam>::iterator removedItem=std::find(addedTeams.begin(), addedTeams.end(), nm); |
|
318 |
*/ |
|
184 | 319 |
} |
320 |
||
321 |
void HWForm::StartMPGame() |
|
322 |
{ |
|
306 | 323 |
CreateGame(ui.pageMultiplayer->gameCFG); |
324 |
||
184 | 325 |
list<HWTeam> teamslist=ui.pageMultiplayer->teamsSelect->getPlayingTeams(); |
326 |
for (list<HWTeam>::const_iterator it = teamslist.begin(); it != teamslist.end(); ++it ) { |
|
207 | 327 |
HWTeamTempParams params=ui.pageMultiplayer->teamsSelect->getTeamParams(it->TeamName); |
213 | 328 |
game->AddTeam(it->TeamName, params); |
184 | 329 |
} |
330 |
game->StartLocal(); |
|
331 |
} |
|
306 | 332 |
|
333 |
void HWForm::GameStateChanged(GameState gameState) |
|
334 |
{ |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
323
diff
changeset
|
335 |
switch(gameState) { |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
323
diff
changeset
|
336 |
case gsStarted: { |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
323
diff
changeset
|
337 |
ui.pageGameStats->labelGameStats->setText(""); |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
323
diff
changeset
|
338 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
323
diff
changeset
|
339 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
323
diff
changeset
|
340 |
case gsFinished: { |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
323
diff
changeset
|
341 |
GoToPage(ID_PAGE_GAMESTATS); |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
323
diff
changeset
|
342 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
323
diff
changeset
|
343 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
323
diff
changeset
|
344 |
default: ; |
307 | 345 |
} |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
323
diff
changeset
|
346 |
|
307 | 347 |
} |
348 |
||
349 |
void HWForm::AddStatText(const QString & msg) |
|
350 |
{ |
|
351 |
ui.pageGameStats->labelGameStats->setText( |
|
352 |
ui.pageGameStats->labelGameStats->text() + msg); |
|
306 | 353 |
} |
354 |
||
355 |
void HWForm::GameStats(char type, const QString & info) |
|
356 |
{ |
|
357 |
switch(type) { |
|
307 | 358 |
case 'r' : { |
359 |
AddStatText(QString("<h1 align=\"center\">%1</h1>").arg(info)); |
|
360 |
break; |
|
361 |
} |
|
362 |
case 'D' : { |
|
363 |
int i = info.indexOf(' '); |
|
364 |
QString message = QLabel::tr("<p>The best shot award was won by <b>%1</b> with <b>%2</b> pts.</p>") |
|
365 |
.arg(info.mid(i + 1), info.left(i)); |
|
366 |
AddStatText(message); |
|
367 |
break; |
|
368 |
} |
|
369 |
case 'K' : { |
|
370 |
QString message = QLabel::tr("<p>A total of <b>%1</b> Hedgehog(s) were killed during this round.</p>").arg(info); |
|
371 |
AddStatText(message); |
|
372 |
break; |
|
373 |
} |
|
306 | 374 |
} |
375 |
} |
|
376 |
||
377 |
void HWForm::CreateGame(GameCFGWidget * gamecfg) |
|
378 |
{ |
|
379 |
game = new HWGame(config, gamecfg); |
|
380 |
connect(game, SIGNAL(GameStateChanged(GameState)), this, SLOT(GameStateChanged(GameState))); |
|
381 |
connect(game, SIGNAL(GameStats(char, const QString &)), this, SLOT(GameStats(char, const QString &))); |
|
382 |
} |