Files
@ r27569:6a9aa0750697
Branch filter:
Location: cpp/openttd-patchpack/source/src/newgrf_townname.cpp - annotation
r27569:6a9aa0750697
3.5 KiB
text/x-c
Codechange: remove need for GetDParamX
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 | r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r15610:623a23fb6560 r15610:623a23fb6560 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r8130:bece2e5c7460 r8214:9a3935f9ef4e r27506:5d6fb3346eaa r6956:6398843f1341 r24804:1320798728bb r24804:1320798728bb r21383:942c32fb8b0e r21383:942c32fb8b0e r27109:b9db4345aeec r24804:1320798728bb r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r27109:b9db4345aeec r27109:b9db4345aeec r23607:36c15679007d r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r23607:36c15679007d r27109:b9db4345aeec r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r27109:b9db4345aeec r6956:6398843f1341 r6956:6398843f1341 r27506:5d6fb3346eaa r6956:6398843f1341 r23607:36c15679007d r27109:b9db4345aeec r27109:b9db4345aeec r27109:b9db4345aeec r27109:b9db4345aeec r27109:b9db4345aeec r27109:b9db4345aeec r6956:6398843f1341 r27109:b9db4345aeec r27506:5d6fb3346eaa r6956:6398843f1341 r27506:5d6fb3346eaa r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r27506:5d6fb3346eaa r6956:6398843f1341 r27109:b9db4345aeec r27109:b9db4345aeec r27109:b9db4345aeec r27506:5d6fb3346eaa r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r24804:1320798728bb r24804:1320798728bb r24804:1320798728bb r6956:6398843f1341 r24804:1320798728bb r27109:b9db4345aeec r27109:b9db4345aeec r27109:b9db4345aeec r27109:b9db4345aeec r6956:6398843f1341 r24804:1320798728bb r24804:1320798728bb r24804:1320798728bb r24804:1320798728bb r24804:1320798728bb r24804:1320798728bb r24804:1320798728bb r27109:b9db4345aeec r24804:1320798728bb r24804:1320798728bb r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r27109:b9db4345aeec r6956:6398843f1341 r6956:6398843f1341 r27109:b9db4345aeec r6956:6398843f1341 r27109:b9db4345aeec r27109:b9db4345aeec r27109:b9db4345aeec r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r27109:b9db4345aeec r6956:6398843f1341 r27109:b9db4345aeec r27109:b9db4345aeec r27109:b9db4345aeec r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 r6956:6398843f1341 | /*
* This file is part of OpenTTD.
* OpenTTD 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.
* OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file newgrf_townname.cpp
* Implementation of Action 0F "universal holder" structure and functions.
* This file implements a linked-lists of townname generators,
* holding everything that the newgrf action 0F will send over to OpenTTD.
*/
#include "stdafx.h"
#include "newgrf_townname.h"
#include "core/alloc_func.hpp"
#include "string_func.h"
#include "strings_internal.h"
#include "table/strings.h"
#include "safeguards.h"
static std::vector<GRFTownName> _grf_townnames;
static std::vector<StringID> _grf_townname_names;
GRFTownName *GetGRFTownName(uint32 grfid)
{
auto found = std::find_if(std::begin(_grf_townnames), std::end(_grf_townnames), [&grfid](const GRFTownName &t){ return t.grfid == grfid; });
if (found != std::end(_grf_townnames)) return &*found;
return nullptr;
}
GRFTownName *AddGRFTownName(uint32 grfid)
{
GRFTownName *t = GetGRFTownName(grfid);
if (t == nullptr) {
t = &_grf_townnames.emplace_back();
t->grfid = grfid;
}
return t;
}
void DelGRFTownName(uint32 grfid)
{
_grf_townnames.erase(std::find_if(std::begin(_grf_townnames), std::end(_grf_townnames), [&grfid](const GRFTownName &t){ return t.grfid == grfid; }));
}
static void RandomPart(StringBuilder &builder, const GRFTownName *t, uint32 seed, byte id)
{
assert(t != nullptr);
for (const auto &partlist : t->partlists[id]) {
byte count = partlist.bitcount;
uint16 maxprob = partlist.maxprob;
uint32 r = (GB(seed, partlist.bitstart, count) * maxprob) >> count;
for (const auto &part : partlist.parts) {
maxprob -= GB(part.prob, 0, 7);
if (maxprob > r) continue;
if (HasBit(part.prob, 7)) {
RandomPart(builder, t, seed, part.id);
} else {
builder += part.text;
}
break;
}
}
}
void GRFTownNameGenerate(StringBuilder &builder, uint32 grfid, uint16 gen, uint32 seed)
{
const GRFTownName *t = GetGRFTownName(grfid);
if (t != nullptr) {
assert(gen < t->styles.size());
RandomPart(builder, t, seed, t->styles[gen].id);
}
}
/** Allocate memory for the NewGRF town names. */
void InitGRFTownGeneratorNames()
{
_grf_townname_names.clear();
for (const auto &t : _grf_townnames) {
for (const auto &style : t.styles) {
_grf_townname_names.push_back(style.name);
}
}
}
const std::vector<StringID>& GetGRFTownNameList()
{
return _grf_townname_names;
}
StringID GetGRFTownNameName(uint16 gen)
{
return gen < _grf_townname_names.size() ? _grf_townname_names[gen] : STR_UNDEFINED;
}
void CleanUpGRFTownNames()
{
_grf_townnames.clear();
}
uint32 GetGRFTownNameId(uint16 gen)
{
for (const auto &t : _grf_townnames) {
if (gen < t.styles.size()) return t.grfid;
gen -= static_cast<uint16>(t.styles.size());
}
/* Fallback to no NewGRF */
return 0;
}
uint16 GetGRFTownNameType(uint16 gen)
{
for (const auto &t : _grf_townnames) {
if (gen < t.styles.size()) return gen;
gen -= static_cast<uint16>(t.styles.size());
}
/* Fallback to english original */
return SPECSTR_TOWNNAME_ENGLISH;
}
|