~ubuntu-branches/ubuntu/wily/hedgewars/wily

« back to all changes in this revision

Viewing changes to QTfrontend/namegen.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitry E. Oboukhov
  • Date: 2011-09-23 10:16:55 UTC
  • mfrom: (1.2.11 upstream)
  • Revision ID: package-import@ubuntu.com-20110923101655-3977th2gc5n0a3pv
Tags: 0.9.16-1
* New upstream version.
 + Downloadable content! Simply click to install any content.
   New voices, hats, maps, themes, translations, music, scripts...
   Hedgewars is now more customisable than ever before! As time goes
   by we will be soliciting community content to feature on this page,
   so remember to check it from time to time. If you decide you want
   to go back to standard Hedgewars, just remove the Data directory
   from your Hedgewars config directory.
 + 3-D rendering! Diorama-like rendering of the game in a variety
   of 3D modes. Let us know which ones work best for you, we didn't
   really have the equipment to test them all.
 + Resizable game window.
 + New utilities! The Time Box will remove one of your hedgehogs
   from the game for a while, protecting from attack until it returns,
   somewhere else on the map. Land spray will allow you to build bridges,
   seal up holes, or just make life unpleasant for your enemies.
 + New single player: Bamboo Thicket, That Sinking Feeling, Newton and
   the Tree and multi-player: The Specialists, Space Invaders,
   Racer - scripts! And a ton more script hooks for scripters
 + New twists on old weapons. Drill strike, seduction and fire have
   been adjusted. Defective mines have been added, rope can attach to
   hogs/crates/barrels again, grenades now have variable bounce (use
   precise key + 1-5). Portal gun is now more usable in flight and
   all game actions are a lot faster.
 + New theme - Golf, dozens of new community hats and a new
   localised Default voice, Ukranian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Hedgewars, a free turn based strategy game
3
3
 * Copyright (c) 2009 Martin Minarik <ttsmj@pokec.sk>
 
4
 * Copyright (c) 2009-2011 Andrey Korotaev <unC0Rr@gmail.com>
4
5
 *
5
6
 * This program is free software; you can redistribute it and/or modify
6
7
 * it under the terms of the GNU General Public License as published by
18
19
 
19
20
#include <QFile>
20
21
#include <QTextStream>
21
 
#include <QApplication>
22
22
#include <QStringList>
23
23
#include <QLineEdit>
24
24
#include "namegen.h"
39
39
 
40
40
 
41
41
 
42
 
void HWNamegen::TeamRandomName(HWTeam*& team, const int &i)
 
42
void HWNamegen::TeamRandomName(HWTeam*& team, const int HedgehogNumber)
43
43
{
44
 
    RandomNameByHat(team,i);
 
44
    RandomNameByHat(team, HedgehogNumber);
45
45
}
46
46
 
47
47
void HWNamegen::TeamRandomNames(HWTeam*& team, const bool changeteamname)
54
54
            if (TypesTeamnames[kind].size() > 0){
55
55
                team->TeamName = TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())];
56
56
            }
57
 
            team->Grave = "Simple"; // Todo: make it semi-random
58
 
            team->Fort = "Island"; // Todo: make it semi-random
 
57
            team->Grave = GetRandomGrave();
 
58
            team->Fort = GetRandomFort();
59
59
            team->Voicepack = "Default";
60
60
        }
61
61
 
 
62
        //give each hedgehog a random name:
 
63
        //TODO: load the dictionary only once! (right now it's loaded once for each hedgehog)
62
64
        for(int i = 0; i < 8; i++)
