From eb93040c3afa552293739177b02ba81791ab7702 Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 09:26:46 +0200
Subject: [PATCH 01/15] Code style, cleanup, minor optimizations

---
 source/helpers.h                | 199 +++++++++++++++++---------------
 source/main.cpp                 |  74 ++++++------
 source/main.h                   |   6 +-
 source/object/FlockModifier.cpp |  46 ++++----
 source/object/FlockRepeller.cpp | 171 +++++++++++++--------------
 source/object/FlockTarget.cpp   | 197 ++++++++++++++++---------------
 6 files changed, 348 insertions(+), 345 deletions(-)

diff --git a/source/helpers.h b/source/helpers.h
index b4b5c44..07409a1 100755
--- a/source/helpers.h
+++ b/source/helpers.h
@@ -1,95 +1,104 @@
-#ifndef _FLOCKHELPERS_H_
-#define _FLOCKHELPERS_H_
-
-
-#define ID_OFLOCKMODIFIER  1029168
-#define ID_OFLOCKREPELLER  1029184
-#define ID_OFLOCKTARGET    1029185
-
-
-#define COLOR_FLOCKTARGET    Vector(0.5, 1.0, 0.5)
-#define COLOR_FLOCKREPELLER  Vector(1.0, 0.3, 0.3)
-
-enum RULEFLAGS
-{
-	RULEFLAGS_NONE          = 0,
-	RULEFLAGS_CENTER        = (1<<0),
-	RULEFLAGS_NEIGHBORDIST  = (1<<1),
-	RULEFLAGS_MATCHVELO     = (1<<2),
-	RULEFLAGS_TARGET        = (1<<3),
-	RULEFLAGS_LEVELFLIGHT   = (1<<4),
-	RULEFLAGS_AVOIDGEO      = (1<<5),
-	RULEFLAGS_TURBULENCE    = (1<<6),
-	RULEFLAGS_SPEEDLIMIT    = (1<<7),
-	RULEFLAGS_REPELL        = (1<<8)
-} ENUM_END_FLAGS(RULEFLAGS);
-
-
-struct TargetData
-{
-	Float32 _weight;
-	Float32 _radius;
-	Bool    _infinite;
-	Vector  _position;
-
-	TargetData() :
-	  _weight(0.0),
-	  _radius(0.0),
-	  _infinite(false),
-	  _position(0.0)
-	{ }
-	
-	TargetData(Float32 weight, Float32 radius, Bool infinite, const Vector &position) :
-	  _weight(weight), _radius(radius), _infinite(infinite), _position(position)
-	{ }
-};
-
-
-struct RepellerData
-{
-	Float32 _weight;
-	Float32 _radius;
-	Vector  _position;
-
-	RepellerData() :
-	  _weight(0.0),
-	  _radius(0.0),
-	  _position(0.0)
-	{ }
-	
-	RepellerData(Float32 weight, Float32 radius, const Vector &position) :
-	  _weight(weight), _radius(radius), _position(position)
-	{ }
-};
-
-
-inline void DrawSphere(BaseDraw* bd, Float32 radius)
-{
-	if (!bd)
-		return;
-
-	Matrix mc;
-	Vector v(DC);
-
-	mc.Scale(radius);
-	bd->DrawCircle(mc);
-	v = mc.v3; mc.v3 = mc.v2; mc.v2 = v;
-	bd->DrawCircle(mc);
-	v = mc.v1; mc.v1 = mc.v3; mc.v3 = v;
-	bd->DrawCircle(mc);
-}
-
-
-inline void Draw3DCross(BaseDraw* bd, Float32 length)
-{
-	if (!bd) return;
-
-	Matrix mc;
-	Vector v(DC);
-
-	bd->DrawLine(Vector(0.0, length, 0.0), Vector(0.0, -length, 0.0), 0);
-	bd->DrawLine(Vector(length, 0.0, 0.0), Vector(-length, 0.0, 0.0), 0);
-	bd->DrawLine(Vector(0.0, 0.0, length), Vector(0.0, 0.0, -length), 0);
-}
-
-#endif
+#ifndef FLOCKHELPERS_H__
+#define FLOCKHELPERS_H__
+
+
+const Int32 ID_OFLOCKMODIFIER = 1029168;
+const Int32 ID_OFLOCKREPELLER = 1029184;
+const Int32 ID_OFLOCKTARGET = 1029185;
+
+const Vector COLOR_FLOCKTARGET(0.5, 1.0, 0.5);
+const Vector COLOR_FLOCKREPELLER(1.0, 0.3, 0.3);
+
+
+/// Flags to indicate which rules are active
+enum RULEFLAGS
+{
+	RULEFLAGS_NONE          = 0,
+	RULEFLAGS_CENTER        = (1<<0),
+	RULEFLAGS_NEIGHBORDIST  = (1<<1),
+	RULEFLAGS_MATCHVELO     = (1<<2),
+	RULEFLAGS_TARGET        = (1<<3),
+	RULEFLAGS_LEVELFLIGHT   = (1<<4),
+	RULEFLAGS_AVOIDGEO      = (1<<5),
+	RULEFLAGS_TURBULENCE    = (1<<6),
+	RULEFLAGS_SPEEDLIMIT    = (1<<7),
+	RULEFLAGS_REPELL        = (1<<8)
+} ENUM_END_FLAGS(RULEFLAGS);
+
+
+/// Data for a FlockTarget
+struct TargetData
+{
+	Float32 _weight;    ///< Weight of this target
+	Float32 _radius;    ///< Radius of this target
+	Bool    _infinite;  ///< Is target radius infinite?
+	Vector  _position;  ///< Global position of target
+
+	TargetData() :
+		_weight(0.0),
+		_radius(0.0),
+		_infinite(false),
+		_position(0.0)
+	{ }
+	
+	TargetData(Float32 weight, Float32 radius, Bool infinite, const Vector &position) :
+		_weight(weight),
+		_radius(radius),
+		_infinite(infinite),
+		_position(position)
+	{ }
+};
+
+
+/// Data for a FlockRepeller
+struct RepellerData
+{
+	Float32 _weight;
+	Float32 _radius;
+	Vector  _position;
+
+	RepellerData() :
+		_weight(0.0),
+		_radius(0.0),
+		_position(0.0)
+	{ }
+	
+	RepellerData(Float32 weight, Float32 radius, const Vector &position) :
+		_weight(weight),
+		_radius(radius),
+		_position(position)
+	{ }
+};
+
+
+inline void DrawSphere(BaseDraw *bd, Float radius)
+{
+	if (!bd)
+		return;
+
+	Matrix mc;
+	Vector v(DC);
+
+	mc.Scale(radius);
+	bd->DrawCircle(mc);
+	v = mc.v3; mc.v3 = mc.v2; mc.v2 = v;
+	bd->DrawCircle(mc);
+	v = mc.v1; mc.v1 = mc.v3; mc.v3 = v;
+	bd->DrawCircle(mc);
+}
+
+
+inline void Draw3DCross(BaseDraw *bd, Float length)
+{
+	if (!bd)
+		return;
+
+	Matrix mc;
+	Vector v(DC);
+
+	bd->DrawLine(Vector(0.0, length, 0.0), Vector(0.0, -length, 0.0), 0);
+	bd->DrawLine(Vector(length, 0.0, 0.0), Vector(-length, 0.0, 0.0), 0);
+	bd->DrawLine(Vector(0.0, 0.0, length), Vector(0.0, 0.0, -length), 0);
+}
+
+#endif // FLOCKHELPERS_H__
diff --git a/source/main.cpp b/source/main.cpp
index aa64473..91e95e4 100755
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -1,37 +1,37 @@
-#include "c4d.h"
-#include "main.h"
-
-
-#define PLUGIN_VERSION  String("0.7")
-
-
-Bool PluginStart()
-{
-	GePrint("FlockModifier " + PLUGIN_VERSION);
-	
-	if (!RegisterFlockModifier())
-		return false;
-	if (!RegisterFlockTarget())
-		return false;
-	if (!RegisterFlockRepeller())
-		return false;
-
-	return true;
-}
-
-void PluginEnd()
-{ }
-
-Bool PluginMessage(Int32 id, void *data)
-{
-	switch (id)
-	{
-		case C4DPL_INIT_SYS:
-			return resource.Init(); // don't start plugin without resource
-
-		case C4DMSG_PRIORITY:
-			return true;
-	}
-
-	return false;
-}
+#include "c4d.h"
+#include "main.h"
+
+
+#define PLUGIN_VERSION  String("0.8")
+
+
+Bool PluginStart()
+{
+	GePrint("FlockModifier " + PLUGIN_VERSION);
+	
+	if (!RegisterFlockModifier())
+		return false;
+	if (!RegisterFlockTarget())
+		return false;
+	if (!RegisterFlockRepeller())
+		return false;
+
+	return true;
+}
+
+void PluginEnd()
+{ }
+
+Bool PluginMessage(Int32 id, void *data)
+{
+	switch (id)
+	{
+		case C4DPL_INIT_SYS:
+			return resource.Init(); // don't start plugin without resource
+
+		case C4DMSG_PRIORITY:
+			return true;
+	}
+
+	return false;
+}
diff --git a/source/main.h b/source/main.h
index da0da35..ff9bebb 100644
--- a/source/main.h
+++ b/source/main.h
@@ -1,5 +1,5 @@
-#ifndef _MAIN_H
-#define _MAIN_H
+#ifndef MAIN_H__
+#define MAIN_H__
 
 #include "c4d.h"
 
@@ -7,4 +7,4 @@ Bool RegisterFlockModifier();
 Bool RegisterFlockTarget();
 Bool RegisterFlockRepeller();
 
-#endif
+#endif // MAIN_H__
diff --git a/source/object/FlockModifier.cpp b/source/object/FlockModifier.cpp
index f14a788..1dee854 100755
--- a/source/object/FlockModifier.cpp
+++ b/source/object/FlockModifier.cpp
@@ -13,25 +13,18 @@ class FlockModifier : public ObjectData
 {
 	INSTANCEOF(FlockModifier, ObjectData)
 	
-public:
-	FlockModifier()
-	{ }
-	
-	~FlockModifier()
-	{ }
-
 public:
 	virtual Bool Init(GeListNode *node);
 	virtual Bool GetDEnabling(GeListNode *node, const DescID &id, const GeData &t_data, DESCFLAGS_ENABLE flags, const BaseContainer *itemdesc);
 	virtual void ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *ss, Int32 pcnt, Float diff);
 	
-	static NodeData *Alloc(void)
+	static NodeData *Alloc()
 	{
 		return NewObjClear(FlockModifier);
 	}
 	
 private:
-	AutoFree<GeRayCollider> _collider;
+	AutoFree<GeRayCollider> _geoAvoidanceCollider;
 };
 
 
@@ -106,15 +99,15 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 
 	Int32 i,j,n;
 
-	BaseContainer* bc = op->GetDataInstance();
-	BaseDocument* doc = op->GetDocument();
+	BaseContainer *bc = op->GetDataInstance();
+	BaseDocument *doc = op->GetDocument();
 	
 	if (!bc || !doc)
 		return;
 
 	// Variables
 	Int32 lCount = pcnt - 1;
-	Vector vParticleDir;
+	Vector vParticleDir(DC);
 
 	// Overall weight
 	Float rWeight = bc->GetFloat(OFLOCK_WEIGHT, 1.0);
@@ -125,24 +118,25 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	rNeighborSightRadius *= rNeighborSightRadius;		// Square
 
 	// Flock Center
-	Vector vCenterflockDir;
+	Vector vCenterflockDir(DC);
 	Float rCenterflockWeight = bc->GetFloat(OFLOCK_CENTER_WEIGHT, 0.0) * 0.1;
 
 	// Neighbor Distance
 	Vector vNeighborDiff(DC);
-	Vector vNeighborDir;
+	Vector vNeighborDir(DC);
 	Float rNeighborWeight = bc->GetFloat(OFLOCK_NEIGHBORDIST_WEIGHT, 0.0) * 0.1;
 	Float rNeighborMinDist = bc->GetFloat(OFLOCK_NEIGHBORDIST_DIST, 0.0);
 
 	// Match Velocity
-	Vector vMatchVelocityDir;
+	Vector vMatchVelocityDir(DC);
 	Float rMatchVelocityWeight = bc->GetFloat(OFLOCK_MATCHVEL_WEIGHT, 0.0) * 0.1;
 
 	// Target
 	Float rTargetGlobalWeight = bc->GetFloat(OFLOCK_TARGET_WEIGHT, 0.0) * 0.1;
 	InExcludeData* inexTarget = (InExcludeData*)bc->GetCustomDataType(OFLOCK_TARGET_LINK, CUSTOMDATATYPE_INEXCLUDE_LIST);
 	Int32 lTargetCount = 0;
-	if (inexTarget) lTargetCount = inexTarget->GetObjectCount();
+	if (inexTarget)
+		lTargetCount = inexTarget->GetObjectCount();
 	maxon::BaseArray<TargetData>targetData;
 
 	// Level flight
@@ -160,17 +154,17 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	Int32 lAvoidGeoMode = bc->GetInt32(OFLOCK_AVOIDGEO_MODE, 1);
 	Float rAvoidGeoWeight = bc->GetFloat(OFLOCK_AVOIDGEO_WEIGHT, 0.0);
 	Float rAvoidGeoDist = bc->GetFloat(OFLOCK_AVOIDGEO_DIST, 0.0);
-	Vector vAvoidGeoDir;
+	Vector vAvoidGeoDir(DC);
 	BaseObject* boAvoidGeoLink = bc->GetObjectLink(OFLOCK_AVOIDGEO_LINK, doc);
-	Matrix mAvoidGeo, mAvoidGeoI;
+	Matrix mAvoidGeo(DC), mAvoidGeoI(DC);
 	Float rAvoidGeoMixval;
 
 	// Turbulence
 	Float rTurbulenceWeight = bc->GetFloat(OFLOCK_TURBULENCE_WEIGHT, 0.0) * 10.0;
 	Float rTurbulenceTime = doc->GetTime().Get() * bc->GetFloat(OFLOCK_TURBULENCE_FREQUENCY, 1.0);
 	Float rTurbulenceScale = 0.1 / ClampValue(bc->GetFloat(OFLOCK_TURBULENCE_SCALE, 1.0), 0.0001, MAXRANGE_FLOAT);
