MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Element_Dmover.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Element_Dmover.h Directed mover Element
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
29 #ifndef ELEMENT_DMOVER_H
30 #define ELEMENT_DMOVER_H
31 
32 #include "Element.h"
33 #include "EventWindow.h"
34 #include "ElementTable.h"
35 #include "Atom.h"
36 #include "itype.h"
37 
38 namespace MFM
39 {
40 
41  template <class CC>
42  class Element_Dmover : public Element<CC>
43  {
44  // Extract short names for parameter types
45  typedef typename CC::ATOM_TYPE T;
46  typedef typename CC::PARAM_CONFIG P;
47  enum {
48  R = P::EVENT_WINDOW_RADIUS,
49  ELT_VERSION = 2
50  };
51 
52  private:
53 
54  ElementParameterS32<CC> m_searchRadius;
55 
56  public:
57 
58  static Element_Dmover<CC> THE_INSTANCE;
59 
61  : Element<CC>(MFM_UUID_FOR("Dmover", ELT_VERSION)),
62  m_searchRadius(this, "radius", "Scan radius",
63  "Max radius to search for empty and non-empty sites.", 0, 1, R/*, 1*/)
64  {
66  Element<CC>::SetName("Dmover");
67  }
68 
69  /*
70  Set how likely your element is to be moved by another
71  element. See Element.h for details.
72  */
73  virtual u32 PercentMovable(const T& you,
74  const T& me, const SPoint& offset) const
75  {
76  return 0;
77  }
78 
79  /* This color will be the default rendering color for your element. */
80  virtual u32 DefaultPhysicsColor() const
81  {
82  return 0xffd2b486;
83  }
84 
85  /*
86  This color will be the color rendered when your element is
87  rendered in lowlighting.
88  */
89  virtual u32 DefaultLowlightColor() const
90  {
91  return 0xff2a241c;
92  }
93 
94  /*
95  This is a short description of your element.
96  */
97  virtual const char* GetDescription() const
98  {
99  return "A Directed Mover moves move southern atoms to northern empty sites";
100  }
101 
102  /*
103  This method is executed every time an atom of your element is
104  chosen for an event. See the tutorial in the wiki for further
105  information.
106  */
107  virtual void Behavior(EventWindow<CC>& window) const
108  {
109  Random & random = window.GetRandom();
110  const MDist<R> & md = MDist<R>::get();
111 
112  SPoint emptyRel;
113  u32 emptyCount = 0;
114 
115  SPoint occupiedRel;
116  u32 occupiedCount = 0;
117 
118  for (u32 idx = md.GetFirstIndex(1);
119  idx <= md.GetLastIndex(m_searchRadius.GetValue());
120  ++idx)
121  {
122  const SPoint rel = md.GetPoint(idx);
123  if (!window.IsLiveSite(rel))
124  {
125  continue;
126  }
127  T other = window.GetRelativeAtom(rel);
128  u32 otherType = other.GetType();
129 
130  bool isOtherEmpty = Element_Empty<CC>::THE_INSTANCE.IsType(otherType);
131 
132  if(rel.GetY() < 0 && isOtherEmpty) // negative Y is up
133  {
134  if (random.OneIn(++emptyCount))
135  {
136  emptyRel = rel;
137  }
138  }
139 
140  if (rel.GetY() > 0 && !isOtherEmpty)
141  {
142  if (random.OneIn(++occupiedCount))
143  {
144  occupiedRel = rel;
145  }
146  }
147  }
148 
149  if (emptyCount > 0 && occupiedCount > 0) // We have a move
150  {
151  window.SwapAtoms(emptyRel, occupiedRel);
152  }
153  }
154  };
155 
156  /*
157  The singleton instance of the Dmover element.
158  */
159  template <class CC>
160  Element_Dmover<CC> Element_Dmover<CC>::THE_INSTANCE;
161 
162 }
163 
164 #endif /* ELEMENT_DMOVER_H */
u32 GetFirstIndex(const u32 radius) const
Definition: MDist.h:112
Definition: Element_Empty.h:41
Definition: Random.h:45
u32 GetLastIndex(const u32 radius) const
Definition: MDist.h:129
virtual u32 PercentMovable(const T &you, const T &me, const SPoint &offset) const
Definition: Element_Dmover.h:73
void SetName(const char *name)
Definition: Element.h:209
void SetAtomicSymbol(const char *symbol)
Definition: Element.h:193
virtual const char * GetDescription() const
Definition: Element_Dmover.h:97
Definition: Element_Dmover.h:42
bool IsLiveSite(const SPoint &location) const
Definition: EventWindow.h:148
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
Definition: Parameter.h:593
virtual u32 DefaultLowlightColor() const
Definition: Element_Dmover.h:89
Definition: MDist.h:69
bool IsType(u32 type) const
Definition: Element.h:345
virtual void Behavior(EventWindow< CC > &window) const
Definition: Element_Dmover.h:107
static MDist< R > & get()
Definition: MDist.tcc:193
Definition: ElementTable.h:43
virtual u32 DefaultPhysicsColor() const
Definition: Element_Dmover.h:80
Definition: Atom.h:43
void SwapAtoms(const SPoint &locA, const SPoint &locB)
Definition: EventWindow.tcc:40