63
65
        {
64
66
            if ((TypesHatnames[kind].size()) > 0){
72
74
}
73
75
 
74
76
 
75
 
void HWNamegen::RandomNameByHat(HWTeam*& team, const int &i)
 
77
void HWNamegen::RandomNameByHat(HWTeam*& team, const int HedgehogNumber)
76
78
{
77
79
    QStringList Dictionaries;
78
 
    HatCfgLoad(team->Hedgehogs[i].Hat,Dictionaries);
 
80
    HatCfgLoad(team->Hedgehogs[HedgehogNumber].Hat,Dictionaries);
79
81
 
80
82
    QStringList Dictionary;
81
83
    DictLoad(Dictionaries[rand()%(Dictionaries.size())],Dictionary);
82
84
 
83
 
    team->Hedgehogs[i].Name = Dictionary[rand()%(Dictionary.size())];
 
85
    team->Hedgehogs[HedgehogNumber].Name = Dictionary[rand()%(Dictionary.size())];
84
86
}
85
87
 
86
88
void HWNamegen::DictLoad(const QString filename, QStringList &list)
87
89
{
88
 
     list.clear();
89
 
 
90
 
     QFile file(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename));
91
 
     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
92
 
     {
93
 
 
94
 
         QTextStream in(&file);
95
 
         while (!in.atEnd()) {
96
 
             QString line = in.readLine();
97
 
             if(line != QString(""))
98
 
                 {list.append(line);}
99
 
         }
100
 
     }
101
 
 
102
 
     if (list.size()==0)
 
90
    list.clear();
 
91
 
 
92
    QFile file;
 
93
    file.setFileName(QString("%1/Data/Names/%2.txt").arg(cfgdir->absolutePath()).arg(filename));
 
94
    if (!file.exists()) file.setFileName(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename));
 
95
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
 
96
    {
 
97
 
 
98
        QTextStream in(&file);
 
99
        while (!in.atEnd()) {
 
100
            QString line = in.readLine();
 
101
            if(line != QString(""))
 
102
                {list.append(line);}
 
103
        }
 
104
    }
 
105
 
 
106
    if (list.size()==0)
103
107
         list.append(filename);
104
108
 
105
109
}
107
111
 
108
112
void HWNamegen::HatCfgLoad(const QString hatname, QStringList &list)
109
113
{
110
 
     list.clear();
111
 
 
112
 
     QFile file(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname));
113
 
     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
114
 
     {
115
 
 
116
 
         QTextStream in(&file);
117
 
         while (!in.atEnd()) {
118
 
             QString line = in.readLine();
119
 
             if(line != QString(""))
120
 
                 {list.append(line);}
121
 
         }
122
 
     }
123
 
 
124
 
     if (list.size()==0)
 
114
    list.clear();
 
115
 
 
116
    QFile file;
 
117
    file.setFileName(QString("%1/Data/Names/%2.cfg").arg(cfgdir->absolutePath()).arg(hatname));
 
118
    if (!file.exists()) file.setFileName(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname));
 
119
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
 
120
    {
 
121
 
 
122
        QTextStream in(&file);
 
123
        while (!in.atEnd()) {
 
124
            QString line = in.readLine();
 
125
            if(line != QString(""))
 
126
                {list.append(line);}
 
127
        }
 
128
    }
 
129
 
 
130
    if (list.size()==0)
125
131
         list.append(QString("generic"));
126
132
 
127
133
}
129
135
 
130
136
void HWNamegen::TypesLoad()
131
137
{
132
 
 
133
 
     QFile file(QString("%1/Names/types.ini").arg(datadir->absolutePath()));
134
 
     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
135
 
         {TypesAvliable = FALSE; return;}
136
 
 
137
 
     int counter = 0; //counter starts with 0 (teamnames mode)
138
 
     TypesTeamnames.append(QStringList());
139
 
     TypesHatnames.append(QStringList());
140
 
 
141
 
     QTextStream in(&file);
142
 
     while (!in.atEnd()) {
143
 
         QString line = in.readLine();
144
 
         if (line == QString("#####")){
145
 
             counter++; //toggle mode (teamnames || hats)
146
 
             if ((counter%2) == 0){
147
 
                 TypesTeamnames.append(QStringList());
148
 
                 TypesHatnames.append(QStringList());
149
 
             }
150
 
         } else if ((line == QString("*****")) || (line == QString("*END*"))){
151
 
             TypesAvliable = TRUE; return; // bye bye
152
 
         } else {
153
 
             if ((counter%2) == 0){ // even => teamnames mode
154
 
                 TypesTeamnames[(counter/2)].append(line);
155
 
             } else { // odd => hats mode
156
 
                 TypesHatnames[((counter-1)/2)].append(line);
157
 
             }
158
 
         }
159
 
//         Types.append(line);
160
 
     }
161
 
         TypesAvliable = TRUE;
162
 
     return;
163
 
}
164
 
 
165
 
 
 
