use BLI_strncpy and BLI_snprintf when the size of the string is known.
[blender-staging.git] / source / blender / blenkernel / intern / boids.c
index 69a42e52247851d60c3145ea89473c939c520945..d454bef3184268cbc1993e10628b824697061262 100644 (file)
@@ -1,8 +1,4 @@
-/* boids.c
- *
- *
- * $Id$
- *
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file blender/blenkernel/intern/boids.c
+ *  \ingroup bke
+ */
+
+
 #include <string.h>
 #include <math.h>
 
 #include "BLI_math.h"
 #include "BLI_blenlib.h"
 #include "BLI_kdtree.h"
+#include "BLI_utildefines.h"
+
 #include "BKE_collision.h"
 #include "BKE_effect.h"
 #include "BKE_boids.h"
 #include "BKE_particle.h"
-#include "BKE_utildefines.h"
+
 #include "BKE_modifier.h"
 
 #include "RNA_enum_types.h"
@@ -58,7 +61,7 @@ typedef struct BoidValues {
 
 static int apply_boid_rule(BoidBrainData *bbd, BoidRule *rule, BoidValues *val, ParticleData *pa, float fuzziness);
 
-static int rule_none(BoidRule *rule, BoidBrainData *data, BoidValues *val, ParticleData *pa)
+static int rule_none(BoidRule *UNUSED(rule), BoidBrainData *UNUSED(data), BoidValues *UNUSED(val), ParticleData *UNUSED(pa))
 {
        return 0;
 }
@@ -149,8 +152,8 @@ static int rule_goal_avoid(BoidRule *rule, BoidBrainData *bbd, BoidValues *val,
                if(rule->type == eBoidRuleType_Goal && boids->options & BOID_ALLOW_CLIMB && surface!=0.0f) {
                        if(!bbd->goal_ob || bbd->goal_priority < priority) {
                                bbd->goal_ob = eob;
-                               VECCOPY(bbd->goal_co, efd.loc);
-                               VECCOPY(bbd->goal_nor, efd.nor);
+                               copy_v3_v3(bbd->goal_co, efd.loc);
+                               copy_v3_v3(bbd->goal_nor, efd.nor);
                        }
                }
                else if(rule->type == eBoidRuleType_Avoid && bpa->data.mode == eBoidMode_Climbing &&
@@ -159,7 +162,7 @@ static int rule_goal_avoid(BoidRule *rule, BoidBrainData *bbd, BoidValues *val,
                        negate_v3_v3(efd.vec_to_point, bpa->gravity);
                }
 
-               VECCOPY(bbd->wanted_co, efd.vec_to_point);
+               copy_v3_v3(bbd->wanted_co, efd.vec_to_point);
                mul_v3_fl(bbd->wanted_co, mul);
 
                bbd->wanted_speed = val->max_speed * priority;
@@ -172,7 +175,7 @@ static int rule_goal_avoid(BoidRule *rule, BoidBrainData *bbd, BoidValues *val,
 
                        if(len2 > 0.0f && efd.distance - surface < len2) {
                                len2 = (efd.distance - surface)/len2;
-                               bbd->wanted_speed *= pow(len2, boids->landing_smoothness);
+                               bbd->wanted_speed *= powf(len2, boids->landing_smoothness);
                        }
                }
 
@@ -201,13 +204,13 @@ static int rule_avoid_collision(BoidRule *rule, BoidBrainData *bbd, BoidValues *
                BVHTreeRayHit hit;
                float radius = val->personal_space * pa->size, ray_dir[3];
 
-               VECCOPY(col.co1, pa->prev_state.co);
+               copy_v3_v3(col.co1, pa->prev_state.co);
                add_v3_v3v3(col.co2, pa->prev_state.co, pa->prev_state.vel);
                sub_v3_v3v3(ray_dir, col.co2, col.co1);
                mul_v3_fl(ray_dir, acbr->look_ahead);
-               col.t = 0.0f;
+               col.f = 0.0f;
                hit.index = -1;
-               hit.dist = col.ray_len = len_v3(ray_dir);
+               hit.dist = col.original_ray_length = len_v3(ray_dir);
 
                /* find out closest deflector object */
                for(coll = bbd->sim->colliders->first; coll; coll=coll->next) {
@@ -215,18 +218,18 @@ static int rule_avoid_collision(BoidRule *rule, BoidBrainData *bbd, BoidValues *
                        if(coll->ob == bpa->ground)
                                continue;
 
-                       col.ob = coll->ob;
+                       col.current = coll->ob;
                        col.md = coll->collmd;
 
                        if(col.md && col.md->bvhtree)
-                               BLI_bvhtree_ray_cast(col.md->bvhtree, col.co1, ray_dir, radius, &hit, particle_intersect_face, &col);
+                               BLI_bvhtree_ray_cast(col.md->bvhtree, col.co1, ray_dir, radius, &hit, BKE_psys_collision_neartest_cb, &col);
                }
                /* then avoid that object */
                if(hit.index>=0) {
-                       t = hit.dist/col.ray_len;
+                       t = hit.dist/col.original_ray_length;
 
                        /* avoid head-on collision */
-                       if(dot_v3v3(col.nor, pa->prev_state.ave) < -0.99) {
+                       if(dot_v3v3(col.pce.nor, pa->prev_state.ave) < -0.99f) {
                                /* don't know why, but uneven range [0.0,1.0] */
                                /* works much better than even [-1.0,1.0] */
                                bbd->wanted_co[0] = BLI_frand();
@@ -234,12 +237,12 @@ static int rule_avoid_collision(BoidRule *rule, BoidBrainData *bbd, BoidValues *
                                bbd->wanted_co[2] = BLI_frand();
                        }
                        else {
-                               VECCOPY(bbd->wanted_co, col.nor);
+                               copy_v3_v3(bbd->wanted_co, col.pce.nor);
                        }
 
                        mul_v3_fl(bbd->wanted_co, (1.0f - t) * val->personal_space * pa->size);
 
-                       bbd->wanted_speed = sqrt(t) * len_v3(pa->prev_state.vel);
+                       bbd->wanted_speed = sqrtf(t) * len_v3(pa->prev_state.vel);
                        bbd->wanted_speed = MAX2(bbd->wanted_speed, val->min_speed);
 
                        return 1;
@@ -251,10 +254,10 @@ static int rule_avoid_collision(BoidRule *rule, BoidBrainData *bbd, BoidValues *
        {
                neighbors = BLI_kdtree_range_search(bbd->sim->psys->tree, acbr->look_ahead * len_v3(pa->prev_state.vel), pa->prev_state.co, pa->prev_state.ave, &ptn);
                if(neighbors > 1) for(n=1; n<neighbors; n++) {
-                       VECCOPY(co1, pa->prev_state.co);
-                       VECCOPY(vel1, pa->prev_state.vel);
-                       VECCOPY(co2, (bbd->sim->psys->particles + ptn[n].index)->prev_state.co);
-                       VECCOPY(vel2, (bbd->sim->psys->particles + ptn[n].index)->prev_state.vel);
+                       copy_v3_v3(co1, pa->prev_state.co);
+                       copy_v3_v3(vel1, pa->prev_state.vel);
+                       copy_v3_v3(co2, (bbd->sim->psys->particles + ptn[n].index)->prev_state.co);
+                       copy_v3_v3(vel2, (bbd->sim->psys->particles + ptn[n].index)->prev_state.vel);
 
                        sub_v3_v3v3(loc, co1, co2);
 
@@ -267,8 +270,8 @@ static int rule_avoid_collision(BoidRule *rule, BoidBrainData *bbd, BoidValues *
                                t = -dot_v3v3(loc, vec)/inp;
                                /* cpa is not too far in the future so investigate further */
                                if(t > 0.0f && t < t_min) {
-                                       VECADDFAC(co1, co1, vel1, t);
-                                       VECADDFAC(co2, co2, vel2, t);
+                                       madd_v3_v3fl(co1, vel1, t);
+                                       madd_v3_v3fl(co2, vel2, t);
                                        
                                        sub_v3_v3v3(vec, co2, co1);
 
@@ -297,10 +300,10 @@ static int rule_avoid_collision(BoidRule *rule, BoidBrainData *bbd, BoidValues *
                if(epsys) {
                        neighbors = BLI_kdtree_range_search(epsys->tree, acbr->look_ahead * len_v3(pa->prev_state.vel), pa->prev_state.co, pa->prev_state.ave, &ptn);
                        if(neighbors > 0) for(n=0; n<neighbors; n++) {
-                               VECCOPY(co1, pa->prev_state.co);
-                               VECCOPY(vel1, pa->prev_state.vel);
-                               VECCOPY(co2, (epsys->particles + ptn[n].index)->prev_state.co);
-                               VECCOPY(vel2, (epsys->particles + ptn[n].index)->prev_state.vel);
+                               copy_v3_v3(co1, pa->prev_state.co);
+                               copy_v3_v3(vel1, pa->prev_state.vel);
+                               copy_v3_v3(co2, (epsys->particles + ptn[n].index)->prev_state.co);
+                               copy_v3_v3(vel2, (epsys->particles + ptn[n].index)->prev_state.vel);
 
                                sub_v3_v3v3(loc, co1, co2);
 
@@ -313,8 +316,8 @@ static int rule_avoid_collision(BoidRule *rule, BoidBrainData *bbd, BoidValues *
                                        t = -dot_v3v3(loc, vec)/inp;
                                        /* cpa is not too far in the future so investigate further */
                                        if(t > 0.0f && t < t_min) {
-                                               VECADDFAC(co1, co1, vel1, t);
-                                               VECADDFAC(co2, co2, vel2, t);
+                                               madd_v3_v3fl(co1, vel1, t);
+                                               madd_v3_v3fl(co2, vel2, t);
                                                
                                                sub_v3_v3v3(vec, co2, co1);
 
@@ -344,7 +347,7 @@ static int rule_avoid_collision(BoidRule *rule, BoidBrainData *bbd, BoidValues *
 
        return ret;
 }
-static int rule_separate(BoidRule *rule, BoidBrainData *bbd, BoidValues *val, ParticleData *pa)
+static int rule_separate(BoidRule *UNUSED(rule), BoidBrainData *bbd, BoidValues *val, ParticleData *pa)
 {
        KDTreeNearest *ptn = NULL;
        ParticleTarget *pt;
@@ -384,7 +387,7 @@ static int rule_separate(BoidRule *rule, BoidBrainData *bbd, BoidValues *val, Pa
        }
        return ret;
 }
-static int rule_flock(BoidRule *rule, BoidBrainData *bbd, BoidValues *val, ParticleData *pa)
+static int rule_flock(BoidRule *UNUSED(rule), BoidBrainData *bbd, BoidValues *UNUSED(val), ParticleData *pa)
 {
        KDTreeNearest ptn[11];
        float vec[3] = {0.0f, 0.0f, 0.0f}, loc[3] = {0.0f, 0.0f, 0.0f};
@@ -432,11 +435,11 @@ static int rule_follow_leader(BoidRule *rule, BoidBrainData *bbd, BoidValues *va
                mul = dot_v3v3(vec, vec);
 
                /* leader is not moving */
-               if(mul < 0.01) {
+               if(mul < 0.01f) {
                        len = len_v3(loc);
                        /* too close to leader */
                        if(len < 2.0f * val->personal_space * pa->size) {
-                               VECCOPY(bbd->wanted_co, loc);
+                               copy_v3_v3(bbd->wanted_co, loc);
                                bbd->wanted_speed = val->max_speed;
                                return 1;
                        }
@@ -446,7 +449,7 @@ static int rule_follow_leader(BoidRule *rule, BoidBrainData *bbd, BoidValues *va
 
                        /* possible blocking of leader in near future */
                        if(t > 0.0f && t < 3.0f) {
-                               VECCOPY(vec2, vec);
+                               copy_v3_v3(vec2, vec);
                                mul_v3_fl(vec2, t);
 
                                sub_v3_v3v3(vec2, loc, vec2);
@@ -454,7 +457,7 @@ static int rule_follow_leader(BoidRule *rule, BoidBrainData *bbd, BoidValues *va
                                len = len_v3(vec2);
 
                                if(len < 2.0f * val->personal_space * pa->size) {
-                                       VECCOPY(bbd->wanted_co, vec2);
+                                       copy_v3_v3(bbd->wanted_co, vec2);
                                        bbd->wanted_speed = val->max_speed * (3.0f - t)/3.0f;
                                        return 1;
                                }
@@ -463,17 +466,17 @@ static int rule_follow_leader(BoidRule *rule, BoidBrainData *bbd, BoidValues *va
 
                /* not blocking so try to follow leader */
                if(p && flbr->options & BRULE_LEADER_IN_LINE) {
-                       VECCOPY(vec, bbd->sim->psys->particles[p-1].prev_state.vel);
-                       VECCOPY(loc, bbd->sim->psys->particles[p-1].prev_state.co);
+                       copy_v3_v3(vec, bbd->sim->psys->particles[p-1].prev_state.vel);
+                       copy_v3_v3(loc, bbd->sim->psys->particles[p-1].prev_state.co);
                }
                else {
-                       VECCOPY(loc, flbr->oloc);
+                       copy_v3_v3(loc, flbr->oloc);
                        sub_v3_v3v3(vec, flbr->loc, flbr->oloc);
-                       mul_v3_fl(vec, 1.0/bbd->timestep);
+                       mul_v3_fl(vec, 1.0f/bbd->timestep);
                }
                
                /* fac is seconds behind leader */
-               VECADDFAC(loc, loc, vec, -flbr->distance);
+               madd_v3_v3fl(loc, vec, -flbr->distance);
 
                sub_v3_v3v3(bbd->wanted_co, loc, pa->prev_state.co);
                bbd->wanted_speed = len_v3(bbd->wanted_co);
@@ -485,18 +488,18 @@ static int rule_follow_leader(BoidRule *rule, BoidBrainData *bbd, BoidValues *va
 
                /* first check we're not blocking any leaders */
                for(i = 0; i< bbd->sim->psys->totpart; i+=n){
-                       VECCOPY(vec, bbd->sim->psys->particles[i].prev_state.vel);
+                       copy_v3_v3(vec, bbd->sim->psys->particles[i].prev_state.vel);
 
                        sub_v3_v3v3(loc, pa->prev_state.co, bbd->sim->psys->particles[i].prev_state.co);
 
                        mul = dot_v3v3(vec, vec);
 
                        /* leader is not moving */
-                       if(mul < 0.01) {
+                       if(mul < 0.01f) {
                                len = len_v3(loc);
                                /* too close to leader */
                                if(len < 2.0f * val->personal_space * pa->size) {
-                                       VECCOPY(bbd->wanted_co, loc);
+                                       copy_v3_v3(bbd->wanted_co, loc);
                                        bbd->wanted_speed = val->max_speed;
                                        return 1;
                                }
@@ -506,7 +509,7 @@ static int rule_follow_leader(BoidRule *rule, BoidBrainData *bbd, BoidValues *va
 
                                /* possible blocking of leader in near future */
                                if(t > 0.0f && t < t_min) {
-                                       VECCOPY(vec2, vec);
+                                       copy_v3_v3(vec2, vec);
                                        mul_v3_fl(vec2, t);
 
                                        sub_v3_v3v3(vec2, loc, vec2);
@@ -515,7 +518,7 @@ static int rule_follow_leader(BoidRule *rule, BoidBrainData *bbd, BoidValues *va
 
                                        if(len < 2.0f * val->personal_space * pa->size) {
                                                t_min = t;
-                                               VECCOPY(bbd->wanted_co, loc);
+                                               copy_v3_v3(bbd->wanted_co, loc);
                                                bbd->wanted_speed = val->max_speed * (3.0f - t)/3.0f;
                                                ret = 1;
                                        }
@@ -527,16 +530,16 @@ static int rule_follow_leader(BoidRule *rule, BoidBrainData *bbd, BoidValues *va
 
                /* not blocking so try to follow leader */
                if(flbr->options & BRULE_LEADER_IN_LINE) {
-                       VECCOPY(vec, bbd->sim->psys->particles[p-1].prev_state.vel);
-                       VECCOPY(loc, bbd->sim->psys->particles[p-1].prev_state.co);
+                       copy_v3_v3(vec, bbd->sim->psys->particles[p-1].prev_state.vel);
+                       copy_v3_v3(loc, bbd->sim->psys->particles[p-1].prev_state.co);
                }
                else {
-                       VECCOPY(vec, bbd->sim->psys->particles[p - p%n].prev_state.vel);
-                       VECCOPY(loc, bbd->sim->psys->particles[p - p%n].prev_state.co);
+                       copy_v3_v3(vec, bbd->sim->psys->particles[p - p%n].prev_state.vel);
+                       copy_v3_v3(loc, bbd->sim->psys->particles[p - p%n].prev_state.co);
                }
                
                /* fac is seconds behind leader */
-               VECADDFAC(loc, loc, vec, -flbr->distance);
+               madd_v3_v3fl(loc, vec, -flbr->distance);
 
                sub_v3_v3v3(bbd->wanted_co, loc, pa->prev_state.co);
                bbd->wanted_speed = len_v3(bbd->wanted_co);
@@ -560,11 +563,11 @@ static int rule_average_speed(BoidRule *rule, BoidBrainData *bbd, BoidValues *va
 
                normalize_v3(bpa->wander);
 
-               VECCOPY(vec, bpa->wander);
+               copy_v3_v3(vec, bpa->wander);
 
                mul_qt_v3(pa->prev_state.rot, vec);
 
-               VECCOPY(bbd->wanted_co, pa->prev_state.ave);
+               copy_v3_v3(bbd->wanted_co, pa->prev_state.ave);
 
                mul_v3_fl(bbd->wanted_co, 1.1f);
 
@@ -578,7 +581,7 @@ static int rule_average_speed(BoidRule *rule, BoidBrainData *bbd, BoidValues *va
                }
        }
        else {
-               VECCOPY(bbd->wanted_co, pa->prev_state.ave);
+               copy_v3_v3(bbd->wanted_co, pa->prev_state.ave);
 
                /* may happen at birth */
                if(dot_v2v2(bbd->wanted_co,bbd->wanted_co)==0.0f) {
@@ -640,7 +643,7 @@ static int rule_fight(BoidRule *rule, BoidBrainData *bbd, BoidValues *val, Parti
                                health += bpa->data.health;
 
                                if(n==0 && pt->mode==PTARGET_MODE_ENEMY && ptn[n].dist < closest_dist) {
-                                       VECCOPY(closest_enemy, ptn[n].co);
+                                       copy_v3_v3(closest_enemy, ptn[n].co);
                                        closest_dist = ptn[n].dist;
                                        enemy_pa = epars + ptn[n].index;
                                }
@@ -723,7 +726,7 @@ static void set_boid_values(BoidValues *val, BoidSettings *boids, ParticleData *
        if(ELEM(bpa->data.mode, eBoidMode_OnLand, eBoidMode_Climbing)) {
                val->max_speed = boids->land_max_speed * bpa->data.health/boids->health;
                val->max_acc = boids->land_max_acc * val->max_speed;
-               val->max_ave = boids->land_max_ave * M_PI * bpa->data.health/boids->health;
+               val->max_ave = boids->land_max_ave * (float)M_PI * bpa->data.health/boids->health;
                val->min_speed = 0.0f; /* no minimum speed on land */
                val->personal_space = boids->land_personal_space;
                val->jump_speed = boids->land_jump_speed * bpa->data.health/boids->health;
@@ -731,14 +734,14 @@ static void set_boid_values(BoidValues *val, BoidSettings *boids, ParticleData *
        else {
                val->max_speed = boids->air_max_speed * bpa->data.health/boids->health;
                val->max_acc = boids->air_max_acc * val->max_speed;
-               val->max_ave = boids->air_max_ave * M_PI * bpa->data.health/boids->health;
+               val->max_ave = boids->air_max_ave * (float)M_PI * bpa->data.health/boids->health;
                val->min_speed = boids->air_min_speed * boids->air_max_speed;
                val->personal_space = boids->air_personal_space;
                val->jump_speed = 0.0f; /* no jumping in air */
        }
 }
 
-static Object *boid_find_ground(BoidBrainData *bbd, ParticleData *pa, float *ground_co, float *ground_nor)
+static Object *boid_find_ground(BoidBrainData *bbd, ParticleData *pa, float ground_co[3], float ground_nor[3])
 {
        BoidParticle *bpa = pa->boid;
 
@@ -770,25 +773,26 @@ static Object *boid_find_ground(BoidBrainData *bbd, ParticleData *pa, float *gro
                /* first try to find below boid */
                copy_v3_v3(col.co1, pa->state.co);
                sub_v3_v3v3(col.co2, pa->state.co, zvec);
-               sub_v3_v3(col.co2, zvec);
                sub_v3_v3v3(ray_dir, col.co2, col.co1);
-               col.t = 0.0f;
+               col.f = 0.0f;
                hit.index = -1;
-               hit.dist = col.ray_len = len_v3(ray_dir);
+               hit.dist = col.original_ray_length = len_v3(ray_dir);
+               col.pce.inside = 0;
 
                for(coll = bbd->sim->colliders->first; coll; coll = coll->next){
-                       col.ob = coll->ob;
+                       col.current = coll->ob;
                        col.md = coll->collmd;
+                       col.fac1 = col.fac2 = 0.f;
 
                        if(col.md && col.md->bvhtree)
-                               BLI_bvhtree_ray_cast(col.md->bvhtree, col.co1, ray_dir, radius, &hit, particle_intersect_face, &col);
+                               BLI_bvhtree_ray_cast(col.md->bvhtree, col.co1, ray_dir, radius, &hit, BKE_psys_collision_neartest_cb, &col);
                }
                /* then use that object */
                if(hit.index>=0) {
-                       t = hit.dist/col.ray_len;
+                       t = hit.dist/col.original_ray_length;
                        interp_v3_v3v3(ground_co, col.co1, col.co2, t);
-                       normalize_v3_v3(ground_nor, col.nor);
-                       return col.hit_ob;
+                       normalize_v3_v3(ground_nor, col.pce.nor);
+                       return col.hit;
                }
 
                /* couldn't find below, so find upmost deflector object */
@@ -796,34 +800,34 @@ static Object *boid_find_ground(BoidBrainData *bbd, ParticleData *pa, float *gro
                sub_v3_v3v3(col.co2, pa->state.co, zvec);
                sub_v3_v3(col.co2, zvec);
                sub_v3_v3v3(ray_dir, col.co2, col.co1);
-               col.t = 0.0f;
+               col.f = 0.0f;
                hit.index = -1;
-               hit.dist = col.ray_len = len_v3(ray_dir);
+               hit.dist = col.original_ray_length = len_v3(ray_dir);
 
                for(coll = bbd->sim->colliders->first; coll; coll = coll->next){
-                       col.ob = coll->ob;
+                       col.current = coll->ob;
                        col.md = coll->collmd;
 
                        if(col.md && col.md->bvhtree)
-                               BLI_bvhtree_ray_cast(col.md->bvhtree, col.co1, ray_dir, radius, &hit, particle_intersect_face, &col);
+                               BLI_bvhtree_ray_cast(col.md->bvhtree, col.co1, ray_dir, radius, &hit, BKE_psys_collision_neartest_cb, &col);
                }
                /* then use that object */
                if(hit.index>=0) {
-                       t = hit.dist/col.ray_len;
+                       t = hit.dist/col.original_ray_length;
                        interp_v3_v3v3(ground_co, col.co1, col.co2, t);
-                       normalize_v3_v3(ground_nor, col.nor);
-                       return col.hit_ob;
+                       normalize_v3_v3(ground_nor, col.pce.nor);
+                       return col.hit;
                }
 
                /* default to z=0 */
-               VECCOPY(ground_co, pa->state.co);
+               copy_v3_v3(ground_co, pa->state.co);
                ground_co[2] = 0;
                ground_nor[0] = ground_nor[1] = 0.0f;
                ground_nor[2] = 1.0f;
                return NULL;
        }
 }
-static int boid_rule_applies(ParticleData *pa, BoidSettings *boids, BoidRule *rule)
+static int boid_rule_applies(ParticleData *pa, BoidSettings *UNUSED(boids), BoidRule *rule)
 {
        BoidParticle *bpa = pa->boid;
 
@@ -849,8 +853,8 @@ void boids_precalc_rules(ParticleSettings *part, float cfra)
 
                                if(flbr->ob && flbr->cfra != cfra) {
                                        /* save object locations for velocity calculations */
-                                       VECCOPY(flbr->oloc, flbr->loc);
-                                       VECCOPY(flbr->loc, flbr->ob->obmat[3]);
+                                       copy_v3_v3(flbr->oloc, flbr->loc);
+                                       copy_v3_v3(flbr->loc, flbr->ob->obmat[3]);
                                        flbr->cfra = cfra;
                                }
                        }
@@ -861,10 +865,10 @@ static void boid_climb(BoidSettings *boids, ParticleData *pa, float *surface_co,
 {
        BoidParticle *bpa = pa->boid;
        float nor[3], vel[3];
-       VECCOPY(nor, surface_nor);
+       copy_v3_v3(nor, surface_nor);
 
        /* gather apparent gravity */
-       VECADDFAC(bpa->gravity, bpa->gravity, surface_nor, -1.0);
+       madd_v3_v3fl(bpa->gravity, surface_nor, -1.0f);
        normalize_v3(bpa->gravity);
 
        /* raise boid it's size from surface */
@@ -900,7 +904,8 @@ static int apply_boid_rule(BoidBrainData *bbd, BoidRule *rule, BoidValues *val,
        else
                return 0;
 }
-static BoidState *get_boid_state(BoidSettings *boids, ParticleData *pa) {
+static BoidState *get_boid_state(BoidSettings *boids, ParticleData *pa)
+{
        BoidState *state = boids->states.first;
        BoidParticle *bpa = pa->boid;
 
@@ -929,6 +934,7 @@ void boid_brain(BoidBrainData *bbd, int p, ParticleData *pa)
        BoidValues val;
        BoidState *state = get_boid_state(boids, pa);
        BoidParticle *bpa = pa->boid;
+       ParticleSystem *psys = bbd->sim->psys;
        int rand;
        //BoidCondition *cond;
 
@@ -951,9 +957,8 @@ void boid_brain(BoidBrainData *bbd, int p, ParticleData *pa)
        bbd->wanted_co[0]=bbd->wanted_co[1]=bbd->wanted_co[2]=bbd->wanted_speed=0.0f;
 
        /* create random seed for every particle & frame */
-       BLI_srandom(bbd->sim->psys->seed + p);
-       rand = BLI_rand();
-       BLI_srandom((int)bbd->cfra + rand);
+       rand = (int)(PSYS_FRAND(psys->seed + p) * 1000);
+       rand = (int)(PSYS_FRAND((int)bbd->cfra + rand) * 1000);
 
        set_boid_values(&val, bbd->part->boids, pa);
 
@@ -992,7 +997,7 @@ void boid_brain(BoidBrainData *bbd, int p, ParticleData *pa)
                                wanted_speed /= (float)n;
                        }
 
-                       VECCOPY(bbd->wanted_co, wanted_co);
+                       copy_v3_v3(bbd->wanted_co, wanted_co);
                        bbd->wanted_speed = wanted_speed;
                        break;
                }
@@ -1002,18 +1007,18 @@ void boid_brain(BoidBrainData *bbd, int p, ParticleData *pa)
        /* decide on jumping & liftoff */
        if(bpa->data.mode == eBoidMode_OnLand) {
                /* fuzziness makes boids capable of misjudgement */
-               float mul = 1.0 + state->rule_fuzziness;
+               float mul = 1.0f + state->rule_fuzziness;
                
                if(boids->options & BOID_ALLOW_FLIGHT && bbd->wanted_co[2] > 0.0f) {
                        float cvel[3], dir[3];
 
-                       VECCOPY(dir, pa->prev_state.ave);
+                       copy_v3_v3(dir, pa->prev_state.ave);
                        normalize_v2(dir);
 
-                       VECCOPY(cvel, bbd->wanted_co);
+                       copy_v3_v3(cvel, bbd->wanted_co);
                        normalize_v2(cvel);
 
-                       if(dot_v2v2(cvel, dir) > 0.95 / mul)
+                       if(dot_v2v2(cvel, dir) > 0.95f / mul)
                                bpa->data.mode = eBoidMode_Liftoff;
                }
                else if(val.jump_speed > 0.0f) {
@@ -1026,10 +1031,10 @@ void boid_brain(BoidBrainData *bbd, int p, ParticleData *pa)
                                float z_v, ground_v, cur_v;
                                float len;
 
-                               VECCOPY(dir, pa->prev_state.ave);
+                               copy_v3_v3(dir, pa->prev_state.ave);
                                normalize_v2(dir);
 
-                               VECCOPY(cvel, bbd->wanted_co);
+                               copy_v3_v3(cvel, bbd->wanted_co);
                                normalize_v2(cvel);
 
                                len = len_v2(pa->prev_state.vel);
@@ -1049,7 +1054,7 @@ void boid_brain(BoidBrainData *bbd, int p, ParticleData *pa)
 
                                                len = MIN2(len, val.jump_speed);
 
-                                               VECCOPY(jump_v, dir);
+                                               copy_v3_v3(jump_v, dir);
                                                jump_v[2] = z_v;
                                                mul_v3_fl(jump_v, ground_v);
 
@@ -1066,7 +1071,7 @@ void boid_brain(BoidBrainData *bbd, int p, ParticleData *pa)
                        }
 
                        if(jump) {
-                               VECCOPY(pa->prev_state.vel, jump_v);
+                               copy_v3_v3(pa->prev_state.vel, jump_v);
                                bpa->data.mode = eBoidMode_Falling;
                        }
                }
@@ -1121,7 +1126,7 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
                        bpa->ground = boid_find_ground(bbd, pa, ground_co, ground_nor);
                        
                        /* level = how many particle sizes above ground */
-                       level = (pa->prev_state.co[2] - ground_co[2])/(2.0f * pa->size) - 0.5;
+                       level = (pa->prev_state.co[2] - ground_co[2])/(2.0f * pa->size) - 0.5f;
 
                        landing_level = - boids->landing_smoothness * pa->prev_state.vel[2] * pa_mass;
 
@@ -1138,12 +1143,12 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
                        }
                }
 
-               VECCOPY(old_dir, pa->prev_state.ave);
+               copy_v3_v3(old_dir, pa->prev_state.ave);
                new_speed = normalize_v3_v3(wanted_dir, bbd->wanted_co);
 
                /* first check if we have valid direction we want to go towards */
                if(new_speed == 0.0f) {
-                       VECCOPY(new_dir, old_dir);
+                       copy_v3_v3(new_dir, old_dir);
                }
                else {
                        float old_dir2[2], wanted_dir2[2], nor[3], angle;
@@ -1167,13 +1172,13 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
 
                        cross_v3_v3v3(nor, old_dir, wanted_dir);
                        axis_angle_to_quat( q,nor, angle);
-                       VECCOPY(new_dir, old_dir);
+                       copy_v3_v3(new_dir, old_dir);
                        mul_qt_v3(q, new_dir);
                        normalize_v3(new_dir);
 
                        /* save direction in case resulting velocity too small */
                        axis_angle_to_quat( q,nor, angle*dtime);
-                       VECCOPY(pa->state.ave, old_dir);
+                       copy_v3_v3(pa->state.ave, old_dir);
                        mul_qt_v3(q, pa->state.ave);
                        normalize_v3(pa->state.ave);
                }
@@ -1187,7 +1192,7 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
                        new_speed = MIN2(bbd->wanted_speed, old_speed + val.max_acc);
 
                /* combine direction and speed */
-               VECCOPY(new_vel, new_dir);
+               copy_v3_v3(new_vel, new_dir);
                mul_v3_fl(new_vel, new_speed);
 
                /* maintain minimum flying velocity if not landing */
@@ -1231,7 +1236,7 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
        add_v3_v3(acc, force);
 
        /* store smoothed acceleration for nice banking etc. */
-       VECADDFAC(bpa->data.acc, bpa->data.acc, acc, dtime);
+       madd_v3_v3fl(bpa->data.acc, acc, dtime);
        mul_v3_fl(bpa->data.acc, 1.0f / (1.0f + dtime));
 
        /* integrate new location & velocity */
@@ -1240,15 +1245,15 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
        /* can get better control allthough it's a bit unphysical       */
        mul_v3_fl(acc, 1.0f/pa_mass);
 
-       VECCOPY(dvec, acc);
+       copy_v3_v3(dvec, acc);
        mul_v3_fl(dvec, dtime*dtime*0.5f);
        
-       VECCOPY(bvec, pa->prev_state.vel);
+       copy_v3_v3(bvec, pa->prev_state.vel);
        mul_v3_fl(bvec, dtime);
        add_v3_v3(dvec, bvec);
        add_v3_v3(pa->state.co, dvec);
 
-       VECADDFAC(pa->state.vel, pa->state.vel, acc, dtime);
+       madd_v3_v3fl(pa->state.vel, acc, dtime);
 
        //if(bpa->data.mode != eBoidMode_InAir)
        bpa->ground = boid_find_ground(bbd, pa, ground_co, ground_nor);
@@ -1257,7 +1262,11 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
        switch(bpa->data.mode) {
                case eBoidMode_InAir:
                {
-                       float grav[3] = {0.0f, 0.0f, bbd->sim->scene->physics_settings.gravity[2] < 0.0f ? -1.0f : 0.0f};
+                       float grav[3];
+
+                       grav[0]= 0.0f;
+                       grav[1]= 0.0f;
+                       grav[2]= bbd->sim->scene->physics_settings.gravity[2] < 0.0f ? -1.0f : 0.0f;
 
                        /* don't take forward acceleration into account (better banking) */
                        if(dot_v3v3(bpa->data.acc, pa->state.vel) > 0.0f) {
@@ -1265,11 +1274,11 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
                                sub_v3_v3v3(dvec, bpa->data.acc, dvec);
                        }
                        else {
-                               VECCOPY(dvec, bpa->data.acc);
+                               copy_v3_v3(dvec, bpa->data.acc);
                        }
 
                        /* gather apparent gravity */
-                       VECADDFAC(bpa->gravity, grav, dvec, -boids->banking);
+                       madd_v3_v3v3fl(bpa->gravity, grav, dvec, -boids->banking);
                        normalize_v3(bpa->gravity);
 
                        /* stick boid on goal when close enough */
@@ -1287,7 +1296,7 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
                                        bpa->data.mode = eBoidMode_OnLand;
                                }
                                /* fly above ground */
-                               else {
+                               else if(bpa->ground) {
                                        pa->state.co[2] = ground_co[2] + pa->size * boids->height;
                                        pa->state.vel[2] = 0.0f;
                                }
@@ -1296,10 +1305,15 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
                }
                case eBoidMode_Falling:
                {
-                       float grav[3] = {0.0f, 0.0f, bbd->sim->scene->physics_settings.gravity[2] < 0.0f ? -1.0f : 0.0f};
+                       float grav[3];
+
+                       grav[0]= 0.0f;
+                       grav[1]= 0.0f;
+                       grav[2]= bbd->sim->scene->physics_settings.gravity[2] < 0.0f ? -1.0f : 0.0f;
+
 
                        /* gather apparent gravity */
-                       VECADDFAC(bpa->gravity, bpa->gravity, grav, dtime);
+                       madd_v3_v3fl(bpa->gravity, grav, dtime);
                        normalize_v3(bpa->gravity);
 
                        if(boids->options & BOID_ALLOW_LAND) {
@@ -1311,7 +1325,7 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
                                        boid_climb(boids, pa, ground_co, ground_nor);
                                }
                                /* land boid when really near ground */
-                               else if(pa->state.co[2] <= ground_co[2] + 1.01 * pa->size * boids->height){
+                               else if(pa->state.co[2] <= ground_co[2] + 1.01f * pa->size * boids->height){
                                        pa->state.co[2] = ground_co[2] + pa->size * boids->height;
                                        pa->state.vel[2] = 0.0f;
                                        bpa->data.mode = eBoidMode_OnLand;
@@ -1328,10 +1342,10 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
                {
                        boid_climb(boids, pa, ground_co, ground_nor);
                        //float nor[3];
-                       //VECCOPY(nor, ground_nor);
+                       //copy_v3_v3(nor, ground_nor);
 
                        ///* gather apparent gravity to r_ve */
-                       //VECADDFAC(pa->r_ve, pa->r_ve, ground_nor, -1.0);
+                       //madd_v3_v3fl(pa->r_ve, ground_nor, -1.0);
                        //normalize_v3(pa->r_ve);
 
                        ///* raise boid it's size from surface */
@@ -1353,7 +1367,7 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
                                boid_climb(boids, pa, ground_co, ground_nor);
                        }
                        /* ground is too far away so boid falls */
-                       else if(pa->state.co[2]-ground_co[2] > 1.1 * pa->size * boids->height)
+                       else if(pa->state.co[2]-ground_co[2] > 1.1f * pa->size * boids->height)
                                bpa->data.mode = eBoidMode_Falling;
                        else {
                                /* constrain to surface */
@@ -1371,12 +1385,12 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
                                sub_v3_v3v3(dvec, bpa->data.acc, dvec);
 
                                /* gather apparent gravity */
-                               VECADDFAC(bpa->gravity, grav, dvec, -boids->banking);
+                               madd_v3_v3v3fl(bpa->gravity, grav, dvec, -boids->banking);
                                normalize_v3(bpa->gravity);
                        }
                        else {
                                /* gather negative surface normal */
-                               VECADDFAC(bpa->gravity, bpa->gravity, ground_nor, -1.0f);
+                               madd_v3_v3fl(bpa->gravity, ground_nor, -1.0f);
                                normalize_v3(bpa->gravity);
                        }
                        break;
@@ -1385,7 +1399,7 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
 
        /* save direction to state.ave unless the boid is falling */
        /* (boids can't effect their direction when falling) */
-       if(bpa->data.mode!=eBoidMode_Falling && len_v3(pa->state.vel) > 0.1*pa->size) {
+       if(bpa->data.mode!=eBoidMode_Falling && len_v3(pa->state.vel) > 0.1f*pa->size) {
                copy_v3_v3(pa->state.ave, pa->state.vel);
                pa->state.ave[2] *= bbd->part->boids->pitch;
                normalize_v3(pa->state.ave);
@@ -1397,7 +1411,7 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
 
        /* calculate rotation matrix based on forward & down vectors */
        if(bpa->data.mode == eBoidMode_InAir) {
-               VECCOPY(mat[0], pa->state.ave);
+               copy_v3_v3(mat[0], pa->state.ave);
 
                project_v3_v3v3(dvec, bpa->gravity, pa->state.ave);
                sub_v3_v3v3(mat[2], bpa->gravity, dvec);
@@ -1408,7 +1422,7 @@ void boid_body(BoidBrainData *bbd, ParticleData *pa)
                sub_v3_v3v3(mat[0], pa->state.ave, dvec);
                normalize_v3(mat[0]);
 
-               VECCOPY(mat[2], bpa->gravity);
+               copy_v3_v3(mat[2], bpa->gravity);
        }
        negate_v3(mat[2]);
        cross_v3_v3v3(mat[1], mat[2], mat[0]);
@@ -1453,7 +1467,7 @@ BoidRule *boid_new_rule(int type)
 
        rule->type = type;
        rule->flag |= BOIDRULE_IN_AIR|BOIDRULE_ON_LAND;
-       strcpy(rule->name, boidrule_type_items[type-1].name);
+       BLI_strncpy(rule->name, boidrule_type_items[type-1].name, sizeof(rule->name));
 
        return rule;
 }
@@ -1489,7 +1503,7 @@ BoidState *boid_new_state(BoidSettings *boids)
 
        state->id = boids->last_state_id++;
        if(state->id)
-               sprintf(state->name, "State %i", state->id);
+               BLI_snprintf(state->name, sizeof(state->name), "State %i", state->id);
        else
                strcpy(state->name, "State");
 
@@ -1500,7 +1514,8 @@ BoidState *boid_new_state(BoidSettings *boids)
        return state;
 }
 
-BoidState *boid_duplicate_state(BoidSettings *boids, BoidState *state) {
+BoidState *boid_duplicate_state(BoidSettings *boids, BoidState *state)
+{
        BoidState *staten = MEM_dupallocN(state);
 
        BLI_duplicatelist(&staten->rules, &state->rules);