summaryrefslogtreecommitdiff
path: root/src/list.cpp
blob: d93b38e60981ff418e8d21ce87b67836c43066c4 (plain)
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
#include "list.hpp"

List::List()
{
        int i;
        DNA d = DNA(HERBAVORE_TYPE);
        Rectangle tmp;
        for(i=0;i<HERBAVORES;i++){
                Organism X(tmp,d);
                organisms.push_back(X);
        }
        
        d = DNA(CARNIVORE_TYPE);
        for(i=0;i<CARNIVORES;i++){
                Organism X(tmp, d);
                organisms.push_back(X);
        }
        
        d = DNA(PLANT_TYPE);
        for(i=0;i<PLANTS;i++){
                Organism X(tmp, d);
                organisms.push_back(X);
        }

        Rectangle R1    = Rectangle(0,0,BOUNDS*2,BOUNDS*2);
        tree            = Quadtree(0, R1);
}

void List::Remove()
{
        for(std::list<Organism>::iterator it = organisms.begin(); it!= organisms.end(); it++)    
                if(it->getHealth()<=0){
                        if(it->getType() == PLANT_TYPE or it->getType() == CORPSE_TYPE)
                                organisms.erase(it--);
                        else{
                                DNA d = DNA(CORPSE_TYPE);
                                Organism X(it->getRectangle(), d);
                                organisms.push_back(X);
                                organisms.erase(it--);
                        }
                }
}

void List::Behavior()
{
        for(std::list<Organism>::iterator it = organisms.begin(); it!= organisms.end(); it++){
                if(it->getType() == PLANT_TYPE or it->getType() == CORPSE_TYPE)
                        it->grow(); 
                else{
                        std::vector<Organism*> near = getNear(*it); 
                        it->giveNearMe(near); 
                        it->Behavior();

                        if(it->getPregnancyReady()){
                                Organism X(it->getRectangle(),it->getChildsDNA());
                                organisms.push_back(X);
                                it->hadPregnancy();
                        }
                }
        }
}

void List::Place()
{ 
        tree.clear();

        for(std::list<Organism>::iterator it = organisms.begin(); it!= organisms.end(); it++)
                tree.insert(&(*it));;
}

std::vector<Organism*> List::getNear(Organism c)
{
        std::vector<Organism*> near;
        near = tree.retrieve(near, c.getRectangle()); 

        for(std::vector<Organism*>::iterator it = near.begin(); it!= near.end(); it++)
                if(c.getBestSense() < Distance(c.getRectangle(),(*it)->getRectangle()))
                        near.erase(it--);

        return near;
}