Use completely random hats when clicking on "Random hats" button in team editor
authorWuzzy <almikes@aol.com>
Sat, 23 Sep 2017 09:12:35 +0200
changeset 12506 92c597704e57
parent 12505 2e7a32c44ab3
child 12507 f4b0e164a0d5
Use completely random hats when clicking on "Random hats" button in team editor - Previously this button used the hat sets from types.txt
QTfrontend/util/namegen.cpp
QTfrontend/util/namegen.h
--- a/QTfrontend/util/namegen.cpp	Sat Sep 23 07:45:18 2017 +0200
+++ b/QTfrontend/util/namegen.cpp	Sat Sep 23 09:12:35 2017 +0200
@@ -77,8 +77,7 @@
 
     // pick team name based on hat
     if (mode == HWNamegen::rtmEverything)
-    {
-        team.setName(getRandomTeamName(kind));
+    {        team.setName(getRandomTeamName(kind));
         team.setGrave(getRandomGrave());
         team.setFort(getRandomFort());
         team.setFlag(getRandomFlag());
@@ -88,7 +87,7 @@
     QStringList dicts;
     QStringList dict;
 
-    if ((TypesHatnames[kind].size()) <= 0)
+    if ((mode == HWNamegen::rtmHogNames || mode == HWNamegen::rtmEverything) && (TypesHatnames[kind].size()) <= 0)
     {
         dicts = dictsForHat(team.hedgehog(0).Hat);
         dict  = dictContents(dicts[rand()%(dicts.size())]);
@@ -96,15 +95,20 @@
 
     for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
     {
-        if (((TypesHatnames[kind].size()) > 0) && (mode == HWNamegen::rtmEverything || mode == HWNamegen::rtmHats))
+        if (mode == HWNamegen::rtmEverything && (TypesHatnames[kind].size()) > 0)
         {
             HWHog hh = team.hedgehog(i);
             hh.Hat = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())];
             team.setHedgehog(i,hh);
         }
+        else if (mode == HWNamegen::rtmHats)
+        {
+            HWNamegen::teamRandomHat(team,i);
+        }
+
         // there is a chance that this hog has the same hat as the previous one
         // let's reuse the hat-specific dict in this case
-        if ((i == 0) || (team.hedgehog(i).Hat != team.hedgehog(i-1).Hat))
+        if ( (mode == HWNamegen::rtmHogNames || mode == HWNamegen::rtmEverything) && ((i == 0) || (team.hedgehog(i).Hat != team.hedgehog(i-1).Hat)))
         {
             dicts = dictsForHat(team.hedgehog(i).Hat);
             dict  = dictContents(dicts[rand()%(dicts.size())]);
@@ -117,6 +121,24 @@
 
 }
 
+void HWNamegen::teamRandomHat(HWTeam & team, const int HedgehogNumber, bool withDLC)
+{
+    HWHog hh = team.hedgehog(HedgehogNumber);
+
+    hh.Hat = getRandomHat(withDLC);
+
+    team.setHedgehog(HedgehogNumber, hh);
+}
+
+void HWNamegen::teamRandomHat(HWTeam & team, const int HedgehogNumber, const QStringList & dict)
+{
+    HWHog hh = team.hedgehog(HedgehogNumber);
+
+    hh.Name = dict[rand()%(dict.size())];
+
+    team.setHedgehog(HedgehogNumber, hh);
+}
+
 void HWNamegen::teamRandomHogName(HWTeam & team, const int HedgehogNumber)
 {
     QStringList dicts = dictsForHat(team.hedgehog(HedgehogNumber).Hat);
@@ -291,6 +313,29 @@
         return QString();
 }
 
+QString HWNamegen::getRandomHat(bool withDLC)
+{
+    QStringList Hats;
+
+    // list all available hats
+    Hats.append(DataManager::instance().entryList(
+                      "Graphics/Hats",
+                      QDir::Files,
+                      QStringList("*.png"),
+                      withDLC
+                  ).replaceInStrings(QRegExp("\\.png$"), "")
+                 );
+
+    if(Hats.size()==0)
+    {
+        // TODO do some serious error handling
+        return "Error";
+    }
+
+    // pick a random hat
+    return Hats[rand()%(Hats.size())];
+}
+
 QString HWNamegen::getRandomGrave(bool withDLC)
 {
     QStringList Graves;
--- a/QTfrontend/util/namegen.h	Sat Sep 23 07:45:18 2017 +0200
+++ b/QTfrontend/util/namegen.h	Sat Sep 23 09:12:35 2017 +0200
@@ -40,14 +40,15 @@
         static void teamRandomFort(HWTeam & team, bool withDLC = true);
         static void teamRandomFlag(HWTeam & team, bool withDLC = true);
         static void teamRandomVoice(HWTeam & team, bool withDLC = true);
+        static void teamRandomHat(HWTeam & team, const int HedgehogNumber, bool withDLC = true);
         static void teamRandomHogName(HWTeam & team, const int HedgehogNumber);
         static void teamRandomEverything(HWTeam & team, const enum RandomTeamMode mode);
-        static void teamRandomHats(HWTeam & team);
 
     private:
         HWNamegen();
 
         static QString getRandomTeamName(int kind);
+        static QString getRandomHat(bool withDLC = true);
         static QString getRandomGrave(bool withDLC = true);
         static QString getRandomFort(bool withDLC = true);
         static QString getRandomFlag(bool withDLC = true);
@@ -61,6 +62,7 @@
         static QStringList dictContents(const QString filename);
         static QStringList dictsForHat(const QString hatname);
 
+        static void teamRandomHat(HWTeam & team, const int HedgehogNumber, const QStringList & dict);
         static void teamRandomHogName(HWTeam & team, const int HedgehogNumber, const QStringList & dict);
 };