[RELEASE] u22-v03

This version is submitted to U22 breau.
This commit is contained in:
2020-09-14 00:00:00 +00:00
parent 360595de37
commit 84c3a02b9a
357 changed files with 29223 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
add_library(locommon
counter.c
easing.c
null.c
physics.c
position.c
ticker.c
)
target_link_libraries(locommon
msgpackc
coly2d
math
mpkutil
)

45
core/locommon/counter.c Normal file
View File

@@ -0,0 +1,45 @@
#include "./counter.h"
#include <assert.h>
#include <stddef.h>
#include <msgpack.h>
#include "util/mpkutil/get.h"
void locommon_counter_initialize(locommon_counter_t* counter, size_t first) {
assert(counter != NULL);
*counter = (typeof(*counter)) {
.next = first,
};
}
void locommon_counter_deinitialize(locommon_counter_t* counter) {
assert(counter != NULL);
}
size_t locommon_counter_count(locommon_counter_t* counter) {
assert(counter != NULL);
return counter->next++;
}
void locommon_counter_pack(
const locommon_counter_t* counter, msgpack_packer* packer) {
assert(counter != NULL);
assert(packer != NULL);
msgpack_pack_uint64(packer, counter->next);
}
bool locommon_counter_unpack(
locommon_counter_t* counter, const msgpack_object* obj) {
assert(counter != NULL);
if (!mpkutil_get_uint64(obj, &counter->next)) {
return false;
}
return true;
}

38
core/locommon/counter.h Normal file
View File

@@ -0,0 +1,38 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <msgpack.h>
typedef struct {
size_t next;
} locommon_counter_t;
void
locommon_counter_initialize(
locommon_counter_t* counter,
size_t first
);
void
locommon_counter_deinitialize(
locommon_counter_t* counter
);
size_t
locommon_counter_count(
locommon_counter_t* counter
);
void
locommon_counter_pack(
const locommon_counter_t* counter,
msgpack_packer* packer
);
bool
locommon_counter_unpack(
locommon_counter_t* counter,
const msgpack_object* obj /* NULLABLE */
);

40
core/locommon/easing.c Normal file
View File

@@ -0,0 +1,40 @@
#include "./easing.h"
#include <assert.h>
#include <stddef.h>
#include "util/math/algorithm.h"
void locommon_easing_linear_float(float* v, float ed, float delta) {
assert(v != NULL);
assert(MATH_FLOAT_VALID(*v));
assert(MATH_FLOAT_VALID(delta));
assert(delta >= 0);
const float flag = MATH_SIGN(ed - *v);
*v += flag * delta;
if ((ed-*v)*flag < 0) *v = ed;
}
void locommon_easing_smooth_float(float* v, float ed, float delta) {
assert(v != NULL);
assert(MATH_FLOAT_VALID(*v));
assert(MATH_FLOAT_VALID(delta));
assert(delta >= 0);
*v += (ed - *v) * MATH_MIN(delta, 1);
}
void locommon_easing_smooth_position(
locommon_position_t* pos, const locommon_position_t* ed, float delta) {
assert(locommon_position_valid(pos));
assert(locommon_position_valid(ed));
assert(MATH_FLOAT_VALID(delta));
vec2_t diff;
locommon_position_sub(&diff, ed, pos);
vec2_muleq(&diff, MATH_MIN(delta, 1));
vec2_addeq(&pos->fract, &diff);
locommon_position_reduce(pos);
}

24
core/locommon/easing.h Normal file
View File

@@ -0,0 +1,24 @@
#pragma once
#include "./position.h"
void
locommon_easing_linear_float(
float* v,
float ed,
float delta
);
void
locommon_easing_smooth_float(
float* v,
float ed,
float delta
);
void
locommon_easing_smooth_position(
locommon_position_t* pos,
const locommon_position_t* ed,
float delta
);

27
core/locommon/input.h Normal file
View File

