summaryrefslogtreecommitdiff
path: root/sim/src/ai.cpp
blob: 7a0f6375bf7d196e1ec69ae398dc42ec50c204f7 (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
#include "ai.h"

#include <algorithm>
#include <utility>

#include <entt/entt.hpp>

#include <kurator/sim/components.h>
#include <kurator/sim/State.h>
#include <kurator/sim/weapons.h>

#include "TeamManager.h"


namespace kurator
{
namespace sim
{


static double find_worst_range(State& ctx, entt::entity ship);


class KeepAtRange
{
public:
	KeepAtRange(State& ctx_, AIShip& ai_, entt::entity self_, entt::entity target_, double distance_) :
		ctx {ctx_},
		ai {ai_},
		self {std::move(self_)},
		target {std::move(target_)},
		distance {distance_}
	{
	}

	void operator()()
	{
		if (!ctx.registry.valid(target)) {
			if (ctx.registry.valid(ai.target))
				ai.action = KeepAtRange(ctx, ai, self, ai.target, distance);
			return;
		}
		const auto here = ctx.registry.get<Transform>(self).position;
		const auto there = ctx.registry.get<Transform>(target).position;
		const auto offset = there - here;
		ai.destination = there - offset.normalized().scale(distance);
	}
private:
	State& ctx;
	AIShip& ai;
	entt::entity self;
	entt::entity target;
	double distance;
};


void
pick_random_targets(State& ctx, TeamManager& manager)
{
	auto view = ctx.registry.view<Team, AIShip>();
	for (auto&& [entity, team, ai] : view.each()) {
		if (!ctx.registry.valid(ai.target))
			ai.target = manager.random(team.id);
		if (!ai.action)
			ai.action = KeepAtRange(ctx, ai, entity, ai.target, find_worst_range(ctx, entity));
	}
}


void
take_actions(State& ctx)
{
	auto view = ctx.registry.view<AIShip>();
	for (auto&& [_, ai] : view.each()) {
		if (ai.action)
			ai.action();
	}
}


void
shoot_at_targets(State& ctx)
{
	auto view = ctx.registry.view<Turret>();
	for (auto&& [entity, turret] : view.each()) {
		if (!ctx.registry.all_of<AIShip, Transform>(turret.owner))
			continue;
		const auto& [state, transform] = ctx.registry.get<AIShip, Transform>(turret.owner);
		if (!ctx.registry.valid(state.target))
			continue;
		const auto& target = ctx.registry.get<Transform>(state.target);
		const auto distance = transform.position.distance(target.position);
		if (distance <= turret.type.effective_range())
			turret.shoot_at(ctx, state.target, distance);  // passing distance here is wrong
	}
}


double
find_worst_range(State& ctx, entt::entity ship)
{
	double range = 20000.0;
	auto turrets = ctx.registry.view<Turret>();
	for (const auto& [_, turret] : turrets.each()) {
		if (turret.owner == ship)
			range = std::min(range, turret.type.optimal_range);
	}
	return range;
}


}  // namespace sim
}  // namespace kurator