home *** CD-ROM | disk | FTP | other *** search
- /*
- * callbacks.c -- xrobots v1.0
- */
-
- #include <X11/X.h>
- #include <X11/Intrinsic.h>
- #include <X11/StringDefs.h>
- #include <math.h>
- #include "game.h"
- #include "callbacks.h"
- #include "graphics.h"
-
- extern Widget sonic_command;
-
- /*----------------------------------------------------------------------*/
- /* Editorial note: For most of the functions in this file, callbacks
- * are nothing more than overgrown actions. Both are used and
- * needed. Unfortunately, there's alot of boilerplate overhead.
- * Actually, all this could be done without any callbacks.
- */
-
- /*ARGSUSED*/
- XtCallbackProc
- teleport_callback(widget,closure,callData)
- Widget widget;
- caddr_t closure;
- caddr_t callData;
- {
- teleport();
- }
-
- /*ARGSUSED*/
- static XtActionProc
- teleport_action(w,event,params,num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- teleport();
- }
-
-
- /*ARGSUSED*/
- XtCallbackProc
- wait_callback(widget,closure,callData)
- Widget widget;
- caddr_t closure;
- caddr_t callData;
- {
- wait_for_em();
- }
-
-
- /*ARGSUSED*/
- static XtActionProc
- wait_action(w,event,params,num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- wait_for_em();
- }
-
-
-
- /*ARGSUSED*/
- XtCallbackProc
- sonic_callback(widget,closure,callData)
- Widget widget;
- caddr_t closure;
- caddr_t callData;
- {
- static Arg arg = { XtNsensitive, False};
- if(!game_active) return;
- XtSetValues(sonic_command,&arg,1);
- sonic_screwdriver();
- }
-
- /*ARGSUSED*/
- static XtActionProc
- sonic_action(w,event,params,num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- static Arg arg = { XtNsensitive, False};
- if(!game_active) return;
- XtSetValues(sonic_command,&arg,1);
- sonic_screwdriver();
- }
-
-
- void
- reset_sonic_button()
- {
- static Arg arg = { XtNsensitive, True };
- XtSetValues(sonic_command,&arg,1);
- }
-
-
- /*ARGSUSED*/
- XtCallbackProc
- new_game_callback(widget,closure,callData)
- Widget widget;
- caddr_t closure;
- caddr_t callData;
- {
- new_game();
- }
-
-
- /*ARGSUSED*/
- static XtActionProc
- new_game_action(w,event,params,num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- new_game();
- }
-
-
- /*ARGSUSED*/
- XtCallbackProc
- quit_callback(widget,closure,callData)
- Widget widget;
- caddr_t closure;
- caddr_t callData;
- {
- quit_game();
- }
-
- /*ARGSUSED*/
- static XtActionProc
- quit_action(w,event,params,num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- quit_game();
- }
-
-
- /*ARGSUSED*/
- static XtActionProc
- do_nothing_action(w,event,params,num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- /* do nothing */
- }
-
-
-
- /*----------------------------------------------------------------------*/
-
-
- int
- determine_direction(button_x,button_y)
- int button_x,button_y;
- {
- /*
- * Given the mouse's x&y position, this routine determines the direction
- * relative to the player, and returns the result coded into a int.
- */
- float slope, invslope;
- int direction = 0;
- int coord_x = pos_to_coord(human_x) + CELLSIZE/2,
- coord_y = pos_to_coord(human_y) + CELLSIZE/2;
-
- if( (abs(coord_x - button_x) < (CELLSIZE/2)+2) &&
- (abs(coord_y - button_y) < (CELLSIZE/2)+2))
- return(STILL); /* cursor is directly over the player */
-
- if(button_x - coord_x != 0) {
- slope = fabs((float)(button_y - coord_y) / (float)(button_x - coord_x));
-
- if( button_x > coord_x ) { /* in coordinates 1 or 4 */
- if( (slope < 2) && (human_x < MAXX) )
- direction = RIGHT;
- }
- else /* in coordinates 2 or 3 */
- if( (slope < 2) && (human_x > -1) )
- direction = LEFT;
- }
-
- if(button_y - coord_y != 0) {
- invslope = fabs((float)(button_x - coord_x) / (float)(button_y - coord_y));
-
- if( button_y > coord_y ) { /* in coordinates 1 or 2 */
- if( (invslope < 2) && (human_y < MAXY) )
- direction |= DOWN;
- }
-
- else /* in coordinates 3 or 4 */
- if( (invslope < 2) && (human_y > -1) )
- direction |= UP;
- }
- return(direction);
- }
-
-
-
- /*----------------------------------------------------------------------*/
-
-
- /*ARGSUSED*/
- static XtActionProc
- move_action(w,event,params,num_params)
- Widget w;
- XButtonEvent *event;
- String *params;
- Cardinal *num_params;
- {
- /*
- * Called to move the player's icon. This action can be called
- * when a mouse button is pressed or when a key is pressed.
- * This is all dependent on the current translations.
- */
- int direction;
- int tmp_human_x = human_x, tmp_human_y = human_y;
- int num_wasted;
- int param_count = *num_params;
-
- if(!game_active) return;
-
- if(!*num_params) { /* no parameters - use the mouse pointer */
-
- direction = determine_direction(event->x,event->y);
- if(!direction) return;
-
- if(direction & UP) tmp_human_y--;
- if(direction & DOWN) tmp_human_y++;
- if(direction & LEFT) tmp_human_x--;
- if(direction & RIGHT) tmp_human_x++;
-
- } else
-
- while(param_count--) {
- /* else pull the direction out of the parameters. */
- /* you can 'cheat' here... but who's gonna tell? */
- if(!strcmp("right",*(params+param_count)))
- tmp_human_x++;
- if(!strcmp("left", *(params+param_count)))
- tmp_human_x--;
- if(!strcmp("up", *(params+param_count)))
- tmp_human_y--;
- if(!strcmp("down", *(params+param_count)))
- tmp_human_y++;
- }
-
- last_human_x = human_x;
- last_human_y = human_y;
-
- if( can_go(tmp_human_x,tmp_human_y) ) {
- human_x = tmp_human_x;
- human_y = tmp_human_y;
- num_wasted = chase();
- show_movement();
- add_score(num_wasted);
- if(!num_robots)
- new_level();
- else
- display_possible_moves();
- pointer_moved((Widget)0,(caddr_t)0,event);
- }
-
- }
-
-
-
-
- /*ARGSUSED*/
- static XtActionProc
- go_here_action(w,event,params,num_params)
- Widget w;
- XButtonEvent *event;
- String *params;
- Cardinal *num_params;
- {
- /*
- * This action causes player's icon to try to go to a spot in the
- * play area. It stops if a move cannot be made.
- */
- int direction;
- int tmp_human_x, tmp_human_y;
- int num_wasted;
-
- if(!game_active) return;
-
- while(direction = determine_direction(event->x,event->y)) {
- if(direction == STILL) break;
- tmp_human_x = human_x;
- tmp_human_y = human_y;
-
- if(direction & UP) tmp_human_y--;
- if(direction & DOWN) tmp_human_y++;
- if(direction & LEFT) tmp_human_x--;
- if(direction & RIGHT) tmp_human_x++;
-
- if( !can_go(tmp_human_x,tmp_human_y) )
- break;
- last_human_x = human_x;
- last_human_y = human_y;
- human_x = tmp_human_x;
- human_y = tmp_human_y;
- num_wasted = chase();
- show_movement();
- add_score(num_wasted);
- if(!num_robots) {
- new_level();
- break;
- }
- if(spiffy) {
- display_possible_moves();
- pointer_moved((Widget)0,(caddr_t)0,event);
- }
- }
- if(spiffy)
- display_possible_moves();
- pointer_moved((Widget)0,(caddr_t)0,event);
- }
-
-
-
- /*ARGSUSED*/
- XtEventHandler
- pointer_moved(w, closure, event)
- Widget w;
- caddr_t closure;
- XPointerMovedEvent *event;
- {
- if(game_active)
- update_pointer( determine_direction(event->x,event->y) );
- }
-
- /*----------------------------------------------------------------------*/
- /*----------------------------------------------------------------------*/
-
- static XtActionsRec actions[] = {
- {"wait", (XtActionProc)wait_action},
- {"teleport", (XtActionProc)teleport_action},
- {"sonic", (XtActionProc)sonic_action},
- {"move", (XtActionProc)move_action},
- {"go_here", (XtActionProc)go_here_action},
- {"quit", (XtActionProc)quit_action},
- {"new_game", (XtActionProc)new_game_action},
- {"do_nothing",(XtActionProc)do_nothing_action},
- };
-
- void
- init_actions()
- {
- XtAddActions(actions,XtNumber(actions));
- }
-
-
-