Skip to content

Encounters: Shuffle algorithm or similar for multiple encounters #4

@valahraban

Description

@valahraban

The encounter script adds a great feature to AID and already does many things I desire when it comes to outputting encounters. encounterWordLists can be adapted to output random encounters in fashion akin to classic video RPGs. Let's use Pokemon mobs as a simple example. We can already do encounterWordLists = {Route1:["Rattata", "Pidgey", "Spearow"]}. To encounter multiple correct mobs on Route 1 we can call {Route1}, {Route1}, {Route1} and it should output the mobs with proper grammar to boot.

On to the issue and feature request. The problem is the above has the possibility to output Pidgey, Pidgey, Pidgey which is not always desired behavior. Adding an option to iterate over an encounterWordList while removing the possibility of duplication gives the users even more control over their encounters while not being too difficult to implement.

So a function that does pickedInsert = getRndFromList(encounterWordLists[insertTag]) i number of times as defined by the user, with duplicates removed or prevented. The Durstenfeld shuffle is an efficient example of this, posting example code below. I believe this would be a cool feature to add to the Encounters script.

function shuffle(array) {
   for (var i = array.length - 1; i >= 0; i--) {
      var rnd = Math.floor(Math.random() * (i + 1));
      var temp = array[i];
      if (i >= 1) {
         //document.write(array[rnd] + ", ");
         console.log(array[rnd] + ", ");
      }
      else {
         //document.write(array[rnd]);
         console.log(array[rnd]);
      }
      array[i] = array[rnd];
      array[rnd] = temp;
   }
}

For our implementation we replace array.length with value determined by the user as long as it's <= array.length. Of course the word list needs to be neatly spliced in an array too.

Thank you for all your hard work and the scripts.

EDIT: Using an improved algorithm and fixed an error I made in previous sample code. I also recorded a gif of how this looks like with regular JS. https://files.catbox.moe/16ug6q.gif

Metadata

Metadata

Assignees

No one assigned

    Labels

    ...maybe?Might consider adding this, if a good point about its utiliy is made.EncountersFor the Encounters frameworkenhancementNew feature or requestideas wanted!Ideas/suggestions requested on this!

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions