QTfrontend/game.cpp
author unc0rr
Tue, 06 Dec 2005 19:46:40 +0000
changeset 25 27aa8030322b
parent 21 dff476dcaaa3
child 26 e32fa14529f8
permissions -rw-r--r--
- Get rid of old frontend - additions to avematan theme - many work on frontend
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
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
     3
 * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
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>
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    39
#include <QTextStream>
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    40
#include <QFile>
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    41
#include "game.h"
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    42
#include "hwconsts.h"
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    43
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    44
HWGame::HWGame()
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    45
{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    46
	IPCServer = new QTcpServer(this);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    47
	IPCServer->setMaxPendingConnections(1);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    48
	if (!IPCServer->listen(QHostAddress("127.0.0.1"), IPC_PORT))
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    49
	{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    50
		QMessageBox::critical(this, tr("Error"),
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    51
				tr("Unable to start the server: %1.")
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    52
				.arg(IPCServer->errorString()));
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    53
	}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    54
	connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    55
	IPCSocket = 0;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    56
	TeamCount = 0;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    57
	seed = "seed";
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    58
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    59
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    60
void HWGame::NewConnection()
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    61
{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    62
	QTcpSocket * client = IPCServer->nextPendingConnection();
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    63
	if(!IPCSocket)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    64
	{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    65
		IPCSocket = client;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    66
		connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    67
		connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    68
		msgsize = 0;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    69
	} else
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    70
	{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    71
		client->disconnectFromHost();
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    72
		delete client;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    73
	}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    74
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    75
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    76
void HWGame::ClientDisconnect()
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    77
{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    78
	IPCSocket = 0;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    79
	SaveDemo("demo.hwd_1");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    80
	delete this;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    81
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    82
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    83
void HWGame::SendTeamConfig(int index)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    84
{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    85
	QFile teamcfg(teams[index]);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    86
	if (!teamcfg.open(QIODevice::ReadOnly))
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    87
	{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    88
		return ;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    89
	}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    90
	QTextStream stream(&teamcfg);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    91
	stream.setCodec("UTF-8");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    92
	QString str;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    93
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    94
	while (!stream.atEnd())
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    95
	{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    96
		str = stream.readLine();
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    97
		if (str.startsWith(";") || (str.length() > 254)) continue;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    98
		str.prepend("e");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
    99
		SendIPC(str.toLocal8Bit());
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   100
	}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   101
	teamcfg.close();
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   102
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   103
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   104
void HWGame::SendConfig()
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   105
{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   106
	SENDIPC("TL");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   107
	SENDIPC("e$gmflags 0");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   108
	SENDIPC("eaddteam");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   109
	SendTeamConfig(0);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   110
	SENDIPC("ecolor 65535");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   111
	SENDIPC("eadd hh0 0");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   112
	SENDIPC("eadd hh1 0");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   113
	SENDIPC("eadd hh2 0");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   114
	SENDIPC("eadd hh3 0");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   115
	SENDIPC("eaddteam");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   116
	SendTeamConfig(1);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   117
	SENDIPC("ecolor 16776960");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   118
	SENDIPC("eadd hh0 1");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   119
	SENDIPC("eadd hh1 1");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   120
	SENDIPC("eadd hh2 1");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   121
	SENDIPC("eadd hh3 1");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   122
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   123
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   124
void HWGame::ParseMessage()
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   125
{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   126
	switch(msgbuf[0])
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   127
	{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   128
		case '?':
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   129
		{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   130
			SENDIPC("!");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   131
			break;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   132
		}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   133
		case 'C':
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   134
		{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   135
			SendConfig();
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   136
			break;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   137
		}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   138
		case '+':
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   139
		{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   140
			break;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   141
		}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   142
		default:
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   143
		{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   144
			demo->append(msgsize);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   145
			demo->append(QByteArray::fromRawData(msgbuf, msgsize));
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   146
		}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   147
	}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   148
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   149
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   150
void HWGame::SendIPC(const char * msg, quint8 len)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   151
{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   152
	IPCSocket->write((char *)&len, 1);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   153
	IPCSocket->write(msg, len);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   154
	if ((len > 5) && !((msg[0] == 'e') && (msg[1] == 'b')))
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   155
	{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   156
		demo->append(len);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   157
		demo->append(QByteArray::fromRawData(msg, len));
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
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   161
void HWGame::SendIPC(const QByteArray & buf)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   162
{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   163
	if (buf.size() > 255) return;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   164
	quint8 len = buf.size();
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   165
	IPCSocket->write((char *)&len, 1);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   166
	IPCSocket->write(buf);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   167
	demo->append(len);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   168
	demo->append(buf);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   169
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   170
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   171
void HWGame::ClientRead()
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   172
{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   173
	qint64 readbytes = 1;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   174
	while (readbytes > 0)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   175
	{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   176
		if (msgsize == 0)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   177
		{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   178
			msgbufsize = 0;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   179
			readbytes = IPCSocket->read((char *)&msgsize, 1);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   180
		} else
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   181
		{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   182
			msgbufsize +=
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   183
			readbytes = IPCSocket->read((char *)&msgbuf[msgbufsize], msgsize - msgbufsize);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   184
			if (msgbufsize = msgsize)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   185
			{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   186
				ParseMessage();
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   187
				msgsize = 0;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   188
			}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   189
		}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   190
	}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   191
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   192
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   193
void HWGame::Start(int Resolution, bool Fullscreen)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   194
{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   195
	if (TeamCount < 2) return;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   196
	QProcess * process;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   197
	QStringList arguments;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   198
	seedgen.GenRNDStr(seed, 10);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   199
	process = new QProcess;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   200
	arguments << resolutions[0][Resolution];
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   201
	arguments << resolutions[1][Resolution];
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   202
	arguments << GetThemeBySeed();
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   203
	arguments << "46631";
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   204
	arguments << seed;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   205
	arguments << (Fullscreen ? "1" : "0");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   206
	process->start("hw", arguments);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   207
	demo = new QByteArray;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   208
	demo->append(seed.toLocal8Bit());
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   209
	demo->append(10);
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
void HWGame::AddTeam(const QString & teamname)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   213
{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   214
	if (TeamCount == 5) return;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   215
	teams[TeamCount] = teamname;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   216
	TeamCount++;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   217
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   218
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   219
QString HWGame::GetThemeBySeed()
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   220
{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   221
	QFile themesfile("Data/Themes/themes.cfg");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   222
	QStringList themes;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   223
	if (themesfile.open(QIODevice::ReadOnly))
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   224
	{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   225
		QTextStream stream(&themesfile);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   226
		QString str;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   227
		while (!stream.atEnd())
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   228
		{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   229
			themes << stream.readLine();
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   230
		}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   231
		themesfile.close();
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   232
	}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   233
	quint32 len = themes.size();
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   234
	if (len == 0)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   235
	{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   236
		QMessageBox::critical(this, "Error", "Cannot access themes.cfg or bad data", "OK");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   237
		return "avematan";
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   238
	}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   239
	if (seed.isEmpty())
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   240
	{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   241
		QMessageBox::critical(this, "Error", "seed not defined", "OK");
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   242
		return "avematan";
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   243
	}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   244
	quint32 k = 0;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   245
	for (int i = 0; i < seed.length(); i++)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   246
	{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   247
		k += seed[i].unicode();
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   248
	}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   249
	return themes[k % len];
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   250
}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   251
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   252
void HWGame::SaveDemo(const QString & filename)
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   253
{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   254
	QFile demofile(filename);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   255
	if (!demofile.open(QIODevice::WriteOnly))
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   256
	{
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   257
		QMessageBox::critical(this,
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   258
				tr("Error"),
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   259
				tr("Cannot save demo to file %s").arg(filename),
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   260
				tr("Quit"));
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   261
		return ;
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   262
	}
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   263
	QDataStream stream(&demofile);
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   264
	stream.writeRawData(demo->constData(), demo->size());
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   265
	demofile.close();
27aa8030322b - Get rid of old frontend
unc0rr
parents: 21
diff changeset
   266
}