-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMoveAction.cpp
More file actions
66 lines (53 loc) · 1.44 KB
/
MoveAction.cpp
File metadata and controls
66 lines (53 loc) · 1.44 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
#include "MoveAction.h"
#include "Unit.h"
MoveAction::MoveAction(json msg) : Action(ActionType::MOVE)
{
decodeJSON(msg);
}
void MoveAction::decodeJSON(json msg)
{
if (!msg.contains("unit_id") || !msg.contains("x") || !msg.contains("y"))
{
is_valid_ = false;
return;
}
unit_id_ = msg["unit_id"];
target_ = Position(msg["x"], msg["y"]);
if (!target_.isValid(Config::game().gridSize))
{
is_valid_ = false;
return;
}
}
json MoveAction::encodeJSON()
{
json js;
js["type"] = "move";
js["unit_id"] = unit_id_;
js["x"] = target_.x;
js["y"] = target_.y;
return js;
}
std::string MoveAction::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 (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_);
if (obj)
return "invalid target position. should be empty";
if (target_.distance(Board::instance().getObjectPositionById(unit->getId())) > 1)
return "invalid move";
Board::instance().moveObjectById(unit->getId(), target_);
unit->resetActionCooldown();
Stats::instance().inc(stat_keys::tiles_traveled);
Stats::instance().inc(stat_keys::actions_executed);
return "";
}