added functions to get current player position for all game engines
[rocksndiamonds.git] / src / game_em / input.c
index 9bcc5c109060b299f6c61b15bc8a71bd3d9d7350..4028f19a553f692e9dc7a3541358f0ab24d27918 100644 (file)
  * handle input from x11 and keyboard and joystick
  */
 
-#include <X11/Xlib.h>
-#include <X11/Xutil.h>
-#include <X11/Xatom.h>
-#include <X11/keysym.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
+#include "main_em.h"
 
-#include "global.h"
-#include "display.h"
-#include "level.h"
 
+unsigned int RandomEM;
 
-#if defined(TARGET_X11)
+struct LEVEL lev;
+struct PLAYER ply[MAX_PLAYERS];
 
-unsigned long Random;
+short **Boom;
+short **Cave;
+short **Next;
+short **Draw;
 
-struct PLAYER ply1;
-struct PLAYER ply2;
-struct LEVEL lev;
+static short *Index[4][HEIGHT];
+static short Array[4][HEIGHT][WIDTH];
 
-unsigned short **Boom;
-unsigned short **Cave;
-unsigned short **Next;
-unsigned short **Draw;
+extern int screen_x;
+extern int screen_y;
 
-static unsigned short *Index[4][HEIGHT];
-static unsigned short Array[4][HEIGHT][WIDTH];
+struct EngineSnapshotInfo_EM engine_snapshot_em;
 
-static int input_die;
-static int input_pause;
-static int input_refresh;
-static int input_esc;
+void game_init_vars(void)
+{
+  int x, y;
+
+  RandomEM = 1684108901;
+
+  for (y = 0; y < HEIGHT; y++)
+    for (x = 0; x < WIDTH; x++)
+      Array[0][y][x] = ZBORDER;
+  for (y = 0; y < HEIGHT; y++)
+    for (x = 0; x < WIDTH; x++)
+      Array[1][y][x] = ZBORDER;
+  for (y = 0; y < HEIGHT; y++)
+    for (x = 0; x < WIDTH; x++)
+      Array[2][y][x] = ZBORDER;
+  for (y = 0; y < HEIGHT; y++)
+    for (x = 0; x < WIDTH; x++)
+      Array[3][y][x] = Xblank;
+
+  for (y = 0; y < HEIGHT; y++)
+    Index[0][y] = Array[0][y];
+  for (y = 0; y < HEIGHT; y++)
+    Index[1][y] = Array[1][y];
+  for (y = 0; y < HEIGHT; y++)
+    Index[2][y] = Array[2][y];
+  for (y = 0; y < HEIGHT; y++)
+    Index[3][y] = Array[3][y];
+
+  Cave = Index[0];
+  Next = Index[1];
+  Draw = Index[2];
+  Boom = Index[3];
+}
 
-static struct timeval tv1;
-static struct timeval tv2;
+void InitGameEngine_EM()
+{
+  prepare_em_level();
 
-static unsigned char keymatrix[32];
+  game_initscreen();
 
-static int player_mode;
-static char player_name[32];
-static int player_level;
+  RedrawPlayfield_EM(FALSE);
+}
 
-static void drawmenu(int pos)
+void UpdateGameDoorValues_EM()
 {
-       char buffer[256];
-       switch(pos) {
-       case 0:
-               switch(player_mode) {
-               case 0: strcpy(buffer, "quit emerald mine"); break;
-               case 1: strcpy(buffer, "single player"); break;
-               case 2: strcpy(buffer, "teamwork"); break;
-               }
-               title_string(4, 0, 40, buffer);
-               break;
-       case 1:
-               sprintf(buffer, "name: %s", player_name);
-               title_string(5, 0, 40, buffer);
-               break;
-       case 2:
-               sprintf(buffer, "level: %d", player_level);
-               title_string(6, 0, 40, buffer);
-               break;
-       case 3:
-               title_string(7, 0, 40, "highscores");
-               break;
-       case 4:
-               title_string(8, 0, 40, "start");
-               break;
-       case 5:
-               title_string(10, 1, 13, "played");
-               title_string(10, 14, 26, "score");
-               title_string(10, 27, 39, "won");
-               break;
-       case 6:
-               sprintf(buffer, "%d", 0);
-               title_string(11, 1, 13, buffer);
-               sprintf(buffer, "%d", 0);
-               title_string(11, 14, 26, buffer);
-               sprintf(buffer, "%d", 0);
-               title_string(11, 27, 39, buffer);
-               break;
-       }
 }
 
