[PATCH] Gnibbles type ahead



[I'm resending this now that I'm a list member.  Apologies if you get
two copies.]

I was recently trying out gnibbles, and was annoyed that most of my
lives ended because gnibbles discarded lookahead.  I was annoyed
enough that I went and patched it to fix the problem.  The patch is
below.  Is this the appropriate list for this?

Thanks,
	Dylan Thurston

Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gnome-games/gnibbles/ChangeLog,v
retrieving revision 1.38
diff -c -r1.38 ChangeLog
*** ChangeLog	2000/05/12 20:31:14	1.38
--- ChangeLog	2000/06/11 08:05:49
***************
*** 1,3 ****
--- 1,11 ----
+ 2000-06-10  Dylan Paul Thurston  <dpt@math.berkeley.edu>
+ 
+ 	* worm.h (GnibblesWorm): added a queue for typeahead key presses
+ 	* worm.c (gnibbles_worm_handle_keypress): modified to store
+ 	typeahead keys
+ 	* gnibbles.c (gnibbles_move_worms): call
+ 	gnibbles_worm_queued_keypress each tick
+ 
  2000-05-12  Andreas Hyden  <a.hyden@cyberpoint.se>
  
  	* gnibbles.desktop: Added Swedish translation.
Index: gnibbles.c
===================================================================
RCS file: /cvs/gnome/gnome-games/gnibbles/gnibbles.c,v
retrieving revision 1.21
diff -c -r1.21 gnibbles.c
*** gnibbles.c	2000/04/26 21:40:39	1.21
--- gnibbles.c	2000/06/11 08:05:50
***************
*** 438,443 ****
--- 438,447 ----
  		if (!dead[i])
  			gnibbles_worm_draw_head (worms[i]);
  
+ 	for (i = 0; i < properties->numworms; i++)
+ 		if (!dead[i])
+ 			gnibbles_worm_queued_keypress (worms[i]);
+ 
  	if (status & GAMEOVER) {
  		gnibbles_play_sound ("crash");
  		gnibbles_play_sound ("gameover");
Index: worm.c
===================================================================
RCS file: /cvs/gnome/gnome-games/gnibbles/worm.c,v
retrieving revision 1.11
diff -c -r1.11 worm.c
*** worm.c	2000/04/26 21:40:39	1.11
--- worm.c	2000/06/11 08:05:52
***************
*** 23,28 ****
--- 23,29 ----
  #include "bonus.h"
  #include "warpmanager.h"
  #include "properties.h"
+ #include <assert.h>
  
  extern gchar board[BOARDWIDTH][BOARDHEIGHT];
  extern GnibblesWorm *worms[NUMWORMS];
***************
*** 67,85 ****
  	worm->stop = 0;
  	worm->length = 1;
  	worm->change = SLENGTH - 1;
! 	worm->keypress = 0;
  }
  
  void gnibbles_worm_handle_keypress (GnibblesWorm *worm, guint keyval)
  {
! 	if (worm->keypress)
! 		return;
! 	
  	if (properties->wormprops[worm->number]->relmove) {
! 		if (keyval == properties->wormprops[worm->number]->left)
  			worm->direction = worm->direction - 1;
! 		if (keyval == properties->wormprops[worm->number]->right)
  			worm->direction = worm->direction + 1;
  		if (worm->direction == 0)
  			worm->direction = 4;
  		if (worm->direction == 5)
--- 68,117 ----
  	worm->stop = 0;
  	worm->length = 1;
  	worm->change = SLENGTH - 1;
! 	worm->keyspressed = -1;
  }
  
  void gnibbles_worm_handle_keypress (GnibblesWorm *worm, guint keyval)
  {
! 	if (worm->keyspressed < 0) {
! 		if (gnibbles_worm_keypress(worm, keyval))
! 			worm->keyspressed = 0;
! 	} else if (worm->keyspressed < KEYAHEAD) {
! 		worm->keyval[worm->keyspressed++] = keyval;
! 	}
! }
! 
! void gnibbles_worm_queued_keypress (GnibblesWorm *worm)
! {
! 	gint i, v = 0;
! 
! 	assert(worm->keyspressed <= KEYAHEAD);
! 	if (worm->keyspressed > 0) {
! 		do {
! 			v = gnibbles_worm_keypress (worm, worm->keyval[0]);
! 			worm->keyspressed--;
! 			for (i = 0; i < worm->keyspressed; i++)
! 				worm->keyval[i] = worm->keyval[i+1];
! 		} while (worm->keyspressed > 0 && !v);
! 		if (!v)
! 			worm->keyspressed = -1;
! 	} else
! 		worm->keyspressed = -1;
! }
! 
! gint gnibbles_worm_keypress (GnibblesWorm *worm, guint keyval)
! {
! 	gint valid = 0;
! 
  	if (properties->wormprops[worm->number]->relmove) {
! 		if (keyval == properties->wormprops[worm->number]->left) {
  			worm->direction = worm->direction - 1;
! 			valid = 1;
! 		}
! 		if (keyval == properties->wormprops[worm->number]->right) {
  			worm->direction = worm->direction + 1;
+ 			valid = 1;
+ 		}
  		if (worm->direction == 0)
  			worm->direction = 4;
  		if (worm->direction == 5)
***************
*** 88,111 ****
  		if ((keyval == properties->wormprops[worm->number]->up) &&
  				(worm->direction != WORMDOWN)) {
  			worm->direction = WORMUP;
! 			worm->keypress = 1;
  		}
  		if ((keyval == properties->wormprops[worm->number]->right) &&
  				(worm->direction !=WORMLEFT)) {
  			worm->direction = WORMRIGHT;
! 			worm->keypress = 1;
  		}
  		if ((keyval == properties->wormprops[worm->number]->down) &&
  				(worm->direction != WORMUP)) {
  			worm->direction = WORMDOWN;
! 			worm->keypress = 1;
  		}
  		if ((keyval == properties->wormprops[worm->number]->left) &&
  				(worm->direction != WORMRIGHT)) {
  			worm->direction = WORMLEFT;
! 			worm->keypress = 1;
  		}
  	}
  }
  
  static gint gnibbles_worm_reverse (gpointer data)
--- 120,144 ----
  		if ((keyval == properties->wormprops[worm->number]->up) &&
  				(worm->direction != WORMDOWN)) {
  			worm->direction = WORMUP;
! 			valid = 1;
  		}
  		if ((keyval == properties->wormprops[worm->number]->right) &&
  				(worm->direction !=WORMLEFT)) {
  			worm->direction = WORMRIGHT;
! 			valid = 1;
  		}
  		if ((keyval == properties->wormprops[worm->number]->down) &&
  				(worm->direction != WORMUP)) {
  			worm->direction = WORMDOWN;
! 			valid = 1;
  		}
  		if ((keyval == properties->wormprops[worm->number]->left) &&
  				(worm->direction != WORMRIGHT)) {
  			worm->direction = WORMLEFT;
! 			valid = 1;
  		}
  	}
