QTfrontend/game.cpp
author displacer
Sun, 01 Oct 2006 20:25:03 +0000
changeset 178 efdc2a63be8e
parent 177 c67c15e6fae3
child 180 ea83b9e9057f
permissions -rw-r--r--
united TCP transfers for start game and receive preview
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
     1
/*
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
     2
 * Hedgewars, a worms-like game
128
223522298250 Update file headers & delete unneeded files
unc0rr
parents: 127
diff changeset
     3
 * Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com>
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
     4
 *
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
     5
 * Distributed under the terms of the BSD-modified licence:
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
     6
 *
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
     7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
     8
 * of this software and associated documentation files (the "Software"), to deal
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
     9
 * with the Software without restriction, including without limitation the
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    11
 * sell copies of the Software, and to permit persons to whom the Software is
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    12
 * furnished to do so, subject to the following conditions:
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    13
 *
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    14
 * 1. Redistributions of source code must retain the above copyright notice,
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    15
 *    this list of conditions and the following disclaimer.
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    17
 *    this list of conditions and the following disclaimer in the documentation
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    18
 *    and/or other materials provided with the distribution.
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    19
 * 3. The name of the author may not be used to endorse or promote products
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    20
 *    derived from this software without specific prior written permission.
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    21
 *
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    25
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    28
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    29
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    30
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    31
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    32
 */
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    33
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    34
#include <QMessageBox>
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    35
#include <QProcess>
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    36
#include <QTimer>
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    37
#include <QString>
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    38
#include <QByteArray>
86
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
    39
#include <QFile>
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    40
#include <QTextStream>
127
ca70467bd0a8 Use QUuid to generate seed, rand() to choose theme
unc0rr
parents: 116
diff changeset
    41
#include <QUuid>
86
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
    42
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    43
#include "game.h"
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    44
#include "hwconsts.h"
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
    45
#include "gameuiconfig.h"
87
ff213e443336 Convert all pages to classes
unc0rr
parents: 86
diff changeset
    46
#include "gamecfgwidget.h"
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    47
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
    48
HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg)
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    49
{
84
0f6669da2fcb Start rewrite ui
unc0rr
parents: 80
diff changeset
    50
	this->config = config;
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
    51
	this->gamecfg = gamecfg;
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    52
	TeamCount = 0;
26
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
    53
	seed = "";
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    54
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    55
178
efdc2a63be8e united TCP transfers for start game and receive preview
displacer
parents: 177
diff changeset
    56
void HWGame::onClientDisconnect()
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    57
{
157
8e3e11bb0a5f Fix demos usability
unc0rr
parents: 149
diff changeset
    58
	SaveDemo(cfgdir->absolutePath() + "/Demos/LastRound.hwd_1");
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    59
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    60
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    61
void HWGame::SendTeamConfig(int index)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    62
{
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
    63
	LocalCFG(teams[index]);
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    64
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    65
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    66
void HWGame::SendConfig()
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    67
{
55
e09f7c952a40 Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents: 52
diff changeset
    68
	SendIPC(QString("eseed %1").arg(seed));
127
ca70467bd0a8 Use QUuid to generate seed, rand() to choose theme
unc0rr
parents: 116
diff changeset
    69
	SendIPC(QString("etheme %1").arg(config->GetRandomTheme()));
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    70
	SENDIPC("TL");
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
    71
	SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()));
141
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    72
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    73
	for (int i = 0; i < TeamCount; i++)
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    74
	{
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    75
		SENDIPC("eaddteam");
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    76
		LocalCFG(teams[i]);
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    77
		SendIPC(QString("ecolor %1").arg(65535 << i * 8));
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    78
		for (int t = 0; t < hdNum[teams[i]]; t++)
149
aae256899c49 Fix hedgehog physics %)
unc0rr
parents: 148
diff changeset
    79
			SendIPC(QString("eadd hh%1 0").arg(t));
141
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    80
	}
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    81
}
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    82
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    83
void HWGame::SendQuickConfig()
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    84
{
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    85
	SendIPC(QString("eseed %1").arg(seed));
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    86
	SendIPC(QString("etheme %1").arg(config->GetRandomTheme()));
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    87
	SENDIPC("TL");
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
    88
	SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()));
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    89
	SENDIPC("eaddteam");