-/* bring it all together */
-int game_start(void)
+void GameActions_EM(byte action[MAX_PLAYERS], boolean warp_mode)
 {
-       int x,y;
-       char name[MAXNAME+2];
-       int temp;
-       int pos;
-
-       Random = 1684108901;
-
-       for(y = 0; y < HEIGHT; y++) for(x = 0; x < WIDTH; x++) Array[0][y][x] = ZBORDER;
-       for(y = 0; y < HEIGHT; y++) for(x = 0; x < WIDTH; x++) Array[1][y][x] = ZBORDER;
-       for(y = 0; y < HEIGHT; y++) for(x = 0; x < WIDTH; x++) Array[2][y][x] = ZBORDER;
-       for(y = 0; y < HEIGHT; y++) for(x = 0; x < WIDTH; x++) Array[3][y][x] = Xblank;
-
-       for(y = 0; y < HEIGHT; y++) Index[0][y] = Array[0][y];
-       for(y = 0; y < HEIGHT; y++) Index[1][y] = Array[1][y];
-       for(y = 0; y < HEIGHT; y++) Index[2][y] = Array[2][y];
-       for(y = 0; y < HEIGHT; y++) Index[3][y] = Array[3][y];
-
-       Cave = Index[0];
-       Next = Index[1];
-       Draw = Index[2];
-       Boom = Index[3];
-
-       player_mode = 1; /* start off as single player */
-       strcpy(player_name, "dave");
-       player_level = 0;
-
-       input_pause = 1    * 0;
-
-       for(;;) {
-
-               pos = 4;
-
-               title_initscreen();
-
-               for(temp = 0; temp < 7; temp++) drawmenu(temp); /* display all lines */
-               title_blitants(4 + pos);
-
-               title_blitscore();
-               title_animscreen();
-
-#if 0
-               printf("--> M1: xwindow == %ld\n", xwindow);
-#endif
-
-               temp = 1;
-               for(;;) {
-                       input_eventloop();
-                       if(input_die) return(0);
-                       if(input_refresh) blitscreen();
-
-#if 0
-                       printf("--> M2: xwindow == %ld\n", xwindow);
-#endif
-
-                       if(!input_pause) {
-                               title_blitants(4 + pos);
-                               title_blitscore();
-                               title_animscreen();
-
-#if 0
-                               printf("--> M3: xwindow == %ld\n", xwindow);
-#endif
-
-                               ply1.joy_n = ply1.joy_e = ply1.joy_s = ply1.joy_w = 0;
-
-                               readjoy();
-
-                               if(temp == 0) {
-                                       if(ply1.joy_fire) break;
-
-                                       if(ply1.joy_e && player_level < 99) {
-                                               player_level++;
-                                               drawmenu(2);
-                                       }
-                                       if(ply1.joy_w && player_level > 0) {
-                                               player_level--;
-                                               drawmenu(2);
-                                       }
-                                       if(ply1.joy_n && pos > 0) {
-                                               drawmenu(pos);
-                                               pos--;
-                                               title_blitants(4 + pos);
-                                       }
-                                       if(ply1.joy_s && pos < 4) {
-                                               drawmenu(pos);
-                                               pos++;
-                                               title_blitants(4 + pos);
-                                       }
-                               }
-                               temp = (ply1.joy_n || ply1.joy_e || ply1.joy_s || ply1.joy_w || ply1.joy_fire);
-                       }
-               }
-
-#if 0
-               printf("--> M4: xwindow == %ld\n", xwindow);
-#endif
-
-               name[MAXNAME] = 0;
-               snprintf(name, MAXNAME+2, "%s/lev%02d", EM_LVL_DIR, player_level);
-               if(name[MAXNAME]) snprintf_overflow("read a level in cave/");
-
-               if(cave_convert(name)) continue;
-
-#if 0
-               printf("--> M5: xwindow == %ld\n", xwindow);
-#endif
-
-               game_initscreen();
-#if 0
-               printf("--> M6: xwindow == %ld\n", xwindow);
-#endif
-               game_blitscore();
-#if 0
-               printf("--> M7: xwindow == %ld\n", xwindow);
-#endif
-               game_animscreen();
-
-#if 0
-               printf("--> M8: xwindow == %ld\n", xwindow);
-#endif
-
-               for(;;) {
-                       input_eventloop();
-                       if(input_die || input_esc) break;
-                       if(input_refresh) blitscreen();
-
-                       if(!input_pause) {
-                               game_animscreen();
-
-                               frame = (frame - 1) & 7;
-
-                               readjoy();
-
-                               if(frame == 7) {
-                                       synchro_1();
-                                       synchro_2();
-                               }
-                               if(frame == 6) {
-                                       synchro_3();
-                                       sound_play();
-                                       game_blitscore();
-                               }
-                       }
-               }
-       }
+  int i;
+  boolean any_player_dropping = FALSE;
+
+  RandomEM = RandomEM * 129 + 1;
+
+  frame = (frame - 1) & 7;
+
+  for (i = 0; i < MAX_PLAYERS; i++)
+    readjoy(action[i], &ply[i]);
+
+  UpdateEngineValues(screen_x / TILEX, screen_y / TILEY, ply[0].x, ply[0].y);
+
+  if (frame == 7)
+  {
+    synchro_1();
+    synchro_2();
+  }
+
+  if (frame == 6)
+  {
+    synchro_3();
+    sound_play();
+
+    UpdateGameDoorValues_EM();
+  }
+
+  for (i = 0; i < MAX_PLAYERS; i++)
+    if (ply[i].joy_drop &&
+       ply[i].dynamite &&
+       ply[i].dynamite_cnt > 0 &&
+       ply[i].dynamite_cnt < 5)
+      any_player_dropping = TRUE;
+
+  CheckSingleStepMode_EM(action, frame, game_em.any_player_moving,
+                        game_em.any_player_snapping, any_player_dropping);
+
+  RedrawPlayfield_EM(FALSE);
 }
 
