-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.h
81 lines (72 loc) · 2.35 KB
/
AI.h
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
#pragma once
// This can be solved much more elegantly using good ol' polymorphism...
// But our master Johnny would not allocate and so shan't we!
// In honor to our master, we waste a bit of memory in order to not touch the heap!
//
enum class AIType
{
JimmyLeet,
Bouncy,
};
// Must NOT contain non-trivial types. Will be copied inside an union.
class JimmyLeetAI
{
flaot time_elapsed;
int seed;
int last_xo;
int last_yo;
public:
JimmyLeetAI() = default;
JimmyLeetAI(int seed) : seed(seed), time_elapsed(0), last_xo(0), last_yo(0) {}
// I could make them virtual but that would be one additional indirection! Never!
void Advance(float delta, class Renderable& entity);
void OnCollide(class Renderable& source, const class Entity& target){};
};
class BouncyAI
{
flaot time_elapsed;
/*
* Space for 12 more bytes!
*/
public:
void Advance(float delta, class Renderable& entity);
static void OnCollide(class Renderable& source, const class Entity& target);
};
class SyntheticIntelligence
{
AIType type;
union ai_union_t
{ // Screw polymorphism, unite into an union!
// Enter the fascinating world of low-level direct memory accesses
JimmyLeetAI johnny;
BouncyAI bouncy;
} ai_union;
public:
SyntheticIntelligence(JimmyLeetAI&& ai) : type(AIType::JimmyLeet) { ai_union.johnny = ai; } //, ai_union{ .johnny = ai } /* only in C++20, let's not require it */
SyntheticIntelligence(BouncyAI&& ai) : type(AIType::Bouncy) { ai_union.bouncy = ai; }
// So much better than a v-table call!
// And totally unextendable to more functions!
// And totally uncomposable!
void Advance(float delta, class Renderable& invader)
{
switch (type)
{
case AIType::JimmyLeet: ai_union.johnny.Advance(delta, invader); break;
case AIType::Bouncy: ai_union.bouncy.Advance(delta, invader); break;
default: throw std::exception("Missing case");
}
}
// One more, I'm sorry
void OnCollide(class Renderable& source, const class Entity& target)
{
switch (type)
{
case AIType::JimmyLeet: ai_union.johnny.OnCollide(source, target); break;
case AIType::Bouncy: ai_union.bouncy.OnCollide(source, target); break;
default: throw std::exception("Missing case");
}
}
// Ok this little experiment...is probably not that viable :)
// but saved that one dynamic allocation!
// Simulating virtual dispatches is so much better with actual virtual dispatches...
};