1
0
Fork 0

Codechange: pass the randomizer directly to the town name generation

pull/10352/head
Rubidium 2023-01-13 22:31:09 +01:00 committed by rubidium42
parent b3b8c3fd2d
commit 3373128233
6 changed files with 10 additions and 7 deletions

View File

@ -301,7 +301,7 @@
EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_TOWN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG); EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_TOWN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
} }
uint32 townnameparts; uint32 townnameparts;
if (!GenerateTownName(&townnameparts)) { if (!GenerateTownName(_interactive_random, &townnameparts)) {
ScriptObject::SetLastError(ScriptError::ERR_NAME_IS_NOT_UNIQUE); ScriptObject::SetLastError(ScriptError::ERR_NAME_IS_NOT_UNIQUE);
return false; return false;
} }

View File

@ -2245,7 +2245,7 @@ bool GenerateTowns(TownLayout layout)
bool city = (_settings_game.economy.larger_towns != 0 && Chance16(1, _settings_game.economy.larger_towns)); bool city = (_settings_game.economy.larger_towns != 0 && Chance16(1, _settings_game.economy.larger_towns));
IncreaseGeneratingWorldProgress(GWP_TOWN); IncreaseGeneratingWorldProgress(GWP_TOWN);
/* Get a unique name for the town. */ /* Get a unique name for the town. */
if (!GenerateTownName(&townnameparts, &town_names)) continue; if (!GenerateTownName(_random, &townnameparts, &town_names)) continue;
/* try 20 times to create a random-sized town for the first loop. */ /* try 20 times to create a random-sized town for the first loop. */
if (CreateRandomTown(20, townnameparts, TSZ_RANDOM, city, layout) != nullptr) current_number++; // If creation was successful, raise a flag. if (CreateRandomTown(20, townnameparts, TSZ_RANDOM, city, layout) != nullptr) current_number++; // If creation was successful, raise a flag.
} while (--total); } while (--total);
@ -2259,7 +2259,7 @@ bool GenerateTowns(TownLayout layout)
/* If current_number is still zero at this point, it means that not a single town has been created. /* If current_number is still zero at this point, it means that not a single town has been created.
* So give it a last try, but now more aggressive */ * So give it a last try, but now more aggressive */
if (GenerateTownName(&townnameparts) && if (GenerateTownName(_random, &townnameparts) &&
CreateRandomTown(10000, townnameparts, TSZ_RANDOM, _settings_game.economy.larger_towns != 0, layout) != nullptr) { CreateRandomTown(10000, townnameparts, TSZ_RANDOM, _settings_game.economy.larger_towns != 0, layout) != nullptr) {
return true; return true;
} }

View File

@ -1146,7 +1146,7 @@ public:
void RandomTownName() void RandomTownName()
{ {
this->townnamevalid = GenerateTownName(&this->townnameparts); this->townnamevalid = GenerateTownName(_interactive_random, &this->townnameparts);
if (!this->townnamevalid) { if (!this->townnamevalid) {
this->townname_editbox.text.DeleteAll(); this->townname_editbox.text.DeleteAll();

View File

@ -112,11 +112,12 @@ bool VerifyTownName(uint32 r, const TownNameParams *par, TownNames *town_names)
/** /**
* Generates valid town name. * Generates valid town name.
* @param randomizer the source of random data for generating the name
* @param townnameparts if a name is generated, it's stored there * @param townnameparts if a name is generated, it's stored there
* @param town_names if a name is generated, check its uniqueness with the set * @param town_names if a name is generated, check its uniqueness with the set
* @return true iff a name was generated * @return true iff a name was generated
*/ */
bool GenerateTownName(uint32 *townnameparts, TownNames *town_names) bool GenerateTownName(Randomizer &randomizer, uint32 *townnameparts, TownNames *town_names)
{ {
TownNameParams par(_settings_game.game_creation.town_name); TownNameParams par(_settings_game.game_creation.town_name);
@ -130,7 +131,7 @@ bool GenerateTownName(uint32 *townnameparts, TownNames *town_names)
* the other towns may take considerable amount of time (10000 is * the other towns may take considerable amount of time (10000 is
* too much). */ * too much). */
for (int i = 1000; i != 0; i--) { for (int i = 1000; i != 0; i--) {
uint32 r = _generating_world ? Random() : InteractiveRandom(); uint32 r = randomizer.Next();
if (!VerifyTownName(r, &par, town_names)) continue; if (!VerifyTownName(r, &par, town_names)) continue;
*townnameparts = r; *townnameparts = r;

View File

@ -10,12 +10,13 @@
#ifndef TOWNNAME_FUNC_H #ifndef TOWNNAME_FUNC_H
#define TOWNNAME_FUNC_H #define TOWNNAME_FUNC_H
#include "core/random_func.hpp"
#include "townname_type.h" #include "townname_type.h"
char *GenerateTownNameString(char *buf, const char *last, size_t lang, uint32 seed); char *GenerateTownNameString(char *buf, const char *last, size_t lang, uint32 seed);
char *GetTownName(char *buff, const TownNameParams *par, uint32 townnameparts, const char *last); char *GetTownName(char *buff, const TownNameParams *par, uint32 townnameparts, const char *last);
char *GetTownName(char *buff, const Town *t, const char *last); char *GetTownName(char *buff, const Town *t, const char *last);
bool VerifyTownName(uint32 r, const TownNameParams *par, TownNames *town_names = nullptr); bool VerifyTownName(uint32 r, const TownNameParams *par, TownNames *town_names = nullptr);
bool GenerateTownName(uint32 *townnameparts, TownNames *town_names = nullptr); bool GenerateTownName(Randomizer &randomizer, uint32 *townnameparts, TownNames *town_names = nullptr);
#endif /* TOWNNAME_FUNC_H */ #endif /* TOWNNAME_FUNC_H */

View File

@ -308,6 +308,7 @@ void PlaceTreesRandomly()
*/ */
uint PlaceTreeGroupAroundTile(TileIndex tile, TreeType treetype, uint radius, uint count, bool set_zone) uint PlaceTreeGroupAroundTile(TileIndex tile, TreeType treetype, uint radius, uint count, bool set_zone)
{ {
assert(_game_mode == GM_EDITOR); // Due to InteractiveRandom being used in this function
assert(treetype < TREE_TOYLAND + TREE_COUNT_TOYLAND); assert(treetype < TREE_TOYLAND + TREE_COUNT_TOYLAND);
const bool allow_desert = treetype == TREE_CACTUS; const bool allow_desert = treetype == TREE_CACTUS;
uint planted = 0; uint planted = 0;