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