]>
git.saurik.com Git - wxWidgets.git/blob - demos/life/game.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Life! game logic
4 // Author: Guillermo Rodriguez Garcia, <guille@iies.es>
8 // Copyright: (c) 2000, Guillermo Rodriguez Garcia
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ==========================================================================
13 // headers, declarations, constants
14 // ==========================================================================
17 #pragma implementation "game.h"
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
32 #include "wx/module.h"
35 #include <string.h> // for memset
37 #define ARRAYSIZE 1024 // size of the static array for BeginFind & co.
38 #define ALLOCBOXES 16 // number of cellboxes to alloc at once
41 // ==========================================================================
43 // ==========================================================================
45 #define HASH(x, y) (((x >> 3) & 0x7f) << 7) + ((y >> 3) & 0x7f)
48 #define HASHSIZE 10000
50 #define HASHSIZE 32768
60 inline bool IsAlive(int dx
, int dy
) const;
61 inline bool SetCell(int dx
, int dy
, bool alive
);
64 wxInt32 m_x
, m_y
; // position in universe
65 wxUint32 m_live1
, m_live2
; // alive cells (1 bit per cell)
66 wxUint32 m_old1
, m_old2
; // old values for m_live1, 2
67 wxUint32 m_on
[8]; // neighbouring info
68 wxUint32 m_dead
; // been dead for n generations
69 CellBox
*m_up
, *m_dn
, *m_lf
, *m_rt
; // neighbour CellBoxes
70 CellBox
*m_prev
, *m_next
; // in linked list
71 CellBox
*m_hprev
, *m_hnext
; // in hash table
76 // Returns whether cell dx, dy in this box is alive
78 bool CellBox::IsAlive(int dx
, int dy
) const
81 return (m_live2
& 1 << ((dy
- 4) * 8 + dx
));
83 return (m_live1
& 1 << ((dy
) * 8 + dx
));
87 // Sets cell dx, dy in this box to 'alive', returns TRUE if
88 // the previous value was different, FALSE if it was the same.
90 bool CellBox::SetCell(int dx
, int dy
, bool alive
)
92 if (IsAlive(dx
, dy
) != alive
)
95 m_live2
^= 1 << ((dy
- 4) * 8 + dx
);
97 m_live1
^= 1 << ((dy
) * 8 + dx
);
99 // reset this here to avoid updating problems
109 // ==========================================================================
111 // ==========================================================================
113 // --------------------------------------------------------------------------
115 // --------------------------------------------------------------------------
120 m_boxes
= new CellBox
*[HASHSIZE
];
123 for (int i
= 0; i
< HASHSIZE
; i
++)
126 m_cells
= new Cell
[ARRAYSIZE
];
141 // Clears the board, freeing all storage.
149 // clear the hash table pointers
150 for (int i
= 0; i
< HASHSIZE
; i
++)
163 // free available boxes
174 // --------------------------------------------------------------------------
175 // Test and set individual cells
176 // --------------------------------------------------------------------------
179 // Returns whether cell (x, y) is alive.
181 bool Life::IsAlive(wxInt32 x
, wxInt32 y
)
183 CellBox
*c
= LinkBox(x
, y
, FALSE
);
185 return (c
&& c
->IsAlive( x
- c
->m_x
, y
- c
->m_y
));
189 // Sets or clears cell (x, y), according to the 'alive' param.
191 void Life::SetCell(wxInt32 x
, wxInt32 y
, bool alive
)
193 CellBox
*c
= LinkBox(x
, y
);
194 wxUint32 dx
= x
- c
->m_x
;
195 wxUint32 dy
= y
- c
->m_y
;
197 if (c
->SetCell(dx
, dy
, alive
))
206 void Life::SetShape(const LifeShape
& shape
)
208 char *p
= shape
.m_data
;
210 int i0
= -(shape
.m_width
/ 2);
211 int j0
= -(shape
.m_height
/ 2);
212 int i1
= i0
+ shape
.m_width
- 1;
213 int j1
= j0
+ shape
.m_height
- 1;
216 for (int j
= j0
; j
<= j1
; j
++)
217 for (int i
= i0
; i
<= i1
; i
++)
218 SetCell(i
, j
, *(p
++) == '*');
221 // --------------------------------------------------------------------------
222 // Cellbox management functions
223 // --------------------------------------------------------------------------
226 // Creates a box in x, y, either taking it from the list
227 // of available boxes, or allocating a new one.
229 CellBox
* Life::CreateBox(wxInt32 x
, wxInt32 y
, wxUint32 hv
)
233 // if there are no available boxes, alloc a few more
235 for (int i
= 1; i
<= ALLOCBOXES
; i
++)
241 // TODO: handle memory errors. Note that right now, if we
242 // couldn't allocate at least one cellbox, we will crash
243 // before leaving CreateBox(). Probably we should try to
244 // allocate some boxes *before* the m_available list goes
245 // empty, so that we have a margin to handle errors
247 wxLogFatalError(_("Out of memory! Aborting..."));
252 c
->m_next
= m_available
;
256 // take a cellbox from the list of available boxes
258 m_available
= c
->m_next
;
261 memset((void *) c
, 0, sizeof(CellBox
));
265 // insert c in the list
268 if (c
->m_next
) c
->m_next
->m_prev
= c
;
270 // insert c in the hash table
271 c
->m_hnext
= m_boxes
[hv
];
273 if (c
->m_hnext
) c
->m_hnext
->m_hprev
= c
;
279 // Returns a pointer to the box (x, y); if it didn't exist yet,
280 // it returns NULL or creates a new one, depending on the value
281 // of the 'create' parameter.
283 CellBox
* Life::LinkBox(wxInt32 x
, wxInt32 y
, bool create
)
292 // search in the hash table
293 for (c
= m_boxes
[hv
]; c
; c
= c
->m_hnext
)
294 if ((c
->m_x
== x
) && (c
->m_y
== y
)) return c
;
296 // if not found, and (create == TRUE), create a new one
297 return create
? CreateBox(x
, y
, hv
) : NULL
;
301 // Removes this box from the list and the hash table and
302 // puts it in the list of available boxes.
304 void Life::KillBox(CellBox
*c
)
306 wxUint32 hv
= HASH(c
->m_x
, c
->m_y
);
308 // remove from the list
310 c
->m_prev
->m_next
= c
->m_next
;
314 // remove from the hash table
315 if (c
!= m_boxes
[hv
])
316 c
->m_hprev
->m_hnext
= c
->m_hnext
;
318 m_boxes
[hv
] = c
->m_hnext
;
321 if (c
->m_next
) c
->m_next
->m_prev
= c
->m_prev
;
322 if (c
->m_hnext
) c
->m_hnext
->m_hprev
= c
->m_hprev
;
323 if (c
->m_up
) c
->m_up
->m_dn
= NULL
;
324 if (c
->m_dn
) c
->m_dn
->m_up
= NULL
;
325 if (c
->m_lf
) c
->m_lf
->m_rt
= NULL
;
326 if (c
->m_rt
) c
->m_rt
->m_lf
= NULL
;
328 // append to the list of available boxes
329 c
->m_next
= m_available
;
333 // --------------------------------------------------------------------------
335 // --------------------------------------------------------------------------
337 // Post eight cells to the cell arrays (changed cells only)
338 void Life::DoLine(wxInt32 i
, wxInt32 j
, wxUint32 live
, wxUint32 old
)
340 wxUint32 diff
= (live
^ old
) & 0x000000ff;
344 for (wxInt32 k
= 8; k
; k
--, i
++)
348 m_cells
[m_ncells
].i
= i
;
349 m_cells
[m_ncells
].j
= j
;
357 // Post eight cells to the cell arrays (alive cells only)
358 void Life::DoLine(wxInt32 i
, wxInt32 j
, wxUint32 live
)
360 if (! (live
& 0x000000ff)) return;
362 for (wxInt32 k
= 8; k
; k
--, i
++)
366 m_cells
[m_ncells
].i
= i
;
367 m_cells
[m_ncells
].j
= j
;
374 void Life::BeginFind(wxInt32 i0
, wxInt32 j0
, wxInt32 i1
, wxInt32 j1
, bool changed
)
376 // TODO: optimize for the case where the maximum number of
377 // cellboxes that fit in the specified viewport is smaller
378 // than the current total of boxes; iterating over the list
379 // should then be faster than searching in the hash table.
381 m_i0
= m_i
= i0
& 0xfffffff8;
382 m_j0
= m_j
= j0
& 0xfffffff8;
383 m_i1
= (i1
+ 7) & 0xfffffff8;
384 m_j1
= (j1
+ 7) & 0xfffffff8;
390 bool Life::FindMore(Cell
*cells
[], size_t *ncells
)
398 for ( ; m_j
<= m_j1
; m_j
+= 8, m_i
= m_i0
)
399 for ( ; m_i
<= m_i1
; m_i
+= 8)
401 if ((c
= LinkBox(m_i
, m_j
, FALSE
)) == NULL
)
404 // check whether there is enough space left in the array
405 if (m_ncells
> (ARRAYSIZE
- 64))
411 DoLine(m_i
, m_j
, c
->m_live1
, c
->m_old1
);
412 DoLine(m_i
, m_j
+ 1, c
->m_live1
>> 8, c
->m_old1
>> 8 );
413 DoLine(m_i
, m_j
+ 2, c
->m_live1
>> 16, c
->m_old1
>> 16);
414 DoLine(m_i
, m_j
+ 3, c
->m_live1
>> 24, c
->m_old1
>> 24);
415 DoLine(m_i
, m_j
+ 4, c
->m_live2
, c
->m_old2
);
416 DoLine(m_i
, m_j
+ 5, c
->m_live2
>> 8, c
->m_old2
>> 8 );
417 DoLine(m_i
, m_j
+ 6, c
->m_live2
>> 16, c
->m_old2
>> 16);
418 DoLine(m_i
, m_j
+ 7, c
->m_live2
>> 24, c
->m_old2
>> 24);
423 for ( ; m_j
<= m_j1
; m_j
+= 8, m_i
= m_i0
)
424 for ( ; m_i
<= m_i1
; m_i
+= 8)
426 if ((c
= LinkBox(m_i
, m_j
, FALSE
)) == NULL
)
429 // check whether there is enough space left in the array
430 if (m_ncells
> (ARRAYSIZE
- 64))
436 DoLine(m_i
, m_j
, c
->m_live1
);
437 DoLine(m_i
, m_j
+ 1, c
->m_live1
>> 8 );
438 DoLine(m_i
, m_j
+ 2, c
->m_live1
>> 16);
439 DoLine(m_i
, m_j
+ 3, c
->m_live1
>> 24);
440 DoLine(m_i
, m_j
+ 4, c
->m_live2
);
441 DoLine(m_i
, m_j
+ 5, c
->m_live2
>> 8 );
442 DoLine(m_i
, m_j
+ 6, c
->m_live2
>> 16);
443 DoLine(m_i
, m_j
+ 7, c
->m_live2
>> 24);
452 // --------------------------------------------------------------------------
454 // --------------------------------------------------------------------------
456 extern unsigned char *g_tab
;
461 // Advance one step in evolution :-)
465 CellBox
*c
, *up
, *dn
, *lf
, *rt
;
466 wxUint32 t1
, t2
, t3
, t4
;
467 bool changed
= FALSE
;
472 // Compute neighbours of each cell
474 // WARNING: unrolled loops and lengthy code follows!
480 if (! (c
->m_live1
|| c
->m_live2
))
491 t1
= c
->m_live1
& 0x000000ff;
496 up
= LinkBox(c
->m_x
, c
->m_y
- 8);
502 c
->m_on
[0] += g_tab2
[t1
];
506 t1
= (c
->m_live2
& 0xff000000) >> 24;
511 dn
= LinkBox(c
->m_x
, c
->m_y
+ 8);
517 c
->m_on
[7] += g_tab2
[t1
];
528 lf
= LinkBox(c
->m_x
- 8, c
->m_y
);
535 lf
->m_up
= LinkBox(c
->m_x
- 8, c
->m_y
- 8);
538 lf
->m_up
->m_on
[7] += 0x10000000;
539 lf
->m_on
[0] += 0x10000000;
540 lf
->m_on
[1] += 0x10000000;
544 lf
->m_on
[0] += 0x10000000;
545 lf
->m_on
[1] += 0x10000000;
546 lf
->m_on
[2] += 0x10000000;
550 lf
->m_on
[1] += 0x10000000;
551 lf
->m_on
[2] += 0x10000000;
552 lf
->m_on
[3] += 0x10000000;
556 lf
->m_on
[2] += 0x10000000;
557 lf
->m_on
[3] += 0x10000000;
558 lf
->m_on
[4] += 0x10000000;
565 lf
= LinkBox(c
->m_x
- 8, c
->m_y
);
570 lf
->m_on
[3] += 0x10000000;
571 lf
->m_on
[4] += 0x10000000;
572 lf
->m_on
[5] += 0x10000000;
576 lf
->m_on
[4] += 0x10000000;
577 lf
->m_on
[5] += 0x10000000;
578 lf
->m_on
[6] += 0x10000000;
582 lf
->m_on
[5] += 0x10000000;
583 lf
->m_on
[6] += 0x10000000;
584 lf
->m_on
[7] += 0x10000000;
590 lf
->m_dn
= LinkBox(c
->m_x
- 8, c
->m_y
+ 8);
593 lf
->m_on
[6] += 0x10000000;
594 lf
->m_on
[7] += 0x10000000;
595 lf
->m_dn
->m_on
[0] += 0x10000000;
604 rt
= LinkBox(c
->m_x
+ 8, c
->m_y
);
611 rt
->m_up
= LinkBox(c
->m_x
+ 8, c
->m_y
- 8);
614 rt
->m_up
->m_on
[7] += 0x00000001;
615 rt
->m_on
[0] += 0x00000001;
616 rt
->m_on
[1] += 0x00000001;
620 rt
->m_on
[0] += 0x00000001;
621 rt
->m_on
[1] += 0x00000001;
622 rt
->m_on
[2] += 0x00000001;
626 rt
->m_on
[1] += 0x00000001;
627 rt
->m_on
[2] += 0x00000001;
628 rt
->m_on
[3] += 0x00000001;
632 rt
->m_on
[2] += 0x00000001;
633 rt
->m_on
[3] += 0x00000001;
634 rt
->m_on
[4] += 0x00000001;
641 rt
= LinkBox(c
->m_x
+ 8, c
->m_y
);
646 rt
->m_on
[3] += 0x00000001;
647 rt
->m_on
[4] += 0x00000001;
648 rt
->m_on
[5] += 0x00000001;
652 rt
->m_on
[4] += 0x00000001;
653 rt
->m_on
[5] += 0x00000001;
654 rt
->m_on
[6] += 0x00000001;
658 rt
->m_on
[5] += 0x00000001;
659 rt
->m_on
[6] += 0x00000001;
660 rt
->m_on
[7] += 0x00000001;
666 rt
->m_dn
= LinkBox(c
->m_x
+ 8, c
->m_y
+ 8);
669 rt
->m_on
[6] += 0x00000001;
670 rt
->m_on
[7] += 0x00000001;
671 rt
->m_dn
->m_on
[0] += 0x00000001;
677 for (i
= 1; i
<= 3; i
++)
679 t1
= ((c
->m_live1
) >> (i
* 8)) & 0x000000ff;
682 c
->m_on
[i
- 1] += g_tab1
[t1
];
683 c
->m_on
[i
] += g_tab2
[t1
];
684 c
->m_on
[i
+ 1] += g_tab1
[t1
];
687 for (i
= 0; i
<= 2; i
++)
689 t1
= ((c
->m_live2
) >> (i
* 8)) & 0x000000ff;
692 c
->m_on
[i
+ 3] += g_tab1
[t1
];
693 c
->m_on
[i
+ 4] += g_tab2
[t1
];
694 c
->m_on
[i
+ 5] += g_tab1
[t1
];
719 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
) & 0xf) ];
720 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 4 ) & 0xf) ] << 4;
722 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 8 ) & 0xf) ] << 8;
723 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 12) & 0xf) ] << 12;
725 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 16) & 0xf) ] << 16;
726 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 20) & 0xf) ] << 20;
728 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 24) & 0xf) ] << 24;
729 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 28) & 0xf) ] << 28;
735 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
) & 0xf) ];
736 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 4 ) & 0xf) ] << 4;
738 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 8 ) & 0xf) ] << 8;
739 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 12) & 0xf) ] << 12;
741 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 16) & 0xf) ] << 16;
742 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 20) & 0xf) ] << 20;
744 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 24) & 0xf) ] << 24;
745 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 28) & 0xf) ] << 28;
747 c
->m_on
[0] = c
->m_on
[1] = c
->m_on
[2] = c
->m_on
[3] =
748 c
->m_on
[4] = c
->m_on
[5] = c
->m_on
[6] = c
->m_on
[7] = 0;
752 // count alive cells (TODO: find a better way to do this)
753 for (int i
= 0; i
< 32; i
++)
755 if (t1
& (1 << i
)) m_numcells
++;
756 if (t2
& (1 << i
)) m_numcells
++;
759 changed
|= ((t1
^ c
->m_old1
) || (t2
^ c
->m_old2
));
761 // mark, and discard if necessary, dead boxes
769 CellBox
*aux
= c
->m_next
;
770 if (c
->m_dead
++ > MAXDEAD
)
780 // ==========================================================================
782 // ==========================================================================
784 // A module to pregenerate lookup tables without having to do it
785 // from the application.
787 class LifeModule
: public wxModule
789 DECLARE_DYNAMIC_CLASS(LifeModule
)
797 IMPLEMENT_DYNAMIC_CLASS(LifeModule
, wxModule
)
799 bool LifeModule::OnInit()
802 g_tab
= new unsigned char [0xfffff];
804 if (!g_tab
) return FALSE
;
806 for (wxUint32 i
= 0; i
< 0xfffff; i
++)
808 wxUint32 val
= i
>> 4;
809 wxUint32 old
= i
& 0x0000f;
812 for (int j
= 0; j
< 4; j
++)
816 if (((val
& 0xf) == 3) || (((val
& 0xf) == 2) && (old
& 0x1)))
823 g_tab
[i
] = (unsigned char) live
;
829 void LifeModule::OnExit()
835 // This table converts from number of neighbors (like in on[]) to
836 // bits, for a set of four cells. It takes as index a five-digit
837 // hexadecimal value (0xNNNNB) where Ns hold number of neighbors
838 // for each cell and B holds their previous state.
840 unsigned char *g_tab
;
842 // This table converts from bits (like in live1, live2) to number
843 // of neighbors for each cell in the upper or lower row.
1105 // This table converts from bits (like in live1, live2) to number
1106 // of neighbors for each cell in the same row (excluding ourselves)