-
Notifications
You must be signed in to change notification settings - Fork 2
/
agent.cpp
1361 lines (1259 loc) · 43.5 KB
/
agent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "agent.h"
#include <cassert>
#include <iostream>
#include <stdexcept>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <set>
#include <random>
#include <chrono>
#include "agent_type.h"
#include "biology.h"
#include "game.h"
using namespace sf;
agent::agent(
const agent_type type,
const double x,
const double y,
const double health,
const double direction,
std::vector<agent_type> prey
) : m_type{type},
m_x{x},
m_y{y},
m_direction{direction},
m_health{health},
m_stamina{100},
m_prey{prey}
{
//An agent of type none is used when a 'null' agent is needed
}
std::ostream& operator<<(std::ostream& os, const agent& a) noexcept
{
os << a.m_type << ' ' << a.m_x << ' ' << a.m_y << ' '
<< a.m_health << ' ' << a.m_stamina;
return os;
}
std::istream& operator>>(std::istream& is, agent& a)
{
is >> a.m_type >> a.m_x >> a.m_y >> a.m_health >> a.m_stamina;
return is;
}
bool operator==(const agent& lhs, const agent& rhs) noexcept{
return
lhs.m_type == rhs.m_type &&
lhs.m_x == rhs.m_x &&
lhs.m_y == rhs.m_y &&
lhs.m_health == rhs.m_health &&
lhs.m_stamina == rhs.m_stamina
;
}
double get_agent_reproduction_health(const agent_type t) noexcept
{
switch(t)
{
case (agent_type::tree):
return 500.0;
default:
return 100.0;
}
}
double pythagoras(double x_length, double y_length)
{
return sqrt((x_length * x_length) + (y_length * y_length));
}
std::vector<agent_type> can_eat(const agent_type type) {
switch (type) {
case agent_type::chameleon:
return {agent_type::worm,
agent_type::spider,
agent_type::bird};
case agent_type::crocodile:
return {agent_type::cow,
agent_type::giraffe,
agent_type::fish,
agent_type::goat,
agent_type::squirrel};
case agent_type::polar_bear:
return {agent_type::fish};
case agent_type::squirrel:
return {agent_type::tree};
case agent_type::whale:
return {agent_type::fish};
case agent_type::snake:
return {agent_type::squirrel};
case agent_type::venus_fly_trap:
return {agent_type::spider,
agent_type::worm};
case agent_type::bird:
return {agent_type::spider,
agent_type::fish,
agent_type::snake,
agent_type::worm};
case agent_type::cow:
return {agent_type::grass};
case agent_type::lion:
return {agent_type::cow,
agent_type::giraffe};
case agent_type::giraffe:
return {agent_type::tree};
//case agent_type::venus_fly_trap:
// return {agent_type::spider };
default:
return {};
}
}
bool is_plant(const agent_type type) noexcept {
//Some plankton are also bacteria, archea, protozoa or animals
return type == agent_type::plankton ||
type == agent_type::grass ||
type == agent_type::tree ||
type == agent_type::cactus ||
type == agent_type::foxgloves ||
type == agent_type::venus_fly_trap ||
type == agent_type::sunflower;
}
void agent::eat(game& g) { //!OCLINT high compexity
//Plants do not eat
if (is_plant(m_type)) return;
//Is agent_type a in food?
for (agent& other : g.get_agents()) {
//Agents never eat themselves
if (this == &other) continue;
// Focal agent will not eat corpses
if (other.get_health() <= 0.0) continue;
//Skip other agent if it is not in range
// NOTE not calculated from the center of the agent
if (!is_in_range(other.get_x(), other.get_y(), 25.0)) continue;
//Skip other agent if it is not a prey type
const agent_type prey_type = other.get_type();
if (std::count(std::begin(m_prey), std::end(m_prey), prey_type) == 0) continue;
// Focal agent will eat the prey
// As in any food chain, energy is lost: the predator gains less energy
// than the prey gains
m_health += biology().get_health_increase_when_eating();
m_stamina += biology().get_stamina_increase_when_eating();
other.set_health(other.get_health() - biology().get_health_decrease_when_eaten());
}
}
bool agent::is_in_range(double x, double y, double range) {
// NOTE not calculated from the center of the agent
// sfml_resources::get().get_agent_sprite(*this).getSize() / 2
return x > m_x - range &&
x < m_x + range &&
y > m_y - range &&
y < m_y + range;
}
//NOTE unused right now, kept for possible later use
//agent agent::nearest_agent(const game& g, agent& a, agent_type type){
// double minD = pythagoras(1000000, 1000000);
// agent near_agent(type);
// for(const agent& ag: g.get_agents()){
// if(ag.get_type() == type){
// double distance = pythagoras(fabs(ag.get_x() - a.get_x()), fabs(ag.get_y() - a.get_y()));
// if(distance < minD){
// minD = distance;
// near_agent = ag;
// }
// }
// }
// return near_agent;
//}
void agent::move(double x, double y)
{
m_x += x;
m_y += y;
}
void agent::move(game &g){ //!OCLINT too complex indeed
// Plants don't move to their food
if (is_plant(m_type)) {
return;
}
//An exhausted agent loses health
if (m_stamina <= 0.0) {
const double change_in_health = 0.2 * (m_stamina - 1.0);
assert(change_in_health <= 0.0);
m_health += change_in_health;
}
//Dead agents stay still
if (m_health <= 0.0) return;
//Corpses stay still
if (m_type == agent_type::corpse) return;
//Move randomly a bit
double temp_x = 0.1 * (-1 + (std::rand() % 3));
double temp_y = 0.1 * (-1 + (std::rand() % 3));
sf::Vector2f center_temp = get_agent_center(*this);
std::vector<tile> t_temp = get_current_tile(g, center_temp.x + temp_x, center_temp.y + temp_y);
if((is_on_tile(g, center_temp.x + temp_x, center_temp.y + temp_y)
&& t_temp[0].get_type() != tile_type::water)
|| (!will_drown(m_type, 12.34) && is_on_tile(g, center_temp.x + temp_x, center_temp.y + temp_y))
|| m_type == agent_type::bird){
m_x += temp_x;
m_y += temp_y;
}
// As long as we don't have a random seed, this can't be used here
// m_x += 0.1 * (-1 + random_double(0, 3));
// m_y += 0.1 * (-1 + random_double(0, 3));
// This doesn't work properly
//
// unsigned int rand = static_cast<unsigned int>(random_int(0, count_n_agents(g) - 1,
// std::rand()));
// std::cout << rand << std::endl;
// agent a = g.get_agents()[rand];
// if(std::find(m_prey.begin(), m_prey.end(), a.get_type()) != m_prey.end()){
// double distance = pythagoras(fabs(m_x - a.get_x()), fabs(m_y - a.get_y()));
// const double vector_length = std::exp(-distance/1000);
// m_dx_motivation += -(0.01 * (m_x - a.get_x())) / vector_length;
// m_dy_motivation += -(0.01 * (m_y - a.get_y())) / vector_length;
// //std::cout << vector_length << " " << rand << " " << count_n_agents(g) << std::endl;
// //std::cout << m_dx_motivation << " + " << m_dy_motivation << " " << rand << std::endl;
// m_x += std::max(-0.35, std::min(m_dx_motivation, 0.35));
// m_y += std::max(-0.35, std::min(m_dy_motivation, 0.35));
// }
if (!destination.empty())
{
double x = -(0.005 * (m_x - destination[0].get_x()));
x = std::max(-0.05, std::min(x, 0.05));
double y = -(0.005 * (m_y - destination[0].get_y()));
y = std::max(-0.05, std::min(y, 0.05));
sf::Vector2f center = get_agent_center(*this);
std::vector<tile> t = get_current_tile(g, center.x + x*2, center.y + y*2);
if((is_on_tile(g, center.x + x*2, center.y + y*2)
&& t[0].get_type() != tile_type::water)
|| m_type == agent_type::bird){
m_x += x;
m_y += y;
}
}
}
void agent::find_destination(game &g){
std::vector<agent> v;
for(const agent& a : g.get_agents())
{
if(std::find(m_prey.begin(), m_prey.end(), a.get_type()) == m_prey.end()) continue;
if (v.empty())
{
v.push_back(a);
}
else
{
const double distance_v = pythagoras(fabs(m_x - v[0].get_x()), fabs(m_y - v[0].get_y()));
const double distance_a = pythagoras(fabs(m_x - a.get_x()), fabs(m_y - a.get_y()));
if (distance_a <distance_v)
{
v[0] = a;
}
}
}
destination = v;
}
void agent::attract_to_agent(game &g, agent_type type){
std::vector<agent> v;
for(const agent& a : g.get_agents())
{
if(a.get_type() != type) continue;
if (v.empty())
{
v.push_back(a);
}
else
{
const double distance_v = pythagoras(abs(m_x - v[0].get_x()), abs(m_y - v[0].get_y()));
const double distance_a = pythagoras(abs(m_x - a.get_x()), abs(m_y - a.get_y()));
if (distance_a <distance_v)
{
v[0] = a;
}
}
}
if (!v.empty())
{
double x = -(0.01 * (m_x - v[0].get_x()));
x = std::max(-0.05, std::min(x, 0.05));
m_x += x;
double y = -(0.01 * (m_y - v[0].get_y()));
y = std::max(-0.05, std::min(y, 0.05));
m_y += y;
return;
}
}
std::vector<agent> agent::process_events(game& g) { //!OCLINT NPath complexity too high
//Do not change game::m_agents in this function!
//Measure at start of function, will check at end as well
const int n_agents_before = static_cast<int>(g.get_agents().size());
std::vector <agent> new_agents;
//Sessile and aquatic species die instantly when on void
if(m_type != agent_type::bird && !is_on_tile(g, *this) && m_type != agent_type::corpse)
{
m_health = 0.0;
return new_agents;
}
if(m_type == agent_type::corpse && corpse_ticks == -1){
corpse_ticks = g.get_n_ticks();
}
//Agents always lose stamina
m_stamina -= 0.0175;
if(g.get_n_ticks() % 100 == 0) find_destination(g);
move(g);
if(m_type == agent_type::spider) attract_to_agent(g, agent_type::venus_fly_trap);
if ((m_type == agent_type::grass || m_type == agent_type::tree
|| m_type == agent_type::cow) && g.allow_damage())
{
const auto kids = reproduce_agents(g, m_type);
std::copy(std::begin(kids), std::end(kids), std::back_inserter(new_agents));
}
//Plants damage each other when nearby
if (is_plant(m_type))
damage_own_type(g, m_type);
// Zero or one tile that the agent is on
const std::vector<tile> tiles = get_on_tile(g, *this);
if (tiles.size() ==1 && tiles[0].get_type() == tile_type::water)
{
// A water tile, on which agents can drown
if (will_drown(m_type, tiles[0].get_depth())
&& !get_on_tile_type(g, *this).empty()
&& get_on_tile_type(g, *this).front() == tile_type::water)
{
m_stamina -= 0.2;
}
}
///Eating others
eat(g);
if(m_type == agent_type::fish || m_type == agent_type::whale){
for(tile& t: g.get_tiles()){
if(is_on_specific_tile(*this, t) && t.get_type() != tile_type::water){
m_health -= 0.01;
}
}
}
//Do not change game::m_agents in this function!
//Measure at end of function
const int n_agents_after = static_cast<int>(g.get_agents().size());
assert(n_agents_before == n_agents_after);
return new_agents;
}
std::vector <agent> agent::reproduce_agents(game& g, agent_type type) { //!OCLINT indeed to complex, but get this merged first :-)
std::vector <agent> new_agents;
if(is_plant(type)){
const double rand = random_double(10, 26) / 1000.0; // 20 extra for the grass self-damage
// Grow
m_health += rand;
}
if (m_health > get_agent_reproduction_health(type))
{
//Random fractions, from 0.0 to 1.0
const double f_parent{random_double(0, 1)};
const double f_kid{random_double(0, 1)};
assert(f_parent >= 0.0 && f_parent < 1.0);
assert(f_kid >= 0.0 && f_kid < 1.0);
//Converted to proportions
//Parent agent will get 0.4-0.6 of health
//Kid agent will get 0.1-0.3 of health
const double p_parent{0.4 + (0.2 * f_parent)};
const double p_kid{0.1 + (0.2 * f_parent)};
assert(p_parent >= 0.4 && p_parent < 0.6);
assert(p_kid >= 0.1 && p_kid < 0.3);
//Convert to new healths
const double health_parent{p_parent * m_health};
const double health_kid{p_kid * m_health};
//Kids grow at new spot
const double max_distance{64.0};
double f_x = 0;
double f_y = 0;
double new_x = 0;
double new_y = 0;
agent new_agent(type, new_x, new_y, health_kid, 0, can_eat(type));
std::vector<tile> t;
// bool water = get_on_tile_type(g, new_agent).size() > 0 &&
// get_on_tile_type(g, new_agent).at(0) == tile_type::water;
while (t.empty()
|| !is_on_tile(g, new_agent)
// || !water
|| !is_on_specific_tile(new_agent.get_x() - 6, new_agent.get_y() - 6, t.front())
|| !is_on_specific_tile(new_agent.get_x() + 18, new_agent.get_y() + 18, t.front())
)
{
// As long as we don't have a random seed, this can't be used here
// f_x = random_double(0, 1);
// f_y = random_double(0, 1);
f_x = static_cast<double>(std::rand()) / (1.0 + static_cast<double>(RAND_MAX));
f_y = static_cast<double>(std::rand()) / (1.0 + static_cast<double>(RAND_MAX));
assert(f_x >= 0.0 && f_x < 1.0);
assert(f_y >= 0.0 && f_y < 1.0);
new_x = m_x + (((f_x * 2.0) - 1.0) * max_distance);
new_y = m_y + (((f_y * 2.0) - 1.0) * max_distance);
new_agent.set_x(new_x);
new_agent.set_y(new_y);
if(is_on_tile(g, new_agent)){
t = get_current_tile(g, new_agent);
// water = get_on_tile_type(g, new_agent).size() > 0 &&
// get_on_tile_type(g, new_agent).at(0) == tile_type::water;
}
}
// g.add_agents( { new_agent } );
m_health = health_parent;
new_agents.push_back(new_agent);
// water = get_on_tile_type(g, new_agent).size() > 0 &&
// get_on_tile_type(g, new_agent).at(0) == tile_type::water;
}
return new_agents;
}
void agent::damage_own_type(game &g, agent_type type)
{
const double MAX_DISTANCE = 30; // The max range to deal damage to an object
const double MAX_DAMAGE = 0.18; // The max damage to deal per frame per agent
// std::vector <agent> all_agents{ g.get_agents() };
// for (agent& current_agent : all_agents)
// {
// if (current_agent == *this)
// continue;
// if (current_agent.get_type() == type)
// {
// double distance = pythagoras(fabs(current_agent.get_x() - m_x), fabs(current_agent.get_y() - m_y));
// if (!(distance <= MAX_DISTANCE))
// continue;
// double rate = 1-distance / MAX_DISTANCE;
// double damage = MAX_DAMAGE * rate;
// double relative_damage = damage / (all_agents.size() - 1);
// m_health -= relative_damage;
// }
// }
agent a = g.get_agents()[static_cast<unsigned>(random_int(0, static_cast<int>(g.get_agents().size() - 1)))];
if (a == *this) return;
if (a.get_type() == type)
{
double distance = pythagoras(fabs(a.get_x() - m_x), fabs(a.get_y() - m_y));
if (!(distance <= MAX_DISTANCE))
return;
double rate = 1-distance / MAX_DISTANCE;
double damage = MAX_DAMAGE * rate;
double relative_damage = damage / (g.get_agents().size() - 1);
m_health -= relative_damage;
}
}
std::vector<agent> create_default_agents() noexcept //!OCLINT indeed too long
{
std::vector<agent> agents;
{
agent a1(agent_type::chameleon, 0, 0, 1, 0, can_eat(agent_type::chameleon));
move_agent_to_tile(a1, -3, 0);
agents.push_back(a1);
}
{
agent a1(agent_type::cow, 0, 0, 1, 0, can_eat(agent_type::cow));
move_agent_to_tile(a1, 0, 0);
agents.push_back(a1);
agent a2(agent_type::cow, 40, 70, 1, 0, can_eat(agent_type::cow));
move_agent_to_tile(a2, 0, 0);
agents.push_back(a2);
agent a3(agent_type::grass, 70, 40, 50 + random_double(0, 50)
, 0, can_eat(agent_type::grass));
move_agent_to_tile(a3, 0, 0);
agents.push_back(a3);
agent a4(agent_type::sunflower, 42, 112, 1, 0, can_eat(agent_type::sunflower));
agent a5(agent_type::foxgloves, 60, 70, 1, 0, can_eat(agent_type::foxgloves));
move_agent_to_tile(a4, 0, 0);
agents.push_back(a4);
move_agent_to_tile(a5, 0, 0);
agents.push_back(a5);
}
{
agent a1(agent_type::cow, 0, 0, 1, 0, can_eat(agent_type::cow));
move_agent_to_tile(a1, 1, 0);
agents.push_back(a1);
agent a2(agent_type::cow, 90, 30, 1, 0, can_eat(agent_type::cow));
move_agent_to_tile(a2, 1, 0);
agents.push_back(a2);
agent a3(agent_type::cow, 30, 90, 1, 0, can_eat(agent_type::cow));
move_agent_to_tile(a3, 1, 0);
agents.push_back(a3);
agent a4(agent_type::worm, 50, 130, 1, 0, can_eat(agent_type::worm));
move_agent_to_tile(a4, 1, 0);
agents.push_back(a4);
}
{
agent a1(agent_type::crocodile, 30, 160, 1, 0, can_eat(agent_type::crocodile));
move_agent_to_tile(a1, 0, 2);
agents.push_back(a1);
agent a2(agent_type::snake, 50, 15, 1, 0, can_eat(agent_type::snake));
move_agent_to_tile(a2, 0, 2);
agents.push_back(a2);
agent a3(agent_type::venus_fly_trap, 30, 160, 1000, 0,
can_eat(agent_type::venus_fly_trap));
move_agent_to_tile(a3, 0, 2);
agents.push_back(a3);
agent a4(agent_type::cactus, 10, 15, 100, 0, can_eat(agent_type::cactus));
move_agent_to_tile(a4, 0, 2);
agents.push_back(a4);
agent a5(agent_type::cactus, 30, 120, 100, 0, can_eat(agent_type::cactus));
move_agent_to_tile(a5, 0, 2);
agents.push_back(a5);
}
{
agent a1(agent_type::crocodile, 0, 0, 1, 0, can_eat(agent_type::crocodile));
move_agent_to_tile(a1, 2, 1);
agents.push_back(a1);
agent a2(agent_type::grass, 0, 0, 50 + random_double(0, 50),
0, can_eat(agent_type::grass));
move_agent_to_tile(a2, 2, 1);
agents.push_back(a2);
}
{
agent a1(agent_type::fish, 0, 0, 1, 0, can_eat(agent_type::fish));
move_agent_to_tile(a1, 3, 2);
agents.push_back(a1);
agent a2(agent_type::fish, 10, 10, 1, 0, can_eat(agent_type::fish));
move_agent_to_tile(a2, 3, 2);
agents.push_back(a2);
agent a3(agent_type::octopus, 50, 70, 1, 0, can_eat(agent_type::octopus));
move_agent_to_tile(a3, 3, 2);
agents.push_back(a3);
}
{
agent a1(agent_type::whale, 0, 0, 1, 0, can_eat(agent_type::whale));
move_agent_to_tile(a1, 3, 2);
agents.push_back(a1);
}
{
agent a1(agent_type::whale, 0, 0, 1, 0, can_eat(agent_type::whale));
move_agent_to_tile(a1, 3, 2);
agents.push_back(a1);
}
{
agent a1(agent_type::fish, 0, 0, 1, 0, can_eat(agent_type::fish));
move_agent_to_tile(a1, 4, 2);
agents.push_back(a1);
agent a2(agent_type::fish, 10, 10, 1, 0, can_eat(agent_type::fish));
move_agent_to_tile(a2, 4, 2);
agents.push_back(a2);
}
{
agent a1(agent_type::grass, 0, 0, 50 + random_double(0, 50),
0, can_eat(agent_type::grass));
move_agent_to_tile(a1, 1, -1);
agents.push_back(a1);
agent a2(agent_type::giraffe, 10, 20, 1, 0, can_eat(agent_type::giraffe));
move_agent_to_tile(a2, 1, -1);
agents.push_back(a2);
//agent a3(agent_type::lion, 120, 20);
//move_agent_to_tile(a3, 1, -1);
//agents.push_back(a3);
agent a4(agent_type::crocodile, 180, 20, 1, 0, can_eat(agent_type::crocodile));
move_agent_to_tile(a4, 1, -1);
agents.push_back(a4);
}
{
agent a1(agent_type::tree, 10, 20, 1, 0, can_eat(agent_type::tree));
move_agent_to_tile(a1, 4, -1);
agents.push_back(a1);
agent a2(agent_type::tree, 40, 10, 1, 0, can_eat(agent_type::tree));
move_agent_to_tile(a2, 4, -1);
agents.push_back(a2);
agent a3(agent_type::tree, 50, 35, 1, 0, can_eat(agent_type::tree));
move_agent_to_tile(a3, 4, -1);
agents.push_back(a3);
agent a4(agent_type::tree, 60, 40, 1, 0, can_eat(agent_type::tree));
move_agent_to_tile(a4, 4, -1);
agents.push_back(a4);
agent a5(agent_type::tree, 35, 65, 1, 0, can_eat(agent_type::tree));
move_agent_to_tile(a5, 4, -1);
agents.push_back(a5);
agent a6(agent_type::tree, 0, 0, 1, 0, can_eat(agent_type::tree));
move_agent_to_tile(a6, 4, -1);
agents.push_back(a6);
agent a7(agent_type::spider, 40, 40, 1, 0, can_eat(agent_type::spider));
move_agent_to_tile(a7, 4, -1);
agents.push_back(a7);
agent a8(agent_type::bird, 75, 150, 1, 0, can_eat(agent_type::bird));
move_agent_to_tile(a8, 4, -1);
agents.push_back(a8);
}
{
agent a1(agent_type::tree, 90, 170, 1, 0, can_eat(agent_type::tree));
move_agent_to_tile(a1, 0, -2);
agents.push_back(a1);
agent a2(agent_type::squirrel, 90, 150, 1, 0, can_eat(agent_type::squirrel));
move_agent_to_tile(a2, 0, -2);
agents.push_back(a2);
}
{
agent a1(agent_type::polar_bear, 4, 112, 1, 0, can_eat(agent_type::polar_bear));
move_agent_to_tile(a1, 5, 0);
agents.push_back(a1);
agent a2(agent_type::polar_bear, 4, 112, 1, 0, can_eat(agent_type::polar_bear));
move_agent_to_tile(a2, 4, 0);
agents.push_back(a2);
}
{
agent a1(agent_type::goat, 190, 90, 1, 0, can_eat(agent_type::goat));
move_agent_to_tile(a1, 1, 2);
agents.push_back(a1);
agent a2(agent_type::goat, 50, 80, 1, 0, can_eat(agent_type::goat));
move_agent_to_tile(a2, 1, 2);
agents.push_back(a2);
}
return agents;
}
void move_agent_to_tile(agent &a, double tile_x, double tile_y) {
a.set_x(a.get_x()+(tile_x*112));
a.set_y(a.get_y()+(tile_y*112));
}
bool agent::is_clicked(const double x, const double y,
const Texture& sprite) const noexcept {
return x > m_x - 5 &&
x < m_x + sprite.getSize().x * 0.2 + 5 &&
y > m_y - 5 &&
y < m_y + sprite.getSize().y * 0.2 + 5;
}
sf::Vector2f agent::get_center(const sf::Texture &sprite) const {
return sf::Vector2f(m_x + sprite.getSize().x * 0.2 / 2.0f,
m_y + sprite.getSize().y * 0.2 / 2.0f);
}
bool will_drown(agent_type a, const int depth) { //!OCLINT can't be simpler
const auto range = get_depth_range(a);
switch (a) {
case agent_type::plankton:
return depth > range.x && depth <= range.y;
case agent_type::worm:
return false;
case agent_type::bird:
return false;
case agent_type::cow:
return true;
case agent_type::giraffe:
return true;
case agent_type::crocodile:
return depth > range.x && depth <= range.y;
case agent_type::chameleon:
return true;
case agent_type::fish:
return depth > range.x && depth <= range.y;
case agent_type::whale:
return false;
case agent_type::goat:
return true;
case agent_type::spider:
return true;
case agent_type::octopus:
return depth > range.x && depth <= range.y;
case agent_type::polar_bear:
return false;
case agent_type::snake:
return true;
default:
return true;
}
}
bool is_auqatic(agent_type a){
return a == agent_type::fish ||
a == agent_type::whale ||
a == agent_type::octopus ||
a == agent_type::plankton ||
a == agent_type::crocodile;
}
int get_min_depth(agent_type a){
switch (a) {
case agent_type::fish:
return 0;
case agent_type::whale:
return 50;
case agent_type::octopus:
return 25;
case agent_type::plankton:
return 25;
case agent_type::crocodile:
return 0;
default:
return 0;
}
}
int get_max_depth(agent_type a){
switch (a) {
case agent_type::fish:
return 50;
case agent_type::whale:
return 100;
case agent_type::octopus:
return 75;
case agent_type::plankton:
return 75;
case agent_type::crocodile:
return 25;
default:
return 0;
}
}
sf::Vector2i get_depth_range(agent_type a){
return sf::Vector2i(get_min_depth(a), get_max_depth(a));
}
void test_agent() //!OCLINT testing functions may be long
{
#define FIX_ISSUE_447
#ifdef FIX_ISSUE_447
//Cacti damage nearby cacti
{
// Make two plants next to each other.
game g({tile(0, 0, 3, 3, 10, tile_type::grassland)},
{agent(agent_type::cactus, 10, 10, 10),
agent(agent_type::cactus, 10, 10, 10)});
sound_type st { sound_type::none };
// Check their initial health.
const double prev_health1 = g.get_agents()[0].get_health();
const double prev_health2 = g.get_agents()[1].get_health();
// Damage time
for(int i = 0; i != 100; ++i){
g.process_events(st);
}
// Check their health after doing damage
const double after_health1 = g.get_agents()[0].get_health();
const double after_health2 = g.get_agents()[1].get_health();
// Plants should have damaged each other
assert(after_health1 < prev_health1);
assert(after_health2 < prev_health2);
}
//Foxgloves damage nearby Foxgloves
{
// Make two plants next to each other.
game g({tile(0, 0, 3, 3, 10, tile_type::grassland)},
{agent(agent_type::foxgloves, 10, 10, 10),
agent(agent_type::foxgloves, 10, 10, 10)});
sound_type st { sound_type::none };
// Check their initial health.
const double prev_health1 = g.get_agents()[0].get_health();
const double prev_health2 = g.get_agents()[1].get_health();
// Damage time.
for(int i = 0; i != 100; ++i){
g.process_events(st);
}
// Check their health after doing damage
const double after_health1 = g.get_agents()[0].get_health();
const double after_health2 = g.get_agents()[1].get_health();
// Plants should have damaged each other
assert(after_health1 < prev_health1);
assert(after_health2 < prev_health2);
}
//Plankton damage nearby Plankton
{
// Make two plants next to each other.
game g({tile(0, 0, 3, 3, 10, tile_type::water)},
{agent(agent_type::plankton, 10, 10, 10),
agent(agent_type::plankton, 10, 10, 10)});
sound_type st { sound_type::none };
// Check their initial health.
const double prev_health1 = g.get_agents()[0].get_health();
const double prev_health2 = g.get_agents()[1].get_health();
// Damage time.
for(int i = 0; i != 100; ++i){
g.process_events(st);
}
// Check their health after doing damage
const double after_health1 = g.get_agents()[0].get_health();
const double after_health2 = g.get_agents()[1].get_health();
// Plants should have damaged each other
assert(after_health1 < prev_health1);
assert(after_health2 < prev_health2);
}
//Sunflowers damage nearby Sunflowers
{
// Make two plants next to each other.
game g({tile(0, 0, 3, 3, 10, tile_type::grassland)},
{agent(agent_type::sunflower, 10, 10, 10),
agent(agent_type::sunflower, 10, 10, 10)});
sound_type st { sound_type::none };
// Check their initial health.
const double prev_health1 = g.get_agents()[0].get_health();
const double prev_health2 = g.get_agents()[1].get_health();
// Damage time.
for(int i = 0; i != 100; ++i){
g.process_events(st);
}
// Check their health after doing damage
const double after_health1 = g.get_agents()[0].get_health();
const double after_health2 = g.get_agents()[1].get_health();
// Plants should have damaged each other
assert(after_health1 < prev_health1);
assert(after_health2 < prev_health2);
}
//Venus fly traps damage nearby Venus fly traps
{
// Make two plants next to each other.
game g({tile(0, 0, 3, 3, 10, tile_type::grassland)},
{agent(agent_type::venus_fly_trap, 10, 10, 10),
agent(agent_type::venus_fly_trap, 10, 10, 10)});
sound_type st { sound_type::none };
// Check their initial health.
const double prev_health1 = g.get_agents()[0].get_health();
const double prev_health2 = g.get_agents()[1].get_health();
// Damage time.
for(int i = 0; i != 100; ++i){
g.process_events(st);
}
// Check their health after doing damage
const double after_health1 = g.get_agents()[0].get_health();
const double after_health2 = g.get_agents()[1].get_health();
// Plants should have damaged each other
assert(after_health1 < prev_health1);
assert(after_health2 < prev_health2);
}
#endif // FIX_ISSUE_447
// A default agent has coordinate (0,0)
{
const agent a(agent_type::cow);
assert(a.get_x() == 0.0);
assert(a.get_y() == 0.0);
}
// An agent has the right coordinats
{
const double x{12.34};
const double y{56.78};
const agent a(agent_type::cow, x, y);
assert(a.get_x() == x);
assert(a.get_y() == y);
}
// Check if species are classified as plants
{
assert(!is_plant(agent_type::bird));
assert(!is_plant(agent_type::cow));
assert(!is_plant(agent_type::chameleon));
assert(!is_plant(agent_type::crocodile));
assert(!is_plant(agent_type::fish));
assert(!is_plant(agent_type::giraffe));
assert(!is_plant(agent_type::goat));
assert( is_plant(agent_type::grass));
assert(!is_plant(agent_type::lion));
assert(!is_plant(agent_type::octopus));
assert( is_plant(agent_type::plankton));
assert(!is_plant(agent_type::snake));
assert(!is_plant(agent_type::polar_bear));
assert(!is_plant(agent_type::spider));
assert(!is_plant(agent_type::squirrel));
assert( is_plant(agent_type::cactus));
assert( is_plant(agent_type::tree));
assert( is_plant(agent_type::venus_fly_trap));
assert(!is_plant(agent_type::whale));
assert(!is_plant(agent_type::worm));
}
// A cow moves
{
game g;
const double x{12.345};
const double y{56.789};
agent a(agent_type::cow, x, y);
assert(is_on_tile(g, a));
for (int i = 0; i < 50; i++) a.move(g);
assert(a.get_x() != x || a.get_y() != y);
}
// A crocodile moves
{
game dummy_game; //Unused
const double x{12.345};
const double y{56.789};
agent a(agent_type::crocodile, x, y);
for (int i = 0; i != 50; ++i) a.move(dummy_game); //To make surer x or y is changed
assert(a.get_x() != x || a.get_y() != y);
}
// A fish moves
{
game g;
const double x{12.345};
const double y{56.789};
agent a(agent_type::fish, x, y);
assert(is_on_tile(g, a));
for (int i = 0; i < 50; i++) a.move(g);
assert(a.get_x() != x || a.get_y() != y);
}
// A bird moves
{
game g;
const double x{12.345};
const double y{56.789};
agent a(agent_type::bird, x, y);
assert(is_on_tile(g, a));
for(int i = 0; i < 10; i++) a.move(g);
assert(a.get_x() != x || a.get_y() != y);
}
// Grass does not move
{
game g;
const double x{12.34};
const double y{56.78};
agent a(agent_type::grass, x, y);
assert(is_on_tile(g, a));
a.move(g);
assert(a.get_x() == x && a.get_y() == y);
}
// Venus Fly Trap does not move
{
game g;
const double x{12.34};
const double y{56.78};
agent a(agent_type::venus_fly_trap, x, y);
assert(is_on_tile(g, a));
a.move(g);
assert(a.get_x() == x && a.get_y() == y);
}
// Agents have health
{
const agent a(agent_type::cow, 0, 0, 10);
assert(a.get_health() > 0.0);
}
// Agents have a direction, that can be read
{
const agent a(agent_type::cow); //Must be const
assert(a.get_direction() == 0.0);
}
// Agents have a direction, that can be set