game.c 4.2 KB

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