1 #include "Creature.hpp"
2 #include "Situation.hpp"
4 #include "InhaleNeed.hpp"
5 #include "IngestNeed.hpp"
7 #include "../app/Assets.hpp"
8 #include "../world/Body.hpp"
9 #include "../world/Planet.hpp"
10 #include "../world/TileType.hpp"
12 #include <glm/gtx/transform.hpp>
28 Creature::~Creature() {
31 void Creature::Hurt(double dt) noexcept {
32 health = std::max(0.0, health - dt);
35 void Creature::Tick(double dt) {
37 for (auto &need : needs) {
40 // do background stuff
41 for (auto &need : needs) {
42 need->ApplyEffect(*this, dt);
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));
54 void Creature::BuildVAO() {
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);
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;
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;
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;
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;
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;
113 vao.ReserveElements(6 * 6, GL_STATIC_DRAW);
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;
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;
136 void Creature::Draw(app::Assets &assets, graphics::Viewport &viewport) {
138 vao.DrawTriangles(6 * 6);
142 void Spawn(Creature &c, world::Planet &p, app::Assets &assets) {
144 c.GetSituation().SetPlanetSurface(p, 0, glm::dvec3(0.0, 0.0, 0.0));
146 // probe surrounding area for common resources
147 int start = p.SideLength() / 2 - 2;
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;
160 for (auto e : yields) {
161 if (assets.data.resources[e.first].state == world::Resource::LIQUID) {
162 if (liquid < 0 || e.second > yields[liquid]) {
165 } else if (assets.data.resources[e.first].state == world::Resource::SOLID) {
166 if (solid < 0 || e.second > yields[solid]) {
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;
177 need->inconvenient = 0.4;
178 need->critical = 0.95;
179 c.AddNeed(std::move(need));
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;
186 need->inconvenient = 0.6;
187 need->critical = 0.95;
188 c.AddNeed(std::move(need));
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));
201 Situation::Situation()
208 Situation::~Situation() {
211 bool Situation::OnPlanet() const noexcept {
212 return type == PLANET_SURFACE;
215 void Situation::SetPlanetSurface(world::Planet &p, int srf, const glm::dvec3 &pos) noexcept {
216 type = PLANET_SURFACE;