fortune.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) 2011 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
string wasSpaceWrap(string txt, string delimiter, integer column) {
    string ret = llGetSubString(txt, 0, 0);
    integer len = llStringLength(txt);
    integer itra=1;
    integer itrb=1;
    do {
        if(itrb % column == 0) {
            while(llGetSubString(txt, itra, itra) != " ") {
                ret += llGetSubString(txt, itra, itra);
                if(++itra>len) return ret;
            }
            ret += delimiter;
            itrb = 1;
            jump next;
        }
        ret += llGetSubString(txt, itra, itra);
        ++itrb;
@next;
    } while(++itra<len);
    return ret;
}
 
integer f;
string say;
 
default {
    state_entry() {
        if(llGetInventoryType("Fortunes") == INVENTORY_NOTECARD) jump schedule;
        llSay(DEBUG_CHANNEL, "Fortunes notecard not found.");
        return;
@schedule;
        llGetNumberOfNotecardLines("Fortunes");
    }
    dataserver(key requested, string data) { 
        f=(integer)data;
        state quote;
    }
    on_rez(integer num) {
        llResetScript();
    }
    changed(integer change) { if(change & CHANGED_REGION_START) llResetScript(); }
}
 
state quote {
    state_entry() {
        llGetNotecardLine("Fortunes", (integer)llFrand(f));
    }
    timer() {
        state speak;
    }
    dataserver(key requested, string data) {
        say = data;
        llSetTimerEvent((float)llStringLength(data)/4.0);
 
    }
    on_rez(integer num) {
        llResetScript();
    }
    changed(integer change) { if(change & CHANGED_REGION_START) llResetScript(); }
}
 
state speak {
    state_entry() {
        llSetText(wasSpaceWrap(say, "\n", 10) + "\n \nO\n  o\n   .", <1,1,0>, 1.0);
        llSetTimerEvent(1);
    }
    timer() {
        state quote;
    }
    on_rez(integer num) {
        llResetScript();
    }
    changed(integer change) { if(change & CHANGED_REGION_START) llResetScript(); }
}