-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathSayMsgTypes.h
481 lines (359 loc) · 11.8 KB
/
SayMsgTypes.h
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
Copyright (c) 2004, Gregory Kuhlmann, Peter Stone
University of Texas at Austin
All right reserved
Based On:
Coachable Agent communication encoding and decoding library
Copyright (C) 2004, John Davin, Carnegie Mellon University
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* SayMsgTypes.h - Defines message types as described by the coachable agents
* communication standard.
*
*
* Creation Date: 11/18/03
* Author: John Davin
*
* USAGE:
* The user of the say message library must subclass the SayMsgTypes classes
* to define the virtual function process().
*
*
*/
#ifndef SAY_MSG_TYPES_H
#define SAY_MSG_TYPES_H
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
#include "SayMsgIDs.h"
#include "SayMsgDataTypes.h"
/*
* Abstract base class for Say message units (eg, OurPos, WantPass, etc)
*/
class SayMsgTypes {
protected:
int SenderPlayerNum;
// holds the playernum of the sender. only needs to be set when decoding msg's
public:
virtual ~SayMsgTypes(){}
/**** Virtual functions defined in the SayMsgTypes subclasses *****
****** (OurPos, WantPass, etc) *****/
/*
* Return number of characters that this msg unit requires.
* Used by SayMsgEncoder::add to determine if we have room for this msg unit in current msg.
* Also used by SayMsgDecoder to determine how many characters to read for each msg type.
*/
virtual unsigned int getCost() = 0;
/*
* Return name of this msg unit.
*/
virtual string getName() = 0;
/*
* Return encoded string representation of the msg type.
*/
virtual string getEncodedStr() = 0;
/*
* Given an encoded string of this msg type's arguments, parse the arguments
* to initialize our private variables
* Return true on success, or false if string could not be parsed (eg, not long enough)
* This should *only* be called by SayMsgDecoder
*/
virtual bool parseStringArgs(string str) = 0;
/**** Virtual function defined by library user in SayMsgUnit subclasses (eg, MyOurPass) ***/
/*
* The user should redefine this function when they define subclasses
* of the SayMsgUnit classes. If they choose not to
*/
virtual void process() {};
/**** Non-virtual functions (defined for all subclasses): *****/
void setPlayernum(int pnum){ SenderPlayerNum = pnum; }
int getPlayernum() { return SenderPlayerNum; }
};
/****************************
* Message type subclasses:
****************************/
class OurPos : virtual public SayMsgTypes {
protected:
float X, Y;
public:
~OurPos(){}
OurPos() : X(0.),Y(0.)
{}
OurPos(float x, float y) : X(x), Y(y)
{}
unsigned int getCost() { return 3; }
string getName(){ return "OurPos"; }
string getEncodedStr(){
string encoding("");
encoding += DataTypes::MsgTypeID::getEncoding(OUR_POS_ID);
encoding += DataTypes::X::getEncoding(X);
encoding += DataTypes::Y::getEncoding(Y);
// cout << "[OurPos]: encoded as: "<<encoding<<endl;
return encoding;
}
/*
* Given a string representing this msg type's arguments, parse the arguments
* to initialize our private variables
* Return true on success, or false if string could not be parsed (eg, not long enough)
*/
bool parseStringArgs(string str){
if(str.size() != getCost()) // not enough chars
return false;
X = DataTypes::X::getDecoding(str[1]);
Y = DataTypes::Y::getDecoding(str[2]);
return true;
}
};
class BallInfo : virtual public SayMsgTypes {
protected:
float X, Y, dX, dY;
unsigned short cycleno;
public:
~BallInfo(){}
BallInfo() : X(0.),Y(0.),dX(0.),dY(0.),cycleno(0)
{}
BallInfo(float x, float y, float dx, float dy,
unsigned short cycle) : X(x), Y(y), dX(dx), dY(dy), cycleno(cycle)
{}
unsigned int getCost() { return 6; }
string getName(){ return "BallInfo"; }
string getEncodedStr(){
string encoding("");
encoding += DataTypes::MsgTypeID::getEncoding(BALL_INFO_ID);
encoding += DataTypes::X::getEncoding(X);
encoding += DataTypes::Y::getEncoding(Y);
encoding += DataTypes::dX::getEncoding(dX);
encoding += DataTypes::dY::getEncoding(dY);
encoding += DataTypes::Cycleno::getEncoding(cycleno);
return encoding;
}
/*
* Given a string representing this msg type's arguments, parse the arguments
* to initialize our private variables
* Return true on success, or false if string could not be parsed (eg, not long enough)
*/
bool parseStringArgs(string str){
if(str.size() != getCost()) // not enough chars
return false;
X = DataTypes::X::getDecoding(str[1]);
Y = DataTypes::Y::getDecoding(str[2]);
dX = DataTypes::dX::getDecoding(str[3]);
dY = DataTypes::dY::getDecoding(str[4]);
cycleno = DataTypes::Cycleno::getDecoding(str[5]);
return true;
}
};
class WeHaveBall : virtual public SayMsgTypes {
protected:
unsigned short playernum;
public:
~WeHaveBall(){}
WeHaveBall() : playernum(0)
{}
WeHaveBall(unsigned short pnum) : playernum(pnum)
{}
unsigned int getCost() { return 2; }
string getName(){ return "WeHaveBall"; }
string getEncodedStr(){
string encoding("");
encoding += DataTypes::MsgTypeID::getEncoding(WE_HAVE_BALL_ID);
encoding += DataTypes::Playernum::getEncoding(playernum);
return encoding;
}
bool parseStringArgs(string str){
if(str.size() != getCost())
return false;
playernum = DataTypes::Playernum::getDecoding(str[1]);
return true;
}
};
class OppHasBall : virtual public SayMsgTypes {
protected:
unsigned short playernum;
public:
~OppHasBall(){}
OppHasBall() : playernum(0)
{}
OppHasBall(unsigned short pnum) : playernum(pnum)
{}
unsigned int getCost() { return 2; }
string getName(){ return "OppHasBall"; }
string getEncodedStr(){
string encoding("");
encoding += DataTypes::MsgTypeID::getEncoding(OPP_HAS_BALL_ID);
encoding += DataTypes::Playernum::getEncoding(playernum);
return encoding;
}
bool parseStringArgs(string str){
if(str.size() != getCost())
return false;
playernum = DataTypes::Playernum::getDecoding(str[1]);
return true;
}
};
class PassToPlayer : virtual public SayMsgTypes {
protected:
unsigned short playernum; // the player we're passing to
public:
~PassToPlayer(){}
PassToPlayer() : playernum(0)
{}
PassToPlayer(unsigned short pnum) : playernum(pnum)
{}
unsigned int getCost() { return 2; }
string getName(){ return "PassToPlayer"; }
string getEncodedStr(){
string encoding("");
encoding += DataTypes::MsgTypeID::getEncoding(PASS_TO_PLAYER_ID);
encoding += DataTypes::Playernum::getEncoding(playernum);
return encoding;
}
bool parseStringArgs(string str){
if(str.size() != getCost())
return false;
playernum = DataTypes::Playernum::getDecoding(str[1]);
return true;
}
};
class PassToCoord : virtual public SayMsgTypes {
protected:
float X, Y;
public:
~PassToCoord(){}
PassToCoord() : X(0.),Y(0.)
{}
PassToCoord(float x, float y) : X(x), Y(y)
{}
unsigned int getCost() { return 3; }
string getName(){ return "PassToCoord"; }
string getEncodedStr(){
string encoding("");
encoding += DataTypes::MsgTypeID::getEncoding(PASS_TO_COORD_ID);
encoding += DataTypes::X::getEncoding(X);
encoding += DataTypes::Y::getEncoding(Y);
return encoding;
}
/*
* Given a string representing this msg type's arguments, parse the arguments
* to initialize our private variables
* Return true on success, or false if string could not be parsed (eg, not long enough)
*/
bool parseStringArgs(string str){
if(str.size() != getCost()) // not enough chars
return false;
X = DataTypes::X::getDecoding(str[1]);
Y = DataTypes::Y::getDecoding(str[2]);
return true;
}
};
class WantPass : virtual public SayMsgTypes {
protected:
// none
public:
~WantPass(){}
WantPass() {}
unsigned int getCost() { return 1; }
string getName(){ return "WantPass"; }
string getEncodedStr(){
string encoding("");
encoding += DataTypes::MsgTypeID::getEncoding(WANT_PASS_ID);
return encoding;
}
bool parseStringArgs(string str){
if(str.size() != getCost()) // str should always be 0 chars
return false;
/* we don't have any arguments, so do nothing */
return true;
}
};
class OppPos : virtual public SayMsgTypes {
protected:
float X, Y;
unsigned short playernum;
unsigned short cycleno;
public:
~OppPos(){}
OppPos()
: X(0.),Y(0.),playernum(0),cycleno(0) { }
OppPos(unsigned short pnum, float x, float y, unsigned short cyclen)
: X(x),Y(y),playernum(pnum),cycleno(cyclen)
{}
unsigned int getCost() { return 4; }
string getName(){ return "OppPos"; }
string getEncodedStr(){
if(playernum < 1 || playernum > 11)
cerr <<"TeammatePos: ERROR: playernum "<<playernum<<" invalid"<<endl;
string encoding("");
encoding += DataTypes::MsgTypeID::getEncoding(OPP_POS_ID+playernum-1);
encoding += DataTypes::X::getEncoding(X);
encoding += DataTypes::Y::getEncoding(Y);
encoding += DataTypes::Cycleno::getEncoding(cycleno);
return encoding;
}
/*
* Given a string representing this msg type's arguments, parse the arguments
* to initialize our private variables
* Return true on success, or false if string could not be parsed (eg, not long enough)
*/
bool parseStringArgs(string str){
if(str.size() != getCost()) // not enough chars
return false;
playernum = DataTypes::MsgTypeID::getDecoding(str[0])-OPP_POS_ID+1;
X = DataTypes::X::getDecoding(str[1]);
Y = DataTypes::Y::getDecoding(str[2]);
cycleno = DataTypes::Cycleno::getDecoding(str[3]);
return true;
}
};
class TeammatePos : virtual public SayMsgTypes {
protected:
float X, Y;
unsigned short playernum;
unsigned short cycleno;
public:
~TeammatePos(){}
TeammatePos()
: X(0.),Y(0.),playernum(0),cycleno(0) { }
TeammatePos(unsigned short pnum, float x, float y, unsigned short cyclen)
: X(x),Y(y),playernum(pnum),cycleno(cyclen)
{}
unsigned int getCost() { return 4; }
string getName(){ return "TeammatePos"; }
string getEncodedStr(){
if(playernum < 1 || playernum > 11)
cerr <<"TeammatePos: ERROR: playernum "<<playernum<<" invalid"<<endl;
string encoding("");
encoding += DataTypes::MsgTypeID::getEncoding(TEAMMATE_POS_ID+playernum-1);
encoding += DataTypes::X::getEncoding(X);
encoding += DataTypes::Y::getEncoding(Y);
encoding += DataTypes::Cycleno::getEncoding(cycleno);
return encoding;
}
/*
* Given a string representing this msg type's arguments, parse the arguments
* to initialize our private variables
* Return true on success, or false if string could not be parsed (eg, not long enough)
*/
bool parseStringArgs(string str){
if(str.size() != getCost()) // not enough chars
return false;
playernum = DataTypes::MsgTypeID::getDecoding(str[0])-TEAMMATE_POS_ID+1;
X = DataTypes::X::getDecoding(str[1]);
Y = DataTypes::Y::getDecoding(str[2]);
cycleno = DataTypes::Cycleno::getDecoding(str[3]);
return true;
}
};
#endif