File: namegen.cpp

package info (click to toggle)
hedgewars 1.0.2-13
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 219,164 kB
  • sloc: pascal: 54,829; cpp: 27,221; ansic: 22,809; java: 8,210; haskell: 6,797; xml: 3,076; sh: 580; objc: 113; python: 105; makefile: 32
file content (518 lines) | stat: -rw-r--r-- 13,748 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
 * Hedgewars, a free turn based strategy game
 * Copyright (c) 2009 Martin Minarik <ttsmj@pokec.sk>
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QStringList>
#include <QLineEdit>

#include "hwform.h"
#include "DataManager.h"

#include "namegen.h"

HWNamegen::HWNamegen() {}

QList<QStringList> HWNamegen::TypesTeamnames;
QList<QStringList> HWNamegen::TypesHatnames;
bool HWNamegen::typesAvailable = false;

void HWNamegen::teamRandomTeamName(HWTeam & team)
{
    QString newName = getRandomTeamName(-1);
    if(!newName.isNull())
        team.setName(newName);
}

void HWNamegen::teamRandomFlag(HWTeam & team, bool withDLC)
{
    team.setFlag(getRandomFlag(withDLC));
}

void HWNamegen::teamRandomVoice(HWTeam & team, bool withDLC)
{
    team.setVoicepack(getRandomVoice(withDLC));
}

void HWNamegen::teamRandomGrave(HWTeam & team, bool withDLC)
{
    team.setGrave(getRandomGrave(withDLC));
}

void HWNamegen::teamRandomFort(HWTeam & team, bool withDLC)
{
    team.setFort(getRandomFort(withDLC));
}

void HWNamegen::teamRandomEverything(HWTeam & team)
{
    // load types if not already loaded
    if (!typesAvailable)
        if (!loadTypes())
            return; // abort if loading failed

    // abort if there are no hat types
    if (TypesHatnames.size() <= 0)
        return;

    // the hat will influence which names the hogs get
    int kind = (rand()%(TypesHatnames.size()));

    team.setGrave(getRandomGrave());
    team.setFort(getRandomFort());
    team.setFlag(getRandomFlag());
    team.setVoicepack(getRandomVoice());

    QStringList dicts;
    QStringList dict;

    // Randomness mode:
    // 0: Themed hats (from types.ini)
    // 1: Equal hats for all
    // 2: Random hat for each hedgehog
    int r = rand() % 10;
    int randomMode;
    if (r <= 4)		// 0-4 (50%)
       randomMode = 0;
    else if (r <= 8)	// 5-8 (40%)
       randomMode = 1;
    else		// 9   (10%)
       randomMode = 2;

    // Generate random hats
    for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
    {
        HWHog hh = team.hedgehog(i);

        if (randomMode == 0)
        {
            hh.Hat = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())];
        }
        else if (randomMode == 1)
        {
            if (i == 0)
            {
                hh.Hat = getRandomHat();
            }
            else
            {
                hh.Hat = team.hedgehog(i-1).Hat;
            }
        }
        else if (randomMode == 2)
        {
            hh.Hat = getRandomHat();
        }

        team.setHedgehog(i,hh);

        // there is a chance that this hog has the same hat as the previous one
        // let's reuse the hat-specific dict in this case
        if ((i == 0) || (team.hedgehog(i).Hat != team.hedgehog(i-1).Hat))
        {
            dicts = dictsForHat(team.hedgehog(i).Hat);
            dict  = dictContents(dicts[rand()%(dicts.size())]);
        }

        // give each hedgehog a random name
        HWNamegen::teamRandomHogName(team,i,dict);
    }

    // If using themed hats, use themed team name.
    // Otherwise, only use “generic” team names from the first team
    // in types.txt.
    if (randomMode == 0)
        team.setName(getRandomTeamName(kind));
    else
        team.setName(getRandomTeamName(0));

}

// Set random hats for entire team
void HWNamegen::teamRandomHats(HWTeam & team, bool withDLC)
{
    // 50% chance that all hogs are set to the same hat.
    // 50% chance that each hog gets a random head individually.

    bool sameHogs = (rand()%2) == 0;
    for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
    {
        HWHog hh = team.hedgehog(i);
        if (sameHogs && i > 0)
            hh.Hat = team.hedgehog(i-1).Hat;
        else
            hh.Hat = getRandomHat(withDLC);
        team.setHedgehog(i, hh);
    }
}