@@ -0,0 +1,27 @@
#pragma once
#include <stdint.h>
#include "util/math/vector.h"
typedef enum {
LOCOMMON_INPUT_BUTTON_LEFT = 1 << 0,
LOCOMMON_INPUT_BUTTON_RIGHT = 1 << 1,
LOCOMMON_INPUT_BUTTON_UP = 1 << 2,
LOCOMMON_INPUT_BUTTON_DOWN = 1 << 3,
LOCOMMON_INPUT_BUTTON_DASH = 1 << 4,
LOCOMMON_INPUT_BUTTON_JUMP = 1 << 5,
LOCOMMON_INPUT_BUTTON_ATTACK = 1 << 6,
LOCOMMON_INPUT_BUTTON_GUARD = 1 << 7,
LOCOMMON_INPUT_BUTTON_MENU = 1 << 8,
} locommon_input_button_t;
typedef uint16_t locommon_input_buttons_t;
typedef struct {
locommon_input_buttons_t buttons;
vec2_t resolution; /* in pixels */
vec2_t dpi;
vec2_t cursor; /* -1~1 */
} locommon_input_t;

118
core/locommon/msgpack.h Normal file
View File

@@ -0,0 +1,118 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <msgpack.h>
#include "util/math/vector.h"
#include "util/mpkutil/get.h"
#include "util/mpkutil/pack.h"
#include "core/locommon/counter.h"
#include "core/locommon/null.h"
/* THE FOLLOWING INCLUDES DESTROY DEPENDENCY STRUCTURE BETWEEN MODULES. :( */
#include "core/loeffect/effect.h"
#include "core/loeffect/generic.h"
#include "core/loeffect/recipient.h"
#include "core/loeffect/stance.h"
#include "core/loentity/entity.h"
#define LOCOMMON_MSGPACK_PACK_ANY_(packer, v) _Generic((v), \
const int32_t*: locommon_msgpack_pack_int32_, \
const uint64_t*: locommon_msgpack_pack_uint64_, \
const float*: locommon_msgpack_pack_float_, \
const bool*: locommon_msgpack_pack_bool_, \
const char*: locommon_msgpack_pack_str_, \
const vec2_t*: locommon_msgpack_pack_vec2_, \
const vec4_t*: locommon_msgpack_pack_vec4_, \
\
const locommon_counter_t*: locommon_counter_pack, \
const locommon_null_t*: locommon_null_pack, \
const locommon_position_t*: locommon_position_pack, \
const locommon_ticker_t*: locommon_ticker_pack, \
\
const loeffect_t*: loeffect_pack, \
const loeffect_generic_immediate_param_t*: loeffect_generic_immediate_param_pack, \
const loeffect_generic_lasting_param_t*: loeffect_generic_lasting_param_pack, \
const loeffect_recipient_effect_param_t*: loeffect_recipient_effect_param_pack, \
const loeffect_stance_set_t*: loeffect_stance_set_pack \
)(v, packer)
#define LOCOMMON_MSGPACK_PACK_ANY(packer, v) \
LOCOMMON_MSGPACK_PACK_ANY_(packer, (const typeof(*v)*) (v))
#define LOCOMMON_MSGPACK_UNPACK_ANY(obj, v) _Generic((v), \
int32_t*: locommon_msgpack_unpack_int32_, \
uint64_t*: locommon_msgpack_unpack_uint64_, \
float*: locommon_msgpack_unpack_float_, \
bool*: locommon_msgpack_unpack_bool_, \
vec2_t*: locommon_msgpack_unpack_vec2_, \
vec4_t*: locommon_msgpack_unpack_vec4_, \
\
locommon_counter_t*: locommon_counter_unpack, \
locommon_null_t*: locommon_null_unpack, \
locommon_position_t*: locommon_position_unpack, \
locommon_ticker_t*: locommon_ticker_unpack, \
\
loeffect_t*: loeffect_unpack, \
loeffect_generic_immediate_param_t*: loeffect_generic_immediate_param_unpack, \
loeffect_generic_lasting_param_t*: loeffect_generic_lasting_param_unpack, \
loeffect_recipient_effect_param_t*: loeffect_recipient_effect_param_unpack, \
loeffect_stance_set_t*: loeffect_stance_set_unpack \
)(v, obj)
static inline void locommon_msgpack_pack_int32_(
const int32_t* v, msgpack_packer* packer) {
msgpack_pack_int32(packer, *v);
}
static inline void locommon_msgpack_pack_uint64_(
const uint64_t* v, msgpack_packer* packer) {
msgpack_pack_uint64(packer, *v);
}
static inline void locommon_msgpack_pack_float_(
const float* v, msgpack_packer* packer) {
msgpack_pack_double(packer, *v);
}
static inline void locommon_msgpack_pack_bool_(
const bool* v, msgpack_packer* packer) {
mpkutil_pack_bool(packer, *v);
}
static inline void locommon_msgpack_pack_str_(
const char* str, msgpack_packer* packer) {
mpkutil_pack_str(packer, str);
}
static inline void locommon_msgpack_pack_vec2_(
const vec2_t* v, msgpack_packer* packer) {
mpkutil_pack_vec2(packer, v);
}
static inline void locommon_msgpack_pack_vec4_(
const vec4_t* v, msgpack_packer* packer) {
mpkutil_pack_vec4(packer, v);
}
static inline bool locommon_msgpack_unpack_int32_(
int32_t* v, const msgpack_object* obj) {
return mpkutil_get_int32(obj, v);
}
static inline bool locommon_msgpack_unpack_uint64_(
uint64_t* v, const msgpack_object* obj) {
return mpkutil_get_uint64(obj, v);
}
static inline bool locommon_msgpack_unpack_float_(
float* v, const msgpack_object* obj) {
return mpkutil_get_float(obj, v);
}
static inline bool locommon_msgpack_unpack_bool_(
bool* v, const msgpack_object* obj) {
return mpkutil_get_bool(obj, v);
}
static inline bool locommon_msgpack_unpack_vec2_(
vec2_t* v, const msgpack_object* obj) {
return mpkutil_get_vec2(obj, v);
}
static inline bool locommon_msgpack_unpack_vec4_(
vec4_t* v, const msgpack_object* obj) {
return mpkutil_get_vec4(obj, v);
}

