1 #ifndef ELEMENT_MQBAR_H
2 #define ELEMENT_MQBAR_H
17 #define QBAR_VERSION 4
52 typedef typename CC::ATOM_TYPE T;
53 typedef typename CC::PARAM_CONFIG P;
54 enum { R = P::EVENT_WINDOW_RADIUS };
57 static u32 toMag(u32 value) {
58 const u32 MAX = (1<<TVBITS)-1;
60 FAIL(ILLEGAL_ARGUMENT);
69 static u32 fromMag(
const u32 value) {
70 const u32 MASK = (1<<TVBITS)-1;
75 template <u32 TXBITS, u32 TYBITS>
76 static u32 toUTiny(
const UPoint & v) {
77 u32 x = toMag<TXBITS>(v.
GetX());
78 u32 y = toMag<TYBITS>(v.
GetY());
82 template <u32 TXBITS, u32 TYBITS>
83 static UPoint toUPoint(
const u32 bits) {
85 u32 x = fromMag<TXBITS>(bits>>TYBITS);
86 u32 y = fromMag<TYBITS>(bits);
93 static const u32 TYPE() {
97 static bool IsOurType(u32 type) {
101 static const u32 BITS_WIDE = 5;
102 static const u32 BITS_HIGH = 7;
103 static const u32 BITS_SYMI = 2;
105 static const u32 BITS_TIMER = 4;
106 static const u32 MAX_TIMER_VALUE = 9;
108 static const u32 BITS_BAR_COORD_LEN = BITS_WIDE + BITS_HIGH;
118 static const u32 STATE_BITS_END = AFTimer::END;
119 static const u32 STATE_BITS_COUNT = STATE_BITS_END - STATE_BITS_START + 1;
129 u32 GetSymI(
const T &atom)
const {
130 if (!IsOurType(atom.GetType()))
135 SPoint GetMax(
const T &atom)
const {
136 if (!IsOurType(atom.GetType()))
141 SPoint GetPos(
const T &atom)
const {
142 if (!IsOurType(atom.GetType()))
147 u32 GetTimer(
const T &atom)
const {
148 if (!IsOurType(atom.GetType()))
153 bool FitsInRep(
const SPoint & v)
const {
157 void SetSize(T &atom,
const SPoint & v)
const {
158 if (!IsOurType(atom.GetType()))
161 FAIL(ILLEGAL_ARGUMENT);
165 void SetPos(T &atom,
const SPoint v)
const {
166 if (!IsOurType(atom.GetType()))
169 FAIL(ILLEGAL_ARGUMENT);
173 void SetSymI(T &atom,
const u32 sym)
const {
174 if (!IsOurType(atom.GetType()))
176 if (sym >= PSYM_SYMMETRY_COUNT)
177 FAIL(ILLEGAL_ARGUMENT);
181 void SetTimer(T &atom,
const u32 tmr)
const {
182 if (!IsOurType(atom.GetType()))
184 if (tmr > MAX_TIMER_VALUE)
185 FAIL(ILLEGAL_ARGUMENT);
191 static T defaultAtom(TYPE(), 0, 0, STATE_BITS_COUNT);
192 const SPoint QBAR_SIZE(27, 3 * 27);
193 SetSize(defaultAtom, QBAR_SIZE);
194 SetPos(defaultAtom, QBAR_SIZE / 4);
195 SetSymI(defaultAtom, 0);
225 SPoint barMax = GetMax(atom);
226 SPoint myPos = GetPos(atom);
227 return ColorMap_SEQ6_PuRd::THE_INSTANCE.
234 u32 timer = GetTimer(atom);
235 return ColorMap_SEQ6_GnBu::THE_INSTANCE.
253 const u32 selfType =
self.GetType();
254 if (!IsOurType(selfType)) FAIL(ILLEGAL_STATE);
256 u32 ourTimer = this->GetTimer(
self);
259 u32 symi = this->GetSymI(
self);
264 SPoint barMax = this->GetMax(
self);
265 SPoint myPos = this->GetPos(
self);
270 u32 inconsistentCount = 0;
273 u32 consistentCount = 0;
282 bool neighborTimer =
false;
288 const SPoint sp = md.GetPoint(idx);
291 bool onGrid = (sp.
GetX()&1)==0 && (sp.
GetY()&1)==0;
294 const SPoint theirBarPos = sp+myPos;
304 const u32 otherType = other.GetType();
310 if (random.
OneIn(++makeCount)) {
313 SetPos(makeGuy,theirBarPos);
314 SetTimer(makeGuy, 0);
320 if (IsOurType(otherType)) {
324 SPoint otherBarMax = GetMax(other);
325 SPoint otherPos = GetPos(other);
327 SPoint otherPosMapped = otherPos-sp;
328 u32 otherTimer = GetTimer(other);
330 if (otherBarMax==barMax && otherPosMapped == myPos) {
334 if (!neighborTimer || otherTimer < minTimer)
335 minTimer = otherTimer;
336 if (!neighborTimer || otherTimer > maxTimer)
337 maxTimer = otherTimer;
338 neighborTimer =
true;
341 u32 otherTimer = GetTimer(other);
344 if (otherTimer < ourTimer && random.
OneIn(++inconsistentCount)) {
361 const u32 otherType = other.GetType();
366 if (random.
OneIn(++eatCount)) {
374 if (isEmpty) ++consistentCount;
376 bool iQBar = IsOurType(otherType);
378 u32 iQBarTimer = GetTimer(other);
379 if (iQBarTimer < ourTimer && random.
OneIn(++inconsistentCount)) {
393 if (inconsistentCount > 0) {
396 if (consistentCount > 3 * inconsistentCount) {
401 }
else if (inconsistentCount > 3 * consistentCount) {
411 u32 ourTimer = GetTimer(
self);
413 if (neighborTimer && minTimer < ourTimer)
417 SetTimer(
self,ourTimer);
424 if (makeCount > 0 && eatCount > 0) {
430 if (myPos == barMax-
SPoint(1,1)) {
434 const u32 offType = offEnd.GetType();
437 u32 symi = GetSymI(
self);
439 if (symi > PSYM_DEG270L)
441 SetSymI(corner,symi);
442 SetPos(corner,
SPoint(0,0));
450 u32 ourTimer = GetTimer(
self);
451 if (ourTimer < MAX_TIMER_VALUE && (ourTimer==0 || (neighborTimer && minTimer+1 >= ourTimer))) {
452 if (random.
OneIn(ourTimer+1)) {
454 SetTimer(
self,ourTimer);
467 Element_MQBar<CC> Element_MQBar<CC>::THE_INSTANCE;
u32 GetFirstIndex(const u32 radius) const
Definition: MDist.h:112
static void Write(BV &bv, u32 val)
Definition: BitField.h:84
Definition: Element_MQBar.h:49
virtual u32 DefaultLowlightColor() const
Definition: Element_MQBar.h:210
virtual const T & GetDefaultAtom() const
Definition: Element_MQBar.h:189
u32 GetType() const
Definition: Element.h:290
virtual u32 DefaultPhysicsColor() const
Definition: Element_MQBar.h:205
Definition: Element_Empty.h:41
static u32 Read(const BV &bv)
Definition: BitField.h:73
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 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:221
static MDist< R > & get()
Definition: MDist.tcc:193
virtual T BuildDefaultAtom() const
Definition: Element_MQBar.h:199
virtual u32 Diffusability(EventWindow< CC > &ew, SPoint nowAt, SPoint maybeAt) const
Definition: Element_MQBar.h:245
Definition: Element_Res.h:48
Definition: ElementTable.h:43
Definition: BitVector.h:47
double GetEuclideanLength() const
Definition: Point.tcc:58
void Message(const char *format,...)
Definition: Logger.h:287
const BitVector< P::BITS_PER_ATOM > & GetBits(const T &atom) const
Definition: Element.h:132
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:215
virtual void Behavior(EventWindow< CC > &window) const
Definition: Element_MQBar.h:249
T GetX() const
Definition: Point.tcc:34