17 #define SBAR_VERSION 1
20 template <
class CC>
class Element_DBar ;
49 class Element_SBar :
public Element<CC>
52 typedef typename CC::ATOM_TYPE T;
53 typedef typename CC::PARAM_CONFIG P;
54 enum { R = P::EVENT_WINDOW_RADIUS };
57 static u32 toSignMag(s32 value) {
58 const u32 SIGN_BIT = 1<<(TVBITS-1);
59 const u32 MAX = SIGN_BIT-1;
67 u32 val = (u32) value;
72 static u32 toMag(u32 value) {
73 const u32 MAX = (1<<TVBITS)-1;
75 FAIL(ILLEGAL_ARGUMENT);
84 static s32 fromSignMag(
const u32 value) {
85 const u32 SIGN_BIT = 1<<(TVBITS-1);
86 const u32 MASK = SIGN_BIT-1;
87 FXP16 val = value&MASK;
94 static u32 fromMag(
const u32 value) {
95 const u32 MASK = (1<<TVBITS)-1;
100 template <u32 TXBITS, u32 TYBITS>
101 static u32 toUTiny(
const UPoint & v) {
102 u32 x = toMag<TXBITS>(v.GetX());
103 u32 y = toMag<TYBITS>(v.GetY());
104 return (x<<TYBITS)|y;
107 template <u32 TXBITS, u32 TYBITS>
108 static UPoint toUPoint(
const u32 bits) {
110 u32 x = fromMag<TXBITS>(bits>>TYBITS);
111 u32 y = fromMag<TYBITS>(bits);
117 static Element_SBar THE_INSTANCE;
118 static const u32 TYPE() {
122 static const u32 TYPE_BITS = 15;
124 static bool IsOurType(u32 type) {
128 static const u32 BITS_WIDE = 5;
129 static const u32 BITS_HIGH = 7;
130 static const u32 BITS_SYMI = 3;
132 static const u32 BITS_BAR_COORD_LEN = BITS_WIDE + BITS_HIGH;
134 static const u32 STATE_SIZE_IDX = 0;
135 static const u32 STATE_SIZE_LEN = BITS_BAR_COORD_LEN;
136 static const u32 STATE_POS_IDX = STATE_SIZE_IDX + STATE_SIZE_LEN;
137 static const u32 STATE_POS_LEN = BITS_BAR_COORD_LEN;
138 static const u32 STATE_SYMI_IDX = STATE_POS_IDX + STATE_POS_LEN;
139 static const u32 STATE_SYMI_LEN = BITS_SYMI;
140 static const u32 STATE_BITS = STATE_SYMI_IDX + STATE_SYMI_LEN;
143 Element<CC>(MFM_UUID_FOR(
"SBar", SBAR_VERSION))
148 u32 GetSymI(
const T &atom)
const {
149 if (!IsOurType(atom.GetType()))
151 return atom.GetStateField(STATE_SYMI_IDX,STATE_SYMI_LEN);
154 SPoint GetMax(
const T &atom)
const {
155 if (!IsOurType(atom.GetType()))
157 return MakeSigned(toUPoint<BITS_WIDE,BITS_HIGH>(atom.GetStateField(STATE_SIZE_IDX,STATE_SIZE_LEN)));
160 SPoint GetPos(
const T &atom)
const {
161 if (!IsOurType(atom.GetType()))
163 return MakeSigned(toUPoint<BITS_WIDE,BITS_HIGH>(atom.GetStateField(STATE_POS_IDX,STATE_POS_LEN)));
166 bool FitsInRep(
const SPoint & v)
const {
167 return v.BoundedBy(SPoint(0,0),SPoint((1<<BITS_WIDE)-1,(1<<BITS_HIGH)-1));
170 void SetSize(T &atom,
const SPoint & v)
const {
171 if (!IsOurType(atom.GetType()))
174 FAIL(ILLEGAL_ARGUMENT);
175 atom.SetStateField(STATE_SIZE_IDX,STATE_SIZE_LEN,toUTiny<BITS_WIDE,BITS_HIGH>(MakeUnsigned(v)));
178 void SetPos(T &atom,
const SPoint v)
const {
179 if (!IsOurType(atom.GetType()))
182 FAIL(ILLEGAL_ARGUMENT);
183 atom.SetStateField(STATE_POS_IDX,STATE_POS_LEN,toUTiny<BITS_WIDE,BITS_HIGH>(MakeUnsigned(v)));
186 void SetSymI(T &atom,
const u32 sym)
const {
187 if (!IsOurType(atom.GetType()))
189 if (sym >= PSYM_SYMMETRY_COUNT)
190 FAIL(ILLEGAL_ARGUMENT);
191 atom.SetStateField(STATE_SYMI_IDX,STATE_SYMI_LEN, sym);
196 static T defaultAtom(TYPE(),0,0,STATE_BITS);
200 T GetAtom(
const SPoint & size,
const SPoint & pos)
const
227 SPoint barMax = GetMax(atom);
228 SPoint myPos = GetPos(atom);
229 return ColorMap_SEQ6_PuRd::THE_INSTANCE.
250 const u32 selfType =
self.GetType();
251 if (!IsOurType(selfType)) FAIL(ILLEGAL_STATE);
254 u32 symi = GetSymI(
self);
259 SPoint barMax = GetMax(
self);
260 SPoint myPos = GetPos(
self);
266 u32 inconsistentCount = 0;
269 u32 consistentCount = 0;
280 const SPoint sp = md.GetPoint(idx);
283 bool onGrid = (sp.
GetX()&1)==0 && (sp.
GetY()&1)==0;
288 const SPoint theirBarPos = sp+myPos;
295 const u32 otherType = other.GetType();
301 if (random.
OneIn(++makeCount)) {
304 SetPos(makeGuy,theirBarPos);
311 if (IsOurType(otherType)) {
315 SPoint otherBarMax = GetMax(other);
316 SPoint otherPos = GetPos(other);
318 SPoint otherPosMapped = otherPos-sp;
319 if (otherBarMax==barMax && otherPosMapped == myPos) {
321 }
else if (random.
OneIn(++inconsistentCount)) {
336 const u32 otherType = other.GetType();
341 if (random.
OneIn(++eatCount)) {
349 if (isEmpty) ++consistentCount;
351 bool isBar = IsOurType(otherType);
353 if (random.
OneIn(++inconsistentCount)) {
366 if (inconsistentCount > 0) {
368 if (consistentCount > 3*inconsistentCount) {
371 }
else if (inconsistentCount > 3*consistentCount) {
377 if (makeCount > 0 && eatCount > 0) {
383 if (myPos == barMax-
SPoint(1,1)) {
387 const u32 offType = offEnd.GetType();
391 u32 symi = GetSymI(
self);
392 symi = (symi+0)%PSYM_SYMMETRY_COUNT;
407 Element_SBar<CC> Element_SBar<CC>::THE_INSTANCE;
u32 GetFirstIndex(const u32 radius) const
Definition: MDist.h:112
u32 GetType() const
Definition: Element.h:290
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 SetAtomicSymbol(const char *symbol)
Definition: Element.h:193
bool BoundedBy(const Point< T > &lowerBound, const Point< T > &upperBound) const
Definition: Point.tcc:223
virtual const T & GetDefaultAtom() const
Definition: Element_SBar.h:194
Definition: Element_DBar.h:59
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
virtual u32 DefaultPhysicsColor() const
Definition: Element_SBar.h:209
const T & GetCenterAtom() const
Definition: EventWindow.h:209
virtual const T & GetDefaultAtom() const
Definition: Element.h:382
virtual u32 DefaultLowlightColor() const
Definition: Element_SBar.h:214
bool IsType(u32 type) const
Definition: Element.h:345
virtual void Behavior(EventWindow< CC > &window) const
Definition: Element_SBar.h:246
Element(const UUID &uuid)
Definition: Element.h:246
static MDist< R > & get()
Definition: MDist.tcc:193
virtual u32 Diffusability(EventWindow< CC > &ew, SPoint nowAt, SPoint maybeAt) const
Definition: Element_SBar.h:242
Definition: Element_Res.h:48
Definition: ElementTable.h:43
virtual u32 PhysicsColor() const
Definition: Element.h:398
double GetEuclideanLength() const
Definition: Point.tcc:58
virtual u32 LocalPhysicsColor(const T &atom, u32 selector) const
Definition: Element_SBar.h:223
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_SBar.h:218
T GetX() const
Definition: Point.tcc:34