QTfrontend/util/namegen.cpp
author unc0rr
Tue, 27 Sep 2011 19:27:19 +0400
changeset 6068 e18713ecf1e0
parent 6060 fdfc01419815
child 6160 863d3edf5690
permissions -rw-r--r--
Introduce EngineInteraction module
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>
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 3344
diff changeset
     4
 * Copyright (c) 2009-2011 Andrey Korotaev <unC0Rr@gmail.com>
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
     5
 *
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
     8
 * the Free Software Foundation; version 2 of the License
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
     9
 *
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    13
 * GNU General Public License for more details.
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    14
 *
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    18
 */
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    19
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    20
#include <QFile>
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    21
#include <QTextStream>
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
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    29
HWNamegen::HWNamegen() {}
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    30
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    31
QList<QStringList> HWNamegen::TypesTeamnames;
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    32
QList<QStringList> HWNamegen::TypesHatnames;
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    33
bool HWNamegen::typesAvailable = false;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    34
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    35
6015
daffc14a518a cleaning up a little bit more, especially team class. we were leaking teams into heap memory on quick game starts btw
sheepluva
parents: 5252
diff changeset
    36
void HWNamegen::teamRandomNames(HWTeam & team, const bool changeteamname)
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    37
{
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    38
    // load types if not already loaded
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    39
    if (!typesAvailable)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    40
        if (!loadTypes())
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    41
            return; // abort if loading failed
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    42
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    43
    // abort if there are no hat types
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    44
    if (TypesHatnames.size() <= 0)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    45
        return;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    46
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    47
    // the hat will influence which names the hogs get
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    48
    int kind = (rand()%(TypesHatnames.size()));
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    49
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    50
    // pick team name based on hat
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    51
    if (changeteamname)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    52
    {
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    53
        if (TypesTeamnames[kind].size() > 0)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    54
            team.setName(TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())]);
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    55
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    56
        team.setGrave(getRandomGrave());
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    57
        team.setFort(getRandomFort());
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    58
        team.setVoicepack("Default");
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    59
    }
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    60
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    61
    QStringList dicts;
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    62
    QStringList dict;
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    63
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    64
    if ((TypesHatnames[kind].size()) <= 0)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    65
    {
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    66
        dicts = dictsForHat(team.hedgehog(0).Hat);
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    67
        dict  = dictContents(dicts[rand()%(dicts.size())]);
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    68
    }
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    69
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    70
    for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    71
    {
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    72
        if ((TypesHatnames[kind].size()) > 0)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    73
        {
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    74
            HWHog hh = team.hedgehog(i);
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    75
            hh.Hat = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())];
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    76
            team.setHedgehog(i,hh);
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2192
diff changeset
    77
        }
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    78
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    79
        // there is a chance that this hog has the same hat as the previous one
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    80
        // let's reuse the hat-specific dict in this case
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    81
        if ((i == 0) or (team.hedgehog(i).Hat != team.hedgehog(i-1).Hat))
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2192
diff changeset
    82
        {
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    83
            dicts = dictsForHat(team.hedgehog(i).Hat);
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    84
            dict  = dictContents(dicts[rand()%(dicts.size())]);
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2192
diff changeset
    85
        }
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    86
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    87
        // give each hedgehog a random name
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    88
        HWNamegen::teamRandomName(team,i,dict);
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2192
diff changeset
    89
    }
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    90
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    91
}
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    92
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    93
void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    94
{
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    95
    QStringList dicts = dictsForHat(team.hedgehog(HedgehogNumber).Hat);
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    96
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    97
    QStringList dict = dictContents(dicts[rand()%(dicts.size())]);
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    98
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    99
    teamRandomName(team, HedgehogNumber, dict);
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   100
}
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   101
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   102
void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber, const QStringList & dict)
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   103
{
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   104
    QStringList namesDict = dict;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   105
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   106
    for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   107
    {
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   108
        namesDict.removeOne(team.hedgehog(i).Name);
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   109
    }
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   110
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   111
    // if our dict doesn't have any new names we'll have to use duplicates
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   112
    if (namesDict.size() < 1)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   113
        namesDict = dict;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   114
6015
daffc14a518a cleaning up a little bit more, especially team class. we were leaking teams into heap memory on quick game starts btw
sheepluva
parents: 5252
diff changeset
   115
    HWHog hh = team.hedgehog(HedgehogNumber);
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   116
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   117
    hh.Name = namesDict[rand()%(namesDict.size())];
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   118
6015
daffc14a518a cleaning up a little bit more, especially team class. we were leaking teams into heap memory on quick game starts btw
sheepluva
parents: 5252
diff changeset
   119
    team.setHedgehog(HedgehogNumber, hh);
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   120
}
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   121
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   122
QStringList HWNamegen::dictContents(const QString filename)
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   123
{
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   124
    QStringList list;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   125
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   126
    QFile file;
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   127
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   128
    // find .cfg to load the names from
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   129
    file.setFileName(QString("%1/Data/Names/%2.txt").arg(cfgdir->absolutePath()).arg(filename));
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   130
    if (!file.exists())
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   131
        file.setFileName(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename));
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   132
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   133
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   134
    {
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   135
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   136
        QTextStream in(&file);
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   137
        while (!in.atEnd())
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   138
        {
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   139
            QString line = in.readLine();
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   140
            if(!line.isEmpty())
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   141
                list.append(line);
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   142
        }
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   143
    }
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   144
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   145
    if (list.size() == 0)
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   146
         list.append(filename);
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   147
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   148
    return list;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   149
}
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   150
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   151
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   152
QStringList HWNamegen::dictsForHat(const QString hatname)
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   153
{
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   154
    QStringList list;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   155
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   156
    QFile file;
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   157
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   158
    // find .cfg to load the names from
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   159
    file.setFileName(QString("%1/Data/Names/%2.cfg").arg(cfgdir->absolutePath()).arg(hatname));
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   160
    if (!file.exists())
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   161
        file.setFileName(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname));
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   162
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   163
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   164
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   165
    {
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   166
        QTextStream in(&file);
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   167
        while (!in.atEnd())
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   168
        {
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   169
            QString line = in.readLine();
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   170
            if(!line.isEmpty())
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   171
                list.append(line);
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   172
        }
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   173
    }
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   174
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   175
    if (list.size() == 0)
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   176
         list.append(QString("generic"));
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   177
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   178
    return list;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   179
}
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   180
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   181
// loades types from ini files. returns true on success.
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   182
bool HWNamegen::loadTypes()
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   183
{
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   184
    QFile file;
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   185
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   186
    // find .cfg to load the names from
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   187
    file.setFileName(QString("%1/Data/Names/types.ini").arg(cfgdir->absolutePath()));
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   188
    if (!file.exists())
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   189
        file.setFileName(QString("%1/Names/types.ini").arg(datadir->absolutePath()));
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   190
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   191
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   192
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   193
        return false;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   194
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   195
    int counter = 0; //counter starts with 0 (teamnames mode)
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   196
    TypesTeamnames.append(QStringList());
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   197
    TypesHatnames.append(QStringList());
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   198
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   199
    QTextStream in(&file);
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   200
    while (!in.atEnd())
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   201
    {
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   202
        QString line = in.readLine();
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   203
        if (line == QString("#####"))
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   204
        {
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   205
            counter++; //toggle mode (teamnames || hats)
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   206
            if ((counter%2) == 0)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   207
            {
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   208
                TypesTeamnames.append(QStringList());
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   209
                TypesHatnames.append(QStringList());
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   210
            }
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   211
        }
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   212
        else if ((line == QString("*****")) || (line == QString("*END*")))
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   213
        {
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   214
            typesAvailable = true;
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   215
            return true; // bye bye
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   216
        }
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   217
        else
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   218
        {
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   219
            if ((counter%2) == 0)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   220
            {
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   221
                // even => teamnames mode
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   222
                TypesTeamnames[(counter/2)].append(line);
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   223
            }
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   224
            else
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   225
            {
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   226
                // odd => hats mode
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   227
                TypesHatnames[((counter-1)/2)].append(line);
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   228
            }
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   229
        }
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   230
    }
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   231
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   232
    typesAvailable = true;
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   233
    return true;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   234
}
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   235
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   236
5114
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   237
6015
daffc14a518a cleaning up a little bit more, especially team class. we were leaking teams into heap memory on quick game starts btw
sheepluva
parents: 5252
diff changeset
   238
