Random team generator rework by ttsmj
authorunc0rr
Sun, 22 Mar 2009 14:35:45 +0000
changeset 1907 a104432e8301
parent 1906 644f93d8f148
child 1908 5be17e24751a
Random team generator rework by ttsmj
QTfrontend/CMakeLists.txt
QTfrontend/game.cpp
QTfrontend/game.h
QTfrontend/hwform.cpp
QTfrontend/hwform.h
QTfrontend/namegen.cpp
QTfrontend/namegen.h
QTfrontend/team.cpp
QTfrontend/team.h
--- a/QTfrontend/CMakeLists.txt	Sun Mar 22 14:32:22 2009 +0000
+++ b/QTfrontend/CMakeLists.txt	Sun Mar 22 14:35:45 2009 +0000
@@ -49,6 +49,7 @@
 	main.cpp
 	hwform.cpp
 	team.cpp
+	namegen.cpp
 	teamselect.cpp
 	teamselhelper.cpp
 	frameTeam.cpp
--- a/QTfrontend/game.cpp	Sun Mar 22 14:32:22 2009 +0000
+++ b/QTfrontend/game.cpp	Sun Mar 22 14:35:45 2009 +0000
@@ -104,19 +104,25 @@
 			.arg((Themes->size() > 0) ? Themes->at(rand() % Themes->size()) : "steel"));
 	HWProto::addStringToBuffer(teamscfg, "eseed " + QUuid::createUuid().toString());
 
-	HWTeam team1(0);
-	team1.difficulty = 0;
-	team1.teamColor = *color1;
-	team1.numHedgehogs = 4;
-	HWProto::addStringListToBuffer(teamscfg,
-			team1.TeamGameConfig(100));
+	HWNamegen namegen;
 
-	HWTeam team2(2);
-	team2.difficulty = 4;
-	team2.teamColor = *color2;
-	team2.numHedgehogs = 4;
+	HWTeam * team1;
+	team1 = new HWTeam;
+	team1->difficulty = 0;
+	team1->teamColor = *color1;
+	team1->numHedgehogs = 4;
+	namegen.TeamRandomNames(team1,TRUE);
 	HWProto::addStringListToBuffer(teamscfg,
-			team2.TeamGameConfig(100));
+			team1->TeamGameConfig(100));
+
+	HWTeam * team2;
+	team2 = new HWTeam;
+	team2->difficulty = 4;
+	team2->teamColor = *color2;
+	team2->numHedgehogs = 4;
+	namegen.TeamRandomNames(team2,TRUE);
+	HWProto::addStringListToBuffer(teamscfg,
+			team2->TeamGameConfig(100));
 
 	HWProto::addStringToBuffer(teamscfg, "eammstore " + *cDefaultAmmoStore);
 	HWProto::addStringToBuffer(teamscfg, "eammstore " + *cDefaultAmmoStore);
--- a/QTfrontend/game.h	Sun Mar 22 14:32:22 2009 +0000
+++ b/QTfrontend/game.h	Sun Mar 22 14:35:45 2009 +0000
@@ -21,6 +21,7 @@
 
 #include <QString>
 #include "team.h"
+#include "namegen.h"
 
 #include "tcpBase.h"
 
--- a/QTfrontend/hwform.cpp	Sun Mar 22 14:32:22 2009 +0000
+++ b/QTfrontend/hwform.cpp	Sun Mar 22 14:35:45 2009 +0000
@@ -38,6 +38,7 @@
 #include "hwform.h"
 #include "game.h"
 #include "team.h"
+#include "namegen.h"
 #include "teamselect.h"
 #include "selectWeapon.h"
 #include "gameuiconfig.h"
@@ -63,6 +64,8 @@
 	ui.pageOptions->CBResolution->addItems(sdli.getResolutions());
 
 	config = new GameUIConfig(this, cfgdir->absolutePath() + "/hedgewars.ini");
+	
+	namegen = new HWNamegen();
 
 	UpdateTeamsLists();
 	UpdateWeapons();
@@ -426,14 +429,14 @@
 void HWForm::RandomNames()
 {
 	editedTeam->GetFromPage(this);
-	editedTeam->TeamRandomNames(FALSE);
+	namegen->TeamRandomNames(editedTeam,FALSE);
 	editedTeam->SetToPage(this);
 }
 
 void HWForm::RandomName(const int &i)
 {
 	editedTeam->GetFromPage(this);
-	editedTeam->TeamRandomName(i);
+	namegen->TeamRandomName(editedTeam,i);
 	editedTeam->SetToPage(this);
 }
 