void HWNamegen::teamRandomHat(HWTeam & team, const int HedgehogNumber, bool withDLC)
{
    HWHog hh = team.hedgehog(HedgehogNumber);

    hh.Hat = getRandomHat(withDLC);

    team.setHedgehog(HedgehogNumber, hh);
}

void HWNamegen::teamRandomHat(HWTeam & team, const int HedgehogNumber, const QStringList & dict)
{
    HWHog hh = team.hedgehog(HedgehogNumber);

    hh.Name = dict[rand()%(dict.size())];

    team.setHedgehog(HedgehogNumber, hh);
}

void HWNamegen::teamRandomHogNames(HWTeam & team)
{
    QStringList dicts, dict;
    for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
    {
        // there is a chance that this hog has the same hat as the previous one
        // let's reuse the hat-specific dict in this case
        if ((i == 0) || (team.hedgehog(i).Hat != team.hedgehog(i-1).Hat))
        {
            dicts = dictsForHat(team.hedgehog(i).Hat);
            dict  = dictContents(dicts[rand()%(dicts.size())]);
        }

        // give each hedgehog a random name
        HWNamegen::teamRandomHogName(team,i,dict);
    }
}

void HWNamegen::teamRandomHogName(HWTeam & team, const int HedgehogNumber)
{
    QStringList dicts = dictsForHat(team.hedgehog(HedgehogNumber).Hat);

    QStringList dict = dictContents(dicts[rand()%(dicts.size())]);

    teamRandomHogName(team, HedgehogNumber, dict);
}

void HWNamegen::teamRandomHogName(HWTeam & team, const int HedgehogNumber, const QStringList & dict)
{
    QStringList namesDict = dict;

    for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
    {
        namesDict.removeOne(team.hedgehog(i).Name);
    }

    // if our dict doesn't have any new names we'll have to use duplicates
    if (namesDict.size() < 1)
        namesDict = dict;

    HWHog hh = team.hedgehog(HedgehogNumber);

    hh.Name = namesDict[rand()%(namesDict.size())];

    team.setHedgehog(HedgehogNumber, hh);
}

void HWNamegen::teamLocalizedDefaultVoice(HWTeam & team, bool withDLC)
{
    team.setVoicepack(getLocalizedDefaultVoice(withDLC));
}

QStringList HWNamegen::dictContents(const QString filename)
{
    QStringList list;

    // find .txt to load the names from
    QFile file(QString("physfs://Names/%1.txt").arg(filename));

    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&file);
        QString line;
        do
        {
            line = in.readLine();

            if(!line.isEmpty())
                list.append(line);
        } while (!line.isNull());
    }

    if (list.size() == 0)
        list.append(filename);

    return list;
}


QStringList HWNamegen::dictsForHat(const QString hatname)
{
    QStringList list;

    // Find and check .cfg to load the dicts from
    QString path = QString("physfs://Names/%1.cfg").arg(hatname);
    QFileInfo check_file(path);

    // Note: The .cfg file is optional; a fallback mechanism is in place (see below)

    // Check if file exists to prevent PhysFS from complaining in console so much
    if (check_file.exists() && check_file.isFile())
    {
        QFile file(path);

        if (file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QTextStream in(&file);
            QString line;
            do
            {
                line = in.readLine();

                if(!line.isEmpty())
                    list.append(line);
            } while (!line.isNull());
        }
    }

    // Use Data/Names/generic.cfg by default
    if (list.size() == 0)
        list.append(QString("generic"));

    return list;
}

// loades types from ini files. returns true on success.
bool HWNamegen::loadTypes()
{
    typesAvailable = false;

    // find .ini to load the names from
    QFile * file = new QFile(QString("physfs://Names/types.ini"));


    if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
    {

        int counter = 0; //counter starts with 0 (teamnames mode)
        TypesTeamnames.append(QStringList());
        TypesHatnames.append(QStringList());

        QTextStream in(file);
        while (!in.atEnd())
        {
            QString line = in.readLine();
            if (line == QString("#####"))
            {
                counter++; //toggle mode (teamnames || hats)
                if ((counter%2) == 0)
                {
                    TypesTeamnames.append(QStringList());
                    TypesHatnames.append(QStringList());
                }
            }
            else if ((line == QString("*****")) || (line == QString("*END*")))
            {
                typesAvailable = true;
                return true; // bye bye
            }
            else
            {
                if ((counter%2) == 0)
                {
                    // even => teamnames mode
                    TypesTeamnames[(counter/2)].append(line);
                }
                else
                {
                    // odd => hats mode
                    TypesHatnames[((counter-1)/2)].append(line);
                }
            }
        }

        typesAvailable = true;
    }

    // this QFile isn't needed any further
    delete file;

    return typesAvailable;
}

