MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Element_Emitter.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Element_Emitter.h Basic data emitting 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 
28 #ifndef ELEMENT_EMITTER_H
29 #define ELEMENT_EMITTER_H
30 
31 #include "Element.h"
32 #include "EventWindow.h"
33 #include "ElementTable.h"
34 #include "Element_Empty.h"
35 #include "Element_Data.h"
37 #include "itype.h"
38 #include "P1Atom.h"
39 
40 namespace MFM
41 {
42 
43 #define EMITTER_VERSION 1
44 #define DATA_CREATE_PER_1000 150
45 
46  template <class CC>
48  {
49  // Extract short names for parameter types
50  typedef typename CC::ATOM_TYPE T;
51  typedef typename CC::PARAM_CONFIG P;
52  enum { R = P::EVENT_WINDOW_RADIUS };
53 
54  public:
55  // Element Data Slot names
56  enum {
57  DATUMS_EMITTED_SLOT,
58  DATUMS_REJECTED_SLOT,
59  DATA_SLOT_COUNT
60  };
61 
62  static Element_Emitter THE_INSTANCE;
63 
64  virtual T BuildDefaultAtom() const {
65  T defaultAtom(this->GetType(), 0, 0, AbstractElement_Reprovert<CC>::STATE_BITS);
66  this->SetGap(defaultAtom,2);
67  return defaultAtom;
68  }
69 
70  Element_Emitter() :
71  AbstractElement_Reprovert<CC>(MFM_UUID_FOR("Emitter", EMITTER_VERSION))
72  {
74  Element<CC>::SetName("Emitter");
75  }
76 
77  virtual u32 PercentMovable(const T& you, const T& me, const SPoint& offset) const
78  {
79  return 0;
80  }
81 
82  virtual u32 DefaultPhysicsColor() const
83  {
84  return 0xff808080;
85  }
86 
87  virtual u32 DefaultLowlightColor() const
88  {
89  return 0xff404040;
90  }
91 
92  virtual const char* GetDescription() const
93  {
94  return "This vertically-reproducing Element emits DATA atoms with randomly generated "
95  "payloads.";
96  }
97 
98  virtual void Behavior(EventWindow<CC>& window) const
99  {
100  Random & random = window.GetRandom();
101 
102  this->ReproduceVertically(window);
103 
104  if(random.OddsOf(DATA_CREATE_PER_1000,1000))
105  {
106  Tile<CC> & tile = window.GetTile();
107  ElementTable<CC> & et = tile.GetElementTable();
108 
109  u64 * datap = et.GetDataAndRegister(this->GetType(), DATA_SLOT_COUNT);
110  ++datap[DATUMS_EMITTED_SLOT]; // Count emission attempts
111 
112  // Pick random nearest empty, if any
113  const MDist<R> & md = MDist<R>::get();
114  for (u32 ring = 1; ring <= 2; ++ring)
115  {
116  u32 emptiesFound = 0;
117  SPoint emptyPoint;
118  for (u32 idx = md.GetFirstIndex(ring); idx <= md.GetLastIndex(ring); ++idx)
119  {
120  const SPoint sp = md.GetPoint(idx);
121  if (!window.IsLiveSite(sp))
122  {
123  continue;
124  }
125  const T other = window.GetRelativeAtom(sp);
126  const u32 otherType = other.GetType();
127  bool isEmpty = otherType == Element_Empty<CC>::THE_INSTANCE.GetType();
128  if (isEmpty && random.OneIn(++emptiesFound))
129  emptyPoint = sp;
130  }
131  if (emptiesFound > 0)
132  {
134  Element_Data<CC>::THE_INSTANCE.SetDatum(atom,random.Between(DATA_MINVAL, DATA_MAXVAL));
135  window.SetRelativeAtom(emptyPoint, atom);
136  return;
137  }
138  }
139 
140  ++datap[DATUMS_REJECTED_SLOT]; // Opps, no room at the inn
141  }
142  }
143  };
144 
145  template <class CC>
146  Element_Emitter<CC> Element_Emitter<CC>::THE_INSTANCE;
147 }
148 
149 #endif /* ELEMENT_EMITTER_H */
u32 GetFirstIndex(const u32 radius) const
Definition: MDist.h:112
u32 GetType() const
Definition: Element.h:290
Definition: Element_Empty.h:41
Definition: Element_Data.h:47
Definition: Random.h:45
u32 GetLastIndex(const u32 radius) const
Definition: MDist.h:129
Tile< CC > & GetTile()
Definition: EventWindow.h:132
void SetName(const char *name)
Definition: Element.h:209
void SetAtomicSymbol(const char *symbol)
Definition: Element.h:193
virtual T BuildDefaultAtom() const
Definition: Element_Emitter.h:64
bool IsLiveSite(const SPoint &location) const
Definition: EventWindow.h:148
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
bool OddsOf(u32 thisMany, u32 outOfThisMany)
Definition: Random.h:183
virtual const T & GetDefaultAtom() const
Definition: Element_Data.h:102
Definition: AbstractElement_Reprovert.h:42
Definition: MDist.h:69
virtual void Behavior(EventWindow< CC > &window) const
Definition: Element_Emitter.h:98
static MDist< R > & get()
Definition: MDist.tcc:193
virtual u32 PercentMovable(const T &you, const T &me, const SPoint &offset) const
Definition: Element_Emitter.h:77
Definition: EventWindow.h:41
Definition: ElementTable.h:46
virtual u32 DefaultLowlightColor() const
Definition: Element_Emitter.h:87
ElementTable< CC > & GetElementTable()
Definition: Tile.h:599
s32 Between(s32 min, s32 max)
Definition: Random.h:188
Definition: ElementTable.h:43
virtual u32 DefaultPhysicsColor() const
Definition: Element_Emitter.h:82
bool SetRelativeAtom(const SPoint &offset, const T &atom)
Definition: EventWindow.tcc:15
Definition: Element_Emitter.h:47
virtual const char * GetDescription() const
Definition: Element_Emitter.h:92