如何在 Screeps 中生成一个带有前面名称标签的随机名称?
How to generate a random name with a preceding nametag in Screeps?
在编程游戏 Screeps 中,我使用以下方法生成 creep:
if(transporters.length < 0 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([WORK,CARRY,MOVE], undefined, {role: 'transporter'});
console.log('Spawning new Transporter: ' + newName);
}
'undefined' 标签用于生成具有随机名称的 creep。现在我想知道,因为我有不同类型的小兵,是否可以在它前面添加角色标签?例如,[Transporter] RandomName.
这可能吗?
您可以实现一个函数,以您想要的格式生成名称,并将 undefined
替换为生成的名称。来自 Screeps documentation:
createCreep(body, [name], [memory])
Start the creep spawning process. The required energy amount can be
withdrawn from all spawns and extensions in the room.
[...]
name (optional) string
The name of a new creep. It should be unique
creep name, i.e. the Game.creeps object should not contain another
creep with the same name (hash key). If not defined, a random name
will be generated.
Screeps 论坛实际上已经有一个 solution 可以满足您的需求。抄录如下:
var names1 = ["Jackson", "Aiden", "Liam", "Lucas", "Noah", "Mason", "Jayden", "Ethan", "Jacob", "Jack", "Caden", "Logan", "Benjamin", "Michael", "Caleb", "Ryan", "Alexander", "Elijah", "James", "William", "Oliver", "Connor", "Matthew", "Daniel", "Luke", "Brayden", "Jayce", "Henry", "Carter", "Dylan", "Gabriel", "Joshua", "Nicholas", "Isaac", "Owen", "Nathan", "Grayson", "Eli", "Landon", "Andrew", "Max", "Samuel", "Gavin", "Wyatt", "Christian", "Hunter", "Cameron", "Evan", "Charlie", "David", "Sebastian", "Joseph", "Dominic", "Anthony", "Colton", "John", "Tyler", "Zachary", "Thomas", "Julian", "Levi", "Adam", "Isaiah", "Alex", "Aaron", "Parker", "Cooper", "Miles", "Chase", "Muhammad", "Christopher", "Blake", "Austin", "Jordan", "Leo", "Jonathan", "Adrian", "Colin", "Hudson", "Ian", "Xavier", "Camden", "Tristan", "Carson", "Jason", "Nolan", "Riley", "Lincoln", "Brody", "Bentley", "Nathaniel", "Josiah", "Declan", "Jake", "Asher", "Jeremiah", "Cole", "Mateo", "Micah", "Elliot"]
var names2 = ["Sophia", "Emma", "Olivia", "Isabella", "Mia", "Ava", "Lily", "Zoe", "Emily", "Chloe", "Layla", "Madison", "Madelyn", "Abigail", "Aubrey", "Charlotte", "Amelia", "Ella", "Kaylee", "Avery", "Aaliyah", "Hailey", "Hannah", "Addison", "Riley", "Harper", "Aria", "Arianna", "Mackenzie", "Lila", "Evelyn", "Adalyn", "Grace", "Brooklyn", "Ellie", "Anna", "Kaitlyn", "Isabelle", "Sophie", "Scarlett", "Natalie", "Leah", "Sarah", "Nora", "Mila", "Elizabeth", "Lillian", "Kylie", "Audrey", "Lucy", "Maya", "Annabelle", "Makayla", "Gabriella", "Elena", "Victoria", "Claire", "Savannah", "Peyton", "Maria", "Alaina", "Kennedy", "Stella", "Liliana", "Allison", "Samantha", "Keira", "Alyssa", "Reagan", "Molly", "Alexandra", "Violet", "Charlie", "Julia", "Sadie", "Ruby", "Eva", "Alice", "Eliana", "Taylor", "Callie", "Penelope", "Camilla", "Bailey", "Kaelyn", "Alexis", "Kayla", "Katherine", "Sydney", "Lauren", "Jasmine", "London", "Bella", "Adeline", "Caroline", "Vivian", "Juliana", "Gianna", "Skyler", "Jordyn"]
Creep.getRandomName = function(prefix){
var name, isNameTaken, tries = 0;
do {
var nameArray = Math.random() > .5 ? names1 : names2;
name = nameArray[Math.floor(Math.random() * nameArray.length)];
if (tries > 3){
name += nameArray[Math.floor(Math.random() * nameArray.length)];
}
tries++;
isNameTaken = Game.creeps[name] !== undefined;
} while (isNameTaken);
return prefix+" "+name;
}
我发现 screeps 中缺少默认的命名方案,因为名称并没有说明 creeps 的作用。为此,我编写了一个名为 util.nameBuilder 的小实用程序 class。
它使用 Memory.nameIndex = {};
来记录前缀被分配给 creep 的次数。因为我不想在 creep 的生成失败时随机增加计数,所以我有一个 getName(role)
和一个 commitName(role)
。 getName()
returns 一个要使用的名称和 commitName()
将使计数增加 1。如果它超过某个阈值(比如 1000),您可能希望 'fold' 计数到防止像 harvester243874 这样的名字。如果 createCreep()
returns 出现 'name already in use' 错误,我的 spawn 方法实际上会调用 commitName(role)
。
这段代码的结果是这样的名字:
收割机 1、建造机 23、升级机 5
var nameBuilder = {
getName: function(role) {
if (Memory.nameIndex === undefined)
Memory.nameIndex = {};
if (Memory.nameIndex[role] === undefined)
Memory.nameIndex[role] = 0;
return role + (Memory.nameIndex[role] + 1);
},
commitName: function(role) {
var newIndex = Memory.nameIndex[role] + 1;
Memory.nameIndex[role] = newIndex;
}
};
module.exports = nameBuilder;
我发现只要用他们的工作和当前游戏时间命名就可以很好地满足需要。
例如:"Builder" + Game.time.toString()
= Builder1234
一个生成物一次只能生成一个 creep,因此名称应该始终是唯一的。
唯一的问题是,如果 2 个以上的生成器试图同时创建一个机器人 (相同的刻度),谁没有先启动生成器,谁就会失败并显示 ERR_NAME_EXISTS
,但你可以在下一个周期得到它们。
与@Matthew-Regul 类似,我只是给了他们一个随机序列号,附在他们的职责上:
'UPGRADER_' + (Math.floor(Math.random() * 65534) + 1)
虽然他消除了极小的重复名字的可能性
"Upgrader_" + spawn.name + "_" + Game.time
创建一个有保证的唯一名称。
在编程游戏 Screeps 中,我使用以下方法生成 creep:
if(transporters.length < 0 && harvesters.length > 2) {
var newName = Game.spawns['SpawnZone'].createCreep([WORK,CARRY,MOVE], undefined, {role: 'transporter'});
console.log('Spawning new Transporter: ' + newName);
}
'undefined' 标签用于生成具有随机名称的 creep。现在我想知道,因为我有不同类型的小兵,是否可以在它前面添加角色标签?例如,[Transporter] RandomName.
这可能吗?
您可以实现一个函数,以您想要的格式生成名称,并将 undefined
替换为生成的名称。来自 Screeps documentation:
createCreep(body, [name], [memory])
Start the creep spawning process. The required energy amount can be withdrawn from all spawns and extensions in the room.
[...]
name (optional) string
The name of a new creep. It should be unique creep name, i.e. the Game.creeps object should not contain another creep with the same name (hash key). If not defined, a random name will be generated.
Screeps 论坛实际上已经有一个 solution 可以满足您的需求。抄录如下:
var names1 = ["Jackson", "Aiden", "Liam", "Lucas", "Noah", "Mason", "Jayden", "Ethan", "Jacob", "Jack", "Caden", "Logan", "Benjamin", "Michael", "Caleb", "Ryan", "Alexander", "Elijah", "James", "William", "Oliver", "Connor", "Matthew", "Daniel", "Luke", "Brayden", "Jayce", "Henry", "Carter", "Dylan", "Gabriel", "Joshua", "Nicholas", "Isaac", "Owen", "Nathan", "Grayson", "Eli", "Landon", "Andrew", "Max", "Samuel", "Gavin", "Wyatt", "Christian", "Hunter", "Cameron", "Evan", "Charlie", "David", "Sebastian", "Joseph", "Dominic", "Anthony", "Colton", "John", "Tyler", "Zachary", "Thomas", "Julian", "Levi", "Adam", "Isaiah", "Alex", "Aaron", "Parker", "Cooper", "Miles", "Chase", "Muhammad", "Christopher", "Blake", "Austin", "Jordan", "Leo", "Jonathan", "Adrian", "Colin", "Hudson", "Ian", "Xavier", "Camden", "Tristan", "Carson", "Jason", "Nolan", "Riley", "Lincoln", "Brody", "Bentley", "Nathaniel", "Josiah", "Declan", "Jake", "Asher", "Jeremiah", "Cole", "Mateo", "Micah", "Elliot"]
var names2 = ["Sophia", "Emma", "Olivia", "Isabella", "Mia", "Ava", "Lily", "Zoe", "Emily", "Chloe", "Layla", "Madison", "Madelyn", "Abigail", "Aubrey", "Charlotte", "Amelia", "Ella", "Kaylee", "Avery", "Aaliyah", "Hailey", "Hannah", "Addison", "Riley", "Harper", "Aria", "Arianna", "Mackenzie", "Lila", "Evelyn", "Adalyn", "Grace", "Brooklyn", "Ellie", "Anna", "Kaitlyn", "Isabelle", "Sophie", "Scarlett", "Natalie", "Leah", "Sarah", "Nora", "Mila", "Elizabeth", "Lillian", "Kylie", "Audrey", "Lucy", "Maya", "Annabelle", "Makayla", "Gabriella", "Elena", "Victoria", "Claire", "Savannah", "Peyton", "Maria", "Alaina", "Kennedy", "Stella", "Liliana", "Allison", "Samantha", "Keira", "Alyssa", "Reagan", "Molly", "Alexandra", "Violet", "Charlie", "Julia", "Sadie", "Ruby", "Eva", "Alice", "Eliana", "Taylor", "Callie", "Penelope", "Camilla", "Bailey", "Kaelyn", "Alexis", "Kayla", "Katherine", "Sydney", "Lauren", "Jasmine", "London", "Bella", "Adeline", "Caroline", "Vivian", "Juliana", "Gianna", "Skyler", "Jordyn"]
Creep.getRandomName = function(prefix){
var name, isNameTaken, tries = 0;
do {
var nameArray = Math.random() > .5 ? names1 : names2;
name = nameArray[Math.floor(Math.random() * nameArray.length)];
if (tries > 3){
name += nameArray[Math.floor(Math.random() * nameArray.length)];
}
tries++;
isNameTaken = Game.creeps[name] !== undefined;
} while (isNameTaken);
return prefix+" "+name;
}
我发现 screeps 中缺少默认的命名方案,因为名称并没有说明 creeps 的作用。为此,我编写了一个名为 util.nameBuilder 的小实用程序 class。
它使用 Memory.nameIndex = {};
来记录前缀被分配给 creep 的次数。因为我不想在 creep 的生成失败时随机增加计数,所以我有一个 getName(role)
和一个 commitName(role)
。 getName()
returns 一个要使用的名称和 commitName()
将使计数增加 1。如果它超过某个阈值(比如 1000),您可能希望 'fold' 计数到防止像 harvester243874 这样的名字。如果 createCreep()
returns 出现 'name already in use' 错误,我的 spawn 方法实际上会调用 commitName(role)
。
这段代码的结果是这样的名字: 收割机 1、建造机 23、升级机 5
var nameBuilder = {
getName: function(role) {
if (Memory.nameIndex === undefined)
Memory.nameIndex = {};
if (Memory.nameIndex[role] === undefined)
Memory.nameIndex[role] = 0;
return role + (Memory.nameIndex[role] + 1);
},
commitName: function(role) {
var newIndex = Memory.nameIndex[role] + 1;
Memory.nameIndex[role] = newIndex;
}
};
module.exports = nameBuilder;
我发现只要用他们的工作和当前游戏时间命名就可以很好地满足需要。
例如:"Builder" + Game.time.toString()
= Builder1234
一个生成物一次只能生成一个 creep,因此名称应该始终是唯一的。
唯一的问题是,如果 2 个以上的生成器试图同时创建一个机器人 (相同的刻度),谁没有先启动生成器,谁就会失败并显示 ERR_NAME_EXISTS
,但你可以在下一个周期得到它们。
与@Matthew-Regul 类似,我只是给了他们一个随机序列号,附在他们的职责上:
'UPGRADER_' + (Math.floor(Math.random() * 65534) + 1)
虽然他消除了极小的重复名字的可能性
"Upgrader_" + spawn.name + "_" + Game.time
创建一个有保证的唯一名称。