138
    QFile file;
 
139
    file.setFileName(QString("%1/Data/Names/types.ini").arg(cfgdir->absolutePath()));
 
140
    if (!file.exists()) file.setFileName(QString("%1/Names/types.ini").arg(datadir->absolutePath()));
 
141
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
 
142
        {TypesAvliable = FALSE; return;}
 
143
 
 
144
    int counter = 0; //counter starts with 0 (teamnames mode)
 
145
    TypesTeamnames.append(QStringList());
 
146
    TypesHatnames.append(QStringList());
 
147
 
 
148
    QTextStream in(&file);
 
149
    while (!in.atEnd()) {
 
150
        QString line = in.readLine();
 
151
        if (line == QString("#####")){
 
152
            counter++; //toggle mode (teamnames || hats)
 
153
            if ((counter%2) == 0){
 
154
                TypesTeamnames.append(QStringList());
 
155
                TypesHatnames.append(QStringList());
 
156
            }
 
157
        } else if ((line == QString("*****")) || (line == QString("*END*"))){
 
158
            TypesAvliable = TRUE; return; // bye bye
 
159
        } else {
 
160
            if ((counter%2) == 0){ // even => teamnames mode
 
161
                TypesTeamnames[(counter/2)].append(line);
 
162
            } else { // odd => hats mode
 
163
                TypesHatnames[((counter-1)/2)].append(line);
 
164
            }
 
165
        }
 
166
//        Types.append(line);
 
167
    }
 
168
        TypesAvliable = TRUE;
 
169
    return;
 
170
}
 
171
 
 
172
 
 
173
 
 
174
QString HWNamegen::GetRandomGrave()
 
175
{
 
176
    QStringList Graves;
 
177
 
 
178
    //list all available Graves
 
179
    QDir tmpdir;
 
180
    tmpdir.cd(cfgdir->absolutePath());
 
181
    tmpdir.cd("Data/Graphics/Graves");
 
182
    tmpdir.setFilter(QDir::Files);
 
183
    Graves.append(tmpdir.entryList(QStringList("*.png")).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1"));
 
184
 
 
185
    tmpdir.cd(datadir->absolutePath());
 
186
    tmpdir.cd("Graphics/Graves");
 
187
    tmpdir.setFilter(QDir::Files);
 
188
    QStringList tmpList = tmpdir.entryList(QStringList("*.png")).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1");
 
189
    for (QStringList::Iterator it = tmpList.begin(); it != tmpList.end(); ++it) 
 
190
        if (!Graves.contains(*it,Qt::CaseInsensitive)) Graves.append(*it);
 
191
 
 
192
    if(Graves.size()==0)
 
193
    {
 
194
        //do some serious error handling
 
195
        return "Error";
 
196
    }
 
197
 
 
198
    //pick a random grave
 
199
    return Graves[rand()%(Graves.size())];
 
200
}
 
201
 
 
202
QString HWNamegen::GetRandomFort()
 
203
{
 
204
    QStringList Forts;
 
205
 
 
206
    //list all available Forts
 
207
    QDir tmpdir;
 
208
    tmpdir.cd(datadir->absolutePath());
 
209
    tmpdir.cd("Forts");
 
210
    tmpdir.setFilter(QDir::Files);
 
211
    Forts.append(tmpdir.entryList(QStringList("*L.png")).replaceInStrings(QRegExp("^(.*)L\\.png"), "\\1"));
 
212
 
 
213
    if(Forts.size()==0)
 
214
    {
 
215
        //do some serious error handling
 
216
        return "Error";
 
217
    }
 
218
 
 
219
    //pick a random fort
 
220
    return Forts[rand()%(Forts.size())];
 
221
}