-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAttackAction.cpp
More file actions
126 lines (106 loc) Β· 3.33 KB
/
AttackAction.cpp
File metadata and controls
126 lines (106 loc) Β· 3.33 KB
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
#include "AttackAction.h"
#include "Unit.h"
AttackAction::AttackAction(json msg) : Action(ActionType::ATTACK)
{
decodeJSON(msg);
}
void AttackAction::decodeJSON(json msg)
{
if (!msg.contains("unit_id") || !msg.contains("x") || !msg.contains("y"))
{
is_valid_ = false;
return;
}
unit_id_ = msg["unit_id"];
target_pos_.x = msg["x"];
target_pos_.y = msg["y"];
if (!target_pos_.isValid(Config::game().gridSize))
{
is_valid_ = false;
return;
}
}
json AttackAction::encodeJSON()
{
json js;
js["type"] = "attack";
js["unit_id"] = unit_id_;
js["x"] = target_pos_.x;
js["y"] = target_pos_.y;
return js;
}
std::string AttackAction::execute(Core *core)
{
if (!is_valid_)
return "invalid input";
Object *unitObj = Board::instance().getObjectById(getUnitId());
if (!unitObj || unitObj->getType() != ObjectType::Unit)
return "invalid or non-existing unit";
Unit *unit = (Unit *)unitObj;
if (Board::instance().getObjectPositionById(unit->getId()).distance(target_pos_) > 1)
return "unit is too far away";
if (unit->getActionCooldown() > 0)
return "unit is on cooldown";
if (unit->getTeamId() != core->getTeamId())
return "unit does not belong to your team";
Object *obj = Board::instance().getObjectAtPos(target_pos_);
if (!obj)
return "no object at target position";
unit->resetActionCooldown();
// calculate attack damage depending on object type
unsigned int damage = 1;
if (obj->getType() == ObjectType::Unit)
{
damage = Config::game().units[unit->getUnitType()].damageUnit;
obj->setHP(obj->getHP() - damage);
Stats::instance().inc(stat_keys::damage_units, damage);
if (((Unit *)obj)->getTeamId() != unit->getTeamId())
Stats::instance().inc(stat_keys::damage_opponent, damage);
else
Stats::instance().inc(stat_keys::damage_self, damage);
}
else if (obj->getType() == ObjectType::Core)
{
damage = Config::game().units[unit->getUnitType()].damageCore;
obj->setHP(obj->getHP() - damage);
Stats::instance().inc(stat_keys::damage_cores, damage);
if (((Core *)obj)->getTeamId() != unit->getTeamId())
Stats::instance().inc(stat_keys::damage_opponent, damage);
else
Stats::instance().inc(stat_keys::damage_self, damage);
}
else if (obj->getType() == ObjectType::Deposit)
{
damage = Config::game().units[unit->getUnitType()].damageDeposit;
obj->setHP(obj->getHP() - damage);
if (obj->getHP() <= 0)
{
unsigned int gems = ((Deposit *)obj)->getBalance();
Board::instance().removeObjectById(obj->getId());
Board::instance().addObject<GemPile>(GemPile(gems), target_pos_);
}
Stats::instance().inc(stat_keys::damage_deposits, damage);
}
else if (obj->getType() == ObjectType::Wall)
{
damage = Config::game().units[unit->getUnitType()].damageWall;
obj->setHP(obj->getHP() - damage);
Stats::instance().inc(stat_keys::damage_walls, damage);
}
else if (obj->getType() == ObjectType::Bomb)
{
Bomb *bomb = (Bomb *)obj;
bomb->explode();
}
else if (obj->getType() == ObjectType::GemPile)
{
unsigned int gems = static_cast<GemPile *>(obj)->getBalance();
unit->setBalance(unit->getBalance() + gems);
Board::instance().removeObjectById(obj->getId());
Stats::instance().inc(stat_keys::gems_gained, gems);
Stats::instance().inc(stat_keys::gempiles_destroyed, gems);
}
Stats::instance().inc(stat_keys::actions_executed);
Stats::instance().inc(stat_keys::damage_total, damage);
return "";
}