MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Grid.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Grid.h Encapsulator for all MFM logic
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 GRID_H
29 #define GRID_H
30 
31 #include "itype.h"
32 #include "Tile.h"
33 #include "ElementTable.h"
34 #include "Random.h"
35 #include "GridConfig.h"
36 #include "ElementRegistry.h"
37 #include "Logger.h"
38 
39 #include "Element_Wall.h"
40 
41 namespace MFM {
42 
46  template <class GC>
47  class Grid
48  {
49  // Extract short type names
50  typedef typename GC::CORE_CONFIG CC;
51  typedef typename CC::ATOM_TYPE T;
52  typedef typename CC::PARAM_CONFIG P;
53  enum { W = GC::GRID_WIDTH};
54  enum { H = GC::GRID_HEIGHT};
55  enum { R = P::EVENT_WINDOW_RADIUS};
56 
57  private:
58  Random m_random;
59 
60  u32 m_seed;
61 
62  void ReinitSeed();
63 
64  const u32 m_width, m_height;
65 
66  SPoint m_lastEventTile;
67 
68  Tile<CC> m_tiles[W][H];
69 
70  bool m_backgroundRadiationEnabled;
71 
73 
74  s32 m_xraySiteOdds;
75 
76  u8 m_gridGeneration;
77 
78  bool m_ignoreThreadingProblems;
79 
83  struct TileControl
84  {
85  virtual const char * GetName() = 0;
86 
87  virtual void MakeRequest(Tile<CC> &) = 0;
88  virtual bool CheckIfReady(Tile<CC> &) = 0;
89  virtual void Execute(Tile<CC> &) = 0;
90 
91  };
92 
96  struct PauseControl : public TileControl
97  {
98  virtual const char * GetName()
99  {
100  return "Pause";
101  }
102 
103  virtual void MakeRequest(Tile<CC> & tile)
104  {
105  tile.RequestPause();
106  }
107  virtual bool CheckIfReady(Tile<CC> & tile)
108  {
109  return tile.IsPauseReady();
110  }
111  virtual void Execute(Tile<CC> & tile)
112  {
113  tile.Pause();
114  }
115  };
116 
120  struct RunControl : public TileControl
121  {
122  virtual const char * GetName()
123  {
124  return "Unpause";
125  }
126 
127  virtual void MakeRequest(Tile<CC> & tile)
128  {
129  tile.Start();
130  }
131  virtual bool CheckIfReady(Tile<CC> & tile)
132  {
133  return tile.IsRunReady();
134  }
135  virtual void Execute(Tile<CC> & tile)
136  {
137  tile.Run();
138  }
139  };
140 
144  void DoTileControl(TileControl & tc);
145 
146  public:
147  void ReportGridStatus(Logger::Level level) ;
148 
149  Random& GetRandom() { return m_random; }
150 
151  bool* GetBackgroundRadiationEnabledPointer()
152  {
153  return &m_backgroundRadiationEnabled;
154  }
155 
156  friend class GridRenderer;
157 
158  void SetSeed(u32 seed);
159 
160  Grid(ElementRegistry<CC>& elts) :
161  m_seed(0),
162  m_width(W),
163  m_height(H),
164  m_er(elts),
165  m_xraySiteOdds(1000),
166  m_gridGeneration(0),
167  m_ignoreThreadingProblems(false)
168  {
169  for (u32 y = 0; y < H; ++y)
170  {
171  for (u32 x = 0; x < W; ++x)
172  {
173  LOG.Debug("Tile[%d][%d] @ %p", x, y, &m_tiles[x][y]);
174  }
175  }
176  }
177 
178  void SetIgnoreThreadingProblems(bool value)
179  {
180  m_ignoreThreadingProblems = value;
181  for(u32 x = 0; x < W; x++)
182  {
183  for(u32 y = 0; y < H; y++)
184  {
185  GetTile(x, y).SetIgnoreThreadingProblems(value);
186  }
187  }
188  }
189 
190  s32* GetXraySiteOddsPtr()
191  {
192  return &m_xraySiteOdds;
193  }
194 
195  void Reinit();
196 
197  const Element<CC> * LookupElement(u32 elementType) const
198  {
199  return m_tiles[0][0].GetElementTable().Lookup(elementType);
200  }
201 
202  ElementRegistry<CC>& GetElementRegistry()
203  {
204  return m_er;
205  }
206 
207  void Needed(Element<CC> & anElement)
208  {
209  anElement.AllocateType(); // Force a type now
210  m_er.RegisterElement(anElement); // Make sure we're in here (How could we not?)
211 
212  for(u32 i = 0; i < W; i++)
213  {
214  for(u32 j = 0; j < H; j++)
215  {
216  m_tiles[i][j].RegisterElement(anElement);
217  }
218  }
219  LOG.Message("Assigned type 0x%04x for %@",anElement.GetType(),&anElement.GetUUID());
220  }
221 
225  template <typename PointerType> class MyIterator
226  {
227  Grid & g;
228  s32 i;
229  s32 j;
230  public:
231  MyIterator(Grid<GC> & g, int i = 0, int j = 0) : g(g), i(i), j(j) { }
232 
233  bool operator!=(const MyIterator &m) const { return i != m.i || j != m.j; }
234  void operator++()
235  {
236  if (j < g.H)
237  {
238  i++;
239  if (i >= g.W)
240  {
241  i = 0;
242  j++;
243  }
244  }
245  }
246  int operator-(const MyIterator &m) const
247  {
248  s32 rows = j-m.j;
249  s32 cols = i-m.i;
250  return rows*g.W + cols;
251  }
252 
253  PointerType operator*() const
254  {
255  return &g.m_tiles[i][j];
256  }
257  };
258 
261 
262  iterator_type begin() { return iterator_type(*this); }
263 
264  const_iterator_type begin() const { return iterator_type(*this); }
265 
266  iterator_type end() { return iterator_type(*this,0,H); }
267 
268  const_iterator_type end() const { return const_iterator_type(*this, 0,H); }
269 
270  ~Grid()
271  { }
272 
284  void SetTileToExecuteOnly(const SPoint& tileLoc, bool value);
285 
292  bool GetTileExecutionStatus(const SPoint& tileLoc);
293 
300  void EmptyTile(const SPoint& tileLoc)
301  {
302  Tile<CC> & tile = GetTile(tileLoc);
303  tile.ClearAtoms();
304  tile.SetGeneration(m_gridGeneration);
305  }
306 
311  void Clear();
312 
318  void CheckCaches();
319 
324  bool IsLegalTileIndex(const SPoint & tileInGrid) const;
325 
339  bool MapGridToTile(const SPoint & siteInGrid, SPoint & tileInGrid, SPoint & siteInTile) const;
340 
347  bool MapGridToUncachedTile(const SPoint & siteInGrid, SPoint & tileInGrid, SPoint & siteInTile) const;
348 
352  static u32 GetHeight() { return H; }
353 
357  static u32 GetWidth() { return W; }
358 
362  static u32 GetHeightSites()
363  {
364  return GetHeight() * Tile<CC>::OWNED_SIDE;
365  }
366 
370  static u32 GetWidthSites()
371  {
372  return GetWidth() * Tile<CC>::OWNED_SIDE;
373  }
374 
378  void Pause()
379  {
380  PauseControl pc;
381  DoTileControl(pc);
382  }
383 
387  void Unpause()
388  {
389  RunControl rc;
390  DoTileControl(rc);
391  }
392 
397  void RecountAtoms();
398 
399  void PlaceAtom(const T& atom, const SPoint& location);
400 
401  void XRayAtom(const SPoint& location);
402 
403  void MaybeXRayAtom(const SPoint& location);
404 
405  const T* GetAtom(SPoint& loc)
406  {
407  SPoint tileInGrid, siteInTile;
408  if (!MapGridToTile(loc, tileInGrid, siteInTile))
409  {
410  LOG.Error("Can't get atom at site (%d,%d): Does not map to grid.",
411  loc.GetX(), loc.GetY());
412  FAIL(ILLEGAL_ARGUMENT); // XXX Change to return bool?
413  }
414  return GetTile(tileInGrid).GetAtom(siteInTile);
415  }
416 
417  T* GetWritableAtom(SPoint& loc)
418  {
419  SPoint tileInGrid, siteInTile;
420  if (!MapGridToTile(loc, tileInGrid, siteInTile))
421  {
422  return NULL;
423  }
424  return GetTile(tileInGrid).GetWritableAtom(siteInTile);
425  }
426 
427  void FillLastEventTile(SPoint& out);
428 
429  inline Tile<CC> & GetTile(const SPoint& pt)
430  {return GetTile(pt.GetX(), pt.GetY());}
431 
432  inline const Tile<CC> & GetTile(const SPoint& pt) const
433  {return GetTile(pt.GetX(), pt.GetY());}
434 
435  inline Tile<CC> & GetTile(u32 x, u32 y)
436  { return m_tiles[x][y]; }
437 
438  inline const Tile<CC> & GetTile(u32 x, u32 y) const
439  { return m_tiles[x][y]; }
440 
441  /* Don't count caches! */
442  static inline const u32 GetTotalSites()
443  { return GetWidthSites() * GetHeightSites(); }
444 
445  u64 GetTotalEventsExecuted() const;
446 
447  void WriteEPSImage(ByteSink & outstrm) const;
448 
449  void WriteEPSAverageImage(ByteSink & outstrm) const;
450 
451  void ResetEPSCounts();
452 
453  u32 GetAtomCount(ElementType atomType) const;
454 
462  double GetEmptySitePercentage() const
463  {
464  return 1.0 - ((double)GetAtomCount(Element_Empty<CC>::THE_INSTANCE.GetType()) /
465  (double)(GetHeightSites() * GetWidthSites()));
466  }
467 
468  void SurroundRectangleWithWall(s32 x, s32 y, s32 w, s32 h, s32 thickness);
469 
474  void RandomNuke();
475 
480  void SetBackgroundRadiation(bool value);
481 
486  {
487  SetBackgroundRadiation(!m_backgroundRadiationEnabled);
488  }
489 
495  {
496  return m_backgroundRadiationEnabled;
497  }
498 
502  void XRay();
503 
504  u32 CountActiveSites() const;
505  };
506 } /* namespace MFM */
507 
508 #include "Grid.tcc"
509 
510 #endif /*GRID_H*/
u32 GetType() const
Definition: Element.h:290
Definition: Element_Empty.h:41
bool GetTileExecutionStatus(const SPoint &tileLoc)
Definition: Grid.tcc:116
Definition: Grid.h:225
Level
Definition: Logger.h:52
void RandomNuke()
Definition: Grid.tcc:474
Definition: Random.h:45
void Start()
Definition: Tile.tcc:1121
void AllocateType()
Definition: Element.h:273
static u32 GetHeight()
Definition: Grid.h:352
void Pause()
Definition: Grid.h:378
bool MapGridToTile(const SPoint &siteInGrid, SPoint &tileInGrid, SPoint &siteInTile) const
Definition: Grid.tcc:132
void ToggleBackgroundRadiation()
Definition: Grid.h:485
void Error(const char *format,...)
Definition: Logger.h:259
T GetY() const
Definition: Point.tcc:40
void CheckCaches()
Definition: Grid.tcc:516
Definition: GridRenderer.h:46
Definition: Grid.h:47
bool IsRunReady()
Definition: Tile.h:1095
Definition: ElementRegistry.h:48
const UUID & GetUUID() const
Definition: Element.h:355
bool IsLegalTileIndex(const SPoint &tileInGrid) const
Definition: Grid.tcc:122
void Unpause()
Definition: Grid.h:387
void Pause()
Definition: Tile.tcc:1137
void Run()
Definition: Tile.h:1104
void SetGeneration(u8 generation)
Definition: Tile.h:539
bool IsBackgroundRadiataionEnabled()
Definition: Grid.h:494
void Clear()
Definition: Grid.tcc:502
void XRay()
Definition: Grid.tcc:553
void Debug(const char *format,...)
Definition: Logger.h:301
static u32 GetWidthSites()
Definition: Grid.h:370
Definition: EventWindow.h:41
void ClearAtoms()
Definition: Tile.tcc:102
void SetTileToExecuteOnly(const SPoint &tileLoc, bool value)
Definition: Grid.tcc:106
void Message(const char *format,...)
Definition: Logger.h:287
static u32 GetHeightSites()
Definition: Grid.h:362
void EmptyTile(const SPoint &tileLoc)
Definition: Grid.h:300
void RecountAtoms()
Definition: Grid.tcc:160
Definition: Atom.h:43
void SetBackgroundRadiation(bool value)
Definition: Grid.tcc:540
bool MapGridToUncachedTile(const SPoint &siteInGrid, SPoint &tileInGrid, SPoint &siteInTile) const
Definition: Grid.tcc:142
double GetEmptySitePercentage() const
Definition: Grid.h:462
static u32 GetWidth()
Definition: Grid.h:357
bool IsPauseReady()
Definition: Tile.tcc:1150
T GetX() const
Definition: Point.tcc:34