]>
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
38 #define ARRAYSIZE 1024 // static array for BeginFind & co.
39 #define ALLOCBOXES 16 // number of cellboxes to alloc at once
40 #define MAXDEAD 8 // tics before removing cellbox from list
43 // ==========================================================================
45 // ==========================================================================
47 #define HASH(x, y) (((x >> 3) & 0x7f) << 7) + ((y >> 3) & 0x7f)
49 #define HASHSIZE 16384 // hash table size (do not change!)
50 #define CELLBOX 8 // cells in a cellbox (do not change!)
57 inline bool IsAlive(int dx
, int dy
) const;
58 inline bool SetCell(int dx
, int dy
, bool alive
);
61 wxInt32 m_x
, m_y
; // position in universe
62 wxUint32 m_live1
, m_live2
; // alive cells (1 bit per cell)
63 wxUint32 m_old1
, m_old2
; // old values for m_live1, 2
64 wxUint32 m_on
[8]; // neighbouring info
65 wxUint32 m_dead
; // been dead for n generations
66 LifeCellBox
*m_up
, *m_dn
, *m_lf
, *m_rt
; // neighbour CellBoxes
67 LifeCellBox
*m_prev
, *m_next
; // in linked list
68 LifeCellBox
*m_hprev
, *m_hnext
; // in hash table
73 // Returns whether cell dx, dy in this box is alive
75 bool LifeCellBox::IsAlive(int dx
, int dy
) const
78 return (m_live2
& 1 << ((dy
- 4) * 8 + dx
)) ? true : false ;
80 return (m_live1
& 1 << ((dy
) * 8 + dx
)) ? true : false ;
84 // Sets cell dx, dy in this box to 'alive', returns true if
85 // the previous value was different, false if it was the same.
87 bool LifeCellBox::SetCell(int dx
, int dy
, bool alive
)
89 if (IsAlive(dx
, dy
) != alive
)
92 m_live2
^= 1 << ((dy
- 4) * 8 + dx
);
94 m_live1
^= 1 << ((dy
) * 8 + dx
);
96 // reset this here to avoid updating problems
106 // ==========================================================================
108 // ==========================================================================
110 // --------------------------------------------------------------------------
112 // --------------------------------------------------------------------------
116 // pattern description
117 m_name
= wxEmptyString
;
118 m_rules
= wxEmptyString
;
119 m_description
= wxEmptyString
;
123 m_boxes
= new LifeCellBox
*[HASHSIZE
];
126 for (int i
= 0; i
< HASHSIZE
; i
++)
129 // state vars for BeginFind & FindMore
130 m_cells
= new LifeCell
[ARRAYSIZE
];
145 // Clears the board, freeing all storage.
151 // clear the hash table pointers
152 for (int i
= 0; i
< HASHSIZE
; i
++)
165 // free available boxes
176 m_name
= wxEmptyString
;
177 m_rules
= wxEmptyString
;
178 m_description
= wxEmptyString
;
182 // --------------------------------------------------------------------------
183 // Test and set individual cells
184 // --------------------------------------------------------------------------
187 // Returns whether cell (x, y) is alive.
189 bool Life::IsAlive(wxInt32 x
, wxInt32 y
)
191 LifeCellBox
*c
= LinkBox(x
, y
, false);
193 return (c
&& c
->IsAlive( x
- c
->m_x
, y
- c
->m_y
));
197 // Sets or clears cell (x, y), according to the 'alive' param.
199 void Life::SetCell(wxInt32 x
, wxInt32 y
, bool alive
)
201 LifeCellBox
*c
= LinkBox(x
, y
);
202 wxUint32 dx
= x
- c
->m_x
;
203 wxUint32 dy
= y
- c
->m_y
;
205 if (c
->SetCell(dx
, dy
, alive
))
214 void Life::SetPattern(const LifePattern
& pattern
)
216 wxArrayString data
= pattern
.m_shape
;
222 for (size_t n
= 0; n
< data
.GetCount(); n
++)
226 if ( (line
.GetChar(0) != wxT('*')) &&
227 (line
.GetChar(0) != wxT('.')) )
229 // assume that it is a digit or a minus sign
230 line
.BeforeFirst(wxT(' ')).ToLong(&x
);
231 line
.AfterFirst(wxT(' ')).ToLong(&y
);
236 for (size_t k
= 0; k
< line
.Len(); k
++)
237 SetCell(x
+ k
, y
, line
.GetChar(k
) == wxT('*'));
243 m_name
= pattern
.m_name
;
244 m_rules
= pattern
.m_rules
;
245 m_description
= pattern
.m_description
;
248 // --------------------------------------------------------------------------
249 // Cellbox management functions
250 // --------------------------------------------------------------------------
253 // Creates a box in x, y, either taking it from the list
254 // of available boxes, or allocating a new one.
256 LifeCellBox
* Life::CreateBox(wxInt32 x
, wxInt32 y
, wxUint32 hv
)
260 // if there are no available boxes, alloc a few more
262 for (int i
= 1; i
<= ALLOCBOXES
; i
++)
264 c
= new LifeCellBox();
268 // TODO: handle memory errors. Note that right now, if we
269 // couldn't allocate at least one cellbox, we will crash
270 // before leaving CreateBox(). Probably we should try to
271 // allocate some boxes *before* the m_available list goes
272 // empty, so that we have a margin to handle errors
274 wxLogFatalError(_("Out of memory! Aborting..."));
279 c
->m_next
= m_available
;
283 // take a cellbox from the list of available boxes
285 m_available
= c
->m_next
;
288 memset((void *) c
, 0, sizeof(LifeCellBox
));
292 // insert c in the list
295 if (c
->m_next
) c
->m_next
->m_prev
= c
;
297 // insert c in the hash table
298 c
->m_hnext
= m_boxes
[hv
];
300 if (c
->m_hnext
) c
->m_hnext
->m_hprev
= c
;
306 // Returns a pointer to the box (x, y); if it didn't exist yet,
307 // it returns NULL or creates a new one, depending on the value
308 // of the 'create' parameter.
310 LifeCellBox
* Life::LinkBox(wxInt32 x
, wxInt32 y
, bool create
)
319 // search in the hash table
320 for (c
= m_boxes
[hv
]; c
; c
= c
->m_hnext
)
321 if ((c
->m_x
== x
) && (c
->m_y
== y
)) return c
;
323 // if not found, and (create == true), create a new one
324 return create
? CreateBox(x
, y
, hv
) : (LifeCellBox
*) NULL
;
328 // Removes this box from the list and the hash table and
329 // puts it in the list of available boxes.
331 void Life::KillBox(LifeCellBox
*c
)
333 wxUint32 hv
= HASH(c
->m_x
, c
->m_y
);
335 // remove from the list
337 c
->m_prev
->m_next
= c
->m_next
;
341 // remove from the hash table
342 if (c
!= m_boxes
[hv
])
343 c
->m_hprev
->m_hnext
= c
->m_hnext
;
345 m_boxes
[hv
] = c
->m_hnext
;
348 if (c
->m_next
) c
->m_next
->m_prev
= c
->m_prev
;
349 if (c
->m_hnext
) c
->m_hnext
->m_hprev
= c
->m_hprev
;
350 if (c
->m_up
) c
->m_up
->m_dn
= NULL
;
351 if (c
->m_dn
) c
->m_dn
->m_up
= NULL
;
352 if (c
->m_lf
) c
->m_lf
->m_rt
= NULL
;
353 if (c
->m_rt
) c
->m_rt
->m_lf
= NULL
;
355 // append to the list of available boxes
356 c
->m_next
= m_available
;
360 // --------------------------------------------------------------------------
362 // --------------------------------------------------------------------------
364 LifeCell
Life::FindCenter()
373 for (c
= m_head
; c
; c
= c
->m_next
)
383 sx
= (sx
/ n
) + CELLBOX
/ 2;
384 sy
= (sy
/ n
) + CELLBOX
/ 2;
388 cell
.i
= (wxInt32
) sx
;
389 cell
.j
= (wxInt32
) sy
;
393 LifeCell
Life::FindNorth()
395 wxInt32 x
= 0, y
= 0;
399 for (c
= m_head
; c
; c
= c
->m_next
)
400 if (!c
->m_dead
&& ((first
) || (c
->m_y
< y
)))
408 cell
.i
= first
? 0 : x
+ CELLBOX
/ 2;
409 cell
.j
= first
? 0 : y
+ CELLBOX
/ 2;
413 LifeCell
Life::FindSouth()
415 wxInt32 x
= 0, y
= 0;
419 for (c
= m_head
; c
; c
= c
->m_next
)
420 if (!c
->m_dead
&& ((first
) || (c
->m_y
> y
)))
428 cell
.i
= first
? 0 : x
+ CELLBOX
/ 2;
429 cell
.j
= first
? 0 : y
+ CELLBOX
/ 2;
433 LifeCell
Life::FindWest()
435 wxInt32 x
= 0, y
= 0;
439 for (c
= m_head
; c
; c
= c
->m_next
)
440 if (!c
->m_dead
&& ((first
) || (c
->m_x
< x
)))
448 cell
.i
= first
? 0 : x
+ CELLBOX
/ 2;
449 cell
.j
= first
? 0 : y
+ CELLBOX
/ 2;
453 LifeCell
Life::FindEast()
455 wxInt32 x
= 0, y
= 0;
459 for (c
= m_head
; c
; c
= c
->m_next
)
460 if (!c
->m_dead
&& ((first
) || (c
->m_x
> x
)))
468 cell
.i
= first
? 0 : x
+ CELLBOX
/ 2;
469 cell
.j
= first
? 0 : y
+ CELLBOX
/ 2;
473 // --------------------------------------------------------------------------
475 // --------------------------------------------------------------------------
478 // Post eight cells to the cell arrays - leave out the fourth
479 // argument (or pass 0, the default value) to post alive cells
480 // only, else it will post cells which have changed.
482 void Life::DoLine(wxInt32 x
, wxInt32 y
, wxUint32 live
, wxUint32 old
)
484 wxUint32 diff
= (live
^ old
) & 0xff;
488 for (wxInt32 k
= 8; k
; k
--, x
++)
492 m_cells
[m_ncells
].i
= x
;
493 m_cells
[m_ncells
].j
= y
;
500 void Life::BeginFind(wxInt32 x0
, wxInt32 y0
, wxInt32 x1
, wxInt32 y1
, bool changed
)
502 // TODO: optimize for the case where the maximum number of
503 // cellboxes that fit in the specified viewport is smaller
504 // than the current total of boxes; iterating over the list
505 // should then be faster than searching in the hash table.
507 m_x0
= m_x
= x0
& 0xfffffff8;
508 m_y0
= m_y
= y0
& 0xfffffff8;
509 m_x1
= (x1
+ 7) & 0xfffffff8;
510 m_y1
= (y1
+ 7) & 0xfffffff8;
516 bool Life::FindMore(LifeCell
*cells
[], size_t *ncells
)
524 for ( ; m_y
<= m_y1
; m_y
+= 8, m_x
= m_x0
)
525 for ( ; m_x
<= m_x1
; m_x
+= 8)
527 if ((c
= LinkBox(m_x
, m_y
, false)) == NULL
)
530 // check whether there is enough space left in the array
531 if (m_ncells
> (ARRAYSIZE
- 64))
537 DoLine(m_x
, m_y
, c
->m_live1
, c
->m_old1
);
538 DoLine(m_x
, m_y
+ 1, c
->m_live1
>> 8, c
->m_old1
>> 8 );
539 DoLine(m_x
, m_y
+ 2, c
->m_live1
>> 16, c
->m_old1
>> 16);
540 DoLine(m_x
, m_y
+ 3, c
->m_live1
>> 24, c
->m_old1
>> 24);
541 DoLine(m_x
, m_y
+ 4, c
->m_live2
, c
->m_old2
);
542 DoLine(m_x
, m_y
+ 5, c
->m_live2
>> 8, c
->m_old2
>> 8 );
543 DoLine(m_x
, m_y
+ 6, c
->m_live2
>> 16, c
->m_old2
>> 16);
544 DoLine(m_x
, m_y
+ 7, c
->m_live2
>> 24, c
->m_old2
>> 24);
549 for ( ; m_y
<= m_y1
; m_y
+= 8, m_x
= m_x0
)
550 for ( ; m_x
<= m_x1
; m_x
+= 8)
552 if ((c
= LinkBox(m_x
, m_y
, false)) == NULL
)
555 // check whether there is enough space left in the array
556 if (m_ncells
> (ARRAYSIZE
- 64))
562 DoLine(m_x
, m_y
, c
->m_live1
);
563 DoLine(m_x
, m_y
+ 1, c
->m_live1
>> 8 );
564 DoLine(m_x
, m_y
+ 2, c
->m_live1
>> 16);
565 DoLine(m_x
, m_y
+ 3, c
->m_live1
>> 24);
566 DoLine(m_x
, m_y
+ 4, c
->m_live2
);
567 DoLine(m_x
, m_y
+ 5, c
->m_live2
>> 8 );
568 DoLine(m_x
, m_y
+ 6, c
->m_live2
>> 16);
569 DoLine(m_x
, m_y
+ 7, c
->m_live2
>> 24);
578 // --------------------------------------------------------------------------
580 // --------------------------------------------------------------------------
582 extern unsigned char *g_tab
;
587 // Advance one step in evolution :-)
591 LifeCellBox
*c
, *up
, *dn
, *lf
, *rt
;
592 wxUint32 t1
, t2
, t3
, t4
;
593 bool changed
= false;
598 // Compute neighbours of each cell
600 // WARNING: unrolled loops and lengthy code follows!
606 if (! (c
->m_live1
|| c
->m_live2
))
617 t1
= c
->m_live1
& 0x000000ff;
622 up
= LinkBox(c
->m_x
, c
->m_y
- 8);
628 c
->m_on
[0] += g_tab2
[t1
];
632 t1
= (c
->m_live2
& 0xff000000) >> 24;
637 dn
= LinkBox(c
->m_x
, c
->m_y
+ 8);
643 c
->m_on
[7] += g_tab2
[t1
];
654 lf
= LinkBox(c
->m_x
- 8, c
->m_y
);
661 lf
->m_up
= LinkBox(c
->m_x
- 8, c
->m_y
- 8);
664 lf
->m_up
->m_on
[7] += 0x10000000;
665 lf
->m_on
[0] += 0x10000000;
666 lf
->m_on
[1] += 0x10000000;
670 lf
->m_on
[0] += 0x10000000;
671 lf
->m_on
[1] += 0x10000000;
672 lf
->m_on
[2] += 0x10000000;
676 lf
->m_on
[1] += 0x10000000;
677 lf
->m_on
[2] += 0x10000000;
678 lf
->m_on
[3] += 0x10000000;
682 lf
->m_on
[2] += 0x10000000;
683 lf
->m_on
[3] += 0x10000000;
684 lf
->m_on
[4] += 0x10000000;
691 lf
= LinkBox(c
->m_x
- 8, c
->m_y
);
696 lf
->m_on
[3] += 0x10000000;
697 lf
->m_on
[4] += 0x10000000;
698 lf
->m_on
[5] += 0x10000000;
702 lf
->m_on
[4] += 0x10000000;
703 lf
->m_on
[5] += 0x10000000;
704 lf
->m_on
[6] += 0x10000000;
708 lf
->m_on
[5] += 0x10000000;
709 lf
->m_on
[6] += 0x10000000;
710 lf
->m_on
[7] += 0x10000000;
716 lf
->m_dn
= LinkBox(c
->m_x
- 8, c
->m_y
+ 8);
719 lf
->m_on
[6] += 0x10000000;
720 lf
->m_on
[7] += 0x10000000;
721 lf
->m_dn
->m_on
[0] += 0x10000000;
730 rt
= LinkBox(c
->m_x
+ 8, c
->m_y
);
737 rt
->m_up
= LinkBox(c
->m_x
+ 8, c
->m_y
- 8);
740 rt
->m_up
->m_on
[7] += 0x00000001;
741 rt
->m_on
[0] += 0x00000001;
742 rt
->m_on
[1] += 0x00000001;
746 rt
->m_on
[0] += 0x00000001;
747 rt
->m_on
[1] += 0x00000001;
748 rt
->m_on
[2] += 0x00000001;
752 rt
->m_on
[1] += 0x00000001;
753 rt
->m_on
[2] += 0x00000001;
754 rt
->m_on
[3] += 0x00000001;
758 rt
->m_on
[2] += 0x00000001;
759 rt
->m_on
[3] += 0x00000001;
760 rt
->m_on
[4] += 0x00000001;
767 rt
= LinkBox(c
->m_x
+ 8, c
->m_y
);
772 rt
->m_on
[3] += 0x00000001;
773 rt
->m_on
[4] += 0x00000001;
774 rt
->m_on
[5] += 0x00000001;
778 rt
->m_on
[4] += 0x00000001;
779 rt
->m_on
[5] += 0x00000001;
780 rt
->m_on
[6] += 0x00000001;
784 rt
->m_on
[5] += 0x00000001;
785 rt
->m_on
[6] += 0x00000001;
786 rt
->m_on
[7] += 0x00000001;
792 rt
->m_dn
= LinkBox(c
->m_x
+ 8, c
->m_y
+ 8);
795 rt
->m_on
[6] += 0x00000001;
796 rt
->m_on
[7] += 0x00000001;
797 rt
->m_dn
->m_on
[0] += 0x00000001;
803 for (i
= 1; i
<= 3; i
++)
805 t1
= ((c
->m_live1
) >> (i
* 8)) & 0x000000ff;
808 c
->m_on
[i
- 1] += g_tab1
[t1
];
809 c
->m_on
[i
] += g_tab2
[t1
];
810 c
->m_on
[i
+ 1] += g_tab1
[t1
];
813 for (i
= 0; i
<= 2; i
++)
815 t1
= ((c
->m_live2
) >> (i
* 8)) & 0x000000ff;
818 c
->m_on
[i
+ 3] += g_tab1
[t1
];
819 c
->m_on
[i
+ 4] += g_tab2
[t1
];
820 c
->m_on
[i
+ 5] += g_tab1
[t1
];
845 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
) & 0xf) ];
846 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 4 ) & 0xf) ] << 4;
848 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 8 ) & 0xf) ] << 8;
849 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 12) & 0xf) ] << 12;
851 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 16) & 0xf) ] << 16;
852 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 20) & 0xf) ] << 20;
854 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 24) & 0xf) ] << 24;
855 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 28) & 0xf) ] << 28;
861 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
) & 0xf) ];
862 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 4 ) & 0xf) ] << 4;
864 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 8 ) & 0xf) ] << 8;
865 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 12) & 0xf) ] << 12;
867 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 16) & 0xf) ] << 16;
868 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 20) & 0xf) ] << 20;
870 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 24) & 0xf) ] << 24;
871 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 28) & 0xf) ] << 28;
873 c
->m_on
[0] = c
->m_on
[1] = c
->m_on
[2] = c
->m_on
[3] =
874 c
->m_on
[4] = c
->m_on
[5] = c
->m_on
[6] = c
->m_on
[7] = 0;
882 t1_
= (t1
& 0x55555555) + (t1
>> 1 & 0x55555555);
883 t1_
= (t1_
& 0x33333333) + (t1_
>> 2 & 0x33333333);
885 t2_
= (t2
& 0x55555555) + (t2
>> 1 & 0x55555555);
886 t2_
= (t2_
& 0x33333333) + (t2_
>> 2 & 0x33333333) + t1_
;
887 t2_
= (t2_
& 0x0F0F0F0F) + (t2_
>> 4 & 0x0F0F0F0F);
888 t2_
= (t2_
& 0x00FF00FF) + (t2_
>> 8 & 0x00FF00FF);
890 m_numcells
+= (t2_
& 0xFF) + (t2_
>> 16 & 0xFF);
892 // Original, slower code
893 for (int i
= 0; i
< 32; i
++)
895 if (t1
& (1 << i
)) m_numcells
++;
896 if (t2
& (1 << i
)) m_numcells
++;
900 changed
|= ((t1
^ c
->m_old1
) || (t2
^ c
->m_old2
));
902 // mark, and discard if necessary, dead boxes
910 LifeCellBox
*aux
= c
->m_next
;
911 if (c
->m_dead
++ > MAXDEAD
)
921 // ==========================================================================
923 // ==========================================================================
925 // A module to pregenerate lookup tables without having to do it
926 // from the application.
928 class LifeModule
: public wxModule
930 DECLARE_DYNAMIC_CLASS(LifeModule
)
938 IMPLEMENT_DYNAMIC_CLASS(LifeModule
, wxModule
)
940 bool LifeModule::OnInit()
943 g_tab
= new unsigned char [0xfffff];
945 if (!g_tab
) return false;
947 for (wxUint32 i
= 0; i
< 0xfffff; i
++)
949 wxUint32 val
= i
>> 4;
950 wxUint32 old
= i
& 0x0000f;
953 for (int j
= 0; j
< 4; j
++)
957 if (((val
& 0xf) == 3) || (((val
& 0xf) == 2) && (old
& 0x1)))
964 g_tab
[i
] = (unsigned char) live
;
970 void LifeModule::OnExit()
976 // This table converts from number of neighbors (like in on[]) to
977 // bits, for a set of four cells. It takes as index a five-digit
978 // hexadecimal value (0xNNNNB) where Ns hold number of neighbors
979 // for each cell and B holds their previous state.
981 unsigned char *g_tab
;
983 // This table converts from bits (like in live1, live2) to number
984 // of neighbors for each cell in the upper or lower row.
1246 // This table converts from bits (like in live1, live2) to number
1247 // of neighbors for each cell in the same row (excluding ourselves)