summaryrefslogtreecommitdiff
path: root/src/list.cpp
blob: fab581334dcc81c25227b83b81670855df10fc68 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "list.hpp"

List::List()
{
    int i;
    Dna defaultDNA;
    //SDL_Rect rect = {0,0,defaultDNA.sizeMax/5,defaultDNA.sizeMax/5};
    Location tmp;
    tmp.x = tmp.y = 0;
    for(i=0;i<CREATURES;i++){
        Creature X(tmp,defaultDNA);
        C.push_back(X);
    }

    //rect = {0,0,RESOURCE_SIZE_START,RESOURCE_SIZE_START};
    for(i=0;i<RESOURCES;i++){
        Resource Y(tmp);
        R.push_back(Y);
    }

    R1 = Rectangle(0,0,60,60);
    tree = Quadtree(0,R1);
}

void List::Remove()
{
    for(std::list<Creature>::iterator it = C.begin(); it!=C.end(); it++)    
        if(it->getHealth()<=0){
            Location tmp = it->getLocation();
            Resource r = Resource(tmp);
            R.push_back(r);
            C.erase(it--);
        }
    
    for(std::list<Resource>::iterator it = R.begin(); it!=R.end(); it++)
        if(it->getAmount()<=0)
            R.erase(it--);
}

void List::Behavior()
{
    for(std::list<Creature>::iterator it = C.begin(); it!=C.end(); it++){
        std::list<Entity*> N = getNear(*it); 
        it->giveN(N); 
        it->Behavior();
        
        if(it->getPregnancyReady()){
            Dna D  = it->getChildDNA();
            Location tmp = it->getLocation();
            Creature X(tmp,D);
            C.push_back(X);
            it->hadPregnancy();
        }
    }
    
    for(std::list<Resource>::iterator it = R.begin(); it!=R.end(); it++)
        it->grow(); 
}

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

    Location tmp;
    tmp.x = tmp.y = 0;
    while(R.size() < MINIMUM_RESOURCES){
        Resource Y(tmp);
        R.push_back(Y);
    }

    for(std::list<Creature>::iterator it = C.begin(); it!=C.end(); it++){
        it->Place();
        tree.insert(&(*it));;
    }

    for(std::list<Resource>::iterator it = R.begin(); it!=R.end(); it++){
        it->Place();
        tree.insert(&(*it));;
    }
}

std::list<Entity*> List::getNear(Creature nC)
{
    std::list<Entity*> N;
    N.clear();
    N = tree.retrieve(N, nC.getGFXD()); 

    //std::vector<Entity*> x{std::begin(N),std::begin(N)};
    return N;
}

std::vector<GraphicsData> List::drawQuadTree(){
    return tree.Draw();
}