game.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* $Id: game.c,v 1.60 2001/05/07 02:41:36 kilobug Exp $ */
  2. #include <math.h>
  3. #include "server.h"
  4. #include <signal.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <sys/time.h>
  8. #include <sys/resource.h>
  9. #include <unistd.h>
  10. item_t *gl_objects;
  11. player_t *gl_player;
  12. conf_t *gl_config;
  13. static void signal_usr1(int foo)
  14. {
  15. }
  16. static void client_died(int foo)
  17. {
  18. gl_player->alive = FALSE;
  19. printf("%d: died.\n", gl_player->team_id);
  20. fflush(stdout);
  21. wait(NULL);
  22. }
  23. static void play_turn(int turn_num)
  24. {
  25. int i;
  26. start_timer();
  27. if (gl_player->new)
  28. {
  29. player_init(gl_player, gl_player->team_id);
  30. gl_player->new = FALSE;
  31. }
  32. else
  33. {
  34. player_new_turn(gl_player, turn_num);
  35. for (i = 0; i < gl_config->nb_objects; i++)
  36. {
  37. if (gl_objects[i].obj.team_id == gl_player->team_id)
  38. switch (gl_objects[i].obj.type)
  39. {
  40. case obj_akx:
  41. player_turn_akx(gl_player, gl_objects[i].obj.id);
  42. break;
  43. case obj_r4d2:
  44. player_turn_r4d2(gl_player, gl_objects[i].obj.id);
  45. break;
  46. }
  47. }
  48. }
  49. stop_timer();
  50. fflush(stdout);
  51. kill(getppid(), SIGUSR1);
  52. sigsuspend(gl_config->mask);
  53. }
  54. void start_client(player_t *player, int turn_num)
  55. {
  56. pid_t pid;
  57. struct rlimit rl;
  58. player->alive = TRUE;
  59. signal(SIGUSR1, signal_usr1);
  60. if (gl_config->gfx)
  61. fflush(gl_config->gfx_clients->wmaster);
  62. if ((pid = fork()))
  63. player->pid = pid;
  64. else
  65. {
  66. close(1);
  67. if (gl_config->gfx)
  68. {
  69. fclose(gl_config->gfx_clients->wmaster);
  70. gl_config->gfx_clients->wmaster = NULL;
  71. gl_config->gfx_clients->master = -1;
  72. close(gl_config->gfx_clients->sock);
  73. gl_config->gfx_clients->sock = -1;
  74. }
  75. dup2(player->pipe_write, 1);
  76. close(player->pipe_write);
  77. close(player->pipe_read);
  78. player->new = TRUE;
  79. gl_player = player;
  80. rl.rlim_cur = rl.rlim_max = 16 * 1024 * 1024;
  81. setrlimit(RLIMIT_DATA, &rl);
  82. while (TRUE)
  83. play_turn(turn_num++);
  84. }
  85. printf("Client %d : pid = %d\n", player->team_id, pid);
  86. fflush(stdout);
  87. }
  88. void turn_client(player_t *play)
  89. {
  90. if (play->alive)
  91. {
  92. kill(play->pid, SIGUSR1);
  93. sigsuspend(gl_config->mask);
  94. }
  95. else
  96. start_client(gl_player, gl_config->turn_num);
  97. }
  98. void genere_pipe()
  99. {
  100. GSList *l;
  101. player_t *player;
  102. int fds[2];
  103. for (l = gl_config->players; l != NULL; l = l->next)
  104. {
  105. pipe(fds);
  106. player = l->data;
  107. player->pipe_read = fds[0];
  108. player->pipe_write = fds[1];
  109. }
  110. }
  111. int check_done()
  112. {
  113. return (map_count_r4d2() == count_r4d2(0));
  114. }
  115. /* lanceur de partie */
  116. void launch_game(conf_t *conf)
  117. {
  118. GSList *l;
  119. int i;
  120. int fds[2];
  121. int pid;
  122. genere_pipe();
  123. signal(SIGCHLD, client_died);
  124. if (pipe(fds) == -1)
  125. {
  126. perror("pipe");
  127. exit(1);
  128. }
  129. fflush(stdout);
  130. if ((pid = fork()) == 0)
  131. {
  132. close(0);
  133. dup2(fds[0], 0);
  134. close(fds[0]);
  135. close(fds[1]);
  136. launch_messager();
  137. }
  138. else
  139. {
  140. /*fclose(stdout);*/
  141. dup2(fds[1], 1);
  142. close(fds[1]);
  143. close(fds[0]);
  144. /*stdout = fdopen(1, "w");*/
  145. printf("Messager : pid = %d\n", pid);
  146. printf("Serveur : pid = %d\n", getpid());
  147. fflush(stdout);
  148. gl_config->messager_pid = pid;
  149. for (conf->turn_num = 0; conf->turn_num <= conf->nb_turns;
  150. conf->turn_num++)
  151. {
  152. printf("Tour n° %d sur %d\n", conf->turn_num,
  153. conf->nb_turns);
  154. fflush(stdout);
  155. for (i = 0; i < conf->nb_objects; i++)
  156. gl_objects[i].obj.change = FALSE;
  157. for (l = conf->players; l != NULL; l = l->next)
  158. {
  159. gl_player = l->data;
  160. turn_client(l->data);
  161. }
  162. if (conf->turn_num != 0)
  163. exec_objects();
  164. if (check_done())
  165. break;
  166. }
  167. }
  168. }