+ 	return valid;
  }
  
  static gint gnibbles_worm_reverse (gpointer data)
***************
*** 199,206 ****
  
  void gnibbles_worm_draw_head (GnibblesWorm *worm)
  {
- 	worm->keypress = 0;
- 	
  	switch (worm->direction) {
  		case WORMUP:
  			worm->xoff[worm->start] = 0;
--- 232,237 ----
Index: worm.h
===================================================================
RCS file: /cvs/gnome/gnome-games/gnibbles/worm.h,v
retrieving revision 1.6
diff -c -r1.6 worm.h
*** worm.h	1999/03/12 07:05:17	1.6
--- worm.h	2000/06/11 08:05:52
***************
*** 36,41 ****
--- 36,45 ----
  
  #define GROWFACTOR 4
  
+ /* Number of typeahead keys to allow */
+ /* Make it large, since we get key up and key down events */
+ #define KEYAHEAD  10
+ 
  typedef struct
  {
  	gint xhead, yhead;
***************
*** 45,51 ****
  	gint start, stop;
  	gint length;
  	gint change;
! 	gint keypress;
  	gint lives;
  	guint score;
  	guint number;
--- 49,56 ----
  	gint start, stop;
  	gint length;
  	gint change;
! 	gint keyspressed;
! 	guint keyval[KEYAHEAD];
  	gint lives;
  	guint score;
  	guint number;
***************
*** 59,64 ****
--- 64,73 ----
  		gint t_direction);
  
  void gnibbles_worm_handle_keypress (GnibblesWorm *worm, guint keyval);
+ 
+ gint gnibbles_worm_keypress (GnibblesWorm *worm, guint keyval);
+ 
+ void gnibbles_worm_queued_keypress (GnibblesWorm *worm);
  
  gint gnibbles_worm_move_test_head (GnibblesWorm *worm);
  




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]