86
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
    90
	LocalCFG(0);
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    91
	SENDIPC("ecolor 65535");
86
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
    92
	SENDIPC("eadd hh0 0");
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
    93
	SENDIPC("eadd hh1 0");
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
    94
	SENDIPC("eadd hh2 0");
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
    95
	SENDIPC("eadd hh3 0");
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    96
	SENDIPC("eaddteam");
97
e7c1df9cce2c - make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents: 87
diff changeset
    97
	LocalCFG(2);
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    98
	SENDIPC("ecolor 16776960");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    99
	SENDIPC("eadd hh0 1");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   100
	SENDIPC("eadd hh1 1");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   101
	SENDIPC("eadd hh2 1");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   102
	SENDIPC("eadd hh3 1");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   103
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   104
43
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   105
void HWGame::ParseMessage(const QByteArray & msg)
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   106
{
141
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   107
	switch(msg.data()[1]) {
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   108
		case '?': {
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   109
			if (gameType == gtNet)
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   110
				emit SendNet(QByteArray("\x01""?"));
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   111
			else
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   112
				SENDIPC("!");
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   113
			break;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   114
		}
141
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   115
		case 'C': {
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   116
			switch (gameType) {
142
1ff0b02fe367 fix 'Simple game' and playing demos
unc0rr
parents: 141
diff changeset
   117
				case gtLocal: {
1ff0b02fe367 fix 'Simple game' and playing demos
unc0rr
parents: 141
diff changeset
   118
				 	SendConfig();
1ff0b02fe367 fix 'Simple game' and playing demos
unc0rr
parents: 141
diff changeset
   119
					break;
1ff0b02fe367 fix 'Simple game' and playing demos
unc0rr
parents: 141
diff changeset
   120
				}
141
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   121
				case gtQLocal: {
142
1ff0b02fe367 fix 'Simple game' and playing demos
unc0rr
parents: 141
diff changeset
   122
				 	SendQuickConfig();
141
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   123
					break;
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   124
				}
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   125
				case gtDemo: break;
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   126
				case gtNet: {
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   127
					SENDIPC("TN");
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   128
					emit SendNet(QByteArray("\x01""C"));
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   129
					break;
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   130
				}
32
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   131
			}
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   132
			break;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   133
		}
141
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   134
		case 'E': {
43
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   135
			QMessageBox::critical(0,
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   136
					"Hedgewars: error message",
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   137
					QString().append(msg.mid(2)).left(msg.size() - 6),
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   138
					QMessageBox::Ok,
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   139
					QMessageBox::NoButton,
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   140
					QMessageBox::NoButton);
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   141
			return;
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   142
		}
141
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   143
		case '+': {
36
a803a00a3272 - Anti-flood and anti-lag changes
unc0rr
parents: 32
diff changeset
   144
			if (gameType == gtNet)
a803a00a3272 - Anti-flood and anti-lag changes
unc0rr
parents: 32
diff changeset
   145
			{
43
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   146
				emit SendNet(msg);
36
a803a00a3272 - Anti-flood and anti-lag changes
unc0rr
parents: 32
diff changeset
   147
			}
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   148
			break;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   149
		}
141
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   150
		default: {
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   151
			if (gameType == gtNet)
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   152
			{
43
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   153
				emit SendNet(msg);
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   154
			}
43
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   155
			demo->append(msg);
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   156
		}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   157
	}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   158
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   159
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   160
void HWGame::SendIPC(const char * msg, quint8 len)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   161
{
32
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   162
	SendIPC(QByteArray::fromRawData(msg, len));
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   163
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   164
55
e09f7c952a40 Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents: 52
diff changeset
   165
void HWGame::SendIPC(const QString & buf)
e09f7c952a40 Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents: 52
diff changeset
   166
{
e09f7c952a40 Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents: 52
diff changeset
   167
	SendIPC(QByteArray().append(buf));
e09f7c952a40 Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents: 52
diff changeset
   168
}
e09f7c952a40 Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents: 52
diff changeset
   169
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   170
void HWGame::SendIPC(const QByteArray & buf)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   171
{
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents: 26
diff changeset
   172
	if (buf.size() > MAXMSGCHARS) return;
40
416d54ce0c34 - More binds
unc0rr
parents: 36
diff changeset
   173
	quint8 len = buf.size();
416d54ce0c34 - More binds
unc0rr
parents: 36
diff changeset
   174
	RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf);
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   175
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   176
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   177
void HWGame::RawSendIPC(const QByteArray & buf)
26
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   178
{
32
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   179
	if (!IPCSocket)
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   180
	{
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   181
		toSendBuf += buf;
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   182
	} else
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   183
	{
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   184
		if (toSendBuf.size() > 0)
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   185
		{
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   186
			IPCSocket->write(toSendBuf);
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   187
			demo->append(toSendBuf);
40
416d54ce0c34 - More binds
unc0rr
parents: 36
diff changeset
   188
			toSendBuf.clear();
32
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   189
		}
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   190
		IPCSocket->write(buf);
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   191
		demo->append(buf);
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   192
	}
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   193
}
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   194
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   195
void HWGame::FromNet(const QByteArray & msg)
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   196
{
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   197
	RawSendIPC(msg);
26
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   198
}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   199
177
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 174
diff changeset
   200
void HWGame::onClientRead()
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 174
diff changeset
   201
{
43
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   202
	quint8 msglen;
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   203
	quint32 bufsize;
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   204
	while (((bufsize = readbuffer.size()) > 0) &&
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   205
			((msglen = readbuffer.data()[0]) < bufsize))
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   206
	{
43
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   207
		QByteArray msg = readbuffer.left(msglen + 1);
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   208
		readbuffer.remove(0, msglen + 1);
e297fea1a2f3 - Removed dark pixels on the corners of health case
unc0rr
parents: 41
diff changeset
   209
		ParseMessage(msg);
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   210
	}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   211
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   212
177
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 174
diff changeset
   213
QStringList HWGame::setArguments()
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 174
diff changeset
   214
{
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 174
diff changeset
   215
	QStringList arguments;
84
0f6669da2fcb Start rewrite ui
unc0rr
parents: 80
diff changeset
   216
	arguments << resolutions[0][config->vid_Resolution()];
0f6669da2fcb Start rewrite ui
unc0rr
parents: 80
diff changeset
   217
	arguments << resolutions[1][config->vid_Resolution()];
55
e09f7c952a40 Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents: 52
diff changeset
   218
	arguments << "16";
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   219
	arguments << "46631";
84
0f6669da2fcb Start rewrite ui
unc0rr
parents: 80
diff changeset
   220
	arguments << (config->vid_Fullscreen() ? "1" : "0");
0f6669da2fcb Start rewrite ui
unc0rr
parents: 80
diff changeset
   221
	arguments << (config->isSoundEnabled() ? "1" : "0");
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 74
diff changeset
   222
	arguments << tr("en.txt");
174
0b2c5b22f644 Changeable volume
unc0rr
parents: 172
diff changeset
   223
	arguments << "128";
177
c67c15e6fae3 prepare uniting tcp classes
displacer
parents: 174
diff changeset
   224
	return arguments;
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   225
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   226
140
50ccde437ea1 teams and hedgedogs num selection added to HWgame
displacer
parents: 128
diff changeset
   227
void HWGame::AddTeam(const QString & teamname, unsigned char numHedgedogs)
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   228
{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   229
	if (TeamCount == 5) return;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   230
	teams[TeamCount] = teamname;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   231
	TeamCount++;
140
50ccde437ea1 teams and hedgedogs num selection added to HWgame
displacer
parents: 128
diff changeset
   232
	hdNum[teamname]=numHedgedogs;
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   233
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   234
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   235
void HWGame::SaveDemo(const QString & filename)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   236
{
157
8e3e11bb0a5f Fix demos usability
unc0rr
parents: 149
diff changeset
   237
	demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD"));
8e3e11bb0a5f Fix demos usability
unc0rr
parents: 149
diff changeset
   238
	demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD"));
8e3e11bb0a5f Fix demos usability
unc0rr
parents: 149
diff changeset
   239
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   240
	QFile demofile(filename);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   241
	if (!demofile.open(QIODevice::WriteOnly))
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   242
	{
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents: 26
diff changeset
   243
		QMessageBox::critical(0,
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   244
				tr("Error"),
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 74
diff changeset
   245
				tr("Cannot save demo to file %1").arg(filename),
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   246
				tr("Quit"));
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   247
		return ;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   248
	}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   249
	QDataStream stream(&demofile);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   250
	stream.writeRawData(demo->constData(), demo->size());
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   251
	demofile.close();
26
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   252
	delete demo;
25
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   253
}
26
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   254
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   255
void HWGame::PlayDemo(const QString & demofilename)
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   256
{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   257
	gameType = gtDemo;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   258
	QFile demofile(demofilename);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   259
	if (!demofile.open(QIODevice::ReadOnly))
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   260
	{
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents: 26
diff changeset
   261
		QMessageBox::critical(0,
26
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   262
				tr("Error"),
80
3c3dc6a148ca - Fixed bug with hedgehog under water using rope
unc0rr
parents: 74
diff changeset
   263
				tr("Cannot open demofile %1").arg(demofilename),
26
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   264
				tr("Quit"));
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   265
		return ;
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   266
	}
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   267
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   268
	// read demo
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   269
	QDataStream stream(&demofile);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   270
	char buf[512];
142
1ff0b02fe367 fix 'Simple game' and playing demos
unc0rr
parents: 141
diff changeset
   271
	int readbytes;
26
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   272
	do
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   273
	{
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   274
		readbytes = stream.readRawData((char *)&buf, 512);
32
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   275
		toSendBuf.append(QByteArray((char *)&buf, readbytes));
26
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   276
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   277
	} while (readbytes > 0);
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   278
	demofile.close();
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   279
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   280
	// run engine
178
efdc2a63be8e united TCP transfers for start game and receive preview
displacer
parents: 177
diff changeset
   281
	demo = new QByteArray;
32
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   282
	Start();
26
e32fa14529f8 Frontend lets define more binds
unc0rr
parents: 25
diff changeset
   283
}
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   284
97
e7c1df9cce2c - make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents: 87
diff changeset
   285
void HWGame::StartNet()
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   286
{
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   287
	gameType = gtNet;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   288
	demo = new QByteArray;
32
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   289
	Start();
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   290
}
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   291
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   292
void HWGame::StartLocal()
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   293
{
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   294
	gameType = gtLocal;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   295
	if (TeamCount < 2) return;
172
5294ada3910b working map preview
displacer
parents: 157
diff changeset
   296
	seed = gamecfg->getCurrentSeed();//QUuid::createUuid().toString();
178
efdc2a63be8e united TCP transfers for start game and receive preview
displacer
parents: 177
diff changeset
   297
	demo = new QByteArray;
32
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
   298
	Start();
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   299
}
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   300
86
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   301
void HWGame::StartQuick()
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   302
{
141
ac3680be1f4b - Small fix for AI
unc0rr
parents: 140
diff changeset
   303
	gameType = gtQLocal;
172
5294ada3910b working map preview
displacer
parents: 157
diff changeset
   304
	seed = gamecfg->getCurrentSeed();//QUuid::createUuid().toString();
178
efdc2a63be8e united TCP transfers for start game and receive preview
displacer
parents: 177
diff changeset
   305
	demo = new QByteArray;
86
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   306
	Start();
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   307
}
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   308
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   309
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   310
void HWGame::LocalCFG(const QString & teamname)
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   311
{
116
00d3d6d2e699 GameUIConfig* member removed
displacer
parents: 110
diff changeset
   312
	HWTeam team(teamname);
86
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   313
	if (!team.LoadFromFile()) {
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   314
		QMessageBox::critical(0,
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   315
				"Error",
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   316
				QString("Cannot load team config ""%1""").arg(teamname),
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   317
				QMessageBox::Ok,
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   318
				QMessageBox::NoButton,
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   319
				QMessageBox::NoButton);
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   320
		return;
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   321
	}
86
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   322
	RawSendIPC(team.IPCTeamInfo());
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   323
}
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   324
86
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   325
void HWGame::LocalCFG(quint8 num)
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   326
{
116
00d3d6d2e699 GameUIConfig* member removed
displacer
parents: 110
diff changeset
   327
	HWTeam team(num);
86
664b536a1c27 Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents: 85
diff changeset
   328
	RawSendIPC(team.IPCTeamInfo());
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 28
diff changeset
   329
}