QTfrontend/namegen.cpp
changeset 6024 d38da7c19e43
parent 6015 daffc14a518a
equal deleted inserted replaced
6022:8ed6e810051a 6024:d38da7c19e43
    24 #include "namegen.h"
    24 #include "namegen.h"
    25 #include "hwform.h"
    25 #include "hwform.h"
    26 #include "hwconsts.h"
    26 #include "hwconsts.h"
    27 
    27 
    28 
    28 
    29 HWNamegen::HWNamegen() :
    29 HWNamegen::HWNamegen() {}
    30     typesAvailable(false)
    30 
    31 {
    31 QList<QStringList> HWNamegen::TypesTeamnames;
    32 
    32 QList<QStringList> HWNamegen::TypesHatnames;
    33     loadTypes();
    33 bool HWNamegen::typesAvailable = false;
    34 }
    34 
    35 
    35 
    36 HWNamegen::~HWNamegen()
    36 void HWNamegen::teamRandomNames(HWTeam & team, const bool changeteamname)
    37 {
    37 {
    38 }
    38     // load types if not already loaded
    39 
    39     if (!typesAvailable)
    40 
    40         if (!loadTypes())
       
    41             return; // abort if loading failed
       
    42 
       
    43     // abort if there are no hat types
       
    44     if (TypesHatnames.size() <= 0)
       
    45         return;
       
    46 
       
    47     // the hat will influence which names the hogs get
       
    48     int kind = (rand()%(TypesHatnames.size()));
       
    49 
       
    50     // pick team name based on hat
       
    51     if (changeteamname)
       
    52     {
       
    53         if (TypesTeamnames[kind].size() > 0)
       
    54             team.setName(TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())]);
       
    55 
       
    56         team.setGrave(getRandomGrave());
       
    57         team.setFort(getRandomFort());
       
    58         team.setVoicepack("Default");
       
    59     }
       
    60 
       
    61     QStringList dicts;
       
    62     QStringList dict;
       
    63 
       
    64     if ((TypesHatnames[kind].size()) <= 0)
       
    65     {
       
    66         dicts = dictsForHat(team.hedgehog(0).Hat);
       
    67         dict  = dictContents(dicts[rand()%(dicts.size())]);
       
    68     }
       
    69 
       
    70     for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
       
    71     {
       
    72         if ((TypesHatnames[kind].size()) > 0)
       
    73         {
       
    74             HWHog hh = team.hedgehog(i);
       
    75             hh.Hat = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())];
       
    76             team.setHedgehog(i,hh);
       
    77         }
       
    78 
       
    79         // there is a chance that this hog has the same hat as the previous one
       
    80         // let's reuse the hat-specific dict in this case
       
    81         if ((i == 0) or (team.hedgehog(i).Hat != team.hedgehog(i-1).Hat))
       
    82         {
       
    83             dicts = dictsForHat(team.hedgehog(i).Hat);
       
    84             dict  = dictContents(dicts[rand()%(dicts.size())]);
       
    85         }
       
    86 
       
    87         // give each hedgehog a random name
       
    88         HWNamegen::teamRandomName(team,i,dict);
       
    89     }
       
    90 
       
    91 }
    41 
    92 
    42 void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber)
    93 void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber)
    43 {
    94 {
    44     randomNameByHat(team, HedgehogNumber);
    95     QStringList dicts = dictsForHat(team.hedgehog(HedgehogNumber).Hat);
    45 }
    96 
    46 
    97     QStringList dict = dictContents(dicts[rand()%(dicts.size())]);
    47 void HWNamegen::teamRandomNames(HWTeam & team, const bool changeteamname)
    98 
    48 {
    99     teamRandomName(team, HedgehogNumber, dict);
    49     if ((TypesHatnames.size() > 0) && typesAvailable){
   100 }
    50 
   101 
    51         int kind = (rand()%(TypesHatnames.size()));
   102 void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber, const QStringList & dict)
    52 
   103 {
    53         if (changeteamname){
   104     QStringList namesDict = dict;
    54             if (TypesTeamnames[kind].size() > 0){
   105 
    55                 team.setName(TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())]);
   106     for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
    56             }
   107     {
    57             team.setGrave(getRandomGrave());
   108         namesDict.removeOne(team.hedgehog(i).Name);
    58             team.setFort(getRandomFort());
   109     }
    59             team.setVoicepack("Default");
   110 
    60         }
   111     // if our dict doesn't have any new names we'll have to use duplicates
    61 
   112     if (namesDict.size() < 1)
    62         //give each hedgehog a random name:
   113         namesDict = dict;
    63         //TODO: load the dictionary only once! (right now it's loaded once for each hedgehog)
       
    64         for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
       
    65         {
       
    66             if ((TypesHatnames[kind].size()) > 0){
       
    67                 HWHog hh = team.hedgehog(i);
       
    68                 hh.Hat = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())];
       
    69                 team.setHedgehog(i,hh);
       
    70             }
       
    71             randomNameByHat(team,i);
       
    72         }
       
    73 
       
    74     }
       
    75 
       
    76 }
       
    77 
       
    78 
       
    79 void HWNamegen::randomNameByHat(HWTeam & team, const int HedgehogNumber)
       
    80 {
       
    81     QStringList Dictionaries;
       
    82     hatCfgLoad(team.hedgehog(HedgehogNumber).Hat,Dictionaries);
       
    83 
       
    84     QStringList Dictionary;
       
    85     dictLoad(Dictionaries[rand()%(Dictionaries.size())],Dictionary);
       
    86 
   114 
    87     HWHog hh = team.hedgehog(HedgehogNumber);
   115     HWHog hh = team.hedgehog(HedgehogNumber);
    88     hh.Name = Dictionary[rand()%(Dictionary.size())];
   116 
       
   117     hh.Name = namesDict[rand()%(namesDict.size())];
       
   118 
    89     team.setHedgehog(HedgehogNumber, hh);
   119     team.setHedgehog(HedgehogNumber, hh);
    90 }
   120 }
    91 
   121 
    92 void HWNamegen::dictLoad(const QString filename, QStringList &list)
   122 QStringList HWNamegen::dictContents(const QString filename)
    93 {
   123 {
    94     list.clear();
   124     QStringList list;
    95 
   125 
    96     QFile file;
   126     QFile file;
       
   127 
       
   128     // find .cfg to load the names from
    97     file.setFileName(QString("%1/Data/Names/%2.txt").arg(cfgdir->absolutePath()).arg(filename));
   129     file.setFileName(QString("%1/Data/Names/%2.txt").arg(cfgdir->absolutePath()).arg(filename));
    98     if (!file.exists()) file.setFileName(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename));
   130     if (!file.exists())
       
   131         file.setFileName(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename));
       
   132 
    99     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
   133     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
   100     {
   134     {
   101 
   135 
   102         QTextStream in(&file);
   136         QTextStream in(&file);
   103         while (!in.atEnd()) {
   137         while (!in.atEnd())
       
   138         {
   104             QString line = in.readLine();
   139             QString line = in.readLine();
   105             if(line != QString(""))
   140             if(!line.isEmpty())
   106                 {list.append(line);}
   141                 list.append(line);
   107         }
   142         }
   108     }
   143     }
   109 
   144 
   110     if (list.size()==0)
   145     if (list.size() == 0)
   111          list.append(filename);
   146          list.append(filename);
   112 
   147 
   113 }
   148     return list;
   114 
   149 }
   115 
   150 
   116 void HWNamegen::hatCfgLoad(const QString hatname, QStringList &list)
   151 
   117 {
   152 QStringList HWNamegen::dictsForHat(const QString hatname)
   118     list.clear();
   153 {
       
   154     QStringList list;
   119 
   155 
   120     QFile file;
   156     QFile file;
       
   157 
       
   158     // find .cfg to load the names from
   121     file.setFileName(QString("%1/Data/Names/%2.cfg").arg(cfgdir->absolutePath()).arg(hatname));
   159     file.setFileName(QString("%1/Data/Names/%2.cfg").arg(cfgdir->absolutePath()).arg(hatname));
   122     if (!file.exists()) file.setFileName(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname));
   160     if (!file.exists())
       
   161         file.setFileName(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname));
       
   162 
       
   163 
   123     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
   164     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
   124     {
   165     {
   125 
       
   126         QTextStream in(&file);
   166         QTextStream in(&file);
   127         while (!in.atEnd()) {
   167         while (!in.atEnd())
       
   168         {
   128             QString line = in.readLine();
   169             QString line = in.readLine();
   129             if(line != QString(""))
   170             if(!line.isEmpty())
   130                 {list.append(line);}
   171                 list.append(line);
   131         }
   172         }
   132     }
   173     }
   133 
   174 
   134     if (list.size()==0)
   175     if (list.size() == 0)
   135          list.append(QString("generic"));
   176          list.append(QString("generic"));
   136 
   177 
   137 }
   178     return list;
   138 
   179 }
   139 
   180 
   140 void HWNamegen::loadTypes()
   181 // loades types from ini files. returns true on success.
       
   182 bool HWNamegen::loadTypes()
   141 {
   183 {
   142     QFile file;
   184     QFile file;
       
   185 
       
   186     // find .cfg to load the names from
   143     file.setFileName(QString("%1/Data/Names/types.ini").arg(cfgdir->absolutePath()));
   187     file.setFileName(QString("%1/Data/Names/types.ini").arg(cfgdir->absolutePath()));
   144     if (!file.exists()) file.setFileName(QString("%1/Names/types.ini").arg(datadir->absolutePath()));
   188     if (!file.exists())
       
   189         file.setFileName(QString("%1/Names/types.ini").arg(datadir->absolutePath()));
       
   190 
       
   191 
   145     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
   192     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
   146         {typesAvailable = false; return;}
   193         return false;
   147 
   194 
   148     int counter = 0; //counter starts with 0 (teamnames mode)
   195     int counter = 0; //counter starts with 0 (teamnames mode)
   149     TypesTeamnames.append(QStringList());
   196     TypesTeamnames.append(QStringList());
   150     TypesHatnames.append(QStringList());
   197     TypesHatnames.append(QStringList());
   151 
   198 
   152     QTextStream in(&file);
   199     QTextStream in(&file);
   153     while (!in.atEnd()) {
   200     while (!in.atEnd())
       
   201     {
   154         QString line = in.readLine();
   202         QString line = in.readLine();
   155         if (line == QString("#####")){
   203         if (line == QString("#####"))
       
   204         {
   156             counter++; //toggle mode (teamnames || hats)
   205             counter++; //toggle mode (teamnames || hats)
   157             if ((counter%2) == 0){
   206             if ((counter%2) == 0)
       
   207             {
   158                 TypesTeamnames.append(QStringList());
   208                 TypesTeamnames.append(QStringList());
   159                 TypesHatnames.append(QStringList());
   209                 TypesHatnames.append(QStringList());
   160             }
   210             }
   161         } else if ((line == QString("*****")) || (line == QString("*END*"))){
   211         }
   162             typesAvailable = true; return; // bye bye
   212         else if ((line == QString("*****")) || (line == QString("*END*")))
   163         } else {
   213         {
   164             if ((counter%2) == 0){ // even => teamnames mode
   214             typesAvailable = true;
       
   215             return true; // bye bye
       
   216         }
       
   217         else
       
   218         {
       
   219             if ((counter%2) == 0)
       
   220             {
       
   221                 // even => teamnames mode
   165                 TypesTeamnames[(counter/2)].append(line);
   222                 TypesTeamnames[(counter/2)].append(line);
   166             } else { // odd => hats mode
   223             }
       
   224             else
       
   225             {
       
   226                 // odd => hats mode
   167                 TypesHatnames[((counter-1)/2)].append(line);
   227                 TypesHatnames[((counter-1)/2)].append(line);
   168             }
   228             }
   169         }
   229         }
   170 //        Types.append(line);
   230     }
   171     }
   231 
   172         typesAvailable = true;
   232     typesAvailable = true;
   173     return;
   233     return true;
   174 }
   234 }
   175 
   235 
   176 
   236 
   177 
   237 
   178 QString HWNamegen::getRandomGrave()
   238 QString HWNamegen::getRandomGrave()