]>
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 /////////////////////////////////////////////////////////////////////////////
13 #error "Sorry, Life! will not work in 16-bit Windows"
16 // ==========================================================================
17 // headers, declarations, constants
18 // ==========================================================================
21 #pragma implementation "game.h"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
36 #include "wx/module.h"
39 #include <string.h> // for memset
42 #define ARRAYSIZE 1024 // static array for BeginFind & co.
43 #define ALLOCBOXES 16 // number of cellboxes to alloc at once
44 #define MAXDEAD 8 // tics before removing cellbox from list
47 // ==========================================================================
49 // ==========================================================================
51 #define HASH(x, y) (((x >> 3) & 0x7f) << 7) + ((y >> 3) & 0x7f)
53 #define HASHSIZE 32768 // hash table size (do not change!)
54 #define CELLBOX 8 // cells in a cellbox (do not change!)
61 inline bool IsAlive(int dx
, int dy
) const;
62 inline bool SetCell(int dx
, int dy
, bool alive
);
65 wxInt32 m_x
, m_y
; // position in universe
66 wxUint32 m_live1
, m_live2
; // alive cells (1 bit per cell)
67 wxUint32 m_old1
, m_old2
; // old values for m_live1, 2
68 wxUint32 m_on
[8]; // neighbouring info
69 wxUint32 m_dead
; // been dead for n generations
70 CellBox
*m_up
, *m_dn
, *m_lf
, *m_rt
; // neighbour CellBoxes
71 CellBox
*m_prev
, *m_next
; // in linked list
72 CellBox
*m_hprev
, *m_hnext
; // in hash table
77 // Returns whether cell dx, dy in this box is alive
79 bool CellBox::IsAlive(int dx
, int dy
) const
82 return (m_live2
& 1 << ((dy
- 4) * 8 + dx
));
84 return (m_live1
& 1 << ((dy
) * 8 + dx
));
88 // Sets cell dx, dy in this box to 'alive', returns TRUE if
89 // the previous value was different, FALSE if it was the same.
91 bool CellBox::SetCell(int dx
, int dy
, bool alive
)
93 if (IsAlive(dx
, dy
) != alive
)
96 m_live2
^= 1 << ((dy
- 4) * 8 + dx
);
98 m_live1
^= 1 << ((dy
) * 8 + dx
);
100 // reset this here to avoid updating problems
110 // ==========================================================================
112 // ==========================================================================
114 // --------------------------------------------------------------------------
116 // --------------------------------------------------------------------------
120 // pattern description
123 m_description
= _("");
127 m_boxes
= new CellBox
*[HASHSIZE
];
130 for (int i
= 0; i
< HASHSIZE
; i
++)
133 // state vars for BeginFind & FindMore
134 m_cells
= new Cell
[ARRAYSIZE
];
149 // Clears the board, freeing all storage.
155 // clear the hash table pointers
156 for (int i
= 0; i
< HASHSIZE
; i
++)
169 // free available boxes
182 m_description
= _("");
186 // --------------------------------------------------------------------------
187 // Test and set individual cells
188 // --------------------------------------------------------------------------
191 // Returns whether cell (x, y) is alive.
193 bool Life::IsAlive(wxInt32 x
, wxInt32 y
)
195 CellBox
*c
= LinkBox(x
, y
, FALSE
);
197 return (c
&& c
->IsAlive( x
- c
->m_x
, y
- c
->m_y
));
201 // Sets or clears cell (x, y), according to the 'alive' param.
203 void Life::SetCell(wxInt32 x
, wxInt32 y
, bool alive
)
205 CellBox
*c
= LinkBox(x
, y
);
206 wxUint32 dx
= x
- c
->m_x
;
207 wxUint32 dy
= y
- c
->m_y
;
209 if (c
->SetCell(dx
, dy
, alive
))
218 void Life::SetPattern(const LifePattern
& pattern
)
220 wxArrayString data
= pattern
.m_shape
;
226 for (size_t n
= 0; n
< data
.GetCount(); n
++)
230 if ( (line
.GetChar(0) != wxT('*')) &&
231 (line
.GetChar(0) != wxT('.')) )
233 // assume that it is a digit or a minus sign
234 line
.BeforeFirst(wxT(' ')).ToLong(&x
);
235 line
.AfterFirst(wxT(' ')).ToLong(&y
);
240 for (size_t k
= 0; k
< line
.Len(); k
++)
241 SetCell(x
+ k
, y
, line
.GetChar(k
) == wxT('*'));
247 m_name
= pattern
.m_name
;
248 m_rules
= pattern
.m_rules
;
249 m_description
= pattern
.m_description
;
252 // --------------------------------------------------------------------------
253 // Cellbox management functions
254 // --------------------------------------------------------------------------
257 // Creates a box in x, y, either taking it from the list
258 // of available boxes, or allocating a new one.
260 CellBox
* Life::CreateBox(wxInt32 x
, wxInt32 y
, wxUint32 hv
)
264 // if there are no available boxes, alloc a few more
266 for (int i
= 1; i
<= ALLOCBOXES
; i
++)
272 // TODO: handle memory errors. Note that right now, if we
273 // couldn't allocate at least one cellbox, we will crash
274 // before leaving CreateBox(). Probably we should try to
275 // allocate some boxes *before* the m_available list goes
276 // empty, so that we have a margin to handle errors
278 wxLogFatalError(_("Out of memory! Aborting..."));
283 c
->m_next
= m_available
;
287 // take a cellbox from the list of available boxes
289 m_available
= c
->m_next
;
292 memset((void *) c
, 0, sizeof(CellBox
));
296 // insert c in the list
299 if (c
->m_next
) c
->m_next
->m_prev
= c
;
301 // insert c in the hash table
302 c
->m_hnext
= m_boxes
[hv
];
304 if (c
->m_hnext
) c
->m_hnext
->m_hprev
= c
;
310 // Returns a pointer to the box (x, y); if it didn't exist yet,
311 // it returns NULL or creates a new one, depending on the value
312 // of the 'create' parameter.
314 CellBox
* Life::LinkBox(wxInt32 x
, wxInt32 y
, bool create
)
323 // search in the hash table
324 for (c
= m_boxes
[hv
]; c
; c
= c
->m_hnext
)
325 if ((c
->m_x
== x
) && (c
->m_y
== y
)) return c
;
327 // if not found, and (create == TRUE), create a new one
328 return create
? CreateBox(x
, y
, hv
) : (CellBox
*) NULL
;
332 // Removes this box from the list and the hash table and
333 // puts it in the list of available boxes.
335 void Life::KillBox(CellBox
*c
)
337 wxUint32 hv
= HASH(c
->m_x
, c
->m_y
);
339 // remove from the list
341 c
->m_prev
->m_next
= c
->m_next
;
345 // remove from the hash table
346 if (c
!= m_boxes
[hv
])
347 c
->m_hprev
->m_hnext
= c
->m_hnext
;
349 m_boxes
[hv
] = c
->m_hnext
;
352 if (c
->m_next
) c
->m_next
->m_prev
= c
->m_prev
;
353 if (c
->m_hnext
) c
->m_hnext
->m_hprev
= c
->m_hprev
;
354 if (c
->m_up
) c
->m_up
->m_dn
= NULL
;
355 if (c
->m_dn
) c
->m_dn
->m_up
= NULL
;
356 if (c
->m_lf
) c
->m_lf
->m_rt
= NULL
;
357 if (c
->m_rt
) c
->m_rt
->m_lf
= NULL
;
359 // append to the list of available boxes
360 c
->m_next
= m_available
;
364 // --------------------------------------------------------------------------
366 // --------------------------------------------------------------------------
368 Cell
Life::FindCenter()
377 for (c
= m_head
; c
; c
= c
->m_next
)
387 sx
= (sx
/ n
) + CELLBOX
/ 2;
388 sy
= (sy
/ n
) + CELLBOX
/ 2;
392 cell
.i
= (wxInt32
) sx
;
393 cell
.j
= (wxInt32
) sy
;
397 Cell
Life::FindNorth()
399 wxInt32 x
= 0, y
= 0;
403 for (c
= m_head
; c
; c
= c
->m_next
)
404 if (!c
->m_dead
&& ((first
) || (c
->m_y
< y
)))
412 cell
.i
= first
? 0 : x
+ CELLBOX
/ 2;
413 cell
.j
= first
? 0 : y
+ CELLBOX
/ 2;
417 Cell
Life::FindSouth()
419 wxInt32 x
= 0, y
= 0;
423 for (c
= m_head
; c
; c
= c
->m_next
)
424 if (!c
->m_dead
&& ((first
) || (c
->m_y
> y
)))
432 cell
.i
= first
? 0 : x
+ CELLBOX
/ 2;
433 cell
.j
= first
? 0 : y
+ CELLBOX
/ 2;
437 Cell
Life::FindWest()
439 wxInt32 x
= 0, y
= 0;
443 for (c
= m_head
; c
; c
= c
->m_next
)
444 if (!c
->m_dead
&& ((first
) || (c
->m_x
< x
)))
452 cell
.i
= first
? 0 : x
+ CELLBOX
/ 2;
453 cell
.j
= first
? 0 : y
+ CELLBOX
/ 2;
457 Cell
Life::FindEast()
459 wxInt32 x
= 0, y
= 0;
463 for (c
= m_head
; c
; c
= c
->m_next
)
464 if (!c
->m_dead
&& ((first
) || (c
->m_x
> x
)))
472 cell
.i
= first
? 0 : x
+ CELLBOX
/ 2;
473 cell
.j
= first
? 0 : y
+ CELLBOX
/ 2;
477 // --------------------------------------------------------------------------
479 // --------------------------------------------------------------------------
482 // Post eight cells to the cell arrays - leave out the fourth
483 // argument (or pass 0, the default value) to post alive cells
484 // only, else it will post cells which have changed.
486 void Life::DoLine(wxInt32 x
, wxInt32 y
, wxUint32 live
, wxUint32 old
)
488 wxUint32 diff
= (live
^ old
) & 0xff;
492 for (wxInt32 k
= 8; k
; k
--, x
++)
496 m_cells
[m_ncells
].i
= x
;
497 m_cells
[m_ncells
].j
= y
;
504 void Life::BeginFind(wxInt32 x0
, wxInt32 y0
, wxInt32 x1
, wxInt32 y1
, bool changed
)
506 // TODO: optimize for the case where the maximum number of
507 // cellboxes that fit in the specified viewport is smaller
508 // than the current total of boxes; iterating over the list
509 // should then be faster than searching in the hash table.
511 m_x0
= m_x
= x0
& 0xfffffff8;
512 m_y0
= m_y
= y0
& 0xfffffff8;
513 m_x1
= (x1
+ 7) & 0xfffffff8;
514 m_y1
= (y1
+ 7) & 0xfffffff8;
520 bool Life::FindMore(Cell
*cells
[], size_t *ncells
)
528 for ( ; m_y
<= m_y1
; m_y
+= 8, m_x
= m_x0
)
529 for ( ; m_x
<= m_x1
; m_x
+= 8)
531 if ((c
= LinkBox(m_x
, m_y
, FALSE
)) == NULL
)
534 // check whether there is enough space left in the array
535 if (m_ncells
> (ARRAYSIZE
- 64))
541 DoLine(m_x
, m_y
, c
->m_live1
, c
->m_old1
);
542 DoLine(m_x
, m_y
+ 1, c
->m_live1
>> 8, c
->m_old1
>> 8 );
543 DoLine(m_x
, m_y
+ 2, c
->m_live1
>> 16, c
->m_old1
>> 16);
544 DoLine(m_x
, m_y
+ 3, c
->m_live1
>> 24, c
->m_old1
>> 24);
545 DoLine(m_x
, m_y
+ 4, c
->m_live2
, c
->m_old2
);
546 DoLine(m_x
, m_y
+ 5, c
->m_live2
>> 8, c
->m_old2
>> 8 );
547 DoLine(m_x
, m_y
+ 6, c
->m_live2
>> 16, c
->m_old2
>> 16);
548 DoLine(m_x
, m_y
+ 7, c
->m_live2
>> 24, c
->m_old2
>> 24);
553 for ( ; m_y
<= m_y1
; m_y
+= 8, m_x
= m_x0
)
554 for ( ; m_x
<= m_x1
; m_x
+= 8)
556 if ((c
= LinkBox(m_x
, m_y
, FALSE
)) == NULL
)
559 // check whether there is enough space left in the array
560 if (m_ncells
> (ARRAYSIZE
- 64))
566 DoLine(m_x
, m_y
, c
->m_live1
);
567 DoLine(m_x
, m_y
+ 1, c
->m_live1
>> 8 );
568 DoLine(m_x
, m_y
+ 2, c
->m_live1
>> 16);
569 DoLine(m_x
, m_y
+ 3, c
->m_live1
>> 24);
570 DoLine(m_x
, m_y
+ 4, c
->m_live2
);
571 DoLine(m_x
, m_y
+ 5, c
->m_live2
>> 8 );
572 DoLine(m_x
, m_y
+ 6, c
->m_live2
>> 16);
573 DoLine(m_x
, m_y
+ 7, c
->m_live2
>> 24);
582 // --------------------------------------------------------------------------
584 // --------------------------------------------------------------------------
586 extern unsigned char *g_tab
;
591 // Advance one step in evolution :-)
595 CellBox
*c
, *up
, *dn
, *lf
, *rt
;
596 wxUint32 t1
, t2
, t3
, t4
;
597 bool changed
= FALSE
;
602 // Compute neighbours of each cell
604 // WARNING: unrolled loops and lengthy code follows!
610 if (! (c
->m_live1
|| c
->m_live2
))
621 t1
= c
->m_live1
& 0x000000ff;
626 up
= LinkBox(c
->m_x
, c
->m_y
- 8);
632 c
->m_on
[0] += g_tab2
[t1
];
636 t1
= (c
->m_live2
& 0xff000000) >> 24;
641 dn
= LinkBox(c
->m_x
, c
->m_y
+ 8);
647 c
->m_on
[7] += g_tab2
[t1
];
658 lf
= LinkBox(c
->m_x
- 8, c
->m_y
);
665 lf
->m_up
= LinkBox(c
->m_x
- 8, c
->m_y
- 8);
668 lf
->m_up
->m_on
[7] += 0x10000000;
669 lf
->m_on
[0] += 0x10000000;
670 lf
->m_on
[1] += 0x10000000;
674 lf
->m_on
[0] += 0x10000000;
675 lf
->m_on
[1] += 0x10000000;
676 lf
->m_on
[2] += 0x10000000;
680 lf
->m_on
[1] += 0x10000000;
681 lf
->m_on
[2] += 0x10000000;
682 lf
->m_on
[3] += 0x10000000;
686 lf
->m_on
[2] += 0x10000000;
687 lf
->m_on
[3] += 0x10000000;
688 lf
->m_on
[4] += 0x10000000;
695 lf
= LinkBox(c
->m_x
- 8, c
->m_y
);
700 lf
->m_on
[3] += 0x10000000;
701 lf
->m_on
[4] += 0x10000000;
702 lf
->m_on
[5] += 0x10000000;
706 lf
->m_on
[4] += 0x10000000;
707 lf
->m_on
[5] += 0x10000000;
708 lf
->m_on
[6] += 0x10000000;
712 lf
->m_on
[5] += 0x10000000;
713 lf
->m_on
[6] += 0x10000000;
714 lf
->m_on
[7] += 0x10000000;
720 lf
->m_dn
= LinkBox(c
->m_x
- 8, c
->m_y
+ 8);
723 lf
->m_on
[6] += 0x10000000;
724 lf
->m_on
[7] += 0x10000000;
725 lf
->m_dn
->m_on
[0] += 0x10000000;
734 rt
= LinkBox(c
->m_x
+ 8, c
->m_y
);
741 rt
->m_up
= LinkBox(c
->m_x
+ 8, c
->m_y
- 8);
744 rt
->m_up
->m_on
[7] += 0x00000001;
745 rt
->m_on
[0] += 0x00000001;
746 rt
->m_on
[1] += 0x00000001;
750 rt
->m_on
[0] += 0x00000001;
751 rt
->m_on
[1] += 0x00000001;
752 rt
->m_on
[2] += 0x00000001;
756 rt
->m_on
[1] += 0x00000001;
757 rt
->m_on
[2] += 0x00000001;
758 rt
->m_on
[3] += 0x00000001;
762 rt
->m_on
[2] += 0x00000001;
763 rt
->m_on
[3] += 0x00000001;
764 rt
->m_on
[4] += 0x00000001;
771 rt
= LinkBox(c
->m_x
+ 8, c
->m_y
);
776 rt
->m_on
[3] += 0x00000001;
777 rt
->m_on
[4] += 0x00000001;
778 rt
->m_on
[5] += 0x00000001;
782 rt
->m_on
[4] += 0x00000001;
783 rt
->m_on
[5] += 0x00000001;
784 rt
->m_on
[6] += 0x00000001;
788 rt
->m_on
[5] += 0x00000001;
789 rt
->m_on
[6] += 0x00000001;
790 rt
->m_on
[7] += 0x00000001;
796 rt
->m_dn
= LinkBox(c
->m_x
+ 8, c
->m_y
+ 8);
799 rt
->m_on
[6] += 0x00000001;
800 rt
->m_on
[7] += 0x00000001;
801 rt
->m_dn
->m_on
[0] += 0x00000001;
807 for (i
= 1; i
<= 3; i
++)
809 t1
= ((c
->m_live1
) >> (i
* 8)) & 0x000000ff;
812 c
->m_on
[i
- 1] += g_tab1
[t1
];
813 c
->m_on
[i
] += g_tab2
[t1
];
814 c
->m_on
[i
+ 1] += g_tab1
[t1
];
817 for (i
= 0; i
<= 2; i
++)
819 t1
= ((c
->m_live2
) >> (i
* 8)) & 0x000000ff;
822 c
->m_on
[i
+ 3] += g_tab1
[t1
];
823 c
->m_on
[i
+ 4] += g_tab2
[t1
];
824 c
->m_on
[i
+ 5] += g_tab1
[t1
];
849 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
) & 0xf) ];
850 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 4 ) & 0xf) ] << 4;
852 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 8 ) & 0xf) ] << 8;
853 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 12) & 0xf) ] << 12;
855 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 16) & 0xf) ] << 16;
856 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 20) & 0xf) ] << 20;
858 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 24) & 0xf) ] << 24;
859 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 28) & 0xf) ] << 28;
865 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
) & 0xf) ];
866 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 4 ) & 0xf) ] << 4;
868 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 8 ) & 0xf) ] << 8;
869 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 12) & 0xf) ] << 12;
871 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 16) & 0xf) ] << 16;
872 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 20) & 0xf) ] << 20;
874 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 24) & 0xf) ] << 24;
875 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 28) & 0xf) ] << 28;
877 c
->m_on
[0] = c
->m_on
[1] = c
->m_on
[2] = c
->m_on
[3] =
878 c
->m_on
[4] = c
->m_on
[5] = c
->m_on
[6] = c
->m_on
[7] = 0;
882 // count alive cells (TODO: find a better way to do this)
883 for (int i
= 0; i
< 32; i
++)
885 if (t1
& (1 << i
)) m_numcells
++;
886 if (t2
& (1 << i
)) m_numcells
++;
889 changed
|= ((t1
^ c
->m_old1
) || (t2
^ c
->m_old2
));
891 // mark, and discard if necessary, dead boxes
899 CellBox
*aux
= c
->m_next
;
900 if (c
->m_dead
++ > MAXDEAD
)
910 // ==========================================================================
912 // ==========================================================================
914 // A module to pregenerate lookup tables without having to do it
915 // from the application.
917 class LifeModule
: public wxModule
919 DECLARE_DYNAMIC_CLASS(LifeModule
)
927 IMPLEMENT_DYNAMIC_CLASS(LifeModule
, wxModule
)
929 bool LifeModule::OnInit()
932 g_tab
= new unsigned char [0xfffff];
934 if (!g_tab
) return FALSE
;
936 for (wxUint32 i
= 0; i
< 0xfffff; i
++)
938 wxUint32 val
= i
>> 4;
939 wxUint32 old
= i
& 0x0000f;
942 for (int j
= 0; j
< 4; j
++)
946 if (((val
& 0xf) == 3) || (((val
& 0xf) == 2) && (old
& 0x1)))
953 g_tab
[i
] = (unsigned char) live
;
959 void LifeModule::OnExit()
965 // This table converts from number of neighbors (like in on[]) to
966 // bits, for a set of four cells. It takes as index a five-digit
967 // hexadecimal value (0xNNNNB) where Ns hold number of neighbors
968 // for each cell and B holds their previous state.
970 unsigned char *g_tab
;
972 // This table converts from bits (like in live1, live2) to number
973 // of neighbors for each cell in the upper or lower row.
1235 // This table converts from bits (like in live1, live2) to number
1236 // of neighbors for each cell in the same row (excluding ourselves)