home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume5 / mazewar-V / daemon.c next >
C/C++ Source or Header  |  1990-12-13  |  8KB  |  338 lines

  1. /*
  2.  * This file is part of "mazewar", an interactive
  3.  * multiuser action game for non-BSD-Unix-systems.
  4.  * Copyright (C) 1988 Hans Korneder      korn@altger.uucp
  5.  * Permission is granted to the public
  6.  * to copy all parts of the source, as long as no money
  7.  * is made out of it, and the copyrightmessage is not removed.
  8.  */
  9.  
  10. #include <signal.h>
  11. #include <pwd.h>
  12. #include "globals.h"
  13. #define N_PLAYERS 10
  14.  
  15. struct player
  16.     {
  17.     int  p_pid;
  18.     int  p_fd;
  19.     int  p_uid;
  20.     int  p_x_pos;
  21.     int  p_y_pos;
  22.     char p_name[9];
  23.     char p_richtg;
  24.     long p_score;
  25.     char p_pipe[32];
  26.     int  last_x[N_PLAYERS];
  27.     int  last_y[N_PLAYERS];
  28.     int  last_r[N_PLAYERS];
  29.     long last_s[N_PLAYERS];
  30.     } players[N_PLAYERS];
  31.  
  32. int delta_x[4] = { 1,0,-1,0 };
  33. int delta_y[4] = { 0,-1,0,1 };
  34.  
  35. int player;
  36. int user_pipe_fd, score_fd;
  37.  
  38. catch(s)
  39. int s;
  40.     {
  41.     signal(s,catch);
  42.     }
  43.  
  44. get_user_info(uid)
  45. int uid;
  46.     {
  47.     extern struct passwd *getpwuid();
  48.     struct passwd *pwd;
  49.     pwd = getpwuid(uid);
  50.     if ( pwd ) strcpy(players[player].p_name,pwd->pw_name);
  51.     else sprintf(players[player].p_name,"*%06d*",uid);
  52.     if ( score_fd >= 0 )
  53.         {
  54.         lseek(score_fd,((long)uid)*sizeof(long),0);
  55.         read(score_fd,&(players[player].p_score),sizeof(long));
  56.         }
  57.     else players[player].p_score = 0L;
  58.     }
  59.  
  60. main()
  61.     {
  62.     extern long time();
  63.     setpgrp();
  64.     catch(SIGALRM);
  65.     catch(SIGPIPE);
  66.     nice(-10); nice(-10); nice(-10); nice(-10);
  67.     srand((unsigned)time((long *)0));
  68.     umask(0);
  69.     score_fd = open(MAZE_SCORES,2);
  70.     for(;;)
  71.         {
  72.         unlink(MAZE_DAEMON);
  73.         mknod(MAZE_DAEMON,010622,0);
  74.         user_pipe_fd = open(MAZE_DAEMON,0);
  75.         if ( user_pipe_fd < 0 ) exit(1);
  76.         for(player=0; player<N_PLAYERS; player++)
  77.             players[player].p_pid = 0;
  78.         while(process_mesg());
  79.         close(user_pipe_fd);
  80.         sleep(2);
  81.         }
  82.     }
  83.  
  84. reposition_player(p)
  85. int p;
  86.     {
  87.     extern int rand();
  88.     int y,x;
  89.     do    {
  90.         x = rand() % MAZE_COLS;
  91.         y = rand() % MAZE_ROWS;
  92.         } while ( maze[y][x] != ' ' );
  93.     players[p].p_x_pos = x;
  94.     players[p].p_y_pos = y;
  95.     players[p].p_richtg = rand()%4;
  96.     }
  97.  
  98. int process_mesg()
  99.     {
  100.     struct bewegung bew;
  101.     if ( read(user_pipe_fd,&bew,sizeof(bew))<=0 ) return 0;
  102.     switch ( bew.be_magic )
  103.         {
  104.         case ANMELD:
  105.             {
  106.             int p;
  107.             char answer_pipe[32];
  108.             read(user_pipe_fd,answer_pipe,sizeof(answer_pipe));
  109.             for(player=0; player<N_PLAYERS; player++)
  110.                 if ( ! players[player].p_pid ) break;
  111.             if ( player==N_PLAYERS ) break;
  112.             strcpy(players[player].p_pipe,answer_pipe);
  113.             players[player].p_fd = open(players[player].p_pipe,1);
  114.             if ( players[player].p_fd<0 ) break;
  115.             players[player].p_pid = bew.be_pid;
  116.             players[player].p_uid = bew.be_code;
  117.             get_user_info(bew.be_code);
  118.             reposition_player(player);
  119.             for(p=0; p<N_PLAYERS; p++)
  120.                 players[player].last_x[p] = -1,
  121.                 players[player].last_s[p] = -1000000000; 
  122.             determine_visibility();
  123.             break;
  124.             }
  125.         case BEWEGUNG:
  126.             for(player=0; player<N_PLAYERS; player++)
  127.                 if ( players[player].p_pid==bew.be_pid ) break;
  128.             if ( player==N_PLAYERS ) break;
  129.             do_cmd(bew.be_code);
  130.             determine_visibility();
  131.             break;
  132.         }
  133.     return 1;
  134.     }
  135.  
  136. send_player_info(p1,p2,x,y,sicht) /* send info 'bout p1 to p2 */
  137. int p1,p2,x,y,sicht;
  138.     {
  139.     struct sp_anzeige spa;
  140.     spa.sp_magic  = SP_ANZ;
  141.     spa.sp_pid    = players[p1].p_pid;
  142.     spa.sp_lfd_nr = p1;
  143.     spa.sp_flag   = sicht|1;
  144.     spa.sp_score  = players[p1].p_score;
  145.     spa.sp_x_pos  = x;
  146.     spa.sp_y_pos  = y;
  147.     spa.sp_richtg = players[p1].p_richtg;
  148.     strcpy(spa.sp_name,players[p1].p_name);
  149.     tell(p2,spa);
  150.     }
  151.  
  152. do_cmd(cmd)
  153. int cmd;
  154.     {
  155.     int p;
  156.     switch ( cmd )
  157.         {
  158.         case EXIT: /* leave game */
  159.             log_out(player);
  160.             break;
  161.         case 'S': /* shoot */
  162.             {
  163.             int new_x, new_y, p;
  164.             players[player].p_score -= 1; /* one shot costs 1 pt */
  165.             new_x=players[player].p_x_pos;
  166.             new_y=players[player].p_y_pos;
  167.             for(;;)
  168.                 {
  169.                 new_x += delta_x[players[player].p_richtg];
  170.                 new_y += delta_y[players[player].p_richtg];
  171.                 if ( maze[new_y][new_x] != ' ' ) break;
  172.                 for(p=0; p<N_PLAYERS; p++)
  173.                     if( players[p].p_pid &&
  174.                         player!=p &&
  175.                         players[p].p_x_pos==new_x &&
  176.                         players[p].p_y_pos==new_y )
  177.                             {
  178.                             players[p].p_score -= 10;
  179.                             reposition_player(p);
  180.                             players[player].p_score += 10;
  181.                             }
  182.                 }
  183.             break;
  184.             }
  185.         case 'A': /* turn right */
  186.             players[player].p_richtg = (players[player].p_richtg+1)%4;
  187.             break;
  188.         case 'D': /* turn left */
  189.             players[player].p_richtg = (players[player].p_richtg+3)%4;
  190.             break;
  191.         case 'X': /* turn back */
  192.             players[player].p_richtg = (players[player].p_richtg+2)%4;
  193.             break;
  194.         case 'R': /* reposition */
  195.             players[player].p_score -= 5; /* one repos costs 5 pts */
  196.             reposition_player(player);
  197.             break;
  198.         case 'W': /* walk */
  199.             {
  200.             int new_x, new_y;
  201.             new_x = players[player].p_x_pos +
  202.                 delta_x[players[player].p_richtg];
  203.             new_y = players[player].p_y_pos +
  204.                 delta_y[players[player].p_richtg];
  205.             if ( maze[new_y][new_x] == ' ' )
  206.                 {
  207.                 players[player].p_x_pos = new_x;
  208.                 players[player].p_y_pos = new_y;
  209.                 }
  210.             break;
  211.             }
  212.         case ' ': /* backwalk */
  213.             {
  214.             int new_x, new_y;
  215.             new_x = players[player].p_x_pos +
  216.                 delta_x[(players[player].p_richtg+2)%4];
  217.             new_y = players[player].p_y_pos +
  218.                 delta_y[(players[player].p_richtg+2)%4];
  219.             if ( maze[new_y][new_x] == ' ' )
  220.                 {
  221.                 players[player].p_x_pos = new_x;
  222.                 players[player].p_y_pos = new_y;
  223.                 }
  224.             break;
  225.             }
  226.         break;
  227.         }
  228.     }
  229.  
  230. log_out(p)
  231. int p;
  232.     {
  233.     int play;
  234.     /* recursion can occur, therefore use local structs ! */
  235.     struct sp_anzeige raus;
  236.     if ( score_fd >= 0 )
  237.         {
  238.         lseek(score_fd,((long)players[p].p_uid)*sizeof(long),0);
  239.         write(score_fd,&(players[player].p_score),sizeof(long));
  240.         }
  241.     raus.sp_magic  = SP_ANZ;
  242.     raus.sp_pid    = players[p].p_pid;
  243.     raus.sp_lfd_nr = p;
  244.     raus.sp_x_pos  = players[p].p_x_pos;
  245.     raus.sp_y_pos  = players[p].p_y_pos;
  246.     raus.sp_flag   = 0;
  247.     for(play=0; play<N_PLAYERS; play++)
  248.         tell(play,raus);
  249.     close (players[p].p_fd  );
  250.     unlink(players[p].p_pipe);
  251.     players[p].p_pid = 0;
  252.     }
  253.  
  254. tell(p,spa)
  255. int p;
  256. struct sp_anzeige spa;
  257.     {
  258.     if ( players[p].p_pid )
  259.         {
  260.         int n_bytes;
  261.         alarm(3);
  262.         n_bytes = write(players[p].p_fd,&spa,sizeof(spa));
  263.         alarm(0);
  264.         if ( n_bytes<1 )
  265.             {
  266.             players[p].p_pid = 0; /* avoid endless recursion */
  267.             log_out(p); /* recursion */
  268.             }
  269.         }
  270.     }
  271.  
  272. determine_visibility()
  273.     {
  274.     register i,j; /* speedup */
  275.     for(i=0; i<N_PLAYERS; i++)
  276.     if ( players[i].p_pid )
  277.     for(j=0; j<N_PLAYERS; j++)
  278.     if ( players[j].p_pid )
  279.     {
  280.     int visibel;
  281.     register struct player *pi, *pj; /* speedup */
  282.     pi = players + i; pj = players + j;
  283.     visibel = (pi->p_x_pos == pj->p_x_pos)
  284.         ||    (pi->p_y_pos == pj->p_y_pos) ;
  285.     if ( visibel )
  286.         {
  287.         /* i can view j. */
  288.         /* did he see him before 
  289.         at the same position ? */
  290.         if ( (pj->p_x_pos == pi->last_x[j]) &&
  291.              (pj->p_y_pos == pi->last_y[j]) &&
  292.              (pj->p_richtg== pi->last_r[j]) &&
  293.              (pj->p_score == pi->last_s[j]) )
  294.             { /* then: nothing to do. */ }
  295.         else /* he was somewhere else before. */
  296.             {
  297.             /* was he in the game at all ? */
  298.             if ( pi->last_x[j]>=0 )
  299.                 /* did he just look in another direction? */
  300.                 if ( (pj->p_x_pos == pi->last_x[j]) &&
  301.                      (pj->p_y_pos == pi->last_y[j]) )
  302.                     { /* nothing to erase then. */ }
  303.                 else
  304.                     /* his last position has to be erased
  305.                     on the screen. */
  306.                     send_player_info(j,i,pi->last_x[j],pi->last_y[j],0);
  307.             /* store new position and send it. */
  308.             pi->last_x[j] = pj->p_x_pos;
  309.             pi->last_y[j] = pj->p_y_pos;
  310.             pi->last_r[j] = pj->p_richtg;
  311.             pi->last_s[j] = pj->p_score;
  312.             send_player_info(j,i,pi->last_x[j],pi->last_y[j],2);
  313.             }
  314.         }
  315.     else
  316.         {
  317.         /* i cannot view j. did he not see him before? */
  318.         if ( pi->last_x[j]<0 )
  319.             /* if score changed, display though */
  320.             if ( pi->last_s[j] == pj->p_score )
  321.                 { /* nothing to do here */ }
  322.             else
  323.                 {
  324.                 send_player_info(j,i,pi->last_x[j],pi->last_y[j],0);
  325.                 pi->last_s[j] = pj->p_score;
  326.                 }
  327.         else /* he was visibel before. */
  328.             {
  329.             /* his last position on the screen has to be erased */
  330.             send_player_info(j,i,pi->last_x[j],pi->last_y[j],0);
  331.             pi->last_x[j] = -1;
  332.             }
  333.         }
  334.     }
  335.     }
  336.  
  337. /* END */
  338.