Copy project
This commit is contained in:
parent
ce7a467af7
commit
26a0fd884f
173 changed files with 157458 additions and 1 deletions
14
ad_astra/AdAstra/src/util/random/random.c
Normal file
14
ad_astra/AdAstra/src/util/random/random.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include "random.h"
|
||||
|
||||
|
||||
static uint16_t currentValue = SEED;
|
||||
|
||||
static uint16_t getRandom16bitNumberModuloPrime() {
|
||||
uint16_t bit = currentValue ^ (currentValue >> 2) ^ (currentValue >> 3) ^ (currentValue >> 5);
|
||||
currentValue = (currentValue >> 1) | (bit << 15);
|
||||
return currentValue % 1031;
|
||||
}
|
||||
|
||||
uint8_t getRandomNumber() {
|
||||
return (uint8_t)(getRandom16bitNumberModuloPrime() ^ getRandom16bitNumberModuloPrime());
|
||||
}
|
||||
14
ad_astra/AdAstra/src/util/random/random.h
Normal file
14
ad_astra/AdAstra/src/util/random/random.h
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef RANDOM_H
|
||||
#define RANDOM_H
|
||||
|
||||
#include <avr/io.h>
|
||||
|
||||
|
||||
// Mustn't be zero, should be lower than 65535
|
||||
#define SEED 42
|
||||
|
||||
// Simple LFSR with some improvements to enhance distribution
|
||||
// while maintaining short execution time
|
||||
uint8_t getRandomNumber();
|
||||
|
||||
#endif
|
||||
34
ad_astra/AdAstra/src/util/rectangle/rectangle.c
Normal file
34
ad_astra/AdAstra/src/util/rectangle/rectangle.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include "rectangle.h"
|
||||
|
||||
|
||||
bool areIntersecting(Rectangle r1, Rectangle r2) {
|
||||
return (
|
||||
r1.position.x < r2.position.x + r2.size.x &&
|
||||
r1.position.x + r1.size.x > r2.position.x &&
|
||||
r1.position.y < r2.position.y + r2.size.y &&
|
||||
r1.position.y + r1.size.y > r2.position.y
|
||||
);
|
||||
}
|
||||
|
||||
bool isInside(Rectangle inner, Rectangle outer) {
|
||||
return (
|
||||
outer.position.x <= inner.position.x &&
|
||||
inner.position.x + inner.size.x <= outer.position.x + outer.size.x &&
|
||||
outer.position.y <= inner.position.y &&
|
||||
inner.position.y + inner.size.y <= outer.position.y + outer.size.y
|
||||
);
|
||||
}
|
||||
|
||||
Vec2 getCenter(Rectangle r) {
|
||||
return (Vec2) {
|
||||
r.position.x + r.size.x / 2,
|
||||
r.position.y + r.size.y / 2
|
||||
};
|
||||
}
|
||||
|
||||
Rectangle translateRectangle(Rectangle r, Vec2 translate) {
|
||||
return (Rectangle) {
|
||||
add(r.position, translate),
|
||||
r.size
|
||||
};
|
||||
}
|
||||
26
ad_astra/AdAstra/src/util/rectangle/rectangle.h
Normal file
26
ad_astra/AdAstra/src/util/rectangle/rectangle.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#ifndef RECTANGLE_H
|
||||
#define RECTANGLE_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "../vec2/vec2.h"
|
||||
|
||||
|
||||
typedef struct {
|
||||
Vec2 position;
|
||||
Vec2 size;
|
||||
} Rectangle;
|
||||
|
||||
|
||||
bool areIntersecting(Rectangle r1, Rectangle r2);
|
||||
|
||||
// Return true only if inner is fully inside of outer
|
||||
bool isInside(Rectangle inner, Rectangle outer);
|
||||
|
||||
// Return the geometrical middle point of the given rectangle
|
||||
Vec2 getCenter(Rectangle r);
|
||||
|
||||
// Return a new rectangle shifted by vector translate
|
||||
Rectangle translateRectangle(Rectangle r, Vec2 translate);
|
||||
|
||||
#endif
|
||||
24
ad_astra/AdAstra/src/util/vec2/vec2.c
Normal file
24
ad_astra/AdAstra/src/util/vec2/vec2.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include "vec2.h"
|
||||
|
||||
|
||||
const Vec2 directions[] = {
|
||||
[north] = (Vec2){0, -1},
|
||||
[west] = (Vec2){-1, 0},
|
||||
[south] = (Vec2){0, 1},
|
||||
[east] = (Vec2){1, 0}
|
||||
};
|
||||
|
||||
Vec2 add(Vec2 v1, Vec2 v2) {
|
||||
return (Vec2){v1.x + v2.x, v1.y + v2.y};
|
||||
}
|
||||
|
||||
Vec2 substract(Vec2 v1, Vec2 v2) {
|
||||
return (Vec2){v1.x - v2.x, v1.y - v2.y};
|
||||
}
|
||||
|
||||
Vec2 clampVec2(Vec2 v) {
|
||||
return (Vec2){
|
||||
v.x == 0 ? 0 : (v.x > 0 ? 1 : -1),
|
||||
v.y == 0 ? 0 : (v.y > 0 ? 1 : -1)
|
||||
};
|
||||
}
|
||||
26
ad_astra/AdAstra/src/util/vec2/vec2.h
Normal file
26
ad_astra/AdAstra/src/util/vec2/vec2.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#ifndef VEC2_H
|
||||
#define VEC2_H
|
||||
|
||||
#include <avr/io.h>
|
||||
|
||||
|
||||
typedef struct {
|
||||
int8_t x;
|
||||
int8_t y;
|
||||
} Vec2;
|
||||
|
||||
typedef enum {
|
||||
north, west, south, east
|
||||
} Direction;
|
||||
|
||||
// The array directions can be indexed by a Direction and
|
||||
// it contains vectors pointing into that direction.
|
||||
const Vec2 directions[4];
|
||||
|
||||
Vec2 add(Vec2 v1, Vec2 v2);
|
||||
Vec2 substract(Vec2 v1, Vec2 v2);
|
||||
|
||||
// Return new vector with ll components between -1 and 1 (inclusive)
|
||||
Vec2 clampVec2(Vec2 vector);
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue