summaryrefslogtreecommitdiff
path: root/src/common.hpp
blob: 54d83f8fe5a0c7d3c29d1fe85cdee202345fa2d6 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
This file is part of Minitrem.

Minitrem is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

Minitrem is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Minitrem.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <iostream>
#include <cinttypes>
#include <cmath>
#include <cassert>
#include <map>
#include <list>
#include <unordered_set>
#include <sstream>
#include <stack>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "math.hpp"

#ifdef __linux
#define CONFIG_SHOW_RSS
#endif

#define COUNT(A) (sizeof(A) / sizeof((A)[0]))

extern bool debug_draw_cmodels;
extern bool debug_draw_paths;
extern bool debug_draw_tile_coords;
extern bool debug_AI;

// time in nanoseconds
// nclock() never returns 0, so it can be used as a null value
typedef uint64_t ntime_t;
ntime_t nclock(void);

#define SEC(x) ((ntime_t)x * 1000000000)
#define MSEC(x) ((ntime_t)x * 1000000)

class freq_counter_t {
	size_t ticks;
	ntime_t t0 = 0;
	float last_reading;

public:
	void tick(void);
	float read(ntime_t ival);
};

extern freq_counter_t fc_render;
extern freq_counter_t fc_game;

extern freq_counter_t fc_traces, fc_trace_tiles, fc_trace_ents;
extern freq_counter_t fc_get_entities, fc_get_entities_ents;

#ifdef CONFIG_SHOW_RSS
size_t sys_get_rss(void);
#endif

namespace procgen {
	class prng_t {
		uint32_t state = 0;

	public:
		void seed(uint32_t seed);
		uint32_t next(void);
		float next_float(void);
		float next_float(float n, float p);
		void unit_vec(float out[2]);
		v2f_t unit_vec2(void);

	};

	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;

	enum {
		LAYER_FLAT,
		LAYER_NORMAL,
		LAYER_TOP,
		LAYER_COUNT
	};

