From 095142eae65820c8daf246def0866ac6accc9c87 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Wed, 9 Sep 2020 10:37:42 +0200 Subject: Cleanup: use bool instead of int --- source/blender/blenkernel/intern/boids.c | 130 ++++++++++++++++--------------- 1 file changed, 66 insertions(+), 64 deletions(-) (limited to 'source/blender/blenkernel/intern/boids.c') diff --git a/source/blender/blenkernel/intern/boids.c b/source/blender/blenkernel/intern/boids.c index a7324ffe738..78ce70a8448 100644 --- a/source/blender/blenkernel/intern/boids.c +++ b/source/blender/blenkernel/intern/boids.c @@ -68,18 +68,18 @@ typedef struct BoidValues { float personal_space, jump_speed; } BoidValues; -static int apply_boid_rule( +static bool apply_boid_rule( BoidBrainData *bbd, BoidRule *rule, BoidValues *val, ParticleData *pa, float fuzziness); -static int rule_none(BoidRule *UNUSED(rule), - BoidBrainData *UNUSED(data), - BoidValues *UNUSED(val), - ParticleData *UNUSED(pa)) +static bool rule_none(BoidRule *UNUSED(rule), + BoidBrainData *UNUSED(data), + BoidValues *UNUSED(val), + ParticleData *UNUSED(pa)) { - return 0; + return false; } -static int rule_goal_avoid(BoidRule *rule, BoidBrainData *bbd, BoidValues *val, ParticleData *pa) +static bool rule_goal_avoid(BoidRule *rule, BoidBrainData *bbd, BoidValues *val, ParticleData *pa) { BoidRuleGoalAvoid *gabr = (BoidRuleGoalAvoid *)rule; BoidSettings *boids = bbd->part->boids; @@ -91,7 +91,7 @@ static int rule_goal_avoid(BoidRule *rule, BoidBrainData *bbd, BoidValues *val, EffectorData efd, cur_efd; float mul = (rule->type == eBoidRuleType_Avoid ? 1.0 : -1.0); float priority = 0.0f, len = 0.0f; - int ret = 0; + bool ret = false; int p = 0; efd.index = cur_efd.index = &p; @@ -207,16 +207,16 @@ static int rule_goal_avoid(BoidRule *rule, BoidBrainData *bbd, BoidValues *val, } } - ret = 1; + ret = true; } return ret; } -static int rule_avoid_collision(BoidRule *rule, - BoidBrainData *bbd, - BoidValues *val, - ParticleData *pa) +static bool rule_avoid_collision(BoidRule *rule, + BoidBrainData *bbd, + BoidValues *val, + ParticleData *pa) { const int raycast_flag = BVH_RAYCAST_DEFAULT & ~BVH_RAYCAST_WATERTIGHT; BoidRuleAvoidCollision *acbr = (BoidRuleAvoidCollision *)rule; @@ -228,7 +228,7 @@ static int rule_avoid_collision(BoidRule *rule, float co1[3], vel1[3], co2[3], vel2[3]; float len, t, inp, t_min = 2.0f; int n, neighbors = 0, nearest = 0; - int ret = 0; + bool ret = 0; // check deflector objects first if (acbr->options & BRULE_ACOLL_WITH_DEFLECTORS && bbd->sim->colliders) { @@ -288,7 +288,7 @@ static int rule_avoid_collision(BoidRule *rule, bbd->wanted_speed = sqrtf(t) * len_v3(pa->prev_state.vel); bbd->wanted_speed = MAX2(bbd->wanted_speed, val->min_speed); - return 1; + return true; } } @@ -413,10 +413,10 @@ static int rule_avoid_collision(BoidRule *rule, return ret; } -static int rule_separate(BoidRule *UNUSED(rule), - BoidBrainData *bbd, - BoidValues *val, - ParticleData *pa) +static bool rule_separate(BoidRule *UNUSED(rule), + BoidBrainData *bbd, + BoidValues *val, + ParticleData *pa) { KDTreeNearest_3d *ptn = NULL; ParticleTarget *pt; @@ -424,7 +424,7 @@ static int rule_separate(BoidRule *UNUSED(rule), float vec[3] = {0.0f, 0.0f, 0.0f}; int neighbors = BLI_kdtree_3d_range_search( bbd->sim->psys->tree, pa->prev_state.co, &ptn, 2.0f * val->personal_space * pa->size); - int ret = 0; + bool ret = false; if (neighbors > 1 && ptn[1].dist != 0.0f) { sub_v3_v3v3(vec, pa->prev_state.co, bbd->sim->psys->particles[ptn[1].index].state.co); @@ -453,7 +453,7 @@ static int rule_separate(BoidRule *UNUSED(rule), add_v3_v3(bbd->wanted_co, vec); bbd->wanted_speed = val->max_speed; len = ptn[0].dist; - ret = 1; + ret = true; } if (ptn) { @@ -464,10 +464,10 @@ static int rule_separate(BoidRule *UNUSED(rule), } return ret; } -static int rule_flock(BoidRule *UNUSED(rule), - BoidBrainData *bbd, - BoidValues *UNUSED(val), - ParticleData *pa) +static bool rule_flock(BoidRule *UNUSED(rule), + BoidBrainData *bbd, + BoidValues *UNUSED(val), + ParticleData *pa) { KDTreeNearest_3d ptn[11]; float vec[3] = {0.0f, 0.0f, 0.0f}, loc[3] = {0.0f, 0.0f, 0.0f}; @@ -479,7 +479,7 @@ static int rule_flock(BoidRule *UNUSED(rule), len_squared_v3v3_with_normal_bias, pa->prev_state.ave); int n; - int ret = 0; + bool ret = false; if (neighbors > 1) { for (n = 1; n < neighbors; n++) { @@ -497,20 +497,21 @@ static int rule_flock(BoidRule *UNUSED(rule), add_v3_v3(bbd->wanted_co, loc); bbd->wanted_speed = len_v3(bbd->wanted_co); - ret = 1; + ret = true; } return ret; } -static int rule_follow_leader(BoidRule *rule, - BoidBrainData *bbd, - BoidValues *val, - ParticleData *pa) +static bool rule_follow_leader(BoidRule *rule, + BoidBrainData *bbd, + BoidValues *val, + ParticleData *pa) { BoidRuleFollowLeader *flbr = (BoidRuleFollowLeader *)rule; float vec[3] = {0.0f, 0.0f, 0.0f}, loc[3] = {0.0f, 0.0f, 0.0f}; float mul, len; int n = (flbr->queue_size <= 1) ? bbd->sim->psys->totpart : flbr->queue_size; - int i, ret = 0, p = pa - bbd->sim->psys->particles; + int i, p = pa - bbd->sim->psys->particles; + bool ret = false; if (flbr->ob) { float vec2[3], t; @@ -530,7 +531,7 @@ static int rule_follow_leader(BoidRule *rule, if (len < 2.0f * val->personal_space * pa->size) { copy_v3_v3(bbd->wanted_co, loc); bbd->wanted_speed = val->max_speed; - return 1; + return true; } } else { @@ -548,7 +549,7 @@ static int rule_follow_leader(BoidRule *rule, if (len < 2.0f * val->personal_space * pa->size) { copy_v3_v3(bbd->wanted_co, vec2); bbd->wanted_speed = val->max_speed * (3.0f - t) / 3.0f; - return 1; + return true; } } } @@ -570,7 +571,7 @@ static int rule_follow_leader(BoidRule *rule, sub_v3_v3v3(bbd->wanted_co, loc, pa->prev_state.co); bbd->wanted_speed = len_v3(bbd->wanted_co); - ret = 1; + ret = true; } else if (p % n) { float vec2[3], t, t_min = 3.0f; @@ -590,7 +591,7 @@ static int rule_follow_leader(BoidRule *rule, if (len < 2.0f * val->personal_space * pa->size) { copy_v3_v3(bbd->wanted_co, loc); bbd->wanted_speed = val->max_speed; - return 1; + return true; } } else { @@ -609,14 +610,14 @@ static int rule_follow_leader(BoidRule *rule, t_min = t; copy_v3_v3(bbd->wanted_co, loc); bbd->wanted_speed = val->max_speed * (3.0f - t) / 3.0f; - ret = 1; + ret = true; } } } } if (ret) { - return 1; + return true; } /* not blocking so try to follow leader */ @@ -635,15 +636,15 @@ static int rule_follow_leader(BoidRule *rule, sub_v3_v3v3(bbd->wanted_co, loc, pa->prev_state.co); bbd->wanted_speed = len_v3(bbd->wanted_co); - ret = 1; + ret = true; } return ret; } -static int rule_average_speed(BoidRule *rule, - BoidBrainData *bbd, - BoidValues *val, - ParticleData *pa) +static bool rule_average_speed(BoidRule *rule, + BoidBrainData *bbd, + BoidValues *val, + ParticleData *pa) { BoidParticle *bpa = pa->boid; BoidRuleAverageSpeed *asbr = (BoidRuleAverageSpeed *)rule; @@ -693,9 +694,9 @@ static int rule_average_speed(BoidRule *rule, } bbd->wanted_speed = asbr->speed * val->max_speed; - return 1; + return true; } -static int rule_fight(BoidRule *rule, BoidBrainData *bbd, BoidValues *val, ParticleData *pa) +static bool rule_fight(BoidRule *rule, BoidBrainData *bbd, BoidValues *val, ParticleData *pa) { BoidRuleFight *fbr = (BoidRuleFight *)rule; KDTreeNearest_3d *ptn = NULL; @@ -708,7 +709,8 @@ static int rule_fight(BoidRule *rule, BoidBrainData *bbd, BoidValues *val, Parti float closest_dist = fbr->distance + 1.0f; float f_strength = 0.0f, e_strength = 0.0f; float health = 0.0f; - int n, ret = 0; + int n; + bool ret = false; /* calculate own group strength */ int neighbors = BLI_kdtree_3d_range_search( @@ -799,16 +801,16 @@ static int rule_fight(BoidRule *rule, BoidBrainData *bbd, BoidValues *val, Parti } } - ret = 1; + ret = true; } return ret; } -typedef int (*boid_rule_cb)(BoidRule *rule, - BoidBrainData *data, - BoidValues *val, - ParticleData *pa); +typedef bool (*boid_rule_cb)(BoidRule *rule, + BoidBrainData *data, + BoidValues *val, + ParticleData *pa); static boid_rule_cb boid_rules[] = { rule_none, @@ -957,24 +959,24 @@ static Object *boid_find_ground(BoidBrainData *bbd, ground_nor[2] = 1.0f; return NULL; } -static int boid_rule_applies(ParticleData *pa, BoidSettings *UNUSED(boids), BoidRule *rule) +static bool boid_rule_applies(ParticleData *pa, BoidSettings *UNUSED(boids), BoidRule *rule) { BoidParticle *bpa = pa->boid; if (rule == NULL) { - return 0; + return false; } if (ELEM(bpa->data.mode, eBoidMode_OnLand, eBoidMode_Climbing) && rule->flag & BOIDRULE_ON_LAND) { - return 1; + return true; } if (bpa->data.mode == eBoidMode_InAir && rule->flag & BOIDRULE_IN_AIR) { - return 1; + return true; } - return 0; + return false; } void boids_precalc_rules(ParticleSettings *part, float cfra) { @@ -1025,27 +1027,27 @@ static float boid_goal_signed_dist(float *boid_co, float *goal_co, float *goal_n return dot_v3v3(vec, goal_nor); } /* wanted_co is relative to boid location */ -static int apply_boid_rule( +static bool apply_boid_rule( BoidBrainData *bbd, BoidRule *rule, BoidValues *val, ParticleData *pa, float fuzziness) { if (rule == NULL) { - return 0; + return false; } - if (boid_rule_applies(pa, bbd->part->boids, rule) == 0) { - return 0; + if (!boid_rule_applies(pa, bbd->part->boids, rule)) { + return false; } - if (boid_rules[rule->type](rule, bbd, val, pa) == 0) { - return 0; + if (!boid_rules[rule->type](rule, bbd, val, pa)) { + return false; } if (fuzziness < 0.0f || compare_len_v3v3(bbd->wanted_co, pa->prev_state.vel, fuzziness * len_v3(pa->prev_state.vel)) == 0) { - return 1; + return true; } - return 0; + return false; } static BoidState *get_boid_state(BoidSettings *boids, ParticleData *pa) { -- cgit v1.2.3