/* Generates a random team name.
kind: Use to select a team name out of a group (types.ini).
Use a negative value if you don't care.
This function may return a null QString on error(this should never happen). */
QString HWNamegen::getRandomTeamName(int kind)
{
    // load types if not already loaded
    if (!typesAvailable)
        if (!loadTypes())
            return QString(); // abort if loading failed

    // abort if there are no hat types
    if (TypesHatnames.size() <= 0)
        return QString();

    if(kind < 0)
        kind = (rand()%(TypesHatnames.size()));

    if (TypesTeamnames[kind].size() > 0)
        return TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())];
    else
        return QString();
}

QString HWNamegen::getRandomHat(bool withDLC)
{
    QStringList Hats;

    // list all available hats
    Hats.append(DataManager::instance().entryList(
                      "Graphics/Hats",
                      QDir::Files,
                      QStringList("*.png"),
                      withDLC
                  ).replaceInStrings(QRegExp("\\.png$"), "")
                 );

    if(Hats.size()==0)
    {
        // TODO do some serious error handling
        return "Error";
    }

    // pick a random hat
    return Hats[rand()%(Hats.size())];
}

QString HWNamegen::getRandomGrave(bool withDLC)
{
    QStringList Graves;

    //list all available Graves
    Graves.append(DataManager::instance().entryList(
                      "Graphics/Graves",
                      QDir::Files,
                      QStringList("*.png"),
                      withDLC
                  ).replaceInStrings(QRegExp("\\.png$"), "")
                 );

    if(Graves.size()==0)
    {
        // TODO do some serious error handling
        return "Error";
    }

    //pick a random grave
    return Graves[rand()%(Graves.size())];
}

QString HWNamegen::getRandomFlag(bool withDLC)
{
    QStringList Flags;

    //list all available flags
    Flags.append(DataManager::instance().entryList(
                      "Graphics/Flags",
                      QDir::Files,
                      QStringList("*.png"),
                      withDLC
                  ).replaceInStrings(QRegExp("\\.png$"), "")
                 );
    //remove internal flags
    Flags.removeAll("cpu");
    Flags.removeAll("cpu_plain");

    if(Flags.size()==0)
    {
        // TODO do some serious error handling
        return "Error";
    }

    //pick a random flag
    return Flags[rand()%(Flags.size())];
}

QString HWNamegen::getRandomFort(bool withDLC)
{
    QStringList Forts;

    //list all available Forts
    Forts.append(DataManager::instance().entryList(
                     "Forts",
                     QDir::Files,
                     QStringList("*L.png"),
                     withDLC
                 ).replaceInStrings(QRegExp("L\\.png$"), "")
                );

    if(Forts.size()==0)
    {
        // TODO do some serious error handling
        return "Error";
    }

    //pick a random fort
    return Forts[rand()%(Forts.size())];
}

QString HWNamegen::getRandomVoice(bool withDLC)
{
    QStringList Voices;

    //list all available voices 
    Voices.append(DataManager::instance().entryList(
                     "Sounds/voices",
                     QDir::Dirs | QDir::NoDotAndDotDot,
                     QStringList("*"),
                     withDLC));

    if(Voices.size()==0)
    {
        // TODO do some serious error handling
        return "Error";
    }

    //pick a random voice
    return Voices[rand()%(Voices.size())];
}

QString HWNamegen::getLocalizedDefaultVoice(bool withDLC)
{
    QStringList entries = DataManager::instance().entryList(
        "Sounds/voices",
        QDir::Dirs | QDir::NoDotAndDotDot,
        QStringList("*"),
        withDLC);

    QString loc = QLocale().name();
    if(entries.contains("Default_" + loc))
    {
        return QString("Default_" + loc);
    }
    else if(entries.contains("Default_" + loc.left(2)))
    {
        return QString("Default_" + loc.left(2));
    }
    else
    {
        return QString("Default");
    }
}