Character Vault
Any Concept / Any System
Compendium
Your System Come To Life
Roll20 for Android
Streamlined for your Tablet
Roll20 for iPad
Streamlined for your Tablet

Personal tools

Difference between revisions of "Script:Language Script"

From Roll20 Wiki

Jump to: navigation, search
m
Line 32: Line 32:
  
 
Finally, the value of <code>languageAvatar</code> will be used to set the avatar of the language characters. If you want to change the image, you may only use a URL from your Roll20 image library.
 
Finally, the value of <code>languageAvatar</code> will be used to set the avatar of the language characters. If you want to change the image, you may only use a URL from your Roll20 image library.
 
=== Code ===
 
<pre data-language="javascript">// Gist: https://gist.github.com/BaldarSilveraxe/d66d4aad2fb6f5baed3e
 
 
var languageAvatar = "https://s3.amazonaws.com/files.d20.io/images/5538325/0OqDji-XRtAd9ilCHbTbOQ/thumb.png?1410447564"
 
var roll20API = roll20API || {};
 
var whoSpoke = "";
 
var whichLanguage = "";
 
var languageSeed = 0;
 
var gibberish = "";
 
 
roll20API.languageData = [
 
{
 
    Description: "Dwarven", 
 
    Sheet: "Language(Dwarven)", 
 
    languageSeed: 2,
 
    characters: "Dwarven"},
 
{
 
    Description: "Elvish", 
 
    Sheet: "Language(Elvish)",   
 
    languageSeed: 4,
 
    characters: "Elvish"}
 
];
 
 
numbers = [];
 
numbers["Standard"] = ["1","2","3","4","5","6","7","8","9","0"];
 
numbers["Dwarven"] = ["·",":","∴","+","◊","◊·","◊:","◊∴","◊+","°"];
 
numbers["Elvish"] = ["·",":","∴","+","¤","¤·","¤:","¤∴","¤+","°"];
 
numbers["Minor"] = ["·",":","∴","+","×","×·","×:","×∴","×+","°"];
 
numbers["Evil"] = ["·",":","∴","+","∏","∏·","∏:","∏∴","∏+","°"];
 
symbols = [];
 
symbols["Standard"] = ["!","@","#","$","%","¦","&","*","(",")","`","-","=","~","_","+","[","]","{","}","|",";","'",":",",",".","/","<",">","?"];
 
symbols["Dwarven"] = ["∫","»","‡","∇","‰","¦","χ","¬","|","]","`","-","Ξ","⊥","_","=","|","|","|","|","|","|","^","|","-","•","||","[","]","Ž"];
 
symbols["Elvish"] = ["~","•","⊕","§","‰","¦","∞","Φ","}","|","`","-","Ξ","≈","_","†","]","[",")","(","|","|","'","∫","≈","·","∠", "∈","∋","¿"];
 
symbols["Minor"] = ["~","•","⊕","§","‰","¦","∞","Φ","}","|","`","-","Ξ","≈","_","†","]","[",")","(","|","|","'","∫","≈","·","∠", "∈","∋","¿"];
 
symbols["Evil"] = ["∧","•","‡","§","‰","¦","χ","Φ","|","]","`","-","Ξ","≈","_","∧","|","|",")","(","|","|","'","∫","≈","•","||","∈","∋","¿"];
 
lower = [];
 
lower["Standard"] = ["b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","z"," "];
 
lower["Dwarven"] = ["⌈","⌉","⌊","⌋","¬","¦","Γ","Λ","Ξ","Π","‡","∫","[","]","|","||","ΠΠ","VV","M","|","|"];
 
lower["Elvish"] = ["λ","Ψ","ϒ","ϖ","φ","ç","þ","Þ","q","r","ξ","∫","~","∈","∋","ω","∪","⊆","⊇","∂"," '"];
 
lower["Minor"] = ["b","c","d","ϒ","þ","h","∫","k","l","m","n","p","q","r","s","‡","v","w","t","×"," "];
 
lower["Evil"] = ["ζ","Ψ","ϒ","ϖ","∏","ç","þ","Þ","q","ξ","ς","∫","ς","∈","∋","ω","∪","⊆","⊇","μ","' "];
 
upper = [];
 
upper["Standard"] = ["B","C","D","F","G","H","J","K","L","M","N","P","Q","R","S","T","V","W","X","Z"];
 
upper["Dwarven"] = ["⌈","⌉","⌊","⌋","¬","¦","Γ","Λ","Ξ","Π","‡","∫","[","]","|","||","ΠΠ","VV","M","|"];
 
upper["Elvish"] = ["λ","Ψ","ϒ","ϖ","φ","ç","þ","Þ","θ","η","ξ","∫","~","∈","∋","ω","∪","⊆","⊇","∂"];
 
upper["Minor"] = ["B","C","D","ϒ","þ","H","∫","K","L","M","N","P","θ","Γ","∂","‡","V","W","‡","χ"];
 
