home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume4 / xrobots / part01 / callbacks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-04  |  7.6 KB  |  365 lines

  1. /*
  2.  * callbacks.c  --  xrobots v1.0
  3.  */
  4.  
  5. #include <X11/X.h>
  6. #include <X11/Intrinsic.h>
  7. #include <X11/StringDefs.h>
  8. #include <math.h>
  9. #include "game.h"
  10. #include "callbacks.h"
  11. #include "graphics.h"
  12.  
  13. extern Widget sonic_command;
  14.  
  15. /*----------------------------------------------------------------------*/
  16. /* Editorial note:  For most of the functions in this file, callbacks 
  17.  *     are nothing more than overgrown actions.  Both are used and
  18.  *    needed.  Unfortunately, there's alot of boilerplate overhead.
  19.  *     Actually, all this could be done without any callbacks.
  20.  */
  21.  
  22. /*ARGSUSED*/
  23. XtCallbackProc
  24. teleport_callback(widget,closure,callData)
  25.   Widget widget;
  26.   caddr_t closure;
  27.   caddr_t callData;
  28. {
  29.   teleport();
  30. }
  31.  
  32. /*ARGSUSED*/
  33. static XtActionProc
  34. teleport_action(w,event,params,num_params)
  35.   Widget w;
  36.   XEvent *event;
  37.   String *params;
  38.   Cardinal *num_params;
  39. {
  40.   teleport();
  41. }
  42.  
  43.  
  44. /*ARGSUSED*/
  45. XtCallbackProc
  46. wait_callback(widget,closure,callData)
  47.   Widget widget;
  48.   caddr_t closure;
  49.   caddr_t callData;
  50. {
  51.   wait_for_em();
  52. }
  53.  
  54.  
  55. /*ARGSUSED*/
  56. static XtActionProc
  57. wait_action(w,event,params,num_params)
  58.   Widget w;
  59.   XEvent *event;
  60.   String *params;
  61.   Cardinal *num_params;
  62. {
  63.   wait_for_em();
  64. }
  65.  
  66.  
  67.  
  68. /*ARGSUSED*/
  69. XtCallbackProc
  70. sonic_callback(widget,closure,callData)
  71.   Widget widget;
  72.   caddr_t closure;
  73.   caddr_t callData;
  74. {
  75.   static Arg arg = { XtNsensitive, False};
  76.   if(!game_active) return;
  77.   XtSetValues(sonic_command,&arg,1);
  78.   sonic_screwdriver();
  79. }
  80.  
  81. /*ARGSUSED*/
  82. static XtActionProc
  83. sonic_action(w,event,params,num_params)
  84.   Widget w;
  85.   XEvent *event;
  86.   String *params;
  87.   Cardinal *num_params;
  88. {
  89.   static Arg arg = { XtNsensitive, False};
  90.   if(!game_active) return;
  91.   XtSetValues(sonic_command,&arg,1);
  92.   sonic_screwdriver();
  93. }
  94.  
  95.  
  96. void
  97. reset_sonic_button()
  98. {
  99.   static Arg arg = { XtNsensitive, True };
  100.   XtSetValues(sonic_command,&arg,1);
  101. }
  102.  
  103.  
  104. /*ARGSUSED*/
  105. XtCallbackProc
  106. new_game_callback(widget,closure,callData)
  107.   Widget widget;
  108.   caddr_t closure;
  109.   caddr_t callData;
  110. {
  111.   new_game();
  112. }
  113.  
  114.  
  115. /*ARGSUSED*/
  116. static XtActionProc
  117. new_game_action(w,event,params,num_params)
  118.   Widget w;
  119.   XEvent *event;
  120.   String *params;
  121.   Cardinal *num_params;
  122. {
  123.   new_game();
  124. }
  125.  
  126.  
  127. /*ARGSUSED*/
  128. XtCallbackProc
  129. quit_callback(widget,closure,callData)
  130.   Widget widget;
  131.   caddr_t closure;
  132.   caddr_t callData;
  133. {
  134.  quit_game();
  135. }
  136.  
  137. /*ARGSUSED*/
  138. static XtActionProc
  139. quit_action(w,event,params,num_params)
  140.   Widget w;
  141.   XEvent *event;
  142.   String *params;
  143.   Cardinal *num_params;
  144. {
  145.   quit_game();
  146. }
  147.  
  148.  
  149. /*ARGSUSED*/
  150. static XtActionProc
  151. do_nothing_action(w,event,params,num_params)
  152.   Widget w;
  153.   XEvent *event;
  154.   String *params;
  155.   Cardinal *num_params;
  156. {
  157.   /* do nothing */
  158. }
  159.  
  160.  
  161.  
  162. /*----------------------------------------------------------------------*/
  163.  
  164.  
  165. int 
  166. determine_direction(button_x,button_y)
  167.   int button_x,button_y;
  168. {
  169. /* 
  170.  * Given the mouse's x&y position, this routine determines the direction
  171.  * relative to the player, and returns the result coded into a int.
  172.  */
  173.   float slope, invslope;
  174.   int direction = 0;
  175.   int coord_x = pos_to_coord(human_x) + CELLSIZE/2,
  176.       coord_y = pos_to_coord(human_y) + CELLSIZE/2;
  177.  
  178.   if( (abs(coord_x - button_x) < (CELLSIZE/2)+2) &&
  179.       (abs(coord_y - button_y) < (CELLSIZE/2)+2)) 
  180.     return(STILL);      /* cursor is directly over the player */ 
  181.  
  182.   if(button_x - coord_x != 0) {
  183.     slope = fabs((float)(button_y - coord_y) / (float)(button_x - coord_x)); 
  184.  
  185.     if( button_x > coord_x ) {            /* in coordinates 1 or 4 */
  186.       if( (slope < 2) && (human_x < MAXX) )
  187.         direction = RIGHT;
  188.     }
  189.     else                     /* in coordinates 2 or 3 */
  190.       if( (slope < 2) && (human_x > -1) )
  191.         direction = LEFT;
  192.   }
  193.  
  194.   if(button_y - coord_y != 0) {
  195.     invslope = fabs((float)(button_x - coord_x) / (float)(button_y - coord_y)); 
  196.  
  197.     if( button_y > coord_y ) {            /* in coordinates 1 or 2 */
  198.       if( (invslope < 2) && (human_y < MAXY) )
  199.         direction |= DOWN;
  200.     }
  201.  
  202.     else                     /* in coordinates 3 or 4 */
  203.       if( (invslope < 2) && (human_y > -1) )
  204.         direction |= UP;
  205.   }
  206.   return(direction);
  207. }
  208.  
  209.  
  210.  
  211. /*----------------------------------------------------------------------*/
  212.  
  213.  
  214. /*ARGSUSED*/
  215. static XtActionProc
  216. move_action(w,event,params,num_params)
  217.   Widget w;
  218.   XButtonEvent *event;
  219.   String *params;
  220.   Cardinal *num_params;
  221. {
  222. /* 
  223.  *  Called to move the player's icon.  This action can be called 
  224.  *  when a mouse button is pressed or when a key is pressed.
  225.  *  This is all dependent on the current translations.
  226.  */
  227.   int direction;
  228.   int tmp_human_x = human_x, tmp_human_y = human_y;
  229.   int num_wasted;
  230.   int param_count = *num_params;
  231.  
  232.   if(!game_active) return;
  233.  
  234.   if(!*num_params) {    /* no parameters - use the mouse pointer */
  235.  
  236.     direction = determine_direction(event->x,event->y);
  237.     if(!direction)  return;
  238.  
  239.     if(direction & UP)    tmp_human_y--;
  240.     if(direction & DOWN)  tmp_human_y++;
  241.     if(direction & LEFT)  tmp_human_x--;
  242.     if(direction & RIGHT) tmp_human_x++;
  243.  
  244.   } else 
  245.  
  246.     while(param_count--) {
  247.       /* else pull the direction out of the parameters. */
  248.       /* you can 'cheat' here... but who's gonna tell? */
  249.       if(!strcmp("right",*(params+param_count)))
  250.         tmp_human_x++;
  251.       if(!strcmp("left", *(params+param_count)))
  252.         tmp_human_x--;
  253.       if(!strcmp("up",   *(params+param_count)))
  254.         tmp_human_y--;
  255.       if(!strcmp("down", *(params+param_count)))
  256.         tmp_human_y++;
  257.     }
  258.  
  259.   last_human_x = human_x;
  260.   last_human_y = human_y;
  261.  
  262.   if( can_go(tmp_human_x,tmp_human_y) ) {
  263.     human_x = tmp_human_x;
  264.     human_y = tmp_human_y;
  265.     num_wasted = chase();
  266.     show_movement();
  267.     add_score(num_wasted);
  268.     if(!num_robots)
  269.       new_level();
  270.     else
  271.       display_possible_moves();
  272.     pointer_moved((Widget)0,(caddr_t)0,event);
  273.   }
  274.  
  275. }
  276.  
  277.  
  278.  
  279.  
  280. /*ARGSUSED*/
  281. static XtActionProc
  282. go_here_action(w,event,params,num_params)
  283.   Widget w;
  284.   XButtonEvent *event;
  285.   String *params;
  286.   Cardinal *num_params;
  287. {
  288. /* 
  289.  * This action causes player's icon to try to go to a spot in the 
  290.  * play area.  It stops if a move cannot be made.
  291.  */
  292.   int direction;
  293.   int tmp_human_x, tmp_human_y;
  294.   int num_wasted;
  295.  
  296.   if(!game_active) return;
  297.  
  298.   while(direction = determine_direction(event->x,event->y)) {
  299.     if(direction == STILL) break;
  300.     tmp_human_x = human_x;
  301.     tmp_human_y = human_y;
  302.  
  303.     if(direction & UP)    tmp_human_y--;
  304.     if(direction & DOWN)  tmp_human_y++;
  305.     if(direction & LEFT)  tmp_human_x--;
  306.     if(direction & RIGHT) tmp_human_x++;
  307.  
  308.     if( !can_go(tmp_human_x,tmp_human_y) ) 
  309.       break;
  310.     last_human_x = human_x;
  311.     last_human_y = human_y;
  312.     human_x = tmp_human_x;
  313.     human_y = tmp_human_y;
  314.     num_wasted = chase();
  315.     show_movement();
  316.     add_score(num_wasted);
  317.     if(!num_robots) {
  318.       new_level();
  319.       break;
  320.     }
  321.     if(spiffy) {
  322.       display_possible_moves();
  323.       pointer_moved((Widget)0,(caddr_t)0,event);
  324.     }
  325.   }
  326.   if(spiffy)
  327.     display_possible_moves();
  328.   pointer_moved((Widget)0,(caddr_t)0,event);
  329. }
  330.  
  331.  
  332.  
  333. /*ARGSUSED*/
  334. XtEventHandler
  335. pointer_moved(w, closure, event)
  336.   Widget w;
  337.   caddr_t closure;
  338.   XPointerMovedEvent *event;
  339. {
  340.   if(game_active) 
  341.     update_pointer( determine_direction(event->x,event->y) );
  342. }
  343.  
  344. /*----------------------------------------------------------------------*/
  345. /*----------------------------------------------------------------------*/
  346.  
  347. static XtActionsRec actions[] = {
  348.   {"wait",      (XtActionProc)wait_action},
  349.   {"teleport",  (XtActionProc)teleport_action},
  350.   {"sonic",     (XtActionProc)sonic_action},
  351.   {"move",      (XtActionProc)move_action},
  352.   {"go_here",   (XtActionProc)go_here_action},
  353.   {"quit",      (XtActionProc)quit_action},
  354.   {"new_game",  (XtActionProc)new_game_action},
  355.   {"do_nothing",(XtActionProc)do_nothing_action},
  356. };
  357.  
  358. void
  359. init_actions()
  360. {
  361.    XtAddActions(actions,XtNumber(actions));
  362. }
  363.  
  364.  
  365.