Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Hopping creature

Catfart Grayson
Registered User
Join date: 16 May 2004
Posts: 264
06-13-2007 23:45
These two scripts create a small physics enabled creature that moves around in a series of small jumps. It attempts to stay on your land, not always successfully. If it detects an avatar on your land, it moves towards it and sits at the avatars feet looking cute, or evil depending on what you put it in.

Drop the two scripts in a small, under 10 mass units, object. It will travel in the direction of the x-axis of the object.

A working, editable version of this code can be found at http://slurl.com/secondlife/olivia/17/95/21/ . Look for the small castle.

The code is open and free for you to use as you want. You may use the code in your own commercial products, but may not sell the creature as it stands.

Permission is given to anyone who wants to copy this code to the Wiki.

Put this code in a script called Brain.
CODE

key current_target;
vector current_target_pos;
float heartbeat = 5;

vector select_new_pos()
{
integer good_pos = FALSE;
while(good_pos == FALSE)
{
float xoffset = llFrand(20) - 10;
float yoffset = llFrand(20) - 10;

if(llGround(<xoffset,yoffset,0>) > llWater(<xoffset,yoffset,0>))
{
if((llGetLandOwnerAt(llGetPos() + <xoffset,yoffset,0>)) == llGetOwner())
{
good_pos = TRUE;
vector current_pos = llGetPos();
float new_x = current_pos.x + xoffset;
float new_y = current_pos.y + yoffset;
return <new_x,new_y,llGround(<xoffset,yoffset,0>)>;
}
}
}
//Shouldnt actually ever get to this line
return llGetPos();

}

default
{
state_entry()
{
llWhisper(0,"pop");
state search_for_avatar;
}
}
state search_for_avatar
{
state_entry()
{

list command=["MOVE",select_new_pos(),1];
llMessageLinked( LINK_SET, 150, llDumpList2String(command, "-=-"), "" );
llSetTimerEvent(30);

}
timer()
{
llSensor("","",AGENT,30,2 * PI);
}
sensor(integer num)
{
integer x = 0;
for(x;x < num;x = x +1)
{
if((llGetLandOwnerAt(llDetectedPos(x))) == llGetOwner())
{
current_target = llDetectedKey(0);
current_target_pos = llDetectedPos(0);
//fund an avatar, move to it
// llWhisper(0,"found target");
state aquired_avatar;
}
list command=["MOVE",select_new_pos(),1];
llMessageLinked( LINK_SET, 150, llDumpList2String(command, "-=-"), "" );
}
}
no_sensor()
{
list command=["MOVE",select_new_pos(),1];
llMessageLinked( LINK_SET, 150, llDumpList2String(command, "-=-"), "" );
}
link_message(integer sender,integer num, string data, key id)
{
if(num == 150)
{
list Arguments = llParseStringKeepNulls( data, ["-=-"], [] );
string Command = llList2String( Arguments, 0 );
if( Command == "ATPOS" )
{
llSensor("","",AGENT,30,2 * PI);
}
}
}
}
state aquired_avatar
{
state_entry()
{
llSensorRepeat("",current_target,AGENT,20,2 * PI, 1);
}
sensor(integer num)
{
current_target_pos = llDetectedPos(0);
if(llGetLandOwnerAt(current_target_pos) == llGetOwner())
{
list command=["MOVE",current_target_pos,2];
llMessageLinked( LINK_SET, 150, llDumpList2String(command, "-=-"), "" );
}
else
{
state search_for_avatar;
}
}
no_sensor()
{
//gone
state search_for_avatar;
}
link_message(integer sender,integer num,string data,key id)
{
if(num == 150)
{
list Arguments = llParseStringKeepNulls( data, ["-=-"], [] );
string Command = llList2String( Arguments, 0 );
if( Command == "ATPOS" )
{
//reached avatar!!
state reached_avatar;
}
}

}
}
state reached_avatar
{
state_entry()
{
// llWhisper(0,"reached target");
llSensorRepeat("",current_target,AGENT,4,2 * PI, 3);
}
sensor(integer blah)
{
}
no_sensor()
{
// llWhisper(0,"lost target");
state search_for_avatar;
}
}


==================
Put the following code in a script called movement

CODE


integer reached_target = TRUE;
vector jump_force = <1,0,2.5>;
vector target_position;
integer target_id;

pop()
{
if(reached_target == FALSE)
{
llRotLookAt(getRotToPointAxisAt(AXIS_FWD , target_position), 10, 0.5);
llApplyImpulse(jump_force * llGetMass(),TRUE);
llPlaySound("pop",1);
// llWhisper(0,"pop");
}
}

vector AXIS_UP = <0,0,1>;
vector AXIS_LEFT = <0,1,0>;
vector AXIS_FWD = <1,0,0>;
// getRotToPointAxisAt()
// Gets the rotation to point the specified axis at the specified position.
// @param axis The axis to point. Easiest to just use an AXIS_* constant.
// @param target The target, in region-local coordinates, to point the axis at.
// @return The rotation necessary to point axis at target.
// Created by Ope Rand, modifyed by Christopher Omega rotation
rotation getRotToPointAxisAt(vector axis, vector target)
{
return llGetRot() * llRotBetween(axis * llGetRot(), target - llGetPos());
}


default
{
state_entry()
{
llWhisper(0,"movement reset");
llSetStatus(STATUS_PHYSICS,TRUE);
llSetStatus(STATUS_ROTATE_X,FALSE);
llSetStatus(STATUS_ROTATE_Y,FALSE);
state move;
}
}
state move
{
state_entry()
{
}
not_at_target()
{
llRotLookAt(getRotToPointAxisAt(AXIS_FWD , target_position), 2, 0.5);
}
link_message(integer sender, integer num,string data, key id)
{
if(num == 150)
{
list Arguments = llParseStringKeepNulls( data, ["-=-"], [] );
string Command = llList2String( Arguments, 0 );
if( Command == "MOVE" )
{
// llWhisper(0,"move");
llTargetRemove(target_id);
target_id = llTarget((vector)llList2String( Arguments, 1 ),2);
target_position = (vector)llList2String( Arguments, 1 );
reached_target = FALSE;
pop();
}
}
}
at_target(integer tnum,vector targetpos,vector ourpos)
{
reached_target =TRUE;
llMessageLinked(LINK_SET,150,"ATPOS","");
}
collision_start(integer num)
{
pop();
}
land_collision_start(vector pos)
{
pop();
}
}
_____________________
Cat
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Discussion Thread
06-17-2007 07:04
/54/cd/191407/1.html
_____________________
i've got nothing. ;)