summaryrefslogtreecommitdiff
path: root/src/game/game.cpp
blob: 72aabf26c18608c64d2c9f14359b1245425816ad (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
/*
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 "game.hpp"

namespace game {

bool load_assets(void)
{
	assets::load();
	return true;
}

size_t selection_cookie = 1;

entity_t::entity_t(game::state_t *game_, int type_) : world::entity_t(type_)
{
	game = game_;
}

entity_t::~entity_t(void)
{
	sleep();
	unlink();
}

void entity_t::place(world::world_t *world_)
{
	bool do_spawn = false;

	if (!world)
		do_spawn = true;

	link(world_);

	if (do_spawn)
		on_spawn();
}

void entity_t::place(world::world_t *world, v2f_t x_)
{
	x = x_;
	cmodel.bounds = size + x;
	render_bounds = render_size + x;

	place(world);
}

void entity_t::wake(void)
{
	awake = true;
	wake_time = game->now;
	game->awake_entities.insert(this);

	if (!ignore_waking)
		on_wake();
}

void entity_t::sleep(void)
{
	awake = false;
	game->awake_entities.erase(this);
}

void state_t::start(void)
{
	unit_soldier_t *soldier;
	unit_repl_t *repl;

	world.generator = worldgen;
	world.generator_data = (void*)this;

	soldier = new unit_soldier_t(this);
	soldier->place(&world, v2f_t(0.5, 0.5));
	repl = new unit_repl_t(this);
	repl->place(&world, v2f_t(1.5, 0.5));

	resume();
}

void state_t::stop(void)
{
}

bool state_t::select_unit(unit_t *unit, int type)
{
	switch (type) {
	case SELECT_NEW:
	case SELECT_OR:
		if (unit->selected == selection_cookie)
			return false;
		else
			goto do_select;

	case SELECT_XOR:
		if (unit->selected == selection_cookie)
			goto do_deselect;
		else
			goto do_select;
	}

do_select:
	unit->selected = selection_cookie;
	selected_units.insert(unit);
	return true;

do_deselect:
	unit->selected = selection_cookie - 1;
	selected_units.erase(unit);
	return false;
}

void state_t::select(rectf_t rect, int type)
{
	bool select_one;
	std::list<world::entity_t*> ents;

	if (type == SELECT_NEW) {
		selection_cookie++;
		selected_units.clear();
	}

	select_one = rect.area() < 0.2f;

	ents = world.get_render_entities(rect);
	if (select_one)
		ents.sort(render::visibility_order);

	for (world::entity_t *ent : ents) {
		unit_t *unit;

		if (ent->type != ET_UNIT)
			continue;

		unit = (unit_t*)ent;

		if (!unit->controllable)
			continue;

		if (select_unit(unit, type)) {
			if (unit->type == unit_t::UNIT_SOLDIER)
				unit->say(text::get(text::SAY_READY), false);
		}

		if (select_one)
			break;
	}
}

enum {
	COMMAND_MOVE,
	COMMAND_FIRE,
	COMMAND_FIRE_ROCKET,
	COMMAND_STOP,

	COMMAND_REPL,
};

bool state_t::populate_pie_menu(std::vector<interface::pie_item_t> &items)
{
	bool soldiers = false, repls = false, rockets = false;

	items.clear();

	if (selected_units.size() == 0)
		return false;

	for (unit_t *unit : selected_units) {
		if (unit->dead || !unit->controllable)
			continue;

		switch (unit->type) {
		case unit_t::UNIT_SOLDIER:
			soldiers = true;
			if (!dynamic_cast<unit_soldier_t*>(unit)->rocket_fired)
				rockets = true;
			break;
		case unit_t::UNIT_REPL:
			repls = true;
			break;

		default:;
		}
	}

	if (soldiers) {
		items.push_back((interface::pie_item_t){"Move", COMMAND_MOVE});
		items.push_back((interface::pie_item_t){"Fire", COMMAND_FIRE});
		if (rockets)
			items.push_back((interface::pie_item_t){"Fire a rocket", COMMAND_FIRE_ROCKET});
		items.push_back((interface::pie_item_t){"Stop", COMMAND_STOP});
	}

	if (repls) {
		items.push_back((interface::pie_item_t){"Replicate", COMMAND_REPL});
	}

	return true;
}

static void command_soldier(unit_soldier_t *soldier, v2f_t x, int number)
{
	switch (number) {
	case COMMAND_MOVE:
		if (!soldier->start_moving(x))
			soldier->say(text::get(text::SAY_NO_PATH), false);
		else {
			soldier->move_marker = std::make_unique<fx_move_marker_t>(soldier->game, soldier->move.path.back());
			soldier->say(text::get(text::SAY_MOVING), false);
		}
		break;

	case COMMAND_STOP:
		soldier->stop_moving();
		soldier->manual_firing = false;
		soldier->say(text::get(text::SAY_STOPPING), false);
		soldier->aim_marker.reset();
		break;

	case COMMAND_FIRE:
		soldier->manual_firing = true;
		soldier->manual_firing_target = x;
		soldier->say(text::get(text::SAY_FIRING), false);
		break;

	case COMMAND_FIRE_ROCKET:
		{
			v2f_t from, v;
			rocket_t *rocket;

			if (soldier->rocket_fired) {
				soldier->say(text::get(text::SAY_NO_ROCKETS));
				break;
			}

			soldier->rocket_fired = true;

			from = soldier->x + v2f_t(0, -0.5);
			rocket = new rocket_t(soldier->game, from, x, soldier);
			rocket->place(&soldier->game->world, from);
		}
	}
}

static void command_repl(unit_repl_t *repl, v2f_t x, int number)
{
	switch (number) {
	case COMMAND_REPL:
		repl->activate();
		break;
	}
}

void state_t::command(v2f_t x, int number)
{
	bool unlink;

	if (!selected_units.size())
		return;

	unlink = (number == COMMAND_MOVE);
	if (unlink)
		for (unit_t *unit : selected_units)
			unit->cmodel.ignore = true;

	for (unit_t *unit : selected_units) {
		if (unit->dead || !unit->controllable)
			continue;

		switch (unit->type) {
		case unit_t::UNIT_SOLDIER:
			command_soldier(dynamic_cast<unit_soldier_t*>(unit),
			                x, number);
			break;

		case unit_t::UNIT_REPL:
			command_repl(dynamic_cast<unit_repl_t*>(unit),
			             x, number);
			break;

		default:;
		}
	}

	if (unlink)
		for (unit_t *unit : selected_units)
			unit->cmodel.ignore = false;
}

void state_t::pause(void)
{
	paused = true;
}

void state_t::resume(void)
{
	t0 = nclock();
	frames_since_t0 = 0;

	paused = false;
}

#define TIME_DELTA ((ntime_t)1000000000 / 100)
#define TIME_LIMIT ((ntime_t)1000000000 / 20)

void state_t::tick(ntime_t time_)
{
	size_t target;

	if (paused)
		return;

	target = (time_ - t0) / TIME_DELTA;

	while (frames_since_t0 < target) {
		// FIXME: Is this non-deterministic enough?
		prng.seed(prng.next() ^ time);

		// setting up old variables (refactor them out eventually)
		now = time * 1.0e-9;
		dt = TIME_DELTA * 1.0e-9;

		for (entity_t *ent : deletion_list)
			delete ent;
		deletion_list.clear();

		// on_think can insert/erase elements of awake_entities so iterate
		// over a copy of it.
		auto copy = awake_entities;
		for (entity_t *ent : copy)
			if (awake_entities.find(ent) != awake_entities.end())
				ent->on_think();

		frames++;
		frames_since_t0++;
		time += TIME_DELTA;

		fc_game.tick();

		if (nclock() - time_ > TIME_LIMIT) {
			size_t left;

			left = target - frames_since_t0;
			t0 = nclock();
			frames_since_t0 = 0;
			frames_behind++;

			interface->print("(Lag: " + std::to_string(left) + ")");

			break;
		}
	}
}

#define XRES 9
#define YRES 9

void state_t::compute_ambience(render::state_t *render)
{
	const size_t samples = XRES * YRES;
	rectf_t area;
	v2f_t origins[AMBIENT_COUNT];
	size_t hits[AMBIENT_COUNT];

	area = render->window_in_world_space();

	for (size_t i = 0; i < AMBIENT_COUNT; i++) {
		origins[i] = v2f_t(0, 0);
		hits[i] = 0;
	}

	// resolution chosen arbitrarily
	for (size_t y = 0; y < YRES; y++)
	for (size_t x = 0; x < XRES; x++) {
		v2f_t point;
		world::tile_t *tile;
		int type;

		point[0] = lerp(area[0][0], area[1][0], y / (YRES - 1.0f));
		point[1] = lerp(area[0][1], area[1][1], x / (XRES - 1.0f));

		tile = world.get_tile(world::tile_index_t(point), world::SECTOR_FULL);

		switch (tile->type) {
		case TILE_DIRT:
			type = AMBIENT_WIND;
			break;

		case TILE_GRAVEL:
		case TILE_STONE:
			type = AMBIENT_CHASM;
			break;

		case TILE_WATER:
			type = AMBIENT_WATER;
			break;

		case TILE_DIRT_RED:
		case TILE_STONE_RED:
			type = AMBIENT_NEXUS;
			break;

		default:
			continue;
		}

		origins[type] += point;
		hits[type]++;
	}

	for (size_t i = 0; i < AMBIENT_COUNT; i++) {
		assets::ambients[i].origin = origins[i] / hits[i];
		assets::ambients[i].weight = (float)hits[i] / samples;
	}
}

void state_t::explosion(v2f_t x)
{
	fx_explosion_t *explosion;

	explosion = new fx_explosion_t(this, x);
	explosion->place(&world);

	for (size_t i = 0; i < 60; i++) {
		v2f_t end;
		world::trace_t trace;
		fx_tracer_t *tracer;
		entity_t *ent;
		int damage;

		end = x + prng.unit_vec2() * 6.0f;
		trace = world.ray_v_all(x, end, CF_SURFACE|CF_SOLID|CF_BODY|CF_BODY_SMALL|CF_DECOS, nullptr);

		tracer = new fx_tracer_t(this, x, trace.end);
		tracer->place(&world);

		if (!trace.hit)
			continue;

		if (!trace.ent)
			continue;

		ent = dynamic_cast<entity_t*>(trace.ent);
		damage = pow(1 - trace.frac, 2.0f) * 20.0f;
		ent->damage(damage, nullptr);
	}
}

} //namespace game