QTfrontend/namegen.cpp
changeset 1907 a104432e8301
child 2192 4763a778c033
equal deleted inserted replaced
1906:644f93d8f148 1907:a104432e8301
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2009 Martin Minarik <ttsmj@pokec.sk>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 #include <QFile>
       
    20 #include <QTextStream>
       
    21 #include <QApplication>
       
    22 #include <QStringList>
       
    23 #include <QLineEdit>
       
    24 #include "namegen.h"
       
    25 #include "hwform.h"
       
    26 #include "hwconsts.h"
       
    27 
       
    28 
       
    29 HWNamegen::HWNamegen() :
       
    30 	TypesAvliable(false)
       
    31 {
       
    32 
       
    33 	TypesLoad();
       
    34 }
       
    35 
       
    36 HWNamegen::~HWNamegen()
       
    37 {
       
    38 }
       
    39 
       
    40 
       
    41 
       
    42 void HWNamegen::TeamRandomName(HWTeam*& team, const int &i)
       
    43 {
       
    44 	RandomNameByHat(team,i);
       
    45 }
       
    46 
       
    47 void HWNamegen::TeamRandomNames(HWTeam*& team, const bool changeteamname)
       
    48 {
       
    49 	if ((TypesHatnames.size() > 0) && TypesAvliable){
       
    50 
       
    51 		int kind = (rand()%(TypesHatnames.size()));
       
    52 
       
    53 		if (changeteamname){
       
    54 			if (TypesTeamnames[kind].size() > 0){
       
    55 				team->TeamName = TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())];
       
    56 			}
       
    57 			team->Grave = "Simple"; // Todo: make it semi-random
       
    58 			team->Fort = "Island"; // Todo: make it semi-random
       
    59 		}
       
    60 
       
    61 		for(int i = 0; i < 8; i++)
       
    62 		{
       
    63 			if ((TypesHatnames[kind].size()) > 0){
       
    64 				team->HHHat[i] = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())];
       
    65 			}
       
    66 			RandomNameByHat(team,i);
       
    67 		}
       
    68 
       
    69 	}
       
    70 
       
    71 }
       
    72 
       
    73 
       
    74 void HWNamegen::RandomNameByHat(HWTeam*& team, const int &i)
       
    75 {
       
    76 	QStringList Dictionaries;
       
    77 	HatCfgLoad(team->HHHat[i],Dictionaries);
       
    78 
       
    79 	QStringList Dictionary;
       
    80 	DictLoad(Dictionaries[rand()%(Dictionaries.size())],Dictionary);
       
    81 
       
    82 	team->HHName[i] = Dictionary[rand()%(Dictionary.size())];
       
    83 }
       
    84 
       
    85 void HWNamegen::DictLoad(const QString filename, QStringList &list)
       
    86 {
       
    87      list.clear();
       
    88 
       
    89      QFile file(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename));
       
    90      if (file.open(QIODevice::ReadOnly | QIODevice::Text))
       
    91      {
       
    92 
       
    93          QTextStream in(&file);
       
    94          while (!in.atEnd()) {
       
    95              QString line = in.readLine();
       
    96              if(line != QString(""))
       
    97                  {list.append(line);}
       
    98          }
       
    99      }
       
   100 
       
   101      if (list.size()==0)
       
   102          list.append(filename);
       
   103 
       
   104 }
       
   105 
       
   106 
       
   107 void HWNamegen::HatCfgLoad(const QString hatname, QStringList &list)
       
   108 {
       
   109      list.clear();
       
   110 
       
   111      QFile file(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname));
       
   112      if (file.open(QIODevice::ReadOnly | QIODevice::Text))
       
   113      {
       
   114 
       
   115          QTextStream in(&file);
       
   116          while (!in.atEnd()) {
       
   117              QString line = in.readLine();
       
   118              if(line != QString(""))
       
   119                  {list.append(line);}
       
   120          }
       
   121      }
       
   122 
       
   123      if (list.size()==0)
       
   124          list.append(QString("generic"));
       
   125 
       
   126 }
       
   127 
       
   128 
       
   129 void HWNamegen::TypesLoad()
       
   130 {
       
   131 
       
   132      QFile file(QString("%1/Names/types.ini").arg(datadir->absolutePath()));
       
   133      if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
       
   134          {TypesAvliable = FALSE; return;}
       
   135 
       
   136      int counter = 0; //counter starts with 0 (teamnames mode)
       
   137      TypesTeamnames.append(QStringList());
       
   138      TypesHatnames.append(QStringList());
       
   139 
       
   140      QTextStream in(&file);
       
   141      while (!in.atEnd()) {
       
   142          QString line = in.readLine();
       
   143          if (line == QString("#####")){
       
   144              counter++; //toggle mode (teamnames || hats)
       
   145              if ((counter%2) == 0){
       
   146                  TypesTeamnames.append(QStringList());
       
   147                  TypesHatnames.append(QStringList());
       
   148              }
       
   149          } else if ((line == QString("*****")) || (line == QString("*END*"))){
       
   150              TypesAvliable = TRUE; return; // bye bye
       
   151          } else {
       
   152              if ((counter%2) == 0){ // even => teamnames mode
       
   153                  TypesTeamnames[(counter/2)].append(line);
       
   154              } else { // odd => hats mode
       
   155                  TypesHatnames[((counter-1)/2)].append(line);
       
   156              }
       
   157          }
       
   158 //         Types.append(line);
       
   159      }
       
   160          TypesAvliable = TRUE;
       
   161      return;
       
   162 }
       
   163 
       
   164