QTfrontend/team.cpp
changeset 2948 3f21a9dc93d0
parent 2879 935ead93893c
child 3159 e5eff81d7635
equal deleted inserted replaced
2947:803b277e4894 2948:3f21a9dc93d0
    27 #include "pages.h"
    27 #include "pages.h"
    28 #include "hwconsts.h"
    28 #include "hwconsts.h"
    29 #include "hats.h"
    29 #include "hats.h"
    30 
    30 
    31 HWTeam::HWTeam(const QString & teamname) :
    31 HWTeam::HWTeam(const QString & teamname) :
    32 	difficulty(0),
    32     difficulty(0),
    33 	numHedgehogs(4),
    33     numHedgehogs(4),
    34 	m_isNetTeam(false)
    34     m_isNetTeam(false)
    35 {
    35 {
    36 	TeamName = teamname;
    36     TeamName = teamname;
    37 	OldTeamName = TeamName;
    37     OldTeamName = TeamName;
    38 	for (int i = 0; i < 8; i++)
    38     for (int i = 0; i < 8; i++)
    39 	{
    39     {
    40 		HHName[i].sprintf("hedgehog %d", i);
    40         HHName[i].sprintf("hedgehog %d", i);
    41 		HHHat[i] = "NoHat";
    41         HHHat[i] = "NoHat";
    42 	}
    42     }
    43 	Grave = "Statue";
    43     Grave = "Statue";
    44 	Fort = "Plane";
    44     Fort = "Plane";
    45 	Voicepack = "Default";
    45     Voicepack = "Default";
    46 	Flag = "hedgewars";
    46     Flag = "hedgewars";
    47 	for(int i = 0; i < BINDS_NUMBER; i++)
    47     for(int i = 0; i < BINDS_NUMBER; i++)
    48 	{
    48     {
    49 		binds[i].action = cbinds[i].action;
    49         binds[i].action = cbinds[i].action;
    50 		binds[i].strbind = cbinds[i].strbind;
    50         binds[i].strbind = cbinds[i].strbind;
    51 	}
    51     }
    52 }
    52 }
    53 
    53 
    54 HWTeam::HWTeam(const QStringList& strLst) :
    54 HWTeam::HWTeam(const QStringList& strLst) :
    55   numHedgehogs(4),
    55   numHedgehogs(4),
    56   m_isNetTeam(true)
    56   m_isNetTeam(true)
    57 {
    57 {
    58 	// net teams are configured from QStringList
    58     // net teams are configured from QStringList
    59 	if(strLst.size() != 23) throw HWTeamConstructException();
    59     if(strLst.size() != 23) throw HWTeamConstructException();
    60 	TeamName = strLst[0];
    60     TeamName = strLst[0];
    61 	Grave = strLst[1];
    61     Grave = strLst[1];
    62 	Fort = strLst[2];
    62     Fort = strLst[2];
    63 	Voicepack = strLst[3];
    63     Voicepack = strLst[3];
    64 	Flag = strLst[4];
    64     Flag = strLst[4];
    65 	Owner = strLst[5];
    65     Owner = strLst[5];
    66 	difficulty = strLst[6].toUInt();
    66     difficulty = strLst[6].toUInt();
    67 	for(int i = 0; i < 8; i++)
    67     for(int i = 0; i < 8; i++)
    68 	{
    68     {
    69 		HHName[i]=strLst[i * 2 + 7];
    69         HHName[i]=strLst[i * 2 + 7];
    70 		HHHat[i]=strLst[i * 2 + 8];
    70         HHHat[i]=strLst[i * 2 + 8];
    71 // Somehow claymore managed an empty hat.  Until we figure out how, this should avoid a repeat
    71 // Somehow claymore managed an empty hat.  Until we figure out how, this should avoid a repeat
    72 // Checking net teams is probably pointless, but can't hurt.
    72 // Checking net teams is probably pointless, but can't hurt.
    73         if (HHHat[i].length() == 0) HHHat[i] = "NoHat"; 
    73         if (HHHat[i].length() == 0) HHHat[i] = "NoHat"; 
    74 	}
    74     }
    75 }
    75 }
    76 
    76 
    77 HWTeam::HWTeam() :
    77 HWTeam::HWTeam() :
    78   difficulty(0),
    78   difficulty(0),
    79   numHedgehogs(4),
    79   numHedgehogs(4),
    80   m_isNetTeam(false)
    80   m_isNetTeam(false)
    81 {
    81 {
    82 	TeamName = QString("Team");
    82     TeamName = QString("Team");
    83 	for (int i = 0; i < 8; i++)
    83     for (int i = 0; i < 8; i++)
    84 	{
    84     {
    85 		HHName[i].sprintf("hedgehog %d", i);
    85         HHName[i].sprintf("hedgehog %d", i);
    86 		HHHat[i] = "NoHat";
    86         HHHat[i] = "NoHat";
    87 	}
    87     }
    88 
    88 
    89 	Grave = QString("Simple"); // default
    89     Grave = QString("Simple"); // default
    90 	Fort = QString("Island"); // default
    90     Fort = QString("Island"); // default
    91 	Voicepack = "Default";
    91     Voicepack = "Default";
    92 	Flag = "hedgewars";
    92     Flag = "hedgewars";
    93 
    93 
    94 	for(int i = 0; i < BINDS_NUMBER; i++)
    94     for(int i = 0; i < BINDS_NUMBER; i++)
    95 	{
    95     {
    96 		binds[i].action = cbinds[i].action;
    96         binds[i].action = cbinds[i].action;
    97 		binds[i].strbind = cbinds[i].strbind;
    97         binds[i].strbind = cbinds[i].strbind;
    98 	}
    98     }
    99 }
    99 }
   100 
   100 
   101 
   101 
   102 bool HWTeam::LoadFromFile()
   102 bool HWTeam::LoadFromFile()
   103 {
   103 {
   104 	numHedgehogs=4;
   104     numHedgehogs=4;
   105 	QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg");
   105     QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg");
   106 	if (!cfgfile.open(QIODevice::ReadOnly)) return false;
   106     if (!cfgfile.open(QIODevice::ReadOnly)) return false;
   107 	QTextStream stream(&cfgfile);
   107     QTextStream stream(&cfgfile);
   108 	stream.setCodec("UTF-8");
   108     stream.setCodec("UTF-8");
   109 	QString str;
   109     QString str;
   110 	QString action;
   110     QString action;
   111 
   111 
   112 	while (!stream.atEnd())
   112     while (!stream.atEnd())
   113 	{
   113     {
   114 		str = stream.readLine();
   114         str = stream.readLine();
   115 		if (str.startsWith(";")) continue;
   115         if (str.startsWith(";")) continue;
   116 		/*if (str.startsWith("name team "))
   116         /*if (str.startsWith("name team "))
   117 		{
   117         {
   118 			str.remove(0, 10);
   118             str.remove(0, 10);
   119 			TeamName = str;
   119             TeamName = str;
   120 		} else*/
   120         } else*/
   121 		if (str.startsWith("name hh"))
   121         if (str.startsWith("name hh"))
   122 		{
   122         {
   123 			str.remove(0, 7);
   123             str.remove(0, 7);
   124 			long i = str.left(1).toLong();
   124             long i = str.left(1).toLong();
   125 			if ((i < 0) || (i > 7)) continue;
   125             if ((i < 0) || (i > 7)) continue;
   126 			str.remove(0, 2);
   126             str.remove(0, 2);
   127 			HHName[i] = str;
   127             HHName[i] = str;
   128 		} else
   128         } else
   129 		if (str.startsWith("hat"))
   129         if (str.startsWith("hat"))
   130 		{
   130         {
   131 			str.remove(0, 3);
   131             str.remove(0, 3);
   132 			long i = str.left(1).toLong();
   132             long i = str.left(1).toLong();
   133 			if ((i < 0) || (i > 7)) continue;
   133             if ((i < 0) || (i > 7)) continue;
   134 			str.remove(0, 2);
   134             str.remove(0, 2);
   135 			HHHat[i] = str;
   135             HHHat[i] = str;
   136 // Somehow claymore managed an empty hat.  Until we figure out how, this should avoid a repeat
   136 // Somehow claymore managed an empty hat.  Until we figure out how, this should avoid a repeat
   137             if (HHHat[i].length() == 0) HHHat[i] = "NoHat"; 
   137             if (HHHat[i].length() == 0) HHHat[i] = "NoHat"; 
   138 		} else
   138         } else
   139 		if (str.startsWith("grave "))
   139         if (str.startsWith("grave "))
   140 		{
   140         {
   141 			str.remove(0, 6);
   141             str.remove(0, 6);
   142 			Grave = str;
   142             Grave = str;
   143 		} else
   143         } else
   144 		if (str.startsWith("fort "))
   144         if (str.startsWith("fort "))
   145 		{
   145         {
   146 			str.remove(0, 5);
   146             str.remove(0, 5);
   147 			Fort = str;
   147             Fort = str;
   148 		} else
   148         } else
   149 		if (str.startsWith("flag "))
   149         if (str.startsWith("flag "))
   150 		{
   150         {
   151 			str.remove(0, 5);
   151             str.remove(0, 5);
   152 			Flag = str;
   152             Flag = str;
   153 		} else
   153         } else
   154 		if (str.startsWith("voicepack "))
   154         if (str.startsWith("voicepack "))
   155 		{
   155         {
   156 			str.remove(0, 10);
   156             str.remove(0, 10);
   157 			Voicepack = str;
   157             Voicepack = str;
   158 		} else
   158         } else
   159 		if (str.startsWith("bind "))
   159         if (str.startsWith("bind "))
   160 		{
   160         {
   161 			str.remove(0, 5);
   161             str.remove(0, 5);
   162 			action = str.section(' ', 1);
   162             action = str.section(' ', 1);
   163 			str = str.section(' ', 0, 0);
   163             str = str.section(' ', 0, 0);
   164 			str.truncate(15);
   164             str.truncate(15);
   165 			for (int i = 0; i < BINDS_NUMBER; i++)
   165             for (int i = 0; i < BINDS_NUMBER; i++)
   166 				if (action == binds[i].action)
   166                 if (action == binds[i].action)
   167 				{
   167                 {
   168 					binds[i].strbind = str;
   168                     binds[i].strbind = str;
   169 					break;
   169                     break;
   170 				}
   170                 }
   171 		} else
   171         } else
   172 		if (str.startsWith("difficulty "))
   172         if (str.startsWith("difficulty "))
   173 		{
   173         {
   174 		  str.remove(0, 11);
   174           str.remove(0, 11);
   175 		  difficulty=str.toUInt();
   175           difficulty=str.toUInt();
   176 		  if (difficulty>5) difficulty=0; // this shouldn't normally happen
   176           if (difficulty>5) difficulty=0; // this shouldn't normally happen
   177 		}
   177         }
   178 	}
   178     }
   179 	cfgfile.close();
   179     cfgfile.close();
   180 	return true;
   180     return true;
   181 }
   181 }
   182 
   182 
   183 bool HWTeam::SaveToFile()
   183 bool HWTeam::SaveToFile()
   184 {
   184 {
   185 	if (OldTeamName != TeamName)
   185     if (OldTeamName != TeamName)
   186 	{
   186     {
   187 		QFile cfgfile(cfgdir->absolutePath() + "/" + OldTeamName + ".cfg");
   187         QFile cfgfile(cfgdir->absolutePath() + "/" + OldTeamName + ".cfg");
   188 		cfgfile.remove();
   188         cfgfile.remove();
   189 		OldTeamName = TeamName;
   189         OldTeamName = TeamName;
   190 	}
   190     }
   191 	QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg");
   191     QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg");
   192 	if (!cfgfile.open(QIODevice::WriteOnly)) return false;
   192     if (!cfgfile.open(QIODevice::WriteOnly)) return false;
   193 	QTextStream stream(&cfgfile);
   193     QTextStream stream(&cfgfile);
   194 	stream.setCodec("UTF-8");
   194     stream.setCodec("UTF-8");
   195 	stream << "; Generated by Hedgewars, do not modify" << endl;
   195     stream << "; Generated by Hedgewars, do not modify" << endl;
   196 	stream << "name team " << TeamName << endl;
   196     stream << "name team " << TeamName << endl;
   197 	for (int i = 0; i < 8; i++)
   197     for (int i = 0; i < 8; i++)
   198 	{
   198     {
   199 		stream << "name hh" << i << " " << HHName[i] << endl;
   199         stream << "name hh" << i << " " << HHName[i] << endl;
   200 		stream << "hat" << i << " " << HHHat[i] << endl;
   200         stream << "hat" << i << " " << HHHat[i] << endl;
   201 	}
   201     }
   202 	stream << "grave " << Grave << endl;
   202     stream << "grave " << Grave << endl;
   203 	stream << "fort " << Fort << endl;
   203     stream << "fort " << Fort << endl;
   204 	stream << "voicepack " << Voicepack << endl;
   204     stream << "voicepack " << Voicepack << endl;
   205 	stream << "flag " << Flag << endl;
   205     stream << "flag " << Flag << endl;
   206 	for(int i = 0; i < BINDS_NUMBER; i++)
   206     for(int i = 0; i < BINDS_NUMBER; i++)
   207 	{
   207     {
   208 		stream << "bind " << binds[i].strbind << " " << binds[i].action << endl;
   208         stream << "bind " << binds[i].strbind << " " << binds[i].action << endl;
   209 	}
   209     }
   210 	stream << "difficulty " << difficulty << endl;
   210     stream << "difficulty " << difficulty << endl;
   211 	cfgfile.close();
   211     cfgfile.close();
   212 	return true;
   212     return true;
   213 }
   213 }
   214 
   214 
   215 void HWTeam::SetToPage(HWForm * hwform)
   215 void HWTeam::SetToPage(HWForm * hwform)
   216 {
   216 {
   217 	hwform->ui.pageEditTeam->TeamNameEdit->setText(TeamName);
   217     hwform->ui.pageEditTeam->TeamNameEdit->setText(TeamName);
   218 	hwform->ui.pageEditTeam->CBTeamLvl->setCurrentIndex(difficulty);
   218     hwform->ui.pageEditTeam->CBTeamLvl->setCurrentIndex(difficulty);
   219 	for(int i = 0; i < 8; i++)
   219     for(int i = 0; i < 8; i++)
   220 	{
   220     {
   221          hwform->ui.pageEditTeam->HHNameEdit[i]->setText(HHName[i]);
   221          hwform->ui.pageEditTeam->HHNameEdit[i]->setText(HHName[i]);
   222          if (HHHat[i].startsWith("Reserved"))
   222          if (HHHat[i].startsWith("Reserved"))
   223             hwform->ui.pageEditTeam->HHHats[i]->setCurrentIndex(hwform->ui.pageEditTeam->HHHats[i]->findData("Reserved "+HHHat[i].remove(0,40), Qt::DisplayRole));
   223             hwform->ui.pageEditTeam->HHHats[i]->setCurrentIndex(hwform->ui.pageEditTeam->HHHats[i]->findData("Reserved "+HHHat[i].remove(0,40), Qt::DisplayRole));
   224          else
   224          else
   225 	        hwform->ui.pageEditTeam->HHHats[i]->setCurrentIndex(hwform->ui.pageEditTeam->HHHats[i]->findData(HHHat[i], Qt::DisplayRole));
   225             hwform->ui.pageEditTeam->HHHats[i]->setCurrentIndex(hwform->ui.pageEditTeam->HHHats[i]->findData(HHHat[i], Qt::DisplayRole));
   226 	}
   226     }
   227 	hwform->ui.pageEditTeam->CBGrave->setCurrentIndex(hwform->ui.pageEditTeam->CBGrave->findText(Grave));
   227     hwform->ui.pageEditTeam->CBGrave->setCurrentIndex(hwform->ui.pageEditTeam->CBGrave->findText(Grave));
   228 	hwform->ui.pageEditTeam->CBFlag->setCurrentIndex(hwform->ui.pageEditTeam->CBFlag->findText(Flag));
   228     hwform->ui.pageEditTeam->CBFlag->setCurrentIndex(hwform->ui.pageEditTeam->CBFlag->findText(Flag));
   229 
   229 
   230 	hwform->ui.pageEditTeam->CBFort->setCurrentIndex(hwform->ui.pageEditTeam->CBFort->findText(Fort));
   230     hwform->ui.pageEditTeam->CBFort->setCurrentIndex(hwform->ui.pageEditTeam->CBFort->findText(Fort));
   231 	hwform->ui.pageEditTeam->CBVoicepack->setCurrentIndex(hwform->ui.pageEditTeam->CBVoicepack->findText(Voicepack));
   231     hwform->ui.pageEditTeam->CBVoicepack->setCurrentIndex(hwform->ui.pageEditTeam->CBVoicepack->findText(Voicepack));
   232 	//hwform->ui.pageEditTeam->CBFort_activated(Fort);
   232     //hwform->ui.pageEditTeam->CBFort_activated(Fort);
   233 
   233 
   234 	for(int i = 0; i < BINDS_NUMBER; i++)
   234     for(int i = 0; i < BINDS_NUMBER; i++)
   235 	{
   235     {
   236 		hwform->ui.pageEditTeam->CBBind[i]->setCurrentIndex(hwform->ui.pageEditTeam->CBBind[i]->findData(binds[i].strbind));
   236         hwform->ui.pageEditTeam->CBBind[i]->setCurrentIndex(hwform->ui.pageEditTeam->CBBind[i]->findData(binds[i].strbind));
   237 	}
   237     }
   238 }
   238 }
   239 
   239 
   240 void HWTeam::GetFromPage(HWForm * hwform)
   240 void HWTeam::GetFromPage(HWForm * hwform)
   241 {
   241 {
   242 	TeamName  = hwform->ui.pageEditTeam->TeamNameEdit->text();
   242     TeamName  = hwform->ui.pageEditTeam->TeamNameEdit->text();
   243 	difficulty = hwform->ui.pageEditTeam->CBTeamLvl->currentIndex();
   243     difficulty = hwform->ui.pageEditTeam->CBTeamLvl->currentIndex();
   244 	for(int i = 0; i < 8; i++)
   244     for(int i = 0; i < 8; i++)
   245 	{
   245     {
   246 		HHName[i] = hwform->ui.pageEditTeam->HHNameEdit[i]->text();
   246         HHName[i] = hwform->ui.pageEditTeam->HHNameEdit[i]->text();
   247         if (hwform->ui.pageEditTeam->HHHats[i]->currentText().startsWith("Reserved"))
   247         if (hwform->ui.pageEditTeam->HHHats[i]->currentText().startsWith("Reserved"))
   248 		    HHHat[i] = "Reserved"+playerHash+hwform->ui.pageEditTeam->HHHats[i]->currentText().remove(0,9);
   248             HHHat[i] = "Reserved"+playerHash+hwform->ui.pageEditTeam->HHHats[i]->currentText().remove(0,9);
   249         else
   249         else
   250 	        HHHat[i] = hwform->ui.pageEditTeam->HHHats[i]->currentText();
   250             HHHat[i] = hwform->ui.pageEditTeam->HHHats[i]->currentText();
   251 	}
   251     }
   252 
   252 
   253 	Grave = hwform->ui.pageEditTeam->CBGrave->currentText();
   253     Grave = hwform->ui.pageEditTeam->CBGrave->currentText();
   254 	Fort = hwform->ui.pageEditTeam->CBFort->currentText();
   254     Fort = hwform->ui.pageEditTeam->CBFort->currentText();
   255 	Voicepack = hwform->ui.pageEditTeam->CBVoicepack->currentText();
   255     Voicepack = hwform->ui.pageEditTeam->CBVoicepack->currentText();
   256 	Flag = hwform->ui.pageEditTeam->CBFlag->currentText();
   256     Flag = hwform->ui.pageEditTeam->CBFlag->currentText();
   257 	for(int i = 0; i < BINDS_NUMBER; i++)
   257     for(int i = 0; i < BINDS_NUMBER; i++)
   258 	{
   258     {
   259 		binds[i].strbind = hwform->ui.pageEditTeam->CBBind[i]->itemData(hwform->ui.pageEditTeam->CBBind[i]->currentIndex()).toString();
   259         binds[i].strbind = hwform->ui.pageEditTeam->CBBind[i]->itemData(hwform->ui.pageEditTeam->CBBind[i]->currentIndex()).toString();
   260 	}
   260     }
   261 }
   261 }
   262 
   262 
   263 QStringList HWTeam::TeamGameConfig(quint32 InitHealth) const
   263 QStringList HWTeam::TeamGameConfig(quint32 InitHealth) const
   264 {
   264 {
   265 	QStringList sl;
   265     QStringList sl;
   266     if (m_isNetTeam)
   266     if (m_isNetTeam)
   267     {
   267     {
   268 	    sl.push_back(QString("eaddteam %3 %1 %2").arg(teamColor.rgb() & 0xffffff).arg(TeamName).arg(QString(QCryptographicHash::hash(Owner.toLatin1(), QCryptographicHash::Md5).toHex())));
   268         sl.push_back(QString("eaddteam %3 %1 %2").arg(teamColor.rgb() & 0xffffff).arg(TeamName).arg(QString(QCryptographicHash::hash(Owner.toLatin1(), QCryptographicHash::Md5).toHex())));
   269 		sl.push_back("erdriven");
   269         sl.push_back("erdriven");
   270     }
   270     }
   271     else sl.push_back(QString("eaddteam %3 %1 %2").arg(teamColor.rgb() & 0xffffff).arg(TeamName).arg(playerHash));
   271     else sl.push_back(QString("eaddteam %3 %1 %2").arg(teamColor.rgb() & 0xffffff).arg(TeamName).arg(playerHash));
   272 
   272 
   273 	sl.push_back(QString("egrave " + Grave));
   273     sl.push_back(QString("egrave " + Grave));
   274 	sl.push_back(QString("efort " + Fort));
   274     sl.push_back(QString("efort " + Fort));
   275 	sl.push_back(QString("evoicepack " + Voicepack));
   275     sl.push_back(QString("evoicepack " + Voicepack));
   276 	sl.push_back(QString("eflag " + Flag));
   276     sl.push_back(QString("eflag " + Flag));
   277 
   277 
   278 	if (!m_isNetTeam)
   278     if (!m_isNetTeam)
   279 		for(int i = 0; i < BINDS_NUMBER; i++)
   279         for(int i = 0; i < BINDS_NUMBER; i++)
   280 			if(!binds[i].strbind.isEmpty())
   280             if(!binds[i].strbind.isEmpty())
   281 				sl.push_back(QString("ebind " + binds[i].strbind + " " + binds[i].action));
   281                 sl.push_back(QString("ebind " + binds[i].strbind + " " + binds[i].action));
   282 
   282 
   283 	for (int t = 0; t < numHedgehogs; t++)
   283     for (int t = 0; t < numHedgehogs; t++)
   284 	{
   284     {
   285 	  sl.push_back(QString("eaddhh %1 %2 %3")
   285       sl.push_back(QString("eaddhh %1 %2 %3")
   286 		       .arg(QString::number(difficulty),
   286                .arg(QString::number(difficulty),
   287 			    QString::number(InitHealth),
   287                 QString::number(InitHealth),
   288 			    HHName[t]));
   288                 HHName[t]));
   289 	  sl.push_back(QString("ehat %1")
   289       sl.push_back(QString("ehat %1")
   290 		       .arg(HHHat[t]));
   290                .arg(HHHat[t]));
   291 	}
   291     }
   292 	return sl;
   292     return sl;
   293 }
   293 }
   294 
   294 
   295 bool HWTeam::isNetTeam() const
   295 bool HWTeam::isNetTeam() const
   296 {
   296 {
   297   return m_isNetTeam;
   297   return m_isNetTeam;