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