The problem comes in during the Switch statement. If I have 1 Switch group it runs fine but it gets exponentially slower each Switch item added. At 4 items it takes about ten seconds. At six items it takes longer than a man can wait and not starve to death.
I'm getting the length of the array for the dice roll to randomly pick an item. I manually feed in how many times it should loop through the Switch. That doesn't seem to add length. I can loop through ten times or twenty times. The time lag comes only when I add more Switch items. Right now four are listed but I'd obviously like to have more.
The Switch is calling an image from a table for each treasure type. So there is a small table with a 1. Coins Image. 2. Bevarages Image, etc. Then it rolls a die based on the length of the array and picks an item. Finally it adds a row to the output table that lists in the chat box. It's weird because I say I want 20 treasure items but there is only 1 switch case. Boom instant. But if I saw I want 1 treasure item but there are 4 switch cases, ten second wait.
There is also an IF statement near the top if the creature has no treasure. It just shoots out a smaller table.
I feel like I'm doing something stupid with my loops that's causing problems but maybe it's just retrieving information from the Lists takes longer and longer?
Any ideas?
Thanks!
Tom
Code: Select all
[h: gemsJSON = json.fromList("Agate,Agate Geode,Zircon")]
[h: beveragesJSON = json.fromList("Flask of Sparkling Cider,Flask of Spicy Rum,Flask of Egg Nog,Flask of Lemonade")]
[h: coinsJSON = json.fromList("Copper,Silver,Gold,Platinum")]
[h: herbsJSON = json.fromList("Case of Cigarettes,Case of Thick Cigars,Pouch of Oolong Tea,Pouch of White Tea")]
[h: jewelleryJSON = json.fromList("Gold Amulet,Silver Toe Ring,Silver Torc")]
[h: foodJSON = json.fromList("Bag of Black Licorice,Bag of Red Licorice,Sourdough")]
[h: cookingJSON = json.fromList("Fondue Kit,Rolling Pin,Fork,Iron Skillet,Wine Carafe")]
[h: lengthGems = json.length(gemsJSON)]
[h: lengthBeverages = json.length(beveragesJSON)]
[h: lengthCoins = json.length(coinsJSON)]
[h: lengthHerbs = json.length(herbsJSON)]
[h: lengthJewellery = json.length(jewelleryJSON)]
[h: lengthFood = json.length(foodJSON)]
[h: lengthCooking = json.length(cookingJSON)]
[h: me = getSelected()]
[h: switchToken(me)]
[h: booty = getSpeech("Booty",me)]
[h, if (booty=="No"), CODE: {
[h: treasureFinal = '<table border=1><tr><th>Creature</th></tr><td>The creature has no treasure.</td></tr></table>']
[h: setSpeech("Booty",treasureFinal)]
};{
[h: rollingDice = input("Dice")]
[h: abort(rollingDice)]
[h: loopTreasure = Dice]
[h: equipment = getProperty("Equipment",me)]
[h: treasureFinal = '<table border=1><tr><th>Item</th><th>Image</th></tr>']
[h, FOREACH (item,equipment), CODE: {
[h: treasureLine = '<tr><td>' + item + '</td><td> </td></tr>']
[h: treasureFinal = treasureFinal + treasureLine]
}]
[h, WHILE(loopTreasure>0), CODE :{
[h: treasureRoll = d4]
[h, switch(treasureRoll), CODE:
case 1: {
[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",1,50))]
[h: roll = roll(lengthGems)]
[h: item = json.get(gemsJSON,roll-1)]
[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
};
case 2: {
[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",2,50))]
[h: roll = roll(lengthBeverages)]
[h: item = json.get(beveragesJSON,roll-1)]
[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
};
case 3: {
[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",3,50))]
[h: roll = roll(lengthCoins)]
[h: item = json.get(coinsJSON,roll-1)]
[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
};
case 4: {
[h: treasureImage = strformat("<img src='%s' />", tableImage("treasureTypeImages",4,50))]
[h: roll = roll(lengthHerbs)]
[h: item = json.get(herbsJSON,roll-1)]
[h: treasureFinal = treasureFinal + '<tr><td>' + item + '</td><td>' + treasureImage + '</td></tr>']
};
default: {[h: "test"]} ]
[h: loopTreasure = loopTreasure - 1]
}]
[h: treasureFinal = treasureFinal + '</table>']
[h: setSpeech("Booty",treasureFinal)]
}]