QTfrontend/game.cpp
changeset 31 99888245a4e8
parent 28 59f7db859b8a
child 32 78bff13b11c0
equal deleted inserted replaced
30:794e98e11b66 31:99888245a4e8
    55 	}
    55 	}
    56 	connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
    56 	connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
    57 	IPCSocket = 0;
    57 	IPCSocket = 0;
    58 	TeamCount = 0;
    58 	TeamCount = 0;
    59 	seed = "";
    59 	seed = "";
       
    60 	cfgdir.setPath(cfgdir.homePath());
       
    61 	cfgdir.cd(".hedgewars");
    60 }
    62 }
    61 
    63 
    62 void HWGame::NewConnection()
    64 void HWGame::NewConnection()
    63 {
    65 {
    64 	QTcpSocket * client = IPCServer->nextPendingConnection();
    66 	QTcpSocket * client = IPCServer->nextPendingConnection();
    82 	delete this;
    84 	delete this;
    83 }
    85 }
    84 
    86 
    85 void HWGame::SendTeamConfig(int index)
    87 void HWGame::SendTeamConfig(int index)
    86 {
    88 {
    87 	QFile teamcfg(teams[index]);
    89 	LocalCFG(teams[index]);
    88 	if (!teamcfg.open(QIODevice::ReadOnly))
       
    89 	{
       
    90 		return ;
       
    91 	}
       
    92 	QTextStream stream(&teamcfg);
       
    93 	stream.setCodec("UTF-8");
       
    94 	QString str;
       
    95 
       
    96 	while (!stream.atEnd())
       
    97 	{
       
    98 		str = stream.readLine();
       
    99 		if (str.startsWith(";") || (str.length() > 254)) continue;
       
   100 		str.prepend("e");
       
   101 		SendIPC(str.toLocal8Bit());
       
   102 	}
       
   103 	teamcfg.close();
       
   104 }
    90 }
   105 
    91 
   106 void HWGame::SendConfig()
    92 void HWGame::SendConfig()
   107 {
    93 {
   108 	if (gameType == gtDemo)
    94 	if (gameType == gtDemo)
   109 	{
    95 	{
   110 		SENDIPC("TD");
    96 		SENDIPC("TD");
   111 		SendIPCRaw(toSendBuf->constData(), toSendBuf->size());
    97 		RawSendIPC(*toSendBuf);
   112 		delete toSendBuf;
    98 		delete toSendBuf;
   113 		return ;
    99 		return ;
   114 	}
   100 	}
   115 	SENDIPC("TL");
   101 	SENDIPC("TL");
   116 	SENDIPC("e$gmflags 0");
   102 //	SENDIPC("e$gmflags 0");
   117 	SENDIPC("eaddteam");
   103 	SENDIPC("eaddteam");
   118 	SendTeamConfig(0);
   104 	SendTeamConfig(0);
       
   105 //	if () SENDIPC("rdriven");
   119 	SENDIPC("ecolor 65535");
   106 	SENDIPC("ecolor 65535");
   120 	SENDIPC("eadd hh0 0");
   107 	SENDIPC("eadd hh0 0");
   121 	SENDIPC("eadd hh1 0");
   108 	SENDIPC("eadd hh1 0");
   122 	SENDIPC("eadd hh2 0");
   109 	SENDIPC("eadd hh2 0");
   123 	SENDIPC("eadd hh3 0");
   110 	SENDIPC("eadd hh3 0");
   136 {
   123 {
   137 	switch(msgbuf[0])
   124 	switch(msgbuf[0])
   138 	{
   125 	{
   139 		case '?':
   126 		case '?':
   140 		{
   127 		{
   141 			SENDIPC("!");
   128 			if (gameType == gtNet)
       
   129 				emit SendNet(QByteArray("\x01""?"));
       
   130 			else
       
   131 				SENDIPC("!");
   142 			break;
   132 			break;
   143 		}
   133 		}
   144 		case 'C':
   134 		case 'C':
   145 		{
   135 		{
   146 			SendConfig();
   136 			if (gameType == gtNet)
       
   137 			{
       
   138 				SENDIPC("TN");
       
   139 				emit SendNet(QByteArray("\x01""C"));
       
   140 			}
       
   141 			else
       
   142 				SendConfig();
   147 			break;
   143 			break;
   148 		}
   144 		}
   149 		case '+':
   145 		case '+':
   150 		{
   146 		{
   151 			break;
   147 			break;
   152 		}
   148 		}
   153 		default:
   149 		default:
   154 		{
   150 		{
       
   151 			if (gameType == gtNet)
       
   152 			{
       
   153 				emit SendNet(QByteArray::fromRawData(msgbuf, msgsize));
       
   154 			}
   155 			demo->append(msgsize);
   155 			demo->append(msgsize);
   156 			demo->append(QByteArray::fromRawData(msgbuf, msgsize));
   156 			demo->append(QByteArray::fromRawData(msgbuf, msgsize));
   157 		}
   157 		}
   158 	}
   158 	}
   159 }
   159 }
   177 	IPCSocket->write(buf);
   177 	IPCSocket->write(buf);
   178 	demo->append(len);
   178 	demo->append(len);
   179 	demo->append(buf);
   179 	demo->append(buf);
   180 }
   180 }
   181 
   181 
   182 void HWGame::SendIPCRaw(const char * msg, quint32 len)
   182 void HWGame::RawSendIPC(const QByteArray & buf)
   183 {
   183 {
   184 	IPCSocket->write(msg, len);
   184 	IPCSocket->write(buf);
   185 	demo->append(QByteArray::fromRawData(msg, len));
   185 	demo->append(buf);
       
   186 }
       
   187 
       
   188 void HWGame::FromNet(const QByteArray & msg)
       
   189 {
       
   190 	// ?local config?
       
   191 	RawSendIPC(msg);
   186 }
   192 }
   187 
   193 
   188 void HWGame::ClientRead()
   194 void HWGame::ClientRead()
   189 {
   195 {
   190 	qint64 readbytes = 1;
   196 	qint64 readbytes = 1;
   207 	}
   213 	}
   208 }
   214 }
   209 
   215 
   210 void HWGame::Start()
   216 void HWGame::Start()
   211 {
   217 {
   212 	gameType = gtLocal;
       
   213 	if (TeamCount < 2) return;
       
   214 	QProcess * process;
   218 	QProcess * process;
   215 	QStringList arguments;
   219 	QStringList arguments;
   216 	seedgen.GenRNDStr(seed, 10);
       
   217 	process = new QProcess;
   220 	process = new QProcess;
   218 	arguments << resolutions[0][vid_Resolution];
   221 	arguments << resolutions[0][vid_Resolution];
   219 	arguments << resolutions[1][vid_Resolution];
   222 	arguments << resolutions[1][vid_Resolution];
   220 	arguments << GetThemeBySeed();
   223 	arguments << GetThemeBySeed();
   221 	arguments << "46631";
   224 	arguments << "46631";
   222 	arguments << seed;
   225 	arguments << seed;
   223 	arguments << (vid_Fullscreen ? "1" : "0");
   226 	arguments << (vid_Fullscreen ? "1" : "0");
   224 	process->start("hw", arguments);
   227 	process->start("hw", arguments);
   225 	demo = new QByteArray;
       
   226 	demo->append(seed.toLocal8Bit());
       
   227 	demo->append(10);
       
   228 }
   228 }
   229 
   229 
   230 void HWGame::AddTeam(const QString & teamname)
   230 void HWGame::AddTeam(const QString & teamname)
   231 {
   231 {
   232 	if (TeamCount == 5) return;
   232 	if (TeamCount == 5) return;
   334 	arguments << seed;
   334 	arguments << seed;
   335 	arguments << (vid_Fullscreen ? "1" : "0");
   335 	arguments << (vid_Fullscreen ? "1" : "0");
   336 	process->start("hw", arguments);
   336 	process->start("hw", arguments);
   337 	demo = new QByteArray;
   337 	demo = new QByteArray;
   338 }
   338 }
       
   339 
       
   340 void HWGame::StartNet(const QString & netseed)
       
   341 {
       
   342 	gameType = gtNet;
       
   343 	seed = netseed;
       
   344 	Start();
       
   345 	demo = new QByteArray;
       
   346 	demo->append(seed.toLocal8Bit());
       
   347 	demo->append(10);
       
   348 }
       
   349 
       
   350 void HWGame::StartLocal()
       
   351 {
       
   352 	gameType = gtLocal;
       
   353 	if (TeamCount < 2) return;
       
   354 	seedgen.GenRNDStr(seed, 10);
       
   355 	Start();
       
   356 	demo = new QByteArray;
       
   357 	demo->append(seed.toLocal8Bit());
       
   358 	demo->append(10);
       
   359 }
       
   360 
       
   361 void HWGame::LocalCFG(const QString & teamname)
       
   362 {
       
   363 	QFile teamcfg(cfgdir.absolutePath() + "/" + teamname + ".cfg");
       
   364 	if (!teamcfg.open(QIODevice::ReadOnly))
       
   365 	{
       
   366 		return ;
       
   367 	}
       
   368 	QTextStream stream(&teamcfg);
       
   369 	stream.setCodec("UTF-8");
       
   370 	QString str;
       
   371 
       
   372 	while (!stream.atEnd())
       
   373 	{
       
   374 		str = stream.readLine();
       
   375 		if (str.startsWith(";") || (str.length() > 254)) continue;
       
   376 		str.prepend("e");
       
   377 		SendIPC(str.toLocal8Bit());
       
   378 	}
       
   379 	teamcfg.close();
       
   380 }