Adventure System - Multiple Defeat Chapter Support


#1

I’m guessing this would be where to place this. I’m not 100% familiar with Javascript as I’ve mostly worked with java but this seems to work and suits my needs.

scripts/games/adventureSystem.js

Somewhere add this

function getDeaths() {
    var i;
    currentAdventure.caught = [];
    // $.consoleLn('Starting the death loop.');
    for (i in currentAdventure.users) {
        range = $.randRange(0, 25);
        //$.consoleLn('random range: ' + range);
        if (range <= 5) {
            // $.consoleLn('Adding a death: ' + currentAdventure.users[i].username);
            currentAdventure.caught.push(currentAdventure.users[i]);
        }    
    }
};

Replace this method

function calculateResult() {
    var i;
    getDeaths();
    var death;
   for (i = currentAdventure.survivors.length - 1; i >= 0; i--) {
        //$.consoleLn('Looping through the survivors to find who to remove.');
        for (death in currentAdventure.caught) {
             //$.consoleLn('Looping through the deaths to find who to remove.');
             if (currentAdventure.caught[death].username == currentAdventure.survivors[i].username) 
                 //$.consoleLn('Found a death: ' + currentAdventure.caught[death].username);
                 currentAdventure.caught.push(currentAdventure.survivors[i]);
                 currentAdventure.survivors.splice(i, 1);
                 //$.consoleLn('Successfully removed: ' + currentAdventure.caught[death].username);
            }
        }
    }
};

Below this line

if (line.indexOf('(caught)') > -1) {

add

calculateResult();

under

function runStory()

Remove the calculateResult(), since we need to calculate it only when attempting to defeat some of the players. and add this where it was

var i;
for (i in currentAdventure.users) { 
    currentAdventure.survivors.push(currentAdventure.users[i]);
}

Replace this line

if (progress < story.lines.length) {

With this

if (progress < story.lines.length && currentAdventure.survivors.length > 0) {

This will allow you to do (caught) in multiple chapters of the same quest, with a 20% chance of surviving per player.


#2

I reformatted it using the ``` markdown so it was a little more readable.

Cheers


#3

Thanks, first time posting on here so was working on improving it.


#4

Would be nice to see this added as a feature in the main build


#5

No worries, just thought I would help. <3


#6

General note when I have tested in JavaScript in another module. Using array.splice() pops an item from the array, but doesn’t change the original size of the loop, so oddness can happen. I do something like this:

        for (var i = users.length - 1; i >= 0; i--) {
            if (!hasKey(newUsers, users[i][0])) {
                users.splice(i, 1);
            }
        }

Go backwards. That way you are popping off the last item on the list and moving down it. I found this while testing a while ago:

1
2
3
4
5
6
----------
1
2
3
splice > 4
splice at 3
6
----------
6
5
splice > 4
splice at 3
3
2
1
var list = [];
list.push('1');
list.push('2');
list.push('3');
list.push('4');
list.push('5');
list.push('6');

for (var i in list) {
    print(list[i]);
}
print('----------');

for (var i in list) {
    if (i == 3) {
        print ('splice > ' + list[i]);
        list.splice(i, 1);
        print ('splice at 3');
    } else {
        print (list[i]);
    }
}

var list = [];
list.push('1');
list.push('2');
list.push('3');
list.push('4');
list.push('5');
list.push('6');
print('----------');
for (var i = list.length - 1; i >= 0; i--) {
    if (i == 3) {
        print ('splice > ' + list[i]);
        list.splice(i, 1);
        print ('splice at 3');
    } else {
        print (list[i]);
    }
}

Because i is still moving forward as an index and you shift the elements down one, the index doesn’t know this and can skip over an entry. As you see above, I remove position 3 then ‘4’ and ‘5’ are also skipped. This is because the index (i) doesn’t know to go back one. If you traverse backwards, not an issue. Since I see you are checking currentAdventure.survivors[i] after the splice, you may be skipping an entry.

Notice the second loop, going backwards, how only one number is skipped.

Just something you should test for and make sure it is working as expected.

Cheers


#7

Thanks for the heads up, this is my first time working with JS so its a bit of a transition learning the syntax and all the methods. In java I’d of just used a list, and used the remove method.

Edited the code to reflect that change, hopefully its good now.


#8

could you upload the file for download if you don’t mind as id like to take a look at where the code goes easier


#9

It is an obscure “bug.” I only found it when trying to merge the $.users object with a newer object and wondering why people disappeared :slight_smile: Glad to help.


#10

Paste.ee - Adventure system Here you go.


#11

awesome Thanks :slight_smile: much easier to see using the compare feature in notepad ++