rnd-19990122-1
[rocksndiamonds.git] / src / events.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  (c) 1995-98 Artsoft Entertainment                       *
5 *              Holger Schemel                              *
6 *              Oststrasse 11a                              *
7 *              33604 Bielefeld                             *
8 *              phone: ++49 +521 290471                     *
9 *              email: aeglos@valinor.owl.de                *
10 *----------------------------------------------------------*
11 *  events.c                                                *
12 ***********************************************************/
13
14 #include "events.h"
15 #include "init.h"
16 #include "screens.h"
17 #include "tools.h"
18 #include "game.h"
19 #include "editor.h"
20 #include "misc.h"
21 #include "tape.h"
22 #include "joystick.h"
23 #include "buttons.h"
24 #include "network.h"
25
26 /* values for key_status */
27 #define KEY_NOT_PRESSED         FALSE
28 #define KEY_RELEASED            FALSE
29 #define KEY_PRESSED             TRUE
30
31 void EventLoop(void)
32 {
33   while(1)
34   {
35     if (XPending(display))      /* got event from X server */
36     {
37       XEvent event;
38
39       XNextEvent(display, &event);
40
41       switch(event.type)
42       {
43         case ButtonPress:
44         case ButtonRelease:
45           HandleButtonEvent((XButtonEvent *) &event);
46           break;
47
48         case MotionNotify:
49           HandleMotionEvent((XMotionEvent *) &event);
50           break;
51
52         case KeyPress:
53         case KeyRelease:
54           HandleKeyEvent((XKeyEvent *) &event);
55           break;
56
57         default:
58           HandleOtherEvents(&event);
59           break;
60       }
61     }
62
63     HandleNoXEvent();
64
65     /* don't use all CPU time when idle; the main loop while playing
66        has its own synchronization and is CPU friendly, too */
67
68     if (game_status != PLAYING)
69     {
70       XSync(display, FALSE);
71       Delay(10);
72     }
73
74     /* refresh window contents from drawing buffer, if needed */
75     BackToFront();
76
77     if (game_status == EXITGAME)
78       return;
79   }
80 }
81
82 void HandleOtherEvents(XEvent *event)
83 {
84   switch(event->type)
85   {
86     case Expose:
87       HandleExposeEvent((XExposeEvent *) event);
88       break;
89
90     case UnmapNotify:
91       SleepWhileUnmapped();
92       break;
93
94     case FocusIn:
95     case FocusOut:
96       HandleFocusEvent((XFocusChangeEvent *) event);
97       break;
98
99     case ClientMessage:
100       HandleClientMessageEvent((XClientMessageEvent *) event);
101       break;
102
103     default:
104       break;
105   }
106 }
107
108 void ClearEventQueue()
109 {
110   while(XPending(display))
111   {
112     XEvent event;
113
114     XNextEvent(display, &event);
115
116     switch(event.type)
117     {
118       case ButtonRelease:
119         button_status = MB_RELEASED;
120         break;
121
122       case KeyRelease:
123         key_joystick_mapping = 0;
124         break;
125
126       default:
127         HandleOtherEvents(&event);
128         break;
129     }
130   }
131 }
132
133 void SleepWhileUnmapped()
134 {
135   boolean window_unmapped = TRUE;
136
137   XAutoRepeatOn(display);
138
139   while(window_unmapped)
140   {
141     XEvent event;
142
143     XNextEvent(display, &event);
144
145     switch(event.type)
146     {
147       case ButtonRelease:
148         button_status = MB_RELEASED;
149         break;
150
151       case KeyRelease:
152         key_joystick_mapping = 0;
153         break;
154
155       case MapNotify:
156         window_unmapped = FALSE;
157         break;
158
159       case UnmapNotify:
160         /* this is only to surely prevent the 'should not happen' case
161          * of recursively looping between 'SleepWhileUnmapped()' and
162          * 'HandleOtherEvents()' which usually calls this funtion.
163          */
164         break;
165
166       default:
167         HandleOtherEvents(&event);
168         break;
169     }
170   }
171
172   if (game_status==PLAYING)
173     XAutoRepeatOff(display);
174 }
175
176 void HandleExposeEvent(XExposeEvent *event)
177 {
178   int x = event->x, y = event->y;
179   int width = event->width, height = event->height;
180
181   if (setup.direct_draw && game_status==PLAYING)
182   {
183     int xx,yy;
184     int x1 = (x-SX)/TILEX, y1 = (y-SY)/TILEY;
185     int x2 = (x-SX+width)/TILEX, y2 = (y-SY+height)/TILEY;
186
187     SetDrawtoField(DRAW_BACKBUFFER);
188
189     for(xx=0; xx<SCR_FIELDX; xx++)
190       for(yy=0; yy<SCR_FIELDY; yy++)
191         if (xx>=x1 && xx<=x2 && yy>=y1 && yy<=y2)
192           DrawScreenField(xx,yy);
193     DrawAllPlayers();
194
195     SetDrawtoField(DRAW_DIRECT);
196   }
197
198   if (setup.soft_scrolling && game_status == PLAYING)
199   {
200     int fx = FX, fy = FY;
201
202     fx += (ScreenMovDir & (MV_LEFT|MV_RIGHT) ? ScreenGfxPos : 0);
203     fy += (ScreenMovDir & (MV_UP|MV_DOWN)    ? ScreenGfxPos : 0);
204
205     XCopyArea(display,fieldbuffer,backbuffer,gc,
206               fx,fy, SXSIZE,SYSIZE,
207               SX,SY);
208   }
209
210   XCopyArea(display,drawto,window,gc, x,y, width,height, x,y);
211
212   XFlush(display);
213 }
214
215 void HandleButtonEvent(XButtonEvent *event)
216 {
217   motion_status = FALSE;
218
219   if (event->type==ButtonPress)
220     button_status = event->button;
221   else
222     button_status = MB_RELEASED;
223
224   HandleButton(event->x, event->y, button_status);
225 }
226
227 void HandleMotionEvent(XMotionEvent *event)
228 {
229   Window root, child;
230   int root_x, root_y;
231   int win_x, win_y;
232   unsigned int mask;
233
234   if (!XQueryPointer(display, window, &root, &child, &root_x, &root_y,
235                      &win_x, &win_y, &mask))
236     return;
237
238   if (!button_status && game_status != LEVELED)
239     return;
240
241   motion_status = TRUE;
242
243   HandleButton(win_x, win_y, button_status);
244 }
245
246 void HandleKeyEvent(XKeyEvent *event)
247 {
248   int key_status = (event->type == KeyPress ? KEY_PRESSED : KEY_RELEASED);
249   KeySym key;
250
251   if (game_status == PLAYING)
252   {
253     /* use '0' instead of 'event->state' to get the key without modifiers */
254     key = XLookupKeysym(event, 0);
255   }
256   else
257   {
258     /* get the key with all modifiers */
259     char buffer[10];
260     int buffer_size = 10;
261     XComposeStatus compose;
262     int char_count;
263
264     char_count = XLookupString(event, buffer, buffer_size, &key, &compose);
265     buffer[char_count] = '\0';
266   }
267
268   HandleKey(key, key_status);
269 }
270
271 void HandleFocusEvent(XFocusChangeEvent *event)
272 {
273   static int old_joystick_status = -1;
274
275   if (event->type == FocusOut)
276   {
277     XAutoRepeatOn(display);
278     old_joystick_status = joystick_status;
279     joystick_status = JOYSTICK_OFF;
280     key_joystick_mapping = 0;
281   }
282   else if (event->type == FocusIn)
283   {
284     if (game_status == PLAYING)
285       XAutoRepeatOff(display);
286     if (old_joystick_status != -1)
287       joystick_status = old_joystick_status;
288   }
289 }
290
291 void HandleClientMessageEvent(XClientMessageEvent *event)
292 {
293 #ifndef MSDOS
294   if ((event->window == window) &&
295       (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
296     CloseAllAndExit(0);
297 #endif
298 }
299
300 void HandleButton(int mx, int my, int button)
301 {
302   static int old_mx = 0, old_my = 0;
303
304   if (button < 0)
305   {
306     mx = old_mx;
307     my = old_my;
308     button = -button;
309   }
310   else
311   {
312     old_mx = mx;
313     old_my = my;
314
315     HandleVideoButtons(mx,my, button);
316     HandleSoundButtons(mx,my, button);
317     HandleGameButtons(mx,my, button);
318   }
319
320   HandleGadgets(mx, my, button);
321
322   switch(game_status)
323   {
324     case MAINMENU:
325       HandleMainMenu(mx,my, 0,0, button);
326       break;
327
328     case TYPENAME:
329       HandleTypeName(0, XK_Return);
330       break;
331
332     case CHOOSELEVEL:
333       HandleChooseLevel(mx,my, 0,0, button);
334       break;
335
336     case HALLOFFAME:
337       HandleHallOfFame(button);
338       break;
339
340     case LEVELED:
341       LevelEd(mx,my, button);
342       break;
343
344     case HELPSCREEN:
345       HandleHelpScreen(button);
346       break;
347
348     case SETUP:
349       HandleSetupScreen(mx,my, 0,0, button);
350       break;
351
352     case SETUPINPUT:
353       HandleSetupInputScreen(mx,my, 0,0, button);
354       break;
355
356     case PLAYING:
357 #ifdef DEBUG
358       if (button == MB_RELEASED)
359       {
360         int sx = (mx - SX) / TILEX;
361         int sy = (my - SY) / TILEY;
362
363         if (IN_VIS_FIELD(sx,sy))
364         {
365           int x = LEVELX(sx);
366           int y = LEVELY(sy);
367
368           printf("INFO: SCREEN(%d, %d), LEVEL(%d, %d)\n", sx, sy, x, y);
369
370           if (!IN_LEV_FIELD(x, y))
371             break;
372
373           printf("      Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
374           printf("      Store[%d][%d] == %d\n", x,y, Store[x][y]);
375           printf("      Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
376           printf("      StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
377           printf("      MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
378           printf("      MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
379           printf("      MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
380           printf("\n");
381         }
382       }
383 #endif
384       break;
385
386     default:
387       break;
388   }
389 }
390
391 void HandleKey(KeySym key, int key_status)
392 {
393   int joy = 0;
394   static struct SetupKeyboardInfo custom_key;
395   static struct
396   {
397     KeySym *keysym_custom;
398     KeySym keysym_default;
399     byte action;
400   } key_info[] =
401   {
402     { &custom_key.left,  DEFAULT_KEY_LEFT,  JOY_LEFT     },
403     { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT    },
404     { &custom_key.up,    DEFAULT_KEY_UP,    JOY_UP       },
405     { &custom_key.down,  DEFAULT_KEY_DOWN,  JOY_DOWN     },
406     { &custom_key.snap,  DEFAULT_KEY_SNAP,  JOY_BUTTON_1 },
407     { &custom_key.bomb,  DEFAULT_KEY_BOMB,  JOY_BUTTON_2 }
408   };
409
410   if (game_status == PLAYING)
411   {
412     int pnr;
413
414     for (pnr=0; pnr<MAX_PLAYERS; pnr++)
415     {
416       int i;
417       byte key_action = 0;
418
419       if (setup.input[pnr].use_joystick)
420         continue;
421
422       custom_key = setup.input[pnr].key;
423
424       for (i=0; i<6; i++)
425         if (key == *key_info[i].keysym_custom)
426           key_action |= key_info[i].action;
427
428       if (key_status == KEY_PRESSED)
429         stored_player[pnr].action |= key_action;
430       else
431         stored_player[pnr].action &= ~key_action;
432     }
433   }
434   else
435   {
436     int i;
437
438     for (i=0; i<6; i++)
439       if (key == key_info[i].keysym_default)
440         joy |= key_info[i].action;
441   }
442
443   if (joy)
444   {
445     if (key_status == KEY_PRESSED)
446       key_joystick_mapping |= joy;
447     else
448       key_joystick_mapping &= ~joy;
449
450     HandleJoystick();
451   }
452
453   if (game_status != PLAYING)
454     key_joystick_mapping = 0;
455
456   if (key_status == KEY_RELEASED)
457     return;
458
459   if (key == XK_Return && game_status == PLAYING && AllPlayersGone)
460   {
461     CloseDoor(DOOR_CLOSE_1);
462     game_status = MAINMENU;
463     DrawMainMenu();
464     return;
465   }
466
467   /* allow quick escape to the main menu with the Escape key */
468   if (key == XK_Escape && game_status != MAINMENU)
469   {
470     if (game_status == LEVELED)
471     {
472       /* draw smaller door */
473       XCopyArea(display, pix[PIX_DOOR], drawto, gc,
474                 DOOR_GFX_PAGEX7, 64,
475                 108, 64,
476                 EX - 4, EY - 12);
477       redraw_mask |= REDRAW_ALL;
478     }
479
480     CloseDoor(DOOR_CLOSE_1 | DOOR_OPEN_2 | DOOR_NO_DELAY);
481     game_status = MAINMENU;
482     DrawMainMenu();
483     return;
484   }
485
486
487
488 #ifndef DEBUG
489
490   if (game_status == PLAYING && (tape.playing || tape.pausing))
491     return;
492
493 #endif
494
495
496
497   HandleGadgetsKeyInput(key);
498
499   switch(game_status)
500   {
501     case TYPENAME:
502       HandleTypeName(0, key);
503       break;
504
505     case MAINMENU:
506     case CHOOSELEVEL:
507     case SETUP:
508     case SETUPINPUT:
509       switch(key)
510       {
511         case XK_Return:
512           if (game_status == MAINMENU)
513             HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
514           else if (game_status == CHOOSELEVEL)
515             HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
516           else if (game_status == SETUP)
517             HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
518           else if (game_status == SETUPINPUT)
519             HandleSetupInputScreen(0,0, 0,0, MB_MENU_CHOICE);
520           break;
521
522         default:
523           break;
524       }
525       break;
526
527     case HELPSCREEN:
528       HandleHelpScreen(MB_RELEASED);
529       break;
530
531     case HALLOFFAME:
532       switch(key)
533       {
534         case XK_Return:
535           game_status = MAINMENU;
536           DrawMainMenu();
537           BackToFront();
538           break;
539
540         default:
541           break;
542       }
543       break;
544
545     case LEVELED:
546       HandleLevelEditorKeyInput(key);
547       LevelNameTyping(key);
548       break;
549
550     case PLAYING:
551     {
552       switch(key)
553       {
554
555 #ifdef DEBUG
556         case XK_0:
557         case XK_1:
558         case XK_2:
559         case XK_3:
560         case XK_4:
561         case XK_5:
562         case XK_6:
563         case XK_7:
564         case XK_8:
565         case XK_9:
566           if (key == XK_0)
567           {
568             if (GameFrameDelay == 500)
569               GameFrameDelay = GAME_FRAME_DELAY;
570             else
571               GameFrameDelay = 500;
572           }
573           else
574             GameFrameDelay = (key - XK_0) * 10;
575           printf("Game speed == %d%% (%d ms delay between two frames)\n",
576                  GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
577           break;
578
579
580 #if 0
581         case XK_a:
582           if (ScrollStepSize == TILEX/8)
583             ScrollStepSize = TILEX/4;
584           else
585             ScrollStepSize = TILEX/8;
586           printf("ScrollStepSize == %d\n", ScrollStepSize);
587           break;
588 #endif
589
590 #if 1
591         case XK_m:
592           if (MoveSpeed == 8)
593           {
594             MoveSpeed = 4;
595             ScrollStepSize = TILEX/4;
596           }
597           else
598           {
599             MoveSpeed = 8;
600             ScrollStepSize = TILEX/8;
601           }
602           printf("MoveSpeed == %d\n", MoveSpeed);
603           break;
604 #endif
605
606         case XK_f:
607           ScrollStepSize = TILEX/8;
608           printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
609           break;
610
611         case XK_g:
612           ScrollStepSize = TILEX/4;
613           printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
614           break;
615
616         case XK_h:
617           ScrollStepSize = TILEX/2;
618           printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
619           break;
620
621         case XK_l:
622           ScrollStepSize = TILEX;
623           printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
624           break;
625
626 #ifndef MSDOS
627         case XK_Q:
628 #endif
629         case XK_q:
630           local_player->dynamite = 1000;
631           break;
632
633
634
635 #if 0
636
637         case XK_z:
638           {
639             int i;
640
641             for(i=0; i<MAX_PLAYERS; i++)
642             {
643               printf("Player %d:\n", i);
644               printf("  jx == %d, jy == %d\n",
645                      stored_player[i].jx, stored_player[i].jy);
646               printf("  last_jx == %d, last_jy == %d\n",
647                      stored_player[i].last_jx, stored_player[i].last_jy);
648             }
649             printf("\n");
650           }
651
652           break;
653 #endif
654 #endif
655
656         default:
657           break;
658       }
659       break;
660     }
661     default:
662       break;
663   }
664 }
665
666 void HandleNoXEvent()
667 {
668   if (button_status && game_status != PLAYING)
669   {
670     HandleButton(0, 0, -button_status);
671     return;
672   }
673
674 #ifndef MSDOS
675   if (options.network)
676     HandleNetworking();
677 #endif
678
679   HandleJoystick();
680
681   if (game_status == PLAYING)
682     HandleGameActions();
683 }
684
685 static int HandleJoystickForAllPlayers()
686 {
687   int i;
688   int result = 0;
689
690   for (i=0; i<MAX_PLAYERS; i++)
691   {
692     byte joy_action = 0;
693
694     /*
695     if (!setup.input[i].use_joystick)
696       continue;
697       */
698
699     joy_action = Joystick(i);
700     result |= joy_action;
701
702
703     if (!setup.input[i].use_joystick)
704       continue;
705
706
707     stored_player[i].action = joy_action;
708   }
709
710   return result;
711 }
712
713 void HandleJoystick()
714 {
715   int joystick  = HandleJoystickForAllPlayers();
716   int keyboard  = key_joystick_mapping;
717   int joy       = (joystick | keyboard);
718   int left      = joy & JOY_LEFT;
719   int right     = joy & JOY_RIGHT;
720   int up        = joy & JOY_UP;
721   int down      = joy & JOY_DOWN;
722   int button    = joy & JOY_BUTTON;
723   int newbutton = (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED);
724   int dx        = (left ? -1    : right ? 1     : 0);
725   int dy        = (up   ? -1    : down  ? 1     : 0);
726
727   switch(game_status)
728   {
729     case MAINMENU:
730     case CHOOSELEVEL:
731     case SETUP:
732     case SETUPINPUT:
733     {
734       static unsigned long joystickmove_delay = 0;
735
736       if (joystick && !button &&
737           !DelayReached(&joystickmove_delay, GADGET_FRAME_DELAY))
738         newbutton = dx = dy = 0;
739
740       if (game_status==MAINMENU)
741         HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
742       else if (game_status==CHOOSELEVEL)
743         HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
744       else if (game_status==SETUP)
745         HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
746       else if (game_status==SETUPINPUT)
747         HandleSetupInputScreen(0,0,dx,dy,
748                                newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
749       break;
750     }
751
752     case HALLOFFAME:
753       HandleHallOfFame(!newbutton);
754       break;
755
756     case HELPSCREEN:
757       HandleHelpScreen(!newbutton);
758       break;
759
760     case PLAYING:
761       if (tape.playing || keyboard)
762         newbutton = ((joy & JOY_BUTTON) != 0);
763
764       if (AllPlayersGone && newbutton)
765       {
766         CloseDoor(DOOR_CLOSE_1);
767         game_status = MAINMENU;
768         DrawMainMenu();
769         return;
770       }
771
772       break;
773
774     default:
775       break;
776   }
777 }