QTfrontend/game.cpp
author displacer
Thu, 08 Feb 2007 20:54:14 +0000
changeset 419 fdeed9718e6b
parent 406 ea90c6358054
child 425 a7e1dabc8fb7
permissions -rw-r--r--
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     1
/*
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     2
 * Hedgewars, a worms-like game
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     3
 * Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     4
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     8
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    12
 * GNU General Public License for more details.
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    13
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    14
 * You should have received a copy of the GNU General Public License
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    15
 * along with this program; if not, write to the Free Software
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    17
 */
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    18
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    19
#include <QMessageBox>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    20
#include <QProcess>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    21
#include <QTimer>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    22
#include <QString>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    23
#include <QByteArray>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    24
#include <QFile>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    25
#include <QTextStream>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    26
#include <QUuid>
341
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    27
#include <QDebug>
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    28
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    29
#include "game.h"
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    30
#include "hwconsts.h"
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    31
#include "gameuiconfig.h"
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    32
#include "gamecfgwidget.h"
339
7535ab6c3820 Run game message added, team and config info provided for net game
displacer
parents: 324
diff changeset
    33
#include "teamselect.h"
210
440575778475 Fixed compilation on *nix .)
unc0rr
parents: 208
diff changeset
    34
#include "KB.h"
239
f9adf3c73bed 'Proto' class to make things easier
unc0rr
parents: 228
diff changeset
    35
#include "proto.h"
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    36
339
7535ab6c3820 Run game message added, team and config info provided for net game
displacer
parents: 324
diff changeset
    37
HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg, TeamSelWidget* pTeamSelWidget) :
7535ab6c3820 Run game message added, team and config info provided for net game
displacer
parents: 324
diff changeset
    38
  TCPBase(true),
7535ab6c3820 Run game message added, team and config info provided for net game
displacer
parents: 324
diff changeset
    39
  m_pTeamSelWidget(pTeamSelWidget)
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    40
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    41
	this->config = config;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    42
	this->gamecfg = gamecfg;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    43
	TeamCount = 0;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    44
	seed = "";
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    45
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    46
419
fdeed9718e6b virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents: 406
diff changeset
    47
