summaryrefslogtreecommitdiff
path: root/src/common.hpp
blob: b7ce2182d9093d30baa8287effdcd78bb0ff2321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <iostream>
#include <cinttypes>
#include <cmath>
#include <cassert>
#include <map>
#include <list>
#include <unordered_set>
#include <stack>
#include <SFML/Graphics.hpp>
#include "math.hpp"

namespace procgen {
	class prng_t {
		uint32_t state = 0;

	public:
		void seed(uint32_t seed);
		uint32_t next(void);
		float next_float(void);
		void unit_vec(float out[2]);

	};

	class perlin_noise_t {
		size_t size;
		float (*table)[2] = nullptr;

		float table_dot(size_t nx, size_t ny, float dx, float dy);

	public:
		~perlin_noise_t();

		void generate(prng_t *prng, size_t size);
		float get(v2f_t x, float scale);
	};
}

namespace render { class state_t; }

namespace world {
	#define SECTOR_SIZE 8

	typedef int64_t coord_t;
	typedef vec_t<coord_t, 2> sector_index_t;
	typedef vec_t<coord_t, 2> tile_index_t;

	static tile_index_t neighbor_offsets[8] = {
		{+1, 0}, {+1, +1}, {0, +1}, {-1, +1},
		{-1, 0}, {-1, -1}, {0, -1}, {+1, -1}
	};

	enum {
		TILE_NONE,
		TILE_DIRT,
		TILE_WALL
	};

	class tile_t {
	public:
		unsigned type : 8;
		unsigned neighbors : 8;
		unsigned variant : 8;
	};

	sector_index_t sector_index_at(v2f_t x);
	tile_index_t tile_index_at(v2f_t x);

	class entity_t;

	class sector_t {
	public:
		sector_index_t index;
		rectf_t bounds;
		std::unordered_set<entity_t*> ents;

		bool empty = true;
		tile_t tiles[SECTOR_SIZE * SECTOR_SIZE];
	};

	typedef int cflags_t;

	typedef struct {
		cflags_t cflags;
		rectf_t bounds;
	} cmodel_t;

	class world_t {
		procgen::prng_t prng;
		procgen::perlin_noise_t perlin;

		std::map<sector_index_t, sector_t> sectors;

		void generate_tile(tile_t *tile, tile_index_t index);
		void generate(sector_t *sector, sector_index_t index, bool partial);

	protected:
		friend render::state_t;
		typedef struct {
			v2f_t x;
			std::string text;
		} debug_t;
		std::list<debug_t> debug;

	public:
		world_t(void);
		sector_t *get_sector(sector_index_t index, bool partial = false);
		tile_t *get_tile(tile_index_t index, bool partial = false);

		bool find_path(v2f_t src, v2f_t dst, cmodel_t *cmodel, entity_t *ignore, std::list<v2f_t> *path);

		// FIXME: iterators instead of returning std::lists
		std::list<sector_t*> get_sectors(rectf_t rect);
		std::list<entity_t*> get_entities(rectf_t rect, cflags_t cflags);
		std::list<entity_t*> get_render_entities(rectf_t rect);

		bool test_rect(rectf_t rect, cflags_t cflags);

		void debug_point(sf::Vector2f point);
	};

	class entity_t {
		world_t *parent_world;
		std::vector<sector_t*> parents;

		void link_to_sector(sector_t *sector);

	protected:
		friend world_t;
		size_t cookie = 0;

	public:
		cmodel_t cmodel;
		rectf_t render_bounds;

		void link(world_t *world);
		void unlink();

		virtual void render_to(render::state_t *render) = 0;
	};

	typedef struct {
		bool accessible;
		float dist;
	} path_node_t;

	class path_finder_t {
	public:
		v2f_t src, dst, tile_center;
		cflags_t cflags;
		path_node_t *nodes;
		size_t width, height;
		tile_index_t base;

		float shortest_dist;
		std::deque<tile_index_t> path, shortest_path;

		~path_finder_t();
		void setup_nodes(v2f_t src_, v2f_t dst_, cflags_t cflags_);
		void eliminate_nodes(rectf_t rect);
		void find_r(tile_index_t index, float dist, float limit);
		bool find(void);
		void export_path(std::list<v2f_t> *list);
	};
}

namespace game {
	bool load_assets(void);

	class state_t {
	public:
		world::world_t world;

		void start(void);
		void tick(double now);

		void debug_click(v2f_t x);
	};
}

namespace interface {
	class state_t {
		sf::RenderWindow *window;
		game::state_t *game;

		struct {
			sf::Vector2f center;
			int target_zoom = 3;
			float zoom = 3.0f;
			bool dragging = false;
			sf::Vector2f drag_ref;
		} camera;

	public:
		state_t(sf::RenderWindow *window_, game::state_t *game);
		void tick(void);
		void render(void);
	};
}

namespace render {
	class animated_texture_t {
		friend state_t;
	protected:
		sf::Texture *frames = NULL;
		size_t frame_count;

	public:
		~animated_texture_t(void);
		bool load(std::string prefix, size_t frame_count_);
	};

	class oriented_sprite_t {
	protected:
		friend state_t;
		animated_texture_t *textures;
		virtual size_t select_index(float angle, bool *mirror) = 0;

	public:
		~oriented_sprite_t(void);
	};

	class oriented_sprite_4M_t : public oriented_sprite_t {
		size_t select_index(float angle, bool *mirror);

	public:
		void load(std::string prefix, size_t xc, size_t yc, size_t nyc);
	};

	class oriented_sprite_4M2_t : public oriented_sprite_t {
		size_t select_index(float angle, bool *mirror);

	public:
		void load(std::string prefix, size_t xc, size_t yc);
	};

	class state_t {
		sf::RenderWindow *window;
		double now;
	public:

		state_t(sf::RenderWindow *window_);
		void begin_frame(double time_);
		void end_frame(void);
		void render(game::state_t *game);
		void render(animated_texture_t *anim, rectf_t bounds, bool mirror = false);
		void render(oriented_sprite_t *sprite, rectf_t bounds, float angle);

		void debug_path(std::list<v2f_t> *path);
	};
}

namespace assets {
	typedef struct {
		render::oriented_sprite_4M_t head_idle, body_idle;
		render::oriented_sprite_4M2_t legs_idle, legs_walking;
	} human_assets_t;

	extern human_assets_t human;
	extern sf::Texture tile_dirt;
	extern sf::Texture tile_wall;

	void load(void);
};

// Divide and round to minus infinity.
template <typename T>
T divide_rmi(T x, T y, T *rem)
{
	T rv;

	if (x >= 0) {
		*rem = x % y;
		return x / y;
	}

	rv = (x + 1) / y - 1;
	*rem = x - rv * y;
	return rv;
}

// Linear interpolation.
template <typename T>
T lerp(T a, T b, T x)
{
	return a * (1 - x) + b * x;
}

// Bilinear interpolation.
template <typename T>
T bilerp(T a, T b, T c, T d, T x, T y)
{
	T ab, cd;

	ab = lerp(a, b, x);
	cd = lerp(c, d, x);
	return lerp(ab, cd, y);
}