game.c 4.1 KB

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