-/* read input device for players
- */
-void readjoy(void)
+/* read input device for players */
+
+void readjoy(byte action, struct PLAYER *ply)
 {
-       unsigned int i;
-       unsigned int north = 0, east = 0, south = 0, west = 0, fire = 0;
-
-       for(i = 0; i < 3; i++) if(keymatrix[northKeyCode[i] >> 3] & 1 << (northKeyCode[i] & 7)) north = 1;
-       for(i = 0; i < 3; i++) if(keymatrix[eastKeyCode[i] >> 3] & 1 << (eastKeyCode[i] & 7)) east = 1;
-       for(i = 0; i < 3; i++) if(keymatrix[southKeyCode[i] >> 3] & 1 << (southKeyCode[i] & 7)) south = 1;
-       for(i = 0; i < 3; i++) if(keymatrix[westKeyCode[i] >> 3] & 1 << (westKeyCode[i] & 7)) west = 1;
-       for(i = 0; i < 3; i++) if(keymatrix[fireKeyCode[i] >> 3] & 1 << (fireKeyCode[i] & 7)) fire = 1;
-
-       ply1.joy_fire = fire;
-       if(ply1.joy_stick || (north | east | south | west)) {
-               ply1.joy_n = north;
-               ply1.joy_e = east;
-               ply1.joy_s = south;
-               ply1.joy_w = west;
-       }
-
-#if 0
-       printf("::: JOY == %d/%d/%d/%d/%d\n",
-              north, east, south, west, fire);
-#endif
+  int north = 0, east = 0, south = 0, west = 0;
+  int snap = 0, drop = 0;
+
+  if (action & JOY_LEFT)
+    west = 1;
+
+  if (action & JOY_RIGHT)
+    east = 1;
+
+  if (action & JOY_UP)
+    north = 1;
+
+  if (action & JOY_DOWN)
+    south = 1;
+
+  if (action & JOY_BUTTON_1)
+    snap = 1;
+
+  if (action & JOY_BUTTON_2)
+    drop = 1;
+
+  ply->joy_snap = snap;
+  ply->joy_drop = drop;
+
+  if (ply->joy_stick || (north | east | south | west))
+  {
+    ply->joy_n = north;
+    ply->joy_e = east;
+    ply->joy_s = south;
+    ply->joy_w = west;
+  }
 }
 
