chaise_longue_pillows.lsl
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3      //
//  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  //
//  rights of fair usage, the disclaimer and warranty conditions.        //
///////////////////////////////////////////////////////////////////////////
 
///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
integer wasMenuIndex = 0;
list wasDialogMenu(list input, list actions, string direction) {
    integer cut = 11-wasListCountExclude(actions, [""]);
    if(direction == ">" &&  (wasMenuIndex+1)*cut+wasMenuIndex+1 < llGetListLength(input)) {
        ++wasMenuIndex;
        jump slice;
    }
    if(direction == "<" && wasMenuIndex-1 >= 0) {
        --wasMenuIndex;
        jump slice;
    }
@slice;
    integer multiple = wasMenuIndex*cut;
    input = llList2List(input, multiple+wasMenuIndex, multiple+cut+wasMenuIndex);
    input = wasListMerge(input, actions, "");
    return input;
}
 
///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
integer wasListCountExclude(list input, list exclude) {
    if(llGetListLength(input) == 0) return 0;
    if(llListFindList(exclude, (list)llList2String(input, 0)) == -1) 
        return 1 + wasListCountExclude(llDeleteSubList(input, 0, 0), exclude);
    return wasListCountExclude(llDeleteSubList(input, 0, 0), exclude);
}
 
///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
list wasListMerge(list l, list m, string merge) {
    if(llGetListLength(l) == 0 && llGetListLength(m) == 0) return [];
    string a = llList2String(m, 0);
    if(a != merge) return [ a ] + wasListMerge(l, llDeleteSubList(m, 0, 0), merge);
    return [ llList2String(l, 0) ] + wasListMerge(llDeleteSubList(l, 0, 0), llDeleteSubList(m, 0, 0), merge);
}
 
string pillow = "";
list menu = [];
integer lock = 0;
key mav = NULL_KEY;
 
default {
    state_entry() {
        mav = llGetOwner();
        integer comChannel = (integer)("0x8" + llGetSubString(mav, 0, 6));
        llListen(comChannel, "", mav, "");
        llDialog(mav, "\n            Welcome to the Chaise Longue.\nCreated in 2013 by Wizardry and Steamworks\n                 29 April 2013: Version: 1.2", [ "Large", "Small" ], comChannel);
    }
    touch_start(integer num) {
        if(llDetectedKey(0) != mav) return;
        lock = 0;
    }
    touch(integer num) {
        if(llDetectedKey(0) != mav) return;
        ++lock;
    }
    touch_end(integer num) {
        if(llDetectedKey(0) != mav) return;
        if(lock > 30) state locked;
        integer comChannel = (integer)("0x8" + llGetSubString(mav, 0, 6));
        llListen(comChannel, "", mav, "");
        llDialog(mav, "\n            Welcome to the Chaise Longue.\nCreated in 2013 by Wizardry and Steamworks\n                 29 April 2013: Version: 1.2", [ "Large", "Small" ], comChannel);
    }
    listen(integer channel, string name, key id, string message) {
        if(message == "Large") {
            pillow = message;
            state textures;
        }
        if(message == "Small") {
            pillow = message;
            state textures;
        }
    }
    changed(integer change) {
        mav = llGetOwner();
    }
    on_rez(integer param) {
        mav = llGetOwner();
        llResetScript();
    }
}
 
state textures {
    state_entry() {
        menu = [];
        integer i = llGetInventoryNumber(INVENTORY_TEXTURE)-1;
        do {
            menu += llGetInventoryName(INVENTORY_TEXTURE, i);
        } while(--i>-1);
        integer comChannel = (integer)("0x8" + llGetSubString(mav, 0, 6));
        llListen(comChannel, "", mav, "");
        llDialog(mav, "\n            Welcome to the Chaise Longue.\nCreated in 2013 by Wizardry and Steamworks\n                 29 April 2013: Version: 1.2", wasDialogMenu(menu, ["⟻ Back", "Next ⟼", "⏏ Exit"], ""), comChannel);
    }
    touch_start(integer num) {
        if(llDetectedKey(0) != mav) return;
        lock = 0;
    }
    touch(integer num) {
        if(llDetectedKey(0) != mav) return;
        ++lock;
    }
    touch_end(integer num) {
        if(llDetectedKey(0) != mav) return;
        if(lock > 30) state locked;
        menu = [];
        integer i = llGetInventoryNumber(INVENTORY_TEXTURE)-1;
        do {
            menu += llGetInventoryName(INVENTORY_TEXTURE, i);
        } while(--i>-1);
        integer comChannel = (integer)("0x8" + llGetSubString(mav, 0, 6));
        llListen(comChannel, "", mav, "");
        llDialog(mav, "\n            Welcome to the Chaise Longue.\nCreated in 2013 by Wizardry and Steamworks\n                 29 April 2013: Version: 1.2", wasDialogMenu(menu, ["⟻ Back", "Next ⟼", "⏏ Exit"], ""), comChannel);
    }
    listen(integer channel, string name, key id, string message) {
        if(message == "⏏ Exit") {
            menu = [];
            state default;
        }
        if(message == "⟻ Back") {
            llDialog(id, "\n            Welcome to the Chaise Longue.\nCreated in 2013 by Wizardry and Steamworks\n                 29 April 2013: Version: 1.2", wasDialogMenu(menu, ["⟻ Back", "Next ⟼", "⏏ Exit"], "<"), channel);
            return;
        }
        if(message == "Next ⟼") {
            llDialog(id, "\n            Welcome to the Chaise Longue.\nCreated in 2013 by Wizardry and Steamworks\n                 29 April 2013: Version: 1.2", wasDialogMenu(menu, ["⟻ Back", "Next ⟼", "⏏ Exit"], ">"), channel);
            return;
        }
        if(llGetInventoryType(message) != INVENTORY_TEXTURE) return;
        if(pillow == "Large") llSetLinkTexture(1, message, ALL_SIDES);
        if(pillow == "Small") llSetLinkTexture(2, message, ALL_SIDES);
        llDialog(id, "\n            Welcome to the Chaise Longue.\nCreated in 2013 by Wizardry and Steamworks\n                 29 April 2013: Version: 1.2", wasDialogMenu(menu, ["⟻ Back", "Next ⟼", "⏏ Exit"], ""), channel);
    }
    changed(integer change) {
        mav = llGetOwner();
    }
    on_rez(integer param) {
        mav = llGetOwner();
        llResetScript();
    }
}
 
state locked {
    state_entry() {
        llOwnerSay("Locked.");
    }
    touch_start(integer num) {
        if(llDetectedKey(0) != mav) return;
        lock = 0;
    }
    touch(integer num) {
        if(llDetectedKey(0) != mav) return;
        ++lock;
    }
    touch_end(integer num) {
        if(lock < 30) return;
        llOwnerSay("Unlocked.");
        state default;
    }
    changed(integer change) {
        mav = llGetOwner();
    }
    on_rez(integer param) {
        mav = llGetOwner();
        llResetScript();
    }
}