MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Element_Shark.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Element_Shark.h An element that acts similarly to a Wa-Tor shark
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_SHARK_H
29 #define ELEMENT_SHARK_H
30 
31 #include "Element.h"
32 #include "EventWindow.h"
33 #include "ElementTable.h"
34 #include "itype.h"
35 #include "Atom.h"
36 #include "AbstractElement_WaPat.h"
37 #include "Element_Fish.h"
38 
39 namespace MFM
40 {
41 
42  template <class CC>
44  {
45  // Short names for params
46  typedef typename CC::ATOM_TYPE T;
47  typedef typename CC::PARAM_CONFIG P;
48  enum {
49  R = P::EVENT_WINDOW_RADIUS,
50  BITS = P::BITS_PER_ATOM,
51 
53  SHARK_ENERGY_LEN = 9,
54 
55  MAX_SHARK_ENERGY = (1<<SHARK_ENERGY_LEN) - 1,
56 
57  INITIAL_DEFAULT_BIRTH_AGE = 15,
58  DEFAULT_ENERGY_PER_FISH = 8
59  };
60 
61  ElementParameterS32<CC> m_sharkBirthAge;
62  ElementParameterS32<CC> m_sharkEnergyPerFish;
63 
64  public:
65 
66  enum
67  {
68  ELEMENT_VERSION = 1
69  };
70 
71  typedef BitField<BitVector<BITS>, VD::U32, SHARK_ENERGY_LEN, SHARK_ENERGY_POS> AFSharkEnergy;
72 
73  u32 GetSharkEnergy(const T& us) const
74  {
75  return AFSharkEnergy::Read(this->GetBits(us));
76  }
77 
78  void SetSharkEnergy(T& us, const u32 age) const
79  {
80  AFSharkEnergy::Write(this->GetBits(us), age);
81  }
82 
83  static Element_Shark THE_INSTANCE;
84  static const u32 TYPE()
85  {
86  return THE_INSTANCE.GetType();
87  }
88 
89  Element_Shark() :
90  AbstractElement_WaPat<CC>(MFM_UUID_FOR("Shark", ELEMENT_VERSION)),
91  m_sharkBirthAge(this, "age", "Birth Age",
92  "Number of events for a shark to mature",
93  1, INITIAL_DEFAULT_BIRTH_AGE, 100/*, 1*/),
94  m_sharkEnergyPerFish(this, "energy", "Energy Per Fish",
95  "Life events gained per fish eaten",
96  1, DEFAULT_ENERGY_PER_FISH, 50/*, 1*/)
97  {
99  Element<CC>::SetName("Shark");
100  }
101 
102  virtual const T & GetDefaultAtom() const
103  {
104  static T defaultAtom(TYPE(),0,0,0);
105 
106  this->SetBirthAge(defaultAtom, m_sharkBirthAge.GetValue());
107  this->SetCurrentAge(defaultAtom, 0);
108  this->SetSharkEnergy(defaultAtom, m_sharkEnergyPerFish.GetValue());
109 
110  return defaultAtom;
111  }
112 
113  virtual u32 DefaultPhysicsColor() const
114  {
115  return 0xffff2244;
116 
117  }
118 
119  // Non-diffusable
120  virtual u32 Diffusability(EventWindow<CC> & ew, SPoint nowAt, SPoint maybeAt) const
121  {
122  return nowAt.Equals(maybeAt)?Element<CC>::COMPLETE_DIFFUSABILITY:0;
123  }
124 
125  virtual u32 PercentMovable(const T& you,
126  const T& me, const SPoint& offset) const
127  {
128  return 0;
129  }
130 
131  virtual void Behavior(EventWindow<CC>& window) const
132  {
133  T self = window.GetCenterAtom();
134  WindowScanner<CC> scanner(window);
135 
136  SPoint fishRel;
137  u32 fishCount = 0;
138 
139  SPoint emptyRel;
140  u32 emptyCount = 0;
141 
142  u32 energy = GetSharkEnergy(self);
143  bool starved = energy == 0;
144 
145  if (starved)
146  {
148  return;
149  }
150 
151  energy = energy - 1;
152  this->SetSharkEnergy(self, energy);
153 
154  u32 age = this->GetCurrentAge(self);
155  // Don't use genetic birth age (yet): bool reproable = age >= this->GetBirthAge(self);
156  bool reproable = age >= (u32) m_sharkBirthAge.GetValue();
157 
158  if (!reproable)
159  {
160  this->SetCurrentAge(self, 1 + age);
161  }
162 
163  scanner.FindRandomAtoms(1, 2,
164  &fishRel, Element_Fish<CC>::THE_INSTANCE.GetType(), &fishCount,
165  &emptyRel, Element_Empty<CC>::THE_INSTANCE.GetType(), &emptyCount);
166 
167  if (fishCount > 0) // Eating
168  {
169  energy = MIN((u32) MAX_SHARK_ENERGY, energy + m_sharkEnergyPerFish.GetValue());
170 
171  if (reproable)
172  {
173  energy = energy / 2; // parent and kid split it
174  }
175 
176  this->SetSharkEnergy(self, energy);
177 
178  if (reproable)
179  {
180  this->SetCurrentAge(self,0); // reset age counter
181  window.SetCenterAtom(self); // and clone a kid
182  }
183  else // or leave empty behind
184  {
186  }
187  window.SetRelativeAtom(fishRel,self); // move or repro
188  }
189  else if (emptyCount > 0) // If can't eat, but can move
190  {
191  if (reproable) // and leave kid behind
192  {
193  energy = energy / 2; // parent and kid split available energy
194  this->SetSharkEnergy(self, energy);
195  this->SetCurrentAge(self,0);
196  window.SetCenterAtom(self);
197  }
198  else // or leave empty behind
199  {
201  }
202  window.SetRelativeAtom(emptyRel,self); // move or repro
203  }
204  else // Can't move
205  {
206  window.SetCenterAtom(self); // But can age and get hungry
207  }
208  }
209  };
210 
211  template <class CC>
212  Element_Shark<CC> Element_Shark<CC>::THE_INSTANCE;
213 }
214 
215 #endif /* ELEMENT_SHARK_H */
static void Write(BV &bv, u32 val)
Definition: BitField.h:84
u32 GetType() const
Definition: Element.h:290
Definition: Element_Empty.h:41
static u32 Read(const BV &bv)
Definition: BitField.h:73
virtual void Behavior(EventWindow< CC > &window) const
Definition: Element_Shark.h:131
void SetCenterAtom(const T &atom)
Definition: EventWindow.h:220
bool Equals(const Point< T > &rhs) const
Definition: Point.tcc:205
virtual u32 PercentMovable(const T &you, const T &me, const SPoint &offset) const
Definition: Element_Shark.h:125
void SetName(const char *name)
Definition: Element.h:209
void SetAtomicSymbol(const char *symbol)
Definition: Element.h:193
void FindRandomAtoms(const u32 radius, const u32 count,...) const
Definition: WindowScanner.tcc:319
Definition: Parameter.h:593
Definition: Atom.h:45
const T & GetCenterAtom() const
Definition: EventWindow.h:209
Definition: AbstractElement_WaPat.h:39
Definition: Element_Fish.h:42
Definition: ElementTable.h:43
Definition: Element_Shark.h:43
virtual u32 DefaultPhysicsColor() const
Definition: Element_Shark.h:113
const BitVector< P::BITS_PER_ATOM > & GetBits(const T &atom) const
Definition: Element.h:132
Definition: WindowScanner.h:40
virtual u32 Diffusability(EventWindow< CC > &ew, SPoint nowAt, SPoint maybeAt) const
Definition: Element_Shark.h:120
bool SetRelativeAtom(const SPoint &offset, const T &atom)
Definition: EventWindow.tcc:15
virtual const T & GetDefaultAtom() const
Definition: Element_Shark.h:102
Definition: Atom.h:43