1 #ifndef ELEMENT_MQBAR_H
2 #define ELEMENT_MQBAR_H
50 class Element_MQBar :
public Element<CC>
53 typedef typename CC::ATOM_TYPE T;
54 typedef typename CC::PARAM_CONFIG P;
56 enum { R = P::EVENT_WINDOW_RADIUS };
59 static u32 toMag(u32 value) {
60 const u32 MAX = (1<<TVBITS)-1;
62 FAIL(ILLEGAL_ARGUMENT);
71 static u32 fromMag(
const u32 value) {
72 const u32 MASK = (1<<TVBITS)-1;
77 template <u32 TXBITS, u32 TYBITS>
78 static u32 toUTiny(
const UPoint & v) {
79 u32 x = toMag<TXBITS>(v.GetX());
80 u32 y = toMag<TYBITS>(v.GetY());
84 template <u32 TXBITS, u32 TYBITS>
85 static UPoint toUPoint(
const u32 bits) {
87 u32 x = fromMag<TXBITS>(bits>>TYBITS);
88 u32 y = fromMag<TYBITS>(bits);
94 static Element_MQBar THE_INSTANCE;
95 static const u32 TYPE() {
99 static bool IsOurType(u32 type) {
103 static const u32 BITS_WIDE = 5;
104 static const u32 BITS_HIGH = 7;
105 static const u32 BITS_SYMI = 2;
107 static const u32 BITS_TIMER = 4;
108 static const u32 MAX_TIMER_VALUE = 9;
110 static const u32 BITS_BAR_COORD_LEN = BITS_WIDE + BITS_HIGH;
112 static const u32 STATE_BITS_START = P3Atom<P>::P3_STATE_BITS_POS;
114 typedef AtomicParameterType<CC, VD::U32, BITS_WIDE, STATE_BITS_START> APBarWidth;
115 typedef AtomicParameterType<CC, VD::U32, BITS_HIGH, APBarWidth::END> APBarHeight;
116 typedef AtomicParameterType<CC, VD::U32, BITS_WIDE, APBarHeight::END> APXPos;
117 typedef AtomicParameterType<CC, VD::U32, BITS_HIGH, APXPos::END> APYPos;
118 typedef AtomicParameterType<CC, VD::U32, BITS_SYMI, APYPos::END> APSymI;
119 typedef AtomicParameterType<CC, VD::U32, BITS_TIMER,APSymI::END> APTimer;
121 APBarWidth m_barWidth;
122 APBarHeight m_barHeight;
128 static const u32 STATE_BITS_END = APTimer::END;
129 static const u32 STATE_BITS_COUNT = STATE_BITS_END - STATE_BITS_START + 1;
132 Element<CC>(MFM_UUID_FOR(
"MQBar", MQBAR_VERSION)),
133 m_barWidth(this,
"width",
"Bar Width",
"Bar width", 0, 15, _GetNOnes32(BITS_WIDE)),
134 m_barHeight(this,
"height",
"Bar Height",
"Bar height", 0, 3*15, _GetNOnes32(BITS_HIGH)),
135 m_xPos(this,
"xpos",
"X pos",
"X position within bar", 0, 0, _GetNOnes32(BITS_WIDE)),
136 m_yPos(this,
"ypos",
"Y pos",
"Y position within bar", 0, 0, _GetNOnes32(BITS_HIGH)),
137 m_symI(this,
"sym",
"Symmetry",
"Bar orientation, 0:N, 1:E, 2:S, 3:W", 0, 0, _GetNOnes32(BITS_SYMI)),
138 m_timer(this,
"timer",
"Timer",
"Cell maturity timer", 0, 0, _GetNOnes32(BITS_TIMER))
146 u32 GetSymI(
const T &atom)
const {
147 if (!IsOurType(atom.GetType()))
150 return m_symI.GetValue(atom);
153 SPoint GetMax(
const T &atom)
const {
154 if (!IsOurType(atom.GetType()))
156 return SPoint(m_barWidth.GetValue(atom), m_barHeight.GetValue(atom));
159 SPoint GetPos(
const T &atom)
const {
160 if (!IsOurType(atom.GetType()))
162 return SPoint(m_xPos.GetValue(atom), m_yPos.GetValue(atom));
165 u32 GetTimer(
const T &atom)
const {
166 if (!IsOurType(atom.GetType()))
168 return m_timer.GetValue(atom);
171 bool FitsInRep(
const SPoint & v)
const {
172 return v.BoundedBy(SPoint(0,0),SPoint(MakeMaskClip(BITS_WIDE),MakeMaskClip(BITS_HIGH)));
175 void SetSize(T &atom,
const SPoint & v)
const {
176 if (!IsOurType(atom.GetType()))
179 FAIL(ILLEGAL_ARGUMENT);
180 m_barWidth.SetValue(atom, v.GetX());
181 m_barHeight.SetValue(atom, v.GetY());
184 void SetPos(T &atom,
const SPoint v)
const {
185 if (!IsOurType(atom.GetType()))
188 FAIL(ILLEGAL_ARGUMENT);
189 m_xPos.SetValue(atom, v.GetX());
190 m_yPos.SetValue(atom, v.GetY());
193 void SetSymI(T &atom,
const u32 sym)
const {
194 if (!IsOurType(atom.GetType()))
196 if (sym >= PSYM_SYMMETRY_COUNT)
197 FAIL(ILLEGAL_ARGUMENT);
198 m_symI.SetValue(atom, sym);
201 void SetTimer(T &atom,
const u32 tmr)
const {
202 if (!IsOurType(atom.GetType()))
204 if (tmr > MAX_TIMER_VALUE)
205 FAIL(ILLEGAL_ARGUMENT);
206 m_timer.SetValue(atom, tmr);
211 static T defaultAtom(TYPE(), 0, 0, STATE_BITS_COUNT);
212 const SPoint QBAR_SIZE(15, 3 * 15);
213 SetSize(defaultAtom, QBAR_SIZE);
214 SetPos(defaultAtom,
SPoint(0,0));
215 SetSymI(defaultAtom, 0);
245 SPoint barMax = GetMax(atom);
246 SPoint myPos = GetPos(atom);
247 return ColorMap_SEQ6_PuRd::THE_INSTANCE.
254 u32 timer = GetTimer(atom);
255 return ColorMap_SEQ6_GnBu::THE_INSTANCE.
273 const u32 selfType =
self.GetType();
274 if (!IsOurType(selfType)) FAIL(ILLEGAL_STATE);
276 u32 ourTimer = this->GetTimer(
self);
279 u32 symi = this->GetSymI(
self);
284 SPoint barMax = this->GetMax(
self);
285 SPoint myPos = this->GetPos(
self);
290 u32 inconsistentCount = 0;
293 u32 consistentCount = 0;
302 bool neighborTimer =
false;
308 const SPoint sp = md.GetPoint(idx);
311 bool onGrid = (sp.
GetX()&1)==0 && (sp.
GetY()&1)==0;
314 const SPoint theirBarPos = sp+myPos;
324 const u32 otherType = other.GetType();
330 if (random.
OneIn(++makeCount)) {
333 SetPos(makeGuy,theirBarPos);
334 SetTimer(makeGuy, 0);
340 if (IsOurType(otherType)) {
344 SPoint otherBarMax = GetMax(other);
345 SPoint otherPos = GetPos(other);
347 SPoint otherPosMapped = otherPos-sp;
348 u32 otherTimer = GetTimer(other);
350 if (otherBarMax==barMax && otherPosMapped == myPos) {
354 if (!neighborTimer || otherTimer < minTimer)
355 minTimer = otherTimer;
356 if (!neighborTimer || otherTimer > maxTimer)
357 maxTimer = otherTimer;
358 neighborTimer =
true;
361 u32 otherTimer = GetTimer(other);
364 if (otherTimer < ourTimer && random.
OneIn(++inconsistentCount)) {
381 const u32 otherType = other.GetType();
386 if (random.
OneIn(++eatCount)) {
394 if (isEmpty) ++consistentCount;
396 bool iQBar = IsOurType(otherType);
398 u32 iQBarTimer = GetTimer(other);
399 if (iQBarTimer < ourTimer && random.
OneIn(++inconsistentCount)) {
413 if (inconsistentCount > 0) {
416 if (consistentCount > 3 * inconsistentCount) {
421 }
else if (inconsistentCount > 3 * consistentCount) {
431 u32 ourTimer = GetTimer(
self);
433 if (neighborTimer && minTimer < ourTimer)
437 SetTimer(
self,ourTimer);
444 if (makeCount > 0 && eatCount > 0) {
450 if (myPos == barMax-
SPoint(1,1)) {
454 const u32 offType = offEnd.GetType();
457 u32 symi = GetSymI(
self);
459 if (symi > PSYM_DEG270L)
461 SetSymI(corner,symi);
462 SetPos(corner,
SPoint(0,0));
470 u32 ourTimer = GetTimer(
self);
471 if (ourTimer < MAX_TIMER_VALUE && (ourTimer==0 || (neighborTimer && minTimer+1 >= ourTimer))) {
472 if (random.
OneIn(ourTimer+1)) {
474 SetTimer(
self,ourTimer);
487 Element_MQBar<CC> Element_MQBar<CC>::THE_INSTANCE;
u32 GetFirstIndex(const u32 radius) const
Definition: MDist.h:112
virtual u32 DefaultLowlightColor() const
Definition: Element_MQBar.h:230
virtual const T & GetDefaultAtom() const
Definition: Element_MQBar.h:209
u32 GetType() const
Definition: Element.h:290
virtual u32 DefaultPhysicsColor() const
Definition: Element_MQBar.h:225
Definition: Element_Empty.h:41
u32 GetLastIndex(const u32 radius) const
Definition: MDist.h:129
void SetSymmetry(const PointSymmetry psym)
Definition: EventWindow.h:110
void SetCenterAtom(const T &atom)
Definition: EventWindow.h:220
u32 NoDiffusability(EventWindow< CC > &ew, SPoint nowAt, SPoint maybeAt) const
Definition: Element.h:508
void SetName(const char *name)
Definition: Element.h:209
void SetAtomicSymbol(const char *symbol)
Definition: Element.h:193
bool BoundedBy(const Point< T > &lowerBound, const Point< T > &upperBound) const
Definition: Point.tcc:223
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
const T & GetCenterAtom() const
Definition: EventWindow.h:209
const UUID & GetUUID() const
Definition: Element.h:355
virtual const T & GetDefaultAtom() const
Definition: Element.h:382
bool IsType(u32 type) const
Definition: Element.h:345
virtual u32 LocalPhysicsColor(const T &atom, u32 selector) const
Definition: Element_MQBar.h:241
Element(const UUID &uuid)
Definition: Element.h:246
static MDist< R > & get()
Definition: MDist.tcc:193
virtual T BuildDefaultAtom() const
Definition: Element_MQBar.h:219
virtual u32 Diffusability(EventWindow< CC > &ew, SPoint nowAt, SPoint maybeAt) const
Definition: Element_MQBar.h:265
Definition: Element_Res.h:48
Definition: ElementTable.h:43
double GetEuclideanLength() const
Definition: Point.tcc:58
void Message(const char *format,...)
Definition: Logger.h:287
bool SetRelativeAtom(const SPoint &offset, const T &atom)
Definition: EventWindow.tcc:15
virtual u32 PercentMovable(const T &you, const T &me, const SPoint &offset) const
Definition: Element_MQBar.h:235
virtual void Behavior(EventWindow< CC > &window) const
Definition: Element_MQBar.h:269
T GetX() const
Definition: Point.tcc:34