-	Vector rTurbulenceAdd1;
-	Vector rTurbulenceAdd2;
+	Vector rTurbulenceAdd1(DC);
+	Vector rTurbulenceAdd2(DC);
 
 	// Repell
 	Float rRepellGlobalWeight = bc->GetFloat(OFLOCK_REPELL_WEIGHT, 0.0);
@@ -258,14 +252,14 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 		rulemask |= RULEFLAGS_AVOIDGEO;
 		
 		// Lazy-alloc collider
-		if (!_collider)
+		if (!_geoAvoidanceCollider)
 		{
-			_collider.Set(GeRayCollider::Alloc());
-			if (!_collider)
+			_geoAvoidanceCollider.Set(GeRayCollider::Alloc());
+			if (!_geoAvoidanceCollider)
 				return;
 		}
 		
-		_collider->Init(boAvoidGeoLink, boAvoidGeoLink->GetDirty(DIRTYFLAGS_CACHE|DIRTYFLAGS_MATRIX|DIRTYFLAGS_DATA));
+		_geoAvoidanceCollider->Init(boAvoidGeoLink, boAvoidGeoLink->GetDirty(DIRTYFLAGS_CACHE|DIRTYFLAGS_MATRIX|DIRTYFLAGS_DATA));
 		mAvoidGeo = boAvoidGeoLink->GetMg();
 		mAvoidGeoI = ~mAvoidGeo;
 	}
