rnd-19990126-2
[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     /*
316     HandleVideoButtons(mx,my, button);
317     HandleSoundButtons(mx,my, button);
318     HandleGameButtons(mx,my, button);
319     */
320   }
321
322   HandleGadgets(mx, my, button);
323
324   switch(game_status)
325   {
326     case MAINMENU:
327       HandleMainMenu(mx,my, 0,0, button);
328       break;
329
330     case TYPENAME:
331       HandleTypeName(0, XK_Return);
332       break;
333
334     case CHOOSELEVEL:
335       HandleChooseLevel(mx,my, 0,0, button);
336       break;
337
338     case HALLOFFAME:
339       HandleHallOfFame(button);
340       break;
341
342     case LEVELED:
343       break;
344
345     case HELPSCREEN:
346       HandleHelpScreen(button);
347       break;
348
349     case SETUP:
350       HandleSetupScreen(mx,my, 0,0, button);
351       break;
352
353     case SETUPINPUT:
354       HandleSetupInputScreen(mx,my, 0,0, button);
355       break;
356
357     case PLAYING:
358 #ifdef DEBUG
359       if (button == MB_RELEASED)
360       {
361         int sx = (mx - SX) / TILEX;
362         int sy = (my - SY) / TILEY;
363
364         if (IN_VIS_FIELD(sx,sy))
365         {
366           int x = LEVELX(sx);
367           int y = LEVELY(sy);
368
369           printf("INFO: SCREEN(%d, %d), LEVEL(%d, %d)\n", sx, sy, x, y);
370
371           if (!IN_LEV_FIELD(x, y))
372             break;
373
374           printf("      Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
375           printf("      Store[%d][%d] == %d\n", x,y, Store[x][y]);
376           printf("      Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
377           printf("      StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
378           printf("      MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
379           printf("      MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
380           printf("      MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
381           printf("\n");
382         }
383       }
384 #endif
385       break;
386
387     default:
388       break;
389   }
390 }
391
392 void HandleKey(KeySym key, int key_status)
393 {
394   int joy = 0;
395   static struct SetupKeyboardInfo custom_key;
396   static struct
397   {
398     KeySym *keysym_custom;
399     KeySym keysym_default;
400     byte action;
401   } key_info[] =
402   {
403     { &custom_key.left,  DEFAULT_KEY_LEFT,  JOY_LEFT     },
404     { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT    },
405     { &custom_key.up,    DEFAULT_KEY_UP,    JOY_UP       },
406     { &custom_key.down,  DEFAULT_KEY_DOWN,  JOY_DOWN     },
407     { &custom_key.snap,  DEFAULT_KEY_SNAP,  JOY_BUTTON_1 },
408     { &custom_key.bomb,  DEFAULT_KEY_BOMB,  JOY_BUTTON_2 }
409   };
410
411   if (game_status == PLAYING)
412   {
413     int pnr;
414
415     for (pnr=0; pnr<MAX_PLAYERS; pnr++)
416     {
417       int i;
418       byte key_action = 0;
419
420       if (setup.input[pnr].use_joystick)
421         continue;
422
423       custom_key = setup.input[pnr].key;
424
425       for (i=0; i<6; i++)
426         if (key == *key_info[i].keysym_custom)
427           key_action |= key_info[i].action;
428
429       if (key_status == KEY_PRESSED)
430         stored_player[pnr].action |= key_action;
431       else
432         stored_player[pnr].action &= ~key_action;
433     }
434   }
435   else
436   {
437     int i;
438
439     for (i=0; i<6; i++)
440       if (key == key_info[i].keysym_default)
441         joy |= key_info[i].action;
442   }
443
444   if (joy)
445   {
446     if (key_status == KEY_PRESSED)
447       key_joystick_mapping |= joy;
448     else
449       key_joystick_mapping &= ~joy;
450
451     HandleJoystick();
452   }
453
454   if (game_status != PLAYING)
455     key_joystick_mapping = 0;
456
457   if (key_status == KEY_RELEASED)
458     return;
459
460   if (key == XK_Return && game_status == PLAYING && AllPlayersGone)
461   {
462     CloseDoor(DOOR_CLOSE_1);
463     game_status = MAINMENU;
464     DrawMainMenu();
465     return;
466   }
467
468   /* allow quick escape to the main menu with the Escape key */
469   if (key == XK_Escape && game_status != MAINMENU)
470   {
471     if (game_status == LEVELED)
472     {
473       /* draw smaller door */
474       XCopyArea(display, pix[PIX_DOOR], drawto, gc,
475                 DOOR_GFX_PAGEX7, 64,
476                 108, 64,
477                 EX - 4, EY - 12);
478       redraw_mask |= REDRAW_ALL;
479     }
480
481     CloseDoor(DOOR_CLOSE_1 | DOOR_OPEN_2 | DOOR_NO_DELAY);
482     game_status = MAINMENU;
483     DrawMainMenu();
484     return;
485   }
486
487
488
489 #ifndef DEBUG
490
491   if (game_status == PLAYING && (tape.playing || tape.pausing))
492     return;
493
494 #endif
495
496
497
498   HandleGadgetsKeyInput(key);
499
500   switch(game_status)
501   {
502     case TYPENAME:
503       HandleTypeName(0, key);
504       break;
505
506     case MAINMENU:
507     case CHOOSELEVEL:
508     case SETUP:
509     case SETUPINPUT:
510       switch(key)
511       {
512         case XK_Return:
513           if (game_status == MAINMENU)
514             HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
515           else if (game_status == CHOOSELEVEL)
516             HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
517           else if (game_status == SETUP)
518             HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
519           else if (game_status == SETUPINPUT)
520             HandleSetupInputScreen(0,0, 0,0, MB_MENU_CHOICE);
521           break;
522
523         default:
524           break;
525       }
526       break;
527
528     case HELPSCREEN:
529       HandleHelpScreen(MB_RELEASED);
530       break;
531
532     case HALLOFFAME:
533       switch(key)
534       {
535         case XK_Return:
536           game_status = MAINMENU;
537           DrawMainMenu();
538           BackToFront();
539           break;
540
541         default:
542           break;
543       }
544       break;
545
546     case LEVELED:
547       HandleLevelEditorKeyInput(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 }