]>
git.saurik.com Git - wxWidgets.git/blob - demos/life/game.cpp
4946b676f2493bdbcb54c5c761967da08bb99ad4
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
41 #define ARRAYSIZE 1024 // size of the static array for BeginFind & co.
42 #define ALLOCBOXES 16 // number of cellboxes to alloc at once
45 // ==========================================================================
47 // ==========================================================================
49 #define HASH(x, y) (((x >> 3) & 0x7f) << 7) + ((y >> 3) & 0x7f)
51 #define HASHSIZE 32768
58 inline bool IsAlive(int dx
, int dy
) const;
59 inline bool SetCell(int dx
, int dy
, bool alive
);
62 wxInt32 m_x
, m_y
; // position in universe
63 wxUint32 m_live1
, m_live2
; // alive cells (1 bit per cell)
64 wxUint32 m_old1
, m_old2
; // old values for m_live1, 2
65 wxUint32 m_on
[8]; // neighbouring info
66 wxUint32 m_dead
; // been dead for n generations
67 CellBox
*m_up
, *m_dn
, *m_lf
, *m_rt
; // neighbour CellBoxes
68 CellBox
*m_prev
, *m_next
; // in linked list
69 CellBox
*m_hprev
, *m_hnext
; // in hash table
74 // Returns whether cell dx, dy in this box is alive
76 bool CellBox::IsAlive(int dx
, int dy
) const
79 return (m_live2
& 1 << ((dy
- 4) * 8 + dx
));
81 return (m_live1
& 1 << ((dy
) * 8 + dx
));
85 // Sets cell dx, dy in this box to 'alive', returns TRUE if
86 // the previous value was different, FALSE if it was the same.
88 bool CellBox::SetCell(int dx
, int dy
, bool alive
)
90 if (IsAlive(dx
, dy
) != alive
)
93 m_live2
^= 1 << ((dy
- 4) * 8 + dx
);
95 m_live1
^= 1 << ((dy
) * 8 + dx
);
97 // reset this here to avoid updating problems
107 // ==========================================================================
109 // ==========================================================================
111 // --------------------------------------------------------------------------
113 // --------------------------------------------------------------------------
118 m_boxes
= new CellBox
*[HASHSIZE
];
121 for (int i
= 0; i
< HASHSIZE
; i
++)
124 m_cells
= new Cell
[ARRAYSIZE
];
139 // Clears the board, freeing all storage.
147 // clear the hash table pointers
148 for (int i
= 0; i
< HASHSIZE
; i
++)
161 // free available boxes
172 // --------------------------------------------------------------------------
173 // Test and set individual cells
174 // --------------------------------------------------------------------------
177 // Returns whether cell (x, y) is alive.
179 bool Life::IsAlive(wxInt32 x
, wxInt32 y
)
181 CellBox
*c
= LinkBox(x
, y
, FALSE
);
183 return (c
&& c
->IsAlive( x
- c
->m_x
, y
- c
->m_y
));
187 // Sets or clears cell (x, y), according to the 'alive' param.
189 void Life::SetCell(wxInt32 x
, wxInt32 y
, bool alive
)
191 CellBox
*c
= LinkBox(x
, y
);
192 wxUint32 dx
= x
- c
->m_x
;
193 wxUint32 dy
= y
- c
->m_y
;
195 if (c
->SetCell(dx
, dy
, alive
))
204 void Life::SetShape(const LifeShape
& shape
)
206 char *p
= shape
.m_data
;
208 int i0
= -(shape
.m_width
/ 2);
209 int j0
= -(shape
.m_height
/ 2);
210 int i1
= i0
+ shape
.m_width
- 1;
211 int j1
= j0
+ shape
.m_height
- 1;
214 for (int j
= j0
; j
<= j1
; j
++)
215 for (int i
= i0
; i
<= i1
; i
++)
216 SetCell(i
, j
, *(p
++) == '*');
219 // --------------------------------------------------------------------------
220 // Cellbox management functions
221 // --------------------------------------------------------------------------
224 // Creates a box in x, y, either taking it from the list
225 // of available boxes, or allocating a new one.
227 CellBox
* Life::CreateBox(wxInt32 x
, wxInt32 y
, wxUint32 hv
)
231 // if there are no available boxes, alloc a few more
233 for (int i
= 1; i
<= ALLOCBOXES
; i
++)
239 // TODO: handle memory errors. Note that right now, if we
240 // couldn't allocate at least one cellbox, we will crash
241 // before leaving CreateBox(). Probably we should try to
242 // allocate some boxes *before* the m_available list goes
243 // empty, so that we have a margin to handle errors
245 wxLogFatalError(_("Out of memory! Aborting..."));
250 c
->m_next
= m_available
;
254 // take a cellbox from the list of available boxes
256 m_available
= c
->m_next
;
259 memset((void *) c
, 0, sizeof(CellBox
));
263 // insert c in the list
266 if (c
->m_next
) c
->m_next
->m_prev
= c
;
268 // insert c in the hash table
269 c
->m_hnext
= m_boxes
[hv
];
271 if (c
->m_hnext
) c
->m_hnext
->m_hprev
= c
;
277 // Returns a pointer to the box (x, y); if it didn't exist yet,
278 // it returns NULL or creates a new one, depending on the value
279 // of the 'create' parameter.
281 CellBox
* Life::LinkBox(wxInt32 x
, wxInt32 y
, bool create
)
290 // search in the hash table
291 for (c
= m_boxes
[hv
]; c
; c
= c
->m_hnext
)
292 if ((c
->m_x
== x
) && (c
->m_y
== y
)) return c
;
294 // if not found, and (create == TRUE), create a new one
295 return create
? CreateBox(x
, y
, hv
) : (CellBox
*) NULL
;
299 // Removes this box from the list and the hash table and
300 // puts it in the list of available boxes.
302 void Life::KillBox(CellBox
*c
)
304 wxUint32 hv
= HASH(c
->m_x
, c
->m_y
);
306 // remove from the list
308 c
->m_prev
->m_next
= c
->m_next
;
312 // remove from the hash table
313 if (c
!= m_boxes
[hv
])
314 c
->m_hprev
->m_hnext
= c
->m_hnext
;
316 m_boxes
[hv
] = c
->m_hnext
;
319 if (c
->m_next
) c
->m_next
->m_prev
= c
->m_prev
;
320 if (c
->m_hnext
) c
->m_hnext
->m_hprev
= c
->m_hprev
;
321 if (c
->m_up
) c
->m_up
->m_dn
= NULL
;
322 if (c
->m_dn
) c
->m_dn
->m_up
= NULL
;
323 if (c
->m_lf
) c
->m_lf
->m_rt
= NULL
;
324 if (c
->m_rt
) c
->m_rt
->m_lf
= NULL
;
326 // append to the list of available boxes
327 c
->m_next
= m_available
;
331 // --------------------------------------------------------------------------
333 // --------------------------------------------------------------------------
335 // Post eight cells to the cell arrays (changed cells only)
336 void Life::DoLine(wxInt32 i
, wxInt32 j
, wxUint32 live
, wxUint32 old
)
338 wxUint32 diff
= (live
^ old
) & 0x000000ff;
342 for (wxInt32 k
= 8; k
; k
--, i
++)
346 m_cells
[m_ncells
].i
= i
;
347 m_cells
[m_ncells
].j
= j
;
355 // Post eight cells to the cell arrays (alive cells only)
356 void Life::DoLine(wxInt32 i
, wxInt32 j
, wxUint32 live
)
358 if (! (live
& 0x000000ff)) return;
360 for (wxInt32 k
= 8; k
; k
--, i
++)
364 m_cells
[m_ncells
].i
= i
;
365 m_cells
[m_ncells
].j
= j
;
372 void Life::BeginFind(wxInt32 i0
, wxInt32 j0
, wxInt32 i1
, wxInt32 j1
, bool changed
)
374 // TODO: optimize for the case where the maximum number of
375 // cellboxes that fit in the specified viewport is smaller
376 // than the current total of boxes; iterating over the list
377 // should then be faster than searching in the hash table.
379 m_i0
= m_i
= i0
& 0xfffffff8;
380 m_j0
= m_j
= j0
& 0xfffffff8;
381 m_i1
= (i1
+ 7) & 0xfffffff8;
382 m_j1
= (j1
+ 7) & 0xfffffff8;
388 bool Life::FindMore(Cell
*cells
[], size_t *ncells
)
396 for ( ; m_j
<= m_j1
; m_j
+= 8, m_i
= m_i0
)
397 for ( ; m_i
<= m_i1
; m_i
+= 8)
399 if ((c
= LinkBox(m_i
, m_j
, FALSE
)) == NULL
)
402 // check whether there is enough space left in the array
403 if (m_ncells
> (ARRAYSIZE
- 64))
409 DoLine(m_i
, m_j
, c
->m_live1
, c
->m_old1
);
410 DoLine(m_i
, m_j
+ 1, c
->m_live1
>> 8, c
->m_old1
>> 8 );
411 DoLine(m_i
, m_j
+ 2, c
->m_live1
>> 16, c
->m_old1
>> 16);
412 DoLine(m_i
, m_j
+ 3, c
->m_live1
>> 24, c
->m_old1
>> 24);
413 DoLine(m_i
, m_j
+ 4, c
->m_live2
, c
->m_old2
);
414 DoLine(m_i
, m_j
+ 5, c
->m_live2
>> 8, c
->m_old2
>> 8 );
415 DoLine(m_i
, m_j
+ 6, c
->m_live2
>> 16, c
->m_old2
>> 16);
416 DoLine(m_i
, m_j
+ 7, c
->m_live2
>> 24, c
->m_old2
>> 24);
421 for ( ; m_j
<= m_j1
; m_j
+= 8, m_i
= m_i0
)
422 for ( ; m_i
<= m_i1
; m_i
+= 8)
424 if ((c
= LinkBox(m_i
, m_j
, FALSE
)) == NULL
)
427 // check whether there is enough space left in the array
428 if (m_ncells
> (ARRAYSIZE
- 64))
434 DoLine(m_i
, m_j
, c
->m_live1
);
435 DoLine(m_i
, m_j
+ 1, c
->m_live1
>> 8 );
436 DoLine(m_i
, m_j
+ 2, c
->m_live1
>> 16);
437 DoLine(m_i
, m_j
+ 3, c
->m_live1
>> 24);
438 DoLine(m_i
, m_j
+ 4, c
->m_live2
);
439 DoLine(m_i
, m_j
+ 5, c
->m_live2
>> 8 );
440 DoLine(m_i
, m_j
+ 6, c
->m_live2
>> 16);
441 DoLine(m_i
, m_j
+ 7, c
->m_live2
>> 24);
450 // --------------------------------------------------------------------------
452 // --------------------------------------------------------------------------
454 extern unsigned char *g_tab
;
459 // Advance one step in evolution :-)
463 CellBox
*c
, *up
, *dn
, *lf
, *rt
;
464 wxUint32 t1
, t2
, t3
, t4
;
465 bool changed
= FALSE
;
470 // Compute neighbours of each cell
472 // WARNING: unrolled loops and lengthy code follows!
478 if (! (c
->m_live1
|| c
->m_live2
))
489 t1
= c
->m_live1
& 0x000000ff;
494 up
= LinkBox(c
->m_x
, c
->m_y
- 8);
500 c
->m_on
[0] += g_tab2
[t1
];
504 t1
= (c
->m_live2
& 0xff000000) >> 24;
509 dn
= LinkBox(c
->m_x
, c
->m_y
+ 8);
515 c
->m_on
[7] += g_tab2
[t1
];
526 lf
= LinkBox(c
->m_x
- 8, c
->m_y
);
533 lf
->m_up
= LinkBox(c
->m_x
- 8, c
->m_y
- 8);
536 lf
->m_up
->m_on
[7] += 0x10000000;
537 lf
->m_on
[0] += 0x10000000;
538 lf
->m_on
[1] += 0x10000000;
542 lf
->m_on
[0] += 0x10000000;
543 lf
->m_on
[1] += 0x10000000;
544 lf
->m_on
[2] += 0x10000000;
548 lf
->m_on
[1] += 0x10000000;
549 lf
->m_on
[2] += 0x10000000;
550 lf
->m_on
[3] += 0x10000000;
554 lf
->m_on
[2] += 0x10000000;
555 lf
->m_on
[3] += 0x10000000;
556 lf
->m_on
[4] += 0x10000000;
563 lf
= LinkBox(c
->m_x
- 8, c
->m_y
);
568 lf
->m_on
[3] += 0x10000000;
569 lf
->m_on
[4] += 0x10000000;
570 lf
->m_on
[5] += 0x10000000;
574 lf
->m_on
[4] += 0x10000000;
575 lf
->m_on
[5] += 0x10000000;
576 lf
->m_on
[6] += 0x10000000;
580 lf
->m_on
[5] += 0x10000000;
581 lf
->m_on
[6] += 0x10000000;
582 lf
->m_on
[7] += 0x10000000;
588 lf
->m_dn
= LinkBox(c
->m_x
- 8, c
->m_y
+ 8);
591 lf
->m_on
[6] += 0x10000000;
592 lf
->m_on
[7] += 0x10000000;
593 lf
->m_dn
->m_on
[0] += 0x10000000;
602 rt
= LinkBox(c
->m_x
+ 8, c
->m_y
);
609 rt
->m_up
= LinkBox(c
->m_x
+ 8, c
->m_y
- 8);
612 rt
->m_up
->m_on
[7] += 0x00000001;
613 rt
->m_on
[0] += 0x00000001;
614 rt
->m_on
[1] += 0x00000001;
618 rt
->m_on
[0] += 0x00000001;
619 rt
->m_on
[1] += 0x00000001;
620 rt
->m_on
[2] += 0x00000001;
624 rt
->m_on
[1] += 0x00000001;
625 rt
->m_on
[2] += 0x00000001;
626 rt
->m_on
[3] += 0x00000001;
630 rt
->m_on
[2] += 0x00000001;
631 rt
->m_on
[3] += 0x00000001;
632 rt
->m_on
[4] += 0x00000001;
639 rt
= LinkBox(c
->m_x
+ 8, c
->m_y
);
644 rt
->m_on
[3] += 0x00000001;
645 rt
->m_on
[4] += 0x00000001;
646 rt
->m_on
[5] += 0x00000001;
650 rt
->m_on
[4] += 0x00000001;
651 rt
->m_on
[5] += 0x00000001;
652 rt
->m_on
[6] += 0x00000001;
656 rt
->m_on
[5] += 0x00000001;
657 rt
->m_on
[6] += 0x00000001;
658 rt
->m_on
[7] += 0x00000001;
664 rt
->m_dn
= LinkBox(c
->m_x
+ 8, c
->m_y
+ 8);
667 rt
->m_on
[6] += 0x00000001;
668 rt
->m_on
[7] += 0x00000001;
669 rt
->m_dn
->m_on
[0] += 0x00000001;
675 for (i
= 1; i
<= 3; i
++)
677 t1
= ((c
->m_live1
) >> (i
* 8)) & 0x000000ff;
680 c
->m_on
[i
- 1] += g_tab1
[t1
];
681 c
->m_on
[i
] += g_tab2
[t1
];
682 c
->m_on
[i
+ 1] += g_tab1
[t1
];
685 for (i
= 0; i
<= 2; i
++)
687 t1
= ((c
->m_live2
) >> (i
* 8)) & 0x000000ff;
690 c
->m_on
[i
+ 3] += g_tab1
[t1
];
691 c
->m_on
[i
+ 4] += g_tab2
[t1
];
692 c
->m_on
[i
+ 5] += g_tab1
[t1
];
717 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
) & 0xf) ];
718 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 4 ) & 0xf) ] << 4;
720 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 8 ) & 0xf) ] << 8;
721 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 12) & 0xf) ] << 12;
723 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 16) & 0xf) ] << 16;
724 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 20) & 0xf) ] << 20;
726 t1
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 24) & 0xf) ] << 24;
727 t1
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 28) & 0xf) ] << 28;
733 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
) & 0xf) ];
734 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 4 ) & 0xf) ] << 4;
736 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 8 ) & 0xf) ] << 8;
737 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 12) & 0xf) ] << 12;
739 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 16) & 0xf) ] << 16;
740 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 20) & 0xf) ] << 20;
742 t2
|= g_tab
[ ((t4
& 0x0000ffff) << 4 ) + ((t3
>> 24) & 0xf) ] << 24;
743 t2
|= g_tab
[ ((t4
& 0xffff0000) >> 12) + ((t3
>> 28) & 0xf) ] << 28;
745 c
->m_on
[0] = c
->m_on
[1] = c
->m_on
[2] = c
->m_on
[3] =
746 c
->m_on
[4] = c
->m_on
[5] = c
->m_on
[6] = c
->m_on
[7] = 0;
750 // count alive cells (TODO: find a better way to do this)
751 for (int i
= 0; i
< 32; i
++)
753 if (t1
& (1 << i
)) m_numcells
++;
754 if (t2
& (1 << i
)) m_numcells
++;
757 changed
|= ((t1
^ c
->m_old1
) || (t2
^ c
->m_old2
));
759 // mark, and discard if necessary, dead boxes
767 CellBox
*aux
= c
->m_next
;
768 if (c
->m_dead
++ > MAXDEAD
)
778 // ==========================================================================
780 // ==========================================================================
782 // A module to pregenerate lookup tables without having to do it
783 // from the application.
785 class LifeModule
: public wxModule
787 DECLARE_DYNAMIC_CLASS(LifeModule
)
795 IMPLEMENT_DYNAMIC_CLASS(LifeModule
, wxModule
)
797 bool LifeModule::OnInit()
800 g_tab
= new unsigned char [0xfffff];
802 if (!g_tab
) return FALSE
;
804 for (wxUint32 i
= 0; i
< 0xfffff; i
++)
806 wxUint32 val
= i
>> 4;
807 wxUint32 old
= i
& 0x0000f;
810 for (int j
= 0; j
< 4; j
++)
814 if (((val
& 0xf) == 3) || (((val
& 0xf) == 2) && (old
& 0x1)))
821 g_tab
[i
] = (unsigned char) live
;
827 void LifeModule::OnExit()
833 // This table converts from number of neighbors (like in on[]) to
834 // bits, for a set of four cells. It takes as index a five-digit
835 // hexadecimal value (0xNNNNB) where Ns hold number of neighbors
836 // for each cell and B holds their previous state.
838 unsigned char *g_tab
;
840 // This table converts from bits (like in live1, live2) to number
841 // of neighbors for each cell in the upper or lower row.
1103 // This table converts from bits (like in live1, live2) to number
1104 // of neighbors for each cell in the same row (excluding ourselves)