--- a/QTfrontend/hwform.h	Sun Mar 22 14:32:22 2009 +0000
+++ b/QTfrontend/hwform.h	Sun Mar 22 14:35:45 2009 +0000
@@ -30,6 +30,7 @@
 
 class HWGame;
 class HWTeam;
+class HWNamegen;
 class HWNewNet;
 class GameUIConfig;
 class HWNetRegisterServer;
@@ -129,6 +130,7 @@
 	HWTeam * editedTeam;
 	HWNewNet * hwnet;
 	GameUIConfig * config;
+	HWNamegen * namegen;
 	AmmoSchemeModel * ammoSchemeModel;
 	QStack<quint8> PagesStack;
 	QTime eggTimer;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/namegen.cpp	Sun Mar 22 14:35:45 2009 +0000
@@ -0,0 +1,164 @@
+/*
+ * Hedgewars, a free turn based strategy game
+ * Copyright (c) 2009 Martin Minarik <ttsmj@pokec.sk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#include <QFile>
+#include <QTextStream>
+#include <QApplication>
+#include <QStringList>
+#include <QLineEdit>
+#include "namegen.h"
+#include "hwform.h"
+#include "hwconsts.h"
+
+
+HWNamegen::HWNamegen() :
+	TypesAvliable(false)
+{
+
+	TypesLoad();
+}
+
+HWNamegen::~HWNamegen()
+{
+}
+
+
+
+void HWNamegen::TeamRandomName(HWTeam*& team, const int &i)
+{
+	RandomNameByHat(team,i);
+}
+
+void HWNamegen::TeamRandomNames(HWTeam*& team, const bool changeteamname)
+{
+	if ((TypesHatnames.size() > 0) && TypesAvliable){
+
+		int kind = (rand()%(TypesHatnames.size()));
+
+		if (changeteamname){
+			if (TypesTeamnames[kind].size() > 0){
+				team->TeamName = TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())];
+			}
+			team->Grave = "Simple"; // Todo: make it semi-random
+			team->Fort = "Island"; // Todo: make it semi-random
+		}
+
+		for(int i = 0; i < 8; i++)
+		{
+			if ((TypesHatnames[kind].size()) > 0){
+				team->HHHat[i] = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())];
+			}
+			RandomNameByHat(team,i);
+		}
+
+	}
+
+}
+
+
+void HWNamegen::RandomNameByHat(HWTeam*& team, const int &i)
+{
+	QStringList Dictionaries;
+	HatCfgLoad(team->HHHat[i],Dictionaries);
+
+	QStringList Dictionary;
+	DictLoad(Dictionaries[rand()%(Dictionaries.size())],Dictionary);
+
+	team->HHName[i] = Dictionary[rand()%(Dictionary.size())];
+}
+
+void HWNamegen::DictLoad(const QString filename, QStringList &list)
+{
+     list.clear();
+
+     QFile file(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename));
+     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
+     {
+
+         QTextStream in(&file);
+         while (!in.atEnd()) {
+             QString line = in.readLine();
+             if(line != QString(""))
+                 {list.append(line);}
+         }
+     }
+
+     if (list.size()==0)
+         list.append(filename);
+
+}
+
+
+void HWNamegen::HatCfgLoad(const QString hatname, QStringList &list)
+{
+     list.clear();
+
+     QFile file(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname));
+     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
+     {
+
+         QTextStream in(&file);
+         while (!in.atEnd()) {
+             QString line = in.readLine();
+             if(line != QString(""))
+                 {list.append(line);}
+         }
+     }
+
+     if (list.size()==0)
+         list.append(QString("generic"));
+
+}
+
+
+void HWNamegen::TypesLoad()
+{
+
+     QFile file(QString("%1/Names/types.ini").arg(datadir->absolutePath()));
+     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
+         {TypesAvliable = FALSE; return;}
+
+     int counter = 0; //counter starts with 0 (teamnames mode)
+     TypesTeamnames.append(QStringList());
+     TypesHatnames.append(QStringList());
+
+     QTextStream in(&file);
+     while (!in.atEnd()) {
+         QString line = in.readLine();
+         if (line == QString("#####")){
+             counter++; //toggle mode (teamnames || hats)
+             if ((counter%2) == 0){
+                 TypesTeamnames.append(QStringList());
+                 TypesHatnames.append(QStringList());
+             }
+         } else if ((line == QString("*****")) || (line == QString("*END*"))){
+             TypesAvliable = TRUE; return; // bye bye
+         } else {
+             if ((counter%2) == 0){ // even => teamnames mode
+                 TypesTeamnames[(counter/2)].append(line);
+             } else { // odd => hats mode
+                 TypesHatnames[((counter-1)/2)].append(line);
+             }
+         }
+//         Types.append(line);
+     }
+         TypesAvliable = TRUE;
+     return;
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/namegen.h	Sun Mar 22 14:35:45 2009 +0000
@@ -0,0 +1,49 @@
+/*
+ * Hedgewars, a free turn based strategy game
+ * Copyright (c) 2009 Martin Minarik <ttsmj@pokec.sk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ */
+
+#ifndef NAMEGEN_H
+#define NAMEGEN_H
+
+#include <QString>
+
+class HWForm;
+class HWTeam;
+
+class HWNamegen
+{
+public:
+	HWNamegen();
+	~HWNamegen();
+
+	void TeamRandomName(HWTeam*& team, const int &i);
+	void TeamRandomNames(HWTeam*& team, const bool changeteamname);
+	void RandomNameByHat(HWTeam*& team, const int &i);
+
+private:
+
+		QList<QStringList> TypesTeamnames;
+		QList<QStringList> TypesHatnames;
+		bool TypesAvliable;
+		void TypesLoad();
+		void DictLoad(const QString filename, QStringList &list);
+		void HatCfgLoad(const QString hatname, QStringList &list);
+};
+
+
+
+#endif
--- a/QTfrontend/team.cpp	Sun Mar 22 14:32:22 2009 +0000
+++ b/QTfrontend/team.cpp	Sun Mar 22 14:35:45 2009 +0000
@@ -23,7 +23,6 @@
 #include <QLineEdit>
 #include "team.h"
 #include "hwform.h"
