reindentert til 4 mellomrom
This commit is contained in:
		
							
								
								
									
										106
									
								
								src/net.c
									
									
									
									
									
								
							
							
						
						
									
										106
									
								
								src/net.c
									
									
									
									
									
								
							@@ -1,3 +1,5 @@
 | 
				
			|||||||
 | 
					/* -*- c-basic-offset: 4 -*- */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <sys/types.h>
 | 
					#include <sys/types.h>
 | 
				
			||||||
#include <sys/socket.h>
 | 
					#include <sys/socket.h>
 | 
				
			||||||
#include <netinet/in.h>
 | 
					#include <netinet/in.h>
 | 
				
			||||||
@@ -18,69 +20,69 @@ int nclients = 0;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
int
 | 
					int
 | 
				
			||||||
errx(int eval, const char *fmt, ...) {
 | 
					errx(int eval, const char *fmt, ...) {
 | 
				
			||||||
  va_list ap;
 | 
					    va_list ap;
 | 
				
			||||||
  vfprintf(stderr, fmt, ap);
 | 
					    vfprintf(stderr, fmt, ap);
 | 
				
			||||||
  exit(eval);
 | 
					    exit(eval);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
net_server(int port) {
 | 
					net_server(int port) {
 | 
				
			||||||
  struct sockaddr_in serveraddr;
 | 
					    struct sockaddr_in serveraddr;
 | 
				
			||||||
  serveraddr.sin_family = AF_INET;
 | 
					    serveraddr.sin_family = AF_INET;
 | 
				
			||||||
  serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
 | 
					    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
 | 
				
			||||||
  serveraddr.sin_port = htons(port);
 | 
					    serveraddr.sin_port = htons(port);
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  servsock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
 | 
					    servsock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
 | 
				
			||||||
  if (servsock<0)
 | 
					    if (servsock<0)
 | 
				
			||||||
    errx(1, "socket: %s", strerror(errno));
 | 
						errx(1, "socket: %s", strerror(errno));
 | 
				
			||||||
  if (bind(servsock, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
 | 
					    if (bind(servsock, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
 | 
				
			||||||
    errx(1, "bind port %d: %s", port, strerror(errno));
 | 
						errx(1, "bind port %d: %s", port, strerror(errno));
 | 
				
			||||||
  if (listen(servsock, 10) < 0)
 | 
					    if (listen(servsock, 10) < 0)
 | 
				
			||||||
    errx(1, "listen: %s", strerror(errno));
 | 
						errx(1, "listen: %s", strerror(errno));
 | 
				
			||||||
  pollfds[0].fd=servsock;
 | 
					    pollfds[0].fd=servsock;
 | 
				
			||||||
  pollfds[0].events=POLLIN|POLLERR|POLLHUP;
 | 
					    pollfds[0].events=POLLIN|POLLERR|POLLHUP;
 | 
				
			||||||
  for (int i=SRV_FDS; i<(SRV_FDS+MAX_CLIENTS); i++) {
 | 
					    for (int i=SRV_FDS; i<(SRV_FDS+MAX_CLIENTS); i++) {
 | 
				
			||||||
    pollfds[0].fd=-1;
 | 
						pollfds[0].fd=-1;
 | 
				
			||||||
    pollfds[0].events=0;
 | 
						pollfds[0].events=0;
 | 
				
			||||||
  }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
net_poll() {
 | 
					net_poll() {
 | 
				
			||||||
  int npoll;
 | 
					    int npoll;
 | 
				
			||||||
  npoll=poll(pollfds, SRV_FDS+MAX_CLIENTS, -1);
 | 
					    npoll=poll(pollfds, SRV_FDS+MAX_CLIENTS, -1);
 | 
				
			||||||
  if (npoll<0)
 | 
					    if (npoll<0)
 | 
				
			||||||
    errx(1, "poll: %s", strerror(errno));
 | 
						errx(1, "poll: %s", strerror(errno));
 | 
				
			||||||
  if (pollfds[0].revents) {
 | 
					    if (pollfds[0].revents) {
 | 
				
			||||||
    struct sockaddr_in clientaddr;
 | 
						struct sockaddr_in clientaddr;
 | 
				
			||||||
    int cfd;
 | 
						int cfd;
 | 
				
			||||||
    socklen_t slen;
 | 
						socklen_t slen;
 | 
				
			||||||
    cfd=accept(servsock, (struct sockaddr *)&clientaddr, &slen);
 | 
						cfd=accept(servsock, (struct sockaddr *)&clientaddr, &slen);
 | 
				
			||||||
    if (cfd<0)
 | 
						if (cfd<0)
 | 
				
			||||||
      errx(1, "accept: %s", strerror(errno));
 | 
						    errx(1, "accept: %s", strerror(errno));
 | 
				
			||||||
    // FIXME log, shout, or something
 | 
						// FIXME log, shout, or something
 | 
				
			||||||
    if (nclients>MAX_CLIENTS) {
 | 
						if (nclients>MAX_CLIENTS) {
 | 
				
			||||||
#define STR_SERVER_FULL "Sorry, the server is full"
 | 
					#define STR_SERVER_FULL "Sorry, the server is full"
 | 
				
			||||||
      write(cfd, STR_SERVER_FULL, sizeof(STR_SERVER_FULL)-1);
 | 
						    write(cfd, STR_SERVER_FULL, sizeof(STR_SERVER_FULL)-1);
 | 
				
			||||||
      close(cfd);
 | 
						    close(cfd);
 | 
				
			||||||
    } else {
 | 
						} else {
 | 
				
			||||||
      int i;
 | 
						    int i;
 | 
				
			||||||
      for (i=SRV_FDS; i<(SRV_FDS+MAX_CLIENTS); i++)
 | 
						    for (i=SRV_FDS; i<(SRV_FDS+MAX_CLIENTS); i++)
 | 
				
			||||||
	if (pollfds[i].fd==-1) break;
 | 
							if (pollfds[i].fd==-1) break;
 | 
				
			||||||
      pollfds[i].fd=cfd;
 | 
						    pollfds[i].fd=cfd;
 | 
				
			||||||
      pollfds[i].events=POLLIN|POLLERR|POLLHUP;
 | 
						    pollfds[i].events=POLLIN|POLLERR|POLLHUP;
 | 
				
			||||||
      nclients++;
 | 
						    nclients++;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						npoll--;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    npoll--;
 | 
					    for (int i=SRV_FDS; npoll && i<(SRV_FDS+MAX_CLIENTS); i++) {
 | 
				
			||||||
  }
 | 
						if (pollfds[SRV_FDS+i].revents) {
 | 
				
			||||||
  for (int i=SRV_FDS; npoll && i<(SRV_FDS+MAX_CLIENTS); i++) {
 | 
						    int n;
 | 
				
			||||||
    if (pollfds[SRV_FDS+i].revents) {
 | 
						    char buf[100];
 | 
				
			||||||
      int n;
 | 
						    n=read(pollfds[SRV_FDS+i].fd, &buf, sizeof(buf));
 | 
				
			||||||
      char buf[100];
 | 
						    // parse buf
 | 
				
			||||||
      n=read(pollfds[SRV_FDS+i].fd, &buf, sizeof(buf));
 | 
						    // handle disconnections
 | 
				
			||||||
      // parse buf
 | 
						}
 | 
				
			||||||
      // handle disconnections
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										215
									
								
								src/server.c
									
									
									
									
									
								
							
							
						
						
									
										215
									
								
								src/server.c
									
									
									
									
									
								
							@@ -1,3 +1,4 @@
 | 
				
			|||||||
 | 
					/* -*- c-basic-offset: 4 -*- */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define NONE 0 
 | 
					#define NONE 0 
 | 
				
			||||||
#define PHARAO 1
 | 
					#define PHARAO 1
 | 
				
			||||||
@@ -11,140 +12,140 @@
 | 
				
			|||||||
#define F_SPLIT 2
 | 
					#define F_SPLIT 2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct square {
 | 
					struct square {
 | 
				
			||||||
  int piece : 4;
 | 
					    int piece : 4;
 | 
				
			||||||
  int side : 1;
 | 
					    int side : 1;
 | 
				
			||||||
  int dir : 2;
 | 
					    int dir : 2;
 | 
				
			||||||
} board[10][8];
 | 
					} board[10][8];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
struct pieceinfo {
 | 
					  struct pieceinfo {
 | 
				
			||||||
  char ascii[];
 | 
					  char ascii[];
 | 
				
			||||||
} pieceinfo[] = {
 | 
					  } pieceinfo[] = {
 | 
				
			||||||
  { //NONE
 | 
					  { //NONE
 | 
				
			||||||
    .ascii = { ' ' } }
 | 
					  .ascii = { ' ' } }
 | 
				
			||||||
  { //PHARAO
 | 
					  { //PHARAO
 | 
				
			||||||
    .ascii = { '=' } }
 | 
					  .ascii = { '=' } }
 | 
				
			||||||
  { //DJHED
 | 
					  { //DJHED
 | 
				
			||||||
    .ascii = { '/', '\\' } }
 | 
					  .ascii = { '/', '\\' } }
 | 
				
			||||||
  { //PYRAMID
 | 
					  { //PYRAMID
 | 
				
			||||||
    .ascii = { 'd', 'b', 'p', 'q' } }
 | 
					  .ascii = { 'd', 'b', 'p', 'q' } }
 | 
				
			||||||
  { //OBELISK
 | 
					  { //OBELISK
 | 
				
			||||||
    .ascii = { 'o' } }
 | 
					  .ascii = { 'o' } }
 | 
				
			||||||
  { //OBELISK2
 | 
					  { //OBELISK2
 | 
				
			||||||
    .ascii = { '@' } }
 | 
					  .ascii = { '@' } }
 | 
				
			||||||
};
 | 
					  };
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
void
 | 
					  void
 | 
				
			||||||
print_board() {
 | 
					  print_board() {
 | 
				
			||||||
  for (int y=0; y<8; y++)
 | 
					  for (int y=0; y<8; y++)
 | 
				
			||||||
    for (int x=0; x<10; x++)
 | 
					  for (int x=0; x<10; x++)
 | 
				
			||||||
      ;
 | 
					  ;
 | 
				
			||||||
}
 | 
					  }
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int
 | 
					int
 | 
				
			||||||
laser(int side) {
 | 
					laser(int side) {
 | 
				
			||||||
  int x, y, dir;
 | 
					    int x, y, dir;
 | 
				
			||||||
  int ddir;
 | 
					    int ddir;
 | 
				
			||||||
  switch (side) {
 | 
					    switch (side) {
 | 
				
			||||||
  case 0:
 | 
					    case 0:
 | 
				
			||||||
    x=9; y=0; dir=0;
 | 
						x=9; y=0; dir=0;
 | 
				
			||||||
    break;
 | 
						break;
 | 
				
			||||||
  case 1:
 | 
					    case 1:
 | 
				
			||||||
    x=0; y=7; dir=2; 
 | 
						x=0; y=7; dir=2; 
 | 
				
			||||||
    break;
 | 
						break;
 | 
				
			||||||
  default:
 | 
					    default:
 | 
				
			||||||
    return 0;
 | 
						return 0;
 | 
				
			||||||
  }
 | 
					    }
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  for (;;) {
 | 
					    for (;;) {
 | 
				
			||||||
    switch (board[x][y].piece) {
 | 
						switch (board[x][y].piece) {
 | 
				
			||||||
    case NONE:
 | 
						case NONE:
 | 
				
			||||||
      break;
 | 
						    break;
 | 
				
			||||||
    case PHARAO:
 | 
						case PHARAO:
 | 
				
			||||||
      return 2 + board[x][y].side; /* GAME OVER */
 | 
						    return 2 + board[x][y].side; /* GAME OVER */
 | 
				
			||||||
    case OBELISK2:
 | 
						case OBELISK2:
 | 
				
			||||||
      board[x][y].piece = OBELISK;
 | 
						    board[x][y].piece = OBELISK;
 | 
				
			||||||
      return 1;
 | 
						    return 1;
 | 
				
			||||||
    case OBELISK:
 | 
						case OBELISK:
 | 
				
			||||||
      board[x][y].piece = NONE;
 | 
						    board[x][y].piece = NONE;
 | 
				
			||||||
      return 1;
 | 
						    return 1;
 | 
				
			||||||
    case DJHED:
 | 
						case DJHED:
 | 
				
			||||||
      ddir = board[x][y].dir - dir;
 | 
						    ddir = board[x][y].dir - dir;
 | 
				
			||||||
      dir=(dir + 1 - 2*(ddir & 1)) & 3;
 | 
						    dir=(dir + 1 - 2*(ddir & 1)) & 3;
 | 
				
			||||||
      break;
 | 
						    break;
 | 
				
			||||||
    case PYRAMID:
 | 
						case PYRAMID:
 | 
				
			||||||
      ddir = board[x][y].dir - dir;
 | 
						    ddir = board[x][y].dir - dir;
 | 
				
			||||||
      if (ddir & 2) {
 | 
						    if (ddir & 2) {
 | 
				
			||||||
	board[x][y].piece = NONE;
 | 
							board[x][y].piece = NONE;
 | 
				
			||||||
	return 1;
 | 
							return 1;
 | 
				
			||||||
      }
 | 
						    }
 | 
				
			||||||
      dir=(dir + 1 - 2*(ddir & 1)) & 3;
 | 
						    dir=(dir + 1 - 2*(ddir & 1)) & 3;
 | 
				
			||||||
      break;
 | 
						    break;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						switch (dir & 3) {
 | 
				
			||||||
 | 
						case 0: y++; if (y>7) return 0; break;
 | 
				
			||||||
 | 
						case 1: x++; if (x>9) return 0; break;
 | 
				
			||||||
 | 
						case 2: y--; if (y<0) return 0; break;
 | 
				
			||||||
 | 
						case 3: x--; if (x<0) return 0; break;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    switch (dir & 3) {
 | 
					 | 
				
			||||||
    case 0: y++; if (y>7) return 0; break;
 | 
					 | 
				
			||||||
    case 1: x++; if (x>9) return 0; break;
 | 
					 | 
				
			||||||
    case 2: y--; if (y<0) return 0; break;
 | 
					 | 
				
			||||||
    case 3: x--; if (x<0) return 0; break;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int
 | 
					int
 | 
				
			||||||
move(int side, int x, int y, int dir, int flags) {
 | 
					move(int side, int x, int y, int dir, int flags) {
 | 
				
			||||||
  if ((x<0) || (x>9) || (y<0) || (y>7))
 | 
					    if ((x<0) || (x>9) || (y<0) || (y>7))
 | 
				
			||||||
    return 0;
 | 
						return 0;
 | 
				
			||||||
  if (!board[x][y].piece)
 | 
					    if (!board[x][y].piece)
 | 
				
			||||||
    return 0;
 | 
						return 0;
 | 
				
			||||||
  if ((board[x][y].side != side))
 | 
					    if ((board[x][y].side != side))
 | 
				
			||||||
    return 0;
 | 
						return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (flags & F_ROTATE) {
 | 
					    if (flags & F_ROTATE) {
 | 
				
			||||||
    if ( !((dir==1) || (dir==-1)))
 | 
						if ( !((dir==1) || (dir==-1)))
 | 
				
			||||||
      return 0;
 | 
						    return 0;
 | 
				
			||||||
    board[x][y].dir = (board[x][y].dir + dir) & 3;
 | 
						board[x][y].dir = (board[x][y].dir + dir) & 3;
 | 
				
			||||||
 | 
						return 1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					    /* dir =
 | 
				
			||||||
 | 
					       0 1 2
 | 
				
			||||||
 | 
					       7 X 3
 | 
				
			||||||
 | 
					       6 5 4
 | 
				
			||||||
 | 
					    */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if ((dir<0) || (dir>7))
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					    int ny = y - (dir<3) + ((dir>3) && (dir<7));
 | 
				
			||||||
 | 
					    int nx = x + ((dir>1) && (dir<5)) - ((dir<1) || (dir>5));
 | 
				
			||||||
 | 
					    if ((nx<0) || (nx>9) || (ny<0) || (ny>7))
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (flags & F_SPLIT) {
 | 
				
			||||||
 | 
						if (board[nx][ny].piece || (board[x][y].piece != OBELISK2))
 | 
				
			||||||
 | 
						    return 0;
 | 
				
			||||||
 | 
						board[x][y].piece = OBELISK;
 | 
				
			||||||
 | 
						board[nx][ny].piece = OBELISK;
 | 
				
			||||||
 | 
						board[nx][ny].side = board[x][y].side;
 | 
				
			||||||
 | 
						return 1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					    if (board[nx][ny].piece &&
 | 
				
			||||||
 | 
						!((board[x][y].piece == DJHED) &&
 | 
				
			||||||
 | 
						  ((board[nx][ny].piece != DJHED) &&
 | 
				
			||||||
 | 
						   (board[nx][ny].piece != PHARAO))))
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    struct square tmp;
 | 
				
			||||||
 | 
					    tmp = board[nx][ny];
 | 
				
			||||||
 | 
					    board[nx][ny] = board[x][y];
 | 
				
			||||||
 | 
					    board[x][y] = tmp;
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
    return 1;
 | 
					    return 1;
 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  
 | 
					 | 
				
			||||||
  /* dir =
 | 
					 | 
				
			||||||
    0 1 2
 | 
					 | 
				
			||||||
    7 X 3
 | 
					 | 
				
			||||||
    6 5 4
 | 
					 | 
				
			||||||
  */
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if ((dir<0) || (dir>7))
 | 
					 | 
				
			||||||
    return 0;
 | 
					 | 
				
			||||||
  int ny = y - (dir<3) + ((dir>3) && (dir<7));
 | 
					 | 
				
			||||||
  int nx = x + ((dir>1) && (dir<5)) - ((dir<1) || (dir>5));
 | 
					 | 
				
			||||||
  if ((nx<0) || (nx>9) || (ny<0) || (ny>7))
 | 
					 | 
				
			||||||
    return 0;
 | 
					 | 
				
			||||||
  
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if (flags & F_SPLIT) {
 | 
					 | 
				
			||||||
    if (board[nx][ny].piece || (board[x][y].piece != OBELISK2))
 | 
					 | 
				
			||||||
      return 0;
 | 
					 | 
				
			||||||
    board[x][y].piece = OBELISK;
 | 
					 | 
				
			||||||
    board[nx][ny].piece = OBELISK;
 | 
					 | 
				
			||||||
    board[nx][ny].side = board[x][y].side;
 | 
					 | 
				
			||||||
    return 1;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  
 | 
					 | 
				
			||||||
  if (board[nx][ny].piece &&
 | 
					 | 
				
			||||||
      !((board[x][y].piece == DJHED) &&
 | 
					 | 
				
			||||||
	((board[nx][ny].piece != DJHED) &&
 | 
					 | 
				
			||||||
	 (board[nx][ny].piece != PHARAO))))
 | 
					 | 
				
			||||||
    return 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  struct square tmp;
 | 
					 | 
				
			||||||
  tmp = board[nx][ny];
 | 
					 | 
				
			||||||
  board[nx][ny] = board[x][y];
 | 
					 | 
				
			||||||
  board[x][y] = tmp;
 | 
					 | 
				
			||||||
  
 | 
					 | 
				
			||||||
  return 1;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user