QTfrontend/namegen.cpp
changeset 6015 daffc14a518a
parent 5252 ded882439548
child 6024 d38da7c19e43
equal deleted inserted replaced
6014:b432c4b9cc9b 6015:daffc14a518a
    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     TypesAvliable(false)
    30     typesAvailable(false)
    31 {
    31 {
    32 
    32 
    33     TypesLoad();
    33     loadTypes();
    34 }
    34 }
    35 
    35 
    36 HWNamegen::~HWNamegen()
    36 HWNamegen::~HWNamegen()
    37 {
    37 {
    38 }
    38 }
    39 
    39 
    40 
    40 
    41 
    41 
    42 void HWNamegen::TeamRandomName(HWTeam*& team, const int HedgehogNumber)
    42 void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber)
    43 {
    43 {
    44     RandomNameByHat(team, HedgehogNumber);
    44     randomNameByHat(team, HedgehogNumber);
    45 }
    45 }
    46 
    46 
    47 void HWNamegen::TeamRandomNames(HWTeam*& team, const bool changeteamname)
    47 void HWNamegen::teamRandomNames(HWTeam & team, const bool changeteamname)
    48 {
    48 {
    49     if ((TypesHatnames.size() > 0) && TypesAvliable){
    49     if ((TypesHatnames.size() > 0) && typesAvailable){
    50 
    50 
    51         int kind = (rand()%(TypesHatnames.size()));
    51         int kind = (rand()%(TypesHatnames.size()));
    52 
    52 
    53         if (changeteamname){
    53         if (changeteamname){
    54             if (TypesTeamnames[kind].size() > 0){
    54             if (TypesTeamnames[kind].size() > 0){
    55                 team->TeamName = TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())];
    55                 team.setName(TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())]);
    56             }
    56             }
    57             team->Grave = GetRandomGrave();
    57             team.setGrave(getRandomGrave());
    58             team->Fort = GetRandomFort();
    58             team.setFort(getRandomFort());
    59             team->Voicepack = "Default";
    59             team.setVoicepack("Default");
    60         }
    60         }
    61 
    61 
    62         //give each hedgehog a random name:
    62         //give each hedgehog a random name:
    63         //TODO: load the dictionary only once! (right now it's loaded once for each hedgehog)
    63         //TODO: load the dictionary only once! (right now it's loaded once for each hedgehog)
    64         for(int i = 0; i < 8; i++)
    64         for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
    65         {
    65         {
    66             if ((TypesHatnames[kind].size()) > 0){
    66             if ((TypesHatnames[kind].size()) > 0){
    67                 team->Hedgehogs[i].Hat = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())];
    67                 HWHog hh = team.hedgehog(i);
    68             }
    68                 hh.Hat = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())];
    69             RandomNameByHat(team,i);
    69                 team.setHedgehog(i,hh);
    70         }
    70             }
    71 
    71             randomNameByHat(team,i);
    72     }
    72         }
    73 
    73 
    74 }
    74     }
    75 
    75 
    76 
    76 }
    77 void HWNamegen::RandomNameByHat(HWTeam*& team, const int HedgehogNumber)
    77 
       
    78 
       
    79 void HWNamegen::randomNameByHat(HWTeam & team, const int HedgehogNumber)
    78 {
    80 {
    79     QStringList Dictionaries;
    81     QStringList Dictionaries;
    80     HatCfgLoad(team->Hedgehogs[HedgehogNumber].Hat,Dictionaries);
    82     hatCfgLoad(team.hedgehog(HedgehogNumber).Hat,Dictionaries);
    81 
    83 
    82     QStringList Dictionary;
    84     QStringList Dictionary;
    83     DictLoad(Dictionaries[rand()%(Dictionaries.size())],Dictionary);
    85     dictLoad(Dictionaries[rand()%(Dictionaries.size())],Dictionary);
    84 
    86 
    85     team->Hedgehogs[HedgehogNumber].Name = Dictionary[rand()%(Dictionary.size())];
    87     HWHog hh = team.hedgehog(HedgehogNumber);
    86 }
    88     hh.Name = Dictionary[rand()%(Dictionary.size())];
    87 
    89     team.setHedgehog(HedgehogNumber, hh);
    88 void HWNamegen::DictLoad(const QString filename, QStringList &list)
    90 }
       
    91 
       
    92 void HWNamegen::dictLoad(const QString filename, QStringList &list)
    89 {
    93 {
    90     list.clear();
    94     list.clear();
    91 
    95 
    92     QFile file;
    96     QFile file;
    93     file.setFileName(QString("%1/Data/Names/%2.txt").arg(cfgdir->absolutePath()).arg(filename));
    97     file.setFileName(QString("%1/Data/Names/%2.txt").arg(cfgdir->absolutePath()).arg(filename));
   107          list.append(filename);
   111          list.append(filename);
   108 
   112 
   109 }
   113 }
   110 
   114 
   111 
   115 
   112 void HWNamegen::HatCfgLoad(const QString hatname, QStringList &list)
   116 void HWNamegen::hatCfgLoad(const QString hatname, QStringList &list)
   113 {
   117 {
   114     list.clear();
   118     list.clear();
   115 
   119 
   116     QFile file;
   120     QFile file;
   117     file.setFileName(QString("%1/Data/Names/%2.cfg").arg(cfgdir->absolutePath()).arg(hatname));
   121     file.setFileName(QString("%1/Data/Names/%2.cfg").arg(cfgdir->absolutePath()).arg(hatname));
   131          list.append(QString("generic"));
   135          list.append(QString("generic"));
   132 
   136 
   133 }
   137 }
   134 
   138 
   135 
   139 
   136 void HWNamegen::TypesLoad()
   140 void HWNamegen::loadTypes()
   137 {
   141 {
   138     QFile file;
   142     QFile file;
   139     file.setFileName(QString("%1/Data/Names/types.ini").arg(cfgdir->absolutePath()));
   143     file.setFileName(QString("%1/Data/Names/types.ini").arg(cfgdir->absolutePath()));
   140     if (!file.exists()) file.setFileName(QString("%1/Names/types.ini").arg(datadir->absolutePath()));
   144     if (!file.exists()) file.setFileName(QString("%1/Names/types.ini").arg(datadir->absolutePath()));
   141     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
   145     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
   142         {TypesAvliable = FALSE; return;}
   146         {typesAvailable = false; return;}
   143 
   147 
   144     int counter = 0; //counter starts with 0 (teamnames mode)
   148     int counter = 0; //counter starts with 0 (teamnames mode)
   145     TypesTeamnames.append(QStringList());
   149     TypesTeamnames.append(QStringList());
   146     TypesHatnames.append(QStringList());
   150     TypesHatnames.append(QStringList());
   147 
   151 
   153             if ((counter%2) == 0){
   157             if ((counter%2) == 0){
   154                 TypesTeamnames.append(QStringList());
   158                 TypesTeamnames.append(QStringList());
   155                 TypesHatnames.append(QStringList());
   159                 TypesHatnames.append(QStringList());
   156             }
   160             }
   157         } else if ((line == QString("*****")) || (line == QString("*END*"))){
   161         } else if ((line == QString("*****")) || (line == QString("*END*"))){
   158             TypesAvliable = TRUE; return; // bye bye
   162             typesAvailable = true; return; // bye bye
   159         } else {
   163         } else {
   160             if ((counter%2) == 0){ // even => teamnames mode
   164             if ((counter%2) == 0){ // even => teamnames mode
   161                 TypesTeamnames[(counter/2)].append(line);
   165                 TypesTeamnames[(counter/2)].append(line);
   162             } else { // odd => hats mode
   166             } else { // odd => hats mode
   163                 TypesHatnames[((counter-1)/2)].append(line);
   167                 TypesHatnames[((counter-1)/2)].append(line);
   164             }
   168             }
   165         }
   169         }
   166 //        Types.append(line);
   170 //        Types.append(line);
   167     }
   171     }
   168         TypesAvliable = TRUE;
   172         typesAvailable = true;
   169     return;
   173     return;
   170 }
   174 }
   171 
   175 
   172 
   176 
   173 
   177 
   174 QString HWNamegen::GetRandomGrave()
   178 QString HWNamegen::getRandomGrave()
   175 {
   179 {
   176     QStringList Graves;
   180     QStringList Graves;
   177 
   181 
   178     //list all available Graves
   182     //list all available Graves
   179     QDir tmpdir;
   183     QDir tmpdir;
   197 
   201 
   198     //pick a random grave
   202     //pick a random grave
   199     return Graves[rand()%(Graves.size())];
   203     return Graves[rand()%(Graves.size())];
   200 }
   204 }
   201 
   205 
   202 QString HWNamegen::GetRandomFort()
   206 QString HWNamegen::getRandomFort()
   203 {
   207 {
   204     QStringList Forts;
   208     QStringList Forts;
   205 
   209 
   206     //list all available Forts
   210     //list all available Forts
   207     QDir tmpdir;
   211     QDir tmpdir;