MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Element_Data.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Element_Data.h Basic integer holding 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_DATA_H
29 #define ELEMENT_DATA_H
30 
31 #include "Element.h"
32 #include "ColorMap.h"
33 #include "EventWindow.h"
34 #include "ElementTable.h"
35 #include "itype.h"
36 #include "P1Atom.h"
37 
38 #define DATA_MAXVAL 1000000
39 #define DATA_MINVAL 1
40 
41 namespace MFM
42 {
43 
44 #define DATA_VERSION 1
45 
46  template <class CC>
47  class Element_Data : public Element<CC>
48  {
49  // Extract short names for parameter types
50  typedef typename CC::ATOM_TYPE T;
51 
52  public:
53  Element_Data() : Element<CC>(MFM_UUID_FOR("Data", DATA_VERSION))
54  {
56  Element<CC>::SetName("Data");
57  }
58 
59  static Element_Data THE_INSTANCE;
60  static const u32 TYPE()
61  {
62  return THE_INSTANCE.GetType();
63  }
64 
65  static const u32 STATE_DATA_IDX = 0;
66  static const u32 STATE_DATA_LEN = 32;
67 
68  u32 GetDatum(const T &atom, u32 badType) const
69  {
70  if (!Atom<CC>::IsType(atom,TYPE()))
71  {
72  return badType;
73  }
74  return atom.GetStateField(STATE_DATA_IDX,STATE_DATA_LEN);
75  }
76 
77  bool SetDatum(T &atom, u32 value) const
78  {
79  if (!Atom<CC>::IsType(atom,TYPE()))
80  {
81  return false;
82  }
83  atom.SetStateField(STATE_DATA_IDX,STATE_DATA_LEN,value);
84  return true;
85  }
86 
87  virtual void AppendDescription(const T* atomPtr, OString64& desc) const
88  {
89  const T& atom = *atomPtr;
90 
91  u32 datum = GetDatum(atom, 0);
92  if(datum)
93  {
94  desc.Printf("Datum: %d", datum);
95  }
96  else
97  {
98  desc.Printf("Datum: INVALID");
99  }
100  }
101 
102  virtual const T & GetDefaultAtom() const
103  {
104  static T defaultAtom(TYPE(),0,0,STATE_DATA_LEN);
105  static bool initted = false;
106  if (!initted)
107  {
108  SetDatum(defaultAtom, (DATA_MAXVAL + DATA_MINVAL) / 2);
109  initted = true;
110  }
111  return defaultAtom;
112  }
113 
114  virtual u32 DefaultPhysicsColor() const
115  {
116  return 0xff0000ff;
117  }
118 
119  virtual u32 DefaultLowlightColor() const
120  {
121  return 0xff00007f;
122  }
123 
124  virtual u32 PercentMovable(const T& you, const T& me, const SPoint& offset) const
125  {
126  return 100;
127  }
128 
129  virtual const char* GetDescription() const
130  {
131  return "An element designed simply to hold a 32-bit value, usually generated "
132  "at random when created by an emitter atom.";
133  }
134 
135  virtual u32 LocalPhysicsColor(const T & atom, u32 selector) const
136  {
137  switch (selector)
138  {
139  case 1:
140  return ColorMap_CubeHelixRev::THE_INSTANCE.
141  GetInterpolatedColor(GetDatum(atom,0),DATA_MINVAL,DATA_MAXVAL,0xffff0000);
142  default:
143  return Element<CC>::PhysicsColor();
144  }
145  }
146 
147  virtual void Behavior(EventWindow<CC>& window) const
148  {
149  u32 val = GetDatum(window.GetCenterAtom(),-1);
150  if (val < DATA_MINVAL || val > DATA_MAXVAL)
151  {
152  FAIL(ILLEGAL_STATE);
153  }
154 
155  this->Diffuse(window);
156  }
157  };
158 
159  template <class CC>
160  Element_Data<CC> Element_Data<CC>::THE_INSTANCE;
161 
162 }
163 
164 #endif /* ELEMENT_DATA_H */
void Diffuse(EventWindow< CC > &window) const
Definition: Element.tcc:73
u32 GetType() const
Definition: Element.h:290
Definition: Element_Data.h:47
virtual const char * GetDescription() const
Definition: Element_Data.h:129
Definition: Atom.h:71
void SetName(const char *name)
Definition: Element.h:209
void SetAtomicSymbol(const char *symbol)
Definition: Element.h:193
virtual u32 PercentMovable(const T &you, const T &me, const SPoint &offset) const
Definition: Element_Data.h:124
virtual void Behavior(EventWindow< CC > &window) const
Definition: Element_Data.h:147
virtual u32 DefaultPhysicsColor() const
Definition: Element_Data.h:114
const T & GetCenterAtom() const
Definition: EventWindow.h:209
virtual const T & GetDefaultAtom() const
Definition: Element_Data.h:102
virtual void AppendDescription(const T *atomPtr, OString64 &desc) const
Definition: Element_Data.h:87
Definition: ElementTable.h:43
virtual u32 PhysicsColor() const
Definition: Element.h:398
virtual u32 DefaultLowlightColor() const
Definition: Element_Data.h:119
virtual u32 LocalPhysicsColor(const T &atom, u32 selector) const
Definition: Element_Data.h:135
Definition: Atom.h:43