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