pgs_predator_wanderer.lsl
//////////////////////////////////////////////////////////
// [K] Kira Komarov - 2011, License: GPLv3              //
// Please see: http://www.gnu.org/licenses/gpl.html     //
// for legal details, rights of fair usage and          //
// the disclaimer and warranty conditions.              //
//////////////////////////////////////////////////////////
 
// Vector that will be filled by the script with
// the initial starting position in region coordinates.
vector _iPos = ZERO_VECTOR;
// Integer that the script will use to detect
// the next target position it will reach.
integer _targetID = 0;
 
// Owner key.
key _owner = NULL_KEY;
 
// Returns the next random coordinates.
//vector nextCoordinates() {
//    float r = llFrand(10);
//    float a = llFrand(TWO_PI);
//    float b = llFrand(TWO_PI);
//    return _iPos + <r * llCos(a) * llCos(b), r * llCos(a) * llSin(b), r * llSin(a)>;
//}
 
vector spherePoint() {
    float x = /* llPow(-1, 1 + (integer) llFrand(2))  * */ llFrand(7.5);
    float y = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(7.5);
    float z = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(7.5);
    if(llPow(x,2) + llPow(y,2) + llPow(z,2) <= 56.25)
        return _iPos - <1, 0, 0> + <x, y, z>;
    return spherePoint();
}
 
// Orientates the primitive's positive z axis 
// towards a position and moves the primitive
// towards that position.
//
// IN: vector representing a position in region
// coordinates.
// OUT: nothing.
moveTo(vector position) {
    llTargetRemove(_targetID);
    _targetID = llTarget(position, 0.8);
    llLookAt(position, 0.6, 0.6);
    llMoveToTarget(position, 3.0);    
}
 
default
{
    state_entry() {
        _owner = llGetOwner();
        llSetStatus(STATUS_PHYSICS, FALSE);
        integer comChannel = ((integer)("0x"+llGetSubString((string)llGetOwner(),-8,-1)) & 0x3FFFFFFF) ^ 0xBFFFFFFF;
        llListen(comChannel+1, "[BIOSE-SX]-[WaS-K] Population Genetics and Selection", "", "");
    }
    listen(integer channel, string name, key id, string message) {
        if(message == "PGS_PAUSE") {
            llSetStatus(STATUS_PHYSICS, FALSE);
            return;
        }
        if(message == "PGS_UNPAUSE") {
            llSetStatus(STATUS_PHYSICS, TRUE);
            llSetForce(<0,0,9.81> * llGetMass(), 0);
            //moveTo(nextCoordinates());
            moveTo(spherePoint());
            return;
        }
        if(_iPos != ZERO_VECTOR) return;
        list _iPosList = llParseString2List(message, ["<", ">", ","], []);
        if(llGetListLength(_iPosList) != 3) return;
        _iPos.x = llList2Float(_iPosList, 0);
        _iPos.y = llList2Float(_iPosList, 1);
        _iPos.z = llList2Float(_iPosList, 2);
        llSetStatus(STATUS_PHYSICS, TRUE);
        llSetForce(<0,0,9.81> * llGetMass(), 0);
        moveTo(spherePoint());
    }
 
    at_target(integer tnum, vector targetpos, vector ourpos) {
        if(tnum != _targetID) return;
        moveTo(spherePoint());
    }
    changed(integer change) {
        if(change & CHANGED_OWNER) llResetScript();
    }
    on_rez(integer num) {
        llResetScript();
    }
    collision_start(integer num) {
        moveTo(spherePoint());
    }
}