HWGame::~HWGame()
fdeed9718e6b virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents: 406
diff changeset
    48
{
fdeed9718e6b virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents: 406
diff changeset
    49
}
fdeed9718e6b virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents: 406
diff changeset
    50
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    51
void HWGame::onClientDisconnect()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    52
{
271
f2f9a3d5b441 Protocol version sets in CMake
unc0rr
parents: 267
diff changeset
    53
	SaveDemo(cfgdir->absolutePath() + "/Demos/LastRound.hwd_" + cProtoVer);
324
f4c109c82a0c Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents: 318
diff changeset
    54
	emit GameStateChanged(gsStopped);
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    55
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    56
253
e7b3687fcb2c fixed: choose map for simple game is working now
displacer
parents: 249
diff changeset
    57
void HWGame::commonConfig()
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    58
{
318
46a43b02bbb3 Game config commands are generated by GameCFGWidget
unc0rr
parents: 312
diff changeset
    59
	QByteArray buf;
341
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    60
	QString gt;
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    61
	switch (gameType) {
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    62
		case gtDemo:
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    63
			gt = "TD";
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    64
			break;
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    65
		case gtNet:
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    66
			gt = "TN";
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    67
			break;
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    68
		default:
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    69
			gt = "TL";
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    70
	}
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    71
	HWProto::addStringToBuffer(buf, gt);
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    72
	qDebug() << "game type" << gt;
318
46a43b02bbb3 Game config commands are generated by GameCFGWidget
unc0rr
parents: 312
diff changeset
    73
	HWProto::addStringListToBuffer(buf, gamecfg->getFullConfig());
341
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    74
	qDebug() << "config" << gamecfg->getFullConfig();
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    75
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    76
	if (m_pTeamSelWidget)
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    77
	{
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    78
		qDebug() << "adding teams from teamselwidget...";
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 341
diff changeset
    79
		QList<HWTeam> teams = m_pTeamSelWidget->getPlayingTeams();
341
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    80
		for(QList<HWTeam>::iterator it = teams.begin(); it != teams.end(); ++it)
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    81
		{
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    82
			qDebug() << "a team...";
352
4665bfe25470 first buggy hedgehogs num net change
displacer
parents: 341
diff changeset
    83
			//(*it).numHedgehogs = 4;
341
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    84
			HWProto::addStringListToBuffer(buf,
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    85
				(*it).TeamGameConfig(gamecfg->getInitHealth()));
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    86
		}
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
    87
	}
318
46a43b02bbb3 Game config commands are generated by GameCFGWidget
unc0rr
parents: 312
diff changeset
    88
	RawSendIPC(buf);
253
e7b3687fcb2c fixed: choose map for simple game is working now
displacer
parents: 249
diff changeset
    89
}
e7b3687fcb2c fixed: choose map for simple game is working now
displacer
parents: 249
diff changeset
    90
e7b3687fcb2c fixed: choose map for simple game is working now
displacer
parents: 249
diff changeset
    91
void HWGame::SendConfig()
e7b3687fcb2c fixed: choose map for simple game is working now
displacer
parents: 249
diff changeset
    92
{
e7b3687fcb2c fixed: choose map for simple game is working now
displacer
parents: 249
diff changeset
    93
	commonConfig();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    94
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    95
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    96
void HWGame::SendQuickConfig()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    97
{
253
e7b3687fcb2c fixed: choose map for simple game is working now
displacer
parents: 249
diff changeset
    98
	commonConfig();
239
f9adf3c73bed 'Proto' class to make things easier
unc0rr
parents: 228
diff changeset
    99
f9adf3c73bed 'Proto' class to make things easier
unc0rr
parents: 228
diff changeset
   100
	QByteArray teamscfg;
f9adf3c73bed 'Proto' class to make things easier
unc0rr
parents: 228
diff changeset
   101
	HWTeam team1(0);
f9adf3c73bed 'Proto' class to make things easier
unc0rr
parents: 228
diff changeset
   102
	team1.difficulty = 0;
341
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
   103
	team1.teamColor = QColor(65535);
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
   104
	team1.numHedgehogs = 4;
312
c36d0b34ac3d Use new parameters, small protocol change
unc0rr
parents: 306
diff changeset
   105
	HWProto::addStringListToBuffer(teamscfg,
341
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
   106
			team1.TeamGameConfig(gamecfg->getInitHealth()));
239
f9adf3c73bed 'Proto' class to make things easier
unc0rr
parents: 228
diff changeset
   107
f9adf3c73bed 'Proto' class to make things easier
unc0rr
parents: 228
diff changeset
   108
	HWTeam team2(2);
f9adf3c73bed 'Proto' class to make things easier
unc0rr
parents: 228
diff changeset
   109
	team2.difficulty = 4;
341
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
   110
	team2.teamColor = QColor(16776960);
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
   111
	team2.numHedgehogs = 4;
312
c36d0b34ac3d Use new parameters, small protocol change
unc0rr
parents: 306
diff changeset
   112
	RawSendIPC(HWProto::addStringListToBuffer(teamscfg,
341
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
   113
			team2.TeamGameConfig(gamecfg->getInitHealth())));
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
   114
}
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
   115
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
   116
void HWGame::SendNetConfig()
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
   117
{
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
   118
	commonConfig();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   119
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   120
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   121
void HWGame::ParseMessage(const QByteArray & msg)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   122
{
306
7b61834edcf6 - Better blowtorch
unc0rr
parents: 297
diff changeset
   123
	switch(msg.at(1)) {
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   124
		case '?': {
395
641ed71c7c93 - Fix net game bug
unc0rr
parents: 379
diff changeset
   125
			SendIPC("!");
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   126
			break;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   127
		}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   128
		case 'C': {
341
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
   129
			qDebug("ASK Config");
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   130
			switch (gameType) {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   131
				case gtLocal: {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   132
				 	SendConfig();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   133
					break;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   134
				}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   135
				case gtQLocal: {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   136
				 	SendQuickConfig();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   137
					break;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   138
				}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   139
				case gtDemo: break;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   140
				case gtNet: {
341
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
   141
					SendNetConfig();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   142
					break;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   143
				}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   144
			}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   145
			break;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   146
		}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   147
		case 'E': {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   148
			QMessageBox::critical(0,
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   149
					"Hedgewars: error message",
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   150
					QString().append(msg.mid(2)).left(msg.size() - 6),
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   151
					QMessageBox::Ok,
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   152
					QMessageBox::NoButton,
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   153
					QMessageBox::NoButton);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   154
			return;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   155
		}
208
a049157d673a Implement Knowledge Base for libs/compilers bugs
unc0rr
parents: 184
diff changeset
   156
		case 'K': {
a049157d673a Implement Knowledge Base for libs/compilers bugs
unc0rr
parents: 184
diff changeset
   157
			ulong kb = msg.mid(2).toULong();
a049157d673a Implement Knowledge Base for libs/compilers bugs
unc0rr
parents: 184
diff changeset
   158
			if (kb && kb <= KBmsgsCount)
a049157d673a Implement Knowledge Base for libs/compilers bugs
unc0rr
parents: 184
diff changeset
   159
			{
a049157d673a Implement Knowledge Base for libs/compilers bugs
unc0rr
parents: 184
diff changeset
   160
				QMessageBox::information(0,
a049157d673a Implement Knowledge Base for libs/compilers bugs
unc0rr
parents: 184
diff changeset
   161
						"Hedgewars: information",
a049157d673a Implement Knowledge Base for libs/compilers bugs
unc0rr
parents: 184
diff changeset
   162
						KBMessages[kb - 1],
a049157d673a Implement Knowledge Base for libs/compilers bugs
unc0rr
parents: 184
diff changeset
   163
						QMessageBox::Ok,
a049157d673a Implement Knowledge Base for libs/compilers bugs
unc0rr
parents: 184
diff changeset
   164
						QMessageBox::NoButton,
a049157d673a Implement Knowledge Base for libs/compilers bugs
unc0rr
parents: 184
diff changeset
   165
						QMessageBox::NoButton);
a049157d673a Implement Knowledge Base for libs/compilers bugs
unc0rr
parents: 184
diff changeset
   166
			}
a049157d673a Implement Knowledge Base for libs/compilers bugs
unc0rr
parents: 184
diff changeset
   167
			return;
a049157d673a Implement Knowledge Base for libs/compilers bugs
unc0rr
parents: 184
diff changeset
   168
		}
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   169
		case '+': {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   170
			if (gameType == gtNet)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   171
			{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   172
				emit SendNet(msg);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   173
			}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   174
			break;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   175
		}
306
7b61834edcf6 - Better blowtorch
unc0rr
parents: 297
diff changeset
   176
		case 'i': {
7b61834edcf6 - Better blowtorch
unc0rr
parents: 297
diff changeset
   177
			emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3)));
7b61834edcf6 - Better blowtorch
unc0rr
parents: 297
diff changeset
   178
			break;
7b61834edcf6 - Better blowtorch
unc0rr
parents: 297
diff changeset
   179
		}
324
f4c109c82a0c Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents: 318
diff changeset
   180
		case 'Q': {
f4c109c82a0c Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents: 318
diff changeset
   181
			emit GameStateChanged(gsInterrupted);
f4c109c82a0c Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents: 318
diff changeset
   182
			break;
f4c109c82a0c Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents: 318
diff changeset
   183
		}
f4c109c82a0c Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents: 318
diff changeset
   184
		case 'q': {
f4c109c82a0c Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents: 318
diff changeset
   185
			emit GameStateChanged(gsFinished);
f4c109c82a0c Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents: 318
diff changeset
   186
			break;
f4c109c82a0c Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents: 318
diff changeset
   187
		}
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   188
		default: {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   189
			if (gameType == gtNet)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   190
			{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   191
				emit SendNet(msg);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   192
			}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   193
			demo->append(msg);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   194
		}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   195
	}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   196
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   197
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   198
void HWGame::FromNet(const QByteArray & msg)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   199
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   200
	RawSendIPC(msg);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   201
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   202
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   203
void HWGame::onClientRead()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   204
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   205
	quint8 msglen;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   206
	quint32 bufsize;
406
ea90c6358054 segfault after endgame fixed?!
displacer
parents: 397
diff changeset
   207
	while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) &&
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   208
			((msglen = readbuffer.data()[0]) < bufsize))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   209
	{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   210
		QByteArray msg = readbuffer.left(msglen + 1);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   211
		readbuffer.remove(0, msglen + 1);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   212
		ParseMessage(msg);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   213
	}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   214
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   215
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   216
QStringList HWGame::setArguments()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   217
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   218
	QStringList arguments;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   219
	arguments << resolutions[0][config->vid_Resolution()];
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   220
	arguments << resolutions[1][config->vid_Resolution()];
