QTfrontend/util/namegen.cpp
author unc0rr
Sun, 25 Nov 2012 22:17:11 +0400
branchflibqtfrontend
changeset 8103 c247346d296f
parent 8049 133e22b5c410
child 8126 5bfa1b2025d6
permissions -rw-r--r--
Convert HWTeam to be a wrapper to flib_team
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>
6952
7f70f37bbf08 license header year range adjustments
sheepluva
parents: 6930
diff changeset
     4
 * Copyright (c) 2004-2012 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>
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
    24
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    25
#include "hwform.h"
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6824
diff changeset
    26
#include "DataManager.h"
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    27
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
    28
#include "namegen.h"
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    29
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    30
HWNamegen::HWNamegen() {}
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    31
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    32
QList<QStringList> HWNamegen::TypesTeamnames;
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    33
QList<QStringList> HWNamegen::TypesHatnames;
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    34
bool HWNamegen::typesAvailable = false;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    35
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    36
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
    37
void HWNamegen::teamRandomNames(HWTeam & team, const bool changeteamname)
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    38
{
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    39
    // load types if not already loaded
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    40
    if (!typesAvailable)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    41
        if (!loadTypes())
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    42
            return; // abort if loading failed
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    43
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    44
    // abort if there are no hat types
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    45
    if (TypesHatnames.size() <= 0)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    46
        return;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    47
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    48
    // the hat will influence which names the hogs get
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    49
    int kind = (rand()%(TypesHatnames.size()));
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    50
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    51
    // pick team name based on hat
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    52
    if (changeteamname)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    53
    {
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    54
        if (TypesTeamnames[kind].size() > 0)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    55
            team.setName(TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())]);
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    56
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    57
        team.setGrave(getRandomGrave());
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    58
        team.setFort(getRandomFort());
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    59
        team.setVoicepack("Default");
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    60
    }
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    61
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    62
    QStringList dicts;
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    63
    QStringList dict;
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    64
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    65
    if ((TypesHatnames[kind].size()) <= 0)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    66
    {
8103
c247346d296f Convert HWTeam to be a wrapper to flib_team
unc0rr
parents: 8049
diff changeset
    67
        dicts = dictsForHat(team.hedgehogHat(0));
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    68
        dict  = dictContents(dicts[rand()%(dicts.size())]);
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    69
    }
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    70
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    71
    for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    72
    {
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    73
        if ((TypesHatnames[kind].size()) > 0)
8103
c247346d296f Convert HWTeam to be a wrapper to flib_team
unc0rr
parents: 8049
diff changeset
    74
            team.setHedgehogHat(i, TypesHatnames[kind][rand()%(TypesHatnames[kind].size())]);
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    75
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    76
        // 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
    77
        // let's reuse the hat-specific dict in this case
8103
c247346d296f Convert HWTeam to be a wrapper to flib_team
unc0rr
parents: 8049
diff changeset
    78
        if ((i == 0) || (team.hedgehogHat(i) != team.hedgehogHat(i-1)))
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2192
diff changeset
    79
        {
8103
c247346d296f Convert HWTeam to be a wrapper to flib_team
unc0rr
parents: 8049
diff changeset
    80
            dicts = dictsForHat(team.hedgehogHat(i));
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    81
            dict  = dictContents(dicts[rand()%(dicts.size())]);
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2192
diff changeset
    82
        }
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    83
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    84
        // give each hedgehog a random name
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    85
        HWNamegen::teamRandomName(team,i,dict);
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2192
diff changeset
    86
    }
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    87
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    88
}
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    89
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    90
void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    91
{
8103
c247346d296f Convert HWTeam to be a wrapper to flib_team
unc0rr
parents: 8049
diff changeset
    92
    QStringList dicts = dictsForHat(team.hedgehogHat(HedgehogNumber));
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
    93
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    94
    QStringList dict = dictContents(dicts[rand()%(dicts.size())]);
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    95
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    96
    teamRandomName(team, HedgehogNumber, dict);
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    97
}
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    98
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
    99
void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber, const QStringList & dict)
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   100
{
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   101
    QStringList namesDict = dict;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   102
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   103
    for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   104
    {
8103
c247346d296f Convert HWTeam to be a wrapper to flib_team
unc0rr
parents: 8049
diff changeset
   105
        namesDict.removeOne(team.hedgehogName(i));
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   106
    }
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   107
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   108
    // 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
   109
    if (namesDict.size() < 1)
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   110
        namesDict = dict;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   111
8103
c247346d296f Convert HWTeam to be a wrapper to flib_team
unc0rr
parents: 8049
diff changeset
   112
    team.setHedgehogName(HedgehogNumber, namesDict[rand()%(namesDict.size())]);
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   113
}
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   114
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   115
QStringList HWNamegen::dictContents(const QString filename)
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   116
{
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   117
    QStringList list;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   118
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   119
    // find .txt to load the names from
8049
133e22b5c410 Get rid of DataManager::findFileForRead
unc0rr
parents: 6952
diff changeset
   120
    QFile * file = new QFile(QString("physfs://Names/%1.txt").arg(filename));
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   121
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   122
    if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
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
   123
    {
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   124
        QTextStream in(file);
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   125
        while (!in.atEnd())
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   126
        {
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
   127
            QString line = in.readLine();
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   128
            if(!line.isEmpty())
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   129
                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
   130
        }
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
   131
    }
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   132
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   133
    // this QFile isn't needed any further
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   134
    delete file;
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   135
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   136
    if (list.size() == 0)
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6212
diff changeset
   137
        list.append(filename);
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   138
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   139
    return list;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   140
}
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   141
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   142
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   143
QStringList HWNamegen::dictsForHat(const QString hatname)
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
    QStringList list;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   146
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   147
    // find .cfg to load the dicts from
