Tried using a fixed meandering path for the snake effect. Doesn't work as of now.
This commit is contained in:
@ -124,6 +124,35 @@ int8_t SnakeEffect::_manual_decision() {
|
||||
return 0;
|
||||
}*/
|
||||
|
||||
/* This uses a predefined meandering path through the complete field.
|
||||
The snake always tries to reach the field matching following criteria:
|
||||
(1) Being free.
|
||||
(2) Having a number smaller than the field the apple is on. (Watch out for "overflows".)
|
||||
*/
|
||||
int8_t SnakeEffect::_manual_decision() {
|
||||
uint16_t head_index = (_head_rounds<<8) | _coords_to_field_id(_pos);
|
||||
uint16_t apple_index = (_head_rounds<<8) | _coords_to_field_id(_apple);
|
||||
uint16_t tail_index = (_tail_rounds<<8) | _coords_to_field_id(_tail);
|
||||
|
||||
if (apple_index < head_index) apple_index += 0x100;
|
||||
|
||||
uint8_t best_dist = 0xFF;
|
||||
int8_t decision = 0;
|
||||
for (int i=-1; i<=1; i++) {
|
||||
Coords new_pos = _new_pos(_dir + i);
|
||||
uint16_t theoretical_index = (_head_rounds<<8) | _coords_to_field_id(new_pos);
|
||||
if (theoretical_index < head_index) theoretical_index += 0x100;
|
||||
int16_t dist = apple_index - theoretical_index;
|
||||
if (dist < 0) dist += window->height * window->width;
|
||||
if (dist < best_dist && _is_free(_dir + i) && theoretical_index<tail_index && theoretical_index<tail_index) {
|
||||
decision = i;
|
||||
best_dist = dist;
|
||||
}
|
||||
}
|
||||
_dir = (_dir + decision) % 4;
|
||||
return decision;
|
||||
}
|
||||
|
||||
bool SnakeEffect::_is_free(uint8_t dir) {
|
||||
return _free_spaces(dir)!=0;
|
||||
}
|
||||
@ -150,6 +179,17 @@ uint8_t SnakeEffect::_free_spaces(uint8_t dir) {
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t SnakeEffect::_coords_to_field_id(Coords c) {
|
||||
if (c.y==0) return window->width - c.x;
|
||||
if (c.x % 2 == 0) {
|
||||
// even columns
|
||||
return window->width + c.x*(window->height - 1) + c.y - 1;
|
||||
} else {
|
||||
// odd columns
|
||||
return window->width + (c.x+1)*(window->height-1) - c.y;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t SnakeEffect::_to_apple(uint8_t dir) {
|
||||
uint8_t d = dir % 4;
|
||||
int8_t d_x = _apple.x - _pos.x;
|
||||
@ -207,7 +247,7 @@ void SnakeEffect::_move() {
|
||||
}
|
||||
_round++;
|
||||
_last_move_at = now;
|
||||
_decide();
|
||||
_manual_decision();
|
||||
|
||||
if (_dying==0 && !_is_free(_dir)) {
|
||||
_dying = 150;
|
||||
@ -215,6 +255,10 @@ void SnakeEffect::_move() {
|
||||
}
|
||||
|
||||
_pos = _new_pos(_dir);
|
||||
|
||||
uint8_t index_head = _coords_to_field_id(_pos);
|
||||
uint8_t index_tail = _coords_to_field_id(_tail);
|
||||
|
||||
if (SNAKE_DEBUG) LOGln("SnakeEffect * new_pos: %d, %d", _pos.x, _pos.y);
|
||||
if (SNAKE_DEBUG) LOGln("SnakeEffect * apple: %d, %d", _apple.x, _apple.y);
|
||||
if (_pos.x==_apple.x && _pos.y==_apple.y) {
|
||||
@ -222,13 +266,26 @@ void SnakeEffect::_move() {
|
||||
_length++;
|
||||
}
|
||||
for (int i=0; i<_pixels; i++) {
|
||||
if (_map[i]>0 && _map[i]<_length-1) _map[i]++;
|
||||
if (_map[i]>0 && _map[i]<_length-1) {
|
||||
_map[i]++;
|
||||
if (_map[i]==_length-1) {
|
||||
_tail = _i2xy(i);
|
||||
}
|
||||
}
|
||||
else _map[i]=0;
|
||||
}
|
||||
_map[_xy2i(_pos)] = 1;
|
||||
if (_pos.x==_apple.x && _pos.y==_apple.y) {
|
||||
_place_apple();
|
||||
}
|
||||
|
||||
if (index_head > _coords_to_field_id(_pos)) _head_rounds++;
|
||||
if (index_tail > _coords_to_field_id(_tail)) _tail_rounds++;
|
||||
if (_head_rounds > 0 && _head_rounds > 0) {
|
||||
uint8_t min = (_head_rounds < _tail_rounds) ? _tail_rounds : _head_rounds;
|
||||
_head_rounds -= min;
|
||||
_tail_rounds -= min;
|
||||
}
|
||||
}
|
||||
|
||||
void SnakeEffect::_draw() {
|
||||
|
Reference in New Issue
Block a user