296
607912ccc5af Implemented FPS limit and show/hide FPS value
unc0rr
parents: 291
diff changeset
   221
	arguments << "16"; // bpp
291
afa378f1e961 automatic assign of game server TCP port
displacer
parents: 271
diff changeset
   222
	arguments << QString("%1").arg(ipc_port);
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   223
	arguments << (config->vid_Fullscreen() ? "1" : "0");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   224
	arguments << (config->isSoundEnabled() ? "1" : "0");
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   225
	arguments << tr("en.txt");
296
607912ccc5af Implemented FPS limit and show/hide FPS value
unc0rr
parents: 291
diff changeset
   226
	arguments << "128"; // sound volume
297
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 296
diff changeset
   227
	arguments << QString::number(config->timerInterval());
267
bf7c1503f569 Engine gets Data path from frontend
unc0rr
parents: 253
diff changeset
   228
	arguments << datadir->absolutePath();
297
279e10ec31b4 'Show FPS' and 'FPS limit' options
unc0rr
parents: 296
diff changeset
   229
	arguments << (config->isShowFPSEnabled() ? "1" : "0");
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   230
	return arguments;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   231
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   232
341
184230eb4151 - Store more team specific values in HWTeam
unc0rr
parents: 339
diff changeset
   233
void HWGame::AddTeam(const QString & teamname)
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   234
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   235
	if (TeamCount == 5) return;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   236
	teams[TeamCount] = teamname;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   237
	TeamCount++;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   238
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   239
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   240
void HWGame::SaveDemo(const QString & filename)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   241
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   242
	demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD"));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   243
	demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD"));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   244
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   245
	QFile demofile(filename);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   246
	if (!demofile.open(QIODevice::WriteOnly))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   247
	{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   248
		QMessageBox::critical(0,
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   249
				tr("Error"),
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   250
				tr("Cannot save demo to file %1").arg(filename),
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   251
				tr("Quit"));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   252
		return ;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   253
	}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   254
	QDataStream stream(&demofile);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   255
	stream.writeRawData(demo->constData(), demo->size());
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   256
	demofile.close();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   257
	delete demo;
