MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Element.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Element.h Base of all MFM elemental types
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_H
29 #define ELEMENT_H
30 
31 #include "Atom.h"
32 #include "Parameter.h"
33 #include "itype.h"
34 #include "StaticLoader.h"
35 #include "EventWindow.h"
36 #include "UUID.h"
37 #include "Dirs.h"
38 #include "Logger.h"
39 #include "BitField.h"
40 
41 namespace MFM
42 {
43  typedef u32 ElementType;
44 
45  template <class CC> class Atom; // FORWARD
46 
50  template <class CC>
51  class Element
52  {
53  private:
54  typedef typename CC::ATOM_TYPE T;
55  typedef typename CC::PARAM_CONFIG P;
56  enum { R = P::EVENT_WINDOW_RADIUS };
57 
62  ElementParameters<CC> m_elementParameters;
63 
68  AtomicParameters<CC> m_atomicParameters;
69 
74  const UUID m_UUID;
75 
79  u32 m_type;
80 
85  bool m_hasType;
86 
91  bool m_renderLowlight;
92 
97  T m_defaultAtom;
98 
103  const char* m_atomicSymbol;
104 
108  const char* m_name;
109 
110  protected:
111 
119  virtual T BuildDefaultAtom() const
120  {
121  T defaultAtom(this->GetType(), 0, 0, 0);
122  return defaultAtom;
123  }
124 
132  const BitVector<P::BITS_PER_ATOM> & GetBits(const T & atom) const
133  {
134  return atom.m_bits;
135  }
136 
145  {
146  return atom.m_bits;
147  }
148 
159  bool IsValidAtomicSymbol(const char* symbol)
160  {
161  MFM_API_ASSERT_NONNULL(symbol);
162 
163  switch (strlen(symbol))
164  {
165  case 2:
166  if (!(symbol[1] >= 'a' && symbol[1] <= 'z'))
167  {
168  return false;
169  }
170  // FALL THROUGH
171 
172  case 1:
173  if (symbol[0] >= 'A' && symbol[0] <= 'Z')
174  {
175  return true;
176  }
177  // FALL THROUGH
178 
179  default:
180  return false;
181  }
182  }
183 
193  void SetAtomicSymbol(const char* symbol)
194  {
195  if(!IsValidAtomicSymbol(symbol))
196  {
197  /* Single or double letters only, like the periodic table. */
198  FAIL(ILLEGAL_ARGUMENT);
199  }
200  m_atomicSymbol = symbol;
201  }
202 
209  void SetName(const char* name)
210  {
211  m_name = name;
212  }
213 
222  void Diffuse(EventWindow<CC>& window) const;
223 
224 
234  virtual u32 PercentMovable(const T& you,
235  const T& me, const SPoint& offset) const = 0;
236 
237  public:
238 
246  Element(const UUID & uuid) : m_UUID(uuid), m_type(0),
247  m_hasType(false),
248  m_renderLowlight(false),
249  m_atomicSymbol("!!"),
250  m_name("UNNAMED")
251  {
252  LOG.Debug("Constructed %@",&m_UUID);
253  }
254 
255  /*
256  // For use by Element_Empty only!
257  Element(const UUID & uuid, u32 type) : m_UUID(uuid), m_type(type),
258  m_hasType(true),
259  m_renderLowlight(false),
260  m_atomicSymbol("!!")
261  { }
262  */
263 
274  {
275  if (!m_hasType)
276  {
277  m_type = StaticLoader<CC,16>::AllocateType(m_UUID);
278  m_hasType = true;
279  m_defaultAtom = BuildDefaultAtom();
280  }
281  }
282 
290  u32 GetType() const
291  {
292  if (!m_hasType)
293  {
294  FAIL(ILLEGAL_STATE);
295  }
296  return m_type;
297  }
298 
305  const char* GetAtomicSymbol() const
306  {
307  return m_atomicSymbol;
308  }
309 
316  const char* GetName() const
317  {
318  return m_name;
319  }
320 
331  virtual void AppendDescription(const T* atom, OString64& desc) const
332  {
333  /* Default to no description */
334  }
335 
345  bool IsType(u32 type) const
346  {
347  return GetType() == type;
348  }
349 
355  const UUID & GetUUID() const
356  {
357  return m_UUID;
358  }
359 
368  virtual void Behavior(EventWindow<CC>& window) const = 0;
369 
382  virtual const T & GetDefaultAtom() const
383  {
384  if (!m_hasType)
385  {
386  FAIL(ILLEGAL_STATE);
387  }
388  return m_defaultAtom;
389  }
390 
398  virtual u32 PhysicsColor() const
399  {
400  if(m_renderLowlight)
401  {
402  return DefaultLowlightColor();
403  }
404  else
405  {
406  return DefaultPhysicsColor();
407  }
408  }
409 
419  virtual const char* GetDescription() const
420  {
421  return "NO DESCRIPTION";
422  }
423 
431  virtual u32 DefaultPhysicsColor() const = 0;
432 
441  virtual u32 DefaultLowlightColor() const
442  {
443  u8 r, g, b;
444  u32 oc = DefaultPhysicsColor();
445 
446  /* Shift them one more to divide by 2 */
447  r = (oc & 0x00ff0000) >> 17;
448  g = (oc & 0x0000ff00) >> 9;
449  b = (oc & 0x000000ff) >> 1;
450 
451  return 0xff000000 | (r << 16) | (g << 8) | b;
452  }
453 
459  {
460  m_renderLowlight = !m_renderLowlight;
461  }
462 
478  virtual u32 LocalPhysicsColor(const T& atom, u32 selector) const
479  {
480  return PhysicsColor();
481  }
482 
497  virtual u32 Diffusability(EventWindow<CC> & ew, SPoint nowAt, SPoint maybeAt) const
498  {
499  return COMPLETE_DIFFUSABILITY;
500  }
501 
508  u32 NoDiffusability(EventWindow<CC> & ew, SPoint nowAt, SPoint maybeAt) const
509  {
510  return (nowAt == maybeAt)? COMPLETE_DIFFUSABILITY : 0;
511  }
512 
516  static const u32 COMPLETE_DIFFUSABILITY = 1000;
517 
518  const ElementParameters<CC> & GetElementParameters() const
519  {
520  return m_elementParameters;
521  }
522 
523  ElementParameters<CC> & GetElementParameters()
524  {
525  return m_elementParameters;
526  }
527 
528  const AtomicParameters<CC> & GetAtomicParameters() const
529  {
530  return m_atomicParameters;
531  }
532 
533  AtomicParameters<CC> & GetAtomicParameters()
534  {
535  return m_atomicParameters;
536  }
537 #if 0
538 <<<<<<< Updated upstream
539 
540  };
541 
542  template <class CC, u32 R>
543  struct ElementParameterNeighborhood : public Parameters::Neighborhood<R>
544  {
545  ElementParameterNeighborhood(Element<CC> * elt,
546  const char * tag,
547  const char * name,
548  const char * description)
549  : Parameters::Neighborhood<R>(elt->GetElementParameters(), tag, name, description)
550  {
551  }
552  };
553 
554  template <class CC>
555  struct ElementParameterS32 : public Parameters::S32
556  {
557  ElementParameterS32(Element<CC> * elt,
558  const char * tag,
559  const char * name,
560  const char * description,
561  s32 min, s32 initial, s32 max, s32 snap)
562  : Parameters::S32(elt->GetElementParameters(), tag, name, description, min, initial, max, snap)
563  {
564  }
565 =======
566 >>>>>>> Stashed changes
567 #endif
568  };
569 }
570 
571 #include "Element.tcc"
572 
573 #endif /* ELEMENT_H */
void Diffuse(EventWindow< CC > &window) const
Definition: Element.tcc:73
u32 GetType() const
Definition: Element.h:290
Definition: Parameter.h:48
void AllocateType()
Definition: Element.h:273
virtual const char * GetDescription() const
Definition: Element.h:419
static const u32 COMPLETE_DIFFUSABILITY
Definition: Element.h:516
u32 NoDiffusability(EventWindow< CC > &ew, SPoint nowAt, SPoint maybeAt) const
Definition: Element.h:508
void ToggleLowlightPhysicsColor()
Definition: Element.h:458
virtual u32 DefaultPhysicsColor() const =0
void SetName(const char *name)
Definition: Element.h:209
void SetAtomicSymbol(const char *symbol)
Definition: Element.h:193
const char * GetName() const
Definition: Element.h:316
virtual u32 Diffusability(EventWindow< CC > &ew, SPoint nowAt, SPoint maybeAt) const
Definition: Element.h:497
const UUID & GetUUID() const
Definition: Element.h:355
virtual const T & GetDefaultAtom() const
Definition: Element.h:382
virtual u32 PercentMovable(const T &you, const T &me, const SPoint &offset) const =0
virtual void Behavior(EventWindow< CC > &window) const =0
bool IsType(u32 type) const
Definition: Element.h:345
void Debug(const char *format,...)
Definition: Logger.h:301
Element(const UUID &uuid)
Definition: Element.h:246
virtual u32 LocalPhysicsColor(const T &atom, u32 selector) const
Definition: Element.h:478
virtual T BuildDefaultAtom() const
Definition: Element.h:119
Definition: ElementTable.h:43
const char * GetAtomicSymbol() const
Definition: Element.h:305
bool IsValidAtomicSymbol(const char *symbol)
Definition: Element.h:159
Definition: BitVector.h:47
virtual u32 PhysicsColor() const
Definition: Element.h:398
Definition: UUID.h:44
Definition: StaticLoader.h:58
const BitVector< P::BITS_PER_ATOM > & GetBits(const T &atom) const
Definition: Element.h:132
virtual void AppendDescription(const T *atom, OString64 &desc) const
Definition: Element.h:331
virtual u32 DefaultLowlightColor() const
Definition: Element.h:441
BitVector< P::BITS_PER_ATOM > & GetBits(T &atom) const
Definition: Element.h:144
Definition: Point.h:45