	// Apparently you can't increment an enum type...
	typedef int layer_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}
	};

	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;

	typedef enum {
		SECTOR_EMPTY,
		SECTOR_PARTIAL,
		SECTOR_FULL
	} sector_level_t;

	class sector_t {
	public:
		sector_index_t index;
		rectf_t bounds;
		std::unordered_set<entity_t*> ents;
		sector_level_t level;
		tile_t tiles[SECTOR_SIZE * SECTOR_SIZE];
	};

	typedef int cflags_t;

	typedef struct {
		cflags_t cflags;
		rectf_t bounds;
		bool ignore = false;
	} cmodel_t;

	typedef struct {
		bool hit;
		v2f_t end;
		float frac;

		entity_t *ent = nullptr;
		tile_t *tile = nullptr;
	} trace_t;

	// for testing
	trace_t ray_v_rect(v2f_t start, v2f_t end, rectf_t rect);

	void register_tile(uint8_t type, cflags_t cflags);

	class world_t {
		procgen::prng_t prng;

		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, sector_level_t level);

	protected:
		friend render::state_t;

	public:
		procgen::perlin_noise_t perlin;

		void (*generator)(world_t *world, sector_index_t index, sector_t *sector,
		                  bool gen_tiles, bool gen_decos, void *data) = 0;
		void *generator_data = 0;

		world_t(void);

		sector_t *get_sector(sector_index_t index, sector_level_t level);
		tile_t *get_tile(tile_index_t index, sector_level_t level);

		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, sector_level_t level);
		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(const cmodel_t *cmodel, const entity_t *ignore);

		trace_t point_v_tiles(v2f_t at, cflags_t cflags);
		trace_t ray_v_ents(v2f_t start, v2f_t end, cflags_t cflags, const entity_t *ignore);
		trace_t ray_v_tiles(v2f_t start, v2f_t end, cflags_t cflags);
		trace_t ray_v_all(v2f_t start, v2f_t end, cflags_t cflags, const entity_t *ignore);
		trace_t ray_v_all_p3d(v2f_t start, v2f_t end, cflags_t cflags, cflags_t end_cflags, const entity_t *ignore);

		struct {
			size_t sectors = 0, tiles = 0;
			size_t entities = 0;
		} stats;
	};

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

		void link_to_sector(sector_t *sector);

	protected:
		friend world_t;
		size_t cookie = 0;

	public:
		world_t *world = 0;
		int type;
		cmodel_t cmodel;
		rectf_t render_bounds;
		render::layer_t render_layer = render::LAYER_NORMAL;

		entity_t(int type_);
		virtual ~entity_t(void) = 0;

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

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

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

	class path_finder_t {
		path_node_t *node_at(tile_index_t index);
		bool is_accessible(tile_index_t index);
		bool diagonal_test(tile_index_t index, size_t i);

	public:
		v2f_t src, dst, tile_center;
		rectf_t bounds;
		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 interface {
	class state_t;

	typedef struct {
		std::string label;
		int action;

		float r0, r1;
		float t0, t1;
	} pie_item_t;
}

namespace game {
	bool load_assets(void);

	class entity_t;
	class unit_t;
	class effect_t;

	enum {
		SELECT_NEW,
		SELECT_OR,
		SELECT_XOR
	};

	class state_t {
		void group_say(std::string text);
		bool select_unit(unit_t *unit, int type);

	public:
		world::world_t world;
		interface::state_t *interface;
		procgen::prng_t prng;
		std::unordered_set<entity_t*> awake_entities;
		std::unordered_set<unit_t*> selected_units;
		// Some deletes have to be deferred to the next frame.
		std::unordered_set<entity_t*> deletion_list;

		ntime_t time = 0; // game time
		double now, dt; // FIXME: refactor the code, use ntime_t everywhere

		// frame timing data (game runs at a different frequency than the renderer)
		bool paused;
		ntime_t t0;
		size_t frames = 0, frames_since_t0, frames_behind = 0;

		void start(void);
		void stop(void);
		void tick(ntime_t time);
		void compute_ambience(render::state_t *render);
		void pause(void);
		void resume(void);

		void wake_area(v2f_t x);
		void explosion(v2f_t x);
		void hivemind_alert(v2f_t x, float r, bool do_move, v2f_t move_to);

		// These are called by the interface.
		void select(rectf_t rect, int type);
		bool populate_pie_menu(std::vector<interface::pie_item_t> &items);
		void command(v2f_t x, int number);
	};
}

namespace interface {
	class pie_menu_t {
		v2f_t x, x_world;
		float radius;
		pie_item_t *selected = nullptr;

	protected:
		friend state_t;
		std::vector<pie_item_t> items;

	public:
		bool is_open = false;

		void open(v2f_t wmouse, v2f_t mouse);
		void close(game::state_t *game);
		void update(v2f_t mouse);
		void render_to(render::state_t *render);
	};

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

		float em;

		struct {
			v2f_t zero_point_s = v2f_t(0, 0);
			v2f_t center = v2f_t(0, 0);

			int zoom = 3;
			float zoom_s = 3.0f;

			bool panning = false;
			sf::Vector2f pan_ref;

			bool following = true;
		} camera;

		struct {
			bool selecting = false;
			int type;
			rectf_t rect;
		} select;

		pie_menu_t pie_menu;

		typedef struct {
			double time;
			std::string text;
		} log_entry_t;

		std::list<log_entry_t> log;

		void start_following(void);
		void stop_following(void);

	public:
		v3f_t camera_3d; // for audio
 
		state_t(sf::RenderWindow *window_, game::state_t *game);
		void tick(double dt);
		void render_to(render::state_t *render);

		void print(std::string str);
	};
}

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);
	};

	typedef enum {
		ALIGN_LEFT_TOP,
		ALIGN_LEFT_BOTTOM,
		ALIGN_CENTER_BOTTOM,
		ALIGN_RIGHT_BOTTOM
	} text_align_t;

	void register_tile(uint8_t type, const char *top, const char *side, float height, layer_t layer);
	bool rendering_order(const world::entity_t *x, const world::entity_t *y);
	bool visibility_order(const world::entity_t *x, const world::entity_t *y);

	class state_t {
		sf::RenderWindow *window;

		void render_tile(world::world_t *world, v2f_t tx, world::tile_t *tile);
		void render_layer(game::state_t *game, rect_t<world::coord_t, 2> &sectors, std::list<world::entity_t*> &ents, layer_t layer);
		void drender_text(rectf_t rect, std::string str);
		void drender_entity(world::entity_t *ent);
	public:
		ntime_t time;
		double dt;

		struct {
			size_t sectors, tiles, entities;
		} stats;

		state_t(sf::RenderWindow *window_);
		void begin_frame(ntime_t time_, double dt);
		void end_frame(void);

		void render(game::state_t *game);
		void render(double phase, animated_texture_t *anim, rectf_t bounds, sf::Color color = sf::Color::White, bool mirror = false);
		void render(double phase, oriented_sprite_t *sprite, rectf_t bounds, float angle);

		void render_text(v2f_t x, float height, std::string str, text_align_t align, sf::Color color);
		void render_rect(rectf_t rect, sf::Color color);
		void render_hlrect(rectf_t rect, sf::Color color);
		void render_line(v2f_t x0, v2f_t x1, sf::Color color);
		void render_circle(v2f_t x, float r, sf::Color color);
		void render_ring_sect(v2f_t x, float r0, float r1, float t0, float t1, sf::Color color);

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

		rectf_t window_in_world_space(void);
	};
}

namespace audio {
	void update(v3f_t view, bool paused);

	class sound_t {
		std::vector<sf::SoundBuffer> sounds;

	public:
		float volume = 1.0f;
		void load(const char *path);
		void play(void);
		void play_3d(v2f_t x);
	};

	class ambient_t {
	public:
		sf::Music sound;
		bool playing = false;

		float volume = 1.0f;
		v2f_t origin = v2f_t(0.0f, 0.0f);
		float weight = 0.0f;

		void load(const char *path);
	};
}

extern render::state_t *debug_render;

// 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;
}

// Linear map between intervals.
template <typename T>
T remap(T am, T ap, T bm, T bp, T x)
{
	return (x - am) * (bp - bm) / (ap - am) + bm;
}

template <typename T>
T clamp(T x, T m, T p)
{
	if (x < m)
		return m;
	if (x > p)
		return p;
	return 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);
}

template <typename T>
static inline void expfade(T *a, T b, T l, T dt)
{
	*a = b + (*a - b) * exp(-l * dt);
}

template <typename T, size_t N>
static inline void expfade(vec_t<T, N> *a, vec_t<T, N>  b, float l, float dt)
{
	*a = b + (*a - b) * exp(-l * dt);
}