QTfrontend/game.cpp
changeset 180 ea83b9e9057f
parent 178 efdc2a63be8e
child 183 57c2ef19f719
equal deleted inserted replaced
179:06e472d3f9f8 180:ea83b9e9057f
    43 #include "game.h"
    43 #include "game.h"
    44 #include "hwconsts.h"
    44 #include "hwconsts.h"
    45 #include "gameuiconfig.h"
    45 #include "gameuiconfig.h"
    46 #include "gamecfgwidget.h"
    46 #include "gamecfgwidget.h"
    47 
    47 
    48 HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg)
    48 HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg) :
       
    49   TCPBase(true)
    49 {
    50 {
    50 	this->config = config;
    51 	this->config = config;
    51 	this->gamecfg = gamecfg;
    52 	this->gamecfg = gamecfg;
    52 	TeamCount = 0;
    53 	TeamCount = 0;
    53 	seed = "";
    54 	seed = "";
    63 	LocalCFG(teams[index]);
    64 	LocalCFG(teams[index]);
    64 }
    65 }
    65 
    66 
    66 void HWGame::SendConfig()
    67 void HWGame::SendConfig()
    67 {
    68 {
    68 	SendIPC(QString("eseed %1").arg(seed));
    69 	SendIPC(QString("eseed %1").arg(seed).toAscii());
    69 	SendIPC(QString("etheme %1").arg(config->GetRandomTheme()));
    70 	SendIPC(QString("etheme %1").arg(config->GetRandomTheme()).toAscii());
    70 	SENDIPC("TL");
    71 	SendIPC("TL");
    71 	SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()));
    72 	SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()).toAscii());
    72 
    73 
    73 	for (int i = 0; i < TeamCount; i++)
    74 	for (int i = 0; i < TeamCount; i++)
    74 	{
    75 	{
    75 		SENDIPC("eaddteam");
    76 		SendIPC("eaddteam");
    76 		LocalCFG(teams[i]);
    77 		LocalCFG(teams[i]);
    77 		SendIPC(QString("ecolor %1").arg(65535 << i * 8));
    78 		SendIPC(QString("ecolor %1").arg(65535 << i * 8).toAscii());
    78 		for (int t = 0; t < hdNum[teams[i]]; t++)
    79 		for (int t = 0; t < hdNum[teams[i]]; t++)
    79 			SendIPC(QString("eadd hh%1 0").arg(t));
    80 			SendIPC(QString("eadd hh%1 0").arg(t).toAscii());
    80 	}
    81 	}
    81 }
    82 }
    82 
    83 
    83 void HWGame::SendQuickConfig()
    84 void HWGame::SendQuickConfig()
    84 {
    85 {
    85 	SendIPC(QString("eseed %1").arg(seed));
    86 	SendIPC(QString("eseed %1").arg(seed).toAscii());
    86 	SendIPC(QString("etheme %1").arg(config->GetRandomTheme()));
    87 	SendIPC(QString("etheme %1").arg(config->GetRandomTheme()).toAscii());
    87 	SENDIPC("TL");
    88 	SendIPC("TL");
    88 	SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()));
    89 	SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()).toAscii());
    89 	SENDIPC("eaddteam");
    90 	SendIPC("eaddteam");
    90 	LocalCFG(0);
    91 	LocalCFG(0);
    91 	SENDIPC("ecolor 65535");
    92 	SendIPC("ecolor 65535");
    92 	SENDIPC("eadd hh0 0");
    93 	SendIPC("eadd hh0 0");
    93 	SENDIPC("eadd hh1 0");
    94 	SendIPC("eadd hh1 0");
    94 	SENDIPC("eadd hh2 0");
    95 	SendIPC("eadd hh2 0");
    95 	SENDIPC("eadd hh3 0");
    96 	SendIPC("eadd hh3 0");
    96 	SENDIPC("eaddteam");
    97 	SendIPC("eaddteam");
    97 	LocalCFG(2);
    98 	LocalCFG(2);
    98 	SENDIPC("ecolor 16776960");
    99 	SendIPC("ecolor 16776960");
    99 	SENDIPC("eadd hh0 1");
   100 	SendIPC("eadd hh0 1");
   100 	SENDIPC("eadd hh1 1");
   101 	SendIPC("eadd hh1 1");
   101 	SENDIPC("eadd hh2 1");
   102 	SendIPC("eadd hh2 1");
   102 	SENDIPC("eadd hh3 1");
   103 	SendIPC("eadd hh3 1");
   103 }
   104 }
   104 
   105 
   105 void HWGame::ParseMessage(const QByteArray & msg)
   106 void HWGame::ParseMessage(const QByteArray & msg)
   106 {
   107 {
   107 	switch(msg.data()[1]) {
   108 	switch(msg.data()[1]) {
   108 		case '?': {
   109 		case '?': {
   109 			if (gameType == gtNet)
   110 			if (gameType == gtNet)
   110 				emit SendNet(QByteArray("\x01""?"));
   111 				emit SendNet(QByteArray("\x01""?"));
   111 			else
   112 			else
   112 				SENDIPC("!");
   113 				SendIPC("!");
   113 			break;
   114 			break;
   114 		}
   115 		}
   115 		case 'C': {
   116 		case 'C': {
   116 			switch (gameType) {
   117 			switch (gameType) {
   117 				case gtLocal: {
   118 				case gtLocal: {
   122 				 	SendQuickConfig();
   123 				 	SendQuickConfig();
   123 					break;
   124 					break;
   124 				}
   125 				}
   125 				case gtDemo: break;
   126 				case gtDemo: break;
   126 				case gtNet: {
   127 				case gtNet: {
   127 					SENDIPC("TN");
   128 					SendIPC("TN");
   128 					emit SendNet(QByteArray("\x01""C"));
   129 					emit SendNet(QByteArray("\x01""C"));
   129 					break;
   130 					break;
   130 				}
   131 				}
   131 			}
   132 			}
   132 			break;
   133 			break;
   152 			{
   153 			{
   153 				emit SendNet(msg);
   154 				emit SendNet(msg);
   154 			}
   155 			}
   155 			demo->append(msg);
   156 			demo->append(msg);
   156 		}
   157 		}
   157 	}
       
   158 }
       
   159 
       
   160 void HWGame::SendIPC(const char * msg, quint8 len)
       
   161 {
       
   162 	SendIPC(QByteArray::fromRawData(msg, len));
       
   163 }
       
   164 
       
   165 void HWGame::SendIPC(const QString & buf)
       
   166 {
       
   167 	SendIPC(QByteArray().append(buf));
       
   168 }
       
   169 
       
   170 void HWGame::SendIPC(const QByteArray & buf)
       
   171 {
       
   172 	if (buf.size() > MAXMSGCHARS) return;
       
   173 	quint8 len = buf.size();
       
   174 	RawSendIPC(QByteArray::fromRawData((char *)&len, 1) + buf);
       
   175 }
       
   176 
       
   177 void HWGame::RawSendIPC(const QByteArray & buf)
       
   178 {
       
   179 	if (!IPCSocket)
       
   180 	{
       
   181 		toSendBuf += buf;
       
   182 	} else
       
   183 	{
       
   184 		if (toSendBuf.size() > 0)
       
   185 		{
       
   186 			IPCSocket->write(toSendBuf);
       
   187 			demo->append(toSendBuf);
       
   188 			toSendBuf.clear();
       
   189 		}
       
   190 		IPCSocket->write(buf);
       
   191 		demo->append(buf);
       
   192 	}
   158 	}
   193 }
   159 }
   194 
   160 
   195 void HWGame::FromNet(const QByteArray & msg)
   161 void HWGame::FromNet(const QByteArray & msg)
   196 {
   162 {
   271 	int readbytes;
   237 	int readbytes;
   272 	do
   238 	do
   273 	{
   239 	{
   274 		readbytes = stream.readRawData((char *)&buf, 512);
   240 		readbytes = stream.readRawData((char *)&buf, 512);
   275 		toSendBuf.append(QByteArray((char *)&buf, readbytes));
   241 		toSendBuf.append(QByteArray((char *)&buf, readbytes));
       
   242 		//SendIPC(QByteArray((char *)&buf, readbytes));
   276 
   243 
   277 	} while (readbytes > 0);
   244 	} while (readbytes > 0);
   278 	demofile.close();
   245 	demofile.close();
   279 
   246 
   280 	// run engine
   247 	// run engine