upper["Evil"] = ["ζ","Ψ","ϒ","ϖ","∏","ç","þ","Þ","θ","ξ","ς","∫","ς","E","∃","ω","∪","⊆","⊇","μ"];
 
vowel = [];
 
vowel["Standard"] = ["a","e","i","o","u","y","A","E","I","O","U","Y"];
 
vowel["Dwarven"]  = ["⌈","⌉","⌊","⌋","¬","¦","Γ","Λ","Ξ","Ξ","Î ","‡"];
 
vowel["Elvish"] = ["í","ä","ö","ý","ú","ë","Í","Ä","Ö","Ÿ","Ú","Ë"];
 
vowel["Minor"] = ["à ","è","ì","õ","ù","ý","À","È","Ì","Ò","Ù","Ý"];
 
vowel["Evil"] = ["ô","ô","û","û","î","î","Ô","Ô","Û","Û","Î","Î"];
 
 
on("ready", function() {
 
    checkForSheets();
 
 
    on("chat:message", function (msg) {
 
        // Exit if not an api command
 
        if (msg.type != "api") return;
 
       
 
        //****Check For Language
 
        checkForLanguage(msg);
 
 
        // Get the API Chat Command
 
        msg.who = msg.who.replace(" (GM)", "");
 
        msg.content = msg.content.replace("(GM) ", "");
 
        var command = msg.content.split(" ", 1);
 
 
    });
 
});
 
 
prepareSend = function(msg) {
 
    var sentence = msg.content.substr(1)
 
    if(sentence.length == 0) {
 
        sendChat("API", "/w " + whoSpoke + " You didn't say anything.");
 
        return;
 
    };
 
 
    var numbersArray = numbers[characters]
 
    var symbolsArray = symbols[characters]
 
    var lowerArray = lower[characters]
 
    var upperArray = upper[characters]
 
    var vowelArray = vowel[characters]
 
    var givenArray = numbersArray.concat(symbolsArray,lowerArray,upperArray,vowelArray);
 
 
    var numbersArrayStandard = numbers["Standard"]
 
    var symbolsArrayStandard = symbols["Standard"]
 
    var lowerArrayStandard = lower["Standard"]
 
    var upperArrayStandard = upper["Standard"]
 
    var vowelArrayStandard = vowel["Standard"]
 
    var standardArray = numbersArrayStandard.concat(symbolsArrayStandard,lowerArrayStandard,upperArrayStandard,vowelArrayStandard);
 
 
    gibberish = sentence;
 
    for (var i=0;i<sentence.length;i++){
 
        var characterGiven = sentence.substr(i,1);
 
        var rng = CustomRandom(characterGiven.charCodeAt(0) + languageSeed);
 
        var gibberishCharacter = "";
 
       
 
        gibberishFunction(standardArray,givenArray,characterGiven)
 
 
    };
 
 
    var theSpeaker = _.findWhere(roll20API.fluencyArray, {isSpeaking: 1});
 
    if(theSpeaker.speaks == -1){
 
        sendChat(whoSpoke + "in mocking " + whichLanguage, gibberish);
 
        return
 
    };
 
   
 
    sendChat("yourself", "/w " + whoSpoke + " '" + sentence +"' in " + whichLanguage + ".");
 
    sendChat(whoSpoke, "/w gm Out loud in " + whichLanguage + ": '" + sentence + "'");
 
   
 
    _.each(roll20API.fluencyArray, function(indexPlayers) {
 
        if(indexPlayers.displayNameShort != whoSpoke){
 
            if(indexPlayers.speaks != -1){
 
                sendChat(whoSpoke, "/w " + indexPlayers.displayNameShort + " '" + sentence +"' in " + whichLanguage + ".");
 
            }else{
 
                sendChat(whoSpoke, "/w " + indexPlayers.displayNameShort + " " + gibberish)
 
            };   
 
        };
 
    });   
 
};
 
 
gibberishFunction = function(standardChar,changeChar,givenChar) {
 
 
        for (var j=0; j<standardChar.length; j++) {
 
            var rng = CustomRandom(givenChar.charCodeAt(0) + languageSeed);
 
            rndSeed = rng.next(1,changeChar.length);
 
            var seedRndInt = Math.round(rndSeed);
 
            if(standardChar[j] == givenChar) {
 
                if(changeChar[seedRndInt] == undefined){
 
                    var replaceCharacter = " ";
 
                }else{
 
                    var replaceCharacter = changeChar[seedRndInt];
 
                };
 
                gibberish = gibberish.replace(givenChar,replaceCharacter);
 
            };
 
        };
 
   
 
};
 
 
//Seeded Random function
 