-#include "predefteams.h"
 #include "pages.h"
 #include "hwconsts.h"
 #include "hats.h"
@@ -33,8 +32,6 @@
 	numHedgehogs(4),
 	m_isNetTeam(false)
 {
-	TypesLoad();
-
 	TeamName = teamname;
 	OldTeamName = TeamName;
 	for (int i = 0; i < 8; i++)
@@ -56,8 +53,6 @@
   numHedgehogs(4),
   m_isNetTeam(true)
 {
-	TypesLoad();
-
 	// net teams are configured from QStringList
 	if(strLst.size() != 22) throw HWTeamConstructException();
 	TeamName = strLst[0];
@@ -73,35 +68,11 @@
 	}
 }
 
-HWTeam::HWTeam(quint8 num) :
+HWTeam::HWTeam() :
   difficulty(0),
   numHedgehogs(4),
   m_isNetTeam(false)
 {
-	TypesLoad();
-
-	num %= PREDEFTEAMS_COUNT;
-/*
-	//This is now generated by TeamRandomNames(TRUE);
-	TeamName = QApplication::translate("teams", pteams[num].TeamName);
-	HHName[0] = QApplication::translate("teams", pteams[num].hh0name);
-	HHName[1] = QApplication::translate("teams", pteams[num].hh1name);
-	HHName[2] = QApplication::translate("teams", pteams[num].hh2name);
-	HHName[3] = QApplication::translate("teams", pteams[num].hh3name);
-	HHName[4] = QApplication::translate("teams", pteams[num].hh4name);
-	HHName[5] = QApplication::translate("teams", pteams[num].hh5name);
-	HHName[6] = QApplication::translate("teams", pteams[num].hh6name);
-	HHName[7] = QApplication::translate("teams", pteams[num].hh7name);
-	HHHat[0] = pteams[num].hh0hat;
-	HHHat[1] = pteams[num].hh1hat;
-	HHHat[2] = pteams[num].hh2hat;
-	HHHat[3] = pteams[num].hh3hat;
-	HHHat[4] = pteams[num].hh4hat;
-	HHHat[5] = pteams[num].hh5hat;
-	HHHat[6] = pteams[num].hh6hat;
-	HHHat[7] = pteams[num].hh7hat;
-*/	
-
 	TeamName = QString("Team");
 	for (int i = 0; i < 8; i++)
 	{
@@ -109,15 +80,14 @@
 		HHHat[i] = "NoHat";
 	}
 
-	Grave = pteams[num].Grave;
-	Fort = pteams[num].Fort;
+	Grave = QString("Simple"); // default
+	Fort = QString("Island"); // default
+
 	for(int i = 0; i < BINDS_NUMBER; i++)
 	{
 		binds[i].action = cbinds[i].action;
 		binds[i].strbind = cbinds[i].strbind;
 	}
-
-	TeamRandomNames(TRUE);
 }
 
 
@@ -294,62 +264,6 @@
 	return sl;
 }
 
