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>
19 #include <glm/gtx/io.hpp>
25 Creature::Creature(world::Simulation &sim)
38 Creature::~Creature() {
41 void Creature::Hurt(double dt) noexcept {
42 health = std::max(0.0, health - dt);
45 void Creature::AddGoal(std::unique_ptr<Goal> &&g) {
46 std::cout << "new goal: " << g->Describe() << std::endl;
48 goals.emplace_back(std::move(g));
53 bool GoalCompare(const std::unique_ptr<Goal> &a, const std::unique_ptr<Goal> &b) {
54 return b->Urgency() < a->Urgency();
59 void Creature::Tick(double dt) {
60 // TODO: better integration method
61 glm::dvec3 acc(steering.Acceleration(*this));
62 situation.Move(vel * dt);
65 for (auto &need : needs) {
68 for (auto &goal : goals) {
71 // do background stuff
72 for (auto &need : needs) {
73 need->ApplyEffect(*this, dt);
78 // if active goal can be interrupted, check priorities
79 if (goals.size() > 1 && goals[0]->Interruptible()) {
80 Goal *old_top = &*goals[0];
81 std::sort(goals.begin(), goals.end(), GoalCompare);
82 Goal *new_top = &*goals[0];
83 if (new_top != old_top) {
84 std::cout << "changing goal from " << old_top->Describe()
85 << " to " << new_top->Describe() << std::endl;
89 for (auto goal = goals.begin(); goal != goals.end();) {
90 if ((*goal)->Complete()) {
91 std::cout << "complete goal: " << (*goal)->Describe() << std::endl;
99 glm::dmat4 Creature::LocalTransform() noexcept {
100 // TODO: surface transform
101 const double half_size = size * 0.5;
102 const glm::dvec3 &pos = situation.Position();
103 return glm::translate(glm::dvec3(pos.x, pos.y, pos.z + half_size))
104 * glm::scale(glm::dvec3(half_size, half_size, half_size));
107 void Creature::BuildVAO() {
109 vao.BindAttributes();
110 vao.EnableAttribute(0);
111 vao.EnableAttribute(1);
112 vao.EnableAttribute(2);
113 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
114 vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, normal));
115 vao.AttributePointer<glm::vec3>(2, false, offsetof(Attributes, texture));
116 vao.ReserveAttributes(6 * 4, GL_STATIC_DRAW);
118 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
119 const float offset = 1.0f;
120 for (int surface = 0; surface < 6; ++surface) {
121 const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
122 const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
124 attrib[4 * surface + 0].position[(surface + 0) % 3] = -offset;
125 attrib[4 * surface + 0].position[(surface + 1) % 3] = -offset;
126 attrib[4 * surface + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
127 attrib[4 * surface + 0].normal[(surface + 0) % 3] = 0.0f;
128 attrib[4 * surface + 0].normal[(surface + 1) % 3] = 0.0f;
129 attrib[4 * surface + 0].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
130 attrib[4 * surface + 0].texture.x = tex_u_begin;
131 attrib[4 * surface + 0].texture.y = 1.0f;
132 attrib[4 * surface + 0].texture.z = surface;
134 attrib[4 * surface + 1].position[(surface + 0) % 3] = -offset;
135 attrib[4 * surface + 1].position[(surface + 1) % 3] = offset;
136 attrib[4 * surface + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
137 attrib[4 * surface + 1].normal[(surface + 0) % 3] = 0.0f;
138 attrib[4 * surface + 1].normal[(surface + 1) % 3] = 0.0f;
139 attrib[4 * surface + 1].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
140 attrib[4 * surface + 1].texture.x = tex_u_end;
141 attrib[4 * surface + 1].texture.y = 1.0f;
142 attrib[4 * surface + 1].texture.z = surface;
144 attrib[4 * surface + 2].position[(surface + 0) % 3] = offset;
145 attrib[4 * surface + 2].position[(surface + 1) % 3] = -offset;
146 attrib[4 * surface + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
147 attrib[4 * surface + 2].normal[(surface + 0) % 3] = 0.0f;
148 attrib[4 * surface + 2].normal[(surface + 1) % 3] = 0.0f;
149 attrib[4 * surface + 2].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
150 attrib[4 * surface + 2].texture.x = tex_u_begin;
151 attrib[4 * surface + 2].texture.y = 0.0f;
152 attrib[4 * surface + 2].texture.z = surface;
154 attrib[4 * surface + 3].position[(surface + 0) % 3] = offset;
155 attrib[4 * surface + 3].position[(surface + 1) % 3] = offset;
156 attrib[4 * surface + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
157 attrib[4 * surface + 3].normal[(surface + 0) % 3] = 0.0f;
158 attrib[4 * surface + 3].normal[(surface + 1) % 3] = 0.0f;
159 attrib[4 * surface + 3].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
160 attrib[4 * surface + 3].texture.x = tex_u_end;
161 attrib[4 * surface + 3].texture.y = 0.0f;
162 attrib[4 * surface + 3].texture.z = surface;
166 vao.ReserveElements(6 * 6, GL_STATIC_DRAW);
168 auto element = vao.MapElements(GL_WRITE_ONLY);
169 for (int surface = 0; surface < 3; ++surface) {
170 element[6 * surface + 0] = 4 * surface + 0;
171 element[6 * surface + 1] = 4 * surface + 2;
172 element[6 * surface + 2] = 4 * surface + 1;
173 element[6 * surface + 3] = 4 * surface + 1;
174 element[6 * surface + 4] = 4 * surface + 2;
175 element[6 * surface + 5] = 4 * surface + 3;
177 for (int surface = 3; surface < 6; ++surface) {
178 element[6 * surface + 0] = 4 * surface + 0;
179 element[6 * surface + 1] = 4 * surface + 1;
180 element[6 * surface + 2] = 4 * surface + 2;
181 element[6 * surface + 3] = 4 * surface + 2;
182 element[6 * surface + 4] = 4 * surface + 1;
183 element[6 * surface + 5] = 4 * surface + 3;
189 void Creature::Draw(app::Assets &assets, graphics::Viewport &viewport) {
191 vao.DrawTriangles(6 * 6);
195 void Spawn(Creature &c, world::Planet &p, app::Assets &assets) {
197 c.GetSituation().SetPlanetSurface(p, 0, p.TileCenter(0, p.SideLength() / 2, p.SideLength() / 2));
200 // probe surrounding area for common resources
201 int start = p.SideLength() / 2 - 2;
203 std::map<int, double> yields;
204 for (int y = start; y < end; ++y) {
205 for (int x = start; x < end; ++x) {
206 const world::TileType &t = assets.data.tile_types[p.TileAt(0, x, y).type];
207 for (auto yield : t.resources) {
208 yields[yield.resource] += yield.ubiquity;
214 for (auto e : yields) {
215 if (assets.data.resources[e.first].state == world::Resource::LIQUID) {
216 if (liquid < 0 || e.second > yields[liquid]) {
219 } else if (assets.data.resources[e.first].state == world::Resource::SOLID) {
220 if (solid < 0 || e.second > yields[solid]) {
226 if (p.HasAtmosphere()) {
227 std::cout << "require breathing " << assets.data.resources[p.Atmosphere()].label << std::endl;
228 std::unique_ptr<Need> need(new InhaleNeed(p.Atmosphere(), 0.5, 0.1));
229 need->name = assets.data.resources[p.Atmosphere()].label;
231 need->inconvenient = 0.4;
232 need->critical = 0.95;
233 c.AddNeed(std::move(need));
236 std::cout << "require drinking " << assets.data.resources[liquid].label << std::endl;
237 std::unique_ptr<Need> need(new IngestNeed(liquid, 0.2, 0.01));
238 need->name = assets.data.resources[liquid].label;
240 need->inconvenient = 0.6;
241 need->critical = 0.95;
242 c.AddNeed(std::move(need));
245 std::cout << "require eating " << assets.data.resources[solid].label << std::endl;
246 std::unique_ptr<Need> need(new IngestNeed(solid, 0.1, 0.001));
247 need->name = assets.data.resources[solid].label;
249 need->inconvenient = 0.6;
250 need->critical = 0.95;
251 c.AddNeed(std::move(need));
255 Situation::Situation()
262 Situation::~Situation() {
265 bool Situation::OnPlanet() const noexcept {
266 return type == PLANET_SURFACE;
269 bool Situation::OnSurface() const noexcept {
270 return type == PLANET_SURFACE;
273 world::Tile &Situation::GetTile() const noexcept {
274 double side_length = planet->SideLength();
275 double offset = side_length * 0.5;
276 double x = std::max(0.0, std::min(side_length, position.x + offset));
277 double y = std::max(0.0, std::min(side_length, position.y + offset));
278 return planet->TileAt(surface, int(x), int(y));
281 const world::TileType &Situation::GetTileType() const noexcept {
282 return planet->GetSimulation().TileTypes()[GetTile().type];
285 void Situation::Move(const glm::dvec3 &dp) noexcept {
288 // enforce ground constraint
290 position[(Surface() + 2) % 3] = std::max(0.0, position[(Surface() + 2) % 3]);
292 position[(Surface() + 2) % 3] = std::min(0.0, position[(Surface() + 2) % 3]);
297 void Situation::SetPlanetSurface(world::Planet &p, int srf, const glm::dvec3 &pos) noexcept {
298 type = PLANET_SURFACE;
313 Steering::~Steering() {
316 void Steering::Halt() noexcept {
321 void Steering::GoTo(const glm::dvec3 &t) noexcept {
327 glm::dvec3 Steering::Acceleration(Creature &c) const noexcept {
330 SumForce(acc, c.Velocity() * -max_accel);
333 glm::dvec3 diff = seek_target - c.GetSituation().Position();
334 if (!allzero(diff)) {
335 SumForce(acc, ((normalize(diff) * max_speed) - c.Velocity()) * max_accel);
341 bool Steering::SumForce(glm::dvec3 &out, const glm::dvec3 &in) const noexcept {
342 if (allzero(in) || anynan(in)) {
345 double cur = allzero(out) ? 0.0 : length(out);
346 double rem = max_accel - cur;
350 double add = length(in);
352 // this method is off if in and out are in different
353 // directions, but gives okayish results
354 out += in * (1.0 / add);