-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.cpp
executable file
·117 lines (106 loc) · 2.63 KB
/
command.cpp
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
#include "command.hpp"
#include "packet.hpp"
#include "protocol.hpp"
Command::Command(CommandType type, const std::vector<unitID> &units, int x, int y)
: type(type), units(units), x(x), y(y), format(targetsTile)
{
}
Command::Command(CommandType type, const std::vector<unitID> &units, unitID unit)
: type(type), units(units), x(unit), y(0), format(targetsUnit)
{
}
Command::Command(CommandType type, int scrapyardId, std::string unitType)
: type(type), scrapyard(scrapyardId), unitType(unitType), format(targetsUnitType)
{
}
Command::Command(CommandType type, int scrapyardId, int index)
: type(type), scrapyard(scrapyardId), x(index), format(targetsQueue)
{
}
Command::Command(CommandType type, int scrapyardId, int x, int y)
: type(type), scrapyard(scrapyardId), x(x), y(y), format(targetsRally)
{
}
void Command::writeToPacket(Packet *packet) const
{
packet->putInt(msg_command);
packet->putChar(format);
packet->putInt(type);
if(format != targetsUnitType)
{
packet->putInt((int)units.size());
for (std::vector<unitID>::const_iterator it = units.begin();
it != units.end();
it++)
{
packet->putInt(*it);
}
}
switch(format)
{
case targetsTile:
packet->putInt(x);
packet->putInt(y);
break;
case targetsUnit:
packet->putInt(x);
break;
case targetsUnitType:
packet->putInt(scrapyard);
packet->putString(unitType);
break;
case targetsQueue:
packet->putInt(scrapyard);
packet->putInt(x);
break;
case targetsRally:
packet->putInt(scrapyard);
packet->putInt(x);
packet->putInt(y);
break;
}
}
void Command::readFromPacket(Packet *packet)
{
// Don't read in the msg_command flag; that gets checked outside.
format = static_cast<CommandFormat>(packet->getChar());
type = static_cast<CommandType>(packet->getInt());
if(format != targetsUnitType)
{
int numUnits = packet->getInt();
units.clear();
for (int i = 0; i < numUnits; i++)
units.push_back(packet->getInt());
}
switch(format)
{
case targetsTile:
scrapyard = 0;
x = packet->getInt();
y = packet->getInt();
unitType = "";
break;
case targetsUnit:
scrapyard = 0;
x = packet->getInt();
y = 0; unitType = "";
break;
case targetsUnitType:
scrapyard = packet->getInt();
x = y = 0;
unitType = packet->getString();
break;
case targetsQueue:
scrapyard = packet->getInt();
x = packet->getInt();
y = 0;
unitType = "";
break;
case targetsRally:
scrapyard = packet->getInt();
x = packet->getInt();
y = packet->getInt();
unitType = "";
break;
}
}