-/* handle events from x windows and block until the next frame
- */
-void input_eventloop(void)
+void SaveEngineSnapshotValues_EM()
 {
-       XEvent event;
-       unsigned int i;
-       unsigned long count;
-
-       if(input_pause) {
-               XPeekEvent(display, &event); /* block until an event arrives */
-
-               if(gettimeofday(&tv1, 0) == -1) tv1.tv_usec = 0;
-       } else {
-               XSync(display, False); /* block until all graphics are drawn */
-
-               if(gettimeofday(&tv2, 0) == -1) tv2.tv_usec = 0;
-               count = tv2.tv_usec + 1000000 - tv1.tv_usec; if(count >= 1000000) count -= 1000000;
-               tv1.tv_usec = tv2.tv_usec;
-               if(count < 25000) {
-                       tv2.tv_sec = 0;
-                       tv2.tv_usec = 25000 - count;
-#if 1
-                       select(0, 0, 0, 0, &tv2); /* sleep a bit */
-#else
-                       usleep(tv2.tv_usec);
-#endif
-               }
-       }
-
-       input_die = 0;
-       input_refresh = 0;
-       lastKeySym = NoSymbol;
-       while(XPending(display)) { /* drain the event queue */
-               XNextEvent(display, &event);
-               switch(event.xany.type) {
-               case KeyPress:
-                       XLookupString(&event.xkey, (char *)&count, 1, &lastKeySym, 0);
-                       break;
-               case Expose:
-                       if(event.xexpose.window == xwindow && event.xexpose.count == 0) input_refresh = 1;
-                       break;
-               case ClientMessage:
-                       if(event.xclient.window == xwindow && (Atom)event.xclient.data.l[0] == deleteAtom) input_die = 1;
-                       break;
-               case EnterNotify:
-                       if(event.xcrossing.window == xwindow) input_pause = 0;
-                       break;
-               case LeaveNotify:
-                       if(event.xcrossing.window == xwindow) input_pause = 1;
-                       break;
-               }
-       }
-       XQueryKeymap(display, keymatrix); /* read the keyboard */
-
-       input_esc = 0;
-       for(i = 0; i < 1; i++) if(keymatrix[escKeyCode[i] >> 3] & 1 << (escKeyCode[i] & 7)) input_esc = 1;
+  int i, j, k;
+
+  engine_snapshot_em.game_em = game_em;
+  engine_snapshot_em.lev = lev;
+
+  engine_snapshot_em.RandomEM = RandomEM;
+  engine_snapshot_em.frame = frame;
+
+  engine_snapshot_em.screen_x = screen_x;
+  engine_snapshot_em.screen_y = screen_y;
+
+  engine_snapshot_em.Boom = Boom;
+  engine_snapshot_em.Cave = Cave;
+  engine_snapshot_em.Next = Next;
+  engine_snapshot_em.Draw = Draw;
+
+  for (i = 0; i < 4; i++)
+    engine_snapshot_em.ply[i] = ply[i];
+
+  for (i = 0; i < 4; i++)
+    for (j = 0; j < HEIGHT; j++)
+      for (k = 0; k < WIDTH; k++)
+       engine_snapshot_em.Array[i][j][k] = Array[i][j][k];
 }
 
-#endif
+void LoadEngineSnapshotValues_EM()
+{
+  int i, j, k;
+
+  game_em = engine_snapshot_em.game_em;
+  lev = engine_snapshot_em.lev;
+
+  RandomEM = engine_snapshot_em.RandomEM;
+  frame = engine_snapshot_em.frame;
+
+  screen_x = engine_snapshot_em.screen_x;
+  screen_y = engine_snapshot_em.screen_y;
+
+  Boom = engine_snapshot_em.Boom;
+  Cave = engine_snapshot_em.Cave;
+  Next = engine_snapshot_em.Next;
+  Draw = engine_snapshot_em.Draw;
+
+  for (i = 0; i < 4; i++)
+    ply[i] = engine_snapshot_em.ply[i];
+
+  for (i = 0; i < 4; i++)
+    for (j = 0; j < HEIGHT; j++)
+      for (k = 0; k < WIDTH; k++)
+       Array[i][j][k] = engine_snapshot_em.Array[i][j][k];
+}