Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.0 kB
1
Indexable
Never
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
  char battle_id; // [rsp+23h] [rbp-C8Dh]
  int i; // [rsp+28h] [rbp-C88h]
  unsigned int real_player_id; // [rsp+2Ch] [rbp-C84h]
  int j; // [rsp+30h] [rbp-C80h]
  int v7; // [rsp+34h] [rbp-C7Ch]
  int k; // [rsp+38h] [rbp-C78h]
  int m; // [rsp+3Ch] [rbp-C74h]
  int v10; // [rsp+40h] [rbp-C70h]
  int n; // [rsp+44h] [rbp-C6Ch]
  int ppid; // [rsp+48h] [rbp-C68h]
  unsigned int pid; // [rsp+50h] [rbp-C60h]
  int call_battles; // [rsp+54h] [rbp-C5Ch]
  int battle_attr; // [rsp+58h] [rbp-C58h]
  __pid_t v16; // [rsp+5Ch] [rbp-C54h]
  struct_a1 *player; // [rsp+60h] [rbp-C50h]
  int fd[7][2]; // [rsp+68h] [rbp-C48h] BYREF
  char s[1024]; // [rsp+A0h] [rbp-C10h] BYREF
  char child_id[1024]; // [rsp+4A0h] [rbp-810h] BYREF
  char cpid[1032]; // [rsp+8A0h] [rbp-410h] BYREF
  unsigned __int64 v22; // [rsp+CA8h] [rbp-8h]

  v22 = __readfsqword(0x28u);
  if ( argc != 3 )
    __assert_fail("argc == 3", "battle.c", 0x5Eu, "main");
  battle_id = *argv[1];
  ppid = atoi(argv[2]);
  pid = getpid();
  call_battles = 2 - num_of_player[battle_id - 65];
  battle_attr = battle_attrs[battle_id - 65];
  sprintf(s, "log_battle%c.txt", battle_id);
  logfile_fp = fopen(s, "w");
  for ( i = 0; i <= 1; ++i )
  {
    pipe(fd[i + 3]);
    pipe(fd[i + 5]);
    v16 = fork();
    fd[0][i] = v16;
    if ( !v16 )
    {
      close(fd[i + 3][0]);
      close(fd[i + 5][1]);
      dup2(fd[i + 3][1], 1);
      dup2(fd[i + 5][0], 0);
      close(fd[i + 3][1]);
      close(fd[i + 5][0]);
      sprintf(cpid, "%d", pid);
      if ( i >= call_battles )
      {
        sprintf(child_id, "%d", battle_children[2 * battle_id - 130 + i]);
        execl("./player", "./player", child_id, cpid, 0LL);
      }
      else
      {
        sprintf(child_id, "%c", battle_children[2 * battle_id - 130 + i]);
        execl("./battle", "./battle", child_id, cpid, 0LL);
      }
    }
    close(fd[i + 3][1]);
    close(fd[i + 5][0]);
    fd[1][i] = fd[i + 3][0];
    fd[2][i] = fd[i + 5][1];
  }
  player = malloc(48uLL);
  for ( j = 0; j <= 1; ++j )
    read_Status(fd[1][j], &player[j], battle_id, battle_children[2 * battle_id - 130 + j], fd[0][j]);
  while ( !player->battle_ended_flag )
  {
    player->current_battle_id = battle_id;
    player[1].current_battle_id = battle_id;
    v7 = player->HP > player[1].HP || player->HP == player[1].HP && player->real_player_id > player[1].real_player_id;
    if ( !play_an_attack(&player[v7], &player[1 - v7], battle_attr) )
    {
      v7 = 1 - v7;
      play_an_attack(&player[v7], &player[1 - v7], battle_attr);
    }
    for ( k = 0; k <= 1; ++k )
      write_Status(fd[2][k], &player[k], battle_id, battle_children[2 * battle_id - 130 + k], fd[0][k]);
    if ( !player->battle_ended_flag )
    {
      for ( m = 0; m <= 1; ++m )
        read_Status(fd[1][m], &player[m], battle_id, battle_children[2 * battle_id - 130 + m], fd[0][m]);
    }
  }
  wait(0LL);
  v10 = 1;
  while ( v10 )
  {
    if ( battle_id == 65 )
    {
      real_player_id = player[v7].real_player_id;
      wait(0LL);
      v10 = 0;
    }
    else
    {
      read_Status(fd[1][v7], &player[v7], battle_id, battle_children[2 * battle_id - 130 + v7], fd[0][v7]);
      write_Status(1, &player[v7], battle_id, battle_parent[battle_id - 65], ppid);
      read_Status(0, &player[v7], battle_id, battle_parent[battle_id - 65], ppid);
      write_Status(fd[2][v7], &player[v7], battle_id, battle_children[2 * battle_id - 130 + v7], fd[0][v7]);
      if ( player[v7].HP <= 0 || player[v7].current_battle_id == 65 && player[v7].battle_ended_flag == 1 )
      {
        wait(0LL);
        v10 = 0;
      }
    }
  }
  if ( battle_id == 65 )
  {
    printf("Champion is P%d\n", real_player_id);
    fflush(_bss_start);
  }
  fclose(logfile_fp);
  free(player);
  for ( n = 0; n <= 1; ++n )
  {
    close(fd[1][n]);
    close(fd[2][n]);
  }
  exit(0);
}