Merge branch 'master' of eng-git.canterbury.ac.nz:mph/ucfk4
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
# Definitions.
|
# Definitions.
|
||||||
CC = avr-gcc
|
CC = avr-gcc
|
||||||
CFLAGS = -mmcu=atmega32u2 -Os -Wall -Wstrict-prototypes -Wextra -g -I../../drivers/avr -I../../drivers -I../../utils
|
CFLAGS = -mmcu=atmega32u2 -Os -Wall -Wstrict-prototypes -Wextra -g -I. -I../../drivers/avr -I../../fonts -I../../drivers -I../../utils
|
||||||
OBJCOPY = avr-objcopy
|
OBJCOPY = avr-objcopy
|
||||||
SIZE = avr-size
|
SIZE = avr-size
|
||||||
DEL = rm
|
DEL = rm
|
||||||
@@ -16,7 +16,10 @@ all: gobble6.out
|
|||||||
|
|
||||||
|
|
||||||
# Compile: create object files from C source files.
|
# Compile: create object files from C source files.
|
||||||
gobble6.o: gobble6.c ../../drivers/avr/system.h ../../drivers/avr/timer.h ../../drivers/display.h ../../drivers/led.h ../../drivers/navswitch.h ../../utils/font.h ../../utils/pacer.h ../../utils/tinygl.h
|
gobble6.o: gobble6.c ../../drivers/avr/system.h ../../drivers/avr/timer.h ../../drivers/display.h ../../drivers/led.h ../../drivers/navswitch.h ../../fonts/font3x5_1.h ../../utils/font.h ../../utils/pacer.h ../../utils/tinygl.h things.h
|
||||||
|
$(CC) -c $(CFLAGS) $< -o $@
|
||||||
|
|
||||||
|
things.o: things.c ../../drivers/avr/system.h ../../drivers/display.h ../../utils/font.h ../../utils/tinygl.h things.h
|
||||||
$(CC) -c $(CFLAGS) $< -o $@
|
$(CC) -c $(CFLAGS) $< -o $@
|
||||||
|
|
||||||
pio.o: ../../drivers/avr/pio.c ../../drivers/avr/pio.h ../../drivers/avr/system.h
|
pio.o: ../../drivers/avr/pio.c ../../drivers/avr/pio.h ../../drivers/avr/system.h
|
||||||
@@ -53,7 +56,7 @@ tinygl.o: ../../utils/tinygl.c ../../drivers/avr/system.h ../../drivers/display.
|
|||||||
|
|
||||||
|
|
||||||
# Link: create output file (executable) from object files.
|
# Link: create output file (executable) from object files.
|
||||||
gobble6.out: gobble6.o pio.o system.o timer.o display.o led.o ledmat.o navswitch.o font.o pacer.o tinygl.o
|
gobble6.out: gobble6.o things.o pio.o system.o timer.o display.o led.o ledmat.o navswitch.o font.o pacer.o tinygl.o
|
||||||
$(CC) $(CFLAGS) $^ -o $@ -lm
|
$(CC) $(CFLAGS) $^ -o $@ -lm
|
||||||
$(SIZE) $@
|
$(SIZE) $@
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
# Descr: Makefile for gobble6
|
# Descr: Makefile for gobble6
|
||||||
|
|
||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -Wall -Wstrict-prototypes -Wextra -g -I../../drivers/test -I../../drivers -I../../utils
|
CFLAGS = -Wall -Wstrict-prototypes -Wextra -g -I. -I../../drivers/test -I../../drivers -I../../fonts -I../../utils
|
||||||
|
|
||||||
DEL = rm
|
DEL = rm
|
||||||
|
|
||||||
@@ -14,7 +14,10 @@ all: gobble6
|
|||||||
|
|
||||||
|
|
||||||
# Compile: create object files from C source files.
|
# Compile: create object files from C source files.
|
||||||
gobble6-test.o: gobble6.c ../../drivers/display.h ../../drivers/led.h ../../drivers/navswitch.h ../../drivers/test/system.h ../../drivers/test/timer.h ../../utils/font.h ../../utils/pacer.h ../../utils/tinygl.h
|
gobble6-test.o: gobble6.c ../../drivers/display.h ../../drivers/led.h ../../drivers/navswitch.h ../../drivers/test/system.h ../../drivers/test/timer.h ../../fonts/font3x5_1.h ../../utils/font.h ../../utils/pacer.h ../../utils/tinygl.h things.h
|
||||||
|
$(CC) -c $(CFLAGS) $< -o $@
|
||||||
|
|
||||||
|
things-test.o: things.c ../../drivers/display.h ../../drivers/test/system.h ../../utils/font.h ../../utils/tinygl.h things.h
|
||||||
$(CC) -c $(CFLAGS) $< -o $@
|
$(CC) -c $(CFLAGS) $< -o $@
|
||||||
|
|
||||||
display-test.o: ../../drivers/display.c ../../drivers/display.h ../../drivers/ledmat.h ../../drivers/test/system.h
|
display-test.o: ../../drivers/display.c ../../drivers/display.h ../../drivers/ledmat.h ../../drivers/test/system.h
|
||||||
@@ -54,14 +57,14 @@ tinygl-test.o: ../../utils/tinygl.c ../../drivers/display.h ../../drivers/test/s
|
|||||||
|
|
||||||
|
|
||||||
# Link: create executable file from object files.
|
# Link: create executable file from object files.
|
||||||
gobble6: gobble6-test.o display-test.o led-test.o ledmat-test.o navswitch-test.o mgetkey-test.o pio-test.o system-test.o timer-test.o font-test.o pacer-test.o tinygl-test.o
|
gobble6: gobble6-test.o things-test.o display-test.o led-test.o ledmat-test.o navswitch-test.o mgetkey-test.o pio-test.o system-test.o timer-test.o font-test.o pacer-test.o tinygl-test.o
|
||||||
$(CC) $(CFLAGS) $^ -o $@ -lrt
|
$(CC) $(CFLAGS) $^ -o $@ -lrt
|
||||||
|
|
||||||
|
|
||||||
# Clean: delete derived files.
|
# Clean: delete derived files.
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
-$(DEL) gobble6 gobble6-test.o display-test.o led-test.o ledmat-test.o navswitch-test.o mgetkey-test.o pio-test.o system-test.o timer-test.o font-test.o pacer-test.o tinygl-test.o
|
-$(DEL) gobble6 gobble6-test.o things-test.o display-test.o led-test.o ledmat-test.o navswitch-test.o mgetkey-test.o pio-test.o system-test.o timer-test.o font-test.o pacer-test.o tinygl-test.o
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+50
-204
@@ -4,8 +4,8 @@
|
|||||||
@brief
|
@brief
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include "system.h"
|
#include "system.h"
|
||||||
#include "pacer.h"
|
#include "pacer.h"
|
||||||
#include "navswitch.h"
|
#include "navswitch.h"
|
||||||
@@ -13,9 +13,8 @@
|
|||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include "tinygl.h"
|
#include "tinygl.h"
|
||||||
#include "../fonts/font3x5_1.h"
|
#include "../fonts/font3x5_1.h"
|
||||||
|
#include "things.h"
|
||||||
|
|
||||||
/* Number of initial objects. */
|
|
||||||
#define NUM_THINGS 6
|
|
||||||
|
|
||||||
#define LOOP_RATE 200
|
#define LOOP_RATE 200
|
||||||
|
|
||||||
@@ -23,183 +22,18 @@
|
|||||||
|
|
||||||
#define NAVSWITCH_RATE 50
|
#define NAVSWITCH_RATE 50
|
||||||
|
|
||||||
#define SAFE_DISTANCE 3
|
#define MONSTER_FLASH_RATE 5
|
||||||
|
|
||||||
#define ANXIETY_LEVEL 2
|
|
||||||
|
|
||||||
#define PANIC_LEVEL 50
|
|
||||||
|
|
||||||
typedef struct thing_struct
|
|
||||||
{
|
|
||||||
tinygl_point_t pos;
|
|
||||||
uint8_t anxiety;
|
|
||||||
bool alive;
|
|
||||||
} thing_t;
|
|
||||||
|
|
||||||
|
|
||||||
static int8_t things_killed_p (thing_t *things, uint8_t num)
|
|
||||||
{
|
|
||||||
uint8_t i;
|
|
||||||
|
|
||||||
for (i = 1; i < num; i++)
|
|
||||||
{
|
|
||||||
if (things[i].alive)
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int8_t thing_find (thing_t *things, uint8_t num, tinygl_point_t pos)
|
|
||||||
{
|
|
||||||
uint8_t i;
|
|
||||||
|
|
||||||
for (i = 0; i < num; i++)
|
|
||||||
{
|
|
||||||
if (things[i].pos.x == pos.x
|
|
||||||
&& things[i].pos.y == pos.y
|
|
||||||
&& things[i].alive)
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static uint8_t thing_distance_squared (thing_t *things, uint8_t this)
|
|
||||||
{
|
|
||||||
int8_t dx;
|
|
||||||
int8_t dy;
|
|
||||||
|
|
||||||
dx = things[this].pos.x - things[0].pos.x;
|
|
||||||
dy = things[this].pos.y - things[0].pos.y;
|
|
||||||
|
|
||||||
return dx * dx + dy * dy;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static uint8_t bounds_p (tinygl_point_t pos)
|
|
||||||
{
|
|
||||||
return pos.x >= 0 && pos.x < TINYGL_WIDTH
|
|
||||||
&& pos.y >= 0 && pos.y < TINYGL_HEIGHT;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void monster_move (thing_t *things, uint8_t num, int8_t dx, int8_t dy)
|
|
||||||
{
|
|
||||||
int8_t other;
|
|
||||||
tinygl_point_t newpos;
|
|
||||||
|
|
||||||
newpos = tinygl_point (things[0].pos.x + dx, things[0].pos.y + dy);
|
|
||||||
if (!bounds_p (newpos))
|
|
||||||
return;
|
|
||||||
|
|
||||||
other = thing_find (things, num, newpos);
|
|
||||||
if (other != -1)
|
|
||||||
things[other].alive = 0;
|
|
||||||
|
|
||||||
tinygl_draw_point (things[0].pos, 0);
|
|
||||||
things[0].pos = newpos;
|
|
||||||
tinygl_draw_point (newpos, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void thing_move (thing_t *things, uint8_t num, uint8_t this)
|
|
||||||
{
|
|
||||||
uint8_t i;
|
|
||||||
uint8_t distsq;
|
|
||||||
uint8_t panic;
|
|
||||||
tinygl_point_t pos;
|
|
||||||
|
|
||||||
tinygl_draw_point (things[this].pos, 0);
|
|
||||||
|
|
||||||
distsq = thing_distance_squared (things, this);
|
|
||||||
pos = things[this].pos;
|
|
||||||
|
|
||||||
if (thing_distance_squared (things, this) <= SAFE_DISTANCE * SAFE_DISTANCE)
|
|
||||||
things[this].anxiety++;
|
|
||||||
else
|
|
||||||
things[this].anxiety = 0;
|
|
||||||
|
|
||||||
panic = things[this].anxiety > ANXIETY_LEVEL
|
|
||||||
&& (rand () % 100 > (100 - PANIC_LEVEL));
|
|
||||||
|
|
||||||
for (i = 0; i < 12; i++)
|
|
||||||
{
|
|
||||||
int8_t dx;
|
|
||||||
int8_t dy;
|
|
||||||
tinygl_point_t newpos;
|
|
||||||
|
|
||||||
dx = (rand () % 3) - 1;
|
|
||||||
dy = (rand () % 3) - 1;
|
|
||||||
|
|
||||||
newpos = tinygl_point (pos.x + dx, pos.y + dy);
|
|
||||||
|
|
||||||
/* Check for a valid position. */
|
|
||||||
if ((dx || dy)
|
|
||||||
&& (! bounds_p (newpos) || thing_find (things, num, newpos) != -1))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
/* Try new position. */
|
|
||||||
things[this].pos = newpos;
|
|
||||||
|
|
||||||
if (panic || thing_distance_squared (things, this) > distsq)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Stay put if cannot find better position. */
|
|
||||||
if (i == 12)
|
|
||||||
things[this].pos = pos;
|
|
||||||
|
|
||||||
tinygl_draw_point (things[this].pos, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void things_move (thing_t *things, uint8_t num)
|
|
||||||
{
|
|
||||||
uint8_t i;
|
|
||||||
|
|
||||||
for (i = 1; i < num; i++)
|
|
||||||
if (things[i].alive)
|
|
||||||
thing_move (things, num, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void things_create (thing_t *things, uint8_t num)
|
|
||||||
{
|
|
||||||
uint8_t i;
|
|
||||||
|
|
||||||
things[0].pos.x = 0;
|
|
||||||
things[0].pos.y = 0;
|
|
||||||
things[0].alive = 1;
|
|
||||||
tinygl_draw_point (things[0].pos, 1);
|
|
||||||
|
|
||||||
for (i = 1; i < num; i++)
|
|
||||||
{
|
|
||||||
uint8_t x;
|
|
||||||
uint8_t y;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
x = rand () % TINYGL_WIDTH;
|
|
||||||
y = rand () % TINYGL_HEIGHT;
|
|
||||||
} while (thing_find (things, i, tinygl_point (x, y)) != -1);
|
|
||||||
|
|
||||||
things[i].pos.x = x;
|
|
||||||
things[i].pos.y = y;
|
|
||||||
things[i].alive = 1;
|
|
||||||
|
|
||||||
tinygl_draw_point (things[i].pos, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main (void)
|
int main (void)
|
||||||
{
|
{
|
||||||
uint16_t tick = 0;
|
uint16_t tick = 0;
|
||||||
uint16_t navswitch_tick = 0;
|
uint16_t navswitch_tick = 0;
|
||||||
uint8_t running = 0;
|
uint16_t monster_flash_tick = 0;
|
||||||
|
bool running = 0;
|
||||||
|
bool game_over = 1;
|
||||||
int duration = 0;
|
int duration = 0;
|
||||||
thing_t things[NUM_THINGS];
|
|
||||||
|
|
||||||
system_init ();
|
system_init ();
|
||||||
navswitch_init ();
|
navswitch_init ();
|
||||||
@@ -227,6 +61,7 @@ int main (void)
|
|||||||
tinygl_update ();
|
tinygl_update ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Poll the navswitch and update monster position. */
|
||||||
navswitch_tick++;
|
navswitch_tick++;
|
||||||
if (navswitch_tick >= LOOP_RATE / NAVSWITCH_RATE)
|
if (navswitch_tick >= LOOP_RATE / NAVSWITCH_RATE)
|
||||||
{
|
{
|
||||||
@@ -234,53 +69,64 @@ int main (void)
|
|||||||
|
|
||||||
navswitch_update ();
|
navswitch_update ();
|
||||||
|
|
||||||
if (running)
|
if (navswitch_push_event_p (NAVSWITCH_NORTH))
|
||||||
{
|
monster_move (0, -1);
|
||||||
duration++;
|
if (navswitch_push_event_p (NAVSWITCH_SOUTH))
|
||||||
if (navswitch_push_event_p (NAVSWITCH_NORTH))
|
monster_move (0, 1);
|
||||||
monster_move (things, NUM_THINGS, 0, -1);
|
if (navswitch_push_event_p (NAVSWITCH_EAST))
|
||||||
if (navswitch_push_event_p (NAVSWITCH_SOUTH))
|
monster_move (1, 0);
|
||||||
monster_move (things, NUM_THINGS, 0, 1);
|
if (navswitch_push_event_p (NAVSWITCH_WEST))
|
||||||
if (navswitch_push_event_p (NAVSWITCH_EAST))
|
monster_move (-1, 0);
|
||||||
monster_move (things, NUM_THINGS, 1, 0);
|
|
||||||
if (navswitch_push_event_p (NAVSWITCH_WEST))
|
|
||||||
monster_move (things, NUM_THINGS, -1, 0);
|
|
||||||
if (navswitch_push_event_p (NAVSWITCH_PUSH))
|
|
||||||
{
|
|
||||||
running = 0;
|
|
||||||
led_set (LED1, running);
|
|
||||||
}
|
|
||||||
if (things_killed_p (things, NUM_THINGS))
|
|
||||||
{
|
|
||||||
char buffer[6];
|
|
||||||
|
|
||||||
running = 0;
|
/* Pause/resume things running around. */
|
||||||
led_set (LED1, running);
|
if (navswitch_push_event_p (NAVSWITCH_PUSH))
|
||||||
sprintf (buffer, "%d", duration);
|
|
||||||
tinygl_text (buffer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
if (navswitch_push_event_p (NAVSWITCH_PUSH))
|
if (! running && game_over)
|
||||||
{
|
{
|
||||||
srand (timer_get ());
|
srand (timer_get ());
|
||||||
tinygl_clear ();
|
tinygl_clear ();
|
||||||
things_create (things, NUM_THINGS);
|
things_create ();
|
||||||
running = 1;
|
|
||||||
duration = 0;
|
duration = 0;
|
||||||
led_set (LED1, running);
|
game_over = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
running = !running;
|
||||||
|
led_set (LED1, running);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (running && things_killed_p ())
|
||||||
|
{
|
||||||
|
char buffer[6];
|
||||||
|
|
||||||
|
running = 0;
|
||||||
|
game_over = 1;
|
||||||
|
led_set (LED1, 0);
|
||||||
|
sprintf (buffer, "%d", duration);
|
||||||
|
tinygl_text (buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Move the things. */
|
||||||
tick++;
|
tick++;
|
||||||
if (tick >= LOOP_RATE / MOVE_RATE)
|
if (tick >= LOOP_RATE / MOVE_RATE)
|
||||||
{
|
{
|
||||||
tick = 0;
|
tick = 0;
|
||||||
|
|
||||||
if (running)
|
if (running)
|
||||||
things_move (things, NUM_THINGS);
|
{
|
||||||
|
duration++;
|
||||||
|
things_move ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Flash the monster. */
|
||||||
|
monster_flash_tick++;
|
||||||
|
if (monster_flash_tick >= LOOP_RATE / MONSTER_FLASH_RATE / 2)
|
||||||
|
{
|
||||||
|
monster_flash_tick = 0;
|
||||||
|
if (running)
|
||||||
|
monster_toggle ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -0,0 +1,193 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include "things.h"
|
||||||
|
#include "tinygl.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* Number of initial objects. */
|
||||||
|
#define THINGS_NUM 6
|
||||||
|
|
||||||
|
#define SAFE_DISTANCE 3
|
||||||
|
|
||||||
|
#define ANXIETY_LEVEL 2
|
||||||
|
|
||||||
|
#define PANIC_LEVEL 50
|
||||||
|
|
||||||
|
#define MOVE_ATTEMPTS 12
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct thing_struct
|
||||||
|
{
|
||||||
|
tinygl_point_t pos;
|
||||||
|
uint8_t anxiety;
|
||||||
|
bool alive;
|
||||||
|
} thing_t;
|
||||||
|
|
||||||
|
|
||||||
|
static thing_t things[THINGS_NUM];
|
||||||
|
|
||||||
|
|
||||||
|
bool things_killed_p (void)
|
||||||
|
{
|
||||||
|
uint8_t i;
|
||||||
|
|
||||||
|
for (i = 1; i < THINGS_NUM; i++)
|
||||||
|
{
|
||||||
|
if (things[i].alive)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int8_t thing_find (tinygl_point_t pos)
|
||||||
|
{
|
||||||
|
uint8_t i;
|
||||||
|
|
||||||
|
for (i = 0; i < THINGS_NUM; i++)
|
||||||
|
{
|
||||||
|
if (things[i].pos.x == pos.x
|
||||||
|
&& things[i].pos.y == pos.y
|
||||||
|
&& things[i].alive)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static uint8_t thing_distance_squared (thing_t *things, uint8_t this)
|
||||||
|
{
|
||||||
|
int8_t dx;
|
||||||
|
int8_t dy;
|
||||||
|
|
||||||
|
dx = things[this].pos.x - things[0].pos.x;
|
||||||
|
dy = things[this].pos.y - things[0].pos.y;
|
||||||
|
|
||||||
|
return dx * dx + dy * dy;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static bool thing_inbounds_p (tinygl_point_t pos)
|
||||||
|
{
|
||||||
|
return pos.x >= 0 && pos.x < TINYGL_WIDTH
|
||||||
|
&& pos.y >= 0 && pos.y < TINYGL_HEIGHT;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static bool thing_position_valid_p (tinygl_point_t pos)
|
||||||
|
{
|
||||||
|
return thing_inbounds_p (pos) && thing_find (pos) == -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void monster_move (int8_t dx, int8_t dy)
|
||||||
|
{
|
||||||
|
int8_t other;
|
||||||
|
tinygl_point_t newpos;
|
||||||
|
|
||||||
|
newpos = tinygl_point (things[0].pos.x + dx, things[0].pos.y + dy);
|
||||||
|
if (!thing_inbounds_p (newpos))
|
||||||
|
return;
|
||||||
|
|
||||||
|
other = thing_find (newpos);
|
||||||
|
if (other != -1)
|
||||||
|
things[other].alive = 0;
|
||||||
|
|
||||||
|
tinygl_draw_point (things[0].pos, 0);
|
||||||
|
things[0].pos = newpos;
|
||||||
|
tinygl_draw_point (newpos, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void monster_toggle (void)
|
||||||
|
{
|
||||||
|
tinygl_pixel_set (things[0].pos, ! tinygl_pixel_get (things[0].pos));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void thing_move (uint8_t this)
|
||||||
|
{
|
||||||
|
uint8_t i;
|
||||||
|
uint8_t distsq;
|
||||||
|
uint8_t panic;
|
||||||
|
tinygl_point_t pos;
|
||||||
|
|
||||||
|
tinygl_draw_point (things[this].pos, 0);
|
||||||
|
|
||||||
|
distsq = thing_distance_squared (things, this);
|
||||||
|
pos = things[this].pos;
|
||||||
|
|
||||||
|
if (thing_distance_squared (things, this) <= SAFE_DISTANCE * SAFE_DISTANCE)
|
||||||
|
things[this].anxiety++;
|
||||||
|
else
|
||||||
|
things[this].anxiety = 0;
|
||||||
|
|
||||||
|
panic = things[this].anxiety > ANXIETY_LEVEL
|
||||||
|
&& (rand () % 100 > (100 - PANIC_LEVEL));
|
||||||
|
|
||||||
|
for (i = 0; i < MOVE_ATTEMPTS; i++)
|
||||||
|
{
|
||||||
|
int8_t dx;
|
||||||
|
int8_t dy;
|
||||||
|
tinygl_point_t newpos;
|
||||||
|
|
||||||
|
dx = (rand () % 3) - 1;
|
||||||
|
dy = (rand () % 3) - 1;
|
||||||
|
|
||||||
|
newpos = tinygl_point (pos.x + dx, pos.y + dy);
|
||||||
|
|
||||||
|
if ((dx || dy) && ! thing_position_valid_p (newpos))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
things[this].pos = newpos;
|
||||||
|
|
||||||
|
if (panic || thing_distance_squared (things, this) > distsq)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Stay put if cannot find better position. */
|
||||||
|
if (i == MOVE_ATTEMPTS)
|
||||||
|
things[this].pos = pos;
|
||||||
|
|
||||||
|
tinygl_draw_point (things[this].pos, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void things_move (void)
|
||||||
|
{
|
||||||
|
uint8_t i;
|
||||||
|
|
||||||
|
for (i = 1; i < THINGS_NUM; i++)
|
||||||
|
if (things[i].alive)
|
||||||
|
thing_move (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void things_create (void)
|
||||||
|
{
|
||||||
|
uint8_t i;
|
||||||
|
|
||||||
|
things[0].pos.x = 0;
|
||||||
|
things[0].pos.y = 0;
|
||||||
|
things[0].alive = 1;
|
||||||
|
tinygl_draw_point (things[0].pos, 1);
|
||||||
|
|
||||||
|
for (i = 1; i < THINGS_NUM; i++)
|
||||||
|
{
|
||||||
|
uint8_t x;
|
||||||
|
uint8_t y;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
x = rand () % TINYGL_WIDTH;
|
||||||
|
y = rand () % TINYGL_HEIGHT;
|
||||||
|
} while (thing_find (tinygl_point (x, y)) != -1);
|
||||||
|
|
||||||
|
things[i].pos.x = x;
|
||||||
|
things[i].pos.y = y;
|
||||||
|
things[i].alive = 1;
|
||||||
|
|
||||||
|
tinygl_draw_point (things[i].pos, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef THINGS_H
|
||||||
|
#define THINGS_H
|
||||||
|
|
||||||
|
#include "system.h"
|
||||||
|
|
||||||
|
bool things_killed_p (void);
|
||||||
|
|
||||||
|
void things_create (void);
|
||||||
|
|
||||||
|
void things_move (void);
|
||||||
|
|
||||||
|
void monster_move (int8_t dx, int8_t dy);
|
||||||
|
|
||||||
|
void monster_toggle (void);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -57,8 +57,15 @@ timer_tick_t timer_wait_until (timer_tick_t when)
|
|||||||
|
|
||||||
now = timer_get ();
|
now = timer_get ();
|
||||||
|
|
||||||
|
/* Note, both when and now are unsigned so they wrap around to
|
||||||
|
zero when they overflow. The difference is also unsigned
|
||||||
|
so when now < when then the result wraps around to a large
|
||||||
|
positive value and fails the following test. */
|
||||||
diff = now - when;
|
diff = now - when;
|
||||||
|
|
||||||
|
/* There is a possibility that we are called when when < now.
|
||||||
|
So we allow a possible overrun defined by
|
||||||
|
TIMER_OVERRUN_MAX. */
|
||||||
if (diff < TIMER_OVERRUN_MAX)
|
if (diff < TIMER_OVERRUN_MAX)
|
||||||
return now;
|
return now;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user