QTfrontend/util/namegen.cpp
branchhedgeroid
changeset 6224 42b256eca362
parent 6212 a5d95f32e17f
child 6616 f77bb02b669f
equal deleted inserted replaced
6055:88cfcd9161d3 6224:42b256eca362
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2009 Martin Minarik <ttsmj@pokec.sk>
       
     4  * Copyright (c) 2009-2011 Andrey Korotaev <unC0Rr@gmail.com>
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License as published by
       
     8  * the Free Software Foundation; version 2 of the License
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    18  */
       
    19 
       
    20 #include <QFile>
       
    21 #include <QTextStream>
       
    22 #include <QStringList>
       
    23 #include <QLineEdit>
       
    24 
       
    25 #include "hwform.h"
       
    26 #include "HWDataManager.h"
       
    27 
       
    28 #include "namegen.h"
       
    29 
       
    30 HWNamegen::HWNamegen() {}
       
    31 
       
    32 QList<QStringList> HWNamegen::TypesTeamnames;
       
    33 QList<QStringList> HWNamegen::TypesHatnames;
       
    34 bool HWNamegen::typesAvailable = false;
       
    35 
       
    36 
       
    37 void HWNamegen::teamRandomNames(HWTeam & team, const bool changeteamname)
       
    38 {
       
    39     // load types if not already loaded
       
    40     if (!typesAvailable)
       
    41         if (!loadTypes())
       
    42             return; // abort if loading failed
       
    43 
       
    44     // abort if there are no hat types
       
    45     if (TypesHatnames.size() <= 0)
       
    46         return;
       
    47 
       
    48     // the hat will influence which names the hogs get
       
    49     int kind = (rand()%(TypesHatnames.size()));
       
    50 
       
    51     // pick team name based on hat
       
    52     if (changeteamname)
       
    53     {
       
    54         if (TypesTeamnames[kind].size() > 0)
       
    55             team.setName(TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())]);
       
    56 
       
    57         team.setGrave(getRandomGrave());
       
    58         team.setFort(getRandomFort());
       
    59         team.setVoicepack("Default");
       
    60     }
       
    61 
       
    62     QStringList dicts;
       
    63     QStringList dict;
       
    64 
       
    65     if ((TypesHatnames[kind].size()) <= 0)
       
    66     {
       
    67         dicts = dictsForHat(team.hedgehog(0).Hat);
       
    68         dict  = dictContents(dicts[rand()%(dicts.size())]);
       
    69     }
       
    70 
       
    71     for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
       
    72     {
       
    73         if ((TypesHatnames[kind].size()) > 0)
       
    74         {
       
    75             HWHog hh = team.hedgehog(i);
       
    76             hh.Hat = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())];
       
    77             team.setHedgehog(i,hh);
       
    78         }
       
    79 
       
    80         // there is a chance that this hog has the same hat as the previous one
       
    81         // let's reuse the hat-specific dict in this case
       
    82         if ((i == 0) or (team.hedgehog(i).Hat != team.hedgehog(i-1).Hat))
       
    83         {
       
    84             dicts = dictsForHat(team.hedgehog(i).Hat);
       
    85             dict  = dictContents(dicts[rand()%(dicts.size())]);
       
    86         }
       
    87 
       
    88         // give each hedgehog a random name
       
    89         HWNamegen::teamRandomName(team,i,dict);
       
    90     }
       
    91 
       
    92 }
       
    93 
       
    94 void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber)
       
    95 {
       
    96     QStringList dicts = dictsForHat(team.hedgehog(HedgehogNumber).Hat);
       
    97 
       
    98     QStringList dict = dictContents(dicts[rand()%(dicts.size())]);
       
    99 
       
   100     teamRandomName(team, HedgehogNumber, dict);
       
   101 }
       
   102 
       
   103 void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber, const QStringList & dict)
       
   104 {
       
   105     QStringList namesDict = dict;
       
   106 
       
   107     for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
       
   108     {
       
   109         namesDict.removeOne(team.hedgehog(i).Name);
       
   110     }
       
   111 
       
   112     // if our dict doesn't have any new names we'll have to use duplicates
       
   113     if (namesDict.size() < 1)
       
   114         namesDict = dict;
       
   115 
       
   116     HWHog hh = team.hedgehog(HedgehogNumber);
       
   117 
       
   118     hh.Name = namesDict[rand()%(namesDict.size())];
       
   119 
       
   120     team.setHedgehog(HedgehogNumber, hh);
       
   121 }
       
   122 
       
   123 QStringList HWNamegen::dictContents(const QString filename)
       
   124 {
       
   125     QStringList list;
       
   126 
       
   127     // find .txt to load the names from
       
   128     QFile * file = new QFile(HWDataManager::instance().findFileForRead(QString(
       
   129                                                 "Names/%1.txt").arg(filename)));
       
   130 
       
   131     if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
       
   132     {
       
   133         QTextStream in(file);
       
   134         while (!in.atEnd())
       
   135         {
       
   136             QString line = in.readLine();
       
   137             if(!line.isEmpty())
       
   138                 list.append(line);
       
   139         }
       
   140     }
       
   141 
       
   142     // this QFile isn't needed any further
       
   143     delete file;
       
   144 
       
   145     if (list.size() == 0)
       
   146          list.append(filename);
       
   147 
       
   148     return list;
       
   149 }
       
   150 
       
   151 
       
   152 QStringList HWNamegen::dictsForHat(const QString hatname)
       
   153 {
       
   154     QStringList list;
       
   155 
       
   156     // find .cfg to load the dicts from
       
   157     QFile * file = new QFile(HWDataManager::instance().findFileForRead(QString(
       
   158                                                 "Names/%1.cfg").arg(hatname)));
       
   159 
       
   160     if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
       
   161     {
       
   162         QTextStream in(file);
       
   163         while (!in.atEnd())
       
   164         {
       
   165             QString line = in.readLine();
       
   166             if(!line.isEmpty())
       
   167                 list.append(line);
       
   168         }
       
   169     }
       
   170 
       
   171     // this QFile isn't needed any further
       
   172     delete file;
       
   173 
       
   174     if (list.size() == 0)
       
   175          list.append(QString("generic"));
       
   176 
       
   177     return list;
       
   178 }
       
   179 
       
   180 // loades types from ini files. returns true on success.
       
   181 bool HWNamegen::loadTypes()
       
   182 {
       
   183     typesAvailable = false;
       
   184 
       
   185     // find .ini to load the names from
       
   186     QFile * file = new QFile(
       
   187         HWDataManager::instance().findFileForRead(QString("Names/types.ini")));
       
   188 
       
   189 
       
   190     if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
       
   191     {
       
   192 
       
   193         int counter = 0; //counter starts with 0 (teamnames mode)
       
   194         TypesTeamnames.append(QStringList());
       
   195         TypesHatnames.append(QStringList());
       
   196 
       
   197         QTextStream in(file);
       
   198         while (!in.atEnd())
       
   199         {
       
   200             QString line = in.readLine();
       
   201             if (line == QString("#####"))
       
   202             {
       
   203                 counter++; //toggle mode (teamnames || hats)
       
   204                 if ((counter%2) == 0)
       
   205                 {
       
   206                     TypesTeamnames.append(QStringList());
       
   207                     TypesHatnames.append(QStringList());
       
   208                 }
       
   209             }
       
   210             else if ((line == QString("*****")) || (line == QString("*END*")))
       
   211             {
       
   212                 typesAvailable = true;
       
   213                 return true; // bye bye
       
   214             }
       
   215             else
       
   216             {
       
   217                 if ((counter%2) == 0)
       
   218                 {
       
   219                     // even => teamnames mode
       
   220                     TypesTeamnames[(counter/2)].append(line);
       
   221                 }
       
   222                 else
       
   223                 {
       
   224                     // odd => hats mode
       
   225                     TypesHatnames[((counter-1)/2)].append(line);
       
   226                 }
       
   227             }
       
   228         }
       
   229 
       
   230         typesAvailable = true;
       
   231     }
       
   232 
       
   233     // this QFile isn't needed any further
       
   234     delete file;
       
   235 
       
   236     return typesAvailable;
       
   237 }
       
   238 
       
   239 
       
   240 
       
   241 QString HWNamegen::getRandomGrave()
       
   242 {
       
   243     QStringList Graves;
       
   244 
       
   245     //list all available Graves
       
   246     Graves.append(HWDataManager::instance().entryList(
       
   247                          "Graphics/Graves",
       
   248                          QDir::Files,
       
   249                          QStringList("*.png")
       
   250                      ).replaceInStrings(QRegExp("\\.png$"), "")
       
   251                  );
       
   252 
       
   253     if(Graves.size()==0)
       
   254     {
       
   255         // TODO do some serious error handling
       
   256         return "Error";
       
   257     }
       
   258 
       
   259     //pick a random grave
       
   260     return Graves[rand()%(Graves.size())];
       
   261 }
       
   262 
       
   263 QString HWNamegen::getRandomFort()
       
   264 {
       
   265     QStringList Forts;
       
   266 
       
   267     //list all available Forts
       
   268     Forts.append(HWDataManager::instance().entryList(
       
   269                         "Forts",
       
   270                         QDir::Files,
       
   271                         QStringList("*L.png")
       
   272                     ).replaceInStrings(QRegExp("L\\.png$"), "")
       
   273                 );
       
   274 
       
   275     if(Forts.size()==0)
       
   276     {
       
   277         // TODO do some serious error handling
       
   278         return "Error";
       
   279     }
       
   280 
       
   281     //pick a random fort
       
   282     return Forts[rand()%(Forts.size())];
       
   283 }