20
core/locommon/null.c Normal file
View File

@@ -0,0 +1,20 @@
#include "./null.h"
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <msgpack.h>
void locommon_null_pack(const locommon_null_t* null, msgpack_packer* packer) {
assert(null != NULL);
assert(null != NULL);
msgpack_pack_nil(packer);
}
bool locommon_null_unpack(locommon_null_t* null, const msgpack_object* obj) {
assert(null != NULL);
return obj != NULL;
}

20
core/locommon/null.h Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
#include <stdbool.h>
#include <msgpack.h>
/* used in some macro templates */
typedef struct {} locommon_null_t;
void
locommon_null_pack(
const locommon_null_t* null,
msgpack_packer* packer
);
bool
locommon_null_unpack(
locommon_null_t* null,
const msgpack_object* obj
);

94
core/locommon/physics.c Normal file
View File

@@ -0,0 +1,94 @@
#include "./physics.h"
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include "util/coly2d/hittest.h"
#include "util/math/algorithm.h"
#include "util/math/vector.h"
#include "./position.h"
bool locommon_physics_entity_valid(const locommon_physics_entity_t* e) {
return
e != NULL &&
locommon_position_valid(&e->pos) &&
vec2_valid(&e->velocity) &&
vec2_valid(&e->size);
}
bool locommon_physics_solve_collision_with_fixed_one(
locommon_physics_entity_t* e1,
const locommon_physics_entity_t* e2,
float dt) {
assert(locommon_physics_entity_valid(e1));
assert(locommon_physics_entity_valid(e2));
assert(vec2_pow_length(&e2->velocity) == 0);
assert(MATH_FLOAT_VALID(dt));
vec2_t size;
vec2_add(&size, &e1->size, &e2->size);
vec2_t pos;
locommon_position_sub(&pos, &e1->pos, &e2->pos);
pos.x /= size.x;
pos.y /= size.y;
vec2_t velocity = e1->velocity;
velocity.x /= size.x;
velocity.y /= size.y;
vec2_t disp;
vec2_mul(&disp, &velocity, dt);
vec2_t spos;
vec2_sub(&spos, &pos, &disp);
static const vec2_t origin = vec2(0, 0);
static const vec2_t sz = vec2(1, 1);
if (!coly2d_hittest_lineseg_and_rect(&spos, &pos, &origin, &sz)) {
return false;
}
if (MATH_ABS(spos.x) < 1 && MATH_ABS(spos.y) < 1) {
float* f = MATH_ABS(spos.x) > MATH_ABS(spos.y)? &spos.x: &spos.y;
*f = MATH_SIGN(*f);
}
vec2_t vt = vec2(MATH_INF, MATH_INF);
if (velocity.x != 0) {
vt.x = -(MATH_SIGN(velocity.x)+spos.x) / velocity.x;
}
if (velocity.y != 0) {
vt.y = -(MATH_SIGN(velocity.y)+spos.y) / velocity.y;
}
/* ---- simulation ---- */
float t = MATH_MIN(vt.x, vt.y);
if (t < 0) t = MATH_MAX(vt.x, vt.y);
if (t < 0 || t >= dt) return false;
vec2_t d, v = velocity, p = spos;
vec2_mul(&d, &v, t);
vec2_addeq(&p, &d);
if (t == vt.x) v.x = 0;
if (t == vt.y) v.y = 0;
vec2_mul(&d, &v, dt-t);
vec2_addeq(&p, &d);
/* ---- return result ---- */
p.x *= size.x;
p.y *= size.y;
v.x *= size.x;
v.y *= size.y;
e1->pos = e2->pos;
vec2_addeq(&e1->pos.fract, &p);
locommon_position_reduce(&e1->pos);
e1->velocity = v;
return true;
}

