aboutsummaryrefslogtreecommitdiff
path: root/source/gamemode.c
blob: 6a09b080bfe3b41eba5c7f572c8b24e673bfaa83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "all.h"

static void MapPrint(game_t * game) {
  u8 width  = game->config.map_x;
  u8 height = game->config.map_y;
  printf("texture, explosive, breakable, lethal, pickup\n");
  for (int i = 0; i < width; ++i) {
    for (int j = 0; j < height; ++j) {
      tile_data_t * tile = &game->tiles.state[i][j];
      printf("%2d:%1d:%1d:%1d:%1d ", tile->texture, tile->explosive, tile->breakable, tile->lethal, tile->pickup);
    }
    printf("\n");
  }
}

static void GamemodeReset(game_t * game) {
  for (int i = 0; i < PLAYER_LIMIT; ++i) {
    for (int j = 0; j < BOMB_LIMIT; ++j) {
      game->bombs.timer[i][j] = 0;
    }
  }

  bzero(game->players.state, sizeof(*game->players.state) * PLAYER_LIMIT);
}

static void PlayerPlaceCorners(game_t * game, u8 * color, size_t color_count) {
  u8 width  = game->config.map_x;
  u8 height = game->config.map_y;
  float player_x[4] =
    {0, (width-1), (width-1), 0                   };
  float player_y[4] =
    {0, (height-1), 0,                  (height-1)};

  for (int i = 0; i < MIN(game->config.player_count, PLAYER_LIMIT); ++i) {
    game->players.x[i] = player_x[i % color_count];
    game->players.y[i] = player_y[i % color_count];
    game->players.color[i] = color[i % color_count];
  }
}

static void MapClearCorners(game_t * game) {
  /* areas that must be passible nothings */
  u8 width  = game->config.map_x;
  u8 height = game->config.map_y;

  u8 offset_x[12] =
    {0, 1, 0,  width-1,  width-2,  width-1, width-1, width-2, width-1,        0,        1,        0};
  u8 offset_y[12] =
    {0, 0, 1, height-1, height-1, height-2,       0,       0,       1, height-1, height-1, height-2};

  for (int i = 0; i < MIN((game->config.player_count * 3), 12); ++i) {
    game->tiles.state[offset_x[i]][offset_y[i]]  = passable_tile;
  }
}

void MultiPlayer(game_t * game) {
  u8 width  = game->config.map_x;
  u8 height = game->config.map_y;
  int i, j;

  GamemodeReset(game);

  for (i = 0; i < width; ++i) {
    for (j = 0; j < height; ++j) {
      game->tiles.state[i][j] = rand() % 10 ? breakable_wall : passable_tile;
      if (rand() % 3 == 0)
      {
        game->tiles.state[i][j].pickup = rand() % 5 ? POWERUP_POWER : POWERUP_BOMB;
      } else if (rand() % 10 == 0) {
        game->tiles.state[i][j].pickup = POWERUP_PIERCE;
      }
    }
  }

  for (i = 1; i < width; i += 2) {
    for (j = 1; j < height; j += 2) {
      game->tiles.state[i][j] = impassable_wall;
    }
  }

  for (i = 0; i < MIN(PLAYER_LIMIT, game->config.player_count); ++i) {
    game->players.state[i].bomb_limit = 1;
    game->players.state[i].power = 2;
    game->players.state[i].speed = 2;
    game->players.state[i].alive = 1;
    game->players.state[i].direction = DOWN;
  }

  game->time_limit = game->config.ups * 60 * 3;

  u8 color[4] = {
    GAME_RED | GAME_GREEN | GAME_OPAQUE,
    GAME_RED | GAME_GREEN | GAME_BLUE | GAME_OPAQUE,
    GAME_GREEN | GAME_OPAQUE,
    GAME_BLUE | GAME_OPAQUE,
  };

  PlayerPlaceCorners(game, color, 4);
  MapClearCorners(game);
  MapPrint(game);
}