]> git.localhorst.tv Git - blank.git/blob - src/ai/Spawner.cpp
special treatment for players
[blank.git] / src / ai / Spawner.cpp
1 #include "Spawner.hpp"
2
3 #include "Chaser.hpp"
4 #include "RandomWalk.hpp"
5 #include "../model/shapes.hpp"
6 #include "../world/BlockLookup.hpp"
7 #include "../world/BlockType.hpp"
8 #include "../world/Entity.hpp"
9 #include "../world/World.hpp"
10
11
12 namespace blank {
13
14 Spawner::Spawner(World &world, std::uint64_t seed)
15 : world(world)
16 , controllers()
17 , random(seed)
18 , timer(64)
19 , despawn_range(128 * 128)
20 , spawn_distance(16 * 16)
21 , max_entities(16)
22 , chunk_range(4) {
23         EntityModel::Buffer buf;
24         {
25                 AABB bounds{{ -0.25f, -0.5f, -0.25f }, { 0.25f, 0.5f, 0.25f }};
26                 CuboidShape shape(bounds);
27                 shape.Vertices(buf, 1.0f);
28                 buf.colors.resize(shape.VertexCount(), { 1.0f, 1.0f, 0.0f });
29                 models[0].Update(buf);
30                 skeletons[0].Bounds(bounds);
31                 skeletons[0].SetNodeModel(&models[0]);
32         }
33         {
34                 AABB bounds{{ -0.5f, -0.25f, -0.5f }, { 0.5f, 0.25f, 0.5f }};
35                 CuboidShape shape(bounds);
36                 buf.Clear();
37                 shape.Vertices(buf, 2.0f);
38                 buf.colors.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
39                 models[1].Update(buf);
40                 skeletons[1].Bounds(bounds);
41                 skeletons[1].SetNodeModel(&models[1]);
42         }
43         {
44                 AABB bounds{{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }};
45                 StairShape shape(bounds, { 0.4f, 0.4f });
46                 buf.Clear();
47                 shape.Vertices(buf, 3.0f);
48                 buf.colors.resize(shape.VertexCount(), { 1.0f, 0.0f, 1.0f });
49                 models[2].Update(buf);
50                 skeletons[2].Bounds(bounds);
51                 skeletons[2].SetNodeModel(&models[2]);
52         }
53
54         timer.Start();
55 }
56
57 Spawner::~Spawner() {
58         for (auto &ctrl : controllers) {
59                 delete ctrl;
60         }
61 }
62
63
64 void Spawner::Update(int dt) {
65         CheckDespawn();
66         timer.Update(dt);
67         if (timer.Hit()) {
68                 TrySpawn();
69         }
70         for (auto &ctrl : controllers) {
71                 ctrl->Update(dt);
72         }
73 }
74
75
76 void Spawner::CheckDespawn() noexcept {
77         const auto &refs = world.Players();
78         for (auto iter = controllers.begin(), end = controllers.end(); iter != end;) {
79                 Entity &e = (*iter)->Controlled();
80                 if (e.Dead()) {
81                         delete *iter;
82                         iter = controllers.erase(iter);
83                         continue;
84                 }
85                 bool safe = false;
86                 for (const Entity *ref : refs) {
87                         glm::vec3 diff(ref->AbsoluteDifference(e));
88                         if (dot(diff, diff) < despawn_range) {
89                                 safe = true;
90                                 break;
91                         }
92                 }
93                 if (!safe) {
94                         e.Kill();
95                         delete *iter;
96                         iter = controllers.erase(iter);
97                 } else {
98                         ++iter;
99                 }
100         }
101 }
102
103 void Spawner::TrySpawn() {
104         if (controllers.size() >= max_entities) return;
105
106         // select random player to punish
107         auto &players = world.Players();
108         if (players.size() == 0) return;
109         Entity &player = *players[random.Next<unsigned short>() % players.size()];
110
111         glm::ivec3 chunk(
112                 (random.Next<unsigned char>() % (chunk_range * 2 + 1)) - chunk_range,
113                 (random.Next<unsigned char>() % (chunk_range * 2 + 1)) - chunk_range,
114                 (random.Next<unsigned char>() % (chunk_range * 2 + 1)) - chunk_range
115         );
116
117         glm::ivec3 pos(
118                 random.Next<unsigned char>() % Chunk::width,
119                 random.Next<unsigned char>() % Chunk::height,
120                 random.Next<unsigned char>() % Chunk::depth
121         );
122
123
124         // distance check
125         glm::vec3 diff(glm::vec3(chunk * Chunk::Extent() - pos) + player.Position());
126         float dist = dot(diff, diff);
127         if (dist > despawn_range || dist < spawn_distance) {
128                 return;
129         }
130
131         // check if the spawn block and the one above it are loaded and inhabitable
132         BlockLookup spawn_block(
133                 world.Loader().Loaded(player.ChunkCoords()),
134                 chunk * Chunk::Extent() + pos);
135         if (!spawn_block || spawn_block.GetType().collide_block) {
136                 return;
137         }
138
139         BlockLookup head_block(spawn_block.Next(Block::FACE_UP));
140         if (!head_block || head_block.GetType().collide_block) {
141                 return;
142         }
143
144         Spawn(player, player.ChunkCoords() + chunk, glm::vec3(pos) + glm::vec3(0.5f));
145 }
146
147 void Spawner::Spawn(Entity &reference, const glm::ivec3 &chunk, const glm::vec3 &pos) {
148         glm::vec3 rot(0.000001f);
149         rot.x *= (random.Next<unsigned short>() % 1024);
150         rot.y *= (random.Next<unsigned short>() % 1024);
151         rot.z *= (random.Next<unsigned short>() % 1024);
152
153         Entity &e = world.AddEntity();
154         e.Name("spawned");
155         e.Position(chunk, pos);
156         e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
157         e.WorldCollidable(true);
158         skeletons[random.Next<unsigned char>() % 3].Instantiate(e.GetModel());
159         e.AngularVelocity(rot);
160         Controller *ctrl;
161         if (random()) {
162                 ctrl = new RandomWalk(e, random.Next<std::uint64_t>());
163         } else {
164                 ctrl = new Chaser(world, e, reference);
165         }
166         controllers.emplace_back(ctrl);
167 }
168
169 }