]> git.localhorst.tv Git - blank.git/blob - src/ai/Spawner.cpp
store players in world save
[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.GetEntity().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         size_t player_num = random.Next<unsigned short>() % players.size();
99         auto i = players.begin(), end = players.end();
100         for (; player_num > 0 && i != end; ++i, --player_num) {
101         }
102         const Player &player = *i;
103
104         int index = random.Next<unsigned int>() % player.GetChunks().TotalChunks();
105
106         glm::ivec3 chunk(player.GetChunks().PositionOf(index));
107
108         glm::ivec3 pos(
109                 random.Next<unsigned char>() % Chunk::width,
110                 random.Next<unsigned char>() % Chunk::height,
111                 random.Next<unsigned char>() % Chunk::depth
112         );
113
114         // distance check
115         //glm::vec3 diff(glm::vec3(chunk * Chunk::Extent() - pos) + player.entity->Position());
116         //float dist = dot(diff, diff);
117         //if (dist > despawn_range || dist < spawn_distance) {
118         //      return;
119         //}
120
121         // check if the spawn block and the one above it are loaded and inhabitable
122         BlockLookup spawn_block(player.GetChunks()[index], pos);
123         if (!spawn_block || spawn_block.GetType().collide_block) {
124                 return;
125         }
126
127         BlockLookup head_block(spawn_block.Next(Block::FACE_UP));
128         if (!head_block || head_block.GetType().collide_block) {
129                 return;
130         }
131
132         Spawn(player.GetEntity(), chunk, glm::vec3(pos) + glm::vec3(0.5f));
133 }
134
135 void Spawner::Spawn(Entity &reference, const glm::ivec3 &chunk, const glm::vec3 &pos) {
136         glm::vec3 rot(0.000001f);
137         rot.x *= (random.Next<unsigned short>() % 1024);
138         rot.y *= (random.Next<unsigned short>() % 1024);
139         rot.z *= (random.Next<unsigned short>() % 1024);
140
141         Entity &e = world.AddEntity();
142         e.Position(chunk, pos);
143         e.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
144         e.WorldCollidable(true);
145         RandomSkeleton().Instantiate(e.GetModel());
146         e.AngularVelocity(rot);
147         Controller *ctrl;
148         if (random()) {
149                 ctrl = new RandomWalk(e, random.Next<std::uint64_t>());
150                 e.Name("walker");
151         } else {
152                 ctrl = new Chaser(world, e, reference);
153                 e.Name("chaser");
154         }
155         controllers.emplace_back(ctrl);
156 }
157
158 CompositeModel &Spawner::RandomSkeleton() noexcept {
159         std::size_t offset = (random.Next<std::size_t>() % skeletons_length) + skeletons_offset;
160         return skeletons[offset];
161 }
162
163 }