99 template <
class T, u32 R>
108 Vector() : x(0), y(0) { }
113 FXP16 GetX()
const {
return x; }
115 FXP16 GetY()
const {
return y; }
118 f.Printf(
"(%06d,%06d)",x,y);
121 friend FXP16 GetDistSq(
const Vector & v1,
const Vector & v2) {
122 return (v1.x-v2.x)*(v1.x-v2.x)+(v1.y-v2.y)*(v1.y-v2.y);
125 friend Vector operator+(
const Vector & v1,
const Vector & v2) {
126 return Vector(v1.x+v2.x,v1.y+v2.y);
128 friend Vector operator-(
const Vector & v1) {
129 return Vector(-v1.x,-v1.y);
131 friend Vector operator-(
const Vector & v1,
const Vector & v2) {
134 friend FXP16 operator*(
const Vector & v1,
const Vector v2) {
135 return v1.x*v2.x+v1.y*v2.y;
137 friend Vector operator*(
const FXP16 num,
const Vector & v1) {
138 return Vector(v1.x*num,v1.y*num);
140 friend Vector operator*(
const Vector & v1,
const FXP16 num) {
143 friend Vector operator/(
const Vector & v1,
const FXP16 num) {
147 Vector & operator=(
const Vector & v1) {
153 Vector & operator+=(
const Vector & v1) {
158 Vector & operator-=(
const Vector & v1) {
163 Vector & operator*=(
const FXP16 num) {
168 Vector & operator/=(
const FXP16 num) {
175 template <u32 TVBITS>
176 static u32 toSignMag(
FXP16 value) {
177 const u32 SIGN_BIT = 1<<(TVBITS-1);
178 const u32 MAX = SIGN_BIT-1;
186 u32 val = (u32) value;
190 template <u32 TVBITS>
191 static FXP16 fromSignMag(
const u32 value) {
192 const u32 SIGN_BIT = 1<<(TVBITS-1);
193 const u32 MASK = SIGN_BIT-1;
194 FXP16 val = value&MASK;
195 if (value & SIGN_BIT)
200 template <u32 TVBITS>
201 static u32 toTinyVector(
const Vector v) {
202 u32 x = toSignMag<TVBITS>(v.x);
203 u32 y = toSignMag<TVBITS>(v.y);
204 return (x<<TVBITS)|y;
207 template <u32 TVBITS>
208 static Vector toVector(
const u32 bits) {
209 const u32 MASK = (1<<TVBITS)-1;
211 FXP16 x = fromSignMag<TVBITS>((bits>>TVBITS)&MASK);
212 FXP16 y = fromSignMag<TVBITS>(bits&MASK);
218 static const u32 TYPE = 0xbd;
219 static const u32 TYPE_BITS = 8;
220 static const u32 TYPE_MASK = (1<<TYPE_BITS)-1;
221 static bool IsBoidType(u32 type) {
222 return (type&TYPE_MASK)==TYPE;
225 static const u32 BITS_PER_DIM = 4;
227 static const u32 STATE_HEADING_IDX = 0;
228 static const u32 STATE_HEADING_LEN = 2 * BITS_PER_DIM;
229 static const u32 STATE_BITS = STATE_HEADING_IDX + STATE_HEADING_LEN;
233 Vector GetHeading(
const T &atom)
const {
234 if (!IsBoidType(atom.GetType()))
236 return toVector<BITS_PER_DIM>(atom.GetStateField(STATE_HEADING_IDX,STATE_HEADING_LEN));
239 void SetHeading(
const T &atom,
const Vector v)
const {
240 if (!IsBoidType(atom.GetType()))
242 atom.SetStateField(STATE_HEADING_IDX,STATE_HEADING_LEN,toTinyVector<BITS_PER_DIM>(v));
250 const FXP16 MAX_DIST = R;
251 const FXP16 MAX_DIST_WGT = R;
252 const FXP16 WGT_PER_DIST = MAX_DIST_WGT / MAX_DIST;
256 const SPoint sp = md.GetPoint(idx);
259 if (sp.
GetX()>1 || sp.
GetY()>1)
continue;
278 FXP16 dist = GetDistSq(spVec,target);
281 weight = MAX_DIST_WGT-dist*WGT_PER_DIST;
287 if (random.
OddsOf(weight,totWgt)) {
297 const double TARGET_SPACING = 1.5;
301 const u32 selfType =
self.GetType();
303 if (!IsBoidType(selfType))
322 const SPoint sp = md.GetPoint(idx);
325 const u32 otherType = other.GetType();
328 if (random.
OneIn(++resCount)) resAt = sp;
332 if (otherType != selfType)
continue;
338 Vector theirHead = GetHeading(other);
340 avgHeading += theirHead;
341 avgPosition += Vector(sp);
346 if (countNgbr > 0 && countNgbr < 3 && resCount > 0) {
353 if (countNgbr == 0) {
357 if (random.
OneIn(3)) {
361 }
else if (countNgbr == 1 && minDist > TARGET_SPACING) {
363 SPoint dest = PickPossibleDestination(window,avgPosition,random);
369 avgHeading /= countNgbr;
370 avgPosition /= countNgbr;
u32 GetFirstIndex(const u32 radius) const
Definition: MDist.h:112
void Diffuse(EventWindow< T > &window) const
Definition: Element_Empty.h:41
u32 GetLastIndex(const u32 radius) const
Definition: MDist.h:129
void SetCenterAtom(const T &atom)
Definition: EventWindow.h:220
bool OneIn(u32 odds)
Definition: Random.h:96
T GetY() const
Definition: Point.tcc:40
Random & GetRandom()
Definition: EventWindow.h:122
const T & GetRelativeAtom(const SPoint &offset) const
Definition: EventWindow.tcc:26
u32 GetManhattanLength() const
Definition: Point.tcc:46
bool OddsOf(u32 thisMany, u32 outOfThisMany)
Definition: Random.h:183
const T & GetCenterAtom() const
Definition: EventWindow.h:209
virtual const T & GetDefaultAtom() const
Definition: Element.h:382
Definition: ByteSink.h:47
Definition: Element_Pair.h:100
static MDist< R > & get()
Definition: MDist.tcc:193
Definition: Element_Res.h:48
Definition: ElementTable.h:43
bool SetRelativeAtom(const SPoint &offset, const T &atom)
Definition: EventWindow.tcc:15
void SwapAtoms(const SPoint &locA, const SPoint &locB)
Definition: EventWindow.tcc:40
T GetX() const
Definition: Point.tcc:34