@@ -435,11 +429,11 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			// --------------
 			if (rulemask & RULEFLAGS_AVOIDGEO)
 			{
-				if (_collider->Intersect(mAvoidGeoI * pp[i].off, mAvoidGeoI.TransformVector(!vParticleDir), rAvoidGeoDist))
+				if (_geoAvoidanceCollider->Intersect(mAvoidGeoI * pp[i].off, mAvoidGeoI.TransformVector(!vParticleDir), rAvoidGeoDist))
 				{
 					GeRayColResult colliderResult;
 
-					if (_collider->GetNearestIntersection(&colliderResult))
+					if (_geoAvoidanceCollider->GetNearestIntersection(&colliderResult))
 					{
 						rAvoidGeoMixval = 1.0 - colliderResult.distance / rAvoidGeoDist;
 						switch (lAvoidGeoMode)
diff --git a/source/object/FlockRepeller.cpp b/source/object/FlockRepeller.cpp
index da538fe..740e6e1 100755
--- a/source/object/FlockRepeller.cpp
+++ b/source/object/FlockRepeller.cpp
@@ -1,84 +1,87 @@
-#include "c4d.h"
-#include "c4d_symbols.h"
-#include "Oflockrepeller.h"
-#include "helpers.h"
-#include "main.h"
-
-
-class FlockRepeller : public ObjectData
-{
-	INSTANCEOF(FlockRepeller, ObjectData)
-	
-public:
-	virtual Bool Init(GeListNode *node);
-	virtual Bool GetDEnabling(GeListNode *node, const DescID &id, const GeData &t_data, DESCFLAGS_ENABLE flags, const BaseContainer *itemdesc);
-	virtual DRAWRESULT Draw(BaseObject *op, DRAWPASS drawpass, BaseDraw *bd, BaseDrawHelp *bh);
-	
-	static NodeData *Alloc(void) { return NewObjClear(FlockRepeller); }
-};
-
-
-Bool FlockRepeller::Init(GeListNode *node)
-{
-	if (!node)
-		return false;
-	
-	BaseContainer *bc = (static_cast<BaseObject*>(node))->GetDataInstance();
-	if (!bc)
-		return false;
-
-	bc->SetBool(OFLOCKREPELLER_ENABLED, true);
-	bc->SetFloat(OFLOCKREPELLER_WEIGHT, 1.0);
-	bc->SetFloat(OFLOCKREPELLER_RADIUS, 75.0);
-
-	return SUPER::Init(node);
-}
-
-Bool FlockRepeller::GetDEnabling(GeListNode *node, const DescID &id, const GeData &t_data, DESCFLAGS_ENABLE flags, const BaseContainer *itemdesc)
-{
-	if (!node)
-		return false;
-	
-	BaseContainer *bc = (static_cast<BaseObject*>(node))->GetDataInstance();
-	if (!bc)
-		return false;
-
-	switch (id[0].id)
-	{
-		case OFLOCKREPELLER_RADIUS:
-		case OFLOCKREPELLER_WEIGHT:
-			return bc->GetBool(OFLOCKREPELLER_ENABLED, false);
-	}
-
-	return SUPER::GetDEnabling(node, id, t_data, flags, itemdesc);
-}
-
-DRAWRESULT FlockRepeller::Draw(BaseObject *op, DRAWPASS drawpass, BaseDraw *bd, BaseDrawHelp *bh)
-{
-	if (drawpass != DRAWPASS_OBJECT)
-		return DRAWRESULT_SKIP;
-	
-	if (!op || !bd || !bh)
-		return DRAWRESULT_ERROR;
-
-	BaseContainer* bc = op->GetDataInstance();
-	if (!bc)
-		return DRAWRESULT_ERROR;
-
-	bd->SetPen(COLOR_FLOCKREPELLER * bc->GetFloat(OFLOCKREPELLER_WEIGHT));
-
-	bd->SetMatrix_Matrix(op, bh->GetMg());
-	DrawSphere(bd, (Float32)bc->GetFloat(OFLOCKREPELLER_RADIUS));
-	bd->SetMatrix_Matrix(nullptr, Matrix());
-
-	return DRAWRESULT_OK;
-}
-
-
-/****************************************************************************
- * Register Plugin Object
- ****************************************************************************/
-Bool RegisterFlockRepeller()
-{
-	return RegisterObjectPlugin(ID_OFLOCKREPELLER, GeLoadString(IDS_OFLOCKREPELLER), 0, FlockRepeller::Alloc, "Oflockrepeller", AutoBitmap("Oflockrepeller.tif"), 0);
-}
+#include "c4d.h"
+#include "c4d_symbols.h"
+#include "Oflockrepeller.h"
+#include "helpers.h"
+#include "main.h"
+
+
+class FlockRepeller : public ObjectData
+{
+	INSTANCEOF(FlockRepeller, ObjectData)
+	
+public:
+	virtual Bool Init(GeListNode *node);
+	virtual Bool GetDEnabling(GeListNode *node, const DescID &id, const GeData &t_data, DESCFLAGS_ENABLE flags, const BaseContainer *itemdesc);
+	virtual DRAWRESULT Draw(BaseObject *op, DRAWPASS drawpass, BaseDraw *bd, BaseDrawHelp *bh);
+	
+	static NodeData *Alloc()
+	{
+		return NewObjClear(FlockRepeller);
+	}
+};
+
+
+Bool FlockRepeller::Init(GeListNode *node)
+{
+	if (!node)
+		return false;
+	
+	BaseContainer *bc = (static_cast<BaseObject*>(node))->GetDataInstance();
+	if (!bc)
+		return false;
+
+	bc->SetBool(OFLOCKREPELLER_ENABLED, true);
+	bc->SetFloat(OFLOCKREPELLER_WEIGHT, 1.0);
+	bc->SetFloat(OFLOCKREPELLER_RADIUS, 75.0);
+
+	return SUPER::Init(node);
+}
+
+Bool FlockRepeller::GetDEnabling(GeListNode *node, const DescID &id, const GeData &t_data, DESCFLAGS_ENABLE flags, const BaseContainer *itemdesc)
+{
+	if (!node)
+		return false;
+	
+	BaseContainer *bc = (static_cast<BaseObject*>(node))->GetDataInstance();
+	if (!bc)
+		return false;
+
+	switch (id[0].id)
+	{
+		case OFLOCKREPELLER_RADIUS:
+		case OFLOCKREPELLER_WEIGHT:
+			return bc->GetBool(OFLOCKREPELLER_ENABLED, false);
+	}
+
+	return SUPER::GetDEnabling(node, id, t_data, flags, itemdesc);
+}
+
+DRAWRESULT FlockRepeller::Draw(BaseObject *op, DRAWPASS drawpass, BaseDraw *bd, BaseDrawHelp *bh)
+{
+	if (drawpass != DRAWPASS_OBJECT)
+		return DRAWRESULT_SKIP;
+	
+	if (!op || !bd || !bh)
+		return DRAWRESULT_ERROR;
+
+	BaseContainer* bc = op->GetDataInstance();
+	if (!bc)
+		return DRAWRESULT_ERROR;
+
+	bd->SetPen(COLOR_FLOCKREPELLER * bc->GetFloat(OFLOCKREPELLER_WEIGHT));
+
+	bd->SetMatrix_Matrix(op, bh->GetMg());
+	DrawSphere(bd, (Float32)bc->GetFloat(OFLOCKREPELLER_RADIUS));
+	bd->SetMatrix_Matrix(nullptr, Matrix());
+
+	return DRAWRESULT_OK;
+}
+
+
+/****************************************************************************
+ * Register Plugin Object
+ ****************************************************************************/
+Bool RegisterFlockRepeller()
+{
+	return RegisterObjectPlugin(ID_OFLOCKREPELLER, GeLoadString(IDS_OFLOCKREPELLER), 0, FlockRepeller::Alloc, "Oflockrepeller", AutoBitmap("Oflockrepeller.tif"), 0);
+}
diff --git a/source/object/FlockTarget.cpp b/source/object/FlockTarget.cpp
index 3e0bccc..284b46e 100755
--- a/source/object/FlockTarget.cpp
+++ b/source/object/FlockTarget.cpp
@@ -1,100 +1,97 @@
-#include "c4d.h"
-#include "c4d_symbols.h"
-#include "Oflocktarget.h"
-#include "helpers.h"
-#include "main.h"
-
-
-class FlockTarget : public ObjectData
-{
-	INSTANCEOF(FlockTarget, ObjectData)
-
-public:
-	virtual Bool Init(GeListNode *node);
-	virtual Bool GetDEnabling(GeListNode *node, const DescID &id, const GeData &t_data, DESCFLAGS_ENABLE flags, const BaseContainer *itemdesc);
-	virtual DRAWRESULT Draw(BaseObject *op, DRAWPASS drawpass, BaseDraw *bd, BaseDrawHelp *bh);
-
-	static NodeData *Alloc(void)
-	{
-		return NewObjClear(FlockTarget);
-	}
-};
-
-
-Bool FlockTarget::Init(GeListNode *node)
-{
-	if (!node)
-		return false;
-	
-	BaseContainer *bc = (static_cast<BaseObject*>(node))->GetDataInstance();
-	if (!bc)
-		return false;
-
-	bc->SetBool(OFLOCKTARGET_ENABLED, true);
-	bc->SetFloat(OFLOCKTARGET_WEIGHT, 1.0);
-	bc->SetFloat(OFLOCKTARGET_RADIUS, 50.0);
-	bc->SetBool(OFLOCKTARGET_RADIUS_INFINITE, true);
-
-	return SUPER::Init(node);
-}
-
-Bool FlockTarget::GetDEnabling(GeListNode *node, const DescID &id, const GeData &t_data, DESCFLAGS_ENABLE flags, const BaseContainer *itemdesc)
-{
-	if (!node)
-		return false;
-	
-	BaseContainer *bc = (static_cast<BaseObject*>(node))->GetDataInstance();
-	if (!bc)
-		return false;
-
-	switch (id[0].id)
-	{
-		case OFLOCKTARGET_RADIUS_INFINITE:
-		case OFLOCKTARGET_WEIGHT:
-			return bc->GetBool(OFLOCKTARGET_ENABLED, false);
-			break;
-
-		case OFLOCKTARGET_RADIUS:
-			return !bc->GetBool(OFLOCKTARGET_RADIUS_INFINITE, true) && bc->GetBool(OFLOCKTARGET_ENABLED, false);
-			break;
-	}
-
-	return SUPER::GetDEnabling(node, id, t_data, flags, itemdesc);
-}
-
-DRAWRESULT FlockTarget::Draw(BaseObject *op, DRAWPASS drawpass, BaseDraw *bd, BaseDrawHelp *bh)
-{
-	if (drawpass != DRAWPASS_OBJECT)
-		return DRAWRESULT_SKIP;
-	
-	if (!op || !bd || !bh)
-		return DRAWRESULT_ERROR;
-
-	BaseContainer* bc = op->GetDataInstance();
-	if (!bc)
-		return DRAWRESULT_ERROR;
-
-	bd->SetPen(COLOR_FLOCKTARGET * bc->GetFloat(OFLOCKTARGET_WEIGHT));
-
-	bd->SetMatrix_Matrix(op, bh->GetMg());
-	if (bc->GetBool(OFLOCKTARGET_RADIUS_INFINITE))
-	{
-		Draw3DCross(bd, 50.0);
-	}
-	else
-	{
-		DrawSphere(bd, (Float32)bc->GetFloat(OFLOCKTARGET_RADIUS, 50.0));
-	}
-	bd->SetMatrix_Matrix(nullptr, Matrix());
-
-	return DRAWRESULT_OK;
-}
-
-
-/****************************************************************************
- * Register Plugin Object
- ****************************************************************************/
-Bool RegisterFlockTarget()
-{
-	return RegisterObjectPlugin(ID_OFLOCKTARGET, GeLoadString(IDS_OFLOCKTARGET), 0, FlockTarget::Alloc, "Oflocktarget", AutoBitmap("Oflocktarget.tif"), 0);
-}
+#include "c4d.h"
+#include "c4d_symbols.h"
+#include "Oflocktarget.h"
+#include "helpers.h"
+#include "main.h"
+
+
+class FlockTarget : public ObjectData
+{
+	INSTANCEOF(FlockTarget, ObjectData)
+
+public:
+	virtual Bool Init(GeListNode *node);
+	virtual Bool GetDEnabling(GeListNode *node, const DescID &id, const GeData &t_data, DESCFLAGS_ENABLE flags, const BaseContainer *itemdesc);
+	virtual DRAWRESULT Draw(BaseObject *op, DRAWPASS drawpass, BaseDraw *bd, BaseDrawHelp *bh);
+
+	static NodeData *Alloc()
+	{
+		return NewObjClear(FlockTarget);
+	}
+};
+
+
+Bool FlockTarget::Init(GeListNode *node)
+{
+	if (!node)
+		return false;
+	
+	BaseContainer *bc = (static_cast<BaseObject*>(node))->GetDataInstance();
+	if (!bc)
+		return false;
+
+	bc->SetBool(OFLOCKTARGET_ENABLED, true);
+	bc->SetFloat(OFLOCKTARGET_WEIGHT, 1.0);
+	bc->SetFloat(OFLOCKTARGET_RADIUS, 50.0);
+	bc->SetBool(OFLOCKTARGET_RADIUS_INFINITE, true);
+
+	return SUPER::Init(node);
+}
+
+Bool FlockTarget::GetDEnabling(GeListNode *node, const DescID &id, const GeData &t_data, DESCFLAGS_ENABLE flags, const BaseContainer *itemdesc)
+{
+	if (!node)
+		return false;
+	
+	BaseContainer *bc = (static_cast<BaseObject*>(node))->GetDataInstance();
+	if (!bc)
+		return false;
+
+	switch (id[0].id)
+	{
+		case OFLOCKTARGET_RADIUS_INFINITE:
+		case OFLOCKTARGET_WEIGHT:
+			return bc->GetBool(OFLOCKTARGET_ENABLED, false);
+			break;
+
+		case OFLOCKTARGET_RADIUS:
+			return !bc->GetBool(OFLOCKTARGET_RADIUS_INFINITE, true) && bc->GetBool(OFLOCKTARGET_ENABLED, false);
+			break;
+	}
+
+	return SUPER::GetDEnabling(node, id, t_data, flags, itemdesc);
+}
+
+DRAWRESULT FlockTarget::Draw(BaseObject *op, DRAWPASS drawpass, BaseDraw *bd, BaseDrawHelp *bh)
+{
+	if (drawpass != DRAWPASS_OBJECT)
+		return DRAWRESULT_SKIP;
+	
+	if (!op || !bd || !bh)
+		return DRAWRESULT_ERROR;
+
+	BaseContainer* bc = op->GetDataInstance();
+	if (!bc)
+		return DRAWRESULT_ERROR;
+
+	bd->SetPen(COLOR_FLOCKTARGET * bc->GetFloat(OFLOCKTARGET_WEIGHT));
+
+	bd->SetMatrix_Matrix(op, bh->GetMg());
+	if (bc->GetBool(OFLOCKTARGET_RADIUS_INFINITE))
+		Draw3DCross(bd, 50.0);
+	else
+		DrawSphere(bd, bc->GetFloat(OFLOCKTARGET_RADIUS, 50.0));
+	
+	bd->SetMatrix_Matrix(nullptr, Matrix());
+
+	return DRAWRESULT_OK;
+}
+
+
+//
+// Register Plugin Object
+//
+Bool RegisterFlockTarget()
+{
+	return RegisterObjectPlugin(ID_OFLOCKTARGET, GeLoadString(IDS_OFLOCKTARGET), 0, FlockTarget::Alloc, "Oflocktarget", AutoBitmap("Oflocktarget.tif"), 0);
+}

From 9b21a365a0b04075d25cd79490fb952db250a7d7 Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 09:30:13 +0200
Subject: [PATCH 02/15] Comment formatting

---
 source/object/FlockModifier.cpp | 6 +++---
 source/object/FlockRepeller.cpp | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/source/object/FlockModifier.cpp b/source/object/FlockModifier.cpp
index 1dee854..f9343fe 100755
--- a/source/object/FlockModifier.cpp
+++ b/source/object/FlockModifier.cpp
@@ -498,9 +498,9 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 }
 
 
-/****************************************************************************
- * Register Plugin Object
- ****************************************************************************/
+//
+// Register Plugin Object
+//
 Bool RegisterFlockModifier()
 {
 	return RegisterObjectPlugin(ID_OFLOCKMODIFIER, GeLoadString(IDS_OFLOCK), OBJECT_PARTICLEMODIFIER, FlockModifier::Alloc, "Oflock", AutoBitmap("Oflock.tif"), 0);
diff --git a/source/object/FlockRepeller.cpp b/source/object/FlockRepeller.cpp
index 740e6e1..e04cf9e 100755
--- a/source/object/FlockRepeller.cpp
+++ b/source/object/FlockRepeller.cpp
@@ -78,9 +78,9 @@ DRAWRESULT FlockRepeller::Draw(BaseObject *op, DRAWPASS drawpass, BaseDraw *bd,
 }
 
 
-/****************************************************************************
- * Register Plugin Object
- ****************************************************************************/
+//
+// Register Plugin Object
+//
 Bool RegisterFlockRepeller()
 {
 	return RegisterObjectPlugin(ID_OFLOCKREPELLER, GeLoadString(IDS_OFLOCKREPELLER), 0, FlockRepeller::Alloc, "Oflockrepeller", AutoBitmap("Oflockrepeller.tif"), 0);

From 12e348734c777a9b0d8a33cfd6759b5c2c32aebc Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 09:33:11 +0200
Subject: [PATCH 03/15] More code style changes

---
 source/object/FlockModifier.cpp | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/source/object/FlockModifier.cpp b/source/object/FlockModifier.cpp
index f9343fe..96586b2 100755
--- a/source/object/FlockModifier.cpp
+++ b/source/object/FlockModifier.cpp
@@ -296,7 +296,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	for (i = 0; i < pcnt; ++i)
 	{
 		// Skip unwanted particles
-		if (!(pp[i].bits & PARTICLEFLAGS_VISIBLE))
+		if (!(pp[i].bits&PARTICLEFLAGS_VISIBLE))
 			continue;
 
 		// Reset values
@@ -309,7 +309,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 		for (j = 0; j < pcnt; ++j)
 		{
 			// Skip unwanted particles
-			if (!(pp[j].bits & PARTICLEFLAGS_VISIBLE) || i == j)
+			if (!(pp[j].bits&PARTICLEFLAGS_VISIBLE) || i == j)
 				continue;
 
 			// General stuff for particle interaction
@@ -325,14 +325,14 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			
 			// Flock Center
 			// ------------
-			if (rulemask & RULEFLAGS_CENTER)
+			if (rulemask&RULEFLAGS_CENTER)
 			{
 				vCenterflockDir += pp[j].off;
 			}
 
 			// Neighbor Distance
 			// -----------------
-			if (rulemask & RULEFLAGS_NEIGHBORDIST)
+			if (rulemask&RULEFLAGS_NEIGHBORDIST)
 			{
 				if (rNeighborDist < rNeighborMinDist)
 					vNeighborDir -= vNeighborDiff;
@@ -340,7 +340,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 
 			// Match Velocity
 			// --------------
-			if (rulemask & RULEFLAGS_MATCHVELO)
+			if (rulemask&RULEFLAGS_MATCHVELO)
 			{
 				vMatchVelocityDir += pp[j].v3;
 			}
@@ -356,14 +356,14 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 
 			// Flock Center
 			// ------------
-			if (rulemask & RULEFLAGS_CENTER)
+			if (rulemask&RULEFLAGS_CENTER)
 			{
 				vParticleDir += ((vCenterflockDir / lCount) - pp[i].off) * rCenterflockWeight;
 			}
 
 			// Neighbor Distance
 			// -----------------
-			if (rulemask & RULEFLAGS_NEIGHBORDIST)
+			if (rulemask&RULEFLAGS_NEIGHBORDIST)
 			{
 				vParticleDir += vNeighborDir * rNeighborWeight;
 			}
@@ -389,21 +389,21 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 
 			// Match Velocity
 			// --------------
-			if (rulemask & RULEFLAGS_MATCHVELO)
+			if (rulemask&RULEFLAGS_MATCHVELO)
 			{
 				vParticleDir += ((vMatchVelocityDir / lCount) - pp[i].v3) * rMatchVelocityWeight;
 			}
 
 			// Turbulence
 			// ----------
-			if (rulemask & RULEFLAGS_TURBULENCE)
+			if (rulemask&RULEFLAGS_TURBULENCE)
 			{
 				vParticleDir += Vector(SNoise(rTurbulenceScale * pp[i].off, rTurbulenceTime), SNoise(rTurbulenceScale * pp[i].off + rTurbulenceAdd1, rTurbulenceTime), SNoise(rTurbulenceScale * pp[i].off + rTurbulenceAdd2, rTurbulenceTime)) * rTurbulenceWeight;
 			}
 
 			// Repell
 			// ------
-			if (rulemask  & RULEFLAGS_REPELL)
+			if (rulemask&RULEFLAGS_REPELL)
 			{
 				for (n = 0; n < repellerData.GetCount(); ++n)
 				{
@@ -414,20 +414,20 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 				}
 			}
 
-			// Add resulting to current velocity
-			vParticleDir = pp[i].v3 + vParticleDir;
+			// Add resulting direction to current velocity
+			vParticleDir += pp[i].v3;
 
 
 			// Level Flight
 			// ------------
-			if (rulemask & RULEFLAGS_LEVELFLIGHT)
+			if (rulemask&RULEFLAGS_LEVELFLIGHT)
 			{
 				vParticleDir.y -= vParticleDir.y * rLevelFlightWeight;
 			}
 
 			// Avoid Geometry
 			// --------------
-			if (rulemask & RULEFLAGS_AVOIDGEO)
+			if (rulemask&RULEFLAGS_AVOIDGEO)
 			{
 				if (_geoAvoidanceCollider->Intersect(mAvoidGeoI * pp[i].off, mAvoidGeoI.TransformVector(!vParticleDir), rAvoidGeoDist))
 				{
@@ -455,7 +455,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			// Speed Limits
 			// ------------
 			rSpeed = vParticleDir.GetSquaredLength() * diff;
-			if (rulemask & RULEFLAGS_SPEEDLIMIT)
+			if (rulemask&RULEFLAGS_SPEEDLIMIT)
 			{
 				switch (lSpeedMode)
 				{

From 4e787a3954230ea085c2d25119f0efe94572458d Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 09:39:41 +0200
Subject: [PATCH 04/15] Minor optimizations for rAvoidGeoDist

---
 source/object/FlockModifier.cpp | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/source/object/FlockModifier.cpp b/source/object/FlockModifier.cpp
index 96586b2..5250e01 100755
--- a/source/object/FlockModifier.cpp
+++ b/source/object/FlockModifier.cpp
@@ -154,6 +154,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	Int32 lAvoidGeoMode = bc->GetInt32(OFLOCK_AVOIDGEO_MODE, 1);
 	Float rAvoidGeoWeight = bc->GetFloat(OFLOCK_AVOIDGEO_WEIGHT, 0.0);
 	Float rAvoidGeoDist = bc->GetFloat(OFLOCK_AVOIDGEO_DIST, 0.0);
+	Float rAvoidGeoDistI = 1.0 / FMax(rAvoidGeoDist, EPSILON);
 	Vector vAvoidGeoDir(DC);
 	BaseObject* boAvoidGeoLink = bc->GetObjectLink(OFLOCK_AVOIDGEO_LINK, doc);
 	Matrix mAvoidGeo(DC), mAvoidGeoI(DC);
@@ -435,7 +436,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 
 					if (_geoAvoidanceCollider->GetNearestIntersection(&colliderResult))
 					{
-						rAvoidGeoMixval = 1.0 - colliderResult.distance / rAvoidGeoDist;
+						rAvoidGeoMixval = 1.0 - colliderResult.distance * rAvoidGeoDistI;
 						switch (lAvoidGeoMode)
 						{
 							case OFLOCK_AVOIDGEO_MODE_SOFT:
@@ -454,21 +455,21 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 
 			// Speed Limits
 			// ------------
-			rSpeed = vParticleDir.GetSquaredLength() * diff;
 			if (rulemask&RULEFLAGS_SPEEDLIMIT)
 			{
+				rSpeed = vParticleDir.GetSquaredLength() * diff;
 				switch (lSpeedMode)
 				{
 					case OFLOCK_SPEED_MODE_SOFT:
 						{
 							if (rSpeed < rSpeedMin)
 							{
-								rSpeedRatio = rSpeedMin / FMax(rSpeed, 0.001);
+								rSpeedRatio = rSpeedMin / FMax(rSpeed, EPSILON);
 								vParticleDir *= Blend(1.0, rSpeedRatio, rSpeedWeight);
 							}
 							else if (rSpeed > rSpeedMax)
 							{
-								rSpeedRatio = rSpeedMax / FMax(rSpeed, 0.001);
+								rSpeedRatio = rSpeedMax / FMax(rSpeed, EPSILON);
 								vParticleDir *= Blend(1.0, rSpeedRatio, rSpeedWeight);
 							}
 						}

From 449ff652b85ae2e2f60e4ef34e5e99f9808b6f68 Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 09:58:18 +0200
Subject: [PATCH 05/15] More cleanup: Datatypes. More optimizations:
 Pre-calculate inverse values and change divisions to multiplications.

---
 source/helpers.h                |  16 ++--
 source/object/FlockModifier.cpp | 159 ++++++++++++++++----------------
 2 files changed, 91 insertions(+), 84 deletions(-)

diff --git a/source/helpers.h b/source/helpers.h
index 07409a1..93988fc 100755
--- a/source/helpers.h
+++ b/source/helpers.h
@@ -29,8 +29,9 @@ enum RULEFLAGS
 /// Data for a FlockTarget
 struct TargetData
 {
-	Float32 _weight;    ///< Weight of this target
-	Float32 _radius;    ///< Radius of this target
+	Float   _weight;    ///< Weight of this target
+	Float   _radius;    ///< Radius of this target
+	Float   _radiusI;   ///< Inverted _radius (for performance reasons)
 	Bool    _infinite;  ///< Is target radius infinite?
 	Vector  _position;  ///< Global position of target
 
@@ -41,7 +42,7 @@ struct TargetData
 		_position(0.0)
 	{ }
 	
-	TargetData(Float32 weight, Float32 radius, Bool infinite, const Vector &position) :
+	TargetData(Float weight, Float radius, Bool infinite, const Vector &position) :
 		_weight(weight),
 		_radius(radius),
 		_infinite(infinite),
@@ -53,9 +54,10 @@ struct TargetData
 /// Data for a FlockRepeller
 struct RepellerData
 {
-	Float32 _weight;
-	Float32 _radius;
-	Vector  _position;
+	Float   _weight;    ///< Weight of this repeller
+	Float   _radius;    ///< Radius of this repeller
+	Float   _radiusI;   ///< Inverted _radius (for performance reasons)
+	Vector  _position;  ///< Global position of repeller
 
 	RepellerData() :
 		_weight(0.0),
@@ -63,7 +65,7 @@ struct RepellerData
 		_position(0.0)
 	{ }
 	
-	RepellerData(Float32 weight, Float32 radius, const Vector &position) :
+	RepellerData(Float weight, Float radius, const Vector &position) :
 		_weight(weight),
 		_radius(radius),
 		_position(position)
diff --git a/source/object/FlockModifier.cpp b/source/object/FlockModifier.cpp
index 5250e01..1e057a6 100755
--- a/source/object/FlockModifier.cpp
+++ b/source/object/FlockModifier.cpp
@@ -110,117 +110,118 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	Vector vParticleDir(DC);
 
 	// Overall weight
-	Float rWeight = bc->GetFloat(OFLOCK_WEIGHT, 1.0);
+	Float fWeight = bc->GetFloat(OFLOCK_WEIGHT, 1.0);
 
 	// Range of sight
-	Float rNeighborDist = 0.0;
-	Float rNeighborSightRadius = bc->GetFloat(OFLOCK_SIGHT, 0.0);
-	rNeighborSightRadius *= rNeighborSightRadius;		// Square
+	Float fNeighborDist = 0.0;
+	Float fNeighborSightRadius = bc->GetFloat(OFLOCK_SIGHT, 0.0);
+	fNeighborSightRadius *= fNeighborSightRadius;		// Square
 
 	// Flock Center
 	Vector vCenterflockDir(DC);
-	Float rCenterflockWeight = bc->GetFloat(OFLOCK_CENTER_WEIGHT, 0.0) * 0.1;
+	Float fCenterflockWeight = bc->GetFloat(OFLOCK_CENTER_WEIGHT, 0.0) * 0.1;
 
 	// Neighbor Distance
 	Vector vNeighborDiff(DC);
 	Vector vNeighborDir(DC);
-	Float rNeighborWeight = bc->GetFloat(OFLOCK_NEIGHBORDIST_WEIGHT, 0.0) * 0.1;
-	Float rNeighborMinDist = bc->GetFloat(OFLOCK_NEIGHBORDIST_DIST, 0.0);
+	Float fNeighborWeight = bc->GetFloat(OFLOCK_NEIGHBORDIST_WEIGHT, 0.0) * 0.1;
+	Float fNeighborMinDist = bc->GetFloat(OFLOCK_NEIGHBORDIST_DIST, 0.0);
 
 	// Match Velocity
 	Vector vMatchVelocityDir(DC);
-	Float rMatchVelocityWeight = bc->GetFloat(OFLOCK_MATCHVEL_WEIGHT, 0.0) * 0.1;
+	Float fMatchVelocityWeight = bc->GetFloat(OFLOCK_MATCHVEL_WEIGHT, 0.0) * 0.1;
 
 	// Target
-	Float rTargetGlobalWeight = bc->GetFloat(OFLOCK_TARGET_WEIGHT, 0.0) * 0.1;
+	Float fTargetGlobalWeight = bc->GetFloat(OFLOCK_TARGET_WEIGHT, 0.0) * 0.1;
 	InExcludeData* inexTarget = (InExcludeData*)bc->GetCustomDataType(OFLOCK_TARGET_LINK, CUSTOMDATATYPE_INEXCLUDE_LIST);
 	Int32 lTargetCount = 0;
 	if (inexTarget)
 		lTargetCount = inexTarget->GetObjectCount();
-	maxon::BaseArray<TargetData>targetData;
+	maxon::BaseArray<TargetData> targetData;
 
 	// Level flight
-	Float rLevelFlightWeight = bc->GetFloat(OFLOCK_LEVELFLIGHT_WEIGHT, 0.0);
+	Float fLevelFlightWeight = bc->GetFloat(OFLOCK_LEVELFLIGHT_WEIGHT, 0.0);
 
 	// Speed limits
-	Float rSpeed = 0.0;
+	Float fSpeed = 0.0;
 	Int32 lSpeedMode = bc->GetInt32(OFLOCK_SPEED_MODE, OFLOCK_SPEED_MODE_SOFT);
-	Float rSpeedWeight = bc->GetFloat(OFLOCK_SPEED_WEIGHT, 0.0);
-	Float rSpeedMin = bc->GetFloat(OFLOCK_SPEED_MIN, 0.0) * diff;
-	Float rSpeedMax = bc->GetFloat(OFLOCK_SPEED_MAX, 100.0) * diff;
-	Float rSpeedRatio;
+	Float fSpeedWeight = bc->GetFloat(OFLOCK_SPEED_WEIGHT, 0.0);
+	Float fSpeedMin = bc->GetFloat(OFLOCK_SPEED_MIN, 0.0) * diff;
+	Float fSpeedMax = bc->GetFloat(OFLOCK_SPEED_MAX, 100.0) * diff;
+	Float fSpeedRatio = 0.0;
 
 	// Geometry avoidance
 	Int32 lAvoidGeoMode = bc->GetInt32(OFLOCK_AVOIDGEO_MODE, 1);
-	Float rAvoidGeoWeight = bc->GetFloat(OFLOCK_AVOIDGEO_WEIGHT, 0.0);
-	Float rAvoidGeoDist = bc->GetFloat(OFLOCK_AVOIDGEO_DIST, 0.0);
-	Float rAvoidGeoDistI = 1.0 / FMax(rAvoidGeoDist, EPSILON);
+	Float fAvoidGeoWeight = bc->GetFloat(OFLOCK_AVOIDGEO_WEIGHT, 0.0);
+	Float fAvoidGeoDist = bc->GetFloat(OFLOCK_AVOIDGEO_DIST, 0.0);
+	Float fAvoidGeoDistI = 1.0 / FMax(fAvoidGeoDist, EPSILON);
 	Vector vAvoidGeoDir(DC);
 	BaseObject* boAvoidGeoLink = bc->GetObjectLink(OFLOCK_AVOIDGEO_LINK, doc);
 	Matrix mAvoidGeo(DC), mAvoidGeoI(DC);
-	Float rAvoidGeoMixval;
+	Float fAvoidGeoMixval = 0.0;
 
 	// Turbulence
-	Float rTurbulenceWeight = bc->GetFloat(OFLOCK_TURBULENCE_WEIGHT, 0.0) * 10.0;
-	Float rTurbulenceTime = doc->GetTime().Get() * bc->GetFloat(OFLOCK_TURBULENCE_FREQUENCY, 1.0);
-	Float rTurbulenceScale = 0.1 / ClampValue(bc->GetFloat(OFLOCK_TURBULENCE_SCALE, 1.0), 0.0001, MAXRANGE_FLOAT);
-	Vector rTurbulenceAdd1(DC);
-	Vector rTurbulenceAdd2(DC);
+	Float fTurbulenceWeight = bc->GetFloat(OFLOCK_TURBULENCE_WEIGHT, 0.0) * 10.0;
+	Float fTurbulenceTime = doc->GetTime().Get() * bc->GetFloat(OFLOCK_TURBULENCE_FREQUENCY, 1.0);
+	Float fTurbulenceScale = 0.1 / ClampValue(bc->GetFloat(OFLOCK_TURBULENCE_SCALE, 1.0), 0.0001, MAXRANGE_FLOAT);
+	Vector vTurbulenceAdd1(DC);
+	Vector vTurbulenceAdd2(DC);
 
 	// Repell
-	Float rRepellGlobalWeight = bc->GetFloat(OFLOCK_REPELL_WEIGHT, 0.0);
+	Float fRepellGlobalWeight = bc->GetFloat(OFLOCK_REPELL_WEIGHT, 0.0);
 	InExcludeData* inexRepell = (InExcludeData*)bc->GetCustomDataType(OFLOCK_REPELL_LINK, CUSTOMDATATYPE_INEXCLUDE_LIST);
 	Int32 lRepellCount = 0;
-	if (inexRepell) lRepellCount = inexRepell->GetObjectCount();
-	maxon::BaseArray<RepellerData>repellerData;
+	if (inexRepell)
+		lRepellCount = inexRepell->GetObjectCount();
+	maxon::BaseArray<RepellerData> repellerData;
 
 
 	/* ------------------- Set rule mask & prepare some data --------------------------- */ 
 	RULEFLAGS rulemask = RULEFLAGS_NONE;
 
 	// Center Flock
-	if (!CompareFloatTolerant(rCenterflockWeight, 0.0))
+	if (!CompareFloatTolerant(fCenterflockWeight, 0.0))
 	{
 		rulemask |= RULEFLAGS_CENTER;
 	}
 
 	// Neighbor Distance
-	if (!CompareFloatTolerant(rNeighborWeight, 0.0))
+	if (!CompareFloatTolerant(fNeighborWeight, 0.0))
 	{
 		rulemask |= RULEFLAGS_NEIGHBORDIST;
-		rNeighborMinDist *= rNeighborMinDist;		// Square
+		fNeighborMinDist *= fNeighborMinDist;		// Square
 	}
 
 	// Match Flock Velocity
-	if (!CompareFloatTolerant(rMatchVelocityWeight, 0.0))
+	if (!CompareFloatTolerant(fMatchVelocityWeight, 0.0))
 	{
 		rulemask |= RULEFLAGS_MATCHVELO;
 	}
 
 	// Level Flight
-	if (!CompareFloatTolerant(rLevelFlightWeight, 0.0))
+	if (!CompareFloatTolerant(fLevelFlightWeight, 0.0))
 	{
 		rulemask |= RULEFLAGS_LEVELFLIGHT;
 	}
 
 	// Turbulence
-	if (!CompareFloatTolerant(rTurbulenceWeight, 0.0))
+	if (!CompareFloatTolerant(fTurbulenceWeight, 0.0))
 	{
 		rulemask |= RULEFLAGS_TURBULENCE;
-		rTurbulenceAdd1 = Vector(PI * 1000.0);
-		rTurbulenceAdd2 = Vector(-PI05 * 1000.0);
+		vTurbulenceAdd1 = Vector(PI * 1000.0);
+		vTurbulenceAdd2 = Vector(-PI05 * 1000.0);
 	}
 
 	// Speed Limit
-	if (((lSpeedMode == OFLOCK_SPEED_MODE_SOFT && !CompareFloatTolerant(rSpeedWeight, 0.0)) || lSpeedMode == OFLOCK_SPEED_MODE_HARD))
+	if (((lSpeedMode == OFLOCK_SPEED_MODE_SOFT && !CompareFloatTolerant(fSpeedWeight, 0.0)) || lSpeedMode == OFLOCK_SPEED_MODE_HARD))
 	{
 		rulemask |= RULEFLAGS_SPEEDLIMIT;
-		rSpeedMin *= rSpeedMin;   // Square
-		rSpeedMax *= rSpeedMax;   // Square
+		fSpeedMin *= fSpeedMin;   // Square
+		fSpeedMax *= fSpeedMax;   // Square
 	}
 
 	// Target
-	if (!CompareFloatTolerant(rTargetGlobalWeight, 0.0) && inexTarget && lTargetCount > 0)
+	if (!CompareFloatTolerant(fTargetGlobalWeight, 0.0) && inexTarget && lTargetCount > 0)
 	{
 		rulemask |= RULEFLAGS_TARGET;
 
@@ -236,11 +237,12 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 				tc = opListItem->GetDataInstance();
 				if (tc && tc->GetBool(OFLOCKTARGET_ENABLED))
 				{
-					TargetData tdata((Float32)tc->GetFloat(OFLOCKREPELLER_WEIGHT, 1.0),
-					                 (Float32)tc->GetFloat(OFLOCKTARGET_RADIUS, 0.0),
+					TargetData tdata(tc->GetFloat(OFLOCKREPELLER_WEIGHT, 1.0),
+					                 tc->GetFloat(OFLOCKTARGET_RADIUS, 0.0),
 					                 tc->GetBool(OFLOCKTARGET_RADIUS_INFINITE, true),
 					                 opListItem->GetMg().off);
-					tdata._radius *= tdata._radius;   // Square
+					tdata._radius *= tdata._radius;  // Square radius
+					tdata._radiusI = 1.0 / FMax(tdata._radius, EPSILON);  // Calculate inverse radius
 					targetData.Append(tdata);
 				}
 			}
@@ -248,7 +250,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	}
 
 	// Avoid Geometry
-	if (((lAvoidGeoMode == OFLOCK_AVOIDGEO_MODE_SOFT && !CompareFloatTolerant(rAvoidGeoWeight, 0.0)) || lAvoidGeoMode == OFLOCK_AVOIDGEO_MODE_HARD) && !CompareFloatTolerant(rAvoidGeoDist, 0.0) && boAvoidGeoLink && boAvoidGeoLink->GetType() == Opolygon && ToPoly(boAvoidGeoLink)->GetPolygonCount() > 0)
+	if (((lAvoidGeoMode == OFLOCK_AVOIDGEO_MODE_SOFT && !CompareFloatTolerant(fAvoidGeoWeight, 0.0)) || lAvoidGeoMode == OFLOCK_AVOIDGEO_MODE_HARD) && !CompareFloatTolerant(fAvoidGeoDist, 0.0) && boAvoidGeoLink && boAvoidGeoLink->GetType() == Opolygon && ToPoly(boAvoidGeoLink)->GetPolygonCount() > 0)
 	{
 		rulemask |= RULEFLAGS_AVOIDGEO;
 		
@@ -266,12 +268,12 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	}
 
 	// Repelling
-	if (!CompareFloatTolerant(rRepellGlobalWeight, 0.0) && inexRepell && lRepellCount > 0)
+	if (!CompareFloatTolerant(fRepellGlobalWeight, 0.0) && inexRepell && lRepellCount > 0)
 	{
 		rulemask |= RULEFLAGS_REPELL;
 
-		BaseObject* opListItem = nullptr;
-		BaseContainer* tc = nullptr;
+		BaseObject *opListItem = nullptr;
+		BaseContainer *tc = nullptr;
 
 		repellerData.Flush();
 		for (i = 0; i < lRepellCount; ++i)
@@ -282,10 +284,11 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 				tc = opListItem->GetDataInstance();
 				if (tc && tc->GetBool(OFLOCKREPELLER_ENABLED))
 				{
-					RepellerData rdata((Float32)tc->GetFloat(OFLOCKREPELLER_WEIGHT, 1.0),
-					                   (Float32)tc->GetFloat(OFLOCKREPELLER_RADIUS, 0.0),
+					RepellerData rdata(tc->GetFloat(OFLOCKREPELLER_WEIGHT, 1.0),
+					                   tc->GetFloat(OFLOCKREPELLER_RADIUS, 0.0),
 					                   opListItem->GetMg().off);
-					rdata._radius *= rdata._radius;   // Square
+					rdata._radius *= rdata._radius;  // Square radius
+					rdata._radiusI = 1.0 / FMax(rdata._radius, EPSILON);  // Calculate inverse radius
 					repellerData.Append(rdata);
 				}
 			}
@@ -318,10 +321,10 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 
 			// Get distance to current particle
 			vNeighborDiff = pp[j].off - pp[i].off;
-			rNeighborDist = vNeighborDiff.GetSquaredLength();
+			fNeighborDist = vNeighborDiff.GetSquaredLength();
 
 			// Skip if particles too far away from each other
-			if (rNeighborDist > rNeighborSightRadius)
+			if (fNeighborDist > fNeighborSightRadius)
 				continue;
 			
 			// Flock Center
@@ -335,7 +338,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			// -----------------
 			if (rulemask&RULEFLAGS_NEIGHBORDIST)
 			{
-				if (rNeighborDist < rNeighborMinDist)
+				if (fNeighborDist < fNeighborMinDist)
 					vNeighborDir -= vNeighborDiff;
 			}
 
@@ -354,19 +357,21 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 		if (lCount > 1)
 		{
 			/* ------------------- Soft Rules --------------------------- */
+			
+			Float fCountI = 1.0 / FMax(lCount, EPSILON);
 
 			// Flock Center
 			// ------------
 			if (rulemask&RULEFLAGS_CENTER)
 			{
-				vParticleDir += ((vCenterflockDir / lCount) - pp[i].off) * rCenterflockWeight;
+				vParticleDir += ((vCenterflockDir * fCountI) - pp[i].off) * fCenterflockWeight;
 			}
 
 			// Neighbor Distance
 			// -----------------
 			if (rulemask&RULEFLAGS_NEIGHBORDIST)
 			{
-				vParticleDir += vNeighborDir * rNeighborWeight;
+				vParticleDir += vNeighborDir * fNeighborWeight;
 			}
 
 			// Target
@@ -379,11 +384,11 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 					Float distLength = dist.GetSquaredLength();
 					if (targetData[n]._infinite)
 					{
-						vParticleDir += dist * targetData[n]._weight * rTargetGlobalWeight;
+						vParticleDir += dist * targetData[n]._weight * fTargetGlobalWeight;
 					}
 					else if (distLength < targetData[n]._radius)
 					{
-						vParticleDir += dist * (1.0 - distLength / targetData[n]._radius) * targetData[n]._weight * rTargetGlobalWeight;
+						vParticleDir += dist * (1.0 - distLength * targetData[n]._radiusI) * targetData[n]._weight * fTargetGlobalWeight;
 					}
 				}
 			}
@@ -392,14 +397,14 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			// --------------
 			if (rulemask&RULEFLAGS_MATCHVELO)
 			{
-				vParticleDir += ((vMatchVelocityDir / lCount) - pp[i].v3) * rMatchVelocityWeight;
+				vParticleDir += ((vMatchVelocityDir * fCountI) - pp[i].v3) * fMatchVelocityWeight;
 			}
 
 			// Turbulence
 			// ----------
 			if (rulemask&RULEFLAGS_TURBULENCE)
 			{
-				vParticleDir += Vector(SNoise(rTurbulenceScale * pp[i].off, rTurbulenceTime), SNoise(rTurbulenceScale * pp[i].off + rTurbulenceAdd1, rTurbulenceTime), SNoise(rTurbulenceScale * pp[i].off + rTurbulenceAdd2, rTurbulenceTime)) * rTurbulenceWeight;
+				vParticleDir += Vector(SNoise(fTurbulenceScale * pp[i].off, fTurbulenceTime), SNoise(fTurbulenceScale * pp[i].off + vTurbulenceAdd1, fTurbulenceTime), SNoise(fTurbulenceScale * pp[i].off + vTurbulenceAdd2, fTurbulenceTime)) * fTurbulenceWeight;
 			}
 
 			// Repell
@@ -411,7 +416,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 					Vector dist = repellerData[n]._position - pp[i].off;
 					Float distLength = dist.GetSquaredLength();
 					if (distLength < repellerData[n]._radius)
-						vParticleDir -= dist * (1.0 - distLength / repellerData[n]._radius) * repellerData[n]._weight * rRepellGlobalWeight;
+						vParticleDir -= dist * (1.0 - distLength * repellerData[n]._radiusI) * repellerData[n]._weight * fRepellGlobalWeight;
 				}
 			}
 
@@ -423,29 +428,29 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			// ------------
 			if (rulemask&RULEFLAGS_LEVELFLIGHT)
 			{
-				vParticleDir.y -= vParticleDir.y * rLevelFlightWeight;
+				vParticleDir.y -= vParticleDir.y * fLevelFlightWeight;
 			}
 
 			// Avoid Geometry
 			// --------------
 			if (rulemask&RULEFLAGS_AVOIDGEO)
 			{
-				if (_geoAvoidanceCollider->Intersect(mAvoidGeoI * pp[i].off, mAvoidGeoI.TransformVector(!vParticleDir), rAvoidGeoDist))
+				if (_geoAvoidanceCollider->Intersect(mAvoidGeoI * pp[i].off, mAvoidGeoI.TransformVector(!vParticleDir), fAvoidGeoDist))
 				{
 					GeRayColResult colliderResult;
 
 					if (_geoAvoidanceCollider->GetNearestIntersection(&colliderResult))
 					{
-						rAvoidGeoMixval = 1.0 - colliderResult.distance * rAvoidGeoDistI;
+						fAvoidGeoMixval = 1.0 - colliderResult.distance * fAvoidGeoDistI;
 						switch (lAvoidGeoMode)
 						{
 							case OFLOCK_AVOIDGEO_MODE_SOFT:
-								vParticleDir = vParticleDir + mAvoidGeo.TransformVector(!colliderResult.s_normal) * vParticleDir.GetLength() * rAvoidGeoMixval * rAvoidGeoWeight;
+								vParticleDir = vParticleDir + mAvoidGeo.TransformVector(!colliderResult.s_normal) * vParticleDir.GetLength() * fAvoidGeoMixval * fAvoidGeoWeight;
 								break;
 
 							default:
 							case OFLOCK_AVOIDGEO_MODE_HARD:
-								vParticleDir = Blend(mAvoidGeo.TransformVector((!colliderResult.s_normal * vParticleDir.GetLength())), vParticleDir, rAvoidGeoMixval);
+								vParticleDir = Blend(mAvoidGeo.TransformVector((!colliderResult.s_normal * vParticleDir.GetLength())), vParticleDir, fAvoidGeoMixval);
 								break;
 						}
 					}
@@ -457,33 +462,33 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			// ------------
 			if (rulemask&RULEFLAGS_SPEEDLIMIT)
 			{
-				rSpeed = vParticleDir.GetSquaredLength() * diff;
+				fSpeed = vParticleDir.GetSquaredLength() * diff;
 				switch (lSpeedMode)
 				{
 					case OFLOCK_SPEED_MODE_SOFT:
 						{
-							if (rSpeed < rSpeedMin)
+							if (fSpeed < fSpeedMin)
 							{
-								rSpeedRatio = rSpeedMin / FMax(rSpeed, EPSILON);
-								vParticleDir *= Blend(1.0, rSpeedRatio, rSpeedWeight);
+								fSpeedRatio = fSpeedMin / FMax(fSpeed, EPSILON);
+								vParticleDir *= Blend(1.0, fSpeedRatio, fSpeedWeight);
 							}
-							else if (rSpeed > rSpeedMax)
+							else if (fSpeed > fSpeedMax)
 							{
-								rSpeedRatio = rSpeedMax / FMax(rSpeed, EPSILON);
-								vParticleDir *= Blend(1.0, rSpeedRatio, rSpeedWeight);
+								fSpeedRatio = fSpeedMax / FMax(fSpeed, EPSILON);
+								vParticleDir *= Blend(1.0, fSpeedRatio, fSpeedWeight);
 							}
 						}
 						break;
 
 					case OFLOCK_SPEED_MODE_HARD:
 						{
-							if (rSpeed < rSpeedMin)
+							if (fSpeed < fSpeedMin)
 							{
-								vParticleDir = !vParticleDir * rSpeedMin;
+								vParticleDir = !vParticleDir * fSpeedMin;
 							}
-							else if (rSpeed > rSpeedMax)
+							else if (fSpeed > fSpeedMax)
 							{
-								vParticleDir = !vParticleDir * rSpeedMax;
+								vParticleDir = !vParticleDir * fSpeedMax;
 							}
 						}
 						break;
@@ -491,7 +496,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			}
 
 			// Add resulting velocity, apply overall weight
-			ss[i].v += Blend(pp[i].v3, vParticleDir, rWeight);
+			ss[i].v += Blend(pp[i].v3, vParticleDir, fWeight);
 		}
 
 		ss[i].count++;

From c89d2cc027e274ec43371fc1658e29db7d06f203 Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 10:03:26 +0200
Subject: [PATCH 06/15] Changed all CompareFloatTolerant() to simple
 comparisons

---
 source/object/FlockModifier.cpp | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/source/object/FlockModifier.cpp b/source/object/FlockModifier.cpp
index 1e057a6..0d7a6e5 100755
--- a/source/object/FlockModifier.cpp
+++ b/source/object/FlockModifier.cpp
@@ -180,32 +180,32 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	RULEFLAGS rulemask = RULEFLAGS_NONE;
 
 	// Center Flock
-	if (!CompareFloatTolerant(fCenterflockWeight, 0.0))
+	if (fCenterflockWeight > 0.0)
 	{
 		rulemask |= RULEFLAGS_CENTER;
 	}
 
 	// Neighbor Distance
-	if (!CompareFloatTolerant(fNeighborWeight, 0.0))
+	if (fNeighborWeight > 0.0)
 	{
 		rulemask |= RULEFLAGS_NEIGHBORDIST;
 		fNeighborMinDist *= fNeighborMinDist;		// Square
 	}
 
 	// Match Flock Velocity
-	if (!CompareFloatTolerant(fMatchVelocityWeight, 0.0))
+	if (fMatchVelocityWeight > 0.0)
 	{
 		rulemask |= RULEFLAGS_MATCHVELO;
 	}
 
 	// Level Flight
-	if (!CompareFloatTolerant(fLevelFlightWeight, 0.0))
+	if (fLevelFlightWeight > 0.0)
 	{
 		rulemask |= RULEFLAGS_LEVELFLIGHT;
 	}
 
 	// Turbulence
-	if (!CompareFloatTolerant(fTurbulenceWeight, 0.0))
+	if (fTurbulenceWeight > 0.0)
 	{
 		rulemask |= RULEFLAGS_TURBULENCE;
 		vTurbulenceAdd1 = Vector(PI * 1000.0);
@@ -213,7 +213,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	}
 
 	// Speed Limit
-	if (((lSpeedMode == OFLOCK_SPEED_MODE_SOFT && !CompareFloatTolerant(fSpeedWeight, 0.0)) || lSpeedMode == OFLOCK_SPEED_MODE_HARD))
+	if (((lSpeedMode == OFLOCK_SPEED_MODE_SOFT && fSpeedWeight > 0.0) || lSpeedMode == OFLOCK_SPEED_MODE_HARD))
 	{
 		rulemask |= RULEFLAGS_SPEEDLIMIT;
 		fSpeedMin *= fSpeedMin;   // Square
@@ -221,7 +221,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	}
 
 	// Target
-	if (!CompareFloatTolerant(fTargetGlobalWeight, 0.0) && inexTarget && lTargetCount > 0)
+	if (fTargetGlobalWeight > 0.0 && inexTarget && lTargetCount > 0)
 	{
 		rulemask |= RULEFLAGS_TARGET;
 
@@ -231,7 +231,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 		targetData.Flush();
 		for (i = 0; i < lTargetCount; ++i)
 		{
-			opListItem = (BaseObject*)inexTarget->ObjectFromIndex(doc, i);
+			opListItem = static_cast<BaseObject*>(inexTarget->ObjectFromIndex(doc, i));
 			if (opListItem)
 			{
 				tc = opListItem->GetDataInstance();
@@ -250,7 +250,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	}
 
 	// Avoid Geometry
-	if (((lAvoidGeoMode == OFLOCK_AVOIDGEO_MODE_SOFT && !CompareFloatTolerant(fAvoidGeoWeight, 0.0)) || lAvoidGeoMode == OFLOCK_AVOIDGEO_MODE_HARD) && !CompareFloatTolerant(fAvoidGeoDist, 0.0) && boAvoidGeoLink && boAvoidGeoLink->GetType() == Opolygon && ToPoly(boAvoidGeoLink)->GetPolygonCount() > 0)
+	if (((lAvoidGeoMode == OFLOCK_AVOIDGEO_MODE_SOFT && fAvoidGeoWeight > 0.0) || lAvoidGeoMode == OFLOCK_AVOIDGEO_MODE_HARD) && fAvoidGeoDist > 0.0 && boAvoidGeoLink && boAvoidGeoLink->GetType() == Opolygon && ToPoly(boAvoidGeoLink)->GetPolygonCount() > 0)
 	{
 		rulemask |= RULEFLAGS_AVOIDGEO;
 		
@@ -268,7 +268,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	}
 
 	// Repelling
-	if (!CompareFloatTolerant(fRepellGlobalWeight, 0.0) && inexRepell && lRepellCount > 0)
+	if (fRepellGlobalWeight > 0.0 && inexRepell && lRepellCount > 0)
 	{
 		rulemask |= RULEFLAGS_REPELL;
 
@@ -278,7 +278,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 		repellerData.Flush();
 		for (i = 0; i < lRepellCount; ++i)
 		{
-			opListItem = (BaseObject*)inexRepell->ObjectFromIndex(doc, i);
+			opListItem = static_cast<BaseObject*>(inexRepell->ObjectFromIndex(doc, i));
 			if (opListItem)
 			{
 				tc = opListItem->GetDataInstance();

From 85665293a758f9effec934a1ad7a30f629c48dfc Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 16:29:09 +0200
Subject: [PATCH 07/15] Replaced all counting for loops with Iterator for
 loops; minor code cleanup (static_cast)

---
 source/object/FlockModifier.cpp | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/source/object/FlockModifier.cpp b/source/object/FlockModifier.cpp
index 0d7a6e5..8f46515 100755
--- a/source/object/FlockModifier.cpp
+++ b/source/object/FlockModifier.cpp
@@ -36,7 +36,7 @@ Bool FlockModifier::Init(GeListNode *node)
 	if (!node)
 		return false;
 
-	BaseContainer *bc = (static_cast<BaseObject*>(node))->GetDataInstance();
+	BaseContainer *bc = static_cast<BaseObject*>(node)->GetDataInstance();
 	if (!bc)
 		return false;
 
@@ -71,7 +71,7 @@ Bool FlockModifier::GetDEnabling(GeListNode *node, const DescID &id,const GeData
 	if (!node)
 		return false;
 	
-	BaseContainer *bc = (static_cast<BaseObject*>(node))->GetDataInstance();
+	BaseContainer *bc = static_cast<BaseObject*>(node)->GetDataInstance();
 	if (!bc)
 		return false;
 	
@@ -378,17 +378,17 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			// ------
 			if (rulemask & RULEFLAGS_TARGET)
 			{
-				for (n = 0; n < targetData.GetCount(); ++n)
+				for (maxon::BaseArray<TargetData>::Iterator target = targetData.Begin(); target != targetData.End(); ++target)
 				{
-					Vector dist = targetData[n]._position - pp[i].off;
+					Vector dist = target->_position - pp[i].off;
 					Float distLength = dist.GetSquaredLength();
-					if (targetData[n]._infinite)
+					if (target->_infinite)
 					{
-						vParticleDir += dist * targetData[n]._weight * fTargetGlobalWeight;
+						vParticleDir += dist * target->_weight * fTargetGlobalWeight;
 					}
-					else if (distLength < targetData[n]._radius)
+					else if (distLength < target->_radius)
 					{
-						vParticleDir += dist * (1.0 - distLength * targetData[n]._radiusI) * targetData[n]._weight * fTargetGlobalWeight;
+						vParticleDir += dist * (1.0 - distLength * target->_radiusI) * target->_weight * fTargetGlobalWeight;
 					}
 				}
 			}
@@ -411,12 +411,12 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			// ------
 			if (rulemask&RULEFLAGS_REPELL)
 			{
-				for (n = 0; n < repellerData.GetCount(); ++n)
+				for (maxon::BaseArray<RepellerData>::Iterator repeller = repellerData.Begin(); repeller != repellerData.End(); ++repeller)
 				{
-					Vector dist = repellerData[n]._position - pp[i].off;
+					Vector dist = repeller->_position - pp[i].off;
 					Float distLength = dist.GetSquaredLength();
-					if (distLength < repellerData[n]._radius)
-						vParticleDir -= dist * (1.0 - distLength * repellerData[n]._radiusI) * repellerData[n]._weight * fRepellGlobalWeight;
+					if (distLength < repeller->_radius)
+						vParticleDir -= dist * (1.0 - distLength * repeller->_radiusI) * repeller->_weight * fRepellGlobalWeight;
 				}
 			}
 

From 226f1664699840969fcd5c0224b823f78ab879e9 Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 16:30:40 +0200
Subject: [PATCH 08/15] Updated changes.txt

---
 changes.txt | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/changes.txt b/changes.txt
index 5c3f9a3..4765bc1 100755
--- a/changes.txt
+++ b/changes.txt
@@ -1,3 +1,7 @@
+0.8
+- Lots of internal optimizations
+- Code style
+
 0.7.1
 - Only internal changes
 - Lazy initialization for GeRayCollider

From 8663ad07b65bdf006e26fa237dc485865506bd54 Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 16:48:32 +0200
Subject: [PATCH 09/15] Optimized loops by using a ref to current element; code
 style

---
 source/object/FlockModifier.cpp | 56 +++++++++++++++++++--------------
 1 file changed, 32 insertions(+), 24 deletions(-)

diff --git a/source/object/FlockModifier.cpp b/source/object/FlockModifier.cpp
index 8f46515..1e1cad2 100755
--- a/source/object/FlockModifier.cpp
+++ b/source/object/FlockModifier.cpp
@@ -97,7 +97,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	if (!op || !pp || !ss)
 		return;
 
-	Int32 i,j,n;
+	Int32 i,j;
 
 	BaseContainer *bc = op->GetDataInstance();
 	BaseDocument *doc = op->GetDocument();
@@ -155,7 +155,6 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	Float fAvoidGeoWeight = bc->GetFloat(OFLOCK_AVOIDGEO_WEIGHT, 0.0);
 	Float fAvoidGeoDist = bc->GetFloat(OFLOCK_AVOIDGEO_DIST, 0.0);
 	Float fAvoidGeoDistI = 1.0 / FMax(fAvoidGeoDist, EPSILON);
-	Vector vAvoidGeoDir(DC);
 	BaseObject* boAvoidGeoLink = bc->GetObjectLink(OFLOCK_AVOIDGEO_LINK, doc);
 	Matrix mAvoidGeo(DC), mAvoidGeoI(DC);
 	Float fAvoidGeoMixval = 0.0;
@@ -189,7 +188,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	if (fNeighborWeight > 0.0)
 	{
 		rulemask |= RULEFLAGS_NEIGHBORDIST;
-		fNeighborMinDist *= fNeighborMinDist;		// Square
+		fNeighborMinDist *= fNeighborMinDist; // Square
 	}
 
 	// Match Flock Velocity
@@ -216,8 +215,8 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	if (((lSpeedMode == OFLOCK_SPEED_MODE_SOFT && fSpeedWeight > 0.0) || lSpeedMode == OFLOCK_SPEED_MODE_HARD))
 	{
 		rulemask |= RULEFLAGS_SPEEDLIMIT;
-		fSpeedMin *= fSpeedMin;   // Square
-		fSpeedMax *= fSpeedMax;   // Square
+		fSpeedMin *= fSpeedMin; // Square
+		fSpeedMax *= fSpeedMax; // Square
 	}
 
 	// Target
@@ -241,8 +240,8 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 					                 tc->GetFloat(OFLOCKTARGET_RADIUS, 0.0),
 					                 tc->GetBool(OFLOCKTARGET_RADIUS_INFINITE, true),
 					                 opListItem->GetMg().off);
-					tdata._radius *= tdata._radius;  // Square radius
-					tdata._radiusI = 1.0 / FMax(tdata._radius, EPSILON);  // Calculate inverse radius
+					tdata._radius *= tdata._radius; // Square radius
+					tdata._radiusI = 1.0 / FMax(tdata._radius, EPSILON); // Calculate inverse radius
 					targetData.Append(tdata);
 				}
 			}
@@ -262,7 +261,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 				return;
 		}
 		
-		_geoAvoidanceCollider->Init(boAvoidGeoLink, boAvoidGeoLink->GetDirty(DIRTYFLAGS_CACHE|DIRTYFLAGS_MATRIX|DIRTYFLAGS_DATA));
+		_geoAvoidanceCollider->Init(boAvoidGeoLink);
 		mAvoidGeo = boAvoidGeoLink->GetMg();
 		mAvoidGeoI = ~mAvoidGeo;
 	}
@@ -287,8 +286,8 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 					RepellerData rdata(tc->GetFloat(OFLOCKREPELLER_WEIGHT, 1.0),
 					                   tc->GetFloat(OFLOCKREPELLER_RADIUS, 0.0),
 					                   opListItem->GetMg().off);
-					rdata._radius *= rdata._radius;  // Square radius
-					rdata._radiusI = 1.0 / FMax(rdata._radius, EPSILON);  // Calculate inverse radius
+					rdata._radius *= rdata._radius; // Square radius
+					rdata._radiusI = 1.0 / FMax(rdata._radius, EPSILON); // Calculate inverse radius
 					repellerData.Append(rdata);
 				}
 			}
@@ -299,8 +298,13 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	// Iterate particles
 	for (i = 0; i < pcnt; ++i)
 	{
+		// Store a ref to the current Particle and current BaseParticle, so we don't have to
+		// do all the pointer arithmetics for pp[i].
+		Particle &currentParticle = pp[i];
+		BaseParticle &currentBaseParticle = ss[i];
+		
 		// Skip unwanted particles
-		if (!(pp[i].bits&PARTICLEFLAGS_VISIBLE))
+		if (!(currentParticle.bits&PARTICLEFLAGS_VISIBLE))
 			continue;
 
 		// Reset values
@@ -312,15 +316,19 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 		// Iterate other particles
 		for (j = 0; j < pcnt; ++j)
 		{
+			// Store a ref to the current other particle, so we don't have to
+			// do all the pointer arithmetics for pp[j].
+			Particle &currentOtherParticle = pp[j];
+
 			// Skip unwanted particles
-			if (!(pp[j].bits&PARTICLEFLAGS_VISIBLE) || i == j)
+			if (!(currentOtherParticle.bits&PARTICLEFLAGS_VISIBLE) || i == j)
 				continue;
 
 			// General stuff for particle interaction
 			// --------------------------------------
 
 			// Get distance to current particle
-			vNeighborDiff = pp[j].off - pp[i].off;
+			vNeighborDiff = currentOtherParticle.off - currentParticle.off;
 			fNeighborDist = vNeighborDiff.GetSquaredLength();
 
 			// Skip if particles too far away from each other
@@ -331,7 +339,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			// ------------
 			if (rulemask&RULEFLAGS_CENTER)
 			{
-				vCenterflockDir += pp[j].off;
+				vCenterflockDir += currentOtherParticle.off;
 			}
 
 			// Neighbor Distance
@@ -346,7 +354,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			// --------------
 			if (rulemask&RULEFLAGS_MATCHVELO)
 			{
-				vMatchVelocityDir += pp[j].v3;
+				vMatchVelocityDir += currentOtherParticle.v3;
 			}
 
 			// Increase counter of considered flockmates
@@ -364,7 +372,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			// ------------
 			if (rulemask&RULEFLAGS_CENTER)
 			{
-				vParticleDir += ((vCenterflockDir * fCountI) - pp[i].off) * fCenterflockWeight;
+				vParticleDir += ((vCenterflockDir * fCountI) - currentParticle.off) * fCenterflockWeight;
 			}
 
 			// Neighbor Distance
@@ -380,7 +388,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			{
 				for (maxon::BaseArray<TargetData>::Iterator target = targetData.Begin(); target != targetData.End(); ++target)
 				{
-					Vector dist = target->_position - pp[i].off;
+					Vector dist = target->_position - currentParticle.off;
 					Float distLength = dist.GetSquaredLength();
 					if (target->_infinite)
 					{
@@ -397,14 +405,14 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			// --------------
 			if (rulemask&RULEFLAGS_MATCHVELO)
 			{
-				vParticleDir += ((vMatchVelocityDir * fCountI) - pp[i].v3) * fMatchVelocityWeight;
+				vParticleDir += ((vMatchVelocityDir * fCountI) - currentParticle.v3) * fMatchVelocityWeight;
 			}
 
 			// Turbulence
 			// ----------
 			if (rulemask&RULEFLAGS_TURBULENCE)
 			{
-				vParticleDir += Vector(SNoise(fTurbulenceScale * pp[i].off, fTurbulenceTime), SNoise(fTurbulenceScale * pp[i].off + vTurbulenceAdd1, fTurbulenceTime), SNoise(fTurbulenceScale * pp[i].off + vTurbulenceAdd2, fTurbulenceTime)) * fTurbulenceWeight;
+				vParticleDir += Vector(SNoise(fTurbulenceScale * currentParticle.off, fTurbulenceTime), SNoise(fTurbulenceScale * currentParticle.off + vTurbulenceAdd1, fTurbulenceTime), SNoise(fTurbulenceScale * currentParticle.off + vTurbulenceAdd2, fTurbulenceTime)) * fTurbulenceWeight;
 			}
 
 			// Repell
@@ -413,7 +421,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			{
 				for (maxon::BaseArray<RepellerData>::Iterator repeller = repellerData.Begin(); repeller != repellerData.End(); ++repeller)
 				{
-					Vector dist = repeller->_position - pp[i].off;
+					Vector dist = repeller->_position - currentParticle.off;
 					Float distLength = dist.GetSquaredLength();
 					if (distLength < repeller->_radius)
 						vParticleDir -= dist * (1.0 - distLength * repeller->_radiusI) * repeller->_weight * fRepellGlobalWeight;
@@ -421,7 +429,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			}
 
 			// Add resulting direction to current velocity
-			vParticleDir += pp[i].v3;
+			vParticleDir += currentParticle.v3;
 
 
 			// Level Flight
@@ -435,7 +443,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			// --------------
 			if (rulemask&RULEFLAGS_AVOIDGEO)
 			{
-				if (_geoAvoidanceCollider->Intersect(mAvoidGeoI * pp[i].off, mAvoidGeoI.TransformVector(!vParticleDir), fAvoidGeoDist))
+				if (_geoAvoidanceCollider->Intersect(mAvoidGeoI * currentParticle.off, mAvoidGeoI.TransformVector(!vParticleDir), fAvoidGeoDist))
 				{
 					GeRayColResult colliderResult;
 
@@ -496,10 +504,10 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			}
 
 			// Add resulting velocity, apply overall weight
-			ss[i].v += Blend(pp[i].v3, vParticleDir, fWeight);
+			currentBaseParticle.v += Blend(currentParticle.v3, vParticleDir, fWeight);
 		}
 
-		ss[i].count++;
+		currentBaseParticle.count++;
 	}
 }
 

From 84f091bf0c24a3ce8ecabaa2fee6f8b4ff7b939e Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 16:55:40 +0200
Subject: [PATCH 10/15] Improved comments

---
 source/object/FlockModifier.cpp | 20 ++++++++++----------
 source/object/FlockRepeller.cpp |  9 +++++++++
 source/object/FlockTarget.cpp   |  9 +++++++++
 3 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/source/object/FlockModifier.cpp b/source/object/FlockModifier.cpp
index 1e1cad2..697c262 100755
--- a/source/object/FlockModifier.cpp
+++ b/source/object/FlockModifier.cpp
@@ -24,13 +24,13 @@ class FlockModifier : public ObjectData
 	}
 	
 private:
-	AutoFree<GeRayCollider> _geoAvoidanceCollider;
+	AutoFree<GeRayCollider> _geoAvoidanceCollider; ///< GeRayCollider for "Avoid Geometry" rule
 };
 
 
-/****************************************************************************
- * Initialize attributes
- ****************************************************************************/
+//
+// Initialize attributes
+//
 Bool FlockModifier::Init(GeListNode *node)
 {
 	if (!node)
@@ -63,9 +63,9 @@ Bool FlockModifier::Init(GeListNode *node)
 	return SUPER::Init(node);
 }
 
-/****************************************************************************
- * Enable/Disable attributes
- ****************************************************************************/
+//
+// Enable/Disable attributes
+//
 Bool FlockModifier::GetDEnabling(GeListNode *node, const DescID &id,const GeData &t_data,DESCFLAGS_ENABLE flags,const BaseContainer *itemdesc)
 {
 	if (!node)
@@ -89,9 +89,9 @@ Bool FlockModifier::GetDEnabling(GeListNode *node, const DescID &id,const GeData
 	return SUPER::GetDEnabling(node, id, t_data, flags, itemdesc);
 }
 
-/****************************************************************************
- * Simulate flock
- ****************************************************************************/
+//
+// Simulate flock
+//
 void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *ss, Int32 pcnt, Float diff)
 {
 	if (!op || !pp || !ss)
diff --git a/source/object/FlockRepeller.cpp b/source/object/FlockRepeller.cpp
index e04cf9e..d70af8e 100755
--- a/source/object/FlockRepeller.cpp
+++ b/source/object/FlockRepeller.cpp
@@ -21,6 +21,9 @@ class FlockRepeller : public ObjectData
 };
 
 
+//
+// Initialize attributes
+//
 Bool FlockRepeller::Init(GeListNode *node)
 {
 	if (!node)
@@ -37,6 +40,9 @@ Bool FlockRepeller::Init(GeListNode *node)
 	return SUPER::Init(node);
 }
 
+//
+// Enable/Disable attributes
+//
 Bool FlockRepeller::GetDEnabling(GeListNode *node, const DescID &id, const GeData &t_data, DESCFLAGS_ENABLE flags, const BaseContainer *itemdesc)
 {
 	if (!node)
@@ -56,6 +62,9 @@ Bool FlockRepeller::GetDEnabling(GeListNode *node, const DescID &id, const GeDat
 	return SUPER::GetDEnabling(node, id, t_data, flags, itemdesc);
 }
 
+//
+// Draw viewport representation
+//
 DRAWRESULT FlockRepeller::Draw(BaseObject *op, DRAWPASS drawpass, BaseDraw *bd, BaseDrawHelp *bh)
 {
 	if (drawpass != DRAWPASS_OBJECT)
diff --git a/source/object/FlockTarget.cpp b/source/object/FlockTarget.cpp
index 284b46e..cd6d170 100755
--- a/source/object/FlockTarget.cpp
+++ b/source/object/FlockTarget.cpp
@@ -21,6 +21,9 @@ class FlockTarget : public ObjectData
 };
 
 
+//
+// Initialize attributes
+//
 Bool FlockTarget::Init(GeListNode *node)
 {
 	if (!node)
@@ -38,6 +41,9 @@ Bool FlockTarget::Init(GeListNode *node)
 	return SUPER::Init(node);
 }
 
+//
+// Enable/Disable attributes
+//
 Bool FlockTarget::GetDEnabling(GeListNode *node, const DescID &id, const GeData &t_data, DESCFLAGS_ENABLE flags, const BaseContainer *itemdesc)
 {
 	if (!node)
@@ -62,6 +68,9 @@ Bool FlockTarget::GetDEnabling(GeListNode *node, const DescID &id, const GeData
 	return SUPER::GetDEnabling(node, id, t_data, flags, itemdesc);
 }
 
+//
+// Draw viewport representation
+//
 DRAWRESULT FlockTarget::Draw(BaseObject *op, DRAWPASS drawpass, BaseDraw *bd, BaseDrawHelp *bh)
 {
 	if (drawpass != DRAWPASS_OBJECT)

From 192afad78291694923e302cb18a3b25ca6ca5173 Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 17:17:12 +0200
Subject: [PATCH 11/15] Bugfix fixed: "dead" boids that don't move anymore

---
 source/object/FlockModifier.cpp | 205 ++++++++++++++++----------------
 1 file changed, 103 insertions(+), 102 deletions(-)

diff --git a/source/object/FlockModifier.cpp b/source/object/FlockModifier.cpp
index 697c262..bada848 100755
--- a/source/object/FlockModifier.cpp
+++ b/source/object/FlockModifier.cpp
@@ -361,7 +361,8 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			lCount++;
 		}
 
-		// Apply any particle interaction that took place
+		// If any other particles have been taken into account for the precalculations,
+		// apply any particle interaction that took place
 		if (lCount > 1)
 		{
 			/* ------------------- Soft Rules --------------------------- */
@@ -372,7 +373,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			// ------------
 			if (rulemask&RULEFLAGS_CENTER)
 			{
-				vParticleDir += ((vCenterflockDir * fCountI) - currentParticle.off) * fCenterflockWeight;
+				vParticleDir += (vCenterflockDir * fCountI - currentParticle.off) * fCenterflockWeight;
 			}
 
 			// Neighbor Distance
@@ -382,130 +383,130 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 				vParticleDir += vNeighborDir * fNeighborWeight;
 			}
 
-			// Target
-			// ------
-			if (rulemask & RULEFLAGS_TARGET)
-			{
-				for (maxon::BaseArray<TargetData>::Iterator target = targetData.Begin(); target != targetData.End(); ++target)
-				{
-					Vector dist = target->_position - currentParticle.off;
-					Float distLength = dist.GetSquaredLength();
-					if (target->_infinite)
-					{
-						vParticleDir += dist * target->_weight * fTargetGlobalWeight;
-					}
-					else if (distLength < target->_radius)
-					{
-						vParticleDir += dist * (1.0 - distLength * target->_radiusI) * target->_weight * fTargetGlobalWeight;
-					}
-				}
-			}
-
 			// Match Velocity
 			// --------------
 			if (rulemask&RULEFLAGS_MATCHVELO)
 			{
 				vParticleDir += ((vMatchVelocityDir * fCountI) - currentParticle.v3) * fMatchVelocityWeight;
 			}
+		}
 
-			// Turbulence
-			// ----------
-			if (rulemask&RULEFLAGS_TURBULENCE)
-			{
-				vParticleDir += Vector(SNoise(fTurbulenceScale * currentParticle.off, fTurbulenceTime), SNoise(fTurbulenceScale * currentParticle.off + vTurbulenceAdd1, fTurbulenceTime), SNoise(fTurbulenceScale * currentParticle.off + vTurbulenceAdd2, fTurbulenceTime)) * fTurbulenceWeight;
-			}
-
-			// Repell
-			// ------
-			if (rulemask&RULEFLAGS_REPELL)
+		// Target
+		// ------
+		if (rulemask&RULEFLAGS_TARGET)
+		{
+			for (maxon::BaseArray<TargetData>::Iterator target = targetData.Begin(); target != targetData.End(); ++target)
 			{
-				for (maxon::BaseArray<RepellerData>::Iterator repeller = repellerData.Begin(); repeller != repellerData.End(); ++repeller)
+				Vector dist = target->_position - currentParticle.off;
+				Float distLength = dist.GetSquaredLength();
+				if (target->_infinite)
 				{
-					Vector dist = repeller->_position - currentParticle.off;
-					Float distLength = dist.GetSquaredLength();
-					if (distLength < repeller->_radius)
-						vParticleDir -= dist * (1.0 - distLength * repeller->_radiusI) * repeller->_weight * fRepellGlobalWeight;
+					vParticleDir += dist * target->_weight * fTargetGlobalWeight;
+				}
+				else if (distLength < target->_radius)
+				{
+					vParticleDir += dist * (1.0 - distLength * target->_radiusI) * target->_weight * fTargetGlobalWeight;
 				}
 			}
-
-			// Add resulting direction to current velocity
-			vParticleDir += currentParticle.v3;
-
-
-			// Level Flight
-			// ------------
-			if (rulemask&RULEFLAGS_LEVELFLIGHT)
+		}
+		
+		// Turbulence
+		// ----------
+		if (rulemask&RULEFLAGS_TURBULENCE)
+		{
+			vParticleDir += Vector(SNoise(fTurbulenceScale * currentParticle.off, fTurbulenceTime), SNoise(fTurbulenceScale * currentParticle.off + vTurbulenceAdd1, fTurbulenceTime), SNoise(fTurbulenceScale * currentParticle.off + vTurbulenceAdd2, fTurbulenceTime)) * fTurbulenceWeight;
+		}
+		
+		// Repell
+		// ------
+		if (rulemask&RULEFLAGS_REPELL)
+		{
+			for (maxon::BaseArray<RepellerData>::Iterator repeller = repellerData.Begin(); repeller != repellerData.End(); ++repeller)
 			{
-				vParticleDir.y -= vParticleDir.y * fLevelFlightWeight;
+				Vector dist = repeller->_position - currentParticle.off;
+				Float distLength = dist.GetSquaredLength();
+				if (distLength < repeller->_radius)
+					vParticleDir -= dist * (1.0 - distLength * repeller->_radiusI) * repeller->_weight * fRepellGlobalWeight;
 			}
-
-			// Avoid Geometry
-			// --------------
-			if (rulemask&RULEFLAGS_AVOIDGEO)
+		}
+		
+		// Add resulting direction to current velocity
+		vParticleDir += currentParticle.v3;
+		
+		
+		// Level Flight
+		// ------------
+		if (rulemask&RULEFLAGS_LEVELFLIGHT)
+		{
+			vParticleDir.y -= vParticleDir.y * fLevelFlightWeight;
+		}
+		
+		// Avoid Geometry
+		// --------------
+		if (rulemask&RULEFLAGS_AVOIDGEO)
+		{
+			if (_geoAvoidanceCollider->Intersect(mAvoidGeoI * currentParticle.off, mAvoidGeoI.TransformVector(!vParticleDir), fAvoidGeoDist))
 			{
-				if (_geoAvoidanceCollider->Intersect(mAvoidGeoI * currentParticle.off, mAvoidGeoI.TransformVector(!vParticleDir), fAvoidGeoDist))
+				GeRayColResult colliderResult;
+				
+				if (_geoAvoidanceCollider->GetNearestIntersection(&colliderResult))
 				{
-					GeRayColResult colliderResult;
-
-					if (_geoAvoidanceCollider->GetNearestIntersection(&colliderResult))
+					fAvoidGeoMixval = 1.0 - colliderResult.distance * fAvoidGeoDistI;
+					switch (lAvoidGeoMode)
 					{
-						fAvoidGeoMixval = 1.0 - colliderResult.distance * fAvoidGeoDistI;
-						switch (lAvoidGeoMode)
-						{
-							case OFLOCK_AVOIDGEO_MODE_SOFT:
-								vParticleDir = vParticleDir + mAvoidGeo.TransformVector(!colliderResult.s_normal) * vParticleDir.GetLength() * fAvoidGeoMixval * fAvoidGeoWeight;
-								break;
-
-							default:
-							case OFLOCK_AVOIDGEO_MODE_HARD:
-								vParticleDir = Blend(mAvoidGeo.TransformVector((!colliderResult.s_normal * vParticleDir.GetLength())), vParticleDir, fAvoidGeoMixval);
-								break;
-						}
+						case OFLOCK_AVOIDGEO_MODE_SOFT:
+							vParticleDir = vParticleDir + mAvoidGeo.TransformVector(!colliderResult.s_normal) * vParticleDir.GetLength() * fAvoidGeoMixval * fAvoidGeoWeight;
+							break;
+							
+						default:
+						case OFLOCK_AVOIDGEO_MODE_HARD:
+							vParticleDir = Blend(mAvoidGeo.TransformVector((!colliderResult.s_normal * vParticleDir.GetLength())), vParticleDir, fAvoidGeoMixval);
+							break;
 					}
 				}
 			}
-
-
-			// Speed Limits
-			// ------------
-			if (rulemask&RULEFLAGS_SPEEDLIMIT)
+		}
+		
+		
+		// Speed Limits
+		// ------------
+		if (rulemask&RULEFLAGS_SPEEDLIMIT)
+		{
+			fSpeed = vParticleDir.GetSquaredLength() * diff;
+			switch (lSpeedMode)
 			{
-				fSpeed = vParticleDir.GetSquaredLength() * diff;
-				switch (lSpeedMode)
+				case OFLOCK_SPEED_MODE_SOFT:
+				{
+					if (fSpeed < fSpeedMin)
+					{
+						fSpeedRatio = fSpeedMin / FMax(fSpeed, EPSILON);
+						vParticleDir *= Blend(1.0, fSpeedRatio, fSpeedWeight);
+					}
+					else if (fSpeed > fSpeedMax)
+					{
+						fSpeedRatio = fSpeedMax / FMax(fSpeed, EPSILON);
+						vParticleDir *= Blend(1.0, fSpeedRatio, fSpeedWeight);
+					}
+				}
+					break;
+					
+				case OFLOCK_SPEED_MODE_HARD:
 				{
-					case OFLOCK_SPEED_MODE_SOFT:
-						{
-							if (fSpeed < fSpeedMin)
-							{
-								fSpeedRatio = fSpeedMin / FMax(fSpeed, EPSILON);
-								vParticleDir *= Blend(1.0, fSpeedRatio, fSpeedWeight);
-							}
-							else if (fSpeed > fSpeedMax)
-							{
-								fSpeedRatio = fSpeedMax / FMax(fSpeed, EPSILON);
-								vParticleDir *= Blend(1.0, fSpeedRatio, fSpeedWeight);
-							}
-						}
-						break;
-
-					case OFLOCK_SPEED_MODE_HARD:
-						{
-							if (fSpeed < fSpeedMin)
-							{
-								vParticleDir = !vParticleDir * fSpeedMin;
-							}
-							else if (fSpeed > fSpeedMax)
-							{
-								vParticleDir = !vParticleDir * fSpeedMax;
-							}
-						}
-						break;
+					if (fSpeed < fSpeedMin)
+					{
+						vParticleDir = !vParticleDir * fSpeedMin;
+					}
+					else if (fSpeed > fSpeedMax)
+					{
+						vParticleDir = !vParticleDir * fSpeedMax;
+					}
 				}
+					break;
 			}
-
-			// Add resulting velocity, apply overall weight
-			currentBaseParticle.v += Blend(currentParticle.v3, vParticleDir, fWeight);
 		}
+		
+		// Add resulting velocity, apply overall weight
+		currentBaseParticle.v += Blend(currentParticle.v3, vParticleDir, fWeight);
 
 		currentBaseParticle.count++;
 	}

From 1b855f8a771e6c9d164f90e4cd5e127c1b3be2ce Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 17:18:57 +0200
Subject: [PATCH 12/15] Updated changes.txt

---
 changes.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/changes.txt b/changes.txt
index 4765bc1..0ad4fe3 100755
--- a/changes.txt
+++ b/changes.txt
@@ -1,4 +1,5 @@
 0.8
+- Fixed bug: "Dead" boids that don't move
 - Lots of internal optimizations
 - Code style
 

From 06a2d5c9f407a8890c5e5869168465c7a9835946 Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 17:22:25 +0200
Subject: [PATCH 13/15] Changed version number to 0.7.5; minor changes in
 main.cpp

---
 changes.txt     | 2 +-
 source/main.cpp | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/changes.txt b/changes.txt
index 0ad4fe3..44e7840 100755
--- a/changes.txt
+++ b/changes.txt
@@ -1,4 +1,4 @@
-0.8
+0.7.5
 - Fixed bug: "Dead" boids that don't move
 - Lots of internal optimizations
 - Code style
diff --git a/source/main.cpp b/source/main.cpp
index 91e95e4..c29d15c 100755
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -2,12 +2,12 @@
 #include "main.h"
 
 
-#define PLUGIN_VERSION  String("0.8")
+#define PLUGIN_VERSION  String("FlockModifier 0.7.5")
 
 
 Bool PluginStart()
 {
-	GePrint("FlockModifier " + PLUGIN_VERSION);
+	GePrint(PLUGIN_VERSION);
 	
 	if (!RegisterFlockModifier())
 		return false;
@@ -20,7 +20,8 @@ Bool PluginStart()
 }
 
 void PluginEnd()
-{ }
+{
+}
 
 Bool PluginMessage(Int32 id, void *data)
 {

From 8012009241396fad441693417a01f5e14fb891a7 Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 17:31:41 +0200
Subject: [PATCH 14/15] Changes in R16 XCode project

---
 FlockModifier_R16.xcodeproj/project.pbxproj | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/FlockModifier_R16.xcodeproj/project.pbxproj b/FlockModifier_R16.xcodeproj/project.pbxproj
index a7090fe..e2600b4 100644
--- a/FlockModifier_R16.xcodeproj/project.pbxproj
+++ b/FlockModifier_R16.xcodeproj/project.pbxproj
@@ -47,7 +47,7 @@
 		01BA96D31E9D815700E8F37F /* FlockModifier.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FlockModifier.cpp; sourceTree = "<group>"; };
 		01BA96D41E9D815700E8F37F /* FlockRepeller.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FlockRepeller.cpp; sourceTree = "<group>"; };
 		01BA96D51E9D815700E8F37F /* FlockTarget.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FlockTarget.cpp; sourceTree = "<group>"; };
-		A08C5CDEA3A66833397B0000 /* FlockModifier_R16.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = FlockModifier_R16.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
+		A08C5CDEA3A66833397B0000 /* FlockModifier.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = FlockModifier.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
 		A0A668333900000000000000 /* debugbase.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = debugbase.xcconfig; path = ../../frameworks/settings/debugbase.xcconfig; sourceTree = SOURCE_ROOT; };
 		A0A668333900000000050000 /* releasebase.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = releasebase.xcconfig; path = ../../frameworks/settings/releasebase.xcconfig; sourceTree = SOURCE_ROOT; };
 		A0A6683339F470FF41020000 /* cinema.framework.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = cinema.framework.xcodeproj; path = ../../frameworks/cinema.framework/project/cinema.framework.xcodeproj; sourceTree = SOURCE_ROOT; };
@@ -106,7 +106,7 @@
 		A0A668333900000000230000 /* products */ = {
 			isa = PBXGroup;
 			children = (
-				A08C5CDEA3A66833397B0000 /* FlockModifier_R16.dylib */,
+				A08C5CDEA3A66833397B0000 /* FlockModifier.dylib */,
 			);
 			name = products;
 			sourceTree = "<group>";
@@ -162,7 +162,7 @@
 			);
 			name = FlockModifier;
 			productName = cinema4dsdk;
-			productReference = A08C5CDEA3A66833397B0000 /* FlockModifier_R16.dylib */;
+			productReference = A08C5CDEA3A66833397B0000 /* FlockModifier.dylib */;
 			productType = "com.apple.product-type.library.dynamic";
 		};
 /* End PBXNativeTarget section */

From 8e812c79499a75e3fbcf830022dbd656965bf351 Mon Sep 17 00:00:00 2001
From: Frank Willeke <frank.willeke@laubwerk.com>
Date: Mon, 22 May 2017 17:32:41 +0200
Subject: [PATCH 15/15] Variable renaming, minor code optimizations

---
 source/object/FlockModifier.cpp | 40 ++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 21 deletions(-)

diff --git a/source/object/FlockModifier.cpp b/source/object/FlockModifier.cpp
index bada848..7dbee14 100755
--- a/source/object/FlockModifier.cpp
+++ b/source/object/FlockModifier.cpp
@@ -79,11 +79,9 @@ Bool FlockModifier::GetDEnabling(GeListNode *node, const DescID &id,const GeData
 	{
 		case OFLOCK_AVOIDGEO_WEIGHT:
 			return bc->GetInt32(OFLOCK_AVOIDGEO_MODE) == OFLOCK_AVOIDGEO_MODE_SOFT;
-			break;
 			
 		case OFLOCK_SPEED_WEIGHT:
 			return bc->GetInt32(OFLOCK_SPEED_MODE) == OFLOCK_SPEED_MODE_SOFT;
-			break;
 	}
 	
 	return SUPER::GetDEnabling(node, id, t_data, flags, itemdesc);
@@ -106,7 +104,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 		return;
 
 	// Variables
-	Int32 lCount = pcnt - 1;
+	Int32 iCount = pcnt - 1;
 	Vector vParticleDir(DC);
 
 	// Overall weight
@@ -134,9 +132,9 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	// Target
 	Float fTargetGlobalWeight = bc->GetFloat(OFLOCK_TARGET_WEIGHT, 0.0) * 0.1;
 	InExcludeData* inexTarget = (InExcludeData*)bc->GetCustomDataType(OFLOCK_TARGET_LINK, CUSTOMDATATYPE_INEXCLUDE_LIST);
-	Int32 lTargetCount = 0;
+	Int32 iTargetCount = 0;
 	if (inexTarget)
-		lTargetCount = inexTarget->GetObjectCount();
+		iTargetCount = inexTarget->GetObjectCount();
 	maxon::BaseArray<TargetData> targetData;
 
 	// Level flight
@@ -144,14 +142,14 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 
 	// Speed limits
 	Float fSpeed = 0.0;
-	Int32 lSpeedMode = bc->GetInt32(OFLOCK_SPEED_MODE, OFLOCK_SPEED_MODE_SOFT);
+	Int32 iSpeedMode = bc->GetInt32(OFLOCK_SPEED_MODE, OFLOCK_SPEED_MODE_SOFT);
 	Float fSpeedWeight = bc->GetFloat(OFLOCK_SPEED_WEIGHT, 0.0);
 	Float fSpeedMin = bc->GetFloat(OFLOCK_SPEED_MIN, 0.0) * diff;
 	Float fSpeedMax = bc->GetFloat(OFLOCK_SPEED_MAX, 100.0) * diff;
 	Float fSpeedRatio = 0.0;
 
 	// Geometry avoidance
-	Int32 lAvoidGeoMode = bc->GetInt32(OFLOCK_AVOIDGEO_MODE, 1);
+	Int32 iAvoidGeoMode = bc->GetInt32(OFLOCK_AVOIDGEO_MODE, 1);
 	Float fAvoidGeoWeight = bc->GetFloat(OFLOCK_AVOIDGEO_WEIGHT, 0.0);
 	Float fAvoidGeoDist = bc->GetFloat(OFLOCK_AVOIDGEO_DIST, 0.0);
 	Float fAvoidGeoDistI = 1.0 / FMax(fAvoidGeoDist, EPSILON);
@@ -169,9 +167,9 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	// Repell
 	Float fRepellGlobalWeight = bc->GetFloat(OFLOCK_REPELL_WEIGHT, 0.0);
 	InExcludeData* inexRepell = (InExcludeData*)bc->GetCustomDataType(OFLOCK_REPELL_LINK, CUSTOMDATATYPE_INEXCLUDE_LIST);
-	Int32 lRepellCount = 0;
+	Int32 iRepellerCount = 0;
 	if (inexRepell)
-		lRepellCount = inexRepell->GetObjectCount();
+		iRepellerCount = inexRepell->GetObjectCount();
 	maxon::BaseArray<RepellerData> repellerData;
 
 
@@ -212,7 +210,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	}
 
 	// Speed Limit
-	if (((lSpeedMode == OFLOCK_SPEED_MODE_SOFT && fSpeedWeight > 0.0) || lSpeedMode == OFLOCK_SPEED_MODE_HARD))
+	if (((iSpeedMode == OFLOCK_SPEED_MODE_SOFT && fSpeedWeight > 0.0) || iSpeedMode == OFLOCK_SPEED_MODE_HARD))
 	{
 		rulemask |= RULEFLAGS_SPEEDLIMIT;
 		fSpeedMin *= fSpeedMin; // Square
@@ -220,7 +218,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	}
 
 	// Target
-	if (fTargetGlobalWeight > 0.0 && inexTarget && lTargetCount > 0)
+	if (fTargetGlobalWeight > 0.0 && inexTarget && iTargetCount > 0)
 	{
 		rulemask |= RULEFLAGS_TARGET;
 
@@ -228,7 +226,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 		BaseContainer *tc = nullptr;
 
 		targetData.Flush();
-		for (i = 0; i < lTargetCount; ++i)
+		for (i = 0; i < iTargetCount; ++i)
 		{
 			opListItem = static_cast<BaseObject*>(inexTarget->ObjectFromIndex(doc, i));
 			if (opListItem)
@@ -249,7 +247,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	}
 
 	// Avoid Geometry
-	if (((lAvoidGeoMode == OFLOCK_AVOIDGEO_MODE_SOFT && fAvoidGeoWeight > 0.0) || lAvoidGeoMode == OFLOCK_AVOIDGEO_MODE_HARD) && fAvoidGeoDist > 0.0 && boAvoidGeoLink && boAvoidGeoLink->GetType() == Opolygon && ToPoly(boAvoidGeoLink)->GetPolygonCount() > 0)
+	if (((iAvoidGeoMode == OFLOCK_AVOIDGEO_MODE_SOFT && fAvoidGeoWeight > 0.0) || iAvoidGeoMode == OFLOCK_AVOIDGEO_MODE_HARD) && fAvoidGeoDist > 0.0 && boAvoidGeoLink && boAvoidGeoLink->GetType() == Opolygon && ToPoly(boAvoidGeoLink)->GetPolygonCount() > 0)
 	{
 		rulemask |= RULEFLAGS_AVOIDGEO;
 		
@@ -267,7 +265,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 	}
 
 	// Repelling
-	if (fRepellGlobalWeight > 0.0 && inexRepell && lRepellCount > 0)
+	if (fRepellGlobalWeight > 0.0 && inexRepell && iRepellerCount > 0)
 	{
 		rulemask |= RULEFLAGS_REPELL;
 
@@ -275,7 +273,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 		BaseContainer *tc = nullptr;
 
 		repellerData.Flush();
-		for (i = 0; i < lRepellCount; ++i)
+		for (i = 0; i < iRepellerCount; ++i)
 		{
 			opListItem = static_cast<BaseObject*>(inexRepell->ObjectFromIndex(doc, i));
 			if (opListItem)
@@ -309,7 +307,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 
 		// Reset values
 		vParticleDir = vCenterflockDir = vNeighborDir = vMatchVelocityDir = Vector();
-		lCount = 0;
+		iCount = 0;
 
 		/* ------------------- Collect particle interaction data --------------------------- */ 
 
@@ -358,16 +356,16 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 			}
 
 			// Increase counter of considered flockmates
-			lCount++;
+			iCount++;
 		}
 
 		// If any other particles have been taken into account for the precalculations,
 		// apply any particle interaction that took place
-		if (lCount > 1)
+		if (iCount > 1)
 		{
 			/* ------------------- Soft Rules --------------------------- */
 			
-			Float fCountI = 1.0 / FMax(lCount, EPSILON);
+			Float fCountI = 1.0 / FMax(iCount, EPSILON);
 
 			// Flock Center
 			// ------------
@@ -452,7 +450,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 				if (_geoAvoidanceCollider->GetNearestIntersection(&colliderResult))
 				{
 					fAvoidGeoMixval = 1.0 - colliderResult.distance * fAvoidGeoDistI;
-					switch (lAvoidGeoMode)
+					switch (iAvoidGeoMode)
 					{
 						case OFLOCK_AVOIDGEO_MODE_SOFT:
 							vParticleDir = vParticleDir + mAvoidGeo.TransformVector(!colliderResult.s_normal) * vParticleDir.GetLength() * fAvoidGeoMixval * fAvoidGeoWeight;
@@ -473,7 +471,7 @@ void FlockModifier::ModifyParticles(BaseObject *op, Particle *pp, BaseParticle *
 		if (rulemask&RULEFLAGS_SPEEDLIMIT)
 		{
 			fSpeed = vParticleDir.GetSquaredLength() * diff;
-			switch (lSpeedMode)
+			switch (iSpeedMode)
 			{
 				case OFLOCK_SPEED_MODE_SOFT:
 				{