rnd-19990109-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   motion_status = TRUE;
230
231   HandleButton(event->x, event->y, button_status);
232 }
233
234 void HandleKeyEvent(XKeyEvent *event)
235 {
236   int key_status = (event->type == KeyPress ? KEY_PRESSED : KEY_RELEASED);
237   unsigned int event_state = (game_status != PLAYING ? event->state : 0);
238   KeySym key = XLookupKeysym(event, event_state);
239
240   HandleKey(key, key_status);
241 }
242
243 void HandleFocusEvent(XFocusChangeEvent *event)
244 {
245   static int old_joystick_status = -1;
246
247   if (event->type == FocusOut)
248   {
249     XAutoRepeatOn(display);
250     old_joystick_status = joystick_status;
251     joystick_status = JOYSTICK_OFF;
252     key_joystick_mapping = 0;
253   }
254   else if (event->type == FocusIn)
255   {
256     if (game_status == PLAYING)
257       XAutoRepeatOff(display);
258     if (old_joystick_status != -1)
259       joystick_status = old_joystick_status;
260   }
261 }
262
263 void HandleClientMessageEvent(XClientMessageEvent *event)
264 {
265 #ifndef MSDOS
266   if ((event->window == window) &&
267       (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
268     CloseAllAndExit(0);
269 #endif
270 }
271
272 void HandleButton(int mx, int my, int button)
273 {
274   static int old_mx = 0, old_my = 0;
275
276   if (button < 0)
277   {
278     mx = old_mx;
279     my = old_my;
280     button = -button;
281   }
282   else
283   {
284     old_mx = mx;
285     old_my = my;
286
287     HandleVideoButtons(mx,my, button);
288     HandleSoundButtons(mx,my, button);
289     HandleGameButtons(mx,my, button);
290   }
291
292   HandleGadgets(mx, my, button);
293
294   switch(game_status)
295   {
296     case MAINMENU:
297       HandleMainMenu(mx,my, 0,0, button);
298       break;
299
300     case TYPENAME:
301       HandleTypeName(0, XK_Return);
302       break;
303
304     case CHOOSELEVEL:
305       HandleChooseLevel(mx,my, 0,0, button);
306       break;
307
308     case HALLOFFAME:
309       HandleHallOfFame(button);
310       break;
311
312     case LEVELED:
313       LevelEd(mx,my, button);
314       break;
315
316     case HELPSCREEN:
317       HandleHelpScreen(button);
318       break;
319
320     case SETUP:
321       HandleSetupScreen(mx,my, 0,0, button);
322       break;
323
324     case SETUPINPUT:
325       HandleSetupInputScreen(mx,my, 0,0, button);
326       break;
327
328     case PLAYING:
329 #ifdef DEBUG
330       if (button == MB_RELEASED)
331       {
332         int sx = (mx - SX) / TILEX;
333         int sy = (my - SY) / TILEY;
334
335         if (IN_VIS_FIELD(sx,sy))
336         {
337           int x = LEVELX(sx);
338           int y = LEVELY(sy);
339
340           printf("INFO: SCREEN(%d, %d), LEVEL(%d, %d)\n", sx, sy, x, y);
341
342           if (!IN_LEV_FIELD(x, y))
343             break;
344
345           printf("      Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
346           printf("      Store[%d][%d] == %d\n", x,y, Store[x][y]);
347           printf("      Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
348           printf("      StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
349           printf("      MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
350           printf("      MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
351           printf("      MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
352           printf("\n");
353         }
354       }
355 #endif
356       break;
357
358     default:
359       break;
360   }
361 }
362
363 void HandleKey(KeySym key, int key_status)
364 {
365   int joy = 0;
366   static struct SetupKeyboardInfo custom_key;
367   static struct
368   {
369     KeySym *keysym_custom;
370     KeySym keysym_default;
371     byte action;
372   } key_info[] =
373   {
374     { &custom_key.left,  DEFAULT_KEY_LEFT,  JOY_LEFT     },
375     { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT    },
376     { &custom_key.up,    DEFAULT_KEY_UP,    JOY_UP       },
377     { &custom_key.down,  DEFAULT_KEY_DOWN,  JOY_DOWN     },
378     { &custom_key.snap,  DEFAULT_KEY_SNAP,  JOY_BUTTON_1 },
379     { &custom_key.bomb,  DEFAULT_KEY_BOMB,  JOY_BUTTON_2 }
380   };
381
382   if (game_status == PLAYING)
383   {
384     int pnr;
385
386     for (pnr=0; pnr<MAX_PLAYERS; pnr++)
387     {
388       int i;
389       byte key_action = 0;
390
391       if (setup.input[pnr].use_joystick)
392         continue;
393
394       custom_key = setup.input[pnr].key;
395
396       for (i=0; i<6; i++)
397         if (key == *key_info[i].keysym_custom)
398           key_action |= key_info[i].action;
399
400       if (key_status == KEY_PRESSED)
401         stored_player[pnr].action |= key_action;
402       else
403         stored_player[pnr].action &= ~key_action;
404     }
405   }
406   else
407   {
408     int i;
409
410     for (i=0; i<6; i++)
411       if (key == key_info[i].keysym_default)
412         joy |= key_info[i].action;
413   }
414
415   if (joy)
416   {
417     if (key_status == KEY_PRESSED)
418       key_joystick_mapping |= joy;
419     else
420       key_joystick_mapping &= ~joy;
421
422     HandleJoystick();
423   }
424
425   if (game_status != PLAYING)
426     key_joystick_mapping = 0;
427
428   if (key_status == KEY_RELEASED)
429     return;
430
431   if (key == XK_Return && game_status == PLAYING && AllPlayersGone)
432   {
433     CloseDoor(DOOR_CLOSE_1);
434     game_status = MAINMENU;
435     DrawMainMenu();
436     return;
437   }
438
439   /* allow quick escape to the main menu with the Escape key */
440   if (key == XK_Escape && game_status != MAINMENU)
441   {
442     if (game_status == LEVELED)
443     {
444       /* draw smaller door */
445       XCopyArea(display, pix[PIX_DOOR], drawto, gc,
446                 DOOR_GFX_PAGEX7, 64,
447                 108, 64,
448                 EX - 4, EY - 12);
449       redraw_mask |= REDRAW_ALL;
450     }
451
452     CloseDoor(DOOR_CLOSE_1 | DOOR_OPEN_2 | DOOR_NO_DELAY);
453     game_status = MAINMENU;
454     DrawMainMenu();
455     return;
456   }
457
458
459
460 #ifndef DEBUG
461
462   if (game_status == PLAYING && (tape.playing || tape.pausing))
463     return;
464
465 #endif
466
467
468
469   HandleGadgetsKeyInput(key);
470
471   switch(game_status)
472   {
473     case TYPENAME:
474       HandleTypeName(0, key);
475       break;
476
477     case MAINMENU:
478     case CHOOSELEVEL:
479     case SETUP:
480     case SETUPINPUT:
481       switch(key)
482       {
483         case XK_Return:
484           if (game_status == MAINMENU)
485             HandleMainMenu(0,0, 0,0, MB_MENU_CHOICE);
486           else if (game_status == CHOOSELEVEL)
487             HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
488           else if (game_status == SETUP)
489             HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
490           else if (game_status == SETUPINPUT)
491             HandleSetupInputScreen(0,0, 0,0, MB_MENU_CHOICE);
492           break;
493
494         default:
495           break;
496       }
497       break;
498
499     case HELPSCREEN:
500       HandleHelpScreen(MB_RELEASED);
501       break;
502
503     case HALLOFFAME:
504       switch(key)
505       {
506         case XK_Return:
507           game_status = MAINMENU;
508           DrawMainMenu();
509           BackToFront();
510           break;
511
512         default:
513           break;
514       }
515       break;
516
517     case LEVELED:
518       HandleLevelEditorKeyInput(key);
519       LevelNameTyping(key);
520       break;
521
522     case PLAYING:
523     {
524       switch(key)
525       {
526
527 #ifdef DEBUG
528         case XK_0:
529         case XK_1:
530         case XK_2:
531         case XK_3:
532         case XK_4:
533         case XK_5:
534         case XK_6:
535         case XK_7:
536         case XK_8:
537         case XK_9:
538           if (key == XK_0)
539           {
540             if (GameFrameDelay == 500)
541               GameFrameDelay = GAME_FRAME_DELAY;
542             else
543               GameFrameDelay = 500;
544           }
545           else
546             GameFrameDelay = (key - XK_0) * 10;
547           printf("Game speed == %d%% (%d ms delay between two frames)\n",
548                  GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
549           break;
550
551
552 #if 0
553         case XK_a:
554           if (ScrollStepSize == TILEX/8)
555             ScrollStepSize = TILEX/4;
556           else
557             ScrollStepSize = TILEX/8;
558           printf("ScrollStepSize == %d\n", ScrollStepSize);
559           break;
560 #endif
561
562 #if 1
563         case XK_m:
564           if (MoveSpeed == 8)
565           {
566             MoveSpeed = 4;
567             ScrollStepSize = TILEX/4;
568           }
569           else
570           {
571             MoveSpeed = 8;
572             ScrollStepSize = TILEX/8;
573           }
574           printf("MoveSpeed == %d\n", MoveSpeed);
575           break;
576 #endif
577
578         case XK_f:
579           ScrollStepSize = TILEX/8;
580           printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
581           break;
582
583         case XK_g:
584           ScrollStepSize = TILEX/4;
585           printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
586           break;
587
588         case XK_h:
589           ScrollStepSize = TILEX/2;
590           printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
591           break;
592
593         case XK_l:
594           ScrollStepSize = TILEX;
595           printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
596           break;
597
598 #ifndef MSDOS
599         case XK_Q:
600 #endif
601         case XK_q:
602           local_player->dynamite = 1000;
603           break;
604
605
606
607 #if 0
608
609         case XK_z:
610           {
611             int i;
612
613             for(i=0; i<MAX_PLAYERS; i++)
614             {
615               printf("Player %d:\n", i);
616               printf("  jx == %d, jy == %d\n",
617                      stored_player[i].jx, stored_player[i].jy);
618               printf("  last_jx == %d, last_jy == %d\n",
619                      stored_player[i].last_jx, stored_player[i].last_jy);
620             }
621             printf("\n");
622           }
623
624           break;
625 #endif
626 #endif
627
628         default:
629           break;
630       }
631       break;
632     }
633     default:
634       break;
635   }
636 }
637
638 void HandleNoXEvent()
639 {
640   if (button_status && game_status != PLAYING)
641   {
642     HandleButton(0, 0, -button_status);
643     return;
644   }
645
646 #ifndef MSDOS
647   if (options.network)
648     HandleNetworking();
649 #endif
650
651   HandleJoystick();
652
653   if (game_status == PLAYING)
654     HandleGameActions();
655 }
656
657 static int HandleJoystickForAllPlayers()
658 {
659   int i;
660   int result = 0;
661
662   for (i=0; i<MAX_PLAYERS; i++)
663   {
664     byte joy_action = 0;
665
666     /*
667     if (!setup.input[i].use_joystick)
668       continue;
669       */
670
671     joy_action = Joystick(i);
672     result |= joy_action;
673
674
675     if (!setup.input[i].use_joystick)
676       continue;
677
678
679     stored_player[i].action = joy_action;
680   }
681
682   return result;
683 }
684
685 void HandleJoystick()
686 {
687   int joystick  = HandleJoystickForAllPlayers();
688   int keyboard  = key_joystick_mapping;
689   int joy       = (joystick | keyboard);
690   int left      = joy & JOY_LEFT;
691   int right     = joy & JOY_RIGHT;
692   int up        = joy & JOY_UP;
693   int down      = joy & JOY_DOWN;
694   int button    = joy & JOY_BUTTON;
695   int newbutton = (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED);
696   int dx        = (left ? -1    : right ? 1     : 0);
697   int dy        = (up   ? -1    : down  ? 1     : 0);
698
699   switch(game_status)
700   {
701     case MAINMENU:
702     case CHOOSELEVEL:
703     case SETUP:
704     case SETUPINPUT:
705     {
706       static unsigned long joystickmove_delay = 0;
707
708       if (joystick && !button &&
709           !DelayReached(&joystickmove_delay, GADGET_FRAME_DELAY))
710         newbutton = dx = dy = 0;
711
712       if (game_status==MAINMENU)
713         HandleMainMenu(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
714       else if (game_status==CHOOSELEVEL)
715         HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
716       else if (game_status==SETUP)
717         HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
718       else if (game_status==SETUPINPUT)
719         HandleSetupInputScreen(0,0,dx,dy,
720                                newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
721       break;
722     }
723
724     case HALLOFFAME:
725       HandleHallOfFame(!newbutton);
726       break;
727
728     case HELPSCREEN:
729       HandleHelpScreen(!newbutton);
730       break;
731
732     case PLAYING:
733       if (tape.playing || keyboard)
734         newbutton = ((joy & JOY_BUTTON) != 0);
735
736       if (AllPlayersGone && newbutton)
737       {
738         CloseDoor(DOOR_CLOSE_1);
739         game_status = MAINMENU;
740         DrawMainMenu();
741         return;
742       }
743
744       break;
745
746     default:
747       break;
748   }
749 }