1 #include "Creature.hpp"
2 #include "Situation.hpp"
3 #include "Steering.hpp"
6 #include "InhaleNeed.hpp"
7 #include "IngestNeed.hpp"
9 #include "../app/Assets.hpp"
10 #include "../world/Body.hpp"
11 #include "../world/Planet.hpp"
12 #include "../world/Simulation.hpp"
13 #include "../world/TileType.hpp"
16 #include <glm/gtx/transform.hpp>
35 Creature::~Creature() {
38 void Creature::Hurt(double dt) noexcept {
39 health = std::max(0.0, health - dt);
42 void Creature::AddGoal(std::unique_ptr<Goal> &&g) {
44 goals.emplace_back(std::move(g));
49 bool GoalCompare(const std::unique_ptr<Goal> &a, const std::unique_ptr<Goal> &b) {
50 return b->Urgency() < a->Urgency();
55 void Creature::Tick(double dt) {
56 // TODO: better integration method
57 glm::dvec3 acc(steering.Acceleration(*this));
58 situation.Move(vel * dt);
61 for (auto &need : needs) {
64 for (auto &goal : goals) {
67 // do background stuff
68 for (auto &need : needs) {
69 need->ApplyEffect(*this, dt);
74 // if active goal can be interrupted, check priorities
75 if (goals.size() > 1 && goals[0]->Interruptible()) {
76 std::sort(goals.begin(), goals.end(), GoalCompare);
78 goals[0]->Action(*this);
79 for (auto goal = goals.begin(); goal != goals.end();) {
80 if ((*goal)->Complete()) {
88 glm::dmat4 Creature::LocalTransform() noexcept {
89 // TODO: surface transform
90 constexpr double half_height = 0.25;
91 const glm::dvec3 &pos = situation.Position();
92 return glm::translate(glm::dvec3(pos.x, pos.y, pos.z + situation.GetPlanet().Radius() + half_height))
93 * glm::scale(glm::dvec3(half_height, half_height, half_height));
96 void Creature::BuildVAO() {
99 vao.EnableAttribute(0);
100 vao.EnableAttribute(1);
101 vao.EnableAttribute(2);
102 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
103 vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, normal));
104 vao.AttributePointer<glm::vec3>(2, false, offsetof(Attributes, texture));
105 vao.ReserveAttributes(6 * 4, GL_STATIC_DRAW);
107 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
108 const float offset = 1.0f;
109 for (int surface = 0; surface < 6; ++surface) {
110 const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
111 const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
113 attrib[4 * surface + 0].position[(surface + 0) % 3] = -offset;
114 attrib[4 * surface + 0].position[(surface + 1) % 3] = -offset;
115 attrib[4 * surface + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
116 attrib[4 * surface + 0].normal[(surface + 0) % 3] = 0.0f;
117 attrib[4 * surface + 0].normal[(surface + 1) % 3] = 0.0f;
118 attrib[4 * surface + 0].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
119 attrib[4 * surface + 0].texture.x = tex_u_begin;
120 attrib[4 * surface + 0].texture.y = 1.0f;
121 attrib[4 * surface + 0].texture.z = surface;
123 attrib[4 * surface + 1].position[(surface + 0) % 3] = -offset;
124 attrib[4 * surface + 1].position[(surface + 1) % 3] = offset;
125 attrib[4 * surface + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
126 attrib[4 * surface + 1].normal[(surface + 0) % 3] = 0.0f;
127 attrib[4 * surface + 1].normal[(surface + 1) % 3] = 0.0f;
128 attrib[4 * surface + 1].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
129 attrib[4 * surface + 1].texture.x = tex_u_end;
130 attrib[4 * surface + 1].texture.y = 1.0f;
131 attrib[4 * surface + 1].texture.z = surface;
133 attrib[4 * surface + 2].position[(surface + 0) % 3] = offset;
134 attrib[4 * surface + 2].position[(surface + 1) % 3] = -offset;
135 attrib[4 * surface + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
136 attrib[4 * surface + 2].normal[(surface + 0) % 3] = 0.0f;
137 attrib[4 * surface + 2].normal[(surface + 1) % 3] = 0.0f;
138 attrib[4 * surface + 2].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
139 attrib[4 * surface + 2].texture.x = tex_u_begin;
140 attrib[4 * surface + 2].texture.y = 0.0f;
141 attrib[4 * surface + 2].texture.z = surface;
143 attrib[4 * surface + 3].position[(surface + 0) % 3] = offset;
144 attrib[4 * surface + 3].position[(surface + 1) % 3] = offset;
145 attrib[4 * surface + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
146 attrib[4 * surface + 3].normal[(surface + 0) % 3] = 0.0f;
147 attrib[4 * surface + 3].normal[(surface + 1) % 3] = 0.0f;
148 attrib[4 * surface + 3].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
149 attrib[4 * surface + 3].texture.x = tex_u_end;
150 attrib[4 * surface + 3].texture.y = 0.0f;
151 attrib[4 * surface + 3].texture.z = surface;
155 vao.ReserveElements(6 * 6, GL_STATIC_DRAW);
157 auto element = vao.MapElements(GL_WRITE_ONLY);
158 for (int surface = 0; surface < 3; ++surface) {
159 element[6 * surface + 0] = 4 * surface + 0;
160 element[6 * surface + 1] = 4 * surface + 2;
161 element[6 * surface + 2] = 4 * surface + 1;
162 element[6 * surface + 3] = 4 * surface + 1;
163 element[6 * surface + 4] = 4 * surface + 2;
164 element[6 * surface + 5] = 4 * surface + 3;
166 for (int surface = 3; surface < 6; ++surface) {
167 element[6 * surface + 0] = 4 * surface + 0;
168 element[6 * surface + 1] = 4 * surface + 1;
169 element[6 * surface + 2] = 4 * surface + 2;
170 element[6 * surface + 3] = 4 * surface + 2;
171 element[6 * surface + 4] = 4 * surface + 1;
172 element[6 * surface + 5] = 4 * surface + 3;
178 void Creature::Draw(app::Assets &assets, graphics::Viewport &viewport) {
180 vao.DrawTriangles(6 * 6);
184 void Spawn(Creature &c, world::Planet &p, app::Assets &assets) {
186 c.GetSituation().SetPlanetSurface(p, 0, glm::dvec3(0.0, 0.0, 0.0));
188 // probe surrounding area for common resources
189 int start = p.SideLength() / 2 - 2;
191 std::map<int, double> yields;
192 for (int y = start; y < end; ++y) {
193 for (int x = start; x < end; ++x) {
194 const world::TileType &t = assets.data.tile_types[p.TileAt(0, x, y).type];
195 for (auto yield : t.resources) {
196 yields[yield.resource] += yield.ubiquity;
202 for (auto e : yields) {
203 if (assets.data.resources[e.first].state == world::Resource::LIQUID) {
204 if (liquid < 0 || e.second > yields[liquid]) {
207 } else if (assets.data.resources[e.first].state == world::Resource::SOLID) {
208 if (solid < 0 || e.second > yields[solid]) {
214 if (p.HasAtmosphere()) {
215 std::cout << "require breathing " << assets.data.resources[p.Atmosphere()].label << std::endl;
216 std::unique_ptr<Need> need(new InhaleNeed(p.Atmosphere(), 0.5, 0.1));
217 need->name = assets.data.resources[p.Atmosphere()].label;
219 need->inconvenient = 0.4;
220 need->critical = 0.95;
221 c.AddNeed(std::move(need));
224 std::cout << "require drinking " << assets.data.resources[liquid].label << std::endl;
225 std::unique_ptr<Need> need(new IngestNeed(liquid, 0.2, 0.01));
226 need->name = assets.data.resources[liquid].label;
228 need->inconvenient = 0.6;
229 need->critical = 0.95;
230 c.AddNeed(std::move(need));
233 std::cout << "require eating " << assets.data.resources[solid].label << std::endl;
234 std::unique_ptr<Need> need(new IngestNeed(solid, 0.1, 0.001));
235 need->name = assets.data.resources[solid].label;
237 need->inconvenient = 0.6;
238 need->critical = 0.95;
239 c.AddNeed(std::move(need));
243 Situation::Situation()
250 Situation::~Situation() {
253 bool Situation::OnPlanet() const noexcept {
254 return type == PLANET_SURFACE;
257 bool Situation::OnSurface() const noexcept {
258 return type == PLANET_SURFACE;
261 world::Tile &Situation::GetTile() const noexcept {
262 double side_length = planet->SideLength();
263 double offset = side_length * 0.5;
264 double x = std::max(0.0, std::min(side_length, position.x + offset));
265 double y = std::max(0.0, std::min(side_length, position.y + offset));
266 return planet->TileAt(surface, int(x), int(y));
269 const world::TileType &Situation::GetTileType() const noexcept {
270 return planet->GetSimulation().TileTypes()[GetTile().type];
273 void Situation::Move(const glm::dvec3 &dp) noexcept {
276 // enforce ground constraint
277 position.z = std::max(0.0, position.z);
281 void Situation::SetPlanetSurface(world::Planet &p, int srf, const glm::dvec3 &pos) noexcept {
282 type = PLANET_SURFACE;
297 Steering::~Steering() {
300 void Steering::Halt() noexcept {
305 void Steering::GoTo(const glm::dvec3 &t) noexcept {
311 glm::dvec3 Steering::Acceleration(Creature &c) const noexcept {
314 SumForce(acc, c.Velocity() * -max_accel);
317 glm::dvec3 diff = seek_target - c.GetSituation().Position();
318 if (!allzero(diff)) {
319 SumForce(acc, ((normalize(diff) * max_speed) - c.Velocity()) * max_accel);
325 bool Steering::SumForce(glm::dvec3 &out, const glm::dvec3 &in) const noexcept {
326 if (allzero(in) || anynan(in)) {
329 double cur = allzero(out) ? 0.0 : length(out);
330 double rem = max_accel - cur;
334 double add = length(in);
336 // this method is off if in and out are in different
337 // directions, but gives okayish results
338 out += in * (1.0 / add);