LensC Samples





Bounce

This is one of the first LensC programs written. Called from an object action that triggers on the verb "bounce", it allows the item named to be bounced to someone else. An example of calling this function can be found by stating oaction 14064.


    program bounce(char)
  
    main()
    {
	/* Declare some variables. */
	obj ball;
	char ch = self;

	/* Argument should look like "bounce ball name" */
	/* First word is bounce, get what comes after that. */
	argument = first_word(argument,WORD_REST); 

	/* First word is object name */
	string arg = first_word(argument,WORD_FIRST);

	/* Rest is name of person to bounce to */
	argument = first_word(argument,WORD_REST);
 
	/* Make sure there is a word here */
        if (string_is_empty(arg)) 
        { 
	    sendt(ch,"Bounce what?\r\n"); 
	    end; 
	}
	/* and a name of who to bounce to */
        if (string_is_empty(argument)) 
	{ 
	    sendt(ch,"Bounce it to who?\r\n");
	    end; 
	}

	/* Get the object reference from the char's inventory, and make
	   sure it is valid */
	if (is_valid_obj(ball = get_obj_carry(ch,arg)))
	{
	    /* Find the target in the same room as the person doing the
	       bouncing, and make sure they're valid. */
	    char target = get_char_room(ch,argument);
	    if (is_valid_char(target))
	    {
		/* Send out some messages */
		act("$n bounces $p to you.",ch,ball,target,ACT_TO_VICT);
		act("You bounce $p to $N.",ch,ball,target,ACT_TO_CHAR);
		act("$n bounces $p to $N.",ch,ball,target,ACT_TO_NOTVICT);
		/* Move the ball from the caller's inventory to the 
		   target's */
		move_obj_to_char(ball,target);
	    }
	    else
		/* if can't find the person, make a message */
		sendt(ch,"They're not here.\r\n");
	}
	else
	    /* if can't find the ball, make a message */
	    sendt(ch,"You don't have that.\r\n");
    }



Seeker

Ok, this little program is executed by a mob (14024 in this case), and it sets the mob running around looking for a named item (cheese in the mouse's case).


program cheesefinder (char)

/* This is a sort of stack of vnums the mob has visited */
int travfrom[50];
int lp=0;

/* Don't want the mob running outside of its zone */
area homezone = room_getarea(ch_in_room(self));

function find_item()
{
    obj target = room_first_content(ch_in_room(self));

    /* Look through all the items in the room. Start with the first... */

    while (is_valid_obj(target))
    {
	/* As long as the item is valid, see if it is the item we're
	   looking for */ 

        if (is_name(argument,obj_getstring(target,OBJ_STRING_NAME)))
        {
	/*
	   Have to invalidate the handle, so that when the item is eaten
	   the fact that it is no longer valid doesn't cause program
	   termination 
	*/
            invalidate target;

	/* Execute get command */
            if (interpret(self,"Get " + argument)) 
            { 
	/* If we got it, execute eat command */
                interpret(self,"Eat " + argument);
	/* and, 50% chance to stop after eating */
                if (random(0,1))
                    end;
            }
            return;
        }
	/* Check the next item in the room */
        target = obj_next_content(target);
    }
}

function int move_around()
{
    room loc = ch_in_room(self);		/* the room we're in */
    int  vnum = room_getval(loc,ROOM_VAL_VNUM); /* Get it's vnum */
    exit ex;
    int dir;
    int choices[6];
    int count = 0;

    /* check all 6 directions */
    for (dir = DIR_N;dir<=DIR_D;dir++)
	/* See if there is a valid exit in that direction */
        if (is_valid_exit((ex = exit_exist(loc,dir))) && 
	/* and that it is not closed */
           !exit_closed(ex) &&
	/* and that it leads to the zone we're staying in */
           homezone == room_getarea(exit_to(ex)))           
	/* If all that is ok, add this direction to the ones we can
	   choose between */ 
            choices[count++]=dir;            

    /* If there is no where to go, stop running */
    if (count==0)
        end;

    /* If we've gone 50 spaces without ever going back where we came from
	reset the stack and start over. This isn't real correct for a 
	path-seeking algorithm, but works for demonstartion
    */
    if (lp >= 50)
        lp = 0;

    /* Randomly choose one of the exits we found */
    dir = choices[random(0,count-1)];

    /* If we have more than one choice, and picked to go back where
	we just came from, make a second choice */
    if (count != 1 && 
      	room_getval(exit_to(exit_exist(loc,dir)),ROOM_VAL_VNUM) == travfrom[lp])
        dir = choices[random(0,count-1)];

    /* Try to move the character */
    if (move_char(self,dir))
    {
	/* if we went back where we came from, remove that from the stack */
        if (travfrom[lp] == room_getval(ch_in_room(self),ROOM_VAL_VNUM))
            lp--;
        else
	/* Otherwise add the vnum of where we just were before moving to
	   the stack. */ 
	    travfrom[lp++] = vnum;
    }
    /* If failed to move the character (for some reason), and it was
	  the only choice, stop the program */
    else if (count == 1)
        end;

    /* return the direction we went in */
    return dir;
}

main()
{
    int i;
    /* Skip the "find" keyword */
    argument = first_word(argument,WORD_REST);

    /* As long as this character hasn't died, keep doing this */
    while (is_valid_char(self))
    {
	/* find the item */
        find_item();
	/* move */
        move_around();
	/* Delay some, to reduce spam */
        for (i=0;i < random(1,3);i++)
            wait();
    }
}