28
core/locommon/physics.h Normal file
View File

@@ -0,0 +1,28 @@
#pragma once
#include <stdbool.h>
#include "util/math/vector.h"
#include "./position.h"
typedef struct {
/* input */
vec2_t size;
/* input/output */
locommon_position_t pos;
vec2_t velocity;
} locommon_physics_entity_t;
bool
locommon_physics_entity_valid(
const locommon_physics_entity_t* e
);
bool /* whether they were collided */
locommon_physics_solve_collision_with_fixed_one(
locommon_physics_entity_t* e1,
const locommon_physics_entity_t* e2,
float dt
);

83
core/locommon/position.c Normal file
View File

@@ -0,0 +1,83 @@
#include "./position.h"
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <msgpack.h>
#include "util/math/algorithm.h"
#include "util/math/vector.h"
#include "util/mpkutil/get.h"
#include "util/mpkutil/pack.h"
int32_t locommon_position_get_district_from_chunk(int32_t x) {
static const int32_t cpd = LOCOMMON_POSITION_CHUNKS_PER_DISTRICT;
return x >= 0? x/cpd: x/cpd-1;
}
int32_t locommon_position_get_chunk_index_in_district(int32_t x) {
static const int32_t cpd = LOCOMMON_POSITION_CHUNKS_PER_DISTRICT;
return x >= 0? x%cpd: cpd-1-(-x-1)%cpd;
}
bool locommon_position_valid(const locommon_position_t* a) {
return a != NULL && vec2_valid(&a->fract);
}
void locommon_position_sub(
vec2_t* a, const locommon_position_t* b, const locommon_position_t* c) {
assert(a != NULL);
assert(locommon_position_valid(b));
assert(locommon_position_valid(c));
vec2_sub(a, &b->fract, &c->fract);
a->x += b->chunk.x - c->chunk.x;
a->y += b->chunk.y - c->chunk.y;
}
void locommon_position_reduce(locommon_position_t* a) {
assert(locommon_position_valid(a));
# define reduce_(e) do { \
a->chunk.e += (int) a->fract.e; \
a->fract.e -= (int) a->fract.e; \
if (a->fract.e < 0) { \
--a->chunk.e; \
++a->fract.e; \
} \
} while (0)
reduce_(x);
reduce_(y);
# undef reduce_
}
void locommon_position_pack(
const locommon_position_t* pos, msgpack_packer* packer) {
assert(locommon_position_valid(pos));
assert(packer != NULL);
msgpack_pack_array(packer, 4);
msgpack_pack_int32(packer, pos->chunk.x);
msgpack_pack_int32(packer, pos->chunk.y);
msgpack_pack_double(packer, pos->fract.x);
msgpack_pack_double(packer, pos->fract.y);
}
bool locommon_position_unpack(
locommon_position_t* pos, const msgpack_object* obj) {
assert(pos != NULL);
const msgpack_object_array* root = mpkutil_get_array(obj);
if (root == NULL || root->size != 4 ||
!mpkutil_get_int32(&root->ptr[0], &pos->chunk.x) ||
!mpkutil_get_int32(&root->ptr[1], &pos->chunk.y) ||
!mpkutil_get_float(&root->ptr[2], &pos->fract.x) ||
!mpkutil_get_float(&root->ptr[3], &pos->fract.y)) {
return false;
}
return true;
}

