Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a random name with a preceding nametag in Screeps?

In the Programming Game Screeps I spawn creeps by using:

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);
}

The 'undefined' tag is to spawn the creep with a random name. Now I'm wondering since I got different types of creeps if I can add a roletag infront of it? Like, for example, [Transporter] RandomName.

Is that possible?

like image 292
user3220962 Avatar asked Oct 01 '16 18:10

user3220962


3 Answers

I found that just naming with their job and the current game time fills the need well.

ex: "Builder" + Game.time.toString() = Builder1234

A spawn can only make one creep at a time, so the name should always be unique.

Only issue is if 2+ spawners try to make a bot at the same time (same tick), whomever didn't start the spawn first will fail with with ERR_NAME_EXISTS, but you could just get them on your next cycle.

enter image description here

like image 74
Matthew Regul Avatar answered Oct 19 '22 11:10

Matthew Regul


You could implement a function that will generate the name in the format you want and replace undefined with the generated name. From 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.

The Screeps Forum actually already has a solution for what you need. Transcript below:

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;
}
like image 41
Lucas Azevedo Avatar answered Oct 19 '22 13:10

Lucas Azevedo


Similar to @Matthew-Regul I just gave them a random serial number attached to their duty :

'UPGRADER_' + (Math.floor(Math.random() * 65534) + 1)

Although his removes the extremely slight possibility of repeated names

like image 3
TorZar Avatar answered Oct 19 '22 12:10

TorZar