Beispiel #1
0
void init_walking(void)
{
	CnetPosition	start;

	CHECK(CNET_set_handler(EV_WALKING, walker, 0));

	choose_position(&start, 0);
	CHECK(CNET_set_position(start));
}
void update_wolf(struct world **read_matrix, struct world **write_matrix, int row, int col) {
    int possibilities[N_ADJACENTS];
    int may_move[N_ADJACENTS];
    int n_possibilities, n_squirrels, n_other;
    int chosen;
    struct world *wolf = &write_matrix[row][col];
    n_possibilities = get_adjacents(row, col, possibilities);

    if(!wolf->starvation_period) {
        kill_wolf(wolf);
        return;
    }
    /*If has adjacents to choose*/
    if(n_possibilities) {
        /*Check for squirrels*/
        n_squirrels = get_cells_with_squirrels(read_matrix, possibilities, n_possibilities, may_move);
        if(n_squirrels) {
            /*At least one squirrel has been found
            	Choose one of them*/
            chosen = choose_position(row, col, n_squirrels);
            /* Eat the squirrel - Now inside move_to
            get_world_coordinates(may_move[chosen], &s_row, &s_col);
            eat_squirrel(wolf, &write_matrix[s_row][s_col]);*/

            /*Move to that position*/
            move_to(row, col, may_move[chosen], read_matrix, write_matrix);
        }

        else {
            /*No squirrels
            	Let's another cell*/
            n_other = get_walkable_cells(read_matrix, possibilities, n_possibilities, may_move, ICE | TREE);
            if(n_other) {
                chosen = choose_position(row, col, n_other);
                /*Move to that position*/
                move_to(row, col, may_move[chosen], read_matrix, write_matrix);
            }
        }
    }
}
/*
 * Update rules for animals in the world
 */
void update_squirrel(struct world **read_matrix, struct world **write_matrix, int row, int col) {
    int possibilities[N_ADJACENTS];
    int may_move[N_ADJACENTS];
    int n_possibilities, n_moves;
    int chosen;

    n_possibilities = get_adjacents(row, col, possibilities);
    n_moves = get_walkable_cells(read_matrix, possibilities, n_possibilities, may_move, ICE | WOLF);
    if(n_moves) {
        chosen = choose_position(row, col, n_moves);
        move_to(row, col, may_move[chosen], read_matrix, write_matrix);
    }
}
Beispiel #4
0
static EVENT_HANDLER(walker)
{
	static	double		dx	= 0.0;
	static	double		dy	= 0.0;
	static	double		newx	= 0.0;
	static	double		newy	= 0.0;
	static	int		nsteps	= 0;

	CnetPosition	now;
	CnetTime		movenext;

	//  IF PAUSED, WE NEED TO CHOOSE A NEW DESTINATION AND WALKING SPEED
	if(paused) {
		CnetPosition	newdest;

		CHECK(CNET_get_position(&now, NULL));

		//  CHOOSE A NEW DESTINATION THAT DOESN'T REQUIRE WALKING THROUGH A WALL!
		do {
			int			newspeed;
			double		dist;

			newdest	= now;
			do {
				choose_position(&newdest, MAX_WALK_DIST);
			} while(through_an_object(now, newdest));

			dx		= newdest.x - now.x;
			dy		= newdest.y - now.y;
			dist	= sqrt(dx*dx + dy*dy);	// only walking in 2D

			newspeed	= CNET_rand() % MAX_SPEED + 1;
			nsteps	= dist / newspeed;
		} while(nsteps < 3);		// ensure we'll take at least 3 steps
		//draw_walk(&now, &newdest);

		//  CALCULATE MOVEMENTS PER STEP
		dx	= (dx / nsteps);
		dy	= (dy / nsteps);
		newx	= now.x;
		newy	= now.y;
		paused	= false;		// and off we go....
	}

	//  WE'RE WALKING;  DO WE STILL HAVE SOME STEPS TO TAKE?
	if(nsteps > 0) {
		newx	+= dx;
		newy	+= dy;

		now.x	 = newx;
		now.y	 = newy;
		now.z	 = 0;
		CHECK(CNET_set_position(now));
		paused	= false;
		--nsteps;
		movenext = WALK_FREQUENCY;
	}
	//  WE'VE FINISHED WALKING, SO WE PAUSE HERE FOR A WHILE
	else {
		paused	= true;
		nsteps	= 0;
		movenext = (CNET_rand() % (MAX_PAUSE-MIN_PAUSE) + MIN_PAUSE) * 1000000;
	}

	//  RESCHEDULE THIS WALKING EVENT
	tid	= CNET_start_timer(EV_WALKING, movenext, data);
}