]> git.localhorst.tv Git - blobs.git/blob - src/creature/creature.cpp
abstract needs a little
[blobs.git] / src / creature / creature.cpp
1 #include "Creature.hpp"
2 #include "Situation.hpp"
3
4 #include "InhaleNeed.hpp"
5 #include "IngestNeed.hpp"
6 #include "Need.hpp"
7 #include "../app/Assets.hpp"
8 #include "../world/Body.hpp"
9 #include "../world/Planet.hpp"
10 #include "../world/TileType.hpp"
11
12 #include <glm/gtx/transform.hpp>
13
14 #include <iostream>
15
16
17 namespace blobs {
18 namespace creature {
19
20 Creature::Creature()
21 : name()
22 , health(1.0)
23 , needs()
24 , situation()
25 , vao() {
26 }
27
28 Creature::~Creature() {
29 }
30
31 void Creature::Hurt(double dt) noexcept {
32         health = std::max(0.0, health - dt);
33 }
34
35 void Creature::Tick(double dt) {
36         // update needs
37         for (auto &need : needs) {
38                 need->Tick(dt);
39         }
40         // do background stuff
41         for (auto &need : needs) {
42                 need->ApplyEffect(*this, dt);
43         }
44 }
45
46 glm::dmat4 Creature::LocalTransform() noexcept {
47         // TODO: surface transform
48         constexpr double half_height = 0.25;
49         const glm::dvec3 &pos = situation.Position();
50         return glm::translate(glm::dvec3(pos.x, pos.y, pos.z + situation.GetPlanet().Radius() + half_height))
51                 * glm::scale(glm::dvec3(half_height, half_height, half_height));
52 }
53
54 void Creature::BuildVAO() {
55         vao.Bind();
56         vao.BindAttributes();
57         vao.EnableAttribute(0);
58         vao.EnableAttribute(1);
59         vao.EnableAttribute(2);
60         vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
61         vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, normal));
62         vao.AttributePointer<glm::vec3>(2, false, offsetof(Attributes, texture));
63         vao.ReserveAttributes(6 * 4, GL_STATIC_DRAW);
64         {
65                 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
66                 const float offset = 1.0f;
67                 for (int surface = 0; surface < 6; ++surface) {
68                         const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
69                         const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
70
71                         attrib[4 * surface + 0].position[(surface + 0) % 3] = -offset;
72                         attrib[4 * surface + 0].position[(surface + 1) % 3] = -offset;
73                         attrib[4 * surface + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
74                         attrib[4 * surface + 0].normal[(surface + 0) % 3] = 0.0f;
75                         attrib[4 * surface + 0].normal[(surface + 1) % 3] = 0.0f;
76                         attrib[4 * surface + 0].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
77                         attrib[4 * surface + 0].texture.x = tex_u_begin;
78                         attrib[4 * surface + 0].texture.y = 1.0f;
79                         attrib[4 * surface + 0].texture.z = surface;
80
81                         attrib[4 * surface + 1].position[(surface + 0) % 3] = -offset;
82                         attrib[4 * surface + 1].position[(surface + 1) % 3] =  offset;
83                         attrib[4 * surface + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
84                         attrib[4 * surface + 1].normal[(surface + 0) % 3] = 0.0f;
85                         attrib[4 * surface + 1].normal[(surface + 1) % 3] = 0.0f;
86                         attrib[4 * surface + 1].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
87                         attrib[4 * surface + 1].texture.x = tex_u_end;
88                         attrib[4 * surface + 1].texture.y = 1.0f;
89                         attrib[4 * surface + 1].texture.z = surface;
90
91                         attrib[4 * surface + 2].position[(surface + 0) % 3] =  offset;
92                         attrib[4 * surface + 2].position[(surface + 1) % 3] = -offset;
93                         attrib[4 * surface + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
94                         attrib[4 * surface + 2].normal[(surface + 0) % 3] = 0.0f;
95                         attrib[4 * surface + 2].normal[(surface + 1) % 3] = 0.0f;
96                         attrib[4 * surface + 2].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
97                         attrib[4 * surface + 2].texture.x = tex_u_begin;
98                         attrib[4 * surface + 2].texture.y = 0.0f;
99                         attrib[4 * surface + 2].texture.z = surface;
100
101                         attrib[4 * surface + 3].position[(surface + 0) % 3] = offset;
102                         attrib[4 * surface + 3].position[(surface + 1) % 3] = offset;
103                         attrib[4 * surface + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
104                         attrib[4 * surface + 3].normal[(surface + 0) % 3] = 0.0f;
105                         attrib[4 * surface + 3].normal[(surface + 1) % 3] = 0.0f;
106                         attrib[4 * surface + 3].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
107                         attrib[4 * surface + 3].texture.x = tex_u_end;
108                         attrib[4 * surface + 3].texture.y = 0.0f;
109                         attrib[4 * surface + 3].texture.z = surface;
110                 }
111         }
112         vao.BindElements();
113         vao.ReserveElements(6 * 6, GL_STATIC_DRAW);
114         {
115                 auto element = vao.MapElements(GL_WRITE_ONLY);
116                 for (int surface = 0; surface < 3; ++surface) {
117                         element[6 * surface + 0] = 4 * surface + 0;
118                         element[6 * surface + 1] = 4 * surface + 2;
119                         element[6 * surface + 2] = 4 * surface + 1;
120                         element[6 * surface + 3] = 4 * surface + 1;
121                         element[6 * surface + 4] = 4 * surface + 2;
122                         element[6 * surface + 5] = 4 * surface + 3;
123                 }
124                 for (int surface = 3; surface < 6; ++surface) {
125                         element[6 * surface + 0] = 4 * surface + 0;
126                         element[6 * surface + 1] = 4 * surface + 1;
127                         element[6 * surface + 2] = 4 * surface + 2;
128                         element[6 * surface + 3] = 4 * surface + 2;
129                         element[6 * surface + 4] = 4 * surface + 1;
130                         element[6 * surface + 5] = 4 * surface + 3;
131                 }
132         }
133         vao.Unbind();
134 }
135
136 void Creature::Draw(app::Assets &assets, graphics::Viewport &viewport) {
137         vao.Bind();
138         vao.DrawTriangles(6 * 6);
139 }
140
141
142 void Spawn(Creature &c, world::Planet &p, app::Assets &assets) {
143         p.AddCreature(&c);
144         c.GetSituation().SetPlanetSurface(p, 0, glm::dvec3(0.0, 0.0, 0.0));
145
146         // probe surrounding area for common resources
147         int start = p.SideLength() / 2 - 2;
148         int end = start + 5;
149         std::map<int, double> yields;
150         for (int y = start; y < end; ++y) {
151                 for (int x = start; x < end; ++x) {
152                         const world::TileType &t = assets.data.tiles[p.TileAt(0, x, y).type];
153                         for (auto yield : t.resources) {
154                                 yields[yield.resource] += yield.ubiquity;
155                         }
156                 }
157         }
158         int liquid = -1;
159         int solid = -1;
160         for (auto e : yields) {
161                 if (assets.data.resources[e.first].state == world::Resource::LIQUID) {
162                         if (liquid < 0 || e.second > yields[liquid]) {
163                                 liquid = e.first;
164                         }
165                 } else if (assets.data.resources[e.first].state == world::Resource::SOLID) {
166                         if (solid < 0 || e.second > yields[solid]) {
167                                 solid = e.first;
168                         }
169                 }
170         }
171
172         if (p.HasAtmosphere()) {
173                 std::cout << "require breathing " << assets.data.resources[p.Atmosphere()].label << std::endl;
174                 std::unique_ptr<Need> need(new InhaleNeed(p.Atmosphere(), 0.25, 0.1));
175                 need->name = assets.data.resources[p.Atmosphere()].label;
176                 need->gain = 0.25;
177                 need->inconvenient = 0.4;
178                 need->critical = 0.95;
179                 c.AddNeed(std::move(need));
180         }
181         if (liquid > -1) {
182                 std::cout << "require drinking " << assets.data.resources[liquid].label << std::endl;
183                 std::unique_ptr<Need> need(new IngestNeed(liquid, 0.2, 0.01));
184                 need->name = assets.data.resources[liquid].label;
185                 need->gain = 0.0001;
186                 need->inconvenient = 0.6;
187                 need->critical = 0.95;
188                 c.AddNeed(std::move(need));
189         }
190         if (solid > -1) {
191                 std::cout << "require eating " << assets.data.resources[solid].label << std::endl;
192                 std::unique_ptr<Need> need(new IngestNeed(solid, 0.03, 0.001));
193                 need->name = assets.data.resources[solid].label;
194                 need->gain = 0.00001;
195                 need->inconvenient = 0.6;
196                 need->critical = 0.95;
197                 c.AddNeed(std::move(need));
198         }
199 }
200
201 Situation::Situation()
202 : planet(nullptr)
203 , position(0.0)
204 , surface(0)
205 , type(LOST) {
206 }
207
208 Situation::~Situation() {
209 }
210
211 bool Situation::OnPlanet() const noexcept {
212         return type == PLANET_SURFACE;
213 }
214
215 void Situation::SetPlanetSurface(world::Planet &p, int srf, const glm::dvec3 &pos) noexcept {
216         type = PLANET_SURFACE;
217         planet = &p;
218         surface = srf;
219         position = pos;
220 }
221
222 }
223 }