#pragma once
#define _XOPEN_SOURCE 700
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#ifndef GRUG_TYPE_NUMBER
#define GRUG_TYPE_NUMBER double
#endif
#ifndef GRUG_TYPE_BOOL
#define GRUG_TYPE_BOOL bool
#endif
#ifndef GRUG_TYPE_STRING
#define GRUG_TYPE_STRING const char*
#endif
#ifndef GRUG_TYPE_ID
#define GRUG_TYPE_ID uint64_t
#endif
#ifndef GRUG_TYPE_ON_FN_ID
#define GRUG_TYPE_ON_FN_ID uint64_t
#endif
union grug_value {
#ifndef GRUG_NO_NUMBER
GRUG_TYPE_NUMBER _number;
#endif
#ifndef GRUG_NO_BOOL
GRUG_TYPE_BOOL _bool;
#endif
#ifndef GRUG_NO_STRING
GRUG_TYPE_STRING _string;
#endif
#ifndef GRUG_NO_ID
GRUG_TYPE_ID _id;
#endif
};
struct grug_state;
struct grug_file_id;
struct grug_entity_id;
enum grug_type_enum {
GRUG_TYPE_ENUM_VOID,
GRUG_TYPE_ENUM_BOOL,
GRUG_TYPE_ENUM_NUMBER,
GRUG_TYPE_ENUM_STRING,
GRUG_TYPE_ENUM_ID,
GRUG_TYPE_ENUM_RESOURCE,
GRUG_TYPE_ENUM_ENTITY
};
struct grug_type {
uint32_t type;
union {
struct {
char* name;
struct grug_type* generics;
size_t generics_len;
} id;
char* resource_extension;
char* entity_type;
} data;
};
#define CALL_GENERIC(state, game_fn_name, generics, ...) p_game_fn_##game_fn_name((state), (const union grug_value[]){ __VA_ARGS__ }, generics)
#define CALL_ARGLESS_GENERIC(state, game_fn_name, generics) p_game_fn_##game_fn_name((state), NULL, generics)
#define CALL(state, game_fn_name, ...) p_game_fn_##game_fn_name((state), (const union grug_value[]){ __VA_ARGS__ }, NULL)
#define CALL_ARGLESS(state, game_fn_name) p_game_fn_##game_fn_name((state), NULL, NULL)
typedef union grug_value (*game_fn)(struct grug_state* state, const union grug_value[], const struct grug_type[]);
typedef game_fn (*generic_fn_reg)(struct grug_type* types);
static inline union grug_value grug_number(GRUG_TYPE_NUMBER v) { union grug_value r; r._number = v; return r; }
static inline union grug_value grug_bool(GRUG_TYPE_BOOL v) { union grug_value r; r._bool = v; return r; }
static inline union grug_value grug_string(GRUG_TYPE_STRING v) { union grug_value r; r._string = v; return r; }
static inline union grug_value grug_id(GRUG_TYPE_ID v) { union grug_value r; r._id = v; return r; }
static inline union grug_value grug_void(void) { union grug_value r = {0}; return r; }
enum grug_runtime_error_type {
GRUG_ON_FN_STACK_OVERFLOW,
GRUG_ON_FN_TIME_LIMIT_EXCEEDED,
GRUG_ON_FN_GAME_FN_ERROR,
};
typedef struct grug_state* (*create_grug_state_t) (
const char* mod_api_path,
const char* mods_dir,
bool safe_mode
);
typedef char* (*parse_mod_api_t)(const char* mod_api_path);
typedef void (*destroy_grug_state_t)(struct grug_state* state);
typedef struct grug_file_id* (*compile_grug_file_t)(struct grug_state* state, const char* file_path, const char** error_out);
typedef void (*destroy_grug_file_t)(struct grug_state* state, struct grug_file_id* file);
typedef struct grug_entity_id* (*create_entity_t)(struct grug_state* state, struct grug_file_id* file, const char** error_out);
typedef void (*destroy_entity_t)(struct grug_state* state, struct grug_entity_id* entity);
typedef void (*update_t)(struct grug_state* state, const char** error_out);
typedef void (*call_export_fn_t)(struct grug_state* state, struct grug_entity_id* entity, const char* fn_name, const union grug_value* args, size_t args_count);
typedef bool (*grug_to_json_t)(struct grug_state* state, const char *input_grug_buffer, char *output_json_buffer, size_t output_buffer_len);
typedef bool (*json_to_grug_t)(struct grug_state* state, const char *input_json_buffer, char *output_grug_buffer, size_t output_buffer_len);
typedef void (*game_fn_error_t)(struct grug_state* state, const char *message);
struct grug_state_vtable {
parse_mod_api_t parse_mod_api;
create_grug_state_t create_grug_state;
destroy_grug_state_t destroy_grug_state;
compile_grug_file_t compile_grug_file;
destroy_grug_file_t destroy_grug_file;
create_entity_t create_entity;
destroy_entity_t destroy_entity;
update_t update;
call_export_fn_t call_export_fn;
grug_to_json_t grug_to_json;
json_to_grug_t json_to_grug;
game_fn_error_t game_fn_error;
};
void grug_tests_run(const char *tests_dir_path,
const char *mod_api_path,
struct grug_state_vtable state_vtable,
const char *whitelisted_test);
void grug_tests_runtime_error_handler(const char *reason,
enum grug_runtime_error_type type,
const char *on_fn_name,
const char *on_fn_path);
union grug_value game_fn_nothing (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_magic (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_initialize (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_initialize_bool (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_identity (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_max (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_say (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_sin (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_cos (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_mega (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_get_false (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_set_is_happy (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_mega_f32 (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_mega_i32 (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_draw (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_utils (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_assert_state_is_not_null (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_Utils_assert_state_is_not_null (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_blocked_alrm (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_spawn (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_spawn_d (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_has_resource (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_has_entity (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_has_string (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_get_opponent (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_get_os (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_set_d (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_set_opponent (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_motherload (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_motherload_subless (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_offset_32_bit_f32 (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_offset_32_bit_i32 (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_offset_32_bit_string (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_talk (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_get_position (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_set_position (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_cause_game_fn_error (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_Utils_cause_game_fn_error (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_call_on_b_fn (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_call_on_b_fn_number (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_Utils_call_on_b_fn (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_store (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_print_csv (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_retrieve (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_box_number (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_vec_number_new (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_vec_number_push (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_vec_number_pop (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_vec_number_insert (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_box (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_box_get (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_box_set (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
game_fn reg_game_fn_default (struct grug_type* types);
union grug_value game_fn_dict (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_dict_from_vec (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_dict_put (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_make_pair (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_pair_first (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);
union grug_value game_fn_pair_second (struct grug_state* grug_state, const union grug_value args[], const struct grug_type generics[]);