379
e4cd3cd209c6 additional error checking
displacer
parents: 352
diff changeset
   258
	demo=0;
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   259
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   260
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   261
void HWGame::PlayDemo(const QString & demofilename)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   262
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   263
	gameType = gtDemo;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   264
	QFile demofile(demofilename);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   265
	if (!demofile.open(QIODevice::ReadOnly))
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   266
	{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   267
		QMessageBox::critical(0,
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   268
				tr("Error"),
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   269
				tr("Cannot open demofile %1").arg(demofilename),
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   270
				tr("Quit"));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   271
		return ;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   272
	}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   273
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   274
	// read demo
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   275
	QDataStream stream(&demofile);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   276
	char buf[512];
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   277
	int readbytes;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   278
	do
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   279
	{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   280
		readbytes = stream.readRawData((char *)&buf, 512);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   281
		toSendBuf.append(QByteArray((char *)&buf, readbytes));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   282
		//SendIPC(QByteArray((char *)&buf, readbytes));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   283
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   284
	} while (readbytes > 0);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   285
	demofile.close();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   286
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   287
	// run engine
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   288
	demo = new QByteArray;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   289
	Start();
306
7b61834edcf6 - Better blowtorch
unc0rr
parents: 297
diff changeset
   290
	emit GameStateChanged(gsStarted);
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   291
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   292
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   293
void HWGame::StartNet()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   294
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   295
	gameType = gtNet;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   296
	demo = new QByteArray;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   297
	Start();
306
7b61834edcf6 - Better blowtorch
unc0rr
parents: 297
diff changeset
   298
	emit GameStateChanged(gsStarted);
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   299
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   300
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   301
void HWGame::StartLocal()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   302
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   303
	gameType = gtLocal;
239
f9adf3c73bed 'Proto' class to make things easier
unc0rr
parents: 228
diff changeset
   304
	seed = gamecfg->getCurrentSeed();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   305
	demo = new QByteArray;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   306
	Start();
306
7b61834edcf6 - Better blowtorch
unc0rr
parents: 297
diff changeset
   307
	emit GameStateChanged(gsStarted);
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   308
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   309
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   310
void HWGame::StartQuick()
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   311
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   312
	gameType = gtQLocal;
239
f9adf3c73bed 'Proto' class to make things easier
unc0rr
parents: 228
diff changeset
   313
	seed = gamecfg->getCurrentSeed();
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   314
	demo = new QByteArray;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   315
	Start();
306
7b61834edcf6 - Better blowtorch
unc0rr
parents: 297
diff changeset
   316
	emit GameStateChanged(gsStarted);
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   317
}