MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AbstractElement_Wanderer.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  AbstractElement_Wanderer.h Abstract Element tutorial base class
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 ABSTRACTELEMENT_WANDERER_H
28 #define ABSTRACTELEMENT_WANDERER_H
29 
30 #include "Element.h"
31 #include "EventWindow.h"
32 #include "ElementTable.h"
33 #include "itype.h"
34 #include "P1Atom.h"
35 
36 namespace MFM
37 {
38  template <class CC>
39  class AbstractElement_Wanderer : public Element<CC>
40  {
41  // Extract short names for parameter types
42  typedef typename CC::ATOM_TYPE T;
43  typedef typename CC::PARAM_CONFIG P;
44  enum { R = P::EVENT_WINDOW_RADIUS };
45 
46  protected:
47  virtual u32 GetWanderDistance() const = 0;
48 
49  public:
50 
52  : Element<CC>(u)
53  { }
54 
55  virtual u32 PercentMovable(const T& you,
56  const T& me, const SPoint& offset) const
57  {
58  return 100;
59  }
60 
61  virtual void Behavior(EventWindow<CC>& window) const
62  {
63  SPoint wanderPt;
64  Random& rand = window.GetRandom();
65  Dir d = (Dir)rand.Create(Dirs::DIR_COUNT);
66 
67  Dirs::FillDir(wanderPt, d);
68 
69  wanderPt *= Dirs::IsCorner(d) ? (GetWanderDistance() / 2) : GetWanderDistance();
70 
71  if(window.IsLiveSite(wanderPt))
72  {
73  if(window.GetRelativeAtom(wanderPt).GetType() ==
75  {
76  window.SwapAtoms(wanderPt, SPoint(0, 0));
77  }
78  }
79  }
80  };
81 }
82 
83 #endif /* ABSTRACTELEMENT_WANDERER_H */
u32 GetType() const
Definition: Element.h:290
static bool IsCorner(Dir dir)
Definition: Dirs.h:73
Definition: Element_Empty.h:41
Definition: Random.h:45
bool IsLiveSite(const SPoint &location) const
Definition: EventWindow.h:148
Random & GetRandom()
Definition: EventWindow.h:122
const T & GetRelativeAtom(const SPoint &offset) const
Definition: EventWindow.tcc:26
u32 Create()
Definition: Random.h:161
Definition: ElementTable.h:43
Definition: UUID.h:44
static void FillDir(SPoint &pt, Dir dir)
Definition: Dirs.cpp:22
virtual void Behavior(EventWindow< CC > &window) const
Definition: AbstractElement_Wanderer.h:61
virtual u32 PercentMovable(const T &you, const T &me, const SPoint &offset) const
Definition: AbstractElement_Wanderer.h:55
Definition: AbstractElement_Wanderer.h:39
Definition: Atom.h:43
void SwapAtoms(const SPoint &locA, const SPoint &locB)
Definition: EventWindow.tcc:40