]> git.lizzy.rs Git - dungeon_game.git/blob - plugins/game/game.h
74fea1c0eac4030549859eba3e154854f2d424e7
[dungeon_game.git] / plugins / game / game.h
1 #ifndef _GAME_H_
2 #define _GAME_H_
3
4 #include <stdbool.h>
5 #include <sys/ioctl.h>
6 #include <stddef.h>
7 #include <time.h>
8 #define MAP_HEIGHT 1000
9 #define MAP_WIDTH 1000
10 #define LIGHT 10
11
12 /* Type definitions */
13
14 struct color
15 {
16         unsigned char r, g, b;
17 };
18
19 struct material
20 {
21         bool solid;
22         struct color color;
23 };
24
25 struct node
26 {
27         struct material *material;
28 };
29
30 struct entity_step_data
31 {
32         double dtime;
33         int dx;
34         int dy;
35         bool visible;
36 };
37
38 struct entity
39 {
40         char *name;
41         int x, y;
42         struct color color;
43         bool use_color;
44         char *texture;
45         bool remove;
46         void *meta;
47         int health;
48         int max_health;
49         bool collide_with_entities;
50
51         void (*on_step)(struct entity *self, struct entity_step_data stepdata);
52         void (*on_collide)(struct entity *self, int x, int y);
53         void (*on_collide_with_entity)(struct entity *self, struct entity *other);
54         void (*on_spawn)(struct entity *self, void *data);
55         void (*on_remove)(struct entity *self);
56         void (*on_death)(struct entity *self);
57         void (*on_damage)(struct entity *self, int damage);
58 };
59
60 struct list
61 {
62         void *element;
63         struct list *next;
64 };
65
66 enum mg_context
67 {
68         MG_CTX_CORRIDOR,
69         MG_CTX_ROOM,
70 };
71
72 struct generator_function
73 {
74         int chance;
75         void (*callback)(int x, int y, enum mg_context ctx);
76 };
77
78 struct input_handler
79 {
80         bool run_if_dead;
81         void (*callback)();
82 };
83
84 struct globalstep
85 {
86         bool run_if_dead;
87         void (*callback)(double dtime);
88 };
89
90 enum direction
91 {
92         UP,
93         LEFT,
94         DOWN,
95         RIGHT,
96 };
97
98 extern struct color black;
99
100 extern struct material wall;
101 extern struct material air;
102 extern struct material outside;
103
104 extern struct node map[MAP_WIDTH][MAP_HEIGHT];
105
106 extern struct entity player;
107 extern struct list *entities;
108
109 extern struct entity *entity_collision_map[MAP_WIDTH][MAP_HEIGHT];
110
111 struct color get_color(const char *str);
112 void set_color(struct color color, bool bg);
113 void light_color(struct color *color, double light);
114 void mix_color(struct color *color, struct color other, double ratio);
115 void dir_to_xy(enum direction dir, int *x, int *y);
116 struct list *add_element(struct list *list, void *element);
117 int clamp(int v, int max, int min);
118 int max(int a, int b);
119 int min(int a, int b);
120 void *make_buffer(void *ptr, size_t size);
121 double calculate_dtime(struct timespec from, struct timespec to);
122 void get_roman_numeral(int number, char **ptr, size_t *len);
123
124 void quit();
125 bool player_dead();
126
127 struct node get_node(int x, int y);
128 bool is_outside(int x, int y);
129 bool is_solid(int x, int y);
130
131 bool spawn(struct entity def, int x, int y, void *data);
132 bool move(struct entity *entity, int xoff, int yoff);
133 void add_health(struct entity *entity, int health);
134
135 void register_air_function(struct generator_function func);
136 void register_input_handler(unsigned char c, struct input_handler handler);
137 void register_render_component(void (*callback)(struct winsize ws));
138 void register_globalstep(struct globalstep step);
139
140 #endif