QTfrontend/game.cpp
changeset 26 e32fa14529f8
parent 25 27aa8030322b
child 28 59f7db859b8a
equal deleted inserted replaced
25:27aa8030322b 26:e32fa14529f8
    39 #include <QTextStream>
    39 #include <QTextStream>
    40 #include <QFile>
    40 #include <QFile>
    41 #include "game.h"
    41 #include "game.h"
    42 #include "hwconsts.h"
    42 #include "hwconsts.h"
    43 
    43 
    44 HWGame::HWGame()
    44 HWGame::HWGame(int Resolution, bool Fullscreen)
    45 {
    45 {
       
    46 	vid_Resolution = Resolution;
       
    47 	vid_Fullscreen = Fullscreen;
    46 	IPCServer = new QTcpServer(this);
    48 	IPCServer = new QTcpServer(this);
    47 	IPCServer->setMaxPendingConnections(1);
    49 	IPCServer->setMaxPendingConnections(1);
    48 	if (!IPCServer->listen(QHostAddress("127.0.0.1"), IPC_PORT))
    50 	if (!IPCServer->listen(QHostAddress("127.0.0.1"), IPC_PORT))
    49 	{
    51 	{
    50 		QMessageBox::critical(this, tr("Error"),
    52 		QMessageBox::critical(this, tr("Error"),
    52 				.arg(IPCServer->errorString()));
    54 				.arg(IPCServer->errorString()));
    53 	}
    55 	}
    54 	connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
    56 	connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
    55 	IPCSocket = 0;
    57 	IPCSocket = 0;
    56 	TeamCount = 0;
    58 	TeamCount = 0;
    57 	seed = "seed";
    59 	seed = "";
    58 }
    60 }
    59 
    61 
    60 void HWGame::NewConnection()
    62 void HWGame::NewConnection()
    61 {
    63 {
    62 	QTcpSocket * client = IPCServer->nextPendingConnection();
    64 	QTcpSocket * client = IPCServer->nextPendingConnection();
   101 	teamcfg.close();
   103 	teamcfg.close();
   102 }
   104 }
   103 
   105 
   104 void HWGame::SendConfig()
   106 void HWGame::SendConfig()
   105 {
   107 {
       
   108 	if (gameType == gtDemo)
       
   109 	{
       
   110 		SENDIPC("TD");
       
   111 		SendIPCRaw(toSendBuf->constData(), toSendBuf->size());
       
   112 		delete toSendBuf;
       
   113 		return ;
       
   114 	}
   106 	SENDIPC("TL");
   115 	SENDIPC("TL");
   107 	SENDIPC("e$gmflags 0");
   116 	SENDIPC("e$gmflags 0");
   108 	SENDIPC("eaddteam");
   117 	SENDIPC("eaddteam");
   109 	SendTeamConfig(0);
   118 	SendTeamConfig(0);
   110 	SENDIPC("ecolor 65535");
   119 	SENDIPC("ecolor 65535");
   166 	IPCSocket->write(buf);
   175 	IPCSocket->write(buf);
   167 	demo->append(len);
   176 	demo->append(len);
   168 	demo->append(buf);
   177 	demo->append(buf);
   169 }
   178 }
   170 
   179 
       
   180 void HWGame::SendIPCRaw(const char * msg, quint32 len)
       
   181 {
       
   182 	IPCSocket->write(msg, len);
       
   183 	demo->append(QByteArray::fromRawData(msg, len));
       
   184 }
       
   185 
   171 void HWGame::ClientRead()
   186 void HWGame::ClientRead()
   172 {
   187 {
   173 	qint64 readbytes = 1;
   188 	qint64 readbytes = 1;
   174 	while (readbytes > 0)
   189 	while (readbytes > 0)
   175 	{
   190 	{
   188 			}
   203 			}
   189 		}
   204 		}
   190 	}
   205 	}
   191 }
   206 }
   192 
   207 
   193 void HWGame::Start(int Resolution, bool Fullscreen)
   208 void HWGame::Start()
   194 {
   209 {
       
   210 	gameType = gtLocal;
   195 	if (TeamCount < 2) return;
   211 	if (TeamCount < 2) return;
   196 	QProcess * process;
   212 	QProcess * process;
   197 	QStringList arguments;
   213 	QStringList arguments;
   198 	seedgen.GenRNDStr(seed, 10);
   214 	seedgen.GenRNDStr(seed, 10);
   199 	process = new QProcess;
   215 	process = new QProcess;
   200 	arguments << resolutions[0][Resolution];
   216 	arguments << resolutions[0][vid_Resolution];
   201 	arguments << resolutions[1][Resolution];
   217 	arguments << resolutions[1][vid_Resolution];
   202 	arguments << GetThemeBySeed();
   218 	arguments << GetThemeBySeed();
   203 	arguments << "46631";
   219 	arguments << "46631";
   204 	arguments << seed;
   220 	arguments << seed;
   205 	arguments << (Fullscreen ? "1" : "0");
   221 	arguments << (vid_Fullscreen ? "1" : "0");
   206 	process->start("hw", arguments);
   222 	process->start("hw", arguments);
   207 	demo = new QByteArray;
   223 	demo = new QByteArray;
   208 	demo->append(seed.toLocal8Bit());
   224 	demo->append(seed.toLocal8Bit());
   209 	demo->append(10);
   225 	demo->append(10);
   210 }
   226 }
   216 	TeamCount++;
   232 	TeamCount++;
   217 }
   233 }
   218 
   234 
   219 QString HWGame::GetThemeBySeed()
   235 QString HWGame::GetThemeBySeed()
   220 {
   236 {
   221 	QFile themesfile("Data/Themes/themes.cfg");
   237 	QFile themesfile(QString(DATA_PATH) + "/Themes/themes.cfg");
   222 	QStringList themes;
   238 	QStringList themes;
   223 	if (themesfile.open(QIODevice::ReadOnly))
   239 	if (themesfile.open(QIODevice::ReadOnly))
   224 	{
   240 	{
   225 		QTextStream stream(&themesfile);
   241 		QTextStream stream(&themesfile);
   226 		QString str;
   242 		QString str;
   261 		return ;
   277 		return ;
   262 	}
   278 	}
   263 	QDataStream stream(&demofile);
   279 	QDataStream stream(&demofile);
   264 	stream.writeRawData(demo->constData(), demo->size());
   280 	stream.writeRawData(demo->constData(), demo->size());
   265 	demofile.close();
   281 	demofile.close();
   266 }
   282 	delete demo;
       
   283 }
       
   284 
       
   285 void HWGame::PlayDemo(const QString & demofilename)
       
   286 {
       
   287 	gameType = gtDemo;
       
   288 	QFile demofile(demofilename);
       
   289 	if (!demofile.open(QIODevice::ReadOnly))
       
   290 	{
       
   291 		QMessageBox::critical(this,
       
   292 				tr("Error"),
       
   293 				tr("Cannot open demofile %s").arg(demofilename),
       
   294 				tr("Quit"));
       
   295 		return ;
       
   296 	}
       
   297 
       
   298 	// read demo
       
   299 	QDataStream stream(&demofile);
       
   300 	toSendBuf = new QByteArray();
       
   301 	char buf[512];
       
   302 	quint32 readbytes;
       
   303 	do
       
   304 	{
       
   305 		readbytes = stream.readRawData((char *)&buf, 512);
       
   306 		toSendBuf -> append(QByteArray((char *)&buf, readbytes));
       
   307 
       
   308 	} while (readbytes > 0);
       
   309 	demofile.close();
       
   310 
       
   311 	// cut seed
       
   312 	quint32 index = toSendBuf->indexOf(10);
       
   313 	if (!index)
       
   314 	{
       
   315 		QMessageBox::critical(this,
       
   316 				tr("Error"),
       
   317 				tr("Corrupted demo file %s").arg(demofilename),
       
   318 				tr("Quit"));
       
   319 		return ;
       
   320 	}
       
   321 	seed = QString(toSendBuf->left(index++));
       
   322 	toSendBuf->remove(0, index);
       
   323 
       
   324 	// run engine
       
   325 	QProcess * process;
       
   326 	QStringList arguments;
       
   327 	process = new QProcess;
       
   328 	arguments << resolutions[0][vid_Resolution];
       
   329 	arguments << resolutions[1][vid_Resolution];
       
   330 	arguments << GetThemeBySeed();
       
   331 	arguments << "46631";
       
   332 	arguments << seed;
       
   333 	arguments << (vid_Fullscreen ? "1" : "0");
       
   334 	process->start("hw", arguments);
       
   335 	demo = new QByteArray;
       
   336 }