aboutsummaryrefslogtreecommitdiff
path: root/game/gst.cpp
blob: f6d5aa18746fdf300eb267e9537f1d21e3182680 (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
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
#include "gst.h"

#include <iostream>

EntityInfo* Gst::get_info (std::string name) {
    for (EntityInfo &info : infos) {
        if (name == info.name) return &info;
    }
}

EntityInfo* Gst::get_info (int id) {
    for (EntityInfo &info : infos) {
        if (id == info.id) return &info;
    }
}

bool Gst::info_has_ability (EntityInfo* info, std::string name) {
    for (int ab : info->abilities) {
        if (abilities[ab].name == name) return true;
    }
    return false;
}

Entity& Gst::get_at (int x, int y) {
    for (Entity &e : entities) {
        if (e.x ==x && e.y == y) return e;
    }
}

float Gst::get_type_bonus (Entity &atk, Entity &def) {
    float b = 1;
    switch(atk.info->ent_class) {
        case EntityInfo::Class::inf: 
        if (def.info->ent_class == EntityInfo::Class::bld) b += 1.0/3;
        if (def.info->ent_class == EntityInfo::Class::sie) b += 1.0/3;
        break;
        
        case EntityInfo::Class::cav: 
        if (def.info->ent_class == EntityInfo::Class::bld) b += -0.5;
        if (def.info->ent_class == EntityInfo::Class::inf) b += 1.0/3;
        if (def.info->ent_class == EntityInfo::Class::ran) b += 1.0/3;
        break;
        
        case EntityInfo::Class::ran: 
        if (def.info->ent_class == EntityInfo::Class::bld) b += -0.5;
        break;
        
        case EntityInfo::Class::sie: 
        if (def.info->ent_class == EntityInfo::Class::bld) b += +0.5;
        break;
    }
    return b;
}

std::vector<Bonus> Gst::get_bonuses (Entity &atk, Entity &def) {
    std::vector<Bonus> bs;
    bs.emplace_back(
        tiles[ground.tiles[ground.at(atk.x, atk.y)]].attack_bonus, 
        Bonus::Id::ground, true);
    bs.emplace_back(
        tiles[ground.tiles[ground.at(def.x, def.y)]].defence_bonus, 
        Bonus::Id::ground, false);
        
    bs.emplace_back(get_type_bonus(atk, def), Bonus::Id::type, true);
    bs.emplace_back(get_type_bonus(def, atk), Bonus::Id::type, false);
    
    if (info_has_ability(atk.info, "Causes Fear")) 
        bs.emplace_back(-1.0f/3, Bonus::Id::ability, false);
    if (info_has_ability(def.info, "Causes Fear")) 
        bs.emplace_back(-1.0f/3, Bonus::Id::ability, true);
    
    if (info_has_ability(atk.info, "Anti-Cavalry")) 
        bs.emplace_back(4.0f/3, Bonus::Id::ability, true);
    if (info_has_ability(def.info, "Anti-Cavalry")) 
        bs.emplace_back(4.0f/3, Bonus::Id::ability, false);
    
    if (info_has_ability(atk.info, "Desert Charge")
    && !info_has_ability(def.info, "Desert Charge")
    && tiles[ground.tiles[ground.at(def.x, def.y)]].name == "Desert") 
        bs.emplace_back(1.0f/3, Bonus::Id::ability, true);
        
    if (info_has_ability(atk.info, "Plains Charge")
    && !info_has_ability(def.info, "Plains Charge")
    && tiles[ground.tiles[ground.at(def.x, def.y)]].name == "Plains") 
        bs.emplace_back(1.0f/3, Bonus::Id::ability, true);
        
    if (info_has_ability(atk.info, "Woodsman")
    && !info_has_ability(def.info, "Woodsman")
    && tiles[ground.tiles[ground.at(def.x, def.y)]].name == "Forest") 
        bs.emplace_back(1.0f/3, Bonus::Id::ability, true);
        
    if (info_has_ability(atk.info, "Volley") && atk.hp >= 50) 
        bs.emplace_back(1.0f/3, Bonus::Id::ability, true);
    
    if (info_has_ability(atk.info, "Frenzy")) 
        bs.emplace_back(1/atk.hp, Bonus::Id::ability, true);
    
    
    return bs;
}

float Gst::get_damage (Entity &atk, Entity &def) {
    float atkmul = 1;
    float defmul = 1;
    
    auto bonuses = get_bonuses(atk, def);
    for (auto bonus : bonuses) {
        if (bonus.atk) { atkmul += bonus.amt; }
        else { defmul += bonus.amt; }
    }
    
    float dam = (atk.info->attack * atk.hp * atkmul) 
        / (2.0f*def.info->defence * defmul);
    
    return dam;
}

bool Gst::get_first_strike (Entity &atk, Entity &def) {
    bool fs { false };
    fs = info_has_ability(atk.info, "First Strike");
    return fs;
}

float clamp (float hp) { if (hp > 100) hp = 100; return hp; }

void Gst::battle (Entity &atk, Entity &def) {
    std::cout << "! attack " << atk.info->name << "(hp:" << atk.hp << "), "
    << def.info->name << "(hp:" << def.hp << ") \n";
    
    bool first_strike_atk = info_has_ability(atk.info, "First Strike");
    bool first_strike_def = info_has_ability(def.info, "First Strike");
    bool skirmish_atk = info_has_ability(atk.info, "Skirmish");
    bool skirmish_def = info_has_ability(def.info, "Skirmish");
    bool anticav_atk = info_has_ability(atk.info, "Anti-Cavalry");
    bool anticav_def = info_has_ability(def.info, "Anti-Cavalry");
    first_strike_atk = first_strike_atk 
        || (skirmish_atk && def.info->range == 1);
    first_strike_def = first_strike_def 
        || (skirmish_def && atk.info->range == 1);
    first_strike_atk = first_strike_atk 
        || (anticav_atk && def.info->ent_class == EntityInfo::Class::cav);
    first_strike_def = first_strike_def 
        || (anticav_def && atk.info->ent_class == EntityInfo::Class::cav);
        
    bool swap = false;
    if (first_strike_def && !first_strike_atk) swap = true;
    if (swap) {
        atk.hp = clamp(atk.hp - get_damage(def, atk));
        if (!info_has_ability(atk.info, "No Counter")) 
            if (atk.hp > 0) def.hp -= get_damage(atk, def);
    } else {
        def.hp = clamp(def.hp - get_damage(atk, def));
        if (!info_has_ability(def.info, "No Counter")) 
            if (def.hp > 0) atk.hp -= get_damage(def, atk);
    }
    
    if (info_has_ability(atk.info, "Rapid Fire")) 
        if (def.hp > 0) 
            def.hp = clamp(def.hp - get_damage(atk, def));
    
    if (info_has_ability(def.info, "Rapid Fire"))
        if (atk.hp > 0) 
            atk.hp = clamp(atk.hp - get_damage(def, atk));
    
    if (atk.hp > 0 && info_has_ability(atk.info, "Zeal")) 
        atk.hp = clamp(atk.hp + 20);
    if (def.hp > 0 && info_has_ability(def.info, "Zeal")) 
        def.hp = clamp(def.hp + 20);
    
    std::cout << "! result " << atk.info->name << "(hp:" << atk.hp << "), "
    << def.info->name << "(hp:" << def.hp << ") \n";
    clear_dead();
}

void Gst::clear_dead() {
    auto i = std::begin(entities);
    while (i != std::end(entities)) {
        if (i->hp <= 0) {
            entities.erase(i);
        }
        else i++;
    }
}

std::vector<int> Gst::get_possible_builds (Entity &ent) {
    std::vector<int> builds;
    for (int id : ent.info->build) {
        if (check_req_build(ent, get_info(id))) {
            builds.push_back(id);
        }
    }
    return builds;
}

bool Gst::check_req_build(Entity &ent, EntityInfo *info) {
    for (int id : info->adjacent) {
        bool adj = false;
        for (Entity &e : entities) {
            if (e.info->id == id) {
                int dist = abs(e.x-ent.x) + abs(e.y-ent.y);
                if (dist == 1) {
                    adj = true;
                }
            }
        }
        if (!adj) return false;
    }
    if (info->id == 100) {
        for (Resource &r : ground.resources) {
            if (r.pos == ground.at(ent.x, ent.y))  {
                return false;
            }
        }
        int mindist = 9999;
        for (Entity &e : entities) {
            if (e.info->id == 100) {
                int dist = abs(e.x-ent.x) + abs(ent.y-e.y);
                if (dist < mindist) {
                    mindist = dist;
                }
            }
        }
        std::cout << "  " << mindist;
        if (mindist < 5) {
            return false;
        }
        return true;
    }
    if (info->id == 101) {
        for (Resource &r : ground.resources) {
            if (r.kind == Resource::Type::food
                && r.pos == ground.at(ent.x, ent.y))  {
                return true;
            }
        }
        return false;
    }
    if (info->id == 102) {
        for (Resource &r : ground.resources) {
            if (r.kind == Resource::Type::gold
                && r.pos == ground.at(ent.x, ent.y))  {
                return true;
            }
        }
        return false;
    }
    return true;
}

bool Gst::check_obstructed (Entity &ent) {
    for (Entity &e : entities) {
        if (&ent != &e && e.x == ent.x && ent.y == e.y) return true;
    }
    return false;
}

void Gst::end_day () {
    turn++;
    if (turn >= players.size()) {
        turn = 0;
        day++;
    }
}