QString HWNamegen::getRandomGrave()
5114
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   239
{
5115
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   240
    QStringList Graves;
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   241
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   242
    //list all available Graves
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   243
    QDir tmpdir;
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   244
    tmpdir.cd(cfgdir->absolutePath());
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   245
    tmpdir.cd("Data/Graphics/Graves");
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   246
    tmpdir.setFilter(QDir::Files);
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   247
    Graves.append(tmpdir.entryList(QStringList("*.png")).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1"));
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   248
5114
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   249
    tmpdir.cd(datadir->absolutePath());
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   250
    tmpdir.cd("Graphics/Graves");
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   251
    tmpdir.setFilter(QDir::Files);
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   252
    QStringList tmpList = tmpdir.entryList(QStringList("*.png")).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1");
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   253
    for (QStringList::Iterator it = tmpList.begin(); it != tmpList.end(); ++it) 
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5115
diff changeset
   254
        if (!Graves.contains(*it,Qt::CaseInsensitive)) Graves.append(*it);
5114
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   255
5115
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   256
    if(Graves.size()==0)
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   257
    {
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   258
        //do some serious error handling
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   259
        return "Error";
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   260
    }
5114
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   261
5115
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   262
    //pick a random grave
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   263
    return Graves[rand()%(Graves.size())];
5114
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   264
}
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   265
6015
daffc14a518a cleaning up a little bit more, especially team class. we were leaking teams into heap memory on quick game starts btw
sheepluva
parents: 5252
diff changeset
   266
QString HWNamegen::getRandomFort()
5114
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   267
{
5115
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   268
    QStringList Forts;
5114
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   269
5115
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   270
    //list all available Forts
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   271
    QDir tmpdir;
5114
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   272
    tmpdir.cd(datadir->absolutePath());
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   273
    tmpdir.cd("Forts");
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   274
    tmpdir.setFilter(QDir::Files);
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   275
    Forts.append(tmpdir.entryList(QStringList("*L.png")).replaceInStrings(QRegExp("^(.*)L\\.png"), "\\1"));
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   276
5115
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   277
    if(Forts.size()==0)
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   278
    {
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   279
        //do some serious error handling
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   280
        return "Error";
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   281
    }
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   282
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   283
    //pick a random fort
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   284
    return Forts[rand()%(Forts.size())];
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   285
}