1 #include "Creature.hpp"
3 #include "Situation.hpp"
4 #include "Steering.hpp"
7 #include "InhaleNeed.hpp"
8 #include "IngestNeed.hpp"
10 #include "../app/Assets.hpp"
11 #include "../world/Body.hpp"
12 #include "../world/Planet.hpp"
13 #include "../world/Simulation.hpp"
14 #include "../world/TileType.hpp"
17 #include <glm/gtx/transform.hpp>
20 #include <glm/gtx/io.hpp>
26 Creature::Creature(world::Simulation &sim)
41 Creature::~Creature() {
44 void Creature::Hurt(double dt) noexcept {
45 health = std::max(0.0, health - dt);
48 void Creature::AddGoal(std::unique_ptr<Goal> &&g) {
49 std::cout << "new goal: " << g->Describe() << std::endl;
51 goals.emplace_back(std::move(g));
56 bool GoalCompare(const std::unique_ptr<Goal> &a, const std::unique_ptr<Goal> &b) {
57 return b->Urgency() < a->Urgency();
62 void Creature::Tick(double dt) {
63 // TODO: better integration method
64 glm::dvec3 acc(steering.Acceleration(*this));
65 situation.Move(vel * dt);
68 for (auto &need : needs) {
71 for (auto &goal : goals) {
74 // do background stuff
75 for (auto &need : needs) {
76 need->ApplyEffect(*this, dt);
81 // if active goal can be interrupted, check priorities
82 if (goals.size() > 1 && goals[0]->Interruptible()) {
83 Goal *old_top = &*goals[0];
84 std::sort(goals.begin(), goals.end(), GoalCompare);
85 Goal *new_top = &*goals[0];
86 if (new_top != old_top) {
87 std::cout << "changing goal from " << old_top->Describe()
88 << " to " << new_top->Describe() << std::endl;
92 for (auto goal = goals.begin(); goal != goals.end();) {
93 if ((*goal)->Complete()) {
94 std::cout << "complete goal: " << (*goal)->Describe() << std::endl;
102 glm::dmat4 Creature::LocalTransform() noexcept {
103 // TODO: surface transform
104 const double half_size = size * 0.5;
105 const glm::dvec3 &pos = situation.Position();
106 return glm::translate(glm::dvec3(pos.x, pos.y, pos.z + half_size))
107 * glm::scale(glm::dvec3(half_size, half_size, half_size));
110 void Creature::BuildVAO() {
112 vao.BindAttributes();
113 vao.EnableAttribute(0);
114 vao.EnableAttribute(1);
115 vao.EnableAttribute(2);
116 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
117 vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, normal));
118 vao.AttributePointer<glm::vec3>(2, false, offsetof(Attributes, texture));
119 vao.ReserveAttributes(6 * 4, GL_STATIC_DRAW);
121 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
122 const float offset = 1.0f;
123 for (int surface = 0; surface < 6; ++surface) {
124 const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
125 const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
127 attrib[4 * surface + 0].position[(surface + 0) % 3] = -offset;
128 attrib[4 * surface + 0].position[(surface + 1) % 3] = -offset;
129 attrib[4 * surface + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
130 attrib[4 * surface + 0].normal[(surface + 0) % 3] = 0.0f;
131 attrib[4 * surface + 0].normal[(surface + 1) % 3] = 0.0f;
132 attrib[4 * surface + 0].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
133 attrib[4 * surface + 0].texture.x = tex_u_begin;
134 attrib[4 * surface + 0].texture.y = 1.0f;
135 attrib[4 * surface + 0].texture.z = surface;
137 attrib[4 * surface + 1].position[(surface + 0) % 3] = -offset;
138 attrib[4 * surface + 1].position[(surface + 1) % 3] = offset;
139 attrib[4 * surface + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
140 attrib[4 * surface + 1].normal[(surface + 0) % 3] = 0.0f;
141 attrib[4 * surface + 1].normal[(surface + 1) % 3] = 0.0f;
142 attrib[4 * surface + 1].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
143 attrib[4 * surface + 1].texture.x = tex_u_end;
144 attrib[4 * surface + 1].texture.y = 1.0f;
145 attrib[4 * surface + 1].texture.z = surface;
147 attrib[4 * surface + 2].position[(surface + 0) % 3] = offset;
148 attrib[4 * surface + 2].position[(surface + 1) % 3] = -offset;
149 attrib[4 * surface + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
150 attrib[4 * surface + 2].normal[(surface + 0) % 3] = 0.0f;
151 attrib[4 * surface + 2].normal[(surface + 1) % 3] = 0.0f;
152 attrib[4 * surface + 2].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
153 attrib[4 * surface + 2].texture.x = tex_u_begin;
154 attrib[4 * surface + 2].texture.y = 0.0f;
155 attrib[4 * surface + 2].texture.z = surface;
157 attrib[4 * surface + 3].position[(surface + 0) % 3] = offset;
158 attrib[4 * surface + 3].position[(surface + 1) % 3] = offset;
159 attrib[4 * surface + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
160 attrib[4 * surface + 3].normal[(surface + 0) % 3] = 0.0f;
161 attrib[4 * surface + 3].normal[(surface + 1) % 3] = 0.0f;
162 attrib[4 * surface + 3].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
163 attrib[4 * surface + 3].texture.x = tex_u_end;
164 attrib[4 * surface + 3].texture.y = 0.0f;
165 attrib[4 * surface + 3].texture.z = surface;
169 vao.ReserveElements(6 * 6, GL_STATIC_DRAW);
171 auto element = vao.MapElements(GL_WRITE_ONLY);
172 for (int surface = 0; surface < 3; ++surface) {
173 element[6 * surface + 0] = 4 * surface + 0;
174 element[6 * surface + 1] = 4 * surface + 2;
175 element[6 * surface + 2] = 4 * surface + 1;
176 element[6 * surface + 3] = 4 * surface + 1;
177 element[6 * surface + 4] = 4 * surface + 2;
178 element[6 * surface + 5] = 4 * surface + 3;
180 for (int surface = 3; surface < 6; ++surface) {
181 element[6 * surface + 0] = 4 * surface + 0;
182 element[6 * surface + 1] = 4 * surface + 1;
183 element[6 * surface + 2] = 4 * surface + 2;
184 element[6 * surface + 3] = 4 * surface + 2;
185 element[6 * surface + 4] = 4 * surface + 1;
186 element[6 * surface + 5] = 4 * surface + 3;
192 void Creature::Draw(app::Assets &assets, graphics::Viewport &viewport) {
194 vao.DrawTriangles(6 * 6);
198 void Spawn(Creature &c, world::Planet &p, app::Assets &assets) {
200 c.GetSituation().SetPlanetSurface(p, 0, p.TileCenter(0, p.SideLength() / 2, p.SideLength() / 2));
202 // probe surrounding area for common resources
203 int start = p.SideLength() / 2 - 2;
205 std::map<int, double> yields;
206 for (int y = start; y < end; ++y) {
207 for (int x = start; x < end; ++x) {
208 const world::TileType &t = assets.data.tile_types[p.TileAt(0, x, y).type];
209 for (auto yield : t.resources) {
210 yields[yield.resource] += yield.ubiquity;
216 for (auto e : yields) {
217 if (assets.data.resources[e.first].state == world::Resource::LIQUID) {
218 if (liquid < 0 || e.second > yields[liquid]) {
221 } else if (assets.data.resources[e.first].state == world::Resource::SOLID) {
222 if (solid < 0 || e.second > yields[solid]) {
229 if (p.HasAtmosphere()) {
230 genome.composition.push_back({
231 p.Atmosphere(), // resource
232 { 0.01, 0.00001 }, // mass
233 { 0.5, 0.001 }, // intake
234 { 0.1, 0.0005 } // penalty
238 genome.composition.push_back({
240 { 0.6, 0.01 }, // mass
241 { 0.2, 0.001 }, // intake
242 { 0.01, 0.002 } // penalty
246 genome.composition.push_back({
248 { 0.4, 0.01 }, // mass
249 { 0.1, 0.001 }, // intake
250 { 0.001, 0.0001 } // penalty
254 genome.Configure(assets, c);
255 c.GetSteering().MaxAcceleration(1.4);
256 c.GetSteering().MaxSpeed(4.4);
259 void Genome::Configure(app::Assets &assets, Creature &c) const {
260 c.GetGenome() = *this;
263 for (const auto &comp : composition) {
264 double comp_mass = comp.mass.FakeNormal(assets.random.SNorm());
265 double intake = comp.intake.FakeNormal(assets.random.SNorm());
266 double penalty = comp.intake.FakeNormal(assets.random.SNorm());
269 volume += comp_mass / assets.data.resources[comp.resource].density;
271 std::unique_ptr<Need> need;
272 if (assets.data.resources[comp.resource].state == world::Resource::SOLID) {
273 need.reset(new IngestNeed(comp.resource, intake, penalty));
274 need->gain = intake * 0.05;
275 } else if (assets.data.resources[comp.resource].state == world::Resource::LIQUID) {
276 need.reset(new IngestNeed(comp.resource, intake, penalty));
277 need->gain = intake * 0.1;
279 need.reset(new InhaleNeed(comp.resource, intake, penalty));
280 need->gain = intake * 0.5;
282 need->name = assets.data.resources[comp.resource].label;
283 need->inconvenient = 0.5;
284 need->critical = 0.95;
285 c.AddNeed(std::move(need));
288 c.Size(std::cbrt(volume));
291 Situation::Situation()
298 Situation::~Situation() {
301 bool Situation::OnPlanet() const noexcept {
302 return type == PLANET_SURFACE;
305 bool Situation::OnSurface() const noexcept {
306 return type == PLANET_SURFACE;
309 world::Tile &Situation::GetTile() const noexcept {
310 double side_length = planet->SideLength();
311 double offset = side_length * 0.5;
312 double x = std::max(0.0, std::min(side_length, position.x + offset));
313 double y = std::max(0.0, std::min(side_length, position.y + offset));
314 return planet->TileAt(surface, int(x), int(y));
317 const world::TileType &Situation::GetTileType() const noexcept {
318 return planet->GetSimulation().TileTypes()[GetTile().type];
321 void Situation::Move(const glm::dvec3 &dp) noexcept {
324 // enforce ground constraint
326 position[(Surface() + 2) % 3] = std::max(0.0, position[(Surface() + 2) % 3]);
328 position[(Surface() + 2) % 3] = std::min(0.0, position[(Surface() + 2) % 3]);
333 void Situation::SetPlanetSurface(world::Planet &p, int srf, const glm::dvec3 &pos) noexcept {
334 type = PLANET_SURFACE;
349 Steering::~Steering() {
352 void Steering::Halt() noexcept {
357 void Steering::GoTo(const glm::dvec3 &t) noexcept {
363 glm::dvec3 Steering::Acceleration(Creature &c) const noexcept {
366 SumForce(acc, c.Velocity() * -max_accel);
369 glm::dvec3 diff = seek_target - c.GetSituation().Position();
370 if (!allzero(diff)) {
371 SumForce(acc, ((normalize(diff) * max_speed) - c.Velocity()) * max_accel);
377 bool Steering::SumForce(glm::dvec3 &out, const glm::dvec3 &in) const noexcept {
378 if (allzero(in) || anynan(in)) {
381 double cur = allzero(out) ? 0.0 : length(out);
382 double rem = max_accel - cur;
386 double add = length(in);
388 // this method is off if in and out are in different
389 // directions, but gives okayish results
390 out += in * (1.0 / add);