-void HWTeam::RandomNameByHat(const int &i)
-{
-
-
-
-	QStringList Dictionaries;
-	HatCfgLoad(HHHat[i],Dictionaries);
-
-
-/*
-
-"Dismissed",
-"Dragon",
-"Mindblower",
-*/
-
-	QStringList Dictionary;
-	DictLoad(Dictionaries[rand()%(Dictionaries.size())],Dictionary);
-
-
-	HHName[i] = Dictionary[rand()%(Dictionary.size())];
-
-}
-
-void HWTeam::TeamRandomName(const int &i)
-{
-
-	RandomNameByHat(i);
-
-
-}
-
-void HWTeam::TeamRandomNames(bool changeteamname)
-{
-
-	if ((TypesHatnames.size() > 0) && TypesAvliable){
-
-		int kind = (rand()%(TypesHatnames.size()));
-
-		if (changeteamname){
-			if (TypesTeamnames[kind].size() > 0){
-				TeamName = TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())];
-			}
-		}
-
-		for(int i = 0; i < 8; i++)
-		{
-			if ((TypesHatnames[kind].size()) > 0){
-				HHHat[i] = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())];
-			}
-			RandomNameByHat(i);
-		}
-
-	}
-}
-
 bool HWTeam::isNetTeam() const
 {
   return m_isNetTeam;
@@ -364,83 +278,4 @@
   return TeamName<t1.TeamName; // if names are equal - test if it is net team
 }
 
-void HWTeam::TypesLoad()
-{
 
-     QFile file(QString("%1/Names/types.ini").arg(datadir->absolutePath()));
-     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
-         TypesAvliable = FALSE;
-
-     int counter = 0; //counter starts with 0 (teamnames mode)
-     TypesTeamnames.append(QStringList());
-     TypesHatnames.append(QStringList());
-
-     QTextStream in(&file);
-     while (!in.atEnd()) {
-         QString line = in.readLine();
-         if (line == QString("#####")){
-             counter++; //toggle mode (teamnames || hats)
-             if ((counter%2) == 0){
-                 TypesTeamnames.append(QStringList());
-                 TypesHatnames.append(QStringList());
-             }
-         } else if ((line == QString("*****")) || (line == QString("*END*"))){
-             TypesAvliable = TRUE; return; // bye bye
-         } else {
-             if ((counter%2) == 0){ // even => teamnames mode
-                 TypesTeamnames[(counter/2)].append(line);
-             } else { // odd => hats mode
-                 TypesHatnames[((counter-1)/2)].append(line);
-             }
-         }
-//         Types.append(line);
-     }
-         TypesAvliable = TRUE;
-     return;
-}
-
-
-void HWTeam::DictLoad(const QString filename, QStringList &list)
-{
-     list.clear();
-
-     QFile file(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename));
-     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
-     {
-
-         QTextStream in(&file);
-         while (!in.atEnd()) {
-             QString line = in.readLine();
-             if(line != QString(""))
-                 {list.append(line);}
-         }
-     }
-
-     if (list.size()==0)
-         list.append(filename);
-
-}
-
-
-void HWTeam::HatCfgLoad(const QString hatname, QStringList &list)
-{
-     list.clear();
-
-     QFile file(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname));
-     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
-     {
-
-         QTextStream in(&file);
-         while (!in.atEnd()) {
-             QString line = in.readLine();
-             if(line != QString(""))
-                 {list.append(line);}
-         }
-     }
-
-     if (list.size()==0)
-         list.append(QString("generic"));
-
-}
-
-
--- a/QTfrontend/team.h	Sun Mar 22 14:32:22 2009 +0000
+++ b/QTfrontend/team.h	Sun Mar 22 14:35:45 2009 +0000
@@ -36,7 +36,7 @@
 	public:
 		HWTeam(const QString & teamname);
 		HWTeam(const QStringList& strLst);
-		HWTeam(quint8 num);
+		HWTeam();
 
 		bool isNetTeam() const;
 
@@ -59,22 +59,12 @@
 		void GetFromPage(HWForm * hwform);
 		QStringList TeamGameConfig(quint32 InitHealth) const;
 
-		void TeamRandomName(const int &i);
-		void TeamRandomNames(bool changeteamname);
-
 		bool operator==(const HWTeam& t1) const;
 		bool operator<(const HWTeam& t1) const;
 	private:
 		bool m_isNetTeam;
 		QString OldTeamName;
-		void RandomNameByHat(const int &i);
 
-		QList<QStringList> TypesTeamnames;
-		QList<QStringList> TypesHatnames;
-		bool TypesAvliable;
-		void TypesLoad();
-		void DictLoad(const QString filename, QStringList &list);
-		void HatCfgLoad(const QString hatname, QStringList &list);
 };
 
 #endif