MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Element_Indexed.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Element_Indexed.h An element with a 'unique' id
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_INDEXED_H
29 #define ELEMENT_INDEXED_H
30 
31 #include "Element.h"
32 #include "EventWindow.h"
33 #include "ElementTable.h"
34 #include "itype.h"
35 #include "P3Atom.h"
36 #include "ColorMap.h"
37 
38 namespace MFM
39 {
40 
44  template <class CC>
45  class Element_Indexed : public Element<CC>
46  {
47  // Extract short names for parameter types
48  typedef typename CC::ATOM_TYPE T;
49  typedef typename CC::PARAM_CONFIG P;
50 
51  public:
53  enum {
54  ELT_VERSION = 1,
55  BITS_IN_INDEX = 24
56  };
58 
59  static Element_Indexed THE_INSTANCE;
60  static const u32 TYPE() {
61  return THE_INSTANCE.GetType();
62  }
63 
64  Element_Indexed() : Element<CC>(MFM_UUID_FOR("Indexed", ELT_VERSION))
65  {
67  Element<CC>::SetName("Indexed");
68  }
69 
70  virtual u32 PercentMovable(const T& you,
71  const T& me, const SPoint& offset) const
72  {
73  return 100;
74  }
75 
76  virtual u32 DefaultPhysicsColor() const
77  {
78  return 0xffff00ff;
79  }
80 
81  virtual u32 DefaultLowlightColor() const
82  {
83  return 0xffaa00aa;
84  }
85 
86  virtual u32 LocalPhysicsColor(const T & atom, u32 selector) const
87  {
88  switch (selector) {
89  case 1: {
90  u32 idx = AFIndex::Read(this->GetBits(atom));
91  return ColorMap_DIV6_RdYlGn::THE_INSTANCE.
92  GetSelectedColor(idx%6, 0, 5, 0xffff0000);
93  }
94  default:
95  return DefaultPhysicsColor();
96  }
97  return 0x0;
98  }
99 
100  virtual const char* GetDescription() const
101  {
102  return "An Element wih an index number.";
103  }
104 
105  virtual void Behavior(EventWindow<CC>& window) const
106  {
107  T self = window.GetCenterAtom();
108 
109  LOG.Debug("IDX#%3d@%p(%2d,%2d)",
110  AFIndex::Read(this->GetBits(self)),
111  (void*) &window.GetTile(),
112  window.GetCenterInTile().GetX(),
113  window.GetCenterInTile().GetY());
114 
115  this->Diffuse(window);
116  }
117 
118  virtual const T & GetDefaultAtom() const
119  {
120  static T defaultAtom(TYPE(), 0, 0, BITS_IN_INDEX);
121  static u32 index = 0;
122  AFIndex::Write(this->GetBits(defaultAtom), ++index);
123  return defaultAtom;
124  }
125 
126  };
127 
128  template <class CC>
129  Element_Indexed<CC> Element_Indexed<CC>::THE_INSTANCE;
130 
131 }
132 
133 #endif /* ELEMENT_INDEXED_H */
static void Write(BV &bv, u32 val)
Definition: BitField.h:84
void Diffuse(EventWindow< CC > &window) const
Definition: Element.tcc:73
u32 GetType() const
Definition: Element.h:290
static u32 Read(const BV &bv)
Definition: BitField.h:73
virtual u32 LocalPhysicsColor(const T &atom, u32 selector) const
Definition: Element_Indexed.h:86
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
Definition: Atom.h:45
const T & GetCenterAtom() const
Definition: EventWindow.h:209
const SPoint & GetCenterInTile() const
Definition: EventWindow.h:180
virtual u32 PercentMovable(const T &you, const T &me, const SPoint &offset) const
Definition: Element_Indexed.h:70
Definition: Element_Indexed.h:45
virtual u32 DefaultLowlightColor() const
Definition: Element_Indexed.h:81
virtual const T & GetDefaultAtom() const
Definition: Element_Indexed.h:118
void Debug(const char *format,...)
Definition: Logger.h:301
virtual const char * GetDescription() const
Definition: Element_Indexed.h:100
virtual void Behavior(EventWindow< CC > &window) const
Definition: Element_Indexed.h:105
Definition: ElementTable.h:43
Definition: BitVector.h:47
const BitVector< P::BITS_PER_ATOM > & GetBits(const T &atom) const
Definition: Element.h:132
virtual u32 DefaultPhysicsColor() const
Definition: Element_Indexed.h:76
Definition: Atom.h:43