8049
133e22b5c410 Get rid of DataManager::findFileForRead
unc0rr
parents: 6952
diff changeset
   148
    QFile * file = new QFile(QString("physfs://Names/%1.cfg").arg(hatname));
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   149
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   150
    if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
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
   151
    {
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   152
        QTextStream in(file);
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   153
        while (!in.atEnd())
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   154
        {
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
   155
            QString line = in.readLine();
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   156
            if(!line.isEmpty())
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   157
                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
   158
        }
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
    }
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   160
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   161
    // this QFile isn't needed any further
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   162
    delete file;
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   163
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   164
    if (list.size() == 0)
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6212
diff changeset
   165
        list.append(QString("generic"));
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   166
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   167
    return list;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   168
}
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   169
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   170
// loades types from ini files. returns true on success.
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   171
bool HWNamegen::loadTypes()
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   172
{
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   173
    typesAvailable = false;
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   174
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   175
    // find .ini to load the names from
8049
133e22b5c410 Get rid of DataManager::findFileForRead
unc0rr
parents: 6952
diff changeset
   176
    QFile * file = new QFile(QString("physfs://Names/types.ini"));
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   177
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   178
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   179
    if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   180
    {
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   181
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   182
        int counter = 0; //counter starts with 0 (teamnames mode)
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   183
        TypesTeamnames.append(QStringList());
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   184
        TypesHatnames.append(QStringList());
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   185
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   186
        QTextStream in(file);
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   187
        while (!in.atEnd())
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   188
        {
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   189
            QString line = in.readLine();
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   190
            if (line == QString("#####"))
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   191
            {
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   192
                counter++; //toggle mode (teamnames || hats)
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   193
                if ((counter%2) == 0)
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   194
                {
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   195
                    TypesTeamnames.append(QStringList());
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   196
                    TypesHatnames.append(QStringList());
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   197
                }
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
   198
            }
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   199
            else if ((line == QString("*****")) || (line == QString("*END*")))
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   200
            {
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   201
                typesAvailable = true;
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   202
                return true; // bye bye
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   203
            }
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   204
            else
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   205
            {
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   206
                if ((counter%2) == 0)
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   207
                {
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   208
                    // even => teamnames mode
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   209
                    TypesTeamnames[(counter/2)].append(line);
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   210
                }
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   211
                else
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   212
                {
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   213
                    // odd => hats mode
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   214
                    TypesHatnames[((counter-1)/2)].append(line);
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   215
                }
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
   216
            }
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
   217
        }
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   218
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   219
        typesAvailable = true;
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
   220
    }
6024
d38da7c19e43 some more cleanups and refactoring.
sheepluva
parents: 6015
diff changeset
   221
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   222
    // this QFile isn't needed any further
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   223
    delete file;
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   224
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   225
    return typesAvailable;
1907
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   226
}
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   227
a104432e8301 Random team generator rework by ttsmj
unc0rr
parents:
diff changeset
   228
5114
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   229
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
   230
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
   231
{
5115
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   232
    QStringList Graves;
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   233
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   234
    //list all available Graves
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6824
diff changeset
   235
    Graves.append(DataManager::instance().entryList(
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6212
diff changeset
   236
                      "Graphics/Graves",
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6212
diff changeset
   237
                      QDir::Files,
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6212
diff changeset
   238
                      QStringList("*.png")
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6212
diff changeset
   239
                  ).replaceInStrings(QRegExp("\\.png$"), "")
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   240
                 );
5114
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   241
5115
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   242
    if(Graves.size()==0)
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   243
    {
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   244
        // TODO do some serious error handling
5115
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   245
        return "Error";
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   246
    }
5114
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   247
5115
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   248
    //pick a random grave
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   249
    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
   250
}
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   251
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
   252
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
   253
{
5115
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   254
    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
   255
5115
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   256
    //list all available Forts
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6824
diff changeset
   257
    Forts.append(DataManager::instance().entryList(
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6212
diff changeset
   258
                     "Forts",
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6212
diff changeset
   259
                     QDir::Files,
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6212
diff changeset
   260
                     QStringList("*L.png")
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6212
diff changeset
   261
                 ).replaceInStrings(QRegExp("L\\.png$"), "")
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   262
                );
5114
a05081bc2769 Improved random name generation (now a random fort and random grave is choosen
Jonathan@Jonathan-PC.fritz.box
parents: 4976
diff changeset
   263
5115
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   264
    if(Forts.size()==0)
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   265
    {
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6060
diff changeset
   266
        // TODO do some serious error handling
5115
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   267
        return "Error";
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   268
    }
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   269
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   270
    //pick a random fort
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   271
    return Forts[rand()%(Forts.size())];
276410cc1178 fix indentation and missing newline at end of file
sheepluva
parents: 5114
diff changeset
   272
}