summaryrefslogtreecommitdiff
path: root/src/Character.cpp
blob: 29f9d5c9f483590565901cb30862550000e80b98 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "Character.hpp"

Character::Character(char nSymbol, int nColor, Location L)
{
    symbol = nSymbol; 
    color = nColor;
    l = L;
    d = Location(100,100);
}

void Character::draw(WINDOW * w)
{
    wattron(w,COLOR_PAIR(color));
    mvwaddch(w,l.x,l.y,symbol);
}

void Character::action(vector <Character> men, vector <Location> impassable)
{
        if(order == "wander")
            wander(men, impassable);
        else if(order == "move")
            heuristic(men,impassable);
}

bool Character::check(Location L, vector <Character> men, vector <Location> impassable)
{
    for(int i = 0; i < men.size(); i++) 
        if(L.x == men[i].getLocation().x && L.y == men[i].getLocation().y)
            return false;
    
    for(int i = 0; i < impassable.size(); i++)
        if(L.x == impassable[i].x && L.y == impassable[i].y)
            return false;
    
    return true;
}

void Character::wander(vector <Character> men, vector <Location> impassable)
{
    vector <Location> possible = getLocal(l,men,impassable);
    int r = rand()%2;
    
    if(r && possible.size())
    { 
        int i = rand() % possible.size();
        move(possible[i]);
    }
}

void Character::heuristic(vector <Character> men, vector <Location> impassable)
{
    vector <Location> open = getLocal(l,men,impassable);  
    
    float v,u;
    Location L;

    if(open.size())
    {
        v = sqrt(pow(open[0].x - d.x,2) + pow(open[0].y - d.y,2));
        L = open[0];
    }
    for(int i = 0; i < open.size(); i++)
    {
        u = sqrt(pow(open[i].x - d.x,2) + pow(open[i].y - d.y,2));
        if (u<v)
        {
            L = open[i];
            v = u;
        }
    }
    move(L);
    if(l.x==d.x && l.y==d.y)
        order="wait";
}

vector <Location> Character::getLocal(Location L, vector <Character> men, vector <Location> impassable)
{
    vector <Location> x;
    Location Lo;

    Lo=Location(L.x+1,L.y);
    if(check(Lo,men,impassable))
        x.push_back(Lo);
    
    Lo=Location(L.x-1,L.y);
    if(check(Lo,men,impassable))
        x.push_back(Lo);

    Lo=Location(L.x,L.y+1);
    if(check(Lo,men,impassable))
        x.push_back(Lo);

    Lo=Location(L.x,L.y-1);
    if(check(Lo,men,impassable))
        x.push_back(Lo);

    Lo=Location(L.x+1,L.y+1);
    if(check(Lo,men,impassable))
        x.push_back(Lo);

    Lo=Location(L.x-1,L.y-1);
    if(check(Lo,men,impassable))
        x.push_back(Lo);

    Lo=Location(L.x+1,L.y-1);
    if(check(Lo,men,impassable))
        x.push_back(Lo);

    Lo=Location(L.x-1,L.y+1);
    if(check(Lo,men,impassable))
        x.push_back(Lo);
    
    return x;
}