]> git.localhorst.tv Git - blobs.git/blob - src/creature/creature.cpp
92fa36795b2cea94e40f8c395c98e5cdb59c1e83
[blobs.git] / src / creature / creature.cpp
1 #include "Creature.hpp"
2 #include "Genome.hpp"
3 #include "Memory.hpp"
4 #include "NameGenerator.hpp"
5 #include "Situation.hpp"
6 #include "Steering.hpp"
7
8 #include "Goal.hpp"
9 #include "IdleGoal.hpp"
10 #include "InhaleNeed.hpp"
11 #include "IngestNeed.hpp"
12 #include "Need.hpp"
13 #include "../app/Assets.hpp"
14 #include "../world/Body.hpp"
15 #include "../world/Planet.hpp"
16 #include "../world/Simulation.hpp"
17 #include "../world/TileType.hpp"
18
19 #include <algorithm>
20 #include <sstream>
21 #include <glm/gtx/transform.hpp>
22
23 #include <iostream>
24 #include <glm/gtx/io.hpp>
25
26
27 namespace blobs {
28 namespace creature {
29
30 Creature::Creature(world::Simulation &sim)
31 : sim(sim)
32 , name()
33 , genome()
34 , properties()
35 , cur_prop(0)
36 , base_color(1.0)
37 , highlight_color(0.0)
38 , mass(1.0)
39 , density(1.0)
40 , size(1.0)
41 , birth(sim.Time())
42 , health(1.0)
43 , on_death()
44 , removable(false)
45 , memory(*this)
46 , needs()
47 , goals()
48 , situation()
49 , steering()
50 , vao() {
51 }
52
53 Creature::~Creature() {
54 }
55
56 glm::dvec4 Creature::HighlightColor() const noexcept {
57         return glm::dvec4(highlight_color, AgeLerp(CurProps().highlight, NextProps().highlight));
58 }
59
60 void Creature::Grow(double amount) noexcept {
61         const double max_mass = AgeLerp(CurProps().mass, NextProps().mass);
62         Mass(std::min(max_mass, mass + amount));
63 }
64
65 void Creature::Hurt(double dt) noexcept {
66         health = std::max(0.0, health - dt);
67         if (health == 0.0) {
68                 std::cout << "[" << int(sim.Time()) << "s] "
69                 << name << " died" << std::endl;
70                 Die();
71         }
72 }
73
74 void Creature::Die() noexcept {
75         needs.clear();
76         goals.clear();
77         steering.Halt();
78         if (on_death) {
79                 on_death(*this);
80         }
81         Remove();
82 }
83
84 double Creature::Size() const noexcept {
85         return size;
86 }
87
88 double Creature::Age() const noexcept {
89         return sim.Time() - birth;
90 }
91
92 double Creature::AgeLerp(double from, double to) const noexcept {
93         return glm::mix(from, to, glm::smoothstep(CurProps().age, NextProps().age, Age()));
94 }
95
96 double Creature::Fertility() const noexcept {
97         return AgeLerp(CurProps().fertility, NextProps().fertility) / 3600.0;
98 }
99
100 void Creature::AddGoal(std::unique_ptr<Goal> &&g) {
101         std::cout << "[" << int(sim.Time()) << "s] " << name << " new goal: " << g->Describe() << std::endl;
102         g->Enable();
103         goals.emplace_back(std::move(g));
104 }
105
106 namespace {
107
108 bool GoalCompare(const std::unique_ptr<Goal> &a, const std::unique_ptr<Goal> &b) {
109         return b->Urgency() < a->Urgency();
110 }
111
112 }
113
114 void Creature::Tick(double dt) {
115         if (cur_prop < 5 && Age() > properties.props[cur_prop + 1].age) {
116                 if (cur_prop == 4) {
117                         std::cout << "[" << int(sim.Time()) << "s] "
118                                 << name << " died of old age" << std::endl;
119                         Die();
120                 } else {
121                         ++cur_prop;
122                 }
123         }
124
125         {
126                 Situation::State state(situation.GetState());
127                 Situation::Derivative a(Step(Situation::Derivative(), 0.0));
128                 Situation::Derivative b(Step(a, dt * 0.5));
129                 Situation::Derivative c(Step(b, dt * 0.5));
130                 Situation::Derivative d(Step(c, dt));
131                 Situation::Derivative f(
132                         (1.0 / 6.0) * (a.vel + 2.0 * (b.vel + c.vel) + d.vel),
133                         (1.0 / 6.0) * (a.acc + 2.0 * (b.acc + c.acc) + d.acc)
134                 );
135                 state.pos += f.vel * dt;
136                 state.vel += f.acc * dt;
137                 situation.SetState(state);
138         }
139
140         memory.Tick(dt);
141         for (auto &need : needs) {
142                 need->Tick(dt);
143         }
144         for (auto &goal : goals) {
145                 goal->Tick(dt);
146         }
147         // do background stuff
148         for (auto &need : needs) {
149                 need->ApplyEffect(*this, dt);
150         }
151         if (goals.empty()) {
152                 return;
153         }
154         // if active goal can be interrupted, check priorities
155         if (goals.size() > 1 && goals[0]->Interruptible()) {
156                 Goal *old_top = &*goals[0];
157                 std::sort(goals.begin(), goals.end(), GoalCompare);
158                 Goal *new_top = &*goals[0];
159                 if (new_top != old_top) {
160                         std::cout << "[" << int(sim.Time()) << "s] " << name
161                                 << " changing goal from " << old_top->Describe()
162                                 << " to " << new_top->Describe() << std::endl;
163                 }
164         }
165         goals[0]->Action();
166         for (auto goal = goals.begin(); goal != goals.end();) {
167                 if ((*goal)->Complete()) {
168                         std::cout << "[" << int(sim.Time()) << "s] " << name
169                                 << " complete goal: " << (*goal)->Describe() << std::endl;
170                         goals.erase(goal);
171                 } else {
172                         ++goal;
173                 }
174         }
175 }
176
177 Situation::Derivative Creature::Step(const Situation::Derivative &ds, double dt) const noexcept {
178         Situation::State s = situation.GetState();
179         s.pos += ds.vel * dt;
180         s.vel += ds.acc * dt;
181         return { s.vel, steering.Acceleration(s) };
182 }
183
184 glm::dmat4 Creature::LocalTransform() noexcept {
185         // TODO: surface transform
186         const double half_size = size * 0.5;
187         const glm::dvec3 &pos = situation.Position();
188         return glm::translate(glm::dvec3(pos.x, pos.y, pos.z + half_size))
189                 * glm::scale(glm::dvec3(half_size, half_size, half_size));
190 }
191
192 void Creature::BuildVAO() {
193         vao.Bind();
194         vao.BindAttributes();
195         vao.EnableAttribute(0);
196         vao.EnableAttribute(1);
197         vao.EnableAttribute(2);
198         vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
199         vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, normal));
200         vao.AttributePointer<glm::vec3>(2, false, offsetof(Attributes, texture));
201         vao.ReserveAttributes(6 * 4, GL_STATIC_DRAW);
202         {
203                 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
204                 const float offset = 1.0f;
205                 for (int surface = 0; surface < 6; ++surface) {
206                         const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
207                         const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
208
209                         attrib[4 * surface + 0].position[(surface + 0) % 3] = -offset;
210                         attrib[4 * surface + 0].position[(surface + 1) % 3] = -offset;
211                         attrib[4 * surface + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
212                         attrib[4 * surface + 0].normal[(surface + 0) % 3] = 0.0f;
213                         attrib[4 * surface + 0].normal[(surface + 1) % 3] = 0.0f;
214                         attrib[4 * surface + 0].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
215                         attrib[4 * surface + 0].texture.x = tex_u_begin;
216                         attrib[4 * surface + 0].texture.y = 1.0f;
217                         attrib[4 * surface + 0].texture.z = surface;
218
219                         attrib[4 * surface + 1].position[(surface + 0) % 3] = -offset;
220                         attrib[4 * surface + 1].position[(surface + 1) % 3] =  offset;
221                         attrib[4 * surface + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
222                         attrib[4 * surface + 1].normal[(surface + 0) % 3] = 0.0f;
223                         attrib[4 * surface + 1].normal[(surface + 1) % 3] = 0.0f;
224                         attrib[4 * surface + 1].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
225                         attrib[4 * surface + 1].texture.x = tex_u_end;
226                         attrib[4 * surface + 1].texture.y = 1.0f;
227                         attrib[4 * surface + 1].texture.z = surface;
228
229                         attrib[4 * surface + 2].position[(surface + 0) % 3] =  offset;
230                         attrib[4 * surface + 2].position[(surface + 1) % 3] = -offset;
231                         attrib[4 * surface + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
232                         attrib[4 * surface + 2].normal[(surface + 0) % 3] = 0.0f;
233                         attrib[4 * surface + 2].normal[(surface + 1) % 3] = 0.0f;
234                         attrib[4 * surface + 2].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
235                         attrib[4 * surface + 2].texture.x = tex_u_begin;
236                         attrib[4 * surface + 2].texture.y = 0.0f;
237                         attrib[4 * surface + 2].texture.z = surface;
238
239                         attrib[4 * surface + 3].position[(surface + 0) % 3] = offset;
240                         attrib[4 * surface + 3].position[(surface + 1) % 3] = offset;
241                         attrib[4 * surface + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
242                         attrib[4 * surface + 3].normal[(surface + 0) % 3] = 0.0f;
243                         attrib[4 * surface + 3].normal[(surface + 1) % 3] = 0.0f;
244                         attrib[4 * surface + 3].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
245                         attrib[4 * surface + 3].texture.x = tex_u_end;
246                         attrib[4 * surface + 3].texture.y = 0.0f;
247                         attrib[4 * surface + 3].texture.z = surface;
248                 }
249         }
250         vao.BindElements();
251         vao.ReserveElements(6 * 6, GL_STATIC_DRAW);
252         {
253                 auto element = vao.MapElements(GL_WRITE_ONLY);
254                 for (int surface = 0; surface < 3; ++surface) {
255                         element[6 * surface + 0] = 4 * surface + 0;
256                         element[6 * surface + 1] = 4 * surface + 2;
257                         element[6 * surface + 2] = 4 * surface + 1;
258                         element[6 * surface + 3] = 4 * surface + 1;
259                         element[6 * surface + 4] = 4 * surface + 2;
260                         element[6 * surface + 5] = 4 * surface + 3;
261                 }
262                 for (int surface = 3; surface < 6; ++surface) {
263                         element[6 * surface + 0] = 4 * surface + 0;
264                         element[6 * surface + 1] = 4 * surface + 1;
265                         element[6 * surface + 2] = 4 * surface + 2;
266                         element[6 * surface + 3] = 4 * surface + 2;
267                         element[6 * surface + 4] = 4 * surface + 1;
268                         element[6 * surface + 5] = 4 * surface + 3;
269                 }
270         }
271         vao.Unbind();
272 }
273
274 void Creature::Draw(graphics::Viewport &viewport) {
275         vao.Bind();
276         vao.DrawTriangles(6 * 6);
277 }
278
279
280 void Spawn(Creature &c, world::Planet &p) {
281         p.AddCreature(&c);
282         c.GetSituation().SetPlanetSurface(p, 0, p.TileCenter(0, p.SideLength() / 2, p.SideLength() / 2));
283
284         // probe surrounding area for common resources
285         int start = p.SideLength() / 2 - 2;
286         int end = start + 5;
287         std::map<int, double> yields;
288         for (int y = start; y < end; ++y) {
289                 for (int x = start; x < end; ++x) {
290                         const world::TileType &t = p.TypeAt(0, x, y);
291                         for (auto yield : t.resources) {
292                                 yields[yield.resource] += yield.ubiquity;
293                         }
294                 }
295         }
296         int liquid = -1;
297         int solid = -1;
298         for (auto e : yields) {
299                 if (c.GetSimulation().Resources()[e.first].state == world::Resource::LIQUID) {
300                         if (liquid < 0 || e.second > yields[liquid]) {
301                                 liquid = e.first;
302                         }
303                 } else if (c.GetSimulation().Resources()[e.first].state == world::Resource::SOLID) {
304                         if (solid < 0 || e.second > yields[solid]) {
305                                 solid = e.first;
306                         }
307                 }
308         }
309
310         Genome genome;
311
312         genome.properties.Birth().age = { 0.0, 0.0 };
313         genome.properties.Birth().mass = { 0.5, 0.05 };
314         genome.properties.Birth().fertility = { 0.0, 0.0 };
315         genome.properties.Birth().highlight = { 0.0, 0.0 };
316
317         genome.properties.Child().age = { 30.0, 1.0 };
318         genome.properties.Child().mass = { 0.7, 0.05 };
319         genome.properties.Child().fertility = { 0.0, 0.0 };
320         genome.properties.Child().highlight = { 0.2, 0.05 };
321
322         genome.properties.Youth().age = { 60.0, 5.0 };
323         genome.properties.Youth().mass = { 0.9, 0.1 };
324         genome.properties.Youth().fertility = { 0.5, 0.03 };
325         genome.properties.Youth().highlight = { 0.9, 0.1 };
326
327         genome.properties.Adult().age = { 120.0, 10.0 };
328         genome.properties.Adult().mass = { 1.2, 0.1 };
329         genome.properties.Adult().fertility = { 0.4, 0.01 };
330         genome.properties.Adult().highlight = { 0.7, 0.1 };
331
332         genome.properties.Elder().age = { 360.0, 30.0 };
333         genome.properties.Elder().mass = { 1.0, 0.05 };
334         genome.properties.Elder().fertility = { 0.1, 0.01 };
335         genome.properties.Elder().highlight = { 0.6, 0.1 };
336
337         genome.properties.Death().age = { 480.0, 60.0 };
338         genome.properties.Death().mass = { 0.9, 0.05 };
339         genome.properties.Death().fertility = { 0.0, 0.0 };
340         genome.properties.Death().highlight = { 0.5, 0.1 };
341
342         glm::dvec3 color_avg(0.0);
343         double color_divisor = 0.0;
344
345         if (p.HasAtmosphere()) {
346                 genome.composition.push_back({
347                         p.Atmosphere(),    // resource
348                         { 0.01, 0.00001 }, // mass
349                         { 0.5,  0.001 },   // intake
350                         { 0.1,  0.0005 },  // penalty
351                         { 0.0,  0.0 },     // growth
352                 });
353                 color_avg += c.GetSimulation().Resources()[p.Atmosphere()].base_color * 0.1;
354                 color_divisor += 0.1;
355         }
356         if (liquid > -1) {
357                 genome.composition.push_back({
358                         liquid,          // resource
359                         { 0.6,  0.01 },  // mass
360                         { 0.2,  0.001 }, // intake
361                         { 0.01, 0.002 }, // penalty
362                         { 0.1, 0.0 },   // growth
363                 });
364                 color_avg += c.GetSimulation().Resources()[liquid].base_color * 0.5;
365                 color_divisor += 0.5;
366         }
367         if (solid > -1) {
368                 genome.composition.push_back({
369                         solid,             // resource
370                         { 0.4,   0.01 },   // mass
371                         { 0.4,   0.001 },  // intake
372                         { 0.001, 0.0001 }, // penalty
373                         { 10.0,  0.002 },   // growth
374                 });
375                 color_avg += c.GetSimulation().Resources()[solid].base_color;
376                 color_divisor += 1.0;
377         }
378
379         if (color_divisor > 0.001) {
380                 color_avg /= color_divisor;
381         }
382         glm::dvec3 hsl = rgb2hsl(color_avg);
383         genome.base_hue = { hsl.x, 0.01 };
384         genome.base_saturation = { hsl.y, 0.01 };
385         genome.base_lightness = { hsl.z, 0.01 };
386
387         genome.Configure(c);
388 }
389
390 void Genome::Configure(Creature &c) const {
391         c.GetGenome() = *this;
392
393         math::GaloisLFSR &random = c.GetSimulation().Assets().random;
394
395         c.GetProperties() = Instantiate(properties, random);
396
397         double mass = 0.0;
398         double volume = 0.0;
399         for (const auto &comp : composition) {
400                 double comp_mass = comp.mass.FakeNormal(random.SNorm());
401                 double intake = comp.intake.FakeNormal(random.SNorm());
402                 double penalty = comp.penalty.FakeNormal(random.SNorm());
403
404                 mass += comp_mass;
405                 volume += comp_mass / c.GetSimulation().Resources()[comp.resource].density;
406
407                 std::unique_ptr<Need> need;
408                 if (c.GetSimulation().Resources()[comp.resource].state == world::Resource::SOLID) {
409                         need.reset(new IngestNeed(comp.resource, intake, penalty));
410                         need->gain = intake * 0.05;
411                 } else if (c.GetSimulation().Resources()[comp.resource].state == world::Resource::LIQUID) {
412                         need.reset(new IngestNeed(comp.resource, intake, penalty));
413                         need->gain = intake * 0.1;
414                 } else {
415                         need.reset(new InhaleNeed(comp.resource, intake, penalty));
416                         need->gain = intake * 0.5;
417                 }
418                 need->name = c.GetSimulation().Resources()[comp.resource].label;
419                 need->growth = comp.growth.FakeNormal(random.SNorm());
420                 need->inconvenient = 0.5;
421                 need->critical = 0.95;
422                 c.AddNeed(std::move(need));
423         }
424
425         glm::dvec3 base_color(
426                 std::fmod(base_hue.FakeNormal(random.SNorm()) + 1.0, 1.0),
427                 glm::clamp(base_saturation.FakeNormal(random.SNorm()), 0.0, 1.0),
428                 glm::clamp(base_lightness.FakeNormal(random.SNorm()), 0.0, 1.0)
429         );
430         glm::dvec3 highlight_color(
431                 std::fmod(base_color.x + 0.5, 1.0),
432                 1.0 - base_color.y,
433                 1.0 - base_color.z
434         );
435         c.BaseColor(hsl2rgb(base_color));
436         c.HighlightColor(hsl2rgb(highlight_color));
437
438         c.Mass(c.GetProperties().props[0].mass);
439         c.Density(mass / volume);
440         c.GetSteering().MaxAcceleration(1.4);
441         c.GetSteering().MaxSpeed(4.4);
442         c.AddGoal(std::unique_ptr<Goal>(new IdleGoal(c)));
443 }
444
445
446 void Split(Creature &c) {
447         Creature *a = new Creature(c.GetSimulation());
448         const Situation &s = c.GetSituation();
449         a->Name(c.GetSimulation().Assets().name.Sequential());
450         // TODO: mutate
451         c.GetGenome().Configure(*a);
452         s.GetPlanet().AddCreature(a);
453         // TODO: duplicate situation somehow
454         a->GetSituation().SetPlanetSurface(
455                 s.GetPlanet(), s.Surface(),
456                 s.Position() + glm::dvec3(0.0, a->Size() * 0.51, 0.0));
457         a->BuildVAO();
458
459         Creature *b = new Creature(c.GetSimulation());
460         b->Name(c.GetSimulation().Assets().name.Sequential());
461         c.GetGenome().Configure(*b);
462         s.GetPlanet().AddCreature(b);
463         b->GetSituation().SetPlanetSurface(
464                 s.GetPlanet(), s.Surface(),
465                 s.Position() + glm::dvec3(0.0, b->Size() * -0.51, 0.0));
466         b->BuildVAO();
467
468         c.Die();
469 }
470
471
472 Memory::Memory(Creature &c)
473 : c(c) {
474 }
475
476 Memory::~Memory() {
477 }
478
479 void Memory::Tick(double dt) {
480         Situation &s = c.GetSituation();
481         if (s.OnSurface()) {
482                 TrackStay({ &s.GetPlanet(), s.Surface(), s.SurfacePosition() }, dt);
483         }
484 }
485
486 void Memory::TrackStay(const Location &l, double t) {
487         const world::TileType &type = l.planet->TypeAt(l.surface, l.coords.x, l.coords.y);
488         auto entry = known_types.find(type.id);
489         if (entry != known_types.end()) {
490                 entry->second.last_been = c.GetSimulation().Time();
491                 entry->second.last_loc = l;
492                 entry->second.time_spent += t;
493         } else {
494                 known_types.emplace(type.id, Stay{
495                         c.GetSimulation().Time(),
496                         l,
497                         c.GetSimulation().Time(),
498                         l,
499                         t
500                 });
501         }
502 }
503
504
505 NameGenerator::NameGenerator()
506 : counter(0) {
507 }
508
509 NameGenerator::~NameGenerator() {
510 }
511
512 std::string NameGenerator::Sequential() {
513         std::stringstream ss;
514         ss << "Blob " << ++counter;
515         return ss.str();
516 }
517
518
519 Situation::Situation()
520 : planet(nullptr)
521 , state(glm::dvec3(0.0), glm::dvec3(0.0))
522 , surface(0)
523 , type(LOST) {
524 }
525
526 Situation::~Situation() {
527 }
528
529 bool Situation::OnPlanet() const noexcept {
530         return type == PLANET_SURFACE;
531 }
532
533 bool Situation::OnSurface() const noexcept {
534         return type == PLANET_SURFACE;
535 }
536
537 bool Situation::OnTile() const noexcept {
538         glm::ivec2 t(planet->SurfacePosition(surface, state.pos));
539         return type == PLANET_SURFACE
540                 && t.x >= 0 && t.x < planet->SideLength()
541                 && t.y >= 0 && t.y < planet->SideLength();
542 }
543
544 glm::ivec2 Situation::SurfacePosition() const noexcept {
545         return planet->SurfacePosition(surface, state.pos);
546 }
547
548 world::Tile &Situation::GetTile() const noexcept {
549         glm::ivec2 t(planet->SurfacePosition(surface, state.pos));
550         return planet->TileAt(surface, t.x, t.y);
551 }
552
553 const world::TileType &Situation::GetTileType() const noexcept {
554         glm::ivec2 t(planet->SurfacePosition(surface, state.pos));
555         return planet->TypeAt(surface, t.x, t.y);
556 }
557
558 void Situation::Move(const glm::dvec3 &dp) noexcept {
559         state.pos += dp;
560         if (OnSurface()) {
561                 // enforce ground constraint
562                 if (Surface() < 3) {
563                         state.pos[(Surface() + 2) % 3] = std::max(0.0, state.pos[(Surface() + 2) % 3]);
564                 } else {
565                         state.pos[(Surface() + 2) % 3] = std::min(0.0, state.pos[(Surface() + 2) % 3]);
566                 }
567         }
568 }
569
570 void Situation::SetPlanetSurface(world::Planet &p, int srf, const glm::dvec3 &pos) noexcept {
571         type = PLANET_SURFACE;
572         planet = &p;
573         surface = srf;
574         state.pos = pos;
575 }
576
577
578 Steering::Steering()
579 : seek_target(0.0)
580 , max_accel(1.0)
581 , max_speed(1.0)
582 , halting(false)
583 , seeking(false) {
584 }
585
586 Steering::~Steering() {
587 }
588
589 void Steering::Halt() noexcept {
590         halting = true;
591         seeking = false;
592 }
593
594 void Steering::GoTo(const glm::dvec3 &t) noexcept {
595         seek_target = t;
596         halting = false;
597         seeking = true;
598 }
599
600 glm::dvec3 Steering::Acceleration(const Situation::State &s) const noexcept {
601         glm::dvec3 acc(0.0);
602         if (halting) {
603                 SumForce(acc, s.vel * -max_accel);
604         }
605         if (seeking) {
606                 glm::dvec3 diff = seek_target - s.pos;
607                 if (!allzero(diff)) {
608                         SumForce(acc, ((normalize(diff) * max_speed) - s.vel) * max_accel);
609                 }
610         }
611         return acc;
612 }
613
614 bool Steering::SumForce(glm::dvec3 &out, const glm::dvec3 &in) const noexcept {
615         if (allzero(in) || anynan(in)) {
616                 return false;
617         }
618         double cur = allzero(out) ? 0.0 : length(out);
619         double rem = max_accel - cur;
620         if (rem < 0.0) {
621                 return true;
622         }
623         double add = length(in);
624         if (add > rem) {
625                 // this method is off if in and out are in different
626                 // directions, but gives okayish results
627                 out += in * (1.0 / add);
628                 return true;
629         } else {
630                 out += in;
631                 return false;
632         }
633 }
634
635 }
636 }