herdem_sheep.c
/**
 * Gets called for each sheep each frame. Repels sheep
 * from dogs and keeps track of when sheep last hit a wall
 */
Gets called for each sheep each frame. Repels sheep
from dogs and keeps track of when sheep last hit a wall
Gets called for each sheep each frame . Repels sheep from dogs and keeps track of when sheep last hit a wall
void herdem_sheep_iterator(jty_actor *actor)
{
    herdem_sheep *sheep = (herdem_sheep *)actor;
    jty_actor_ls *dog_ls;
    herdem_dog *dog;
    jty_vector r_rel;
    double r_rel_mag_sq; 
    sheep->parent.ax = 0;
    sheep->parent.ay = 0;
    sheep->panicked = false;
    if (jty_actor_has_left_map(actor)) {
        herdem_engine->saved_sheeps += 1;
        herdem_saved_tally_update(herdem_engine->info_board->saved_tally);
        fprintf(stderr, "A sheep has been saved!!\n");
        free_herdem_sheep(sheep);
        return;
    }
    if (random() % 800 == 0) {
        sheep->parent.vx = (float)random() / (float)RAND_MAX;
        sheep->parent.vy = sqrt(1 - pow(sheep->parent.vx, 2));
    }
    for (dog_ls = herdem_engine->dogs; dog_ls != NULL; dog_ls = dog_ls->next) {
        dog = (herdem_dog *)(dog_ls->actor);
        r_rel.x = sheep->parent.x - dog->parent.x;
        r_rel.y = sheep->parent.y - dog->parent.y;
        r_rel_mag_sq = jty_vector_mag_sq(r_rel); 
        if (r_rel_mag_sq < pow(dog->scare_radius_max, 2)) {
            double r_rel_mag = sqrt(r_rel_mag_sq);
            double m = (dog->normal_speed - sheep->normal_speed) / (dog->scare_radius_min - dog->scare_radius_max);
            double c = dog->normal_speed - m * dog->scare_radius_min;
            if (!sheep->touching_wall) {
                sheep->parent.vx = 1 / r_rel_mag * r_rel.x * (m * r_rel_mag + c);
                sheep->parent.vy = 1 / r_rel_mag * r_rel.y * (m * r_rel_mag + c);
            }
            sheep->panicked = true;
        }
    }
    jty_vector v = {.x = sheep->parent.vx, .y = sheep->parent.vy};
    double sheep_speed = sqrt(jty_vector_mag_sq(v));
    if (sheep_speed == 0) {
        sheep->parent.vx = (float)random() / (float)RAND_MAX;
        sheep->parent.vy = sqrt(1 - pow(sheep->parent.vx, 2));
        eight_way_direction_change((jty_actor *)sheep);
        return;
    }
    sheep->parent.ax += -1 / sheep_speed * v.x * (sheep_speed - sheep->normal_speed) * 1;
    sheep->parent.ay += -1 / sheep_speed * v.y * (sheep_speed - sheep->normal_speed) * 1;
    if (sheep->touching_wall == 0){
        eight_way_direction_change((jty_actor *)sheep);
    }
    if (sheep->touching_wall > 0) {
        sheep->touching_wall -= 1;
    }
}
void herdem_sheep_iterator ( jty_actor * actor ) { herdem_sheep * sheep = ( herdem_sheep * ) actor ; jty_actor_ls * dog_ls ; herdem_dog * dog ; jty_vector r_rel ; double r_rel_mag_sq ; sheep -> parent . ax = 0 ; sheep -> parent . ay = 0 ; sheep -> panicked = false ; if ( jty_actor_has_left_map ( actor ) ) { herdem_engine -> saved_sheeps += 1 ; herdem_saved_tally_update ( herdem_engine -> info_board -> saved_tally ) ; fprintf ( stderr , " \n " ) ; free_herdem_sheep ( sheep ) ; return ; } if ( random ( ) % 800 == 0 ) { sheep -> parent . vx = ( float ) random ( ) / ( float ) RAND_MAX ; sheep -> parent . vy = sqrt ( 1 - pow ( sheep -> parent . vx , 2 ) ) ; } for ( dog_ls = herdem_engine -> dogs ; dog_ls != NULL ; dog_ls = dog_ls -> next ) { dog = ( herdem_dog * ) ( dog_ls -> actor ) ; r_rel . x = sheep -> parent . x - dog -> parent . x ; r_rel . y = sheep -> parent . y - dog -> parent . y ; r_rel_mag_sq = jty_vector_mag_sq ( r_rel ) ; if ( r_rel_mag_sq < pow ( dog -> scare_radius_max , 2 ) ) { double r_rel_mag = sqrt ( r_rel_mag_sq ) ; double m = ( dog -> normal_speed - sheep -> normal_speed ) / ( dog -> scare_radius_min - dog -> scare_radius_max ) ; double c = dog -> normal_speed - m * dog -> scare_radius_min ; if ( ! sheep -> touching_wall ) { sheep -> parent . vx = 1 / r_rel_mag * r_rel . x * ( m * r_rel_mag + c ) ; sheep -> parent . vy = 1 / r_rel_mag * r_rel . y * ( m * r_rel_mag + c ) ; } sheep -> panicked = true ; } } jty_vector v = { . x = sheep -> parent . vx , . y = sheep -> parent . vy } ; double sheep_speed = sqrt ( jty_vector_mag_sq ( v ) ) ; if ( sheep_speed == 0 ) { sheep -> parent . vx = ( float ) random ( ) / ( float ) RAND_MAX ; sheep -> parent . vy = sqrt ( 1 - pow ( sheep -> parent . vx , 2 ) ) ; eight_way_direction_change ( ( jty_actor * ) sheep ) ; return ; } sheep -> parent . ax += -1 / sheep_speed * v . x * ( sheep_speed - sheep -> normal_speed ) * 1 ; sheep -> parent . ay += -1 / sheep_speed * v . y * ( sheep_speed - sheep -> normal_speed ) * 1 ; if ( sheep -> touching_wall == 0 ) { eight_way_direction_change ( ( jty_actor * ) sheep ) ; } if ( sheep -> touching_wall > 0 ) { sheep -> touching_wall -= 1 ; } }
Gets called for each sheep each frame.
Gets called for each sheep each frame .
/* Magnitude squared of r_rel */