feat: Implement random name generation
This commit is contained in:
parent
f2d62e084c
commit
82716d8685
5 changed files with 247 additions and 4 deletions
|
@ -99,6 +99,7 @@ set(RESOURCE_HEADERS
|
|||
src/resource/folder.h
|
||||
src/resource/gfffile.h
|
||||
src/resource/keyfile.h
|
||||
src/resource/ltrfile.h
|
||||
src/resource/lytfile.h
|
||||
src/resource/pefile.h
|
||||
src/resource/resources.h
|
||||
|
@ -116,6 +117,7 @@ set(RESOURCE_SOURCES
|
|||
src/resource/folder.cpp
|
||||
src/resource/gfffile.cpp
|
||||
src/resource/keyfile.cpp
|
||||
src/resource/ltrfile.cpp
|
||||
src/resource/lytfile.cpp
|
||||
src/resource/pefile.cpp
|
||||
src/resource/rimfile.cpp
|
||||
|
|
|
@ -17,6 +17,9 @@
|
|||
|
||||
#include "nameentry.h"
|
||||
|
||||
#include "../../../common/streamutil.h"
|
||||
#include "../../../resource/resources.h"
|
||||
|
||||
#include "../colorutil.h"
|
||||
|
||||
#include "chargen.h"
|
||||
|
@ -50,21 +53,33 @@ NameEntry::NameEntry(CharacterGeneration *charGen, GameVersion version, const Gr
|
|||
void NameEntry::load() {
|
||||
GUI::load();
|
||||
|
||||
_nameBoxEdit = &getControl("NAME_BOX_EDIT");
|
||||
|
||||
setControlText("NAME_BOX_EDIT", "");
|
||||
disableControl("BTN_RANDOM");
|
||||
|
||||
loadLtrFile("humanm", _maleLtr);
|
||||
loadLtrFile("humanf", _femaleLtr);
|
||||
loadLtrFile("humanl", _lastNameLtr);
|
||||
}
|
||||
|
||||
void NameEntry::loadLtrFile(const string &resRef, LtrFile <r) {
|
||||
shared_ptr<ByteArray> data(Resources::instance().get(resRef, ResourceType::LetterComboProbability));
|
||||
ltr.load(wrap(data));
|
||||
}
|
||||
|
||||
bool NameEntry::handle(const SDL_Event &event) {
|
||||
if (event.type == SDL_KEYDOWN && _input.handle(event)) {
|
||||
setControlText("NAME_BOX_EDIT", _input.text());
|
||||
_nameBoxEdit->setTextMessage(_input.text());
|
||||
return true;
|
||||
}
|
||||
|
||||
return GUI::handle(event);
|
||||
}
|
||||
|
||||
void NameEntry::onClick(const string &control) {
|
||||
if (control == "END_BTN") {
|
||||
if (control == "BTN_RANDOM") {
|
||||
_nameBoxEdit->setTextMessage(getRandomName());
|
||||
|
||||
} else if (control == "END_BTN") {
|
||||
_charGen->setQuickStep(2);
|
||||
_charGen->openQuick();
|
||||
|
||||
|
@ -73,6 +88,15 @@ void NameEntry::onClick(const string &control) {
|
|||
}
|
||||
}
|
||||
|
||||
string NameEntry::getRandomName() const {
|
||||
Gender gender = _charGen->character().gender;
|
||||
const LtrFile <r = gender == Gender::Female ? _femaleLtr : _maleLtr;
|
||||
string firstName(ltr.getRandomName(8));
|
||||
string lastName(ltr.getRandomName(8));
|
||||
|
||||
return firstName + " " + lastName;
|
||||
}
|
||||
|
||||
} // namespace game
|
||||
|
||||
} // namespace reone
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
#include "../../../gui/gui.h"
|
||||
#include "../../../gui/textinput.h"
|
||||
#include "../../../resource/ltrfile.h"
|
||||
|
||||
namespace reone {
|
||||
|
||||
|
@ -36,8 +37,16 @@ public:
|
|||
private:
|
||||
CharacterGeneration *_charGen { nullptr };
|
||||
gui::TextInput _input;
|
||||
gui::Control *_nameBoxEdit { nullptr };
|
||||
resource::LtrFile _maleLtr;
|
||||
resource::LtrFile _femaleLtr;
|
||||
resource::LtrFile _lastNameLtr;
|
||||
|
||||
void onClick(const std::string &control) override;
|
||||
|
||||
void loadLtrFile(const std::string &resRef, resource::LtrFile <r);
|
||||
|
||||
std::string getRandomName() const;
|
||||
};
|
||||
|
||||
} // namespace game
|
||||
|
|
154
src/resource/ltrfile.cpp
Normal file
154
src/resource/ltrfile.cpp
Normal file
|
@ -0,0 +1,154 @@
|
|||
/*
|
||||
* Copyright (c) 2020 The reone project contributors
|
||||
*
|
||||
* 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, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ltrfile.h"
|
||||
|
||||
#include <cctype>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
#include "../common/random.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace reone {
|
||||
|
||||
namespace resource {
|
||||
|
||||
static const vector<char> g_letters {
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\'', '-'
|
||||
};
|
||||
|
||||
LtrFile::LtrFile() : BinaryFile(8, "LTR V1.0") {
|
||||
}
|
||||
|
||||
void LtrFile::doLoad() {
|
||||
_letterCount = readByte();
|
||||
if (_letterCount != 28) {
|
||||
throw runtime_error("Unsupported letter count");
|
||||
}
|
||||
readLetterSet(_singleLetters);
|
||||
|
||||
_doubleLetters.resize(_letterCount);
|
||||
for (auto &set : _doubleLetters) {
|
||||
readLetterSet(set);
|
||||
}
|
||||
|
||||
_trippleLetters.resize(_letterCount);
|
||||
for (auto &sets : _trippleLetters) {
|
||||
sets.resize(_letterCount);
|
||||
for (auto &set : sets) {
|
||||
readLetterSet(set);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LtrFile::readLetterSet(LetterSet &set) {
|
||||
set.start.resize(_letterCount);
|
||||
for (int i = 0; i < _letterCount; ++i) {
|
||||
set.start[i] = readFloat();
|
||||
}
|
||||
|
||||
set.mid.resize(_letterCount);
|
||||
for (int i = 0; i < _letterCount; ++i) {
|
||||
set.mid[i] = readFloat();
|
||||
}
|
||||
|
||||
set.end.resize(_letterCount);
|
||||
for (int i = 0; i < _letterCount; ++i) {
|
||||
set.end[i] = readFloat();
|
||||
}
|
||||
}
|
||||
|
||||
string LtrFile::getRandomName(int maxLength) const {
|
||||
string name;
|
||||
|
||||
float probability;
|
||||
int firstLetterIdx = 0;
|
||||
int secondLetterIdx = 0;
|
||||
|
||||
// First three letters
|
||||
|
||||
probability = random(0.0f, 1.0f);
|
||||
for (int i = 0; i < _letterCount; ++i) {
|
||||
if (_singleLetters.start[i] > probability) {
|
||||
name += toupper(g_letters[i]);
|
||||
firstLetterIdx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
probability = random(0.0f, 1.0f);
|
||||
for (int i = 0; i < _letterCount; ++i) {
|
||||
if (_doubleLetters[firstLetterIdx].start[i] > probability) {
|
||||
name += g_letters[i];
|
||||
secondLetterIdx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
probability = random(0.0f, 1.0f);
|
||||
for (int i = 0; i < _letterCount; ++i) {
|
||||
if (_trippleLetters[firstLetterIdx][secondLetterIdx].start[i] > probability) {
|
||||
name += g_letters[i];
|
||||
firstLetterIdx = secondLetterIdx;
|
||||
secondLetterIdx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// END First three letters
|
||||
|
||||
if (maxLength < 4) return move(name);
|
||||
|
||||
// Middle letters
|
||||
|
||||
int length = random(0, maxLength - 3);
|
||||
for (int i = 0; i < length; ++i) {
|
||||
probability = random(0.0f, 1.0f);
|
||||
for (int j = 0; j < _letterCount; ++j) {
|
||||
if (_trippleLetters[firstLetterIdx][secondLetterIdx].mid[j] > probability) {
|
||||
name += g_letters[j];
|
||||
firstLetterIdx = secondLetterIdx;
|
||||
secondLetterIdx = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// END Middle letters
|
||||
|
||||
// Final letter
|
||||
|
||||
probability = random(0.0f, 1.0f);
|
||||
for (int i = 0; i < _letterCount; ++i) {
|
||||
if (_trippleLetters[firstLetterIdx][secondLetterIdx].end[i] > probability) {
|
||||
name += g_letters[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// END Final letter
|
||||
|
||||
return move(name);
|
||||
}
|
||||
|
||||
} // namespace resource
|
||||
|
||||
} // namespace reone
|
54
src/resource/ltrfile.h
Normal file
54
src/resource/ltrfile.h
Normal file
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Copyright (c) 2020 The reone project contributors
|
||||
*
|
||||
* 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, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "binfile.h"
|
||||
|
||||
namespace reone {
|
||||
|
||||
namespace resource {
|
||||
|
||||
/**
|
||||
* Encapsulates the LTR file format, used to generate random names.
|
||||
*/
|
||||
class LtrFile : public BinaryFile {
|
||||
public:
|
||||
LtrFile();
|
||||
|
||||
std::string getRandomName(int maxLength) const;
|
||||
|
||||
private:
|
||||
struct LetterSet {
|
||||
std::vector<float> start;
|
||||
std::vector<float> mid;
|
||||
std::vector<float> end;
|
||||
};
|
||||
|
||||
int _letterCount { 0 };
|
||||
LetterSet _singleLetters;
|
||||
std::vector<LetterSet> _doubleLetters;
|
||||
std::vector<std::vector<LetterSet>> _trippleLetters;
|
||||
|
||||
void doLoad() override;
|
||||
|
||||
void readLetterSet(LetterSet &set);
|
||||
};
|
||||
|
||||
} // namespace resource
|
||||
|
||||
} // namespace reone
|
Loading…
Reference in a new issue