summaryrefslogtreecommitdiff
path: root/src/engine/moves/vec.c
blob: 6da3eb94ce6b9ac1325f94128794200ce03c47ce (plain)
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
#include "engine/moves.h"

#include <assert.h>
#include <stdlib.h>

void moves_init(moves_t *moves) {
  return moves_init_wcapacity(moves, 16);
}

void moves_deinit(moves_t moves) {
  free(moves.moves);
}

void moves_init_wcapacity(moves_t *moves, u32 capacity) {
  moves->moves = malloc(capacity * sizeof(*moves));
  moves->length = 0;
  moves->capacity = capacity;
}

moves_t moves_empty() {
  return (moves_t) {
    .moves = 0,
    .capacity = 0,
    .length = 0,
  };
}

int min(int a, int b) {
  if (a > b) return b;
  return a;
}

void _add_move(
  moves_t* moves,
  square_t from,
  square_t to,
  struct add_move_params params
) {
  assert(moves->moves != 0);
  assert(from != to);

  if (moves->length + 1 >= moves->capacity) {
    moves->capacity += min(32, moves->capacity);
    moves->moves = realloc(
      moves->moves,
      moves->capacity * sizeof(*moves->moves)
    );
  }
  moves->moves[moves->length++] = (move_t) {
    .from = from,
    .to = to,
    .flags = params.flags,
  };
}