2010-04-11 11:37:31 +00:00
|
|
|
/* Copyright (c) 2010 Jan Lieven
|
2010-04-11 06:40:32 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "../config.h"
|
|
|
|
#include "../pixel.h"
|
|
|
|
#include "../util.h"
|
|
|
|
#include "../random/prng.h"
|
|
|
|
|
|
|
|
void ltn_ant() {
|
|
|
|
clear_screen(0);
|
|
|
|
|
|
|
|
struct {
|
2010-04-11 11:37:31 +00:00
|
|
|
char x , y;
|
2010-04-11 06:40:32 +00:00
|
|
|
char dx, dy; /* Vector can only be: (0,1),(1,0),(0,-1),(-1,0) */
|
2010-04-11 11:37:31 +00:00
|
|
|
char ox, oy; /* Used to set old pixels so brightness 2 */
|
2010-04-11 06:40:32 +00:00
|
|
|
} ant;
|
|
|
|
|
2010-04-15 12:34:40 +00:00
|
|
|
char temp, i = 0, refill = 0;
|
|
|
|
uint16_t cycles = 500;
|
2010-04-11 06:40:32 +00:00
|
|
|
|
|
|
|
/* Random startposition and direction */
|
|
|
|
ant.x = random8() % NUM_COLS;
|
|
|
|
ant.y = random8() % NUM_ROWS;
|
|
|
|
|
|
|
|
/* Make sure we do have a valid vector */
|
|
|
|
do {
|
2010-04-15 12:34:40 +00:00
|
|
|
ant.dx = (random8() % 3) - 1;
|
|
|
|
ant.dy = (random8() % 3) - 1;
|
2010-04-11 06:40:32 +00:00
|
|
|
} while(ant.dx == ant.dy);
|
|
|
|
|
2010-04-15 12:34:40 +00:00
|
|
|
while(cycles != 0) {
|
2010-04-11 06:40:32 +00:00
|
|
|
if(get_pixel((pixel) {ant.x, ant.y}) == 0) { /* If the pixel is not set turn it on */
|
|
|
|
setpixel((pixel) {ant.x, ant.y}, 3);
|
2010-04-11 11:37:31 +00:00
|
|
|
|
|
|
|
temp = ant.dx;
|
2010-04-11 06:40:32 +00:00
|
|
|
ant.dx = ant.dy;
|
2010-04-11 11:37:31 +00:00
|
|
|
ant.dy = -temp; /* Turn 90 degrees to the right */
|
|
|
|
|
|
|
|
/* Lets the last pixel be darker than the latest */
|
|
|
|
if(i == 1)
|
|
|
|
setpixel((pixel) {ant.ox, ant.oy}, 2);
|
2010-04-15 12:34:40 +00:00
|
|
|
else
|
|
|
|
i = 1;
|
2010-04-11 11:37:31 +00:00
|
|
|
ant.ox = ant.x;
|
|
|
|
ant.oy = ant.y;
|
|
|
|
|
2010-04-11 06:40:32 +00:00
|
|
|
} else {
|
|
|
|
setpixel((pixel) {ant.x, ant.y}, 0);
|
2010-04-11 11:37:31 +00:00
|
|
|
|
|
|
|
temp = ant.dy;
|
2010-04-11 06:40:32 +00:00
|
|
|
ant.dy = ant.dx;
|
2010-04-11 11:37:31 +00:00
|
|
|
ant.dx = -temp; /* Turn 90 degrees to the left */
|
2010-04-11 06:40:32 +00:00
|
|
|
}
|
2010-04-11 11:37:31 +00:00
|
|
|
|
2010-04-11 06:40:32 +00:00
|
|
|
wait(100);
|
|
|
|
|
2010-04-11 11:37:31 +00:00
|
|
|
ant.x += ant.dx;
|
|
|
|
ant.y += ant.dy;
|
|
|
|
|
2010-04-15 12:34:40 +00:00
|
|
|
/* The ant will appear on the oppisite side of the field if it would leave */
|
|
|
|
if(ant.x <= 0)
|
|
|
|
ant.x += (NUM_ROWS - 1);
|
|
|
|
if(ant.x >= NUM_ROWS)
|
|
|
|
ant.x -= (NUM_ROWS + 1);
|
|
|
|
if(ant.y <= 0)
|
|
|
|
ant.y += (NUM_COLS - 1);
|
|
|
|
if(ant.y >= NUM_COLS)
|
|
|
|
ant.y -= (NUM_COLS + 1);
|
|
|
|
|
|
|
|
cycles--;
|
|
|
|
}
|
2010-04-11 11:37:31 +00:00
|
|
|
wait(300);
|
2010-04-11 06:40:32 +00:00
|
|
|
|
|
|
|
}
|