-
Notifications
You must be signed in to change notification settings - Fork 5
/
detection_history.c
61 lines (55 loc) · 2.06 KB
/
detection_history.c
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
#include "detection_history.h"
#include "enemy_detection.h"
#include <stddef.h>
#include <stdint.h>
static bool detection_cmp(const detection_t *a, const detection_t *b)
{
return a->line == b->line && a->enemy.position == b->enemy.position
&& a->enemy.range == b->enemy.range;
}
static bool valid_history_entry(const detection_t *detection)
{
return detection->enemy.position != ENEMY_POS_NONE || detection->line != LINE_DETECTION_NONE;
}
/*
static void print_history(history_t *history)
{
trace("===========\n");
for (uint16_t i = 0; i < HISTORY_SIZE; i++) {
const uint16_t hist_idx = (i <= history->idx) ?
(history->idx - i) :
(HISTORY_SIZE + history->idx - i);
if (valid_history_entry(&history->detections[hist_idx])) {
trace("%s %s\n", line_detection_str(history->detections[hist_idx].line),
enemy_pos_str(history->detections[hist_idx].enemy.position));
} else {
break;
}
}
}
*/
void detection_history_save(detection_history_t *history, const detection_t *detection)
{
if (!detection_cmp(detection, &history->detections[history->idx])) {
if (detection->enemy.position != ENEMY_POS_NONE || detection->line != LINE_DETECTION_NONE) {
history->idx = (history->idx + 1) % HISTORY_SIZE;
history->detections[history->idx] = *detection;
}
}
}
const enemy_detection_t *detection_history_last_enemy_direction(const detection_history_t *history)
{
for (uint16_t i = 0; i < HISTORY_SIZE; i++) {
const uint16_t hist_idx =
(i <= history->idx) ? (history->idx - i) : (HISTORY_SIZE + history->idx - i);
if (valid_history_entry(&history->detections[hist_idx])) {
if (enemy_at_left(&history->detections[hist_idx].enemy)
|| enemy_at_right(&history->detections[hist_idx].enemy)) {
return &(history->detections[hist_idx].enemy);
}
} else {
break;
}
}
return NULL;
}