MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Element_Creg.h
1 /* -*- mode:C++ -*-
2  Element_Creg.h Configurable density regulator
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 
27 #ifndef ELEMENT_CREG_H
28 #define ELEMENT_CREG_H
29 
30 #include "Element.h"
31 #include "EventWindow.h"
32 #include "ElementTable.h"
33 #include "itype.h"
34 
35 namespace MFM
36 {
37 
38 #define CREG_VERSION 1
39 
40  template <class CC>
41  class Element_Creg : public Element<CC>
42  {
43  // Extract short names for parameter types
44  typedef typename CC::ATOM_TYPE T;
45  typedef typename CC::PARAM_CONFIG P;
46  enum { R = P::EVENT_WINDOW_RADIUS };
47 
48  private:
49  ElementParameterS32<CC> m_targetDensity;
50 
51  public:
52  static Element_Creg<CC> THE_INSTANCE;
53 
54  Element_Creg()
55  : Element<CC>(MFM_UUID_FOR("Creg", CREG_VERSION)),
56  m_targetDensity(this, "density", "Target Density",
57  "The Creg will try to fill this many spots in its event "
58  "window with other Creg.", 0, 3, 41/*, 1*/)
59 
60  {
62  Element<CC>::SetName("Creg");
63  }
64 
65  virtual u32 PercentMovable(const T& you,
66  const T& me, const SPoint& offset) const
67  {
68  return 100;
69  }
70 
71  virtual u32 DefaultPhysicsColor() const
72  {
73  return 0xffff8300;
74  }
75 
76  virtual u32 DefaultLowlightColor() const
77  {
78  return 0xff774100;
79  }
80 
81  virtual const char* GetDescription() const
82  {
83  return "The Configurable Regulator regulates the density of the universe based "
84  "on a configurable parameter.";
85  }
86 
87  virtual void Behavior(EventWindow<CC>& window) const
88  {
89  const MDist<R> & md = MDist<R>::get();
90  Random& rand = window.GetRandom();
91 
92  SPoint cregAtom;
93  s32 cregCount = 0;
94  SPoint nonCregAtom;
95  s32 nonCregCount = 0;
96 
97  for(u32 i = md.GetFirstIndex(0); i <= md.GetLastIndex(R); i++)
98  {
99  const SPoint& rel = md.GetPoint(i);
100  const T& atom = window.GetRelativeAtom(rel);
101 
103  {
104  continue; /* We're not going to destroy walls.*/
105  }
106 
108  {
109  cregCount++;
110  if(rand.OneIn(cregCount))
111  {
112  cregAtom = rel;
113  }
114  }
115  else
116  {
117  nonCregCount++;
118  if(rand.OneIn(nonCregCount))
119  {
120  nonCregAtom = rel;
121  }
122  }
123  }
124 
125  if(cregCount > (u32) m_targetDensity.GetValue())
126  {
128  }
129  else if(cregCount < m_targetDensity.GetValue())
130  {
132  }
133  }
134  };
135 
136  template <class CC>
137  Element_Creg<CC> Element_Creg<CC>::THE_INSTANCE;
138 
139 }
140 
141 #endif /* ELEMENT_CREG_H */
u32 GetFirstIndex(const u32 radius) const
Definition: MDist.h:112
u32 GetType() const
Definition: Element.h:290
Definition: Element_Empty.h:41
Definition: Random.h:45
virtual const char * GetDescription() const
Definition: Element_Creg.h:81
u32 GetLastIndex(const u32 radius) const
Definition: MDist.h:129
Definition: Atom.h:71
void SetName(const char *name)
Definition: Element.h:209
void SetAtomicSymbol(const char *symbol)
Definition: Element.h:193
bool OneIn(u32 odds)
Definition: Random.h:96
Random & GetRandom()
Definition: EventWindow.h:122
const T & GetRelativeAtom(const SPoint &offset) const
Definition: EventWindow.tcc:26
Definition: Element_Creg.h:41
Definition: Parameter.h:593
virtual u32 DefaultPhysicsColor() const
Definition: Element_Creg.h:71
virtual const T & GetDefaultAtom() const
Definition: Element.h:382
virtual void Behavior(EventWindow< CC > &window) const
Definition: Element_Creg.h:87
virtual u32 PercentMovable(const T &you, const T &me, const SPoint &offset) const
Definition: Element_Creg.h:65
Definition: MDist.h:69
static MDist< R > & get()
Definition: MDist.tcc:193
Definition: Element_Wall.h:42
Definition: ElementTable.h:43
bool SetRelativeAtom(const SPoint &offset, const T &atom)
Definition: EventWindow.tcc:15
virtual u32 DefaultLowlightColor() const
Definition: Element_Creg.h:76
Definition: Atom.h:43