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