]> git.localhorst.tv Git - blobs.git/blob - src/world/sim.cpp
track top ten for each record
[blobs.git] / src / world / sim.cpp
1 #include "Record.hpp"
2 #include "Simulation.hpp"
3
4 #include "Body.hpp"
5 #include "Planet.hpp"
6 #include "Sun.hpp"
7 #include "../creature/Creature.hpp"
8 #include "../ui/string.hpp"
9
10 #include <algorithm>
11 #include <iostream>
12
13
14 namespace blobs {
15 namespace world {
16
17 int Record::Update(creature::Creature &c, double value, double time) noexcept {
18         int found = -1;
19         for (int i = 0; i < MAX; ++i) {
20                 if (value > rank[i].value) {
21                         found = i;
22                         break;
23                 }
24         }
25         if (found < 0) {
26                 return -1;
27         }
28         int previous = -1;
29         for (int i = 0; i < MAX; ++i) {
30                 if (rank[i].holder == &c) {
31                         previous = i;
32                         break;
33                 }
34         }
35         if (previous < 0) {
36                 // move all below down by one
37                 std::copy_backward(rank + found, rank + MAX - 1, rank + MAX);
38         } else if (found > previous) {
39                 // better than last, but not an improvement
40                 // this ensures only one slot occupied per creature
41                 return found;
42         } else if (found < previous) {
43                 // move all in between down by one
44                 std::copy_backward(rank + found, rank + previous, rank + previous + 1);
45         }
46         // insert new
47         rank[found].holder = &c;
48         rank[found].value = value;
49         rank[found].time = time;
50         return found;
51 }
52
53 std::string Record::ValueString(int i) const {
54         if (i < 0 || i >= MAX || !rank[i].holder) {
55                 return "—";
56         }
57         switch (type) {
58                 default:
59                 case VALUE:
60                         return ui::DecimalString(rank[i].value, 2);
61                 case LENGTH:
62                         return ui::LengthString(rank[i].value);
63                 case MASS:
64                         return ui::MassString(rank[i].value);
65                 case PERCENTAGE:
66                         return ui::PercentageString(rank[i].value);
67                 case TIME:
68                         return ui::TimeString(rank[i].value);
69         }
70 }
71
72 Simulation::Simulation(Body &r, app::Assets &assets)
73 : root(r)
74 , assets(assets)
75 , bodies()
76 , planets()
77 , suns()
78 , alive()
79 , dead()
80 , time(0.0)
81 , records(7) {
82         AddBody(r);
83         records[0].name = "Age";
84         records[0].type = Record::TIME;
85         records[1].name = "Mass";
86         records[1].type = Record::MASS;
87         records[2].name = "Size";
88         records[2].type = Record::LENGTH;
89         records[3].name = "Strength";
90         records[4].name = "Stamina";
91         records[5].name = "Dexerty";
92         records[6].name = "Intelligence";
93 }
94
95 Simulation::~Simulation() {
96         for (auto c : alive) {
97                 delete c;
98         }
99         for (auto c : dead) {
100                 delete c;
101         }
102 }
103
104 void Simulation::Tick(double dt) {
105         time += dt;
106         for (auto body : bodies) {
107                 body->Tick(dt);
108         }
109         for (auto c : alive) {
110                 CheckRecords(*c);
111         }
112 }
113
114 void Simulation::AddBody(Body &b) {
115         b.SetSimulation(*this);
116         bodies.insert(&b);
117 }
118
119 void Simulation::AddPlanet(Planet &p) {
120         AddBody(p);
121         planets.insert(&p);
122 }
123
124 void Simulation::AddSun(Sun &s) {
125         AddBody(s);
126         suns.insert(&s);
127 }
128
129 void Simulation::SetAlive(creature::Creature *c) {
130         alive.push_back(c);
131 }
132
133 void Simulation::SetDead(creature::Creature *c) {
134         auto entry = std::find(alive.begin(), alive.end(), c);
135         if (entry != alive.end()) {
136                 alive.erase(entry);
137         }
138         dead.push_back(c);
139         CheckRecords(*c);
140 }
141
142 void Simulation::CheckRecords(creature::Creature &c) noexcept {
143         { // age
144                 creature::Creature *prev = records[0].rank[0].holder;
145                 int rank = records[0].Update(c, c.Age(), time);
146                 if (rank == 0 && prev && prev != &c) {
147                         LogRecord(records[0]);
148                 }
149         }
150         { // mass
151                 creature::Creature *prev = records[1].rank[0].holder;
152                 int rank = records[1].Update(c, c.Mass(), time);
153                 if (rank == 0 && prev && prev != &c) {
154                         LogRecord(records[1]);
155                 }
156         }
157         { // size
158                 creature::Creature *prev = records[2].rank[0].holder;
159                 int rank = records[2].Update(c, c.Size(), time);
160                 if (rank == 0 && prev && prev != &c) {
161                         LogRecord(records[2]);
162                 }
163         }
164         { // strength
165                 creature::Creature *prev = records[3].rank[0].holder;
166                 int rank = records[3].Update(c, c.Strength(), time);
167                 if (rank == 0 && prev && prev != &c) {
168                         LogRecord(records[3]);
169                 }
170         }
171         { // stamina
172                 creature::Creature *prev = records[4].rank[0].holder;
173                 int rank = records[4].Update(c, c.Stamina(), time);
174                 if (rank == 0 && prev && prev != &c) {
175                         LogRecord(records[4]);
176                 }
177         }
178         { // dexerty
179                 creature::Creature *prev = records[5].rank[0].holder;
180                 int rank = records[5].Update(c, c.Dexerty(), time);
181                 if (rank == 0 && prev && prev != &c) {
182                         LogRecord(records[5]);
183                 }
184         }
185         { // intelligence
186                 creature::Creature *prev = records[6].rank[0].holder;
187                 int rank = records[6].Update(c, c.Intelligence(), time);
188                 if (rank == 0 && prev && prev != &c) {
189                         LogRecord(records[6]);
190                 }
191         }
192 }
193
194 void Simulation::LogRecord(const Record &r) {
195         Log() << "at age " << ui::TimeString(r.rank[0].holder->Age()) << " "
196                 << r.rank[0].holder->Name() << " broke the " << r.name << " record of "
197                 << r.ValueString(1) << " by " << r.rank[1].holder->Name()
198                 << " (established " << ui::TimeString(r.rank[1].time) << ")" << std::endl;
199 }
200
201 std::ostream &Simulation::Log() {
202         return std::cout << '[' << ui::TimeString(Time()) << "] ";
203 }
204
205 }
206 }