var CustomRandom = function(nseed) {   
 
    var seed, constant = Math.pow(2, 13)+1, prime = 1987, maximum = 1000;   
 
    if (nseed) {
 
        seed = nseed;   
 
    }
 
    if (seed == null) {
 
        // If no seed is given use date and time.
 
        seed = (new Date()).getTime(); 
 
    }return{   
 
        next : function(min, max) {   
 
            seed *= constant;   
 
            seed += prime;   
 
            // if 'min' and 'max' are not provided, return random number between 0 & 1
 
            return min && max ? min+seed%maximum/maximum*(max-min) : seed%maximum/maximum; 
 
        }
 
    }
 
};
 
 
checkForFluency = function(msg) {
 
    if(findObjs({ _type: "character", name: msg.who }).length !== 0){
 
        var spokenByIds = findObjs({ _type: "character", name: msg.who })[0].get("controlledby").split(",");
 
        if (spokenByIds[0].length == 0){
 
            var languageError = "Uncontrolled Language Sheet."
 
            sendChat("API", languageError);
 
            return;
 
        };
 
    }else{
 
        var languageError = "Language Sheet does not exist.";
 
        sendChat("API", languageError);
 
        return;
 
    };
 
    var allPlayers = findObjs({_type: "player"}, {caseInsensitive: true});
 
    roll20API.fluencyArray = []
 
    _.each(allPlayers, function(indexPlayers) {
 
        var isSpeaking = 0;
 
        if(indexPlayers.get("_id") == msg.playerid){
 
            isSpeaking = 1;
 
            whoSpoke = indexPlayers.get("_displayname").substr(0,indexPlayers.get("_displayname").indexOf(' '));
 
        };
 
        var displayNameShort = indexPlayers.get("_displayname").substr(0,indexPlayers.get("_displayname").indexOf(' '));
 
        var displayNameFull = indexPlayers.get("_displayname")
 
        var playerID = indexPlayers.get("_id");
 
        var asWho = msg.who
 
        var speaks = spokenByIds.indexOf(indexPlayers.get("_id"));
 
        roll20API.fluencyArray.push({
 
            isSpeaking: isSpeaking,
 
            displayNameShort: displayNameShort,
 
            displayNameFull: displayNameFull,
 
            playerID: playerID,
 
            asWho: asWho,
 
            speaks: speaks
 
        });
 
    });
 
    prepareSend(msg);
 
};
 
 
checkForLanguage = function(msg) {
 
    var isLanguage = false;
 
    _.each(roll20API.languageData, function(eachLanguage) {
 
        var saidAs = msg.who;
 
        var languageToCheck = eachLanguage.Sheet;
 
        if(saidAs == languageToCheck){
 
            isLanguage = true;
 
            whichLanguage = eachLanguage.Description;
 
            languageSeed = eachLanguage.languageSeed;
 
            characters = eachLanguage.characters;
 
        };
 
    });
 
    if(isLanguage == false){return};
 
    checkForFluency(msg);
 
};
 
 
checkForSheets = function() {
 
        _.each(roll20API.languageData, function(eachLanguage) {
 
        var languageExist = findObjs({
 
                _type: "character",
 
                name: eachLanguage.Sheet
 
        });
 
        if (languageExist.length == 0) {
 
            createObj("character", {
 
                avatar: languageAvatar,
 
                name: eachLanguage.Sheet,
 
                archived: false
 
            });
 
        };
 
    });
 
};</pre>
 

Revision as of 22:21, 8 January 2015

API ScriptAuthor: Stephen S.
Version: 2.0
Last Modified: 2014-09-11
Code: Language Script
Dependencies: None
Conflicts: None

Allows users to convert things they type into gibberish for characters who don't speak the language the player is speaking as. Language sheets are created automatically, allowing players to set their Speaking As dropdown to the desired language in order to speak it.

What Did He Say? expands on version 1 of this script. There is significant difference between the usage of the two scripts, enough to list them separately.

Syntax

Select a language to speak as, and type a message into chat starting with !. Players with a character that speaks the selected language will receive the actual message, while other players will receive garbage.

Installation

Available languages are defined near the top of the script. Each language is a JavaScript object in the format:

{
    Description: "Elvish",   
    Sheet: "Language(Elvish)",    
    languageSeed: 4, 
    characters: "Elvish"
}
  • Description: A description of the language, to use in a sentence. For example, "Legolas is speaking Elvish."
  • Sheet: The name of the character to associate with the language. The character will be created if it does not already exist.
    • CRITICAL: Assign control of this character to all players able to speak it!
  • languageSeed: A number to assist int gibberish generation.
  • characters: A key to use for distinguishing languages from one another when generating gibberish. This will allow you to generate different gibberish for different languages.


For each unique value of characters, you should create an array of numbers, symbols, lower-case consonants, upper-case consonants, and vowels.

Finally, the value of languageAvatar will be used to set the avatar of the language characters. If you want to change the image, you may only use a URL from your Roll20 image library.