summaryrefslogtreecommitdiff
path: root/src/game/units.cpp
blob: 65f0a2c17ae1efc401d9f88ea4aed69784a627c1 (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
#include "game.hpp"

namespace game {

world::cmodel_t unit_t::make_cmodel(v2f_t at)
{
	world::cmodel_t cmodel;

	cmodel.bounds[0] = at + size[0];
	cmodel.bounds[1] = at + size[1];
	cmodel.cflags = cflags;

	return cmodel;
}

void unit_t::compute_bounds()
{
	render_bounds[0] = x + render_size[0];
	render_bounds[1] = x + render_size[1];
}

unit_t::unit_t(game::state_t *game_, unit_t::type_t type_) : entity_t(ET_UNIT)
{
	type = type_;
	game = game_;
}

void unit_t::render_to(render::state_t *render)
{
	if (selected == selection_cookie)
		render->render_hlrect(render_bounds, sf::Color::Blue);

	if (!dead && health < max_health)
	{
		rectf_t bar;
		float frac;
		sf::Color color;

		bar[0][0] = (render_bounds[0][0] + render_bounds[1][0]) / 2;
		bar[0][1] = render_bounds[0][1] - 0.05f;
		bar[0][0] -= 0.3f;
		bar[1][0] = bar[0][0] + 0.6f;
		bar[1][1] = bar[0][1] + 0.05f;
		render->render_rect(bar, sf::Color::Black);

		frac = (float)health / max_health;

		if (frac < 0.3f)
			color = sf::Color::Red;
		else if (frac < 0.75f)
			color = sf::Color::Yellow;
		else
			color = sf::Color::Green;

		bar[1][0] = lerp(bar[0][0], bar[1][0], frac);
		render->render_rect(bar, color);
	}

	if (move.moving && debug_draw_paths)
		render->debug_path(&move.path);

	if (say_time + 5.0 > game->now) {
		v2f_t text_pos;
		float height;

		text_pos = render_bounds[0] + v2f_t(render_bounds.dim(0) / 2, -render_bounds.dim(1) * 0.1);
		height = size.dim_min() * 0.20f;
		render->render_text(text_pos, height, say_text,
				    render::ALIGN_CENTER_BOTTOM,
				    sf::Color::White);
	}
}

void unit_t::say(std::string str)
{
	say_text = str;
	say_time = game->now;
	game->interface->print(name + ": " + str);
}

void unit_t::place(world::world_t *world_, v2f_t x_)
{
	world = world_;
	x = x_;
	move.moving = false;

	unlink();
	cmodel = make_cmodel(x);
	compute_bounds();
	link(world);
}

bool unit_t::keep_moving(double speed)
{
	float time;
	bool rv = true;

	if (!move.moving)
		return true;

	if (move.blocked && game->now < move.next_attempt)
		return true;

	time = game->dt * speed;

	while (time > 0.0f) {
		v2f_t delta, next, x_new;
		world::cmodel_t cmodel_next;

		if (!move.path.size()) {
			move.moving = false;
			break;
		}

		next = *move.path.begin();
		delta = next - x;

		if (delta.len() != 0.0f)
			move.angle = delta.angle();

		if (delta.len() >= time) {
			x_new = x + delta * time / delta.len();
			time -= delta.len();
		} else {
			x_new = next;
			time -= delta.len();
			move.path.pop_front();
		}

		cmodel_next = make_cmodel(x_new);
		if (!world->test_rect(&cmodel_next, this)) {
			x = x_new;
			cmodel = cmodel_next;
			continue;
		}

		if (move.attempts_left) {
			move.blocked = true;
			move.attempts_left--;
			move.next_attempt = game->now + 0.2f;
		} else {
			if ((x - move.dst).len() > 1.5f)
				rv = false;
			move.moving = false;
		}
		break;
	}

	unlink();
	compute_bounds();
	link(world);
	return rv;
}

bool unit_t::start_moving(v2f_t dst, world::cflags_t cflags)
{
	world::cmodel_t rep;

	if (!world) {
		printf("unit_t::start_moving: entity is not linked\n");
		return false;
	}

	move.dst = dst;
	move.path.clear();

	rep.cflags = cflags;
	rep.bounds = cmodel.bounds;

	if (!world->find_path(x, move.dst, &rep, this, &move.path)) {
		move.moving = false;
		return false;
	}

	move.moving = true;

	move.blocked = false;
	move.attempts_left = 10;
	move.next_attempt = -INFINITY;
	return true;
}

void unit_t::damage(int points, unit_t *attacker)
{
	fx_blood_t *blood;

	blood = new fx_blood_t(game, x, type == UNIT_ALIEN);
	game->add_effect(blood);

	health -= points;
	if (health < 0) {
		game->interface->print(name + " " + text::get(text::UNIT_DEATH) + ".");
		dead = true;
		death_time = game->now;
		cflags = 0;
		die();
	}
}

void unit_t::try_attack(unit_t *target)
{
	std::stringstream ss;
	size_t hit_roll;
	size_t dmg_roll;

	ss << name << " " << text::get(text::UNIT_ATTACK) << " " << target->name << ": ";

	hit_roll = game->roll(&cs.hit_roll);
	ss << hit_roll << " vs " << target->cs.armor_class;

	if (hit_roll == 1 || hit_roll < target->cs.armor_class) {
		ss << " (" << text::get((hit_roll == 1 ? text::UNIT_CRITICAL_MISS : text::UNIT_MISS)) << ")";
		game->interface->print(ss.str());
		return;
	}

	dmg_roll = game->roll(&cs.damage_roll);

	if (hit_roll < 20) {
		ss << ", " << text::get(text::UNIT_DAMAGE) << ": ";
		ss << dmg_roll - cs.damage_roll.bonus << " + " << cs.damage_roll.bonus << " = " << dmg_roll;
	} else {
		ss << " (" << text::get(text::UNIT_CRITICAL_HIT) << ")" << ", " << text::get(text::UNIT_DAMAGE) << ": ";
		ss << "(" << dmg_roll - cs.damage_roll.bonus << " + " << cs.damage_roll.bonus << ") x 2 = " << dmg_roll * 2;
		dmg_roll *= 2;
	}

	game->interface->print(ss.str());
	target->damage(dmg_roll, this);
}

static unit_t *find_target(world::world_t *world, v2f_t x, float r,
                           unit_t::type_t type)
{
	rectf_t rect;
	float nearest_dist = INFINITY;
	unit_t *nearest = NULL;

	rect[0] = x - v2f_t(r, r);
	rect[1] = x + v2f_t(r, r);

	for (world::entity_t *ent : world->get_entities(rect, -1)) {
		unit_t *unit;
		float dist;

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

		unit = (unit_t*)ent;

		if (unit->type != type)
			continue;

		if (unit->dead)
			continue;

		dist = (unit->x - x).len();
		if (dist < nearest_dist) {
			nearest_dist = dist;
			nearest = unit;
		}
	}

	return nearest;
}

human_t::human_t(game::state_t *game) : unit_t(game, UNIT_HUMAN)
{
	cflags = CF_BODY;
	health = max_health = 20;
	size[0] = v2f_t(-0.4f, -0.4f);
	size[1] = v2f_t(+0.4f, +0.4f);
	render_size[0] = v2f_t(-0.5f, -1.0f);
	render_size[1] = v2f_t(+0.5f, +0.5f);
	name = text::get(text::UNIT_HUMAN);

	cs.armor_class = 10;
	cs.hit_roll = roll_params_t(20);
	cs.damage_roll = roll_params_t(4);
}

void human_t::wake(unit_t *by_whom)
{
}

void human_t::sleep(void)
{
}

void human_t::think(void)
{
	if (game->now > next_targetting) {
		unit_t *target;

		target = find_target(world, x, 10.0f, UNIT_ALIEN);
		if (target) {
			last_target_time = game->now;
			last_target_x = target->x;

			if (last_attack + 1.5 < game->now) {
				world::trace_t trace;
				fx_tracer_t *tracer;

				tracer = new fx_tracer_t(game, x, target->x);
				game->add_effect(tracer);
				std::cout << tracer->cmodel.bounds << std::endl;

				trace = world->trace(x, target->x, CF_SOLID);
				if (!trace.hit) {
					last_attack = game->now;
					try_attack(target);
				}
			}

			//start_moving(target->x, CF_SOLID);
		}

		next_targetting = game->now + 0.2;
	}

	if (!keep_moving(4.0))
		say(text::get(text::SAY_BLOCKED));
}

void human_t::die(void)
{
	render_size[0] = v2f_t(-0.75f, -0.5f);
	render_size[1] = v2f_t(+0.75f, +0.5f);
	render_layer = -1;
	cmodel.cflags = CF_BACKGROUND;
	unlink();
	compute_bounds();
	link(world);
}

void human_t::render_to(render::state_t *render)
{
	if (!dead) {
		render::oriented_sprite_t *legs, *body;
		float body_angle;

		if (move.moving && !move.blocked)
			legs = &assets::human.legs_walking;
		else
			legs = &assets::human.legs_idle;

		if (last_target_time + 3 > game->now) {
			if (last_attack + 0.1 > game->now)
				body = &assets::human.body_firing;
			else
				body = &assets::human.body_aiming;

			body_angle = (last_target_x - x).angle();

		} else {
			body = &assets::human.body_idle;
			body_angle = move.angle;
		}

		render->render(legs, render_bounds, move.angle);
		render->render(body, render_bounds, body_angle);
		render->render(&assets::human.head_idle, render_bounds, body_angle);
	} else
		render->render(&assets::human.dead, render_bounds);

	unit_t::render_to(render);
}

alien_t::alien_t(game::state_t *game) : unit_t(game, UNIT_ALIEN)
{
	cflags = CF_BODY_SMALL;
	health = max_health = 4;
	size[0] = v2f_t(-0.2f, -0.2f);
	size[1] = v2f_t(+0.2f, +0.2f);
	render_size[0] = v2f_t(-0.3f, -0.3f);
	render_size[1] = v2f_t(+0.3f, +0.3f);
	name = text::get(text::UNIT_ALIEN);

	cs.armor_class = 5;
	cs.hit_roll = roll_params_t(20);
	cs.damage_roll = roll_params_t(3, 6, 1);
}

void alien_t::wake(unit_t *by_whom)
{
	start_moving(by_whom->x, CF_SOLID);
	next_targetting = game->now + 0.2;
}

void alien_t::sleep(void)
{
}

void alien_t::die(void)
{
	render_layer = -1;
	cmodel.cflags = CF_BACKGROUND;
}

void alien_t::think(void)
{
	if (game->now > next_targetting) {
		unit_t *target;

		target = find_target(world, x, 10.0f, UNIT_HUMAN);
		if (target) {
			if (last_attack + 0.5 < game->now) {
				world::trace_t trace;

				if ((x - target->x).len() < 1.0f) {
					trace = world->trace(x, target->x, CF_SOLID);
					if (!trace.hit)
						try_attack(target);

					last_attack = game->now;
				}
			}

			start_moving(target->x, CF_SOLID);
		}

		next_targetting = game->now + 0.2;
	}

	keep_moving(7.0);
}

void alien_t::render_to(render::state_t *render)
{
	bool moving;

	moving = move.moving && !move.blocked;

	if (!dead)
		render->render((moving ? &assets::alien.walking :
		               &assets::alien.idle), render_bounds, move.angle);
	else
		render->render(&assets::alien.dead, render_bounds);

	unit_t::render_to(render);
}

} // namespace game