59
core/locommon/position.h Normal file
View File

@@ -0,0 +1,59 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <msgpack.h>
#include "util/math/vector.h"
typedef struct {
struct {
int32_t x, y;
} chunk;
vec2_t fract;
} locommon_position_t;
#define locommon_position(chunk_x, chunk_y, fract) \
((locommon_position_t) { {chunk_x, chunk_y, }, fract })
#define LOCOMMON_POSITION_CHUNKS_PER_DISTRICT 7
int32_t
locommon_position_get_district_from_chunk(
int32_t x
);
int32_t
locommon_position_get_chunk_index_in_district(
int32_t x
);
bool
locommon_position_valid(
const locommon_position_t* a
);
void
locommon_position_sub(
vec2_t* a,
const locommon_position_t* b,
const locommon_position_t* c
);
void
locommon_position_reduce(
locommon_position_t* a
);
void
locommon_position_pack(
const locommon_position_t* pos,
msgpack_packer* packer
);
bool
locommon_position_unpack(
locommon_position_t* pos,
const msgpack_object* obj
);

46
core/locommon/ticker.c Normal file
View File

@@ -0,0 +1,46 @@
#include "./ticker.h"
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <msgpack.h>
#include "util/mpkutil/get.h"
void locommon_ticker_initialize(locommon_ticker_t* ticker, uint64_t time) {
assert(ticker != NULL);
*ticker = (typeof(*ticker)) { .time = time, };
}
void locommon_ticker_deinitialize(locommon_ticker_t* ticker) {
(void) ticker;
}
void locommon_ticker_tick(locommon_ticker_t* ticker, uint64_t time) {
assert(ticker != NULL);
assert(ticker->time <= time);
ticker->delta = time - ticker->time;
ticker->delta_f = ticker->delta*1.f / LOCOMMON_TICKER_UNIT;
ticker->time = time;
}
void locommon_ticker_pack(
const locommon_ticker_t* ticker, msgpack_packer* packer) {
assert(ticker != NULL);
assert(packer != NULL);
msgpack_pack_uint64(packer, ticker->time);
}
bool locommon_ticker_unpack(
locommon_ticker_t* ticker, const msgpack_object* obj) {
assert(ticker != NULL);
if (!mpkutil_get_uint64(obj, &ticker->time)) {
return false;
}
return true;
}

44
core/locommon/ticker.h Normal file
View File

@@ -0,0 +1,44 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <msgpack.h>
#define LOCOMMON_TICKER_UNIT 1000
typedef struct {
uint64_t time;
int64_t delta;
float delta_f;
} locommon_ticker_t;
void
locommon_ticker_initialize(
locommon_ticker_t* ticker,
uint64_t time
);
void
locommon_ticker_deinitialize(
locommon_ticker_t* ticker
);
void
locommon_ticker_tick(
locommon_ticker_t* ticker,
uint64_t time
);
void
locommon_ticker_pack(
const locommon_ticker_t* ticker,
msgpack_packer* packer
);
bool
locommon_ticker_unpack(
locommon_ticker_t* ticker,
const msgpack_object* obj
);